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 271 of file khr_swapchain_impl_vk.cc.

271  {
273 }

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 308 of file khr_swapchain_impl_vk.cc.

308  {
309  auto context_strong = context_.lock();
310  if (!context_strong) {
311  return KHRSwapchainImplVK::AcquireResult{};
312  }
313 
314  const auto& context = ContextVK::Cast(*context_strong);
315 
316  current_frame_ = (current_frame_ + 1u) % synchronizers_.size();
317 
318  const auto& sync = synchronizers_[current_frame_];
319 
320  //----------------------------------------------------------------------------
321  /// Wait on the host for the synchronizer fence.
322  ///
323  if (!sync->WaitForFence(context.GetDevice())) {
324  VALIDATION_LOG << "Could not wait for fence.";
325  return KHRSwapchainImplVK::AcquireResult{};
326  }
327 
328  //----------------------------------------------------------------------------
329  /// Get the next image index.
330  ///
331  /// @bug Non-infinite timeouts are not supported on some older Android
332  /// devices and the only indication we get is log spam which serves to
333  /// add confusion. Just use an infinite timeout instead of being
334  /// defensive.
335  auto [acq_result, index] = context.GetDevice().acquireNextImageKHR(
336  *swapchain_, // swapchain
337  std::numeric_limits<uint64_t>::max(), // timeout (ns)
338  *sync->render_ready, // signal semaphore
339  nullptr // fence
340  );
341 
342  switch (acq_result) {
343  case vk::Result::eSuccess:
344  // Keep going.
345  break;
346  case vk::Result::eSuboptimalKHR:
347  case vk::Result::eErrorOutOfDateKHR:
348  // A recoverable error. Just say we are out of date.
349  return AcquireResult{true /* out of date */};
350  break;
351  default:
352  // An unrecoverable error.
353  VALIDATION_LOG << "Could not acquire next swapchain image: "
354  << vk::to_string(acq_result);
355  return AcquireResult{false /* out of date */};
356  }
357 
358  if (index >= images_.size()) {
359  VALIDATION_LOG << "Swapchain returned an invalid image index.";
360  return KHRSwapchainImplVK::AcquireResult{};
361  }
362 
363  /// Record all subsequent cmd buffers as part of the current frame.
364  context.GetGPUTracer()->MarkFrameStart();
365 
366  auto image = images_[index % images_.size()];
367  uint32_t image_index = index;
368  return AcquireResult{SurfaceVK::WrapSwapchainImage(
369  transients_, // transients
370  image, // swapchain image
371  [weak_swapchain = weak_from_this(), image, image_index]() -> bool {
372  auto swapchain = weak_swapchain.lock();
373  if (!swapchain) {
374  return false;
375  }
376  return swapchain->Present(image, image_index);
377  } // swap callback
378  )};
379 }

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

Referenced by impeller::KHRSwapchainVK::AcquireNextDrawable().

◆ 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 112 of file khr_swapchain_impl_vk.cc.

117  {
118  return std::shared_ptr<KHRSwapchainImplVK>(new KHRSwapchainImplVK(
119  context, std::move(surface), size, enable_msaa, old_swapchain));
120 }

Referenced by impeller::KHRSwapchainVK::AcquireNextDrawable().

◆ DestroySwapchain()

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

Definition at line 291 of file khr_swapchain_impl_vk.cc.

291  {
292  WaitIdle();
293  is_valid_ = false;
294  synchronizers_.clear();
295  images_.clear();
296  context_.reset();
297  return {std::move(surface_), std::move(swapchain_)};
298 }

Referenced by ~KHRSwapchainImplVK().

◆ GetContext()

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

Definition at line 304 of file khr_swapchain_impl_vk.cc.

304  {
305  return context_.lock();
306 }

◆ GetSize()

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

Definition at line 275 of file khr_swapchain_impl_vk.cc.

275  {
276  return size_;
277 }

◆ GetSurfaceFormat()

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

Definition at line 300 of file khr_swapchain_impl_vk.cc.

300  {
301  return surface_format_;
302 }

◆ IsValid()

bool impeller::KHRSwapchainImplVK::IsValid ( ) const

Definition at line 279 of file khr_swapchain_impl_vk.cc.

279  {
280  return is_valid_;
281 }

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


The documentation for this class was generated from the following files:
impeller::SurfaceVK::WrapSwapchainImage
static std::unique_ptr< SurfaceVK > WrapSwapchainImage(const std::shared_ptr< SwapchainTransientsVK > &transients, const std::shared_ptr< TextureSourceVK > &swapchain_image, SwapCallback swap_callback)
Wrap the swapchain image in a Surface, which provides the additional configuration required for usage...
Definition: surface_vk.cc:13
impeller::KHRSwapchainImplVK::DestroySwapchain
std::pair< vk::UniqueSurfaceKHR, vk::UniqueSwapchainKHR > DestroySwapchain()
Definition: khr_swapchain_impl_vk.cc:291
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13