Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[canvaskit] Fix incorrect calculation of ImageFilter paint bounds #54980

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions lib/web_ui/lib/src/engine/canvaskit/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,32 +415,34 @@ class ImageFilterEngineLayer extends ContainerLayer
} else {
convertible = _filter as CkManagedSkImageFilterConvertible;
}
final ui.Rect childPaintBounds =
ui.Rect childPaintBounds =
prerollChildren(prerollContext, childMatrix);
if (_filter is ui.ColorFilter) {
// If the filter is a ColorFilter, the extended paint bounds will be the
// entire screen, which is not what we want.
paintBounds = childPaintBounds;
} else {
convertible.withSkImageFilter((skFilter) {
paintBounds = rectFromSkIRect(
skFilter.getOutputBounds(toSkRect(childPaintBounds)),
);
});
}
childPaintBounds = childPaintBounds.translate(_offset.dx, _offset.dy);
if (_filter is ui.ColorFilter) {
// If the filter is a ColorFilter, the extended paint bounds will be the
// entire screen, which is not what we want.
paintBounds = childPaintBounds;
} else {
convertible.withSkImageFilter((skFilter) {
paintBounds = rectFromSkIRect(
skFilter.getOutputBounds(toSkRect(childPaintBounds)),
);
});
}
prerollContext.mutatorsStack.pop();
}

@override
void paint(PaintContext paintContext) {
assert(needsPainting);
final ui.Rect offsetPaintBounds = paintBounds.shift(-_offset);
paintContext.internalNodesCanvas.save();
paintContext.internalNodesCanvas.translate(_offset.dx, _offset.dy);
paintContext.internalNodesCanvas
.clipRect(paintBounds, ui.ClipOp.intersect, false);
.clipRect(offsetPaintBounds, ui.ClipOp.intersect, false);
final CkPaint paint = CkPaint();
paint.imageFilter = _filter;
paintContext.internalNodesCanvas.saveLayer(paintBounds, paint);
paintContext.internalNodesCanvas.saveLayer(offsetPaintBounds, paint);
paintChildren(paintContext);
paintContext.internalNodesCanvas.restore();
paintContext.internalNodesCanvas.restore();
Expand Down
25 changes: 25 additions & 0 deletions lib/web_ui/test/ui/scene_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,31 @@ Future<void> testMain() async {
await matchGoldenFile('scene_builder_image_filter.png', region: region);
});

// Regression test for https://github.com/flutter/flutter/issues/154303
test('image filter layer with offset', () async {
final ui.SceneBuilder sceneBuilder = ui.SceneBuilder();

sceneBuilder.pushClipRect(const ui.Rect.fromLTWH(100, 100, 100, 100));
sceneBuilder.pushImageFilter(
ui.ImageFilter.blur(
sigmaX: 5.0,
sigmaY: 5.0,
),
offset: const ui.Offset(100, 100),
);

sceneBuilder.addPicture(ui.Offset.zero, drawPicture((ui.Canvas canvas) {
canvas.drawCircle(const ui.Offset(50, 50), 25,
ui.Paint()..color = const ui.Color(0xFF00FF00));
}));

await renderScene(sceneBuilder.build());
await matchGoldenFile(
'scene_builder_image_filter_with_offset.png',
region: region,
);
});

test('color filter layer', () async {
final ui.SceneBuilder sceneBuilder = ui.SceneBuilder();
const ui.ColorFilter sepia = ui.ColorFilter.matrix(<double>[
Expand Down