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"
11 
12 #if FML_OS_ANDROID
14 #endif // FML_OS_ANDROID
15 
16 namespace impeller {
17 
18 std::shared_ptr<SwapchainVK> SwapchainVK::Create(
19  const std::shared_ptr<Context>& context,
20  vk::UniqueSurfaceKHR surface,
21  const ISize& size,
22  bool enable_msaa) {
23  auto swapchain = std::shared_ptr<KHRSwapchainVK>(
24  new KHRSwapchainVK(context, std::move(surface), size, enable_msaa));
25  if (!swapchain->IsValid()) {
26  VALIDATION_LOG << "Could not create valid swapchain.";
27  return nullptr;
28  }
29  return swapchain;
30 }
31 
32 #if FML_OS_ANDROID
33 std::shared_ptr<SwapchainVK> SwapchainVK::Create(
34  const std::shared_ptr<Context>& context,
35  ANativeWindow* p_window,
36  const CreateTransactionCB& cb,
37  bool enable_msaa) {
38  TRACE_EVENT0("impeller", "CreateAndroidSwapchain");
39  if (!context) {
40  return nullptr;
41  }
42 
43  android::NativeWindow window(p_window);
44  if (!window.IsValid()) {
45  return nullptr;
46  }
47 
48  // Use AHB Swapchains if they are opted in.
51  CreateTransactionCB callback =
52  android_get_device_api_level() >= 34 ? cb : CreateTransactionCB({});
53  auto ahb_swapchain = std::shared_ptr<AHBSwapchainVK>(new AHBSwapchainVK(
54  context, //
55  window.GetHandle(), //
56  callback, //
57  window.GetSize(), //
58  enable_msaa //
59  ));
60 
61  if (ahb_swapchain->IsValid()) {
62  return ahb_swapchain;
63  } else {
65  << "Could not create AHB swapchain. Falling back to KHR variant.";
66  }
67  }
68 
69  vk::AndroidSurfaceCreateInfoKHR surface_info;
70  surface_info.setWindow(window.GetHandle());
71  auto [result, surface] =
72  ContextVK::Cast(*context).GetInstance().createAndroidSurfaceKHRUnique(
73  surface_info);
74  if (result != vk::Result::eSuccess) {
75  VALIDATION_LOG << "Could not create KHR Android Surface: "
76  << vk::to_string(result);
77  return nullptr;
78  }
79 
80  // Fallback to KHR swapchains if AHB swapchains aren't available.
81  return Create(context, std::move(surface), window.GetSize(), enable_msaa);
82 }
83 #endif // FML_OS_ANDROID
84 
85 SwapchainVK::SwapchainVK() = default;
86 
87 SwapchainVK::~SwapchainVK() = default;
88 
89 } // namespace impeller
static bool IsAvailableOnPlatform()
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
bool GetShouldEnableSurfaceControlSwapchain() const
Whether the Android Surface control based swapchain should be enabled.
Definition: context_vk.cc:735
vk::Instance GetInstance() const
Definition: context_vk.cc:585
A swapchain implemented backed by VK_KHR_swapchain and VK_KHR_surface.
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::function< android::SurfaceTransaction()> CreateTransactionCB
#define VALIDATION_LOG
Definition: validation.h:91