Flutter Impeller
impeller::KHRSwapchainImplVK Class Referencefinal

An instance of a swapchain that does NOT adapt to going out of date with the underlying surface. Errors will be indicated when the next drawable is acquired from this implementation of the swapchain. If the error is due the swapchain going out of date, the caller must recreate another instance by optionally stealing this implementations guts. More...

#include <khr_swapchain_impl_vk.h>

Inheritance diagram for impeller::KHRSwapchainImplVK:

Classes

struct  AcquireResult
 

Public Member Functions

 ~KHRSwapchainImplVK ()
 
bool IsValid () const
 
AcquireResult AcquireNextDrawable ()
 
vk::Format GetSurfaceFormat () const
 
std::shared_ptr< ContextGetContext () const
 
std::pair< vk::UniqueSurfaceKHR, vk::UniqueSwapchainKHR > DestroySwapchain ()
 
const ISizeGetSize () const
 

Static Public Member Functions

static std::shared_ptr< KHRSwapchainImplVKCreate (const std::shared_ptr< Context > &context, vk::UniqueSurfaceKHR surface, const ISize &size, bool enable_msaa=true, vk::SwapchainKHR old_swapchain=VK_NULL_HANDLE)
 

Detailed Description

An instance of a swapchain that does NOT adapt to going out of date with the underlying surface. Errors will be indicated when the next drawable is acquired from this implementation of the swapchain. If the error is due the swapchain going out of date, the caller must recreate another instance by optionally stealing this implementations guts.

Definition at line 31 of file khr_swapchain_impl_vk.h.

Constructor & Destructor Documentation

◆ ~KHRSwapchainImplVK()

impeller::KHRSwapchainImplVK::~KHRSwapchainImplVK ( )

Definition at line 313 of file khr_swapchain_impl_vk.cc.

313  {
315 }

References DestroySwapchain().

Member Function Documentation

◆ AcquireNextDrawable()

KHRSwapchainImplVK::AcquireResult impeller::KHRSwapchainImplVK::AcquireNextDrawable ( )

Wait on the host for the synchronizer fence.

Get the next image index.

Record all subsequent cmd buffers as part of the current frame.

Definition at line 350 of file khr_swapchain_impl_vk.cc.

350  {
351  auto context_strong = context_.lock();
352  if (!context_strong) {
353  return KHRSwapchainImplVK::AcquireResult{};
354  }
355 
356  const auto& context = ContextVK::Cast(*context_strong);
357 
358  current_frame_ = (current_frame_ + 1u) % synchronizers_.size();
359 
360  const auto& sync = synchronizers_[current_frame_];
361 
362  //----------------------------------------------------------------------------
363  /// Wait on the host for the synchronizer fence.
364  ///
365  if (!sync->WaitForFence(context.GetDevice())) {
366  VALIDATION_LOG << "Could not wait for fence.";
367  return KHRSwapchainImplVK::AcquireResult{};
368  }
369 
370  //----------------------------------------------------------------------------
371  /// Get the next image index.
372  ///
373  auto [acq_result, index] = context.GetDevice().acquireNextImageKHR(
374  *swapchain_, // swapchain
375  1'000'000'000, // timeout (ns) 1000ms
376  *sync->render_ready, // signal semaphore
377  nullptr // fence
378  );
379 
380  switch (acq_result) {
381  case vk::Result::eSuccess:
382  // Keep going.
383  break;
384  case vk::Result::eSuboptimalKHR:
385  case vk::Result::eErrorOutOfDateKHR:
386  // A recoverable error. Just say we are out of date.
387  return AcquireResult{true /* out of date */};
388  break;
389  default:
390  // An unrecoverable error.
391  VALIDATION_LOG << "Could not acquire next swapchain image: "
392  << vk::to_string(acq_result);
393  return AcquireResult{false /* out of date */};
394  }
395 
396  if (index >= images_.size()) {
397  VALIDATION_LOG << "Swapchain returned an invalid image index.";
398  return KHRSwapchainImplVK::AcquireResult{};
399  }
400 
401  /// Record all subsequent cmd buffers as part of the current frame.
402  context.GetGPUTracer()->MarkFrameStart();
403 
404  auto image = images_[index % images_.size()];
405  uint32_t image_index = index;
406  return AcquireResult{KHRSurfaceVK::WrapSwapchainImage(
407  context_strong, // context
408  image, // swapchain image
409  [weak_swapchain = weak_from_this(), image, image_index]() -> bool {
410  auto swapchain = weak_swapchain.lock();
411  if (!swapchain) {
412  return false;
413  }
414  return swapchain->Present(image, image_index);
415  }, // swap callback
416  enable_msaa_ //
417  )};
418 }

References impeller::BackendCast< ContextVK, Context >::Cast(), and VALIDATION_LOG.

◆ Create()

std::shared_ptr< KHRSwapchainImplVK > impeller::KHRSwapchainImplVK::Create ( const std::shared_ptr< Context > &  context,
vk::UniqueSurfaceKHR  surface,
const ISize size,
bool  enable_msaa = true,
vk::SwapchainKHR  old_swapchain = VK_NULL_HANDLE 
)
static

Definition at line 119 of file khr_swapchain_impl_vk.cc.

124  {
125  return std::shared_ptr<KHRSwapchainImplVK>(new KHRSwapchainImplVK(
126  context, std::move(surface), size, enable_msaa, old_swapchain));
127 }

Referenced by impeller::KHRSwapchainVK::AcquireNextDrawable(), and impeller::KHRSwapchainVK::Create().

◆ DestroySwapchain()

std::pair< vk::UniqueSurfaceKHR, vk::UniqueSwapchainKHR > impeller::KHRSwapchainImplVK::DestroySwapchain ( )

Definition at line 333 of file khr_swapchain_impl_vk.cc.

333  {
334  WaitIdle();
335  is_valid_ = false;
336  synchronizers_.clear();
337  images_.clear();
338  context_.reset();
339  return {std::move(surface_), std::move(swapchain_)};
340 }

Referenced by ~KHRSwapchainImplVK().

◆ GetContext()

std::shared_ptr< Context > impeller::KHRSwapchainImplVK::GetContext ( ) const

Definition at line 346 of file khr_swapchain_impl_vk.cc.

346  {
347  return context_.lock();
348 }

◆ GetSize()

const ISize & impeller::KHRSwapchainImplVK::GetSize ( ) const

Definition at line 317 of file khr_swapchain_impl_vk.cc.

317  {
318  return size_;
319 }

◆ GetSurfaceFormat()

vk::Format impeller::KHRSwapchainImplVK::GetSurfaceFormat ( ) const

Definition at line 342 of file khr_swapchain_impl_vk.cc.

342  {
343  return surface_format_;
344 }

◆ IsValid()

bool impeller::KHRSwapchainImplVK::IsValid ( ) const

Definition at line 321 of file khr_swapchain_impl_vk.cc.

321  {
322  return is_valid_;
323 }

The documentation for this class was generated from the following files:
impeller::KHRSwapchainImplVK::DestroySwapchain
std::pair< vk::UniqueSurfaceKHR, vk::UniqueSwapchainKHR > DestroySwapchain()
Definition: khr_swapchain_impl_vk.cc:333
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13