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