Skip to content

Commit e6aa89e

Browse files
authored
Tolerate floating-point rounding in paintImage centerSlice assertion (flutter#185789)
## Summary `paintImage` uses a strict `==` comparison between `sourceSize` and `inputSize` to verify that the chosen `BoxFit` does not drop image pixels when `centerSlice` is set. The check has two failure modes that aren't actually bugs in the caller: 1. When the image's pixel dimensions are not evenly divisible by `scale`, the `inputSize / scale * scale` round trip lands a few ULPs away from the original `inputSize`. The assertion fires under strict equality even though the math is correct. 2. When the destination rect is too small to fit the `centerSlice` borders, `outputSize` becomes non-positive and `applyBoxFit` short-circuits to `Size.zero`. The assertion then compares `(0,0)` against the inner-input size and fires for a reason unrelated to what it is meant to catch (this is the configuration in the linked issue's repro). This change: - Compares with `precisionErrorTolerance` instead of `==`, absorbing the FP rounding. - Skips the assertion entirely when the centerSlice borders can't fit the destination rect (so the assertion no longer fires for the degenerate case it wasn't designed to police). The original safety check — that the chosen `BoxFit` didn't shrink the source — is preserved for the cases it was meant to catch. ## Test plan - [x] Added `centerSlice tolerates floating-point rounding when scale divides unevenly` — exercises the FP-tolerance path with a destination rect large enough to enclose the borders. - [x] Added `centerSlice does not throw when the destination rect is smaller than the slice borders` — reproduces the exact configuration in the linked issue (208×142 image, scale 3, 1×1 centerSlice into a 100×46 rect) which previously failed the assertion. - [x] `flutter test packages/flutter/test/painting/paint_image_test.dart` — all tests pass. - [x] `flutter test packages/flutter/test/painting/decoration_test.dart packages/flutter/test/rendering/image_test.dart` — pass. - [x] `flutter analyze` — no issues on the changed files. Fixes flutter#27827
1 parent 6f1ed6c commit e6aa89e

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎packages/flutter/lib/src/painting/decoration_image.dart‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,16 @@ void paintImage({
553553
Size outputSize = rect.size;
554554
var inputSize = Size(image.width.toDouble(), image.height.toDouble());
555555
Offset? sliceBorder;
556+
// Tracks whether the destination rect is large enough to fit the
557+
// centerSlice's outer borders. When it is not, [applyBoxFit] is called with
558+
// a non-positive size and degenerates to [Size.zero], which would otherwise
559+
// trip the source/input-size assertion below for an unrelated reason.
560+
var sliceFits = true;
556561
if (centerSlice != null) {
557562
sliceBorder = inputSize / scale - centerSlice.size as Offset;
558563
outputSize = outputSize - sliceBorder as Size;
559564
inputSize = inputSize - sliceBorder * scale as Size;
565+
sliceFits = outputSize.width > 0.0 && outputSize.height > 0.0;
560566
}
561567
fit ??= centerSlice == null ? BoxFit.scaleDown : BoxFit.fill;
562568
assert(centerSlice == null || (fit != BoxFit.none && fit != BoxFit.cover));
@@ -567,9 +573,14 @@ void paintImage({
567573
outputSize += sliceBorder!;
568574
destinationSize += sliceBorder;
569575
// We don't have the ability to draw a subset of the image at the same time
570-
// as we apply a nine-patch stretch.
576+
// as we apply a nine-patch stretch. A tolerance is used to absorb
577+
// floating-point rounding from the `inputSize / scale * scale` round trip
578+
// when the image's pixel dimensions are not evenly divisible by `scale`.
579+
final sourceInputDelta = (sourceSize - inputSize) as Offset;
571580
assert(
572-
sourceSize == inputSize,
581+
!sliceFits ||
582+
(sourceInputDelta.dx.abs() <= precisionErrorTolerance &&
583+
sourceInputDelta.dy.abs() <= precisionErrorTolerance),
573584
'centerSlice was used with a BoxFit that does not guarantee that the image is fully visible.',
574585
);
575586
}

‎packages/flutter/test/painting/paint_image_test.dart‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ class TestCanvas implements Canvas {
2020
void main() {
2121
late ui.Image image300x300;
2222
late ui.Image image300x200;
23+
late ui.Image image208x142;
2324

2425
setUpAll(() async {
2526
image300x300 = await createTestImage(width: 300, height: 300, cache: false);
2627
image300x200 = await createTestImage(width: 300, height: 200, cache: false);
28+
image208x142 = await createTestImage(width: 208, height: 142, cache: false);
2729
});
2830

2931
setUp(() {
@@ -180,6 +182,51 @@ void main() {
180182
expect(command.positionalArguments[2], equals(const Rect.fromLTRB(20.0, 40.0, 860.0, 840.0)));
181183
});
182184

185+
test('centerSlice tolerates floating-point rounding when scale divides unevenly', () async {
186+
// Regression test for https://github.com/flutter/flutter/issues/27827.
187+
// With image=208x142, scale=3 and a centerSlice large enough that the
188+
// destination rect still encloses the borders, `inputSize / scale * scale`
189+
// does not exactly round-trip through floating-point. The strict-equality
190+
// assertion that used to live here would fail by a few ULPs.
191+
final canvas = TestCanvas();
192+
paintImage(
193+
canvas: canvas,
194+
rect: const Rect.fromLTWH(0.0, 0.0, 200.0, 100.0),
195+
image: image208x142,
196+
scale: 3.0,
197+
centerSlice: const Rect.fromLTRB(15.0, 15.0, 17.0, 17.0),
198+
);
199+
200+
final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
201+
return invocation.memberName == #drawImageNine;
202+
});
203+
expect(command.positionalArguments[0], equals(image208x142));
204+
});
205+
206+
test(
207+
'centerSlice does not throw when the destination rect is smaller than the slice borders',
208+
() async {
209+
// Regression test for https://github.com/flutter/flutter/issues/27827.
210+
// With a 100x46 rect and a 208x142 image at scale 3, subtracting the
211+
// slice borders from `outputSize` makes it non-positive. `applyBoxFit`
212+
// then degenerates to `Size.zero` and the legacy strict-equality
213+
// assertion fired against the inner-input size for that unrelated reason.
214+
final canvas = TestCanvas();
215+
paintImage(
216+
canvas: canvas,
217+
rect: const Rect.fromLTWH(0.0, 0.0, 100.0, 46.0),
218+
image: image208x142,
219+
scale: 3.0,
220+
centerSlice: const Rect.fromLTRB(15.5, 15.5, 16.5, 16.5),
221+
);
222+
223+
final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
224+
return invocation.memberName == #drawImageNine;
225+
});
226+
expect(command.positionalArguments[0], equals(image208x142));
227+
},
228+
);
229+
183230
testWidgets('Reports Image painting', (WidgetTester tester) async {
184231
late ImageSizeInfo imageSizeInfo;
185232
var count = 0;

0 commit comments

Comments
 (0)