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 
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 
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
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
vk::Instance GetInstance() const
Definition: context_vk.cc:585
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
std::shared_ptr< impeller::Context > GetContext() const
Definition: context.cc:20
bool IsVulkan() const
Definition: context.cc:43
SwapchainVK(Context &context, VkSurfaceKHR surface)
Definition: swapchain_vk.cc:11
ScopedObject< SurfaceVK > AcquireNextSurface()
Definition: swapchain_vk.cc:45
ScopedObject< Object > Ref(Object *object)
Definition: object.h:146
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:43
#define VALIDATION_LOG
Definition: validation.h:91