Skip to content

Commit 7232346

Browse files
[web] Keep ImageSource alive until Picture disposal (flutter#192516)
Retains underlying `ImageSource` references across CanvasKit and Skwasm canvas drawing operations until the resulting `Picture` is disposed. This prevents premature DOM texture closure and black rendering when an image handle is disposed before rasterization. Fixes flutter#191800 Fixes flutter#192347 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). 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. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 4c2db31 commit 7232346

11 files changed

Lines changed: 671 additions & 51 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvas.dart‎

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ class CkCanvas implements LayerCanvas {
2626
return ckRecorder.beginRecording(cullRect);
2727
}
2828

29-
CkCanvas.fromSkCanvas(this.skCanvas);
29+
CkCanvas.fromSkCanvas(this.skCanvas, {CkPictureRecorder? recorder, PictureImageTracker? tracker})
30+
: _recorder = recorder,
31+
_tracker = tracker;
32+
33+
// Holds a reference to the recorder to prevent premature garbage collection
34+
// of the recorder while this canvas is active. This ensures the recorder's
35+
// finalizer does not run prematurely to release tracked image sources.
36+
// ignore: unused_field
37+
final CkPictureRecorder? _recorder;
38+
PictureImageTracker? _tracker;
39+
40+
/// Clears the tracker reference when recording finishes so subsequent
41+
/// operations (if any) do not retain images.
42+
void clearTracker() {
43+
_tracker = null;
44+
}
3045

3146
// Cubic equation coefficients recommended by Mitchell & Netravali
3247
// in their paper on cubic interpolation.
@@ -82,6 +97,7 @@ class CkCanvas implements LayerCanvas {
8297
@override
8398
void drawArc(ui.Rect oval, double startAngle, double sweepAngle, bool useCenter, ui.Paint paint) {
8499
assert(rectIsValid(oval));
100+
_tracker?.recordPaint(paint);
85101
const double toDegrees = 180 / math.pi;
86102

87103
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
@@ -98,6 +114,7 @@ class CkCanvas implements LayerCanvas {
98114
@override
99115
void drawCircle(ui.Offset c, double radius, ui.Paint paint) {
100116
assert(offsetIsValid(c));
117+
_tracker?.recordPaint(paint);
101118
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
102119
skCanvas.drawCircle(c.dx, c.dy, radius, skPaint);
103120
skPaint.delete();
@@ -112,6 +129,7 @@ class CkCanvas implements LayerCanvas {
112129
void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) {
113130
assert(rrectIsValid(outer));
114131
assert(rrectIsValid(inner));
132+
_tracker?.recordPaint(paint);
115133
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
116134
skCanvas.drawDRRect(toSkRRect(outer), toSkRRect(inner), skPaint);
117135
skPaint.delete();
@@ -124,9 +142,15 @@ class CkCanvas implements LayerCanvas {
124142
throw ArgumentError('The image being drawn must be a CanvasKit image.');
125143
}
126144

145+
/// Draws [image] onto the canvas with its top-left corner at [offset].
146+
///
147+
/// Retains the underlying DOM [ImageSource] for [image] and any shader
148+
/// images in [paint] until the recorded picture is disposed.
127149
@override
128150
void drawImage(ui.Image image, ui.Offset offset, ui.Paint paint) {
129151
assert(offsetIsValid(offset));
152+
_tracker?.recordImage(image);
153+
_tracker?.recordPaint(paint);
130154
final SkImage skImage = _getSkImage(image);
131155
final ui.FilterQuality filterQuality = paint.filterQuality;
132156
final SkPaint skPaint = (paint as CkPaint).toSkPaint(defaultBlurTileMode: ui.TileMode.clamp);
@@ -157,6 +181,8 @@ class CkCanvas implements LayerCanvas {
157181
void drawImageRect(ui.Image image, ui.Rect src, ui.Rect dst, ui.Paint paint) {
158182
assert(rectIsValid(src));
159183
assert(rectIsValid(dst));
184+
_tracker?.recordImage(image);
185+
_tracker?.recordPaint(paint);
160186
final SkImage skImage = _getSkImage(image);
161187

162188
final ui.FilterQuality filterQuality = paint.filterQuality;
@@ -188,6 +214,8 @@ class CkCanvas implements LayerCanvas {
188214
void drawImageNine(ui.Image image, ui.Rect center, ui.Rect dst, ui.Paint paint) {
189215
assert(rectIsValid(center));
190216
assert(rectIsValid(dst));
217+
_tracker?.recordImage(image);
218+
_tracker?.recordPaint(paint);
191219
final SkImage skImage = _getSkImage(image);
192220
final SkPaint skPaint = (paint as CkPaint).toSkPaint(defaultBlurTileMode: ui.TileMode.clamp);
193221
skCanvas.drawImageNine(
@@ -204,6 +232,7 @@ class CkCanvas implements LayerCanvas {
204232
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) {
205233
assert(offsetIsValid(p1));
206234
assert(offsetIsValid(p2));
235+
_tracker?.recordPaint(paint);
207236
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
208237
skCanvas.drawLine(p1.dx, p1.dy, p2.dx, p2.dy, skPaint);
209238
skPaint.delete();
@@ -212,18 +241,26 @@ class CkCanvas implements LayerCanvas {
212241
@override
213242
void drawOval(ui.Rect rect, ui.Paint paint) {
214243
assert(rectIsValid(rect));
244+
_tracker?.recordPaint(paint);
215245
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
216246
skCanvas.drawOval(toSkRect(rect), skPaint);
217247
skPaint.delete();
218248
}
219249

220250
@override
221251
void drawPaint(ui.Paint paint) {
252+
_tracker?.recordPaint(paint);
222253
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
223254
skCanvas.drawPaint(skPaint);
224255
skPaint.delete();
225256
}
226257

258+
/// Draws a text [paragraph] at [offset].
259+
///
260+
/// Note: When [paragraph] is a [CkParagraph], its layout and shaders are
261+
/// encapsulated natively by SkParagraph. When [paragraph] is a [WebParagraph],
262+
/// painting is dispatched back to this canvas where paints with image shaders
263+
/// are automatically tracked.
227264
@override
228265
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
229266
assert(offsetIsValid(offset));
@@ -238,6 +275,7 @@ class CkCanvas implements LayerCanvas {
238275

239276
@override
240277
void drawPath(ui.Path path, ui.Paint paint) {
278+
_tracker?.recordPaint(paint);
241279
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
242280
skCanvas.drawPath(((path as EnginePath).backendPath as CkPath).skiaObject, skPaint);
243281
skPaint.delete();
@@ -246,11 +284,15 @@ class CkCanvas implements LayerCanvas {
246284
@override
247285
void drawPicture(ui.Picture picture) {
248286
assert((picture as CkPicture).debugCheckNotDisposed('Failed to draw picture.'));
287+
if (picture case CkPicture(:final imageTracker?)) {
288+
_tracker?.recordFrom(imageTracker);
289+
}
249290
skCanvas.drawPicture((picture as CkPicture).skiaObject);
250291
}
251292

252293
@override
253294
void drawPoints(ui.PointMode pointMode, List<ui.Offset> points, ui.Paint paint) {
295+
_tracker?.recordPaint(paint);
254296
final SkFloat32List skPoints = toMallocedSkPoints(points);
255297
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
256298
skCanvas.drawPoints(toSkPointMode(pointMode), skPoints.toTypedArray(), skPaint);
@@ -263,6 +305,7 @@ class CkCanvas implements LayerCanvas {
263305
if (points.length % 2 != 0) {
264306
throw ArgumentError('"points" must have an even number of values.');
265307
}
308+
_tracker?.recordPaint(paint);
266309
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
267310
skCanvas.drawPoints(toSkPointMode(pointMode), points, skPaint);
268311
skPaint.delete();
@@ -271,13 +314,15 @@ class CkCanvas implements LayerCanvas {
271314
@override
272315
void drawRRect(ui.RRect rrect, ui.Paint paint) {
273316
assert(rrectIsValid(rrect));
317+
_tracker?.recordPaint(paint);
274318
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
275319
skCanvas.drawRRect(toSkRRect(rrect), skPaint);
276320
skPaint.delete();
277321
}
278322

279323
@override
280324
void drawRSuperellipse(ui.RSuperellipse rsuperellipse, ui.Paint paint) {
325+
_tracker?.recordPaint(paint);
281326
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
282327
final (ui.Path path, ui.Offset offset) = rsuperellipse.toPathOffset();
283328
translate(offset.dx, offset.dy);
@@ -289,6 +334,7 @@ class CkCanvas implements LayerCanvas {
289334
@override
290335
void drawRect(ui.Rect rect, ui.Paint paint) {
291336
assert(rectIsValid(rect));
337+
_tracker?.recordPaint(paint);
292338
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
293339
skCanvas.drawRect(toSkRect(rect), skPaint);
294340
skPaint.delete();
@@ -312,6 +358,7 @@ class CkCanvas implements LayerCanvas {
312358
if (ckVertices == null) {
313359
return;
314360
}
361+
_tracker?.recordPaint(paint);
315362
final SkPaint skPaint = (paint as CkPaint).toSkPaint();
316363
skCanvas.drawVertices(ckVertices.skiaObject, toSkBlendMode(blendMode), skPaint);
317364
skPaint.delete();
@@ -339,6 +386,7 @@ class CkCanvas implements LayerCanvas {
339386

340387
@override
341388
void saveLayer(ui.Rect? bounds, ui.Paint paint) {
389+
_tracker?.recordPaint(paint);
342390
if (bounds == null) {
343391
saveLayerWithoutBounds(paint);
344392
} else {
@@ -361,6 +409,7 @@ class CkCanvas implements LayerCanvas {
361409

362410
@override
363411
void saveLayerWithFilter(ui.Rect? bounds, ui.Paint? paint, ui.ImageFilter filter) {
412+
_tracker?.recordPaint(paint);
364413
final EngineImageFilter engineFilter;
365414
if (filter is ui.ColorFilter) {
366415
engineFilter = EngineColorFilterImageFilter(colorFilter: filter as EngineColorFilter);
@@ -442,7 +491,6 @@ class CkCanvas implements LayerCanvas {
442491
ui.Paint paint,
443492
) {
444493
assert(colors == null || colors.isEmpty || blendMode != null);
445-
final SkImage skAtlas = _getSkImage(atlas);
446494

447495
final int rectCount = rects.length;
448496
if (transforms.length != rectCount) {
@@ -453,6 +501,9 @@ class CkCanvas implements LayerCanvas {
453501
'If non-null, "colors" length must match that of "transforms" and "rects".',
454502
);
455503
}
504+
_tracker?.recordImage(atlas);
505+
_tracker?.recordPaint(paint);
506+
final SkImage skAtlas = _getSkImage(atlas);
456507

457508
final rstTransformBuffer = Float32List(rectCount * 4);
458509
final rectBuffer = Float32List(rectCount * 4);
@@ -500,7 +551,6 @@ class CkCanvas implements LayerCanvas {
500551
ui.Paint paint,
501552
) {
502553
assert(colors == null || blendMode != null);
503-
final SkImage skAtlas = _getSkImage(atlas);
504554

505555
final int rectCount = rects.length;
506556
if (rstTransforms.length != rectCount) {
@@ -514,6 +564,9 @@ class CkCanvas implements LayerCanvas {
514564
'If non-null, "colors" length must be one fourth the length of "rstTransforms" and "rects".',
515565
);
516566
}
567+
_tracker?.recordImage(atlas);
568+
_tracker?.recordPaint(paint);
569+
final SkImage skAtlas = _getSkImage(atlas);
517570

518571
Uint32List? unsignedColors;
519572
if (colors != null) {

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ class CkUniqueRef<T extends JSObject> extends UniqueRef<T> {
2424

2525
/// Manages the lifecycle of a C++ object referenced by multiple Dart objects.
2626
class CkCountedRef<R extends StackTraceDebugger, T extends JSObject> extends CountedRef<R, T> {
27-
CkCountedRef(super.nativeObject, super.debugReferrer, super.debugLabel, {super.onDisposed})
28-
: super(
29-
onDispose: (T obj) {
30-
final deletable = obj as SkDeletable;
31-
if (!deletable.isDeleted()) {
32-
deletable.delete();
33-
}
34-
},
35-
);
27+
CkCountedRef(
28+
super.nativeObject,
29+
super.debugReferrer,
30+
super.debugLabel, {
31+
super.onDisposed,
32+
void Function(T)? onDispose,
33+
}) : super(
34+
onDispose: (T obj) {
35+
final deletable = obj as SkDeletable;
36+
if (!deletable.isDeleted()) {
37+
deletable.delete();
38+
}
39+
onDispose?.call(obj);
40+
},
41+
);
3642
}

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:ui/ui.dart' as ui;
88

99
import '../layer/layer_painting.dart';
1010
import '../primitives/image.dart';
11+
import '../primitives/image_source.dart';
1112
import '../util.dart';
1213
import 'canvas.dart';
1314
import 'canvaskit_api.dart';
@@ -18,19 +19,31 @@ import 'surface.dart';
1819

1920
/// Implements [ui.Picture] on top of [SkPicture].
2021
class CkPicture implements LayerPicture, StackTraceDebugger {
21-
CkPicture(SkPicture skPicture) : _isClone = false {
22-
_ref = CkCountedRef<CkPicture, SkPicture>(skPicture, this, 'Picture');
22+
CkPicture(SkPicture skPicture, [this.imageTracker]) : _isClone = false {
23+
_ref = CkCountedRef<CkPicture, SkPicture>(
24+
skPicture,
25+
this,
26+
'Picture',
27+
onDispose: imageTracker == null
28+
? null
29+
: (SkPicture _) {
30+
imageTracker!.releaseAll();
31+
},
32+
);
2333
_initStackTrace();
2434
}
2535

26-
CkPicture._clone(CkCountedRef<CkPicture, SkPicture> ref) : _isClone = true {
36+
CkPicture._clone(CkCountedRef<CkPicture, SkPicture> ref, this.imageTracker) : _isClone = true {
2737
_ref = ref;
2838
ref.ref(this);
2939
_initStackTrace();
3040
}
3141

3242
final bool _isClone;
3343

44+
/// Retained image sources for images recorded onto this picture.
45+
final PictureImageTracker? imageTracker;
46+
3447
late final CkCountedRef<CkPicture, SkPicture> _ref;
3548

3649
SkPicture get skiaObject => _ref.nativeObject;
@@ -91,6 +104,9 @@ class CkPicture implements LayerPicture, StackTraceDebugger {
91104
@override
92105
void dispose() {
93106
assert(debugCheckNotDisposed('Cannot dispose picture.'));
107+
if (_isDisposed) {
108+
return;
109+
}
94110
assert(() {
95111
_debugDisposalStackTrace = StackTrace.current;
96112
return true;
@@ -152,7 +168,7 @@ class CkPicture implements LayerPicture, StackTraceDebugger {
152168

153169
@override
154170
LayerPicture clone() {
155-
return CkPicture._clone(_ref);
171+
return CkPicture._clone(_ref, imageTracker);
156172
}
157173

158174
void _initStackTrace() {

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture_recorder.dart‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,41 @@
44

55
import 'dart:typed_data';
66

7+
import 'package:meta/meta.dart';
78
import 'package:ui/src/engine.dart';
89
import 'package:ui/ui.dart' as ui;
910

11+
/// Records a sequence of canvas drawing commands into a [CkPicture].
12+
///
13+
/// Manages a [PictureImageTracker] that retains any [ImageSource] instances
14+
/// drawn to the recording canvas until the resulting picture is finished and
15+
/// subsequently disposed.
16+
///
17+
/// If a recorder is abandoned and collected by garbage collection without
18+
/// [endRecording] being called, [finalizer] releases all retained image
19+
/// sources to prevent memory leaks.
1020
class CkPictureRecorder implements LayerPictureRecorder {
21+
@visibleForTesting
22+
static Finalizer finalizer = NativeMemoryFinalizer((Object tracker) {
23+
(tracker as PictureImageTracker).releaseAll();
24+
});
25+
1126
SkPictureRecorder? _skRecorder;
1227
CkCanvas? _recordingCanvas;
1328

29+
/// Tracks [ImageSource] references for draw operations recorded on this recorder.
30+
final PictureImageTracker tracker = PictureImageTracker();
31+
32+
/// Begins recording canvas commands within the specified [bounds].
1433
CkCanvas beginRecording(ui.Rect bounds) {
1534
final SkPictureRecorder recorder = _skRecorder = SkPictureRecorder();
1635
final Float32List skRect = toSkRect(bounds);
1736
final SkCanvas skCanvas = recorder.beginRecording(skRect);
18-
return _recordingCanvas = CkCanvas.fromSkCanvas(skCanvas);
37+
finalizer.attach(this, tracker, detach: this);
38+
return _recordingCanvas = CkCanvas.fromSkCanvas(skCanvas, recorder: this, tracker: tracker);
1939
}
2040

41+
/// The active recording canvas, or null if not recording.
2142
CkCanvas? get recordingCanvas => _recordingCanvas;
2243

2344
@override
@@ -28,10 +49,16 @@ class CkPictureRecorder implements LayerPictureRecorder {
2849
throw StateError('PictureRecorder is not recording');
2950
}
3051

52+
// Detach the unended-recording finalizer now that ownership of the tracker
53+
// is being handed off to the resulting CkPicture.
54+
finalizer.detach(this);
55+
_recordingCanvas?.clearTracker();
56+
_recordingCanvas = null;
3157
final SkPicture skPicture = recorder.finishRecordingAsPicture();
3258
recorder.delete();
3359
_skRecorder = null;
34-
final result = CkPicture(skPicture);
60+
final PictureImageTracker? imageTracker = tracker.isEmpty ? null : tracker;
61+
final result = CkPicture(skPicture, imageTracker);
3562
// We invoke the handler here, not in the picture constructor, because we want
3663
// [result.approximateBytesUsed] to be available for the handler.
3764
ui.Picture.onCreate?.call(result);

0 commit comments

Comments
 (0)