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