Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
layer.h
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#ifndef FLUTTER_FLOW_LAYERS_LAYER_H_
6#define FLUTTER_FLOW_LAYERS_LAYER_H_
7
8#include <algorithm>
9#include <memory>
10#include <unordered_set>
11#include <vector>
12
13#include "flutter/common/graphics/texture.h"
14#include "flutter/display_list/dl_canvas.h"
15#include "flutter/flow/diff_context.h"
16#include "flutter/flow/embedded_views.h"
17#include "flutter/flow/layer_snapshot_store.h"
18#include "flutter/flow/layers/layer_state_stack.h"
19#include "flutter/flow/raster_cache.h"
20#include "flutter/flow/stopwatch.h"
21#include "flutter/fml/build_config.h"
22#include "flutter/fml/compiler_specific.h"
23#include "flutter/fml/logging.h"
24#include "flutter/fml/macros.h"
25#include "flutter/fml/trace_event.h"
34
35class GrDirectContext;
36
37namespace flutter {
38
39namespace testing {
40class MockLayer;
41} // namespace testing
42
43class ContainerLayer;
44class DisplayListLayer;
45class PerformanceOverlayLayer;
46class TextureLayer;
47class RasterCacheItem;
48
49static constexpr SkRect kGiantRect = SkRect::MakeLTRB(-1E9F, -1E9F, 1E9F, 1E9F);
50
51// This should be an exact copy of the Clip enum in painting.dart.
53
61
62 // These allow us to paint in the end of subtree Preroll.
65 std::shared_ptr<TextureRegistry> texture_registry;
66
67 // These allow us to track properties like elevation, opacity, and the
68 // presence of a platform view during Preroll.
69 bool has_platform_view = false;
70 // These allow us to track properties like elevation, opacity, and the
71 // presence of a texture layer during Preroll.
72 bool has_texture_layer = false;
73
74 // The list of flags that describe which rendering state attributes
75 // (such as opacity, ColorFilter, ImageFilter) a given layer can
76 // render itself without requiring the parent to perform a protective
77 // saveLayer with those attributes.
78 // For containers, the flags will be set to the intersection (logical
79 // and) of all of the state bits that all of the children can render
80 // or to 0 if some of the children overlap and, as such, cannot apply
81 // those attributes individually and separately.
83
84 std::vector<RasterCacheItem*>* raster_cached_entries;
85};
86
88 // When splitting the scene into multiple canvases (e.g when embedding
89 // a platform view on iOS) during the paint traversal we apply any state
90 // changes which affect children (i.e. saveLayer attributes) to the
91 // state_stack and any local rendering state changes for leaf layers to
92 // the canvas or builder.
93 // When we switch a canvas or builder (when painting a PlatformViewLayer)
94 // the new canvas receives all of the stateful changes from the state_stack
95 // to put it into the exact same state that the outgoing canvas had at the
96 // time it was swapped out.
97 // The state stack lazily applies saveLayer calls to its current canvas,
98 // allowing leaf layers to report that they can handle rendering some of
99 // its state attributes themselves via the |applyState| method.
102
103 // Whether current canvas is an overlay canvas. Used to determine if the
104 // raster cache is painting to a surface that will be displayed above a
105 // platform view, in which case it will attempt to preserve the R-Tree.
107
113 std::shared_ptr<TextureRegistry> texture_registry;
115
116 // Snapshot store to collect leaf layer snapshots. The store is non-null
117 // only when leaf layer tracing is enabled.
120 bool impeller_enabled = false;
122};
123
124// Represents a single composited layer. Created on the UI thread but then
125// subsequently used on the Rasterizer thread.
126class Layer {
127 public:
128 // The state attribute flags that represent which attributes a
129 // layer can render if it plans to use a saveLayer call in its
130 // |Paint| method.
131 static constexpr int kSaveLayerRenderFlags =
135
136 // The state attribute flags that represent which attributes a
137 // layer can render if it will be rendering its content/children
138 // from a cached representation.
139 static constexpr int kRasterCacheRenderFlags =
141
142 Layer();
143 virtual ~Layer();
144
145 void AssignOldLayer(Layer* old_layer) {
146 original_layer_id_ = old_layer->original_layer_id_;
147 }
148
149 // Used to establish link between old layer and new layer that replaces it.
150 // If this method returns true, it is assumed that this layer replaces the old
151 // layer in tree and is able to diff with it.
152 virtual bool IsReplacing(DiffContext* context, const Layer* old_layer) const {
153 return original_layer_id_ == old_layer->original_layer_id_;
154 }
155
156 // Performs diff with given layer
157 virtual void Diff(DiffContext* context, const Layer* old_layer) {}
158
159 // Used when diffing retained layer; In case the layer is identical, it
160 // doesn't need to be diffed, but the paint region needs to be stored in diff
161 // context so that it can be used in next frame
162 virtual void PreservePaintRegion(DiffContext* context) {
163 // retained layer means same instance so 'this' is used to index into both
164 // current and old region
165 context->SetLayerPaintRegion(this, context->GetOldLayerPaintRegion(this));
166 }
167
168 virtual void Preroll(PrerollContext* context) = 0;
169
170 // Used during Preroll by layers that employ a saveLayer to manage the
171 // PrerollContext settings with values affected by the saveLayer mechanism.
172 // This object must be created before calling Preroll on the children to
173 // set up the state for the children and then restore the state upon
174 // destruction.
176 public:
177 [[nodiscard]] static AutoPrerollSaveLayerState Create(
178 PrerollContext* preroll_context,
179 bool save_layer_is_active = true,
180 bool layer_itself_performs_readback = false);
181
183
184 private:
186 bool save_layer_is_active,
187 bool layer_itself_performs_readback);
188
189 PrerollContext* preroll_context_;
190 bool save_layer_is_active_;
191 bool layer_itself_performs_readback_;
192
193 bool prev_surface_needs_readback_;
194 };
195
196 virtual void Paint(PaintContext& context) const = 0;
197
198 virtual void PaintChildren(PaintContext& context) const { FML_DCHECK(false); }
199
200 bool subtree_has_platform_view() const { return subtree_has_platform_view_; }
202 subtree_has_platform_view_ = value;
203 }
204
205 // Returns the paint bounds in the layer's local coordinate system
206 // as determined during Preroll(). The bounds should include any
207 // transform, clip or distortions performed by the layer itself,
208 // but not any similar modifications inherited from its ancestors.
209 const SkRect& paint_bounds() const { return paint_bounds_; }
210
211 // This must be set by the time Preroll() returns otherwise the layer will
212 // be assumed to have empty paint bounds (paints no content).
213 // The paint bounds should be independent of the context outside of this
214 // layer as the layer may be painted under different conditions than
215 // the Preroll context. The most common example of this condition is
216 // that we might Preroll the layer with a cull_rect established by a
217 // clip layer above it but then we might be asked to paint anyway if
218 // another layer above us needs to cache its children. During the
219 // paint operation that arises due to the caching, the clip will
220 // be the bounds of the layer needing caching, not the cull_rect
221 // that we saw in the overall Preroll operation.
223 paint_bounds_ = paint_bounds;
224 }
225
226 // Determines if the layer has any content.
227 bool is_empty() const { return paint_bounds_.isEmpty(); }
228
229 // Determines if the Paint() method is necessary based on the properties
230 // of the indicated PaintContext object.
231 bool needs_painting(PaintContext& context) const {
232 if (subtree_has_platform_view_) {
233 // Workaround for the iOS embedder. The iOS embedder expects that
234 // if we preroll it, then we will later call its Paint() method.
235 // Now that we preroll all layers without any culling, we may
236 // call its Preroll() without calling its Paint(). For now, we
237 // will not perform paint culling on any subtree that has a
238 // platform view.
239 // See https://github.com/flutter/flutter/issues/81419
240 return true;
241 }
242 return !context.state_stack.painting_is_nop() &&
243 !context.state_stack.content_culled(paint_bounds_);
244 }
245
246 // Propagated unique_id of the first layer in "chain" of replacement layers
247 // that can be diffed.
248 uint64_t original_layer_id() const { return original_layer_id_; }
249
250 uint64_t unique_id() const { return unique_id_; }
251
254 }
255 virtual const ContainerLayer* as_container_layer() const { return nullptr; }
257 return nullptr;
258 }
259 virtual const TextureLayer* as_texture_layer() const { return nullptr; }
261 return nullptr;
262 }
263 virtual const testing::MockLayer* as_mock_layer() const { return nullptr; }
264
265 private:
266 SkRect paint_bounds_;
267 uint64_t unique_id_;
268 uint64_t original_layer_id_;
269 bool subtree_has_platform_view_ = false;
270
271 static uint64_t NextUniqueID();
272
274};
275
276} // namespace flutter
277
278#endif // FLUTTER_FLOW_LAYERS_LAYER_H_
static sk_sp< Effect > Create()
void SetLayerPaintRegion(const Layer *layer, const PaintRegion &region)
PaintRegion GetOldLayerPaintRegion(const Layer *layer) const
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:37
Collects snapshots of layers during frame rasterization.
static constexpr int kCallerCanApplyColorFilter
bool content_culled(const SkRect &content_bounds) const
static constexpr int kCallerCanApplyImageFilter
static constexpr int kCallerCanApplyOpacity
virtual void Diff(DiffContext *context, const Layer *old_layer)
Definition layer.h:157
virtual void Preroll(PrerollContext *context)=0
uint64_t original_layer_id() const
Definition layer.h:248
virtual RasterCacheKeyID caching_key_id() const
Definition layer.h:252
virtual ~Layer()
bool is_empty() const
Definition layer.h:227
virtual void PreservePaintRegion(DiffContext *context)
Definition layer.h:162
const SkRect & paint_bounds() const
Definition layer.h:209
virtual const testing::MockLayer * as_mock_layer() const
Definition layer.h:263
static constexpr int kSaveLayerRenderFlags
Definition layer.h:131
void set_subtree_has_platform_view(bool value)
Definition layer.h:201
void AssignOldLayer(Layer *old_layer)
Definition layer.h:145
static constexpr int kRasterCacheRenderFlags
Definition layer.h:139
virtual void PaintChildren(PaintContext &context) const
Definition layer.h:198
virtual bool IsReplacing(DiffContext *context, const Layer *old_layer) const
Definition layer.h:152
virtual const PerformanceOverlayLayer * as_performance_overlay_layer() const
Definition layer.h:260
bool subtree_has_platform_view() const
Definition layer.h:200
bool needs_painting(PaintContext &context) const
Definition layer.h:231
virtual const DisplayListLayer * as_display_list_layer() const
Definition layer.h:256
virtual void Paint(PaintContext &context) const =0
virtual const ContainerLayer * as_container_layer() const
Definition layer.h:255
virtual const TextureLayer * as_texture_layer() const
Definition layer.h:259
uint64_t unique_id() const
Definition layer.h:250
void set_paint_bounds(const SkRect &paint_bounds)
Definition layer.h:222
uint8_t value
#define FML_DCHECK(condition)
Definition logging.h:103
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
@ kAntiAlias
Definition layer.h:52
@ kAntiAliasWithSaveLayer
Definition layer.h:52
@ kNone
Definition layer.h:52
@ kHardEdge
Definition layer.h:52
static constexpr SkRect kGiantRect
Definition layer.h:49
bool isEmpty() const
Definition SkRect.h:693
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
bool enable_leaf_layer_tracing
Definition layer.h:119
ExternalViewEmbedder * view_embedder
Definition layer.h:110
impeller::AiksContext * aiks_context
Definition layer.h:121
bool rendering_above_platform_view
Definition layer.h:106
sk_sp< SkColorSpace > dst_color_space
Definition layer.h:109
const Stopwatch & raster_time
Definition layer.h:111
const Stopwatch & ui_time
Definition layer.h:112
LayerSnapshotStore * layer_snapshot_store
Definition layer.h:118
std::shared_ptr< TextureRegistry > texture_registry
Definition layer.h:113
DlCanvas * canvas
Definition layer.h:101
GrDirectContext * gr_context
Definition layer.h:108
LayerStateStack & state_stack
Definition layer.h:100
const RasterCache * raster_cache
Definition layer.h:114
GrDirectContext * gr_context
Definition layer.h:56
std::vector< RasterCacheItem * > * raster_cached_entries
Definition layer.h:84
std::shared_ptr< TextureRegistry > texture_registry
Definition layer.h:65
const Stopwatch & ui_time
Definition layer.h:64
const Stopwatch & raster_time
Definition layer.h:63
sk_sp< SkColorSpace > dst_color_space
Definition layer.h:59
RasterCache * raster_cache
Definition layer.h:55
LayerStateStack & state_stack
Definition layer.h:58
bool surface_needs_readback
Definition layer.h:60
ExternalViewEmbedder * view_embedder
Definition layer.h:57