blob: 265ee6c428a3f774f70228e39c58fcbc2848d482 [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
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800497VkResult Noop(...) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500498 return VK_SUCCESS;
499}
500
501PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) {
502 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
503 return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr);
504 }
505 if (strcmp(name, "vkCreateDevice") == 0) {
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800506 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
Michael Lentine03c64b02015-08-26 18:27:26 -0500507 }
508 if (!device)
509 return GetGlobalDeviceProcAddr(name);
510 Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500511 return loader_device->instance->drv.GetDeviceProcAddr(device, name);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500512}
513
Jesse Hall04f4f472015-08-16 19:51:04 -0700514// -----------------------------------------------------------------------------
515// "Bottom" functions. These are called at the end of the instance dispatch
516// chain.
517
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700518void DestroyInstanceBottom(VkInstance instance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700519 // These checks allow us to call DestroyInstanceBottom from any error path
520 // in CreateInstanceBottom, before the driver instance is fully initialized.
521 if (instance->drv.vtbl.instance != VK_NULL_HANDLE &&
522 instance->drv.vtbl.DestroyInstance) {
523 instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance);
524 }
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500525 if (instance->message) {
526 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500527 DebugDestroyMessageCallback =
528 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
529 vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback"));
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500530 DebugDestroyMessageCallback(instance, instance->message);
531 }
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800532 for (auto it = instance->active_layers.begin();
533 it != instance->active_layers.end(); ++it) {
534 DeactivateLayer(instance, instance, it);
535 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700536 const VkAllocCallbacks* alloc = instance->alloc;
537 instance->~VkInstance_T();
538 alloc->pfnFree(alloc->pUserData, instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700539}
540
541VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info,
542 VkInstance* instance_ptr) {
543 Instance* instance = *instance_ptr;
544 VkResult result;
545
546 result =
547 g_hwdevice->CreateInstance(create_info, &instance->drv.vtbl.instance);
548 if (result != VK_SUCCESS) {
549 DestroyInstanceBottom(instance);
550 return result;
551 }
552
Michael Lentine03c64b02015-08-26 18:27:26 -0500553 if (!LoadInstanceVtbl(
554 instance->drv.vtbl.instance, instance->drv.vtbl.instance,
555 g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700556 DestroyInstanceBottom(instance);
557 return VK_ERROR_INITIALIZATION_FAILED;
558 }
559
560 // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be
561 // queryable from the Instance, and that the resulting function work for any
562 // VkDevice created from the instance.
563 instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
564 g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance,
565 "vkGetDeviceProcAddr"));
566 if (!instance->drv.GetDeviceProcAddr) {
567 ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr");
568 DestroyInstanceBottom(instance);
569 return VK_ERROR_INITIALIZATION_FAILED;
570 }
571
572 hwvulkan_dispatch_t* dispatch =
573 reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance);
574 if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
575 // Skip setting dispatch->vtbl on the driver instance handle, since we
576 // never intentionally call through it; we go through Instance::drv.vtbl
577 // instead.
578 } else {
579 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
580 dispatch->magic);
581 DestroyInstanceBottom(instance);
582 return VK_ERROR_INITIALIZATION_FAILED;
583 }
584
585 uint32_t num_physical_devices = 0;
586 result = instance->drv.vtbl.EnumeratePhysicalDevices(
587 instance->drv.vtbl.instance, &num_physical_devices, nullptr);
588 if (result != VK_SUCCESS) {
589 DestroyInstanceBottom(instance);
590 return VK_ERROR_INITIALIZATION_FAILED;
591 }
592 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
593 result = instance->drv.vtbl.EnumeratePhysicalDevices(
594 instance->drv.vtbl.instance, &num_physical_devices,
595 instance->physical_devices);
596 if (result != VK_SUCCESS) {
597 DestroyInstanceBottom(instance);
598 return VK_ERROR_INITIALIZATION_FAILED;
599 }
600 for (uint32_t i = 0; i < num_physical_devices; i++) {
601 dispatch = reinterpret_cast<hwvulkan_dispatch_t*>(
602 instance->physical_devices[i]);
603 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
604 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
605 dispatch->magic);
606 DestroyInstanceBottom(instance);
607 return VK_ERROR_INITIALIZATION_FAILED;
608 }
609 dispatch->vtbl = instance->vtbl;
610 }
611 instance->drv.num_physical_devices = num_physical_devices;
612
613 instance->num_physical_devices = instance->drv.num_physical_devices;
614 return VK_SUCCESS;
615}
616
617VkResult EnumeratePhysicalDevicesBottom(VkInstance instance,
618 uint32_t* pdev_count,
619 VkPhysicalDevice* pdevs) {
620 uint32_t count = instance->num_physical_devices;
621 if (pdevs) {
622 count = std::min(count, *pdev_count);
623 std::copy(instance->physical_devices,
624 instance->physical_devices + count, pdevs);
625 }
626 *pdev_count = count;
627 return VK_SUCCESS;
628}
629
630VkResult GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev,
631 VkPhysicalDeviceFeatures* features) {
632 return GetVtbl(pdev)
633 ->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features);
634}
635
636VkResult GetPhysicalDeviceFormatPropertiesBottom(
637 VkPhysicalDevice pdev,
638 VkFormat format,
639 VkFormatProperties* properties) {
640 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties(
641 pdev, format, properties);
642}
643
644VkResult GetPhysicalDeviceImageFormatPropertiesBottom(
645 VkPhysicalDevice pdev,
646 VkFormat format,
647 VkImageType type,
648 VkImageTiling tiling,
649 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700650 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700651 VkImageFormatProperties* properties) {
652 return GetVtbl(pdev)
653 ->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties(
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700654 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700655}
656
657VkResult GetPhysicalDevicePropertiesBottom(
658 VkPhysicalDevice pdev,
659 VkPhysicalDeviceProperties* properties) {
660 return GetVtbl(pdev)
661 ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties);
662}
663
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700664VkResult GetPhysicalDeviceQueueFamilyPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700665 VkPhysicalDevice pdev,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700666 uint32_t* pCount,
667 VkQueueFamilyProperties* properties) {
668 return GetVtbl(pdev)
669 ->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties(
670 pdev, pCount, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700671}
672
673VkResult GetPhysicalDeviceMemoryPropertiesBottom(
674 VkPhysicalDevice pdev,
675 VkPhysicalDeviceMemoryProperties* properties) {
676 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties(
677 pdev, properties);
678}
679
680VkResult CreateDeviceBottom(VkPhysicalDevice pdev,
681 const VkDeviceCreateInfo* create_info,
682 VkDevice* out_device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500683 Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700684 VkResult result;
685
686 void* mem = instance.alloc->pfnAlloc(instance.alloc->pUserData,
687 sizeof(Device), alignof(Device),
688 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
689 if (!mem)
690 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500691 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700692
Jesse Hall9a16f972015-10-28 15:59:53 -0700693 result = ActivateAllLayers(create_info, &instance, device);
694 if (result != VK_SUCCESS) {
695 DestroyDevice(device);
696 return result;
697 }
698
Jesse Hall04f4f472015-08-16 19:51:04 -0700699 VkDevice drv_device;
700 result = instance.drv.vtbl.CreateDevice(pdev, create_info, &drv_device);
701 if (result != VK_SUCCESS) {
702 DestroyDevice(device);
703 return result;
704 }
705
Jesse Hall04f4f472015-08-16 19:51:04 -0700706 hwvulkan_dispatch_t* dispatch =
707 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
708 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
709 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500710 PFN_vkDestroyDevice destroy_device =
711 reinterpret_cast<PFN_vkDestroyDevice>(
712 instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice"));
713 destroy_device(drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700714 DestroyDevice(device);
715 return VK_ERROR_INITIALIZATION_FAILED;
716 }
717 dispatch->vtbl = &device->vtbl_storage;
718
Jesse Hallb1352bc2015-09-04 16:12:33 -0700719 device->vtbl_storage.GetSurfacePropertiesKHR = GetSurfacePropertiesKHR;
720 device->vtbl_storage.GetSurfaceFormatsKHR = GetSurfaceFormatsKHR;
721 device->vtbl_storage.GetSurfacePresentModesKHR = GetSurfacePresentModesKHR;
722 device->vtbl_storage.CreateSwapchainKHR = CreateSwapchainKHR;
723 device->vtbl_storage.DestroySwapchainKHR = DestroySwapchainKHR;
724 device->vtbl_storage.GetSwapchainImagesKHR = GetSwapchainImagesKHR;
725 device->vtbl_storage.AcquireNextImageKHR = AcquireNextImageKHR;
726 device->vtbl_storage.QueuePresentKHR = QueuePresentKHR;
727
Michael Lentine03c64b02015-08-26 18:27:26 -0500728 void* base_object = static_cast<void*>(drv_device);
729 void* next_object = base_object;
730 VkLayerLinkedListElem* next_element;
731 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr;
732 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500733 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500734 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
735
736 for (size_t i = elem_list.size(); i > 0; i--) {
737 size_t idx = i - 1;
738 next_element = &elem_list[idx];
739 next_element->get_proc_addr =
740 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
741 next_element->base_object = base_object;
742 next_element->next_element = next_object;
743 next_object = static_cast<void*>(next_element);
744
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500745 auto& name = device->active_layers[idx]->first;
746 auto& handle = device->active_layers[idx]->second.handle;
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, (name + "GetDeviceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500749 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500750 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500751 dlsym(handle, "vkGetDeviceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700752 if (!next_get_proc_addr) {
753 ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500754 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700755 next_object = next_element->next_element;
756 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
757 next_element->get_proc_addr);
758 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500759 }
760 }
761
762 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
763 static_cast<VkDevice>(next_object), next_get_proc_addr,
764 device->vtbl_storage)) {
765 DestroyDevice(device);
766 return VK_ERROR_INITIALIZATION_FAILED;
767 }
768
769 PFN_vkCreateDevice layer_createDevice =
770 reinterpret_cast<PFN_vkCreateDevice>(
771 device->vtbl_storage.GetDeviceProcAddr(drv_device,
772 "vkCreateDevice"));
773 layer_createDevice(pdev, create_info, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700774
775 *out_device = drv_device;
776 return VK_SUCCESS;
777}
778
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700779VkResult EnumerateDeviceExtensionPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700780 VkPhysicalDevice pdev,
781 const char* layer_name,
782 uint32_t* properties_count,
783 VkExtensionProperties* properties) {
784 // TODO: what are we supposed to do with layer_name here?
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700785 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties(
786 pdev, layer_name, properties_count, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700787}
788
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700789VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev,
790 uint32_t* properties_count,
791 VkLayerProperties* properties) {
792 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700793 pdev, properties_count, properties);
794}
795
796VkResult GetPhysicalDeviceSparseImageFormatPropertiesBottom(
797 VkPhysicalDevice pdev,
798 VkFormat format,
799 VkImageType type,
800 uint32_t samples,
801 VkImageUsageFlags usage,
802 VkImageTiling tiling,
803 uint32_t* properties_count,
804 VkSparseImageFormatProperties* properties) {
805 return GetVtbl(pdev)
806 ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties(
807 pdev, format, type, samples, usage, tiling, properties_count,
808 properties);
809}
810
811PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*);
812
813const InstanceVtbl kBottomInstanceFunctions = {
814 // clang-format off
815 .instance = nullptr,
816 .CreateInstance = CreateInstanceBottom,
817 .DestroyInstance = DestroyInstanceBottom,
818 .GetInstanceProcAddr = GetInstanceProcAddrBottom,
819 .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom,
820 .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom,
821 .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom,
822 .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700823 .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700824 .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700825 .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom,
826 .CreateDevice = CreateDeviceBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700827 .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom,
828 .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700829 .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700830 .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR,
Jesse Hall04f4f472015-08-16 19:51:04 -0700831 // clang-format on
832};
833
834PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500835 // TODO: Possibly move this into the instance table
836 // TODO: Possibly register the callbacks in the loader
837 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
838 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
839 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
840 }
841 if (strcmp(name, "vkCreateInstance") == 0) {
842 return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
843 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700844 return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
845}
846
847} // namespace
848
849// -----------------------------------------------------------------------------
850// Global functions. These are called directly from the loader entry points,
851// without going through a dispatch table.
852
853namespace vulkan {
854
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700855VkResult EnumerateInstanceExtensionProperties(
856 const char* /*layer_name*/,
857 uint32_t* count,
858 VkExtensionProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700859 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700860 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700861
862 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700863 ALOGW("vkEnumerateInstanceExtensionProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700864
865 *count = 0;
866 return VK_SUCCESS;
867}
868
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700869VkResult EnumerateInstanceLayerProperties(uint32_t* count,
870 VkLayerProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700871 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700872 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700873
874 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700875 ALOGW("vkEnumerateInstanceLayerProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700876
877 *count = 0;
878 return VK_SUCCESS;
879}
880
881VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
882 VkInstance* out_instance) {
883 VkResult result;
884
885 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700886 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700887
888 VkInstanceCreateInfo local_create_info = *create_info;
889 if (!local_create_info.pAllocCb)
890 local_create_info.pAllocCb = &kDefaultAllocCallbacks;
891 create_info = &local_create_info;
892
893 void* instance_mem = create_info->pAllocCb->pfnAlloc(
894 create_info->pAllocCb->pUserData, sizeof(Instance), alignof(Instance),
895 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
896 if (!instance_mem)
897 return VK_ERROR_OUT_OF_HOST_MEMORY;
898 Instance* instance = new (instance_mem) Instance(create_info->pAllocCb);
899
900 instance->vtbl_storage = kBottomInstanceFunctions;
901 instance->vtbl_storage.instance = instance;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500902 instance->message = VK_NULL_HANDLE;
Jesse Hall04f4f472015-08-16 19:51:04 -0700903
Michael Lentine03c64b02015-08-26 18:27:26 -0500904 // Scan layers
Michael Lentine03c64b02015-08-26 18:27:26 -0500905 CallbackAllocator<char> string_allocator(instance->alloc);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500906
Michael Lentine03c64b02015-08-26 18:27:26 -0500907 String dir_name("/data/local/tmp/vulkan/", string_allocator);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500908 FindLayersInDirectory(*instance, dir_name);
Michael Lentine1c69b9e2015-09-14 13:26:59 -0500909 const std::string& path = LoaderData::GetInstance().layer_path;
910 dir_name.assign(path.c_str(), path.size());
911 dir_name.append("/");
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500912 FindLayersInDirectory(*instance, dir_name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700913
Jesse Hall9a16f972015-10-28 15:59:53 -0700914 result = ActivateAllLayers(create_info, instance, instance);
915 if (result != VK_SUCCESS) {
916 DestroyInstanceBottom(instance);
917 return result;
918 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500919
920 void* base_object = static_cast<void*>(instance);
921 void* next_object = base_object;
922 VkLayerLinkedListElem* next_element;
923 PFN_vkGetInstanceProcAddr next_get_proc_addr =
924 kBottomInstanceFunctions.GetInstanceProcAddr;
925 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700926 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500927 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
928
929 for (size_t i = elem_list.size(); i > 0; i--) {
930 size_t idx = i - 1;
931 next_element = &elem_list[idx];
932 next_element->get_proc_addr =
933 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
934 next_element->base_object = base_object;
935 next_element->next_element = next_object;
936 next_object = static_cast<void*>(next_element);
937
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500938 auto& name = instance->active_layers[idx]->first;
939 auto& handle = instance->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500940 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500941 dlsym(handle, (name + "GetInstanceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500942 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500943 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500944 dlsym(handle, "vkGetInstanceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700945 if (!next_get_proc_addr) {
946 ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500947 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700948 next_object = next_element->next_element;
949 next_get_proc_addr =
950 reinterpret_cast<PFN_vkGetInstanceProcAddr>(
951 next_element->get_proc_addr);
952 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500953 }
954 }
955
956 if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object),
957 static_cast<VkInstance>(next_object),
958 next_get_proc_addr, instance->vtbl_storage)) {
959 DestroyInstanceBottom(instance);
960 return VK_ERROR_INITIALIZATION_FAILED;
961 }
962
Michael Lentine950bb4f2015-09-14 13:26:30 -0500963 // Force enable callback extension if required
964 bool enable_callback =
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500965 property_get_bool("debug.vulkan.enable_callback", false);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500966 bool enable_logging = enable_callback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500967 const char* extension_name = "DEBUG_REPORT";
Michael Lentine950bb4f2015-09-14 13:26:30 -0500968 if (enable_callback) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500969 enable_callback = AddExtensionToCreateInfo(
970 local_create_info, extension_name, instance->alloc);
Michael Lentine950bb4f2015-09-14 13:26:30 -0500971 }
972
Jesse Hall04f4f472015-08-16 19:51:04 -0700973 *out_instance = instance;
Michael Lentine03c64b02015-08-26 18:27:26 -0500974 result = instance->vtbl_storage.CreateInstance(create_info, out_instance);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500975 if (enable_callback)
976 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -0700977 if (result <= 0) {
978 // For every layer, including the loader top and bottom layers:
979 // - If a call to the next CreateInstance fails, the layer must clean
980 // up anything it has successfully done so far, and propagate the
981 // error upwards.
982 // - If a layer successfully calls the next layer's CreateInstance, and
983 // afterwards must fail for some reason, it must call the next layer's
984 // DestroyInstance before returning.
985 // - The layer must not call the next layer's DestroyInstance if that
986 // layer's CreateInstance wasn't called, or returned failure.
987
988 // On failure, CreateInstanceBottom frees the instance struct, so it's
989 // already gone at this point. Nothing to do.
990 }
991
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500992 if (enable_logging) {
993 PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500994 DebugCreateMessageCallback =
995 reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
996 vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback"));
997 DebugCreateMessageCallback(
998 instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
999 LogDebugMessageCallback, NULL, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001000 }
1001
Jesse Hall04f4f472015-08-16 19:51:04 -07001002 return result;
1003}
1004
1005PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
1006 if (!instance)
1007 return GetGlobalInstanceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001008 // TODO: Possibly move this into the instance table
1009 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
1010 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
1011 if (!instance->vtbl)
1012 return NULL;
1013 PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr;
1014 return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name));
1015 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001016 // For special-case functions we always return the loader entry
1017 if (strcmp(name, "vkGetInstanceProcAddr") == 0 ||
1018 strcmp(name, "vkGetDeviceProcAddr") == 0) {
1019 return GetGlobalInstanceProcAddr(name);
1020 }
1021 return GetSpecificInstanceProcAddr(instance->vtbl, name);
1022}
1023
1024PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
1025 if (!device)
1026 return GetGlobalDeviceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001027 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
1028 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
1029 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001030 if (strcmp(name, "vkGetDeviceQueue") == 0) {
1031 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
1032 }
1033 if (strcmp(name, "vkCreateCommandBuffer") == 0) {
1034 return reinterpret_cast<PFN_vkVoidFunction>(CreateCommandBuffer);
1035 }
1036 if (strcmp(name, "vkDestroyDevice") == 0) {
1037 return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
1038 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001039 // For special-case functions we always return the loader entry
1040 if (strcmp(name, "vkGetDeviceQueue") == 0 ||
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001041 strcmp(name, "vkCreateCommandBuffer") == 0 ||
Jesse Hall04f4f472015-08-16 19:51:04 -07001042 strcmp(name, "vkDestroyDevice") == 0) {
1043 return GetGlobalDeviceProcAddr(name);
1044 }
1045 return GetSpecificDeviceProcAddr(GetVtbl(device), name);
1046}
1047
1048VkResult GetDeviceQueue(VkDevice drv_device,
1049 uint32_t family,
1050 uint32_t index,
1051 VkQueue* out_queue) {
1052 VkResult result;
1053 VkQueue queue;
1054 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1055 result = vtbl->GetDeviceQueue(drv_device, family, index, &queue);
1056 if (result != VK_SUCCESS)
1057 return result;
1058 hwvulkan_dispatch_t* dispatch =
1059 reinterpret_cast<hwvulkan_dispatch_t*>(queue);
Jesse Hall74f28dd2015-10-29 18:49:53 -07001060 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != vtbl) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001061 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic);
1062 return VK_ERROR_INITIALIZATION_FAILED;
1063 }
1064 dispatch->vtbl = vtbl;
1065 *out_queue = queue;
1066 return VK_SUCCESS;
1067}
1068
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001069VkResult CreateCommandBuffer(VkDevice drv_device,
1070 const VkCmdBufferCreateInfo* create_info,
1071 VkCmdBuffer* out_cmdbuf) {
1072 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1073 VkCmdBuffer cmdbuf;
1074 VkResult result =
1075 vtbl->CreateCommandBuffer(drv_device, create_info, &cmdbuf);
1076 if (result != VK_SUCCESS)
1077 return result;
1078 hwvulkan_dispatch_t* dispatch =
1079 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuf);
1080 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
1081 ALOGE("invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR,
1082 dispatch->magic);
1083 return VK_ERROR_INITIALIZATION_FAILED;
1084 }
1085 dispatch->vtbl = vtbl;
1086 *out_cmdbuf = cmdbuf;
1087 return VK_SUCCESS;
1088}
1089
Jesse Hall04f4f472015-08-16 19:51:04 -07001090VkResult DestroyDevice(VkDevice drv_device) {
1091 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1092 Device* device = static_cast<Device*>(vtbl->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001093 for (auto it = device->active_layers.begin();
1094 it != device->active_layers.end(); ++it) {
1095 DeactivateLayer(device, device->instance, it);
1096 }
1097 device->active_layers.clear();
Jesse Hall04f4f472015-08-16 19:51:04 -07001098 vtbl->DestroyDevice(drv_device);
1099 DestroyDevice(device);
1100 return VK_SUCCESS;
1101}
1102
Jesse Halld7b994a2015-09-07 14:17:37 -07001103void* AllocDeviceMem(VkDevice device,
1104 size_t size,
1105 size_t align,
1106 VkSystemAllocType type) {
1107 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001108 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001109 return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, type);
1110}
1111
1112void FreeDeviceMem(VkDevice device, void* ptr) {
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 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1116}
1117
1118const DeviceVtbl& GetDriverVtbl(VkDevice device) {
1119 // TODO(jessehall): This actually returns the API-level vtbl for the
1120 // device, not the driver entry points. Given the current use -- getting
1121 // the driver's private swapchain-related functions -- that works, but is
1122 // misleading and likely to cause bugs. Fix as part of separating the
1123 // loader->driver interface from the app->loader interface.
1124 return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage;
1125}
1126
1127const DeviceVtbl& GetDriverVtbl(VkQueue queue) {
1128 // TODO(jessehall): This actually returns the API-level vtbl for the
1129 // device, not the driver entry points. Given the current use -- getting
1130 // the driver's private swapchain-related functions -- that works, but is
1131 // misleading and likely to cause bugs. Fix as part of separating the
1132 // loader->driver interface from the app->loader interface.
1133 return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage;
1134}
1135
Jesse Hall04f4f472015-08-16 19:51:04 -07001136} // namespace vulkan