blob: d1b369e75ca1e902ab98aa64e03cc4bb9d588b93 [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 Hall3fbc8562015-11-29 22:10:52 -080061// Standard-library allocator that delegates to VkAllocationCallbacks.
Jesse Hall03b6fe12015-11-24 12:44:21 -080062//
63// TODO(jessehall): This class currently always uses
Jesse Hall3fbc8562015-11-29 22:10:52 -080064// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
Jesse Hall03b6fe12015-11-24 12:44:21 -080065// 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
Jesse Hall3fbc8562015-11-29 22:10:52 -080073 CallbackAllocator(const VkAllocationCallbacks* alloc_input)
Michael Lentine03c64b02015-08-26 18:27:26 -050074 : 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) {
Jesse Hall3fbc8562015-11-29 22:10:52 -080081 void* mem =
82 alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
83 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine03c64b02015-08-26 18:27:26 -050084 return static_cast<T*>(mem);
85 }
86
87 void deallocate(T* array, std::size_t /*n*/) {
88 alloc->pfnFree(alloc->pUserData, array);
89 }
90
Jesse Hall3fbc8562015-11-29 22:10:52 -080091 const VkAllocationCallbacks* alloc;
Michael Lentine03c64b02015-08-26 18:27:26 -050092};
93// These are needed in order to move Strings
94template <class T>
95bool operator==(const CallbackAllocator<T>& alloc1,
96 const CallbackAllocator<T>& alloc2) {
97 return alloc1.alloc == alloc2.alloc;
98}
99template <class T>
100bool operator!=(const CallbackAllocator<T>& alloc1,
101 const CallbackAllocator<T>& alloc2) {
102 return !(alloc1 == alloc2);
103}
104
105template <class Key,
106 class T,
107 class Hash = std::hash<Key>,
108 class Pred = std::equal_to<Key> >
109using UnorderedMap =
110 std::unordered_map<Key,
111 T,
112 Hash,
113 Pred,
114 CallbackAllocator<std::pair<const Key, T> > >;
115
116template <class T>
117using Vector = std::vector<T, CallbackAllocator<T> >;
118
119typedef std::basic_string<char,
120 std::char_traits<char>,
121 CallbackAllocator<char> > String;
122
123} // namespace
124
125// -----------------------------------------------------------------------------
126
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500127namespace {
128
129struct LayerData {
130 String path;
131 SharedLibraryHandle handle;
132 uint32_t ref_count;
133};
134
135typedef UnorderedMap<String, LayerData>::iterator LayerMapIterator;
136
137} // namespace
138
Jesse Hall04f4f472015-08-16 19:51:04 -0700139struct VkInstance_T {
Jesse Hall3fbc8562015-11-29 22:10:52 -0800140 VkInstance_T(const VkAllocationCallbacks* alloc_callbacks)
Michael Lentine03c64b02015-08-26 18:27:26 -0500141 : vtbl(&vtbl_storage),
142 alloc(alloc_callbacks),
143 num_physical_devices(0),
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500144 layers(CallbackAllocator<std::pair<String, LayerData> >(alloc)),
145 active_layers(CallbackAllocator<String>(alloc)) {
146 pthread_mutex_init(&layer_lock, 0);
Jesse Hall04f4f472015-08-16 19:51:04 -0700147 memset(&vtbl_storage, 0, sizeof(vtbl_storage));
148 memset(physical_devices, 0, sizeof(physical_devices));
149 memset(&drv.vtbl, 0, sizeof(drv.vtbl));
150 drv.GetDeviceProcAddr = nullptr;
151 drv.num_physical_devices = 0;
152 }
153
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500154 ~VkInstance_T() { pthread_mutex_destroy(&layer_lock); }
155
Jesse Hall04f4f472015-08-16 19:51:04 -0700156 InstanceVtbl* vtbl;
157 InstanceVtbl vtbl_storage;
158
Jesse Hall3fbc8562015-11-29 22:10:52 -0800159 const VkAllocationCallbacks* alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700160 uint32_t num_physical_devices;
161 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
162
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500163 pthread_mutex_t layer_lock;
164 // Map of layer names to layer data
165 UnorderedMap<String, LayerData> layers;
166 // Vector of layers active for this instance
167 Vector<LayerMapIterator> active_layers;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500168 VkDbgMsgCallback message;
Michael Lentine03c64b02015-08-26 18:27:26 -0500169
Jesse Hall04f4f472015-08-16 19:51:04 -0700170 struct Driver {
171 // Pointers to driver entry points. Used explicitly by the loader; not
172 // set as the dispatch table for any objects.
173 InstanceVtbl vtbl;
174
175 // Pointer to the driver's get_device_proc_addr, must be valid for any
176 // of the driver's physical devices. Not part of the InstanceVtbl since
177 // it's not an Instance/PhysicalDevice function.
178 PFN_vkGetDeviceProcAddr GetDeviceProcAddr;
179
180 // Number of physical devices owned by this driver.
181 uint32_t num_physical_devices;
182 } drv; // may eventually be an array
183};
184
185// -----------------------------------------------------------------------------
186
187namespace {
188
189typedef VkInstance_T Instance;
190
191struct Device {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500192 Device(Instance* instance_input)
193 : instance(instance_input),
194 active_layers(CallbackAllocator<LayerMapIterator>(instance->alloc)) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700195 memset(&vtbl_storage, 0, sizeof(vtbl_storage));
196 vtbl_storage.device = this;
197 }
198 DeviceVtbl vtbl_storage;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500199 Instance* instance;
200 // Vector of layers active for this device
201 Vector<LayerMapIterator> active_layers;
Jesse Hall04f4f472015-08-16 19:51:04 -0700202};
203
204// -----------------------------------------------------------------------------
205// Utility Code
206
207inline const InstanceVtbl* GetVtbl(VkPhysicalDevice physicalDevice) {
208 return *reinterpret_cast<InstanceVtbl**>(physicalDevice);
209}
210
211inline const DeviceVtbl* GetVtbl(VkDevice device) {
212 return *reinterpret_cast<DeviceVtbl**>(device);
213}
Jesse Halld7b994a2015-09-07 14:17:37 -0700214inline const DeviceVtbl* GetVtbl(VkQueue queue) {
215 return *reinterpret_cast<DeviceVtbl**>(queue);
216}
Jesse Hall04f4f472015-08-16 19:51:04 -0700217
Jesse Hall3fbc8562015-11-29 22:10:52 -0800218void* DefaultAllocate(void*,
219 size_t size,
220 size_t alignment,
221 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800222 void* ptr = nullptr;
223 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
224 // additionally requires that it be at least sizeof(void*).
225 return posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0
226 ? ptr
227 : nullptr;
228}
229
Jesse Hall3fbc8562015-11-29 22:10:52 -0800230void* DefaultReallocate(void*,
231 void* ptr,
232 size_t size,
233 size_t alignment,
234 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800235 if (size == 0) {
236 free(ptr);
237 return nullptr;
238 }
239
240 // TODO(jessehall): Right now we never shrink allocations; if the new
241 // request is smaller than the existing chunk, we just continue using it.
242 // Right now the loader never reallocs, so this doesn't matter. If that
243 // changes, or if this code is copied into some other project, this should
244 // probably have a heuristic to allocate-copy-free when doing so will save
245 // "enough" space.
246 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
247 if (size <= old_size)
248 return ptr;
249
250 void* new_ptr = nullptr;
251 if (posix_memalign(&new_ptr, alignment, size) != 0)
252 return nullptr;
253 if (ptr) {
254 memcpy(new_ptr, ptr, std::min(old_size, size));
255 free(ptr);
256 }
257 return new_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700258}
259
260void DefaultFree(void*, void* pMem) {
261 free(pMem);
262}
263
Jesse Hall3fbc8562015-11-29 22:10:52 -0800264const VkAllocationCallbacks kDefaultAllocCallbacks = {
Jesse Hall04f4f472015-08-16 19:51:04 -0700265 .pUserData = nullptr,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800266 .pfnAllocation = DefaultAllocate,
267 .pfnReallocation = DefaultReallocate,
Jesse Hall04f4f472015-08-16 19:51:04 -0700268 .pfnFree = DefaultFree,
269};
270
271hwvulkan_device_t* g_hwdevice;
272bool EnsureInitialized() {
273 static std::once_flag once_flag;
274 static const hwvulkan_module_t* module;
275
276 std::call_once(once_flag, []() {
277 int result;
278 result = hw_get_module("vulkan",
279 reinterpret_cast<const hw_module_t**>(&module));
280 if (result != 0) {
281 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result),
282 result);
283 return;
284 }
285 result = module->common.methods->open(
286 &module->common, HWVULKAN_DEVICE_0,
287 reinterpret_cast<hw_device_t**>(&g_hwdevice));
288 if (result != 0) {
289 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
290 result);
291 module = nullptr;
292 return;
293 }
294 });
295
296 return module != nullptr && g_hwdevice != nullptr;
297}
298
299void DestroyDevice(Device* device) {
Jesse Hall3fbc8562015-11-29 22:10:52 -0800300 const VkAllocationCallbacks* alloc = device->instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700301 device->~Device();
302 alloc->pfnFree(alloc->pUserData, device);
303}
304
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500305void FindLayersInDirectory(Instance& instance, const String& dir_name) {
Jesse Hall0ecdd3e2015-10-29 11:20:07 -0700306 DIR* directory = opendir(dir_name.c_str());
307 if (!directory) {
308 android_LogPriority log_priority =
309 (errno == ENOENT) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_ERROR;
310 LOG_PRI(log_priority, LOG_TAG,
311 "failed to open layer directory '%s': %s (%d)",
312 dir_name.c_str(), strerror(errno), errno);
313 return;
Michael Lentine03c64b02015-08-26 18:27:26 -0500314 }
Jesse Hall0ecdd3e2015-10-29 11:20:07 -0700315
316 Vector<VkLayerProperties> properties(
317 CallbackAllocator<VkLayerProperties>(instance.alloc));
318 struct dirent* entry;
319 while ((entry = readdir(directory))) {
320 size_t length = strlen(entry->d_name);
321 if (strncmp(entry->d_name, "libVKLayer", 10) != 0 ||
322 strncmp(entry->d_name + length - 3, ".so", 3) != 0)
323 continue;
324 // Open so
325 SharedLibraryHandle layer_handle =
326 dlopen((dir_name + entry->d_name).c_str(), RTLD_NOW | RTLD_LOCAL);
327 if (!layer_handle) {
328 ALOGE("%s failed to load with error %s; Skipping", entry->d_name,
329 dlerror());
330 continue;
331 }
332
333 // Get Layers in so
334 PFN_vkEnumerateInstanceLayerProperties get_layer_properties =
335 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
336 dlsym(layer_handle, "vkEnumerateInstanceLayerProperties"));
337 if (!get_layer_properties) {
338 ALOGE(
339 "%s failed to find vkEnumerateInstanceLayerProperties with "
340 "error %s; Skipping",
341 entry->d_name, dlerror());
342 dlclose(layer_handle);
343 continue;
344 }
345 uint32_t count;
346 get_layer_properties(&count, nullptr);
347
348 properties.resize(count);
349 get_layer_properties(&count, &properties[0]);
350
351 // Add Layers to potential list
352 for (uint32_t i = 0; i < count; ++i) {
353 String layer_name(properties[i].layerName,
354 CallbackAllocator<char>(instance.alloc));
355 LayerData layer_data = {dir_name + entry->d_name, 0, 0};
356 instance.layers.insert(std::make_pair(layer_name, layer_data));
357 ALOGV("Found layer %s", properties[i].layerName);
358 }
359 dlclose(layer_handle);
360 }
361
362 closedir(directory);
Michael Lentine03c64b02015-08-26 18:27:26 -0500363}
364
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500365template <class TObject>
366void ActivateLayer(TObject* object, Instance* instance, const String& name) {
367 // If object has layer, do nothing
368 auto element = instance->layers.find(name);
Michael Lentine233ac732015-11-18 18:28:07 -0800369 if (element == instance->layers.end()) {
370 return;
371 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500372 if (std::find(object->active_layers.begin(), object->active_layers.end(),
373 element) != object->active_layers.end()) {
374 ALOGW("Layer %s already activated; skipping", name.c_str());
375 return;
376 }
377 // If layer is not open, open it
378 LayerData& layer_data = element->second;
379 pthread_mutex_lock(&instance->layer_lock);
380 if (layer_data.ref_count == 0) {
381 SharedLibraryHandle layer_handle =
382 dlopen(layer_data.path.c_str(), RTLD_NOW | RTLD_LOCAL);
383 if (!layer_handle) {
384 pthread_mutex_unlock(&instance->layer_lock);
385 ALOGE("%s failed to load with error %s; Skipping",
386 layer_data.path.c_str(), dlerror());
387 return;
388 }
389 layer_data.handle = layer_handle;
390 }
391 layer_data.ref_count++;
392 pthread_mutex_unlock(&instance->layer_lock);
393 ALOGV("Activating layer %s", name.c_str());
394 object->active_layers.push_back(element);
395}
396
Michael Lentine1d1e65f2015-11-19 14:23:06 -0800397void DeactivateLayer(Instance* instance,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500398 Vector<LayerMapIterator>::iterator& element) {
399 LayerMapIterator& layer_map_data = *element;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500400 LayerData& layer_data = layer_map_data->second;
401 pthread_mutex_lock(&instance->layer_lock);
402 layer_data.ref_count--;
403 if (!layer_data.ref_count) {
404 dlclose(layer_data.handle);
405 }
406 pthread_mutex_unlock(&instance->layer_lock);
407}
408
Michael Lentine9da191b2015-10-13 11:08:45 -0500409struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500410 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500411 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500412};
413
Michael Lentine9da191b2015-10-13 11:08:45 -0500414void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500415 const char* value,
416 void* data) {
417 const char prefix[] = "debug.vulkan.layer.";
418 const size_t prefixlen = sizeof(prefix) - 1;
419 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
420 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500421 const char* number_str = name + prefixlen;
422 long layer_number = strtol(number_str, nullptr, 10);
423 if (layer_number <= 0 || layer_number == LONG_MAX) {
424 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
425 number_str);
426 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500427 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500428 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
429 Vector<String>* layer_names = instance_names_pair->layer_names;
430 Instance* instance = instance_names_pair->instance;
431 size_t layer_size = static_cast<size_t>(layer_number);
432 if (layer_size > layer_names->size()) {
433 layer_names->resize(layer_size,
434 String(CallbackAllocator<char>(instance->alloc)));
435 }
436 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500437}
438
439template <class TInfo, class TObject>
Jesse Hall9a16f972015-10-28 15:59:53 -0700440VkResult ActivateAllLayers(TInfo create_info, Instance* instance, TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500441 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
442 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
443 "Cannot activate layers for unknown object %p", object);
444 CallbackAllocator<char> string_allocator(instance->alloc);
445 // Load system layers
446 {
447 char layer_prop[PROPERTY_VALUE_MAX];
448 property_get("debug.vulkan.layers", layer_prop, "");
449 String layer_name(string_allocator);
450 String layer_prop_str(layer_prop, string_allocator);
451 size_t end, start = 0;
452 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
453 layer_name = layer_prop_str.substr(start, end - start);
Michael Lentine233ac732015-11-18 18:28:07 -0800454 ActivateLayer(object, instance, layer_name);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500455 start = end + 1;
456 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500457 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
458 InstanceNamesPair instance_names_pair = {.instance = instance,
459 .layer_names = &layer_names};
460 property_list(SetLayerNamesFromProperty,
461 static_cast<void*>(&instance_names_pair));
462 for (auto layer_name_element : layer_names) {
463 ActivateLayer(object, instance, layer_name_element);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500464 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500465 }
466 // Load app layers
Jesse Hall03b6fe12015-11-24 12:44:21 -0800467 for (uint32_t i = 0; i < create_info->enabledLayerNameCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500468 String layer_name(create_info->ppEnabledLayerNames[i],
469 string_allocator);
470 auto element = instance->layers.find(layer_name);
471 if (element == instance->layers.end()) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700472 ALOGE("requested %s layer '%s' not present",
473 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ?
474 "instance" : "device",
475 layer_name.c_str());
476 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500477 } else {
478 ActivateLayer(object, instance, layer_name);
479 }
480 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700481 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500482}
483
484template <class TCreateInfo>
485bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
486 const char* extension_name,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800487 const VkAllocationCallbacks* alloc) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800488 for (uint32_t i = 0; i < local_create_info.enabledExtensionNameCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500489 if (!strcmp(extension_name,
490 local_create_info.ppEnabledExtensionNames[i])) {
491 return false;
492 }
493 }
Jesse Hall03b6fe12015-11-24 12:44:21 -0800494 uint32_t extension_count = local_create_info.enabledExtensionNameCount;
495 local_create_info.enabledExtensionNameCount++;
Jesse Hall3fbc8562015-11-29 22:10:52 -0800496 void* mem = alloc->pfnAllocation(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800497 alloc->pUserData,
498 local_create_info.enabledExtensionNameCount * sizeof(char*),
Jesse Hall3fbc8562015-11-29 22:10:52 -0800499 alignof(char*), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500500 if (mem) {
501 const char** enabled_extensions = static_cast<const char**>(mem);
502 for (uint32_t i = 0; i < extension_count; ++i) {
503 enabled_extensions[i] =
504 local_create_info.ppEnabledExtensionNames[i];
505 }
506 enabled_extensions[extension_count] = extension_name;
507 local_create_info.ppEnabledExtensionNames = enabled_extensions;
508 } else {
509 ALOGW("%s extension cannot be enabled: memory allocation failed",
510 extension_name);
Jesse Hall03b6fe12015-11-24 12:44:21 -0800511 local_create_info.enabledExtensionNameCount--;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500512 return false;
513 }
514 return true;
515}
516
517template <class T>
518void FreeAllocatedCreateInfo(T& local_create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800519 const VkAllocationCallbacks* alloc) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500520 alloc->pfnFree(
521 alloc->pUserData,
522 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
523}
524
Michael Lentineeb970862015-10-15 12:42:22 -0500525VkBool32 LogDebugMessageCallback(VkFlags message_flags,
526 VkDbgObjectType /*obj_type*/,
527 uint64_t /*src_object*/,
528 size_t /*location*/,
529 int32_t message_code,
530 const char* layer_prefix,
531 const char* message,
532 void* /*user_data*/) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500533 if (message_flags & VK_DBG_REPORT_ERROR_BIT) {
534 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
535 } else if (message_flags & VK_DBG_REPORT_WARN_BIT) {
536 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
537 }
Michael Lentineeb970862015-10-15 12:42:22 -0500538 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500539}
540
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800541VkResult Noop(...) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500542 return VK_SUCCESS;
543}
544
545PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) {
546 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
547 return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr);
548 }
549 if (strcmp(name, "vkCreateDevice") == 0) {
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800550 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
Michael Lentine03c64b02015-08-26 18:27:26 -0500551 }
Michael Lentine88594d72015-11-12 12:49:45 -0800552 // WSI extensions are not in the driver so return the loader functions
Michael Lentine88594d72015-11-12 12:49:45 -0800553 if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
554 return reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR);
555 }
556 if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
557 return reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR);
558 }
559 if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
560 return reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR);
561 }
562 if (strcmp(name, "vkAcquireNextImageKHR") == 0) {
563 return reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR);
564 }
565 if (strcmp(name, "vkQueuePresentKHR") == 0) {
566 return reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR);
567 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500568 if (!device)
569 return GetGlobalDeviceProcAddr(name);
570 Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500571 return loader_device->instance->drv.GetDeviceProcAddr(device, name);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500572}
573
Jesse Hall04f4f472015-08-16 19:51:04 -0700574// -----------------------------------------------------------------------------
575// "Bottom" functions. These are called at the end of the instance dispatch
576// chain.
577
Jesse Hall03b6fe12015-11-24 12:44:21 -0800578void DestroyInstanceBottom(VkInstance instance,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800579 const VkAllocationCallbacks* allocator) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700580 // These checks allow us to call DestroyInstanceBottom from any error path
581 // in CreateInstanceBottom, before the driver instance is fully initialized.
582 if (instance->drv.vtbl.instance != VK_NULL_HANDLE &&
583 instance->drv.vtbl.DestroyInstance) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800584 instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance,
585 allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700586 }
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500587 if (instance->message) {
588 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500589 DebugDestroyMessageCallback =
590 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
591 vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback"));
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500592 DebugDestroyMessageCallback(instance, instance->message);
593 }
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800594 for (auto it = instance->active_layers.begin();
595 it != instance->active_layers.end(); ++it) {
Michael Lentine1d1e65f2015-11-19 14:23:06 -0800596 DeactivateLayer(instance, it);
Michael Lentined1d5e5e2015-11-02 18:32:04 -0800597 }
Jesse Hall3fbc8562015-11-29 22:10:52 -0800598 const VkAllocationCallbacks* alloc = instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700599 instance->~VkInstance_T();
600 alloc->pfnFree(alloc->pUserData, instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700601}
602
603VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800604 const VkAllocationCallbacks* allocator,
Jesse Hall04f4f472015-08-16 19:51:04 -0700605 VkInstance* instance_ptr) {
606 Instance* instance = *instance_ptr;
607 VkResult result;
608
Jesse Hall03b6fe12015-11-24 12:44:21 -0800609 result = g_hwdevice->CreateInstance(create_info, instance->alloc,
610 &instance->drv.vtbl.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700611 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800612 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700613 return result;
614 }
615
Michael Lentine03c64b02015-08-26 18:27:26 -0500616 if (!LoadInstanceVtbl(
617 instance->drv.vtbl.instance, instance->drv.vtbl.instance,
618 g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800619 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700620 return VK_ERROR_INITIALIZATION_FAILED;
621 }
622
623 // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be
624 // queryable from the Instance, and that the resulting function work for any
625 // VkDevice created from the instance.
626 instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
627 g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance,
628 "vkGetDeviceProcAddr"));
629 if (!instance->drv.GetDeviceProcAddr) {
630 ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr");
Jesse Hall03b6fe12015-11-24 12:44:21 -0800631 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700632 return VK_ERROR_INITIALIZATION_FAILED;
633 }
634
635 hwvulkan_dispatch_t* dispatch =
636 reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance);
637 if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
638 // Skip setting dispatch->vtbl on the driver instance handle, since we
639 // never intentionally call through it; we go through Instance::drv.vtbl
640 // instead.
641 } else {
642 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
643 dispatch->magic);
Jesse Hall03b6fe12015-11-24 12:44:21 -0800644 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700645 return VK_ERROR_INITIALIZATION_FAILED;
646 }
647
648 uint32_t num_physical_devices = 0;
649 result = instance->drv.vtbl.EnumeratePhysicalDevices(
650 instance->drv.vtbl.instance, &num_physical_devices, nullptr);
651 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800652 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700653 return VK_ERROR_INITIALIZATION_FAILED;
654 }
655 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
656 result = instance->drv.vtbl.EnumeratePhysicalDevices(
657 instance->drv.vtbl.instance, &num_physical_devices,
658 instance->physical_devices);
659 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800660 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700661 return VK_ERROR_INITIALIZATION_FAILED;
662 }
663 for (uint32_t i = 0; i < num_physical_devices; i++) {
664 dispatch = reinterpret_cast<hwvulkan_dispatch_t*>(
665 instance->physical_devices[i]);
666 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
667 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
668 dispatch->magic);
Jesse Hall03b6fe12015-11-24 12:44:21 -0800669 DestroyInstanceBottom(instance, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700670 return VK_ERROR_INITIALIZATION_FAILED;
671 }
672 dispatch->vtbl = instance->vtbl;
673 }
674 instance->drv.num_physical_devices = num_physical_devices;
675
676 instance->num_physical_devices = instance->drv.num_physical_devices;
677 return VK_SUCCESS;
678}
679
680VkResult EnumeratePhysicalDevicesBottom(VkInstance instance,
681 uint32_t* pdev_count,
682 VkPhysicalDevice* pdevs) {
683 uint32_t count = instance->num_physical_devices;
684 if (pdevs) {
685 count = std::min(count, *pdev_count);
686 std::copy(instance->physical_devices,
687 instance->physical_devices + count, pdevs);
688 }
689 *pdev_count = count;
690 return VK_SUCCESS;
691}
692
Jesse Hall606a54e2015-11-19 22:17:28 -0800693void GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev,
694 VkPhysicalDeviceFeatures* features) {
695 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features);
Jesse Hall04f4f472015-08-16 19:51:04 -0700696}
697
Jesse Hall606a54e2015-11-19 22:17:28 -0800698void GetPhysicalDeviceFormatPropertiesBottom(VkPhysicalDevice pdev,
699 VkFormat format,
700 VkFormatProperties* properties) {
701 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700702 pdev, format, properties);
703}
704
Jesse Hall606a54e2015-11-19 22:17:28 -0800705void GetPhysicalDeviceImageFormatPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700706 VkPhysicalDevice pdev,
707 VkFormat format,
708 VkImageType type,
709 VkImageTiling tiling,
710 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700711 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700712 VkImageFormatProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800713 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties(
714 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700715}
716
Jesse Hall606a54e2015-11-19 22:17:28 -0800717void GetPhysicalDevicePropertiesBottom(VkPhysicalDevice pdev,
718 VkPhysicalDeviceProperties* properties) {
719 GetVtbl(pdev)
Jesse Hall04f4f472015-08-16 19:51:04 -0700720 ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties);
721}
722
Jesse Hall606a54e2015-11-19 22:17:28 -0800723void GetPhysicalDeviceQueueFamilyPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700724 VkPhysicalDevice pdev,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700725 uint32_t* pCount,
726 VkQueueFamilyProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800727 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties(
728 pdev, pCount, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700729}
730
Jesse Hall606a54e2015-11-19 22:17:28 -0800731void GetPhysicalDeviceMemoryPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700732 VkPhysicalDevice pdev,
733 VkPhysicalDeviceMemoryProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800734 GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700735 pdev, properties);
736}
737
738VkResult CreateDeviceBottom(VkPhysicalDevice pdev,
739 const VkDeviceCreateInfo* create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800740 const VkAllocationCallbacks* allocator,
Jesse Hall04f4f472015-08-16 19:51:04 -0700741 VkDevice* out_device) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500742 Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700743 VkResult result;
744
Jesse Hall03b6fe12015-11-24 12:44:21 -0800745 if (!allocator) {
746 if (instance.alloc)
747 allocator = instance.alloc;
748 else
749 allocator = &kDefaultAllocCallbacks;
750 }
751
Jesse Hall3fbc8562015-11-29 22:10:52 -0800752 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
753 alignof(Device),
754 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700755 if (!mem)
756 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500757 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700758
Jesse Hall9a16f972015-10-28 15:59:53 -0700759 result = ActivateAllLayers(create_info, &instance, device);
760 if (result != VK_SUCCESS) {
761 DestroyDevice(device);
762 return result;
763 }
764
Jesse Hall04f4f472015-08-16 19:51:04 -0700765 VkDevice drv_device;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800766 result = instance.drv.vtbl.CreateDevice(pdev, create_info, allocator,
767 &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700768 if (result != VK_SUCCESS) {
769 DestroyDevice(device);
770 return result;
771 }
772
Jesse Hall04f4f472015-08-16 19:51:04 -0700773 hwvulkan_dispatch_t* dispatch =
774 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
775 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
776 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500777 PFN_vkDestroyDevice destroy_device =
778 reinterpret_cast<PFN_vkDestroyDevice>(
779 instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800780 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700781 DestroyDevice(device);
782 return VK_ERROR_INITIALIZATION_FAILED;
783 }
784 dispatch->vtbl = &device->vtbl_storage;
785
Michael Lentine03c64b02015-08-26 18:27:26 -0500786 void* base_object = static_cast<void*>(drv_device);
787 void* next_object = base_object;
788 VkLayerLinkedListElem* next_element;
789 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr;
790 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500791 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500792 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
793
794 for (size_t i = elem_list.size(); i > 0; i--) {
795 size_t idx = i - 1;
796 next_element = &elem_list[idx];
797 next_element->get_proc_addr =
798 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
799 next_element->base_object = base_object;
800 next_element->next_element = next_object;
801 next_object = static_cast<void*>(next_element);
802
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500803 auto& name = device->active_layers[idx]->first;
804 auto& handle = device->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -0500805 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500806 dlsym(handle, (name + "GetDeviceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -0500807 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500808 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500809 dlsym(handle, "vkGetDeviceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -0700810 if (!next_get_proc_addr) {
811 ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500812 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -0700813 next_object = next_element->next_element;
814 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
815 next_element->get_proc_addr);
816 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500817 }
818 }
819
820 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
821 static_cast<VkDevice>(next_object), next_get_proc_addr,
822 device->vtbl_storage)) {
823 DestroyDevice(device);
824 return VK_ERROR_INITIALIZATION_FAILED;
825 }
826
827 PFN_vkCreateDevice layer_createDevice =
828 reinterpret_cast<PFN_vkCreateDevice>(
829 device->vtbl_storage.GetDeviceProcAddr(drv_device,
830 "vkCreateDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800831 layer_createDevice(pdev, create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700832
Michael Lentine88594d72015-11-12 12:49:45 -0800833 // TODO(mlentine) : This is needed to use WSI layer validation. Remove this
834 // when new version of layer initialization exits.
835 if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object),
836 static_cast<VkDevice>(next_object), next_get_proc_addr,
837 device->vtbl_storage)) {
838 DestroyDevice(device);
839 return VK_ERROR_INITIALIZATION_FAILED;
840 }
841
Jesse Hall04f4f472015-08-16 19:51:04 -0700842 *out_device = drv_device;
843 return VK_SUCCESS;
844}
845
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700846VkResult EnumerateDeviceExtensionPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700847 VkPhysicalDevice pdev,
848 const char* layer_name,
849 uint32_t* properties_count,
850 VkExtensionProperties* properties) {
851 // TODO: what are we supposed to do with layer_name here?
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700852 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties(
853 pdev, layer_name, properties_count, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700854}
855
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700856VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev,
857 uint32_t* properties_count,
858 VkLayerProperties* properties) {
859 return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700860 pdev, properties_count, properties);
861}
862
Jesse Hall606a54e2015-11-19 22:17:28 -0800863void GetPhysicalDeviceSparseImageFormatPropertiesBottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700864 VkPhysicalDevice pdev,
865 VkFormat format,
866 VkImageType type,
867 uint32_t samples,
868 VkImageUsageFlags usage,
869 VkImageTiling tiling,
870 uint32_t* properties_count,
871 VkSparseImageFormatProperties* properties) {
Jesse Hall606a54e2015-11-19 22:17:28 -0800872 GetVtbl(pdev)
Jesse Hall04f4f472015-08-16 19:51:04 -0700873 ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties(
874 pdev, format, type, samples, usage, tiling, properties_count,
875 properties);
876}
877
878PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*);
879
880const InstanceVtbl kBottomInstanceFunctions = {
881 // clang-format off
882 .instance = nullptr,
883 .CreateInstance = CreateInstanceBottom,
884 .DestroyInstance = DestroyInstanceBottom,
885 .GetInstanceProcAddr = GetInstanceProcAddrBottom,
886 .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom,
887 .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom,
888 .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom,
889 .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700890 .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700891 .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700892 .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom,
893 .CreateDevice = CreateDeviceBottom,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700894 .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom,
895 .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom,
Jesse Hall04f4f472015-08-16 19:51:04 -0700896 .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700897 .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR,
Jesse Hallb00daad2015-11-29 19:46:20 -0800898 .GetPhysicalDeviceSurfaceCapabilitiesKHR = GetPhysicalDeviceSurfaceCapabilitiesKHR,
899 .GetPhysicalDeviceSurfaceFormatsKHR = GetPhysicalDeviceSurfaceFormatsKHR,
900 .GetPhysicalDeviceSurfacePresentModesKHR = GetPhysicalDeviceSurfacePresentModesKHR,
Jesse Hall04f4f472015-08-16 19:51:04 -0700901 // clang-format on
902};
903
904PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500905 // TODO: Possibly move this into the instance table
906 // TODO: Possibly register the callbacks in the loader
907 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
908 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
909 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
910 }
911 if (strcmp(name, "vkCreateInstance") == 0) {
912 return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
913 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700914 return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
915}
916
917} // namespace
918
919// -----------------------------------------------------------------------------
920// Global functions. These are called directly from the loader entry points,
921// without going through a dispatch table.
922
923namespace vulkan {
924
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700925VkResult EnumerateInstanceExtensionProperties(
926 const char* /*layer_name*/,
927 uint32_t* count,
928 VkExtensionProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700929 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700930 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700931
932 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700933 ALOGW("vkEnumerateInstanceExtensionProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700934
935 *count = 0;
936 return VK_SUCCESS;
937}
938
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700939VkResult EnumerateInstanceLayerProperties(uint32_t* count,
940 VkLayerProperties* /*properties*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700941 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700942 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700943
944 // TODO: not yet implemented
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700945 ALOGW("vkEnumerateInstanceLayerProperties not implemented");
Jesse Hall04f4f472015-08-16 19:51:04 -0700946
947 *count = 0;
948 return VK_SUCCESS;
949}
950
951VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800952 const VkAllocationCallbacks* allocator,
Jesse Hall04f4f472015-08-16 19:51:04 -0700953 VkInstance* out_instance) {
954 VkResult result;
955
956 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700957 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700958
Jesse Hall03b6fe12015-11-24 12:44:21 -0800959 if (!allocator)
960 allocator = &kDefaultAllocCallbacks;
961
Jesse Hall04f4f472015-08-16 19:51:04 -0700962 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -0700963 create_info = &local_create_info;
964
Jesse Hall3fbc8562015-11-29 22:10:52 -0800965 void* instance_mem = allocator->pfnAllocation(
966 allocator->pUserData, sizeof(Instance), alignof(Instance),
967 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700968 if (!instance_mem)
969 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800970 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700971
972 instance->vtbl_storage = kBottomInstanceFunctions;
973 instance->vtbl_storage.instance = instance;
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500974 instance->message = VK_NULL_HANDLE;
Jesse Hall04f4f472015-08-16 19:51:04 -0700975
Michael Lentine03c64b02015-08-26 18:27:26 -0500976 // Scan layers
Michael Lentine03c64b02015-08-26 18:27:26 -0500977 CallbackAllocator<char> string_allocator(instance->alloc);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500978
Michael Lentine03c64b02015-08-26 18:27:26 -0500979 String dir_name("/data/local/tmp/vulkan/", string_allocator);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500980 FindLayersInDirectory(*instance, dir_name);
Michael Lentine1c69b9e2015-09-14 13:26:59 -0500981 const std::string& path = LoaderData::GetInstance().layer_path;
982 dir_name.assign(path.c_str(), path.size());
983 dir_name.append("/");
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500984 FindLayersInDirectory(*instance, dir_name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700985
Jesse Hall9a16f972015-10-28 15:59:53 -0700986 result = ActivateAllLayers(create_info, instance, instance);
987 if (result != VK_SUCCESS) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800988 DestroyInstanceBottom(instance, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -0700989 return result;
990 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500991
992 void* base_object = static_cast<void*>(instance);
993 void* next_object = base_object;
994 VkLayerLinkedListElem* next_element;
995 PFN_vkGetInstanceProcAddr next_get_proc_addr =
996 kBottomInstanceFunctions.GetInstanceProcAddr;
997 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700998 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500999 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
1000
1001 for (size_t i = elem_list.size(); i > 0; i--) {
1002 size_t idx = i - 1;
1003 next_element = &elem_list[idx];
1004 next_element->get_proc_addr =
1005 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
1006 next_element->base_object = base_object;
1007 next_element->next_element = next_object;
1008 next_object = static_cast<void*>(next_element);
1009
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001010 auto& name = instance->active_layers[idx]->first;
1011 auto& handle = instance->active_layers[idx]->second.handle;
Michael Lentine03c64b02015-08-26 18:27:26 -05001012 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001013 dlsym(handle, (name + "GetInstanceProcAddr").c_str()));
Michael Lentine03c64b02015-08-26 18:27:26 -05001014 if (!next_get_proc_addr) {
Michael Lentine03c64b02015-08-26 18:27:26 -05001015 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001016 dlsym(handle, "vkGetInstanceProcAddr"));
Michael Lentine1f0f5392015-09-11 14:54:34 -07001017 if (!next_get_proc_addr) {
1018 ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s",
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001019 name.c_str(), dlerror());
Michael Lentine1f0f5392015-09-11 14:54:34 -07001020 next_object = next_element->next_element;
1021 next_get_proc_addr =
1022 reinterpret_cast<PFN_vkGetInstanceProcAddr>(
1023 next_element->get_proc_addr);
1024 }
Michael Lentine03c64b02015-08-26 18:27:26 -05001025 }
1026 }
1027
1028 if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object),
1029 static_cast<VkInstance>(next_object),
1030 next_get_proc_addr, instance->vtbl_storage)) {
Jesse Hall03b6fe12015-11-24 12:44:21 -08001031 DestroyInstanceBottom(instance, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -05001032 return VK_ERROR_INITIALIZATION_FAILED;
1033 }
1034
Michael Lentine950bb4f2015-09-14 13:26:30 -05001035 // Force enable callback extension if required
1036 bool enable_callback =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001037 property_get_bool("debug.vulkan.enable_callback", false);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001038 bool enable_logging = enable_callback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001039 const char* extension_name = "DEBUG_REPORT";
Michael Lentine950bb4f2015-09-14 13:26:30 -05001040 if (enable_callback) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001041 enable_callback = AddExtensionToCreateInfo(
1042 local_create_info, extension_name, instance->alloc);
Michael Lentine950bb4f2015-09-14 13:26:30 -05001043 }
1044
Jesse Hall04f4f472015-08-16 19:51:04 -07001045 *out_instance = instance;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001046 result = instance->vtbl_storage.CreateInstance(create_info, allocator,
1047 out_instance);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001048 if (enable_callback)
1049 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -07001050 if (result <= 0) {
1051 // For every layer, including the loader top and bottom layers:
1052 // - If a call to the next CreateInstance fails, the layer must clean
1053 // up anything it has successfully done so far, and propagate the
1054 // error upwards.
1055 // - If a layer successfully calls the next layer's CreateInstance, and
1056 // afterwards must fail for some reason, it must call the next layer's
1057 // DestroyInstance before returning.
1058 // - The layer must not call the next layer's DestroyInstance if that
1059 // layer's CreateInstance wasn't called, or returned failure.
1060
1061 // On failure, CreateInstanceBottom frees the instance struct, so it's
1062 // already gone at this point. Nothing to do.
1063 }
1064
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001065 if (enable_logging) {
1066 PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback;
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001067 DebugCreateMessageCallback =
1068 reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
1069 vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback"));
1070 DebugCreateMessageCallback(
1071 instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1072 LogDebugMessageCallback, NULL, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001073 }
1074
Jesse Hall04f4f472015-08-16 19:51:04 -07001075 return result;
1076}
1077
1078PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
1079 if (!instance)
1080 return GetGlobalInstanceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001081 // TODO: Possibly move this into the instance table
1082 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
1083 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
1084 if (!instance->vtbl)
1085 return NULL;
1086 PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr;
1087 return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name));
1088 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001089 // For special-case functions we always return the loader entry
1090 if (strcmp(name, "vkGetInstanceProcAddr") == 0 ||
1091 strcmp(name, "vkGetDeviceProcAddr") == 0) {
1092 return GetGlobalInstanceProcAddr(name);
1093 }
1094 return GetSpecificInstanceProcAddr(instance->vtbl, name);
1095}
1096
1097PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
1098 if (!device)
1099 return GetGlobalDeviceProcAddr(name);
Michael Lentine03c64b02015-08-26 18:27:26 -05001100 if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
1101 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
1102 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001103 if (strcmp(name, "vkGetDeviceQueue") == 0) {
1104 return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
1105 }
Jesse Hallfbf97b02015-11-20 14:17:03 -08001106 if (strcmp(name, "vkAllocCommandBuffers") == 0) {
1107 return reinterpret_cast<PFN_vkVoidFunction>(AllocCommandBuffers);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001108 }
1109 if (strcmp(name, "vkDestroyDevice") == 0) {
1110 return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
1111 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001112 return GetSpecificDeviceProcAddr(GetVtbl(device), name);
1113}
1114
Jesse Hall606a54e2015-11-19 22:17:28 -08001115void GetDeviceQueue(VkDevice drv_device,
1116 uint32_t family,
1117 uint32_t index,
1118 VkQueue* out_queue) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001119 VkResult result;
1120 VkQueue queue;
1121 const DeviceVtbl* vtbl = GetVtbl(drv_device);
Jesse Hall606a54e2015-11-19 22:17:28 -08001122 vtbl->GetDeviceQueue(drv_device, family, index, &queue);
Jesse Hall04f4f472015-08-16 19:51:04 -07001123 hwvulkan_dispatch_t* dispatch =
1124 reinterpret_cast<hwvulkan_dispatch_t*>(queue);
Jesse Hall606a54e2015-11-19 22:17:28 -08001125 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != vtbl)
Jesse Hall04f4f472015-08-16 19:51:04 -07001126 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic);
Jesse Hall04f4f472015-08-16 19:51:04 -07001127 dispatch->vtbl = vtbl;
1128 *out_queue = queue;
Jesse Hall04f4f472015-08-16 19:51:04 -07001129}
1130
Jesse Hallfbf97b02015-11-20 14:17:03 -08001131VkResult AllocCommandBuffers(VkDevice device,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001132 const VkCommandBufferAllocateInfo* alloc_info,
1133 VkCommandBuffer* cmdbufs) {
Jesse Hallfbf97b02015-11-20 14:17:03 -08001134 const DeviceVtbl* vtbl = GetVtbl(device);
Jesse Hall3fbc8562015-11-29 22:10:52 -08001135 VkResult result = vtbl->AllocateCommandBuffers(device, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001136 if (result != VK_SUCCESS)
1137 return result;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001138 for (uint32_t i = 0; i < alloc_info->bufferCount; i++) {
Jesse Hallfbf97b02015-11-20 14:17:03 -08001139 hwvulkan_dispatch_t* dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001140 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hallfbf97b02015-11-20 14:17:03 -08001141 ALOGE_IF(dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001142 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hallfbf97b02015-11-20 14:17:03 -08001143 dispatch->magic);
1144 dispatch->vtbl = vtbl;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001145 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001146 return VK_SUCCESS;
1147}
1148
Jesse Hall03b6fe12015-11-24 12:44:21 -08001149VkResult DestroyDevice(VkDevice drv_device,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001150 const VkAllocationCallbacks* /*allocator*/) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001151 const DeviceVtbl* vtbl = GetVtbl(drv_device);
1152 Device* device = static_cast<Device*>(vtbl->device);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001153 for (auto it = device->active_layers.begin();
1154 it != device->active_layers.end(); ++it) {
Michael Lentine1d1e65f2015-11-19 14:23:06 -08001155 DeactivateLayer(device->instance, it);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001156 }
Jesse Hall03b6fe12015-11-24 12:44:21 -08001157 vtbl->DestroyDevice(drv_device, device->instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -07001158 DestroyDevice(device);
1159 return VK_SUCCESS;
1160}
1161
Jesse Hall1356b0d2015-11-23 17:24:58 -08001162void* AllocMem(VkInstance instance,
1163 size_t size,
1164 size_t align,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001165 VkSystemAllocationScope scope) {
1166 const VkAllocationCallbacks* alloc_cb = instance->alloc;
1167 return alloc_cb->pfnAllocation(alloc_cb->pUserData, size, align, scope);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001168}
1169
1170void FreeMem(VkInstance instance, void* ptr) {
Jesse Hall3fbc8562015-11-29 22:10:52 -08001171 const VkAllocationCallbacks* alloc_cb = instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001172 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1173}
1174
1175void* AllocMem(VkDevice device,
1176 size_t size,
1177 size_t align,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001178 VkSystemAllocationScope scope) {
1179 const VkAllocationCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001180 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Hall3fbc8562015-11-29 22:10:52 -08001181 return alloc_cb->pfnAllocation(alloc_cb->pUserData, size, align, scope);
Jesse Halld7b994a2015-09-07 14:17:37 -07001182}
1183
Jesse Hall1356b0d2015-11-23 17:24:58 -08001184void FreeMem(VkDevice device, void* ptr) {
Jesse Hall3fbc8562015-11-29 22:10:52 -08001185 const VkAllocationCallbacks* alloc_cb =
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001186 static_cast<Device*>(GetVtbl(device)->device)->instance->alloc;
Jesse Halld7b994a2015-09-07 14:17:37 -07001187 alloc_cb->pfnFree(alloc_cb->pUserData, ptr);
1188}
1189
1190const DeviceVtbl& GetDriverVtbl(VkDevice device) {
1191 // TODO(jessehall): This actually returns the API-level vtbl for the
1192 // device, not the driver entry points. Given the current use -- getting
1193 // the driver's private swapchain-related functions -- that works, but is
1194 // misleading and likely to cause bugs. Fix as part of separating the
1195 // loader->driver interface from the app->loader interface.
1196 return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage;
1197}
1198
1199const DeviceVtbl& GetDriverVtbl(VkQueue queue) {
1200 // TODO(jessehall): This actually returns the API-level vtbl for the
1201 // device, not the driver entry points. Given the current use -- getting
1202 // the driver's private swapchain-related functions -- that works, but is
1203 // misleading and likely to cause bugs. Fix as part of separating the
1204 // loader->driver interface from the app->loader interface.
1205 return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage;
1206}
1207
Jesse Hall04f4f472015-08-16 19:51:04 -07001208} // namespace vulkan