Skip to content

Commit 9ccc966

Browse files
authored
Share engine layers with the framework (flutter#6406)
To make the PR minimal, we currently only share the engine layer when `pushPhysicalShape` (for Fuchsia) or `pushOffset` (for `RepaintBoundary` and `Opacity`) are called. They should be sufficient for our short-term perf goal. In the future, we can incrementally share more engine layers with the framework. flutter#21756
1 parent fa719e3 commit 9ccc966

File tree

14 files changed

+137
-33
lines changed

14 files changed

+137
-33
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ TYPE: LicenseType.bsd
475475
FILE: ../../../flutter/flutter_kernel_transformers/lib/track_widget_constructor_locations.dart
476476
FILE: ../../../flutter/fml/paths_unittests.cc
477477
FILE: ../../../flutter/lib/ui/isolate_name_server.dart
478+
FILE: ../../../flutter/lib/ui/painting/engine_layer.cc
479+
FILE: ../../../flutter/lib/ui/painting/engine_layer.h
478480
FILE: ../../../flutter/lib/ui/painting/image_encoding.cc
479481
FILE: ../../../flutter/lib/ui/painting/image_encoding.h
480482
FILE: ../../../flutter/lib/ui/plugins.dart

flow/layers/container_layer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ContainerLayer::ContainerLayer() {}
1010

1111
ContainerLayer::~ContainerLayer() = default;
1212

13-
void ContainerLayer::Add(std::unique_ptr<Layer> layer) {
13+
void ContainerLayer::Add(std::shared_ptr<Layer> layer) {
1414
layer->set_parent(this);
1515
layers_.push_back(std::move(layer));
1616
}

flow/layers/container_layer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class ContainerLayer : public Layer {
1515
ContainerLayer();
1616
~ContainerLayer() override;
1717

18-
void Add(std::unique_ptr<Layer> layer);
18+
void Add(std::shared_ptr<Layer> layer);
1919

2020
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
2121

2222
#if defined(OS_FUCHSIA)
2323
void UpdateScene(SceneUpdateContext& context) override;
2424
#endif // defined(OS_FUCHSIA)
2525

26-
const std::vector<std::unique_ptr<Layer>>& layers() const { return layers_; }
26+
const std::vector<std::shared_ptr<Layer>>& layers() const { return layers_; }
2727

2828
protected:
2929
void PrerollChildren(PrerollContext* context,
@@ -36,7 +36,7 @@ class ContainerLayer : public Layer {
3636
#endif // defined(OS_FUCHSIA)
3737

3838
private:
39-
std::vector<std::unique_ptr<Layer>> layers_;
39+
std::vector<std::shared_ptr<Layer>> layers_;
4040

4141
FML_DISALLOW_COPY_AND_ASSIGN(ContainerLayer);
4242
};

flow/layers/layer_tree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class LayerTree {
3838

3939
Layer* root_layer() const { return root_layer_.get(); }
4040

41-
void set_root_layer(std::unique_ptr<Layer> root_layer) {
41+
void set_root_layer(std::shared_ptr<Layer> root_layer) {
4242
root_layer_ = std::move(root_layer);
4343
}
4444

@@ -73,7 +73,7 @@ class LayerTree {
7373

7474
private:
7575
SkISize frame_size_; // Physical pixels.
76-
std::unique_ptr<Layer> root_layer_;
76+
std::shared_ptr<Layer> root_layer_;
7777
fml::TimeDelta construction_time_;
7878
uint32_t rasterizer_tracing_threshold_;
7979
bool checkerboard_raster_cache_images_;

lib/ui/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ source_set("ui") {
2323
"painting/canvas.h",
2424
"painting/codec.cc",
2525
"painting/codec.h",
26+
"painting/engine_layer.h",
27+
"painting/engine_layer.cc",
2628
"painting/frame_info.cc",
2729
"painting/frame_info.h",
2830
"painting/gradient.cc",

lib/ui/compositing.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
6969
/// This is equivalent to [pushTransform] with a matrix with only translation.
7070
///
7171
/// See [pop] for details about the operation stack.
72-
void pushOffset(double dx, double dy) native 'SceneBuilder_pushOffset';
72+
EngineLayer pushOffset(double dx, double dy) native 'SceneBuilder_pushOffset';
7373

7474
/// Pushes a rectangular clip operation onto the operation stack.
7575
///
@@ -177,10 +177,10 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
177177
///
178178
/// See [pop] for details about the operation stack, and [Clip] for different clip modes.
179179
// ignore: deprecated_member_use
180-
void pushPhysicalShape({ Path path, double elevation, Color color, Color shadowColor, Clip clipBehavior = defaultClipBehavior}) {
181-
_pushPhysicalShape(path, elevation, color.value, shadowColor?.value ?? 0xFF000000, clipBehavior.index);
180+
EngineLayer pushPhysicalShape({ Path path, double elevation, Color color, Color shadowColor, Clip clipBehavior = defaultClipBehavior}) {
181+
return _pushPhysicalShape(path, elevation, color.value, shadowColor?.value ?? 0xFF000000, clipBehavior.index);
182182
}
183-
void _pushPhysicalShape(Path path, double elevation, int color, int shadowColor, int clipBehavior) native
183+
EngineLayer _pushPhysicalShape(Path path, double elevation, int color, int shadowColor, int clipBehavior) native
184184
'SceneBuilder_pushPhysicalShape';
185185

186186
/// Ends the effect of the most recently pushed operation.
@@ -191,6 +191,14 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
191191
/// stack.
192192
void pop() native 'SceneBuilder_pop';
193193

194+
/// Add a retained engine layer subtree from previous frames.
195+
///
196+
/// All the engine layers that are in the subtree of the retained layer will
197+
/// be automatically appended to the current engine layer tree. Therefore,
198+
/// once this is called, there's no need to call [addToScene] for its children
199+
/// layers.
200+
EngineLayer addRetained(EngineLayer retainedLayer) native 'SceneBuilder_addRetained';
201+
194202
/// Adds an object to the scene that displays performance statistics.
195203
///
196204
/// Useful during development to assess the performance of the application.

lib/ui/compositing/scene.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, Scene);
2626

2727
DART_BIND_ALL(Scene, FOR_EACH_BINDING)
2828

29-
fml::RefPtr<Scene> Scene::create(std::unique_ptr<flow::Layer> rootLayer,
29+
fml::RefPtr<Scene> Scene::create(std::shared_ptr<flow::Layer> rootLayer,
3030
uint32_t rasterizerTracingThreshold,
3131
bool checkerboardRasterCacheImages,
3232
bool checkerboardOffscreenLayers) {
@@ -35,7 +35,7 @@ fml::RefPtr<Scene> Scene::create(std::unique_ptr<flow::Layer> rootLayer,
3535
checkerboardRasterCacheImages, checkerboardOffscreenLayers);
3636
}
3737

38-
Scene::Scene(std::unique_ptr<flow::Layer> rootLayer,
38+
Scene::Scene(std::shared_ptr<flow::Layer> rootLayer,
3939
uint32_t rasterizerTracingThreshold,
4040
bool checkerboardRasterCacheImages,
4141
bool checkerboardOffscreenLayers)

lib/ui/compositing/scene.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Scene : public RefCountedDartWrappable<Scene> {
2424

2525
public:
2626
~Scene() override;
27-
static fml::RefPtr<Scene> create(std::unique_ptr<flow::Layer> rootLayer,
27+
static fml::RefPtr<Scene> create(std::shared_ptr<flow::Layer> rootLayer,
2828
uint32_t rasterizerTracingThreshold,
2929
bool checkerboardRasterCacheImages,
3030
bool checkerboardOffscreenLayers);
@@ -40,7 +40,7 @@ class Scene : public RefCountedDartWrappable<Scene> {
4040
static void RegisterNatives(tonic::DartLibraryNatives* natives);
4141

4242
private:
43-
explicit Scene(std::unique_ptr<flow::Layer> rootLayer,
43+
explicit Scene(std::shared_ptr<flow::Layer> rootLayer,
4444
uint32_t rasterizerTracingThreshold,
4545
bool checkerboardRasterCacheImages,
4646
bool checkerboardOffscreenLayers);

lib/ui/compositing/scene_builder.cc

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, SceneBuilder);
5353
V(SceneBuilder, pushShaderMask) \
5454
V(SceneBuilder, pushPhysicalShape) \
5555
V(SceneBuilder, pop) \
56+
V(SceneBuilder, addRetained) \
5657
V(SceneBuilder, addPicture) \
5758
V(SceneBuilder, addTexture) \
5859
V(SceneBuilder, addChildScene) \
@@ -80,11 +81,12 @@ void SceneBuilder::pushTransform(const tonic::Float64List& matrix4) {
8081
PushLayer(std::move(layer));
8182
}
8283

83-
void SceneBuilder::pushOffset(double dx, double dy) {
84+
fml::RefPtr<EngineLayer> SceneBuilder::pushOffset(double dx, double dy) {
8485
SkMatrix sk_matrix = SkMatrix::MakeTrans(dx, dy);
85-
auto layer = std::make_unique<flow::TransformLayer>();
86+
auto layer = std::make_shared<flow::TransformLayer>();
8687
layer->set_transform(sk_matrix);
87-
PushLayer(std::move(layer));
88+
PushLayer(layer);
89+
return EngineLayer::MakeRetained(layer);
8890
}
8991

9092
void SceneBuilder::pushClipRect(double left,
@@ -148,21 +150,29 @@ void SceneBuilder::pushShaderMask(Shader* shader,
148150
PushLayer(std::move(layer));
149151
}
150152

151-
void SceneBuilder::pushPhysicalShape(const CanvasPath* path,
152-
double elevation,
153-
int color,
154-
int shadow_color,
155-
int clipBehavior) {
153+
fml::RefPtr<EngineLayer> SceneBuilder::pushPhysicalShape(const CanvasPath* path,
154+
double elevation,
155+
int color,
156+
int shadow_color,
157+
int clipBehavior) {
156158
const SkPath& sk_path = path->path();
157159
flow::Clip clip_behavior = static_cast<flow::Clip>(clipBehavior);
158-
auto layer = std::make_unique<flow::PhysicalShapeLayer>(clip_behavior);
160+
auto layer = std::make_shared<flow::PhysicalShapeLayer>(clip_behavior);
159161
layer->set_path(sk_path);
160162
layer->set_elevation(elevation);
161163
layer->set_color(static_cast<SkColor>(color));
162164
layer->set_shadow_color(static_cast<SkColor>(shadow_color));
163165
layer->set_device_pixel_ratio(
164166
UIDartState::Current()->window()->viewport_metrics().device_pixel_ratio);
165-
PushLayer(std::move(layer));
167+
PushLayer(layer);
168+
return EngineLayer::MakeRetained(layer);
169+
}
170+
171+
void SceneBuilder::addRetained(fml::RefPtr<EngineLayer> retainedLayer) {
172+
if (!current_layer_) {
173+
return;
174+
}
175+
current_layer_->Add(retainedLayer->Layer());
166176
}
167177

168178
void SceneBuilder::pop() {
@@ -260,7 +270,7 @@ fml::RefPtr<Scene> SceneBuilder::build() {
260270
return scene;
261271
}
262272

263-
void SceneBuilder::PushLayer(std::unique_ptr<flow::ContainerLayer> layer) {
273+
void SceneBuilder::PushLayer(std::shared_ptr<flow::ContainerLayer> layer) {
264274
FML_DCHECK(layer);
265275

266276
if (!root_layer_) {

lib/ui/compositing/scene_builder.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flutter/lib/ui/compositing/scene.h"
1313
#include "flutter/lib/ui/compositing/scene_host.h"
1414
#include "flutter/lib/ui/dart_wrapper.h"
15+
#include "flutter/lib/ui/painting/engine_layer.h"
1516
#include "flutter/lib/ui/painting/image_filter.h"
1617
#include "flutter/lib/ui/painting/path.h"
1718
#include "flutter/lib/ui/painting/picture.h"
@@ -33,7 +34,7 @@ class SceneBuilder : public RefCountedDartWrappable<SceneBuilder> {
3334
~SceneBuilder() override;
3435

3536
void pushTransform(const tonic::Float64List& matrix4);
36-
void pushOffset(double dx, double dy);
37+
fml::RefPtr<EngineLayer> pushOffset(double dx, double dy);
3738
void pushClipRect(double left,
3839
double right,
3940
double top,
@@ -50,11 +51,13 @@ class SceneBuilder : public RefCountedDartWrappable<SceneBuilder> {
5051
double maskRectTop,
5152
double maskRectBottom,
5253
int blendMode);
53-
void pushPhysicalShape(const CanvasPath* path,
54-
double elevation,
55-
int color,
56-
int shadowColor,
57-
int clipBehavior);
54+
fml::RefPtr<EngineLayer> pushPhysicalShape(const CanvasPath* path,
55+
double elevation,
56+
int color,
57+
int shadowColor,
58+
int clipBehavior);
59+
60+
void addRetained(fml::RefPtr<EngineLayer> retainedLayer);
5861

5962
void pop();
6063

@@ -92,14 +95,14 @@ class SceneBuilder : public RefCountedDartWrappable<SceneBuilder> {
9295
private:
9396
SceneBuilder();
9497

95-
std::unique_ptr<flow::ContainerLayer> root_layer_;
98+
std::shared_ptr<flow::ContainerLayer> root_layer_;
9699
flow::ContainerLayer* current_layer_ = nullptr;
97100

98101
int rasterizer_tracing_threshold_ = 0;
99102
bool checkerboard_raster_cache_images_ = false;
100103
bool checkerboard_offscreen_layers_ = false;
101104

102-
void PushLayer(std::unique_ptr<flow::ContainerLayer> layer);
105+
void PushLayer(std::shared_ptr<flow::ContainerLayer> layer);
103106

104107
FML_DISALLOW_COPY_AND_ASSIGN(SceneBuilder);
105108
};

0 commit comments

Comments
 (0)