Skip to content

Commit

Permalink
Apply a clip rectangle if a color filter is used
Browse files Browse the repository at this point in the history
Skia's SkCanvas::saveLayer does not implicitly clip to the bounds rect.

See flutter/flutter#142620
  • Loading branch information
jason-simmons committed Feb 2, 2024
1 parent 8351705 commit 84c8531
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ class RenderPictureVectorGraphic extends RenderBox {
context.canvas.translate(offset.dx, offset.dy);
}
if (_opacityValue != 1.0 || colorFilter != null) {
context.canvas.clipRect(Offset.zero & size);
context.canvas.saveLayer(Offset.zero & size, colorPaint);
}
context.canvas.drawPicture(pictureInfo.picture);
Expand Down
35 changes: 35 additions & 0 deletions packages/vector_graphics/test/render_vector_graphics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,28 @@ void main() {

expect(data.dispose, returnsNormally);
});

test('Color filter applies clip', () async {
final RenderPictureVectorGraphic render = RenderPictureVectorGraphic(
pictureInfo,
const ui.ColorFilter.mode(Colors.green, ui.BlendMode.difference),
null,
);
render.layout(BoxConstraints.tight(const Size(50, 50)));
final FakePaintingContext context = FakePaintingContext();
render.paint(context, Offset.zero);

expect(context.canvas.lastClipRect,
equals(const ui.Rect.fromLTRB(0, 0, 50, 50)));
});
}

class FakeCanvas extends Fake implements Canvas {
ui.Image? lastImage;
Rect? lastSrc;
Rect? lastDst;
Paint? lastPaint;
Rect? lastClipRect;

@override
void drawImageRect(ui.Image image, Rect src, Rect dst, Paint paint) {
Expand All @@ -423,6 +438,26 @@ class FakeCanvas extends Fake implements Canvas {
lastDst = dst;
lastPaint = paint;
}

@override
void drawPicture(ui.Picture picture) {}

@override
int getSaveCount() {
return 0;
}

@override
void restoreToCount(int count) {}

@override
void saveLayer(Rect? bounds, Paint paint) {}

@override
void clipRect(ui.Rect rect,
{ui.ClipOp clipOp = ui.ClipOp.intersect, bool doAntiAlias = true}) {
lastClipRect = rect;
}
}

class FakeHistoryCanvas extends Fake implements Canvas {
Expand Down

0 comments on commit 84c8531

Please sign in to comment.