blob: 03265ef313689cdd8fbad8d281e9e27f42b7fb44 [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>
Jesse Hall04f4f472015-08-16 19:51:04 -070025#include <pthread.h>
Jesse Hall03b6fe12015-11-24 12:44:21 -080026#include <stdlib.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070027#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
Jesse Hall03b6fe12015-11-24 12:44:21 -080061// Standard-library allocator that delegates to VkAllocCallbacks.
62//
63// TODO(jessehall): This class currently always uses
64// VK_SYSTEM_ALLOC_SCOPE_INSTANCE. The scope to use could be a template
65// parameter or a constructor parameter. The former would help catch bugs
66// where we use the wrong scope, e.g. adding a command-scope string to an
67// instance-scope vector. But that might also be pretty annoying to deal with.
Michael Lentine03c64b02015-08-26 18:27:26 -050068template <class T>
69class CallbackAllocator {
70 public:
71 typedef T value_type;
72
73 CallbackAllocator(const VkAllocCallbacks* alloc_input)
74 : alloc(alloc_input) {}
75
76 template <class T2>
77 CallbackAllocator(const CallbackAllocator<T2>& other)
78 : alloc(other.alloc) {}
79
80 T* allocate(std::size_t n) {
81 void* mem = alloc->pfnAlloc(alloc->pUserData, n * sizeof(T), alignof(T),
Jesse Hall03b6fe12015-11-24 12:44:21 -080082 VK_SYSTEM_ALLOC_SCOPE_INSTANCE);
Michael Lentine03c64b02015-08-26 18:27:26 -050083 return static_cast<T*>(mem);
84 }
85
86 void deallocate(T* array, std::size_t /*n*/) {
87 alloc->pfnFree(alloc->pUserData, array);
88 }
89
90 const VkAllocCallbacks* alloc;
91};
92// These are needed in order to move Strings
93template <class T>
94bool operator==(const CallbackAllocator<T>& alloc1,
95 const CallbackAllocator<T>& alloc2) {
96 return alloc1.alloc == alloc2.alloc;
97}
98template <class T>
99bool operator!=(const CallbackAllocator<T>& alloc1,
100 const CallbackAllocator<T>& alloc2) {
101 return !(alloc1 == alloc2);
102}
103
104template <class Key,
105 class T,
106 class Hash = std::hash<Key>,
107 class Pred = std::equal_to<Key> >
108using UnorderedMap =
109 std::unordered_map<Key,
110 T,
111 Hash,
112 Pred,
113 CallbackAllocator<std::pair<const Key, T> > >;
114
115template <class T>
116using Vector = std::vector<T, CallbackAllocator<T> >;
117
118typedef std::basic_string<char,
119 std::char_traits<char>,
120 CallbackAllocator<char> > String;
121
122} // namespace
123
124// -----------------------------------------------------------------------------
125
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500126namespace {
127
128struct LayerData {
129 String path;
130 SharedLibraryHandle handle;
131 uint32_t ref_count;
132};
133
134typedef UnorderedMap<String, LayerData>::iterator LayerMapIterator;
135
136} // namespace
137
Jesse Hall04f4f472015-08-16 19:51:04 -0700138struct VkInstance_T {
139 VkInstance_T(const VkAllocCallbacks* alloc_callbacks)
Michael Lentine03c64b02015-08-26 18:27:26 -0500140 : vtbl(&vtbl_storage),
141 alloc(alloc_callbacks),
142 num_physical_devices(0),
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500143 layers(CallbackAllocator<std::pair<String, LayerData> >(alloc)),
144 active_layers(CallbackAllocator<String>(alloc)) {
145 pthread_mutex_init(&layer_lock, 0);
Jesse Hall04f4f472015-08-16 19:51:04 -0700146 memset(&vtbl_storage, 0, sizeof(vtbl_storage));
147 memset(physical_devices, 0, sizeof(physical_devices));
148 memset(&drv.vtbl, 0, sizeof(drv.vtbl));
149 drv.GetDeviceProcAddr = nullptr;
150 drv.num_physical_devices = 0;
151 }
152
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500153 ~VkInstance_T() { pthread_mutex_destroy(&layer_lock); }
154
Jesse Hall04f4f472015-08-16 19:51:04 -0700155 InstanceVtbl* vtbl;
156 InstanceVtbl vtbl_storage;
157
158 const VkAllocCallbacks* alloc;
159 uint32_t num_physical_devices;
160 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
161
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500162 pthread_mutex_t layer_lock;
163 // Map of layer names to layer data
164 UnorderedMap<String, LayerData> layers;
165 // Vector of layers active for this instance
166 Vector<LayerMapIterator> active_layers;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500167 VkDbgMsgCallback message;
Michael Lentine03c64b02015-08-26 18:27:26 -0500168
Jesse Hall04f4f472015-08-16 19:51:04 -0700169 struct Driver {
170 // Pointers to driver entry points. Used explicitly by the loader; not
171 // set as the dispatch table for any objects.
172 InstanceVtbl vtbl;
173
174 // Pointer to the driver's get_device_proc_addr, must be valid for any
175 // of the driver's physical devices. Not part of the InstanceVtbl since
176 // it's not an Instance/PhysicalDevice function.
177 PFN_vkGetDeviceProcAddr GetDeviceProcAddr;
178
179 // Number of physical devices owned by this driver.
180 uint32_t num_physical_devices;
181 } drv; // may eventually be an array
182};
183
184// -----------------------------------------------------------------------------
185
186namespace {
187
188typedef VkInstance_T Instance;
189
190struct Device {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500191 Device(Instance* instance_input)
192 : instance(instance_input),
193 active_layers(CallbackAllocator<LayerMapIterator>(instance->alloc)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700194 memset(&vtbl_storage, 0, sizeof(vtbl_storage));
195 vtbl_storage.device = this;
196 }
197 DeviceVtbl vtbl_storage;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500198 Instance* instance;
199 // Vector of layers active for this device
200 Vector<LayerMapIterator> active_layers;
Jesse Hall04f4f472015-08-16 19:51:04 -0700201};
202
203// -----------------------------------------------------------------------------
204// Utility Code
205
206inline const InstanceVtbl* GetVtbl(VkPhysicalDevice physicalDevice) {
207 return *reinterpret_cast<InstanceVtbl**>(physicalDevice);
208}
209
210inline const DeviceVtbl* GetVtbl(VkDevice device) {
211 return *reinterpret_cast<DeviceVtbl**>(device);
212}
Jesse Halld7b994a2015-09-07 14:17:37 -0700213inline const DeviceVtbl* GetVtbl(VkQueue queue) {
214 return *reinterpret_cast<DeviceVtbl**>(queue);
215}
Jesse Hall04f4f472015-08-16 19:51:04 -0700216
Jesse Hall03b6fe12015-11-24 12:44:21 -0800217void* DefaultAlloc(void*, size_t size, size_t alignment, VkSystemAllocScope) {
218 void* ptr = nullptr;
219 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
220 // additionally requires that it be at least sizeof(void*).
221 return posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0
222 ? ptr
223 : nullptr;
224}
225
226void* DefaultRealloc(void*,
227 void* ptr,
228 size_t size,
229 size_t alignment,
230 VkSystemAllocScope) {
231 if (size == 0) {
232 free(ptr);
233 return nullptr;
234 }
235
236 // TODO(jessehall): Right now we never shrink allocations; if the new
237 // request is smaller than the existing chunk, we just continue using it.
238 // Right now the loader never reallocs, so this doesn't matter. If that
239 // changes, or if this code is copied into some other project, this should
240 // probably have a heuristic to allocate-copy-free when doing so will save
241 // "enough" space.
242 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
243 if (size <= old_size)
244 return ptr;
245
246 void* new_ptr = nullptr;
247 if (posix_memalign(&new_ptr, alignment, size) != 0)
248 return nullptr;
249 if (ptr) {
250 memcpy(new_ptr, ptr, std::min(old_size, size));
251 free(ptr);
252 }
253 return new_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700254}
255
256void DefaultFree(void*, void* pMem) {
257 free(pMem);
258}
259
260const VkAllocCallbacks kDefaultAllocCallbacks = {
261 .pUserData = nullptr,
262 .pfnAlloc = DefaultAlloc,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800263 .pfnRealloc = DefaultRealloc,
Jesse Hall04f4f472015-08-16 19:51:04 -0700264 .pfnFree = DefaultFree,
265};
266
267hwvulkan_device_t* g_hwdevice;
268bool EnsureInitialized() {
269 static std::once_flag once_flag;
270 static const hwvulkan_module_t* module;
271
272 std::call_once(once_flag, []() {
273 int result;
274 result = hw_get_module("vulkan",
275 reinterpret_cast<const hw_module_t**>(&module));
276 if (result != 0) {
277 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result),
278 result);
279 return;
280 }
281 result = module->common.methods->open(
282 &module->common, HWVULKAN_DEVICE_0,
283 reinterpret_cast<hw_device_t**>(&g_hwdevice));
284 if (result != 0) {
285 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
286 result);
287 module = nullptr;
288 return;
289 }
290 });
291
292 return module != nullptr && g_hwdevice != nullptr;
293}
294
295void DestroyDevice(Device* device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500296 const VkAllocCallbacks* alloc = device->instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700297 device->~Device();
298 alloc->pfnFree(alloc->pUserData, device);
299}
300
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500301void FindLayersInDirectory(Instance& instance, const String& dir_name) {
Jesse Hall0ecdd3e2015-10-29 11:20:07 -0700302 DIR* directory = opendir(dir_name.c_str());
303 if (!directory) {
304 android_LogPriority log_priority =
305 (errno == ENOENT) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_ERROR;
306 LOG_PRI(log_priority, LOG_TAG,
307 "failed to open layer directory '%s': %s (%d)",
308 dir_name.c_str(), strerror(errno), errno);
309 return;
Michael Lentine03c64b02015-08-26 18:27:26 -0500310 }
Jesse Hall0ecdd3e2015-10-29 11:20:07 -0700311
312 Vector<VkLayerProperties> properties(
313 CallbackAllocator<VkLayerProperties>(instance.alloc));
314 struct dirent* entry;
315 while ((entry = readdir(directory))) {
316 size_t length = strlen(entry->d_name);
317 if (strncmp(entry->d_name, "libVKLayer", 10) != 0 ||
318 strncmp(entry->d_name + length - 3, ".so", 3) != 0)
319 continue;
320 // Open so
321 SharedLibraryHandle layer_handle =
322 dlopen((dir_name + entry->d_name).c_str(), RTLD_NOW | RTLD_LOCAL);
323 if (!layer_handle) {
324 ALOGE("%s failed to load with error %s; Skipping", entry->d_name,
325 dlerror());
326 continue;
327 }
328
329 // Get Layers in so
330 PFN_vkEnumerateInstanceLayerProperties get_layer_properties =
331 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
332 dlsym(layer_handle, "vkEnumerateInstanceLayerProperties"));
333 if (!get_layer_properties) {
334 ALOGE(
335 "%s failed to find vkEnumerateInstanceLayerProperties with "
336 "error %s; Skipping",
337 entry->d_name, dlerror());
338 dlclose(layer_handle);
339 continue;
340 }
341 uint32_t count;
342 get_layer_properties(&count, nullptr);
343
344 properties.resize(count);
345 get_layer_properties(&count, &properties[0]);
346
347 // Add Layers to potential list
348 for (uint32_t i = 0; i < count; ++i) {
349 String layer_name(properties[i].layerName,
350 CallbackAllocator<char>(instance.alloc));
351 LayerData layer_data = {dir_name + entry->d_name, 0, 0};
352 instance.layers.insert(std::make_pair(layer_name, layer_data));
353 ALOGV("Found layer %s", properties[i].layerName);
354 }
355 dlclose(layer_handle);
356 }
357
358 closedir(directory);
Michael Lentine03c64b02015-08-26 18:27:26 -0500359}
360
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500361template <class TObject>
362void ActivateLayer(TObject* object, Instance* instance, const String& name) {
363 // If object has layer, do nothing
364 auto element = instance->layers.find(name);
Michael Lentine233ac732015-11-18 18:28:07 -0800365 if (element == instance->layers.end()) {
366 return;
367 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500368 if (std::find(object->active_layers.begin(), object->active_layers.end(),
369 element) != object->active_layers.end()) {
370 ALOGW("Layer %s already activated; skipping", name.c_str());
371 return;
372 }
373 // If layer is not open, open it
374 LayerData& layer_data = element->second;
375 pthread_mutex_lock(&instance->layer_lock);
376 if (layer_data.ref_count == 0) {
377 SharedLibraryHandle layer_handle =
378 dlopen(layer_data.path.c_str(), RTLD_NOW | RTLD_LOCAL);
379 if (!layer_handle) {
380 pthread_mutex_unlock(&instance->layer_lock);
381 ALOGE("%s failed to load with error %s; Skipping",
382 layer_data.path.c_str(), dlerror());
383 return;
384 }
385 layer_data.handle = layer_handle;
386 }
387 layer_data.ref_count++;
388 pthread_mutex_unlock(&instance->layer_lock);
389 ALOGV("Activating layer %s", name.c_str());
390 object->active_layers.push_back(element);
391}
392
Michael Lentine1d1e65f2015-11-19 14:23:06 -0800393void DeactivateLayer(Instance* instance,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500394 Vector<LayerMapIterator>::iterator& element) {
395 LayerMapIterator& layer_map_data = *element;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500396 LayerData& layer_data = layer_map_data->second;
397 pthread_mutex_lock(&instance->layer_lock);
398 layer_data.ref_count--;
399 if (!layer_data.ref_count) {
400 dlclose(layer_data.handle);
401 }
402 pthread_mutex_unlock(&instance->layer_lock);
403}
404
Michael Lentine9da191b2015-10-13 11:08:45 -0500405struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500406 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500407 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500408};
409
Michael Lentine9da191b2015-10-13 11:08:45 -0500410void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500411 const char* value,
412 void* data) {
413 const char prefix[] = "debug.vulkan.layer.";
414 const size_t prefixlen = sizeof(prefix) - 1;
415 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
416 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500417 const char* number_str = name + prefixlen;
418 long layer_number = strtol(number_str, nullptr, 10);
419 if (layer_number <= 0 || layer_number == LONG_MAX) {
420 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
421 number_str);
422 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500423 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500424 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
425 Vector<String>* layer_names = instance_names_pair->layer_names;
426 Instance* instance = instance_names_pair->instance;
427 size_t layer_size = static_cast<size_t>(layer_number);
428 if (layer_size > layer_names->size()) {
429 layer_names->resize(layer_size,
430 String(CallbackAllocator<char>(instance->alloc)));
431 }
432 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500433}
434
435template <class TInfo, class TObject>
Jesse Hall9a16f972015-10-28 15:59:53 -0700436VkResult ActivateAllLayers(TInfo create_info, Instance* instance, TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500437 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
438 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
439 "Cannot activate layers for unknown object %p", object);
440 CallbackAllocator<char> string_allocator(instance->alloc);
441 // Load system layers
442 {
443 char layer_prop[PROPERTY_VALUE_MAX];
444 property_get("debug.vulkan.layers", layer_prop, "");
445 String layer_name(string_allocator);
446 String layer_prop_str(layer_prop, string_allocator);
447 size_t end, start = 0;
448 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
449 layer_name = layer_prop_str.substr(start, end - start);
Michael Lentine233ac732015-11-18 18:28:07 -0800450 ActivateLayer(object, instance, layer_name);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500451 start = end + 1;
452 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500453 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
454 InstanceNamesPair instance_names_pair = {.instance = instance,
455 .layer_names = &layer_names};
456 property_list(SetLayerNamesFromProperty,
457 static_cast<void*>(&instance_names_pair));
458 for (auto layer_name_element : layer_names) {
459 ActivateLayer(object, instance, layer_name_element);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500460 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500461 }
462 // Load app layers
Jesse Hall03b6fe12015-11-24 12:44:21 -0800463 for (uint32_t i = 0; i < create_info->enabledLayerNameCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500464 String layer_name(create_info->ppEnabledLayerNames[i],
465 string_allocator);
466 auto element = instance->layers.find(layer_name);
467 if (element == instance->layers.end()) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700468 ALOGE("requested %s layer '%s' not present",
469 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ?
470 "instance" : "device",
471 layer_name.c_str());
472 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500473 } else {
474 ActivateLayer(object, instance, layer_name);
475 }
476 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700477 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500478}
479
480template <class TCreateInfo>
481bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
482 const char* extension_name,
483 const VkAllocCallbacks* alloc) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800484 for (uint32_t i = 0; i < local_create_info.enabledExtensionNameCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500485 if (!strcmp(extension_name,
486 local_create_info.ppEnabledExtensionNames[i])) {
487 return false;
488 }
489 }
Jesse Hall03b6fe12015-11-24 12:44:21 -0800490 uint32_t extension_count = local_create_info.enabledExtensionNameCount;
491 local_create_info.enabledExtensionNameCount++;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500492 void* mem = alloc->pfnAlloc(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800493 alloc->pUserData,
494 local_create_info.enabledExtensionNameCount * sizeof(char*),
495 alignof(char*), VK_SYSTEM_ALLOC_SCOPE_INSTANCE);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500496 if (mem) {
497 const char** enabled_extensions = static_cast<const char**>(mem);
498 for (uint32_t i = 0; i < extension_count; ++i) {
499 enabled_extensions[i] =
500 local_create_info.ppEnabledExtensionNames[i];
501 }
502 enabled_extensions[extension_count] = extension_name;
503 local_create_info.ppEnabledExtensionNames = enabled_extensions;
504 } else {
505 ALOGW("%s extension cannot be enabled: memory allocation failed",
506 extension_name);
Jesse Hall03b6fe12015-11-24 12:44:21 -0800507 local_create_info.enabledExtensionNameCount--;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500508 return false;
509 }
510 return true;
511}
512
513template <class T>
514void FreeAllocatedCreateInfo(T& local_create_info,
515 const VkAllocCallbacks* alloc) {
516 alloc->pfnFree(
517 alloc->pUserData,
518 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
519}
520
Michael Lentineeb970862015-10-15 12:42:22 -0500521VkBool32 LogDebugMessageCallback(VkFlags message_flags,
522 VkDbgObjectType /*obj_type*/,
523 uint64_t /*src_object*/,
524 size_t /*location*/,
525 int32_t message_code,
526 const char* layer_prefix,
527 const char* message,
528 void* /*user_data*/) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500529 if (message_flags & VK_DBG_REPORT_ERROR_BIT) {
530 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
531 } else if (message_flags & VK_DBG_REPORT_WARN_BIT) {
532 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
533 }
Michael Lentineeb970862015-10-15 12:42:22 -0500534 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500535}
536
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800537VkResult Noop(...) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500538 return VK_SUCCESS;
539}
540
541PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) {
542 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
543 return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr);
544 }
545 if (strcmp(name, "vkCreateDevice") == 0) {
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800546 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
Michael Lentine03c64b02015-08-26 18:27:26 -0500547 }
Michael Lentine88594d72015-11-12 12:49:45 -0800548 // WSI extensions are not in the driver so return the loader functions
Michael Lentine88594d72015-11-12 12:49:45 -0800549 if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
550 return reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR);
551 }
552 if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
553 return reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR);
554 }
555 if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
556 return reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR);
557 }
558 if (strcmp(name, "vkAcquireNextImageKHR") == 0) {
559 return reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR);
560 }
561 if (strcmp(name, "vkQueuePresentKHR") == 0) {
562 return reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR);
563 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500564 if (!device)
565 return GetGlobalDeviceProcAddr(name);
566 Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500567 return loader_device->instance->drv.GetDeviceProcAddr(device, name);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500568}
569
Jesse Hall04f4f472015-08-16 19:51:04 -0700570// -----------------------------------------------------------------------------
571// "Bottom" functions. These are called at the end of the instance dispatch
572// chain.
573
Jesse Hall03b6fe12015-11-24 12:44:21 -0800574void DestroyInstanceBottom(VkInstance instance,
575 const VkAllocCallbacks* allocator) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700576 // These checks allow us to call DestroyInstanceBottom from any error path
577 // in CreateInstanceBottom, before the driver instance is fully initialized.
578 if (instance->drv.vtbl.instance != VK_NULL_HANDLE &&
579 instance->drv.vtbl.DestroyInstance) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800580 instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance,
581 allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700582 }
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500583 if (instance->message) {
584 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500585 DebugDestroyMessageCallback =
586 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
587 vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback"));
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500588 DebugDestroyMessageCallback(instance, instance->message);
589 }
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800590 for (auto it = instance->active_layers.begin();
591 it != instance->active_layers.end(); ++it) {
Michael Lentine1d1e65f2015-11-19 14:23:06 -0800592 DeactivateLayer(instance, it);
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800593 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700594 const VkAllocCallbacks* alloc = instance->alloc;
595 instance->~VkInstance_T();
596 alloc->pfnFree(alloc->pUserData, instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700597}
598
599VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800600 const VkAllocCallbacks* allocator,
Jesse Hall04f4f472015-08-16 19:51:04 -0700601 VkInstance* instance_ptr) {
602 Instance* instance = *instance_ptr;
603 VkResult result;
604
Jesse Hall03b6fe12015-11-24 12:44:21 -0800605 result = g_hwdevice->CreateInstance(create_info, instance->alloc,
606 &instance->drv.vtbl.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700607 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800608 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700609 return result;
610 }
611
Michael Lentine03c64b02015-08-26 18:27:26 -0500612 if (!LoadInstanceVtbl(
613 instance->drv.vtbl.instance, instance->drv.vtbl.instance,
614 g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800615 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700616 return VK_ERROR_INITIALIZATION_FAILED;
617 }
618
619 // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be
620 // queryable from the Instance, and that the resulting function work for any
621 // VkDevice created from the instance.
622 instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
623 g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance,
624 "vkGetDeviceProcAddr"));
625 if (!instance->drv.GetDeviceProcAddr) {
626 ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr");
Jesse Hall03b6fe12015-11-24 12:44:21 -0800627 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700628 return VK_ERROR_INITIALIZATION_FAILED;
629 }
630
631 hwvulkan_dispatch_t* dispatch =
632 reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance);
633 if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
634 // Skip setting dispatch->vtbl on the driver instance handle, since we
635 // never intentionally call through it; we go through Instance::drv.vtbl
636 // instead.
637 } else {
638 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
639 dispatch->magic);
Jesse Hall03b6fe12015-11-24 12:44:21 -0800640 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700641 return VK_ERROR_INITIALIZATION_FAILED;
642 }
643
644 uint32_t num_physical_devices = 0;
645 result = instance->drv.vtbl.EnumeratePhysicalDevices(
646 instance->drv.vtbl.instance, &num_physical_devices, nullptr);
647 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800648 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700649 return VK_ERROR_INITIALIZATION_FAILED;
650 }
651 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
652 result = instance->drv.vtbl.EnumeratePhysicalDevices(
653 instance->drv.vtbl.instance, &num_physical_devices,
654 instance->physical_devices);
655 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800656 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700657 return VK_ERROR_INITIALIZATION_FAILED;
658 }
659 for (uint32_t i = 0; i < num_physical_devices; i++) {
660 dispatch = reinterpret_cast<hwvulkan_dispatch_t*>(
661 instance->physical_devices[i]);
662 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
663 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
664 dispatch->magic);
Jesse Hall03b6fe12015-11-24 12:44:21 -0800665 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700666 return VK_ERROR_INITIALIZATION_FAILED;
667 }
668 dispatch->vtbl = instance->vtbl;
669 }
670 instance->drv.num_physical_devices = num_physical_devices;
671
672 instance->num_physical_devices = instance->drv.num_physical_devices;
673 return VK_SUCCESS;
674}
675
676VkResult EnumeratePhysicalDevicesBottom(VkInstance instance,
677 uint32_t* pdev_count,
678 VkPhysicalDevice* pdevs) {
679 uint32_t count = instance->num_physical_devices;
680 if (pdevs) {
681 count = std::min(count, *pdev_count);
682 std::copy(instance->physical_devices,
683 instance->physical_devices + count, pdevs);
684 }
685 *pdev_count = count;
686 return VK_SUCCESS;
687}
688
Jesse Hall606a54e2015-11-19 22:17:28 -0800689void GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev,
690 VkPhysicalDeviceFeatures* features) {
691 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features);
Jesse Hall04f4f472015-08-16 19:51:04 -0700692}
693
Jesse Hall606a54e2015-11-19 22:17:28 -0800694void GetPhysicalDeviceFormatPropertiesBottom(VkPhysicalDevice pdev,
695 VkFormat format,
696 VkFormatProperties* properties) {
697 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700698 pdev, format, properties);
699}
700
Jesse Hall606a54e2015-11-19 22:17:28 -0800701void GetPhysicalDeviceImageFormatPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700702 VkPhysicalDevice pdev,
703 VkFormat format,
704 VkImageType type,
705 VkImageTiling tiling,
706 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700707 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700708 VkImageFormatProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800709 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties(
710 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700711}
712
Jesse Hall606a54e2015-11-19 22:17:28 -0800713void GetPhysicalDevicePropertiesBottom(VkPhysicalDevice pdev,
714 VkPhysicalDeviceProperties* properties) {
715 GetVtbl(pdev)
Jesse Hall04f4f472015-08-16 19:51:04 -0700716 ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties);
717}
718
Jesse Hall606a54e2015-11-19 22:17:28 -0800719void GetPhysicalDeviceQueueFamilyPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700720 VkPhysicalDevice pdev,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700721 uint32_t* pCount,
722 VkQueueFamilyProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800723 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties(
724 pdev, pCount, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700725}
726
Jesse Hall606a54e2015-11-19 22:17:28 -0800727void GetPhysicalDeviceMemoryPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700728 VkPhysicalDevice pdev,
729 VkPhysicalDeviceMemoryProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800730 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700731 pdev, properties);
732}
733
734VkResult CreateDeviceBottom(VkPhysicalDevice pdev,
735 const VkDeviceCreateInfo* create_info,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800736 const VkAllocCallbacks* allocator,
Jesse Hall04f4f472015-08-16 19:51:04 -0700737 VkDevice* out_device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500738 Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700739 VkResult result;
740
Jesse Hall03b6fe12015-11-24 12:44:21 -0800741 if (!allocator) {
742 if (instance.alloc)
743 allocator = instance.alloc;
744 else
745 allocator = &kDefaultAllocCallbacks;
746 }
747
748 void* mem =
749 allocator->pfnAlloc(allocator->pUserData, sizeof(Device),
750 alignof(Device), VK_SYSTEM_ALLOC_SCOPE_DEVICE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700751 if (!mem)
752 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500753 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700754
Jesse Hall9a16f972015-10-28 15:59:53 -0700755 result = ActivateAllLayers(create_info, &instance, device);
756 if (result != VK_SUCCESS) {
757 DestroyDevice(device);
758 return result;
759 }
760
Jesse Hall04f4f472015-08-16 19:51:04 -0700761 VkDevice drv_device;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800762 result = instance.drv.vtbl.CreateDevice(pdev, create_info, allocator,
763 &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700764 if (result != VK_SUCCESS) {
765 DestroyDevice(device);
766 return result;
767 }
768
Jesse Hall04f4f472015-08-16 19:51:04 -0700769 hwvulkan_dispatch_t* dispatch =
770 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
771 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
772 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500773 PFN_vkDestroyDevice destroy_device =
774 reinterpret_cast<PFN_vkDestroyDevice>(
775 instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800776 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700777 DestroyDevice(device);
778 return VK_ERROR_INITIALIZATION_FAILED;
779 }
780 dispatch->vtbl = &device->vtbl_storage;
781
Michael Lentine03c64b02015-08-26 18:27:26 -0500782 void* base_object = static_cast<void*>(drv_device);
783 void* next_object = base_object;
784 VkLayerLinkedListElem* next_element;
785 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr;
786 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500787 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500788 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
789
790 for (size_t i = elem_list.size(); i > 0; i--) {
791 size_t idx = i - 1;
792 next_element = &elem_list[idx];
793 next_element->get_proc_addr =
794 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
795 next_element->base_object = base_object;
796 next_element->next_element = next_object;
797 next_object = static_cast<void*>(next_element);
798
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500799 auto& name = device->active_layers[idx]->first;
800 auto& handle = device->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500801 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500802 dlsym(handle, (name + "GetDeviceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500803 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500804 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500805 dlsym(handle, "vkGetDeviceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700806 if (!next_get_proc_addr) {
807 ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500808 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700809 next_object = next_element->next_element;
810 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
811 next_element->get_proc_addr);
812 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500813 }
814 }
815
816 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
817 static_cast<VkDevice>(next_object), next_get_proc_addr,
818 device->vtbl_storage)) {
819 DestroyDevice(device);
820 return VK_ERROR_INITIALIZATION_FAILED;
821 }
822
823 PFN_vkCreateDevice layer_createDevice =
824 reinterpret_cast<PFN_vkCreateDevice>(
825 device->vtbl_storage.GetDeviceProcAddr(drv_device,
826 "vkCreateDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800827 layer_createDevice(pdev, create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700828
Michael Lentine88594d72015-11-12 12:49:45 -0800829 // TODO(mlentine) : This is needed to use WSI layer validation. Remove this
830 // when new version of layer initialization exits.
831 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
832 static_cast<VkDevice>(next_object), next_get_proc_addr,
833 device->vtbl_storage)) {
834 DestroyDevice(device);
835 return VK_ERROR_INITIALIZATION_FAILED;
836 }
837
Jesse Hall04f4f472015-08-16 19:51:04 -0700838 *out_device = drv_device;
839 return VK_SUCCESS;
840}
841
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700842VkResult EnumerateDeviceExtensionPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700843 VkPhysicalDevice pdev,
844 const char* layer_name,
845 uint32_t* properties_count,
846 VkExtensionProperties* properties) {
847 // TODO: what are we supposed to do with layer_name here?
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700848 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties(
849 pdev, layer_name, properties_count, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700850}
851
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700852VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev,
853 uint32_t* properties_count,
854 VkLayerProperties* properties) {
855 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700856 pdev, properties_count, properties);
857}
858
Jesse Hall606a54e2015-11-19 22:17:28 -0800859void GetPhysicalDeviceSparseImageFormatPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700860 VkPhysicalDevice pdev,
861 VkFormat format,
862 VkImageType type,
863 uint32_t samples,
864 VkImageUsageFlags usage,
865 VkImageTiling tiling,
866 uint32_t* properties_count,
867 VkSparseImageFormatProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800868 GetVtbl(pdev)
Jesse Hall04f4f472015-08-16 19:51:04 -0700869 ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties(
870 pdev, format, type, samples, usage, tiling, properties_count,
871 properties);
872}
873
874PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*);
875
876const InstanceVtbl kBottomInstanceFunctions = {
877 // clang-format off
878 .instance = nullptr,
879 .CreateInstance = CreateInstanceBottom,
880 .DestroyInstance = DestroyInstanceBottom,
881 .GetInstanceProcAddr = GetInstanceProcAddrBottom,
882 .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom,
883 .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom,
884 .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom,
885 .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700886 .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700887 .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700888 .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom,
889 .CreateDevice = CreateDeviceBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700890 .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom,
891 .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700892 .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700893 .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR,
Jesse Hallb00daad2015-11-29 19:46:20 -0800894 .GetPhysicalDeviceSurfaceCapabilitiesKHR = GetPhysicalDeviceSurfaceCapabilitiesKHR,
895 .GetPhysicalDeviceSurfaceFormatsKHR = GetPhysicalDeviceSurfaceFormatsKHR,
896 .GetPhysicalDeviceSurfacePresentModesKHR = GetPhysicalDeviceSurfacePresentModesKHR,
Jesse Hall04f4f472015-08-16 19:51:04 -0700897 // clang-format on
898};
899
900PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500901 // TODO: Possibly move this into the instance table
902 // TODO: Possibly register the callbacks in the loader
903 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
904 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
905 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
906 }
907 if (strcmp(name, "vkCreateInstance") == 0) {
908 return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
909 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700910 return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
911}
912
913} // namespace
914
915// -----------------------------------------------------------------------------
916// Global functions. These are called directly from the loader entry points,
917// without going through a dispatch table.
918
919namespace vulkan {
920
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700921VkResult EnumerateInstanceExtensionProperties(
922 const char* /*layer_name*/,
923 uint32_t* count,
924 VkExtensionProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700925 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700926 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700927
928 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700929 ALOGW("vkEnumerateInstanceExtensionProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700930
931 *count = 0;
932 return VK_SUCCESS;
933}
934
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700935VkResult EnumerateInstanceLayerProperties(uint32_t* count,
936 VkLayerProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700937 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700938 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700939
940 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700941 ALOGW("vkEnumerateInstanceLayerProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700942
943 *count = 0;
944 return VK_SUCCESS;
945}
946
947VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800948 const VkAllocCallbacks* allocator,
Jesse Hall04f4f472015-08-16 19:51:04 -0700949 VkInstance* out_instance) {
950 VkResult result;
951
952 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700953 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700954
Jesse Hall03b6fe12015-11-24 12:44:21 -0800955 if (!allocator)
956 allocator = &kDefaultAllocCallbacks;
957
Jesse Hall04f4f472015-08-16 19:51:04 -0700958 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -0700959 create_info = &local_create_info;
960
Jesse Hall03b6fe12015-11-24 12:44:21 -0800961 void* instance_mem =
962 allocator->pfnAlloc(allocator->pUserData, sizeof(Instance),
963 alignof(Instance), VK_SYSTEM_ALLOC_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700964 if (!instance_mem)
965 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800966 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700967
968 instance->vtbl_storage = kBottomInstanceFunctions;
969 instance->vtbl_storage.instance = instance;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500970 instance->message = VK_NULL_HANDLE;
Jesse Hall04f4f472015-08-16 19:51:04 -0700971
Michael Lentine03c64b02015-08-26 18:27:26 -0500972 // Scan layers
Michael Lentine03c64b02015-08-26 18:27:26 -0500973 CallbackAllocator<char> string_allocator(instance->alloc);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500974
Michael Lentine03c64b02015-08-26 18:27:26 -0500975 String dir_name("/data/local/tmp/vulkan/", string_allocator);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500976 FindLayersInDirectory(*instance, dir_name);
Michael Lentine1c69b9e2015-09-14 13:26:59 -0500977 const std::string& path = LoaderData::GetInstance().layer_path;
978 dir_name.assign(path.c_str(), path.size());
979 dir_name.append("/");
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500980 FindLayersInDirectory(*instance, dir_name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700981
Jesse Hall9a16f972015-10-28 15:59:53 -0700982 result = ActivateAllLayers(create_info, instance, instance);
983 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800984 DestroyInstanceBottom(instance, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -0700985 return result;
986 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500987
988 void* base_object = static_cast<void*>(instance);
989 void* next_object = base_object;
990 VkLayerLinkedListElem* next_element;
991 PFN_vkGetInstanceProcAddr next_get_proc_addr =
992 kBottomInstanceFunctions.GetInstanceProcAddr;
993 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700994 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500995 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
996
997 for (size_t i = elem_list.size(); i > 0; i--) {
998 size_t idx = i - 1;
999 next_element = &elem_list[idx];
1000 next_element->get_proc_addr =
1001 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
1002 next_element->base_object = base_object;
1003 next_element->next_element = next_object;
1004 next_object = static_cast<void*>(next_element);
1005
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001006 auto& name = instance->active_layers[idx]->first;
1007 auto& handle = instance->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -05001008 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001009 dlsym(handle, (name + "GetInstanceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -05001010 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -05001011 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001012 dlsym(handle, "vkGetInstanceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -07001013 if (!next_get_proc_addr) {
1014 ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001015 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -07001016 next_object = next_element->next_element;
1017 next_get_proc_addr =
1018 reinterpret_cast<PFN_vkGetInstanceProcAddr>(
1019 next_element->get_proc_addr);
1020 }
Michael Lentine03c64b02015-08-26 18:27:26 -05001021 }
1022 }
1023
1024 if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object),
1025 static_cast<VkInstance>(next_object),
1026 next_get_proc_addr, instance->vtbl_storage)) {
Jesse Hall03b6fe12015-11-24 12:44:21 -08001027 DestroyInstanceBottom(instance, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -05001028 return VK_ERROR_INITIALIZATION_FAILED;
1029 }
1030
Michael Lentine950bb4f2015-09-14 13:26:30 -05001031 // Force enable callback extension if required
1032 bool enable_callback =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001033 property_get_bool("debug.vulkan.enable_callback", false);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001034 bool enable_logging = enable_callback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001035 const char* extension_name = "DEBUG_REPORT";
Michael Lentine950bb4f2015-09-14 13:26:30 -05001036 if (enable_callback) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001037 enable_callback = AddExtensionToCreateInfo(
1038 local_create_info, extension_name, instance->alloc);
Michael Lentine950bb4f2015-09-14 13:26:30 -05001039 }
1040
Jesse Hall04f4f472015-08-16 19:51:04 -07001041 *out_instance = instance;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001042 result = instance->vtbl_storage.CreateInstance(create_info, allocator,
1043 out_instance);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001044 if (enable_callback)
1045 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -07001046 if (result <= 0) {
1047 // For every layer, including the loader top and bottom layers:
1048 // - If a call to the next CreateInstance fails, the layer must clean
1049 // up anything it has successfully done so far, and propagate the
1050 // error upwards.
1051 // - If a layer successfully calls the next layer's CreateInstance, and
1052 // afterwards must fail for some reason, it must call the next layer's
1053 // DestroyInstance before returning.
1054 // - The layer must not call the next layer's DestroyInstance if that
1055 // layer's CreateInstance wasn't called, or returned failure.
1056
1057 // On failure, CreateInstanceBottom frees the instance struct, so it's
1058 // already gone at this point. Nothing to do.
1059 }
1060
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001061 if (enable_logging) {
1062 PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001063 DebugCreateMessageCallback =
1064 reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
1065 vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback"));
1066 DebugCreateMessageCallback(
1067 instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1068 LogDebugMessageCallback, NULL, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001069 }
1070
Jesse Hall04f4f472015-08-16 19:51:04 -07001071 return result;
1072}
1073
1074PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
1075 if (!instance)
1076 return GetGlobalInstanceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001077 // TODO: Possibly move this into the instance table
1078 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
1079 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
1080 if (!instance->vtbl)
1081 return NULL;
1082 PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr;
1083 return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name));
1084 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001085 // For special-case functions we always return the loader entry
1086 if (strcmp(name, "vkGetInstanceProcAddr") == 0 ||
1087 strcmp(name, "vkGetDeviceProcAddr") == 0) {
1088 return GetGlobalInstanceProcAddr(name);
1089 }
1090 return GetSpecificInstanceProcAddr(instance->vtbl, name);
1091}
1092
1093PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
1094 if (!device)
1095 return GetGlobalDeviceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001096 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
1097 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
1098 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001099 if (strcmp(name, "vkGetDeviceQueue") == 0) {
1100 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
1101 }
Jesse Hallfbf97b02015-11-20 14:17:03 -08001102 if (strcmp(name, "vkAllocCommandBuffers") == 0) {
1103 return reinterpret_cast<PFN_vkVoidFunction>(AllocCommandBuffers);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001104 }
1105 if (strcmp(name, "vkDestroyDevice") == 0) {
1106 return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
1107 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001108 return GetSpecificDeviceProcAddr(GetVtbl(device), name);
1109}
1110
Jesse Hall606a54e2015-11-19 22:17:28 -08001111void GetDeviceQueue(VkDevice drv_device,
1112 uint32_t family,
1113 uint32_t index,
1114 VkQueue* out_queue) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001115 VkResult result;
1116 VkQueue queue;
1117 const DeviceVtbl* vtbl = GetVtbl(drv_device);
Jesse Hall606a54e2015-11-19 22:17:28 -08001118 vtbl->GetDeviceQueue(drv_device, family, index, &queue);
Jesse Hall04f4f472015-08-16 19:51:04 -07001119 hwvulkan_dispatch_t* dispatch =
1120 reinterpret_cast<hwvulkan_dispatch_t*>(queue);
Jesse Hall606a54e2015-11-19 22:17:28 -08001121 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != vtbl)
Jesse Hall04f4f472015-08-16 19:51:04 -07001122 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Jesse Hall04f4f472015-08-16 19:51:04 -07001123 dispatch->vtbl = vtbl;
1124 *out_queue = queue;
Jesse Hall04f4f472015-08-16 19:51:04 -07001125}
1126
Jesse Hallfbf97b02015-11-20 14:17:03 -08001127VkResult AllocCommandBuffers(VkDevice device,
1128 const VkCmdBufferAllocInfo* alloc_info,
1129 VkCmdBuffer* cmdbuffers) {
1130 const DeviceVtbl* vtbl = GetVtbl(device);
1131 VkResult result = vtbl->AllocCommandBuffers(device, alloc_info, cmdbuffers);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001132 if (result != VK_SUCCESS)
1133 return result;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001134 for (uint32_t i = 0; i < alloc_info->bufferCount; i++) {
Jesse Hallfbf97b02015-11-20 14:17:03 -08001135 hwvulkan_dispatch_t* dispatch =
1136 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuffers[i]);
1137 ALOGE_IF(dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
1138 "invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR,
1139 dispatch->magic);
1140 dispatch->vtbl = vtbl;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001141 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001142 return VK_SUCCESS;
1143}
1144
Jesse Hall03b6fe12015-11-24 12:44:21 -08001145VkResult DestroyDevice(VkDevice drv_device,
1146 const VkAllocCallbacks* /*allocator*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001147 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1148 Device* device = static_cast<Device*>(vtbl->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001149 for (auto it = device->active_layers.begin();
1150 it != device->active_layers.end(); ++it) {
Michael Lentine1d1e65f2015-11-19 14:23:06 -08001151 DeactivateLayer(device->instance, it);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001152 }
Jesse Hall03b6fe12015-11-24 12:44:21 -08001153 vtbl->DestroyDevice(drv_device, device->instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -07001154 DestroyDevice(device);
1155 return VK_SUCCESS;
1156}
1157
Jesse Hall1356b0d2015-11-23 17:24:58 -08001158void* AllocMem(VkInstance instance,
1159 size_t size,
1160 size_t align,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001161 VkSystemAllocScope scope) {
Jesse Hall1356b0d2015-11-23 17:24:58 -08001162 const VkAllocCallbacks* alloc_cb = instance->alloc;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001163 return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, scope);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001164}
1165
1166void FreeMem(VkInstance instance, void* ptr) {
1167 const VkAllocCallbacks* alloc_cb = instance->alloc;
1168 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1169}
1170
1171void* AllocMem(VkDevice device,
1172 size_t size,
1173 size_t align,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001174 VkSystemAllocScope scope) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001175 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001176 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001177 return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, scope);
Jesse Halld7b994a2015-09-07 14:17:37 -07001178}
1179
Jesse Hall1356b0d2015-11-23 17:24:58 -08001180void FreeMem(VkDevice device, void* ptr) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001181 const VkAllocCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001182 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001183 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1184}
1185
1186const DeviceVtbl& GetDriverVtbl(VkDevice device) {
1187 // TODO(jessehall): This actually returns the API-level vtbl for the
1188 // device, not the driver entry points. Given the current use -- getting
1189 // the driver's private swapchain-related functions -- that works, but is
1190 // misleading and likely to cause bugs. Fix as part of separating the
1191 // loader->driver interface from the app->loader interface.
1192 return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage;
1193}
1194
1195const DeviceVtbl& GetDriverVtbl(VkQueue queue) {
1196 // TODO(jessehall): This actually returns the API-level vtbl for the
1197 // device, not the driver entry points. Given the current use -- getting
1198 // the driver's private swapchain-related functions -- that works, but is
1199 // misleading and likely to cause bugs. Fix as part of separating the
1200 // loader->driver interface from the app->loader interface.
1201 return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage;
1202}
1203
Jesse Hall04f4f472015-08-16 19:51:04 -07001204} // namespace vulkan