Perform software conversion for unsupported pixel formats.
This commit is contained in:
@@ -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<size_t>(4096u, reserved);
|
||||
return Reserve(NextPowerOfTwoSize(reserved));
|
||||
}
|
||||
|
||||
bool Allocation::Reserve(size_t reserved) {
|
||||
// Reserve at least one page of data.
|
||||
reserved = std::max<size_t>(4096u, reserved);
|
||||
|
||||
if (reserved == reserved_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -28,8 +28,7 @@ std::optional<TextureDescriptor> 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;
|
||||
|
||||
@@ -4,15 +4,20 @@
|
||||
|
||||
#include "impeller/image/image.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#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<const fml::Mapping> 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<const fml::Mapping>& 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<Allocation>();
|
||||
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<uint8_t>::max();
|
||||
break;
|
||||
case Image::Format::GreyAlpha:
|
||||
dest[j + 0] = std::numeric_limits<uint8_t>::max();
|
||||
dest[j + 1] = std::numeric_limits<uint8_t>::max();
|
||||
dest[j + 2] = std::numeric_limits<uint8_t>::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<uint8_t>::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<fml::NonOwnedMapping>(
|
||||
rgba_allocation->GetBuffer(), //
|
||||
rgba_allocation->GetLength(), //
|
||||
[rgba_allocation](auto, auto) {}) //
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#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<const fml::Mapping> allocation);
|
||||
|
||||
~Image();
|
||||
@@ -34,17 +35,17 @@ class Image {
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
Format GetComponents() const;
|
||||
Format GetFormat() const;
|
||||
|
||||
const std::shared_ptr<const fml::Mapping>& GetAllocation() const;
|
||||
|
||||
Image ConvertToRGBA() const;
|
||||
|
||||
private:
|
||||
ISize size_;
|
||||
Format components_ = Format::Invalid;
|
||||
Format format_ = Format::Invalid;
|
||||
std::shared_ptr<const fml::Mapping> allocation_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Image);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user