Flutter Impeller
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<SwapchainVK> SwapchainVK::Create(
14  const std::shared_ptr<Context>& context,
15  vk::UniqueSurfaceKHR surface) {
16  auto impl = SwapchainImplVK::Create(context, std::move(surface));
17  if (!impl || !impl->IsValid()) {
18  VALIDATION_LOG << "Failed to create SwapchainVK implementation.";
19  return nullptr;
20  }
21  return std::shared_ptr<SwapchainVK>(new SwapchainVK(std::move(impl)));
22 }
23 
24 SwapchainVK::SwapchainVK(std::shared_ptr<SwapchainImplVK> impl)
25  : impl_(std::move(impl)) {}
26 
27 SwapchainVK::~SwapchainVK() = default;
28 
29 bool SwapchainVK::IsValid() const {
30  return impl_ ? impl_->IsValid() : false;
31 }
32 
33 std::unique_ptr<Surface> SwapchainVK::AcquireNextDrawable() {
34  if (!IsValid()) {
35  return nullptr;
36  }
37 
38  TRACE_EVENT0("impeller", __FUNCTION__);
39 
40  auto result = impl_->AcquireNextDrawable();
41  if (!result.out_of_date) {
42  return std::move(result.surface);
43  }
44 
45  TRACE_EVENT0("impeller", "RecreateSwapchain");
46 
47  // This swapchain implementation indicates that it is out of date. Tear it
48  // down and make a new one.
49  auto context = impl_->GetContext();
50  auto [surface, old_swapchain] = impl_->DestroySwapchain();
51 
52  auto new_impl = SwapchainImplVK::Create(context, //
53  std::move(surface), //
54  *old_swapchain, //
55  impl_->GetLastTransform() //
56  );
57  if (!new_impl || !new_impl->IsValid()) {
58  VALIDATION_LOG << "Could not update swapchain.";
59  // The old swapchain is dead because we took its surface. This is
60  // unrecoverable.
61  impl_.reset();
62  return nullptr;
63  }
64  impl_ = std::move(new_impl);
65 
66  //----------------------------------------------------------------------------
67  /// We managed to recreate the swapchain in the new configuration. Try again.
68  ///
69  return AcquireNextDrawable();
70 }
71 
72 vk::Format SwapchainVK::GetSurfaceFormat() const {
73  return IsValid() ? impl_->GetSurfaceFormat() : vk::Format::eUndefined;
74 }
75 
76 } // namespace impeller
swapchain_vk.h
impeller::SwapchainVK::Create
static std::shared_ptr< SwapchainVK > Create(const std::shared_ptr< Context > &context, vk::UniqueSurfaceKHR surface)
Definition: swapchain_vk.cc:13
validation.h
impeller::SwapchainVK::IsValid
bool IsValid() const
Definition: swapchain_vk.cc:29
impeller::SwapchainVK::GetSurfaceFormat
vk::Format GetSurfaceFormat() const
Definition: swapchain_vk.cc:72
impeller::SwapchainImplVK::Create
static std::shared_ptr< SwapchainImplVK > Create(const std::shared_ptr< Context > &context, vk::UniqueSurfaceKHR surface, vk::SwapchainKHR old_swapchain=VK_NULL_HANDLE, vk::SurfaceTransformFlagBitsKHR last_transform=vk::SurfaceTransformFlagBitsKHR::eIdentity)
Definition: swapchain_impl_vk.cc:140
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::SwapchainVK
A swapchain that adapts to the underlying surface going out of date. If the caller cannot acquire the...
Definition: swapchain_vk.h:25
std
Definition: comparable.h:95
impeller
Definition: aiks_context.cc:10
impeller::SwapchainVK::AcquireNextDrawable
std::unique_ptr< Surface > AcquireNextDrawable()
Definition: swapchain_vk.cc:33
swapchain_impl_vk.h
impeller::SwapchainVK::~SwapchainVK
~SwapchainVK()