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  : context_(std::move(context)), desc_(desc) {
14  if (!desc_.IsAllocatable()) {
15  VALIDATION_LOG << "Swapchain image is not allocatable.";
16  return;
17  }
18  is_valid_ = true;
19 }
20 
22 
24  {
25  Lock lock(pool_mutex_);
26  if (!pool_.empty()) {
27  // Buffers are pushed to the back of the queue. To give the ready fences
28  // the most time to signal, pick a buffer from the front of the queue.
29  auto entry = pool_.front();
30  pool_.pop_front();
31  return entry;
32  }
33  }
34  return PoolEntry{CreateTexture()};
35 }
36 
37 void AHBTexturePoolVK::Push(std::shared_ptr<AHBTextureSourceVK> texture,
38  fml::UniqueFD render_ready_fence) {
39  if (!texture) {
40  return;
41  }
42  Lock lock(pool_mutex_);
43  pool_.push_back(PoolEntry{std::move(texture), std::move(render_ready_fence)});
44 }
45 
46 std::shared_ptr<AHBTextureSourceVK> AHBTexturePoolVK::CreateTexture() const {
47  TRACE_EVENT0("impeller", "CreateSwapchainTexture");
48  auto context = context_.lock();
49  if (!context) {
50  VALIDATION_LOG << "Context died before image could be created.";
51  return nullptr;
52  }
53 
54  auto ahb = std::make_unique<android::HardwareBuffer>(desc_);
55  if (!ahb->IsValid()) {
56  VALIDATION_LOG << "Could not create hardware buffer of size: "
57  << desc_.size;
58  return nullptr;
59  }
60 
61  auto ahb_texture_source =
62  std::make_shared<AHBTextureSourceVK>(context, std::move(ahb), true);
63  if (!ahb_texture_source->IsValid()) {
64  VALIDATION_LOG << "Could not create hardware buffer texture source for "
65  "swapchain image of size: "
66  << desc_.size;
67  return nullptr;
68  }
69 
70  return ahb_texture_source;
71 }
72 
74  return is_valid_;
75 }
76 
77 } // namespace impeller
PoolEntry Pop()
Pops an texture source from the pool. If the pool is empty, a new texture source is created and retur...
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.
std::optional< PipelineDescriptor > desc_
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