From 6b613131d1ce1c6f4a7ec28ce521e048e8e8379d Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 29 Jan 2024 23:48:35 -0800 Subject: [PATCH] [Impeller] Add interface for submitting multiple command buffers at once. (flutter/engine#50139) The Impeller Vulkan backend benefits from batching submission to the vk graphics queue. Managing this automatically is non-trivial and adds surprising/fragile thread based behavior, see: https://github.com/flutter/engine/pull/49870 Instead, introduce an impeller::CommandQueue object that command buffers must be submitted to in lieu of CommandBuffer->Submit, which has been made private. TLDR old ```c++ buffer->Submit(); ``` new ```c++ context.GetQueue()->Submit({buffer}); ``` The Metal and GLES implementations internally just call the private CommandBuffer->Submit, though there may be future opportunities to simplify here. The Vulkan implementation is where the meat is. Aiks takes advantage of this by storing all command buffers on the aiks context while rendering a frame, and then performing one submit in aiks_context render. I don't think this will introduce any thread safety problems, as we don't guarantee much about aiks context - nor do we use it in a multithreaded context as far as I know. Other tasks such as image upload still just directly submit their command buffers via the queue. Fixes https://github.com/flutter/flutter/issues/141123 --- .../ci/licenses_golden/licenses_flutter | 8 + .../src/flutter/impeller/aiks/aiks_context.cc | 1 + .../flutter/impeller/aiks/aiks_unittests.cc | 17 +- .../impeller/aiks/testing/context_mock.h | 5 + .../impeller/aiks/testing/context_spy.cc | 15 ++ .../impeller/aiks/testing/context_spy.h | 10 + .../entity/contents/content_context.cc | 17 +- .../entity/contents/content_context.h | 12 ++ .../contents/content_context_unittests.cc | 3 + .../flutter/impeller/entity/entity_pass.cc | 11 +- .../entity/geometry/point_field_geometry.cc | 3 +- .../impeller/entity/inline_pass_context.cc | 19 +- .../impeller/entity/inline_pass_context.h | 6 +- .../golden_tests/vulkan_screenshotter.mm | 11 +- .../flutter/impeller/playground/playground.cc | 6 +- engine/src/flutter/impeller/renderer/BUILD.gn | 2 + .../renderer/backend/gles/context_gles.cc | 7 + .../renderer/backend/gles/context_gles.h | 5 + .../renderer/backend/metal/context_mtl.h | 5 + .../renderer/backend/metal/context_mtl.mm | 6 + .../renderer/backend/metal/surface_mtl.mm | 2 +- .../impeller/renderer/backend/vulkan/BUILD.gn | 2 + .../backend/vulkan/command_buffer_vk.cc | 12 +- .../backend/vulkan/command_encoder_vk.cc | 55 +---- .../backend/vulkan/command_encoder_vk.h | 6 +- .../vulkan/command_encoder_vk_unittests.cc | 15 +- .../backend/vulkan/command_queue_vk.cc | 91 ++++++++ .../backend/vulkan/command_queue_vk.h | 34 +++ .../renderer/backend/vulkan/context_vk.cc | 7 + .../renderer/backend/vulkan/context_vk.h | 5 + .../renderer/backend/vulkan/gpu_tracer_vk.cc | 4 +- .../backend/vulkan/surface_context_vk.cc | 4 + .../backend/vulkan/surface_context_vk.h | 4 + .../vulkan/test/gpu_tracer_unittests.cc | 22 +- .../renderer/backend/vulkan/texture_vk.cc | 2 +- .../impeller/renderer/command_buffer.cc | 25 --- .../impeller/renderer/command_buffer.h | 53 ++--- .../impeller/renderer/command_queue.cc | 33 +++ .../flutter/impeller/renderer/command_queue.h | 50 +++++ .../impeller/renderer/compute_tessellator.cc | 3 +- .../impeller/renderer/compute_unittests.cc | 195 ++++++++++-------- .../src/flutter/impeller/renderer/context.h | 6 +- .../impeller/renderer/renderer_unittests.cc | 11 +- .../flutter/impeller/renderer/testing/mocks.h | 15 ++ engine/src/flutter/impeller/scene/scene.cc | 5 +- engine/src/flutter/lib/gpu/command_buffer.cc | 12 +- engine/src/flutter/lib/gpu/command_buffer.h | 5 +- .../lib/ui/painting/image_decoder_impeller.cc | 4 +- .../ui/painting/image_decoder_unittests.cc | 6 + .../ui/painting/image_encoding_impeller.cc | 4 +- .../ui/painting/image_encoding_unittests.cc | 12 +- engine/src/flutter/shell/common/rasterizer.cc | 4 +- .../android/android_context_gl_unittests.cc | 4 + .../android/image_external_texture_vk.cc | 2 +- 54 files changed, 590 insertions(+), 293 deletions(-) create mode 100644 engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.cc create mode 100644 engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.h create mode 100644 engine/src/flutter/impeller/renderer/command_queue.cc create mode 100644 engine/src/flutter/impeller/renderer/command_queue.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 513f30ec14..78dd5978f4 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -5449,6 +5449,8 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/command_encoder_vk.cc ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/command_pool_vk.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/command_queue_vk.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/command_queue_vk.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/compute_pass_vk.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/compute_pass_vk.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/compute_pipeline_vk.cc + ../../../flutter/LICENSE @@ -5521,6 +5523,8 @@ ORIGIN: ../../../flutter/impeller/renderer/command.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/command.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/command_buffer.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/command_buffer.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/renderer/command_queue.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/renderer/command_queue.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/compute_pass.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/compute_pass.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/compute_pipeline_builder.cc + ../../../flutter/LICENSE @@ -8298,6 +8302,8 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_encoder_vk.cc FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_pool_vk.h +FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_queue_vk.cc +FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_queue_vk.h FILE: ../../../flutter/impeller/renderer/backend/vulkan/compute_pass_vk.cc FILE: ../../../flutter/impeller/renderer/backend/vulkan/compute_pass_vk.h FILE: ../../../flutter/impeller/renderer/backend/vulkan/compute_pipeline_vk.cc @@ -8371,6 +8377,8 @@ FILE: ../../../flutter/impeller/renderer/command.cc FILE: ../../../flutter/impeller/renderer/command.h FILE: ../../../flutter/impeller/renderer/command_buffer.cc FILE: ../../../flutter/impeller/renderer/command_buffer.h +FILE: ../../../flutter/impeller/renderer/command_queue.cc +FILE: ../../../flutter/impeller/renderer/command_queue.h FILE: ../../../flutter/impeller/renderer/compute_pass.cc FILE: ../../../flutter/impeller/renderer/compute_pass.h FILE: ../../../flutter/impeller/renderer/compute_pipeline_builder.cc diff --git a/engine/src/flutter/impeller/aiks/aiks_context.cc b/engine/src/flutter/impeller/aiks/aiks_context.cc index 3be53f9537..3ebf874707 100644 --- a/engine/src/flutter/impeller/aiks/aiks_context.cc +++ b/engine/src/flutter/impeller/aiks/aiks_context.cc @@ -53,6 +53,7 @@ bool AiksContext::Render(const Picture& picture, } fml::ScopedCleanupClosure closure([&]() { + content_context_->FlushCommandBuffers(); if (reset_host_buffer) { content_context_->GetTransientsBuffer().Reset(); } diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index a40c1e0728..25e20e316d 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -141,8 +141,7 @@ TEST_P(AiksTest, CanRenderColorFilterWithInvertColorsDrawPaint) { namespace { bool GenerateMipmap(const std::shared_ptr& context, std::shared_ptr texture, - std::string label, - bool async_submit) { + std::string label) { auto buffer = context->CreateCommandBuffer(); if (!buffer) { return false; @@ -152,23 +151,19 @@ bool GenerateMipmap(const std::shared_ptr& context, return false; } pass->GenerateMipmap(std::move(texture), std::move(label)); - if (async_submit) { - return buffer->EncodeAndSubmit(pass, context->GetResourceAllocator()); - } pass->EncodeCommands(context->GetResourceAllocator()); - return buffer->SubmitCommands(); + return context->GetCommandQueue()->Submit({buffer}).ok(); } void CanRenderTiledTexture(AiksTest* aiks_test, Entity::TileMode tile_mode, - bool async_submit = false, Matrix local_matrix = {}) { auto context = aiks_test->GetContext(); ASSERT_TRUE(context); auto texture = aiks_test->CreateTextureForFixture("table_mountain_nx.png", /*enable_mipmapping=*/true); - GenerateMipmap(context, texture, "table_mountain_nx", async_submit); + GenerateMipmap(context, texture, "table_mountain_nx"); Canvas canvas; canvas.Scale(aiks_test->GetContentScale()); canvas.Translate({100.0f, 100.0f, 0}); @@ -215,10 +210,6 @@ TEST_P(AiksTest, CanRenderTiledTextureClamp) { CanRenderTiledTexture(this, Entity::TileMode::kClamp); } -TEST_P(AiksTest, CanRenderTiledTextureClampAsync) { - CanRenderTiledTexture(this, Entity::TileMode::kClamp, /*async_submit=*/true); -} - TEST_P(AiksTest, CanRenderTiledTextureRepeat) { CanRenderTiledTexture(this, Entity::TileMode::kRepeat); } @@ -232,7 +223,7 @@ TEST_P(AiksTest, CanRenderTiledTextureDecal) { } TEST_P(AiksTest, CanRenderTiledTextureClampWithTranslate) { - CanRenderTiledTexture(this, Entity::TileMode::kClamp, /*async_submit=*/false, + CanRenderTiledTexture(this, Entity::TileMode::kClamp, Matrix::MakeTranslation({172.f, 172.f, 0.f})); } diff --git a/engine/src/flutter/impeller/aiks/testing/context_mock.h b/engine/src/flutter/impeller/aiks/testing/context_mock.h index b2d9138503..c28b2f5776 100644 --- a/engine/src/flutter/impeller/aiks/testing/context_mock.h +++ b/engine/src/flutter/impeller/aiks/testing/context_mock.h @@ -111,6 +111,11 @@ class ContextMock : public Context { (), (const, override)); + MOCK_METHOD(std::shared_ptr, + GetCommandQueue, + (), + (const override)); + MOCK_METHOD(void, Shutdown, (), (override)); }; diff --git a/engine/src/flutter/impeller/aiks/testing/context_spy.cc b/engine/src/flutter/impeller/aiks/testing/context_spy.cc index 460013cc28..325dc33155 100644 --- a/engine/src/flutter/impeller/aiks/testing/context_spy.cc +++ b/engine/src/flutter/impeller/aiks/testing/context_spy.cc @@ -3,12 +3,23 @@ // found in the LICENSE file. #include +#include "impeller/renderer/command_buffer.h" +#include "impeller/renderer/command_queue.h" #include "impeller/aiks/testing/context_spy.h" namespace impeller { namespace testing { +fml::Status NoopCommandQueue::Submit( + const std::vector>& buffers, + const CompletionCallback& completion_callback) { + if (completion_callback) { + completion_callback(CommandBuffer::Status::kCompleted); + } + return fml::Status(); +} + std::shared_ptr ContextSpy::Make() { return std::shared_ptr(new ContextSpy()); } @@ -50,6 +61,10 @@ std::shared_ptr ContextSpy::MakeContext( return real_context->GetPipelineLibrary(); }); + ON_CALL(*mock_context, GetCommandQueue).WillByDefault([shared_this]() { + return shared_this->command_queue_; + }); + ON_CALL(*mock_context, CreateCommandBuffer) .WillByDefault([real_context, shared_this]() { auto real_buffer = real_context->CreateCommandBuffer(); diff --git a/engine/src/flutter/impeller/aiks/testing/context_spy.h b/engine/src/flutter/impeller/aiks/testing/context_spy.h index fe251b89e5..c4fb6c84fa 100644 --- a/engine/src/flutter/impeller/aiks/testing/context_spy.h +++ b/engine/src/flutter/impeller/aiks/testing/context_spy.h @@ -9,10 +9,18 @@ #include "impeller/aiks/testing/context_mock.h" #include "impeller/entity/contents/test/recording_render_pass.h" +#include "impeller/renderer/command_queue.h" namespace impeller { namespace testing { +class NoopCommandQueue : public CommandQueue { + public: + fml::Status Submit( + const std::vector>& buffers, + const CompletionCallback& completion_callback = {}) override; +}; + /// Forwards calls to a real Context but can store information about how /// the Context was used. class ContextSpy : public std::enable_shared_from_this { @@ -23,6 +31,8 @@ class ContextSpy : public std::enable_shared_from_this { const std::shared_ptr& real_context); std::vector> render_passes_; + std::shared_ptr command_queue_ = + std::make_shared(); private: ContextSpy() = default; diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index f4c186199a..be6f63df11 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -200,7 +200,8 @@ ContentContext::ContentContext( ? std::make_shared( context_->GetResourceAllocator()) : std::move(render_target_allocator)), - host_buffer_(HostBuffer::Create(context_->GetResourceAllocator())) { + host_buffer_(HostBuffer::Create(context_->GetResourceAllocator())), + pending_command_buffers_(std::make_unique()) { if (!context_ || !context_->IsValid()) { return; } @@ -477,9 +478,10 @@ fml::StatusOr ContentContext::MakeSubpass( return fml::Status(fml::StatusCode::kUnknown, ""); } - if (!sub_command_buffer->EncodeAndSubmit(sub_renderpass)) { + if (!sub_renderpass->EncodeCommands()) { return fml::Status(fml::StatusCode::kUnknown, ""); } + RecordCommandBuffer(std::move(sub_command_buffer)); return subpass_target; } @@ -532,4 +534,15 @@ void ContentContext::ClearCachedRuntimeEffectPipeline( } } +void ContentContext::RecordCommandBuffer( + std::shared_ptr command_buffer) const { + pending_command_buffers_->command_buffers.push_back( + std::move(command_buffer)); +} + +void ContentContext::FlushCommandBuffers() const { + auto buffers = std::move(pending_command_buffers_->command_buffers); + GetContext()->GetCommandQueue()->Submit(buffers); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index 2bcbb72506..7b43e469a7 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -18,6 +18,7 @@ #include "impeller/core/host_buffer.h" #include "impeller/entity/entity.h" #include "impeller/renderer/capabilities.h" +#include "impeller/renderer/command_buffer.h" #include "impeller/renderer/pipeline.h" #include "impeller/renderer/pipeline_descriptor.h" #include "impeller/renderer/render_target.h" @@ -268,6 +269,12 @@ using TiledTextureExternalPipeline = TiledTextureFillExternalFragmentShader>; #endif // IMPELLER_ENABLE_OPENGLES +// A struct used to isolate command buffer storage from the content +// context options to preserve const-ness. +struct PendingCommandBuffers { + std::vector> command_buffers; +}; + /// Pipeline state configuration. /// /// Each unique combination of these options requires a different pipeline state @@ -715,6 +722,10 @@ class ContentContext { void SetWireframe(bool wireframe); + void RecordCommandBuffer(std::shared_ptr command_buffer) const; + + void FlushCommandBuffers() const; + using SubpassCallback = std::function; @@ -1005,6 +1016,7 @@ class ContentContext { #endif // IMPELLER_ENABLE_3D std::shared_ptr render_target_cache_; std::shared_ptr host_buffer_; + std::unique_ptr pending_command_buffers_; bool wireframe_ = false; ContentContext(const ContentContext&) = delete; diff --git a/engine/src/flutter/impeller/entity/contents/content_context_unittests.cc b/engine/src/flutter/impeller/entity/contents/content_context_unittests.cc index 38d17c1818..ca56680aaf 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context_unittests.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context_unittests.cc @@ -4,6 +4,7 @@ #include +#include "fml/logging.h" #include "gtest/gtest.h" #include "impeller/core/allocator.h" @@ -11,6 +12,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/geometry/color.h" #include "impeller/renderer/capabilities.h" +#include "impeller/renderer/command_queue.h" #include "impeller/renderer/pipeline.h" #include "impeller/renderer/pipeline_descriptor.h" @@ -52,6 +54,7 @@ class FakeContext : public Context { std::shared_ptr GetPipelineLibrary() const { return nullptr; } + std::shared_ptr GetCommandQueue() const { FML_UNREACHABLE(); } std::shared_ptr CreateCommandBuffer() const { return nullptr; } void Shutdown() {} diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index 74e343f989..94c4cf5061 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -387,11 +387,12 @@ bool EntityPass::Render(ContentContext& renderer, blit_pass->AddCopy( offscreen_target.GetRenderTarget().GetRenderTargetTexture(), root_render_target.GetRenderTargetTexture()); - if (!command_buffer->EncodeAndSubmit( - blit_pass, renderer.GetContext()->GetResourceAllocator())) { + if (!blit_pass->EncodeCommands( + renderer.GetContext()->GetResourceAllocator())) { VALIDATION_LOG << "Failed to encode root pass blit command."; return false; } + renderer.RecordCommandBuffer(std::move(command_buffer)); } else { auto render_pass = command_buffer->CreateRenderPass(root_render_target); render_pass->SetLabel("EntityPass Root Render Pass"); @@ -415,10 +416,11 @@ bool EntityPass::Render(ContentContext& renderer, } } - if (!command_buffer->EncodeAndSubmit(render_pass)) { + if (!render_pass->EncodeCommands()) { VALIDATION_LOG << "Failed to encode root pass command buffer."; return false; } + renderer.RecordCommandBuffer(std::move(command_buffer)); } return true; @@ -855,8 +857,7 @@ bool EntityPass::OnRender( collapsed_parent_pass) const { TRACE_EVENT0("impeller", "EntityPass::OnRender"); - const std::shared_ptr& context = renderer.GetContext(); - InlinePassContext pass_context(context, pass_target, + InlinePassContext pass_context(renderer, pass_target, GetTotalPassReads(renderer), GetElementCount(), collapsed_parent_pass); if (!pass_context.IsValid()) { diff --git a/engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc b/engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc index 5b5cd8caf7..69d9345513 100644 --- a/engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc +++ b/engine/src/flutter/impeller/entity/geometry/point_field_geometry.cc @@ -220,9 +220,10 @@ GeometryResult PointFieldGeometry::GetPositionBufferGPU( output = geometry_uv_buffer; } - if (!compute_pass->EncodeCommands() || !cmd_buffer->SubmitCommands()) { + if (!compute_pass->EncodeCommands()) { return {}; } + renderer.RecordCommandBuffer(std::move(cmd_buffer)); return { .type = PrimitiveType::kTriangle, diff --git a/engine/src/flutter/impeller/entity/inline_pass_context.cc b/engine/src/flutter/impeller/entity/inline_pass_context.cc index 2ebfbbfaa3..d9258acd79 100644 --- a/engine/src/flutter/impeller/entity/inline_pass_context.cc +++ b/engine/src/flutter/impeller/entity/inline_pass_context.cc @@ -10,6 +10,7 @@ #include "impeller/base/allocation.h" #include "impeller/base/validation.h" #include "impeller/core/formats.h" +#include "impeller/entity/contents/content_context.h" #include "impeller/entity/entity_pass_target.h" #include "impeller/renderer/command_buffer.h" #include "impeller/renderer/texture_mipmap.h" @@ -17,12 +18,12 @@ namespace impeller { InlinePassContext::InlinePassContext( - std::shared_ptr context, + const ContentContext& renderer, EntityPassTarget& pass_target, uint32_t pass_texture_reads, uint32_t entity_count, std::optional collapsed_parent_pass) - : context_(std::move(context)), + : renderer_(renderer), pass_target_(pass_target), entity_count_(entity_count), is_collapsed_(collapsed_parent_pass.has_value()) { @@ -67,15 +68,13 @@ bool InlinePassContext::EndPass() { const std::shared_ptr& target_texture = GetPassTarget().GetRenderTarget().GetRenderTargetTexture(); if (target_texture->GetMipCount() > 1) { - fml::Status mip_status = - AddMipmapGeneration(command_buffer_, context_, target_texture); + fml::Status mip_status = AddMipmapGeneration( + command_buffer_, renderer_.GetContext(), target_texture); if (!mip_status.ok()) { return false; } } - if (!command_buffer_->SubmitCommands()) { - return false; - } + renderer_.RecordCommandBuffer(std::move(command_buffer_)); pass_ = nullptr; command_buffer_ = nullptr; @@ -97,7 +96,7 @@ InlinePassContext::RenderPassResult InlinePassContext::GetRenderPass( /// time this method is called, but it'll also run if the pass has been /// previously ended via `EndPass`. - command_buffer_ = context_->CreateCommandBuffer(); + command_buffer_ = renderer_.GetContext()->CreateCommandBuffer(); if (!command_buffer_) { VALIDATION_LOG << "Could not create command buffer."; return {}; @@ -122,7 +121,7 @@ InlinePassContext::RenderPassResult InlinePassContext::GetRenderPass( ->second.resolve_texture != nullptr; if (pass_count_ > 0 && is_msaa) { result.backdrop_texture = - pass_target_.Flip(*context_->GetResourceAllocator()); + pass_target_.Flip(*renderer_.GetContext()->GetResourceAllocator()); if (!result.backdrop_texture) { VALIDATION_LOG << "Could not flip the EntityPass render target."; } @@ -174,7 +173,7 @@ InlinePassContext::RenderPassResult InlinePassContext::GetRenderPass( result.pass = pass_; - if (!context_->GetCapabilities()->SupportsReadFromResolve() && + if (!renderer_.GetContext()->GetCapabilities()->SupportsReadFromResolve() && result.backdrop_texture == result.pass->GetRenderTarget().GetRenderTargetTexture()) { VALIDATION_LOG << "EntityPass backdrop restore configuration is not valid " diff --git a/engine/src/flutter/impeller/entity/inline_pass_context.h b/engine/src/flutter/impeller/entity/inline_pass_context.h index 595d291b5a..43ee0f6b98 100644 --- a/engine/src/flutter/impeller/entity/inline_pass_context.h +++ b/engine/src/flutter/impeller/entity/inline_pass_context.h @@ -7,10 +7,10 @@ #include +#include "impeller/entity/contents/content_context.h" #include "impeller/entity/entity_pass_target.h" #include "impeller/renderer/context.h" #include "impeller/renderer/render_pass.h" -#include "impeller/renderer/render_target.h" namespace impeller { @@ -22,7 +22,7 @@ class InlinePassContext { }; InlinePassContext( - std::shared_ptr context, + const ContentContext& renderer, EntityPassTarget& pass_target, uint32_t pass_texture_reads, uint32_t entity_count, @@ -45,7 +45,7 @@ class InlinePassContext { RenderPassResult GetRenderPass(uint32_t pass_depth); private: - std::shared_ptr context_; + const ContentContext& renderer_; EntityPassTarget& pass_target_; std::shared_ptr command_buffer_; std::shared_ptr pass_; diff --git a/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm b/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm index afa3a0262a..57c109e565 100644 --- a/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm +++ b/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm @@ -36,10 +36,13 @@ std::unique_ptr ReadTexture( fml::AutoResetWaitableEvent latch; success = - command_buffer->SubmitCommands([&latch](CommandBuffer::Status status) { - FML_CHECK(status == CommandBuffer::Status::kCompleted); - latch.Signal(); - }); + surface_context->GetCommandQueue() + ->Submit({command_buffer}, + [&latch](CommandBuffer::Status status) { + FML_CHECK(status == CommandBuffer::Status::kCompleted); + latch.Signal(); + }) + .ok(); FML_CHECK(success); latch.Wait(); diff --git a/engine/src/flutter/impeller/playground/playground.cc b/engine/src/flutter/impeller/playground/playground.cc index df44559af8..f070ff316a 100644 --- a/engine/src/flutter/impeller/playground/playground.cc +++ b/engine/src/flutter/impeller/playground/playground.cc @@ -306,7 +306,7 @@ bool Playground::OpenPlaygroundHere( ImGui_ImplImpeller_RenderDrawData(ImGui::GetDrawData(), *pass); pass->EncodeCommands(); - if (!buffer->SubmitCommands()) { + if (!renderer->GetContext()->GetCommandQueue()->Submit({buffer}).ok()) { return false; } } @@ -350,7 +350,7 @@ bool Playground::OpenPlaygroundHere(SinglePassCallback pass_callback) { } pass->EncodeCommands(); - if (!buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({buffer}).ok()) { return false; } return true; @@ -422,7 +422,7 @@ static std::shared_ptr CreateTextureForDecompressedImage( blit_pass->SetLabel("Mipmap Blit Pass"); blit_pass->GenerateMipmap(texture); blit_pass->EncodeCommands(context->GetResourceAllocator()); - if (!command_buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) { FML_DLOG(ERROR) << "Failed to submit blit pass command buffer."; return nullptr; } diff --git a/engine/src/flutter/impeller/renderer/BUILD.gn b/engine/src/flutter/impeller/renderer/BUILD.gn index 1456b65cb3..9fdc8964bc 100644 --- a/engine/src/flutter/impeller/renderer/BUILD.gn +++ b/engine/src/flutter/impeller/renderer/BUILD.gn @@ -55,6 +55,8 @@ impeller_component("renderer") { "command.h", "command_buffer.cc", "command_buffer.h", + "command_queue.cc", + "command_queue.h", "compute_pass.cc", "compute_pass.h", "compute_pipeline_builder.cc", diff --git a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc index 779255dffa..7629eff28e 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc @@ -9,6 +9,7 @@ #include "impeller/base/validation.h" #include "impeller/renderer/backend/gles/command_buffer_gles.h" #include "impeller/renderer/backend/gles/gpu_tracer_gles.h" +#include "impeller/renderer/command_queue.h" namespace impeller { @@ -67,6 +68,7 @@ ContextGLES::ContextGLES( } gpu_tracer_ = std::make_shared(GetReactor()->GetProcTable(), enable_gpu_tracing); + command_queue_ = std::make_shared(); is_valid_ = true; } @@ -138,4 +140,9 @@ const std::shared_ptr& ContextGLES::GetCapabilities() return device_capabilities_; } +// |Context| +std::shared_ptr ContextGLES::GetCommandQueue() const { + return command_queue_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h index 3b1d198702..2a0f09aca3 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h @@ -14,6 +14,7 @@ #include "impeller/renderer/backend/gles/sampler_library_gles.h" #include "impeller/renderer/backend/gles/shader_library_gles.h" #include "impeller/renderer/capabilities.h" +#include "impeller/renderer/command_queue.h" #include "impeller/renderer/context.h" namespace impeller { @@ -48,6 +49,7 @@ class ContextGLES final : public Context, std::shared_ptr pipeline_library_; std::shared_ptr sampler_library_; std::shared_ptr resource_allocator_; + std::shared_ptr command_queue_; std::shared_ptr gpu_tracer_; // Note: This is stored separately from the ProcTableGLES CapabilitiesGLES @@ -85,6 +87,9 @@ class ContextGLES final : public Context, // |Context| const std::shared_ptr& GetCapabilities() const override; + // |Context| + std::shared_ptr GetCommandQueue() const override; + // |Context| void Shutdown() override; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h index f0735a3219..6497871b28 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h @@ -21,6 +21,7 @@ #include "impeller/renderer/backend/metal/pipeline_library_mtl.h" #include "impeller/renderer/backend/metal/shader_library_mtl.h" #include "impeller/renderer/capabilities.h" +#include "impeller/renderer/command_queue.h" #include "impeller/renderer/context.h" #if TARGET_OS_SIMULATOR @@ -80,6 +81,9 @@ class ContextMTL final : public Context, // |Context| std::shared_ptr CreateCommandBuffer() const override; + // |Context| + std::shared_ptr GetCommandQueue() const override; + // |Context| const std::shared_ptr& GetCapabilities() const override; @@ -126,6 +130,7 @@ class ContextMTL final : public Context, #endif // IMPELLER_DEBUG std::deque> tasks_awaiting_gpu_; std::unique_ptr sync_switch_observer_; + std::shared_ptr command_queue_ip_; bool is_valid_ = false; ContextMTL( 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 9c2342d302..8b23d4bed6 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm @@ -128,6 +128,7 @@ ContextMTL::ContextMTL( device_capabilities_ = InferMetalCapabilities(device_, PixelFormat::kB8G8R8A8UNormInt); + command_queue_ip_ = std::make_shared(); #ifdef IMPELLER_DEBUG gpu_tracer_ = std::make_shared(); #endif // IMPELLER_DEBUG @@ -394,4 +395,9 @@ void ContextMTL::SyncSwitchObserver::OnSyncSwitchUpdate(bool new_is_disabled) { } } +// |Context| +std::shared_ptr ContextMTL::GetCommandQueue() const { + return command_queue_ip_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/metal/surface_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/surface_mtl.mm index 20488fee9d..f3d5240807 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/surface_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/surface_mtl.mm @@ -242,7 +242,7 @@ bool SurfaceMTL::Present() const { blit_pass->AddCopy(source_texture_, destination_texture_, std::nullopt, clip_rect_->GetOrigin()); blit_pass->EncodeCommands(context->GetResourceAllocator()); - if (!blit_command_buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({blit_command_buffer}).ok()) { return false; } } diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/BUILD.gn b/engine/src/flutter/impeller/renderer/backend/vulkan/BUILD.gn index 63cf211048..78a356cb49 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/BUILD.gn +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/BUILD.gn @@ -46,6 +46,8 @@ impeller_component("vulkan") { "command_encoder_vk.h", "command_pool_vk.cc", "command_pool_vk.h", + "command_queue_vk.cc", + "command_queue_vk.h", "compute_pass_vk.cc", "compute_pass_vk.h", "compute_pipeline_vk.cc", diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/command_buffer_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/command_buffer_vk.cc index c63dbec45d..62e271f7b1 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/command_buffer_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/command_buffer_vk.cc @@ -7,6 +7,7 @@ #include #include +#include "fml/logging.h" #include "impeller/renderer/backend/vulkan/blit_pass_vk.h" #include "impeller/renderer/backend/vulkan/command_encoder_vk.h" #include "impeller/renderer/backend/vulkan/compute_pass_vk.h" @@ -49,16 +50,7 @@ const std::shared_ptr& CommandBufferVK::GetEncoder() { } bool CommandBufferVK::OnSubmitCommands(CompletionCallback callback) { - if (!encoder_) { - encoder_ = encoder_factory_->Create(); - } - if (!callback) { - return encoder_->Submit(); - } - return encoder_->Submit([callback](bool submitted) { - callback(submitted ? CommandBuffer::Status::kCompleted - : CommandBuffer::Status::kError); - }); + FML_UNREACHABLE() } void CommandBufferVK::OnWaitUntilScheduled() {} diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.cc index cd8dc86c05..3e809a8e28 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.cc @@ -81,29 +81,10 @@ bool CommandEncoderVK::IsValid() const { return is_valid_; } -bool CommandEncoderVK::Submit(SubmitCallback callback) { - // Make sure to call callback with `false` if anything returns early. - bool fail_callback = !!callback; - if (!IsValid()) { - VALIDATION_LOG << "Cannot submit invalid CommandEncoderVK."; - if (fail_callback) { - callback(false); - } - return false; - } - - // Success or failure, you only get to submit once. - fml::ScopedCleanupClosure reset([&]() { - if (fail_callback) { - callback(false); - } - Reset(); - }); - +bool CommandEncoderVK::EndCommandBuffer() const { InsertDebugMarker("QueueSubmit"); auto command_buffer = GetCommandBuffer(); - tracked_objects_->GetGPUProbe().RecordCmdBufferEnd(command_buffer); auto status = command_buffer.end(); @@ -111,39 +92,7 @@ bool CommandEncoderVK::Submit(SubmitCallback callback) { VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(status); return false; } - std::shared_ptr strong_device = device_holder_.lock(); - if (!strong_device) { - VALIDATION_LOG << "Device lost."; - return false; - } - auto [fence_result, fence] = strong_device->GetDevice().createFenceUnique({}); - if (fence_result != vk::Result::eSuccess) { - VALIDATION_LOG << "Failed to create fence: " << vk::to_string(fence_result); - return false; - } - - vk::SubmitInfo submit_info; - std::vector buffers = {command_buffer}; - submit_info.setCommandBuffers(buffers); - status = queue_->Submit(submit_info, *fence); - if (status != vk::Result::eSuccess) { - VALIDATION_LOG << "Failed to submit queue: " << vk::to_string(status); - return false; - } - - // Submit will proceed, call callback with true when it is done and do not - // call when `reset` is collected. - fail_callback = false; - return fence_waiter_->AddFence( - std::move(fence), - [callback, tracked_objects = std::move(tracked_objects_)]() mutable { - // Ensure tracked objects are destructed before calling any final - // callbacks. - tracked_objects.reset(); - if (callback) { - callback(true); - } - }); + return true; } vk::CommandBuffer CommandEncoderVK::GetCommandBuffer() const { diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h index f24e53c9b0..8e5aa11e7e 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h @@ -10,6 +10,7 @@ #include #include "impeller/renderer/backend/vulkan/command_pool_vk.h" +#include "impeller/renderer/backend/vulkan/command_queue_vk.h" #include "impeller/renderer/backend/vulkan/context_vk.h" #include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h" #include "impeller/renderer/backend/vulkan/device_holder.h" @@ -60,8 +61,6 @@ class CommandEncoderVK { bool IsValid() const; - bool Submit(SubmitCallback callback = {}); - bool Track(std::shared_ptr object); bool Track(std::shared_ptr buffer); @@ -82,12 +81,15 @@ class CommandEncoderVK { void InsertDebugMarker(std::string_view label) const; + bool EndCommandBuffer() const; + fml::StatusOr AllocateDescriptorSets( const vk::DescriptorSetLayout& layout, const ContextVK& context); private: friend class ContextVK; + friend class CommandQueueVK; std::weak_ptr device_holder_; std::shared_ptr tracked_objects_; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk_unittests.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk_unittests.cc index 3f17a6e99c..7f4dde9b0b 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk_unittests.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/command_encoder_vk_unittests.cc @@ -8,6 +8,7 @@ #include "fml/synchronization/waitable_event.h" #include "impeller/renderer/backend/vulkan/command_encoder_vk.h" #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h" +#include "impeller/renderer/command_buffer.h" namespace impeller { namespace testing { @@ -48,13 +49,13 @@ TEST(CommandEncoderVKTest, CleanupAfterSubmit) { fml::AutoResetWaitableEvent wait_for_thread_join; auto context = MockVulkanContextBuilder().Build(); std::thread thread([&] { - CommandEncoderFactoryVK factory(context); - std::shared_ptr encoder = factory.Create(); - encoder->Submit([&](bool success) { - ASSERT_TRUE(success); - wait_for_thread_join.Wait(); - wait_for_submit.Signal(); - }); + auto buffer = context->CreateCommandBuffer(); + context->GetCommandQueue()->Submit( + {buffer}, [&](CommandBuffer::Status status) { + ASSERT_EQ(status, CommandBuffer::Status::kCompleted); + wait_for_thread_join.Wait(); + wait_for_submit.Signal(); + }); }); thread.join(); wait_for_thread_join.Signal(); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.cc new file mode 100644 index 0000000000..11842a1046 --- /dev/null +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.cc @@ -0,0 +1,91 @@ +// 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 "fml/status.h" + +#include "impeller/renderer/backend/vulkan/command_queue_vk.h" + +#include "impeller/base/validation.h" +#include "impeller/renderer/backend/vulkan/command_buffer_vk.h" +#include "impeller/renderer/backend/vulkan/command_encoder_vk.h" +#include "impeller/renderer/backend/vulkan/context_vk.h" +#include "impeller/renderer/backend/vulkan/fence_waiter_vk.h" +#include "impeller/renderer/backend/vulkan/tracked_objects_vk.h" +#include "impeller/renderer/command_buffer.h" + +namespace impeller { + +CommandQueueVK::CommandQueueVK(const std::weak_ptr& context) + : context_(context) {} + +CommandQueueVK::~CommandQueueVK() = default; + +fml::Status CommandQueueVK::Submit( + const std::vector>& buffers, + const CompletionCallback& completion_callback) { + if (buffers.empty()) { + return fml::Status(fml::StatusCode::kInvalidArgument, + "No command buffers provided."); + } + // Success or failure, you only get to submit once. + fml::ScopedCleanupClosure reset([&]() { + if (completion_callback) { + completion_callback(CommandBuffer::Status::kError); + } + }); + + std::vector vk_buffers; + std::vector> tracked_objects; + vk_buffers.reserve(buffers.size()); + tracked_objects.reserve(buffers.size()); + for (const std::shared_ptr& buffer : buffers) { + auto encoder = CommandBufferVK::Cast(*buffer).GetEncoder(); + if (!encoder->EndCommandBuffer()) { + return fml::Status(fml::StatusCode::kCancelled, + "Failed to end command buffer."); + } + tracked_objects.push_back(encoder->tracked_objects_); + vk_buffers.push_back(encoder->GetCommandBuffer()); + encoder->Reset(); + } + + auto context = context_.lock(); + if (!context) { + VALIDATION_LOG << "Device lost."; + return fml::Status(fml::StatusCode::kCancelled, "Device lost."); + } + auto [fence_result, fence] = context->GetDevice().createFenceUnique({}); + if (fence_result != vk::Result::eSuccess) { + VALIDATION_LOG << "Failed to create fence: " << vk::to_string(fence_result); + return fml::Status(fml::StatusCode::kCancelled, "Failed to create fence."); + } + + vk::SubmitInfo submit_info; + submit_info.setCommandBuffers(vk_buffers); + auto status = context->GetGraphicsQueue()->Submit(submit_info, *fence); + if (status != vk::Result::eSuccess) { + VALIDATION_LOG << "Failed to submit queue: " << vk::to_string(status); + return fml::Status(fml::StatusCode::kCancelled, "Failed to submit queue: "); + } + + // Submit will proceed, call callback with true when it is done and do not + // call when `reset` is collected. + auto added_fence = context->GetFenceWaiter()->AddFence( + std::move(fence), [completion_callback, tracked_objects = std::move( + tracked_objects)]() mutable { + // Ensure tracked objects are destructed before calling any final + // callbacks. + tracked_objects.clear(); + if (completion_callback) { + completion_callback(CommandBuffer::Status::kCompleted); + } + }); + if (!added_fence) { + return fml::Status(fml::StatusCode::kCancelled, "Failed to add fence."); + } + reset.Release(); + return fml::Status(); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.h new file mode 100644 index 0000000000..6397391c84 --- /dev/null +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/command_queue_vk.h @@ -0,0 +1,34 @@ +// 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. + +#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_COMMAND_QUEUE_VK_H_ +#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_COMMAND_QUEUE_VK_H_ + +#include "impeller/renderer/command_queue.h" + +namespace impeller { + +class ContextVK; + +class CommandQueueVK : public CommandQueue { + public: + explicit CommandQueueVK(const std::weak_ptr& context); + + ~CommandQueueVK() override; + + fml::Status Submit( + const std::vector>& buffers, + const CompletionCallback& completion_callback = {}) override; + + private: + std::weak_ptr context_; + + CommandQueueVK(const CommandQueueVK&) = delete; + + CommandQueueVK& operator=(const CommandQueueVK&) = delete; +}; + +} // namespace impeller + +#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_COMMAND_QUEUE_VK_H_ diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc index e573d6aa5b..1a625f357a 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc @@ -5,6 +5,7 @@ #include "impeller/renderer/backend/vulkan/context_vk.h" #include "fml/concurrent_message_loop.h" +#include "impeller/renderer/backend/vulkan/command_queue_vk.h" #ifdef FML_OS_ANDROID #include @@ -26,6 +27,7 @@ #include "impeller/renderer/backend/vulkan/command_buffer_vk.h" #include "impeller/renderer/backend/vulkan/command_encoder_vk.h" #include "impeller/renderer/backend/vulkan/command_pool_vk.h" +#include "impeller/renderer/backend/vulkan/command_queue_vk.h" #include "impeller/renderer/backend/vulkan/debug_report_vk.h" #include "impeller/renderer/backend/vulkan/fence_waiter_vk.h" #include "impeller/renderer/backend/vulkan/gpu_tracer_vk.h" @@ -445,6 +447,7 @@ void ContextVK::Setup(Settings settings) { command_pool_recycler_ = std::move(command_pool_recycler); descriptor_pool_recycler_ = std::move(descriptor_pool_recycler); device_name_ = std::string(physical_device_properties.deviceName); + command_queue_vk_ = std::make_shared(weak_from_this()); is_valid_ = true; // Create the GPU Tracer later because it depends on state from @@ -563,4 +566,8 @@ std::shared_ptr ContextVK::GetGPUTracer() const { return gpu_tracer_; } +std::shared_ptr ContextVK::GetCommandQueue() const { + return command_queue_vk_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h index ac059ebed2..07884f1f1e 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h @@ -20,6 +20,7 @@ #include "impeller/renderer/backend/vulkan/sampler_library_vk.h" #include "impeller/renderer/backend/vulkan/shader_library_vk.h" #include "impeller/renderer/capabilities.h" +#include "impeller/renderer/command_queue.h" #include "impeller/renderer/context.h" namespace impeller { @@ -35,6 +36,7 @@ class ResourceManagerVK; class SurfaceContextVK; class GPUTracerVK; class DescriptorPoolRecyclerVK; +class CommandQueueVK; class ContextVK final : public Context, public BackendCast, @@ -163,6 +165,8 @@ class ContextVK final : public Context, return descriptor_pool_recycler_; } + std::shared_ptr GetCommandQueue() const override; + std::shared_ptr GetGPUTracer() const; void RecordFrameEndTime() const; @@ -197,6 +201,7 @@ class ContextVK final : public Context, std::unique_ptr queue_submit_thread_; std::shared_ptr gpu_tracer_; std::shared_ptr descriptor_pool_recycler_; + std::shared_ptr command_queue_vk_; bool sync_presentation_ = false; const uint64_t hash_; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/gpu_tracer_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/gpu_tracer_vk.cc index 19733d6a25..85e66d5c01 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/gpu_tracer_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/gpu_tracer_vk.cc @@ -33,7 +33,7 @@ GPUTracerVK::GPUTracerVK(std::weak_ptr context) // The device does not support timestamp queries. return; } - // Disable tracing in release mode. +// Disable tracing in release mode. #ifdef IMPELLER_DEBUG enabled_ = true; #endif // IMPELLER_DEBUG @@ -61,7 +61,7 @@ void GPUTracerVK::InitializeQueryPool(const ContextVK& context) { buffer_vk.GetEncoder()->GetCommandBuffer().resetQueryPool( trace_states_[i].query_pool.get(), 0, kPoolSize); } - if (!buffer->SubmitCommands()) { + if (!context.GetCommandQueue()->Submit({buffer}).ok()) { VALIDATION_LOG << "Failed to reset query pool for trace events."; enabled_ = false; } diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.cc index 646d8a8d15..8abea86d3c 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.cc @@ -48,6 +48,10 @@ std::shared_ptr SurfaceContextVK::CreateCommandBuffer() const { return parent_->CreateCommandBuffer(); } +std::shared_ptr SurfaceContextVK::GetCommandQueue() const { + return parent_->GetCommandQueue(); +} + const std::shared_ptr& SurfaceContextVK::GetCapabilities() const { return parent_->GetCapabilities(); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.h index cf849c576d..56d6b0275d 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_context_vk.h @@ -9,6 +9,7 @@ #include "impeller/base/backend_cast.h" #include "impeller/renderer/backend/vulkan/vk.h" +#include "impeller/renderer/command_queue.h" #include "impeller/renderer/context.h" namespace impeller { @@ -62,6 +63,9 @@ class SurfaceContextVK : public Context, // |Context| const std::shared_ptr& GetCapabilities() const override; + // |Context| + std::shared_ptr GetCommandQueue() const override; + // |Context| void Shutdown() override; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/test/gpu_tracer_unittests.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/test/gpu_tracer_unittests.cc index bb3fa21e27..a9a267b19e 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/test/gpu_tracer_unittests.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/test/gpu_tracer_unittests.cc @@ -7,7 +7,6 @@ #include "fml/synchronization/count_down_latch.h" #include "gtest/gtest.h" #include "impeller/renderer//backend/vulkan/command_encoder_vk.h" -#include "impeller/renderer/backend/vulkan/command_buffer_vk.h" #include "impeller/renderer/backend/vulkan/context_vk.h" #include "impeller/renderer/backend/vulkan/gpu_tracer_vk.h" #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h" @@ -29,8 +28,11 @@ TEST(GPUTracerVK, CanTraceCmdBuffer) { auto latch = std::make_shared(1u); - if (!cmd_buffer->SubmitCommands( - [latch](CommandBuffer::Status status) { latch->CountDown(); })) { + if (!context->GetCommandQueue() + ->Submit( + {cmd_buffer}, + [latch](CommandBuffer::Status status) { latch->CountDown(); }) + .ok()) { GTEST_FAIL() << "Failed to submit cmd buffer"; } @@ -56,8 +58,11 @@ TEST(GPUTracerVK, DoesNotTraceOutsideOfFrameWorkload) { blit_pass->EncodeCommands(context->GetResourceAllocator()); auto latch = std::make_shared(1u); - if (!cmd_buffer->SubmitCommands( - [latch](CommandBuffer::Status status) { latch->CountDown(); })) { + if (!context->GetCommandQueue() + ->Submit( + {cmd_buffer}, + [latch](CommandBuffer::Status status) { latch->CountDown(); }) + .ok()) { GTEST_FAIL() << "Failed to submit cmd buffer"; } @@ -85,8 +90,11 @@ TEST(GPUTracerVK, TracesWithPartialFrameOverlap) { tracer->MarkFrameEnd(); auto latch = std::make_shared(1u); - if (!cmd_buffer->SubmitCommands( - [latch](CommandBuffer::Status status) { latch->CountDown(); })) { + if (!context->GetCommandQueue() + ->Submit( + {cmd_buffer}, + [latch](CommandBuffer::Status status) { latch->CountDown(); }) + .ok()) { GTEST_FAIL() << "Failed to submit cmd buffer"; } diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc index 879e5afd31..f96b4940a4 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc @@ -125,7 +125,7 @@ bool TextureVK::OnSetContents(const uint8_t* contents, } } - return cmd_buffer->SubmitCommands(); + return context->GetCommandQueue()->Submit({cmd_buffer}).ok(); } bool TextureVK::OnSetContents(std::shared_ptr mapping, diff --git a/engine/src/flutter/impeller/renderer/command_buffer.cc b/engine/src/flutter/impeller/renderer/command_buffer.cc index 7a675f560d..5ff1113c10 100644 --- a/engine/src/flutter/impeller/renderer/command_buffer.cc +++ b/engine/src/flutter/impeller/renderer/command_buffer.cc @@ -34,31 +34,6 @@ void CommandBuffer::WaitUntilScheduled() { return OnWaitUntilScheduled(); } -bool CommandBuffer::EncodeAndSubmit( - const std::shared_ptr& render_pass) { - if (!render_pass->IsValid() || !IsValid()) { - return false; - } - if (!render_pass->EncodeCommands()) { - return false; - } - - return SubmitCommands(nullptr); -} - -bool CommandBuffer::EncodeAndSubmit( - const std::shared_ptr& blit_pass, - const std::shared_ptr& allocator) { - if (!blit_pass->IsValid() || !IsValid()) { - return false; - } - if (!blit_pass->EncodeCommands(allocator)) { - return false; - } - - return SubmitCommands(nullptr); -} - std::shared_ptr CommandBuffer::CreateRenderPass( const RenderTarget& render_target) { auto pass = OnCreateRenderPass(render_target); diff --git a/engine/src/flutter/impeller/renderer/command_buffer.h b/engine/src/flutter/impeller/renderer/command_buffer.h index bbc0288cb7..4794941f28 100644 --- a/engine/src/flutter/impeller/renderer/command_buffer.h +++ b/engine/src/flutter/impeller/renderer/command_buffer.h @@ -8,7 +8,6 @@ #include #include -#include "flutter/fml/macros.h" #include "impeller/renderer/blit_pass.h" #include "impeller/renderer/compute_pass.h" @@ -18,6 +17,7 @@ class ComputePass; class Context; class RenderPass; class RenderTarget; +class CommandQueue; namespace testing { class CommandBufferMock; @@ -60,42 +60,6 @@ class CommandBuffer { virtual void SetLabel(const std::string& label) const = 0; - //---------------------------------------------------------------------------- - /// @brief Schedule the command encoded by render passes within this - /// command buffer on the GPU. The encoding of these commnands is - /// performed immediately on the calling thread. - /// - /// A command buffer may only be committed once. - /// - /// @param[in] callback The completion callback. - /// - [[nodiscard]] bool SubmitCommands(const CompletionCallback& callback); - - [[nodiscard]] bool SubmitCommands(); - - //---------------------------------------------------------------------------- - /// @brief Schedule the command encoded by render passes within this - /// command buffer on the GPU. The enqueing of this buffer is - /// performed immediately but encoding is pushed to a worker - /// thread if possible. - /// - /// A command buffer may only be committed once. - /// - [[nodiscard]] virtual bool EncodeAndSubmit( - const std::shared_ptr& render_pass); - - //---------------------------------------------------------------------------- - /// @brief Schedule the command encoded by blit passes within this - /// command buffer on the GPU. The enqueing of this buffer is - /// performed immediately but encoding is pushed to a worker - /// thread if possible. - /// - /// A command buffer may only be committed once. - /// - [[nodiscard]] virtual bool EncodeAndSubmit( - const std::shared_ptr& blit_pass, - const std::shared_ptr& allocator); - //---------------------------------------------------------------------------- /// @brief Force execution of pending GPU commands. /// @@ -143,6 +107,21 @@ class CommandBuffer { virtual std::shared_ptr OnCreateComputePass() = 0; private: + friend class CommandQueue; + + //---------------------------------------------------------------------------- + /// @brief Schedule the command encoded by render passes within this + /// command buffer on the GPU. The encoding of these commnands is + /// performed immediately on the calling thread. + /// + /// A command buffer may only be committed once. + /// + /// @param[in] callback The completion callback. + /// + [[nodiscard]] bool SubmitCommands(const CompletionCallback& callback); + + [[nodiscard]] bool SubmitCommands(); + CommandBuffer(const CommandBuffer&) = delete; CommandBuffer& operator=(const CommandBuffer&) = delete; diff --git a/engine/src/flutter/impeller/renderer/command_queue.cc b/engine/src/flutter/impeller/renderer/command_queue.cc new file mode 100644 index 0000000000..531fa87942 --- /dev/null +++ b/engine/src/flutter/impeller/renderer/command_queue.cc @@ -0,0 +1,33 @@ +// 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 "impeller/renderer/command_queue.h" +#include "impeller/renderer/command_buffer.h" + +namespace impeller { + +CommandQueue::CommandQueue() = default; + +CommandQueue::~CommandQueue() = default; + +fml::Status CommandQueue::Submit( + const std::vector>& buffers, + const CompletionCallback& completion_callback) { + if (buffers.empty()) { + if (completion_callback) { + completion_callback(CommandBuffer::Status::kError); + } + return fml::Status(fml::StatusCode::kInvalidArgument, + "No command buffers provided."); + } + for (const std::shared_ptr& buffer : buffers) { + if (!buffer->SubmitCommands(completion_callback)) { + return fml::Status(fml::StatusCode::kCancelled, + "Failed to submit command buffer."); + } + } + return fml::Status(); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/command_queue.h b/engine/src/flutter/impeller/renderer/command_queue.h new file mode 100644 index 0000000000..140b1c396e --- /dev/null +++ b/engine/src/flutter/impeller/renderer/command_queue.h @@ -0,0 +1,50 @@ +// 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. + +#ifndef FLUTTER_IMPELLER_RENDERER_COMMAND_QUEUE_H_ +#define FLUTTER_IMPELLER_RENDERER_COMMAND_QUEUE_H_ + +#include + +#include "fml/status.h" +#include "impeller/renderer/command_buffer.h" + +namespace impeller { + +/// @brief An interface for submitting command buffers to the GPU for +/// encoding and execution. +class CommandQueue { + public: + using CompletionCallback = std::function; + + CommandQueue(); + + virtual ~CommandQueue(); + + /// @brief Submit one or more command buffer objects to be encoded and + /// executed on the GPU. + /// + /// The order of the provided buffers determines the ordering in which + /// they are submitted. + /// + /// The returned status only indicates if the command buffer was + /// successfully submitted. Successful completion of the command buffer + /// can only be checked in the optional completion callback. + /// + /// Only the Metal and Vulkan backends can give a status beyond + /// successful encoding. This callback may be called more than once and + /// potentially on a different thread. + virtual fml::Status Submit( + const std::vector>& buffers, + const CompletionCallback& completion_callback = {}); + + private: + CommandQueue(const CommandQueue&) = delete; + + CommandQueue& operator=(const CommandQueue&) = delete; +}; + +} // namespace impeller + +#endif // FLUTTER_IMPELLER_RENDERER_COMMAND_QUEUE_H_ diff --git a/engine/src/flutter/impeller/renderer/compute_tessellator.cc b/engine/src/flutter/impeller/renderer/compute_tessellator.cc index f700f3a9fb..8e8d084e7a 100644 --- a/engine/src/flutter/impeller/renderer/compute_tessellator.cc +++ b/engine/src/flutter/impeller/renderer/compute_tessellator.cc @@ -6,6 +6,7 @@ #include +#include "impeller/core/host_buffer.h" #include "impeller/renderer/command_buffer.h" #include "impeller/renderer/path_polyline.comp.h" #include "impeller/renderer/pipeline_library.h" @@ -170,7 +171,7 @@ ComputeTessellator::Status ComputeTessellator::Tessellate( return Status::kCommandInvalid; } - if (!cmd_buffer->SubmitCommands(callback)) { + if (!context->GetCommandQueue()->Submit({cmd_buffer}, callback).ok()) { return Status::kCommandInvalid; } diff --git a/engine/src/flutter/impeller/renderer/compute_unittests.cc b/engine/src/flutter/impeller/renderer/compute_unittests.cc index 8a3cb87694..1147f720ae 100644 --- a/engine/src/flutter/impeller/renderer/compute_unittests.cc +++ b/engine/src/flutter/impeller/renderer/compute_unittests.cc @@ -75,27 +75,33 @@ TEST_P(ComputeTest, CanCreateComputePass) { ASSERT_TRUE(pass->EncodeCommands()); fml::AutoResetWaitableEvent latch; - ASSERT_TRUE(cmd_buffer->SubmitCommands([&latch, output_buffer, &input_0, - &input_1]( - CommandBuffer::Status status) { - EXPECT_EQ(status, CommandBuffer::Status::kCompleted); + ASSERT_TRUE( + context->GetCommandQueue() + ->Submit( + {cmd_buffer}, + [&latch, output_buffer, &input_0, + &input_1](CommandBuffer::Status status) { + EXPECT_EQ(status, CommandBuffer::Status::kCompleted); - auto view = DeviceBuffer::AsBufferView(output_buffer); - EXPECT_EQ(view.range.length, sizeof(CS::Output)); + auto view = DeviceBuffer::AsBufferView(output_buffer); + EXPECT_EQ(view.range.length, sizeof(CS::Output)); - CS::Output* output = - reinterpret_cast*>(output_buffer->OnGetContents()); - EXPECT_TRUE(output); - for (size_t i = 0; i < kCount; i++) { - Vector4 vector = output->elements[i]; - Vector4 computed = input_0.elements[i] * input_1.elements[i]; - EXPECT_EQ(vector, - Vector4(computed.x + 2 + input_1.some_struct.i, - computed.y + 3 + input_1.some_struct.vf.x, - computed.z + 5 + input_1.some_struct.vf.y, computed.w)); - } - latch.Signal(); - })); + CS::Output* output = + reinterpret_cast*>( + output_buffer->OnGetContents()); + EXPECT_TRUE(output); + for (size_t i = 0; i < kCount; i++) { + Vector4 vector = output->elements[i]; + Vector4 computed = input_0.elements[i] * input_1.elements[i]; + EXPECT_EQ(vector, + Vector4(computed.x + 2 + input_1.some_struct.i, + computed.y + 3 + input_1.some_struct.vf.x, + computed.z + 5 + input_1.some_struct.vf.y, + computed.w)); + } + latch.Signal(); + }) + .ok()); latch.Wait(); } @@ -139,24 +145,29 @@ TEST_P(ComputeTest, CanComputePrefixSum) { ASSERT_TRUE(pass->EncodeCommands()); fml::AutoResetWaitableEvent latch; - ASSERT_TRUE(cmd_buffer->SubmitCommands([&latch, output_buffer]( - CommandBuffer::Status status) { - EXPECT_EQ(status, CommandBuffer::Status::kCompleted); + ASSERT_TRUE( + context->GetCommandQueue() + ->Submit({cmd_buffer}, + [&latch, output_buffer](CommandBuffer::Status status) { + EXPECT_EQ(status, CommandBuffer::Status::kCompleted); - auto view = DeviceBuffer::AsBufferView(output_buffer); - EXPECT_EQ(view.range.length, sizeof(CS::OutputData)); + auto view = DeviceBuffer::AsBufferView(output_buffer); + EXPECT_EQ(view.range.length, + sizeof(CS::OutputData)); - CS::OutputData* output = reinterpret_cast*>( - output_buffer->OnGetContents()); - EXPECT_TRUE(output); + CS::OutputData* output = + reinterpret_cast*>( + output_buffer->OnGetContents()); + EXPECT_TRUE(output); - constexpr uint32_t expected[kCount] = {1, 3, 6, 10, 15}; - for (size_t i = 0; i < kCount; i++) { - auto computed_sum = output->data[i]; - EXPECT_EQ(computed_sum, expected[i]); - } - latch.Signal(); - })); + constexpr uint32_t expected[kCount] = {1, 3, 6, 10, 15}; + for (size_t i = 0; i < kCount; i++) { + auto computed_sum = output->data[i]; + EXPECT_EQ(computed_sum, expected[i]); + } + latch.Signal(); + }) + .ok()); latch.Wait(); } @@ -192,19 +203,24 @@ TEST_P(ComputeTest, 1DThreadgroupSizingIsCorrect) { ASSERT_TRUE(pass->EncodeCommands()); fml::AutoResetWaitableEvent latch; - ASSERT_TRUE(cmd_buffer->SubmitCommands([&latch, output_buffer]( - CommandBuffer::Status status) { - EXPECT_EQ(status, CommandBuffer::Status::kCompleted); + ASSERT_TRUE( + context->GetCommandQueue() + ->Submit({cmd_buffer}, + [&latch, output_buffer](CommandBuffer::Status status) { + EXPECT_EQ(status, CommandBuffer::Status::kCompleted); - auto view = DeviceBuffer::AsBufferView(output_buffer); - EXPECT_EQ(view.range.length, sizeof(CS::OutputData)); + auto view = DeviceBuffer::AsBufferView(output_buffer); + EXPECT_EQ(view.range.length, + sizeof(CS::OutputData)); - CS::OutputData* output = reinterpret_cast*>( - output_buffer->OnGetContents()); - EXPECT_TRUE(output); - EXPECT_EQ(output->data[kCount - 1], kCount - 1); - latch.Signal(); - })); + CS::OutputData* output = + reinterpret_cast*>( + output_buffer->OnGetContents()); + EXPECT_TRUE(output); + EXPECT_EQ(output->data[kCount - 1], kCount - 1); + latch.Signal(); + }) + .ok()); latch.Wait(); } @@ -247,7 +263,7 @@ TEST_P(ComputeTest, CanComputePrefixSumLargeInteractive) { pass->Compute(ISize(kCount, 1)); pass->EncodeCommands(); host_buffer->Reset(); - return cmd_buffer->SubmitCommands(); + return context->GetCommandQueue()->Submit({cmd_buffer}).ok(); }; ASSERT_TRUE(OpenPlaygroundHere(callback)); } @@ -322,27 +338,34 @@ TEST_P(ComputeTest, MultiStageInputAndOutput) { ASSERT_TRUE(pass->EncodeCommands()); fml::AutoResetWaitableEvent latch; - ASSERT_TRUE(cmd_buffer->SubmitCommands([&latch, &output_buffer_1, - &output_buffer_2]( - CommandBuffer::Status status) { - EXPECT_EQ(status, CommandBuffer::Status::kCompleted); + ASSERT_TRUE( + context->GetCommandQueue() + ->Submit({cmd_buffer}, + [&latch, &output_buffer_1, + &output_buffer_2](CommandBuffer::Status status) { + EXPECT_EQ(status, CommandBuffer::Status::kCompleted); - CS1::Output* output_1 = reinterpret_cast*>( - output_buffer_1->OnGetContents()); - EXPECT_TRUE(output_1); - EXPECT_EQ(output_1->count, 10u); - EXPECT_THAT(output_1->elements, - ::testing::ElementsAre(0, 0, 2, 3, 4, 6, 6, 9, 8, 12)); + CS1::Output* output_1 = + reinterpret_cast*>( + output_buffer_1->OnGetContents()); + EXPECT_TRUE(output_1); + EXPECT_EQ(output_1->count, 10u); + EXPECT_THAT( + output_1->elements, + ::testing::ElementsAre(0, 0, 2, 3, 4, 6, 6, 9, 8, 12)); - CS2::Output* output_2 = reinterpret_cast*>( - output_buffer_2->OnGetContents()); - EXPECT_TRUE(output_2); - EXPECT_EQ(output_2->count, 10u); - EXPECT_THAT(output_2->elements, - ::testing::ElementsAre(0, 0, 4, 6, 8, 12, 12, 18, 16, 24)); + CS2::Output* output_2 = + reinterpret_cast*>( + output_buffer_2->OnGetContents()); + EXPECT_TRUE(output_2); + EXPECT_EQ(output_2->count, 10u); + EXPECT_THAT(output_2->elements, + ::testing::ElementsAre(0, 0, 4, 6, 8, 12, 12, + 18, 16, 24)); - latch.Signal(); - })); + latch.Signal(); + }) + .ok()); latch.Wait(); } @@ -395,27 +418,33 @@ TEST_P(ComputeTest, CanCompute1DimensionalData) { ASSERT_TRUE(pass->EncodeCommands()); fml::AutoResetWaitableEvent latch; - ASSERT_TRUE(cmd_buffer->SubmitCommands([&latch, output_buffer, &input_0, - &input_1]( - CommandBuffer::Status status) { - EXPECT_EQ(status, CommandBuffer::Status::kCompleted); + ASSERT_TRUE( + context->GetCommandQueue() + ->Submit( + {cmd_buffer}, + [&latch, output_buffer, &input_0, + &input_1](CommandBuffer::Status status) { + EXPECT_EQ(status, CommandBuffer::Status::kCompleted); - auto view = DeviceBuffer::AsBufferView(output_buffer); - EXPECT_EQ(view.range.length, sizeof(CS::Output)); + auto view = DeviceBuffer::AsBufferView(output_buffer); + EXPECT_EQ(view.range.length, sizeof(CS::Output)); - CS::Output* output = - reinterpret_cast*>(output_buffer->OnGetContents()); - EXPECT_TRUE(output); - for (size_t i = 0; i < kCount; i++) { - Vector4 vector = output->elements[i]; - Vector4 computed = input_0.elements[i] * input_1.elements[i]; - EXPECT_EQ(vector, - Vector4(computed.x + 2 + input_1.some_struct.i, - computed.y + 3 + input_1.some_struct.vf.x, - computed.z + 5 + input_1.some_struct.vf.y, computed.w)); - } - latch.Signal(); - })); + CS::Output* output = + reinterpret_cast*>( + output_buffer->OnGetContents()); + EXPECT_TRUE(output); + for (size_t i = 0; i < kCount; i++) { + Vector4 vector = output->elements[i]; + Vector4 computed = input_0.elements[i] * input_1.elements[i]; + EXPECT_EQ(vector, + Vector4(computed.x + 2 + input_1.some_struct.i, + computed.y + 3 + input_1.some_struct.vf.x, + computed.z + 5 + input_1.some_struct.vf.y, + computed.w)); + } + latch.Signal(); + }) + .ok()); latch.Wait(); } diff --git a/engine/src/flutter/impeller/renderer/context.h b/engine/src/flutter/impeller/renderer/context.h index 1f80c296e8..41500a97a1 100644 --- a/engine/src/flutter/impeller/renderer/context.h +++ b/engine/src/flutter/impeller/renderer/context.h @@ -11,9 +11,8 @@ #include "impeller/core/allocator.h" #include "impeller/core/capture.h" #include "impeller/core/formats.h" -#include "impeller/core/host_buffer.h" #include "impeller/renderer/capabilities.h" -#include "impeller/renderer/pool.h" +#include "impeller/renderer/command_queue.h" #include "impeller/renderer/sampler_library.h" namespace impeller { @@ -163,6 +162,9 @@ class Context { /// virtual std::shared_ptr CreateCommandBuffer() const = 0; + /// @brief Return the graphics queue for submitting command buffers. + virtual std::shared_ptr GetCommandQueue() const = 0; + //---------------------------------------------------------------------------- /// @brief Force all pending asynchronous work to finish. This is /// achieved by deleting all owned concurrent message loops. diff --git a/engine/src/flutter/impeller/renderer/renderer_unittests.cc b/engine/src/flutter/impeller/renderer/renderer_unittests.cc index c1cc687096..c5336fb743 100644 --- a/engine/src/flutter/impeller/renderer/renderer_unittests.cc +++ b/engine/src/flutter/impeller/renderer/renderer_unittests.cc @@ -554,7 +554,7 @@ TEST_P(RendererTest, CanBlitTextureToTexture) { pass->EncodeCommands(); } - if (!buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({buffer}).ok()) { return false; } host_buffer->Reset(); @@ -639,10 +639,9 @@ TEST_P(RendererTest, CanBlitTextureToBuffer) { // Blit `bridge` to the top left corner of the texture. pass->AddCopy(bridge, device_buffer); - pass->EncodeCommands(context->GetResourceAllocator()); - if (!buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({buffer}).ok()) { return false; } } @@ -690,7 +689,7 @@ TEST_P(RendererTest, CanBlitTextureToBuffer) { pass->Draw().ok(); } pass->EncodeCommands(); - if (!buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({buffer}).ok()) { return false; } } @@ -812,7 +811,7 @@ TEST_P(RendererTest, CanGenerateMipmaps) { pass->EncodeCommands(); } - if (!buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({buffer}).ok()) { return false; } host_buffer->Reset(); @@ -1257,7 +1256,7 @@ TEST_P(RendererTest, StencilMask) { pass->EncodeCommands(); } - if (!buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({buffer}).ok()) { return false; } host_buffer->Reset(); diff --git a/engine/src/flutter/impeller/renderer/testing/mocks.h b/engine/src/flutter/impeller/renderer/testing/mocks.h index 7e7ac5dbe2..78b1d388ea 100644 --- a/engine/src/flutter/impeller/renderer/testing/mocks.h +++ b/engine/src/flutter/impeller/renderer/testing/mocks.h @@ -10,6 +10,7 @@ #include "impeller/core/sampler_descriptor.h" #include "impeller/core/texture.h" #include "impeller/renderer/command_buffer.h" +#include "impeller/renderer/command_queue.h" #include "impeller/renderer/context.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/render_target.h" @@ -164,6 +165,11 @@ class MockImpellerContext : public Context { GetCapabilities, (), (const, override)); + + MOCK_METHOD(std::shared_ptr, + GetCommandQueue, + (), + (const, override)); }; class MockTexture : public Texture { @@ -200,6 +206,15 @@ class MockCapabilities : public Capabilities { MOCK_METHOD(PixelFormat, GetDefaultDepthStencilFormat, (), (const, override)); }; +class MockCommandQueue : public CommandQueue { + public: + MOCK_METHOD(fml::Status, + Submit, + (const std::vector>& buffers, + const CompletionCallback& cb), + (override)); +}; + class MockSamplerLibrary : public SamplerLibrary { public: MOCK_METHOD(const std::unique_ptr&, diff --git a/engine/src/flutter/impeller/scene/scene.cc b/engine/src/flutter/impeller/scene/scene.cc index 172e1c1e4a..4e5e656120 100644 --- a/engine/src/flutter/impeller/scene/scene.cc +++ b/engine/src/flutter/impeller/scene/scene.cc @@ -53,7 +53,10 @@ bool Scene::Render(const RenderTarget& render_target, // TODO(bdero): Do post processing. - if (!command_buffer->SubmitCommands()) { + if (!scene_context_->GetContext() + ->GetCommandQueue() + ->Submit({command_buffer}) + .ok()) { FML_LOG(ERROR) << "Failed to submit command buffer."; return false; } diff --git a/engine/src/flutter/lib/gpu/command_buffer.cc b/engine/src/flutter/lib/gpu/command_buffer.cc index f0dd7e0234..b2f8fcd6c5 100644 --- a/engine/src/flutter/lib/gpu/command_buffer.cc +++ b/engine/src/flutter/lib/gpu/command_buffer.cc @@ -17,8 +17,10 @@ namespace gpu { IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, CommandBuffer); CommandBuffer::CommandBuffer( + std::shared_ptr context, std::shared_ptr command_buffer) - : command_buffer_(std::move(command_buffer)) {} + : context_(std::move(context)), + command_buffer_(std::move(command_buffer)) {} CommandBuffer::~CommandBuffer() = default; @@ -35,7 +37,8 @@ bool CommandBuffer::Submit() { for (auto& encodable : encodables_) { encodable->EncodeCommands(); } - return command_buffer_->SubmitCommands(); + + return context_->GetCommandQueue()->Submit({command_buffer_}).ok(); } bool CommandBuffer::Submit( @@ -43,7 +46,9 @@ bool CommandBuffer::Submit( for (auto& encodable : encodables_) { encodable->EncodeCommands(); } - return command_buffer_->SubmitCommands(completion_callback); + return context_->GetCommandQueue() + ->Submit({command_buffer_}, completion_callback) + .ok(); } } // namespace gpu @@ -57,6 +62,7 @@ bool InternalFlutterGpu_CommandBuffer_Initialize( Dart_Handle wrapper, flutter::gpu::Context* contextWrapper) { auto res = fml::MakeRefCounted( + contextWrapper->GetContext(), contextWrapper->GetContext()->CreateCommandBuffer()); res->AssociateWithDartWrapper(wrapper); diff --git a/engine/src/flutter/lib/gpu/command_buffer.h b/engine/src/flutter/lib/gpu/command_buffer.h index 2125d75cc1..7e4e3c0198 100644 --- a/engine/src/flutter/lib/gpu/command_buffer.h +++ b/engine/src/flutter/lib/gpu/command_buffer.h @@ -18,8 +18,8 @@ class CommandBuffer : public RefCountedDartWrappable { FML_FRIEND_MAKE_REF_COUNTED(CommandBuffer); public: - explicit CommandBuffer( - std::shared_ptr command_buffer); + CommandBuffer(std::shared_ptr context, + std::shared_ptr command_buffer); std::shared_ptr GetCommandBuffer(); @@ -32,6 +32,7 @@ class CommandBuffer : public RefCountedDartWrappable { ~CommandBuffer() override; private: + std::shared_ptr context_; std::shared_ptr command_buffer_; std::vector> encodables_; 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 e577b7ac73..3ef8a8de53 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc @@ -340,7 +340,7 @@ static std::pair, std::string> UnsafeUploadTextureToPrivate( } blit_pass->EncodeCommands(context->GetResourceAllocator()); - if (!command_buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) { std::string decode_error("Failed to submit blit pass command buffer."); FML_DLOG(ERROR) << decode_error; return std::make_pair(nullptr, decode_error); @@ -459,7 +459,7 @@ ImageDecoderImpeller::UploadTextureToStorage( blit_pass->GenerateMipmap(texture); blit_pass->EncodeCommands(context->GetResourceAllocator()); - if (!command_buffer->SubmitCommands()) { + if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) { decode_error = "Failed to submit blit pass command buffer."; return; } diff --git a/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc b/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc index 430dd9c48a..210cab29ee 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc @@ -23,6 +23,8 @@ #include "flutter/testing/test_dart_native_resolver.h" #include "flutter/testing/test_gl_surface.h" #include "flutter/testing/testing.h" +#include "fml/logging.h" +#include "impeller/renderer/command_queue.h" #include "third_party/skia/include/codec/SkCodecAnimation.h" #include "third_party/skia/include/core/SkData.h" #include "third_party/skia/include/core/SkImage.h" @@ -65,6 +67,10 @@ class TestImpellerContext : public impeller::Context { return nullptr; } + std::shared_ptr GetCommandQueue() const override { + FML_UNREACHABLE(); + } + std::shared_ptr CreateCommandBuffer() const override { command_buffer_count_ += 1; return nullptr; diff --git a/engine/src/flutter/lib/ui/painting/image_encoding_impeller.cc b/engine/src/flutter/lib/ui/painting/image_encoding_impeller.cc index a0b61da066..e2e7cf9e96 100644 --- a/engine/src/flutter/lib/ui/painting/image_encoding_impeller.cc +++ b/engine/src/flutter/lib/ui/painting/image_encoding_impeller.cc @@ -178,7 +178,9 @@ void ImageEncodingImpeller::ConvertDlImageToSkImage( encode_task(sk_image); }; - if (!command_buffer->SubmitCommands(completion)) { + if (!impeller_context->GetCommandQueue() + ->Submit({command_buffer}, completion) + .ok()) { FML_LOG(ERROR) << "Failed to submit commands."; } } diff --git a/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc b/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc index 8de593ce4b..b035e59631 100644 --- a/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc +++ b/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc @@ -193,11 +193,13 @@ TEST_F(ShellTest, EncodeImageAccessesSyncSwitch) { using ::impeller::testing::MockAllocator; using ::impeller::testing::MockBlitPass; using ::impeller::testing::MockCommandBuffer; +using ::impeller::testing::MockCommandQueue; using ::impeller::testing::MockDeviceBuffer; using ::impeller::testing::MockImpellerContext; using ::impeller::testing::MockTexture; using ::testing::_; using ::testing::DoAll; +using ::testing::Invoke; using ::testing::InvokeArgument; using ::testing::Return; @@ -208,6 +210,7 @@ std::shared_ptr MakeConvertDlImageToSkImageContext( auto command_buffer = std::make_shared(context); auto allocator = std::make_shared(); auto blit_pass = std::make_shared(); + auto command_queue = std::make_shared(); impeller::DeviceBufferDescriptor device_buffer_desc; device_buffer_desc.size = buffer.size(); auto device_buffer = std::make_shared(device_buffer_desc); @@ -215,13 +218,14 @@ std::shared_ptr MakeConvertDlImageToSkImageContext( EXPECT_CALL(*blit_pass, IsValid).WillRepeatedly(Return(true)); EXPECT_CALL(*command_buffer, IsValid).WillRepeatedly(Return(true)); EXPECT_CALL(*command_buffer, OnCreateBlitPass).WillOnce(Return(blit_pass)); - EXPECT_CALL(*command_buffer, OnSubmitCommands(_)) - .WillOnce( - DoAll(InvokeArgument<0>(impeller::CommandBuffer::Status::kCompleted), - Return(true))); EXPECT_CALL(*context, GetResourceAllocator).WillRepeatedly(Return(allocator)); EXPECT_CALL(*context, CreateCommandBuffer).WillOnce(Return(command_buffer)); EXPECT_CALL(*device_buffer, OnGetContents).WillOnce(Return(buffer.data())); + EXPECT_CALL(*command_queue, Submit(_, _)) + .WillRepeatedly( + DoAll(InvokeArgument<1>(impeller::CommandBuffer::Status::kCompleted), + Return(fml::Status()))); + EXPECT_CALL(*context, GetCommandQueue).WillRepeatedly(Return(command_queue)); return context; } } // namespace diff --git a/engine/src/flutter/shell/common/rasterizer.cc b/engine/src/flutter/shell/common/rasterizer.cc index d9f3a9e8a1..8d6edce8d9 100644 --- a/engine/src/flutter/shell/common/rasterizer.cc +++ b/engine/src/flutter/shell/common/rasterizer.cc @@ -911,7 +911,9 @@ ScreenshotLayerTreeAsImageImpeller( sk_data = SkData::MakeWithCopy(buffer->OnGetContents(), buffer_desc.size); }; - if (!command_buffer->SubmitCommands(completion)) { + if (!impeller_context->GetCommandQueue() + ->Submit({command_buffer}, completion) + .ok()) { FML_LOG(ERROR) << "Failed to submit commands."; } latch.Wait(); diff --git a/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc b/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc index 859a1d3100..ec6690b912 100644 --- a/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc +++ b/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc @@ -74,6 +74,10 @@ class TestImpellerContext : public impeller::Context { FML_UNREACHABLE(); } + std::shared_ptr GetCommandQueue() const override { + FML_UNREACHABLE(); + } + void Shutdown() override { did_shutdown = true; } bool did_shutdown = false; diff --git a/engine/src/flutter/shell/platform/android/image_external_texture_vk.cc b/engine/src/flutter/shell/platform/android/image_external_texture_vk.cc index 714b9929de..8d2346e695 100644 --- a/engine/src/flutter/shell/platform/android/image_external_texture_vk.cc +++ b/engine/src/flutter/shell/platform/android/image_external_texture_vk.cc @@ -82,7 +82,7 @@ void ImageExternalTextureVK::ProcessFrame(PaintContext& context, if (!texture->SetLayout(barrier)) { return; } - if (!buffer->SubmitCommands()) { + if (!impeller_context_->GetCommandQueue()->Submit({buffer}).ok()) { return; } }