From a347148d5a6fc487d49d0072fc24825ba9e54ffc Mon Sep 17 00:00:00 2001 From: Yegor Date: Tue, 29 Sep 2020 12:17:03 -0700 Subject: [PATCH] fix EnginePicture.toImage; implement rawRgba toByteData; add test (flutter/engine#21370) --- .../web_ui/lib/src/engine/bitmap_canvas.dart | 4 +- .../web_ui/lib/src/engine/canvas_pool.dart | 4 +- .../lib/src/engine/html_image_codec.dart | 9 +++ .../lib/web_ui/lib/src/engine/picture.dart | 18 ++++-- .../test/engine/image_to_byte_data_test.dart | 50 +++++++++++++++++ .../engine/canvas_to_picture_test.dart | 55 ------------------- 6 files changed, 76 insertions(+), 64 deletions(-) create mode 100644 engine/src/flutter/lib/web_ui/test/engine/image_to_byte_data_test.dart delete mode 100644 engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_to_picture_test.dart diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart index a9d0df4975..0f054559b1 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart @@ -201,8 +201,8 @@ class BitmapCanvas extends EngineCanvas { return _devicePixelRatio == EngineWindow.browserDevicePixelRatio; } - /// Returns a data URI containing a representation of the image in this - /// canvas. + /// Returns a "data://" URI containing a representation of the image in this + /// canvas in PNG format. String toDataUrl() { return _canvasPool.toDataUrl(); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart index 1cf369b63f..d56351124e 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart @@ -319,8 +319,8 @@ class _CanvasPool extends _SaveStackTracking { } } - // Returns a data URI containing a representation of the image in this - // canvas. + // Returns a "data://" URI containing a representation of the image in this + // canvas in PNG format. String toDataUrl() => _canvas!.toDataUrl(); @override diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart index 6a6a7e6aaf..3b434685f2 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart @@ -139,6 +139,15 @@ class HtmlImage implements ui.Image { @override Future toByteData({ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba}) { + if (format == ui.ImageByteFormat.rawRgba) { + final html.CanvasElement canvas = html.CanvasElement() + ..width = width + ..height = height; + final html.CanvasRenderingContext2D ctx = canvas.context2D; + ctx.drawImage(imgElement, 0, 0); + final html.ImageData imageData = ctx.getImageData(0, 0, width, height); + return Future.value(imageData.data.buffer.asByteData()); + } if (imgElement.src?.startsWith('data:') == true) { final data = UriData.fromUri(Uri.parse(imgElement.src!)); return Future.value(data.contentAsBytes().buffer.asByteData()); diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart index 45bf816ce5..946f348a66 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart @@ -54,11 +54,19 @@ class EnginePicture implements ui.Picture { ..src = imageDataUrl ..width = width ..height = height; - return HtmlImage( - imageElement, - width, - height, - ); + + // The image loads asynchronously. We need to wait before returning, + // otherwise the returned HtmlImage will be temporarily unusable. + final Completer onImageLoaded = Completer.sync(); + imageElement.onError.first.then(onImageLoaded.completeError); + imageElement.onLoad.first.then((_) { + onImageLoaded.complete(HtmlImage( + imageElement, + width, + height, + )); + }); + return onImageLoaded.future; } @override diff --git a/engine/src/flutter/lib/web_ui/test/engine/image_to_byte_data_test.dart b/engine/src/flutter/lib/web_ui/test/engine/image_to_byte_data_test.dart new file mode 100644 index 0000000000..14140a4af1 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/engine/image_to_byte_data_test.dart @@ -0,0 +1,50 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// @dart = 2.6 +import 'dart:typed_data'; + +import 'package:ui/ui.dart'; +import 'package:ui/src/engine.dart'; + +import 'package:test/bootstrap/browser.dart'; +import 'package:test/test.dart'; + +void main() { + internalBootstrapBrowserTest(() => testMain); +} + +void testMain() async { + setUp(() async { + await webOnlyInitializePlatform(); + webOnlyFontCollection.debugRegisterTestFonts(); + await webOnlyFontCollection.ensureFontsLoaded(); + }); + + test('Picture.toImage().toByteData()', () async { + final EnginePictureRecorder recorder = PictureRecorder(); + final RecordingCanvas canvas = + recorder.beginRecording(Rect.fromLTRB(0, 0, 2, 2)); + canvas.drawColor(Color(0xFFCCDD00), BlendMode.srcOver); + final Picture testPicture = recorder.endRecording(); + final Image testImage = await testPicture.toImage(2, 2); + final ByteData bytes = + await testImage.toByteData(format: ImageByteFormat.rawRgba); + expect( + bytes.buffer.asUint32List(), + [0xFF00DDCC, 0xFF00DDCC, 0xFF00DDCC, 0xFF00DDCC], + ); + + final ByteData pngBytes = + await testImage.toByteData(format: ImageByteFormat.png); + + // PNG-encoding is browser-specific, but the header is standard. We only + // test the header. + final List pngHeader = [137, 80, 78, 71, 13, 10, 26, 10]; + expect( + pngBytes.buffer.asUint8List().sublist(0, pngHeader.length), + pngHeader, + ); + }); +} diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_to_picture_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_to_picture_test.dart deleted file mode 100644 index 02c5a9c16b..0000000000 --- a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_to_picture_test.dart +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// @dart = 2.6 -import 'dart:html' as html; - -import 'package:ui/ui.dart'; -import 'package:ui/src/engine.dart'; - -import 'package:test/bootstrap/browser.dart'; -import 'package:test/test.dart'; - -void main() { - internalBootstrapBrowserTest(() => testMain); -} - -void testMain() async { - final Rect region = Rect.fromLTWH(0, 0, 500, 500); - - setUp(() async { - debugShowClipLayers = true; - SurfaceSceneBuilder.debugForgetFrameScene(); - for (html.Node scene in html.document.querySelectorAll('flt-scene')) { - scene.remove(); - } - - await webOnlyInitializePlatform(); - webOnlyFontCollection.debugRegisterTestFonts(); - await webOnlyFontCollection.ensureFontsLoaded(); - }); - - test('Convert Canvas to Picture', () async { - final SurfaceSceneBuilder builder = SurfaceSceneBuilder(); - final Picture testPicture = await _drawTestPictureWithCircle(region); - builder.addPicture(Offset.zero, testPicture); - - html.document.body.append(builder - .build() - .webOnlyRootElement); - - //await matchGoldenFile('canvas_to_picture.png', region: region, write: true); - }); -} - -Picture _drawTestPictureWithCircle(Rect region) { - final EnginePictureRecorder recorder = PictureRecorder(); - final RecordingCanvas canvas = recorder.beginRecording(region); - canvas.drawOval( - region, - Paint() - ..style = PaintingStyle.fill - ..color = Color(0xFF00FF00)); - return recorder.endRecording(); -}