Flutter Impeller
khr_swapchain_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"
10 
11 namespace impeller {
12 
13 KHRSwapchainVK::KHRSwapchainVK(const std::shared_ptr<Context>& context,
14  vk::UniqueSurfaceKHR surface,
15  const ISize& size,
16  bool enable_msaa)
17  : size_(size), enable_msaa_(enable_msaa) {
18  auto impl = KHRSwapchainImplVK::Create(context, //
19  std::move(surface), //
20  size_, //
21  enable_msaa_ //
22  );
23  if (!impl || !impl->IsValid()) {
24  VALIDATION_LOG << "Failed to create SwapchainVK implementation.";
25  return;
26  }
27  impl_ = std::move(impl);
28 }
29 
31 
33  return impl_ ? impl_->IsValid() : false;
34 }
35 
37  // Update the size of the swapchain. On the next acquired drawable,
38  // the sizes may no longer match, forcing the swapchain to be recreated.
39  size_ = size;
40 }
41 
42 std::unique_ptr<Surface> KHRSwapchainVK::AcquireNextDrawable() {
43  if (!IsValid()) {
44  return nullptr;
45  }
46 
47  TRACE_EVENT0("impeller", __FUNCTION__);
48 
49  auto result = impl_->AcquireNextDrawable();
50  if (!result.out_of_date && size_ == impl_->GetSize()) {
51  return std::move(result.surface);
52  }
53 
54  TRACE_EVENT0("impeller", "RecreateSwapchain");
55 
56  // This swapchain implementation indicates that it is out of date. Tear it
57  // down and make a new one.
58  auto context = impl_->GetContext();
59  auto [surface, old_swapchain] = impl_->DestroySwapchain();
60 
61  auto new_impl = KHRSwapchainImplVK::Create(context, //
62  std::move(surface), //
63  size_, //
64  enable_msaa_, //
65  *old_swapchain //
66  );
67  if (!new_impl || !new_impl->IsValid()) {
68  VALIDATION_LOG << "Could not update swapchain.";
69  // The old swapchain is dead because we took its surface. This is
70  // unrecoverable.
71  impl_.reset();
72  return nullptr;
73  }
74  impl_ = std::move(new_impl);
75 
76  //----------------------------------------------------------------------------
77  /// We managed to recreate the swapchain in the new configuration. Try again.
78  ///
79  return AcquireNextDrawable();
80 }
81 
83  return IsValid() ? impl_->GetSurfaceFormat() : vk::Format::eUndefined;
84 }
85 
86 } // namespace impeller
khr_swapchain_vk.h
impeller::ISize
ISize64 ISize
Definition: size.h:140
impeller::KHRSwapchainVK::IsValid
bool IsValid() const override
Definition: khr_swapchain_vk.cc:32
khr_swapchain_impl_vk.h
impeller::KHRSwapchainImplVK::AcquireResult::surface
std::unique_ptr< Surface > surface
Definition: khr_swapchain_impl_vk.h:46
impeller::KHRSwapchainVK::UpdateSurfaceSize
void UpdateSurfaceSize(const ISize &size) override
Mark the current swapchain configuration as dirty, forcing it to be recreated on the next frame.
Definition: khr_swapchain_vk.cc:36
impeller::KHRSwapchainImplVK::AcquireNextDrawable
AcquireResult AcquireNextDrawable()
Definition: khr_swapchain_impl_vk.cc:314
impeller::KHRSwapchainVK::~KHRSwapchainVK
~KHRSwapchainVK()
validation.h
impeller::TSize
Definition: size.h:19
impeller::KHRSwapchainVK::GetSurfaceFormat
vk::Format GetSurfaceFormat() const override
Definition: khr_swapchain_vk.cc:82
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::KHRSwapchainImplVK::IsValid
bool IsValid() const
Definition: khr_swapchain_impl_vk.cc:285
impeller::KHRSwapchainImplVK::Create
static std::shared_ptr< 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)
Definition: khr_swapchain_impl_vk.cc:118
impeller::KHRSwapchainVK::AcquireNextDrawable
std::unique_ptr< Surface > AcquireNextDrawable() override
Definition: khr_swapchain_vk.cc:42
impeller
Definition: aiks_blend_unittests.cc:18