| 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/toolkit/interop/backend/vulkan/swapchain_vk.h"
6
7 #include "impeller/renderer/backend/vulkan/context_vk.h"
8
9 namespace impeller::interop {
10
11 SwapchainVK::SwapchainVK(Context& context, VkSurfaceKHR c_surface)
12 : context_(Ref(&context)) {
13 if (!context.IsVulkan()) {
14 VALIDATION_LOG << "Context is not Vulkan.";
15 return;
16 }
17
18 if (!c_surface) {
19 VALIDATION_LOG << "Invalid surface.";
20 return;
21 }
22
23 // Creating a unique object from a raw handle requires fetching the owner
24 // manually.
25 auto surface = vk::UniqueSurfaceKHR(
26 vk::SurfaceKHR{c_surface},
27 impeller::ContextVK::Cast(*context_->GetContext()).GetInstance());
28 auto swapchain = impeller::SwapchainVK::Create(context.GetContext(), //
29 std::move(surface), //
30 ISize::MakeWH(1, 1) //
31 );
32 if (!swapchain) {
33 VALIDATION_LOG << "Could not create Vulkan swapchain.";
34 return;
35 }
36 swapchain_ = std::move(swapchain);
37 }
38
39 SwapchainVK::~SwapchainVK() = default;
40
41 bool SwapchainVK::IsValid() const {
42 return swapchain_ && swapchain_->IsValid();
43 }
44
45 ScopedObject<SurfaceVK> SwapchainVK::AcquireNextSurface() {
46 if (!IsValid()) {
47 return nullptr;
48 }
49
50 auto impeller_surface = swapchain_->AcquireNextDrawable();
51 if (!impeller_surface) {
52 VALIDATION_LOG << "Could not acquire next drawable.";
53 return nullptr;
54 }
55
56 auto surface = Create<SurfaceVK>(*context_, std::move(impeller_surface));
57 if (!surface || !surface->IsValid()) {
58 VALIDATION_LOG << "Could not create valid surface.";
59 return nullptr;
60 }
61
62 return surface;
63 }
64
65 } // namespace impeller::interop
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
impeller::ContextVK::GetInstance
vk::Instance GetInstance() const
Definition: context_vk.cc:585
impeller::SwapchainVK::Create
static std::shared_ptr< SwapchainVK > Create(const std::shared_ptr< Context > &context, vk::UniqueSurfaceKHR surface, const ISize &size, bool enable_msaa=true)
Definition: swapchain_vk.cc:18
impeller::interop::Context
Definition: context.h:16
impeller::interop::Context::GetContext
std::shared_ptr< impeller::Context > GetContext() const
Definition: context.cc:20
impeller::interop::Context::IsVulkan
bool IsVulkan() const
Definition: context.cc:43
impeller::interop::ScopedObject
Definition: object.h:67
impeller::interop::SwapchainVK::SwapchainVK
SwapchainVK(Context &context, VkSurfaceKHR surface)
Definition: swapchain_vk.cc:11
impeller::interop::SwapchainVK::AcquireNextSurface
ScopedObject< SurfaceVK > AcquireNextSurface()
Definition: swapchain_vk.cc:45
impeller::interop::SwapchainVK::IsValid
bool IsValid() const
Definition: swapchain_vk.cc:41
impeller::interop::SwapchainVK::~SwapchainVK
~SwapchainVK()
impeller::interop
Definition: context_gles.cc:12
impeller::interop::Ref
ScopedObject< Object > Ref(Object *object)
Definition: object.h:146
context_vk.h
impeller::TSize::MakeWH
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:43
swapchain_vk.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91