Skip to content

[engine] Resize decoded images in 2:1 steps to fix cacheWidth aliasing - #196718

Closed
mohamedalsamrai wants to merge 1 commit into
flutter:masterfrom
mohamedalsamrai:fix-resize-aliasing-193779
Closed

mohamedalsamrai wants to merge 1 commit into
flutter:masterfrom
mohamedalsamrai:fix-resize-aliasing-193779

Conversation

@mohamedalsamrai

@mohamedalsamrai mohamedalsamrai commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Decode-time resizing (cacheWidth/cacheHeight, ResizeImage, targetWidth/targetHeight) scales the image in a single linear step. A linear filter only reads the 2x2 source pixels around each sample, so when the image shrinks by more than 2x, most of the source is skipped and thin lines break up. In the issue's case (a 1024px logo shown at 120px), each output pixel covers about 8.5x8.5 source pixels and reads 4 of them.

This PR resizes in steps instead. Every side that is at least twice as long as the target is halved first (a 2:1 linear step averages two pixels per side, like building a mipmap level), then one final linear step scales by less than 2x. The result is close to what the GPU draws for the full image with FilterQuality.medium, the Image widget's default, which is what the issue asks for.

The logic is in two new helpers, ImageDecoder::GetResizeStepSize and ImageDecoder::ResizePixels, and every resize path now uses them:

  • Skia: ResizeRasterImage.
  • Impeller on the CPU: ResizeOnCpu, used when the image is larger than the max texture size or texture-to-texture blits aren't supported.
  • Impeller on the GPU: UnsafeUploadTextureToPrivate does the halving with BlitPass::ResizeTexture into intermediate textures, so no backend changes are needed. The full-size texture also stops getting a mip chain when it's about to be resized. On Vulkan and GLES that chain was generated and never read.
  • Animated images: ResizeFrame in multi_frame_codec.cc, added in [engine] Honor targetWidth/targetHeight for animated images #194615.

Why not mipmaps?

SkMipmapMode::kLinear on the CPU would be a one-line change, but:

  • SkMipmap has no downsampler for kBGR_101010x_XR or kRGBA_F32, so wide-gamut and float images would quietly keep aliasing.
  • Skia keeps the mip chain of the last resize in the global SkResourceCache until the cache is used again.
  • The GPU path can't filter trilinearly in a blit, so the two paths would resize differently.

Blitting from a mip level of the source on the GPU would need a mip level parameter on ResizeTexture in every backend (MPS on Metal would need a texture view), and a mip level halves both sides, which doesn't fit targets with a different aspect ratio.

Cost

  • Vulkan and GLES: no more blits than before and about the same transient memory. The intermediate textures (under 1/3 of the source when the aspect ratio is kept) replace the mip chain that was never read.
  • Metal: under 1/3 of the source size in extra transient textures, plus a few MPS dispatches.
  • CPU: under 1/3 of the source pixels written in the extra steps, small next to decoding.
  • If only one side gets halved, the intermediate steps can add up to about the size of the source.

Tests

  • ImageDecoderNoGLTest.GetResizeStepSizeHalvesTowardsTargetSize
  • ImageDecoderNoGLTest.ResizePixelsDoesNotAliasThinLines: 1px black and white stripes, 256px to 30px, must come out flat grey. A single linear step gives anything from almost black to almost white.
  • ImageDecoderNoGLTest.ImpellerCpuResizeDoesNotAliasThinLines: the same check through DecompressTexture without blit support.
  • ImageDecoderNoGLTest.ImpellerGpuResizeHalvesTowardsTargetSize: checks the blit sizes with mocks (256 → 128 → 64 → 32 → 30) and that only the final texture gets mipmaps.
  • ImageDecoderTest.SkiaResizeDoesNotAliasThinLines: the same check through ImageDecoderSkia. It's in the GL-only test file, so it doesn't run on macOS.
  • ImpellerUploadTracksSchedulingReceipt no longer expects a GetBackendType call, since it isn't called when there's no resize.

I don't have the disk space to build the engine locally, so these tests run for the first time on CI. The comparison below is a simulation with Skia's SkPixmap::scalePixels (skia-python), not a device capture. For thin rings at 1024px resized to 120px, the PSNR against the GPU-drawn full image goes from 12.3 dB to 39.8 dB.

