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 "flutter/fml/synchronization/semaphore.h"
12 #include "impeller/base/thread.h"
20 #include "vulkan/vulkan_handles.hpp"
21 
22 namespace impeller {
23 
24 //------------------------------------------------------------------------------
25 /// @brief The implementation of a swapchain at a specific size. Resizes to
26 /// the surface will cause the instance of the swapchain impl at
27 /// that size to be discarded along with all its caches and
28 /// transients.
29 ///
30 class AHBSwapchainImplVK final
31  : public std::enable_shared_from_this<AHBSwapchainImplVK> {
32  public:
33  //----------------------------------------------------------------------------
34  /// @brief Create a swapchain of a specific size whose images will be
35  /// presented to the provided surface control.
36  ///
37  /// @param[in] context The context whose allocators will be used to
38  /// create swapchain image resources.
39  /// @param[in] surface_control The surface control to which the swapchain
40  /// images will be presented.
41  /// @param[in] size The size of the swapchain images. This is
42  /// constant for the lifecycle of the swapchain
43  /// impl.
44  /// @param[in] enable_msaa If the swapchain images will be presented
45  /// using a render target that enables MSAA. This
46  /// allows for additional caching of transients.
47  ///
48  /// @return A valid swapchain impl if one can be created. `nullptr`
49  /// otherwise.
50  ///
51  static std::shared_ptr<AHBSwapchainImplVK> Create(
52  const std::weak_ptr<Context>& context,
53  std::weak_ptr<android::SurfaceControl> surface_control,
54  const ISize& size,
55  bool enable_msaa,
56  size_t swapchain_image_count);
57 
59 
61 
63 
64  //----------------------------------------------------------------------------
65  /// @return The size of the swapchain images that will be displayed on the
66  /// surface control.
67  ///
68  const ISize& GetSize() const;
69 
70  //----------------------------------------------------------------------------
71  /// @return If the swapchain impl is valid. If it is not, the instance
72  /// must be discarded. There is no error recovery.
73  ///
74  bool IsValid() const;
75 
76  //----------------------------------------------------------------------------
77  /// @brief Get the descriptor used to create the hardware buffers that
78  /// will be displayed on the surface control.
79  ///
80  /// @return The descriptor.
81  ///
83 
84  //----------------------------------------------------------------------------
85  /// @brief Acquire the next surface that can be used to present to the
86  /// swapchain.
87  ///
88  /// @return A surface if one can be created. If one cannot be created, it
89  /// is likely due to resource exhaustion.
90  ///
91  std::unique_ptr<Surface> AcquireNextDrawable();
92 
93  void AddFinalCommandBuffer(std::shared_ptr<CommandBuffer> cmd_buffer);
94 
95  private:
96  using AutoSemaSignaler = std::shared_ptr<fml::ScopedCleanupClosure>;
97 
98  std::weak_ptr<android::SurfaceControl> surface_control_;
100  std::shared_ptr<AHBTexturePoolVK> pool_;
101  std::shared_ptr<SwapchainTransientsVK> transients_;
102  // In C++20, this mutex can be replaced by the shared pointer specialization
103  // of std::atomic.
104  Mutex currently_displayed_texture_mutex_;
105  std::shared_ptr<AHBTextureSourceVK> currently_displayed_texture_
106  IPLR_GUARDED_BY(currently_displayed_texture_mutex_);
107  std::shared_ptr<fml::Semaphore> pending_presents_;
108 
109  struct FrameData {
110  std::shared_ptr<CommandBuffer> command_buffer;
111  vk::UniqueSemaphore semaphore;
112  };
113 
114  std::array<FrameData, 3> frame_data_;
115  size_t frame_index_ = 0;
116  bool is_valid_ = false;
117 
118  explicit AHBSwapchainImplVK(
119  const std::weak_ptr<Context>& context,
120  std::weak_ptr<android::SurfaceControl> surface_control,
121  const ISize& size,
122  bool enable_msaa,
123  size_t swapchain_image_count);
124 
125  bool Present(const AutoSemaSignaler& signaler,
126  const std::shared_ptr<AHBTextureSourceVK>& texture);
127 
128  vk::UniqueSemaphore CreateRenderReadySemaphore(
129  const std::shared_ptr<fml::UniqueFD>& fd) const;
130 
131  bool SubmitWaitForRenderReady(
132  const std::shared_ptr<fml::UniqueFD>& render_ready_fence,
133  const std::shared_ptr<AHBTextureSourceVK>& texture);
134 
135  std::shared_ptr<ExternalFenceVK> SubmitSignalForPresentReady(
136  const std::shared_ptr<AHBTextureSourceVK>& texture) const;
137 
138  void OnTextureUpdatedOnSurfaceControl(
139  const AutoSemaSignaler& signaler,
140  std::shared_ptr<AHBTextureSourceVK> texture,
141  ASurfaceTransactionStats* stats);
142 };
143 
144 } // namespace impeller
145 
146 #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 ...
AHBSwapchainImplVK(const AHBSwapchainImplVK &)=delete
AHBSwapchainImplVK & operator=(const AHBSwapchainImplVK &)=delete
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.
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 descriptor use to specify hardware buffer allocations.