diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index f4e63b1bd0..d2318510b9 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -144,7 +144,6 @@ ../../../flutter/impeller/display_list/skia_conversions_unittests.cc ../../../flutter/impeller/docs ../../../flutter/impeller/entity/contents/clip_contents_unittests.cc -../../../flutter/impeller/entity/contents/content_context_unittests.cc ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc ../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc ../../../flutter/impeller/entity/contents/host_buffer_unittests.cc diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index b4cd00fa9d..bdc8533b04 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -4,7 +4,6 @@ #include "flutter/impeller/aiks/aiks_unittests.h" -#include #include #include #include @@ -22,7 +21,6 @@ #include "impeller/aiks/paint_pass_delegate.h" #include "impeller/aiks/testing/context_spy.h" #include "impeller/entity/contents/solid_color_contents.h" -#include "impeller/entity/render_target_cache.h" #include "impeller/geometry/color.h" #include "impeller/geometry/constants.h" #include "impeller/geometry/geometry_asserts.h" @@ -35,7 +33,6 @@ #include "impeller/renderer/command_buffer.h" #include "impeller/renderer/snapshot.h" #include "impeller/typographer/backends/skia/text_frame_skia.h" -#include "impeller/typographer/backends/skia/typographer_context_skia.h" #include "impeller/typographer/backends/stb/text_frame_stb.h" #include "impeller/typographer/backends/stb/typeface_stb.h" #include "impeller/typographer/backends/stb/typographer_context_stb.h" @@ -3061,12 +3058,13 @@ TEST_P(AiksTest, MipmapGenerationWorksCorrectly) { auto texture = GetContext()->GetResourceAllocator()->CreateTexture(texture_descriptor); - ASSERT_TRUE(!!texture); - ASSERT_TRUE(texture->SetContents(mapping)); - + auto device_buffer = + GetContext()->GetResourceAllocator()->CreateBufferWithCopy(*mapping); auto command_buffer = GetContext()->CreateCommandBuffer(); auto blit_pass = command_buffer->CreateBlitPass(); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)), + texture); blit_pass->GenerateMipmap(texture); EXPECT_TRUE(blit_pass->EncodeCommands(GetContext()->GetResourceAllocator())); EXPECT_TRUE(GetContext()->GetCommandQueue()->Submit({command_buffer}).ok()); diff --git a/engine/src/flutter/impeller/core/device_buffer.cc b/engine/src/flutter/impeller/core/device_buffer.cc index 3eae7e0a5f..1df3c03b36 100644 --- a/engine/src/flutter/impeller/core/device_buffer.cc +++ b/engine/src/flutter/impeller/core/device_buffer.cc @@ -22,21 +22,6 @@ BufferView DeviceBuffer::AsBufferView(std::shared_ptr buffer) { return view; } -std::shared_ptr DeviceBuffer::AsTexture( - Allocator& allocator, - const TextureDescriptor& descriptor, - uint16_t row_bytes) const { - auto texture = allocator.CreateTexture(descriptor); - if (!texture) { - return nullptr; - } - if (!texture->SetContents(std::make_shared( - OnGetContents(), desc_.size))) { - return nullptr; - } - return texture; -} - const DeviceBufferDescriptor& DeviceBuffer::GetDeviceBufferDescriptor() const { return desc_; } diff --git a/engine/src/flutter/impeller/core/device_buffer.h b/engine/src/flutter/impeller/core/device_buffer.h index 0306fad46e..07dba77ccf 100644 --- a/engine/src/flutter/impeller/core/device_buffer.h +++ b/engine/src/flutter/impeller/core/device_buffer.h @@ -12,7 +12,6 @@ #include "impeller/core/buffer_view.h" #include "impeller/core/device_buffer_descriptor.h" #include "impeller/core/range.h" -#include "impeller/core/texture.h" namespace impeller { @@ -31,11 +30,6 @@ class DeviceBuffer { /// @brief Create a buffer view of this entire buffer. static BufferView AsBufferView(std::shared_ptr buffer); - virtual std::shared_ptr AsTexture( - Allocator& allocator, - const TextureDescriptor& descriptor, - uint16_t row_bytes) const; - const DeviceBufferDescriptor& GetDeviceBufferDescriptor() const; virtual uint8_t* OnGetContents() const = 0; diff --git a/engine/src/flutter/impeller/core/texture.h b/engine/src/flutter/impeller/core/texture.h index af14a9e933..3be29ae71c 100644 --- a/engine/src/flutter/impeller/core/texture.h +++ b/engine/src/flutter/impeller/core/texture.h @@ -20,11 +20,13 @@ class Texture { virtual void SetLabel(std::string_view label) = 0; + // Deprecated: use BlitPass::AddCopy instead. [[nodiscard]] bool SetContents(const uint8_t* contents, size_t length, size_t slice = 0, bool is_opaque = false); + // Deprecated: use BlitPass::AddCopy instead. [[nodiscard]] bool SetContents(std::shared_ptr mapping, size_t slice = 0, bool is_opaque = false); @@ -39,6 +41,10 @@ class Texture { const TextureDescriptor& GetTextureDescriptor() const; + /// Update the coordinate system used by the texture. + /// + /// The setting is used to conditionally invert the coordinates to + /// account for the different origin of GLES textures. void SetCoordinateSystem(TextureCoordinateSystem coordinate_system); TextureCoordinateSystem GetCoordinateSystem() const; diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 9bd3fb78df..c529d46684 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -239,7 +239,6 @@ impeller_component("entity_unittests") { sources = [ "contents/clip_contents_unittests.cc", - "contents/content_context_unittests.cc", "contents/filters/gaussian_blur_filter_contents_unittests.cc", "contents/filters/inputs/filter_input_unittests.cc", "contents/host_buffer_unittests.cc", diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index 428ef5bd04..fe3338fdaa 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -263,7 +263,17 @@ ContentContext::ContentContext( desc.size = ISize{1, 1}; empty_texture_ = GetContext()->GetResourceAllocator()->CreateTexture(desc); auto data = Color::BlackTransparent().ToR8G8B8A8(); - if (!empty_texture_->SetContents(data.data(), 4)) { + auto cmd_buffer = GetContext()->CreateCommandBuffer(); + auto blit_pass = cmd_buffer->CreateBlitPass(); + auto& host_buffer = GetTransientsBuffer(); + auto buffer_view = host_buffer.Emplace(data); + blit_pass->AddCopy(buffer_view, empty_texture_); + + if (!blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()) || + !GetContext() + ->GetCommandQueue() + ->Submit({std::move(cmd_buffer)}) + .ok()) { VALIDATION_LOG << "Failed to create empty texture."; } } diff --git a/engine/src/flutter/impeller/entity/contents/content_context_unittests.cc b/engine/src/flutter/impeller/entity/contents/content_context_unittests.cc deleted file mode 100644 index 1063b069bb..0000000000 --- a/engine/src/flutter/impeller/entity/contents/content_context_unittests.cc +++ /dev/null @@ -1,356 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include -#include -#include -#include - -#include "fml/logging.h" -#include "gtest/gtest.h" - -#include "impeller/base/backend_cast.h" -#include "impeller/base/comparable.h" -#include "impeller/core/allocator.h" -#include "impeller/core/device_buffer_descriptor.h" -#include "impeller/core/formats.h" -#include "impeller/core/texture_descriptor.h" -#include "impeller/entity/contents/content_context.h" -#include "impeller/entity/contents/test/recording_render_pass.h" -#include "impeller/geometry/color.h" -#include "impeller/renderer/capabilities.h" -#include "impeller/renderer/command_buffer.h" -#include "impeller/renderer/command_queue.h" -#include "impeller/renderer/pipeline.h" -#include "impeller/renderer/pipeline_descriptor.h" -#include "impeller/renderer/pipeline_library.h" -#include "impeller/renderer/render_pass.h" -#include "impeller/renderer/shader_function.h" -#include "impeller/renderer/shader_library.h" - -namespace impeller { -namespace testing { - -namespace { -class FakeTexture : public Texture { - public: - explicit FakeTexture(const TextureDescriptor& desc) : Texture(desc) {} - - ~FakeTexture() override {} - - void SetLabel(std::string_view label) {} - - bool IsValid() const override { return true; } - - ISize GetSize() const override { return {1, 1}; } - - Scalar GetYCoordScale() const override { return 1.0; } - - bool OnSetContents(const uint8_t* contents, - size_t length, - size_t slice) override { - if (GetTextureDescriptor().GetByteSizeOfBaseMipLevel() != length) { - return false; - } - did_set_contents = true; - return true; - } - - bool OnSetContents(std::shared_ptr mapping, - size_t slice) override { - did_set_contents = true; - return true; - } - - bool did_set_contents = false; -}; - -class FakeAllocator : public Allocator, - public BackendCast { - public: - FakeAllocator() : Allocator() {} - - uint16_t MinimumBytesPerRow(PixelFormat format) const override { return 0; } - ISize GetMaxTextureSizeSupported() const override { return ISize(1, 1); } - - std::shared_ptr OnCreateBuffer( - const DeviceBufferDescriptor& desc) override { - return nullptr; - } - std::shared_ptr OnCreateTexture( - const TextureDescriptor& desc) override { - if (desc.size == ISize{1, 1}) { - auto result = std::make_shared(desc); - textures.push_back(result); - return result; - } - return nullptr; - } - - std::vector> textures = {}; -}; - -class FakePipeline : public Pipeline { - public: - FakePipeline(std::weak_ptr library, - const PipelineDescriptor& desc) - : Pipeline(std::move(library), desc) {} - - ~FakePipeline() override {} - - bool IsValid() const override { return true; } -}; - -class FakeComputePipeline : public Pipeline { - public: - FakeComputePipeline(std::weak_ptr library, - const ComputePipelineDescriptor& desc) - : Pipeline(std::move(library), desc) {} - - ~FakeComputePipeline() override {} - - bool IsValid() const override { return true; } -}; - -class FakePipelineLibrary : public PipelineLibrary { - public: - FakePipelineLibrary() {} - - ~FakePipelineLibrary() override {} - - bool IsValid() const override { return true; } - - PipelineFuture GetPipeline(PipelineDescriptor descriptor, - bool async) override { - auto pipeline = - std::make_shared(weak_from_this(), descriptor); - std::promise>> promise; - promise.set_value(std::move(pipeline)); - return PipelineFuture{ - .descriptor = descriptor, - .future = - std::shared_future>>( - promise.get_future())}; - } - - PipelineFuture GetPipeline( - ComputePipelineDescriptor descriptor, - bool async) override { - auto pipeline = - std::make_shared(weak_from_this(), descriptor); - std::promise>> promise; - promise.set_value(std::move(pipeline)); - return PipelineFuture{ - .descriptor = descriptor, - .future = std::shared_future< - std::shared_ptr>>( - promise.get_future())}; - } - - void RemovePipelinesWithEntryPoint( - std::shared_ptr function) {} -}; - -class FakeShaderFunction : public ShaderFunction { - public: - FakeShaderFunction(UniqueID parent_library_id, - std::string name, - ShaderStage stage) - : ShaderFunction(parent_library_id, std::move(name), stage){}; - - ~FakeShaderFunction() override {} -}; - -class FakeShaderLibrary : public ShaderLibrary { - public: - ~FakeShaderLibrary() override {} - - bool IsValid() const override { return true; } - - std::shared_ptr GetFunction(std::string_view name, - ShaderStage stage) { - return std::make_shared(UniqueID{}, std::string(name), - stage); - } - - void RegisterFunction(std::string name, - ShaderStage stage, - std::shared_ptr code, - RegistrationCallback callback) override {} - - void UnregisterFunction(std::string name, ShaderStage stage) override {} -}; - -class FakeCommandBuffer : public CommandBuffer { - public: - explicit FakeCommandBuffer(std::weak_ptr context) - : CommandBuffer(std::move(context)) {} - - ~FakeCommandBuffer() {} - - bool IsValid() const override { return true; } - - void SetLabel(const std::string& label) const override {} - - std::shared_ptr OnCreateRenderPass( - RenderTarget render_target) override { - return std::make_shared(nullptr, context_.lock(), - render_target); - } - - std::shared_ptr OnCreateBlitPass() override { FML_UNREACHABLE() } - - virtual bool OnSubmitCommands(CompletionCallback callback) { return true; } - - void OnWaitUntilScheduled() {} - - std::shared_ptr OnCreateComputePass() override { - FML_UNREACHABLE(); - } -}; - -class FakeContext : public Context, - public std::enable_shared_from_this { - public: - explicit FakeContext( - const std::string& gpu_model = "", - PixelFormat default_color_format = PixelFormat::kR8G8B8A8UNormInt) - : Context(), - allocator_(std::make_shared()), - capabilities_(std::shared_ptr( - CapabilitiesBuilder() - .SetDefaultColorFormat(default_color_format) - .Build())), - pipelines_(std::make_shared()), - queue_(std::make_shared()), - shader_library_(std::make_shared()), - gpu_model_(gpu_model) {} - - BackendType GetBackendType() const override { return BackendType::kVulkan; } - std::string DescribeGpuModel() const override { return gpu_model_; } - bool IsValid() const override { return true; } - const std::shared_ptr& GetCapabilities() const override { - return capabilities_; - } - std::shared_ptr GetResourceAllocator() const override { - return allocator_; - } - std::shared_ptr GetShaderLibrary() const { - return shader_library_; - } - std::shared_ptr GetSamplerLibrary() const { return nullptr; } - std::shared_ptr GetPipelineLibrary() const { - return pipelines_; - } - std::shared_ptr GetCommandQueue() const { return queue_; } - std::shared_ptr CreateCommandBuffer() const { - return std::make_shared(shared_from_this()); - } - void Shutdown() {} - - private: - std::shared_ptr allocator_; - std::shared_ptr capabilities_; - std::shared_ptr pipelines_; - std::shared_ptr queue_; - std::shared_ptr shader_library_; - std::string gpu_model_; -}; -} // namespace - -TEST(ContentContext, CachesPipelines) { - auto context = std::make_shared(); - - auto create_callback = [&]() { - return std::make_shared(context->GetPipelineLibrary(), - PipelineDescriptor{}); - }; - - ContentContext content_context(context, nullptr); - ContentContextOptions optionsA{.blend_mode = BlendMode::kSourceOver}; - ContentContextOptions optionsB{.blend_mode = BlendMode::kSource}; - - auto pipelineA = content_context.GetCachedRuntimeEffectPipeline( - "A", optionsA, create_callback); - - auto pipelineA2 = content_context.GetCachedRuntimeEffectPipeline( - "A", optionsA, create_callback); - - auto pipelineA3 = content_context.GetCachedRuntimeEffectPipeline( - "A", optionsB, create_callback); - - auto pipelineB = content_context.GetCachedRuntimeEffectPipeline( - "B", optionsB, create_callback); - - ASSERT_EQ(pipelineA.get(), pipelineA2.get()); - ASSERT_NE(pipelineA.get(), pipelineA3.get()); - ASSERT_NE(pipelineB.get(), pipelineA.get()); -} - -TEST(ContentContext, InvalidatesAllPipelinesWithSameUniqueNameOnClear) { - auto context = std::make_shared(); - ContentContext content_context(context, nullptr); - ContentContextOptions optionsA{.blend_mode = BlendMode::kSourceOver}; - ContentContextOptions optionsB{.blend_mode = BlendMode::kSource}; - - auto create_callback = [&]() { - return std::make_shared(context->GetPipelineLibrary(), - PipelineDescriptor{}); - }; - - auto pipelineA = content_context.GetCachedRuntimeEffectPipeline( - "A", optionsA, create_callback); - - auto pipelineA2 = content_context.GetCachedRuntimeEffectPipeline( - "A", optionsB, create_callback); - - auto pipelineB = content_context.GetCachedRuntimeEffectPipeline( - "B", optionsB, create_callback); - - ASSERT_TRUE(pipelineA); - ASSERT_TRUE(pipelineA2); - ASSERT_TRUE(pipelineB); - - ASSERT_EQ(pipelineA, content_context.GetCachedRuntimeEffectPipeline( - "A", optionsA, create_callback)); - ASSERT_EQ(pipelineA2, content_context.GetCachedRuntimeEffectPipeline( - "A", optionsB, create_callback)); - ASSERT_EQ(pipelineB, content_context.GetCachedRuntimeEffectPipeline( - "B", optionsB, create_callback)); - - content_context.ClearCachedRuntimeEffectPipeline("A"); - - ASSERT_NE(pipelineA, content_context.GetCachedRuntimeEffectPipeline( - "A", optionsA, create_callback)); - ASSERT_NE(pipelineA2, content_context.GetCachedRuntimeEffectPipeline( - "A", optionsB, create_callback)); - ASSERT_EQ(pipelineB, content_context.GetCachedRuntimeEffectPipeline( - "B", optionsB, create_callback)); - - content_context.ClearCachedRuntimeEffectPipeline("B"); - - ASSERT_NE(pipelineB, content_context.GetCachedRuntimeEffectPipeline( - "B", optionsB, create_callback)); -} - -TEST(ContentContext, InitializeCommonlyUsedShadersIfNeeded) { - ScopedValidationFatal fatal_validations; - // Set a pixel format that is larger than 32bpp. - auto context = std::make_shared("Mali G70", - PixelFormat::kR16G16B16A16Float); - ContentContext content_context(context, nullptr); - - FakeAllocator& fake_allocator = - FakeAllocator::Cast(*context->GetResourceAllocator()); - -#if IMPELLER_ENABLE_3D - EXPECT_EQ(fake_allocator.textures.size(), 2u); -#else - EXPECT_EQ(fake_allocator.textures.size(), 1u); -#endif // IMPELLER_ENABLE_3D -} - -} // namespace testing -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc index 7f3ac8e322..cced93ac3b 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc @@ -684,6 +684,8 @@ std::optional BlendFilterContents::CreateFramebufferAdvancedBlend( return std::nullopt; } + std::shared_ptr foreground_texture; + ContentContext::SubpassCallback subpass_callback = [&](const ContentContext& renderer, RenderPass& pass) { @@ -736,20 +738,7 @@ std::optional BlendFilterContents::CreateFramebufferAdvancedBlend( // texture for the foreground color. std::shared_ptr src_texture; if (foreground_color.has_value()) { - TextureDescriptor desc; - desc.size = {1, 1}; - desc.format = PixelFormat::kR8G8B8A8UNormInt; - desc.storage_mode = StorageMode::kHostVisible; - src_texture = - renderer.GetContext()->GetResourceAllocator()->CreateTexture(desc); - if (!src_texture) { - return false; - } - - if (!src_texture->SetContents( - foreground_color->Premultiply().ToR8G8B8A8().data(), 4u)) { - return false; - } + src_texture = foreground_texture; } else { auto src_snapshot = inputs[0]->GetSnapshot("ForegroundAdvancedBlend", renderer, entity); @@ -864,6 +853,29 @@ std::optional BlendFilterContents::CreateFramebufferAdvancedBlend( std::shared_ptr cmd_buffer = renderer.GetContext()->CreateCommandBuffer(); + + // Generate a 1x1 texture to implement foreground color blending. + if (foreground_color.has_value()) { + TextureDescriptor desc; + desc.size = {1, 1}; + desc.format = PixelFormat::kR8G8B8A8UNormInt; + desc.storage_mode = StorageMode::kDevicePrivate; + foreground_texture = + renderer.GetContext()->GetResourceAllocator()->CreateTexture(desc); + if (!foreground_texture) { + return std::nullopt; + } + auto blit_pass = cmd_buffer->CreateBlitPass(); + auto buffer_view = renderer.GetTransientsBuffer().Emplace( + foreground_color->Premultiply().ToR8G8B8A8()); + + blit_pass->AddCopy(std::move(buffer_view), foreground_texture); + if (!blit_pass->EncodeCommands( + renderer.GetContext()->GetResourceAllocator())) { + return std::nullopt; + } + } + auto render_target = renderer.MakeSubpass("FramebufferBlend", dst_snapshot->texture->GetSize(), cmd_buffer, subpass_callback); diff --git a/engine/src/flutter/impeller/entity/contents/gradient_generator.cc b/engine/src/flutter/impeller/entity/contents/gradient_generator.cc index 6672fb3941..2d6431ee8b 100644 --- a/engine/src/flutter/impeller/entity/contents/gradient_generator.cc +++ b/engine/src/flutter/impeller/entity/contents/gradient_generator.cc @@ -2,15 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include - #include "impeller/entity/contents/gradient_generator.h" #include "flutter/fml/logging.h" +#include "impeller/base/strings.h" +#include "impeller/core/device_buffer.h" +#include "impeller/core/formats.h" #include "impeller/core/texture.h" -#include "impeller/entity/contents/content_context.h" #include "impeller/renderer/context.h" -#include "impeller/renderer/render_pass.h" namespace impeller { @@ -33,11 +32,20 @@ std::shared_ptr CreateGradientTexture( return nullptr; } - auto mapping = std::make_shared(gradient_data.color_bytes); - if (!texture->SetContents(mapping)) { - FML_DLOG(ERROR) << "Could not copy contents into Impeller texture."; + auto data_mapping = + std::make_shared(gradient_data.color_bytes); + auto buffer = + context->GetResourceAllocator()->CreateBufferWithCopy(*data_mapping); + + auto cmd_buffer = context->CreateCommandBuffer(); + auto blit_pass = cmd_buffer->CreateBlitPass(); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(buffer)), texture); + + if (!blit_pass->EncodeCommands(context->GetResourceAllocator()) || + !context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) { return nullptr; } + texture->SetLabel(impeller::SPrintF("Gradient(%p)", texture.get()).c_str()); return texture; } diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 14b7e2d493..3d739d0373 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -12,6 +12,7 @@ #include "flutter/display_list/testing/dl_test_snippets.h" #include "fml/logging.h" #include "gtest/gtest.h" +#include "impeller/core/device_buffer.h" #include "impeller/core/formats.h" #include "impeller/core/texture_descriptor.h" #include "impeller/entity/contents/atlas_contents.h" @@ -2070,6 +2071,9 @@ static std::vector> CreateTestYUVTextures( uv_data.push_back(j % 2 == 0 ? u : v); } } + auto cmd_buffer = context->CreateCommandBuffer(); + auto blit_pass = cmd_buffer->CreateBlitPass(); + impeller::TextureDescriptor y_texture_descriptor; y_texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible; y_texture_descriptor.format = PixelFormat::kR8UNormInt; @@ -2077,9 +2081,10 @@ static std::vector> CreateTestYUVTextures( auto y_texture = context->GetResourceAllocator()->CreateTexture(y_texture_descriptor); auto y_mapping = std::make_shared(y_data); - if (!y_texture->SetContents(y_mapping)) { - FML_DLOG(ERROR) << "Could not copy contents into Y texture."; - } + auto y_mapping_buffer = + context->GetResourceAllocator()->CreateBufferWithCopy(*y_mapping); + + blit_pass->AddCopy(DeviceBuffer::AsBufferView(y_mapping_buffer), y_texture); impeller::TextureDescriptor uv_texture_descriptor; uv_texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible; @@ -2088,8 +2093,14 @@ static std::vector> CreateTestYUVTextures( auto uv_texture = context->GetResourceAllocator()->CreateTexture(uv_texture_descriptor); auto uv_mapping = std::make_shared(uv_data); - if (!uv_texture->SetContents(uv_mapping)) { - FML_DLOG(ERROR) << "Could not copy contents into UV texture."; + auto uv_mapping_buffer = + context->GetResourceAllocator()->CreateBufferWithCopy(*uv_mapping); + + blit_pass->AddCopy(DeviceBuffer::AsBufferView(uv_mapping_buffer), uv_texture); + + if (!blit_pass->EncodeCommands(context->GetResourceAllocator()) || + !context->GetCommandQueue()->Submit({cmd_buffer}).ok()) { + FML_DLOG(ERROR) << "Could not copy contents into Y/UV texture."; } return {y_texture, uv_texture}; diff --git a/engine/src/flutter/impeller/playground/playground.cc b/engine/src/flutter/impeller/playground/playground.cc index bf899cf274..9a173c56ec 100644 --- a/engine/src/flutter/impeller/playground/playground.cc +++ b/engine/src/flutter/impeller/playground/playground.cc @@ -414,27 +414,26 @@ static std::shared_ptr CreateTextureForDecompressedImage( return nullptr; } - auto uploaded = texture->SetContents(decompressed_image.GetAllocation()); - if (!uploaded) { - VALIDATION_LOG << "Could not upload texture to device memory for fixture."; + auto command_buffer = context->CreateCommandBuffer(); + if (!command_buffer) { + FML_DLOG(ERROR) << "Could not create command buffer for mipmap generation."; return nullptr; } + command_buffer->SetLabel("Mipmap Command Buffer"); + + auto blit_pass = command_buffer->CreateBlitPass(); + auto buffer_view = DeviceBuffer::AsBufferView( + context->GetResourceAllocator()->CreateBufferWithCopy( + *decompressed_image.GetAllocation())); + blit_pass->AddCopy(buffer_view, texture); if (enable_mipmapping) { - auto command_buffer = context->CreateCommandBuffer(); - if (!command_buffer) { - FML_DLOG(ERROR) - << "Could not create command buffer for mipmap generation."; - return nullptr; - } - command_buffer->SetLabel("Mipmap Command Buffer"); - auto blit_pass = command_buffer->CreateBlitPass(); blit_pass->SetLabel("Mipmap Blit Pass"); blit_pass->GenerateMipmap(texture); - blit_pass->EncodeCommands(context->GetResourceAllocator()); - if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) { - FML_DLOG(ERROR) << "Failed to submit blit pass command buffer."; - return nullptr; - } + } + blit_pass->EncodeCommands(context->GetResourceAllocator()); + if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) { + FML_DLOG(ERROR) << "Failed to submit blit pass command buffer."; + return nullptr; } return texture; } @@ -492,14 +491,24 @@ std::shared_ptr Playground::CreateTextureCubeForFixture( } texture->SetLabel("Texture cube"); + auto cmd_buffer = renderer_->GetContext()->CreateCommandBuffer(); + auto blit_pass = cmd_buffer->CreateBlitPass(); for (size_t i = 0; i < fixture_names.size(); i++) { - auto uploaded = - texture->SetContents(images[i].GetAllocation()->GetMapping(), - images[i].GetAllocation()->GetSize(), i); - if (!uploaded) { - VALIDATION_LOG << "Could not upload texture to device memory."; - return nullptr; - } + auto device_buffer = + renderer_->GetContext()->GetResourceAllocator()->CreateBufferWithCopy( + *images[i].GetAllocation()); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), texture, {}, + "", /*slice=*/i); + } + + if (!blit_pass->EncodeCommands( + renderer_->GetContext()->GetResourceAllocator()) || + !renderer_->GetContext() + ->GetCommandQueue() + ->Submit({std::move(cmd_buffer)}) + .ok()) { + VALIDATION_LOG << "Could not upload texture to device memory."; + return nullptr; } return texture; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc index f30ce5446f..93753abbdb 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc @@ -5,7 +5,9 @@ #include "impeller/renderer/backend/gles/blit_command_gles.h" #include "flutter/fml/closure.h" +#include "fml/trace_event.h" #include "impeller/base/validation.h" +#include "impeller/geometry/point.h" #include "impeller/renderer/backend/gles/device_buffer_gles.h" #include "impeller/renderer/backend/gles/texture_gles.h" @@ -117,6 +119,168 @@ bool BlitCopyTextureToTextureCommandGLES::Encode( return true; }; +namespace { +struct TexImage2DData { + GLint internal_format = 0; + GLenum external_format = GL_NONE; + GLenum type = GL_NONE; + BufferView buffer_view; + + explicit TexImage2DData(PixelFormat pixel_format) { + switch (pixel_format) { + case PixelFormat::kA8UNormInt: + internal_format = GL_ALPHA; + external_format = GL_ALPHA; + type = GL_UNSIGNED_BYTE; + break; + case PixelFormat::kR8UNormInt: + internal_format = GL_RED; + external_format = GL_RED; + type = GL_UNSIGNED_BYTE; + break; + case PixelFormat::kR8G8B8A8UNormInt: + case PixelFormat::kB8G8R8A8UNormInt: + case PixelFormat::kR8G8B8A8UNormIntSRGB: + case PixelFormat::kB8G8R8A8UNormIntSRGB: + internal_format = GL_RGBA; + external_format = GL_RGBA; + type = GL_UNSIGNED_BYTE; + break; + case PixelFormat::kR32G32B32A32Float: + internal_format = GL_RGBA; + external_format = GL_RGBA; + type = GL_FLOAT; + break; + case PixelFormat::kR16G16B16A16Float: + internal_format = GL_RGBA; + external_format = GL_RGBA; + type = GL_HALF_FLOAT; + break; + case PixelFormat::kS8UInt: + // Pure stencil textures are only available in OpenGL 4.4+, which is + // ~0% of mobile devices. Instead, we use a depth-stencil texture and + // only use the stencil component. + // + // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml + case PixelFormat::kD24UnormS8Uint: + internal_format = GL_DEPTH_STENCIL; + external_format = GL_DEPTH_STENCIL; + type = GL_UNSIGNED_INT_24_8; + break; + case PixelFormat::kUnknown: + case PixelFormat::kD32FloatS8UInt: + case PixelFormat::kR8G8UNormInt: + case PixelFormat::kB10G10R10XRSRGB: + case PixelFormat::kB10G10R10XR: + case PixelFormat::kB10G10R10A10XR: + return; + } + is_valid_ = true; + } + + TexImage2DData(PixelFormat pixel_format, BufferView p_buffer_view) + : TexImage2DData(pixel_format) { + buffer_view = std::move(p_buffer_view); + } + + bool IsValid() const { return is_valid_; } + + private: + bool is_valid_ = false; +}; +} // namespace + +BlitCopyBufferToTextureCommandGLES::~BlitCopyBufferToTextureCommandGLES() = + default; + +std::string BlitCopyBufferToTextureCommandGLES::GetLabel() const { + return label; +} + +bool BlitCopyBufferToTextureCommandGLES::Encode( + const ReactorGLES& reactor) const { + TextureGLES& texture_gles = TextureGLES::Cast(*destination); + + if (texture_gles.GetType() != TextureGLES::Type::kTexture) { + VALIDATION_LOG << "Incorrect texture usage flags for setting contents on " + "this texture object."; + return false; + } + + if (texture_gles.IsWrapped()) { + VALIDATION_LOG << "Cannot set the contents of a wrapped texture."; + return false; + } + + const auto& tex_descriptor = texture_gles.GetTextureDescriptor(); + + if (tex_descriptor.size.IsEmpty()) { + return true; + } + + if (!tex_descriptor.IsValid() || + source.range.length < tex_descriptor.GetByteSizeOfBaseMipLevel()) { + return false; + } + + destination->SetCoordinateSystem(TextureCoordinateSystem::kUploadFromHost); + + GLenum texture_type; + GLenum texture_target; + switch (tex_descriptor.type) { + case TextureType::kTexture2D: + texture_type = GL_TEXTURE_2D; + texture_target = GL_TEXTURE_2D; + break; + case TextureType::kTexture2DMultisample: + VALIDATION_LOG << "Multisample texture uploading is not supported for " + "the OpenGLES backend."; + return false; + case TextureType::kTextureCube: + texture_type = GL_TEXTURE_CUBE_MAP; + texture_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice; + break; + case TextureType::kTextureExternalOES: + texture_type = GL_TEXTURE_EXTERNAL_OES; + texture_target = GL_TEXTURE_EXTERNAL_OES; + break; + } + + TexImage2DData data = TexImage2DData(tex_descriptor.format, source); + if (!data.IsValid()) { + VALIDATION_LOG << "Invalid texture format."; + return false; + } + + auto gl_handle = texture_gles.GetGLHandle(); + if (!gl_handle.has_value()) { + VALIDATION_LOG + << "Texture was collected before it could be uploaded to the GPU."; + return false; + } + const auto& gl = reactor.GetProcTable(); + gl.BindTexture(texture_type, gl_handle.value()); + const GLvoid* tex_data = + data.buffer_view.buffer->OnGetContents() + data.buffer_view.range.offset; + + { + TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes", + std::to_string(data.buffer_view.range.length).c_str()); + gl.TexImage2D(texture_target, // target + 0u, // LOD level + data.internal_format, // internal format + tex_descriptor.size.width, // width + tex_descriptor.size.height, // height + 0u, // border + data.external_format, // external format + data.type, // type + tex_data // data + ); + } + texture_gles.MarkContentsInitialized(); + return true; +} + BlitCopyTextureToBufferCommandGLES::~BlitCopyTextureToBufferCommandGLES() = default; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.h index 5fa9c802f3..f4cd901a3e 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.h @@ -20,6 +20,16 @@ struct BlitEncodeGLES : BackendCast { [[nodiscard]] virtual bool Encode(const ReactorGLES& reactor) const = 0; }; +struct BlitCopyBufferToTextureCommandGLES + : public BlitEncodeGLES, + public BlitCopyBufferToTextureCommand { + ~BlitCopyBufferToTextureCommandGLES() override; + + std::string GetLabel() const override; + + [[nodiscard]] bool Encode(const ReactorGLES& reactor) const override; +}; + struct BlitCopyTextureToTextureCommandGLES : public BlitEncodeGLES, public BlitCopyTextureToTextureCommand { diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc index 92364c4c7c..100259a490 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc @@ -104,7 +104,7 @@ bool BlitPassGLES::OnCopyTextureToTextureCommand( command->source_region = source_region; command->destination_origin = destination_origin; - commands_.emplace_back(std::move(command)); + commands_.push_back(std::move(command)); return true; } @@ -122,7 +122,26 @@ bool BlitPassGLES::OnCopyTextureToBufferCommand( command->source_region = source_region; command->destination_offset = destination_offset; - commands_.emplace_back(std::move(command)); + commands_.push_back(std::move(command)); + return true; +} + +// |BlitPass| +bool BlitPassGLES::OnCopyBufferToTextureCommand( + BufferView source, + std::shared_ptr destination, + IPoint destination_origin, + std::string label, + uint32_t slice) { + auto command = std::make_unique(); + command->label = label; + command->source = std::move(source); + command->destination = std::move(destination); + command->destination_origin = destination_origin; + command->label = label; + command->slice = slice; + + commands_.push_back(std::move(command)); return true; } @@ -133,7 +152,7 @@ bool BlitPassGLES::OnGenerateMipmapCommand(std::shared_ptr texture, command->label = label; command->texture = std::move(texture); - commands_.emplace_back(std::move(command)); + commands_.push_back(std::move(command)); return true; } diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h index 170e3346f4..58eaed6e8e 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h @@ -58,10 +58,9 @@ class BlitPassGLES final : public BlitPass, bool OnCopyBufferToTextureCommand(BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label) override { - IMPELLER_UNIMPLEMENTED; - return false; - } + std::string label, + uint32_t slice) override; + // |BlitPass| bool OnGenerateMipmapCommand(std::shared_ptr texture, std::string label) override; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc index a67486bd37..b55d2fe54d 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc @@ -153,10 +153,6 @@ bool CapabilitiesGLES::SupportsSSBO() const { return false; } -bool CapabilitiesGLES::SupportsBufferToTextureBlits() const { - return false; -} - bool CapabilitiesGLES::SupportsTextureToTextureBlits() const { return false; } diff --git a/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.h index afcc71b418..d551acafac 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.h @@ -86,9 +86,6 @@ class CapabilitiesGLES final // |Capabilities| bool SupportsSSBO() const override; - // |Capabilities| - bool SupportsBufferToTextureBlits() const override; - // |Capabilities| bool SupportsTextureToTextureBlits() const override; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/test/capabilities_unittests.cc b/engine/src/flutter/impeller/renderer/backend/gles/test/capabilities_unittests.cc index 2ad20916a7..9ae3611822 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/test/capabilities_unittests.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/test/capabilities_unittests.cc @@ -17,7 +17,6 @@ TEST(CapabilitiesGLES, CanInitializeWithDefaults) { EXPECT_FALSE(capabilities->SupportsOffscreenMSAA()); EXPECT_FALSE(capabilities->SupportsSSBO()); - EXPECT_FALSE(capabilities->SupportsBufferToTextureBlits()); EXPECT_FALSE(capabilities->SupportsTextureToTextureBlits()); EXPECT_FALSE(capabilities->SupportsFramebufferFetch()); EXPECT_FALSE(capabilities->SupportsCompute()); diff --git a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc index c656b0d7d0..af745e23fd 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc @@ -18,6 +18,7 @@ namespace impeller { +namespace { static bool IsDepthStencilFormat(PixelFormat format) { switch (format) { case PixelFormat::kS8UInt: @@ -55,76 +56,6 @@ static TextureGLES::Type GetTextureTypeFromDescriptor( : TextureGLES::Type::kTexture; } -HandleType ToHandleType(TextureGLES::Type type) { - switch (type) { - case TextureGLES::Type::kTexture: - case TextureGLES::Type::kTextureMultisampled: - return HandleType::kTexture; - case TextureGLES::Type::kRenderBuffer: - case TextureGLES::Type::kRenderBufferMultisampled: - return HandleType::kRenderBuffer; - } - FML_UNREACHABLE(); -} - -TextureGLES::TextureGLES(ReactorGLES::Ref reactor, TextureDescriptor desc) - : TextureGLES(std::move(reactor), desc, false, std::nullopt) {} - -TextureGLES::TextureGLES(ReactorGLES::Ref reactor, - TextureDescriptor desc, - enum IsWrapped wrapped) - : TextureGLES(std::move(reactor), desc, true, std::nullopt) {} - -std::shared_ptr TextureGLES::WrapFBO(ReactorGLES::Ref reactor, - TextureDescriptor desc, - GLuint fbo) { - return std::shared_ptr( - new TextureGLES(std::move(reactor), desc, true, fbo)); -} - -TextureGLES::TextureGLES(std::shared_ptr reactor, - TextureDescriptor desc, - bool is_wrapped, - std::optional fbo) - : Texture(desc), - reactor_(std::move(reactor)), - type_(GetTextureTypeFromDescriptor(GetTextureDescriptor())), - handle_(reactor_->CreateHandle(ToHandleType(type_))), - is_wrapped_(is_wrapped), - wrapped_fbo_(fbo) { - // Ensure the texture descriptor itself is valid. - if (!GetTextureDescriptor().IsValid()) { - VALIDATION_LOG << "Invalid texture descriptor."; - return; - } - // Ensure the texture doesn't exceed device capabilities. - const auto tex_size = GetTextureDescriptor().size; - const auto max_size = - reactor_->GetProcTable().GetCapabilities()->max_texture_size; - if (tex_size.Max(max_size) != max_size) { - VALIDATION_LOG << "Texture of size " << tex_size - << " would exceed max supported size of " << max_size << "."; - return; - } - - is_valid_ = true; -} - -// |Texture| -TextureGLES::~TextureGLES() { - reactor_->CollectHandle(handle_); -} - -// |Texture| -bool TextureGLES::IsValid() const { - return is_valid_; -} - -// |Texture| -void TextureGLES::SetLabel(std::string_view label) { - reactor_->SetDebugLabel(handle_, std::string{label.data(), label.size()}); -} - struct TexImage2DData { GLint internal_format = 0; GLenum external_format = GL_NONE; @@ -194,6 +125,77 @@ struct TexImage2DData { private: bool is_valid_ = false; }; +} // namespace + +HandleType ToHandleType(TextureGLES::Type type) { + switch (type) { + case TextureGLES::Type::kTexture: + case TextureGLES::Type::kTextureMultisampled: + return HandleType::kTexture; + case TextureGLES::Type::kRenderBuffer: + case TextureGLES::Type::kRenderBufferMultisampled: + return HandleType::kRenderBuffer; + } + FML_UNREACHABLE(); +} + +TextureGLES::TextureGLES(ReactorGLES::Ref reactor, TextureDescriptor desc) + : TextureGLES(std::move(reactor), desc, false, std::nullopt) {} + +TextureGLES::TextureGLES(ReactorGLES::Ref reactor, + TextureDescriptor desc, + enum IsWrapped wrapped) + : TextureGLES(std::move(reactor), desc, true, std::nullopt) {} + +std::shared_ptr TextureGLES::WrapFBO(ReactorGLES::Ref reactor, + TextureDescriptor desc, + GLuint fbo) { + return std::shared_ptr( + new TextureGLES(std::move(reactor), desc, true, fbo)); +} + +TextureGLES::TextureGLES(std::shared_ptr reactor, + TextureDescriptor desc, + bool is_wrapped, + std::optional fbo) + : Texture(desc), + reactor_(std::move(reactor)), + type_(GetTextureTypeFromDescriptor(GetTextureDescriptor())), + handle_(reactor_->CreateHandle(ToHandleType(type_))), + is_wrapped_(is_wrapped), + wrapped_fbo_(fbo) { + // Ensure the texture descriptor itself is valid. + if (!GetTextureDescriptor().IsValid()) { + VALIDATION_LOG << "Invalid texture descriptor."; + return; + } + // Ensure the texture doesn't exceed device capabilities. + const auto tex_size = GetTextureDescriptor().size; + const auto max_size = + reactor_->GetProcTable().GetCapabilities()->max_texture_size; + if (tex_size.Max(max_size) != max_size) { + VALIDATION_LOG << "Texture of size " << tex_size + << " would exceed max supported size of " << max_size << "."; + return; + } + + is_valid_ = true; +} + +// |Texture| +TextureGLES::~TextureGLES() { + reactor_->CollectHandle(handle_); +} + +// |Texture| +bool TextureGLES::IsValid() const { + return is_valid_; +} + +// |Texture| +void TextureGLES::SetLabel(std::string_view label) { + reactor_->SetDebugLabel(handle_, std::string{label.data(), label.size()}); +} // |Texture| bool TextureGLES::OnSetContents(const uint8_t* contents, @@ -234,11 +236,8 @@ bool TextureGLES::OnSetContents(std::shared_ptr mapping, return true; } - if (!tex_descriptor.IsValid()) { - return false; - } - - if (mapping->GetSize() < tex_descriptor.GetByteSizeOfBaseMipLevel()) { + if (!tex_descriptor.IsValid() || + mapping->GetSize() < tex_descriptor.GetByteSizeOfBaseMipLevel()) { return false; } @@ -456,6 +455,10 @@ bool TextureGLES::Bind() const { return true; } +void TextureGLES::MarkContentsInitialized() const { + contents_initialized_ = true; +} + bool TextureGLES::GenerateMipmap() { if (!IsValid()) { return false; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h index 5a8c3a79b3..a1827876d8 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h @@ -60,9 +60,9 @@ class TextureGLES final : public Texture, std::optional GetFBO() const { return wrapped_fbo_; } - private: - friend class AllocatorMTL; + void MarkContentsInitialized() const; + private: ReactorGLES::Ref reactor_; const Type type_; HandleGLES handle_; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm index 17c8b51ba7..f07c8d09bd 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm @@ -132,7 +132,7 @@ bool BlitCopyBufferToTextureCommandMTL::Encode( sourceBytesPerImage:destination_bytes_per_image sourceSize:source_size_mtl toTexture:destination_mtl - destinationSlice:0 + destinationSlice:slice destinationLevel:0 destinationOrigin:destination_origin_mtl]; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h index 33a27f5799..6ddfe3d55a 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h @@ -56,7 +56,8 @@ class BlitPassMTL final : public BlitPass { bool OnCopyBufferToTextureCommand(BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label) override; + std::string label, + uint32_t slice) override; // |BlitPass| bool OnGenerateMipmapCommand(std::shared_ptr texture, diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm index b884d01c0c..ce58225feb 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm @@ -5,6 +5,7 @@ #include "impeller/renderer/backend/metal/blit_pass_mtl.h" #include #include +#include #include #include "flutter/fml/closure.h" @@ -127,12 +128,14 @@ bool BlitPassMTL::OnCopyBufferToTextureCommand( BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label) { + std::string label, + uint32_t slice) { auto command = std::make_unique(); - command->label = label; + command->label = std::move(label); command->source = std::move(source); command->destination = std::move(destination); command->destination_origin = destination_origin; + command->slice = slice; commands_.emplace_back(std::move(command)); return true; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm index 6248368687..22882ea0bd 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm @@ -56,7 +56,6 @@ static std::unique_ptr InferMetalCapabilities( return CapabilitiesBuilder() .SetSupportsOffscreenMSAA(true) .SetSupportsSSBO(true) - .SetSupportsBufferToTextureBlits(true) .SetSupportsTextureToTextureBlits(true) .SetSupportsDecalSamplerAddressMode(true) .SetSupportsFramebufferFetch(DeviceSupportsFramebufferFetch(device)) diff --git a/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.h index f2a55b5bd6..6980669003 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.h @@ -36,11 +36,6 @@ class DeviceBufferMTL final // |DeviceBuffer| uint8_t* OnGetContents() const override; - // |DeviceBuffer| - std::shared_ptr AsTexture(Allocator& allocator, - const TextureDescriptor& descriptor, - uint16_t row_bytes) const override; - // |DeviceBuffer| bool OnCopyHostBuffer(const uint8_t* source, Range source_range, diff --git a/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.mm index c2ea8d89ef..ff763f2643 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/device_buffer_mtl.mm @@ -29,30 +29,6 @@ uint8_t* DeviceBufferMTL::OnGetContents() const { return reinterpret_cast(buffer_.contents); } -std::shared_ptr DeviceBufferMTL::AsTexture( - Allocator& allocator, - const TextureDescriptor& descriptor, - uint16_t row_bytes) const { - auto mtl_texture_desc = ToMTLTextureDescriptor(descriptor); - - if (!mtl_texture_desc) { - VALIDATION_LOG << "Texture descriptor was invalid."; - return nullptr; - } - - if (@available(iOS 13.0, macos 10.15, *)) { - mtl_texture_desc.resourceOptions = buffer_.resourceOptions; - } - - auto texture = [buffer_ newTextureWithDescriptor:mtl_texture_desc - offset:0 - bytesPerRow:row_bytes]; - if (!texture) { - return nullptr; - } - return TextureMTL::Create(descriptor, texture); -} - [[nodiscard]] bool DeviceBufferMTL::OnCopyHostBuffer(const uint8_t* source, Range source_range, size_t offset) { diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc index b5c3d6a147..922c2d2848 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc @@ -262,6 +262,22 @@ bool BlitCopyBufferToTextureCommandVK::Encode(CommandEncoderVK& encoder) const { image_copy // ); + // Transition to shader-read. + { + BarrierVK barrier; + barrier.cmd_buffer = cmd_buffer; + barrier.src_access = vk::AccessFlagBits::eTransferWrite; + barrier.src_stage = vk::PipelineStageFlagBits::eTransfer; + barrier.dst_access = vk::AccessFlagBits::eShaderRead; + barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader; + + barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal; + + if (!dst.SetLayout(barrier)) { + return false; + } + } + return true; } diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc index 32f00964c6..1dfb570fd7 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc @@ -97,13 +97,15 @@ bool BlitPassVK::OnCopyBufferToTextureCommand( BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label) { + std::string label, + uint32_t slice) { auto command = std::make_unique(); command->source = std::move(source); command->destination = std::move(destination); command->destination_origin = destination_origin; command->label = std::move(label); + command->slice = slice; commands_.push_back(std::move(command)); return true; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h index 2aaeda68da..f693023ff6 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h @@ -56,7 +56,8 @@ class BlitPassVK final : public BlitPass { bool OnCopyBufferToTextureCommand(BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label) override; + std::string label, + uint32_t slice) override; // |BlitPass| bool OnGenerateMipmapCommand(std::shared_ptr texture, std::string label) override; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.cc index a052954a65..7b465b7040 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.cc @@ -536,11 +536,6 @@ bool CapabilitiesVK::SupportsSSBO() const { return true; } -// |Capabilities| -bool CapabilitiesVK::SupportsBufferToTextureBlits() const { - return true; -} - // |Capabilities| bool CapabilitiesVK::SupportsTextureToTextureBlits() const { return true; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.h index d1e327b687..632a96a4be 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.h @@ -186,9 +186,6 @@ class CapabilitiesVK final : public Capabilities, // |Capabilities| bool SupportsSSBO() const override; - // |Capabilities| - bool SupportsBufferToTextureBlits() const override; - // |Capabilities| bool SupportsTextureToTextureBlits() const override; diff --git a/engine/src/flutter/impeller/renderer/blit_command.h b/engine/src/flutter/impeller/renderer/blit_command.h index 7f456f6593..07c951e470 100644 --- a/engine/src/flutter/impeller/renderer/blit_command.h +++ b/engine/src/flutter/impeller/renderer/blit_command.h @@ -33,6 +33,7 @@ struct BlitCopyBufferToTextureCommand : public BlitCommand { BufferView source; std::shared_ptr destination; IPoint destination_origin; + uint32_t slice = 0; }; struct BlitGenerateMipmapCommand : public BlitCommand { diff --git a/engine/src/flutter/impeller/renderer/blit_pass.cc b/engine/src/flutter/impeller/renderer/blit_pass.cc index 81c4e500b0..56e919b7ac 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass.cc +++ b/engine/src/flutter/impeller/renderer/blit_pass.cc @@ -123,7 +123,8 @@ bool BlitPass::AddCopy(std::shared_ptr source, bool BlitPass::AddCopy(BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label) { + std::string label, + uint32_t slice) { if (!destination) { VALIDATION_LOG << "Attempted to add a texture blit with no destination."; return false; @@ -141,7 +142,8 @@ bool BlitPass::AddCopy(BufferView source, } return OnCopyBufferToTextureCommand(std::move(source), std::move(destination), - destination_origin, std::move(label)); + destination_origin, std::move(label), + slice); } bool BlitPass::GenerateMipmap(std::shared_ptr texture, diff --git a/engine/src/flutter/impeller/renderer/blit_pass.h b/engine/src/flutter/impeller/renderer/blit_pass.h index 64ab734c10..3ea790c5a4 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass.h +++ b/engine/src/flutter/impeller/renderer/blit_pass.h @@ -93,13 +93,16 @@ class BlitPass { /// destination buffer. /// @param[in] label The optional debug label to give the /// command. + /// @param[in] slice For cubemap textures, the slice to write + /// data to. /// /// @return If the command was valid for subsequent commitment. /// bool AddCopy(BufferView source, std::shared_ptr destination, IPoint destination_origin = {}, - std::string label = ""); + std::string label = "", + uint32_t slice = 0); //---------------------------------------------------------------------------- /// @brief Record a command to generate all mip levels for a texture. @@ -146,7 +149,8 @@ class BlitPass { BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label) = 0; + std::string label, + uint32_t slice) = 0; virtual bool OnGenerateMipmapCommand(std::shared_ptr texture, std::string label) = 0; diff --git a/engine/src/flutter/impeller/renderer/capabilities.cc b/engine/src/flutter/impeller/renderer/capabilities.cc index 1ef1fe38a8..395fecf1b8 100644 --- a/engine/src/flutter/impeller/renderer/capabilities.cc +++ b/engine/src/flutter/impeller/renderer/capabilities.cc @@ -27,11 +27,6 @@ class StandardCapabilities final : public Capabilities { // |Capabilities| bool SupportsSSBO() const override { return supports_ssbo_; } - // |Capabilities| - bool SupportsBufferToTextureBlits() const override { - return supports_buffer_to_texture_blits_; - } - // |Capabilities| bool SupportsTextureToTextureBlits() const override { return supports_texture_to_texture_blits_; @@ -88,7 +83,6 @@ class StandardCapabilities final : public Capabilities { private: StandardCapabilities(bool supports_offscreen_msaa, bool supports_ssbo, - bool supports_buffer_to_texture_blits, bool supports_texture_to_texture_blits, bool supports_framebuffer_fetch, bool supports_compute, @@ -102,7 +96,6 @@ class StandardCapabilities final : public Capabilities { PixelFormat default_glyph_atlas_format) : supports_offscreen_msaa_(supports_offscreen_msaa), supports_ssbo_(supports_ssbo), - supports_buffer_to_texture_blits_(supports_buffer_to_texture_blits), supports_texture_to_texture_blits_(supports_texture_to_texture_blits), supports_framebuffer_fetch_(supports_framebuffer_fetch), supports_compute_(supports_compute), @@ -120,7 +113,6 @@ class StandardCapabilities final : public Capabilities { bool supports_offscreen_msaa_ = false; bool supports_ssbo_ = false; - bool supports_buffer_to_texture_blits_ = false; bool supports_texture_to_texture_blits_ = false; bool supports_framebuffer_fetch_ = false; bool supports_compute_ = false; @@ -152,12 +144,6 @@ CapabilitiesBuilder& CapabilitiesBuilder::SetSupportsSSBO(bool value) { return *this; } -CapabilitiesBuilder& CapabilitiesBuilder::SetSupportsBufferToTextureBlits( - bool value) { - supports_buffer_to_texture_blits_ = value; - return *this; -} - CapabilitiesBuilder& CapabilitiesBuilder::SetSupportsTextureToTextureBlits( bool value) { supports_texture_to_texture_blits_ = value; @@ -227,7 +213,6 @@ std::unique_ptr CapabilitiesBuilder::Build() { return std::unique_ptr(new StandardCapabilities( // supports_offscreen_msaa_, // supports_ssbo_, // - supports_buffer_to_texture_blits_, // supports_texture_to_texture_blits_, // supports_framebuffer_fetch_, // supports_compute_, // diff --git a/engine/src/flutter/impeller/renderer/capabilities.h b/engine/src/flutter/impeller/renderer/capabilities.h index cfdf988565..b86a90fa86 100644 --- a/engine/src/flutter/impeller/renderer/capabilities.h +++ b/engine/src/flutter/impeller/renderer/capabilities.h @@ -28,11 +28,6 @@ class Capabilities { /// Objects (SSBOs) to pipelines. virtual bool SupportsSSBO() const = 0; - /// @brief Whether the context backend supports blitting from a given - /// `DeviceBuffer` view to a texture region (via the relevant - /// `BlitPass::AddCopy` overloads). - virtual bool SupportsBufferToTextureBlits() const = 0; - /// @brief Whether the context backend supports blitting from one texture /// region to another texture region (via the relevant /// `BlitPass::AddCopy` overloads). @@ -128,8 +123,6 @@ class CapabilitiesBuilder { CapabilitiesBuilder& SetSupportsSSBO(bool value); - CapabilitiesBuilder& SetSupportsBufferToTextureBlits(bool value); - CapabilitiesBuilder& SetSupportsTextureToTextureBlits(bool value); CapabilitiesBuilder& SetSupportsFramebufferFetch(bool value); @@ -157,7 +150,6 @@ class CapabilitiesBuilder { private: bool supports_offscreen_msaa_ = false; bool supports_ssbo_ = false; - bool supports_buffer_to_texture_blits_ = false; bool supports_texture_to_texture_blits_ = false; bool supports_framebuffer_fetch_ = false; bool supports_compute_ = false; diff --git a/engine/src/flutter/impeller/renderer/capabilities_unittests.cc b/engine/src/flutter/impeller/renderer/capabilities_unittests.cc index 2692d067ad..e92d373410 100644 --- a/engine/src/flutter/impeller/renderer/capabilities_unittests.cc +++ b/engine/src/flutter/impeller/renderer/capabilities_unittests.cc @@ -20,7 +20,6 @@ namespace testing { CAPABILITY_TEST(SupportsOffscreenMSAA, false); CAPABILITY_TEST(SupportsSSBO, false); -CAPABILITY_TEST(SupportsBufferToTextureBlits, false); CAPABILITY_TEST(SupportsTextureToTextureBlits, false); CAPABILITY_TEST(SupportsFramebufferFetch, false); CAPABILITY_TEST(SupportsCompute, false); diff --git a/engine/src/flutter/impeller/renderer/renderer_unittests.cc b/engine/src/flutter/impeller/renderer/renderer_unittests.cc index 545e4cd6c4..a6fe1c23a2 100644 --- a/engine/src/flutter/impeller/renderer/renderer_unittests.cc +++ b/engine/src/flutter/impeller/renderer/renderer_unittests.cc @@ -973,43 +973,6 @@ TEST_P(RendererTest, InactiveUniforms) { OpenPlaygroundHere(callback); } -TEST_P(RendererTest, CanCreateCPUBackedTexture) { - if (GetParam() == PlaygroundBackend::kOpenGLES) { - GTEST_SKIP_("CPU backed textures are not supported on OpenGLES."); - } - - auto context = GetContext(); - auto allocator = context->GetResourceAllocator(); - size_t dimension = 2; - - do { - ISize size(dimension, dimension); - TextureDescriptor texture_descriptor; - texture_descriptor.storage_mode = StorageMode::kHostVisible; - texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt; - texture_descriptor.size = size; - auto row_bytes = - std::max(static_cast(size.width * 4), - allocator->MinimumBytesPerRow(texture_descriptor.format)); - auto buffer_size = size.height * row_bytes; - - DeviceBufferDescriptor buffer_descriptor; - buffer_descriptor.storage_mode = StorageMode::kHostVisible; - buffer_descriptor.size = buffer_size; - - auto buffer = allocator->CreateBuffer(buffer_descriptor); - - ASSERT_TRUE(buffer); - - auto texture = buffer->AsTexture(*allocator, texture_descriptor, row_bytes); - - ASSERT_TRUE(texture); - ASSERT_TRUE(texture->IsValid()); - - dimension *= 2; - } while (dimension <= 8192); -} - TEST_P(RendererTest, DefaultIndexSize) { using VS = BoxFadeVertexShader; diff --git a/engine/src/flutter/impeller/renderer/testing/mocks.h b/engine/src/flutter/impeller/renderer/testing/mocks.h index 45b519cc3c..848a811393 100644 --- a/engine/src/flutter/impeller/renderer/testing/mocks.h +++ b/engine/src/flutter/impeller/renderer/testing/mocks.h @@ -83,7 +83,8 @@ class MockBlitPass : public BlitPass { (BufferView source, std::shared_ptr destination, IPoint destination_origin, - std::string label), + std::string label, + uint32_t slice), (override)); MOCK_METHOD(bool, OnGenerateMipmapCommand, @@ -193,7 +194,6 @@ class MockCapabilities : public Capabilities { MOCK_METHOD(bool, SupportsOffscreenMSAA, (), (const, override)); MOCK_METHOD(bool, SupportsImplicitResolvingMSAA, (), (const, override)); MOCK_METHOD(bool, SupportsSSBO, (), (const, override)); - MOCK_METHOD(bool, SupportsBufferToTextureBlits, (), (const, override)); MOCK_METHOD(bool, SupportsTextureToTextureBlits, (), (const, override)); MOCK_METHOD(bool, SupportsFramebufferFetch, (), (const, override)); MOCK_METHOD(bool, SupportsCompute, (), (const, override)); diff --git a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc index 11a612660a..36c6af3076 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc @@ -9,10 +9,14 @@ #include "flutter/fml/logging.h" #include "flutter/fml/trace_event.h" +#include "fml/closure.h" +#include "fml/mapping.h" #include "impeller/base/allocation.h" #include "impeller/core/allocator.h" +#include "impeller/renderer/command_buffer.h" #include "impeller/typographer/backends/skia/glyph_atlas_context_skia.h" #include "impeller/typographer/backends/skia/typeface_skia.h" +#include "impeller/typographer/glyph_atlas.h" #include "impeller/typographer/rectangle_packer.h" #include "impeller/typographer/typographer_context.h" #include "include/core/SkColor.h" @@ -257,8 +261,11 @@ static std::shared_ptr CreateAtlasBitmap(const GlyphAtlas& atlas, return bitmap; } -static bool UpdateGlyphTextureAtlas(std::shared_ptr bitmap, - const std::shared_ptr& texture) { +static bool UpdateGlyphTextureAtlas( + std::shared_ptr bitmap, + const std::shared_ptr& allocator, + const std::shared_ptr& texture, + const std::shared_ptr& blit_pass) { TRACE_EVENT0("impeller", __FUNCTION__); FML_DCHECK(bitmap != nullptr); @@ -270,12 +277,17 @@ static bool UpdateGlyphTextureAtlas(std::shared_ptr bitmap, [bitmap](auto, auto) mutable { bitmap.reset(); } // proc ); - return texture->SetContents(mapping); + std::shared_ptr device_buffer = + allocator->CreateBufferWithCopy(*mapping); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)), + texture); + return blit_pass->EncodeCommands(allocator); } static std::shared_ptr UploadGlyphTextureAtlas( const std::shared_ptr& allocator, std::shared_ptr bitmap, + const std::shared_ptr& blit_pass, const ISize& atlas_size, PixelFormat format) { TRACE_EVENT0("impeller", __FUNCTION__); @@ -287,7 +299,7 @@ static std::shared_ptr UploadGlyphTextureAtlas( const auto& pixmap = bitmap->pixmap(); TextureDescriptor texture_descriptor; - texture_descriptor.storage_mode = StorageMode::kHostVisible; + texture_descriptor.storage_mode = StorageMode::kDevicePrivate; texture_descriptor.format = format; texture_descriptor.size = atlas_size; @@ -296,21 +308,25 @@ static std::shared_ptr UploadGlyphTextureAtlas( return nullptr; } - auto texture = allocator->CreateTexture(texture_descriptor); + std::shared_ptr texture = + allocator->CreateTexture(texture_descriptor); if (!texture || !texture->IsValid()) { return nullptr; } texture->SetLabel("GlyphAtlas"); - auto mapping = std::make_shared( - reinterpret_cast(bitmap->getAddr(0, 0)), // data - texture_descriptor.GetByteSizeOfBaseMipLevel(), // size - [bitmap](auto, auto) mutable { bitmap.reset(); } // proc - ); + std::shared_ptr mapping = + std::make_shared( + reinterpret_cast(bitmap->getAddr(0, 0)), // data + texture_descriptor.GetByteSizeOfBaseMipLevel(), // size + [bitmap](auto, auto) mutable { bitmap.reset(); } // proc + ); + std::shared_ptr device_buffer = + allocator->CreateBufferWithCopy(*mapping); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)), + texture); + blit_pass->EncodeCommands(allocator); - if (!texture->SetContents(mapping)) { - return nullptr; - } return texture; } @@ -329,6 +345,12 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( if (font_glyph_map.empty()) { return last_atlas; } + std::shared_ptr cmd_buffer = context.CreateCommandBuffer(); + std::shared_ptr blit_pass = cmd_buffer->CreateBlitPass(); + + fml::ScopedCleanupClosure closure([&cmd_buffer, &context]() { + context.GetCommandQueue()->Submit({std::move(cmd_buffer)}); + }); // --------------------------------------------------------------------------- // Step 1: Determine if the atlas type and font glyph pairs are compatible @@ -387,7 +409,8 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( // --------------------------------------------------------------------------- // Step 5a: Update the existing texture with the updated bitmap. // --------------------------------------------------------------------------- - if (!UpdateGlyphTextureAtlas(bitmap, last_atlas->GetTexture())) { + if (!UpdateGlyphTextureAtlas(bitmap, context.GetResourceAllocator(), + last_atlas->GetTexture(), blit_pass)) { return nullptr; } return last_atlas; @@ -407,8 +430,8 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( font_glyph_pairs.push_back({scaled_font, glyph}); } } - auto glyph_atlas = std::make_shared(type); - auto atlas_size = OptimumAtlasSizeForFontGlyphPairs( + std::shared_ptr glyph_atlas = std::make_shared(type); + ISize atlas_size = OptimumAtlasSizeForFontGlyphPairs( font_glyph_pairs, // glyph_positions, // atlas_context, // @@ -450,6 +473,21 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( } atlas_context_skia.UpdateBitmap(bitmap); + // If the new atlas size is the same size as the previous texture, reuse the + // texture and treat this as an updated that replaces all glyphs. + if (last_atlas && last_atlas->GetTexture()) { + std::shared_ptr last_texture = last_atlas->GetTexture(); + if (atlas_size == last_texture->GetSize()) { + if (!UpdateGlyphTextureAtlas(bitmap, context.GetResourceAllocator(), + last_texture, blit_pass)) { + return nullptr; + } + + glyph_atlas->SetTexture(last_texture); + return glyph_atlas; + } + } + // --------------------------------------------------------------------------- // Step 7b: Upload the atlas as a texture. // --------------------------------------------------------------------------- @@ -462,8 +500,8 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( format = PixelFormat::kR8G8B8A8UNormInt; break; } - auto texture = UploadGlyphTextureAtlas(context.GetResourceAllocator(), bitmap, - atlas_size, format); + std::shared_ptr texture = UploadGlyphTextureAtlas( + context.GetResourceAllocator(), bitmap, blit_pass, atlas_size, format); if (!texture) { return nullptr; } diff --git a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc index ee73d1ea4a..d7deda3f3a 100644 --- a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc +++ b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc @@ -9,8 +9,11 @@ #include "flutter/fml/logging.h" #include "flutter/fml/trace_event.h" +#include "fml/closure.h" +#include "fml/mapping.h" #include "impeller/base/allocation.h" #include "impeller/core/allocator.h" +#include "impeller/core/device_buffer.h" #include "impeller/typographer/backends/stb/glyph_atlas_context_stb.h" #include "impeller/typographer/font_glyph_pair.h" #include "typeface_stb.h" @@ -309,6 +312,8 @@ static std::shared_ptr CreateAtlasBitmap(const GlyphAtlas& atlas, // static bool UpdateGlyphTextureAtlas(std::shared_ptr bitmap, static bool UpdateGlyphTextureAtlas(std::shared_ptr& bitmap, + const std::shared_ptr& allocator, + std::shared_ptr& blit_pass, const std::shared_ptr& texture) { TRACE_EVENT0("impeller", __FUNCTION__); @@ -323,11 +328,16 @@ static bool UpdateGlyphTextureAtlas(std::shared_ptr& bitmap, // specify a release proc. ); - return texture->SetContents(mapping); + std::shared_ptr device_buffer = + allocator->CreateBufferWithCopy(*mapping); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), texture); + + return blit_pass->EncodeCommands(allocator); } static std::shared_ptr UploadGlyphTextureAtlas( const std::shared_ptr& allocator, + std::shared_ptr& blit_pass, std::shared_ptr& bitmap, const ISize& atlas_size, PixelFormat format) { @@ -339,7 +349,7 @@ static std::shared_ptr UploadGlyphTextureAtlas( FML_DCHECK(bitmap != nullptr); TextureDescriptor texture_descriptor; - texture_descriptor.storage_mode = StorageMode::kHostVisible; + texture_descriptor.storage_mode = StorageMode::kDevicePrivate; texture_descriptor.format = format; texture_descriptor.size = atlas_size; @@ -354,16 +364,20 @@ static std::shared_ptr UploadGlyphTextureAtlas( } texture->SetLabel("GlyphAtlas"); - auto mapping = std::make_shared( - reinterpret_cast(bitmap->GetPixels()), // data - texture_descriptor.GetByteSizeOfBaseMipLevel() // size - // As the bitmap is static in this module I believe we don't need to - // specify a release proc. - ); - - if (!texture->SetContents(mapping)) { + std::shared_ptr mapping = + std::make_shared( + reinterpret_cast(bitmap->GetPixels()), // data + texture_descriptor.GetByteSizeOfBaseMipLevel() // size + // As the bitmap is static in this module I believe we don't need to + // specify a release proc. + ); + std::shared_ptr device_buffer = + allocator->CreateBufferWithCopy(*mapping); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), texture); + if (!blit_pass->EncodeCommands(allocator)) { return nullptr; } + return texture; } @@ -383,6 +397,13 @@ std::shared_ptr TypographerContextSTB::CreateGlyphAtlas( return last_atlas; } + std::shared_ptr cmd_buffer = context.CreateCommandBuffer(); + std::shared_ptr blit_pass = cmd_buffer->CreateBlitPass(); + + fml::ScopedCleanupClosure closure([&cmd_buffer, &context]() { + context.GetCommandQueue()->Submit({std::move(cmd_buffer)}); + }); + // --------------------------------------------------------------------------- // Step 1: Determine if the atlas type and font glyph pairs are compatible // with the current atlas and reuse if possible. @@ -441,7 +462,8 @@ std::shared_ptr TypographerContextSTB::CreateGlyphAtlas( // --------------------------------------------------------------------------- // Step 5a: Update the existing texture with the updated bitmap. // --------------------------------------------------------------------------- - if (!UpdateGlyphTextureAtlas(bitmap, last_atlas->GetTexture())) { + if (!UpdateGlyphTextureAtlas(bitmap, context.GetResourceAllocator(), + blit_pass, last_atlas->GetTexture())) { return nullptr; } return last_atlas; @@ -519,8 +541,8 @@ std::shared_ptr TypographerContextSTB::CreateGlyphAtlas( : PixelFormat::kR8G8B8A8UNormInt; break; } - auto texture = UploadGlyphTextureAtlas(context.GetResourceAllocator(), bitmap, - atlas_size, format); + auto texture = UploadGlyphTextureAtlas(context.GetResourceAllocator(), + blit_pass, bitmap, atlas_size, format); if (!texture) { return nullptr; } diff --git a/engine/src/flutter/impeller/typographer/typographer_unittests.cc b/engine/src/flutter/impeller/typographer/typographer_unittests.cc index 51e05bda31..4f7b120999 100644 --- a/engine/src/flutter/impeller/typographer/typographer_unittests.cc +++ b/engine/src/flutter/impeller/typographer/typographer_unittests.cc @@ -338,8 +338,7 @@ TEST_P(TypographerTest, RectanglePackerAddsNonoverlapingRectangles) { ASSERT_EQ(packer->PercentFull(), 0); } -TEST_P(TypographerTest, - GlyphAtlasTextureIsRecycledWhenContentsAreNotRecreated) { +TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledWhenContentsAreRecreated) { auto context = TypographerContextSkia::Make(); auto atlas_context = context->CreateGlyphAtlasContext(); ASSERT_TRUE(context && context->IsValid()); @@ -371,7 +370,7 @@ TEST_P(TypographerTest, auto new_packer = atlas_context->GetRectPacker(); - ASSERT_NE(second_texture, first_texture); + ASSERT_EQ(second_texture, first_texture); ASSERT_NE(old_packer, new_packer); } diff --git a/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc b/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc index 7532399699..cd081699f7 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc @@ -494,18 +494,10 @@ void ImageDecoderImpeller::Decode(fml::RefPtr descriptor, gpu_disabled_switch]() { sk_sp image; std::string decode_error; - if (context->GetCapabilities()->SupportsBufferToTextureBlits()) { - 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); - } else { - std::tie(image, decode_error) = UploadTextureToStorage( - context, bitmap_result.sk_bitmap, gpu_disabled_switch, - impeller::StorageMode::kDevicePrivate, - /*create_mips=*/true); - result(image, 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); }; if (context->GetBackendType() == impeller::Context::BackendType::kOpenGLES) { diff --git a/engine/src/flutter/lib/ui/painting/image_decoder_no_gl_unittests.h b/engine/src/flutter/lib/ui/painting/image_decoder_no_gl_unittests.h index 0376f6a2cd..b1839f0768 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder_no_gl_unittests.h +++ b/engine/src/flutter/lib/ui/painting/image_decoder_no_gl_unittests.h @@ -44,12 +44,6 @@ class TestImpellerDeviceBuffer : public DeviceBuffer { ~TestImpellerDeviceBuffer() { free(bytes_); } private: - std::shared_ptr AsTexture(Allocator& allocator, - const TextureDescriptor& descriptor, - uint16_t row_bytes) const override { - return nullptr; - } - bool SetLabel(const std::string& label) override { return true; } bool SetLabel(const std::string& label, Range range) override { return true; }