diff --git a/engine/src/flutter/impeller/base/allocation.cc b/engine/src/flutter/impeller/base/allocation.cc index d4f98ad4ff..e41d0f9e6c 100644 --- a/engine/src/flutter/impeller/base/allocation.cc +++ b/engine/src/flutter/impeller/base/allocation.cc @@ -28,8 +28,9 @@ size_t Allocation::GetReservedLength() const { return reserved_; } -bool Allocation::Truncate(size_t length) { - if (!ReserveNPOT(length)) { +bool Allocation::Truncate(size_t length, bool npot) { + const auto reserved = npot ? ReserveNPOT(length) : Reserve(length); + if (!reserved) { return false; } length_ = length; @@ -53,13 +54,12 @@ static uint32_t NextPowerOfTwoSize(uint32_t x) { } bool Allocation::ReserveNPOT(size_t reserved) { + // Reserve at least one page of data. + reserved = std::max(4096u, reserved); return Reserve(NextPowerOfTwoSize(reserved)); } bool Allocation::Reserve(size_t reserved) { - // Reserve at least one page of data. - reserved = std::max(4096u, reserved); - if (reserved == reserved_) { return true; } diff --git a/engine/src/flutter/impeller/base/allocation.h b/engine/src/flutter/impeller/base/allocation.h index a58974e8f4..b4bef69bae 100644 --- a/engine/src/flutter/impeller/base/allocation.h +++ b/engine/src/flutter/impeller/base/allocation.h @@ -21,7 +21,7 @@ class Allocation { size_t GetReservedLength() const; - [[nodiscard]] bool Truncate(size_t length); + [[nodiscard]] bool Truncate(size_t length, bool npot = true); private: uint8_t* buffer_ = nullptr; diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.mm b/engine/src/flutter/impeller/compositor/texture_descriptor.mm index 5bdcc81e50..1c092b2902 100644 --- a/engine/src/flutter/impeller/compositor/texture_descriptor.mm +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.mm @@ -28,8 +28,7 @@ std::optional TextureDescriptor::MakeFromImageResult( return std::nullopt; } - const auto pixel_format = - FormatForImageResultComponents(result.GetComponents()); + const auto pixel_format = FormatForImageResultComponents(result.GetFormat()); if (!pixel_format.has_value()) { FML_DLOG(ERROR) << "Unknown image format."; return std::nullopt; diff --git a/engine/src/flutter/impeller/image/image.cc b/engine/src/flutter/impeller/image/image.cc index e8b15cfa5c..443f1edbf0 100644 --- a/engine/src/flutter/impeller/image/image.cc +++ b/engine/src/flutter/impeller/image/image.cc @@ -4,15 +4,20 @@ #include "impeller/image/image.h" +#include + +#include "flutter/fml/mapping.h" +#include "impeller/base/allocation.h" + namespace impeller { Image::Image() = default; Image::Image(ISize size, - Format components, + Format format, std::shared_ptr allocation) - : size_(size), components_(components), allocation_(std::move(allocation)) { - if (!allocation_ || !size.IsPositive()) { + : size_(size), format_(format), allocation_(std::move(allocation)) { + if (!allocation_ || !size.IsPositive() || format_ == Format::Invalid) { return; } is_valid_ = true; @@ -28,12 +33,89 @@ const ISize& Image::GetSize() const { return size_; } -Image::Format Image::GetComponents() const { - return components_; +Image::Format Image::GetFormat() const { + return format_; } const std::shared_ptr& Image::GetAllocation() const { return allocation_; } +static size_t GetBytesPerPixel(Image::Format format) { + switch (format) { + case Image::Format::Invalid: + return 0u; + case Image::Format::Grey: + return 1u; + case Image::Format::GreyAlpha: + return 1u; + case Image::Format::RGB: + return 3u; + case Image::Format::RGBA: + return 4; + } + return 0u; +} + +Image Image::ConvertToRGBA() const { + if (!is_valid_) { + return {}; + } + + if (format_ == Format::RGBA) { + return Image{size_, format_, allocation_}; + } + + const auto bpp = GetBytesPerPixel(format_); + const auto source_byte_size = size_.Area() * bpp; + if (allocation_->GetSize() < source_byte_size) { + return {}; + } + + auto rgba_allocation = std::make_shared(); + if (!rgba_allocation->Truncate(size_.Area() * 4u, false)) { + return {}; + } + + const uint8_t* source = allocation_->GetMapping(); + uint8_t* dest = rgba_allocation->GetBuffer(); + + for (size_t i = 0, j = 0; i < source_byte_size; i += bpp, j += 4u) { + switch (format_) { + case Image::Format::Grey: + dest[j + 0] = source[i]; + dest[j + 1] = source[i]; + dest[j + 2] = source[i]; + dest[j + 3] = std::numeric_limits::max(); + break; + case Image::Format::GreyAlpha: + dest[j + 0] = std::numeric_limits::max(); + dest[j + 1] = std::numeric_limits::max(); + dest[j + 2] = std::numeric_limits::max(); + dest[j + 3] = source[i]; + break; + case Image::Format::RGB: + dest[j + 0] = source[i + 0]; + dest[j + 1] = source[i + 1]; + dest[j + 2] = source[i + 2]; + dest[j + 3] = std::numeric_limits::max(); + break; + case Image::Format::Invalid: + case Image::Format::RGBA: + // Should never happen. The necessary checks have already been + // performed. + FML_CHECK(false); + break; + } + } + + return Image{ + size_, Format::RGBA, + std::make_shared( + rgba_allocation->GetBuffer(), // + rgba_allocation->GetLength(), // + [rgba_allocation](auto, auto) {}) // + }; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/image/image.h b/engine/src/flutter/impeller/image/image.h index 3c3271f5eb..f888ab2445 100644 --- a/engine/src/flutter/impeller/image/image.h +++ b/engine/src/flutter/impeller/image/image.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" @@ -25,7 +26,7 @@ class Image { Image(); Image(ISize size, - Format components, + Format format, std::shared_ptr allocation); ~Image(); @@ -34,17 +35,17 @@ class Image { bool IsValid() const; - Format GetComponents() const; + Format GetFormat() const; const std::shared_ptr& GetAllocation() const; + Image ConvertToRGBA() const; + private: ISize size_; - Format components_ = Format::Invalid; + Format format_ = Format::Invalid; std::shared_ptr allocation_; bool is_valid_ = false; - - FML_DISALLOW_COPY_AND_ASSIGN(Image); }; } // namespace impeller diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index 922c6af7c6..2c81ea01dc 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -48,18 +48,18 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { ASSERT_TRUE(vertex_buffer); // Contents texture. - CompressedImage image( + CompressedImage compressed_image( flutter::testing::OpenFixtureAsMapping("bay_bridge.jpg")); - auto result = image.Decode(); - ASSERT_TRUE(result.IsValid()); - auto texture_descriptor = TextureDescriptor::MakeFromImageResult(result); + auto image = compressed_image.Decode().ConvertToRGBA(); + ASSERT_TRUE(image.IsValid()); + auto texture_descriptor = TextureDescriptor::MakeFromImageResult(image); ASSERT_TRUE(texture_descriptor.has_value()); auto texture = context->GetPermanentsAllocator()->CreateTexture( StorageMode::kHostVisible, texture_descriptor.value()); ASSERT_TRUE(texture); texture->SetLabel("Bay Bridge"); - auto uploaded = texture->SetContents(result.GetAllocation()->GetMapping(), - result.GetAllocation()->GetSize()); + auto uploaded = texture->SetContents(image.GetAllocation()->GetMapping(), + image.GetAllocation()->GetSize()); ASSERT_TRUE(uploaded); auto sampler = context->GetSamplerLibrary()->GetSampler({}); ASSERT_TRUE(sampler);