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 bbd03eb461..3a6e72bb95 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: 75729099b6f4d4e78ceb110b9305fc9905b91519 +revision: ab1f2da642d6c5188b312965759ce7157fd073b9 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 b9daedc91f..c7c223b38e 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 @@ -579,7 +579,9 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking { void drawImage(ui.Image image, ui.Offset p, ui.PaintData paint) { _applyPaint(paint); final HtmlImage htmlImage = image; - final html.Element imgElement = htmlImage.cloneImageElement(); + final html.ImageElement imgElement = htmlImage.cloneImageElement(); + String blendMode = ctx.globalCompositeOperation; + imgElement.style.mixBlendMode = blendMode; _drawImage(imgElement, p); _childOverdraw = true; } @@ -618,6 +620,8 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking { } else { _applyPaint(paint); final html.Element imgElement = htmlImage.cloneImageElement(); + final ui.BlendMode blendMode = paint.blendMode; + imgElement.style.mixBlendMode = _stringForBlendMode(blendMode); if (requiresClipping) { save(); clipRect(dst); diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_blend_golden_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_blend_golden_test.dart new file mode 100644 index 0000000000..b4074d0823 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/canvas_blend_golden_test.dart @@ -0,0 +1,136 @@ +// 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 'dart:math' as math; +import 'dart:js_util' as js_util; + +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); + final Paint testPaint = Paint()..color = const Color(0xFFFF0000); + + // 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)}) 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, maxDiffRate: 0.03); + } 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(); + }); + + test('Blend circles with difference and color', () async { + final RecordingCanvas rc = + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); + rc.save(); + rc.drawRect( + Rect.fromLTRB(0, 0, 400, 400), + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromARGB(255, 255, 255, 255)); + rc.drawCircle( + Offset(100, 100), + 80.0, + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromARGB(128, 255, 0, 0) + ..blendMode = BlendMode.difference); + + rc.drawCircle( + Offset(170, 100), + 80.0, + Paint() + ..style = PaintingStyle.fill + ..blendMode = BlendMode.color + ..color = const Color.fromARGB(128, 0, 255, 0)); + + rc.drawCircle( + Offset(135, 170), + 80.0, + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromARGB(128, 255, 0, 0)); + await _checkScreenshot(rc, 'canvas_blend_circle_diff_color'); + }); + + test('Blend circle and text with multiply', () async { + final RecordingCanvas rc = + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); + rc.save(); + rc.drawRect( + Rect.fromLTRB(0, 0, 400, 400), + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromARGB(255, 255, 255, 255)); + rc.drawCircle( + Offset(100, 100), + 80.0, + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromARGB(128, 255, 0, 0) + ..blendMode = BlendMode.difference); + rc.drawCircle( + Offset(170, 100), + 80.0, + Paint() + ..style = PaintingStyle.fill + ..blendMode = BlendMode.color + ..color = const Color.fromARGB(128, 0, 255, 0)); + + rc.drawCircle( + Offset(135, 170), + 80.0, + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromARGB(128, 255, 0, 0)); + rc.drawImage(createTestImage(), Offset(135.0, 130.0), + Paint()..blendMode = BlendMode.multiply); + await _checkScreenshot(rc, 'canvas_blend_image_multiply'); + }); +} + +HtmlImage createTestImage() { + const int width = 100; + const int height = 50; + html.CanvasElement canvas = + new html.CanvasElement(width: width, height: height); + html.CanvasRenderingContext2D ctx = canvas.context2D; + ctx.fillStyle = '#E04040'; + ctx.fillRect(0, 0, 33, 50); + ctx.fill(); + ctx.fillStyle = '#40E080'; + ctx.fillRect(33, 0, 33, 50); + ctx.fill(); + ctx.fillStyle = '#2040E0'; + ctx.fillRect(66, 0, 33, 50); + ctx.fill(); + html.ImageElement imageElement = html.ImageElement(); + imageElement.src = js_util.callMethod(canvas, 'toDataURL', []); + return HtmlImage(imageElement, width, height); +}