forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulkan_proc_table.cc
219 lines (188 loc) · 6.76 KB
/
vulkan_proc_table.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/vulkan/vulkan_proc_table.h"
#include <dlfcn.h>
#include "flutter/fml/logging.h"
#define ACQUIRE_PROC(name, context) \
if (!(name = AcquireProc("vk" #name, context))) { \
FML_DLOG(INFO) << "Could not acquire proc: vk" << #name; \
return false; \
}
namespace vulkan {
VulkanProcTable::VulkanProcTable()
: handle_(nullptr), acquired_mandatory_proc_addresses_(false) {
acquired_mandatory_proc_addresses_ =
OpenLibraryHandle() && SetupLoaderProcAddresses();
}
VulkanProcTable::~VulkanProcTable() {
CloseLibraryHandle();
}
bool VulkanProcTable::HasAcquiredMandatoryProcAddresses() const {
return acquired_mandatory_proc_addresses_;
}
bool VulkanProcTable::IsValid() const {
return instance_ && device_;
}
bool VulkanProcTable::AreInstanceProcsSetup() const {
return instance_;
}
bool VulkanProcTable::AreDeviceProcsSetup() const {
return device_;
}
bool VulkanProcTable::SetupLoaderProcAddresses() {
if (handle_ == nullptr) {
return true;
}
GetInstanceProcAddr =
#if VULKAN_LINK_STATICALLY
GetInstanceProcAddr = &vkGetInstanceProcAddr;
#else // VULKAN_LINK_STATICALLY
reinterpret_cast<PFN_vkGetInstanceProcAddr>(
dlsym(handle_, "vkGetInstanceProcAddr"));
#endif // VULKAN_LINK_STATICALLY
if (!GetInstanceProcAddr) {
FML_DLOG(WARNING) << "Could not acquire vkGetInstanceProcAddr.";
return false;
}
VulkanHandle<VkInstance> null_instance(VK_NULL_HANDLE, nullptr);
ACQUIRE_PROC(CreateInstance, null_instance);
ACQUIRE_PROC(EnumerateInstanceExtensionProperties, null_instance);
ACQUIRE_PROC(EnumerateInstanceLayerProperties, null_instance);
return true;
}
bool VulkanProcTable::SetupInstanceProcAddresses(
const VulkanHandle<VkInstance>& handle) {
ACQUIRE_PROC(CreateDevice, handle);
ACQUIRE_PROC(DestroyDevice, handle);
ACQUIRE_PROC(DestroyInstance, handle);
ACQUIRE_PROC(EnumerateDeviceLayerProperties, handle);
ACQUIRE_PROC(EnumeratePhysicalDevices, handle);
ACQUIRE_PROC(GetDeviceProcAddr, handle);
ACQUIRE_PROC(GetPhysicalDeviceFeatures, handle);
ACQUIRE_PROC(GetPhysicalDeviceQueueFamilyProperties, handle);
#if OS_ANDROID
ACQUIRE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR, handle);
ACQUIRE_PROC(GetPhysicalDeviceSurfaceFormatsKHR, handle);
ACQUIRE_PROC(GetPhysicalDeviceSurfacePresentModesKHR, handle);
ACQUIRE_PROC(GetPhysicalDeviceSurfaceSupportKHR, handle);
ACQUIRE_PROC(DestroySurfaceKHR, handle);
ACQUIRE_PROC(CreateAndroidSurfaceKHR, handle);
#endif // OS_ANDROID
// The debug report functions are optional. We don't want proc acquisition to
// fail here because the optional methods were not present (since ACQUIRE_PROC
// returns false on failure). Wrap the optional proc acquisitions in an
// anonymous lambda and invoke it. We don't really care about the result since
// users of Debug reporting functions check for their presence explicitly.
[this, &handle]() -> bool {
ACQUIRE_PROC(CreateDebugReportCallbackEXT, handle);
ACQUIRE_PROC(DestroyDebugReportCallbackEXT, handle);
return true;
}();
instance_ = {handle, nullptr};
return true;
}
bool VulkanProcTable::SetupDeviceProcAddresses(
const VulkanHandle<VkDevice>& handle) {
ACQUIRE_PROC(AllocateCommandBuffers, handle);
ACQUIRE_PROC(AllocateMemory, handle);
ACQUIRE_PROC(BeginCommandBuffer, handle);
ACQUIRE_PROC(BindImageMemory, handle);
ACQUIRE_PROC(CmdPipelineBarrier, handle);
ACQUIRE_PROC(CreateCommandPool, handle);
ACQUIRE_PROC(CreateFence, handle);
ACQUIRE_PROC(CreateImage, handle);
ACQUIRE_PROC(CreateSemaphore, handle);
ACQUIRE_PROC(DestroyCommandPool, handle);
ACQUIRE_PROC(DestroyFence, handle);
ACQUIRE_PROC(DestroyImage, handle);
ACQUIRE_PROC(DestroySemaphore, handle);
ACQUIRE_PROC(DeviceWaitIdle, handle);
ACQUIRE_PROC(EndCommandBuffer, handle);
ACQUIRE_PROC(FreeCommandBuffers, handle);
ACQUIRE_PROC(FreeMemory, handle);
ACQUIRE_PROC(GetDeviceQueue, handle);
ACQUIRE_PROC(GetImageMemoryRequirements, handle);
ACQUIRE_PROC(QueueSubmit, handle);
ACQUIRE_PROC(QueueWaitIdle, handle);
ACQUIRE_PROC(ResetCommandBuffer, handle);
ACQUIRE_PROC(ResetFences, handle);
ACQUIRE_PROC(WaitForFences, handle);
#if OS_ANDROID
ACQUIRE_PROC(AcquireNextImageKHR, handle);
ACQUIRE_PROC(CreateSwapchainKHR, handle);
ACQUIRE_PROC(DestroySwapchainKHR, handle);
ACQUIRE_PROC(GetSwapchainImagesKHR, handle);
ACQUIRE_PROC(QueuePresentKHR, handle);
#endif // OS_ANDROID
#if OS_FUCHSIA
ACQUIRE_PROC(GetMemoryFuchsiaHandleKHR, handle);
ACQUIRE_PROC(ImportSemaphoreFuchsiaHandleKHR, handle);
#endif // OS_FUCHSIA
device_ = {handle, nullptr};
return true;
}
bool VulkanProcTable::OpenLibraryHandle() {
#if VULKAN_LINK_STATICALLY
static char kDummyLibraryHandle = '\0';
handle_ = reinterpret_cast<decltype(handle_)>(&kDummyLibraryHandle);
return true;
#else // VULKAN_LINK_STATICALLY
dlerror(); // clear existing errors on thread.
handle_ = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
if (handle_ == nullptr) {
FML_DLOG(WARNING) << "Could not open the vulkan library: " << dlerror();
return false;
}
return true;
#endif // VULKAN_LINK_STATICALLY
}
bool VulkanProcTable::CloseLibraryHandle() {
#if VULKAN_LINK_STATICALLY
handle_ = nullptr;
return true;
#else
if (handle_ != nullptr) {
dlerror(); // clear existing errors on thread.
if (dlclose(handle_) != 0) {
FML_DLOG(ERROR) << "Could not close the vulkan library handle. This "
"indicates a leak.";
FML_DLOG(ERROR) << dlerror();
}
handle_ = nullptr;
}
return handle_ == nullptr;
#endif
}
PFN_vkVoidFunction VulkanProcTable::AcquireProc(
const char* proc_name,
const VulkanHandle<VkInstance>& instance) const {
if (proc_name == nullptr || !GetInstanceProcAddr) {
return nullptr;
}
// A VK_NULL_HANDLE as the instance is an acceptable parameter.
return GetInstanceProcAddr(instance, proc_name);
}
PFN_vkVoidFunction VulkanProcTable::AcquireProc(
const char* proc_name,
const VulkanHandle<VkDevice>& device) const {
if (proc_name == nullptr || !device || !GetDeviceProcAddr) {
return nullptr;
}
return GetDeviceProcAddr(device, proc_name);
}
GrVkGetProc VulkanProcTable::CreateSkiaGetProc() const {
if (!IsValid()) {
return nullptr;
}
return [this](const char* proc_name, VkInstance instance, VkDevice device) {
if (device != VK_NULL_HANDLE) {
auto result = AcquireProc(proc_name, {device, nullptr});
if (result != nullptr) {
return result;
}
}
return AcquireProc(proc_name, {instance, nullptr});
};
}
} // namespace vulkan