Initialize skwasm codecs before handing them back to the user. (flutter/engine#43274)

Benchmarks were failing because the code was reading the `frameCount` and `repetitionCount` before reading any frames out of the codec. The codec gets implicitly initialized when you read a frame, but we should return it to the user initialized so that `frameCount` and `repetitionCount` work even if you haven't read a frame yet. This is consistent with how CanvasKit's codec works.

Also, modified our unit tests so that they exercise the codecs in this way.
This commit is contained in:
Jackson Gardner
2023-06-27 13:17:30 -07:00
committed by GitHub
parent 7f00ce647f
commit 3a44bbda74
3 changed files with 8 additions and 2 deletions

View File

@@ -369,11 +369,12 @@ class SkwasmRenderer implements Renderer {
if (contentType == null) {
throw Exception('Could not determine content type of image from data');
}
final ui.Codec baseDecoder = SkwasmImageDecoder(
final SkwasmImageDecoder baseDecoder = SkwasmImageDecoder(
contentType: contentType,
dataSource: list.toJS,
debugSource: 'encoded image bytes',
);
await baseDecoder.initialize();
if (targetWidth == null && targetHeight == null) {
return baseDecoder;
}
@@ -395,11 +396,13 @@ class SkwasmRenderer implements Renderer {
if (contentType == null) {
throw Exception('Could not determine content type of image at url $uri');
}
return SkwasmImageDecoder(
final SkwasmImageDecoder decoder = SkwasmImageDecoder(
contentType: contentType,
dataSource: response.body as JSAny,
debugSource: uri.toString(),
);
await decoder.initialize();
return decoder;
}
@override

View File

@@ -29,6 +29,7 @@ Future<void> testMain() async {
final ui.Codec codec = await renderer.instantiateImageCodecFromUrl(
Uri(path: '/test_images/mandrill_128.png')
);
expect(codec.frameCount, 1);
final ui.FrameInfo info = await codec.getNextFrame();
final ui.Image image = info.image;

View File

@@ -288,6 +288,7 @@ Future<void> testMain() async {
final ui.Codec codec = await renderer.instantiateImageCodecFromUrl(
Uri(path: '/test_images/mandrill_128.png')
);
expect(codec.frameCount, 1);
final ui.FrameInfo info = await codec.getNextFrame();
return info.image;
@@ -300,6 +301,7 @@ Future<void> testMain() async {
targetWidth: 150,
targetHeight: 150,
);
expect(codec.frameCount, 1);
final ui.FrameInfo info = await codec.getNextFrame();
return info.image;