Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ if (enable_unittests) {
"fixtures/hello_loop_2.gif",
"fixtures/hello_loop_2.webp",
"fixtures/FontManifest.json",
"fixtures/unmultiplied_alpha.png",
"fixtures/WideGamutIndexed.png",
"//flutter/third_party/txt/third_party/fonts/Roboto-Medium.ttf",
]
Expand Down
Binary file added lib/ui/fixtures/unmultiplied_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions lib/ui/painting/image_decoder_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ DecompressResult ImageDecoderImpeller::DecompressTexture(
bitmap->setImmutable();
}

// If the image is unpremultiplied, fix it.
if (alpha_type == SkAlphaType::kUnpremul_SkAlphaType) {
// Single copy of ImpellerAllocator crashes.
auto premul_allocator = std::make_shared<ImpellerAllocator>(allocator);
auto premul_bitmap = std::make_shared<SkBitmap>();
premul_bitmap->setInfo(bitmap->info().makeAlphaType(kPremul_SkAlphaType));
if (!premul_bitmap->tryAllocPixels(premul_allocator.get())) {
std::string decode_error(
"Could not allocate intermediate for premultiplication conversion.");
FML_DLOG(ERROR) << decode_error;
return DecompressResult{.decode_error = decode_error};
}
// readPixels() handles converting pixels to premultiplied form.
bitmap->readPixels(premul_bitmap->pixmap());
premul_bitmap->setImmutable();
bitmap_allocator = premul_allocator;
bitmap = premul_bitmap;
}
Comment on lines +220 to +225
Copy link
Contributor

Choose a reason for hiding this comment

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

So this code here is creating a new bitmap, writing the contents and applying the premultiplication to the old bitmap, then overwriting the local so we only refer to the new bitmap.

Seems reasonable!


if (bitmap->dimensions() == target_size) {
std::shared_ptr<impeller::DeviceBuffer> buffer =
bitmap_allocator->GetDeviceBuffer();
Expand Down
37 changes: 37 additions & 0 deletions lib/ui/painting/image_decoder_no_gl_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "flutter/lib/ui/painting/image_decoder_no_gl_unittests.h"

#include "flutter/fml/endianness.h"
#include "include/core/SkColorType.h"

namespace flutter {
namespace testing {
Expand Down Expand Up @@ -186,5 +187,41 @@ TEST(ImageDecoderNoGLTest, ImpellerWideGamutIndexedPng) {
#endif // IMPELLER_SUPPORTS_RENDERING
}

TEST(ImageDecoderNoGLTest, ImepllerUnmultipliedAlphaPng) {
#if defined(OS_FUCHSIA)
GTEST_SKIP() << "Fuchsia can't load the test fixtures.";
#endif
auto data = flutter::testing::OpenFixtureAsSkData("unmultiplied_alpha.png");
auto image = SkImages::DeferredFromEncodedData(data);
ASSERT_TRUE(image != nullptr);
ASSERT_EQ(SkISize::Make(11, 11), image->dimensions());

ImageGeneratorRegistry registry;
std::shared_ptr<ImageGenerator> generator =
registry.CreateCompatibleGenerator(data);
ASSERT_TRUE(generator);

auto descriptor = fml::MakeRefCounted<ImageDescriptor>(std::move(data),
std::move(generator));

#if IMPELLER_SUPPORTS_RENDERING
std::shared_ptr<impeller::Allocator> allocator =
std::make_shared<impeller::TestImpellerAllocator>();
std::optional<DecompressResult> result =
ImageDecoderImpeller::DecompressTexture(
descriptor.get(), SkISize::Make(11, 11), {11, 11},
/*supports_wide_gamut=*/true, allocator);
ASSERT_EQ(result->image_info.colorType(), kRGBA_8888_SkColorType);

const SkPixmap& pixmap = result->sk_bitmap->pixmap();
const uint32_t* pixel_ptr = static_cast<const uint32_t*>(pixmap.addr());
// Test the upper left pixel is premultiplied and not solid red.
ASSERT_EQ(*pixel_ptr, (uint32_t)0x1000001);
// Test a pixel in the green box is still green.
ASSERT_EQ(*(pixel_ptr + 11 * 4 + 4), (uint32_t)0xFF00FF00);

#endif // IMPELLER_SUPPORTS_RENDERING
}

} // namespace testing
} // namespace flutter