blob: 6683196f2174e3c933485a4aaa79f49565c49106 [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>
389void ActivateAllLayers(TInfo create_info, Instance* instance, TObject* object) {
390 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()) {
424 ALOGW("Cannot activate layer %s as it was not found.",
425 layer_name.c_str());
426 } else {
427 ActivateLayer(object, instance, layer_name);
428 }
429 }
430}
431
432template <class TCreateInfo>
433bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
434 const char* extension_name,
435 const VkAllocCallbacks* alloc) {
436 for (uint32_t i = 0; i < local_create_info.extensionCount; ++i) {
437 if (!strcmp(extension_name,
438 local_create_info.ppEnabledExtensionNames[i])) {
439 return false;
440 }
441 }
442 uint32_t extension_count = local_create_info.extensionCount;
443 local_create_info.extensionCount++;
444 void* mem = alloc->pfnAlloc(
445 alloc->pUserData, local_create_info.extensionCount * sizeof(char*),
446 alignof(char*), VK_SYSTEM_ALLOC_TYPE_INTERNAL);
447 if (mem) {
448 const char** enabled_extensions = static_cast<const char**>(mem);
449 for (uint32_t i = 0; i < extension_count; ++i) {
450 enabled_extensions[i] =
451 local_create_info.ppEnabledExtensionNames[i];
452 }
453 enabled_extensions[extension_count] = extension_name;
454 local_create_info.ppEnabledExtensionNames = enabled_extensions;
455 } else {
456 ALOGW("%s extension cannot be enabled: memory allocation failed",
457 extension_name);
458 local_create_info.extensionCount--;
459 return false;
460 }
461 return true;
462}
463
464template <class T>
465void FreeAllocatedCreateInfo(T& local_create_info,
466 const VkAllocCallbacks* alloc) {
467 alloc->pfnFree(
468 alloc->pUserData,
469 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
470}
471
Michael Lentineeb970862015-10-15 12:42:22 -0500472VkBool32 LogDebugMessageCallback(VkFlags message_flags,
473 VkDbgObjectType /*obj_type*/,
474 uint64_t /*src_object*/,
475 size_t /*location*/,
476 int32_t message_code,
477 const char* layer_prefix,
478 const char* message,
479 void* /*user_data*/) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500480 if (message_flags & VK_DBG_REPORT_ERROR_BIT) {
481 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
482 } else if (message_flags & VK_DBG_REPORT_WARN_BIT) {
483 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
484 }
Michael Lentineeb970862015-10-15 12:42:22 -0500485 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500486}
487
488VkResult CreateDeviceNoop(VkPhysicalDevice,
489 const VkDeviceCreateInfo*,
490 VkDevice*) {
491 return VK_SUCCESS;
492}
493
494PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) {
495 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
496 return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr);
497 }
498 if (strcmp(name, "vkCreateDevice") == 0) {
499 return reinterpret_cast<PFN_vkVoidFunction>(CreateDeviceNoop);
500 }
501 if (!device)
502 return GetGlobalDeviceProcAddr(name);
503 Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500504 return loader_device->instance->drv.GetDeviceProcAddr(device, name);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500505}
506
Jesse Hall04f4f472015-08-16 19:51:04 -0700507// -----------------------------------------------------------------------------
508// "Bottom" functions. These are called at the end of the instance dispatch
509// chain.
510
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700511void DestroyInstanceBottom(VkInstance instance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700512 // These checks allow us to call DestroyInstanceBottom from any error path
513 // in CreateInstanceBottom, before the driver instance is fully initialized.
514 if (instance->drv.vtbl.instance != VK_NULL_HANDLE &&
515 instance->drv.vtbl.DestroyInstance) {
516 instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance);
517 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500518 for (auto it = instance->active_layers.begin();
519 it != instance->active_layers.end(); ++it) {
520 DeactivateLayer(instance, instance, it);
Michael Lentine03c64b02015-08-26 18:27:26 -0500521 }
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500522 if (instance->message) {
523 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500524 DebugDestroyMessageCallback =
525 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
526 vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback"));
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500527 DebugDestroyMessageCallback(instance, instance->message);
528 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700529 const VkAllocCallbacks* alloc = instance->alloc;
530 instance->~VkInstance_T();
531 alloc->pfnFree(alloc->pUserData, instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700532}
533
534VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info,
535 VkInstance* instance_ptr) {
536 Instance* instance = *instance_ptr;
537 VkResult result;
538
539 result =
540 g_hwdevice->CreateInstance(create_info, &instance->drv.vtbl.instance);
541 if (result != VK_SUCCESS) {
542 DestroyInstanceBottom(instance);
543 return result;
544 }
545
Michael Lentine03c64b02015-08-26 18:27:26 -0500546 if (!LoadInstanceVtbl(
547 instance->drv.vtbl.instance, instance->drv.vtbl.instance,
548 g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700549 DestroyInstanceBottom(instance);
550 return VK_ERROR_INITIALIZATION_FAILED;
551 }
552
553 // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be
554 // queryable from the Instance, and that the resulting function work for any
555 // VkDevice created from the instance.
556 instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
557 g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance,
558 "vkGetDeviceProcAddr"));
559 if (!instance->drv.GetDeviceProcAddr) {
560 ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr");
561 DestroyInstanceBottom(instance);
562 return VK_ERROR_INITIALIZATION_FAILED;
563 }
564
565 hwvulkan_dispatch_t* dispatch =
566 reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance);
567 if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
568 // Skip setting dispatch->vtbl on the driver instance handle, since we
569 // never intentionally call through it; we go through Instance::drv.vtbl
570 // instead.
571 } else {
572 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
573 dispatch->magic);
574 DestroyInstanceBottom(instance);
575 return VK_ERROR_INITIALIZATION_FAILED;
576 }
577
578 uint32_t num_physical_devices = 0;
579 result = instance->drv.vtbl.EnumeratePhysicalDevices(
580 instance->drv.vtbl.instance, &num_physical_devices, nullptr);
581 if (result != VK_SUCCESS) {
582 DestroyInstanceBottom(instance);
583 return VK_ERROR_INITIALIZATION_FAILED;
584 }
585 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
586 result = instance->drv.vtbl.EnumeratePhysicalDevices(
587 instance->drv.vtbl.instance, &num_physical_devices,
588 instance->physical_devices);
589 if (result != VK_SUCCESS) {
590 DestroyInstanceBottom(instance);
591 return VK_ERROR_INITIALIZATION_FAILED;
592 }
593 for (uint32_t i = 0; i < num_physical_devices; i++) {
594 dispatch = reinterpret_cast<hwvulkan_dispatch_t*>(
595 instance->physical_devices[i]);
596 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
597 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
598 dispatch->magic);
599 DestroyInstanceBottom(instance);
600 return VK_ERROR_INITIALIZATION_FAILED;
601 }
602 dispatch->vtbl = instance->vtbl;
603 }
604 instance->drv.num_physical_devices = num_physical_devices;
605
606 instance->num_physical_devices = instance->drv.num_physical_devices;
607 return VK_SUCCESS;
608}
609
610VkResult EnumeratePhysicalDevicesBottom(VkInstance instance,
611 uint32_t* pdev_count,
612 VkPhysicalDevice* pdevs) {
613 uint32_t count = instance->num_physical_devices;
614 if (pdevs) {
615 count = std::min(count, *pdev_count);
616 std::copy(instance->physical_devices,
617 instance->physical_devices + count, pdevs);
618 }
619 *pdev_count = count;
620 return VK_SUCCESS;
621}
622
623VkResult GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev,
624 VkPhysicalDeviceFeatures* features) {
625 return GetVtbl(pdev)
626 ->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features);
627}
628
629VkResult GetPhysicalDeviceFormatPropertiesBottom(
630 VkPhysicalDevice pdev,
631 VkFormat format,
632 VkFormatProperties* properties) {
633 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties(
634 pdev, format, properties);
635}
636
637VkResult GetPhysicalDeviceImageFormatPropertiesBottom(
638 VkPhysicalDevice pdev,
639 VkFormat format,
640 VkImageType type,
641 VkImageTiling tiling,
642 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700643 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700644 VkImageFormatProperties* properties) {
645 return GetVtbl(pdev)
646 ->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties(
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700647 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700648}
649
650VkResult GetPhysicalDevicePropertiesBottom(
651 VkPhysicalDevice pdev,
652 VkPhysicalDeviceProperties* properties) {
653 return GetVtbl(pdev)
654 ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties);
655}
656
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700657VkResult GetPhysicalDeviceQueueFamilyPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700658 VkPhysicalDevice pdev,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700659 uint32_t* pCount,
660 VkQueueFamilyProperties* properties) {
661 return GetVtbl(pdev)
662 ->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties(
663 pdev, pCount, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700664}
665
666VkResult GetPhysicalDeviceMemoryPropertiesBottom(
667 VkPhysicalDevice pdev,
668 VkPhysicalDeviceMemoryProperties* properties) {
669 return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties(
670 pdev, properties);
671}
672
673VkResult CreateDeviceBottom(VkPhysicalDevice pdev,
674 const VkDeviceCreateInfo* create_info,
675 VkDevice* out_device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500676 Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700677 VkResult result;
678
679 void* mem = instance.alloc->pfnAlloc(instance.alloc->pUserData,
680 sizeof(Device), alignof(Device),
681 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
682 if (!mem)
683 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500684 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700685
686 VkDevice drv_device;
687 result = instance.drv.vtbl.CreateDevice(pdev, create_info, &drv_device);
688 if (result != VK_SUCCESS) {
689 DestroyDevice(device);
690 return result;
691 }
692
Jesse Hall04f4f472015-08-16 19:51:04 -0700693 hwvulkan_dispatch_t* dispatch =
694 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
695 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
696 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500697 PFN_vkDestroyDevice destroy_device =
698 reinterpret_cast<PFN_vkDestroyDevice>(
699 instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice"));
700 destroy_device(drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700701 DestroyDevice(device);
702 return VK_ERROR_INITIALIZATION_FAILED;
703 }
704 dispatch->vtbl = &device->vtbl_storage;
705
Jesse Hallb1352bc2015-09-04 16:12:33 -0700706 device->vtbl_storage.GetSurfacePropertiesKHR = GetSurfacePropertiesKHR;
707 device->vtbl_storage.GetSurfaceFormatsKHR = GetSurfaceFormatsKHR;
708 device->vtbl_storage.GetSurfacePresentModesKHR = GetSurfacePresentModesKHR;
709 device->vtbl_storage.CreateSwapchainKHR = CreateSwapchainKHR;
710 device->vtbl_storage.DestroySwapchainKHR = DestroySwapchainKHR;
711 device->vtbl_storage.GetSwapchainImagesKHR = GetSwapchainImagesKHR;
712 device->vtbl_storage.AcquireNextImageKHR = AcquireNextImageKHR;
713 device->vtbl_storage.QueuePresentKHR = QueuePresentKHR;
714
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500715 ActivateAllLayers(create_info, &instance, device);
716
Michael Lentine03c64b02015-08-26 18:27:26 -0500717 void* base_object = static_cast<void*>(drv_device);
718 void* next_object = base_object;
719 VkLayerLinkedListElem* next_element;
720 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr;
721 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500722 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500723 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
724
725 for (size_t i = elem_list.size(); i > 0; i--) {
726 size_t idx = i - 1;
727 next_element = &elem_list[idx];
728 next_element->get_proc_addr =
729 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
730 next_element->base_object = base_object;
731 next_element->next_element = next_object;
732 next_object = static_cast<void*>(next_element);
733
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500734 auto& name = device->active_layers[idx]->first;
735 auto& handle = device->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500736 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500737 dlsym(handle, (name + "GetDeviceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500738 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500739 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500740 dlsym(handle, "vkGetDeviceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700741 if (!next_get_proc_addr) {
742 ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500743 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700744 next_object = next_element->next_element;
745 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
746 next_element->get_proc_addr);
747 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500748 }
749 }
750
751 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
752 static_cast<VkDevice>(next_object), next_get_proc_addr,
753 device->vtbl_storage)) {
754 DestroyDevice(device);
755 return VK_ERROR_INITIALIZATION_FAILED;
756 }
757
758 PFN_vkCreateDevice layer_createDevice =
759 reinterpret_cast<PFN_vkCreateDevice>(
760 device->vtbl_storage.GetDeviceProcAddr(drv_device,
761 "vkCreateDevice"));
762 layer_createDevice(pdev, create_info, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700763
764 *out_device = drv_device;
765 return VK_SUCCESS;
766}
767
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700768VkResult EnumerateDeviceExtensionPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700769 VkPhysicalDevice pdev,
770 const char* layer_name,
771 uint32_t* properties_count,
772 VkExtensionProperties* properties) {
773 // TODO: what are we supposed to do with layer_name here?
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700774 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties(
775 pdev, layer_name, properties_count, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700776}
777
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700778VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev,
779 uint32_t* properties_count,
780 VkLayerProperties* properties) {
781 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700782 pdev, properties_count, properties);
783}
784
785VkResult GetPhysicalDeviceSparseImageFormatPropertiesBottom(
786 VkPhysicalDevice pdev,
787 VkFormat format,
788 VkImageType type,
789 uint32_t samples,
790 VkImageUsageFlags usage,
791 VkImageTiling tiling,
792 uint32_t* properties_count,
793 VkSparseImageFormatProperties* properties) {
794 return GetVtbl(pdev)
795 ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties(
796 pdev, format, type, samples, usage, tiling, properties_count,
797 properties);
798}
799
800PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*);
801
802const InstanceVtbl kBottomInstanceFunctions = {
803 // clang-format off
804 .instance = nullptr,
805 .CreateInstance = CreateInstanceBottom,
806 .DestroyInstance = DestroyInstanceBottom,
807 .GetInstanceProcAddr = GetInstanceProcAddrBottom,
808 .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom,
809 .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom,
810 .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom,
811 .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700812 .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700813 .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700814 .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom,
815 .CreateDevice = CreateDeviceBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700816 .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom,
817 .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700818 .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700819 .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR,
Jesse Hall04f4f472015-08-16 19:51:04 -0700820 // clang-format on
821};
822
Michael Lentine03c64b02015-08-26 18:27:26 -0500823VkResult Noop(...) {
824 return VK_SUCCESS;
825}
826
Jesse Hall04f4f472015-08-16 19:51:04 -0700827PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500828 // TODO: Possibly move this into the instance table
829 // TODO: Possibly register the callbacks in the loader
830 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
831 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
832 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
833 }
834 if (strcmp(name, "vkCreateInstance") == 0) {
835 return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
836 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700837 return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
838}
839
840} // namespace
841
842// -----------------------------------------------------------------------------
843// Global functions. These are called directly from the loader entry points,
844// without going through a dispatch table.
845
846namespace vulkan {
847
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700848VkResult EnumerateInstanceExtensionProperties(
849 const char* /*layer_name*/,
850 uint32_t* count,
851 VkExtensionProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700852 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700853 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700854
855 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700856 ALOGW("vkEnumerateInstanceExtensionProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700857
858 *count = 0;
859 return VK_SUCCESS;
860}
861
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700862VkResult EnumerateInstanceLayerProperties(uint32_t* count,
863 VkLayerProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700864 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700865 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700866
867 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700868 ALOGW("vkEnumerateInstanceLayerProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700869
870 *count = 0;
871 return VK_SUCCESS;
872}
873
874VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
875 VkInstance* out_instance) {
876 VkResult result;
877
878 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700879 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700880
881 VkInstanceCreateInfo local_create_info = *create_info;
882 if (!local_create_info.pAllocCb)
883 local_create_info.pAllocCb = &kDefaultAllocCallbacks;
884 create_info = &local_create_info;
885
886 void* instance_mem = create_info->pAllocCb->pfnAlloc(
887 create_info->pAllocCb->pUserData, sizeof(Instance), alignof(Instance),
888 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
889 if (!instance_mem)
890 return VK_ERROR_OUT_OF_HOST_MEMORY;
891 Instance* instance = new (instance_mem) Instance(create_info->pAllocCb);
892
893 instance->vtbl_storage = kBottomInstanceFunctions;
894 instance->vtbl_storage.instance = instance;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500895 instance->message = VK_NULL_HANDLE;
Jesse Hall04f4f472015-08-16 19:51:04 -0700896
Michael Lentine03c64b02015-08-26 18:27:26 -0500897 // Scan layers
Michael Lentine03c64b02015-08-26 18:27:26 -0500898 CallbackAllocator<char> string_allocator(instance->alloc);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500899
Michael Lentine03c64b02015-08-26 18:27:26 -0500900 String dir_name("/data/local/tmp/vulkan/", string_allocator);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500901 FindLayersInDirectory(*instance, dir_name);
Michael Lentine1c69b9e2015-09-14 13:26:59 -0500902 const std::string& path = LoaderData::GetInstance().layer_path;
903 dir_name.assign(path.c_str(), path.size());
904 dir_name.append("/");
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500905 FindLayersInDirectory(*instance, dir_name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700906
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500907 ActivateAllLayers(create_info, instance, instance);
Michael Lentine03c64b02015-08-26 18:27:26 -0500908
909 void* base_object = static_cast<void*>(instance);
910 void* next_object = base_object;
911 VkLayerLinkedListElem* next_element;
912 PFN_vkGetInstanceProcAddr next_get_proc_addr =
913 kBottomInstanceFunctions.GetInstanceProcAddr;
914 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700915 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500916 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
917
918 for (size_t i = elem_list.size(); i > 0; i--) {
919 size_t idx = i - 1;
920 next_element = &elem_list[idx];
921 next_element->get_proc_addr =
922 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
923 next_element->base_object = base_object;
924 next_element->next_element = next_object;
925 next_object = static_cast<void*>(next_element);
926
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500927 auto& name = instance->active_layers[idx]->first;
928 auto& handle = instance->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500929 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500930 dlsym(handle, (name + "GetInstanceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500931 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500932 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500933 dlsym(handle, "vkGetInstanceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700934 if (!next_get_proc_addr) {
935 ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500936 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700937 next_object = next_element->next_element;
938 next_get_proc_addr =
939 reinterpret_cast<PFN_vkGetInstanceProcAddr>(
940 next_element->get_proc_addr);
941 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500942 }
943 }
944
945 if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object),
946 static_cast<VkInstance>(next_object),
947 next_get_proc_addr, instance->vtbl_storage)) {
948 DestroyInstanceBottom(instance);
949 return VK_ERROR_INITIALIZATION_FAILED;
950 }
951
Michael Lentine950bb4f2015-09-14 13:26:30 -0500952 // Force enable callback extension if required
953 bool enable_callback =
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500954 property_get_bool("debug.vulkan.enable_callback", false);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500955 bool enable_logging = enable_callback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500956 const char* extension_name = "DEBUG_REPORT";
Michael Lentine950bb4f2015-09-14 13:26:30 -0500957 if (enable_callback) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500958 enable_callback = AddExtensionToCreateInfo(
959 local_create_info, extension_name, instance->alloc);
Michael Lentine950bb4f2015-09-14 13:26:30 -0500960 }
961
Jesse Hall04f4f472015-08-16 19:51:04 -0700962 *out_instance = instance;
Michael Lentine03c64b02015-08-26 18:27:26 -0500963 result = instance->vtbl_storage.CreateInstance(create_info, out_instance);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500964 if (enable_callback)
965 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -0700966 if (result <= 0) {
967 // For every layer, including the loader top and bottom layers:
968 // - If a call to the next CreateInstance fails, the layer must clean
969 // up anything it has successfully done so far, and propagate the
970 // error upwards.
971 // - If a layer successfully calls the next layer's CreateInstance, and
972 // afterwards must fail for some reason, it must call the next layer's
973 // DestroyInstance before returning.
974 // - The layer must not call the next layer's DestroyInstance if that
975 // layer's CreateInstance wasn't called, or returned failure.
976
977 // On failure, CreateInstanceBottom frees the instance struct, so it's
978 // already gone at this point. Nothing to do.
979 }
980
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500981 if (enable_logging) {
982 PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500983 DebugCreateMessageCallback =
984 reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
985 vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback"));
986 DebugCreateMessageCallback(
987 instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
988 LogDebugMessageCallback, NULL, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500989 }
990
Jesse Hall04f4f472015-08-16 19:51:04 -0700991 return result;
992}
993
994PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
995 if (!instance)
996 return GetGlobalInstanceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -0500997 // TODO: Possibly move this into the instance table
998 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
999 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
1000 if (!instance->vtbl)
1001 return NULL;
1002 PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr;
1003 return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name));
1004 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001005 // For special-case functions we always return the loader entry
1006 if (strcmp(name, "vkGetInstanceProcAddr") == 0 ||
1007 strcmp(name, "vkGetDeviceProcAddr") == 0) {
1008 return GetGlobalInstanceProcAddr(name);
1009 }
1010 return GetSpecificInstanceProcAddr(instance->vtbl, name);
1011}
1012
1013PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
1014 if (!device)
1015 return GetGlobalDeviceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001016 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
1017 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
1018 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001019 if (strcmp(name, "vkGetDeviceQueue") == 0) {
1020 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
1021 }
1022 if (strcmp(name, "vkCreateCommandBuffer") == 0) {
1023 return reinterpret_cast<PFN_vkVoidFunction>(CreateCommandBuffer);
1024 }
1025 if (strcmp(name, "vkDestroyDevice") == 0) {
1026 return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
1027 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001028 // For special-case functions we always return the loader entry
1029 if (strcmp(name, "vkGetDeviceQueue") == 0 ||
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001030 strcmp(name, "vkCreateCommandBuffer") == 0 ||
Jesse Hall04f4f472015-08-16 19:51:04 -07001031 strcmp(name, "vkDestroyDevice") == 0) {
1032 return GetGlobalDeviceProcAddr(name);
1033 }
1034 return GetSpecificDeviceProcAddr(GetVtbl(device), name);
1035}
1036
1037VkResult GetDeviceQueue(VkDevice drv_device,
1038 uint32_t family,
1039 uint32_t index,
1040 VkQueue* out_queue) {
1041 VkResult result;
1042 VkQueue queue;
1043 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1044 result = vtbl->GetDeviceQueue(drv_device, family, index, &queue);
1045 if (result != VK_SUCCESS)
1046 return result;
1047 hwvulkan_dispatch_t* dispatch =
1048 reinterpret_cast<hwvulkan_dispatch_t*>(queue);
1049 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != &vtbl) {
1050 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic);
1051 return VK_ERROR_INITIALIZATION_FAILED;
1052 }
1053 dispatch->vtbl = vtbl;
1054 *out_queue = queue;
1055 return VK_SUCCESS;
1056}
1057
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001058VkResult CreateCommandBuffer(VkDevice drv_device,
1059 const VkCmdBufferCreateInfo* create_info,
1060 VkCmdBuffer* out_cmdbuf) {
1061 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1062 VkCmdBuffer cmdbuf;
1063 VkResult result =
1064 vtbl->CreateCommandBuffer(drv_device, create_info, &cmdbuf);
1065 if (result != VK_SUCCESS)
1066 return result;
1067 hwvulkan_dispatch_t* dispatch =
1068 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuf);
1069 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
1070 ALOGE("invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR,
1071 dispatch->magic);
1072 return VK_ERROR_INITIALIZATION_FAILED;
1073 }
1074 dispatch->vtbl = vtbl;
1075 *out_cmdbuf = cmdbuf;
1076 return VK_SUCCESS;
1077}
1078
Jesse Hall04f4f472015-08-16 19:51:04 -07001079VkResult DestroyDevice(VkDevice drv_device) {
1080 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1081 Device* device = static_cast<Device*>(vtbl->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001082 for (auto it = device->active_layers.begin();
1083 it != device->active_layers.end(); ++it) {
1084 DeactivateLayer(device, device->instance, it);
1085 }
1086 device->active_layers.clear();
Jesse Hall04f4f472015-08-16 19:51:04 -07001087 vtbl->DestroyDevice(drv_device);
1088 DestroyDevice(device);
1089 return VK_SUCCESS;
1090}
1091
Jesse Halld7b994a2015-09-07 14:17:37 -07001092void* AllocDeviceMem(VkDevice device,
1093 size_t size,
1094 size_t align,
1095 VkSystemAllocType type) {
1096 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001097 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001098 return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, type);
1099}
1100
1101void FreeDeviceMem(VkDevice device, void* ptr) {
1102 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001103 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001104 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1105}
1106
1107const DeviceVtbl& GetDriverVtbl(VkDevice device) {
1108 // TODO(jessehall): This actually returns the API-level vtbl for the
1109 // device, not the driver entry points. Given the current use -- getting
1110 // the driver's private swapchain-related functions -- that works, but is
1111 // misleading and likely to cause bugs. Fix as part of separating the
1112 // loader->driver interface from the app->loader interface.
1113 return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage;
1114}
1115
1116const DeviceVtbl& GetDriverVtbl(VkQueue queue) {
1117 // TODO(jessehall): This actually returns the API-level vtbl for the
1118 // device, not the driver entry points. Given the current use -- getting
1119 // the driver's private swapchain-related functions -- that works, but is
1120 // misleading and likely to cause bugs. Fix as part of separating the
1121 // loader->driver interface from the app->loader interface.
1122 return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage;
1123}
1124
Jesse Hall04f4f472015-08-16 19:51:04 -07001125} // namespace vulkan