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 b3e80abdc2..906f3242e3 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: 33b24a20bb9717699b847f633278f2b7bffde875 +revision: 04c9d19f106cf0b1bf409bf691bd5334950553ea 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 ea1441a4bf..831307fcb0 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 @@ -198,29 +198,12 @@ class BitmapCanvas extends EngineCanvas { } /// Sets the global paint styles to correspond to [paint]. - void _applyPaint(SurfacePaintData paint) { - ContextStateHandle contextHandle = _canvasPool.contextHandle; - contextHandle - ..lineWidth = paint.strokeWidth ?? 1.0 - ..blendMode = paint.blendMode - ..strokeCap = paint.strokeCap - ..strokeJoin = paint.strokeJoin - ..filter = _maskFilterToCss(paint.maskFilter); + void _setUpPaint(SurfacePaintData paint) { + _canvasPool.contextHandle.setUpPaint(paint); + } - if (paint.shader != null) { - final EngineGradient engineShader = paint.shader; - final Object paintStyle = - engineShader.createPaintStyle(_canvasPool.context); - contextHandle.fillStyle = paintStyle; - contextHandle.strokeStyle = paintStyle; - } else if (paint.color != null) { - final String colorString = colorToCssString(paint.color); - contextHandle.fillStyle = colorString; - contextHandle.strokeStyle = colorString; - } else { - contextHandle.fillStyle = ''; - contextHandle.strokeStyle = ''; - } + void _tearDownPaint() { + _canvasPool.contextHandle.tearDownPaint(); } @override @@ -299,50 +282,58 @@ class BitmapCanvas extends EngineCanvas { @override void drawLine(ui.Offset p1, ui.Offset p2, SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.strokeLine(p1, p2); + _tearDownPaint(); } @override void drawPaint(SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.fill(); + _tearDownPaint(); } @override void drawRect(ui.Rect rect, SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.drawRect(rect, paint.style); + _tearDownPaint(); } @override void drawRRect(ui.RRect rrect, SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.drawRRect(rrect, paint.style); + _tearDownPaint(); } @override void drawDRRect(ui.RRect outer, ui.RRect inner, SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.drawDRRect(outer, inner, paint.style); + _tearDownPaint(); } @override void drawOval(ui.Rect rect, SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.drawOval(rect, paint.style); + _tearDownPaint(); } @override void drawCircle(ui.Offset c, double radius, SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.drawCircle(c, radius, paint.style); + _tearDownPaint(); } @override void drawPath(ui.Path path, SurfacePaintData paint) { - _applyPaint(paint); + _setUpPaint(paint); _canvasPool.drawPath(path, paint.style); + _tearDownPaint(); } @override @@ -442,8 +433,6 @@ class BitmapCanvas extends EngineCanvas { !requiresClipping && paint.colorFilter == null) { _drawImage(image, dst.topLeft, paint); - _childOverdraw = true; - _canvasPool.closeCurrentCanvas(); } else { if (requiresClipping) { save(); @@ -683,14 +672,15 @@ class BitmapCanvas extends EngineCanvas { ctx.font = style.cssFontString; _cachedLastStyle = style; } - _applyPaint(paragraph._paint.paintData); - + _setUpPaint(paragraph._paint.paintData); double y = offset.dy + paragraph.alphabeticBaseline; final int len = lines.length; for (int i = 0; i < len; i++) { _drawTextLine(style, lines[i], offset.dx, y); y += paragraph._lineHeight; } + _tearDownPaint(); + return; } @@ -769,23 +759,28 @@ class BitmapCanvas extends EngineCanvas { _canvasPool.currentTransform, vertices, blendMode, paint); } + /// Stores paint data used by [drawPoints]. We cannot use the original paint + /// data object because painting style is determined by [ui.PointMode] and + /// not by [SurfacePointData.style]. + static SurfacePaintData _drawPointsPaint = SurfacePaintData() + ..strokeCap = ui.StrokeCap.round + ..strokeJoin = ui.StrokeJoin.round + ..blendMode = ui.BlendMode.srcOver; + @override - void drawPoints(ui.PointMode pointMode, Float32List points, - double strokeWidth, ui.Color color) { - ContextStateHandle contextHandle = _canvasPool.contextHandle; - contextHandle - ..lineWidth = strokeWidth - ..blendMode = ui.BlendMode.srcOver - ..strokeCap = ui.StrokeCap.round - ..strokeJoin = ui.StrokeJoin.round - ..filter = ''; - final String cssColor = colorToCssString(color); + void drawPoints(ui.PointMode pointMode, Float32List points, SurfacePaintData paint) { if (pointMode == ui.PointMode.points) { - contextHandle.fillStyle = cssColor; + _drawPointsPaint.style = ui.PaintingStyle.stroke; } else { - contextHandle.strokeStyle = cssColor; + _drawPointsPaint.style = ui.PaintingStyle.fill; } - _canvasPool.drawPoints(pointMode, points, strokeWidth / 2.0); + _drawPointsPaint.color = paint.color; + _drawPointsPaint.strokeWidth = paint.strokeWidth; + _drawPointsPaint.maskFilter = paint.maskFilter; + + _setUpPaint(_drawPointsPaint); + _canvasPool.drawPoints(pointMode, points, paint.strokeWidth / 2.0); + _tearDownPaint(); } @override @@ -969,11 +964,19 @@ List _clipContent(List<_SaveClipEntry> clipStack, return [root]..addAll(clipDefs); } -String _maskFilterToCss(ui.MaskFilter maskFilter) { - if (maskFilter == null) { +/// Converts a [maskFilter] to the value to be used on a ``. +/// +/// Only supported in non-WebKit browsers. +String _maskFilterToCanvasFilter(ui.MaskFilter maskFilter) { + assert( + browserEngine != BrowserEngine.webkit, + 'WebKit (Safari) does not support `filter` canvas property.', + ); + if (maskFilter != null) { + return 'blur(${maskFilter.webOnlySigma}px)'; + } else { return 'none'; } - return 'blur(${maskFilter.webOnlySigma}px)'; } int _filterIdCounter = 0; 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 f605b33009..1f55400bd4 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 @@ -135,7 +135,7 @@ class _CanvasPool extends _SaveStackTracking { ++_activeElementCount; _context = _canvas.context2D; - _contextHandle = ContextStateHandle(_context); + _contextHandle = ContextStateHandle(this, _context); _initializeViewport(requiresClearRect); _replayClipStack(); } @@ -624,7 +624,7 @@ class _CanvasPool extends _SaveStackTracking { // alpha for the paint the path is painted in addition to the shadow, // which is undesirable. context.translate(shadow.offset.dx, shadow.offset.dy); - context.filter = _maskFilterToCss( + context.filter = _maskFilterToCanvasFilter( ui.MaskFilter.blur(ui.BlurStyle.normal, shadow.blurWidth)); context.strokeStyle = ''; context.fillStyle = solidColor; @@ -690,8 +690,10 @@ class _CanvasPool extends _SaveStackTracking { // to initialize current values. // class ContextStateHandle { - html.CanvasRenderingContext2D context; - ContextStateHandle(this.context); + final html.CanvasRenderingContext2D context; + final _CanvasPool _canvasPool; + + ContextStateHandle(this._canvasPool, this.context); ui.BlendMode _currentBlendMode = ui.BlendMode.srcOver; ui.StrokeCap _currentStrokeCap = ui.StrokeCap.butt; ui.StrokeJoin _currentStrokeJoin = ui.StrokeJoin.miter; @@ -700,7 +702,6 @@ class ContextStateHandle { Object _currentFillStyle; Object _currentStrokeStyle; double _currentLineWidth = 1.0; - String _currentFilter = 'none'; set blendMode(ui.BlendMode blendMode) { if (blendMode != _currentBlendMode) { @@ -747,10 +748,124 @@ class ContextStateHandle { } } - set filter(String filter) { - if (_currentFilter != filter) { - _currentFilter = filter; - context.filter = filter; + ui.MaskFilter _currentFilter; + SurfacePaintData _lastUsedPaint; + + /// The painting state. + /// + /// Used to validate that the [setUpPaint] and [tearDownPaint] are called in + /// a correct sequence. + bool _debugIsPaintSetUp = false; + + /// Whether to use WebKit's method of rendering [MaskFilter]. + /// + /// This is used in screenshot tests to test Safari codepaths. + static bool debugEmulateWebKitMaskFilter = false; + + bool get _renderMaskFilterForWebkit => browserEngine == BrowserEngine.webkit || debugEmulateWebKitMaskFilter; + + /// Sets paint properties on the current canvas. + /// + /// [tearDownPaint] must be called after calling this method. + void setUpPaint(SurfacePaintData paint) { + if (assertionsEnabled) { + assert(!_debugIsPaintSetUp); + _debugIsPaintSetUp = true; + } + + _lastUsedPaint = paint; + lineWidth = paint.strokeWidth ?? 1.0; + blendMode = paint.blendMode; + strokeCap = paint.strokeCap; + strokeJoin = paint.strokeJoin; + + if (paint.shader != null) { + final EngineGradient engineShader = paint.shader; + final Object paintStyle = + engineShader.createPaintStyle(_canvasPool.context); + fillStyle = paintStyle; + strokeStyle = paintStyle; + } else if (paint.color != null) { + final String colorString = colorToCssString(paint.color); + fillStyle = colorString; + strokeStyle = colorString; + } else { + fillStyle = ''; + strokeStyle = ''; + } + + final ui.MaskFilter maskFilter = paint?.maskFilter; + if (!_renderMaskFilterForWebkit) { + if (_currentFilter != maskFilter) { + _currentFilter = maskFilter; + context.filter = _maskFilterToCanvasFilter(maskFilter); + } + } else { + // WebKit does not support the `filter` property. Instead we apply a + // shadow to the shape of the same color as the paint and the same blur + // as the mask filter. + // + // Note that on WebKit the cached value of _currentFilter is not useful. + // Instead we destructure it into the shadow properties and cache those. + if (maskFilter != null) { + context.save(); + context.shadowBlur = convertSigmaToRadius(maskFilter.webOnlySigma); + if (paint?.color != null) { + // Shadow color must be fully opaque. + context.shadowColor = colorToCssString(paint.color.withAlpha(255)); + } else { + context.shadowColor = colorToCssString(const ui.Color(0xFF000000)); + } + + // On the web a shadow must always be painted together with the shape + // that casts it. In order to paint just the shadow, we offset the shape + // by a large enough value that it moved outside the canvas bounds, then + // offset the shadow in the opposite direction such that it lands exactly + // where the shape is. + const double kOutsideTheBoundsOffset = 50000; + + context.translate(-kOutsideTheBoundsOffset, 0); + + // Shadow offset is not affected by the current canvas context transform. + // We have to apply the transform ourselves. To do that we transform the + // tip of the vector from the shape to the shadow, then we transform the + // origin (0, 0). The desired shadow offset is the difference between the + // two. In vector notation, this is: + // + // transformedShadowDelta = M*shadowDelta - M*origin. + final Float32List tempVector = Float32List(2); + tempVector[0] = kOutsideTheBoundsOffset * window.devicePixelRatio; + _canvasPool.currentTransform.transform2(tempVector); + double shadowOffsetX = tempVector[0]; + double shadowOffsetY = tempVector[1]; + + tempVector[0] = tempVector[1] = 0; + _canvasPool.currentTransform.transform2(tempVector); + context.shadowOffsetX = shadowOffsetX - tempVector[0]; + context.shadowOffsetY = shadowOffsetY - tempVector[1]; + } + } + } + + /// Removes paint properties on the current canvas used by the last draw + /// command. + /// + /// Not all properties are cleared. Properties that are set by all paint + /// commands prior to painting do not need to be cleared. + /// + /// Must be called after calling [setUpPaint]. + void tearDownPaint() { + if (assertionsEnabled) { + assert(_debugIsPaintSetUp); + _debugIsPaintSetUp = false; + } + + final ui.MaskFilter maskFilter = _lastUsedPaint?.maskFilter; + if (maskFilter != null && _renderMaskFilterForWebkit) { + // On Safari (WebKit) we use a translated shadow to emulate + // MaskFilter.blur. We use restore to undo the translation and + // shadow attributes. + context.restore(); } } @@ -782,8 +897,10 @@ class ContextStateHandle { _currentFillStyle = context.fillStyle; context.strokeStyle = ''; _currentStrokeStyle = context.strokeStyle; - context.filter = 'none'; - _currentFilter = 'none'; + context.shadowBlur = 0; + context.shadowColor = 'none'; + context.shadowOffsetX = 0; + context.shadowOffsetY = 0; context.globalCompositeOperation = 'source-over'; _currentBlendMode = ui.BlendMode.srcOver; context.lineWidth = 1.0; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart index c774578df2..2ccb4b6aeb 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart @@ -190,8 +190,7 @@ class DomCanvas extends EngineCanvas with SaveElementStackTracking { } @override - void drawPoints(ui.PointMode pointMode, Float32List points, - double strokeWidth, ui.Color color) { + void drawPoints(ui.PointMode pointMode, Float32List points, SurfacePaintData paint) { throw UnimplementedError(); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/engine_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/engine_canvas.dart index c918206e3d..b78b5d7ad5 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/engine_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/engine_canvas.dart @@ -70,8 +70,7 @@ abstract class EngineCanvas { void drawVertices( ui.Vertices vertices, ui.BlendMode blendMode, SurfacePaintData paint); - void drawPoints(ui.PointMode pointMode, Float32List points, - double strokeWidth, ui.Color color); + void drawPoints(ui.PointMode pointMode, Float32List points, SurfacePaintData paint); /// Extension of Canvas API to mark the end of a stream of painting commands /// to enable re-use/dispose optimizations. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/houdini_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/houdini_canvas.dart index 260b13d123..dcf9e2a7c6 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/houdini_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/houdini_canvas.dart @@ -236,8 +236,7 @@ class HoudiniCanvas extends EngineCanvas with SaveElementStackTracking { } @override - void drawPoints(ui.PointMode pointMode, Float32List points, - double strokeWidth, ui.Color color) { + void drawPoints(ui.PointMode pointMode, Float32List points, SurfacePaintData paint) { // TODO(flutter_web): implement. } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/recording_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/recording_canvas.dart index 5bce4c3157..33565b7cfb 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/recording_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/recording_canvas.dart @@ -534,7 +534,7 @@ class RecordingCanvas { } _hasArbitraryPaint = true; _didDraw = true; - final PaintDrawPoints command = PaintDrawPoints(pointMode, points, paint.strokeWidth, paint.color); + final PaintDrawPoints command = PaintDrawPoints(pointMode, points, paint.paintData); _growPaintBoundsByPoints(points, paint.strokeWidth, paint, command); _commands.add(command); } @@ -990,19 +990,18 @@ class PaintDrawVertices extends DrawCommand { class PaintDrawPoints extends DrawCommand { final Float32List points; final ui.PointMode pointMode; - final double strokeWidth; - final ui.Color color; - PaintDrawPoints(this.pointMode, this.points, this.strokeWidth, this.color); + final SurfacePaintData paint; + PaintDrawPoints(this.pointMode, this.points, this.paint); @override void apply(EngineCanvas canvas) { - canvas.drawPoints(pointMode, points, strokeWidth, color); + canvas.drawPoints(pointMode, points, paint); } @override String toString() { if (assertionsEnabled) { - return 'drawPoints($pointMode, $points, $strokeWidth, $color)'; + return 'drawPoints($pointMode, $points, $paint)'; } else { return super.toString(); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart index d0301e192b..fedb5f796e 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart @@ -511,3 +511,12 @@ class _FastMatrix64 { transformedY = matrix[13] + (matrix[1] * x) + (matrix[5] * y); } } + +/// Roughly the inverse of [ui.Shadow.convertRadiusToSigma]. +/// +/// This does not inverse [ui.Shadow.convertRadiusToSigma] exactly, because on +/// the Web the difference between sigma and blur radius is different from +/// Flutter mobile. +double convertSigmaToRadius(double sigma) { + return sigma * 2.0; +} diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart index bd7b5eac56..721e4ed3c9 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart @@ -373,7 +373,9 @@ class Matrix4 { return scaled(arg); } if (arg is Vector3) { - return transformed3(arg); + final Vector3 copy = arg.clone(); + transform3(copy.storage); + return copy; } if (arg is Matrix4) { return multiplied(arg); @@ -970,38 +972,38 @@ class Matrix4 { return rotate3(out); } - /// Transform [arg] of type [Vector3] using the transformation defined by - /// [this]. - Vector3 transform3(Vector3 arg) { - final Float32List argStorage = arg._v3storage; - final double x = (_m4storage[0] * argStorage[0]) + - (_m4storage[4] * argStorage[1]) + - (_m4storage[8] * argStorage[2]) + + /// Transforms a 3-component vector in-place. + void transform3(Float32List vector) { + final double x = (_m4storage[0] * vector[0]) + + (_m4storage[4] * vector[1]) + + (_m4storage[8] * vector[2]) + _m4storage[12]; - final double y = (_m4storage[1] * argStorage[0]) + - (_m4storage[5] * argStorage[1]) + - (_m4storage[9] * argStorage[2]) + + final double y = (_m4storage[1] * vector[0]) + + (_m4storage[5] * vector[1]) + + (_m4storage[9] * vector[2]) + _m4storage[13]; - final double z = (_m4storage[2] * argStorage[0]) + - (_m4storage[6] * argStorage[1]) + - (_m4storage[10] * argStorage[2]) + + final double z = (_m4storage[2] * vector[0]) + + (_m4storage[6] * vector[1]) + + (_m4storage[10] * vector[2]) + _m4storage[14]; - argStorage[0] = x; - argStorage[1] = y; - argStorage[2] = z; - return arg; + vector[0] = x; + vector[1] = y; + vector[2] = z; } - /// Transform a copy of [arg] of type [Vector3] using the transformation - /// defined by [this]. If a [out] parameter is supplied, the copy is stored in - /// [out]. - Vector3 transformed3(Vector3 arg, [Vector3 out]) { - if (out == null) { - out = Vector3.copy(arg); - } else { - out.setFrom(arg); - } - return transform3(out); + /// Transforms a 2-component vector in-place. + /// + /// This transformation forgets the final Z component. If you need the + /// Z component, see [transform3]. + void transform2(Float32List vector) { + double x = vector[0]; + double y = vector[1]; + vector[0] = (_m4storage[0] * x) + + (_m4storage[4] * y) + + _m4storage[12]; + vector[1] = (_m4storage[1] * x) + + (_m4storage[5] * y) + + _m4storage[13]; } /// Copies [this] into [array] starting at [offset]. @@ -1057,6 +1059,22 @@ class Matrix4 { Float64List toFloat64() { return Float64List.fromList(_m4storage); } + + @override + String toString() { + if (assertionsEnabled) { + String fmt(int index) { + return storage[index].toStringAsFixed(2); + } + + return '[${fmt(0)}, ${fmt(4)}, ${fmt(8)}, ${fmt(12)}]\n' + '[${fmt(1)}, ${fmt(5)}, ${fmt(9)}, ${fmt(13)}]\n' + '[${fmt(2)}, ${fmt(6)}, ${fmt(10)}, ${fmt(14)}]\n' + '[${fmt(3)}, ${fmt(7)}, ${fmt(11)}, ${fmt(15)}]'; + } else { + return super.toString(); + } + } } /// 3D column vector. diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_draw_points_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_draw_points_test.dart index f5c7f97299..8688140b29 100644 --- a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_draw_points_test.dart +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_draw_points_test.dart @@ -27,29 +27,30 @@ void main() async { }); test('draws points in all 3 modes', () async { - final double strokeWidth = 2.0; - final Color color = Color(0xFF0000FF); + final SurfacePaintData paint = SurfacePaintData(); + paint.strokeWidth = 2.0; + paint.color = Color(0xFF0000FF); final Float32List points = offsetListToFloat32List([ Offset(10, 10), Offset(50, 10), Offset(70, 70), Offset(170, 70) ]); - canvas.drawPoints(PointMode.points, points, strokeWidth, color); + canvas.drawPoints(PointMode.points, points, paint); final Float32List points2 = offsetListToFloat32List([ Offset(10, 110), Offset(50, 110), Offset(70, 170), Offset(170, 170) ]); - canvas.drawPoints(PointMode.lines, points2, strokeWidth, color); + canvas.drawPoints(PointMode.lines, points2, paint); final Float32List points3 = offsetListToFloat32List([ Offset(10, 210), Offset(50, 210), Offset(70, 270), Offset(170, 270) ]); - canvas.drawPoints(PointMode.polygon, points3, strokeWidth, color); + canvas.drawPoints(PointMode.polygon, points3, paint); html.document.body.append(canvas.rootElement); await matchGoldenFile('canvas_draw_points.png', region: region); diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_mask_filter_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_mask_filter_test.dart new file mode 100644 index 0000000000..77b86bf82a --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_mask_filter_test.dart @@ -0,0 +1,175 @@ +// 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 'dart:math' as math; +import 'dart:typed_data'; + +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 { + // Commit a recording canvas to a bitmap, and compare with the expected + Future _checkScreenshot(RecordingCanvas rc, String fileName, Rect screenRect) async { + final EngineCanvas engineCanvas = BitmapCanvas(screenRect); + + rc.endRecording(); + rc.apply(engineCanvas, screenRect); + + // 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: screenRect, maxDiffRatePercent: 0.0); + } finally { + // The page is reused across tests, so remove the element after taking the + // Scuba screenshot. + sceneElement.remove(); + } + } + + setUp(() async { + debugEmulateFlutterTesterEnvironment = true; + await webOnlyInitializePlatform(); + webOnlyFontCollection.debugRegisterTestFonts(); + await webOnlyFontCollection.ensureFontsLoaded(); + }); + + tearDown(() { + ContextStateHandle.debugEmulateWebKitMaskFilter = false; + }); + + // Regression test for https://github.com/flutter/flutter/issues/55930 + void testMaskFilterBlur({bool isSafariMode}) { + final String browser = isSafariMode ? 'Safari' : 'Chrome'; + + test('renders MaskFilter.blur in $browser', () async { + const double screenWidth = 800.0; + const double screenHeight = 150.0; + const Rect screenRect = Rect.fromLTWH(0, 0, screenWidth, screenHeight); + + ContextStateHandle.debugEmulateWebKitMaskFilter = isSafariMode; + final RecordingCanvas rc = RecordingCanvas(screenRect); + rc.translate(0, 75); + + final SurfacePaint paint = SurfacePaint() + ..maskFilter = MaskFilter.blur(BlurStyle.normal, 5); + + rc.translate(50, 0); + rc.drawRect( + Rect.fromCircle(center: Offset.zero, radius: 30), + paint, + ); + + rc.translate(100, 0); + paint.color = Color(0xFF00FF00); + rc.drawRRect( + RRect.fromRectAndRadius( + Rect.fromCircle(center: Offset.zero, radius: 30), + Radius.circular(20), + ), + paint, + ); + + rc.translate(100, 0); + paint.color = Color(0xFF0000FF); + rc.drawCircle(Offset.zero, 30, paint); + + rc.translate(100, 0); + paint.color = Color(0xFF00FFFF); + rc.drawPath( + SurfacePath() + ..moveTo(-20, 0) + ..lineTo(0, -50) + ..lineTo(20, 0) + ..lineTo(0, 50) + ..close(), + paint, + ); + + rc.translate(100, 0); + paint.color = Color(0xFFFF00FF); + rc.drawOval( + Rect.fromCenter(center: Offset.zero, width: 40, height: 100), + paint, + ); + + rc.translate(100, 0); + paint.color = Color(0xFF888800); + paint.strokeWidth = 5; + rc.drawLine( + Offset(-20, -50), + Offset(20, 50), + paint, + ); + + rc.translate(100, 0); + paint.color = Color(0xFF888888); + rc.drawDRRect( + RRect.fromRectAndRadius( + Rect.fromCircle(center: Offset.zero, radius: 35), + Radius.circular(20), + ), + RRect.fromRectAndRadius( + Rect.fromCircle(center: Offset.zero, radius: 15), + Radius.circular(7), + ), + paint, + ); + + rc.translate(100, 0); + paint.color = Color(0xFF6500C9); + rc.drawRawPoints( + PointMode.points, + Float32List.fromList([-10, -10, -10, 10, 10, -10, 10, 10]), + paint, + ); + + await _checkScreenshot(rc, 'mask_filter_$browser', screenRect); + }); + + test('renders transformed MaskFilter.blur in $browser', () async { + const double screenWidth = 300.0; + const double screenHeight = 300.0; + const Rect screenRect = Rect.fromLTWH(0, 0, screenWidth, screenHeight); + + ContextStateHandle.debugEmulateWebKitMaskFilter = isSafariMode; + final RecordingCanvas rc = RecordingCanvas(screenRect); + rc.translate(150, 150); + + final SurfacePaint paint = SurfacePaint() + ..maskFilter = MaskFilter.blur(BlurStyle.normal, 5); + + final List colors = [ + Color(0xFF000000), + Color(0xFF00FF00), + Color(0xFF0000FF), + Color(0xFF00FFFF), + Color(0xFFFF00FF), + Color(0xFF888800), + Color(0xFF888888), + Color(0xFF6500C9), + ]; + + for (Color color in colors) { + paint.color = color; + rc.rotate(math.pi / 4); + rc.drawRect( + Rect.fromCircle(center: const Offset(90, 0), radius: 20), + paint, + ); + } + + await _checkScreenshot(rc, 'mask_filter_transformed_$browser', screenRect); + }); + } + + testMaskFilterBlur(isSafariMode: false); + testMaskFilterBlur(isSafariMode: true); +} diff --git a/engine/src/flutter/lib/web_ui/test/mock_engine_canvas.dart b/engine/src/flutter/lib/web_ui/test/mock_engine_canvas.dart index cb5eaeb07d..a88dccc0b1 100644 --- a/engine/src/flutter/lib/web_ui/test/mock_engine_canvas.dart +++ b/engine/src/flutter/lib/web_ui/test/mock_engine_canvas.dart @@ -234,13 +234,11 @@ class MockEngineCanvas implements EngineCanvas { } @override - void drawPoints(PointMode pointMode, Float32List points, double strokeWidth, - Color color) { + void drawPoints(PointMode pointMode, Float32List points, SurfacePaintData paint) { _called('drawPoints', arguments: { 'pointMode': pointMode, 'points': points, - 'strokeWidth': strokeWidth, - 'color': color, + 'paint': paint, }); }