Flutter Engine
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 <memory>
9 #include <vector>
10 
11 #include "flutter/flow/embedded_views.h"
12 #include "flutter/flow/instrumentation.h"
13 #include "flutter/flow/raster_cache.h"
14 #include "flutter/flow/texture.h"
15 #include "flutter/fml/build_config.h"
16 #include "flutter/fml/compiler_specific.h"
17 #include "flutter/fml/logging.h"
18 #include "flutter/fml/macros.h"
19 #include "flutter/fml/trace_event.h"
20 #include "third_party/skia/include/core/SkCanvas.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "third_party/skia/include/core/SkColorFilter.h"
23 #include "third_party/skia/include/core/SkMatrix.h"
24 #include "third_party/skia/include/core/SkPath.h"
25 #include "third_party/skia/include/core/SkPicture.h"
26 #include "third_party/skia/include/core/SkRRect.h"
27 #include "third_party/skia/include/core/SkRect.h"
28 #include "third_party/skia/include/utils/SkNWayCanvas.h"
29 
30 #if defined(LEGACY_FUCHSIA_EMBEDDER)
31 #include "flutter/flow/scene_update_context.h" //nogncheck
32 #include "lib/ui/scenic/cpp/resources.h" //nogncheck
33 #include "lib/ui/scenic/cpp/session.h" //nogncheck
34 #endif
35 
36 namespace flutter {
37 
38 static constexpr SkRect kGiantRect = SkRect::MakeLTRB(-1E9F, -1E9F, 1E9F, 1E9F);
39 
40 // This should be an exact copy of the Clip enum in painting.dart.
42 
45  GrDirectContext* gr_context;
48  SkColorSpace* dst_color_space;
49  SkRect cull_rect;
51 
52  // These allow us to paint in the end of subtree Preroll.
58 
59  // These allow us to track properties like elevation, opacity, and the
60  // prescence of a platform view during Preroll.
61  bool has_platform_view = false;
62 #if defined(LEGACY_FUCHSIA_EMBEDDER)
63  // True if, during the traversal so far, we have seen a child_scene_layer.
64  // Informs whether a layer needs to be system composited.
65  bool child_scene_layer_exists_below = false;
66 #endif
67 };
68 
69 // Represents a single composited layer. Created on the UI thread but then
70 // subquently used on the Rasterizer thread.
71 class Layer {
72  public:
73  Layer();
74  virtual ~Layer();
75 
76  virtual void Preroll(PrerollContext* context, const SkMatrix& matrix);
77 
78  // Used during Preroll by layers that employ a saveLayer to manage the
79  // PrerollContext settings with values affected by the saveLayer mechanism.
80  // This object must be created before calling Preroll on the children to
81  // set up the state for the children and then restore the state upon
82  // destruction.
84  public:
85  [[nodiscard]] static AutoPrerollSaveLayerState Create(
86  PrerollContext* preroll_context,
87  bool save_layer_is_active = true,
88  bool layer_itself_performs_readback = false);
89 
91 
92  private:
94  bool save_layer_is_active,
95  bool layer_itself_performs_readback);
96 
97  PrerollContext* preroll_context_;
98  bool save_layer_is_active_;
99  bool layer_itself_performs_readback_;
100 
101  bool prev_surface_needs_readback_;
102  };
103 
104  struct PaintContext {
105  // When splitting the scene into multiple canvases (e.g when embedding
106  // a platform view on iOS) during the paint traversal we apply the non leaf
107  // flow layers to all canvases, and leaf layers just to the "current"
108  // canvas. Applying the non leaf layers to all canvases ensures that when
109  // we switch a canvas (when painting a PlatformViewLayer) the next canvas
110  // has the exact same state as the current canvas.
111  // The internal_nodes_canvas is a SkNWayCanvas which is used by non leaf
112  // and applies the operations to all canvases.
113  // The leaf_nodes_canvas is the "current" canvas and is used by leaf
114  // layers.
116  SkCanvas* leaf_nodes_canvas;
117  GrDirectContext* gr_context;
125  };
126 
127  // Calls SkCanvas::saveLayer and restores the layer upon destruction. Also
128  // draws a checkerboard over the layer if that is enabled in the PaintContext.
130  public:
131  [[nodiscard]] static AutoSaveLayer Create(const PaintContext& paint_context,
132  const SkRect& bounds,
133  const SkPaint* paint);
134 
135  [[nodiscard]] static AutoSaveLayer Create(
136  const PaintContext& paint_context,
137  const SkCanvas::SaveLayerRec& layer_rec);
138 
139  ~AutoSaveLayer();
140 
141  private:
142  AutoSaveLayer(const PaintContext& paint_context,
143  const SkRect& bounds,
144  const SkPaint* paint);
145 
146  AutoSaveLayer(const PaintContext& paint_context,
147  const SkCanvas::SaveLayerRec& layer_rec);
148 
149  const PaintContext& paint_context_;
150  const SkRect bounds_;
151  };
152 
153  virtual void Paint(PaintContext& context) const = 0;
154 
155 #if defined(LEGACY_FUCHSIA_EMBEDDER)
156  // Updates the system composited scene.
157  virtual void UpdateScene(std::shared_ptr<SceneUpdateContext> context);
158  virtual void CheckForChildLayerBelow(PrerollContext* context);
159 #endif
160 
161  bool needs_system_composite() const { return needs_system_composite_; }
163  needs_system_composite_ = value;
164  }
165 
166  const SkRect& paint_bounds() const { return paint_bounds_; }
167 
168  // This must be set by the time Preroll() returns otherwise the layer will
169  // be assumed to have empty paint bounds (paints no content).
170  void set_paint_bounds(const SkRect& paint_bounds) {
171  paint_bounds_ = paint_bounds;
172  }
173 
174  bool needs_painting() const { return !paint_bounds_.isEmpty(); }
175 
176  uint64_t unique_id() const { return unique_id_; }
177 
178  protected:
179 #if defined(LEGACY_FUCHSIA_EMBEDDER)
180  bool child_layer_exists_below_ = false;
181 #endif
182 
183  private:
184  SkRect paint_bounds_;
185  uint64_t unique_id_;
186  bool needs_system_composite_;
187 
188  static uint64_t NextUniqueID();
189 
191 };
192 
193 } // namespace flutter
194 
195 #endif // FLUTTER_FLOW_LAYERS_LAYER_H_
const Stopwatch & ui_time
Definition: layer.h:54
ExternalViewEmbedder * view_embedder
Definition: layer.h:118
const Stopwatch & ui_time
Definition: layer.h:120
TextureRegistry & texture_registry
Definition: layer.h:121
GrDirectContext * gr_context
Definition: layer.h:45
const bool checkerboard_offscreen_layers
Definition: layer.h:123
const Stopwatch & raster_time
Definition: layer.h:53
static constexpr SkRect kGiantRect
Definition: layer.h:38
GrDirectContext * gr_context
Definition: layer.h:117
const Stopwatch & raster_time
Definition: layer.h:119
TextureRegistry & texture_registry
Definition: layer.h:55
const RasterCache * raster_cache
Definition: layer.h:122
uint8_t value
const bool checkerboard_offscreen_layers
Definition: layer.h:56
bool needs_system_composite() const
Definition: layer.h:161
const float frame_device_pixel_ratio
Definition: layer.h:124
bool needs_painting() const
Definition: layer.h:174
SkColorSpace * dst_color_space
Definition: layer.h:48
const SkRect & paint_bounds() const
Definition: layer.h:166
const float frame_device_pixel_ratio
Definition: layer.h:57
SkCanvas * leaf_nodes_canvas
Definition: layer.h:116
void set_paint_bounds(const SkRect &paint_bounds)
Definition: layer.h:170
uint64_t unique_id() const
Definition: layer.h:176
Clip
Definition: layer.h:41
RasterCache * raster_cache
Definition: layer.h:44
SkCanvas * internal_nodes_canvas
Definition: layer.h:115
ExternalViewEmbedder * view_embedder
Definition: layer.h:46
bool surface_needs_readback
Definition: layer.h:50
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
void set_needs_system_composite(bool value)
Definition: layer.h:162
MutatorsStack & mutators_stack
Definition: layer.h:47