| 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"
12 #include "impeller/renderer/backend/vulkan/android/ahb_texture_source_vk.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 ///
29 class AHBTexturePoolVK {
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,
56 android::HardwareBufferDescriptor desc,
57 size_t max_entries = 3u);
58
59 ~AHBTexturePoolVK();
60
61 AHBTexturePoolVK(const AHBTexturePoolVK&) = delete;
62
63 AHBTexturePoolVK& operator=(const AHBTexturePoolVK&) = delete;
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_;
110 const android::HardwareBufferDescriptor desc_;
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_
impeller::AHBTexturePoolVK::AHBTexturePoolVK
AHBTexturePoolVK(std::weak_ptr< Context > context, android::HardwareBufferDescriptor desc, size_t max_entries=3u)
Create a new (empty) texture pool.
Definition: ahb_texture_pool_vk.cc:11
impeller::AHBTexturePoolVK::PoolEntry
Definition: ahb_texture_pool_vk.h:31
impeller::AHBTexturePoolVK::Pop
PoolEntry Pop()
Pops an texture source from the pool. If the pool is empty, a new texture source is created and retur...
Definition: ahb_texture_pool_vk.cc:27
impeller::AHBTexturePoolVK::PerformGC
void PerformGC()
Perform an explicit GC of the pool items. This happens implicitly when a texture source us pushed int...
Definition: ahb_texture_pool_vk.cc:78
impeller::AHBTexturePoolVK::operator=
AHBTexturePoolVK & operator=(const AHBTexturePoolVK &)=delete
impeller::AHBTexturePoolVK::~AHBTexturePoolVK
~AHBTexturePoolVK()
impeller::AHBTexturePoolVK::PoolEntry::IsValid
constexpr bool IsValid() const
Definition: ahb_texture_pool_vk.h:41
impeller::AHBTexturePoolVK
Maintains a bounded pool of hardware buffer backed texture sources that can be used as swapchain imag...
Definition: ahb_texture_pool_vk.h:29
impeller::android::HardwareBufferDescriptor
A descriptor use to specify hardware buffer allocations.
Definition: hardware_buffer.h:47
impeller::AHBTexturePoolVK::PoolEntry::texture
std::shared_ptr< AHBTextureSourceVK > texture
Definition: ahb_texture_pool_vk.h:32
impeller::AHBTexturePoolVK::PoolEntry::PoolEntry
PoolEntry(std::shared_ptr< AHBTextureSourceVK > p_item, fml::UniqueFD p_render_ready_fence={})
Definition: ahb_texture_pool_vk.h:35
impeller::AHBTexturePoolVK::PoolEntry::render_ready_fence
std::shared_ptr< fml::UniqueFD > render_ready_fence
Definition: ahb_texture_pool_vk.h:33
std
Definition: comparable.h:95
impeller::AHBTextureSourceVK
A texture source that wraps an instance of AHardwareBuffer.
Definition: ahb_texture_source_vk.h:32
impeller::AHBTexturePoolVK::Push
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.
Definition: ahb_texture_pool_vk.cc:41
ahb_texture_source_vk.h
thread.h
impeller
Definition: allocation.cc:12
IPLR_REQUIRES
#define IPLR_REQUIRES(...)
Definition: thread_safety.h:30
impeller::AHBTexturePoolVK::IsValid
bool IsValid() const
If the pool can create and pool hardware buffer backed texture sources. The only reason valid texture...
Definition: ahb_texture_pool_vk.cc:93