Expose picture.debugDisposed, make sure we do not crash when working with disposed pictures (flutter/engine#31453)
This commit is contained in:
@@ -718,6 +718,7 @@ class SceneBuilder extends NativeFieldWrapperClass1 {
|
||||
bool isComplexHint = false,
|
||||
bool willChangeHint = false,
|
||||
}) {
|
||||
assert(!picture.debugDisposed);
|
||||
final int hints = (isComplexHint ? 1 : 0) | (willChangeHint ? 2 : 0);
|
||||
_addPicture(offset.dx, offset.dy, picture, hints);
|
||||
}
|
||||
|
||||
@@ -266,12 +266,20 @@ void SceneBuilder::addPicture(double dx,
|
||||
double dy,
|
||||
Picture* picture,
|
||||
int hints) {
|
||||
if (!picture) {
|
||||
// Picture::dispose was called and it has been collected.
|
||||
return;
|
||||
}
|
||||
|
||||
// Explicitly check for both conditions, since the picture object might have
|
||||
// been disposed but not collected yet, but the display list and picture are
|
||||
// both null.
|
||||
if (picture->picture()) {
|
||||
auto layer = std::make_unique<flutter::PictureLayer>(
|
||||
SkPoint::Make(dx, dy), UIDartState::CreateGPUObject(picture->picture()),
|
||||
!!(hints & 1), !!(hints & 2));
|
||||
AddLayer(std::move(layer));
|
||||
} else {
|
||||
} else if (picture->display_list()) {
|
||||
auto layer = std::make_unique<flutter::DisplayListLayer>(
|
||||
SkPoint::Make(dx, dy),
|
||||
UIDartState::CreateGPUObject(picture->display_list()), !!(hints & 1),
|
||||
|
||||
@@ -5113,6 +5113,7 @@ class Picture extends NativeFieldWrapperClass1 {
|
||||
/// Although the image is returned synchronously, the picture is actually
|
||||
/// rasterized the first time the image is drawn and then cached.
|
||||
Future<Image> toImage(int width, int height) {
|
||||
assert(!_disposed);
|
||||
if (width <= 0 || height <= 0)
|
||||
throw Exception('Invalid image dimensions.');
|
||||
return _futurize(
|
||||
@@ -5130,7 +5131,31 @@ class Picture extends NativeFieldWrapperClass1 {
|
||||
|
||||
/// Release the resources used by this object. The object is no longer usable
|
||||
/// after this method is called.
|
||||
void dispose() native 'Picture_dispose';
|
||||
void dispose() {
|
||||
assert(!_disposed);
|
||||
assert(() {
|
||||
_disposed = true;
|
||||
return true;
|
||||
}());
|
||||
_dispose();
|
||||
}
|
||||
|
||||
void _dispose() native 'Picture_dispose';
|
||||
|
||||
|
||||
bool _disposed = false;
|
||||
/// Whether this reference to the underlying picture is [dispose]d.
|
||||
///
|
||||
/// This only returns a valid value if asserts are enabled, and must not be
|
||||
/// used otherwise.
|
||||
bool get debugDisposed {
|
||||
bool? disposed;
|
||||
assert(() {
|
||||
disposed = _disposed;
|
||||
return true;
|
||||
}());
|
||||
return disposed ?? (throw StateError('Picture.debugDisposed is only available when asserts are enabled.'));
|
||||
}
|
||||
|
||||
/// Returns the approximate number of bytes allocated for this object.
|
||||
///
|
||||
|
||||
@@ -153,6 +153,7 @@ abstract class Canvas {
|
||||
abstract class Picture {
|
||||
Future<Image> toImage(int width, int height);
|
||||
void dispose();
|
||||
bool get debugDisposed;
|
||||
int get approximateBytesUsed;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../profiler.dart';
|
||||
import '../util.dart';
|
||||
import 'canvas.dart';
|
||||
import 'canvaskit_api.dart';
|
||||
import 'image.dart';
|
||||
@@ -30,10 +31,13 @@ class CkPicture extends ManagedSkiaObject<SkPicture> implements ui.Picture {
|
||||
@override
|
||||
int get approximateBytesUsed => 0;
|
||||
|
||||
/// Whether the picture has been disposed of.
|
||||
///
|
||||
/// This is indended to be used in tests and assertions only.
|
||||
bool get debugIsDisposed => _isDisposed;
|
||||
@override
|
||||
bool get debugDisposed {
|
||||
if (assertionsEnabled) {
|
||||
return _isDisposed;
|
||||
}
|
||||
throw StateError('Picture.debugDisposed is only available when asserts are enabled.');
|
||||
}
|
||||
|
||||
/// This is set to true when [dispose] is called and is never reset back to
|
||||
/// false.
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'package:ui/ui.dart' as ui;
|
||||
import 'html/bitmap_canvas.dart';
|
||||
import 'html/recording_canvas.dart';
|
||||
import 'html_image_codec.dart';
|
||||
import 'util.dart';
|
||||
|
||||
/// An implementation of [ui.PictureRecorder] backed by a [RecordingCanvas].
|
||||
class EnginePictureRecorder implements ui.PictureRecorder {
|
||||
@@ -75,8 +76,21 @@ class EnginePicture implements ui.Picture {
|
||||
return onImageLoaded.future;
|
||||
}
|
||||
|
||||
bool _disposed = false;
|
||||
|
||||
@override
|
||||
void dispose() {}
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
@override
|
||||
bool get debugDisposed {
|
||||
if (assertionsEnabled) {
|
||||
return _disposed;
|
||||
}
|
||||
throw StateError('Picture.debugDisposed is only available when asserts are enabled.');
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
int get approximateBytesUsed => 0;
|
||||
|
||||
@@ -27,11 +27,11 @@ void testMain() {
|
||||
canvas.drawPaint(ui.Paint());
|
||||
final CkPicture picture = recorder.endRecording() as CkPicture;
|
||||
expect(picture.rawSkiaObject, isNotNull);
|
||||
expect(picture.debugIsDisposed, isFalse);
|
||||
expect(picture.debugDisposed, isFalse);
|
||||
picture.debugCheckNotDisposed('Test.'); // must not throw
|
||||
picture.dispose();
|
||||
expect(picture.rawSkiaObject, isNull);
|
||||
expect(picture.debugIsDisposed, isTrue);
|
||||
expect(picture.debugDisposed, isTrue);
|
||||
|
||||
StateError? actualError;
|
||||
try {
|
||||
@@ -79,7 +79,7 @@ void testMain() {
|
||||
|
||||
// Deletion is softer than disposal. An object may still be resurrected
|
||||
// if it was deleted prematurely.
|
||||
expect(picture.debugIsDisposed, isFalse);
|
||||
expect(picture.debugDisposed, isFalse);
|
||||
expect(picture.resurrect(), isNotNull);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,6 +8,35 @@ import 'dart:ui';
|
||||
import 'package:litetest/litetest.dart';
|
||||
|
||||
void main() {
|
||||
test('addPicture with disposed picture does not crash', () {
|
||||
bool assertsEnabled = false;
|
||||
assert(() {
|
||||
assertsEnabled = true;
|
||||
return true;
|
||||
}());
|
||||
final PictureRecorder recorder = PictureRecorder();
|
||||
final Canvas canvas = Canvas(recorder);
|
||||
canvas.drawPaint(Paint());
|
||||
final Picture picture = recorder.endRecording();
|
||||
picture.dispose();
|
||||
|
||||
assert(picture.debugDisposed);
|
||||
|
||||
final SceneBuilder builder = SceneBuilder();
|
||||
if (assertsEnabled) {
|
||||
expect(
|
||||
() => builder.addPicture(Offset.zero, picture),
|
||||
throwsA(isInstanceOf<AssertionError>()),
|
||||
);
|
||||
} else {
|
||||
builder.addPicture(Offset.zero, picture);
|
||||
}
|
||||
|
||||
final Scene scene = builder.build();
|
||||
expect(scene != null, true);
|
||||
scene.dispose();
|
||||
});
|
||||
|
||||
test('pushTransform validates the matrix', () {
|
||||
final SceneBuilder builder = SceneBuilder();
|
||||
final Float64List matrix4 = Float64List.fromList(<double>[
|
||||
|
||||
Reference in New Issue
Block a user