[web] implement decodeImageFromPixels for CanvasKit (flutter/engine#29223)
* [web] implement decodeImageFromPixels for CanvasKit
This commit is contained in:
@@ -2118,9 +2118,9 @@ class SkImageInfo {
|
||||
external factory SkImageInfo({
|
||||
required int width,
|
||||
required int height,
|
||||
SkAlphaType alphaType,
|
||||
ColorSpace colorSpace,
|
||||
SkColorType colorType,
|
||||
required SkColorType colorType,
|
||||
required SkAlphaType alphaType,
|
||||
required ColorSpace colorSpace,
|
||||
});
|
||||
external SkAlphaType get alphaType;
|
||||
external ColorSpace get colorSpace;
|
||||
|
||||
@@ -19,6 +19,36 @@ ui.Codec skiaInstantiateImageCodec(Uint8List list,
|
||||
return CkAnimatedImage.decodeFromBytes(list, 'encoded image bytes');
|
||||
}
|
||||
|
||||
// TODO(yjbanov): add support for targetWidth/targetHeight (https://github.com/flutter/flutter/issues/34075)
|
||||
void skiaDecodeImageFromPixels(
|
||||
Uint8List pixels,
|
||||
int width,
|
||||
int height,
|
||||
ui.PixelFormat format,
|
||||
ui.ImageDecoderCallback callback, {
|
||||
int? rowBytes,
|
||||
int? targetWidth,
|
||||
int? targetHeight,
|
||||
bool allowUpscaling = true,
|
||||
}) {
|
||||
// Run in a timer to avoid janking the current frame by moving the decoding
|
||||
// work outside the frame event.
|
||||
Timer.run(() {
|
||||
final SkImage skImage = canvasKit.MakeImage(
|
||||
SkImageInfo(
|
||||
width: width,
|
||||
height: height,
|
||||
colorType: format == ui.PixelFormat.rgba8888 ? canvasKit.ColorType.RGBA_8888 : canvasKit.ColorType.BGRA_8888,
|
||||
alphaType: canvasKit.AlphaType.Premul,
|
||||
colorSpace: SkColorSpaceSRGB,
|
||||
),
|
||||
pixels,
|
||||
rowBytes ?? 4 * width,
|
||||
);
|
||||
return callback(CkImage(skImage));
|
||||
});
|
||||
}
|
||||
|
||||
/// Thrown when the web engine fails to decode an image, either due to a
|
||||
/// network issue, corrupted image contents, or missing codec.
|
||||
class ImageCodecException implements Exception {
|
||||
|
||||
@@ -585,15 +585,17 @@ void decodeImageFromPixels(
|
||||
bool allowUpscaling = true,
|
||||
}) {
|
||||
if (engine.useCanvasKit) {
|
||||
engine.skiaInstantiateImageCodec(
|
||||
engine.skiaDecodeImageFromPixels(
|
||||
pixels,
|
||||
width,
|
||||
height,
|
||||
format.index,
|
||||
rowBytes,
|
||||
).getNextFrame().then((FrameInfo info) {
|
||||
callback(info.image);
|
||||
});
|
||||
format,
|
||||
callback,
|
||||
rowBytes: rowBytes,
|
||||
targetWidth: targetWidth,
|
||||
targetHeight: targetHeight,
|
||||
allowUpscaling: allowUpscaling,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:html' as html;
|
||||
import 'dart:typed_data';
|
||||
|
||||
@@ -252,6 +253,32 @@ void testMain() {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('decodeImageFromPixels', () async {
|
||||
Future<ui.Image> _testDecodeFromPixels(int width, int height) async {
|
||||
final Completer<ui.Image> completer = Completer<ui.Image>();
|
||||
ui.decodeImageFromPixels(
|
||||
Uint8List.fromList(List<int>.filled(width * height * 4, 0, growable: false)),
|
||||
width,
|
||||
height,
|
||||
ui.PixelFormat.rgba8888,
|
||||
(ui.Image image) {
|
||||
completer.complete(image);
|
||||
},
|
||||
);
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
final ui.Image image1 = await _testDecodeFromPixels(10, 20);
|
||||
expect(image1, isNotNull);
|
||||
expect(image1.width, 10);
|
||||
expect(image1.height, 20);
|
||||
|
||||
final ui.Image image2 = await _testDecodeFromPixels(40, 100);
|
||||
expect(image2, isNotNull);
|
||||
expect(image2.width, 40);
|
||||
expect(image2.height, 100);
|
||||
});
|
||||
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/60040
|
||||
}, skip: isIosSafari);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user