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