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