before_after_193779

Fixes #193779

Pre-launch Checklist

@github-actions github-actions Bot added the engine flutter/engine related. See also e: labels. label Nov 16, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a step-by-step image resizing mechanism to prevent aliasing when scaling down images by more than 2x. It adds GetResizeStepSize and ResizePixels helper methods to ImageDecoder and integrates them across Skia, Impeller (both CPU and GPU paths), and multi-frame codec image decoding paths. Additionally, comprehensive unit tests are added to verify that the resizing logic works correctly and prevents aliasing on thin lines. There are no review comments, so I have no feedback to provide.

@gaaclarke
gaaclarke self-requested a review November 17, 2026 21:33

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mohamedalsamrai I'm not a huge fan of this. The reason why is that we are making the experience of loading large images better at a cost to complexity and performance when really if people care they shouldn't be loading such large images. It's like making a car with an armored side so people can keep driving while crashing into the median =)

I think as an alternative we can edit our webp decoder path to do the resizing while decoding, which will give you the behavior you want while also having the benefit of using way less memory. Then we should just update our documentation that if someone wants to get this behavior they should use webp.

@mohamedalsamrai

Copy link
Copy Markdown
Contributor Author

Hey @mohamedalsamrai I'm not a huge fan of this. The reason why is that we are making the experience of loading large images better at a cost to complexity and performance when really if people care they shouldn't be loading such large images. It's like making a car with an armored side so people can keep driving while crashing into the median =)

I think as an alternative we can edit our webp decoder path to do the resizing while decoding, which will give you the behavior you want while also having the benefit of using way less memory. Then we should just update our documentation that if someone wants to get this behavior they should use webp.

Ha fair In my defense, the driver is following Flutter's GPS. The debugInvertOversizedImages error tells people to add a cacheWidth, and with a PNG that road ends at the median :)

Static WebP already decodes straight to the cacheWidth size (SkWebpCodec is an SkScalingCodec), so for WebP it's only docs. Did you mean animated WebP? MultiFrameCodec still decodes full frames and resizes them after.

If you'd rather leave PNG alone, I can close this and send two small PRs instead, one for the docs and one that stops generating the mip chain for the full-size texture before it's resized, since Vulkan and GLES never read it. Which do you prefer?

@gaaclarke

Copy link
Copy Markdown
Member

If you'd rather leave PNG alone, I can close this and send two small PRs instead, one for the docs and one that stops generating the mip chain for the full-size texture before it's resized, since Vulkan and GLES never read it. Which do you prefer?

I think that's a good step in the right direction. Thanks @mohamedalsamrai. I'll close this for now, maybe we'll change our mind later. I think if we are in doubt, it's better to chose simplicity.

@gaaclarke gaaclarke closed this Nov 19, 2026
@mohamedalsamrai

Copy link
Copy Markdown
Contributor Author

Makes sense, thanks @gaaclarke for the review. I'll open the two PRs.

opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-flutter__flutter that referenced this pull request Nov 25, 2026
flutter#197013)

When a decoded image is resized on the GPU,
`UnsafeUploadTextureToPrivate` uploads it to a full-size texture and
blits that into a texture of the target size. On Vulkan and GLES the
full-size texture also got a full mip chain, but the resize blit only
reads level 0 and the texture isn't used after it, so those mipmaps were
never read. Metal already skipped them. This skips them on every
backend, which saves the mip generation blits and the memory of the mip
levels (about a third of the full-size texture).

Only the resized texture gets mipmaps now. The new
`ImpellerGpuResizeOnlyGeneratesMipmapsForResizedTexture` test checks
this with mocks, and `ImpellerUploadTracksSchedulingReceipt` no longer
expects a `GetBackendType` call, since it isn't called when the image
isn't resized.

This follows the discussion in flutter#196718.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant in-code documentation (doc comments with
`///`).
- [x] If this PR introduces a new feature or capability, I created and
linked a website documentation issue or PR in [flutter/website] (or
verified none is needed).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[flutter/website]: https://github.com/flutter/website
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine flutter/engine related. See also e: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cacheWidth/cacheHeight decode-time resize aliases large downscales (bilinear without mipmaps)

2 participants