Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
60 context->state_stack.transform_3x3());
61
62 SkRect child_bounds = SkRect::MakeEmpty();
63
64 PrerollChildren(context, &child_bounds);
65
66 if (!filter_) {
67 child_bounds.offset(offset_);
68 set_paint_bounds(child_bounds);
69 return;
70 }
71
72 // Our saveLayer would apply any outstanding opacity or any outstanding
73 // color filter after it applies our image filter. So we can apply either
74 // of those attributes with our saveLayer.
75 context->renderable_state_flags =
78
79 const SkIRect filter_in_bounds = child_bounds.roundOut();
80 SkIRect filter_out_bounds;
81 filter_->map_device_bounds(filter_in_bounds, SkMatrix::I(),
82 filter_out_bounds);
83 child_bounds.set(filter_out_bounds);
84 child_bounds.offset(offset_);
85
86 set_paint_bounds(child_bounds);
87
88 // CacheChildren only when the transformed_filter_ doesn't equal null.
89 // So in here we reset the LayerRasterCacheItem cache state.
90 layer_raster_cache_item_->MarkNotCacheChildren();
91
92 transformed_filter_ =
93 filter_->makeWithLocalMatrix(context->state_stack.transform_3x3());
94 if (transformed_filter_) {
95 layer_raster_cache_item_->MarkCacheChildren();
96 }
97}
98
100 FML_DCHECK(needs_painting(context));
101
102 auto mutator = context.state_stack.save();
103
104 if (context.raster_cache) {
105 // Try drawing the layer cache item from the cache before applying the
106 // image filter if it was cached with the filter applied.
107 if (!layer_raster_cache_item_->IsCacheChildren()) {
109 if (layer_raster_cache_item_->Draw(context,
110 context.state_stack.fill(paint))) {
111 return;
112 }
113 }
114 }
115
116 // Only apply the offset if not being raster-cached to avoid the offset being
117 // applied twice.
118 mutator.translate(offset_);
119 if (context.raster_cache) {
120 mutator.integralTransform();
121 }
122
123 if (context.raster_cache && layer_raster_cache_item_->IsCacheChildren()) {
124 // If we render the children from cache then we need the special
125 // transformed version of the filter so we must process it into the
126 // cache paint object manually.
127 FML_DCHECK(transformed_filter_ != nullptr);
129 context.state_stack.fill(paint);
130 paint.setImageFilter(transformed_filter_);
131 if (layer_raster_cache_item_->Draw(context, &paint)) {
132 return;
133 }
134 }
135
136 // Now apply the image filter and then try rendering the children.
137 mutator.applyImageFilter(child_paint_bounds(), filter_);
138
139 PaintChildren(context);
140}
141
142} // namespace flutter
static float prev(float f)
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
static const SkMatrix & I()
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)
void WillPaintWithIntegralTransform()
void SetLayerPaintRegion(const Layer *layer, const PaintRegion &region)
void MarkSubtreeDirty(const PaintRegion &previous_paint_region=PaintRegion())
PaintRegion CurrentSubtreeRegion() const
void PushFilterBoundsAdjustment(const FilterBoundsAdjustment &filter)
bool has_raster_cache() const
PaintRegion GetOldLayerPaintRegion(const Layer *layer) const
bool IsSubtreeDirty() const
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)
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:231
void set_paint_bounds(const SkRect &paint_bounds)
Definition layer.h:222
const Paint & paint
#define FML_DCHECK(condition)
Definition logging.h:103
bool NotEquals(const T *a, const T *b)
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
Point offset
float fX
x-axis value
float fY
y-axis value
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:100
const RasterCache * raster_cache
Definition layer.h:114
LayerStateStack & state_stack
Definition layer.h:58