Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds drawn image wide gamut test #126715

Merged
merged 3 commits into from May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -162,5 +162,14 @@ void main() {
expect(_findColor(result, _deepRed), isTrue);
expect(_findColor(result, <double>[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<Object?> result =
await channel.invokeMethod('test') as List<Object?>;
expect(_findColor(result, <double>[0.0, 1.0, 0.0]), isTrue);
});
});
}
49 changes: 48 additions & 1 deletion dev/integration_tests/wide_gamut_test/lib/main.dart
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -196,6 +199,42 @@ class _SaveLayerDrawer extends CustomPainter {
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}

Future<ui.Image> _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<ui.Image> completer = Completer<ui.Image>();
ui.decodeImageFromPixels(Uint8List.view(byteData!.buffer),
canvasSize.toInt(),
canvasSize.toInt(),
ui.PixelFormat.rgbaFloat32, (ui.Image image) {
completer.complete(image);
});
return completer.future;
}

Future<ui.Image> _loadImage() async {
final ui.ImmutableBuffer buffer =
await ui.ImmutableBuffer.fromUint8List(base64Decode(_displayP3Logo));
Expand Down Expand Up @@ -226,6 +265,12 @@ class _MyHomePageState extends State<MyHomePage> {
_image = value;
});
});
} else if (widget.setup == Setup.drawnImage) {
_drawImage().then((ui.Image? value) {
setState(() {
_image = value;
});
});
}
super.initState();
}
Expand All @@ -238,6 +283,8 @@ class _MyHomePageState extends State<MyHomePage> {
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:
Expand Down