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

Commit a38cabf

Browse files
committed
cull zero-sized rects in ui.Canvas when filling them
1 parent 187322c commit a38cabf

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

lib/ui/painting.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5857,6 +5857,9 @@ base class _NativeCanvas extends NativeFieldWrapperClass1 implements Canvas {
58575857
void clipRect(Rect rect, { ClipOp clipOp = ClipOp.intersect, bool doAntiAlias = true }) {
58585858
assert(_rectIsValid(rect));
58595859
rect = _sorted(rect);
5860+
// Even if rect is still empty - which implies it has a zero dimension -
5861+
// we still need to perform the clipRect operation as it will effectively
5862+
// nullify any further rendering until the next restore call.
58605863
_clipRect(rect.left, rect.top, rect.right, rect.bottom, clipOp.index, doAntiAlias);
58615864
}
58625865

@@ -5930,7 +5933,9 @@ base class _NativeCanvas extends NativeFieldWrapperClass1 implements Canvas {
59305933
void drawRect(Rect rect, Paint paint) {
59315934
assert(_rectIsValid(rect));
59325935
rect = _sorted(rect);
5933-
_drawRect(rect.left, rect.top, rect.right, rect.bottom, paint._objects, paint._data);
5936+
if (paint.style != PaintingStyle.fill || !rect.isEmpty) {
5937+
_drawRect(rect.left, rect.top, rect.right, rect.bottom, paint._objects, paint._data);
5938+
}
59345939
}
59355940

59365941
@Native<Void Function(Pointer<Void>, Double, Double, Double, Double, Handle, Handle)>(symbol: 'Canvas::drawRect')
@@ -5959,7 +5964,9 @@ base class _NativeCanvas extends NativeFieldWrapperClass1 implements Canvas {
59595964
void drawOval(Rect rect, Paint paint) {
59605965
assert(_rectIsValid(rect));
59615966
rect = _sorted(rect);
5962-
_drawOval(rect.left, rect.top, rect.right, rect.bottom, paint._objects, paint._data);
5967+
if (paint.style != PaintingStyle.fill || !rect.isEmpty) {
5968+
_drawOval(rect.left, rect.top, rect.right, rect.bottom, paint._objects, paint._data);
5969+
}
59635970
}
59645971

59655972
@Native<Void Function(Pointer<Void>, Double, Double, Double, Double, Handle, Handle)>(symbol: 'Canvas::drawOval')

testing/dart/canvas_test.dart

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,37 +568,71 @@ void main() async {
568568
..color = color
569569
..strokeWidth = 5.0;
570570

571+
final Rect tallThin = Rect.fromLTRB(
572+
min(rect.left, rect.right) - 10,
573+
rect.top,
574+
min(rect.left, rect.right) - 10,
575+
rect.bottom,
576+
);
577+
final Rect wideThin = Rect.fromLTRB(
578+
rect.left,
579+
min(rect.top, rect.bottom) - 10,
580+
rect.right,
581+
min(rect.top, rect.bottom) - 10,
582+
);
583+
571584
canvas.save();
572585
canvas.translate(x, y);
573586

574587
paint.style = PaintingStyle.fill;
575588
canvas.drawRect(rect, paint);
589+
canvas.drawRect(tallThin, paint);
590+
canvas.drawRect(wideThin, paint);
576591

577592
canvas.save();
578593
canvas.translate(0, 100);
579594
paint.style = PaintingStyle.stroke;
580595
canvas.drawRect(rect, paint);
596+
canvas.drawRect(tallThin, paint);
597+
canvas.drawRect(wideThin, paint);
581598
canvas.restore();
582599

583-
584600
canvas.save();
585601
canvas.translate(100, 0);
586602
paint.style = PaintingStyle.fill;
587603
canvas.drawOval(rect, paint);
604+
canvas.drawOval(tallThin, paint);
605+
canvas.drawOval(wideThin, paint);
588606
canvas.restore();
589607

590608
canvas.save();
591609
canvas.translate(100, 100);
592610
paint.style = PaintingStyle.stroke;
593611
canvas.drawOval(rect, paint);
612+
canvas.drawOval(tallThin, paint);
613+
canvas.drawOval(wideThin, paint);
594614
canvas.restore();
595615

596616
canvas.save();
597617
canvas.translate(50, 50);
618+
619+
canvas.save();
598620
canvas.clipRect(rect);
599621
canvas.drawPaint(paint);
600622
canvas.restore();
601623

624+
canvas.save();
625+
canvas.clipRect(tallThin);
626+
canvas.drawPaint(paint);
627+
canvas.restore();
628+
629+
canvas.save();
630+
canvas.clipRect(wideThin);
631+
canvas.drawPaint(paint);
632+
canvas.restore();
633+
634+
canvas.restore();
635+
602636
canvas.restore();
603637
}
604638

0 commit comments

Comments
 (0)