blob: a57e5da93028e3b1a2d92d505210194c6bcdd679 [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
Jesse Hall80523e22016-01-06 16:47:54 -080017#define LOG_NDEBUG 0
Michael Lentine9dbe67f2015-09-16 15:53:50 -050018
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>
Jesse Hall21597662015-12-18 13:48:24 -080028#include <sys/prctl.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070029// standard C++ headers
30#include <algorithm>
31#include <mutex>
Michael Lentine03c64b02015-08-26 18:27:26 -050032#include <sstream>
33#include <string>
34#include <unordered_map>
35#include <vector>
Jesse Hall04f4f472015-08-16 19:51:04 -070036// platform/library headers
Michael Lentine03c64b02015-08-26 18:27:26 -050037#include <cutils/properties.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070038#include <hardware/hwvulkan.h>
39#include <log/log.h>
Michael Lentinecd6cabf2015-09-14 17:32:59 -050040#include <vulkan/vk_debug_report_lunarg.h>
Michael Lentine1c69b9e2015-09-14 13:26:59 -050041#include <vulkan/vulkan_loader_data.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070042
43using namespace vulkan;
44
45static const uint32_t kMaxPhysicalDevices = 4;
46
Michael Lentine03c64b02015-08-26 18:27:26 -050047namespace {
48
49// These definitions are taken from the LunarG Vulkan Loader. They are used to
50// enforce compatability between the Loader and Layers.
51typedef void* (*PFN_vkGetProcAddr)(void* obj, const char* pName);
52
53typedef struct VkLayerLinkedListElem_ {
54 PFN_vkGetProcAddr get_proc_addr;
55 void* next_element;
56 void* base_object;
57} VkLayerLinkedListElem;
58
Jesse Hall1f91d392015-12-11 16:28:44 -080059// ----------------------------------------------------------------------------
Michael Lentine03c64b02015-08-26 18:27:26 -050060
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>,
Jesse Hall1f91d392015-12-11 16:28:44 -0800108 class Pred = std::equal_to<Key>>
Michael Lentine03c64b02015-08-26 18:27:26 -0500109using UnorderedMap =
110 std::unordered_map<Key,
111 T,
112 Hash,
113 Pred,
Jesse Hall1f91d392015-12-11 16:28:44 -0800114 CallbackAllocator<std::pair<const Key, T>>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500115
116template <class T>
Jesse Hall1f91d392015-12-11 16:28:44 -0800117using Vector = std::vector<T, CallbackAllocator<T>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500118
Jesse Hall1f91d392015-12-11 16:28:44 -0800119typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
120 String;
Michael Lentine03c64b02015-08-26 18:27:26 -0500121
Jesse Hall1f91d392015-12-11 16:28:44 -0800122// ----------------------------------------------------------------------------
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500123
Jesse Halle1b12782015-11-30 11:27:32 -0800124VKAPI_ATTR void* DefaultAllocate(void*,
125 size_t size,
126 size_t alignment,
127 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800128 void* ptr = nullptr;
129 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
130 // additionally requires that it be at least sizeof(void*).
131 return posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0
132 ? ptr
133 : nullptr;
134}
135
Jesse Halle1b12782015-11-30 11:27:32 -0800136VKAPI_ATTR void* DefaultReallocate(void*,
137 void* ptr,
138 size_t size,
139 size_t alignment,
140 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800141 if (size == 0) {
142 free(ptr);
143 return nullptr;
144 }
145
146 // TODO(jessehall): Right now we never shrink allocations; if the new
147 // request is smaller than the existing chunk, we just continue using it.
148 // Right now the loader never reallocs, so this doesn't matter. If that
149 // changes, or if this code is copied into some other project, this should
150 // probably have a heuristic to allocate-copy-free when doing so will save
151 // "enough" space.
152 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
153 if (size <= old_size)
154 return ptr;
155
156 void* new_ptr = nullptr;
157 if (posix_memalign(&new_ptr, alignment, size) != 0)
158 return nullptr;
159 if (ptr) {
160 memcpy(new_ptr, ptr, std::min(old_size, size));
161 free(ptr);
162 }
163 return new_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700164}
165
Jesse Halle1b12782015-11-30 11:27:32 -0800166VKAPI_ATTR void DefaultFree(void*, void* pMem) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700167 free(pMem);
168}
169
Jesse Hall3fbc8562015-11-29 22:10:52 -0800170const VkAllocationCallbacks kDefaultAllocCallbacks = {
Jesse Hall04f4f472015-08-16 19:51:04 -0700171 .pUserData = nullptr,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800172 .pfnAllocation = DefaultAllocate,
173 .pfnReallocation = DefaultReallocate,
Jesse Hall04f4f472015-08-16 19:51:04 -0700174 .pfnFree = DefaultFree,
175};
176
Jesse Hall1f91d392015-12-11 16:28:44 -0800177// ----------------------------------------------------------------------------
Jesse Hall80523e22016-01-06 16:47:54 -0800178// Global Data and Initialization
Jesse Hall1f91d392015-12-11 16:28:44 -0800179
Jesse Hall80523e22016-01-06 16:47:54 -0800180hwvulkan_device_t* g_hwdevice = nullptr;
181void LoadVulkanHAL() {
182 static const hwvulkan_module_t* module;
183 int result =
184 hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
185 if (result != 0) {
186 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result);
187 return;
188 }
189 result = module->common.methods->open(
190 &module->common, HWVULKAN_DEVICE_0,
191 reinterpret_cast<hw_device_t**>(&g_hwdevice));
192 if (result != 0) {
193 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
194 result);
195 module = nullptr;
196 return;
197 }
198}
199
Jesse Hall04f4f472015-08-16 19:51:04 -0700200bool EnsureInitialized() {
201 static std::once_flag once_flag;
Jesse Hall04f4f472015-08-16 19:51:04 -0700202 std::call_once(once_flag, []() {
Jesse Hall80523e22016-01-06 16:47:54 -0800203 LoadVulkanHAL();
204 DiscoverLayers();
Jesse Hall04f4f472015-08-16 19:51:04 -0700205 });
Jesse Hall80523e22016-01-06 16:47:54 -0800206 return g_hwdevice != nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700207}
208
Jesse Hall1f91d392015-12-11 16:28:44 -0800209// -----------------------------------------------------------------------------
210
211struct Instance {
212 Instance(const VkAllocationCallbacks* alloc_callbacks)
213 : dispatch_ptr(&dispatch),
214 handle(reinterpret_cast<VkInstance>(&dispatch_ptr)),
215 get_instance_proc_addr(nullptr),
216 alloc(alloc_callbacks),
217 num_physical_devices(0),
Jesse Hall80523e22016-01-06 16:47:54 -0800218 active_layers(CallbackAllocator<LayerRef>(alloc)),
Jesse Hall1f91d392015-12-11 16:28:44 -0800219 message(VK_NULL_HANDLE) {
220 memset(&dispatch, 0, sizeof(dispatch));
221 memset(physical_devices, 0, sizeof(physical_devices));
Jesse Hall1f91d392015-12-11 16:28:44 -0800222 drv.instance = VK_NULL_HANDLE;
223 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
224 drv.num_physical_devices = 0;
225 }
226
Jesse Hall80523e22016-01-06 16:47:54 -0800227 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800228
229 const InstanceDispatchTable* dispatch_ptr;
230 const VkInstance handle;
231 InstanceDispatchTable dispatch;
232
233 // TODO(jessehall): Only needed by GetInstanceProcAddr_Top for
234 // vkDbg*MessageCallback. Points to the outermost layer's function. Remove
235 // once the DEBUG_CALLBACK is integrated into the API file.
236 PFN_vkGetInstanceProcAddr get_instance_proc_addr;
237
238 const VkAllocationCallbacks* alloc;
239 uint32_t num_physical_devices;
240 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
241
Jesse Hall80523e22016-01-06 16:47:54 -0800242 Vector<LayerRef> active_layers;
Jesse Hall1f91d392015-12-11 16:28:44 -0800243 VkDbgMsgCallback message;
244
245 struct {
246 VkInstance instance;
247 DriverDispatchTable dispatch;
248 uint32_t num_physical_devices;
249 } drv; // may eventually be an array
250};
251
252struct Device {
253 Device(Instance* instance_)
254 : instance(instance_),
Jesse Hall80523e22016-01-06 16:47:54 -0800255 active_layers(CallbackAllocator<LayerRef>(instance->alloc)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800256 memset(&dispatch, 0, sizeof(dispatch));
257 }
258 DeviceDispatchTable dispatch;
259 Instance* instance;
260 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Jesse Hall80523e22016-01-06 16:47:54 -0800261 Vector<LayerRef> active_layers;
Jesse Hall1f91d392015-12-11 16:28:44 -0800262};
263
264template <typename THandle>
265struct HandleTraits {};
266template <>
267struct HandleTraits<VkInstance> {
268 typedef Instance LoaderObjectType;
269};
270template <>
271struct HandleTraits<VkPhysicalDevice> {
272 typedef Instance LoaderObjectType;
273};
274template <>
275struct HandleTraits<VkDevice> {
276 typedef Device LoaderObjectType;
277};
278template <>
279struct HandleTraits<VkQueue> {
280 typedef Device LoaderObjectType;
281};
282template <>
283struct HandleTraits<VkCommandBuffer> {
284 typedef Device LoaderObjectType;
285};
286
287template <typename THandle>
288typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
289 THandle handle) {
290 // TODO(jessehall): Make Instance and Device POD types (by removing the
291 // non-default constructors), so that offsetof is actually legal to use.
292 // The specific case we're using here is safe in gcc/clang (and probably
293 // most other C++ compilers), but isn't guaranteed by C++.
294 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
295#pragma clang diagnostic push
296#pragma clang diagnostic ignored "-Winvalid-offsetof"
297 const size_t kDispatchOffset = offsetof(ObjectType, dispatch);
298#pragma clang diagnostic pop
299
300 const auto& dispatch = GetDispatchTable(handle);
301 uintptr_t dispatch_addr = reinterpret_cast<uintptr_t>(&dispatch);
302 uintptr_t object_addr = dispatch_addr - kDispatchOffset;
303 return *reinterpret_cast<ObjectType*>(object_addr);
304}
305
306// -----------------------------------------------------------------------------
307
Jesse Hall04f4f472015-08-16 19:51:04 -0700308void DestroyDevice(Device* device) {
Jesse Hall3fbc8562015-11-29 22:10:52 -0800309 const VkAllocationCallbacks* alloc = device->instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700310 device->~Device();
311 alloc->pfnFree(alloc->pUserData, device);
312}
313
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500314template <class TObject>
Jesse Hall80523e22016-01-06 16:47:54 -0800315bool ActivateLayer(TObject* object, const char* name) {
316 LayerRef layer(GetLayerRef(name));
317 if (!layer)
318 return false;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500319 if (std::find(object->active_layers.begin(), object->active_layers.end(),
Jesse Hall80523e22016-01-06 16:47:54 -0800320 layer) == object->active_layers.end())
321 object->active_layers.push_back(std::move(layer));
322 ALOGV("activated layer '%s'", name);
323 return true;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500324}
325
Michael Lentine9da191b2015-10-13 11:08:45 -0500326struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500327 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500328 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500329};
330
Michael Lentine9da191b2015-10-13 11:08:45 -0500331void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500332 const char* value,
333 void* data) {
334 const char prefix[] = "debug.vulkan.layer.";
335 const size_t prefixlen = sizeof(prefix) - 1;
336 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
337 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500338 const char* number_str = name + prefixlen;
339 long layer_number = strtol(number_str, nullptr, 10);
340 if (layer_number <= 0 || layer_number == LONG_MAX) {
341 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
342 number_str);
343 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500344 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500345 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
346 Vector<String>* layer_names = instance_names_pair->layer_names;
347 Instance* instance = instance_names_pair->instance;
348 size_t layer_size = static_cast<size_t>(layer_number);
349 if (layer_size > layer_names->size()) {
350 layer_names->resize(layer_size,
351 String(CallbackAllocator<char>(instance->alloc)));
352 }
353 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500354}
355
356template <class TInfo, class TObject>
Jesse Hall1f91d392015-12-11 16:28:44 -0800357VkResult ActivateAllLayers(TInfo create_info,
358 Instance* instance,
359 TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500360 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
361 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
362 "Cannot activate layers for unknown object %p", object);
363 CallbackAllocator<char> string_allocator(instance->alloc);
364 // Load system layers
Jesse Hall21597662015-12-18 13:48:24 -0800365 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500366 char layer_prop[PROPERTY_VALUE_MAX];
367 property_get("debug.vulkan.layers", layer_prop, "");
368 String layer_name(string_allocator);
369 String layer_prop_str(layer_prop, string_allocator);
370 size_t end, start = 0;
371 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
372 layer_name = layer_prop_str.substr(start, end - start);
Jesse Hall80523e22016-01-06 16:47:54 -0800373 ActivateLayer(object, layer_name.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500374 start = end + 1;
375 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500376 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
377 InstanceNamesPair instance_names_pair = {.instance = instance,
378 .layer_names = &layer_names};
379 property_list(SetLayerNamesFromProperty,
380 static_cast<void*>(&instance_names_pair));
381 for (auto layer_name_element : layer_names) {
Jesse Hall80523e22016-01-06 16:47:54 -0800382 ActivateLayer(object, layer_name_element.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500383 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500384 }
385 // Load app layers
Jesse Hall03b6fe12015-11-24 12:44:21 -0800386 for (uint32_t i = 0; i < create_info->enabledLayerNameCount; ++i) {
Jesse Hall80523e22016-01-06 16:47:54 -0800387 if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700388 ALOGE("requested %s layer '%s' not present",
Jesse Hall1f91d392015-12-11 16:28:44 -0800389 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
390 ? "instance"
391 : "device",
Jesse Hall80523e22016-01-06 16:47:54 -0800392 create_info->ppEnabledLayerNames[i]);
Jesse Hall9a16f972015-10-28 15:59:53 -0700393 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500394 }
395 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700396 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500397}
398
399template <class TCreateInfo>
400bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
401 const char* extension_name,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800402 const VkAllocationCallbacks* alloc) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800403 for (uint32_t i = 0; i < local_create_info.enabledExtensionNameCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500404 if (!strcmp(extension_name,
405 local_create_info.ppEnabledExtensionNames[i])) {
406 return false;
407 }
408 }
Jesse Hall03b6fe12015-11-24 12:44:21 -0800409 uint32_t extension_count = local_create_info.enabledExtensionNameCount;
410 local_create_info.enabledExtensionNameCount++;
Jesse Hall3fbc8562015-11-29 22:10:52 -0800411 void* mem = alloc->pfnAllocation(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800412 alloc->pUserData,
413 local_create_info.enabledExtensionNameCount * sizeof(char*),
Jesse Hall3fbc8562015-11-29 22:10:52 -0800414 alignof(char*), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500415 if (mem) {
416 const char** enabled_extensions = static_cast<const char**>(mem);
417 for (uint32_t i = 0; i < extension_count; ++i) {
418 enabled_extensions[i] =
419 local_create_info.ppEnabledExtensionNames[i];
420 }
421 enabled_extensions[extension_count] = extension_name;
422 local_create_info.ppEnabledExtensionNames = enabled_extensions;
423 } else {
424 ALOGW("%s extension cannot be enabled: memory allocation failed",
425 extension_name);
Jesse Hall03b6fe12015-11-24 12:44:21 -0800426 local_create_info.enabledExtensionNameCount--;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500427 return false;
428 }
429 return true;
430}
431
432template <class T>
433void FreeAllocatedCreateInfo(T& local_create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800434 const VkAllocationCallbacks* alloc) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500435 alloc->pfnFree(
436 alloc->pUserData,
437 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
438}
439
Jesse Halle1b12782015-11-30 11:27:32 -0800440VKAPI_ATTR
Michael Lentineeb970862015-10-15 12:42:22 -0500441VkBool32 LogDebugMessageCallback(VkFlags message_flags,
442 VkDbgObjectType /*obj_type*/,
443 uint64_t /*src_object*/,
444 size_t /*location*/,
445 int32_t message_code,
446 const char* layer_prefix,
447 const char* message,
448 void* /*user_data*/) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500449 if (message_flags & VK_DBG_REPORT_ERROR_BIT) {
450 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
451 } else if (message_flags & VK_DBG_REPORT_WARN_BIT) {
452 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
453 }
Michael Lentineeb970862015-10-15 12:42:22 -0500454 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500455}
456
Jesse Hall06193802015-12-03 16:12:51 -0800457VkResult Noop() {
Michael Lentine03c64b02015-08-26 18:27:26 -0500458 return VK_SUCCESS;
459}
460
Jesse Hall1f91d392015-12-11 16:28:44 -0800461} // anonymous namespace
462
463namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500464
Jesse Hall04f4f472015-08-16 19:51:04 -0700465// -----------------------------------------------------------------------------
466// "Bottom" functions. These are called at the end of the instance dispatch
467// chain.
468
Jesse Hall1f91d392015-12-11 16:28:44 -0800469VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
470 const VkAllocationCallbacks* allocator,
471 VkInstance* vkinstance) {
472 Instance& instance = GetDispatchParent(*vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700473 VkResult result;
474
Jesse Hall1f91d392015-12-11 16:28:44 -0800475 result = g_hwdevice->CreateInstance(create_info, instance.alloc,
476 &instance.drv.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700477 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800478 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700479 return result;
480 }
481
Jesse Hall1f91d392015-12-11 16:28:44 -0800482 if (!LoadDriverDispatchTable(instance.drv.instance,
483 g_hwdevice->GetInstanceProcAddr,
484 instance.drv.dispatch)) {
485 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700486 return VK_ERROR_INITIALIZATION_FAILED;
487 }
488
Jesse Hall1f91d392015-12-11 16:28:44 -0800489 hwvulkan_dispatch_t* drv_dispatch =
490 reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
491 if (drv_dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
492 // Skip setting drv_dispatch->vtbl, since we never call through it;
493 // we go through instance.drv.dispatch instead.
Jesse Hall04f4f472015-08-16 19:51:04 -0700494 } else {
495 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800496 drv_dispatch->magic);
497 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700498 return VK_ERROR_INITIALIZATION_FAILED;
499 }
500
501 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800502 result = instance.drv.dispatch.EnumeratePhysicalDevices(
503 instance.drv.instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700504 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800505 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700506 return VK_ERROR_INITIALIZATION_FAILED;
507 }
508 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800509 result = instance.drv.dispatch.EnumeratePhysicalDevices(
510 instance.drv.instance, &num_physical_devices,
511 instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700512 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800513 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700514 return VK_ERROR_INITIALIZATION_FAILED;
515 }
516 for (uint32_t i = 0; i < num_physical_devices; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800517 hwvulkan_dispatch_t* pdev_dispatch =
518 reinterpret_cast<hwvulkan_dispatch_t*>(
519 instance.physical_devices[i]);
520 if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700521 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800522 pdev_dispatch->magic);
523 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700524 return VK_ERROR_INITIALIZATION_FAILED;
525 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800526 pdev_dispatch->vtbl = instance.dispatch_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700527 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800528 instance.drv.num_physical_devices = num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700529
Jesse Hall1f91d392015-12-11 16:28:44 -0800530 instance.num_physical_devices = instance.drv.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700531 return VK_SUCCESS;
532}
533
Jesse Hall1f91d392015-12-11 16:28:44 -0800534PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) {
535 PFN_vkVoidFunction pfn;
536 if ((pfn = GetLoaderBottomProcAddr(name)))
537 return pfn;
538 // TODO: Possibly move this into the instance table
539 // TODO: Possibly register the callbacks in the loader
540 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
541 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
542 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
543 }
544 return nullptr;
545}
546
547VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
548 uint32_t* pdev_count,
549 VkPhysicalDevice* pdevs) {
550 Instance& instance = GetDispatchParent(vkinstance);
551 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700552 if (pdevs) {
553 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800554 std::copy(instance.physical_devices, instance.physical_devices + count,
555 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700556 }
557 *pdev_count = count;
558 return VK_SUCCESS;
559}
560
Jesse Hall1f91d392015-12-11 16:28:44 -0800561void GetPhysicalDeviceProperties_Bottom(
562 VkPhysicalDevice pdev,
563 VkPhysicalDeviceProperties* properties) {
564 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
565 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700566}
567
Jesse Hall1f91d392015-12-11 16:28:44 -0800568void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
569 VkPhysicalDeviceFeatures* features) {
570 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
571 features);
572}
573
574void GetPhysicalDeviceMemoryProperties_Bottom(
575 VkPhysicalDevice pdev,
576 VkPhysicalDeviceMemoryProperties* properties) {
577 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
578 pdev, properties);
579}
580
581void GetPhysicalDeviceQueueFamilyProperties_Bottom(
582 VkPhysicalDevice pdev,
583 uint32_t* pCount,
584 VkQueueFamilyProperties* properties) {
585 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
586 pdev, pCount, properties);
587}
588
589void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
590 VkFormat format,
591 VkFormatProperties* properties) {
592 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700593 pdev, format, properties);
594}
595
Jesse Hall1f91d392015-12-11 16:28:44 -0800596VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700597 VkPhysicalDevice pdev,
598 VkFormat format,
599 VkImageType type,
600 VkImageTiling tiling,
601 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700602 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700603 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800604 return GetDispatchParent(pdev)
605 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800606 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700607}
608
Jesse Hall1f91d392015-12-11 16:28:44 -0800609void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700610 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800611 VkFormat format,
612 VkImageType type,
613 VkSampleCountFlagBits samples,
614 VkImageUsageFlags usage,
615 VkImageTiling tiling,
616 uint32_t* properties_count,
617 VkSparseImageFormatProperties* properties) {
618 GetDispatchParent(pdev)
619 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
620 pdev, format, type, samples, usage, tiling, properties_count,
621 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700622}
623
Jesse Halle1b12782015-11-30 11:27:32 -0800624VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800625VkResult EnumerateDeviceExtensionProperties_Bottom(
Jesse Hall80523e22016-01-06 16:47:54 -0800626 VkPhysicalDevice /*pdev*/,
627 const char* /*layer_name*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800628 uint32_t* properties_count,
Jesse Hall80523e22016-01-06 16:47:54 -0800629 VkExtensionProperties* /*properties*/) {
630 // TODO(jessehall): Implement me...
631 *properties_count = 0;
632 return VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700633}
634
Jesse Halle1b12782015-11-30 11:27:32 -0800635VKAPI_ATTR
Jesse Hall80523e22016-01-06 16:47:54 -0800636VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800637 uint32_t* properties_count,
Jesse Hall80523e22016-01-06 16:47:54 -0800638 VkLayerProperties* /*properties*/) {
639 // TODO(jessehall): Implement me...
640 *properties_count = 0;
641 return VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800642}
643
644VKAPI_ATTR
645VkResult CreateDevice_Bottom(VkPhysicalDevice pdev,
646 const VkDeviceCreateInfo* create_info,
647 const VkAllocationCallbacks* allocator,
648 VkDevice* device_out) {
649 Instance& instance = GetDispatchParent(pdev);
Jesse Hall04f4f472015-08-16 19:51:04 -0700650 VkResult result;
651
Jesse Hall03b6fe12015-11-24 12:44:21 -0800652 if (!allocator) {
653 if (instance.alloc)
654 allocator = instance.alloc;
655 else
656 allocator = &kDefaultAllocCallbacks;
657 }
658
Jesse Hall3fbc8562015-11-29 22:10:52 -0800659 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
660 alignof(Device),
661 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700662 if (!mem)
663 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500664 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700665
Jesse Hall9a16f972015-10-28 15:59:53 -0700666 result = ActivateAllLayers(create_info, &instance, device);
667 if (result != VK_SUCCESS) {
668 DestroyDevice(device);
669 return result;
670 }
671
Jesse Hall04f4f472015-08-16 19:51:04 -0700672 VkDevice drv_device;
Jesse Hall1f91d392015-12-11 16:28:44 -0800673 result = instance.drv.dispatch.CreateDevice(pdev, create_info, allocator,
674 &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700675 if (result != VK_SUCCESS) {
676 DestroyDevice(device);
677 return result;
678 }
679
Jesse Hall1f91d392015-12-11 16:28:44 -0800680 hwvulkan_dispatch_t* drv_dispatch =
Jesse Hall04f4f472015-08-16 19:51:04 -0700681 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800682 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
683 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
684 drv_dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500685 PFN_vkDestroyDevice destroy_device =
686 reinterpret_cast<PFN_vkDestroyDevice>(
Jesse Hall1f91d392015-12-11 16:28:44 -0800687 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
688 "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800689 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700690 DestroyDevice(device);
691 return VK_ERROR_INITIALIZATION_FAILED;
692 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800693 drv_dispatch->vtbl = &device->dispatch;
694 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
695 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
696 "vkGetDeviceProcAddr"));
Jesse Hall04f4f472015-08-16 19:51:04 -0700697
Michael Lentine03c64b02015-08-26 18:27:26 -0500698 void* base_object = static_cast<void*>(drv_device);
699 void* next_object = base_object;
700 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800701 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetDeviceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500702 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500703 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500704 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
705
706 for (size_t i = elem_list.size(); i > 0; i--) {
707 size_t idx = i - 1;
708 next_element = &elem_list[idx];
709 next_element->get_proc_addr =
710 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
711 next_element->base_object = base_object;
712 next_element->next_element = next_object;
713 next_object = static_cast<void*>(next_element);
714
Jesse Hall80523e22016-01-06 16:47:54 -0800715 next_get_proc_addr = device->active_layers[idx].GetGetDeviceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500716 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800717 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500718 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800719 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500720 }
721 }
722
Jesse Hall1f91d392015-12-11 16:28:44 -0800723 // This is the magic call that initializes all the layer devices and
724 // allows them to create their device_handle -> device_data mapping.
725 next_get_proc_addr(static_cast<VkDevice>(next_object),
726 "vkGetDeviceProcAddr");
727
728 // We must create all the layer devices *before* retrieving the device
729 // procaddrs, so that the layers know which extensions are enabled and
730 // therefore which functions to return procaddrs for.
731 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
732 next_get_proc_addr(drv_device, "vkCreateDevice"));
733 create_device(pdev, create_info, allocator, &drv_device);
734
735 if (!LoadDeviceDispatchTable(static_cast<VkDevice>(base_object),
736 next_get_proc_addr, device->dispatch)) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500737 DestroyDevice(device);
738 return VK_ERROR_INITIALIZATION_FAILED;
739 }
740
Jesse Hall1f91d392015-12-11 16:28:44 -0800741 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700742 return VK_SUCCESS;
743}
744
Jesse Hall1f91d392015-12-11 16:28:44 -0800745void DestroyInstance_Bottom(VkInstance vkinstance,
746 const VkAllocationCallbacks* allocator) {
747 Instance& instance = GetDispatchParent(vkinstance);
748
749 // These checks allow us to call DestroyInstance_Bottom from any error
750 // path in CreateInstance_Bottom, before the driver instance is fully
751 // initialized.
752 if (instance.drv.instance != VK_NULL_HANDLE &&
753 instance.drv.dispatch.DestroyInstance) {
754 instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
755 }
756 if (instance.message) {
757 PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback;
758 DebugDestroyMessageCallback =
759 reinterpret_cast<PFN_vkDbgDestroyMsgCallback>(
760 vkGetInstanceProcAddr(vkinstance, "vkDbgDestroyMsgCallback"));
761 DebugDestroyMessageCallback(vkinstance, instance.message);
762 }
Jesse Hall80523e22016-01-06 16:47:54 -0800763 instance.active_layers.clear();
Jesse Hall1f91d392015-12-11 16:28:44 -0800764 const VkAllocationCallbacks* alloc = instance.alloc;
765 instance.~Instance();
766 alloc->pfnFree(alloc->pUserData, &instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700767}
768
Jesse Hall1f91d392015-12-11 16:28:44 -0800769PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
770 const char* name) {
771 if (strcmp(name, "vkCreateDevice") == 0) {
772 // TODO(jessehall): Blegh, having this here is disgusting. The current
773 // layer init process can't call through the instance dispatch table's
774 // vkCreateDevice, because that goes through the instance layers rather
775 // than through the device layers. So we need to be able to get the
776 // vkCreateDevice pointer through the *device* layer chain.
777 //
778 // Because we've already created the driver device before calling
779 // through the layer vkCreateDevice functions, the loader bottom proc
780 // is a no-op.
Michael Lentine03c64b02015-08-26 18:27:26 -0500781 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
782 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800783
784 // VK_ANDROID_native_buffer should be hidden from applications and layers.
785 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
786 PFN_vkVoidFunction pfn;
787 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
788 strcmp(name, "vkAcquireImageANDROID") == 0 ||
789 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
790 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500791 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800792 if ((pfn = GetLoaderBottomProcAddr(name)))
793 return pfn;
794 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700795}
796
Jesse Hall04f4f472015-08-16 19:51:04 -0700797// -----------------------------------------------------------------------------
Jesse Hall1f91d392015-12-11 16:28:44 -0800798// Loader top functions. These are called directly from the loader entry
799// points or from the application (via vkGetInstanceProcAddr) without going
800// through a dispatch table.
Jesse Hall04f4f472015-08-16 19:51:04 -0700801
Jesse Hall1f91d392015-12-11 16:28:44 -0800802VkResult EnumerateInstanceExtensionProperties_Top(
Jesse Hall80523e22016-01-06 16:47:54 -0800803 const char* layer_name,
804 uint32_t* properties_count,
805 VkExtensionProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700806 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700807 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700808
Jesse Hall80523e22016-01-06 16:47:54 -0800809 const VkExtensionProperties* extensions = nullptr;
810 uint32_t num_extensions = 0;
811 if (layer_name) {
812 GetLayerExtensions(layer_name, &extensions, &num_extensions);
813 } else {
814 static const VkExtensionProperties kInstanceExtensions[] =
815 {{VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_REVISION},
816 {VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, VK_KHR_ANDROID_SURFACE_REVISION}};
817 extensions = kInstanceExtensions;
818 num_extensions = sizeof(kInstanceExtensions) / sizeof(kInstanceExtensions[0]);
819 // TODO(jessehall): We need to also enumerate extensions supported by
820 // implicitly-enabled layers. Currently we don't have that list of
821 // layers until instance creation.
822 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700823
Jesse Hall80523e22016-01-06 16:47:54 -0800824 if (!properties || *properties_count > num_extensions)
825 *properties_count = num_extensions;
826 if (properties)
827 std::copy(extensions, extensions + *properties_count, properties);
828 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700829}
830
Jesse Hall80523e22016-01-06 16:47:54 -0800831VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
832 VkLayerProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700833 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700834 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700835
Jesse Hall80523e22016-01-06 16:47:54 -0800836 uint32_t layer_count =
837 EnumerateLayers(properties ? *properties_count : 0, properties);
838 if (!properties || *properties_count > layer_count)
839 *properties_count = layer_count;
840 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700841}
842
Jesse Hall1f91d392015-12-11 16:28:44 -0800843VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
844 const VkAllocationCallbacks* allocator,
845 VkInstance* instance_out) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700846 VkResult result;
847
848 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700849 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700850
Jesse Hall03b6fe12015-11-24 12:44:21 -0800851 if (!allocator)
852 allocator = &kDefaultAllocCallbacks;
853
Jesse Hall04f4f472015-08-16 19:51:04 -0700854 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -0700855 create_info = &local_create_info;
856
Jesse Hall3fbc8562015-11-29 22:10:52 -0800857 void* instance_mem = allocator->pfnAllocation(
858 allocator->pUserData, sizeof(Instance), alignof(Instance),
859 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700860 if (!instance_mem)
861 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800862 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700863
Jesse Hall9a16f972015-10-28 15:59:53 -0700864 result = ActivateAllLayers(create_info, instance, instance);
865 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800866 DestroyInstance_Bottom(instance->handle, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -0700867 return result;
868 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500869
Jesse Hall1f91d392015-12-11 16:28:44 -0800870 void* base_object = static_cast<void*>(instance->handle);
Michael Lentine03c64b02015-08-26 18:27:26 -0500871 void* next_object = base_object;
872 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800873 PFN_vkGetInstanceProcAddr next_get_proc_addr = GetInstanceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500874 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700875 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500876 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
877
878 for (size_t i = elem_list.size(); i > 0; i--) {
879 size_t idx = i - 1;
880 next_element = &elem_list[idx];
881 next_element->get_proc_addr =
882 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
883 next_element->base_object = base_object;
884 next_element->next_element = next_object;
885 next_object = static_cast<void*>(next_element);
886
Jesse Hall80523e22016-01-06 16:47:54 -0800887 next_get_proc_addr =
888 instance->active_layers[idx].GetGetInstanceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500889 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800890 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500891 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800892 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500893 }
894 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800895 instance->get_instance_proc_addr = next_get_proc_addr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500896
Jesse Hall1f91d392015-12-11 16:28:44 -0800897 // This is the magic call that initializes all the layer instances and
898 // allows them to create their instance_handle -> instance_data mapping.
899 next_get_proc_addr(static_cast<VkInstance>(next_object),
900 "vkGetInstanceProcAddr");
901
902 if (!LoadInstanceDispatchTable(static_cast<VkInstance>(base_object),
903 next_get_proc_addr, instance->dispatch)) {
904 DestroyInstance_Bottom(instance->handle, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -0500905 return VK_ERROR_INITIALIZATION_FAILED;
906 }
907
Michael Lentine950bb4f2015-09-14 13:26:30 -0500908 // Force enable callback extension if required
Jesse Hall21597662015-12-18 13:48:24 -0800909 bool enable_callback = false;
910 bool enable_logging = false;
911 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
912 enable_callback =
913 property_get_bool("debug.vulkan.enable_callback", false);
914 enable_logging = enable_callback;
915 if (enable_callback) {
916 enable_callback = AddExtensionToCreateInfo(
917 local_create_info, "DEBUG_REPORT", instance->alloc);
918 }
Michael Lentine950bb4f2015-09-14 13:26:30 -0500919 }
920
Jesse Hall1f91d392015-12-11 16:28:44 -0800921 *instance_out = instance->handle;
922 PFN_vkCreateInstance create_instance =
923 reinterpret_cast<PFN_vkCreateInstance>(
924 next_get_proc_addr(instance->handle, "vkCreateInstance"));
925 result = create_instance(create_info, allocator, instance_out);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500926 if (enable_callback)
927 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -0700928 if (result <= 0) {
929 // For every layer, including the loader top and bottom layers:
930 // - If a call to the next CreateInstance fails, the layer must clean
931 // up anything it has successfully done so far, and propagate the
932 // error upwards.
933 // - If a layer successfully calls the next layer's CreateInstance, and
934 // afterwards must fail for some reason, it must call the next layer's
935 // DestroyInstance before returning.
936 // - The layer must not call the next layer's DestroyInstance if that
937 // layer's CreateInstance wasn't called, or returned failure.
938
Jesse Hall1f91d392015-12-11 16:28:44 -0800939 // On failure, CreateInstance_Bottom frees the instance struct, so it's
Jesse Hall04f4f472015-08-16 19:51:04 -0700940 // already gone at this point. Nothing to do.
941 }
942
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500943 if (enable_logging) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800944 PFN_vkDbgCreateMsgCallback dbg_create_msg_callback;
945 dbg_create_msg_callback = reinterpret_cast<PFN_vkDbgCreateMsgCallback>(
946 GetInstanceProcAddr_Top(instance->handle,
947 "vkDbgCreateMsgCallback"));
948 dbg_create_msg_callback(
949 instance->handle, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
950 LogDebugMessageCallback, nullptr, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500951 }
952
Jesse Hall04f4f472015-08-16 19:51:04 -0700953 return result;
954}
955
Jesse Hall1f91d392015-12-11 16:28:44 -0800956PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
957 const char* name) {
958 // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
959 if (!vkinstance)
960 return GetLoaderGlobalProcAddr(name);
961
962 const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
963 PFN_vkVoidFunction pfn;
964 // Always go through the loader-top function if there is one.
965 if ((pfn = GetLoaderTopProcAddr(name)))
966 return pfn;
967 // Otherwise, look up the handler in the instance dispatch table
968 if ((pfn = GetDispatchProcAddr(dispatch, name)))
969 return pfn;
970 // TODO(jessehall): Generate these into the instance dispatch table, and
971 // add loader-bottom procs for them.
Michael Lentine03c64b02015-08-26 18:27:26 -0500972 if (strcmp(name, "vkDbgCreateMsgCallback") == 0 ||
973 strcmp(name, "vkDbgDestroyMsgCallback") == 0) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800974 return GetDispatchParent(vkinstance)
975 .get_instance_proc_addr(vkinstance, name);
Michael Lentine03c64b02015-08-26 18:27:26 -0500976 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800977 // Anything not handled already must be a device-dispatched function
978 // without a loader-top. We must return a function that will dispatch based
979 // on the dispatchable object parameter -- which is exactly what the
980 // exported functions do. So just return them here.
981 return GetLoaderExportProcAddr(name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700982}
983
Jesse Hall1f91d392015-12-11 16:28:44 -0800984void DestroyInstance_Top(VkInstance instance,
985 const VkAllocationCallbacks* allocator) {
986 if (!instance)
987 return;
988 GetDispatchTable(instance).DestroyInstance(instance, allocator);
989}
990
991PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
992 PFN_vkVoidFunction pfn;
Jesse Hall04f4f472015-08-16 19:51:04 -0700993 if (!device)
Jesse Hall1f91d392015-12-11 16:28:44 -0800994 return nullptr;
995 if ((pfn = GetLoaderTopProcAddr(name)))
996 return pfn;
997 return GetDispatchProcAddr(GetDispatchTable(device), name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700998}
999
Jesse Hall1f91d392015-12-11 16:28:44 -08001000void GetDeviceQueue_Top(VkDevice vkdevice,
1001 uint32_t family,
1002 uint32_t index,
1003 VkQueue* queue_out) {
1004 const auto& table = GetDispatchTable(vkdevice);
1005 table.GetDeviceQueue(vkdevice, family, index, queue_out);
1006 hwvulkan_dispatch_t* queue_dispatch =
1007 reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
1008 if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
1009 queue_dispatch->vtbl != &table)
1010 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
1011 queue_dispatch->magic);
1012 queue_dispatch->vtbl = &table;
Jesse Hall04f4f472015-08-16 19:51:04 -07001013}
1014
Jesse Hall1f91d392015-12-11 16:28:44 -08001015VkResult AllocateCommandBuffers_Top(
1016 VkDevice vkdevice,
1017 const VkCommandBufferAllocateInfo* alloc_info,
1018 VkCommandBuffer* cmdbufs) {
1019 const auto& table = GetDispatchTable(vkdevice);
1020 VkResult result =
1021 table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001022 if (result != VK_SUCCESS)
1023 return result;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001024 for (uint32_t i = 0; i < alloc_info->bufferCount; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001025 hwvulkan_dispatch_t* cmdbuf_dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001026 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hall1f91d392015-12-11 16:28:44 -08001027 ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001028 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -08001029 cmdbuf_dispatch->magic);
1030 cmdbuf_dispatch->vtbl = &table;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001031 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001032 return VK_SUCCESS;
1033}
1034
Jesse Hall1f91d392015-12-11 16:28:44 -08001035void DestroyDevice_Top(VkDevice vkdevice,
1036 const VkAllocationCallbacks* /*allocator*/) {
1037 if (!vkdevice)
1038 return;
1039 Device& device = GetDispatchParent(vkdevice);
Jesse Hall1f91d392015-12-11 16:28:44 -08001040 device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
1041 DestroyDevice(&device);
Jesse Hall04f4f472015-08-16 19:51:04 -07001042}
1043
Jesse Hall1f91d392015-12-11 16:28:44 -08001044// -----------------------------------------------------------------------------
1045
1046const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
1047 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001048}
1049
Jesse Hall1f91d392015-12-11 16:28:44 -08001050const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
1051 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001052}
1053
Jesse Hall1f91d392015-12-11 16:28:44 -08001054const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
1055 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001056}
1057
Jesse Hall1f91d392015-12-11 16:28:44 -08001058const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
1059 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001060}
1061
Jesse Hall04f4f472015-08-16 19:51:04 -07001062} // namespace vulkan