Skip to content

Commit da30eec

Browse files
gabrimaticRenzo-Olivares
authored andcommitted
Avoid centerSlice assertion failure under floating-point rounding
The centerSlice path in `paintImage` used a strict `==` comparison between `sourceSize` and `inputSize`. 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`, and the assertion fires for what is otherwise a valid configuration. The same code path also fires when the destination rect is too small to fit the centerSlice borders: `outputSize` becomes non-positive, `applyBoxFit` degenerates to `Size.zero`, and the assertion compares against zero for an unrelated reason. This change tolerates double-precision rounding via `precisionErrorTolerance` and skips the assertion entirely when the slice borders cannot fit, which preserves the original safety check for real BoxFit-induced source dropping while no longer breaking on either edge case. Refs flutter#27827
1 parent 08d2a47 commit da30eec

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

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

Lines changed: 12 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,13 @@ 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`.
571579
assert(
572-
sourceSize == inputSize,
580+
!sliceFits ||
581+
((sourceSize.width - inputSize.width).abs() <= precisionErrorTolerance &&
582+
(sourceSize.height - inputSize.height).abs() <= precisionErrorTolerance),
573583
'centerSlice was used with a BoxFit that does not guarantee that the image is fully visible.',
574584
);
575585
}

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

Lines changed: 44 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,48 @@ 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('centerSlice does not throw when the destination rect is smaller than the slice borders', () async {
207+
// Regression test for https://github.com/flutter/flutter/issues/27827.
208+
// With a 100x46 rect and a 208x142 image at scale 3, subtracting the
209+
// slice borders from `outputSize` makes it non-positive. `applyBoxFit`
210+
// then degenerates to `Size.zero` and the legacy strict-equality
211+
// assertion fired against the inner-input size for that unrelated reason.
212+
final canvas = TestCanvas();
213+
paintImage(
214+
canvas: canvas,
215+
rect: const Rect.fromLTWH(0.0, 0.0, 100.0, 46.0),
216+
image: image208x142,
217+
scale: 3.0,
218+
centerSlice: const Rect.fromLTRB(15.5, 15.5, 16.5, 16.5),
219+
);
220+
221+
final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
222+
return invocation.memberName == #drawImageNine;
223+
});
224+
expect(command.positionalArguments[0], equals(image208x142));
225+
});
226+
183227
testWidgets('Reports Image painting', (WidgetTester tester) async {
184228
late ImageSizeInfo imageSizeInfo;
185229
var count = 0;

0 commit comments

Comments
 (0)