blob: 583514a567a6431f66e2134f2e0c178dc9825597 [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);
323 if (std::find(object->active_layers.begin(), object->active_layers.end(),
324 element) != object->active_layers.end()) {
325 ALOGW("Layer %s already activated; skipping", name.c_str());
326 return;
327 }
328 // If layer is not open, open it
329 LayerData& layer_data = element->second;
330 pthread_mutex_lock(&instance->layer_lock);
331 if (layer_data.ref_count == 0) {
332 SharedLibraryHandle layer_handle =
333 dlopen(layer_data.path.c_str(), RTLD_NOW | RTLD_LOCAL);
334 if (!layer_handle) {
335 pthread_mutex_unlock(&instance->layer_lock);
336 ALOGE("%s failed to load with error %s; Skipping",
337 layer_data.path.c_str(), dlerror());
338 return;
339 }
340 layer_data.handle = layer_handle;
341 }
342 layer_data.ref_count++;
343 pthread_mutex_unlock(&instance->layer_lock);
344 ALOGV("Activating layer %s", name.c_str());
345 object->active_layers.push_back(element);
346}
347
348template <class TObject>
349void DeactivateLayer(TObject* object,
350 Instance* instance,
351 Vector<LayerMapIterator>::iterator& element) {
352 LayerMapIterator& layer_map_data = *element;
353 object->active_layers.erase(element);
354 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);
408 auto element = instance->layers.find(layer_name);
409 if (element != instance->layers.end()) {
410 ActivateLayer(object, instance, layer_name);
411 }
412 start = end + 1;
413 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500414 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
415 InstanceNamesPair instance_names_pair = {.instance = instance,
416 .layer_names = &layer_names};
417 property_list(SetLayerNamesFromProperty,
418 static_cast<void*>(&instance_names_pair));
419 for (auto layer_name_element : layer_names) {
420 ActivateLayer(object, instance, layer_name_element);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500421 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500422 }
423 // Load app layers
424 for (uint32_t i = 0; i < create_info->layerCount; ++i) {
425 String layer_name(create_info->ppEnabledLayerNames[i],
426 string_allocator);
427 auto element = instance->layers.find(layer_name);
428 if (element == instance->layers.end()) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700429 ALOGE("requested %s layer '%s' not present",
430 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ?
431 "instance" : "device",
432 layer_name.c_str());
433 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500434 } else {
435 ActivateLayer(object, instance, layer_name);
436 }
437 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700438 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500439}
440
441template <class TCreateInfo>
442bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
443 const char* extension_name,
444 const VkAllocCallbacks* alloc) {
445 for (uint32_t i = 0; i < local_create_info.extensionCount; ++i) {
446 if (!strcmp(extension_name,
447 local_create_info.ppEnabledExtensionNames[i])) {
448 return false;
449 }
450 }
451 uint32_t extension_count = local_create_info.extensionCount;
452 local_create_info.extensionCount++;
453 void* mem = alloc->pfnAlloc(
454 alloc->pUserData, local_create_info.extensionCount * sizeof(char*),
455 alignof(char*), VK_SYSTEM_ALLOC_TYPE_INTERNAL);
456 if (mem) {
457 const char** enabled_extensions = static_cast<const char**>(mem);
458 for (uint32_t i = 0; i < extension_count; ++i) {
459 enabled_extensions[i] =
460 local_create_info.ppEnabledExtensionNames[i];
461 }
462 enabled_extensions[extension_count] = extension_name;
463 local_create_info.ppEnabledExtensionNames = enabled_extensions;
464 } else {
465 ALOGW("%s extension cannot be enabled: memory allocation failed",
466 extension_name);
467 local_create_info.extensionCount--;
468 return false;
469 }
470 return true;
471}
472
473template <class T>
474void FreeAllocatedCreateInfo(T& local_create_info,
475 const VkAllocCallbacks* alloc) {
476 alloc->pfnFree(
477 alloc->pUserData,
478 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
479}
480
Michael Lentineeb970862015-10-15 12:42:22 -0500481VkBool32 LogDebugMessageCallback(VkFlags message_flags,
482 VkDbgObjectType /*obj_type*/,
483 uint64_t /*src_object*/,
484 size_t /*location*/,
485 int32_t message_code,
486 const char* layer_prefix,
487 const char* message,
488 void* /*user_data*/) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500489 if (message_flags & VK_DBG_REPORT_ERROR_BIT) {
490 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
491 } else if (message_flags & VK_DBG_REPORT_WARN_BIT) {
492 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
493 }
Michael Lentineeb970862015-10-15 12:42:22 -0500494 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500495}
496
497VkResult CreateDeviceNoop(VkPhysicalDevice,
498 const VkDeviceCreateInfo*,
499 VkDevice*) {
500 return VK_SUCCESS;
501}
502
503PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) {
504 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
505 return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr);
506 }
507 if (strcmp(name, "vkCreateDevice") == 0) {
508 return reinterpret_cast<PFN_vkVoidFunction>(CreateDeviceNoop);
509 }
510 if (!device)
511 return GetGlobalDeviceProcAddr(name);
512 Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500513 return loader_device->instance->drv.GetDeviceProcAddr(device, name);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500514}
515
Jesse Hall04f4f472015-08-16 19:51:04 -0700516// -----------------------------------------------------------------------------
517// "Bottom" functions. These are called at the end of the instance dispatch
518// chain.
519
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700520void DestroyInstanceBottom(VkInstance instance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700521 // These checks allow us to call DestroyInstanceBottom from any error path
522 // in CreateInstanceBottom, before the driver instance is fully initialized.
523 if (instance->drv.vtbl.instance != VK_NULL_HANDLE &&
524 instance->drv.vtbl.DestroyInstance) {
525 instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance);
526 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500527 for (auto it = instance->active_layers.begin();
528 it != instance->active_layers.end(); ++it) {
529 DeactivateLayer(instance, instance, it);
Michael Lentine03c64b02015-08-26 18:27:26 -0500530 }
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500531 if (instance->message) {
532 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500533 DebugDestroyMessageCallback =
534 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
535 vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback"));
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500536 DebugDestroyMessageCallback(instance, instance->message);
537 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700538 const VkAllocCallbacks* alloc = instance->alloc;
539 instance->~VkInstance_T();
540 alloc->pfnFree(alloc->pUserData, instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700541}
542
543VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info,
544 VkInstance* instance_ptr) {
545 Instance* instance = *instance_ptr;
546 VkResult result;
547
548 result =
549 g_hwdevice->CreateInstance(create_info, &instance->drv.vtbl.instance);
550 if (result != VK_SUCCESS) {
551 DestroyInstanceBottom(instance);
552 return result;
553 }
554
Michael Lentine03c64b02015-08-26 18:27:26 -0500555 if (!LoadInstanceVtbl(
556 instance->drv.vtbl.instance, instance->drv.vtbl.instance,
557 g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700558 DestroyInstanceBottom(instance);
559 return VK_ERROR_INITIALIZATION_FAILED;
560 }
561
562 // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be
563 // queryable from the Instance, and that the resulting function work for any
564 // VkDevice created from the instance.
565 instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
566 g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance,
567 "vkGetDeviceProcAddr"));
568 if (!instance->drv.GetDeviceProcAddr) {
569 ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr");
570 DestroyInstanceBottom(instance);
571 return VK_ERROR_INITIALIZATION_FAILED;
572 }
573
574 hwvulkan_dispatch_t* dispatch =
575 reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance);
576 if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
577 // Skip setting dispatch->vtbl on the driver instance handle, since we
578 // never intentionally call through it; we go through Instance::drv.vtbl
579 // instead.
580 } else {
581 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
582 dispatch->magic);
583 DestroyInstanceBottom(instance);
584 return VK_ERROR_INITIALIZATION_FAILED;
585 }
586
587 uint32_t num_physical_devices = 0;
588 result = instance->drv.vtbl.EnumeratePhysicalDevices(
589 instance->drv.vtbl.instance, &num_physical_devices, nullptr);
590 if (result != VK_SUCCESS) {
591 DestroyInstanceBottom(instance);
592 return VK_ERROR_INITIALIZATION_FAILED;
593 }
594 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
595 result = instance->drv.vtbl.EnumeratePhysicalDevices(
596 instance->drv.vtbl.instance, &num_physical_devices,
597 instance->physical_devices);
598 if (result != VK_SUCCESS) {
599 DestroyInstanceBottom(instance);
600 return VK_ERROR_INITIALIZATION_FAILED;
601 }
602 for (uint32_t i = 0; i < num_physical_devices; i++) {
603 dispatch = reinterpret_cast<hwvulkan_dispatch_t*>(
604 instance->physical_devices[i]);
605 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
606 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
607 dispatch->magic);
608 DestroyInstanceBottom(instance);
609 return VK_ERROR_INITIALIZATION_FAILED;
610 }
611 dispatch->vtbl = instance->vtbl;
612 }
613 instance->drv.num_physical_devices = num_physical_devices;
614
615 instance->num_physical_devices = instance->drv.num_physical_devices;
616 return VK_SUCCESS;
617}
618
619VkResult EnumeratePhysicalDevicesBottom(VkInstance instance,
620 uint32_t* pdev_count,
621 VkPhysicalDevice* pdevs) {
622 uint32_t count = instance->num_physical_devices;
623 if (pdevs) {
624 count = std::min(count, *pdev_count);
625 std::copy(instance->physical_devices,
626 instance->physical_devices + count, pdevs);
627 }
628 *pdev_count = count;
629 return VK_SUCCESS;
630}
631
632VkResult GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev,
633 VkPhysicalDeviceFeatures* features) {
634 return GetVtbl(pdev)
635 ->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features);
636}
637
638VkResult GetPhysicalDeviceFormatPropertiesBottom(
639 VkPhysicalDevice pdev,
640 VkFormat format,
641 VkFormatProperties* properties) {
642 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties(
643 pdev, format, properties);
644}
645
646VkResult GetPhysicalDeviceImageFormatPropertiesBottom(
647 VkPhysicalDevice pdev,
648 VkFormat format,
649 VkImageType type,
650 VkImageTiling tiling,
651 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700652 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700653 VkImageFormatProperties* properties) {
654 return GetVtbl(pdev)
655 ->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties(
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700656 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700657}
658
659VkResult GetPhysicalDevicePropertiesBottom(
660 VkPhysicalDevice pdev,
661 VkPhysicalDeviceProperties* properties) {
662 return GetVtbl(pdev)
663 ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties);
664}
665
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700666VkResult GetPhysicalDeviceQueueFamilyPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700667 VkPhysicalDevice pdev,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700668 uint32_t* pCount,
669 VkQueueFamilyProperties* properties) {
670 return GetVtbl(pdev)
671 ->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties(
672 pdev, pCount, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700673}
674
675VkResult GetPhysicalDeviceMemoryPropertiesBottom(
676 VkPhysicalDevice pdev,
677 VkPhysicalDeviceMemoryProperties* properties) {
678 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties(
679 pdev, properties);
680}
681
682VkResult CreateDeviceBottom(VkPhysicalDevice pdev,
683 const VkDeviceCreateInfo* create_info,
684 VkDevice* out_device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500685 Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700686 VkResult result;
687
688 void* mem = instance.alloc->pfnAlloc(instance.alloc->pUserData,
689 sizeof(Device), alignof(Device),
690 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
691 if (!mem)
692 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500693 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700694
Jesse Hall9a16f972015-10-28 15:59:53 -0700695 result = ActivateAllLayers(create_info, &instance, device);
696 if (result != VK_SUCCESS) {
697 DestroyDevice(device);
698 return result;
699 }
700
Jesse Hall04f4f472015-08-16 19:51:04 -0700701 VkDevice drv_device;
702 result = instance.drv.vtbl.CreateDevice(pdev, create_info, &drv_device);
703 if (result != VK_SUCCESS) {
704 DestroyDevice(device);
705 return result;
706 }
707
Jesse Hall04f4f472015-08-16 19:51:04 -0700708 hwvulkan_dispatch_t* dispatch =
709 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
710 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
711 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500712 PFN_vkDestroyDevice destroy_device =
713 reinterpret_cast<PFN_vkDestroyDevice>(
714 instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice"));
715 destroy_device(drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700716 DestroyDevice(device);
717 return VK_ERROR_INITIALIZATION_FAILED;
718 }
719 dispatch->vtbl = &device->vtbl_storage;
720
Jesse Hallb1352bc2015-09-04 16:12:33 -0700721 device->vtbl_storage.GetSurfacePropertiesKHR = GetSurfacePropertiesKHR;
722 device->vtbl_storage.GetSurfaceFormatsKHR = GetSurfaceFormatsKHR;
723 device->vtbl_storage.GetSurfacePresentModesKHR = GetSurfacePresentModesKHR;
724 device->vtbl_storage.CreateSwapchainKHR = CreateSwapchainKHR;
725 device->vtbl_storage.DestroySwapchainKHR = DestroySwapchainKHR;
726 device->vtbl_storage.GetSwapchainImagesKHR = GetSwapchainImagesKHR;
727 device->vtbl_storage.AcquireNextImageKHR = AcquireNextImageKHR;
728 device->vtbl_storage.QueuePresentKHR = QueuePresentKHR;
729
Michael Lentine03c64b02015-08-26 18:27:26 -0500730 void* base_object = static_cast<void*>(drv_device);
731 void* next_object = base_object;
732 VkLayerLinkedListElem* next_element;
733 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr;
734 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500735 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500736 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
737
738 for (size_t i = elem_list.size(); i > 0; i--) {
739 size_t idx = i - 1;
740 next_element = &elem_list[idx];
741 next_element->get_proc_addr =
742 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
743 next_element->base_object = base_object;
744 next_element->next_element = next_object;
745 next_object = static_cast<void*>(next_element);
746
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500747 auto& name = device->active_layers[idx]->first;
748 auto& handle = device->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500749 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500750 dlsym(handle, (name + "GetDeviceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500751 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500752 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500753 dlsym(handle, "vkGetDeviceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700754 if (!next_get_proc_addr) {
755 ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500756 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700757 next_object = next_element->next_element;
758 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
759 next_element->get_proc_addr);
760 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500761 }
762 }
763
764 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
765 static_cast<VkDevice>(next_object), next_get_proc_addr,
766 device->vtbl_storage)) {
767 DestroyDevice(device);
768 return VK_ERROR_INITIALIZATION_FAILED;
769 }
770
771 PFN_vkCreateDevice layer_createDevice =
772 reinterpret_cast<PFN_vkCreateDevice>(
773 device->vtbl_storage.GetDeviceProcAddr(drv_device,
774 "vkCreateDevice"));
775 layer_createDevice(pdev, create_info, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700776
777 *out_device = drv_device;
778 return VK_SUCCESS;
779}
780
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700781VkResult EnumerateDeviceExtensionPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700782 VkPhysicalDevice pdev,
783 const char* layer_name,
784 uint32_t* properties_count,
785 VkExtensionProperties* properties) {
786 // TODO: what are we supposed to do with layer_name here?
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700787 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties(
788 pdev, layer_name, properties_count, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700789}
790
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700791VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev,
792 uint32_t* properties_count,
793 VkLayerProperties* properties) {
794 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700795 pdev, properties_count, properties);
796}
797
798VkResult GetPhysicalDeviceSparseImageFormatPropertiesBottom(
799 VkPhysicalDevice pdev,
800 VkFormat format,
801 VkImageType type,
802 uint32_t samples,
803 VkImageUsageFlags usage,
804 VkImageTiling tiling,
805 uint32_t* properties_count,
806 VkSparseImageFormatProperties* properties) {
807 return GetVtbl(pdev)
808 ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties(
809 pdev, format, type, samples, usage, tiling, properties_count,
810 properties);
811}
812
813PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*);
814
815const InstanceVtbl kBottomInstanceFunctions = {
816 // clang-format off
817 .instance = nullptr,
818 .CreateInstance = CreateInstanceBottom,
819 .DestroyInstance = DestroyInstanceBottom,
820 .GetInstanceProcAddr = GetInstanceProcAddrBottom,
821 .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom,
822 .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom,
823 .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom,
824 .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700825 .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700826 .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700827 .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom,
828 .CreateDevice = CreateDeviceBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700829 .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom,
830 .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700831 .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700832 .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR,
Jesse Hall04f4f472015-08-16 19:51:04 -0700833 // clang-format on
834};
835
Michael Lentine03c64b02015-08-26 18:27:26 -0500836VkResult Noop(...) {
837 return VK_SUCCESS;
838}
839
Jesse Hall04f4f472015-08-16 19:51:04 -0700840PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500841 // TODO: Possibly move this into the instance table
842 // TODO: Possibly register the callbacks in the loader
843 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
844 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
845 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
846 }
847 if (strcmp(name, "vkCreateInstance") == 0) {
848 return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
849 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700850 return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
851}
852
853} // namespace
854
855// -----------------------------------------------------------------------------
856// Global functions. These are called directly from the loader entry points,
857// without going through a dispatch table.
858
859namespace vulkan {
860
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700861VkResult EnumerateInstanceExtensionProperties(
862 const char* /*layer_name*/,
863 uint32_t* count,
864 VkExtensionProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700865 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700866 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700867
868 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700869 ALOGW("vkEnumerateInstanceExtensionProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700870
871 *count = 0;
872 return VK_SUCCESS;
873}
874
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700875VkResult EnumerateInstanceLayerProperties(uint32_t* count,
876 VkLayerProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700877 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700878 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700879
880 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700881 ALOGW("vkEnumerateInstanceLayerProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700882
883 *count = 0;
884 return VK_SUCCESS;
885}
886
887VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
888 VkInstance* out_instance) {
889 VkResult result;
890
891 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700892 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700893
894 VkInstanceCreateInfo local_create_info = *create_info;
895 if (!local_create_info.pAllocCb)
896 local_create_info.pAllocCb = &kDefaultAllocCallbacks;
897 create_info = &local_create_info;
898
899 void* instance_mem = create_info->pAllocCb->pfnAlloc(
900 create_info->pAllocCb->pUserData, sizeof(Instance), alignof(Instance),
901 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
902 if (!instance_mem)
903 return VK_ERROR_OUT_OF_HOST_MEMORY;
904 Instance* instance = new (instance_mem) Instance(create_info->pAllocCb);
905
906 instance->vtbl_storage = kBottomInstanceFunctions;
907 instance->vtbl_storage.instance = instance;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500908 instance->message = VK_NULL_HANDLE;
Jesse Hall04f4f472015-08-16 19:51:04 -0700909
Michael Lentine03c64b02015-08-26 18:27:26 -0500910 // Scan layers
Michael Lentine03c64b02015-08-26 18:27:26 -0500911 CallbackAllocator<char> string_allocator(instance->alloc);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500912
Michael Lentine03c64b02015-08-26 18:27:26 -0500913 String dir_name("/data/local/tmp/vulkan/", string_allocator);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500914 FindLayersInDirectory(*instance, dir_name);
Michael Lentine1c69b9e2015-09-14 13:26:59 -0500915 const std::string& path = LoaderData::GetInstance().layer_path;
916 dir_name.assign(path.c_str(), path.size());
917 dir_name.append("/");
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500918 FindLayersInDirectory(*instance, dir_name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700919
Jesse Hall9a16f972015-10-28 15:59:53 -0700920 result = ActivateAllLayers(create_info, instance, instance);
921 if (result != VK_SUCCESS) {
922 DestroyInstanceBottom(instance);
923 return result;
924 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500925
926 void* base_object = static_cast<void*>(instance);
927 void* next_object = base_object;
928 VkLayerLinkedListElem* next_element;
929 PFN_vkGetInstanceProcAddr next_get_proc_addr =
930 kBottomInstanceFunctions.GetInstanceProcAddr;
931 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700932 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500933 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
934
935 for (size_t i = elem_list.size(); i > 0; i--) {
936 size_t idx = i - 1;
937 next_element = &elem_list[idx];
938 next_element->get_proc_addr =
939 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
940 next_element->base_object = base_object;
941 next_element->next_element = next_object;
942 next_object = static_cast<void*>(next_element);
943
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500944 auto& name = instance->active_layers[idx]->first;
945 auto& handle = instance->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500946 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500947 dlsym(handle, (name + "GetInstanceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500948 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500949 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500950 dlsym(handle, "vkGetInstanceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700951 if (!next_get_proc_addr) {
952 ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500953 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700954 next_object = next_element->next_element;
955 next_get_proc_addr =
956 reinterpret_cast<PFN_vkGetInstanceProcAddr>(
957 next_element->get_proc_addr);
958 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500959 }
960 }
961
962 if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object),
963 static_cast<VkInstance>(next_object),
964 next_get_proc_addr, instance->vtbl_storage)) {
965 DestroyInstanceBottom(instance);
966 return VK_ERROR_INITIALIZATION_FAILED;
967 }
968
Michael Lentine950bb4f2015-09-14 13:26:30 -0500969 // Force enable callback extension if required
970 bool enable_callback =
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500971 property_get_bool("debug.vulkan.enable_callback", false);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500972 bool enable_logging = enable_callback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500973 const char* extension_name = "DEBUG_REPORT";
Michael Lentine950bb4f2015-09-14 13:26:30 -0500974 if (enable_callback) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500975 enable_callback = AddExtensionToCreateInfo(
976 local_create_info, extension_name, instance->alloc);
Michael Lentine950bb4f2015-09-14 13:26:30 -0500977 }
978
Jesse Hall04f4f472015-08-16 19:51:04 -0700979 *out_instance = instance;
Michael Lentine03c64b02015-08-26 18:27:26 -0500980 result = instance->vtbl_storage.CreateInstance(create_info, out_instance);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500981 if (enable_callback)
982 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -0700983 if (result <= 0) {
984 // For every layer, including the loader top and bottom layers:
985 // - If a call to the next CreateInstance fails, the layer must clean
986 // up anything it has successfully done so far, and propagate the
987 // error upwards.
988 // - If a layer successfully calls the next layer's CreateInstance, and
989 // afterwards must fail for some reason, it must call the next layer's
990 // DestroyInstance before returning.
991 // - The layer must not call the next layer's DestroyInstance if that
992 // layer's CreateInstance wasn't called, or returned failure.
993
994 // On failure, CreateInstanceBottom frees the instance struct, so it's
995 // already gone at this point. Nothing to do.
996 }
997
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500998 if (enable_logging) {
999 PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001000 DebugCreateMessageCallback =
1001 reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
1002 vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback"));
1003 DebugCreateMessageCallback(
1004 instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1005 LogDebugMessageCallback, NULL, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001006 }
1007
Jesse Hall04f4f472015-08-16 19:51:04 -07001008 return result;
1009}
1010
1011PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
1012 if (!instance)
1013 return GetGlobalInstanceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001014 // TODO: Possibly move this into the instance table
1015 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
1016 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
1017 if (!instance->vtbl)
1018 return NULL;
1019 PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr;
1020 return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name));
1021 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001022 // For special-case functions we always return the loader entry
1023 if (strcmp(name, "vkGetInstanceProcAddr") == 0 ||
1024 strcmp(name, "vkGetDeviceProcAddr") == 0) {
1025 return GetGlobalInstanceProcAddr(name);
1026 }
1027 return GetSpecificInstanceProcAddr(instance->vtbl, name);
1028}
1029
1030PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
1031 if (!device)
1032 return GetGlobalDeviceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001033 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
1034 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
1035 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001036 if (strcmp(name, "vkGetDeviceQueue") == 0) {
1037 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
1038 }
1039 if (strcmp(name, "vkCreateCommandBuffer") == 0) {
1040 return reinterpret_cast<PFN_vkVoidFunction>(CreateCommandBuffer);
1041 }
1042 if (strcmp(name, "vkDestroyDevice") == 0) {
1043 return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
1044 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001045 // For special-case functions we always return the loader entry
1046 if (strcmp(name, "vkGetDeviceQueue") == 0 ||
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001047 strcmp(name, "vkCreateCommandBuffer") == 0 ||
Jesse Hall04f4f472015-08-16 19:51:04 -07001048 strcmp(name, "vkDestroyDevice") == 0) {
1049 return GetGlobalDeviceProcAddr(name);
1050 }
1051 return GetSpecificDeviceProcAddr(GetVtbl(device), name);
1052}
1053
1054VkResult GetDeviceQueue(VkDevice drv_device,
1055 uint32_t family,
1056 uint32_t index,
1057 VkQueue* out_queue) {
1058 VkResult result;
1059 VkQueue queue;
1060 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1061 result = vtbl->GetDeviceQueue(drv_device, family, index, &queue);
1062 if (result != VK_SUCCESS)
1063 return result;
1064 hwvulkan_dispatch_t* dispatch =
1065 reinterpret_cast<hwvulkan_dispatch_t*>(queue);
Jesse Hall74f28dd2015-10-29 18:49:53 -07001066 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != vtbl) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001067 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic);
1068 return VK_ERROR_INITIALIZATION_FAILED;
1069 }
1070 dispatch->vtbl = vtbl;
1071 *out_queue = queue;
1072 return VK_SUCCESS;
1073}
1074
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001075VkResult CreateCommandBuffer(VkDevice drv_device,
1076 const VkCmdBufferCreateInfo* create_info,
1077 VkCmdBuffer* out_cmdbuf) {
1078 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1079 VkCmdBuffer cmdbuf;
1080 VkResult result =
1081 vtbl->CreateCommandBuffer(drv_device, create_info, &cmdbuf);
1082 if (result != VK_SUCCESS)
1083 return result;
1084 hwvulkan_dispatch_t* dispatch =
1085 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuf);
1086 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
1087 ALOGE("invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR,
1088 dispatch->magic);
1089 return VK_ERROR_INITIALIZATION_FAILED;
1090 }
1091 dispatch->vtbl = vtbl;
1092 *out_cmdbuf = cmdbuf;
1093 return VK_SUCCESS;
1094}
1095
Jesse Hall04f4f472015-08-16 19:51:04 -07001096VkResult DestroyDevice(VkDevice drv_device) {
1097 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1098 Device* device = static_cast<Device*>(vtbl->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001099 for (auto it = device->active_layers.begin();
1100 it != device->active_layers.end(); ++it) {
1101 DeactivateLayer(device, device->instance, it);
1102 }
1103 device->active_layers.clear();
Jesse Hall04f4f472015-08-16 19:51:04 -07001104 vtbl->DestroyDevice(drv_device);
1105 DestroyDevice(device);
1106 return VK_SUCCESS;
1107}
1108
Jesse Halld7b994a2015-09-07 14:17:37 -07001109void* AllocDeviceMem(VkDevice device,
1110 size_t size,
1111 size_t align,
1112 VkSystemAllocType type) {
1113 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001114 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001115 return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, type);
1116}
1117
1118void FreeDeviceMem(VkDevice device, void* ptr) {
1119 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001120 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001121 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1122}
1123
1124const DeviceVtbl& GetDriverVtbl(VkDevice device) {
1125 // TODO(jessehall): This actually returns the API-level vtbl for the
1126 // device, not the driver entry points. Given the current use -- getting
1127 // the driver's private swapchain-related functions -- that works, but is
1128 // misleading and likely to cause bugs. Fix as part of separating the
1129 // loader->driver interface from the app->loader interface.
1130 return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage;
1131}
1132
1133const DeviceVtbl& GetDriverVtbl(VkQueue queue) {
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(queue)->device)->vtbl_storage;
1140}
1141
Jesse Hall04f4f472015-08-16 19:51:04 -07001142} // namespace vulkan