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 
59 
61 
63 
64  //----------------------------------------------------------------------------
65  /// @brief If the pool can create and pool hardware buffer backed texture
66  /// sources. The only reason valid textures cannot be obtained
67  /// from a valid pool is because of resource exhaustion.
68  ///
69  /// @return `true` if valid, `false` otherwise.
70  ///
71  bool IsValid() const;
72 
73  //----------------------------------------------------------------------------
74  /// @brief Pops an texture source from the pool. If the pool is empty, a
75  /// new texture source is created and returned.
76  ///
77  /// This operation is thread-safe.
78  ///
79  /// @return A texture source that can be used as a swapchain image. This
80  /// can be nullptr in case of resource exhaustion.
81  ///
82  PoolEntry Pop();
83 
84  //----------------------------------------------------------------------------
85  /// @brief Push a popped texture back into the pool. This also performs a
86  /// GC.
87  ///
88  /// This operation is thread-safe.
89  ///
90  /// @warning Only a texture source obtained from the same pool can be
91  /// returned to it. It is user error to mix and match texture
92  /// sources from different pools.
93  ///
94  /// @param[in] texture The texture to be returned to the pool.
95  ///
96  void Push(std::shared_ptr<AHBTextureSourceVK> texture,
97  fml::UniqueFD render_ready_fence);
98 
99  private:
100  const std::weak_ptr<Context> context_;
102  bool is_valid_ = false;
103  Mutex pool_mutex_;
104  std::deque<PoolEntry> pool_ IPLR_GUARDED_BY(pool_mutex_);
105 
106  std::shared_ptr<AHBTextureSourceVK> CreateTexture() const;
107 };
108 
109 } // namespace impeller
110 
111 #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(const AHBTexturePoolVK &)=delete
bool IsValid() const
If the pool can create and pool hardware buffer backed texture sources. The only reason valid texture...
AHBTexturePoolVK(std::weak_ptr< Context > context, android::HardwareBufferDescriptor desc)
Create a new (empty) texture pool.
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
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.