Flutter Engine
The Flutter Engine
image_filter_layer.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/flow/layers/image_filter_layer.h"
6
7#include "flutter/display_list/utils/dl_comparable.h"
8#include "flutter/flow/layers/layer.h"
9#include "flutter/flow/raster_cache_util.h"
10
11namespace flutter {
12
13ImageFilterLayer::ImageFilterLayer(std::shared_ptr<const DlImageFilter> filter,
14 const SkPoint& offset)
16 RasterCacheUtil::kMinimumRendersBeforeCachingFilterLayer),
17 offset_(offset),
18 filter_(std::move(filter)),
19 transformed_filter_(nullptr) {}
20
21void ImageFilterLayer::Diff(DiffContext* context, const Layer* old_layer) {
22 DiffContext::AutoSubtreeRestore subtree(context);
23 auto* prev = static_cast<const ImageFilterLayer*>(old_layer);
24 if (!context->IsSubtreeDirty()) {
26 if (NotEquals(filter_, prev->filter_) || offset_ != prev->offset_) {
27 context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
28 }
29 }
30
31 context->PushTransform(SkMatrix::Translate(offset_.fX, offset_.fY));
32 if (context->has_raster_cache()) {
34 }
35
36 if (filter_) {
37 auto filter = filter_->makeWithLocalMatrix(context->GetTransform3x3());
38 if (filter) {
39 // This transform will be applied to every child rect in the subtree
40 context->PushFilterBoundsAdjustment([filter](SkRect rect) {
41 SkIRect filter_out_bounds;
42 filter->map_device_bounds(rect.roundOut(), SkMatrix::I(),
43 filter_out_bounds);
44 return SkRect::Make(filter_out_bounds);
45 });
46 }
47 }
48 DiffChildren(context, prev);
49 context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
50}
51
53 auto mutator = context->state_stack.save();
54 mutator.translate(offset_);
55
58
59#if !SLIMPELLER
61 context->state_stack.transform_3x3());
62#endif // !SLIMPELLER
63
64 SkRect child_bounds = SkRect::MakeEmpty();
65
66 PrerollChildren(context, &child_bounds);
67
68 if (!filter_) {
69 child_bounds.offset(offset_);
70 set_paint_bounds(child_bounds);
71 return;
72 }
73
74 // Our saveLayer would apply any outstanding opacity or any outstanding
75 // color filter after it applies our image filter. So we can apply either
76 // of those attributes with our saveLayer.
77 context->renderable_state_flags =
80
81 const SkIRect filter_in_bounds = child_bounds.roundOut();
82 SkIRect filter_out_bounds;
83 filter_->map_device_bounds(filter_in_bounds, SkMatrix::I(),
84 filter_out_bounds);
85 child_bounds.set(filter_out_bounds);
86 child_bounds.offset(offset_);
87
88 set_paint_bounds(child_bounds);
89
90#if !SLIMPELLER
91 // CacheChildren only when the transformed_filter_ doesn't equal null.
92 // So in here we reset the LayerRasterCacheItem cache state.
93 layer_raster_cache_item_->MarkNotCacheChildren();
94#endif // !SLIMPELLER
95
96 transformed_filter_ =
97 filter_->makeWithLocalMatrix(context->state_stack.transform_3x3());
98
99#if !SLIMPELLER
100 if (transformed_filter_) {
101 layer_raster_cache_item_->MarkCacheChildren();
102 }
103#endif // !SLIMPELLER
104}
105
107 FML_DCHECK(needs_painting(context));
108
109 auto mutator = context.state_stack.save();
110
111#if !SLIMPELLER
112 if (context.raster_cache) {
113 // Try drawing the layer cache item from the cache before applying the
114 // image filter if it was cached with the filter applied.
115 if (!layer_raster_cache_item_->IsCacheChildren()) {
117 if (layer_raster_cache_item_->Draw(context,
118 context.state_stack.fill(paint))) {
119 return;
120 }
121 }
122 }
123#endif // !SLIMPELLER
124
125 // Only apply the offset if not being raster-cached to avoid the offset being
126 // applied twice.
127 mutator.translate(offset_);
128
129#if !SLIMPELLER
130 if (context.raster_cache) {
131 mutator.integralTransform();
132 }
133
134 if (context.raster_cache && layer_raster_cache_item_->IsCacheChildren()) {
135 // If we render the children from cache then we need the special
136 // transformed version of the filter so we must process it into the
137 // cache paint object manually.
138 FML_DCHECK(transformed_filter_ != nullptr);
140 context.state_stack.fill(paint);
141 paint.setImageFilter(transformed_filter_);
142 if (layer_raster_cache_item_->Draw(context, &paint)) {
143 return;
144 }
145 }
146#endif // !SLIMPELLER
147
148 // Now apply the image filter and then try rendering the children.
149 mutator.applyImageFilter(child_paint_bounds(), filter_);
150
151 PaintChildren(context);
152}
153
154} // namespace flutter
static float prev(float f)
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
std::unique_ptr< LayerRasterCacheItem > layer_raster_cache_item_
void PrerollChildren(PrerollContext *context, SkRect *child_paint_bounds)
void PaintChildren(PaintContext &context) const override
virtual void DiffChildren(DiffContext *context, const ContainerLayer *old_layer)
const SkRect & child_paint_bounds() const
SkMatrix GetTransform3x3() const
void PushTransform(const SkMatrix &transform)
Definition: diff_context.cc:49
void WillPaintWithIntegralTransform()
Definition: diff_context.h:89
void SetLayerPaintRegion(const Layer *layer, const PaintRegion &region)
void MarkSubtreeDirty(const PaintRegion &previous_paint_region=PaintRegion())
PaintRegion CurrentSubtreeRegion() const
void PushFilterBoundsAdjustment(const FilterBoundsAdjustment &filter)
Definition: diff_context.cc:76
bool has_raster_cache() const
Definition: diff_context.h:167
PaintRegion GetOldLayerPaintRegion(const Layer *layer) const
bool IsSubtreeDirty() const
Definition: diff_context.h:108
void Diff(DiffContext *context, const Layer *old_layer) override
void Paint(PaintContext &context) const override
void Preroll(PrerollContext *context) override
ImageFilterLayer(std::shared_ptr< const DlImageFilter > filter, const SkPoint &offset=SkPoint::Make(0, 0))
void translate(SkScalar tx, SkScalar ty)
SkMatrix transform_3x3() const
void fill(MutatorsStack *mutators)
static constexpr int kCallerCanApplyColorFilter
static constexpr int kCallerCanApplyOpacity
static AutoPrerollSaveLayerState Create(PrerollContext *preroll_context, bool save_layer_is_active=true, bool layer_itself_performs_readback=false)
Definition: layer.cc:40
bool needs_painting(PaintContext &context) const
Definition: layer.h:232
void set_paint_bounds(const SkRect &paint_bounds)
Definition: layer.h:223
const Paint & paint
Definition: color_source.cc:38
#define FML_DCHECK(condition)
Definition: logging.h:103
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
bool NotEquals(const T *a, const T *b)
Definition: dl_comparable.h:70
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition: switches.h:191
Definition: ref_ptr.h:256
SeparatedVector2 offset
Definition: SkRect.h:32
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static constexpr SkRect MakeEmpty()
Definition: SkRect.h:595
void roundOut(SkIRect *dst) const
Definition: SkRect.h:1241
void offset(float dx, float dy)
Definition: SkRect.h:1016
void set(const SkIRect &src)
Definition: SkRect.h:849
LayerStateStack & state_stack
Definition: layer.h:101
LayerStateStack & state_stack
Definition: layer.h:59
int renderable_state_flags
Definition: layer.h:83