Flutter Engine
The Flutter Engine
scene_builder.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/lib/ui/compositing/scene_builder.h"
6
7#include "flutter/flow/layers/backdrop_filter_layer.h"
8#include "flutter/flow/layers/clip_path_layer.h"
9#include "flutter/flow/layers/clip_rect_layer.h"
10#include "flutter/flow/layers/clip_rrect_layer.h"
11#include "flutter/flow/layers/color_filter_layer.h"
12#include "flutter/flow/layers/container_layer.h"
13#include "flutter/flow/layers/display_list_layer.h"
14#include "flutter/flow/layers/image_filter_layer.h"
15#include "flutter/flow/layers/layer.h"
16#include "flutter/flow/layers/layer_tree.h"
17#include "flutter/flow/layers/opacity_layer.h"
18#include "flutter/flow/layers/performance_overlay_layer.h"
19#include "flutter/flow/layers/platform_view_layer.h"
20#include "flutter/flow/layers/shader_mask_layer.h"
21#include "flutter/flow/layers/texture_layer.h"
22#include "flutter/flow/layers/transform_layer.h"
23#include "flutter/fml/build_config.h"
24#include "flutter/lib/ui/floating_point.h"
25#include "flutter/lib/ui/painting/matrix.h"
26#include "flutter/lib/ui/painting/shader.h"
31
32namespace flutter {
33
35
36SceneBuilder::SceneBuilder() {
37 // Add a ContainerLayer as the root layer, so that AddLayer operations are
38 // always valid.
39 PushLayer(std::make_shared<flutter::ContainerLayer>());
40}
41
43
45 tonic::Float64List& matrix4,
46 const fml::RefPtr<EngineLayer>& oldLayer) {
47 SkM44 sk_matrix = ToSkM44(matrix4);
48 auto layer = std::make_shared<flutter::TransformLayer>(sk_matrix);
49 PushLayer(layer);
50 // matrix4 has to be released before we can return another Dart object
51 matrix4.Release();
52 EngineLayer::MakeRetained(layer_handle, layer);
53
54 if (oldLayer && oldLayer->Layer()) {
55 layer->AssignOldLayer(oldLayer->Layer().get());
56 }
57}
58
60 double dx,
61 double dy,
62 const fml::RefPtr<EngineLayer>& oldLayer) {
64 auto layer = std::make_shared<flutter::TransformLayer>(sk_matrix);
65 PushLayer(layer);
66 EngineLayer::MakeRetained(layer_handle, layer);
67
68 if (oldLayer && oldLayer->Layer()) {
69 layer->AssignOldLayer(oldLayer->Layer().get());
70 }
71}
72
74 double left,
75 double right,
76 double top,
77 double bottom,
78 int clipBehavior,
79 const fml::RefPtr<EngineLayer>& oldLayer) {
81 SafeNarrow(right), SafeNarrow(bottom));
82 flutter::Clip clip_behavior = static_cast<flutter::Clip>(clipBehavior);
83 auto layer =
84 std::make_shared<flutter::ClipRectLayer>(clipRect, clip_behavior);
85 PushLayer(layer);
86 EngineLayer::MakeRetained(layer_handle, layer);
87
88 if (oldLayer && oldLayer->Layer()) {
89 layer->AssignOldLayer(oldLayer->Layer().get());
90 }
91}
92
94 const RRect& rrect,
95 int clipBehavior,
96 const fml::RefPtr<EngineLayer>& oldLayer) {
97 flutter::Clip clip_behavior = static_cast<flutter::Clip>(clipBehavior);
98 auto layer =
99 std::make_shared<flutter::ClipRRectLayer>(rrect.sk_rrect, clip_behavior);
100 PushLayer(layer);
101 EngineLayer::MakeRetained(layer_handle, layer);
102
103 if (oldLayer && oldLayer->Layer()) {
104 layer->AssignOldLayer(oldLayer->Layer().get());
105 }
106}
107
109 const CanvasPath* path,
110 int clipBehavior,
111 const fml::RefPtr<EngineLayer>& oldLayer) {
112 flutter::Clip clip_behavior = static_cast<flutter::Clip>(clipBehavior);
113 FML_DCHECK(clip_behavior != flutter::Clip::kNone);
114 auto layer =
115 std::make_shared<flutter::ClipPathLayer>(path->path(), clip_behavior);
116 PushLayer(layer);
117 EngineLayer::MakeRetained(layer_handle, layer);
118
119 if (oldLayer && oldLayer->Layer()) {
120 layer->AssignOldLayer(oldLayer->Layer().get());
121 }
122}
123
125 int alpha,
126 double dx,
127 double dy,
128 const fml::RefPtr<EngineLayer>& oldLayer) {
129 auto layer = std::make_shared<flutter::OpacityLayer>(
130 alpha, SkPoint::Make(SafeNarrow(dx), SafeNarrow(dy)));
131 PushLayer(layer);
132 EngineLayer::MakeRetained(layer_handle, layer);
133
134 if (oldLayer && oldLayer->Layer()) {
135 layer->AssignOldLayer(oldLayer->Layer().get());
136 }
137}
138
141 const fml::RefPtr<EngineLayer>& oldLayer) {
142 auto layer =
143 std::make_shared<flutter::ColorFilterLayer>(color_filter->filter());
144 PushLayer(layer);
145 EngineLayer::MakeRetained(layer_handle, layer);
146
147 if (oldLayer && oldLayer->Layer()) {
148 layer->AssignOldLayer(oldLayer->Layer().get());
149 }
150}
151
153 const ImageFilter* image_filter,
154 double dx,
155 double dy,
156 const fml::RefPtr<EngineLayer>& oldLayer) {
157 auto layer = std::make_shared<flutter::ImageFilterLayer>(
158 image_filter->filter(), SkPoint::Make(SafeNarrow(dx), SafeNarrow(dy)));
159 PushLayer(layer);
160 EngineLayer::MakeRetained(layer_handle, layer);
161
162 if (oldLayer && oldLayer->Layer()) {
163 layer->AssignOldLayer(oldLayer->Layer().get());
164 }
165}
166
168 Dart_Handle layer_handle,
169 ImageFilter* filter,
170 int blendMode,
171 const fml::RefPtr<EngineLayer>& oldLayer) {
172 auto layer = std::make_shared<flutter::BackdropFilterLayer>(
173 filter->filter(), static_cast<DlBlendMode>(blendMode));
174 PushLayer(layer);
175 EngineLayer::MakeRetained(layer_handle, layer);
176
177 if (oldLayer && oldLayer->Layer()) {
178 layer->AssignOldLayer(oldLayer->Layer().get());
179 }
180}
181
183 Shader* shader,
184 double maskRectLeft,
185 double maskRectRight,
186 double maskRectTop,
187 double maskRectBottom,
188 int blendMode,
189 int filterQualityIndex,
190 const fml::RefPtr<EngineLayer>& oldLayer) {
191 SkRect rect =
192 SkRect::MakeLTRB(SafeNarrow(maskRectLeft), SafeNarrow(maskRectTop),
193 SafeNarrow(maskRectRight), SafeNarrow(maskRectBottom));
194 auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
195 auto layer = std::make_shared<flutter::ShaderMaskLayer>(
196 shader->shader(sampling), rect, static_cast<DlBlendMode>(blendMode));
197 PushLayer(layer);
198 EngineLayer::MakeRetained(layer_handle, layer);
199
200 if (oldLayer && oldLayer->Layer()) {
201 layer->AssignOldLayer(oldLayer->Layer().get());
202 }
203}
204
206 AddLayer(retainedLayer->Layer());
207}
208
210 PopLayer();
211}
212
214 double dy,
216 int hints) {
217 if (!picture) {
218 // Picture::dispose was called and it has been collected.
219 return;
220 }
221
222 // Explicitly check for display_list, since the picture object might have
223 // been disposed but not collected yet, but the display list is null.
224 if (picture->display_list()) {
225 auto layer = std::make_unique<flutter::DisplayListLayer>(
226 SkPoint::Make(SafeNarrow(dx), SafeNarrow(dy)), picture->display_list(),
227 !!(hints & 1), !!(hints & 2));
228 AddLayer(std::move(layer));
229 }
230}
231
233 double dy,
234 double width,
235 double height,
236 int64_t textureId,
237 bool freeze,
238 int filterQualityIndex) {
239 auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
240 auto layer = std::make_unique<flutter::TextureLayer>(
242 SkSize::Make(SafeNarrow(width), SafeNarrow(height)), textureId, freeze,
243 sampling);
244 AddLayer(std::move(layer));
245}
246
248 double dy,
249 double width,
250 double height,
251 int64_t viewId) {
252 auto layer = std::make_unique<flutter::PlatformViewLayer>(
255 AddLayer(std::move(layer));
256}
257
258void SceneBuilder::addPerformanceOverlay(uint64_t enabledOptions,
259 double left,
260 double right,
261 double top,
262 double bottom) {
264 SafeNarrow(right), SafeNarrow(bottom));
265 auto layer =
266 std::make_unique<flutter::PerformanceOverlayLayer>(enabledOptions);
267 layer->set_paint_bounds(rect);
268 AddLayer(std::move(layer));
269}
270
272 FML_DCHECK(layer_stack_.size() >= 1);
273
274 Scene::create(scene_handle, std::move(layer_stack_[0]));
275 layer_stack_.clear();
276 ClearDartWrapper(); // may delete this object.
277}
278
279void SceneBuilder::AddLayer(std::shared_ptr<Layer> layer) {
280 FML_DCHECK(layer);
281
282 if (!layer_stack_.empty()) {
283 layer_stack_.back()->Add(std::move(layer));
284 }
285}
286
287void SceneBuilder::PushLayer(std::shared_ptr<ContainerLayer> layer) {
288 AddLayer(layer);
289 layer_stack_.push_back(std::move(layer));
290}
291
292void SceneBuilder::PopLayer() {
293 // We never pop the root layer, so that AddLayer operations are always valid.
294 if (layer_stack_.size() > 1) {
295 layer_stack_.pop_back();
296 }
297}
298
299} // namespace flutter
static sk_sp< SkImage > color_filter(const SkImage *image, SkColorFilter *colorFilter)
Definition: SkM44.h:150
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
static void MakeRetained(Dart_Handle dart_handle, const std::shared_ptr< flutter::ContainerLayer > &layer)
Definition: engine_layer.h:21
const std::shared_ptr< const DlImageFilter > filter() const
Definition: image_filter.h:38
static DlImageSampling SamplingFromIndex(int filterQualityIndex)
Definition: image_filter.cc:32
void pushOffset(Dart_Handle layer_handle, double dx, double dy, const fml::RefPtr< EngineLayer > &oldLayer)
~SceneBuilder() override
void pushBackdropFilter(Dart_Handle layer_handle, ImageFilter *filter, int blendMode, const fml::RefPtr< EngineLayer > &oldLayer)
void addTexture(double dx, double dy, double width, double height, int64_t textureId, bool freeze, int filterQuality)
void build(Dart_Handle scene_handle)
void pushShaderMask(Dart_Handle layer_handle, Shader *shader, double maskRectLeft, double maskRectRight, double maskRectTop, double maskRectBottom, int blendMode, int filterQualityIndex, const fml::RefPtr< EngineLayer > &oldLayer)
void pushClipRect(Dart_Handle layer_handle, double left, double right, double top, double bottom, int clipBehavior, const fml::RefPtr< EngineLayer > &oldLayer)
void pushClipPath(Dart_Handle layer_handle, const CanvasPath *path, int clipBehavior, const fml::RefPtr< EngineLayer > &oldLayer)
void addPlatformView(double dx, double dy, double width, double height, int64_t viewId)
void pushClipRRect(Dart_Handle layer_handle, const RRect &rrect, int clipBehavior, const fml::RefPtr< EngineLayer > &oldLayer)
void addRetained(const fml::RefPtr< EngineLayer > &retainedLayer)
void pushImageFilter(Dart_Handle layer_handle, const ImageFilter *image_filter, double dx, double dy, const fml::RefPtr< EngineLayer > &oldLayer)
void addPicture(double dx, double dy, Picture *picture, int hints)
void addPerformanceOverlay(uint64_t enabledOptions, double left, double right, double top, double bottom)
void pushTransform(Dart_Handle layer_handle, tonic::Float64List &matrix4, const fml::RefPtr< EngineLayer > &oldLayer)
void pushColorFilter(Dart_Handle layer_handle, const ColorFilter *color_filter, const fml::RefPtr< EngineLayer > &oldLayer)
void pushOpacity(Dart_Handle layer_handle, int alpha, double dx, double dy, const fml::RefPtr< EngineLayer > &oldLayer)
static void create(Dart_Handle scene_handle, std::shared_ptr< flutter::Layer > rootLayer)
Definition: scene.cc:27
virtual std::shared_ptr< DlColorSource > shader(DlImageSampling)=0
T * get() const
Definition: ref_ptr.h:116
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
SkMatrix sk_matrix
#define FML_DCHECK(condition)
Definition: logging.h:103
clipRect(r.rect, r.opAA.op(), r.opAA.aa())) template<> void Draw
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
SkRRect rrect
Definition: SkRecords.h:232
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkSamplingOptions sampling
Definition: SkRecords.h:337
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, FlutterGpuTestClass)
Clip
Definition: layer.h:53
@ kNone
Definition: layer.h:53
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
Definition: switches.h:57
static float SafeNarrow(double value)
constexpr SkM44 ToSkM44(const DlMatrix &matrix)
int32_t height
int32_t width
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56