| Flutter Impeller |

ahb_swapchain_vk.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h"
6
7 #include "flutter/fml/trace_event.h"
8 #include "impeller/renderer/backend/vulkan/context_vk.h"
9 #include "impeller/renderer/backend/vulkan/formats_vk.h"
10 #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_formats.h"
11 #include "third_party/vulkan-deps/vulkan-headers/src/include/vulkan/vulkan_enums.hpp"
12
13 namespace impeller {
14
15 bool AHBSwapchainVK::IsAvailableOnPlatform() {
16 return android::SurfaceControl::IsAvailableOnPlatform() &&
17 android::HardwareBuffer::IsAvailableOnPlatform();
18 }
19
20 AHBSwapchainVK::AHBSwapchainVK(const std::shared_ptr<Context>& context,
21 ANativeWindow* window,
22 const vk::UniqueSurfaceKHR& surface,
23 const ISize& size,
24 bool enable_msaa)
25 : context_(context),
26 surface_control_(
27 std::make_shared<android::SurfaceControl>(window, "ImpellerSurface")),
28 enable_msaa_(enable_msaa) {
29 const auto [caps_result, surface_caps] =
30 ContextVK::Cast(*context).GetPhysicalDevice().getSurfaceCapabilitiesKHR(
31 *surface);
32 if (caps_result == vk::Result::eSuccess) {
33 swapchain_image_count_ =
34 std::clamp(surface_caps.minImageCount + 1u, // preferred image count
35 surface_caps.minImageCount, // min count cannot be zero
36 surface_caps.maxImageCount == 0u
37 ? surface_caps.minImageCount + 1u
38 : surface_caps.maxImageCount // max zero means no limit
39 );
40 }
41
42 UpdateSurfaceSize(size);
43 }
44
45 AHBSwapchainVK::~AHBSwapchainVK() = default;
46
47 // |SwapchainVK|
48 bool AHBSwapchainVK::IsValid() const {
49 return impl_ ? impl_->IsValid() : false;
50 }
51
52 // |SwapchainVK|
53 std::unique_ptr<Surface> AHBSwapchainVK::AcquireNextDrawable() {
54 if (!IsValid()) {
55 return nullptr;
56 }
57
58 TRACE_EVENT0("impeller", __FUNCTION__);
59 return impl_->AcquireNextDrawable();
60 }
61
62 // |SwapchainVK|
63 vk::Format AHBSwapchainVK::GetSurfaceFormat() const {
64 return IsValid()
65 ? ToVKImageFormat(ToPixelFormat(impl_->GetDescriptor().format))
66 : vk::Format::eUndefined;
67 }
68
69 // |SwapchainVK|
70 void AHBSwapchainVK::AddFinalCommandBuffer(
71 std::shared_ptr<CommandBuffer> cmd_buffer) const {
72 return impl_->AddFinalCommandBuffer(cmd_buffer);
73 }
74
75 // |SwapchainVK|
76 void AHBSwapchainVK::UpdateSurfaceSize(const ISize& size) {
77 if (impl_ && impl_->GetSize() == size) {
78 return;
79 }
80 TRACE_EVENT0("impeller", __FUNCTION__);
81 auto impl = AHBSwapchainImplVK::Create(context_, //
82 surface_control_, //
83 size, //
84 enable_msaa_, //
85 swapchain_image_count_ //
86 );
87 if (!impl || !impl->IsValid()) {
88 VALIDATION_LOG << "Could not resize swapchain to size: " << size;
89 return;
90 }
91 impl_ = std::move(impl);
92 }
93
94 } // namespace impeller
impeller::android::HardwareBuffer::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: hardware_buffer.cc:107
ahb_swapchain_vk.h
ahb_formats.h
impeller::android::SurfaceControl::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: surface_control.cc:50
impeller::ContextVK::GetPhysicalDevice
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:620
impeller::AHBSwapchainVK::AHBSwapchainVK
AHBSwapchainVK(const AHBSwapchainVK &)=delete
formats_vk.h
impeller::AHBSwapchainImplVK::Create
static std::shared_ptr< AHBSwapchainImplVK > Create(const std::weak_ptr< Context > &context, std::weak_ptr< android::SurfaceControl > surface_control, const ISize &size, bool enable_msaa, size_t swapchain_image_count)
Create a swapchain of a specific size whose images will be presented to the provided surface control.
Definition: ahb_swapchain_impl_vk.cc:39
impeller::AHBSwapchainVK::AcquireNextDrawable
std::unique_ptr< Surface > AcquireNextDrawable() override
Definition: ahb_swapchain_vk.cc:53
impeller::TSize
Definition: size.h:25
impeller::ToVKImageFormat
constexpr vk::Format ToVKImageFormat(PixelFormat format)
Definition: formats_vk.h:146
impeller::AHBSwapchainVK::AddFinalCommandBuffer
void AddFinalCommandBuffer(std::shared_ptr< CommandBuffer > cmd_buffer) const override
Definition: ahb_swapchain_vk.cc:70
impeller::AHBSwapchainVK::~AHBSwapchainVK
~AHBSwapchainVK() override
impeller::AHBSwapchainVK::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: ahb_swapchain_vk.cc:15
impeller::AHBSwapchainVK::UpdateSurfaceSize
void UpdateSurfaceSize(const ISize &size) override
Mark the current swapchain configuration as dirty, forcing it to be recreated on the next frame.
Definition: ahb_swapchain_vk.cc:76
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::AHBSwapchainVK::IsValid
bool IsValid() const override
Definition: ahb_swapchain_vk.cc:48
std
Definition: comparable.h:95
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
context_vk.h
impeller
Definition: allocation.cc:12
impeller::AHBSwapchainVK::GetSurfaceFormat
vk::Format GetSurfaceFormat() const override
Definition: ahb_swapchain_vk.cc:63
impeller::ToPixelFormat
static PixelFormat ToPixelFormat(AHardwareBuffer_Format format)
Definition: ahb_texture_source_vk.cc:225