Skip to content

Commit 264ca2c

Browse files
authored
[Impeller] Keep the aspect ratio when shrinking images to the max texture size (flutter#194010)
Impeller clamps each side of a decoded image to the max texture size on its own, so an image bigger than the limit on one side gets squished. A 1000x50000 image on a device with a 16384 limit comes out 1000x16384. This shrinks both sides by the same factor instead, so it comes out 328x16384 and keeps its shape. The image attached to flutter#177239 (6984x32975) comes out 6984x16384 today and 3470x16384 with this change. We hit this in immich when the original of a long image loads (immich-app/immich#11987, immich-app/immich#31690). For now we cap the decode size in the app (immich-app/immich#29367), but every app that shows big images needs the same workaround. The existing `VerifySimpleDecoding` case expected the squish (a 60x20 target with a 10x10 limit came back 10x10), so it now expects 10x3, and a tall case (20x60, expects 3x10) is added. Before and after on a Pixel 7 (Vulkan, 32768 limit) with a 1000x50000 image of circles: <img width="1128" height="2254" alt="case1-A-vs-B" src="https://github.com/user-attachments/assets/4eabb7ee-7dc2-4a65-a50f-fc1b5a597be4" /> Fixes flutter#177239 ## 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]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] 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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- 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/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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
1 parent f408b4d commit 264ca2c

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/lib/ui/painting/image_decoder_impeller.h"
66

7+
#include <cmath>
78
#include <format>
89
#include <memory>
910

@@ -361,11 +362,22 @@ ImageDecoderImpeller::DecompressTexture(
361362

362363
const SkISize source_size = SkISize::Make(descriptor->image_info().width,
363364
descriptor->image_info().height);
364-
const SkISize target_size =
365-
SkISize::Make(std::min(max_texture_size.width,
366-
static_cast<int64_t>(options.target_width)),
367-
std::min(max_texture_size.height,
368-
static_cast<int64_t>(options.target_height)));
365+
SkISize target_size =
366+
SkISize::Make(options.target_width, options.target_height);
367+
// Shrink both sides by the same factor so the aspect ratio is kept, but
368+
// never below one pixel.
369+
if (options.target_width > max_texture_size.width ||
370+
options.target_height > max_texture_size.height) {
371+
const double scale = std::min(static_cast<double>(max_texture_size.width) /
372+
std::max(options.target_width, 1u),
373+
static_cast<double>(max_texture_size.height) /
374+
std::max(options.target_height, 1u));
375+
target_size = SkISize::Make(
376+
std::max(1,
377+
static_cast<int>(std::lround(options.target_width * scale))),
378+
std::max(1,
379+
static_cast<int>(std::lround(options.target_height * scale))));
380+
}
369381

370382
// Fast path for when the input requires no decompressing or conversion.
371383
if (!descriptor->is_compressed() && source_size == target_size &&

‎engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,13 +920,21 @@ TEST(ImageDecoderTest, VerifySimpleDecoding) {
920920
EXPECT_EQ(result_2->image_info.size.height, 2);
921921

922922
// If the destination size is larger than the max texture size the image
923-
// is scaled down.
923+
// is scaled down, keeping its aspect ratio.
924924
auto result_3 = ImageDecoderImpeller::DecompressTexture(
925925
descriptor.get(), {.target_width = 60, .target_height = 20}, {10, 10},
926926
/*supports_wide_gamut=*/false, capabilities, allocator);
927927
ASSERT_TRUE(result_3.ok());
928928
EXPECT_EQ(result_3->image_info.size.width, 10);
929-
EXPECT_EQ(result_3->image_info.size.height, 10);
929+
EXPECT_EQ(result_3->image_info.size.height, 3);
930+
931+
// Same for a tall destination size.
932+
auto result_tall = ImageDecoderImpeller::DecompressTexture(
933+
descriptor.get(), {.target_width = 20, .target_height = 60}, {10, 10},
934+
/*supports_wide_gamut=*/false, capabilities, allocator);
935+
ASSERT_TRUE(result_tall.ok());
936+
EXPECT_EQ(result_tall->image_info.size.width, 3);
937+
EXPECT_EQ(result_tall->image_info.size.height, 10);
930938

931939
// CPU resize is forced.
932940
auto result_4 = ImageDecoderImpeller::DecompressTexture(

0 commit comments

Comments
 (0)