Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b034550

Browse files
committed
SceneBuilder.addPicture returns the layer
1 parent 304ee81 commit b034550

File tree

11 files changed

+93
-31
lines changed

11 files changed

+93
-31
lines changed

lib/ui/compositing.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ class PhysicalShapeEngineLayer extends _EngineLayerWrapper {
188188
PhysicalShapeEngineLayer._(EngineLayer nativeLayer) : super._(nativeLayer);
189189
}
190190

191+
/// An opaque handle to a picture engine layer.
192+
///
193+
/// Instances of this class are created by [SceneBuilder.addPicture].
194+
///
195+
/// {@macro dart.ui.sceneBuilder.oldLayerCompatibility}
196+
class PictureEngineLayer extends _EngineLayerWrapper {
197+
PictureEngineLayer._(EngineLayer nativeLayer) : super._(nativeLayer);
198+
}
199+
191200
/// Builds a [Scene] containing the given visuals.
192201
///
193202
/// A [Scene] can then be rendered using [FlutterView.render].
@@ -699,18 +708,27 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
699708
/// Adds a [Picture] to the scene.
700709
///
701710
/// The picture is rasterized at the given offset.
702-
void addPicture(
711+
PictureEngineLayer addPicture(
703712
Offset offset,
704713
Picture picture, {
705714
bool isComplexHint = false,
706715
bool willChangeHint = false,
716+
PictureEngineLayer? oldLayer,
707717
}) {
718+
assert(_debugCheckCanBeUsedAsOldLayer(oldLayer, 'addPicture'));
719+
final EngineLayer engineLayer = EngineLayer._();
708720
final int hints = (isComplexHint ? 1 : 0) | (willChangeHint ? 2 : 0);
709-
_addPicture(offset.dx, offset.dy, picture, hints);
721+
_addPicture(engineLayer, offset.dx, offset.dy, picture, hints, oldLayer?._nativeLayer);
722+
return PictureEngineLayer._(engineLayer);
710723
}
711724

712-
void _addPicture(double dx, double dy, Picture picture, int hints)
713-
native 'SceneBuilder_addPicture';
725+
void _addPicture(
726+
EngineLayer outEngineLayer,
727+
double dx,
728+
double dy,
729+
Picture picture,
730+
int hints,
731+
EngineLayer? oldLayer) native 'SceneBuilder_addPicture';
714732

715733
/// Adds a backend texture to the scene.
716734
///

lib/ui/compositing/scene_builder.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,20 @@ void SceneBuilder::pop() {
272272
PopLayer();
273273
}
274274

275-
void SceneBuilder::addPicture(double dx,
275+
void SceneBuilder::addPicture(Dart_Handle layer_handle,
276+
double dx,
276277
double dy,
277278
Picture* picture,
278-
int hints) {
279-
auto layer = std::make_unique<flutter::PictureLayer>(
279+
int hints,
280+
fml::RefPtr<EngineLayer> oldLayer) {
281+
auto layer = std::make_shared<flutter::PictureLayer>(
280282
SkPoint::Make(dx, dy), UIDartState::CreateGPUObject(picture->picture()),
281283
!!(hints & 1), !!(hints & 2));
282-
AddLayer(std::move(layer));
284+
AddLayer(layer);
285+
EngineLayer::MakeRetained(layer_handle, layer);
286+
if (oldLayer && oldLayer->Layer()) {
287+
layer->AssignOldLayer(oldLayer->Layer().get());
288+
}
283289
}
284290

285291
void SceneBuilder::addTexture(double dx,

lib/ui/compositing/scene_builder.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ class SceneBuilder : public RefCountedDartWrappable<SceneBuilder> {
101101
double top,
102102
double bottom);
103103

104-
void addPicture(double dx, double dy, Picture* picture, int hints);
104+
void addPicture(Dart_Handle layer_handle,
105+
double dx,
106+
double dy,
107+
Picture* picture,
108+
int hints,
109+
fml::RefPtr<EngineLayer> oldLayer);
105110

106111
void addTexture(double dx,
107112
double dy,

lib/ui/painting/engine_layer.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/lib/ui/painting/engine_layer.h"
66

7+
#include "flutter/lib/ui/ui_dart_state.h"
78
#include "third_party/tonic/converter/dart_converter.h"
89
#include "third_party/tonic/dart_args.h"
910
#include "third_party/tonic/dart_binding_macros.h"
@@ -13,14 +14,14 @@ using tonic::ToDart;
1314

1415
namespace flutter {
1516

16-
EngineLayer::EngineLayer(std::shared_ptr<flutter::ContainerLayer> layer)
17+
EngineLayer::EngineLayer(std::shared_ptr<flutter::Layer> layer)
1718
: layer_(layer) {}
1819

1920
EngineLayer::~EngineLayer() = default;
2021

2122
size_t EngineLayer::GetAllocationSize() const {
2223
// Provide an approximation of the total memory impact of this object to the
23-
// Dart GC. The ContainerLayer may hold references to a tree of other layers,
24+
// Dart GC. The Layer may hold references to a tree of other layers,
2425
// which in turn may contain Skia objects.
2526
return 3000;
2627
};

lib/ui/painting/engine_layer.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ class EngineLayer : public RefCountedDartWrappable<EngineLayer> {
2424

2525
size_t GetAllocationSize() const override;
2626

27-
static void MakeRetained(Dart_Handle dart_handle,
28-
std::shared_ptr<flutter::ContainerLayer> layer) {
29-
auto engine_layer = fml::MakeRefCounted<EngineLayer>(layer);
27+
static fml::RefPtr<EngineLayer> MakeRetained(
28+
Dart_Handle dart_handle,
29+
std::shared_ptr<flutter::Layer> layer) {
30+
auto engine_layer = fml::MakeRefCounted<EngineLayer>(std::move(layer));
3031
engine_layer->AssociateWithDartWrapper(dart_handle);
32+
return engine_layer;
3133
}
3234

3335
static void RegisterNatives(tonic::DartLibraryNatives* natives);
3436

35-
std::shared_ptr<flutter::ContainerLayer> Layer() const { return layer_; }
37+
std::shared_ptr<flutter::Layer> Layer() const { return layer_; }
3638

3739
private:
38-
explicit EngineLayer(std::shared_ptr<flutter::ContainerLayer> layer);
39-
std::shared_ptr<flutter::ContainerLayer> layer_;
40+
explicit EngineLayer(std::shared_ptr<flutter::Layer> layer);
41+
std::shared_ptr<flutter::Layer> layer_;
4042

4143
FML_FRIEND_MAKE_REF_COUNTED(EngineLayer);
4244
};

lib/web_ui/lib/src/engine/canvaskit/layer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class ShaderMaskEngineLayer extends ContainerLayer implements ui.ShaderMaskEngin
411411
}
412412

413413
/// A layer containing a [Picture].
414-
class PictureLayer extends Layer {
414+
class PictureLayer extends Layer implements ui.PictureEngineLayer {
415415
/// The picture to paint into the canvas.
416416
final CkPicture picture;
417417

lib/web_ui/lib/src/engine/canvaskit/layer_scene_builder.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class LayerScene implements ui.Scene {
1515
@override
1616
Future<ui.Image> toImage(int width, int height) {
1717
ui.Picture picture = layerTree.flatten();
18-
return picture.toImage(width, height);
18+
return picture.toImage(width, height);/
1919
}
2020
}
2121

@@ -45,14 +45,21 @@ class LayerSceneBuilder implements ui.SceneBuilder {
4545
}
4646

4747
@override
48-
void addPicture(
48+
ui.PictureEngineLayer addPicture(
4949
ui.Offset offset,
5050
ui.Picture picture, {
5151
bool isComplexHint = false,
5252
bool willChangeHint = false,
53+
ui.PictureEngineLayer? oldLayer,
5354
}) {
54-
currentLayer.add(PictureLayer(
55-
picture as CkPicture, offset, isComplexHint, willChangeHint));
55+
final PictureLayer layer = PictureLayer(
56+
picture as CkPicture,
57+
offset,
58+
isComplexHint,
59+
willChangeHint,
60+
);
61+
currentLayer.add(layer);
62+
return layer;
5663
}
5764

5865
@override

lib/web_ui/lib/src/engine/html/picture.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void _recycleCanvas(EngineCanvas? canvas) {
7878

7979
/// A surface that uses a combination of `<canvas>`, `<div>` and `<p>` elements
8080
/// to draw shapes and text.
81-
class PersistedPicture extends PersistedLeafSurface {
81+
class PersistedPicture extends PersistedLeafSurface implements ui.PictureEngineLayer {
8282
PersistedPicture(this.dx, this.dy, this.picture, this.hints)
8383
: localPaintBounds = picture.recordingCanvas!.pictureBounds;
8484

lib/web_ui/lib/src/engine/html/scene_builder.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,12 @@ class SurfaceSceneBuilder implements ui.SceneBuilder {
359359
///
360360
/// The picture is rasterized at the given offset.
361361
@override
362-
void addPicture(
362+
ui.PictureEngineLayer addPicture(
363363
ui.Offset offset,
364364
ui.Picture picture, {
365365
bool isComplexHint = false,
366366
bool willChangeHint = false,
367+
ui.PictureEngineLayer? oldLayer,
367368
}) {
368369
int hints = 0;
369370
if (isComplexHint) {
@@ -372,8 +373,14 @@ class SurfaceSceneBuilder implements ui.SceneBuilder {
372373
if (willChangeHint) {
373374
hints |= 2;
374375
}
375-
_addSurface(PersistedPicture(
376-
offset.dx, offset.dy, picture as EnginePicture, hints));
376+
final PersistedPicture layer = PersistedPicture(
377+
offset.dx,
378+
offset.dy,
379+
picture as EnginePicture,
380+
hints,
381+
);
382+
_addSurface(layer);
383+
return layer;
377384
}
378385

379386
/// Adds a backend texture to the scene.

lib/web_ui/lib/src/ui/compositing.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ abstract class ShaderMaskEngineLayer implements EngineLayer {}
3131

3232
abstract class PhysicalShapeEngineLayer implements EngineLayer {}
3333

34+
abstract class PictureEngineLayer implements EngineLayer {}
35+
3436
abstract class SceneBuilder {
3537
factory SceneBuilder() {
3638
if (engine.useCanvasKit) {
@@ -99,11 +101,12 @@ abstract class SceneBuilder {
99101
void addRetained(EngineLayer retainedLayer);
100102
void pop();
101103
void addPerformanceOverlay(int enabledOptions, Rect bounds);
102-
void addPicture(
104+
PictureEngineLayer addPicture(
103105
Offset offset,
104106
Picture picture, {
105107
bool isComplexHint = false,
106108
bool willChangeHint = false,
109+
PictureEngineLayer? oldLayer,
107110
});
108111
void addTexture(
109112
int textureId, {

0 commit comments

Comments
 (0)