Skip to content

Commit dd5f870

Browse files
[web] Keep ImageFilter.blur bounds in equality and description (flutter#196013)
On the web, `ImageFilter.blur` accepted the optional `bounds` argument and then dropped it. As a result, a bounded blur compared equal to the matching unbounded blur, and its description had no `bounds:` part. Native keeps `bounds` in `==`, `hashCode` and `debugShortDescription`. This PR forwards `bounds` from the web `dart:ui` `ImageFilter.blur` to `EngineBlurImageFilter`, which now stores it and includes it in `==`, `hashCode` and `debugShortDescription`/`toString()`, in the same format as native `_GaussianBlurImageFilter` (`blur(10.1, 10.1, clamp, bounds: Rect.fromLTRB(...))`). Rendering does not change: every web backend still draws an unbounded blur, which is also what the native Skia backend does with `bounds`. With framework-visible behavior now matching native, 'ImageFilter resolves correctly' in `image_filter_config_test.dart` no longer needs `skip: kIsWeb`. Part of flutter#175899 ## Behavioral Changes - Web `ImageFilter.blur(..., bounds: r)` equality: - Before: equal to the same blur without `bounds` (and to one with any other `bounds`). - After: equal only to a blur with the same sigmas, tile mode and an equal `bounds`, and `hashCode` follows the same rule. As on native, a layer or widget whose filter changes only in `bounds` now counts as changed. - Web `ImageFilter.blur(sigmaX: 10.1, sigmaY: 10.1, tileMode: TileMode.clamp, bounds: Rect.fromLTRB(0.1, 0.1, 100.2, 100.2))` description: - Before: `debugShortDescription` was `blur(10.1, 10.1, clamp)`. - After: it is `blur(10.1, 10.1, clamp, bounds: Rect.fromLTRB(0.1, 0.1, 100.2, 100.2))` (`toString()` gets the `ImageFilter.` prefix), matching native. - Blurs without `bounds` behave exactly as before. Rendered output does not change on any web backend. ## Where to focus - **Primary review file:** `engine/src/flutter/lib/web_ui/lib/src/engine/primitives/image_filter.dart`. `bounds` is an optional, final field on `EngineBlurImageFilter` and takes part in `==`/`hashCode`/description, mirroring native `_GaussianBlurImageFilter` field for field (including the `_boundsString()` helper). - **Intentional non-change:** `getBackendFilter` and `filterBounds` ignore `bounds`, so every backend falls back to an unbounded blur, like the native Skia backend. Passing `bounds` through the renderer, so the Impeller-based skwasm variant can render a true bounded blur, is a separate follow-up. Because `bounds` is final, the existing per-instance backend filter cache (keyed on tile mode) stays valid. - **Tests:** `test/engine/surface/filters/image_filter_test.dart` uses one table-driven test for equality, hash, compose and description, and adds a check that bounded and unbounded blurs report the same `filterBounds`. It now calls `setUpUnitTests()` because `filterBounds` needs a renderer. Zero sigmas print as `0` on dart2js and `0.0` on dart2wasm, so the expected string is built with `0.0.toString()`. - **Framework test:** the framework's web CI runs against a web SDK built from the engine in the same commit, so the unskipped test uses the new `dart:ui` behavior. I checked it locally against a rebuilt web SDK on Chrome (JS and Wasm), and on the VM. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent e648016 commit dd5f870

4 files changed

Lines changed: 111 additions & 8 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/painting.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,13 @@ class ImageFilter {
645645
double sigmaX = 0.0,
646646
double sigmaY = 0.0,
647647
TileMode? tileMode,
648-
// ignore: avoid_unused_constructor_parameters
649648
Rect? bounds,
650-
}) => engine.EngineImageFilter.blur(sigmaX: sigmaX, sigmaY: sigmaY, tileMode: tileMode);
649+
}) => engine.EngineImageFilter.blur(
650+
sigmaX: sigmaX,
651+
sigmaY: sigmaY,
652+
tileMode: tileMode,
653+
bounds: bounds,
654+
);
651655

652656
factory ImageFilter.dilate({double radiusX = 0.0, double radiusY = 0.0}) =>
653657
engine.EngineImageFilter.dilate(radiusX: radiusX, radiusY: radiusY);

‎engine/src/flutter/lib/web_ui/lib/src/engine/primitives/image_filter.dart‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class EngineImageFilter implements ui.ImageFilter {
1919
required double sigmaX,
2020
required double sigmaY,
2121
required ui.TileMode? tileMode,
22+
ui.Rect? bounds,
2223
}) = EngineBlurImageFilter;
2324

2425
factory EngineImageFilter.color({required EngineColorFilter colorFilter}) =
@@ -109,12 +110,24 @@ class EngineColorFilterImageFilter extends EngineImageFilter {
109110
}
110111

111112
class EngineBlurImageFilter extends EngineImageFilter {
112-
EngineBlurImageFilter({required this.sigmaX, required this.sigmaY, required this.tileMode});
113+
EngineBlurImageFilter({
114+
required this.sigmaX,
115+
required this.sigmaY,
116+
required this.tileMode,
117+
this.bounds,
118+
});
113119

114120
final double sigmaX;
115121
final double sigmaY;
116122
final ui.TileMode? tileMode;
117123

124+
/// The optional bounds passed to [ui.ImageFilter.blur].
125+
///
126+
/// This is currently only kept for [operator ==], [hashCode] and
127+
/// [debugShortDescription]. It is not yet used for rendering, so the
128+
/// backends fall back to an unbounded blur, as native Skia does.
129+
final ui.Rect? bounds;
130+
118131
BackendImageFilter? _cachedFilter;
119132
ui.TileMode? _cachedTileMode;
120133

@@ -153,14 +166,18 @@ class EngineBlurImageFilter extends EngineImageFilter {
153166
return other is EngineBlurImageFilter &&
154167
other.sigmaX == sigmaX &&
155168
other.sigmaY == sigmaY &&
169+
other.bounds == bounds &&
156170
other.tileMode == tileMode;
157171
}
158172

159173
@override
160-
int get hashCode => Object.hash(sigmaX, sigmaY, tileMode);
174+
int get hashCode => Object.hash(sigmaX, sigmaY, bounds, tileMode);
161175

162176
@override
163-
String get debugShortDescription => 'blur($sigmaX, $sigmaY, ${tileModeString(tileMode)})';
177+
String get debugShortDescription =>
178+
'blur($sigmaX, $sigmaY, ${tileModeString(tileMode)}${_boundsString()})';
179+
180+
String _boundsString() => bounds == null ? '' : ', bounds: $bounds';
164181
}
165182

