Tolerate floating-point rounding in paintImage centerSlice assertion (#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
S
Soroush Yousefpour committed
e6aa89eea34850cb1fd25d9f4d0b2fad379b00bf
Parent: 6f1ed6c
Committed by GitHub <noreply@github.com>
on 8/26/2026, 12:55:46 PM