Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
mdebbar committed Dec 16, 2024
1 parent f2912c6 commit 0d6b4b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/web_ui/lib/src/engine/canvaskit/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:js_interop';
import 'dart:math' as math;
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
Expand Down Expand Up @@ -658,10 +659,13 @@ sealed class ImageSource {
/// image source.
int refCount = 0;

@visibleForTesting
bool debugIsClosed = false;

void close() {
print('Closing with refs = $refCount');
if (refCount == 0) {
_doClose();
debugIsClosed = true;
}
}

Expand Down Expand Up @@ -716,7 +720,6 @@ class ImageBitmapImageSource extends ImageSource {

@override
void _doClose() {
print('!!!!!!!');
imageBitmap.close();
}

Expand Down
6 changes: 6 additions & 0 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ extension DomWindowExtension on DomWindow {
/// The Trusted Types API (when available).
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
external DomTrustedTypePolicyFactory? get trustedTypes;

@JS('createImageBitmap')
external JSPromise<JSAny?> _createImageBitmap(DomImageData source);
Future<DomImageBitmap> createImageBitmap(DomImageData source) {
return js_util.promiseToFuture<DomImageBitmap>(_createImageBitmap(source));
}
}

typedef DomRequestAnimationFrameCallback = void Function(JSNumber highResTime);
Expand Down
30 changes: 27 additions & 3 deletions lib/web_ui/test/canvaskit/image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import 'dart:typed_data';

import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine/canvaskit/image.dart';
import 'package:ui/src/engine/image_decoder.dart';
import 'package:ui/src/engine/util.dart';
import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;

import 'common.dart';
import 'test_data.dart';

void main() {
internalBootstrapBrowserTest(() => testMain);
Expand Down Expand Up @@ -131,6 +130,31 @@ void testMain() {

expect(activeImages.length, 0);
});

test('CkImage does not close image source too early', () async {
final ImageSource imageSource = ImageBitmapImageSource(
await domWindow.createImageBitmap(createBlankDomImageData(4, 4)),
);

final SkImage skImage1 = canvasKit.MakeAnimatedImageFromEncoded(k4x4PngImage)!.makeImageAtCurrentFrame();
final CkImage image1 = CkImage(skImage1, imageSource: imageSource);

final SkImage skImage2 = canvasKit.MakeAnimatedImageFromEncoded(k4x4PngImage)!.makeImageAtCurrentFrame();
final CkImage image2 = CkImage(skImage2, imageSource: imageSource);

final CkImage image3 = image1.clone();

expect(imageSource.debugIsClosed, isFalse);

image1.dispose();
expect(imageSource.debugIsClosed, isFalse);

image2.dispose();
expect(imageSource.debugIsClosed, isFalse);

image3.dispose();
expect(imageSource.debugIsClosed, isTrue);
});
}

Future<ui.Image> _createImage() => _createPicture().toImage(10, 10);
Expand Down

0 comments on commit 0d6b4b5

Please sign in to comment.