[web] Implement stepped image downscaling for CanvasKit and Skwasm#184741
[web] Implement stepped image downscaling for CanvasKit and Skwasm#184741harryterkelsen wants to merge 9 commits intoflutter:masterfrom
Conversation
This change implements a high-quality stepped downscaling strategy (similar to mipmap generation) to resolve rendering artifacts (aliasing) caused by scaling down images by large factors (scale < 0.5) on the web. Key changes: - Added `ImageDownscaler` with a ref-counted cache for downscaled images linked to the image's source box. - Integrated the downscaler into `drawImageRect` for both CanvasKit and Skwasm renderers. - Conditioned the trigger on `FilterQuality.medium` or higher and scaling factor < 0.5 in both dimensions. - Handled cache eviction automatically when the source `CkImage` or `SkwasmImage` is fully disposed. - Added unit tests for the downscaling algorithm and caching disposal behavior. This fixes the jagged edges and pixelated artifacts seen when heavily downscaling large images in the web engine.
There was a problem hiding this comment.
Code Review
This pull request implements iterative downscaling for images in the Web UI to improve rendering quality when scaling down significantly, adding a DownscaledImageCache and updating drawImageRect in CanvasKit and Skwasm. Feedback indicates that the source rectangle is currently ignored in the downscaling logic and cache keys, which will cause incorrect rendering for sub-regions and sprite sheets. Additionally, the logic should be restricted to target dimensions of at least 1x1 to prevent potential exceptions, and the source rectangle must be passed through the downscaling functions.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request implements iterative downscaling for the web engine's CanvasKit and Skwasm renderers to improve image quality during significant downscaling. It introduces a caching mechanism for downscaled variants and updates drawImageRect to use stepped downscaling when FilterQuality is medium or higher. Feedback identifies potential memory growth in the cache during animations, logic errors in the downscaling loop for extreme aspect ratios, and opportunities to optimize GPU memory by disposing of intermediate textures immediately. Additionally, using lower filter quality for intermediate steps in Skwasm is recommended for performance and consistency.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces iterative downscaling for images in the Web UI to mitigate aliasing artifacts, adding a DownscaledImageCache and a stepped scaling algorithm for CanvasKit and Skwasm. Feedback includes broadening the downscaling trigger to support non-uniform scaling, using triple-slash documentation for private members as per the repository style guide, and simplifying the intermediate image disposal logic within the scaling loop.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces an iterative downscaling mechanism for images in the Flutter web engine, specifically for CanvasKit and Skwasm renderers. It adds a new image_downscaler.dart utility to handle high-quality downscaling and caching, and updates the drawImageRect methods in both renderers to utilize this logic when appropriate. The review feedback suggests optimizing performance by reusing paint objects across intermediate downscaling steps and notes potential future improvements for cache key management.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request implements iterative downscaling for the web engine to address aliasing artifacts during significant downscaling operations. It introduces a DownscaledImageCache and integrates it into the drawImageRect implementations for both CanvasKit and Skwasm. Feedback identifies an optimization to lazily initialize paint objects, avoiding unnecessary allocations when a downscaled image is already present in the cache.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request implements iterative downscaling for the web engine's CanvasKit and Skwasm backends to improve image quality during large downscaling operations. It introduces a DownscaledImageCache to manage intermediate downscaled variants and logic to perform stepped downscaling. A review comment suggests refactoring the createSteppedDownscaledImage function to extract a helper for the drawing logic, which would reduce code duplication between the iterative loop and the final output step.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces iterative downscaling for the CanvasKit and Skwasm web engines to improve image quality during significant downscaling operations. It adds a new DownscaledImageCache and logic to perform downscaling in multiple steps. Review feedback indicates that the cache disposal logic in CkImage and SkwasmImage may prematurely evict entries when image clones are involved, as it triggers on the disposal of a single clone rather than the underlying reference-counted object. Additionally, it is suggested to refine the stepping threshold in createSteppedDownscaledImage to strictly maintain a maximum 2x downscale per iteration.
|
Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change). If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review. For more guidance, visit Writing a golden file test for Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
This change implements a high-quality stepped downscaling strategy (similar to mipmap generation) to resolve rendering artifacts (aliasing) caused by scaling down images by large factors (scale < 0.5) on the web.
Key changes:
ImageDownscalerwith a ref-counted cache for downscaled images linked to the image's source box.drawImageRectfor both CanvasKit and Skwasm renderers.FilterQuality.mediumor higher and scaling factor < 0.5 in both dimensions.CkImageorSkwasmImageis fully disposed.This fixes the jagged edges and pixelated artifacts seen when heavily downscaling large images in the web engine.
Before:

After:

Fixes #135655
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.