[web] Support raw straight RGBA format in Image.toByteData() in CanvasKit (flutter/engine#28339)

This commit is contained in:
ColdPaleLight
2021-08-28 19:36:01 +08:00
committed by GitHub
parent b0a38b9129
commit 90fdd72297
3 changed files with 19 additions and 3 deletions

View File

@@ -308,10 +308,11 @@ class CkImage implements ui.Image, StackTraceDebugger {
ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba,
}) {
assert(_debugCheckIsNotDisposed());
final SkAlphaType alphaType = format == ui.ImageByteFormat.rawStraightRgba ? canvasKit.AlphaType.Unpremul : canvasKit.AlphaType.Premul;
final ByteData? data = _encodeImage(
skImage: skImage,
format: format,
alphaType: canvasKit.AlphaType.Premul,
alphaType: alphaType,
colorType: canvasKit.ColorType.RGBA_8888,
colorSpace: SkColorSpaceSRGB,
);
@@ -331,7 +332,7 @@ class CkImage implements ui.Image, StackTraceDebugger {
}) {
Uint8List? bytes;
if (format == ui.ImageByteFormat.rawRgba) {
if (format == ui.ImageByteFormat.rawRgba || format == ui.ImageByteFormat.rawStraightRgba) {
final SkImageInfo imageInfo = SkImageInfo(
alphaType: alphaType,
colorType: colorType,

View File

@@ -180,6 +180,10 @@ class HtmlImage implements ui.Image {
ctx.drawImage(imgElement, 0, 0);
final html.ImageData imageData = ctx.getImageData(0, 0, width, height);
return Future<ByteData?>.value(imageData.data.buffer.asByteData());
} else if (format == ui.ImageByteFormat.rawStraightRgba) {
// TODO(ColdPaleLight): https://github.com/flutter/flutter/issues/89094
throw UnsupportedError(
'Image.toByteData(format: ui.ImageByteFormat.rawStraightRgba) not yet implemented for HTML');
}
if (imgElement.src?.startsWith('data:') == true) {
final UriData data = UriData.fromUri(Uri.parse(imgElement.src!));

View File

@@ -1195,7 +1195,7 @@ void _canvasTests() {
.beginRecording(Float32List.fromList(<double>[0, 0, 1, 1]));
otherCanvas.drawRect(
Float32List.fromList(<double>[0, 0, 1, 1]),
SkPaint(),
SkPaint()..setColorInt(0xAAFFFFFF),
);
final CkPicture picture =
CkPicture(otherRecorder.finishRecordingAsPicture(), null, null);
@@ -1203,6 +1203,17 @@ void _canvasTests() {
final ByteData rawData =
await image.toByteData(format: ui.ImageByteFormat.rawRgba);
expect(rawData.lengthInBytes, greaterThan(0));
expect(
rawData.buffer.asUint32List(),
<int>[0xAAAAAAAA],
);
final ByteData rawStraightData =
await image.toByteData(format: ui.ImageByteFormat.rawStraightRgba);
expect(rawStraightData.lengthInBytes, greaterThan(0));
expect(
rawStraightData.buffer.asUint32List(),
<int>[0xAAFFFFFF],
);
final ByteData pngData =
await image.toByteData(format: ui.ImageByteFormat.png);
expect(pngData.lengthInBytes, greaterThan(0));