From b515ff55b91ed9f1d1e5b2f41403877314964a1a Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Thu, 11 Aug 2022 17:20:05 -0700 Subject: [PATCH] [Impeller] [vulkan] Add support for wrapped surfaces (flutter/engine#35359) --- .../backend/vulkan/playground_impl_vk.cc | 8 +-- .../backend/vulkan/playground_impl_vk.h | 3 -- .../renderer/backend/vulkan/allocator_vk.cc | 13 ++++- .../renderer/backend/vulkan/context_vk.cc | 24 ++++----- .../renderer/backend/vulkan/context_vk.h | 3 +- .../backend/vulkan/surface_producer_vk.cc | 18 +++++-- .../backend/vulkan/surface_producer_vk.h | 6 ++- .../renderer/backend/vulkan/surface_vk.cc | 29 ++++++++--- .../renderer/backend/vulkan/surface_vk.h | 1 + .../renderer/backend/vulkan/swapchain_vk.cc | 4 ++ .../renderer/backend/vulkan/swapchain_vk.h | 2 + .../renderer/backend/vulkan/texture_vk.cc | 49 +++++++++++++------ .../renderer/backend/vulkan/texture_vk.h | 43 ++++++++++++---- 13 files changed, 144 insertions(+), 59 deletions(-) diff --git a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc index 958f5f2531..f154f5326f 100644 --- a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc +++ b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc @@ -85,9 +85,11 @@ PlaygroundImplVK::PlaygroundImplVK() void PlaygroundImplVK::SetupSwapchain() { ContextVK* context_vk = reinterpret_cast(context_.get()); auto window = reinterpret_cast(handle_.get()); - ::glfwCreateWindowSurface(context_vk->GetInstance(), window, nullptr, - &surface_); - context_vk->SetupSwapchain(surface_); + vk::Instance instance = context_vk->GetInstance(); + VkSurfaceKHR surface_tmp; + ::glfwCreateWindowSurface(instance, window, nullptr, &surface_tmp); + vk::UniqueSurfaceKHR surface{surface_tmp, instance}; + context_vk->SetupSwapchain(std::move(surface)); } PlaygroundImplVK::~PlaygroundImplVK() = default; diff --git a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h index 693bc38763..ec5d6c6421 100644 --- a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h +++ b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h @@ -27,9 +27,6 @@ class PlaygroundImplVK final : public PlaygroundImpl { using UniqueHandle = std::unique_ptr; UniqueHandle handle_; - // surface - VkSurfaceKHR surface_; - // |PlaygroundImpl| std::shared_ptr GetContext() const override; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc index ade9dd215f..0d6304c3ee 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -102,8 +102,17 @@ std::shared_ptr AllocatorVK::CreateTexture( return nullptr; } - return std::make_shared(desc, context_, allocator_, img, - allocation, allocation_info); + auto texture_info = std::make_unique(TextureInfoVK{ + .backing_type = TextureBackingTypeVK::kAllocatedTexture, + .allocated_texture = + { + .allocator = &allocator_, + .allocation = allocation, + .allocation_info = allocation_info, + .image = img, + }, + }); + return std::make_shared(desc, &context_, std::move(texture_info)); } // |Allocator| 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 27a15e2932..6c3c0b3e0e 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc @@ -469,8 +469,9 @@ vk::Instance ContextVK::GetInstance() const { return *instance_; } -void ContextVK::SetupSwapchain(vk::SurfaceKHR surface) { - auto present_queue_out = PickPresentQueue(physical_device_, surface); +void ContextVK::SetupSwapchain(vk::UniqueSurfaceKHR surface) { + surface_ = std::move(surface); + auto present_queue_out = PickPresentQueue(physical_device_, *surface); if (!present_queue_out.has_value()) { return; } @@ -478,18 +479,19 @@ void ContextVK::SetupSwapchain(vk::SurfaceKHR surface) { device_->getQueue(present_queue_out->family, present_queue_out->index); auto swapchain_details = - SwapchainDetailsVK::Create(physical_device_, surface); + SwapchainDetailsVK::Create(physical_device_, *surface); if (!swapchain_details) { return; } - swapchain_ = SwapchainVK::Create(*device_, surface, *swapchain_details); - - surface_producer_ = SurfaceProducerVK::Create({ - .device = *device_, - .graphics_queue = graphics_queue_, - .present_queue = present_queue_, - .swapchain = swapchain_.get(), - }); + swapchain_ = SwapchainVK::Create(*device_, *surface, *swapchain_details); + auto weak_this = weak_from_this(); + surface_producer_ = SurfaceProducerVK::Create( + weak_this, { + .device = *device_, + .graphics_queue = graphics_queue_, + .present_queue = present_queue_, + .swapchain = swapchain_.get(), + }); } } // 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 eadc810d44..1741c67827 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h @@ -59,7 +59,7 @@ class ContextVK final : public Context, public BackendCast { vk::Instance GetInstance() const; - void SetupSwapchain(vk::SurfaceKHR surface); + void SetupSwapchain(vk::UniqueSurfaceKHR surface); private: std::shared_ptr worker_task_runner_; @@ -75,6 +75,7 @@ class ContextVK final : public Context, public BackendCast { vk::Queue compute_queue_; vk::Queue transfer_queue_; vk::Queue present_queue_; + vk::UniqueSurfaceKHR surface_; std::unique_ptr swapchain_; std::unique_ptr graphics_command_pool_; std::unique_ptr surface_producer_; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.cc index d43d9dee66..d0fad4b56c 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.cc @@ -12,8 +12,10 @@ namespace impeller { std::unique_ptr SurfaceProducerVK::Create( + std::weak_ptr context, const SurfaceProducerCreateInfoVK& create_info) { - auto surface_producer = std::make_unique(create_info); + auto surface_producer = + std::make_unique(context, create_info); if (!surface_producer->SetupSyncObjects()) { FML_LOG(ERROR) << "Failed to setup sync objects."; return nullptr; @@ -23,8 +25,9 @@ std::unique_ptr SurfaceProducerVK::Create( } SurfaceProducerVK::SurfaceProducerVK( + std::weak_ptr context, const SurfaceProducerCreateInfoVK& create_info) - : create_info_(create_info) {} + : context_(context), create_info_(create_info) {} SurfaceProducerVK::~SurfaceProducerVK() = default; @@ -63,9 +66,14 @@ std::unique_ptr SurfaceProducerVK::AcquireSurface( return Present(image_index); }; - return SurfaceVK::WrapSwapchainImage( - create_info_.swapchain->GetSwapchainImage(image_index), - std::move(swap_callback)); + if (auto context = context_.lock()) { + ContextVK* context_vk = reinterpret_cast(context.get()); + return SurfaceVK::WrapSwapchainImage( + create_info_.swapchain->GetSwapchainImage(image_index), context_vk, + std::move(swap_callback)); + } else { + return nullptr; + } } bool SurfaceProducerVK::SetupSyncObjects() { diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.h index 96ae70d17a..6d3ed2e116 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_producer_vk.h @@ -23,15 +23,19 @@ struct SurfaceProducerCreateInfoVK { class SurfaceProducerVK { public: static std::unique_ptr Create( + std::weak_ptr context, const SurfaceProducerCreateInfoVK& create_info); - explicit SurfaceProducerVK(const SurfaceProducerCreateInfoVK& create_info); + SurfaceProducerVK(std::weak_ptr context, + const SurfaceProducerCreateInfoVK& create_info); ~SurfaceProducerVK(); std::unique_ptr AcquireSurface(vk::CommandBuffer command_buffer); private: + std::weak_ptr context_; + bool SetupSyncObjects(); bool Submit(vk::CommandBuffer buffer); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.cc index b3c766d36d..f87a36aeb9 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.cc @@ -4,9 +4,12 @@ #include "impeller/renderer/backend/vulkan/surface_vk.h" +#include "impeller/renderer/backend/vulkan/texture_vk.h" + namespace impeller { std::unique_ptr SurfaceVK::WrapSwapchainImage( SwapchainImageVK* swapchain_image, + ContextVK* context, SwapCallback swap_callback) { if (!swapchain_image) { return nullptr; @@ -20,10 +23,15 @@ std::unique_ptr SurfaceVK::WrapSwapchainImage( color0_tex.sample_count = SampleCount::kCount1; ColorAttachment color0; - // TODO (kaushikiska): this needs to be fixed. - // color0.texture = std::make_shared( - // gl_context.GetReactor(), std::move(color0_tex), - // TextureGLES::IsWrapped::kWrapped); + auto color_texture_info = std::make_unique(TextureInfoVK{ + .backing_type = TextureBackingTypeVK::kWrappedTexture, + .wrapped_texture = + { + .swapchain_image = swapchain_image, + }, + }); + color0.texture = std::make_shared(std::move(color0_tex), context, + std::move(color_texture_info)); color0.clear_color = Color::DarkSlateGray(); color0.load_action = LoadAction::kClear; color0.store_action = StoreAction::kStore; @@ -38,10 +46,15 @@ std::unique_ptr SurfaceVK::WrapSwapchainImage( StencilAttachment stencil0; stencil0.clear_stencil = 0; - // TODO (kaushikiska): this needs to be fixed. - // stencil0.texture = std::make_shared( - // gl_context.GetReactor(), std::move(stencil0_tex), - // TextureGLES::IsWrapped::kWrapped); + auto stencil_texture_info = std::make_unique(TextureInfoVK{ + .backing_type = TextureBackingTypeVK::kWrappedTexture, + .wrapped_texture = + { + .swapchain_image = swapchain_image, + }, + }); + stencil0.texture = std::make_shared( + std::move(stencil0_tex), context, std::move(stencil_texture_info)); stencil0.load_action = LoadAction::kClear; stencil0.store_action = StoreAction::kDontCare; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.h index 2e9403dd0c..ac6797e6af 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/surface_vk.h @@ -19,6 +19,7 @@ class SurfaceVK final : public Surface { static std::unique_ptr WrapSwapchainImage( SwapchainImageVK* swapchain_image, + ContextVK* context, SwapCallback swap_callback); SurfaceVK(RenderTarget target, diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.cc index b57cfee1ec..df88759c74 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.cc @@ -138,6 +138,10 @@ ISize SwapchainImageVK::GetSize() const { return ISize(extent_.width, extent_.height); } +vk::Image SwapchainImageVK::GetImage() const { + return image_; +} + SwapchainImageVK::~SwapchainImageVK() = default; } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.h index 265eaf9fde..c57a8800d0 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain_vk.h @@ -26,6 +26,8 @@ class SwapchainImageVK { ISize GetSize() const; + vk::Image GetImage() const; + private: vk::Image image_; vk::UniqueImageView image_view_; 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 8d46611667..9a6950ee7b 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc @@ -7,32 +7,32 @@ namespace impeller { TextureVK::TextureVK(TextureDescriptor desc, - ContextVK& context, - const VmaAllocator& allocator, - VkImage image, - VmaAllocation allocation, - VmaAllocationInfo allocation_info) + ContextVK* context, + std::unique_ptr texture_info) : Texture(desc), context_(context), - allocator_(allocator), - image_(image), - allocation_(allocation), - allocation_info_(allocation_info) {} + texture_info_(std::move(texture_info)) {} TextureVK::~TextureVK() { - if (image_) { - vmaDestroyImage(allocator_, image_, allocation_); + if (!IsWrapped() && IsValid()) { + const auto& texture = texture_info_->allocated_texture; + vmaDestroyImage(*texture.allocator, texture.image, texture.allocation); } } void TextureVK::SetLabel(std::string_view label) { - context_.SetDebugName(vk::Image{image_}, label); + context_->SetDebugName(GetImage(), label); } bool TextureVK::OnSetContents(const uint8_t* contents, size_t length, size_t slice) { - if (!image_ || !contents) { + if (IsWrapped()) { + FML_LOG(ERROR) << "Cannot set contents of a wrapped texture"; + return false; + } + + if (!IsValid() || !contents) { return false; } @@ -45,7 +45,8 @@ bool TextureVK::OnSetContents(const uint8_t* contents, } // currently we are only supporting 2d textures, no cube textures etc. - memcpy(allocation_info_.pMappedData, contents, length); + auto mapping = texture_info_->allocated_texture.allocation_info.pMappedData; + memcpy(mapping, contents, length); return true; } @@ -58,11 +59,29 @@ bool TextureVK::OnSetContents(std::shared_ptr mapping, } bool TextureVK::IsValid() const { - return image_; + switch (texture_info_->backing_type) { + case TextureBackingTypeVK::kAllocatedTexture: + return texture_info_->allocated_texture.image; + case TextureBackingTypeVK::kWrappedTexture: + return texture_info_->wrapped_texture.swapchain_image; + } } ISize TextureVK::GetSize() const { return GetTextureDescriptor().size; } +bool TextureVK::IsWrapped() const { + return texture_info_->backing_type == TextureBackingTypeVK::kWrappedTexture; +} + +vk::Image TextureVK::GetImage() const { + switch (texture_info_->backing_type) { + case TextureBackingTypeVK::kAllocatedTexture: + return texture_info_->allocated_texture.image; + case TextureBackingTypeVK::kWrappedTexture: + return texture_info_->wrapped_texture.swapchain_image->GetImage(); + } +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h index 0bc53f7dc6..e3e667c195 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h @@ -7,29 +7,52 @@ #include "flutter/fml/macros.h" #include "impeller/base/backend_cast.h" #include "impeller/renderer/backend/vulkan/context_vk.h" +#include "impeller/renderer/backend/vulkan/swapchain_vk.h" #include "impeller/renderer/backend/vulkan/vk.h" #include "impeller/renderer/texture.h" namespace impeller { +enum class TextureBackingTypeVK { + kAllocatedTexture, + kWrappedTexture, +}; + +struct WrappedTextureInfoVK { + SwapchainImageVK* swapchain_image = nullptr; +}; + +struct AllocatedTextureInfoVK { + VmaAllocator* allocator = nullptr; + VmaAllocation allocation = nullptr; + VmaAllocationInfo allocation_info = {}; + VkImage image = nullptr; +}; + +struct TextureInfoVK { + TextureBackingTypeVK backing_type; + union { + WrappedTextureInfoVK wrapped_texture; + AllocatedTextureInfoVK allocated_texture; + }; +}; + class TextureVK final : public Texture, public BackendCast { public: TextureVK(TextureDescriptor desc, - ContextVK& context, - const VmaAllocator& allocator, - VkImage image, - VmaAllocation allocation, - VmaAllocationInfo allocation_info); + ContextVK* context, + std::unique_ptr texture_info); // |Texture| ~TextureVK() override; + bool IsWrapped() const; + private: - ContextVK& context_; - const VmaAllocator& allocator_; - VkImage image_; - VmaAllocation allocation_; - VmaAllocationInfo allocation_info_; + ContextVK* context_; + std::unique_ptr texture_info_; + + vk::Image GetImage() const; // |Texture| void SetLabel(std::string_view label) override;