From 931be8696b353640964216094ca05aa42afe5817 Mon Sep 17 00:00:00 2001 From: Ferhat Date: Wed, 9 Oct 2019 15:51:31 -0700 Subject: [PATCH] [web] Implement basic radial gradient (TileMode.clamp, no transform) (flutter/engine#12811) * Add GradientRadial paintStyle implementation --- .../flutter/lib/web_ui/dev/goldens_lock.yaml | 2 +- .../lib/web_ui/lib/src/engine/shader.dart | 22 ++-- .../flutter/lib/web_ui/lib/src/ui/canvas.dart | 3 +- .../engine/radial_gradient_golden_test.dart | 100 ++++++++++++++++++ 4 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 engine/src/flutter/lib/web_ui/test/golden_tests/engine/radial_gradient_golden_test.dart diff --git a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml index 30c9fa9043..eb438eef2a 100644 --- a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml +++ b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml @@ -1,2 +1,2 @@ repository: https://github.com/flutter/goldens.git -revision: 8859d44913f7e63ff61f1a2662ec5eaa8708fe79 +revision: 7efcec3e8b0bbb6748a992b23a0a89300aa323c7 \ No newline at end of file diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/shader.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/shader.dart index 26e06838d0..fb591fa6f9 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/shader.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/shader.dart @@ -162,7 +162,8 @@ class GradientLinear extends EngineGradient { } } -// TODO(flutter_web): Add screenshot tests when infra is ready. +// TODO(flutter_web): For transforms and tile modes implement as webgl +// shader instead. See https://github.com/flutter/flutter/issues/32819 class GradientRadial extends EngineGradient { GradientRadial(this.center, this.radius, this.colors, this.colorStops, this.tileMode, this.matrix4) @@ -177,20 +178,25 @@ class GradientRadial extends EngineGradient { @override Object createPaintStyle(html.CanvasRenderingContext2D ctx) { - // TODO(flutter_web): see https://github.com/flutter/flutter/issues/32819 if (matrix4 != null && !Matrix4.fromFloat64List(matrix4).isIdentity()) { - throw UnimplementedError('matrix4 not supported in GradientRadial shader'); + throw UnimplementedError( + 'matrix4 not supported in GradientRadial shader'); } - final html.CanvasGradient gradient = - ctx.createRadialGradient(center.dx, center.dy, 0, center.dx, center.dy, radius); + if (tileMode != ui.TileMode.clamp) { + throw UnimplementedError( + 'TileMode not supported in GradientRadial shader'); + } + final html.CanvasGradient gradient = ctx.createRadialGradient( + center.dx, center.dy, 0, center.dx, center.dy, radius); if (colorStops == null) { assert(colors.length == 2); gradient.addColorStop(0, colors[0].toCssString()); gradient.addColorStop(1, colors[1].toCssString()); return gradient; - } - for (int i = 0; i < colors.length; i++) { - gradient.addColorStop(colorStops[i], colors[i].toCssString()); + } else { + for (int i = 0; i < colors.length; i++) { + gradient.addColorStop(colorStops[i], colors[i].toCssString()); + } } return gradient; } diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart index 6f0050973a..283757d975 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart @@ -888,7 +888,8 @@ class Canvas { } void drawVertices(Vertices vertices, BlendMode blendMode, Paint paint) { - assert(vertices != null); // vertices is checked on the engine side + if (vertices == null) return; + //assert(vertices != null); // vertices is checked on the engine side assert(paint != null); assert(blendMode != null); _canvas.drawVertices(vertices, blendMode, paint); diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/radial_gradient_golden_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/radial_gradient_golden_test.dart new file mode 100644 index 0000000000..725a23ba0e --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/radial_gradient_golden_test.dart @@ -0,0 +1,100 @@ +// 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. + +import 'dart:html' as html; + +import 'package:ui/ui.dart' hide TextStyle; +import 'package:ui/src/engine.dart'; +import 'package:test/test.dart'; + +import 'package:web_engine_tester/golden_tester.dart'; + +void main() async { + const double screenWidth = 600.0; + const double screenHeight = 800.0; + const Rect screenRect = Rect.fromLTWH(0, 0, screenWidth, screenHeight); + + // Commit a recording canvas to a bitmap, and compare with the expected + Future _checkScreenshot(RecordingCanvas rc, String fileName, + {Rect region = const Rect.fromLTWH(0, 0, 500, 500), + bool write = false}) async { + final EngineCanvas engineCanvas = BitmapCanvas(screenRect); + rc.apply(engineCanvas); + + // Wrap in so that our CSS selectors kick in. + final html.Element sceneElement = html.Element.tag('flt-scene'); + try { + sceneElement.append(engineCanvas.rootElement); + html.document.body.append(sceneElement); + await matchGoldenFile('$fileName.png', region: region, write: write); + } finally { + // The page is reused across tests, so remove the element after taking the + // golden screenshot. + sceneElement.remove(); + } + } + + setUp(() async { + debugEmulateFlutterTesterEnvironment = true; + await webOnlyInitializePlatform(); + webOnlyFontCollection.debugRegisterTestFonts(); + await webOnlyFontCollection.ensureFontsLoaded(); + }); + + Future _testGradient(String fileName, Shader shader, + {Rect paintRect = const Rect.fromLTRB(50, 50, 300, 300), + Rect shaderRect = const Rect.fromLTRB(50, 50, 300, 300)}) async { + final RecordingCanvas rc = + RecordingCanvas(const Rect.fromLTRB(0, 0, 500, 500)); + final Paint paint = Paint()..shader = shader; + final Path path = Path(); + path.addRect(paintRect); + rc.drawPath(path, paint); + await _checkScreenshot(rc, fileName); + } + + test('Should draw centered radial gradient.', () async { + Rect shaderRect = const Rect.fromLTRB(50, 50, 300, 300); + await _testGradient( + 'radial_gradient_centered', + Gradient.radial( + Offset((shaderRect.left + shaderRect.right) / 2, + (shaderRect.top + shaderRect.bottom) / 2), + shaderRect.width / 2, + [ + const Color.fromARGB(255, 0, 0, 0), + const Color.fromARGB(255, 0, 0, 255) + ]), + shaderRect: shaderRect); + }); + + test('Should draw right bottom centered radial gradient.', () async { + Rect shaderRect = const Rect.fromLTRB(50, 50, 300, 300); + await _testGradient( + 'radial_gradient_right_bottom', + Gradient.radial( + Offset(shaderRect.right, shaderRect.bottom), shaderRect.width / 2, [ + const Color.fromARGB(255, 0, 0, 0), + const Color.fromARGB(255, 0, 0, 255) + ]), + shaderRect: shaderRect); + }); + + test('Should draw with radial gradient with TileMode.clamp.', () async { + Rect shaderRect = const Rect.fromLTRB(50, 50, 100, 100); + await _testGradient( + 'radial_gradient_tilemode_clamp', + Gradient.radial( + Offset((shaderRect.left + shaderRect.right) / 2, + (shaderRect.top + shaderRect.bottom) / 2), + shaderRect.width / 2, + [ + const Color.fromARGB(255, 0, 0, 0), + const Color.fromARGB(255, 0, 0, 255) + ], + [0.0, 1.0], + TileMode.clamp), + shaderRect: shaderRect); + }); +}