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