| Flutter Impeller |

ahb_swapchain_impl_vk.h
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 #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_SWAPCHAIN_AHB_AHB_SWAPCHAIN_IMPL_VK_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_SWAPCHAIN_AHB_AHB_SWAPCHAIN_IMPL_VK_H_
7
8 #include <memory>
9
10 #include "flutter/fml/closure.h"
11 #include "impeller/base/thread.h"
12 #include "impeller/renderer/backend/vulkan/android/ahb_texture_source_vk.h"
13 #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h"
14 #include "impeller/renderer/backend/vulkan/swapchain/ahb/external_semaphore_vk.h"
15 #include "impeller/renderer/backend/vulkan/swapchain/swapchain_transients_vk.h"
16 #include "impeller/renderer/surface.h"
17 #include "impeller/toolkit/android/hardware_buffer.h"
18 #include "impeller/toolkit/android/surface_control.h"
19 #include "impeller/toolkit/android/surface_transaction.h"
20 #include "vulkan/vulkan_handles.hpp"
21
22 namespace impeller {
23
24 using CreateTransactionCB = std::function<android::SurfaceTransaction()>;
25
26 static constexpr const size_t kMaxPendingPresents = 2u;
27
28 struct AHBFrameSynchronizerVK {
29 vk::UniqueFence acquire;
30 vk::UniqueSemaphore render_ready = {};
31 std::shared_ptr<ExternalSemaphoreVK> present_ready;
32 std::shared_ptr<CommandBuffer> final_cmd_buffer;
33 bool is_valid = false;
34
35 explicit AHBFrameSynchronizerVK(const vk::Device& device);
36
37 ~AHBFrameSynchronizerVK();
38
39 bool IsValid() const;
40
41 bool WaitForFence(const vk::Device& device);
42 };
43
44 //------------------------------------------------------------------------------
45 /// @brief The implementation of a swapchain at a specific size. Resizes to
46 /// the surface will cause the instance of the swapchain impl at
47 /// that size to be discarded along with all its caches and
48 /// transients.
49 ///
50 class AHBSwapchainImplVK final
51 : public std::enable_shared_from_this<AHBSwapchainImplVK> {
52 public:
53 //----------------------------------------------------------------------------
54 /// @brief Create a swapchain of a specific size whose images will be
55 /// presented to the provided surface control.
56 ///
57 /// @param[in] context The context whose allocators will be used to
58 /// create swapchain image resources.
59 /// @param[in] surface_control The surface control to which the swapchain
60 /// images will be presented.
61 /// @param[in] size The size of the swapchain images. This is
62 /// constant for the lifecycle of the swapchain
63 /// impl.
64 /// @param[in] enable_msaa If the swapchain images will be presented
65 /// using a render target that enables MSAA. This
66 /// allows for additional caching of transients.
67 ///
68 /// @return A valid swapchain impl if one can be created. `nullptr`
69 /// otherwise.
70 ///
71 static std::shared_ptr<AHBSwapchainImplVK> Create(
72 const std::weak_ptr<Context>& context,
73 std::weak_ptr<android::SurfaceControl> surface_control,
74 const CreateTransactionCB& cb,
75 const ISize& size,
76 bool enable_msaa);
77
78 ~AHBSwapchainImplVK();
79
80 AHBSwapchainImplVK(const AHBSwapchainImplVK&) = delete;
81
82 AHBSwapchainImplVK& operator=(const AHBSwapchainImplVK&) = delete;
83
84 //----------------------------------------------------------------------------
85 /// @return The size of the swapchain images that will be displayed on the
86 /// surface control.
87 ///
88 const ISize& GetSize() const;
89
90 //----------------------------------------------------------------------------
91 /// @return If the swapchain impl is valid. If it is not, the instance
92 /// must be discarded. There is no error recovery.
93 ///
94 bool IsValid() const;
95
96 //----------------------------------------------------------------------------
97 /// @brief Get the descriptor used to create the hardware buffers that
98 /// will be displayed on the surface control.
99 ///
100 /// @return The descriptor.
101 ///
102 const android::HardwareBufferDescriptor& GetDescriptor() const;
103
104 //----------------------------------------------------------------------------
105 /// @brief Acquire the next surface that can be used to present to the
106 /// swapchain.
107 ///
108 /// @return A surface if one can be created. If one cannot be created, it
109 /// is likely due to resource exhaustion.
110 ///
111 std::unique_ptr<Surface> AcquireNextDrawable();
112
113 void AddFinalCommandBuffer(std::shared_ptr<CommandBuffer> cmd_buffer);
114
115 private:
116 using AutoSemaSignaler = std::shared_ptr<fml::ScopedCleanupClosure>;
117
118 std::weak_ptr<android::SurfaceControl> surface_control_;
119 android::HardwareBufferDescriptor desc_;
120 std::shared_ptr<AHBTexturePoolVK> pool_;
121 std::shared_ptr<SwapchainTransientsVK> transients_;
122
123 // In C++20, this mutex can be replaced by the shared pointer specialization
124 // of std::atomic.
125 Mutex currently_displayed_texture_mutex_;
126 std::shared_ptr<AHBTextureSourceVK> currently_displayed_texture_
127 IPLR_GUARDED_BY(currently_displayed_texture_mutex_);
128
129 std::vector<std::unique_ptr<AHBFrameSynchronizerVK>> frame_data_;
130 size_t frame_index_ = 0;
131 CreateTransactionCB cb_;
132 bool is_valid_ = false;
133
134 explicit AHBSwapchainImplVK(
135 const std::weak_ptr<Context>& context,
136 std::weak_ptr<android::SurfaceControl> surface_control,
137 const CreateTransactionCB& cb,
138 const ISize& size,
139 bool enable_msaa);
140
141 bool Present(const std::shared_ptr<AHBTextureSourceVK>& texture);
142
143 vk::UniqueSemaphore CreateRenderReadySemaphore(
144 const std::shared_ptr<fml::UniqueFD>& fd) const;
145
146 bool ImportRenderReady(
147 const std::shared_ptr<fml::UniqueFD>& render_ready_fence,
148 const std::shared_ptr<AHBTextureSourceVK>& texture);
149
150 std::shared_ptr<ExternalSemaphoreVK> SubmitSignalForPresentReady(
151 const std::shared_ptr<AHBTextureSourceVK>& texture) const;
152
153 void OnTextureUpdatedOnSurfaceControl(
154 std::shared_ptr<AHBTextureSourceVK> texture,
155 ASurfaceTransactionStats* stats);
156 };
157
158 } // namespace impeller
159
160 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_SWAPCHAIN_AHB_AHB_SWAPCHAIN_IMPL_VK_H_
ahb_texture_pool_vk.h
ahb_texture_source_vk.h
impeller::AHBSwapchainImplVK
The implementation of a swapchain at a specific size. Resizes to the surface will cause the instance ...
Definition: ahb_swapchain_impl_vk.h:51
impeller::AHBSwapchainImplVK::IsValid
bool IsValid() const
Definition: ahb_swapchain_impl_vk.cc:111
impeller::AHBSwapchainImplVK::Create
static std::shared_ptr< AHBSwapchainImplVK > Create(const std::weak_ptr< Context > &context, std::weak_ptr< android::SurfaceControl > surface_control, const CreateTransactionCB &cb, const ISize &size, bool enable_msaa)
Create a swapchain of a specific size whose images will be presented to the provided surface control.
Definition: ahb_swapchain_impl_vk.cc:66
impeller::AHBSwapchainImplVK::AHBSwapchainImplVK
AHBSwapchainImplVK(const AHBSwapchainImplVK &)=delete
impeller::AHBSwapchainImplVK::operator=
AHBSwapchainImplVK & operator=(const AHBSwapchainImplVK &)=delete
impeller::AHBSwapchainImplVK::AcquireNextDrawable
std::unique_ptr< Surface > AcquireNextDrawable()
Acquire the next surface that can be used to present to the swapchain.
Definition: ahb_swapchain_impl_vk.cc:120
impeller::AHBSwapchainImplVK::GetDescriptor
const android::HardwareBufferDescriptor & GetDescriptor() const
Get the descriptor used to create the hardware buffers that will be displayed on the surface control.
Definition: ahb_swapchain_impl_vk.cc:115
impeller::AHBSwapchainImplVK::AddFinalCommandBuffer
void AddFinalCommandBuffer(std::shared_ptr< CommandBuffer > cmd_buffer)
Definition: ahb_swapchain_impl_vk.cc:222
impeller::AHBSwapchainImplVK::~AHBSwapchainImplVK
~AHBSwapchainImplVK()
impeller::AHBSwapchainImplVK::GetSize
const ISize & GetSize() const
Definition: ahb_swapchain_impl_vk.cc:107
impeller::android::SurfaceTransaction
A wrapper for ASurfaceTransaction. https://developer.android.com/ndk/reference/group/native-activity#...
Definition: surface_transaction.h:50
external_semaphore_vk.h
hardware_buffer.h
impeller
Definition: allocation.cc:12
impeller::CreateTransactionCB
std::function< android::SurfaceTransaction()> CreateTransactionCB
Definition: ahb_swapchain_impl_vk.h:24
impeller::kMaxPendingPresents
static constexpr const size_t kMaxPendingPresents
Definition: ahb_swapchain_impl_vk.h:26
surface.h
impeller::AHBFrameSynchronizerVK
Definition: ahb_swapchain_impl_vk.h:28
impeller::AHBFrameSynchronizerVK::~AHBFrameSynchronizerVK
~AHBFrameSynchronizerVK()
impeller::AHBFrameSynchronizerVK::is_valid
bool is_valid
Definition: ahb_swapchain_impl_vk.h:33
impeller::AHBFrameSynchronizerVK::final_cmd_buffer
std::shared_ptr< CommandBuffer > final_cmd_buffer
Definition: ahb_swapchain_impl_vk.h:32
impeller::AHBFrameSynchronizerVK::IsValid
bool IsValid() const
Definition: ahb_swapchain_impl_vk.cc:44
impeller::AHBFrameSynchronizerVK::AHBFrameSynchronizerVK
AHBFrameSynchronizerVK(const vk::Device &device)
Definition: ahb_swapchain_impl_vk.cc:31
impeller::AHBFrameSynchronizerVK::render_ready
vk::UniqueSemaphore render_ready
Definition: ahb_swapchain_impl_vk.h:30
impeller::AHBFrameSynchronizerVK::WaitForFence
bool WaitForFence(const vk::Device &device)
Definition: ahb_swapchain_impl_vk.cc:48
impeller::AHBFrameSynchronizerVK::acquire
vk::UniqueFence acquire
Definition: ahb_swapchain_impl_vk.h:29
impeller::AHBFrameSynchronizerVK::present_ready
std::shared_ptr< ExternalSemaphoreVK > present_ready
Definition: ahb_swapchain_impl_vk.h:31
impeller::TSize
Definition: size.h:25
impeller::android::HardwareBufferDescriptor
A descriptor use to specify hardware buffer allocations.
Definition: hardware_buffer.h:47
surface_control.h
surface_transaction.h
swapchain_transients_vk.h
thread.h