diff --git a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc index 0212d0cd82d24..b50972e4060a8 100644 --- a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc +++ b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc @@ -44,9 +44,11 @@ std::shared_ptr AHBSwapchainImplVK::Create( const std::weak_ptr& context, std::weak_ptr surface_control, const ISize& size, - bool enable_msaa) { - auto impl = std::shared_ptr(new AHBSwapchainImplVK( - context, std::move(surface_control), size, enable_msaa)); + bool enable_msaa, + size_t swapchain_image_count) { + auto impl = std::shared_ptr( + new AHBSwapchainImplVK(context, std::move(surface_control), size, + enable_msaa, swapchain_image_count)); return impl->IsValid() ? impl : nullptr; } @@ -54,11 +56,13 @@ AHBSwapchainImplVK::AHBSwapchainImplVK( const std::weak_ptr& context, std::weak_ptr surface_control, const ISize& size, - bool enable_msaa) + bool enable_msaa, + size_t swapchain_image_count) : surface_control_(std::move(surface_control)), pending_presents_(std::make_shared(kMaxPendingPresents)) { desc_ = android::HardwareBufferDescriptor::MakeForSwapchainImage(size); - pool_ = std::make_shared(context, desc_); + pool_ = + std::make_shared(context, desc_, swapchain_image_count); if (!pool_->IsValid()) { return; } @@ -245,7 +249,6 @@ vk::UniqueSemaphore AHBSwapchainImplVK::CreateRenderReadySemaphore( } const auto& context_vk = ContextVK::Cast(*context); - const auto& device = context_vk.GetDevice(); auto signal_wait = device.createSemaphoreUnique({}); diff --git a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h index f865efc7001d9..72979cb1682da 100644 --- a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h +++ b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h @@ -51,7 +51,8 @@ class AHBSwapchainImplVK final const std::weak_ptr& context, std::weak_ptr surface_control, const ISize& size, - bool enable_msaa); + bool enable_msaa, + size_t swapchain_image_count); ~AHBSwapchainImplVK(); @@ -107,7 +108,8 @@ class AHBSwapchainImplVK final const std::weak_ptr& context, std::weak_ptr surface_control, const ISize& size, - bool enable_msaa); + bool enable_msaa, + size_t swapchain_image_count); bool Present(const AutoSemaSignaler& signaler, const std::shared_ptr& texture); diff --git a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc index 2532a36ef2483..ad805a0a8cd72 100644 --- a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc +++ b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc @@ -5,8 +5,10 @@ #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h" #include "flutter/fml/trace_event.h" +#include "impeller/renderer/backend/vulkan/context_vk.h" #include "impeller/renderer/backend/vulkan/formats_vk.h" #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_formats.h" +#include "third_party/vulkan-deps/vulkan-headers/src/include/vulkan/vulkan_enums.hpp" namespace impeller { @@ -17,12 +19,26 @@ bool AHBSwapchainVK::IsAvailableOnPlatform() { AHBSwapchainVK::AHBSwapchainVK(const std::shared_ptr& context, ANativeWindow* window, + vk::UniqueSurfaceKHR surface, const ISize& size, bool enable_msaa) : context_(context), surface_control_( std::make_shared(window, "ImpellerSurface")), enable_msaa_(enable_msaa) { + const auto [caps_result, surface_caps] = + ContextVK::Cast(*context).GetPhysicalDevice().getSurfaceCapabilitiesKHR( + *surface); + if (caps_result == vk::Result::eSuccess) { + swapchain_image_count_ = + std::clamp(surface_caps.minImageCount + 1u, // preferred image count + surface_caps.minImageCount, // min count cannot be zero + surface_caps.maxImageCount == 0u + ? surface_caps.minImageCount + 1u + : surface_caps.maxImageCount // max zero means no limit + ); + } + UpdateSurfaceSize(size); } @@ -56,10 +72,11 @@ void AHBSwapchainVK::UpdateSurfaceSize(const ISize& size) { return; } TRACE_EVENT0("impeller", __FUNCTION__); - auto impl = AHBSwapchainImplVK::Create(context_, // - surface_control_, // - size, // - enable_msaa_ // + auto impl = AHBSwapchainImplVK::Create(context_, // + surface_control_, // + size, // + enable_msaa_, // + swapchain_image_count_ // ); if (!impl || !impl->IsValid()) { VALIDATION_LOG << "Could not resize swapchain to size: " << size; diff --git a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h index b95e1c2e9287f..a3b330f71cfc5 100644 --- a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h +++ b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h @@ -52,10 +52,12 @@ class AHBSwapchainVK final : public SwapchainVK { std::weak_ptr context_; std::shared_ptr surface_control_; const bool enable_msaa_; + size_t swapchain_image_count_ = 3u; std::shared_ptr impl_; explicit AHBSwapchainVK(const std::shared_ptr& context, ANativeWindow* window, + vk::UniqueSurfaceKHR surface, const ISize& size, bool enable_msaa); }; diff --git a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h index 19429adedcd0c..f6d38408d4a4b 100644 --- a/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h +++ b/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h @@ -9,7 +9,6 @@ #include "flutter/fml/unique_fd.h" #include "impeller/base/thread.h" -#include "impeller/base/timing.h" #include "impeller/renderer/backend/vulkan/android/ahb_texture_source_vk.h" namespace impeller { @@ -30,14 +29,12 @@ namespace impeller { class AHBTexturePoolVK { public: struct PoolEntry { - TimePoint last_access_time; std::shared_ptr texture; std::shared_ptr render_ready_fence; explicit PoolEntry(std::shared_ptr p_item, fml::UniqueFD p_render_ready_fence = {}) - : last_access_time(Clock::now()), - texture(std::move(p_item)), + : texture(std::move(p_item)), render_ready_fence(std::make_shared( std::move(p_render_ready_fence))) {} diff --git a/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc b/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc index 70e9553d454df..a777e66a943f5 100644 --- a/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc +++ b/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc @@ -44,6 +44,17 @@ std::shared_ptr SwapchainVK::Create( return nullptr; } + vk::AndroidSurfaceCreateInfoKHR surface_info; + surface_info.setWindow(window.GetHandle()); + auto [result, surface] = + ContextVK::Cast(*context).GetInstance().createAndroidSurfaceKHRUnique( + surface_info); + if (result != vk::Result::eSuccess) { + VALIDATION_LOG << "Could not create KHR Android Surface: " + << vk::to_string(result); + return nullptr; + } + // TODO(147533): AHB swapchains on emulators are not functional. const auto emulator = ContextVK::Cast(*context).GetDriverInfo()->IsEmulator(); @@ -52,6 +63,7 @@ std::shared_ptr SwapchainVK::Create( auto ahb_swapchain = std::shared_ptr(new AHBSwapchainVK( context, // window.GetHandle(), // + std::move(surface), // window.GetSize(), // enable_msaa // )); @@ -65,16 +77,6 @@ std::shared_ptr SwapchainVK::Create( } // Fallback to KHR swapchains if AHB swapchains aren't available. - vk::AndroidSurfaceCreateInfoKHR surface_info; - surface_info.setWindow(window.GetHandle()); - auto [result, surface] = - ContextVK::Cast(*context).GetInstance().createAndroidSurfaceKHRUnique( - surface_info); - if (result != vk::Result::eSuccess) { - VALIDATION_LOG << "Could not create KHR Android Surface: " - << vk::to_string(result); - return nullptr; - } return Create(context, std::move(surface), window.GetSize(), enable_msaa); } #endif // FML_OS_ANDROID