Flutter Engine
flutter_runner::VulkanSurfacePool Class Referencefinal

#include <vulkan_surface_pool.h>

Public Member Functions

 VulkanSurfacePool (vulkan::VulkanProvider &vulkan_provider, sk_sp< GrDirectContext > context, scenic::Session *scenic_session)
 
 ~VulkanSurfacePool ()
 
std::unique_ptr< VulkanSurfaceAcquireSurface (const SkISize &size)
 
void SubmitSurface (std::unique_ptr< SurfaceProducerSurface > surface)
 
void AgeAndCollectOldBuffers ()
 
void ShrinkToFit ()
 

Static Public Attributes

static constexpr int kMaxSurfaces = 12
 
static constexpr int kMaxSurfaceAge = 3
 

Detailed Description

Definition at line 15 of file vulkan_surface_pool.h.

Constructor & Destructor Documentation

◆ VulkanSurfacePool()

flutter_runner::VulkanSurfacePool::VulkanSurfacePool ( vulkan::VulkanProvider vulkan_provider,
sk_sp< GrDirectContext >  context,
scenic::Session *  scenic_session 
)

Definition at line 24 of file vulkan_surface_pool.cc.

27  : vulkan_provider_(vulkan_provider),
28  context_(std::move(context)),
29  scenic_session_(scenic_session) {}

◆ ~VulkanSurfacePool()

flutter_runner::VulkanSurfacePool::~VulkanSurfacePool ( )

Definition at line 31 of file vulkan_surface_pool.cc.

31 {}

Member Function Documentation

◆ AcquireSurface()

std::unique_ptr< VulkanSurface > flutter_runner::VulkanSurfacePool::AcquireSurface ( const SkISize &  size)

Definition at line 33 of file vulkan_surface_pool.cc.

References flutter_runner::CreateVulkanImage(), FML_DCHECK, FML_DLOG, fml::size(), TRACE_EVENT2, TRACE_EVENT_INSTANT0, and flutter_runner::VulkanImage::vk_memory_requirements.

34  {
35  auto surface = GetCachedOrCreateSurface(size);
36 
37  if (surface == nullptr) {
38  FML_DLOG(ERROR) << "Could not acquire surface";
39  return nullptr;
40  }
41 
42  if (!surface->FlushSessionAcquireAndReleaseEvents()) {
43  FML_DLOG(ERROR) << "Could not flush acquire/release events for buffer.";
44  return nullptr;
45  }
46 
47  return surface;
48 }
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define FML_DLOG(severity)
Definition: logging.h:85

◆ AgeAndCollectOldBuffers()

void flutter_runner::VulkanSurfacePool::AgeAndCollectOldBuffers ( )

Definition at line 186 of file vulkan_surface_pool.cc.

References FML_DLOG, kMaxSurfaceAge, TRACE_EVENT0, TRACE_EVENT1, and TRACE_EVENT_INSTANT0.

186  {
187  TRACE_EVENT0("flutter", "VulkanSurfacePool::AgeAndCollectOldBuffers");
188 
189  // Remove all surfaces that are no longer valid or are too old.
190  size_t size_before = available_surfaces_.size();
191  available_surfaces_.erase(
192  std::remove_if(available_surfaces_.begin(), available_surfaces_.end(),
193  [&](auto& surface) {
194  return !surface->IsValid() ||
195  surface->AdvanceAndGetAge() >= kMaxSurfaceAge;
196  }),
197  available_surfaces_.end());
198  TRACE_EVENT1("flutter", "AgeAndCollect", "aged surfaces",
199  (size_before - available_surfaces_.size()));
200 
201  // Look for a surface that has both a larger |VkDeviceMemory| allocation
202  // than is necessary for its |VkImage|, and has a stable size history.
203  auto surface_to_remove_it = std::find_if(
204  available_surfaces_.begin(), available_surfaces_.end(),
205  [](const auto& surface) {
206  return surface->IsOversized() && surface->HasStableSizeHistory();
207  });
208  // If we found such a surface, then destroy it and cache a new one that only
209  // uses a necessary amount of memory.
210  if (surface_to_remove_it != available_surfaces_.end()) {
211  TRACE_EVENT_INSTANT0("flutter", "replacing surface with smaller one");
212  auto size = (*surface_to_remove_it)->GetSize();
213  available_surfaces_.erase(surface_to_remove_it);
214  auto new_surface = CreateSurface(size);
215  if (new_surface != nullptr) {
216  available_surfaces_.push_back(std::move(new_surface));
217  } else {
218  FML_DLOG(ERROR) << "Failed to create a new shrunk surface";
219  }
220  }
221 
222  TraceStats();
223 }
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
#define TRACE_EVENT_INSTANT0(category_group, name)
Definition: trace_event.h:104
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
Definition: trace_event.h:79
#define FML_DLOG(severity)
Definition: logging.h:85

