Flutter Impeller
ahb_texture_pool_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_TEXTURE_POOL_VK_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_SWAPCHAIN_AHB_AHB_TEXTURE_POOL_VK_H_
7 
8 #include <deque>
9 
10 #include "flutter/fml/unique_fd.h"
11 #include "impeller/base/thread.h"
13 
14 namespace impeller {
15 
16 //------------------------------------------------------------------------------
17 /// @brief Maintains a bounded pool of hardware buffer backed texture
18 /// sources that can be used as swapchain images.
19 ///
20 /// The number of cached entries in the texture pool is capped to a
21 /// caller specified value.
22 ///
23 /// If a previously cached entry cannot be obtained from the pool, a
24 /// new entry is created. The only case where a valid texture source
25 /// cannot be obtained is due to resource exhaustion.
26 ///
27 /// Pools are thread-safe.
28 ///
30  public:
31  struct PoolEntry {
32  std::shared_ptr<AHBTextureSourceVK> texture;
33  std::shared_ptr<fml::UniqueFD> render_ready_fence;
34 
35  explicit PoolEntry(std::shared_ptr<AHBTextureSourceVK> p_item,
36  fml::UniqueFD p_render_ready_fence = {})
37  : texture(std::move(p_item)),
38  render_ready_fence(std::make_shared<fml::UniqueFD>(
39  std::move(p_render_ready_fence))) {}
40 
41  constexpr bool IsValid() const { return !!texture; }
42  };
43 
44  //----------------------------------------------------------------------------
45  /// @brief Create a new (empty) texture pool.
46  ///
47  /// @param[in] context The context whose allocators will be used to
48  /// create the resources for the texture sources.
49  /// @param[in] desc The descriptor of the hardware buffers that
50  /// will be used to create the backing stores of
51  /// the texture sources.
52  /// @param[in] max_entries The maximum entries that will remain cached
53  /// in the pool.
54  ///
55  explicit AHBTexturePoolVK(std::weak_ptr<Context> context,
57  size_t max_entries = 3u);
58 
60 
62 
64 
65  //----------------------------------------------------------------------------
66  /// @brief If the pool can create and pool hardware buffer backed texture
67  /// sources. The only reason valid textures cannot be obtained
68  /// from a valid pool is because of resource exhaustion.
69  ///
70  /// @return `true` if valid, `false` otherwise.
71  ///
72  bool IsValid() const;
73 
74  //----------------------------------------------------------------------------
75  /// @brief Pops an texture source from the pool. If the pool is empty, a
76  /// new texture source is created and returned.
77  ///
78  /// This operation is thread-safe.
79  ///
80  /// @return A texture source that can be used as a swapchain image. This
81  /// can be nullptr in case of resource exhaustion.
82  ///
83  PoolEntry Pop();
84 
85  //----------------------------------------------------------------------------
86  /// @brief Push a popped texture back into the pool. This also performs a
87  /// GC.
88  ///
89  /// This operation is thread-safe.
90  ///
91  /// @warning Only a texture source obtained from the same pool can be
92  /// returned to it. It is user error to mix and match texture
93  /// sources from different pools.
94  ///
95  /// @param[in] texture The texture to be returned to the pool.
96  ///
97  void Push(std::shared_ptr<AHBTextureSourceVK> texture,
98  fml::UniqueFD render_ready_fence);
99 
100  //----------------------------------------------------------------------------
101  /// @brief Perform an explicit GC of the pool items. This happens
102  /// implicitly when a texture source us pushed into the pool but
103  /// one may be necessary explicitly if there is no push back into
104  /// the pool for a long time.
105  ///
106  void PerformGC();
107 
108  private:
109  const std::weak_ptr<Context> context_;
111  const size_t max_entries_;
112  bool is_valid_ = false;
113  Mutex pool_mutex_;
114  std::deque<PoolEntry> pool_ IPLR_GUARDED_BY(pool_mutex_);
115 
116  void PerformGCLocked() IPLR_REQUIRES(pool_mutex_);
117 
118  std::shared_ptr<AHBTextureSourceVK> CreateTexture() const;
119 };
120 
121 } // namespace impeller
122 
123 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_SWAPCHAIN_AHB_AHB_TEXTURE_POOL_VK_H_
Maintains a bounded pool of hardware buffer backed texture sources that can be used as swapchain imag...
PoolEntry Pop()
Pops an texture source from the pool. If the pool is empty, a new texture source is created and retur...
AHBTexturePoolVK(std::weak_ptr< Context > context, android::HardwareBufferDescriptor desc, size_t max_entries=3u)
Create a new (empty) texture pool.
void PerformGC()
Perform an explicit GC of the pool items. This happens implicitly when a texture source us pushed int...
AHBTexturePoolVK(const AHBTexturePoolVK &)=delete
bool IsValid() const
If the pool can create and pool hardware buffer backed texture sources. The only reason valid texture...
void Push(std::shared_ptr< AHBTextureSourceVK > texture, fml::UniqueFD render_ready_fence)
Push a popped texture back into the pool. This also performs a GC.
AHBTexturePoolVK & operator=(const AHBTexturePoolVK &)=delete
A texture source that wraps an instance of AHardwareBuffer.
Definition: comparable.h:95
std::shared_ptr< AHBTextureSourceVK > texture
std::shared_ptr< fml::UniqueFD > render_ready_fence
PoolEntry(std::shared_ptr< AHBTextureSourceVK > p_item, fml::UniqueFD p_render_ready_fence={})
constexpr bool IsValid() const
A descriptor use to specify hardware buffer allocations.
#define IPLR_REQUIRES(...)
Definition: thread_safety.h:30