| Flutter Impeller |

ahb_texture_pool_vk.cc
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 #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h"
6
7 #include "flutter/fml/trace_event.h"
8
9 namespace impeller {
10
11 AHBTexturePoolVK::AHBTexturePoolVK(std::weak_ptr<Context> context,
12 android::HardwareBufferDescriptor desc,
13 size_t max_entries)
14 : context_(std::move(context)), desc_(desc), max_entries_(max_entries) {
15 if (!desc_.IsAllocatable()) {
16 VALIDATION_LOG << "Swapchain image is not allocatable.";
17 return;
18 }
19 for (auto i = 0u; i < max_entries_; i++) {
20 pool_.emplace_back(CreateTexture());
21 }
22 is_valid_ = true;
23 }
24
25 AHBTexturePoolVK::~AHBTexturePoolVK() = default;
26
27 AHBTexturePoolVK::PoolEntry AHBTexturePoolVK::Pop() {
28 {
29 Lock lock(pool_mutex_);
30 if (!pool_.empty()) {
31 // Buffers are pushed to the back of the queue. To give the ready fences
32 // the most time to signal, pick a buffer from the front of the queue.
33 auto entry = pool_.front();
34 pool_.pop_front();
35 return entry;
36 }
37 }
38 return PoolEntry{CreateTexture()};
39 }
40
41 void AHBTexturePoolVK::Push(std::shared_ptr<AHBTextureSourceVK> texture,
42 fml::UniqueFD render_ready_fence) {
43 if (!texture) {
44 return;
45 }
46 Lock lock(pool_mutex_);
47 pool_.push_back(PoolEntry{std::move(texture), std::move(render_ready_fence)});
48 PerformGCLocked();
49 }
50
51 std::shared_ptr<AHBTextureSourceVK> AHBTexturePoolVK::CreateTexture() const {
52 TRACE_EVENT0("impeller", "CreateSwapchainTexture");
53 auto context = context_.lock();
54 if (!context) {
55 VALIDATION_LOG << "Context died before image could be created.";
56 return nullptr;
57 }
58
59 auto ahb = std::make_unique<android::HardwareBuffer>(desc_);
60 if (!ahb->IsValid()) {
61 VALIDATION_LOG << "Could not create hardware buffer of size: "
62 << desc_.size;
63 return nullptr;
64 }
65
66 auto ahb_texture_source =
67 std::make_shared<AHBTextureSourceVK>(context, std::move(ahb), true);
68 if (!ahb_texture_source->IsValid()) {
69 VALIDATION_LOG << "Could not create hardware buffer texture source for "
70 "swapchain image of size: "
71 << desc_.size;
72 return nullptr;
73 }
74
75 return ahb_texture_source;
76 }
77
78 void AHBTexturePoolVK::PerformGC() {
79 Lock lock(pool_mutex_);
80 PerformGCLocked();
81 }
82
83 void AHBTexturePoolVK::PerformGCLocked() {
84 while (!pool_.empty() && (pool_.size() > max_entries_)) {
85 // Buffers are pushed to the back of the queue and popped from the front.
86 // The ones at the back should be given the most time for their fences to
87 // signal. If we are going to get rid of textures, they might as well be the
88 // newest ones since their fences will take the longest to signal.
89 pool_.pop_back();
90 }
91 }
92
93 bool AHBTexturePoolVK::IsValid() const {
94 return is_valid_;
95 }
96
97 } // namespace impeller
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::Lock
Definition: thread.h:72
impeller::AHBTexturePoolVK::~AHBTexturePoolVK
~AHBTexturePoolVK()
impeller::android::HardwareBufferDescriptor
A descriptor use to specify hardware buffer allocations.
Definition: hardware_buffer.h:47
impeller::android::HardwareBufferDescriptor::size
ISize size
Definition: hardware_buffer.h:49
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
std
Definition: comparable.h:95
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_pool_vk.h
impeller::android::HardwareBufferDescriptor::IsAllocatable
bool IsAllocatable() const
If hardware buffers can be created using this descriptor. Allocatable descriptors may still cause fai...
Definition: hardware_buffer.cc:52
impeller
Definition: allocation.cc:12
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