blob: fe3311445a62c1398a11cc5ec882ff1787b57bdc [file] [log] [blame]
Jesse Halld02edcb2015-09-08 07:44:48 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Lentine9dbe67f2015-09-16 15:53:50 -050017//#define LOG_NDEBUG 0
18
Jesse Hall04f4f472015-08-16 19:51:04 -070019// module header
20#include "loader.h"
21// standard C headers
Michael Lentine03c64b02015-08-26 18:27:26 -050022#include <dirent.h>
23#include <dlfcn.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070024#include <inttypes.h>
25#include <malloc.h>
26#include <pthread.h>
27#include <string.h>
28// standard C++ headers
29#include <algorithm>
30#include <mutex>
Michael Lentine03c64b02015-08-26 18:27:26 -050031#include <sstream>
32#include <string>
33#include <unordered_map>
34#include <vector>
Jesse Hall04f4f472015-08-16 19:51:04 -070035// platform/library headers
Michael Lentine03c64b02015-08-26 18:27:26 -050036#include <cutils/properties.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070037#include <hardware/hwvulkan.h>
38#include <log/log.h>
Michael Lentinecd6cabf2015-09-14 17:32:59 -050039#include <vulkan/vk_debug_report_lunarg.h>
Michael Lentine1c69b9e2015-09-14 13:26:59 -050040#include <vulkan/vulkan_loader_data.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070041
42using namespace vulkan;
43
44static const uint32_t kMaxPhysicalDevices = 4;
45
Michael Lentine03c64b02015-08-26 18:27:26 -050046namespace {
47
48// These definitions are taken from the LunarG Vulkan Loader. They are used to
49// enforce compatability between the Loader and Layers.
50typedef void* (*PFN_vkGetProcAddr)(void* obj, const char* pName);
51
52typedef struct VkLayerLinkedListElem_ {
53 PFN_vkGetProcAddr get_proc_addr;
54 void* next_element;
55 void* base_object;
56} VkLayerLinkedListElem;
57
58// Define Handle typedef to be void* as returned from dlopen.
59typedef void* SharedLibraryHandle;
60
61// Custom versions of std classes that use the vulkan alloc callback.
62template <class T>
63class CallbackAllocator {
64 public:
65 typedef T value_type;
66
67 CallbackAllocator(const VkAllocCallbacks* alloc_input)
68 : alloc(alloc_input) {}
69
70 template <class T2>
71 CallbackAllocator(const CallbackAllocator<T2>& other)
72 : alloc(other.alloc) {}
73
74 T* allocate(std::size_t n) {
75 void* mem = alloc->pfnAlloc(alloc->pUserData, n * sizeof(T), alignof(T),
76 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
77 return static_cast<T*>(mem);
78 }
79
80 void deallocate(T* array, std::size_t /*n*/) {
81 alloc->pfnFree(alloc->pUserData, array);
82 }
83
84 const VkAllocCallbacks* alloc;
85};
86// These are needed in order to move Strings
87template <class T>
88bool operator==(const CallbackAllocator<T>& alloc1,
89 const CallbackAllocator<T>& alloc2) {
90 return alloc1.alloc == alloc2.alloc;
91}
92template <class T>
93bool operator!=(const CallbackAllocator<T>& alloc1,
94 const CallbackAllocator<T>& alloc2) {
95 return !(alloc1 == alloc2);
96}
97
98template <class Key,
99 class T,
100 class Hash = std::hash<Key>,
101 class Pred = std::equal_to<Key> >
102using UnorderedMap =
103 std::unordered_map<Key,
104 T,
105 Hash,
106 Pred,
107 CallbackAllocator<std::pair<const Key, T> > >;
108
109template <class T>
110using Vector = std::vector<T, CallbackAllocator<T> >;
111
112typedef std::basic_string<char,
113 std::char_traits<char>,
114 CallbackAllocator<char> > String;
115
116} // namespace
117
118// -----------------------------------------------------------------------------
119
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500120namespace {
121
122struct LayerData {
123 String path;
124 SharedLibraryHandle handle;
125 uint32_t ref_count;
126};
127
128typedef UnorderedMap<String, LayerData>::iterator LayerMapIterator;
129
130} // namespace
131
Jesse Hall04f4f472015-08-16 19:51:04 -0700132struct VkInstance_T {
133 VkInstance_T(const VkAllocCallbacks* alloc_callbacks)
Michael Lentine03c64b02015-08-26 18:27:26 -0500134 : vtbl(&vtbl_storage),
135 alloc(alloc_callbacks),
136 num_physical_devices(0),
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500137 layers(CallbackAllocator<std::pair<String, LayerData> >(alloc)),
138 active_layers(CallbackAllocator<String>(alloc)) {
139 pthread_mutex_init(&layer_lock, 0);
Jesse Hall04f4f472015-08-16 19:51:04 -0700140 memset(&vtbl_storage, 0, sizeof(vtbl_storage));
141 memset(physical_devices, 0, sizeof(physical_devices));
142 memset(&drv.vtbl, 0, sizeof(drv.vtbl));
143 drv.GetDeviceProcAddr = nullptr;
144 drv.num_physical_devices = 0;
145 }
146
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500147 ~VkInstance_T() { pthread_mutex_destroy(&layer_lock); }
148
Jesse Hall04f4f472015-08-16 19:51:04 -0700149 InstanceVtbl* vtbl;
150 InstanceVtbl vtbl_storage;
151
152 const VkAllocCallbacks* alloc;
153 uint32_t num_physical_devices;
154 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
155
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500156 pthread_mutex_t layer_lock;
157 // Map of layer names to layer data
158 UnorderedMap<String, LayerData> layers;
159 // Vector of layers active for this instance
160 Vector<LayerMapIterator> active_layers;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500161 VkDbgMsgCallback message;
Michael Lentine03c64b02015-08-26 18:27:26 -0500162
Jesse Hall04f4f472015-08-16 19:51:04 -0700163 struct Driver {
164 // Pointers to driver entry points. Used explicitly by the loader; not
165 // set as the dispatch table for any objects.
166 InstanceVtbl vtbl;
167
168 // Pointer to the driver's get_device_proc_addr, must be valid for any
169 // of the driver's physical devices. Not part of the InstanceVtbl since
170 // it's not an Instance/PhysicalDevice function.
171 PFN_vkGetDeviceProcAddr GetDeviceProcAddr;
172
173 // Number of physical devices owned by this driver.
174 uint32_t num_physical_devices;
175 } drv; // may eventually be an array
176};
177
178// -----------------------------------------------------------------------------
179
180namespace {
181
182typedef VkInstance_T Instance;
183
184struct Device {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500185 Device(Instance* instance_input)
186 : instance(instance_input),
187 active_layers(CallbackAllocator<LayerMapIterator>(instance->alloc)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700188 memset(&vtbl_storage, 0, sizeof(vtbl_storage));
189 vtbl_storage.device = this;
190 }
191 DeviceVtbl vtbl_storage;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500192 Instance* instance;
193 // Vector of layers active for this device
194 Vector<LayerMapIterator> active_layers;
Jesse Hall04f4f472015-08-16 19:51:04 -0700195};
196
197// -----------------------------------------------------------------------------
198// Utility Code
199
200inline const InstanceVtbl* GetVtbl(VkPhysicalDevice physicalDevice) {
201 return *reinterpret_cast<InstanceVtbl**>(physicalDevice);
202}
203
204inline const DeviceVtbl* GetVtbl(VkDevice device) {
205 return *reinterpret_cast<DeviceVtbl**>(device);
206}
Jesse Halld7b994a2015-09-07 14:17:37 -0700207inline const DeviceVtbl* GetVtbl(VkQueue queue) {
208 return *reinterpret_cast<DeviceVtbl**>(queue);
209}
Jesse Hall04f4f472015-08-16 19:51:04 -0700210
211void* DefaultAlloc(void*, size_t size, size_t alignment, VkSystemAllocType) {
212 return memalign(alignment, size);
213}
214
215void DefaultFree(void*, void* pMem) {
216 free(pMem);
217}
218
219const VkAllocCallbacks kDefaultAllocCallbacks = {
220 .pUserData = nullptr,
221 .pfnAlloc = DefaultAlloc,
222 .pfnFree = DefaultFree,
223};
224
225hwvulkan_device_t* g_hwdevice;
226bool EnsureInitialized() {
227 static std::once_flag once_flag;
228 static const hwvulkan_module_t* module;
229
230 std::call_once(once_flag, []() {
231 int result;
232 result = hw_get_module("vulkan",
233 reinterpret_cast<const hw_module_t**>(&module));
234 if (result != 0) {
235 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result),
236 result);
237 return;
238 }
239 result = module->common.methods->open(
240 &module->common, HWVULKAN_DEVICE_0,
241 reinterpret_cast<hw_device_t**>(&g_hwdevice));
242 if (result != 0) {
243 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
244 result);
245 module = nullptr;
246 return;
247 }
248 });
249
250 return module != nullptr && g_hwdevice != nullptr;
251}
252
253void DestroyDevice(Device* device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500254 const VkAllocCallbacks* alloc = device->instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700255 device->~Device();
256 alloc->pfnFree(alloc->pUserData, device);
257}
258
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500259void FindLayersInDirectory(Instance& instance, const String& dir_name) {
Jesse Hall0ecdd3e2015-10-29 11:20:07 -0700260 DIR* directory = opendir(dir_name.c_str());
261 if (!directory) {
262 android_LogPriority log_priority =
263 (errno == ENOENT) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_ERROR;
264 LOG_PRI(log_priority, LOG_TAG,
265 "failed to open layer directory '%s': %s (%d)",
266 dir_name.c_str(), strerror(errno), errno);
267 return;
Michael Lentine03c64b02015-08-26 18:27:26 -0500268 }
Jesse Hall0ecdd3e2015-10-29 11:20:07 -0700269
270 Vector<VkLayerProperties> properties(
271 CallbackAllocator<VkLayerProperties>(instance.alloc));
272 struct dirent* entry;
273 while ((entry = readdir(directory))) {
274 size_t length = strlen(entry->d_name);
275 if (strncmp(entry->d_name, "libVKLayer", 10) != 0 ||
276 strncmp(entry->d_name + length - 3, ".so", 3) != 0)
277 continue;
278 // Open so
279 SharedLibraryHandle layer_handle =
280 dlopen((dir_name + entry->d_name).c_str(), RTLD_NOW | RTLD_LOCAL);
281 if (!layer_handle) {
282 ALOGE("%s failed to load with error %s; Skipping", entry->d_name,
283 dlerror());
284 continue;
285 }
286
287 // Get Layers in so
288 PFN_vkEnumerateInstanceLayerProperties get_layer_properties =
289 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
290 dlsym(layer_handle, "vkEnumerateInstanceLayerProperties"));
291 if (!get_layer_properties) {
292 ALOGE(
293 "%s failed to find vkEnumerateInstanceLayerProperties with "
294 "error %s; Skipping",
295 entry->d_name, dlerror());
296 dlclose(layer_handle);
297 continue;
298 }
299 uint32_t count;
300 get_layer_properties(&count, nullptr);
301
302 properties.resize(count);
303 get_layer_properties(&count, &properties[0]);
304
305 // Add Layers to potential list
306 for (uint32_t i = 0; i < count; ++i) {
307 String layer_name(properties[i].layerName,
308 CallbackAllocator<char>(instance.alloc));
309 LayerData layer_data = {dir_name + entry->d_name, 0, 0};
310 instance.layers.insert(std::make_pair(layer_name, layer_data));
311 ALOGV("Found layer %s", properties[i].layerName);
312 }
313 dlclose(layer_handle);
314 }
315
316 closedir(directory);
Michael Lentine03c64b02015-08-26 18:27:26 -0500317}
318
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500319template <class TObject>
320void ActivateLayer(TObject* object, Instance* instance, const String& name) {
321 // If object has layer, do nothing
322 auto element = instance->layers.find(name);
Michael Lentine233ac732015-11-18 18:28:07 -0800323 if (element == instance->layers.end()) {
324 return;
325 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500326 if (std::find(object->active_layers.begin(), object->active_layers.end(),
327 element) != object->active_layers.end()) {
328 ALOGW("Layer %s already activated; skipping", name.c_str());
329 return;
330 }
331 // If layer is not open, open it
332 LayerData& layer_data = element->second;
333 pthread_mutex_lock(&instance->layer_lock);
334 if (layer_data.ref_count == 0) {
335 SharedLibraryHandle layer_handle =
336 dlopen(layer_data.path.c_str(), RTLD_NOW | RTLD_LOCAL);
337 if (!layer_handle) {
338 pthread_mutex_unlock(&instance->layer_lock);
339 ALOGE("%s failed to load with error %s; Skipping",
340 layer_data.path.c_str(), dlerror());
341 return;
342 }
343 layer_data.handle = layer_handle;
344 }
345 layer_data.ref_count++;
346 pthread_mutex_unlock(&instance->layer_lock);
347 ALOGV("Activating layer %s", name.c_str());
348 object->active_layers.push_back(element);
349}
350
Michael Lentine1d1e65f2015-11-19 14:23:06 -0800351void DeactivateLayer(Instance* instance,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500352 Vector<LayerMapIterator>::iterator& element) {
353 LayerMapIterator& layer_map_data = *element;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500354 LayerData& layer_data = layer_map_data->second;
355 pthread_mutex_lock(&instance->layer_lock);
356 layer_data.ref_count--;
357 if (!layer_data.ref_count) {
358 dlclose(layer_data.handle);
359 }
360 pthread_mutex_unlock(&instance->layer_lock);
361}
362
Michael Lentine9da191b2015-10-13 11:08:45 -0500363struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500364 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500365 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500366};
367
Michael Lentine9da191b2015-10-13 11:08:45 -0500368void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500369 const char* value,
370 void* data) {
371 const char prefix[] = "debug.vulkan.layer.";
372 const size_t prefixlen = sizeof(prefix) - 1;
373 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
374 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500375 const char* number_str = name + prefixlen;
376 long layer_number = strtol(number_str, nullptr, 10);
377 if (layer_number <= 0 || layer_number == LONG_MAX) {
378 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
379 number_str);
380 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500381 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500382 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
383 Vector<String>* layer_names = instance_names_pair->layer_names;
384 Instance* instance = instance_names_pair->instance;
385 size_t layer_size = static_cast<size_t>(layer_number);
386 if (layer_size > layer_names->size()) {
387 layer_names->resize(layer_size,
388 String(CallbackAllocator<char>(instance->alloc)));
389 }
390 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500391}
392
393template <class TInfo, class TObject>
Jesse Hall9a16f972015-10-28 15:59:53 -0700394VkResult ActivateAllLayers(TInfo create_info, Instance* instance, TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500395 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
396 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
397 "Cannot activate layers for unknown object %p", object);
398 CallbackAllocator<char> string_allocator(instance->alloc);
399 // Load system layers
400 {
401 char layer_prop[PROPERTY_VALUE_MAX];
402 property_get("debug.vulkan.layers", layer_prop, "");
403 String layer_name(string_allocator);
404 String layer_prop_str(layer_prop, string_allocator);
405 size_t end, start = 0;
406 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
407 layer_name = layer_prop_str.substr(start, end - start);
Michael Lentine233ac732015-11-18 18:28:07 -0800408 ActivateLayer(object, instance, layer_name);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500409 start = end + 1;
410 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500411 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
412 InstanceNamesPair instance_names_pair = {.instance = instance,
413 .layer_names = &layer_names};
414 property_list(SetLayerNamesFromProperty,
415 static_cast<void*>(&instance_names_pair));
416 for (auto layer_name_element : layer_names) {
417 ActivateLayer(object, instance, layer_name_element);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500418 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500419 }
420 // Load app layers
421 for (uint32_t i = 0; i < create_info->layerCount; ++i) {
422 String layer_name(create_info->ppEnabledLayerNames[i],
423 string_allocator);
424 auto element = instance->layers.find(layer_name);
425 if (element == instance->layers.end()) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700426 ALOGE("requested %s layer '%s' not present",
427 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ?
428 "instance" : "device",
429 layer_name.c_str());
430 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500431 } else {
432 ActivateLayer(object, instance, layer_name);
433 }
434 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700435 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500436}
437
438template <class TCreateInfo>
439bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
440 const char* extension_name,
441 const VkAllocCallbacks* alloc) {
442 for (uint32_t i = 0; i < local_create_info.extensionCount; ++i) {
443 if (!strcmp(extension_name,
444 local_create_info.ppEnabledExtensionNames[i])) {
445 return false;
446 }
447 }
448 uint32_t extension_count = local_create_info.extensionCount;
449 local_create_info.extensionCount++;
450 void* mem = alloc->pfnAlloc(
451 alloc->pUserData, local_create_info.extensionCount * sizeof(char*),
452 alignof(char*), VK_SYSTEM_ALLOC_TYPE_INTERNAL);
453 if (mem) {
454 const char** enabled_extensions = static_cast<const char**>(mem);
455 for (uint32_t i = 0; i < extension_count; ++i) {
456 enabled_extensions[i] =
457 local_create_info.ppEnabledExtensionNames[i];
458 }
459 enabled_extensions[extension_count] = extension_name;
460 local_create_info.ppEnabledExtensionNames = enabled_extensions;
461 } else {
462 ALOGW("%s extension cannot be enabled: memory allocation failed",
463 extension_name);
464 local_create_info.extensionCount--;
465 return false;
466 }
467 return true;
468}
469
470template <class T>
471void FreeAllocatedCreateInfo(T& local_create_info,
472 const VkAllocCallbacks* alloc) {
473 alloc->pfnFree(
474 alloc->pUserData,
475 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
476}
477
Michael Lentineeb970862015-10-15 12:42:22 -0500478VkBool32 LogDebugMessageCallback(VkFlags message_flags,
479 VkDbgObjectType /*obj_type*/,
480 uint64_t /*src_object*/,
481 size_t /*location*/,
482 int32_t message_code,
483 const char* layer_prefix,
484 const char* message,
485 void* /*user_data*/) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500486 if (message_flags & VK_DBG_REPORT_ERROR_BIT) {
487 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
488 } else if (message_flags & VK_DBG_REPORT_WARN_BIT) {
489 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
490 }
Michael Lentineeb970862015-10-15 12:42:22 -0500491 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500492}
493
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800494VkResult Noop(...) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500495 return VK_SUCCESS;
496}
497
498PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) {
499 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
500 return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr);
501 }
502 if (strcmp(name, "vkCreateDevice") == 0) {
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800503 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
Michael Lentine03c64b02015-08-26 18:27:26 -0500504 }
Michael Lentine88594d72015-11-12 12:49:45 -0800505 // WSI extensions are not in the driver so return the loader functions
506 if (strcmp(name, "vkGetSurfacePropertiesKHR") == 0) {
507 return reinterpret_cast<PFN_vkVoidFunction>(GetSurfacePropertiesKHR);
508 }
509 if (strcmp(name, "vkGetSurfaceFormatsKHR") == 0) {
510 return reinterpret_cast<PFN_vkVoidFunction>(GetSurfaceFormatsKHR);
511 }
512 if (strcmp(name, "vkGetSurfacePresentModesKHR") == 0) {
513 return reinterpret_cast<PFN_vkVoidFunction>(GetSurfacePresentModesKHR);
514 }
515 if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
516 return reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR);
517 }
518 if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
519 return reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR);
520 }
521 if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
522 return reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR);
523 }
524 if (strcmp(name, "vkAcquireNextImageKHR") == 0) {
525 return reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR);
526 }
527 if (strcmp(name, "vkQueuePresentKHR") == 0) {
528 return reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR);
529 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500530 if (!device)
531 return GetGlobalDeviceProcAddr(name);
532 Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500533 return loader_device->instance->drv.GetDeviceProcAddr(device, name);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500534}
535
Jesse Hall04f4f472015-08-16 19:51:04 -0700536// -----------------------------------------------------------------------------
537// "Bottom" functions. These are called at the end of the instance dispatch
538// chain.
539
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700540void DestroyInstanceBottom(VkInstance instance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700541 // These checks allow us to call DestroyInstanceBottom from any error path
542 // in CreateInstanceBottom, before the driver instance is fully initialized.
543 if (instance->drv.vtbl.instance != VK_NULL_HANDLE &&
544 instance->drv.vtbl.DestroyInstance) {
545 instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance);
546 }
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500547 if (instance->message) {
548 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500549 DebugDestroyMessageCallback =
550 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
551 vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback"));
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500552 DebugDestroyMessageCallback(instance, instance->message);
553 }
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800554 for (auto it = instance->active_layers.begin();
555 it != instance->active_layers.end(); ++it) {
Michael Lentine1d1e65f2015-11-19 14:23:06 -0800556 DeactivateLayer(instance, it);
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800557 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700558 const VkAllocCallbacks* alloc = instance->alloc;
559 instance->~VkInstance_T();
560 alloc->pfnFree(alloc->pUserData, instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700561}
562
563VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info,
564 VkInstance* instance_ptr) {
565 Instance* instance = *instance_ptr;
566 VkResult result;
567
568 result =
569 g_hwdevice->CreateInstance(create_info, &instance->drv.vtbl.instance);
570 if (result != VK_SUCCESS) {
571 DestroyInstanceBottom(instance);
572 return result;
573 }
574
Michael Lentine03c64b02015-08-26 18:27:26 -0500575 if (!LoadInstanceVtbl(
576 instance->drv.vtbl.instance, instance->drv.vtbl.instance,
577 g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700578 DestroyInstanceBottom(instance);
579 return VK_ERROR_INITIALIZATION_FAILED;
580 }
581
582 // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be
583 // queryable from the Instance, and that the resulting function work for any
584 // VkDevice created from the instance.
585 instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
586 g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance,
587 "vkGetDeviceProcAddr"));
588 if (!instance->drv.GetDeviceProcAddr) {
589 ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr");
590 DestroyInstanceBottom(instance);
591 return VK_ERROR_INITIALIZATION_FAILED;
592 }
593
594 hwvulkan_dispatch_t* dispatch =
595 reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance);
596 if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
597 // Skip setting dispatch->vtbl on the driver instance handle, since we
598 // never intentionally call through it; we go through Instance::drv.vtbl
599 // instead.
600 } else {
601 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
602 dispatch->magic);
603 DestroyInstanceBottom(instance);
604 return VK_ERROR_INITIALIZATION_FAILED;
605 }
606
607 uint32_t num_physical_devices = 0;
608 result = instance->drv.vtbl.EnumeratePhysicalDevices(
609 instance->drv.vtbl.instance, &num_physical_devices, nullptr);
610 if (result != VK_SUCCESS) {
611 DestroyInstanceBottom(instance);
612 return VK_ERROR_INITIALIZATION_FAILED;
613 }
614 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
615 result = instance->drv.vtbl.EnumeratePhysicalDevices(
616 instance->drv.vtbl.instance, &num_physical_devices,
617 instance->physical_devices);
618 if (result != VK_SUCCESS) {
619 DestroyInstanceBottom(instance);
620 return VK_ERROR_INITIALIZATION_FAILED;
621 }
622 for (uint32_t i = 0; i < num_physical_devices; i++) {
623 dispatch = reinterpret_cast<hwvulkan_dispatch_t*>(
624 instance->physical_devices[i]);
625 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
626 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
627 dispatch->magic);
628 DestroyInstanceBottom(instance);
629 return VK_ERROR_INITIALIZATION_FAILED;
630 }
631 dispatch->vtbl = instance->vtbl;
632 }
633 instance->drv.num_physical_devices = num_physical_devices;
634
635 instance->num_physical_devices = instance->drv.num_physical_devices;
636 return VK_SUCCESS;
637}
638
639VkResult EnumeratePhysicalDevicesBottom(VkInstance instance,
640 uint32_t* pdev_count,
641 VkPhysicalDevice* pdevs) {
642 uint32_t count = instance->num_physical_devices;
643 if (pdevs) {
644 count = std::min(count, *pdev_count);
645 std::copy(instance->physical_devices,
646 instance->physical_devices + count, pdevs);
647 }
648 *pdev_count = count;
649 return VK_SUCCESS;
650}
651
652VkResult GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev,
653 VkPhysicalDeviceFeatures* features) {
654 return GetVtbl(pdev)
655 ->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features);
656}
657
658VkResult GetPhysicalDeviceFormatPropertiesBottom(
659 VkPhysicalDevice pdev,
660 VkFormat format,
661 VkFormatProperties* properties) {
662 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties(
663 pdev, format, properties);
664}
665
666VkResult GetPhysicalDeviceImageFormatPropertiesBottom(
667 VkPhysicalDevice pdev,
668 VkFormat format,
669 VkImageType type,
670 VkImageTiling tiling,
671 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700672 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700673 VkImageFormatProperties* properties) {
674 return GetVtbl(pdev)
675 ->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties(
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700676 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700677}
678
679VkResult GetPhysicalDevicePropertiesBottom(
680 VkPhysicalDevice pdev,
681 VkPhysicalDeviceProperties* properties) {
682 return GetVtbl(pdev)
683 ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties);
684}
685
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700686VkResult GetPhysicalDeviceQueueFamilyPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700687 VkPhysicalDevice pdev,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700688 uint32_t* pCount,
689 VkQueueFamilyProperties* properties) {
690 return GetVtbl(pdev)
691 ->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties(
692 pdev, pCount, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700693}
694
695VkResult GetPhysicalDeviceMemoryPropertiesBottom(
696 VkPhysicalDevice pdev,
697 VkPhysicalDeviceMemoryProperties* properties) {
698 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties(
699 pdev, properties);
700}
701
702VkResult CreateDeviceBottom(VkPhysicalDevice pdev,
703 const VkDeviceCreateInfo* create_info,
704 VkDevice* out_device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500705 Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700706 VkResult result;
707
708 void* mem = instance.alloc->pfnAlloc(instance.alloc->pUserData,
709 sizeof(Device), alignof(Device),
710 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
711 if (!mem)
712 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500713 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700714
Jesse Hall9a16f972015-10-28 15:59:53 -0700715 result = ActivateAllLayers(create_info, &instance, device);
716 if (result != VK_SUCCESS) {
717 DestroyDevice(device);
718 return result;
719 }
720
Jesse Hall04f4f472015-08-16 19:51:04 -0700721 VkDevice drv_device;
722 result = instance.drv.vtbl.CreateDevice(pdev, create_info, &drv_device);
723 if (result != VK_SUCCESS) {
724 DestroyDevice(device);
725 return result;
726 }
727
Jesse Hall04f4f472015-08-16 19:51:04 -0700728 hwvulkan_dispatch_t* dispatch =
729 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
730 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
731 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500732 PFN_vkDestroyDevice destroy_device =
733 reinterpret_cast<PFN_vkDestroyDevice>(
734 instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice"));
735 destroy_device(drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700736 DestroyDevice(device);
737 return VK_ERROR_INITIALIZATION_FAILED;
738 }
739 dispatch->vtbl = &device->vtbl_storage;
740
Michael Lentine03c64b02015-08-26 18:27:26 -0500741 void* base_object = static_cast<void*>(drv_device);
742 void* next_object = base_object;
743 VkLayerLinkedListElem* next_element;
744 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr;
745 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500746 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500747 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
748
749 for (size_t i = elem_list.size(); i > 0; i--) {
750 size_t idx = i - 1;
751 next_element = &elem_list[idx];
752 next_element->get_proc_addr =
753 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
754 next_element->base_object = base_object;
755 next_element->next_element = next_object;
756 next_object = static_cast<void*>(next_element);
757
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500758 auto& name = device->active_layers[idx]->first;
759 auto& handle = device->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500760 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500761 dlsym(handle, (name + "GetDeviceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500762 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500763 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500764 dlsym(handle, "vkGetDeviceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700765 if (!next_get_proc_addr) {
766 ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500767 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700768 next_object = next_element->next_element;
769 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
770 next_element->get_proc_addr);
771 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500772 }
773 }
774
775 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
776 static_cast<VkDevice>(next_object), next_get_proc_addr,
777 device->vtbl_storage)) {
778 DestroyDevice(device);
779 return VK_ERROR_INITIALIZATION_FAILED;
780 }
781
782 PFN_vkCreateDevice layer_createDevice =
783 reinterpret_cast<PFN_vkCreateDevice>(
784 device->vtbl_storage.GetDeviceProcAddr(drv_device,
785 "vkCreateDevice"));
786 layer_createDevice(pdev, create_info, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700787
Michael Lentine88594d72015-11-12 12:49:45 -0800788 // TODO(mlentine) : This is needed to use WSI layer validation. Remove this
789 // when new version of layer initialization exits.
790 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
791 static_cast<VkDevice>(next_object), next_get_proc_addr,
792 device->vtbl_storage)) {
793 DestroyDevice(device);
794 return VK_ERROR_INITIALIZATION_FAILED;
795 }
796
Jesse Hall04f4f472015-08-16 19:51:04 -0700797 *out_device = drv_device;
798 return VK_SUCCESS;
799}
800
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700801VkResult EnumerateDeviceExtensionPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700802 VkPhysicalDevice pdev,
803 const char* layer_name,
804 uint32_t* properties_count,
805 VkExtensionProperties* properties) {
806 // TODO: what are we supposed to do with layer_name here?
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700807 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties(
808 pdev, layer_name, properties_count, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700809}
810
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700811VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev,
812 uint32_t* properties_count,
813 VkLayerProperties* properties) {
814 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700815 pdev, properties_count, properties);
816}
817
818VkResult GetPhysicalDeviceSparseImageFormatPropertiesBottom(
819 VkPhysicalDevice pdev,
820 VkFormat format,
821 VkImageType type,
822 uint32_t samples,
823 VkImageUsageFlags usage,
824 VkImageTiling tiling,
825 uint32_t* properties_count,
826 VkSparseImageFormatProperties* properties) {
827 return GetVtbl(pdev)
828 ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties(
829 pdev, format, type, samples, usage, tiling, properties_count,
830 properties);
831}
832
833PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*);
834
835const InstanceVtbl kBottomInstanceFunctions = {
836 // clang-format off
837 .instance = nullptr,
838 .CreateInstance = CreateInstanceBottom,
839 .DestroyInstance = DestroyInstanceBottom,
840 .GetInstanceProcAddr = GetInstanceProcAddrBottom,
841 .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom,
842 .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom,
843 .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom,
844 .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700845 .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700846 .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700847 .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom,
848 .CreateDevice = CreateDeviceBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700849 .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom,
850 .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700851 .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700852 .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR,
Jesse Hall04f4f472015-08-16 19:51:04 -0700853 // clang-format on
854};
855
856PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500857 // TODO: Possibly move this into the instance table
858 // TODO: Possibly register the callbacks in the loader
859 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
860 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
861 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
862 }
863 if (strcmp(name, "vkCreateInstance") == 0) {
864 return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
865 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700866 return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
867}
868
869} // namespace
870
871// -----------------------------------------------------------------------------
872// Global functions. These are called directly from the loader entry points,
873// without going through a dispatch table.
874
875namespace vulkan {
876
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700877VkResult EnumerateInstanceExtensionProperties(
878 const char* /*layer_name*/,
879 uint32_t* count,
880 VkExtensionProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700881 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700882 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700883
884 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700885 ALOGW("vkEnumerateInstanceExtensionProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700886
887 *count = 0;
888 return VK_SUCCESS;
889}
890
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700891VkResult EnumerateInstanceLayerProperties(uint32_t* count,
892 VkLayerProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700893 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700894 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700895
896 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700897 ALOGW("vkEnumerateInstanceLayerProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700898
899 *count = 0;
900 return VK_SUCCESS;
901}
902
903VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
904 VkInstance* out_instance) {
905 VkResult result;
906
907 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700908 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700909
910 VkInstanceCreateInfo local_create_info = *create_info;
911 if (!local_create_info.pAllocCb)
912 local_create_info.pAllocCb = &kDefaultAllocCallbacks;
913 create_info = &local_create_info;
914
915 void* instance_mem = create_info->pAllocCb->pfnAlloc(
916 create_info->pAllocCb->pUserData, sizeof(Instance), alignof(Instance),
917 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
918 if (!instance_mem)
919 return VK_ERROR_OUT_OF_HOST_MEMORY;
920 Instance* instance = new (instance_mem) Instance(create_info->pAllocCb);
921
922 instance->vtbl_storage = kBottomInstanceFunctions;
923 instance->vtbl_storage.instance = instance;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500924 instance->message = VK_NULL_HANDLE;
Jesse Hall04f4f472015-08-16 19:51:04 -0700925
Michael Lentine03c64b02015-08-26 18:27:26 -0500926 // Scan layers
Michael Lentine03c64b02015-08-26 18:27:26 -0500927 CallbackAllocator<char> string_allocator(instance->alloc);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500928
Michael Lentine03c64b02015-08-26 18:27:26 -0500929 String dir_name("/data/local/tmp/vulkan/", string_allocator);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500930 FindLayersInDirectory(*instance, dir_name);
Michael Lentine1c69b9e2015-09-14 13:26:59 -0500931 const std::string& path = LoaderData::GetInstance().layer_path;
932 dir_name.assign(path.c_str(), path.size());
933 dir_name.append("/");
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500934 FindLayersInDirectory(*instance, dir_name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700935
Jesse Hall9a16f972015-10-28 15:59:53 -0700936 result = ActivateAllLayers(create_info, instance, instance);
937 if (result != VK_SUCCESS) {
938 DestroyInstanceBottom(instance);
939 return result;
940 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500941
942 void* base_object = static_cast<void*>(instance);
943 void* next_object = base_object;
944 VkLayerLinkedListElem* next_element;
945 PFN_vkGetInstanceProcAddr next_get_proc_addr =
946 kBottomInstanceFunctions.GetInstanceProcAddr;
947 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700948 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500949 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
950
951 for (size_t i = elem_list.size(); i > 0; i--) {
952 size_t idx = i - 1;
953 next_element = &elem_list[idx];
954 next_element->get_proc_addr =
955 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
956 next_element->base_object = base_object;
957 next_element->next_element = next_object;
958 next_object = static_cast<void*>(next_element);
959
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500960 auto& name = instance->active_layers[idx]->first;
961 auto& handle = instance->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500962 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500963 dlsym(handle, (name + "GetInstanceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500964 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500965 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500966 dlsym(handle, "vkGetInstanceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700967 if (!next_get_proc_addr) {
968 ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500969 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700970 next_object = next_element->next_element;
971 next_get_proc_addr =
972 reinterpret_cast<PFN_vkGetInstanceProcAddr>(
973 next_element->get_proc_addr);
974 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500975 }
976 }
977
978 if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object),
979 static_cast<VkInstance>(next_object),
980 next_get_proc_addr, instance->vtbl_storage)) {
981 DestroyInstanceBottom(instance);
982 return VK_ERROR_INITIALIZATION_FAILED;
983 }
984
Michael Lentine950bb4f2015-09-14 13:26:30 -0500985 // Force enable callback extension if required
986 bool enable_callback =
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500987 property_get_bool("debug.vulkan.enable_callback", false);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500988 bool enable_logging = enable_callback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500989 const char* extension_name = "DEBUG_REPORT";
Michael Lentine950bb4f2015-09-14 13:26:30 -0500990 if (enable_callback) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500991 enable_callback = AddExtensionToCreateInfo(
992 local_create_info, extension_name, instance->alloc);
Michael Lentine950bb4f2015-09-14 13:26:30 -0500993 }
994
Jesse Hall04f4f472015-08-16 19:51:04 -0700995 *out_instance = instance;
Michael Lentine03c64b02015-08-26 18:27:26 -0500996 result = instance->vtbl_storage.CreateInstance(create_info, out_instance);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500997 if (enable_callback)
998 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -0700999 if (result <= 0) {
1000 // For every layer, including the loader top and bottom layers:
1001 // - If a call to the next CreateInstance fails, the layer must clean
1002 // up anything it has successfully done so far, and propagate the
1003 // error upwards.
1004 // - If a layer successfully calls the next layer's CreateInstance, and
1005 // afterwards must fail for some reason, it must call the next layer's
1006 // DestroyInstance before returning.
1007 // - The layer must not call the next layer's DestroyInstance if that
1008 // layer's CreateInstance wasn't called, or returned failure.
1009
1010 // On failure, CreateInstanceBottom frees the instance struct, so it's
1011 // already gone at this point. Nothing to do.
1012 }
1013
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001014 if (enable_logging) {
1015 PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001016 DebugCreateMessageCallback =
1017 reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
1018 vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback"));
1019 DebugCreateMessageCallback(
1020 instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1021 LogDebugMessageCallback, NULL, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001022 }
1023
Jesse Hall04f4f472015-08-16 19:51:04 -07001024 return result;
1025}
1026
1027PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
1028 if (!instance)
1029 return GetGlobalInstanceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001030 // TODO: Possibly move this into the instance table
1031 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
1032 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
1033 if (!instance->vtbl)
1034 return NULL;
1035 PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr;
1036 return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name));
1037 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001038 // For special-case functions we always return the loader entry
1039 if (strcmp(name, "vkGetInstanceProcAddr") == 0 ||
1040 strcmp(name, "vkGetDeviceProcAddr") == 0) {
1041 return GetGlobalInstanceProcAddr(name);
1042 }
1043 return GetSpecificInstanceProcAddr(instance->vtbl, name);
1044}
1045
1046PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
1047 if (!device)
1048 return GetGlobalDeviceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001049 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
1050 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
1051 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001052 if (strcmp(name, "vkGetDeviceQueue") == 0) {
1053 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
1054 }
1055 if (strcmp(name, "vkCreateCommandBuffer") == 0) {
1056 return reinterpret_cast<PFN_vkVoidFunction>(CreateCommandBuffer);
1057 }
1058 if (strcmp(name, "vkDestroyDevice") == 0) {
1059 return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
1060 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001061 return GetSpecificDeviceProcAddr(GetVtbl(device), name);
1062}
1063
1064VkResult GetDeviceQueue(VkDevice drv_device,
1065 uint32_t family,
1066 uint32_t index,
1067 VkQueue* out_queue) {
1068 VkResult result;
1069 VkQueue queue;
1070 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1071 result = vtbl->GetDeviceQueue(drv_device, family, index, &queue);
1072 if (result != VK_SUCCESS)
1073 return result;
1074 hwvulkan_dispatch_t* dispatch =
1075 reinterpret_cast<hwvulkan_dispatch_t*>(queue);
Jesse Hall74f28dd2015-10-29 18:49:53 -07001076 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != vtbl) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001077 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic);
1078 return VK_ERROR_INITIALIZATION_FAILED;
1079 }
1080 dispatch->vtbl = vtbl;
1081 *out_queue = queue;
1082 return VK_SUCCESS;
1083}
1084
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001085VkResult CreateCommandBuffer(VkDevice drv_device,
1086 const VkCmdBufferCreateInfo* create_info,
1087 VkCmdBuffer* out_cmdbuf) {
1088 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1089 VkCmdBuffer cmdbuf;
1090 VkResult result =
1091 vtbl->CreateCommandBuffer(drv_device, create_info, &cmdbuf);
1092 if (result != VK_SUCCESS)
1093 return result;
1094 hwvulkan_dispatch_t* dispatch =
1095 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuf);
1096 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
1097 ALOGE("invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR,
1098 dispatch->magic);
1099 return VK_ERROR_INITIALIZATION_FAILED;
1100 }
1101 dispatch->vtbl = vtbl;
1102 *out_cmdbuf = cmdbuf;
1103 return VK_SUCCESS;
1104}
1105
Jesse Hall04f4f472015-08-16 19:51:04 -07001106VkResult DestroyDevice(VkDevice drv_device) {
1107 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1108 Device* device = static_cast<Device*>(vtbl->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001109 for (auto it = device->active_layers.begin();
1110 it != device->active_layers.end(); ++it) {
Michael Lentine1d1e65f2015-11-19 14:23:06 -08001111 DeactivateLayer(device->instance, it);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001112 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001113 vtbl->DestroyDevice(drv_device);
1114 DestroyDevice(device);
1115 return VK_SUCCESS;
1116}
1117
Jesse Halld7b994a2015-09-07 14:17:37 -07001118void* AllocDeviceMem(VkDevice device,
1119 size_t size,
1120 size_t align,
1121 VkSystemAllocType type) {
1122 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001123 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001124 return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, type);
1125}
1126
1127void FreeDeviceMem(VkDevice device, void* ptr) {
1128 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001129 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001130 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1131}
1132
1133const DeviceVtbl& GetDriverVtbl(VkDevice device) {
1134 // TODO(jessehall): This actually returns the API-level vtbl for the
1135 // device, not the driver entry points. Given the current use -- getting
1136 // the driver's private swapchain-related functions -- that works, but is
1137 // misleading and likely to cause bugs. Fix as part of separating the
1138 // loader->driver interface from the app->loader interface.
1139 return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage;
1140}
1141
1142const DeviceVtbl& GetDriverVtbl(VkQueue queue) {
1143 // TODO(jessehall): This actually returns the API-level vtbl for the
1144 // device, not the driver entry points. Given the current use -- getting
1145 // the driver's private swapchain-related functions -- that works, but is
1146 // misleading and likely to cause bugs. Fix as part of separating the
1147 // loader->driver interface from the app->loader interface.
1148 return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage;
1149}
1150
Jesse Hall04f4f472015-08-16 19:51:04 -07001151} // namespace vulkan