blob: fa7559a9177638422eb787897f31162db452cd9b [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) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500260 DIR* directory;
261 struct dirent* entry;
262 if ((directory = opendir(dir_name.c_str()))) {
263 Vector<VkLayerProperties> properties(
264 CallbackAllocator<VkLayerProperties>(instance.alloc));
265 while ((entry = readdir(directory))) {
266 size_t length = strlen(entry->d_name);
267 if (strncmp(entry->d_name, "libVKLayer", 10) != 0 ||
268 strncmp(entry->d_name + length - 3, ".so", 3) != 0)
269 continue;
270 // Open so
271 SharedLibraryHandle layer_handle = dlopen(
272 (dir_name + entry->d_name).c_str(), RTLD_NOW | RTLD_LOCAL);
273 if (!layer_handle) {
274 ALOGE("%s failed to load with error %s; Skipping",
275 entry->d_name, dlerror());
276 continue;
277 }
278
279 // Get Layers in so
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700280 PFN_vkEnumerateInstanceLayerProperties get_layer_properties =
281 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
282 dlsym(layer_handle, "vkEnumerateInstanceLayerProperties"));
Michael Lentine03c64b02015-08-26 18:27:26 -0500283 if (!get_layer_properties) {
284 ALOGE(
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700285 "%s failed to find vkEnumerateInstanceLayerProperties with "
Michael Lentine03c64b02015-08-26 18:27:26 -0500286 "error %s; Skipping",
287 entry->d_name, dlerror());
288 dlclose(layer_handle);
289 continue;
290 }
291 uint32_t count;
292 get_layer_properties(&count, nullptr);
293
294 properties.resize(count);
295 get_layer_properties(&count, &properties[0]);
296
297 // Add Layers to potential list
Michael Lentine03c64b02015-08-26 18:27:26 -0500298 for (uint32_t i = 0; i < count; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500299 String layer_name(properties[i].layerName,
300 CallbackAllocator<char>(instance.alloc));
301 LayerData layer_data = {dir_name + entry->d_name, 0, 0};
302 instance.layers.insert(std::make_pair(layer_name, layer_data));
Michael Lentine03c64b02015-08-26 18:27:26 -0500303 ALOGV("Found layer %s", properties[i].layerName);
304 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500305 dlclose(layer_handle);
Michael Lentine03c64b02015-08-26 18:27:26 -0500306 }
307 closedir(directory);
308 } else {
309 ALOGE("Failed to Open Directory %s: %s (%d)", dir_name.c_str(),
310 strerror(errno), errno);
311 }
312}
313
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500314template <class TObject>
315void ActivateLayer(TObject* object, Instance* instance, const String& name) {
316 // If object has layer, do nothing
317 auto element = instance->layers.find(name);
318 if (std::find(object->active_layers.begin(), object->active_layers.end(),
319 element) != object->active_layers.end()) {
320 ALOGW("Layer %s already activated; skipping", name.c_str());
321 return;
322 }
323 // If layer is not open, open it
324 LayerData& layer_data = element->second;
325 pthread_mutex_lock(&instance->layer_lock);
326 if (layer_data.ref_count == 0) {
327 SharedLibraryHandle layer_handle =
328 dlopen(layer_data.path.c_str(), RTLD_NOW | RTLD_LOCAL);
329 if (!layer_handle) {
330 pthread_mutex_unlock(&instance->layer_lock);
331 ALOGE("%s failed to load with error %s; Skipping",
332 layer_data.path.c_str(), dlerror());
333 return;
334 }
335 layer_data.handle = layer_handle;
336 }
337 layer_data.ref_count++;
338 pthread_mutex_unlock(&instance->layer_lock);
339 ALOGV("Activating layer %s", name.c_str());
340 object->active_layers.push_back(element);
341}
342
343template <class TObject>
344void DeactivateLayer(TObject* object,
345 Instance* instance,
346 Vector<LayerMapIterator>::iterator& element) {
347 LayerMapIterator& layer_map_data = *element;
348 object->active_layers.erase(element);
349 LayerData& layer_data = layer_map_data->second;
350 pthread_mutex_lock(&instance->layer_lock);
351 layer_data.ref_count--;
352 if (!layer_data.ref_count) {
353 dlclose(layer_data.handle);
354 }
355 pthread_mutex_unlock(&instance->layer_lock);
356}
357
Michael Lentine9da191b2015-10-13 11:08:45 -0500358struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500359 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500360 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500361};
362
Michael Lentine9da191b2015-10-13 11:08:45 -0500363void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500364 const char* value,
365 void* data) {
366 const char prefix[] = "debug.vulkan.layer.";
367 const size_t prefixlen = sizeof(prefix) - 1;
368 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
369 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500370 const char* number_str = name + prefixlen;
371 long layer_number = strtol(number_str, nullptr, 10);
372 if (layer_number <= 0 || layer_number == LONG_MAX) {
373 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
374 number_str);
375 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500376 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500377 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
378 Vector<String>* layer_names = instance_names_pair->layer_names;
379 Instance* instance = instance_names_pair->instance;
380 size_t layer_size = static_cast<size_t>(layer_number);
381 if (layer_size > layer_names->size()) {
382 layer_names->resize(layer_size,
383 String(CallbackAllocator<char>(instance->alloc)));
384 }
385 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500386}
387
388template <class TInfo, class TObject>
Jesse Hall9a16f972015-10-28 15:59:53 -0700389VkResult ActivateAllLayers(TInfo create_info, Instance* instance, TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500390 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
391 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
392 "Cannot activate layers for unknown object %p", object);
393 CallbackAllocator<char> string_allocator(instance->alloc);
394 // Load system layers
395 {
396 char layer_prop[PROPERTY_VALUE_MAX];
397 property_get("debug.vulkan.layers", layer_prop, "");
398 String layer_name(string_allocator);
399 String layer_prop_str(layer_prop, string_allocator);
400 size_t end, start = 0;
401 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
402 layer_name = layer_prop_str.substr(start, end - start);
403 auto element = instance->layers.find(layer_name);
404 if (element != instance->layers.end()) {
405 ActivateLayer(object, instance, layer_name);
406 }
407 start = end + 1;
408 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500409 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
410 InstanceNamesPair instance_names_pair = {.instance = instance,
411 .layer_names = &layer_names};
412 property_list(SetLayerNamesFromProperty,
413 static_cast<void*>(&instance_names_pair));
414 for (auto layer_name_element : layer_names) {
415 ActivateLayer(object, instance, layer_name_element);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500416 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500417 }
418 // Load app layers
419 for (uint32_t i = 0; i < create_info->layerCount; ++i) {
420 String layer_name(create_info->ppEnabledLayerNames[i],
421 string_allocator);
422 auto element = instance->layers.find(layer_name);
423 if (element == instance->layers.end()) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700424 ALOGE("requested %s layer '%s' not present",
425 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ?
426 "instance" : "device",
427 layer_name.c_str());
428 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500429 } else {
430 ActivateLayer(object, instance, layer_name);
431 }
432 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700433 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500434}
435
436template <class TCreateInfo>
437bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
438 const char* extension_name,
439 const VkAllocCallbacks* alloc) {
440 for (uint32_t i = 0; i < local_create_info.extensionCount; ++i) {
441 if (!strcmp(extension_name,
442 local_create_info.ppEnabledExtensionNames[i])) {
443 return false;
444 }
445 }
446 uint32_t extension_count = local_create_info.extensionCount;
447 local_create_info.extensionCount++;
448 void* mem = alloc->pfnAlloc(
449 alloc->pUserData, local_create_info.extensionCount * sizeof(char*),
450 alignof(char*), VK_SYSTEM_ALLOC_TYPE_INTERNAL);
451 if (mem) {
452 const char** enabled_extensions = static_cast<const char**>(mem);
453 for (uint32_t i = 0; i < extension_count; ++i) {
454 enabled_extensions[i] =
455 local_create_info.ppEnabledExtensionNames[i];
456 }
457 enabled_extensions[extension_count] = extension_name;
458 local_create_info.ppEnabledExtensionNames = enabled_extensions;
459 } else {
460 ALOGW("%s extension cannot be enabled: memory allocation failed",
461 extension_name);
462 local_create_info.extensionCount--;
463 return false;
464 }
465 return true;
466}
467
468template <class T>
469void FreeAllocatedCreateInfo(T& local_create_info,
470 const VkAllocCallbacks* alloc) {
471 alloc->pfnFree(
472 alloc->pUserData,
473 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
474}
475
Michael Lentineeb970862015-10-15 12:42:22 -0500476VkBool32 LogDebugMessageCallback(VkFlags message_flags,
477 VkDbgObjectType /*obj_type*/,
478 uint64_t /*src_object*/,
479 size_t /*location*/,
480 int32_t message_code,
481 const char* layer_prefix,
482 const char* message,
483 void* /*user_data*/) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500484 if (message_flags & VK_DBG_REPORT_ERROR_BIT) {
485 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
486 } else if (message_flags & VK_DBG_REPORT_WARN_BIT) {
487 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
488 }
Michael Lentineeb970862015-10-15 12:42:22 -0500489 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500490}
491
492VkResult CreateDeviceNoop(VkPhysicalDevice,
493 const VkDeviceCreateInfo*,
494 VkDevice*) {
495 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) {
503 return reinterpret_cast<PFN_vkVoidFunction>(CreateDeviceNoop);
504 }
505 if (!device)
506 return GetGlobalDeviceProcAddr(name);
507 Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500508 return loader_device->instance->drv.GetDeviceProcAddr(device, name);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500509}
510
Jesse Hall04f4f472015-08-16 19:51:04 -0700511// -----------------------------------------------------------------------------
512// "Bottom" functions. These are called at the end of the instance dispatch
513// chain.
514
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700515void DestroyInstanceBottom(VkInstance instance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700516 // These checks allow us to call DestroyInstanceBottom from any error path
517 // in CreateInstanceBottom, before the driver instance is fully initialized.
518 if (instance->drv.vtbl.instance != VK_NULL_HANDLE &&
519 instance->drv.vtbl.DestroyInstance) {
520 instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance);
521 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500522 for (auto it = instance->active_layers.begin();
523 it != instance->active_layers.end(); ++it) {
524 DeactivateLayer(instance, instance, it);
Michael Lentine03c64b02015-08-26 18:27:26 -0500525 }
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500526 if (instance->message) {
527 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500528 DebugDestroyMessageCallback =
529 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
530 vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback"));
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500531 DebugDestroyMessageCallback(instance, instance->message);
532 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700533 const VkAllocCallbacks* alloc = instance->alloc;
534 instance->~VkInstance_T();
535 alloc->pfnFree(alloc->pUserData, instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700536}
537
538VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info,
539 VkInstance* instance_ptr) {
540 Instance* instance = *instance_ptr;
541 VkResult result;
542
543 result =
544 g_hwdevice->CreateInstance(create_info, &instance->drv.vtbl.instance);
545 if (result != VK_SUCCESS) {
546 DestroyInstanceBottom(instance);
547 return result;
548 }
549
Michael Lentine03c64b02015-08-26 18:27:26 -0500550 if (!LoadInstanceVtbl(
551 instance->drv.vtbl.instance, instance->drv.vtbl.instance,
552 g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700553 DestroyInstanceBottom(instance);
554 return VK_ERROR_INITIALIZATION_FAILED;
555 }
556
557 // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be
558 // queryable from the Instance, and that the resulting function work for any
559 // VkDevice created from the instance.
560 instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
561 g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance,
562 "vkGetDeviceProcAddr"));
563 if (!instance->drv.GetDeviceProcAddr) {
564 ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr");
565 DestroyInstanceBottom(instance);
566 return VK_ERROR_INITIALIZATION_FAILED;
567 }
568
569 hwvulkan_dispatch_t* dispatch =
570 reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance);
571 if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
572 // Skip setting dispatch->vtbl on the driver instance handle, since we
573 // never intentionally call through it; we go through Instance::drv.vtbl
574 // instead.
575 } else {
576 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
577 dispatch->magic);
578 DestroyInstanceBottom(instance);
579 return VK_ERROR_INITIALIZATION_FAILED;
580 }
581
582 uint32_t num_physical_devices = 0;
583 result = instance->drv.vtbl.EnumeratePhysicalDevices(
584 instance->drv.vtbl.instance, &num_physical_devices, nullptr);
585 if (result != VK_SUCCESS) {
586 DestroyInstanceBottom(instance);
587 return VK_ERROR_INITIALIZATION_FAILED;
588 }
589 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
590 result = instance->drv.vtbl.EnumeratePhysicalDevices(
591 instance->drv.vtbl.instance, &num_physical_devices,
592 instance->physical_devices);
593 if (result != VK_SUCCESS) {
594 DestroyInstanceBottom(instance);
595 return VK_ERROR_INITIALIZATION_FAILED;
596 }
597 for (uint32_t i = 0; i < num_physical_devices; i++) {
598 dispatch = reinterpret_cast<hwvulkan_dispatch_t*>(
599 instance->physical_devices[i]);
600 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
601 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
602 dispatch->magic);
603 DestroyInstanceBottom(instance);
604 return VK_ERROR_INITIALIZATION_FAILED;
605 }
606 dispatch->vtbl = instance->vtbl;
607 }
608 instance->drv.num_physical_devices = num_physical_devices;
609
610 instance->num_physical_devices = instance->drv.num_physical_devices;
611 return VK_SUCCESS;
612}
613
614VkResult EnumeratePhysicalDevicesBottom(VkInstance instance,
615 uint32_t* pdev_count,
616 VkPhysicalDevice* pdevs) {
617 uint32_t count = instance->num_physical_devices;
618 if (pdevs) {
619 count = std::min(count, *pdev_count);
620 std::copy(instance->physical_devices,
621 instance->physical_devices + count, pdevs);
622 }
623 *pdev_count = count;
624 return VK_SUCCESS;
625}
626
627VkResult GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev,
628 VkPhysicalDeviceFeatures* features) {
629 return GetVtbl(pdev)
630 ->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features);
631}
632
633VkResult GetPhysicalDeviceFormatPropertiesBottom(
634 VkPhysicalDevice pdev,
635 VkFormat format,
636 VkFormatProperties* properties) {
637 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties(
638 pdev, format, properties);
639}
640
641VkResult GetPhysicalDeviceImageFormatPropertiesBottom(
642 VkPhysicalDevice pdev,
643 VkFormat format,
644 VkImageType type,
645 VkImageTiling tiling,
646 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700647 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700648 VkImageFormatProperties* properties) {
649 return GetVtbl(pdev)
650 ->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties(
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700651 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700652}
653
654VkResult GetPhysicalDevicePropertiesBottom(
655 VkPhysicalDevice pdev,
656 VkPhysicalDeviceProperties* properties) {
657 return GetVtbl(pdev)
658 ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties);
659}
660
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700661VkResult GetPhysicalDeviceQueueFamilyPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700662 VkPhysicalDevice pdev,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700663 uint32_t* pCount,
664 VkQueueFamilyProperties* properties) {
665 return GetVtbl(pdev)
666 ->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties(
667 pdev, pCount, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700668}
669
670VkResult GetPhysicalDeviceMemoryPropertiesBottom(
671 VkPhysicalDevice pdev,
672 VkPhysicalDeviceMemoryProperties* properties) {
673 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties(
674 pdev, properties);
675}
676
677VkResult CreateDeviceBottom(VkPhysicalDevice pdev,
678 const VkDeviceCreateInfo* create_info,
679 VkDevice* out_device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500680 Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700681 VkResult result;
682
683 void* mem = instance.alloc->pfnAlloc(instance.alloc->pUserData,
684 sizeof(Device), alignof(Device),
685 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
686 if (!mem)
687 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500688 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700689
Jesse Hall9a16f972015-10-28 15:59:53 -0700690 result = ActivateAllLayers(create_info, &instance, device);
691 if (result != VK_SUCCESS) {
692 DestroyDevice(device);
693 return result;
694 }
695
Jesse Hall04f4f472015-08-16 19:51:04 -0700696 VkDevice drv_device;
697 result = instance.drv.vtbl.CreateDevice(pdev, create_info, &drv_device);
698 if (result != VK_SUCCESS) {
699 DestroyDevice(device);
700 return result;
701 }
702
Jesse Hall04f4f472015-08-16 19:51:04 -0700703 hwvulkan_dispatch_t* dispatch =
704 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
705 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
706 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500707 PFN_vkDestroyDevice destroy_device =
708 reinterpret_cast<PFN_vkDestroyDevice>(
709 instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice"));
710 destroy_device(drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700711 DestroyDevice(device);
712 return VK_ERROR_INITIALIZATION_FAILED;
713 }
714 dispatch->vtbl = &device->vtbl_storage;
715
Jesse Hallb1352bc2015-09-04 16:12:33 -0700716 device->vtbl_storage.GetSurfacePropertiesKHR = GetSurfacePropertiesKHR;
717 device->vtbl_storage.GetSurfaceFormatsKHR = GetSurfaceFormatsKHR;
718 device->vtbl_storage.GetSurfacePresentModesKHR = GetSurfacePresentModesKHR;
719 device->vtbl_storage.CreateSwapchainKHR = CreateSwapchainKHR;
720 device->vtbl_storage.DestroySwapchainKHR = DestroySwapchainKHR;
721 device->vtbl_storage.GetSwapchainImagesKHR = GetSwapchainImagesKHR;
722 device->vtbl_storage.AcquireNextImageKHR = AcquireNextImageKHR;
723 device->vtbl_storage.QueuePresentKHR = QueuePresentKHR;
724
Michael Lentine03c64b02015-08-26 18:27:26 -0500725 void* base_object = static_cast<void*>(drv_device);
726 void* next_object = base_object;
727 VkLayerLinkedListElem* next_element;
728 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr;
729 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500730 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500731 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
732
733 for (size_t i = elem_list.size(); i > 0; i--) {
734 size_t idx = i - 1;
735 next_element = &elem_list[idx];
736 next_element->get_proc_addr =
737 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
738 next_element->base_object = base_object;
739 next_element->next_element = next_object;
740 next_object = static_cast<void*>(next_element);
741
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500742 auto& name = device->active_layers[idx]->first;
743 auto& handle = device->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500744 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500745 dlsym(handle, (name + "GetDeviceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500746 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500747 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500748 dlsym(handle, "vkGetDeviceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700749 if (!next_get_proc_addr) {
750 ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500751 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700752 next_object = next_element->next_element;
753 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
754 next_element->get_proc_addr);
755 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500756 }
757 }
758
759 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
760 static_cast<VkDevice>(next_object), next_get_proc_addr,
761 device->vtbl_storage)) {
762 DestroyDevice(device);
763 return VK_ERROR_INITIALIZATION_FAILED;
764 }
765
766 PFN_vkCreateDevice layer_createDevice =
767 reinterpret_cast<PFN_vkCreateDevice>(
768 device->vtbl_storage.GetDeviceProcAddr(drv_device,
769 "vkCreateDevice"));
770 layer_createDevice(pdev, create_info, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700771
772 *out_device = drv_device;
773 return VK_SUCCESS;
774}
775
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700776VkResult EnumerateDeviceExtensionPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700777 VkPhysicalDevice pdev,
778 const char* layer_name,
779 uint32_t* properties_count,
780 VkExtensionProperties* properties) {
781 // TODO: what are we supposed to do with layer_name here?
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700782 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties(
783 pdev, layer_name, properties_count, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700784}
785
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700786VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev,
787 uint32_t* properties_count,
788 VkLayerProperties* properties) {
789 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700790 pdev, properties_count, properties);
791}
792
793VkResult GetPhysicalDeviceSparseImageFormatPropertiesBottom(
794 VkPhysicalDevice pdev,
795 VkFormat format,
796 VkImageType type,
797 uint32_t samples,
798 VkImageUsageFlags usage,
799 VkImageTiling tiling,
800 uint32_t* properties_count,
801 VkSparseImageFormatProperties* properties) {
802 return GetVtbl(pdev)
803 ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties(
804 pdev, format, type, samples, usage, tiling, properties_count,
805 properties);
806}
807
808PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*);
809
810const InstanceVtbl kBottomInstanceFunctions = {
811 // clang-format off
812 .instance = nullptr,
813 .CreateInstance = CreateInstanceBottom,
814 .DestroyInstance = DestroyInstanceBottom,
815 .GetInstanceProcAddr = GetInstanceProcAddrBottom,
816 .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom,
817 .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom,
818 .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom,
819 .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700820 .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700821 .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700822 .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom,
823 .CreateDevice = CreateDeviceBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700824 .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom,
825 .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700826 .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700827 .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR,
Jesse Hall04f4f472015-08-16 19:51:04 -0700828 // clang-format on
829};
830
Michael Lentine03c64b02015-08-26 18:27:26 -0500831VkResult Noop(...) {
832 return VK_SUCCESS;
833}
834
Jesse Hall04f4f472015-08-16 19:51:04 -0700835PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500836 // TODO: Possibly move this into the instance table
837 // TODO: Possibly register the callbacks in the loader
838 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
839 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
840 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
841 }
842 if (strcmp(name, "vkCreateInstance") == 0) {
843 return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
844 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700845 return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
846}
847
848} // namespace
849
850// -----------------------------------------------------------------------------
851// Global functions. These are called directly from the loader entry points,
852// without going through a dispatch table.
853
854namespace vulkan {
855
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700856VkResult EnumerateInstanceExtensionProperties(
857 const char* /*layer_name*/,
858 uint32_t* count,
859 VkExtensionProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700860 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700861 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700862
863 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700864 ALOGW("vkEnumerateInstanceExtensionProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700865
866 *count = 0;
867 return VK_SUCCESS;
868}
869
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700870VkResult EnumerateInstanceLayerProperties(uint32_t* count,
871 VkLayerProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700872 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700873 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700874
875 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700876 ALOGW("vkEnumerateInstanceLayerProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700877
878 *count = 0;
879 return VK_SUCCESS;
880}
881
882VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
883 VkInstance* out_instance) {
884 VkResult result;
885
886 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700887 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700888
889 VkInstanceCreateInfo local_create_info = *create_info;
890 if (!local_create_info.pAllocCb)
891 local_create_info.pAllocCb = &kDefaultAllocCallbacks;
892 create_info = &local_create_info;
893
894 void* instance_mem = create_info->pAllocCb->pfnAlloc(
895 create_info->pAllocCb->pUserData, sizeof(Instance), alignof(Instance),
896 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
897 if (!instance_mem)
898 return VK_ERROR_OUT_OF_HOST_MEMORY;
899 Instance* instance = new (instance_mem) Instance(create_info->pAllocCb);
900
901 instance->vtbl_storage = kBottomInstanceFunctions;
902 instance->vtbl_storage.instance = instance;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500903 instance->message = VK_NULL_HANDLE;
Jesse Hall04f4f472015-08-16 19:51:04 -0700904
Michael Lentine03c64b02015-08-26 18:27:26 -0500905 // Scan layers
Michael Lentine03c64b02015-08-26 18:27:26 -0500906 CallbackAllocator<char> string_allocator(instance->alloc);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500907
Michael Lentine03c64b02015-08-26 18:27:26 -0500908 String dir_name("/data/local/tmp/vulkan/", string_allocator);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500909 FindLayersInDirectory(*instance, dir_name);
Michael Lentine1c69b9e2015-09-14 13:26:59 -0500910 const std::string& path = LoaderData::GetInstance().layer_path;
911 dir_name.assign(path.c_str(), path.size());
912 dir_name.append("/");
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500913 FindLayersInDirectory(*instance, dir_name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700914
Jesse Hall9a16f972015-10-28 15:59:53 -0700915 result = ActivateAllLayers(create_info, instance, instance);
916 if (result != VK_SUCCESS) {
917 DestroyInstanceBottom(instance);
918 return result;
919 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500920
921 void* base_object = static_cast<void*>(instance);
922 void* next_object = base_object;
923 VkLayerLinkedListElem* next_element;
924 PFN_vkGetInstanceProcAddr next_get_proc_addr =
925 kBottomInstanceFunctions.GetInstanceProcAddr;
926 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700927 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500928 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
929
930 for (size_t i = elem_list.size(); i > 0; i--) {
931 size_t idx = i - 1;
932 next_element = &elem_list[idx];
933 next_element->get_proc_addr =
934 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
935 next_element->base_object = base_object;
936 next_element->next_element = next_object;
937 next_object = static_cast<void*>(next_element);
938
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500939 auto& name = instance->active_layers[idx]->first;
940 auto& handle = instance->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500941 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500942 dlsym(handle, (name + "GetInstanceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500943 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500944 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500945 dlsym(handle, "vkGetInstanceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700946 if (!next_get_proc_addr) {
947 ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500948 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700949 next_object = next_element->next_element;
950 next_get_proc_addr =
951 reinterpret_cast<PFN_vkGetInstanceProcAddr>(
952 next_element->get_proc_addr);
953 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500954 }
955 }
956
957 if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object),
958 static_cast<VkInstance>(next_object),
959 next_get_proc_addr, instance->vtbl_storage)) {
960 DestroyInstanceBottom(instance);
961 return VK_ERROR_INITIALIZATION_FAILED;
962 }
963
Michael Lentine950bb4f2015-09-14 13:26:30 -0500964 // Force enable callback extension if required
965 bool enable_callback =
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500966 property_get_bool("debug.vulkan.enable_callback", false);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500967 bool enable_logging = enable_callback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500968 const char* extension_name = "DEBUG_REPORT";
Michael Lentine950bb4f2015-09-14 13:26:30 -0500969 if (enable_callback) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500970 enable_callback = AddExtensionToCreateInfo(
971 local_create_info, extension_name, instance->alloc);
Michael Lentine950bb4f2015-09-14 13:26:30 -0500972 }
973
Jesse Hall04f4f472015-08-16 19:51:04 -0700974 *out_instance = instance;
Michael Lentine03c64b02015-08-26 18:27:26 -0500975 result = instance->vtbl_storage.CreateInstance(create_info, out_instance);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500976 if (enable_callback)
977 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -0700978 if (result <= 0) {
979 // For every layer, including the loader top and bottom layers:
980 // - If a call to the next CreateInstance fails, the layer must clean
981 // up anything it has successfully done so far, and propagate the
982 // error upwards.
983 // - If a layer successfully calls the next layer's CreateInstance, and
984 // afterwards must fail for some reason, it must call the next layer's
985 // DestroyInstance before returning.
986 // - The layer must not call the next layer's DestroyInstance if that
987 // layer's CreateInstance wasn't called, or returned failure.
988
989 // On failure, CreateInstanceBottom frees the instance struct, so it's
990 // already gone at this point. Nothing to do.
991 }
992
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500993 if (enable_logging) {
994 PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500995 DebugCreateMessageCallback =
996 reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
997 vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback"));
998 DebugCreateMessageCallback(
999 instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1000 LogDebugMessageCallback, NULL, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001001 }
1002
Jesse Hall04f4f472015-08-16 19:51:04 -07001003 return result;
1004}
1005
1006PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
1007 if (!instance)
1008 return GetGlobalInstanceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001009 // TODO: Possibly move this into the instance table
1010 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
1011 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
1012 if (!instance->vtbl)
1013 return NULL;
1014 PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr;
1015 return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name));
1016 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001017 // For special-case functions we always return the loader entry
1018 if (strcmp(name, "vkGetInstanceProcAddr") == 0 ||
1019 strcmp(name, "vkGetDeviceProcAddr") == 0) {
1020 return GetGlobalInstanceProcAddr(name);
1021 }
1022 return GetSpecificInstanceProcAddr(instance->vtbl, name);
1023}
1024
1025PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
1026 if (!device)
1027 return GetGlobalDeviceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001028 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
1029 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
1030 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001031 if (strcmp(name, "vkGetDeviceQueue") == 0) {
1032 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
1033 }
1034 if (strcmp(name, "vkCreateCommandBuffer") == 0) {
1035 return reinterpret_cast<PFN_vkVoidFunction>(CreateCommandBuffer);
1036 }
1037 if (strcmp(name, "vkDestroyDevice") == 0) {
1038 return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
1039 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001040 // For special-case functions we always return the loader entry
1041 if (strcmp(name, "vkGetDeviceQueue") == 0 ||
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001042 strcmp(name, "vkCreateCommandBuffer") == 0 ||
Jesse Hall04f4f472015-08-16 19:51:04 -07001043 strcmp(name, "vkDestroyDevice") == 0) {
1044 return GetGlobalDeviceProcAddr(name);
1045 }
1046 return GetSpecificDeviceProcAddr(GetVtbl(device), name);
1047}
1048
1049VkResult GetDeviceQueue(VkDevice drv_device,
1050 uint32_t family,
1051 uint32_t index,
1052 VkQueue* out_queue) {
1053 VkResult result;
1054 VkQueue queue;
1055 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1056 result = vtbl->GetDeviceQueue(drv_device, family, index, &queue);
1057 if (result != VK_SUCCESS)
1058 return result;
1059 hwvulkan_dispatch_t* dispatch =
1060 reinterpret_cast<hwvulkan_dispatch_t*>(queue);
1061 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != &vtbl) {
1062 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic);
1063 return VK_ERROR_INITIALIZATION_FAILED;
1064 }
1065 dispatch->vtbl = vtbl;
1066 *out_queue = queue;
1067 return VK_SUCCESS;
1068}
1069
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001070VkResult CreateCommandBuffer(VkDevice drv_device,
1071 const VkCmdBufferCreateInfo* create_info,
1072 VkCmdBuffer* out_cmdbuf) {
1073 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1074 VkCmdBuffer cmdbuf;
1075 VkResult result =
1076 vtbl->CreateCommandBuffer(drv_device, create_info, &cmdbuf);
1077 if (result != VK_SUCCESS)
1078 return result;
1079 hwvulkan_dispatch_t* dispatch =
1080 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuf);
1081 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
1082 ALOGE("invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR,
1083 dispatch->magic);
1084 return VK_ERROR_INITIALIZATION_FAILED;
1085 }
1086 dispatch->vtbl = vtbl;
1087 *out_cmdbuf = cmdbuf;
1088 return VK_SUCCESS;
1089}
1090
Jesse Hall04f4f472015-08-16 19:51:04 -07001091VkResult DestroyDevice(VkDevice drv_device) {
1092 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1093 Device* device = static_cast<Device*>(vtbl->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001094 for (auto it = device->active_layers.begin();
1095 it != device->active_layers.end(); ++it) {
1096 DeactivateLayer(device, device->instance, it);
1097 }
1098 device->active_layers.clear();
Jesse Hall04f4f472015-08-16 19:51:04 -07001099 vtbl->DestroyDevice(drv_device);
1100 DestroyDevice(device);
1101 return VK_SUCCESS;
1102}
1103
Jesse Halld7b994a2015-09-07 14:17:37 -07001104void* AllocDeviceMem(VkDevice device,
1105 size_t size,
1106 size_t align,
1107 VkSystemAllocType type) {
1108 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001109 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001110 return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, type);
1111}
1112
1113void FreeDeviceMem(VkDevice device, void* ptr) {
1114 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001115 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001116 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1117}
1118
1119const DeviceVtbl& GetDriverVtbl(VkDevice device) {
1120 // TODO(jessehall): This actually returns the API-level vtbl for the
1121 // device, not the driver entry points. Given the current use -- getting
1122 // the driver's private swapchain-related functions -- that works, but is
1123 // misleading and likely to cause bugs. Fix as part of separating the
1124 // loader->driver interface from the app->loader interface.
1125 return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage;
1126}
1127
1128const DeviceVtbl& GetDriverVtbl(VkQueue queue) {
1129 // TODO(jessehall): This actually returns the API-level vtbl for the
1130 // device, not the driver entry points. Given the current use -- getting
1131 // the driver's private swapchain-related functions -- that works, but is
1132 // misleading and likely to cause bugs. Fix as part of separating the
1133 // loader->driver interface from the app->loader interface.
1134 return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage;
1135}
1136
Jesse Hall04f4f472015-08-16 19:51:04 -07001137} // namespace vulkan