Skip to content

Commit 8991e16

Browse files
walley892suryaKommana2662
authored andcommitted
Fix HDR image packing for ios (flutter#192526)
In Impeller's image decoder, we pack opaque wide-gamut images into 10-bit XR on iOS to save memory. This works for opaque SDR wide-gamut images, but HDR image content can exceed the 10-bit XR color range. This causes overflow and spilling issues, leading to artifacts like shown in flutter#191805. This PR removes the memory optimization for HDR images. Fixes flutter#191805 ## 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.
1 parent b9af61d commit 8991e16

2 files changed

Lines changed: 100 additions & 8 deletions

File tree

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ bool IsWideGamut(const SkColorSpace* color_space) {
7676
return area > kSrgbGamutArea;
7777
}
7878

79+
bool IsHdr(const SkColorSpace* color_space) {
80+
if (!color_space || color_space->isSRGB()) {
81+
return false;
82+
}
83+
skcms_TransferFunction fn;
84+
color_space->transferFn(&fn);
85+
skcms_TFType type = skcms_TransferFunction_getType(&fn);
86+
return type == skcms_TFType_PQ || type == skcms_TFType_PQish ||
87+
type == skcms_TFType_HLG || type == skcms_TFType_HLGish ||
88+
type == skcms_TFType_HLGinvish;
89+
}
90+
7991
absl::StatusOr<ImageDecoderImpeller::ImageInfo> ToImageInfo(
8092
const SkImageInfo& sk_image_format) {
8193
const std::optional<impeller::PixelFormat> pixel_format =
@@ -121,8 +133,10 @@ absl::StatusOr<SkImageInfo> CreateImageInfo(
121133
const SkISize& decode_size,
122134
bool supports_wide_gamut,
123135
ImageDecoder::TargetPixelFormat target_format) {
124-
const bool is_wide_gamut =
125-
supports_wide_gamut ? IsWideGamut(base_image_info.colorSpace()) : false;
136+
const SkColorSpace* color_space = base_image_info.colorSpace();
137+
const bool is_hdr = IsHdr(color_space);
138+
const bool is_extended_range =
139+
supports_wide_gamut && (IsWideGamut(color_space) || is_hdr);
126140
SkAlphaType alpha_type =
127141
ChooseCompatibleAlphaType(base_image_info.alphaType());
128142
if (target_format != ImageDecoder::TargetPixelFormat::kDontCare) {
@@ -135,12 +149,13 @@ absl::StatusOr<SkImageInfo> CreateImageInfo(
135149
.makeColorType(target_skia_color_type.value())
136150
.makeAlphaType(alpha_type)
137151
.makeColorSpace(SkColorSpace::MakeSRGB());
138-
} else if (is_wide_gamut) {
139-
// Use 10-bit for opaque images (less memory: 4 bytes vs 8 bytes per pixel).
140-
// Use F16 for images with alpha channel.
141-
SkColorType color_type = alpha_type == SkAlphaType::kOpaque_SkAlphaType
142-
? kBGR_101010x_XR_SkColorType
143-
: kRGBA_F16_SkColorType;
152+
} else if (is_extended_range) {
153+
// Use 10-bit for opaque SDR images (less memory: 4 bytes vs 8 bytes per
154+
// pixel). Use F16 for HDR images or images with alpha channel.
155+
SkColorType color_type =
156+
(!is_hdr && alpha_type == SkAlphaType::kOpaque_SkAlphaType)
157+
? kBGR_101010x_XR_SkColorType
158+
: kRGBA_F16_SkColorType;
144159
return base_image_info.makeWH(decode_size.width(), decode_size.height())
145160
.makeColorType(color_type)
146161
.makeAlphaType(alpha_type)

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,5 +447,82 @@ TEST(ImageDecoderNoGLTest, ImpellerUnmultipliedAlphaPng) {
447447
#endif // IMPELLER_SUPPORTS_RENDERING
448448
}
449449

450+
#if IMPELLER_SUPPORTS_RENDERING
451+
void ExpectDecodedFormat(sk_sp<SkColorSpace> color_space,
452+
bool supports_wide_gamut,
453+
impeller::PixelFormat expected_format) {
454+
uint8_t pixel_data[] = {255, 255, 255, 255};
455+
sk_sp<SkData> sk_data = SkData::MakeWithCopy(pixel_data, sizeof(pixel_data));
456+
auto immutable_buffer =
457+
fml::MakeRefCounted<ImmutableBuffer>(std::move(sk_data));
458+
459+
ImageDescriptor::ImageInfo image_info = {
460+
.width = 1,
461+
.height = 1,
462+
.format = ImageDescriptor::PixelFormat::kRGBA8888,
463+
.alpha_type = kOpaque_SkAlphaType,
464+
.color_space = std::move(color_space),
465+
};
466+
auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
467+
immutable_buffer->data(), image_info, sizeof(pixel_data));
468+
469+
std::shared_ptr<impeller::Capabilities> capabilities =
470+
impeller::CapabilitiesBuilder()
471+
.SetSupportsTextureToTextureBlits(true)
472+
.Build();
473+
std::shared_ptr<impeller::Allocator> allocator =
474+
std::make_shared<impeller::TestImpellerAllocator>();
475+
476+
absl::StatusOr<ImageDecoderImpeller::DecompressResult> result =
477+
ImageDecoderImpeller::DecompressTexture(
478+
descriptor.get(), {.target_width = 1, .target_height = 1}, {1, 1},
479+
supports_wide_gamut, capabilities, allocator);
480+
481+
ASSERT_TRUE(result.ok());
482+
EXPECT_EQ(result->image_info.format, expected_format);
483+
}
484+
#endif // IMPELLER_SUPPORTS_RENDERING
485+
486+
TEST(ImageDecoderNoGLTest, ImpellerHdrPqImageDecodesToF16) {
487+
#if IMPELLER_SUPPORTS_RENDERING
488+
ExpectDecodedFormat(
489+
SkColorSpace::MakeRGB(SkNamedTransferFn::kPQ, SkNamedGamut::kRec2020),
490+
/*supports_wide_gamut=*/true, impeller::PixelFormat::kR16G16B16A16Float);
491+
#endif // IMPELLER_SUPPORTS_RENDERING
492+
}
493+
494+
TEST(ImageDecoderNoGLTest, ImpellerHdrHlgImageDecodesToF16) {
495+
#if IMPELLER_SUPPORTS_RENDERING
496+
ExpectDecodedFormat(
497+
SkColorSpace::MakeRGB(SkNamedTransferFn::kHLG, SkNamedGamut::kRec2020),
498+
/*supports_wide_gamut=*/true, impeller::PixelFormat::kR16G16B16A16Float);
499+
#endif // IMPELLER_SUPPORTS_RENDERING
500+
}
501+
502+
TEST(ImageDecoderNoGLTest, ImpellerOpaqueSdrWideGamutDecodesToB10XR) {
503+
#if IMPELLER_SUPPORTS_RENDERING
504+
ExpectDecodedFormat(
505+
SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3),
506+
/*supports_wide_gamut=*/true, impeller::PixelFormat::kB10G10R10XR);
507+
#endif // IMPELLER_SUPPORTS_RENDERING
508+
}
509+
510+
TEST(ImageDecoderNoGLTest, ImpellerHdrImageWithoutWideGamutDecodesToF16) {
511+
#if IMPELLER_SUPPORTS_RENDERING
512+
ExpectDecodedFormat(
513+
SkColorSpace::MakeRGB(SkNamedTransferFn::kPQ, SkNamedGamut::kSRGB),
514+
/*supports_wide_gamut=*/true, impeller::PixelFormat::kR16G16B16A16Float);
515+
#endif // IMPELLER_SUPPORTS_RENDERING
516+
}
517+
518+
TEST(ImageDecoderNoGLTest,
519+
ImpellerHdrImageWithoutWideGamutSupportDecodesToRGBA8888) {
520+
#if IMPELLER_SUPPORTS_RENDERING
521+
ExpectDecodedFormat(
522+
SkColorSpace::MakeRGB(SkNamedTransferFn::kPQ, SkNamedGamut::kRec2020),
523+
/*supports_wide_gamut=*/false, impeller::PixelFormat::kR8G8B8A8UNormInt);
524+
#endif // IMPELLER_SUPPORTS_RENDERING
525+
}
526+
450527
} // namespace testing
451528
} // namespace flutter

0 commit comments

Comments
 (0)