◆ ShrinkToFit()

void flutter_runner::VulkanSurfacePool::ShrinkToFit ( )

Definition at line 225 of file vulkan_surface_pool.cc.

References FML_DLOG, and TRACE_EVENT0.

225  {
226  TRACE_EVENT0("flutter", "VulkanSurfacePool::ShrinkToFit");
227  // Reset all oversized surfaces in |available_surfaces_| so that the old
228  // surfaces and new surfaces don't exist at the same time at any point,
229  // reducing our peak memory footprint.
230  std::vector<SkISize> sizes_to_recreate;
231  for (auto& surface : available_surfaces_) {
232  if (surface->IsOversized()) {
233  sizes_to_recreate.push_back(surface->GetSize());
234  surface.reset();
235  }
236  }
237  available_surfaces_.erase(std::remove(available_surfaces_.begin(),
238  available_surfaces_.end(), nullptr),
239  available_surfaces_.end());
240  for (const auto& size : sizes_to_recreate) {
241  auto surface = CreateSurface(size);
242  if (surface != nullptr) {
243  available_surfaces_.push_back(std::move(surface));
244  } else {
245  FML_DLOG(ERROR) << "Failed to create resized surface";
246  }
247  }
248 
249  TraceStats();
250 }
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define FML_DLOG(severity)
Definition: logging.h:85

◆ SubmitSurface()

void flutter_runner::VulkanSurfacePool::SubmitSurface ( std::unique_ptr< SurfaceProducerSurface surface)

Definition at line 114 of file vulkan_surface_pool.cc.

References kMaxSurfaces, fml::size(), TRACE_EVENT0, TRACE_EVENT2, and TRACE_EVENT_INSTANT0.

115  {
116  TRACE_EVENT0("flutter", "VulkanSurfacePool::SubmitSurface");
117 
118  // This cast is safe because |VulkanSurface| is the only implementation of
119  // |SurfaceProducerSurface| for Flutter on Fuchsia. Additionally, it is
120  // required, because we need to access |VulkanSurface| specific information
121  // of the surface (such as the amount of VkDeviceMemory it contains).
122  auto vulkan_surface = std::unique_ptr<VulkanSurface>(
123  static_cast<VulkanSurface*>(p_surface.release()));
124  if (!vulkan_surface) {
125  return;
126  }
127 
128  uintptr_t surface_key = reinterpret_cast<uintptr_t>(vulkan_surface.get());
129  auto insert_iterator = pending_surfaces_.insert(std::make_pair(
130  surface_key, // key
131  std::move(vulkan_surface) // value
132  ));
133  if (insert_iterator.second) {
134  insert_iterator.first->second->SignalWritesFinished(std::bind(
135  &VulkanSurfacePool::RecyclePendingSurface, this, surface_key));
136  }
137 }
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75

Member Data Documentation

◆ kMaxSurfaceAge

constexpr int flutter_runner::VulkanSurfacePool::kMaxSurfaceAge = 3
static

Definition at line 21 of file vulkan_surface_pool.h.

Referenced by AgeAndCollectOldBuffers().

◆ kMaxSurfaces

constexpr int flutter_runner::VulkanSurfacePool::kMaxSurfaces = 12
static

Definition at line 19 of file vulkan_surface_pool.h.

Referenced by SubmitSurface().


The documentation for this class was generated from the following files: