Skip to content

Conversation

@harryterkelsen
Copy link
Contributor

Fixes #82878

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions bot added engine flutter/engine related. See also e: labels. platform-web Web applications specifically labels Nov 26, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a clone method for LayerPicture and updates PictureLayer to dispose of its picture, addressing a memory management issue on the web. By cloning Picture objects when they are added to a LayerSceneBuilder, the application can safely dispose of its original Picture handle while the engine retains its own copy. The underlying graphics resources are kept alive via reference counting and are only released when the PictureLayer itself is disposed. The changes are consistently applied across both the CanvasKit and Skwasm backends and are accompanied by a relevant test case. The overall approach is sound and effectively resolves the issue. I've added a couple of minor suggestions to add documentation for new functions to improve maintainability.

Comment on lines +41 to +42
@Native<Void Function(PictureHandle)>(symbol: 'picture_ref', isLeaf: true)
external void pictureRef(PictureHandle handle);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the Flutter Style Guide, all public members should have documentation.1 Please add a doc comment for the pictureRef function to describe its purpose.

/// Increments the reference count of the underlying native picture object.
@Native<Void Function(PictureHandle)>(symbol: 'picture_ref', isLeaf: true)
external void pictureRef(PictureHandle handle);

Style Guide References

Footnotes

  1. All public members should have documentation.

