From 6380075503705b5ad730caf9159caba0be366b9d Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Mon, 15 May 2023 15:54:22 -0700 Subject: [PATCH] Adds drawn image wide gamut test (#126715) integration test for https://github.com/flutter/engine/pull/41994 issue https://github.com/flutter/flutter/issues/126620 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../integration_test/app_test.dart | 9 ++++ .../wide_gamut_test/lib/main.dart | 49 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart b/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart index 6f402e1dbe..834a54b06b 100644 --- a/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart +++ b/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart @@ -162,5 +162,14 @@ void main() { expect(_findColor(result, _deepRed), isTrue); expect(_findColor(result, [0.0, 1.0, 0.0]), isTrue); }); + testWidgets('draw image with wide gamut works', (WidgetTester tester) async { + app.run(app.Setup.drawnImage); + await tester.pumpAndSettle(const Duration(seconds: 2)); + + const MethodChannel channel = MethodChannel('flutter/screenshot'); + final List result = + await channel.invokeMethod('test') as List; + expect(_findColor(result, [0.0, 1.0, 0.0]), isTrue); + }); }); } diff --git a/dev/integration_tests/wide_gamut_test/lib/main.dart b/dev/integration_tests/wide_gamut_test/lib/main.dart index 0663c8a772..696e8a3429 100644 --- a/dev/integration_tests/wide_gamut_test/lib/main.dart +++ b/dev/integration_tests/wide_gamut_test/lib/main.dart @@ -2,7 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async' show Completer; import 'dart:convert' show base64Decode; +import 'dart:typed_data' show ByteData, Uint8List; import 'dart:ui' as ui; import 'package:flutter/material.dart'; @@ -135,13 +137,14 @@ const String _displayP3Logo = 'gr3yrjmlwqXLjmWw1O2I5Wmp9Xxjyh+AVIZ6ADIAqcwClakzeMgApDILVKbO4CED' 'kMosUJk6g4dUBuRfvf1am9VRqzYAAAAASUVORK5CYII='; -void main() => run(Setup.blur); +void main() => run(Setup.drawnImage); enum Setup { none, image, canvasSaveLayer, blur, + drawnImage, } void run(Setup setup) { @@ -196,6 +199,42 @@ class _SaveLayerDrawer extends CustomPainter { bool shouldRepaint(covariant CustomPainter oldDelegate) => true; } +Future _drawImage() async { + final ui.PictureRecorder recorder = ui.PictureRecorder(); + const Size markerSize = Size(120, 120); + final double canvasSize = markerSize.height + 3; + final Canvas canvas = Canvas( + recorder, + Rect.fromLTWH(0, 0, canvasSize, canvasSize), + ); + + final Paint ovalPaint = Paint()..color = const Color(0xff00ff00); + final Path ovalPath = Path() + ..addOval(Rect.fromLTWH( + (canvasSize - markerSize.width) / 2, + 1, + markerSize.width, + markerSize.height, + )); + canvas.drawPath(ovalPath, ovalPaint); + + final ui.Picture picture = recorder.endRecording(); + final ui.Image image = await picture.toImage( + canvasSize.toInt(), + (canvasSize + 0).toInt(), + ); + final ByteData? byteData = + await image.toByteData(format: ui.ImageByteFormat.rawExtendedRgba128); + final Completer completer = Completer(); + ui.decodeImageFromPixels(Uint8List.view(byteData!.buffer), + canvasSize.toInt(), + canvasSize.toInt(), + ui.PixelFormat.rgbaFloat32, (ui.Image image) { + completer.complete(image); + }); + return completer.future; +} + Future _loadImage() async { final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(base64Decode(_displayP3Logo)); @@ -226,6 +265,12 @@ class _MyHomePageState extends State { _image = value; }); }); + } else if (widget.setup == Setup.drawnImage) { + _drawImage().then((ui.Image? value) { + setState(() { + _image = value; + }); + }); } super.initState(); } @@ -238,6 +283,8 @@ class _MyHomePageState extends State { imageWidget = Container(); case Setup.image: imageWidget = Image.memory(base64Decode(_displayP3Logo)); + case Setup.drawnImage: + imageWidget = CustomPaint(painter: _SaveLayerDrawer(_image)); case Setup.canvasSaveLayer: imageWidget = CustomPaint(painter: _SaveLayerDrawer(_image)); case Setup.blur: