Skip to content

Commit f0b10c5

Browse files
authored
[web] Prevent using DOM nodes for canvas with large number of draws (#22064)
1 parent 4f4599b commit f0b10c5

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

lib/web_ui/lib/src/engine/bitmap_canvas.dart

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,18 @@ class BitmapCanvas extends EngineCanvas {
324324
/// DOM to render correctly.
325325
/// - Pictures typically have large rect/rounded rectangles as background
326326
/// prefer DOM if canvas has not been allocated yet.
327+
///
328+
/// Future optimization: The check below can be used to prevent excessive
329+
/// canvas sandwiching (switching between dom and multiple canvas(s)).
330+
/// Once RecordingCanvas is updated to detect switch count, this can be
331+
/// enabled.
332+
/// (_canvasPool._canvas == null &&
333+
/// paint.maskFilter == null &&
334+
/// paint.shader == null &&
335+
/// paint.style != ui.PaintingStyle.stroke)
336+
///
327337
bool _useDomForRendering(SurfacePaintData paint) =>
328-
_preserveImageData == false && (
329-
_contains3dTransform ||
330-
(_canvasPool._canvas == null &&
331-
paint.maskFilter == null &&
332-
paint.shader == null &&
333-
paint.style != ui.PaintingStyle.stroke));
338+
_preserveImageData == false && _contains3dTransform;
334339

335340
@override
336341
void drawColor(ui.Color color, ui.BlendMode blendMode) {
@@ -488,10 +493,13 @@ class BitmapCanvas extends EngineCanvas {
488493
html.Element svgElm = _pathToSvgElement(
489494
surfacePath, paint, '${pathBounds.right}', '${pathBounds.bottom}');
490495
if (!_canvasPool.isClipped) {
491-
svgElm.style
492-
..transform = matrix4ToCssTransform(transform)
493-
..transformOrigin = '0 0 0'
494-
..position = 'absolute';
496+
html.CssStyleDeclaration style = svgElm.style;
497+
style.position = 'absolute';
498+
if (!transform.isIdentity()) {
499+
style
500+
..transform = matrix4ToCssTransform(transform)
501+
..transformOrigin = '0 0 0';
502+
}
495503
}
496504
_drawElement(svgElm, ui.Offset(0, 0), paint);
497505
} else {

lib/web_ui/test/golden_tests/engine/backdrop_filter_golden_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ void testMain() async {
6060
.build()
6161
.webOnlyRootElement);
6262

63-
await matchGoldenFile('backdrop_filter_clip.png', region: region);
63+
await matchGoldenFile('backdrop_filter_clip.png', region: region,
64+
maxDiffRatePercent: 0.8);
6465
});
6566

6667
test('Background should only blur at ancestor clip boundary after move', () async {
@@ -110,7 +111,8 @@ void testMain() async {
110111
.build()
111112
.webOnlyRootElement);
112113

113-
await matchGoldenFile('backdrop_filter_clip_moved.png', region: region);
114+
await matchGoldenFile('backdrop_filter_clip_moved.png', region: region,
115+
maxDiffRatePercent: 0.8);
114116
});
115117
}
116118

0 commit comments

Comments
 (0)