Comment on lines +59 to +62
SKWASM_EXPORT void picture_ref(DisplayList* picture) {
livePictureCount++;
picture->ref();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better maintainability and clarity, especially for exported functions, please add a comment explaining what picture_ref does. It's helpful to describe that it increments the reference count of the DisplayList.

// Increments the reference count of the given DisplayList object. This is used
// when a picture is cloned on the Dart side.
SKWASM_EXPORT void picture_ref(DisplayList* picture) {
  livePictureCount++;
  picture->ref();
}

// Add a clone of the picture to the layer tree so that the original picture
// can be disposed.
currentLayer.add(
PictureLayer((picture as LayerPicture).clone(), offset, isComplexHint, willChangeHint),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really feels like a picture should be a CountedRef, and PictureLayer should somehow do picture.ref(this);. That would avoid the unnecessary cloning of pictures, and the _isClone thing.

But (ugh!) CountedRef is designed to only work with C++/JSObject, so... probably outside the scope of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there's a bit of clunkiness because we use our implementations of the dart:ui classes to be both how we represent the Skia objects to the framework but also to be how the engine holds onto the Skia objects itself. In the native engine there is another layer of abstraction in the C++ UI library between the dart:ui types and the underlying Skia objects.

Comment on lines +96 to +98
if (!_isClone) {
ui.Picture.onDispose?.call(this);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By doing it this way, we are reporting the disposal of the picture as soon as the framework calls dispose on the original picture, even though the "picture" is still alive in the engine. This probably defeats the purpose of the callback.

I'm guessing the purpose of the callback is to report when the picture is actually disposed, i.e. when all clones have been disposed and the native object is released.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches how the native engine does it:

Picture.onDispose?.call(this);

Once Picture.dispose() is called, the framework has no way to access the picture anymore.

The reason the native engine can call _dispose right away and doesn't need to create a clone of the image is that the native implementation of Picture.dispose just derefs the backing display_list, and when addPicture is called, it creates a new reference to the backing display_list. On the web, the Picture object is both how we represent the underlying Skia picture to the framework and how we hold the reference to the Skia picture ourselves, so we clone the Picture

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I see now. Okay if it matches the native behavior, then sounds good!

Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +96 to +98
if (!_isClone) {
ui.Picture.onDispose?.call(this);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I see now. Okay if it matches the native behavior, then sounds good!


implicitView.debugPhysicalSizeOverride = const ui.Size(200, 200);
implicitView.debugForceResize();
await renderScene(sb.build());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should've been failing all along. According to the SceneBuilder.build docs:

After calling this function, the scene builder object is invalid and cannot be used further.

Can we make LayerSceneBuilder.build explicitly throw when it's called more than once? (not necessarily in this PR).

Copy link
Contributor Author

@harryterkelsen harryterkelsen Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Let's make it so that the SceneBuilder disposes itself after build() is called, which is basically what the native engine does

I'll add a TODO

Comment on lines 26 to 28
void dispose() {
// This is a no-op for all Layer types except PictureLayer.
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing ui.EngineLayer.dispose being called inside the engine, so I'm assuming that the framework is calling it when it's done rendering the scene.

If that's the case, why not override RootLayer.dispose and do the clonedPictures cleanup there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's basically what I changed it to, except I overrode dispose() in ContainerLayer and dispose of any straggling PictureLayers there.

Comment on lines +25 to +26
_sceneToRender?.dispose();
_sceneToRender = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good candidate for whenComplete?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@harryterkelsen
Copy link
Contributor Author

Thanks Mouad! PTAL

@harryterkelsen harryterkelsen added this pull request to the merge queue Dec 3, 2025
Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines 32 to 33
_sceneToRender?.dispose();
_sceneToRender = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary here since it's already done in whenComplete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Good catch

/// The pictures that were cloned into this layer tree.
///
/// They need to be disposed of after the tree has been rendered.
final List<LayerPicture> clonedPictures = <LayerPicture>[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't needed anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! Done

@harryterkelsen harryterkelsen removed this pull request from the merge queue due to a manual request Dec 3, 2025
@harryterkelsen harryterkelsen added the autosubmit Merge PR when tree becomes green via auto submit App label Dec 3, 2025
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Dec 4, 2025
@auto-submit
Copy link
Contributor

auto-submit bot commented Dec 4, 2025

autosubmit label was removed for flutter/flutter/179162, because - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label.

@harryterkelsen harryterkelsen added the autosubmit Merge PR when tree becomes green via auto submit App label Dec 4, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Dec 4, 2025
Merged via the queue into flutter:master with commit 42d6f77 Dec 4, 2025
182 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Dec 4, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Dec 4, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Dec 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Dec 5, 2025
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Dec 5, 2025
Roll Flutter from 69d8710fadcd to 5b8720312a94 (42 revisions)

flutter/flutter@69d8710...5b87203

2025-12-05 engine-flutter-autoroll@skia.org Roll Packages from d39e481 to b17d3ff (3 revisions) (flutter/flutter#179505)
2025-12-05 bungeman@chromium.org Move target_cpu into use_rbe block in Fuchsia build (flutter/flutter#179458)
2025-12-05 engine-flutter-autoroll@skia.org Roll Skia from fe6bf18a3f6b to a31411879251 (3 revisions) (flutter/flutter#179499)
2025-12-05 sokolovskyi.konstantin@gmail.com Force WASM single threading in Chrome extensions. (flutter/flutter#179400)
2025-12-05 engine-flutter-autoroll@skia.org Roll Skia from aea282ea0bcd to fe6bf18a3f6b (7 revisions) (flutter/flutter#179489)
2025-12-05 dolt@guide.inc fix: check both pointer count and action before reusing MotionEvent (flutter/flutter#178528)
2025-12-05 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from VtDPX2k1kosHxjKUE... to vDeTl_KBeLJY8nCAx... (flutter/flutter#179478)
2025-12-05 bkonyi@google.com [ Widget Preview ] Add embedded Widget Inspector support (flutter/flutter#178116)
2025-12-05 30870216+gaaclarke@users.noreply.github.com bubble up fragment shader errors (flutter/flutter#179363)
2025-12-05 engine-flutter-autoroll@skia.org Roll Dart SDK from eda82318e193 to 42fe3327abca (1 revision) (flutter/flutter#179472)
2025-12-05 jesswon@google.com [Android 16] Use AVD With New Android Renderer (flutter/flutter#179306)
2025-12-05 bkonyi@google.com Unpin `package:dwds` dependency (flutter/flutter#179462)
2025-12-05 flar@google.com [Impeller] include uniform info in impellerc json reflections (flutter/flutter#179317)
2025-12-05 engine-flutter-autoroll@skia.org Roll Skia from 1591b066f49b to aea282ea0bcd (1 revision) (flutter/flutter#179468)
2025-12-04 engine-flutter-autoroll@skia.org Roll Skia from e1923478562b to 1591b066f49b (3 revisions) (flutter/flutter#179460)
2025-12-04 jacksongardner@google.com Cherry-pick flutter 3.38.4 changelog back to master. (flutter/flutter#179463)
2025-12-04 1961493+harryterkelsen@users.noreply.github.com [web] Add clone method to LayerPicture and dispose pictures in PictureLayer (flutter/flutter#179162)
2025-12-04 30870216+gaaclarke@users.noreply.github.com Fixes merge conflict from high bitrate texture tests (flutter/flutter#179416)
2025-12-04 116356835+AbdeMohlbi@users.noreply.github.com Fix typos in `VirtualDisplayController.java` (flutter/flutter#179411)
2025-12-04 engine-flutter-autoroll@skia.org Roll Dart SDK from 2de44cc08970 to eda82318e193 (1 revision) (flutter/flutter#179453)
2025-12-04 engine-flutter-autoroll@skia.org Roll Skia from 55d94a54f453 to e1923478562b (1 revision) (flutter/flutter#179449)
2025-12-04 engine-flutter-autoroll@skia.org Roll Packages from 8cb4903 to d39e481 (8 revisions) (flutter/flutter#179451)
2025-12-04 bruno.leroux@gmail.com Add DropdownMenu.selectOnly (flutter/flutter#179189)
2025-12-04 engine-flutter-autoroll@skia.org Roll Dart SDK from 7e6bfc6af55c to 2de44cc08970 (6 revisions) (flutter/flutter#179443)
2025-12-04 engine-flutter-autoroll@skia.org Roll Skia from b8f79d7316c0 to 55d94a54f453 (1 revision) (flutter/flutter#179439)
2025-12-04 6655696+guidezpl@users.noreply.github.com Revise README for link updates and terminology changes (flutter/flutter#179357)
2025-12-04 engine-flutter-autoroll@skia.org Roll Skia from ce19122e3982 to b8f79d7316c0 (3 revisions) (flutter/flutter#179436)
2025-12-04 bruno.leroux@gmail.com Update some BottomNavigationBar comments to reflect theme normalization (flutter/flutter#179404)
2025-12-04 robert.ancell@canonical.com Replace use of eglCreateImage with eglCreateImageKHR to reduce EGL requirement (flutter/flutter#179310)
2025-12-04 engine-flutter-autoroll@skia.org Roll Skia from 81a9a0751f00 to ce19122e3982 (5 revisions) (flutter/flutter#179430)
2025-12-04 30870216+gaaclarke@users.noreply.github.com [impellerc] adds entry prefix flag to avoid shader collisions (flutter/flutter#179160)
2025-12-04 jon.i@hotmail.fr [Windows] Allow apps to prefer high power GPUs (flutter/flutter#177653)
2025-12-04 bkonyi@google.com [ Infra ] Shard `Windows tool_tests_commands` (flutter/flutter#179409)
2025-12-04 nshahan@google.com [flutter_tools] Fix filename typo (flutter/flutter#179427)
2025-12-04 fluttergithubbot@gmail.com Marks Linux_pixel_7pro draw_arcs_all_stroke_styles_perf__timeline_summary to be unflaky (flutter/flutter#179392)
2025-12-03 fluttergithubbot@gmail.com Marks Mac_ios draw_arcs_all_fill_styles_perf_ios__timeline_summary to be unflaky (flutter/flutter#179391)
2025-12-03 fluttergithubbot@gmail.com Marks Linux_pixel_7pro draw_arcs_all_fill_styles_perf__timeline_summary to be unflaky (flutter/flutter#179390)
2025-12-03 jason-simmons@users.noreply.github.com Roll Abseil to 564023aa5376 (flutter/flutter#179421)
2025-12-03 30870216+gaaclarke@users.noreply.github.com Made wide gamut tests give more information in their failure (flutter/flutter#179415)
2025-12-03 engine-flutter-autoroll@skia.org Roll Skia from c20f797ab6f9 to 81a9a0751f00 (5 revisions) (flutter/flutter#179417)
2025-12-03 737941+loic-sharma@users.noreply.github.com Migrate samples and docs to RadioGroup (flutter/flutter#179158)
2025-12-03 iinozemtsev@google.com Roll Dart SDK to 3.11.0-200.1.beta (flutter/flutter#179399)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine flutter/engine related. See also e: labels. platform-web Web applications specifically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Layer.dispose for CanvasKit layers

2 participants