Better handle image codec instantiation failure (flutter/engine#22809)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
@@ -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(
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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';
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user