forked from firka/flutter
[Impeller] use blit pass to resize decoded images. (flutter/engine#54606)
Fixes https://github.com/flutter/flutter/issues/153623 Fixes https://github.com/flutter/flutter/issues/153788 * Avoids slow CPU resizer * Avoids us having to write any code at all to resize these images * Avoids reading from the GPU staging buffers that are not read cached (similar to Glyph cache performance issues). Also changes the single frame codec upload to either use the device private storage path or to defer upload until the GPU returns. This allows us to ensure that mips are always constructed and makes sure that we don't need to keep around the CPU texture reisze that depends on the Skia software backend. Separately, I updated the deferred task system to allow separate success/failure tasks to make it easier to track the final state.
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/geometry/point.h"
|
||||
#include "impeller/renderer/backend/gles/device_buffer_gles.h"
|
||||
#include "impeller/renderer/backend/gles/reactor_gles.h"
|
||||
#include "impeller/renderer/backend/gles/texture_gles.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -73,7 +74,7 @@ bool BlitCopyTextureToTextureCommandGLES::Encode(
|
||||
// emulate the blit when it's not available in the driver.
|
||||
if (!gl.BlitFramebuffer.IsAvailable()) {
|
||||
// TODO(135818): Emulate the blit using a raster draw call here.
|
||||
FML_LOG(ERROR) << "Texture blit fallback not implemented yet for GLES2.";
|
||||
VALIDATION_LOG << "Texture blit fallback not implemented yet for GLES2.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -353,4 +354,69 @@ bool BlitGenerateMipmapCommandGLES::Encode(const ReactorGLES& reactor) const {
|
||||
return true;
|
||||
};
|
||||
|
||||
////// BlitResizeTextureCommandGLES
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
BlitResizeTextureCommandGLES::~BlitResizeTextureCommandGLES() = default;
|
||||
|
||||
std::string BlitResizeTextureCommandGLES::GetLabel() const {
|
||||
return "Resize Texture";
|
||||
}
|
||||
|
||||
bool BlitResizeTextureCommandGLES::Encode(const ReactorGLES& reactor) const {
|
||||
const auto& gl = reactor.GetProcTable();
|
||||
|
||||
// glBlitFramebuffer is a GLES3 proc. Since we target GLES2, we need to
|
||||
// emulate the blit when it's not available in the driver.
|
||||
if (!gl.BlitFramebuffer.IsAvailable()) {
|
||||
// TODO(135818): Emulate the blit using a raster draw call here.
|
||||
VALIDATION_LOG << "Texture blit fallback not implemented yet for GLES2.";
|
||||
return false;
|
||||
}
|
||||
|
||||
GLuint read_fbo = GL_NONE;
|
||||
GLuint draw_fbo = GL_NONE;
|
||||
fml::ScopedCleanupClosure delete_fbos([&gl, &read_fbo, &draw_fbo]() {
|
||||
DeleteFBO(gl, read_fbo, GL_READ_FRAMEBUFFER);
|
||||
DeleteFBO(gl, draw_fbo, GL_DRAW_FRAMEBUFFER);
|
||||
});
|
||||
|
||||
{
|
||||
auto read = ConfigureFBO(gl, source, GL_READ_FRAMEBUFFER);
|
||||
if (!read.has_value()) {
|
||||
return false;
|
||||
}
|
||||
read_fbo = read.value();
|
||||
}
|
||||
|
||||
{
|
||||
auto draw = ConfigureFBO(gl, destination, GL_DRAW_FRAMEBUFFER);
|
||||
if (!draw.has_value()) {
|
||||
return false;
|
||||
}
|
||||
draw_fbo = draw.value();
|
||||
}
|
||||
|
||||
gl.Disable(GL_SCISSOR_TEST);
|
||||
gl.Disable(GL_DEPTH_TEST);
|
||||
gl.Disable(GL_STENCIL_TEST);
|
||||
|
||||
const IRect source_region = IRect::MakeSize(source->GetSize());
|
||||
const IRect destination_region = IRect::MakeSize(destination->GetSize());
|
||||
|
||||
gl.BlitFramebuffer(source_region.GetX(), // srcX0
|
||||
source_region.GetY(), // srcY0
|
||||
source_region.GetWidth(), // srcX1
|
||||
source_region.GetHeight(), // srcY1
|
||||
destination_region.GetX(), // dstX0
|
||||
destination_region.GetY(), // dstY0
|
||||
destination_region.GetWidth(), // dstX1
|
||||
destination_region.GetHeight(), // dstY1
|
||||
GL_COLOR_BUFFER_BIT, // mask
|
||||
GL_LINEAR // filter
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -59,6 +59,15 @@ struct BlitGenerateMipmapCommandGLES : public BlitEncodeGLES,
|
||||
[[nodiscard]] bool Encode(const ReactorGLES& reactor) const override;
|
||||
};
|
||||
|
||||
struct BlitResizeTextureCommandGLES : public BlitEncodeGLES,
|
||||
public BlitResizeTextureCommand {
|
||||
~BlitResizeTextureCommandGLES() override;
|
||||
|
||||
std::string GetLabel() const override;
|
||||
|
||||
[[nodiscard]] bool Encode(const ReactorGLES& reactor) const override;
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_BLIT_COMMAND_GLES_H_
|
||||
|
||||
@@ -157,4 +157,15 @@ bool BlitPassGLES::OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
|
||||
return true;
|
||||
}
|
||||
|
||||
// |BlitPass|
|
||||
bool BlitPassGLES::ResizeTexture(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination) {
|
||||
auto command = std::make_unique<BlitResizeTextureCommandGLES>();
|
||||
command->source = source;
|
||||
command->destination = destination;
|
||||
|
||||
commands_.push_back(std::move(command));
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -40,6 +40,10 @@ class BlitPassGLES final : public BlitPass,
|
||||
bool EncodeCommands(
|
||||
const std::shared_ptr<Allocator>& transients_allocator) const override;
|
||||
|
||||
// |BlitPass|
|
||||
bool ResizeTexture(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination) override;
|
||||
|
||||
// |BlitPass|
|
||||
bool OnCopyTextureToTextureCommand(std::shared_ptr<Texture> source,
|
||||
std::shared_ptr<Texture> destination,
|
||||
|
||||
@@ -59,7 +59,10 @@ impeller_component("metal") {
|
||||
"//flutter/fml",
|
||||
]
|
||||
|
||||
frameworks = [ "Metal.framework" ]
|
||||
frameworks = [
|
||||
"Metal.framework",
|
||||
"MetalPerformanceShaders.framework",
|
||||
]
|
||||
}
|
||||
|
||||
impeller_component("metal_unittests") {
|
||||
|
||||
@@ -21,6 +21,7 @@ class BlitPassMTL final : public BlitPass {
|
||||
|
||||
id<MTLBlitCommandEncoder> encoder_ = nil;
|
||||
id<MTLCommandBuffer> buffer_ = nil;
|
||||
id<MTLDevice> device_ = nil;
|
||||
bool is_valid_ = false;
|
||||
bool is_metal_trace_active_ = false;
|
||||
// Many parts of the codebase will start writing to a render pass but
|
||||
@@ -28,7 +29,7 @@ class BlitPassMTL final : public BlitPass {
|
||||
// so that in the dtor we can always ensure the render pass is finished.
|
||||
mutable bool did_finish_encoding_ = false;
|
||||
|
||||
explicit BlitPassMTL(id<MTLCommandBuffer> buffer);
|
||||
explicit BlitPassMTL(id<MTLCommandBuffer> buffer, id<MTLDevice> device);
|
||||
|
||||
// |BlitPass|
|
||||
bool IsValid() const override;
|
||||
@@ -40,6 +41,10 @@ class BlitPassMTL final : public BlitPass {
|
||||
bool EncodeCommands(
|
||||
const std::shared_ptr<Allocator>& transients_allocator) const override;
|
||||
|
||||
// |BlitPass|
|
||||
bool ResizeTexture(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination) override;
|
||||
|
||||
// |BlitPass|
|
||||
bool OnCopyTextureToTextureCommand(std::shared_ptr<Texture> source,
|
||||
std::shared_ptr<Texture> destination,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "impeller/renderer/backend/metal/blit_pass_mtl.h"
|
||||
#include <Metal/Metal.h>
|
||||
#import <MetalPerformanceShaders/MetalPerformanceShaders.h>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
@@ -25,7 +26,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
BlitPassMTL::BlitPassMTL(id<MTLCommandBuffer> buffer) : buffer_(buffer) {
|
||||
BlitPassMTL::BlitPassMTL(id<MTLCommandBuffer> buffer, id<MTLDevice> device)
|
||||
: buffer_(buffer), device_(device) {
|
||||
if (!buffer_) {
|
||||
return;
|
||||
}
|
||||
@@ -105,7 +107,28 @@ bool BlitPassMTL::OnCopyTextureToTextureCommand(
|
||||
[encoder_ popDebugGroup];
|
||||
}
|
||||
#endif // IMPELLER_DEBUG
|
||||
return true;
|
||||
}
|
||||
|
||||
// |BlitPass|
|
||||
bool BlitPassMTL::ResizeTexture(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination) {
|
||||
auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture();
|
||||
if (!source_mtl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture();
|
||||
if (!destination_mtl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
[encoder_ endEncoding];
|
||||
auto filter = [[MPSImageBilinearScale alloc] initWithDevice:device_];
|
||||
[filter encodeToCommandBuffer:buffer_
|
||||
sourceTexture:source_mtl
|
||||
destinationTexture:destination_mtl];
|
||||
encoder_ = [buffer_ blitCommandEncoder];
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,11 @@ class CommandBufferMTL final : public CommandBuffer {
|
||||
private:
|
||||
friend class ContextMTL;
|
||||
|
||||
id<MTLCommandBuffer> buffer_ = nullptr;
|
||||
id<MTLCommandBuffer> buffer_ = nil;
|
||||
id<MTLDevice> device_ = nil;
|
||||
|
||||
CommandBufferMTL(const std::weak_ptr<const Context>& context,
|
||||
id<MTLDevice> device,
|
||||
id<MTLCommandQueue> queue);
|
||||
|
||||
// |CommandBuffer|
|
||||
|
||||
@@ -128,8 +128,11 @@ static id<MTLCommandBuffer> CreateCommandBuffer(id<MTLCommandQueue> queue) {
|
||||
}
|
||||
|
||||
CommandBufferMTL::CommandBufferMTL(const std::weak_ptr<const Context>& context,
|
||||
id<MTLDevice> device,
|
||||
id<MTLCommandQueue> queue)
|
||||
: CommandBuffer(context), buffer_(CreateCommandBuffer(queue)) {}
|
||||
: CommandBuffer(context),
|
||||
buffer_(CreateCommandBuffer(queue)),
|
||||
device_(device) {}
|
||||
|
||||
CommandBufferMTL::~CommandBufferMTL() = default;
|
||||
|
||||
@@ -208,7 +211,7 @@ std::shared_ptr<BlitPass> CommandBufferMTL::OnCreateBlitPass() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto pass = std::shared_ptr<BlitPassMTL>(new BlitPassMTL(buffer_));
|
||||
auto pass = std::shared_ptr<BlitPassMTL>(new BlitPassMTL(buffer_, device_));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "flutter/fml/concurrent_message_loop.h"
|
||||
#include "flutter/fml/synchronization/sync_switch.h"
|
||||
#include "fml/closure.h"
|
||||
#include "impeller/base/backend_cast.h"
|
||||
#include "impeller/core/sampler.h"
|
||||
#include "impeller/renderer/backend/metal/allocator_mtl.h"
|
||||
@@ -136,7 +137,8 @@ class ContextMTL final : public Context,
|
||||
#endif // IMPELLER_DEBUG
|
||||
|
||||
// |Context|
|
||||
void StoreTaskForGPU(const std::function<void()>& task) override;
|
||||
void StoreTaskForGPU(const fml::closure& task,
|
||||
const fml::closure& failure) override;
|
||||
|
||||
private:
|
||||
class SyncSwitchObserver : public fml::SyncSwitch::Observer {
|
||||
@@ -149,6 +151,11 @@ class ContextMTL final : public Context,
|
||||
ContextMTL& parent_;
|
||||
};
|
||||
|
||||
struct PendingTasks {
|
||||
fml::closure task;
|
||||
fml::closure failure;
|
||||
};
|
||||
|
||||
id<MTLDevice> device_ = nullptr;
|
||||
id<MTLCommandQueue> command_queue_ = nullptr;
|
||||
std::shared_ptr<ShaderLibraryMTL> shader_library_;
|
||||
@@ -157,7 +164,7 @@ class ContextMTL final : public Context,
|
||||
std::shared_ptr<AllocatorMTL> resource_allocator_;
|
||||
std::shared_ptr<const Capabilities> device_capabilities_;
|
||||
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
|
||||
std::deque<std::function<void()>> tasks_awaiting_gpu_;
|
||||
std::deque<PendingTasks> tasks_awaiting_gpu_;
|
||||
std::unique_ptr<SyncSwitchObserver> sync_switch_observer_;
|
||||
std::shared_ptr<CommandQueue> command_queue_ip_;
|
||||
#ifdef IMPELLER_DEBUG
|
||||
|
||||
@@ -338,7 +338,7 @@ std::shared_ptr<CommandBuffer> ContextMTL::CreateCommandBufferInQueue(
|
||||
}
|
||||
|
||||
auto buffer = std::shared_ptr<CommandBufferMTL>(
|
||||
new CommandBufferMTL(weak_from_this(), queue));
|
||||
new CommandBufferMTL(weak_from_this(), device_, queue));
|
||||
if (!buffer->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -377,17 +377,21 @@ id<MTLCommandBuffer> ContextMTL::CreateMTLCommandBuffer(
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void ContextMTL::StoreTaskForGPU(const std::function<void()>& task) {
|
||||
tasks_awaiting_gpu_.emplace_back(task);
|
||||
void ContextMTL::StoreTaskForGPU(const fml::closure& task,
|
||||
const fml::closure& failure) {
|
||||
tasks_awaiting_gpu_.push_back(PendingTasks{task, failure});
|
||||
while (tasks_awaiting_gpu_.size() > kMaxTasksAwaitingGPU) {
|
||||
tasks_awaiting_gpu_.front()();
|
||||
PendingTasks front = std::move(tasks_awaiting_gpu_.front());
|
||||
if (front.failure) {
|
||||
front.failure();
|
||||
}
|
||||
tasks_awaiting_gpu_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void ContextMTL::FlushTasksAwaitingGPU() {
|
||||
for (const auto& task : tasks_awaiting_gpu_) {
|
||||
task();
|
||||
task.task();
|
||||
}
|
||||
tasks_awaiting_gpu_.clear();
|
||||
}
|
||||
|
||||
@@ -319,6 +319,90 @@ bool BlitPassVK::OnCopyBufferToTextureCommand(
|
||||
return true;
|
||||
}
|
||||
|
||||
// |BlitPass|
|
||||
bool BlitPassVK::ResizeTexture(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination) {
|
||||
auto& encoder = *command_buffer_->GetEncoder();
|
||||
const auto& cmd_buffer = encoder.GetCommandBuffer();
|
||||
|
||||
const auto& src = TextureVK::Cast(*source);
|
||||
const auto& dst = TextureVK::Cast(*destination);
|
||||
|
||||
if (!encoder.Track(source) || !encoder.Track(destination)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BarrierVK src_barrier;
|
||||
src_barrier.cmd_buffer = cmd_buffer;
|
||||
src_barrier.new_layout = vk::ImageLayout::eTransferSrcOptimal;
|
||||
src_barrier.src_access = vk::AccessFlagBits::eTransferWrite |
|
||||
vk::AccessFlagBits::eShaderWrite |
|
||||
vk::AccessFlagBits::eColorAttachmentWrite;
|
||||
src_barrier.src_stage = vk::PipelineStageFlagBits::eTransfer |
|
||||
vk::PipelineStageFlagBits::eFragmentShader |
|
||||
vk::PipelineStageFlagBits::eColorAttachmentOutput;
|
||||
src_barrier.dst_access = vk::AccessFlagBits::eTransferRead;
|
||||
src_barrier.dst_stage = vk::PipelineStageFlagBits::eTransfer;
|
||||
|
||||
BarrierVK dst_barrier;
|
||||
dst_barrier.cmd_buffer = cmd_buffer;
|
||||
dst_barrier.new_layout = vk::ImageLayout::eTransferDstOptimal;
|
||||
dst_barrier.src_access = {};
|
||||
dst_barrier.src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
|
||||
dst_barrier.dst_access =
|
||||
vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eTransferWrite;
|
||||
dst_barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader |
|
||||
vk::PipelineStageFlagBits::eTransfer;
|
||||
|
||||
if (!src.SetLayout(src_barrier) || !dst.SetLayout(dst_barrier)) {
|
||||
VALIDATION_LOG << "Could not complete layout transitions.";
|
||||
return false;
|
||||
}
|
||||
|
||||
vk::ImageBlit blit;
|
||||
blit.srcSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
|
||||
blit.srcSubresource.baseArrayLayer = 0u;
|
||||
blit.srcSubresource.layerCount = 1u;
|
||||
blit.srcSubresource.mipLevel = 0;
|
||||
|
||||
blit.dstSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
|
||||
blit.dstSubresource.baseArrayLayer = 0u;
|
||||
blit.dstSubresource.layerCount = 1u;
|
||||
blit.dstSubresource.mipLevel = 0;
|
||||
|
||||
// offsets[0] is origin.
|
||||
blit.srcOffsets[1].x = std::max<int32_t>(source->GetSize().width, 1u);
|
||||
blit.srcOffsets[1].y = std::max<int32_t>(source->GetSize().height, 1u);
|
||||
blit.srcOffsets[1].z = 1u;
|
||||
|
||||
// offsets[0] is origin.
|
||||
blit.dstOffsets[1].x = std::max<int32_t>(destination->GetSize().width, 1u);
|
||||
blit.dstOffsets[1].y = std::max<int32_t>(destination->GetSize().height, 1u);
|
||||
blit.dstOffsets[1].z = 1u;
|
||||
|
||||
cmd_buffer.blitImage(src.GetImage(), //
|
||||
src_barrier.new_layout, //
|
||||
dst.GetImage(), //
|
||||
dst_barrier.new_layout, //
|
||||
1, //
|
||||
&blit, //
|
||||
vk::Filter::eLinear
|
||||
|
||||
);
|
||||
|
||||
// Convert back to shader read
|
||||
|
||||
BarrierVK barrier;
|
||||
barrier.cmd_buffer = cmd_buffer;
|
||||
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
|
||||
barrier.src_access = {};
|
||||
barrier.src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
|
||||
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
|
||||
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
|
||||
|
||||
return dst.SetLayout(barrier);
|
||||
}
|
||||
|
||||
// |BlitPass|
|
||||
bool BlitPassVK::OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
|
||||
std::string label) {
|
||||
|
||||
@@ -37,6 +37,10 @@ class BlitPassVK final : public BlitPass {
|
||||
bool EncodeCommands(
|
||||
const std::shared_ptr<Allocator>& transients_allocator) const override;
|
||||
|
||||
// |BlitPass|
|
||||
bool ResizeTexture(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination) override;
|
||||
|
||||
// |BlitPass|
|
||||
bool ConvertTextureToShaderRead(
|
||||
const std::shared_ptr<Texture>& texture) override;
|
||||
|
||||
@@ -22,6 +22,11 @@ struct BlitCopyTextureToTextureCommand : public BlitCommand {
|
||||
IPoint destination_origin;
|
||||
};
|
||||
|
||||
struct BlitResizeTextureCommand : public BlitCommand {
|
||||
std::shared_ptr<Texture> source;
|
||||
std::shared_ptr<Texture> destination;
|
||||
};
|
||||
|
||||
struct BlitCopyTextureToBufferCommand : public BlitCommand {
|
||||
std::shared_ptr<Texture> source;
|
||||
std::shared_ptr<DeviceBuffer> destination;
|
||||
|
||||
@@ -39,6 +39,14 @@ class BlitPass {
|
||||
virtual bool ConvertTextureToShaderRead(
|
||||
const std::shared_ptr<Texture>& texture);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Resize the [source] texture into the [destination] texture.
|
||||
///
|
||||
/// On Metal platforms, [destination] is required to be non-lossy
|
||||
/// and have the Shader read capability.
|
||||
virtual bool ResizeTexture(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination) = 0;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Record a command to copy the contents of one texture to
|
||||
/// another texture. The blit area is limited by the intersection
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <cstdint>
|
||||
#include "fml/mapping.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/core/device_buffer.h"
|
||||
#include "impeller/core/device_buffer_descriptor.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/texture_descriptor.h"
|
||||
@@ -130,5 +133,44 @@ TEST_P(BlitPassTest, CanBlitSmallRegionToUninitializedTexture) {
|
||||
EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
|
||||
}
|
||||
|
||||
TEST_P(BlitPassTest, CanResizeTextures) {
|
||||
auto context = GetContext();
|
||||
auto cmd_buffer = context->CreateCommandBuffer();
|
||||
auto blit_pass = cmd_buffer->CreateBlitPass();
|
||||
|
||||
TextureDescriptor dst_format;
|
||||
dst_format.storage_mode = StorageMode::kDevicePrivate;
|
||||
dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
dst_format.size = {10, 10};
|
||||
dst_format.usage = TextureUsage::kShaderRead | TextureUsage::kShaderWrite;
|
||||
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
|
||||
|
||||
TextureDescriptor src_format;
|
||||
src_format.storage_mode = StorageMode::kDevicePrivate;
|
||||
src_format.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
src_format.size = {100, 100};
|
||||
auto src = context->GetResourceAllocator()->CreateTexture(src_format);
|
||||
|
||||
std::vector<uint8_t> bytes(src_format.GetByteSizeOfBaseMipLevel());
|
||||
for (auto i = 0u; i < src_format.GetByteSizeOfBaseMipLevel(); i += 4) {
|
||||
// RGBA
|
||||
bytes[i + 0] = 255;
|
||||
bytes[i + 1] = 0;
|
||||
bytes[i + 2] = 0;
|
||||
bytes[i + 3] = 255;
|
||||
}
|
||||
auto mapping = fml::DataMapping(bytes);
|
||||
auto staging = context->GetResourceAllocator()->CreateBufferWithCopy(mapping);
|
||||
|
||||
ASSERT_TRUE(dst);
|
||||
ASSERT_TRUE(src);
|
||||
ASSERT_TRUE(staging);
|
||||
|
||||
EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(staging), src));
|
||||
EXPECT_TRUE(blit_pass->ResizeTexture(src, dst));
|
||||
EXPECT_TRUE(blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()));
|
||||
EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "fml/closure.h"
|
||||
#include "impeller/core/allocator.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/renderer/capabilities.h"
|
||||
@@ -176,10 +177,14 @@ class Context {
|
||||
/// being available or that the task has been canceled. The task should
|
||||
/// operate with the `SyncSwitch` to make sure the GPU is accessible.
|
||||
///
|
||||
/// If the queue of pending tasks is cleared without GPU access, then the
|
||||
/// failure callback will be invoked and the primary task function will not
|
||||
///
|
||||
/// Threadsafe.
|
||||
///
|
||||
/// `task` will be executed on the platform thread.
|
||||
virtual void StoreTaskForGPU(const std::function<void()>& task) {
|
||||
virtual void StoreTaskForGPU(const fml::closure& task,
|
||||
const fml::closure& failure) {
|
||||
FML_CHECK(false && "not supported in this context");
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,12 @@ class MockBlitPass : public BlitPass {
|
||||
(const, override));
|
||||
MOCK_METHOD(void, OnSetLabel, (std::string label), (override));
|
||||
|
||||
MOCK_METHOD(bool,
|
||||
ResizeTexture,
|
||||
(const std::shared_ptr<Texture>& source,
|
||||
const std::shared_ptr<Texture>& destination),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(bool,
|
||||
OnCopyTextureToTextureCommand,
|
||||
(std::shared_ptr<Texture> source,
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include "flutter/impeller/renderer/context.h"
|
||||
#include "impeller/base/strings.h"
|
||||
#include "impeller/core/device_buffer.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/texture_descriptor.h"
|
||||
#include "impeller/display_list/skia_conversions.h"
|
||||
#include "impeller/geometry/size.h"
|
||||
#include "third_party/skia/include/core/SkAlphaType.h"
|
||||
@@ -26,7 +28,6 @@
|
||||
#include "third_party/skia/include/core/SkPixelRef.h"
|
||||
#include "third_party/skia/include/core/SkPixmap.h"
|
||||
#include "third_party/skia/include/core/SkPoint.h"
|
||||
#include "third_party/skia/include/core/SkSamplingOptions.h"
|
||||
#include "third_party/skia/include/core/SkSize.h"
|
||||
|
||||
namespace flutter {
|
||||
@@ -224,59 +225,68 @@ DecompressResult ImageDecoderImpeller::DecompressTexture(
|
||||
bitmap = premul_bitmap;
|
||||
}
|
||||
|
||||
if (bitmap->dimensions() == target_size) {
|
||||
std::shared_ptr<impeller::DeviceBuffer> buffer =
|
||||
bitmap_allocator->GetDeviceBuffer();
|
||||
if (!buffer) {
|
||||
return DecompressResult{.decode_error = "Unable to get device buffer"};
|
||||
}
|
||||
buffer->Flush();
|
||||
|
||||
std::optional<SkImageInfo> resize_info =
|
||||
bitmap->dimensions() == target_size
|
||||
? std::nullopt
|
||||
: std::optional<SkImageInfo>(image_info.makeDimensions(target_size));
|
||||
|
||||
if (source_size.width() > max_texture_size.width ||
|
||||
source_size.height() > max_texture_size.height) {
|
||||
//----------------------------------------------------------------------------
|
||||
/// 2. If the decoded image isn't the requested target size and the src size
|
||||
/// exceeds the device max texture size, perform a slow CPU reisze.
|
||||
///
|
||||
TRACE_EVENT0("impeller", "SlowCPUDecodeScale");
|
||||
const auto scaled_image_info = image_info.makeDimensions(target_size);
|
||||
|
||||
auto scaled_bitmap = std::make_shared<SkBitmap>();
|
||||
auto scaled_allocator = std::make_shared<ImpellerAllocator>(allocator);
|
||||
scaled_bitmap->setInfo(scaled_image_info);
|
||||
if (!scaled_bitmap->tryAllocPixels(scaled_allocator.get())) {
|
||||
std::string decode_error(
|
||||
"Could not allocate scaled bitmap for image decompression.");
|
||||
FML_DLOG(ERROR) << decode_error;
|
||||
return DecompressResult{.decode_error = decode_error};
|
||||
}
|
||||
if (!bitmap->pixmap().scalePixels(
|
||||
scaled_bitmap->pixmap(),
|
||||
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone))) {
|
||||
FML_LOG(ERROR) << "Could not scale decoded bitmap data.";
|
||||
}
|
||||
scaled_bitmap->setImmutable();
|
||||
|
||||
std::shared_ptr<impeller::DeviceBuffer> buffer =
|
||||
bitmap_allocator->GetDeviceBuffer();
|
||||
scaled_allocator->GetDeviceBuffer();
|
||||
if (!buffer) {
|
||||
return DecompressResult{.decode_error = "Unable to get device buffer"};
|
||||
}
|
||||
buffer->Flush();
|
||||
|
||||
return DecompressResult{.device_buffer = std::move(buffer),
|
||||
.sk_bitmap = bitmap,
|
||||
.image_info = bitmap->info()};
|
||||
.sk_bitmap = scaled_bitmap,
|
||||
.image_info = scaled_bitmap->info()};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// 2. If the decoded image isn't the requested target size, resize it.
|
||||
///
|
||||
|
||||
TRACE_EVENT0("impeller", "DecodeScale");
|
||||
const auto scaled_image_info = image_info.makeDimensions(target_size);
|
||||
|
||||
auto scaled_bitmap = std::make_shared<SkBitmap>();
|
||||
auto scaled_allocator = std::make_shared<ImpellerAllocator>(allocator);
|
||||
scaled_bitmap->setInfo(scaled_image_info);
|
||||
if (!scaled_bitmap->tryAllocPixels(scaled_allocator.get())) {
|
||||
std::string decode_error(
|
||||
"Could not allocate scaled bitmap for image decompression.");
|
||||
FML_DLOG(ERROR) << decode_error;
|
||||
return DecompressResult{.decode_error = decode_error};
|
||||
}
|
||||
if (!bitmap->pixmap().scalePixels(
|
||||
scaled_bitmap->pixmap(),
|
||||
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone))) {
|
||||
FML_LOG(ERROR) << "Could not scale decoded bitmap data.";
|
||||
}
|
||||
scaled_bitmap->setImmutable();
|
||||
|
||||
std::shared_ptr<impeller::DeviceBuffer> buffer =
|
||||
scaled_allocator->GetDeviceBuffer();
|
||||
buffer->Flush();
|
||||
|
||||
if (!buffer) {
|
||||
return DecompressResult{.decode_error = "Unable to get device buffer"};
|
||||
}
|
||||
return DecompressResult{.device_buffer = std::move(buffer),
|
||||
.sk_bitmap = scaled_bitmap,
|
||||
.image_info = scaled_bitmap->info()};
|
||||
.sk_bitmap = bitmap,
|
||||
.image_info = bitmap->info(),
|
||||
.resize_info = resize_info};
|
||||
}
|
||||
|
||||
/// Only call this method if the GPU is available.
|
||||
static std::pair<sk_sp<DlImage>, std::string> UnsafeUploadTextureToPrivate(
|
||||
// static
|
||||
std::pair<sk_sp<DlImage>, std::string>
|
||||
ImageDecoderImpeller::UnsafeUploadTextureToPrivate(
|
||||
const std::shared_ptr<impeller::Context>& context,
|
||||
const std::shared_ptr<impeller::DeviceBuffer>& buffer,
|
||||
const SkImageInfo& image_info) {
|
||||
const SkImageInfo& image_info,
|
||||
const std::optional<SkImageInfo>& resize_info) {
|
||||
const auto pixel_format =
|
||||
impeller::skia_conversions::ToPixelFormat(image_info.colorType());
|
||||
if (!pixel_format) {
|
||||
@@ -292,6 +302,12 @@ static std::pair<sk_sp<DlImage>, std::string> UnsafeUploadTextureToPrivate(
|
||||
texture_descriptor.size = {image_info.width(), image_info.height()};
|
||||
texture_descriptor.mip_count = texture_descriptor.size.MipCount();
|
||||
texture_descriptor.compression_type = impeller::CompressionType::kLossy;
|
||||
if (context->GetBackendType() == impeller::Context::BackendType::kMetal &&
|
||||
resize_info.has_value()) {
|
||||
// The MPS used to resize images on iOS does not require mip generation.
|
||||
// Remove mip count if we are resizing the image on the GPU.
|
||||
texture_descriptor.mip_count = 1;
|
||||
}
|
||||
|
||||
auto dest_texture =
|
||||
context->GetResourceAllocator()->CreateTexture(texture_descriptor);
|
||||
@@ -323,59 +339,107 @@ static std::pair<sk_sp<DlImage>, std::string> UnsafeUploadTextureToPrivate(
|
||||
blit_pass->SetLabel("Mipmap Blit Pass");
|
||||
blit_pass->AddCopy(impeller::DeviceBuffer::AsBufferView(buffer),
|
||||
dest_texture);
|
||||
if (texture_descriptor.size.MipCount() > 1) {
|
||||
if (texture_descriptor.mip_count > 1) {
|
||||
blit_pass->GenerateMipmap(dest_texture);
|
||||
}
|
||||
|
||||
std::shared_ptr<impeller::Texture> result_texture = dest_texture;
|
||||
if (resize_info.has_value()) {
|
||||
impeller::TextureDescriptor resize_desc;
|
||||
resize_desc.storage_mode = impeller::StorageMode::kDevicePrivate;
|
||||
resize_desc.format = pixel_format.value();
|
||||
resize_desc.size = {resize_info->width(), resize_info->height()};
|
||||
resize_desc.mip_count = resize_desc.size.MipCount();
|
||||
resize_desc.compression_type = impeller::CompressionType::kLossy;
|
||||
resize_desc.usage = impeller::TextureUsage::kShaderRead;
|
||||
if (context->GetBackendType() == impeller::Context::BackendType::kMetal) {
|
||||
// Resizing requires a MPS on Metal platforms.
|
||||
resize_desc.usage |= impeller::TextureUsage::kShaderWrite;
|
||||
resize_desc.compression_type = impeller::CompressionType::kLossless;
|
||||
}
|
||||
|
||||
auto resize_texture =
|
||||
context->GetResourceAllocator()->CreateTexture(resize_desc);
|
||||
if (!resize_texture) {
|
||||
std::string decode_error("Could not create resized Impeller texture.");
|
||||
FML_DLOG(ERROR) << decode_error;
|
||||
return std::make_pair(nullptr, decode_error);
|
||||
}
|
||||
|
||||
blit_pass->ResizeTexture(/*source=*/dest_texture,
|
||||
/*destination=*/resize_texture);
|
||||
if (resize_desc.mip_count > 1) {
|
||||
blit_pass->GenerateMipmap(resize_texture);
|
||||
}
|
||||
|
||||
result_texture = std::move(resize_texture);
|
||||
}
|
||||
blit_pass->EncodeCommands(context->GetResourceAllocator());
|
||||
|
||||
if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) {
|
||||
std::string decode_error("Failed to submit blit pass command buffer.");
|
||||
std::string decode_error("Failed to submit image decoding command buffer.");
|
||||
FML_DLOG(ERROR) << decode_error;
|
||||
return std::make_pair(nullptr, decode_error);
|
||||
}
|
||||
|
||||
return std::make_pair(
|
||||
impeller::DlImageImpeller::Make(std::move(dest_texture)), std::string());
|
||||
impeller::DlImageImpeller::Make(std::move(result_texture)),
|
||||
std::string());
|
||||
}
|
||||
|
||||
std::pair<sk_sp<DlImage>, std::string>
|
||||
ImageDecoderImpeller::UploadTextureToPrivate(
|
||||
void ImageDecoderImpeller::UploadTextureToPrivate(
|
||||
ImageResult result,
|
||||
const std::shared_ptr<impeller::Context>& context,
|
||||
const std::shared_ptr<impeller::DeviceBuffer>& buffer,
|
||||
const SkImageInfo& image_info,
|
||||
const std::shared_ptr<SkBitmap>& bitmap,
|
||||
const std::optional<SkImageInfo>& resize_info,
|
||||
const std::shared_ptr<fml::SyncSwitch>& gpu_disabled_switch) {
|
||||
TRACE_EVENT0("impeller", __FUNCTION__);
|
||||
if (!context) {
|
||||
return std::make_pair(nullptr, "No Impeller context is available");
|
||||
result(nullptr, "No Impeller context is available");
|
||||
return;
|
||||
}
|
||||
if (!buffer) {
|
||||
return std::make_pair(nullptr, "No Impeller device buffer is available");
|
||||
result(nullptr, "No Impeller device buffer is available");
|
||||
return;
|
||||
}
|
||||
|
||||
std::pair<sk_sp<DlImage>, std::string> result;
|
||||
gpu_disabled_switch->Execute(
|
||||
fml::SyncSwitch::Handlers()
|
||||
.SetIfFalse([&result, context, buffer, image_info] {
|
||||
result = UnsafeUploadTextureToPrivate(context, buffer, image_info);
|
||||
.SetIfFalse([&result, context, buffer, image_info, resize_info] {
|
||||
sk_sp<DlImage> image;
|
||||
std::string decode_error;
|
||||
std::tie(image, decode_error) = std::tie(image, decode_error) =
|
||||
UnsafeUploadTextureToPrivate(context, buffer, image_info,
|
||||
resize_info);
|
||||
result(image, decode_error);
|
||||
})
|
||||
.SetIfTrue([&result, context, bitmap, gpu_disabled_switch] {
|
||||
// create_mips is false because we already know the GPU is disabled.
|
||||
result =
|
||||
UploadTextureToStorage(context, bitmap, gpu_disabled_switch,
|
||||
impeller::StorageMode::kHostVisible,
|
||||
/*create_mips=*/false);
|
||||
.SetIfTrue([&result, context, buffer, image_info, resize_info] {
|
||||
// The `result` function must be copied in the capture list for each
|
||||
// closure or the stack allocated callback will be cleared by the
|
||||
// time to closure is executed later.
|
||||
context->StoreTaskForGPU(
|
||||
[result, context, buffer, image_info, resize_info]() {
|
||||
sk_sp<DlImage> image;
|
||||
std::string decode_error;
|
||||
std::tie(image, decode_error) =
|
||||
std::tie(image, decode_error) =
|
||||
UnsafeUploadTextureToPrivate(context, buffer,
|
||||
image_info, resize_info);
|
||||
result(image, decode_error);
|
||||
},
|
||||
[result]() {
|
||||
result(nullptr,
|
||||
"Image upload failed due to loss of GPU access.");
|
||||
});
|
||||
}));
|
||||
return result;
|
||||
}
|
||||
|
||||
std::pair<sk_sp<DlImage>, std::string>
|
||||
ImageDecoderImpeller::UploadTextureToStorage(
|
||||
const std::shared_ptr<impeller::Context>& context,
|
||||
std::shared_ptr<SkBitmap> bitmap,
|
||||
const std::shared_ptr<fml::SyncSwitch>& gpu_disabled_switch,
|
||||
impeller::StorageMode storage_mode,
|
||||
bool create_mips) {
|
||||
std::shared_ptr<SkBitmap> bitmap) {
|
||||
TRACE_EVENT0("impeller", __FUNCTION__);
|
||||
if (!context) {
|
||||
return std::make_pair(nullptr, "No Impeller context is available");
|
||||
@@ -394,11 +458,10 @@ ImageDecoderImpeller::UploadTextureToStorage(
|
||||
}
|
||||
|
||||
impeller::TextureDescriptor texture_descriptor;
|
||||
texture_descriptor.storage_mode = storage_mode;
|
||||
texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
|
||||
texture_descriptor.format = pixel_format.value();
|
||||
texture_descriptor.size = {image_info.width(), image_info.height()};
|
||||
texture_descriptor.mip_count =
|
||||
create_mips ? texture_descriptor.size.MipCount() : 1;
|
||||
texture_descriptor.mip_count = 1;
|
||||
|
||||
auto texture =
|
||||
context->GetResourceAllocator()->CreateTexture(texture_descriptor);
|
||||
@@ -421,43 +484,6 @@ ImageDecoderImpeller::UploadTextureToStorage(
|
||||
}
|
||||
|
||||
texture->SetLabel(impeller::SPrintF("ui.Image(%p)", texture.get()).c_str());
|
||||
|
||||
if (texture_descriptor.mip_count > 1u && create_mips) {
|
||||
std::optional<std::string> decode_error;
|
||||
|
||||
// The only platform that needs mipmapping unconditionally is GL.
|
||||
// GL based platforms never disable GPU access.
|
||||
// This is only really needed for iOS.
|
||||
gpu_disabled_switch->Execute(fml::SyncSwitch::Handlers().SetIfFalse(
|
||||
[context, &texture, &decode_error] {
|
||||
auto command_buffer = context->CreateCommandBuffer();
|
||||
if (!command_buffer) {
|
||||
decode_error =
|
||||
"Could not create command buffer for mipmap generation.";
|
||||
return;
|
||||
}
|
||||
command_buffer->SetLabel("Mipmap Command Buffer");
|
||||
|
||||
auto blit_pass = command_buffer->CreateBlitPass();
|
||||
if (!blit_pass) {
|
||||
decode_error = "Could not create blit pass for mipmap generation.";
|
||||
return;
|
||||
}
|
||||
blit_pass->SetLabel("Mipmap Blit Pass");
|
||||
blit_pass->GenerateMipmap(texture);
|
||||
blit_pass->EncodeCommands(context->GetResourceAllocator());
|
||||
if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) {
|
||||
decode_error = "Failed to submit blit pass command buffer.";
|
||||
return;
|
||||
}
|
||||
command_buffer->WaitUntilScheduled();
|
||||
}));
|
||||
if (decode_error.has_value()) {
|
||||
FML_DLOG(ERROR) << decode_error.value();
|
||||
return std::make_pair(nullptr, decode_error.value());
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_pair(impeller::DlImageImpeller::Make(std::move(texture)),
|
||||
std::string());
|
||||
}
|
||||
@@ -509,14 +535,21 @@ void ImageDecoderImpeller::Decode(fml::RefPtr<ImageDescriptor> descriptor,
|
||||
|
||||
auto upload_texture_and_invoke_result = [result, context, bitmap_result,
|
||||
gpu_disabled_switch]() {
|
||||
sk_sp<DlImage> image;
|
||||
std::string decode_error;
|
||||
std::tie(image, decode_error) = UploadTextureToPrivate(
|
||||
context, bitmap_result.device_buffer, bitmap_result.image_info,
|
||||
bitmap_result.sk_bitmap, gpu_disabled_switch);
|
||||
result(image, decode_error);
|
||||
UploadTextureToPrivate(result, context, //
|
||||
bitmap_result.device_buffer, //
|
||||
bitmap_result.image_info, //
|
||||
bitmap_result.sk_bitmap, //
|
||||
bitmap_result.resize_info, //
|
||||
gpu_disabled_switch //
|
||||
);
|
||||
};
|
||||
io_runner->PostTask(upload_texture_and_invoke_result);
|
||||
// The I/O image uploads are not threadsafe on GLES.
|
||||
if (context->GetBackendType() ==
|
||||
impeller::Context::BackendType::kOpenGLES) {
|
||||
io_runner->PostTask(upload_texture_and_invoke_result);
|
||||
} else {
|
||||
upload_texture_and_invoke_result();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "flutter/lib/ui/painting/image_decoder.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/geometry/size.h"
|
||||
#include "include/core/SkImageInfo.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -41,6 +42,7 @@ struct DecompressResult {
|
||||
std::shared_ptr<impeller::DeviceBuffer> device_buffer;
|
||||
std::shared_ptr<SkBitmap> sk_bitmap;
|
||||
SkImageInfo image_info;
|
||||
std::optional<SkImageInfo> resize_info = std::nullopt;
|
||||
std::string decode_error;
|
||||
};
|
||||
|
||||
@@ -69,34 +71,30 @@ class ImageDecoderImpeller final : public ImageDecoder {
|
||||
const std::shared_ptr<impeller::Allocator>& allocator);
|
||||
|
||||
/// @brief Create a device private texture from the provided host buffer.
|
||||
/// This method is only suported on the metal backend.
|
||||
///
|
||||
/// @param result The image result closure that accepts the DlImage and
|
||||
/// any encoding error messages.
|
||||
/// @param context The Impeller graphics context.
|
||||
/// @param buffer A host buffer containing the image to be uploaded.
|
||||
/// @param image_info Format information about the particular image.
|
||||
/// @param bitmap A bitmap containg the image to be uploaded.
|
||||
/// @param gpu_disabled_switch Whether the GPU is available command encoding.
|
||||
/// @return A DlImage.
|
||||
static std::pair<sk_sp<DlImage>, std::string> UploadTextureToPrivate(
|
||||
static void UploadTextureToPrivate(
|
||||
ImageResult result,
|
||||
const std::shared_ptr<impeller::Context>& context,
|
||||
const std::shared_ptr<impeller::DeviceBuffer>& buffer,
|
||||
const SkImageInfo& image_info,
|
||||
const std::shared_ptr<SkBitmap>& bitmap,
|
||||
const std::optional<SkImageInfo>& resize_info,
|
||||
const std::shared_ptr<fml::SyncSwitch>& gpu_disabled_switch);
|
||||
|
||||
/// @brief Create a host visible texture from the provided bitmap.
|
||||
/// @brief Create a texture from the provided bitmap.
|
||||
/// @param context The Impeller graphics context.
|
||||
/// @param bitmap A bitmap containg the image to be uploaded.
|
||||
/// @param create_mips Whether mipmaps should be generated for the given
|
||||
/// image.
|
||||
/// @param gpu_disabled_switch Whether the GPU is available for mipmap
|
||||
/// creation.
|
||||
/// @return A DlImage.
|
||||
static std::pair<sk_sp<DlImage>, std::string> UploadTextureToStorage(
|
||||
const std::shared_ptr<impeller::Context>& context,
|
||||
std::shared_ptr<SkBitmap> bitmap,
|
||||
const std::shared_ptr<fml::SyncSwitch>& gpu_disabled_switch,
|
||||
impeller::StorageMode storage_mode,
|
||||
bool create_mips = true);
|
||||
std::shared_ptr<SkBitmap> bitmap);
|
||||
|
||||
private:
|
||||
using FutureContext = std::shared_future<std::shared_ptr<impeller::Context>>;
|
||||
@@ -104,6 +102,13 @@ class ImageDecoderImpeller final : public ImageDecoder {
|
||||
const bool supports_wide_gamut_;
|
||||
std::shared_ptr<fml::SyncSwitch> gpu_disabled_switch_;
|
||||
|
||||
/// Only call this method if the GPU is available.
|
||||
static std::pair<sk_sp<DlImage>, std::string> UnsafeUploadTextureToPrivate(
|
||||
const std::shared_ptr<impeller::Context>& context,
|
||||
const std::shared_ptr<impeller::DeviceBuffer>& buffer,
|
||||
const SkImageInfo& image_info,
|
||||
const std::optional<SkImageInfo>& resize_info);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ImageDecoderImpeller);
|
||||
};
|
||||
|
||||
|
||||
@@ -76,11 +76,32 @@ class TestImpellerContext : public impeller::Context {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void StoreTaskForGPU(const std::function<void()>& task,
|
||||
const std::function<void()>& failure) override {
|
||||
tasks_.push_back(PendingTask{task, failure});
|
||||
}
|
||||
|
||||
void FlushTasks(bool fail = false) {
|
||||
for (auto& task : tasks_) {
|
||||
if (fail) {
|
||||
task.task();
|
||||
} else {
|
||||
task.failure();
|
||||
}
|
||||
}
|
||||
tasks_.clear();
|
||||
}
|
||||
|
||||
void Shutdown() override {}
|
||||
|
||||
mutable size_t command_buffer_count_ = 0;
|
||||
|
||||
private:
|
||||
struct PendingTask {
|
||||
std::function<void()> task;
|
||||
std::function<void()> failure;
|
||||
};
|
||||
std::vector<PendingTask> tasks_;
|
||||
std::shared_ptr<const Capabilities> capabilities_;
|
||||
};
|
||||
|
||||
@@ -336,16 +357,66 @@ TEST_F(ImageDecoderFixtureTest, ImpellerUploadToSharedNoGpu) {
|
||||
desc.size = bitmap->computeByteSize();
|
||||
auto buffer = std::make_shared<impeller::TestImpellerDeviceBuffer>(desc);
|
||||
|
||||
auto result = ImageDecoderImpeller::UploadTextureToPrivate(
|
||||
no_gpu_access_context, buffer, info, bitmap, gpu_disabled_switch);
|
||||
bool invoked = false;
|
||||
auto cb = [&invoked](const sk_sp<DlImage>& image,
|
||||
const std::string& message) { invoked = true; };
|
||||
|
||||
ImageDecoderImpeller::UploadTextureToPrivate(
|
||||
cb, no_gpu_access_context, buffer, info, bitmap, std::nullopt,
|
||||
gpu_disabled_switch);
|
||||
|
||||
EXPECT_EQ(no_gpu_access_context->command_buffer_count_, 0ul);
|
||||
EXPECT_FALSE(invoked);
|
||||
|
||||
auto result = ImageDecoderImpeller::UploadTextureToStorage(
|
||||
no_gpu_access_context, bitmap);
|
||||
|
||||
ASSERT_EQ(no_gpu_access_context->command_buffer_count_, 0ul);
|
||||
ASSERT_EQ(result.second, "");
|
||||
|
||||
result = ImageDecoderImpeller::UploadTextureToStorage(
|
||||
no_gpu_access_context, bitmap, gpu_disabled_switch,
|
||||
impeller::StorageMode::kHostVisible, true);
|
||||
ASSERT_EQ(no_gpu_access_context->command_buffer_count_, 0ul);
|
||||
ASSERT_EQ(result.second, "");
|
||||
no_gpu_access_context->FlushTasks(/*fail=*/true);
|
||||
}
|
||||
|
||||
TEST_F(ImageDecoderFixtureTest,
|
||||
ImpellerUploadToSharedNoGpuTaskFlushingFailure) {
|
||||
#if !IMPELLER_SUPPORTS_RENDERING
|
||||
GTEST_SKIP() << "Impeller only test.";
|
||||
#endif // IMPELLER_SUPPORTS_RENDERING
|
||||
|
||||
auto no_gpu_access_context =
|
||||
std::make_shared<impeller::TestImpellerContext>();
|
||||
auto gpu_disabled_switch = std::make_shared<fml::SyncSwitch>(true);
|
||||
|
||||
auto info = SkImageInfo::Make(10, 10, SkColorType::kRGBA_8888_SkColorType,
|
||||
SkAlphaType::kPremul_SkAlphaType);
|
||||
auto bitmap = std::make_shared<SkBitmap>();
|
||||
bitmap->allocPixels(info, 10 * 4);
|
||||
impeller::DeviceBufferDescriptor desc;
|
||||
desc.size = bitmap->computeByteSize();
|
||||
auto buffer = std::make_shared<impeller::TestImpellerDeviceBuffer>(desc);
|
||||
|
||||
sk_sp<DlImage> image;
|
||||
std::string message;
|
||||
bool invoked = false;
|
||||
auto cb = [&invoked, &image, &message](sk_sp<DlImage> p_image,
|
||||
std::string p_message) {
|
||||
invoked = true;
|
||||
image = std::move(p_image);
|
||||
message = std::move(p_message);
|
||||
};
|
||||
|
||||
ImageDecoderImpeller::UploadTextureToPrivate(
|
||||
cb, no_gpu_access_context, buffer, info, bitmap, std::nullopt,
|
||||
gpu_disabled_switch);
|
||||
|
||||
EXPECT_EQ(no_gpu_access_context->command_buffer_count_, 0ul);
|
||||
EXPECT_FALSE(invoked);
|
||||
|
||||
no_gpu_access_context->FlushTasks(/*fail=*/true);
|
||||
|
||||
EXPECT_TRUE(invoked);
|
||||
// Creation of the dl image will still fail with the mocked context.
|
||||
EXPECT_NE(message, "");
|
||||
}
|
||||
|
||||
TEST_F(ImageDecoderFixtureTest, ImpellerNullColorspace) {
|
||||
@@ -707,8 +778,8 @@ TEST(ImageDecoderTest, VerifySimpleDecoding) {
|
||||
auto data = flutter::testing::OpenFixtureAsSkData("Horizontal.jpg");
|
||||
auto image = SkImages::DeferredFromEncodedData(data);
|
||||
ASSERT_TRUE(image != nullptr);
|
||||
ASSERT_EQ(600, image->width());
|
||||
ASSERT_EQ(200, image->height());
|
||||
EXPECT_EQ(600, image->width());
|
||||
EXPECT_EQ(200, image->height());
|
||||
|
||||
ImageGeneratorRegistry registry;
|
||||
std::shared_ptr<ImageGenerator> generator =
|
||||
@@ -719,24 +790,36 @@ TEST(ImageDecoderTest, VerifySimpleDecoding) {
|
||||
std::move(generator));
|
||||
auto compressed_image = ImageDecoderSkia::ImageFromCompressedData(
|
||||
descriptor.get(), 6, 2, fml::tracing::TraceFlow(""));
|
||||
ASSERT_EQ(compressed_image->width(), 6);
|
||||
ASSERT_EQ(compressed_image->height(), 2);
|
||||
ASSERT_EQ(compressed_image->alphaType(), kOpaque_SkAlphaType);
|
||||
EXPECT_EQ(compressed_image->width(), 6);
|
||||
EXPECT_EQ(compressed_image->height(), 2);
|
||||
EXPECT_EQ(compressed_image->alphaType(), kOpaque_SkAlphaType);
|
||||
|
||||
#if IMPELLER_SUPPORTS_RENDERING
|
||||
// Bitmap sizes reflect the original image size as resizing is done on the
|
||||
// GPU if the src size is smaller than the max texture size.
|
||||
std::shared_ptr<impeller::Allocator> allocator =
|
||||
std::make_shared<impeller::TestImpellerAllocator>();
|
||||
auto result_1 = ImageDecoderImpeller::DecompressTexture(
|
||||
descriptor.get(), SkISize::Make(6, 2), {100, 100},
|
||||
descriptor.get(), SkISize::Make(6, 2), {1000, 1000},
|
||||
/*supports_wide_gamut=*/false, allocator);
|
||||
ASSERT_EQ(result_1.sk_bitmap->width(), 6);
|
||||
ASSERT_EQ(result_1.sk_bitmap->height(), 2);
|
||||
EXPECT_EQ(result_1.sk_bitmap->width(), 75);
|
||||
EXPECT_EQ(result_1.sk_bitmap->height(), 25);
|
||||
|
||||
// Bitmap sizes reflect the scaled size if the source size is larger than
|
||||
// max texture size even if destination size isn't max texture size.
|
||||
auto result_2 = ImageDecoderImpeller::DecompressTexture(
|
||||
descriptor.get(), SkISize::Make(6, 2), {10, 10},
|
||||
/*supports_wide_gamut=*/false, allocator);
|
||||
EXPECT_EQ(result_2.sk_bitmap->width(), 6);
|
||||
EXPECT_EQ(result_2.sk_bitmap->height(), 2);
|
||||
|
||||
// If the destination size is larger than the max texture size the image
|
||||
// is scaled down.
|
||||
auto result_3 = ImageDecoderImpeller::DecompressTexture(
|
||||
descriptor.get(), SkISize::Make(60, 20), {10, 10},
|
||||
/*supports_wide_gamut=*/false, allocator);
|
||||
ASSERT_EQ(result_2.sk_bitmap->width(), 10);
|
||||
ASSERT_EQ(result_2.sk_bitmap->height(), 10);
|
||||
EXPECT_EQ(result_3.sk_bitmap->width(), 10);
|
||||
EXPECT_EQ(result_3.sk_bitmap->height(), 10);
|
||||
#endif // IMPELLER_SUPPORTS_RENDERING
|
||||
}
|
||||
|
||||
@@ -963,8 +1046,7 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) {
|
||||
PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); });
|
||||
}
|
||||
|
||||
TEST_F(ImageDecoderFixtureTest,
|
||||
MultiFrameCodecProducesATextureEvenIfGPUIsDisabledOnImpeller) {
|
||||
TEST_F(ImageDecoderFixtureTest, MultiFrameCodecIsPausedWhenGPUIsUnavailable) {
|
||||
auto settings = CreateSettingsForFixture();
|
||||
settings.enable_impeller = true;
|
||||
auto vm_ref = DartVMRef::Create(settings);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "flutter/lib/ui/painting/image_encoding_impeller.h"
|
||||
|
||||
#include "flutter/lib/ui/painting/image.h"
|
||||
#include "fml/status.h"
|
||||
#include "impeller/core/device_buffer.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/renderer/command_buffer.h"
|
||||
@@ -90,8 +91,7 @@ void DoConvertImageToRasterImpellerWithRetry(
|
||||
// task on the Context so it can be executed when the GPU becomes available.
|
||||
if (status.code() == fml::StatusCode::kUnavailable) {
|
||||
impeller_context->StoreTaskForGPU(
|
||||
[dl_image, encode_task = std::move(encode_task),
|
||||
is_gpu_disabled_sync_switch, impeller_context,
|
||||
[dl_image, encode_task, is_gpu_disabled_sync_switch, impeller_context,
|
||||
retry_runner]() mutable {
|
||||
auto retry_task = [dl_image, encode_task = std::move(encode_task),
|
||||
is_gpu_disabled_sync_switch, impeller_context] {
|
||||
@@ -111,6 +111,10 @@ void DoConvertImageToRasterImpellerWithRetry(
|
||||
} else {
|
||||
retry_task();
|
||||
}
|
||||
},
|
||||
[encode_task]() {
|
||||
encode_task(
|
||||
fml::Status(fml::StatusCode::kUnavailable, "GPU unavailable."));
|
||||
});
|
||||
} else {
|
||||
// Pass on errors that are not `kUnavailable`.
|
||||
|
||||
@@ -374,8 +374,8 @@ TEST_F(ShellTest, EncodeImageFailsWithoutGPUImpeller) {
|
||||
shell->GetIOManager()->GetImpellerContext();
|
||||
// This will cause the stored tasks to overflow and start throwing them
|
||||
// away.
|
||||
for (int i = 0; i < impeller::Context::kMaxTasksAwaitingGPU; ++i) {
|
||||
impeller_context->StoreTaskForGPU([] {});
|
||||
for (int i = 0; i < impeller::Context::kMaxTasksAwaitingGPU; i++) {
|
||||
impeller_context->StoreTaskForGPU([] {}, [] {});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -147,10 +147,7 @@ MultiFrameCodec::State::GetNextFrameImage(
|
||||
// This is safe regardless of whether the GPU is available or not because
|
||||
// without mipmap creation there is no command buffer encoding done.
|
||||
return ImageDecoderImpeller::UploadTextureToStorage(
|
||||
impeller_context, std::make_shared<SkBitmap>(bitmap),
|
||||
std::make_shared<fml::SyncSwitch>(),
|
||||
impeller::StorageMode::kHostVisible,
|
||||
/*create_mips=*/false);
|
||||
impeller_context, std::make_shared<SkBitmap>(bitmap));
|
||||
}
|
||||
#endif // IMPELLER_SUPPORTS_RENDERING
|
||||
|
||||
|
||||
@@ -137,10 +137,11 @@ void SnapshotControllerImpeller::MakeRasterSnapshot(
|
||||
if (context) {
|
||||
context->GetContext()->StoreTaskForGPU(
|
||||
[context, sync_switch, display_list = std::move(display_list),
|
||||
picture_size, callback = std::move(callback)] {
|
||||
picture_size, callback] {
|
||||
callback(DoMakeRasterSnapshot(display_list, picture_size,
|
||||
sync_switch, context));
|
||||
});
|
||||
},
|
||||
[callback]() { callback(nullptr); });
|
||||
} else {
|
||||
callback(nullptr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user