Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle image codec instantiation failure #22809

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CanvasKit {
external SkTextDirectionEnum get TextDirection;
external SkFontWeightEnum get FontWeight;
external SkFontSlantEnum get FontSlant;
external SkAnimatedImage MakeAnimatedImageFromEncoded(Uint8List imageData);
external SkAnimatedImage? MakeAnimatedImageFromEncoded(Uint8List imageData);
external SkShaderNamespace get Shader;
external SkMaskFilterNamespace get MaskFilter;
external SkColorFilterNamespace get ColorFilter;
Expand Down
11 changes: 7 additions & 4 deletions lib/web_ui/lib/src/engine/canvaskit/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
part of engine;

/// Instantiates a [ui.Codec] backed by an `SkAnimatedImage` from Skia.
void skiaInstantiateImageCodec(Uint8List list, Callback<ui.Codec> callback,
ui.Codec skiaInstantiateImageCodec(Uint8List list,
[int? width, int? height, int? format, int? rowBytes]) {
final CkAnimatedImage codec = CkAnimatedImage.decodeFromBytes(list);
callback(codec);
return CkAnimatedImage.decodeFromBytes(list);
}

/// Instantiates a [ui.Codec] backed by an `SkAnimatedImage` from Skia after
Expand Down Expand Up @@ -49,7 +48,11 @@ class CkAnimatedImage extends ManagedSkiaObject<SkAnimatedImage> implements ui.C

@override
SkAnimatedImage createDefault() {
return canvasKit.MakeAnimatedImageFromEncoded(_bytes);
final SkAnimatedImage? animatedImage = canvasKit.MakeAnimatedImageFromEncoded(_bytes);
if (animatedImage == null) {
throw Exception('Failed to decode image');
}
return animatedImage;
}

@override
Expand Down
32 changes: 10 additions & 22 deletions lib/web_ui/lib/src/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,14 @@ Future<Codec> instantiateImageCodec(
int? targetWidth,
int? targetHeight,
bool allowUpscaling = true,
}) {
return _futurize<Codec>((engine.Callback<Codec> callback) =>
// TODO: Implement targetWidth and targetHeight support.
_instantiateImageCodec(list, callback));
}

String? _instantiateImageCodec(Uint8List list, engine.Callback<Codec> callback) {
}) async {
if (engine.useCanvasKit) {
engine.skiaInstantiateImageCodec(list, callback);
return null;
// TODO: Implement targetWidth and targetHeight support.
return engine.skiaInstantiateImageCodec(list);
} else {
final html.Blob blob = html.Blob(<dynamic>[list.buffer]);
return engine.HtmlBlobCodec(blob);
}
final html.Blob blob = html.Blob(<dynamic>[list.buffer]);
callback(engine.HtmlBlobCodec(blob));
return null;
}

Future<Codec> webOnlyInstantiateImageCodecFromUrl(Uri uri,
Expand Down Expand Up @@ -565,12 +559,9 @@ Future<Codec> _createBmp(
}
}

final Completer<Codec> codecCompleter = Completer<Codec>();
_instantiateImageCodec(
return instantiateImageCodec(
bmpData.buffer.asUint8List(),
(Codec codec) => codecCompleter.complete(codec),
);
return codecCompleter.future;
}

void decodeImageFromPixels(
Expand All @@ -587,16 +578,13 @@ void decodeImageFromPixels(
if (engine.useCanvasKit) {
engine.skiaInstantiateImageCodec(
pixels,
(Codec codec) {
codec.getNextFrame().then((FrameInfo info) {
callback(info.image);
});
},
width,
height,
format.index,
rowBytes,
);
).getNextFrame().then((FrameInfo info) {
callback(info.image);
});
return;
}

Expand Down
10 changes: 10 additions & 0 deletions lib/web_ui/test/canvaskit/image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// @dart = 2.6
import 'dart:html' show ProgressEvent;
import 'dart:typed_data';

import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -114,6 +115,15 @@ void testMain() {
expect((await image.toByteData(format: ui.ImageByteFormat.png)).lengthInBytes, greaterThan(0));
testCollector.collectNow();
});

test('Reports error when failing to decode image', () async {
try {
await ui.instantiateImageCodec(Uint8List(0));
fail('Expected to throw');
} on Exception catch (exception) {
expect(exception.toString(), 'Exception: Failed to decode image');
}
});
// TODO: https://github.com/flutter/flutter/issues/60040
}, skip: isIosSafari);
}