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 
30 KHRSwapchainVK::~KHRSwapchainVK() = default;
31 
32 bool KHRSwapchainVK::IsValid() const {
33  return impl_ ? impl_->IsValid() : false;
34 }
35 
36 void KHRSwapchainVK::UpdateSurfaceSize(const ISize& size) {
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 void KHRSwapchainVK::AddFinalCommandBuffer(
43  std::shared_ptr<CommandBuffer> cmd_buffer) const {
44  impl_->AddFinalCommandBuffer(std::move(cmd_buffer));
45 }
46 
47 std::unique_ptr<Surface> KHRSwapchainVK::AcquireNextDrawable() {
48  if (!IsValid()) {
49  return nullptr;
50  }
51 
52  TRACE_EVENT0("impeller", __FUNCTION__);
53 
54  auto result = impl_->AcquireNextDrawable();
55  if (!result.out_of_date && size_ == impl_->GetSize()) {
56  return std::move(result.surface);
57  }
58 
59  TRACE_EVENT0("impeller", "RecreateSwapchain");
60 
61  // This swapchain implementation indicates that it is out of date. Tear it
62  // down and make a new one.
63  auto context = impl_->GetContext();
64  auto [surface, old_swapchain] = impl_->DestroySwapchain();
65 
66  auto new_impl = KHRSwapchainImplVK::Create(context, //
67  std::move(surface), //
68  size_, //
69  enable_msaa_, //
70  *old_swapchain //
71  );
72  if (!new_impl || !new_impl->IsValid()) {
73  VALIDATION_LOG << "Could not update swapchain.";
74  // The old swapchain is dead because we took its surface. This is
75  // unrecoverable.
76  impl_.reset();
77  return nullptr;
78  }
79  impl_ = std::move(new_impl);
80 
81  //----------------------------------------------------------------------------
82  /// We managed to recreate the swapchain in the new configuration. Try again.
83  ///
84  return AcquireNextDrawable();
85 }
86 
87 vk::Format KHRSwapchainVK::GetSurfaceFormat() const {
88  return IsValid() ? impl_->GetSurfaceFormat() : vk::Format::eUndefined;
89 }
90 
91 } // namespace impeller
ScopedObject< Object > Create(CtorArgs &&... args)
Definition: object.h:160
ISize64 ISize
Definition: size.h:174
#define VALIDATION_LOG
Definition: validation.h:91