166183
class EngineMatrixImageFilter extends EngineImageFilter {

‎engine/src/flutter/lib/web_ui/test/engine/surface/filters/image_filter_test.dart‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import 'package:test/test.dart';
99
import 'package:ui/src/engine.dart';
1010
import 'package:ui/ui.dart';
1111

12+
import '../../../common/test_initialization.dart';
13+
1214
void main() {
1315
internalBootstrapBrowserTest(() => testMain);
1416
}
1517

1618
void testMain() {
19+
setUpUnitTests();
1720
group('ImageFilter constructors', () {
1821
test('matrix is copied', () {
1922
final matrix = Matrix4.identity();
@@ -75,5 +78,86 @@ void testMain() {
7578
expect(filter3, isNot(equals(filter2)));
7679
expect(filter3, filter3);
7780
});
81+
82+
test('blur bounds participate in ==, hashCode and description', () {
83+
const bounds1 = Rect.fromLTRB(0.5, 1.5, 10.5, 20.5);
84+
const bounds2 = Rect.fromLTRB(0.5, 1.5, 10.5, 30.5);
85+
ImageFilter blur({Rect? bounds}) =>
86+
ImageFilter.blur(sigmaX: 2.5, sigmaY: 3.5, tileMode: TileMode.decal, bounds: bounds);
87+
88+
final ImageFilter unbounded = blur();
89+
final ImageFilter bounded1a = blur(bounds: bounds1);
90+
final ImageFilter bounded1b = blur(bounds: bounds1);
91+
final ImageFilter bounded2 = blur(bounds: bounds2);
92+
93+
expect(bounded1a, bounded1b);
94+
expect(bounded1a.hashCode, bounded1b.hashCode);
95+
expect(bounded1a, isNot(equals(bounded2)));
96+
expect(bounded2, isNot(equals(bounded1a)));
97+
expect(bounded1a, isNot(equals(unbounded)));
98+
expect(unbounded, isNot(equals(bounded1a)));
99+
100+
// Built at runtime, so equal rects are distinct instances.
101+
Rect runtimeBounds() => Rect.fromLTRB(double.parse('0.5'), 1.5, 10.5, 20.5);
102+
final equalPairs = <(ImageFilter, ImageFilter)>[
103+
(blur(bounds: runtimeBounds()), blur(bounds: runtimeBounds())),
104+
// ignore: avoid_redundant_argument_values
105+
(ImageFilter.blur(sigmaX: 2.5, bounds: null), ImageFilter.blur(sigmaX: 2.5)),
106+
(ImageFilter.blur(bounds: runtimeBounds()), ImageFilter.blur(bounds: runtimeBounds())),
107+
];
108+
for (final (ImageFilter a, ImageFilter b) in equalPairs) {
109+
expect(a, b);
110+
expect(<ImageFilter>{a}.contains(b), isTrue);
111+
}
112+
113+
ImageFilter compose(ImageFilter inner) => ImageFilter.compose(outer: bounded2, inner: inner);
114+
expect(compose(bounded1a), compose(bounded1b));
115+
expect(compose(bounded1a).hashCode, compose(bounded1b).hashCode);
116+
expect(compose(bounded1a), isNot(equals(compose(unbounded))));
117+
expect(
118+
compose(bounded1a).debugShortDescription,
119+
'${bounded1a.debugShortDescription} -> ${bounded2.debugShortDescription}',
120+
);
121+
122+
final zero = 0.0.toString(); // '0' on dart2js, '0.0' on dart2wasm.
123+
final cases = <(ImageFilter, String)>[
124+
(unbounded, 'blur(2.5, 3.5, decal)'),
125+
(bounded1a, 'blur(2.5, 3.5, decal, bounds: Rect.fromLTRB(0.5, 1.5, 10.5, 20.5))'),
126+
(bounded2, 'blur(2.5, 3.5, decal, bounds: Rect.fromLTRB(0.5, 1.5, 10.5, 30.5))'),
127+
(
128+
ImageFilter.blur(sigmaX: 2.5, sigmaY: 3.5, bounds: bounds1),
129+
'blur(2.5, 3.5, unspecified, bounds: Rect.fromLTRB(0.5, 1.5, 10.5, 20.5))',
130+
),
131+
(
132+
blur(bounds: Rect.zero),
133+
'blur(2.5, 3.5, decal, bounds: Rect.fromLTRB(0.0, 0.0, 0.0, 0.0))',
134+
),
135+
(
136+
ImageFilter.blur(tileMode: TileMode.mirror, bounds: bounds1),
137+
'blur($zero, $zero, mirror, bounds: Rect.fromLTRB(0.5, 1.5, 10.5, 20.5))',
138+
),
139+
];
140+
for (final (ImageFilter filter, String description) in cases) {
141+
expect(filter.debugShortDescription, description);
142+
expect(filter.toString(), 'ImageFilter.$description');
143+
final blurFilter = filter as EngineBlurImageFilter;
144+
final copy = ImageFilter.blur(
145+
sigmaX: blurFilter.sigmaX,
146+
sigmaY: blurFilter.sigmaY,
147+
tileMode: blurFilter.tileMode,
148+
);
149+
expect(filter == copy, !description.contains('bounds:'), reason: description);
150+
}
151+
});
152+
153+
test('bounded blur has the same filter bounds as an unbounded blur', () {
154+
const input = Rect.fromLTRB(0, 0, 20, 20);
155+
Rect filterBounds(ImageFilter filter) => (filter as EngineImageFilter).filterBounds(input);
156+
for (final sigma in <double>[2.5, 0]) {
157+
final bounded = ImageFilter.blur(sigmaX: sigma, sigmaY: sigma, bounds: input.deflate(5));
158+
final unbounded = ImageFilter.blur(sigmaX: sigma, sigmaY: sigma);
159+
expect(filterBounds(bounded), filterBounds(unbounded), reason: 'sigma: $sigma');
160+
}
161+
});
78162
});
79163
}

‎packages/flutter/test/rendering/image_filter_config_test.dart‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:ui' as ui;
66

7-
import 'package:flutter/foundation.dart' show kIsWeb;
87
import 'package:flutter/rendering.dart';
98
import 'package:flutter_test/flutter_test.dart';
109

@@ -129,7 +128,6 @@ void main() {
129128
'blur(10.1, 10.1, clamp, bounds: Rect.fromLTRB(0.1, 0.1, 100.2, 100.2)) '
130129
'-> blur(10.1, 10.1, clamp)',
131130
);
132-
}, skip: kIsWeb); // `bounds` is currently not supported on Web.
133-
// https://github.com/flutter/flutter/issues/175899
131+
});
134132
});
135133
}

0 commit comments

Comments
 (0)