Skip to content

Commit

Permalink
Initialize skwasm codecs before handing them back to the user. (flutt…
Browse files Browse the repository at this point in the history
…er#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.
  • Loading branch information
eyebrowsoffire authored Jun 27, 2023
1 parent 6f9cc07 commit 0e020c2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/web_ui/lib/src/engine/skwasm/skwasm_impl/renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/web_ui/test/ui/filters_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions lib/web_ui/test/ui/image_golden_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 0e020c2

Please sign in to comment.