Repository navigation
[engine] Resize decoded images in 2:1 steps to fix cacheWidth aliasing - #196718
mohamedalsamrai wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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 Static WebP already decodes straight to the 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. |
|
Makes sense, thanks @gaaclarke for the review. I'll open the two PRs. |
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
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, theImagewidget's default, which is what the issue asks for.The logic is in two new helpers,
ImageDecoder::GetResizeStepSizeandImageDecoder::ResizePixels, and every resize path now uses them:ResizeRasterImage.ResizeOnCpu, used when the image is larger than the max texture size or texture-to-texture blits aren't supported.UnsafeUploadTextureToPrivatedoes the halving withBlitPass::ResizeTextureinto 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.ResizeFrameinmulti_frame_codec.cc, added in [engine] Honor targetWidth/targetHeight for animated images #194615.Why not mipmaps?
SkMipmapMode::kLinearon the CPU would be a one-line change, but:SkMipmaphas no downsampler forkBGR_101010x_XRorkRGBA_F32, so wide-gamut and float images would quietly keep aliasing.SkResourceCacheuntil the cache is used again.Blitting from a mip level of the source on the GPU would need a mip level parameter on
ResizeTexturein 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
Tests
ImageDecoderNoGLTest.GetResizeStepSizeHalvesTowardsTargetSizeImageDecoderNoGLTest.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 throughDecompressTexturewithout 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 throughImageDecoderSkia. It's in the GL-only test file, so it doesn't run on macOS.ImpellerUploadTracksSchedulingReceiptno longer expects aGetBackendTypecall, 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.Fixes #193779
Pre-launch Checklist
///).