| 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/swapchain_vk.h"
6
7 #include "flutter/fml/trace_event.h"
8 #include "impeller/renderer/backend/vulkan/context_vk.h"
9 #include "impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_vk.h"
10 #include "impeller/renderer/backend/vulkan/vk.h"
11
12 #if FML_OS_ANDROID
13 #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h"
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 vk::AndroidSurfaceCreateInfoKHR surface_info;
49 surface_info.setWindow(window.GetHandle());
50 auto [result, surface] =
51 ContextVK::Cast(*context).GetInstance().createAndroidSurfaceKHRUnique(
52 surface_info);
53 if (result != vk::Result::eSuccess) {
54 VALIDATION_LOG << "Could not create KHR Android Surface: "
55 << vk::to_string(result);
56 return nullptr;
57 }
58
59 // Use AHB Swapchains if they are opted in.
60 if (ContextVK::Cast(*context).GetShouldEnableSurfaceControlSwapchain() &&
61 AHBSwapchainVK::IsAvailableOnPlatform() &&
62 android_get_device_api_level() >= 34) {
63 FML_LOG(WARNING) << "Using Android SurfaceControl Swapchain.";
64 auto ahb_swapchain = std::shared_ptr<AHBSwapchainVK>(new AHBSwapchainVK(
65 context, //
66 window.GetHandle(), //
67 cb, //
68 surface, //
69 window.GetSize(), //
70 enable_msaa //
71 ));
72
73 if (ahb_swapchain->IsValid()) {
74 return ahb_swapchain;
75 } else {
76 VALIDATION_LOG
77 << "Could not create AHB swapchain. Falling back to KHR variant.";
78 }
79 }
80
81 // Fallback to KHR swapchains if AHB swapchains aren't available.
82 return Create(context, std::move(surface), window.GetSize(), enable_msaa);
83 }
84 #endif // FML_OS_ANDROID
85
86 SwapchainVK::SwapchainVK() = default;
87
88 SwapchainVK::~SwapchainVK() = default;
89
90 } // namespace impeller
ahb_swapchain_vk.h
impeller::AHBSwapchainVK::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: ahb_swapchain_vk.cc:15
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
impeller::ContextVK::GetShouldEnableSurfaceControlSwapchain
bool GetShouldEnableSurfaceControlSwapchain() const
Whether the Android Surface control based swapchain should be enabled.
Definition: context_vk.cc:737
impeller::ContextVK::GetInstance
vk::Instance GetInstance() const
Definition: context_vk.cc:587
impeller::KHRSwapchainVK
A swapchain implemented backed by VK_KHR_swapchain and VK_KHR_surface.
Definition: khr_swapchain_vk.h:24
impeller::SwapchainVK::~SwapchainVK
virtual ~SwapchainVK()
impeller::SwapchainVK::SwapchainVK
SwapchainVK()
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
khr_swapchain_vk.h
impeller
Definition: allocation.cc:12
impeller::CreateTransactionCB
std::function< android::SurfaceTransaction()> CreateTransactionCB
Definition: ahb_swapchain_impl_vk.h:24
context_vk.h
swapchain_vk.h
impeller::TSize
Definition: size.h:25
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
vk.h