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 084f6e21c6..023534e813 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: 1839716cce5da0fd4c442395c7161f29dd983d8e +revision: 263d816b73df47fca2fdc0fbce7ad09f17aa30b6 diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/backdrop_filter.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/backdrop_filter.dart index f0c9df39e4..6ed5b2356d 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/backdrop_filter.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/backdrop_filter.dart @@ -20,6 +20,7 @@ class PersistedBackdropFilter extends PersistedContainerSurface html.Element get childContainer => _childContainer; html.Element _childContainer; html.Element _filterElement; + ui.Rect _activeClipBounds; // Cached inverted transform for _transform. Matrix4 _invertedTransform; // Reference to transform last used to cache [_invertedTransform]. @@ -65,14 +66,41 @@ class PersistedBackdropFilter extends PersistedContainerSurface _invertedTransform = Matrix4.inverted(_transform); _previousTransform = _transform; } - final ui.Rect rect = transformRect(_invertedTransform, ui.Rect.fromLTRB(0, 0, - ui.window.physicalSize.width, ui.window.physicalSize.height)); + // https://api.flutter.dev/flutter/widgets/BackdropFilter-class.html + // Defines the effective area as the parent/ancestor clip or if not + // available, the whole screen. + // + // The CSS backdrop-filter will use isolation boundary defined in + // https://drafts.fxtf.org/filter-effects-2/#BackdropFilterProperty + // Therefore we need to use parent clip element bounds for + // backdrop boundary. + final double dpr = ui.window.devicePixelRatio; + ui.Rect rect = transformRect(_invertedTransform, ui.Rect.fromLTRB(0, 0, + ui.window.physicalSize.width * dpr, + ui.window.physicalSize.height * dpr)); + double left = rect.left; + double top = rect.top; + double width = rect.width; + double height = rect.height; + PersistedContainerSurface parentSurface = parent; + while (parentSurface != null) { + if (parentSurface.isClipping) { + _activeClipBounds = parentSurface._localClipBounds; + left = _activeClipBounds.left; + top = _activeClipBounds.top; + width = _activeClipBounds.width; + height = _activeClipBounds.height; + break; + } + parentSurface = parentSurface.parent; + } final html.CssStyleDeclaration filterElementStyle = _filterElement.style; filterElementStyle ..position = 'absolute' - ..transform = 'translate(${rect.left}px, ${rect.top}px)' - ..width = '${rect.width}px' - ..height = '${rect.height}px'; + ..left = '${left}px' + ..top = '${top}px' + ..width = '${width}px' + ..height = '${height}px'; if (browserEngine == BrowserEngine.firefox) { // For FireFox for now render transparent black background. // TODO(flutter_web): Switch code to use filter when @@ -97,6 +125,28 @@ class PersistedBackdropFilter extends PersistedContainerSurface super.update(oldSurface); if (filter != oldSurface.filter) { apply(); + } else { + _checkForUpdatedAncestorClipElement(); } } + + void _checkForUpdatedAncestorClipElement() { + // If parent clip element has moved, adjust bounds. + PersistedContainerSurface parentSurface = parent; + while (parentSurface != null) { + if (parentSurface.isClipping) { + if (parentSurface._localClipBounds != _activeClipBounds) { + apply(); + } + break; + } + parentSurface = parentSurface.parent; + } + } + + @override + void retain() { + super.retain(); + _checkForUpdatedAncestorClipElement(); + } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/clip.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/clip.dart index 43c683b21b..46529c65a3 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/clip.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/clip.dart @@ -100,6 +100,9 @@ class PersistedClipRect extends PersistedContainerSurface apply(); } } + + @override + bool get isClipping => true; } /// A surface that creates a rounded rectangular clip. @@ -153,6 +156,9 @@ class PersistedClipRRect extends PersistedContainerSurface apply(); } } + + @override + bool get isClipping => true; } class PersistedPhysicalShape extends PersistedContainerSurface @@ -395,6 +401,9 @@ class PersistedClipPath extends PersistedContainerSurface _clipElement = null; super.discard(); } + + @override + bool get isClipping => true; } /// Creates an svg clipPath and applies it to [element]. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/surface.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/surface.dart index b8ea68f6cb..28c0134b15 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/surface/surface.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/surface/surface.dart @@ -735,6 +735,11 @@ abstract class PersistedContainerSurface extends PersistedSurface { } } + // Children should override if they are performing clipping. + // + // Used by BackdropFilter to locate it's ancestor clip element. + bool get isClipping => false; + void _debugValidateContainerUpdate(PersistedContainerSurface oldSurface) { // At the end of this all children should have an element each, and it // should be attached to this container's element. diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/backdrop_filter_golden_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/backdrop_filter_golden_test.dart new file mode 100644 index 0000000000..5b0cc417e1 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/backdrop_filter_golden_test.dart @@ -0,0 +1,148 @@ +// 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 'package:ui/ui.dart'; +import 'package:ui/src/engine.dart'; +import 'package:test/test.dart'; + +import 'package:web_engine_tester/golden_tester.dart'; + +final Rect region = Rect.fromLTWH(0, 0, 500, 500); + +void main() async { + setUp(() async { + debugShowClipLayers = true; + SurfaceSceneBuilder.debugForgetFrameScene(); + for (html.Node scene in html.document.querySelectorAll('flt-scene')) { + scene.remove(); + } + + await webOnlyInitializePlatform(); + webOnlyFontCollection.debugRegisterTestFonts(); + await webOnlyFontCollection.ensureFontsLoaded(); + }); + + // The black circle on the left should not be blurred since it is outside + // the clip boundary around backdrop filter. However there should be only + // one red dot since the other one should be blurred by filter. + test('Background should only blur at ancestor clip boundary', () async { + final SurfaceSceneBuilder builder = SurfaceSceneBuilder(); + final Picture backgroundPicture = _drawBackground(); + builder.addPicture(Offset.zero, backgroundPicture); + + builder.pushClipRect( + const Rect.fromLTRB(10, 10, 300, 300), + ); + final Picture circles1 = _drawTestPictureWithCircles(30, 30); + builder.addPicture(Offset.zero, circles1); + + builder.pushClipRect( + const Rect.fromLTRB(60, 10, 300, 300), + ); + builder.pushBackdropFilter(ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0), + oldLayer: null); + final Picture circles2 = _drawTestPictureWithCircles(90, 30); + builder.addPicture(Offset.zero, circles2); + builder.pop(); + builder.pop(); + builder.pop(); + + html.document.body.append(builder + .build() + .webOnlyRootElement); + + await matchGoldenFile('backdrop_filter_clip.png', region: region); + }); + + test('Background should only blur at ancestor clip boundary after move', () async { + final SurfaceSceneBuilder builder = SurfaceSceneBuilder(); + final Picture backgroundPicture = _drawBackground(); + builder.addPicture(Offset.zero, backgroundPicture); + ClipRectEngineLayer clipEngineLayer = builder.pushClipRect( + const Rect.fromLTRB(10, 10, 300, 300), + ); + final Picture circles1 = _drawTestPictureWithCircles(30, 30); + builder.addPicture(Offset.zero, circles1); + ClipRectEngineLayer clipEngineLayer2 = builder.pushClipRect( + const Rect.fromLTRB(60, 10, 300, 300), + ); + BackdropFilterEngineLayer oldBackdropFilterLayer = + builder.pushBackdropFilter(ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0), + oldLayer: null); + final Picture circles2 = _drawTestPictureWithCircles(90, 30); + builder.addPicture(Offset.zero, circles2); + builder.pop(); + builder.pop(); + builder.pop(); + builder.build(); + + // Now reparent filter layer in next scene. + final SurfaceSceneBuilder builder2 = SurfaceSceneBuilder(); + builder2.addPicture(Offset.zero, backgroundPicture); + builder2.pushClipRect( + const Rect.fromLTRB(10, 10, 300, 300), + oldLayer: clipEngineLayer + ); + builder2.addPicture(Offset.zero, circles1); + builder2.pushClipRect( + const Rect.fromLTRB(10, 75, 300, 300), + oldLayer: clipEngineLayer2 + ); + builder2.pushBackdropFilter(ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0), + oldLayer: oldBackdropFilterLayer); + builder2.addPicture(Offset.zero, circles2); + builder2.pop(); + builder2.pop(); + builder2.pop(); + + html.document.body.append(builder2 + .build() + .webOnlyRootElement); + + await matchGoldenFile('backdrop_filter_clip_moved.png', region: region); + }); +} + +Picture _drawTestPictureWithCircles(double offsetX, double offsetY) { + final EnginePictureRecorder recorder = PictureRecorder(); + final RecordingCanvas canvas = + recorder.beginRecording(const Rect.fromLTRB(0, 0, 400, 400)); + canvas.drawCircle( + Offset(offsetX + 10, offsetY + 10), 10, Paint()..style = PaintingStyle.fill); + canvas.drawCircle( + Offset(offsetX + 60, offsetY + 10), + 10, + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromRGBO(255, 0, 0, 1)); + canvas.drawCircle( + Offset(offsetX + 10, offsetY + 60), + 10, + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromRGBO(0, 255, 0, 1)); + canvas.drawCircle( + Offset(offsetX + 60, offsetY + 60), + 10, + Paint() + ..style = PaintingStyle.fill + ..color = const Color.fromRGBO(0, 0, 255, 1)); + return recorder.endRecording(); +} + +Picture _drawBackground() { + final EnginePictureRecorder recorder = PictureRecorder(); + final RecordingCanvas canvas = + recorder.beginRecording(const Rect.fromLTRB(0, 0, 400, 400)); + canvas.drawRect( + Rect.fromLTWH(8, 8, 400.0 - 16, 400.0 - 16), + Paint() + ..style = PaintingStyle.fill + ..color = Color(0xFFE0FFE0) + ); + return recorder.endRecording(); +}