[Impeller] Clamp texture decompression to maximum supported texture size. (flutter/engine#35373)

This commit is contained in:
magicianA
2022-08-17 04:10:19 +08:00
committed by GitHub
parent 9d1055d3ff
commit 95c5efa85b
10 changed files with 77 additions and 5 deletions

View File

@@ -69,6 +69,8 @@ class Allocator {
std::shared_ptr<DeviceBuffer> CreateBufferWithCopy(
const fml::Mapping& mapping);
virtual ISize GetMaxTextureSizeSupported() const = 0;
protected:
Allocator();

View File

@@ -42,4 +42,9 @@ std::shared_ptr<Texture> AllocatorGLES::CreateTexture(
return std::make_shared<TextureGLES>(reactor_, std::move(desc));
}
// |Allocator|
ISize AllocatorGLES::GetMaxTextureSizeSupported() const {
return reactor_->GetProcTable().GetCapabilities()->max_texture_size;
}
} // namespace impeller

View File

@@ -35,6 +35,9 @@ class AllocatorGLES final : public Allocator {
StorageMode mode,
const TextureDescriptor& desc) override;
// |Allocator|
ISize GetMaxTextureSizeSupported() const override;
FML_DISALLOW_COPY_AND_ASSIGN(AllocatorGLES);
};

View File

@@ -26,6 +26,7 @@ class AllocatorMTL final : public Allocator {
bool supports_memoryless_targets_ = false;
bool supports_uma_ = false;
bool is_valid_ = false;
ISize max_texture_supported_;
AllocatorMTL(id<MTLDevice> device, std::string label);
@@ -41,6 +42,9 @@ class AllocatorMTL final : public Allocator {
StorageMode mode,
const TextureDescriptor& desc) override;
// |Allocator|
ISize GetMaxTextureSizeSupported() const override;
FML_DISALLOW_COPY_AND_ASSIGN(AllocatorMTL);
};

View File

@@ -50,6 +50,34 @@ static bool DeviceHasUnifiedMemoryArchitecture(id<MTLDevice> device) {
FML_UNREACHABLE();
}
static ISize DeviceMaxTextureSizeSupported(id<MTLDevice> device) {
// Since Apple didn't expose API for us to get the max texture size, we have
// to use hardcoded data from
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
// According to the feature set table, there are two supported max sizes :
// 16384 and 8192 for devices flutter support. The former is used on macs and
// latest ios devices. The latter is used on old ios devices.
if (@available(macOS 10.15, iOS 13, tvOS 13, *)) {
if ([device supportsFamily:MTLGPUFamilyApple3] ||
[device supportsFamily:MTLGPUFamilyMacCatalyst1] ||
[device supportsFamily:MTLGPUFamilyMac1]) {
return {16384, 16384};
}
return {8192, 8192};
} else {
#if FML_OS_IOS
if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v1] ||
[device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v1]) {
return {16384, 16384};
}
#endif
#if FML_OS_MACOSX
return {16384, 16384};
#endif
return {8192, 8192};
}
}
AllocatorMTL::AllocatorMTL(id<MTLDevice> device, std::string label)
: device_(device), allocator_label_(std::move(label)) {
if (!device_) {
@@ -58,6 +86,7 @@ AllocatorMTL::AllocatorMTL(id<MTLDevice> device, std::string label)
supports_memoryless_targets_ = DeviceSupportsMemorylessTargets(device_);
supports_uma_ = DeviceHasUnifiedMemoryArchitecture(device_);
max_texture_supported_ = DeviceMaxTextureSizeSupported(device_);
is_valid_ = true;
}
@@ -173,4 +202,8 @@ std::shared_ptr<Texture> AllocatorMTL::CreateTexture(
return std::make_shared<TextureMTL>(desc, texture);
}
ISize AllocatorMTL::GetMaxTextureSizeSupported() const {
return max_texture_supported_;
}
} // namespace impeller

View File

@@ -153,4 +153,11 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::CreateBuffer(StorageMode mode,
std::move(device_allocation));
}
// |Allocator|
ISize AllocatorVK::GetMaxTextureSizeSupported() const {
// TODO(magicianA): Get correct max texture size for Vulkan.
// 4096 is the required limit, see below:
// https://registry.khronos.org/vulkan/specs/1.2-extensions/html/vkspec.html#limits-minmax
return {4096, 4096};
}
} // namespace impeller

View File

@@ -45,6 +45,9 @@ class AllocatorVK final : public Allocator {
StorageMode mode,
const TextureDescriptor& desc) override;
// |Allocator|
ISize GetMaxTextureSizeSupported() const override;
FML_DISALLOW_COPY_AND_ASSIGN(AllocatorVK);
};

View File

@@ -58,7 +58,8 @@ static std::optional<impeller::PixelFormat> ToPixelFormat(SkColorType type) {
std::shared_ptr<SkBitmap> ImageDecoderImpeller::DecompressTexture(
ImageDescriptor* descriptor,
SkISize target_size) {
SkISize target_size,
impeller::ISize max_texture_size) {
TRACE_EVENT0("impeller", __FUNCTION__);
if (!descriptor) {
FML_DLOG(ERROR) << "Invalid descriptor.";
@@ -70,6 +71,10 @@ std::shared_ptr<SkBitmap> ImageDecoderImpeller::DecompressTexture(
<< "Uncompressed images are not implemented in Impeller yet.";
return nullptr;
}
target_size.set(std::min(static_cast<int32_t>(max_texture_size.width),
target_size.width()),
std::min(static_cast<int32_t>(max_texture_size.height),
target_size.height()));
const SkISize source_size = descriptor->image_info().dimensions();
auto decode_size = descriptor->get_scaled_dimensions(std::max(
@@ -200,8 +205,12 @@ void ImageDecoderImpeller::Decode(fml::RefPtr<ImageDescriptor> descriptor,
io_runner = runners_.GetIOTaskRunner(), //
result //
]() {
auto max_size_supported =
context->GetResourceAllocator()->GetMaxTextureSizeSupported();
// Always decompress on the concurrent runner.
auto bitmap = DecompressTexture(raw_descriptor, target_size);
auto bitmap =
DecompressTexture(raw_descriptor, target_size, max_size_supported);
if (!bitmap) {
result(nullptr);
return;

View File

@@ -9,6 +9,7 @@
#include "flutter/fml/macros.h"
#include "flutter/lib/ui/painting/image_decoder.h"
#include "impeller/geometry/size.h"
namespace impeller {
class Context;
@@ -33,7 +34,8 @@ class ImageDecoderImpeller final : public ImageDecoder {
static std::shared_ptr<SkBitmap> DecompressTexture(
ImageDescriptor* descriptor,
SkISize target_size);
SkISize target_size,
impeller::ISize max_texture_size);
private:
using FutureContext = std::shared_future<std::shared_ptr<impeller::Context>>;

View File

@@ -503,10 +503,14 @@ TEST(ImageDecoderTest, VerifySimpleDecoding) {
SkISize::Make(6, 2));
#if IMPELLER_SUPPORTS_RENDERING
ASSERT_EQ(ImageDecoderImpeller::DecompressTexture(descriptor.get(),
SkISize::Make(6, 2))
ASSERT_EQ(ImageDecoderImpeller::DecompressTexture(
descriptor.get(), SkISize::Make(6, 2), {100, 100})
->dimensions(),
SkISize::Make(6, 2));
ASSERT_EQ(ImageDecoderImpeller::DecompressTexture(
descriptor.get(), SkISize::Make(60, 20), {10, 10})
->dimensions(),
SkISize::Make(10, 10));
#endif // IMPELLER_SUPPORTS_RENDERING
}