blob: 68ca3c2297ad9e3ccd33329ec4400b80082e80e0 [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 Hall504db7f2016-01-14 15:53:57 -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 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
Jesse Hall1f91d392015-12-11 16:28:44 -080058// ----------------------------------------------------------------------------
Michael Lentine03c64b02015-08-26 18:27:26 -050059
Jesse Hall3fbc8562015-11-29 22:10:52 -080060// Standard-library allocator that delegates to VkAllocationCallbacks.
Jesse Hall03b6fe12015-11-24 12:44:21 -080061//
62// TODO(jessehall): This class currently always uses
Jesse Hall3fbc8562015-11-29 22:10:52 -080063// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
Jesse Hall03b6fe12015-11-24 12:44:21 -080064// parameter or a constructor parameter. The former would help catch bugs
65// where we use the wrong scope, e.g. adding a command-scope string to an
66// instance-scope vector. But that might also be pretty annoying to deal with.
Michael Lentine03c64b02015-08-26 18:27:26 -050067template <class T>
68class CallbackAllocator {
69 public:
70 typedef T value_type;
71
Jesse Hall3fbc8562015-11-29 22:10:52 -080072 CallbackAllocator(const VkAllocationCallbacks* alloc_input)
Michael Lentine03c64b02015-08-26 18:27:26 -050073 : alloc(alloc_input) {}
74
75 template <class T2>
76 CallbackAllocator(const CallbackAllocator<T2>& other)
77 : alloc(other.alloc) {}
78
79 T* allocate(std::size_t n) {
Jesse Hall3fbc8562015-11-29 22:10:52 -080080 void* mem =
81 alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
82 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine03c64b02015-08-26 18:27:26 -050083 return static_cast<T*>(mem);
84 }
85
86 void deallocate(T* array, std::size_t /*n*/) {
87 alloc->pfnFree(alloc->pUserData, array);
88 }
89
Jesse Hall3fbc8562015-11-29 22:10:52 -080090 const VkAllocationCallbacks* alloc;
Michael Lentine03c64b02015-08-26 18:27:26 -050091};
92// These are needed in order to move Strings
93template <class T>
94bool operator==(const CallbackAllocator<T>& alloc1,
95 const CallbackAllocator<T>& alloc2) {
96 return alloc1.alloc == alloc2.alloc;
97}
98template <class T>
99bool operator!=(const CallbackAllocator<T>& alloc1,
100 const CallbackAllocator<T>& alloc2) {
101 return !(alloc1 == alloc2);
102}
103
104template <class Key,
105 class T,
106 class Hash = std::hash<Key>,
Jesse Hall1f91d392015-12-11 16:28:44 -0800107 class Pred = std::equal_to<Key>>
Michael Lentine03c64b02015-08-26 18:27:26 -0500108using UnorderedMap =
109 std::unordered_map<Key,
110 T,
111 Hash,
112 Pred,
Jesse Hall1f91d392015-12-11 16:28:44 -0800113 CallbackAllocator<std::pair<const Key, T>>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500114
115template <class T>
Jesse Hall1f91d392015-12-11 16:28:44 -0800116using Vector = std::vector<T, CallbackAllocator<T>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500117
Jesse Hall1f91d392015-12-11 16:28:44 -0800118typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
119 String;
Michael Lentine03c64b02015-08-26 18:27:26 -0500120
Jesse Hall1f91d392015-12-11 16:28:44 -0800121// ----------------------------------------------------------------------------
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500122
Jesse Halle1b12782015-11-30 11:27:32 -0800123VKAPI_ATTR void* DefaultAllocate(void*,
124 size_t size,
125 size_t alignment,
126 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800127 void* ptr = nullptr;
128 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
129 // additionally requires that it be at least sizeof(void*).
130 return posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0
131 ? ptr
132 : nullptr;
133}
134
Jesse Halle1b12782015-11-30 11:27:32 -0800135VKAPI_ATTR void* DefaultReallocate(void*,
136 void* ptr,
137 size_t size,
138 size_t alignment,
139 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800140 if (size == 0) {
141 free(ptr);
142 return nullptr;
143 }
144
145 // TODO(jessehall): Right now we never shrink allocations; if the new
146 // request is smaller than the existing chunk, we just continue using it.
147 // Right now the loader never reallocs, so this doesn't matter. If that
148 // changes, or if this code is copied into some other project, this should
149 // probably have a heuristic to allocate-copy-free when doing so will save
150 // "enough" space.
151 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
152 if (size <= old_size)
153 return ptr;
154
155 void* new_ptr = nullptr;
156 if (posix_memalign(&new_ptr, alignment, size) != 0)
157 return nullptr;
158 if (ptr) {
159 memcpy(new_ptr, ptr, std::min(old_size, size));
160 free(ptr);
161 }
162 return new_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700163}
164
Jesse Halle1b12782015-11-30 11:27:32 -0800165VKAPI_ATTR void DefaultFree(void*, void* pMem) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700166 free(pMem);
167}
168
Jesse Hall3fbc8562015-11-29 22:10:52 -0800169const VkAllocationCallbacks kDefaultAllocCallbacks = {
Jesse Hall04f4f472015-08-16 19:51:04 -0700170 .pUserData = nullptr,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800171 .pfnAllocation = DefaultAllocate,
172 .pfnReallocation = DefaultReallocate,
Jesse Hall04f4f472015-08-16 19:51:04 -0700173 .pfnFree = DefaultFree,
174};
175
Jesse Hall1f91d392015-12-11 16:28:44 -0800176// ----------------------------------------------------------------------------
Jesse Hall80523e22016-01-06 16:47:54 -0800177// Global Data and Initialization
Jesse Hall1f91d392015-12-11 16:28:44 -0800178
Jesse Hall80523e22016-01-06 16:47:54 -0800179hwvulkan_device_t* g_hwdevice = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800180InstanceExtensionSet g_driver_instance_extensions;
181
Jesse Hall80523e22016-01-06 16:47:54 -0800182void LoadVulkanHAL() {
183 static const hwvulkan_module_t* module;
184 int result =
185 hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
186 if (result != 0) {
187 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result);
188 return;
189 }
190 result = module->common.methods->open(
191 &module->common, HWVULKAN_DEVICE_0,
192 reinterpret_cast<hw_device_t**>(&g_hwdevice));
193 if (result != 0) {
194 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
195 result);
196 module = nullptr;
197 return;
198 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800199
200 VkResult vkresult;
201 uint32_t count;
202 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
203 nullptr, &count, nullptr)) != VK_SUCCESS) {
204 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
205 vkresult);
206 g_hwdevice->common.close(&g_hwdevice->common);
207 g_hwdevice = nullptr;
208 module = nullptr;
209 return;
210 }
211 VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
212 alloca(count * sizeof(VkExtensionProperties)));
213 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
214 nullptr, &count, extensions)) != VK_SUCCESS) {
215 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
216 vkresult);
217 g_hwdevice->common.close(&g_hwdevice->common);
218 g_hwdevice = nullptr;
219 module = nullptr;
220 return;
221 }
222 ALOGV_IF(count > 0, "Driver-supported instance extensions:");
223 for (uint32_t i = 0; i < count; i++) {
224 ALOGV(" %s (v%u)", extensions[i].extensionName,
225 extensions[i].specVersion);
226 InstanceExtension id =
227 InstanceExtensionFromName(extensions[i].extensionName);
228 if (id != kInstanceExtensionCount)
229 g_driver_instance_extensions.set(id);
230 }
231 // Ignore driver attempts to support loader extensions
232 g_driver_instance_extensions.reset(kKHR_surface);
233 g_driver_instance_extensions.reset(kKHR_android_surface);
Jesse Hall80523e22016-01-06 16:47:54 -0800234}
235
Jesse Hall04f4f472015-08-16 19:51:04 -0700236bool EnsureInitialized() {
237 static std::once_flag once_flag;
Jesse Hall04f4f472015-08-16 19:51:04 -0700238 std::call_once(once_flag, []() {
Jesse Hall80523e22016-01-06 16:47:54 -0800239 LoadVulkanHAL();
240 DiscoverLayers();
Jesse Hall04f4f472015-08-16 19:51:04 -0700241 });
Jesse Hall80523e22016-01-06 16:47:54 -0800242 return g_hwdevice != nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700243}
244
Jesse Hall1f91d392015-12-11 16:28:44 -0800245// -----------------------------------------------------------------------------
246
247struct Instance {
248 Instance(const VkAllocationCallbacks* alloc_callbacks)
249 : dispatch_ptr(&dispatch),
250 handle(reinterpret_cast<VkInstance>(&dispatch_ptr)),
Jesse Hall1f91d392015-12-11 16:28:44 -0800251 alloc(alloc_callbacks),
252 num_physical_devices(0),
Jesse Hall80523e22016-01-06 16:47:54 -0800253 active_layers(CallbackAllocator<LayerRef>(alloc)),
Jesse Hall1f91d392015-12-11 16:28:44 -0800254 message(VK_NULL_HANDLE) {
255 memset(&dispatch, 0, sizeof(dispatch));
256 memset(physical_devices, 0, sizeof(physical_devices));
Jesse Hall1f91d392015-12-11 16:28:44 -0800257 drv.instance = VK_NULL_HANDLE;
258 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
259 drv.num_physical_devices = 0;
260 }
261
Jesse Hall80523e22016-01-06 16:47:54 -0800262 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800263
264 const InstanceDispatchTable* dispatch_ptr;
265 const VkInstance handle;
266 InstanceDispatchTable dispatch;
267
Jesse Hall1f91d392015-12-11 16:28:44 -0800268 const VkAllocationCallbacks* alloc;
269 uint32_t num_physical_devices;
270 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800271 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800272
Jesse Hall80523e22016-01-06 16:47:54 -0800273 Vector<LayerRef> active_layers;
Jesse Hall715b86a2016-01-16 16:34:29 -0800274 VkDebugReportCallbackEXT message;
275 DebugReportCallbackList debug_report_callbacks;
Jesse Hall1f91d392015-12-11 16:28:44 -0800276
277 struct {
278 VkInstance instance;
279 DriverDispatchTable dispatch;
280 uint32_t num_physical_devices;
281 } drv; // may eventually be an array
282};
283
284struct Device {
285 Device(Instance* instance_)
286 : instance(instance_),
Jesse Hall80523e22016-01-06 16:47:54 -0800287 active_layers(CallbackAllocator<LayerRef>(instance->alloc)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800288 memset(&dispatch, 0, sizeof(dispatch));
289 }
290 DeviceDispatchTable dispatch;
291 Instance* instance;
292 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Jesse Hall80523e22016-01-06 16:47:54 -0800293 Vector<LayerRef> active_layers;
Jesse Hall1f91d392015-12-11 16:28:44 -0800294};
295
296template <typename THandle>
297struct HandleTraits {};
298template <>
299struct HandleTraits<VkInstance> {
300 typedef Instance LoaderObjectType;
301};
302template <>
303struct HandleTraits<VkPhysicalDevice> {
304 typedef Instance LoaderObjectType;
305};
306template <>
307struct HandleTraits<VkDevice> {
308 typedef Device LoaderObjectType;
309};
310template <>
311struct HandleTraits<VkQueue> {
312 typedef Device LoaderObjectType;
313};
314template <>
315struct HandleTraits<VkCommandBuffer> {
316 typedef Device LoaderObjectType;
317};
318
319template <typename THandle>
320typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
321 THandle handle) {
322 // TODO(jessehall): Make Instance and Device POD types (by removing the
323 // non-default constructors), so that offsetof is actually legal to use.
324 // The specific case we're using here is safe in gcc/clang (and probably
325 // most other C++ compilers), but isn't guaranteed by C++.
326 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
327#pragma clang diagnostic push
328#pragma clang diagnostic ignored "-Winvalid-offsetof"
329 const size_t kDispatchOffset = offsetof(ObjectType, dispatch);
330#pragma clang diagnostic pop
331
332 const auto& dispatch = GetDispatchTable(handle);
333 uintptr_t dispatch_addr = reinterpret_cast<uintptr_t>(&dispatch);
334 uintptr_t object_addr = dispatch_addr - kDispatchOffset;
335 return *reinterpret_cast<ObjectType*>(object_addr);
336}
337
338// -----------------------------------------------------------------------------
339
Jesse Hall04f4f472015-08-16 19:51:04 -0700340void DestroyDevice(Device* device) {
Jesse Hall3fbc8562015-11-29 22:10:52 -0800341 const VkAllocationCallbacks* alloc = device->instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700342 device->~Device();
343 alloc->pfnFree(alloc->pUserData, device);
344}
345
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500346template <class TObject>
Jesse Hallaa410942016-01-17 13:07:10 -0800347LayerRef GetLayerRef(const char* name);
348template <>
349LayerRef GetLayerRef<Instance>(const char* name) {
350 return GetInstanceLayerRef(name);
351}
352template <>
353LayerRef GetLayerRef<Device>(const char* name) {
354 return GetDeviceLayerRef(name);
355}
356
357template <class TObject>
Jesse Hall80523e22016-01-06 16:47:54 -0800358bool ActivateLayer(TObject* object, const char* name) {
Jesse Hallaa410942016-01-17 13:07:10 -0800359 LayerRef layer(GetLayerRef<TObject>(name));
Jesse Hall80523e22016-01-06 16:47:54 -0800360 if (!layer)
361 return false;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500362 if (std::find(object->active_layers.begin(), object->active_layers.end(),
Jesse Hall80523e22016-01-06 16:47:54 -0800363 layer) == object->active_layers.end())
364 object->active_layers.push_back(std::move(layer));
365 ALOGV("activated layer '%s'", name);
366 return true;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500367}
368
Michael Lentine9da191b2015-10-13 11:08:45 -0500369struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500370 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500371 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500372};
373
Michael Lentine9da191b2015-10-13 11:08:45 -0500374void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500375 const char* value,
376 void* data) {
377 const char prefix[] = "debug.vulkan.layer.";
378 const size_t prefixlen = sizeof(prefix) - 1;
379 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
380 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500381 const char* number_str = name + prefixlen;
382 long layer_number = strtol(number_str, nullptr, 10);
383 if (layer_number <= 0 || layer_number == LONG_MAX) {
384 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
385 number_str);
386 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500387 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500388 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
389 Vector<String>* layer_names = instance_names_pair->layer_names;
390 Instance* instance = instance_names_pair->instance;
391 size_t layer_size = static_cast<size_t>(layer_number);
392 if (layer_size > layer_names->size()) {
393 layer_names->resize(layer_size,
394 String(CallbackAllocator<char>(instance->alloc)));
395 }
396 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500397}
398
399template <class TInfo, class TObject>
Jesse Hall1f91d392015-12-11 16:28:44 -0800400VkResult ActivateAllLayers(TInfo create_info,
401 Instance* instance,
402 TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500403 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
404 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
405 "Cannot activate layers for unknown object %p", object);
406 CallbackAllocator<char> string_allocator(instance->alloc);
407 // Load system layers
Jesse Hall21597662015-12-18 13:48:24 -0800408 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500409 char layer_prop[PROPERTY_VALUE_MAX];
410 property_get("debug.vulkan.layers", layer_prop, "");
411 String layer_name(string_allocator);
412 String layer_prop_str(layer_prop, string_allocator);
413 size_t end, start = 0;
414 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
415 layer_name = layer_prop_str.substr(start, end - start);
Jesse Hall80523e22016-01-06 16:47:54 -0800416 ActivateLayer(object, layer_name.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500417 start = end + 1;
418 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500419 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
420 InstanceNamesPair instance_names_pair = {.instance = instance,
421 .layer_names = &layer_names};
422 property_list(SetLayerNamesFromProperty,
423 static_cast<void*>(&instance_names_pair));
424 for (auto layer_name_element : layer_names) {
Jesse Hall80523e22016-01-06 16:47:54 -0800425 ActivateLayer(object, layer_name_element.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500426 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500427 }
428 // Load app layers
Jesse Hall3dd678a2016-01-08 21:52:01 -0800429 for (uint32_t i = 0; i < create_info->enabledLayerCount; ++i) {
Jesse Hall80523e22016-01-06 16:47:54 -0800430 if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700431 ALOGE("requested %s layer '%s' not present",
Jesse Hall1f91d392015-12-11 16:28:44 -0800432 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
433 ? "instance"
434 : "device",
Jesse Hall80523e22016-01-06 16:47:54 -0800435 create_info->ppEnabledLayerNames[i]);
Jesse Hall9a16f972015-10-28 15:59:53 -0700436 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500437 }
438 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700439 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500440}
441
442template <class TCreateInfo>
443bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
444 const char* extension_name,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800445 const VkAllocationCallbacks* alloc) {
Jesse Hall3dd678a2016-01-08 21:52:01 -0800446 for (uint32_t i = 0; i < local_create_info.enabledExtensionCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500447 if (!strcmp(extension_name,
448 local_create_info.ppEnabledExtensionNames[i])) {
449 return false;
450 }
451 }
Jesse Hall3dd678a2016-01-08 21:52:01 -0800452 uint32_t extension_count = local_create_info.enabledExtensionCount;
453 local_create_info.enabledExtensionCount++;
Jesse Hall3fbc8562015-11-29 22:10:52 -0800454 void* mem = alloc->pfnAllocation(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800455 alloc->pUserData,
Jesse Hall3dd678a2016-01-08 21:52:01 -0800456 local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*),
457 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500458 if (mem) {
459 const char** enabled_extensions = static_cast<const char**>(mem);
460 for (uint32_t i = 0; i < extension_count; ++i) {
461 enabled_extensions[i] =
462 local_create_info.ppEnabledExtensionNames[i];
463 }
464 enabled_extensions[extension_count] = extension_name;
465 local_create_info.ppEnabledExtensionNames = enabled_extensions;
466 } else {
467 ALOGW("%s extension cannot be enabled: memory allocation failed",
468 extension_name);
Jesse Hall3dd678a2016-01-08 21:52:01 -0800469 local_create_info.enabledExtensionCount--;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500470 return false;
471 }
472 return true;
473}
474
475template <class T>
476void FreeAllocatedCreateInfo(T& local_create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800477 const VkAllocationCallbacks* alloc) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500478 alloc->pfnFree(
479 alloc->pUserData,
480 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
481}
482
Jesse Halle1b12782015-11-30 11:27:32 -0800483VKAPI_ATTR
Jesse Hall715b86a2016-01-16 16:34:29 -0800484VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags,
485 VkDebugReportObjectTypeEXT /*objectType*/,
486 uint64_t /*object*/,
Michael Lentineeb970862015-10-15 12:42:22 -0500487 size_t /*location*/,
488 int32_t message_code,
489 const char* layer_prefix,
490 const char* message,
491 void* /*user_data*/) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800492 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500493 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
Jesse Hall715b86a2016-01-16 16:34:29 -0800494 } else if (flags & VK_DEBUG_REPORT_WARN_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500495 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
496 }
Michael Lentineeb970862015-10-15 12:42:22 -0500497 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500498}
499
Jesse Hall06193802015-12-03 16:12:51 -0800500VkResult Noop() {
Michael Lentine03c64b02015-08-26 18:27:26 -0500501 return VK_SUCCESS;
502}
503
Jesse Hall1f91d392015-12-11 16:28:44 -0800504} // anonymous namespace
505
506namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500507
Jesse Hall04f4f472015-08-16 19:51:04 -0700508// -----------------------------------------------------------------------------
509// "Bottom" functions. These are called at the end of the instance dispatch
510// chain.
511
Jesse Hall1f91d392015-12-11 16:28:44 -0800512VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
513 const VkAllocationCallbacks* allocator,
514 VkInstance* vkinstance) {
515 Instance& instance = GetDispatchParent(*vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700516 VkResult result;
517
Jesse Halla7ac76d2016-01-08 22:29:42 -0800518 VkInstanceCreateInfo driver_create_info = *create_info;
519 driver_create_info.enabledLayerCount = 0;
520 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800521
522 InstanceExtensionSet enabled_extensions;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800523 driver_create_info.enabledExtensionCount = 0;
524 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall715b86a2016-01-16 16:34:29 -0800525 size_t max_names =
526 std::min(static_cast<size_t>(create_info->enabledExtensionCount),
527 g_driver_instance_extensions.count());
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800528 if (max_names > 0) {
529 const char** names =
530 static_cast<const char**>(alloca(max_names * sizeof(char*)));
531 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800532 const char* name = create_info->ppEnabledExtensionNames[i];
533 InstanceExtension id = InstanceExtensionFromName(name);
534 if (id != kInstanceExtensionCount) {
535 if (g_driver_instance_extensions[id]) {
536 names[driver_create_info.enabledExtensionCount++] = name;
537 enabled_extensions.set(id);
538 continue;
539 }
540 if (id == kKHR_surface || id == kKHR_android_surface ||
541 id == kEXT_debug_report) {
542 enabled_extensions.set(id);
543 continue;
544 }
545 }
546
547 bool supported = false;
548 for (const auto& layer : instance.active_layers) {
549 if (layer.SupportsExtension(name))
550 supported = true;
551 }
552 if (!supported) {
553 ALOGE(
554 "requested instance extension '%s' not supported by "
555 "loader, driver, or any active layers",
556 name);
557 DestroyInstance_Bottom(instance.handle, allocator);
558 return VK_ERROR_EXTENSION_NOT_PRESENT;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800559 }
560 }
561 driver_create_info.ppEnabledExtensionNames = names;
562 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800563
564 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Jesse Hall1f91d392015-12-11 16:28:44 -0800565 &instance.drv.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700566 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800567 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700568 return result;
569 }
570
Jesse Hall1f91d392015-12-11 16:28:44 -0800571 if (!LoadDriverDispatchTable(instance.drv.instance,
572 g_hwdevice->GetInstanceProcAddr,
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800573 enabled_extensions, instance.drv.dispatch)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800574 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700575 return VK_ERROR_INITIALIZATION_FAILED;
576 }
577
Jesse Hall1f91d392015-12-11 16:28:44 -0800578 hwvulkan_dispatch_t* drv_dispatch =
579 reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
580 if (drv_dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
581 // Skip setting drv_dispatch->vtbl, since we never call through it;
582 // we go through instance.drv.dispatch instead.
Jesse Hall04f4f472015-08-16 19:51:04 -0700583 } else {
584 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800585 drv_dispatch->magic);
586 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700587 return VK_ERROR_INITIALIZATION_FAILED;
588 }
589
590 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800591 result = instance.drv.dispatch.EnumeratePhysicalDevices(
592 instance.drv.instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700593 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800594 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700595 return VK_ERROR_INITIALIZATION_FAILED;
596 }
597 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800598 result = instance.drv.dispatch.EnumeratePhysicalDevices(
599 instance.drv.instance, &num_physical_devices,
600 instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700601 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800602 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700603 return VK_ERROR_INITIALIZATION_FAILED;
604 }
Jesse Hallb1471272016-01-17 21:36:58 -0800605
606 Vector<VkExtensionProperties> extensions(
607 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700608 for (uint32_t i = 0; i < num_physical_devices; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800609 hwvulkan_dispatch_t* pdev_dispatch =
610 reinterpret_cast<hwvulkan_dispatch_t*>(
611 instance.physical_devices[i]);
612 if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700613 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800614 pdev_dispatch->magic);
615 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700616 return VK_ERROR_INITIALIZATION_FAILED;
617 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800618 pdev_dispatch->vtbl = instance.dispatch_ptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800619
620 uint32_t count;
621 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
622 instance.physical_devices[i], nullptr, &count, nullptr)) !=
623 VK_SUCCESS) {
624 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
625 result);
626 continue;
627 }
628 extensions.resize(count);
629 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
630 instance.physical_devices[i], nullptr, &count,
631 extensions.data())) != VK_SUCCESS) {
632 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
633 result);
634 continue;
635 }
636 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
637 for (const auto& extension : extensions) {
638 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
639 DeviceExtension id =
640 DeviceExtensionFromName(extension.extensionName);
641 if (id == kDeviceExtensionCount) {
642 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
643 extension.extensionName);
644 } else {
645 instance.physical_device_driver_extensions[i].set(id);
646 }
647 }
648 // Ignore driver attempts to support loader extensions
649 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700650 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800651 instance.drv.num_physical_devices = num_physical_devices;
Jesse Hall1f91d392015-12-11 16:28:44 -0800652 instance.num_physical_devices = instance.drv.num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800653
Jesse Hall04f4f472015-08-16 19:51:04 -0700654 return VK_SUCCESS;
655}
656
Jesse Hall1f91d392015-12-11 16:28:44 -0800657PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) {
658 PFN_vkVoidFunction pfn;
659 if ((pfn = GetLoaderBottomProcAddr(name)))
660 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -0800661 return nullptr;
662}
663
664VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
665 uint32_t* pdev_count,
666 VkPhysicalDevice* pdevs) {
667 Instance& instance = GetDispatchParent(vkinstance);
668 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700669 if (pdevs) {
670 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800671 std::copy(instance.physical_devices, instance.physical_devices + count,
672 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700673 }
674 *pdev_count = count;
675 return VK_SUCCESS;
676}
677
Jesse Hall1f91d392015-12-11 16:28:44 -0800678void GetPhysicalDeviceProperties_Bottom(
679 VkPhysicalDevice pdev,
680 VkPhysicalDeviceProperties* properties) {
681 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
682 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700683}
684
Jesse Hall1f91d392015-12-11 16:28:44 -0800685void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
686 VkPhysicalDeviceFeatures* features) {
687 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
688 features);
689}
690
691void GetPhysicalDeviceMemoryProperties_Bottom(
692 VkPhysicalDevice pdev,
693 VkPhysicalDeviceMemoryProperties* properties) {
694 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
695 pdev, properties);
696}
697
698void GetPhysicalDeviceQueueFamilyProperties_Bottom(
699 VkPhysicalDevice pdev,
700 uint32_t* pCount,
701 VkQueueFamilyProperties* properties) {
702 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
703 pdev, pCount, properties);
704}
705
706void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
707 VkFormat format,
708 VkFormatProperties* properties) {
709 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700710 pdev, format, properties);
711}
712
Jesse Hall1f91d392015-12-11 16:28:44 -0800713VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700714 VkPhysicalDevice pdev,
715 VkFormat format,
716 VkImageType type,
717 VkImageTiling tiling,
718 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700719 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700720 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800721 return GetDispatchParent(pdev)
722 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800723 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700724}
725
Jesse Hall1f91d392015-12-11 16:28:44 -0800726void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700727 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800728 VkFormat format,
729 VkImageType type,
730 VkSampleCountFlagBits samples,
731 VkImageUsageFlags usage,
732 VkImageTiling tiling,
733 uint32_t* properties_count,
734 VkSparseImageFormatProperties* properties) {
735 GetDispatchParent(pdev)
736 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
737 pdev, format, type, samples, usage, tiling, properties_count,
738 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700739}
740
Jesse Halle1b12782015-11-30 11:27:32 -0800741VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800742VkResult EnumerateDeviceExtensionProperties_Bottom(
Jesse Hallb1471272016-01-17 21:36:58 -0800743 VkPhysicalDevice gpu,
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800744 const char* layer_name,
Jesse Hall1f91d392015-12-11 16:28:44 -0800745 uint32_t* properties_count,
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800746 VkExtensionProperties* properties) {
747 const VkExtensionProperties* extensions = nullptr;
748 uint32_t num_extensions = 0;
749 if (layer_name) {
750 GetDeviceLayerExtensions(layer_name, &extensions, &num_extensions);
751 } else {
Jesse Hallb1471272016-01-17 21:36:58 -0800752 Instance& instance = GetDispatchParent(gpu);
753 size_t gpu_idx = 0;
754 while (instance.physical_devices[gpu_idx] != gpu)
755 gpu_idx++;
756 const DeviceExtensionSet driver_extensions =
757 instance.physical_device_driver_extensions[gpu_idx];
758
759 // We only support VK_KHR_swapchain if the GPU supports
760 // VK_ANDROID_native_buffer
761 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
762 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
763 if (driver_extensions[kANDROID_native_buffer]) {
764 available[num_extensions++] = VkExtensionProperties{
765 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
766 }
767
768 // TODO(jessehall): We need to also enumerate extensions supported by
769 // implicitly-enabled layers. Currently we don't have that list of
770 // layers until instance creation.
771 extensions = available;
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800772 }
773
774 if (!properties || *properties_count > num_extensions)
775 *properties_count = num_extensions;
776 if (properties)
777 std::copy(extensions, extensions + *properties_count, properties);
778 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700779}
780
Jesse Halle1b12782015-11-30 11:27:32 -0800781VKAPI_ATTR
Jesse Hall80523e22016-01-06 16:47:54 -0800782VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800783 uint32_t* properties_count,
Jesse Hallaa410942016-01-17 13:07:10 -0800784 VkLayerProperties* properties) {
785 uint32_t layer_count =
786 EnumerateDeviceLayers(properties ? *properties_count : 0, properties);
787 if (!properties || *properties_count > layer_count)
788 *properties_count = layer_count;
789 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800790}
791
792VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800793VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800794 const VkDeviceCreateInfo* create_info,
795 const VkAllocationCallbacks* allocator,
796 VkDevice* device_out) {
Jesse Hallb1471272016-01-17 21:36:58 -0800797 Instance& instance = GetDispatchParent(gpu);
Jesse Hall04f4f472015-08-16 19:51:04 -0700798 VkResult result;
799
Jesse Hall03b6fe12015-11-24 12:44:21 -0800800 if (!allocator) {
801 if (instance.alloc)
802 allocator = instance.alloc;
803 else
804 allocator = &kDefaultAllocCallbacks;
805 }
806
Jesse Hall3fbc8562015-11-29 22:10:52 -0800807 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
808 alignof(Device),
809 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700810 if (!mem)
811 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500812 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700813
Jesse Hall9a16f972015-10-28 15:59:53 -0700814 result = ActivateAllLayers(create_info, &instance, device);
815 if (result != VK_SUCCESS) {
816 DestroyDevice(device);
817 return result;
818 }
819
Jesse Hallb1471272016-01-17 21:36:58 -0800820 size_t gpu_idx = 0;
821 while (instance.physical_devices[gpu_idx] != gpu)
822 gpu_idx++;
823
824 uint32_t num_driver_extensions = 0;
825 const char** driver_extensions = static_cast<const char**>(
826 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
827 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
828 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800829 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800830 if (id != kDeviceExtensionCount) {
831 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
832 driver_extensions[num_driver_extensions++] = name;
833 continue;
834 }
835 if (id == kKHR_swapchain &&
836 instance.physical_device_driver_extensions
837 [gpu_idx][kANDROID_native_buffer]) {
838 driver_extensions[num_driver_extensions++] =
839 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
840 continue;
841 }
Jesse Hallb1471272016-01-17 21:36:58 -0800842 }
Jesse Hallb1471272016-01-17 21:36:58 -0800843 bool supported = false;
844 for (const auto& layer : device->active_layers) {
845 if (layer.SupportsExtension(name))
846 supported = true;
847 }
848 if (!supported) {
849 ALOGE(
Jesse Hallae3b70d2016-01-17 22:05:29 -0800850 "requested device extension '%s' not supported by loader, "
851 "driver, or any active layers",
Jesse Hallb1471272016-01-17 21:36:58 -0800852 name);
853 DestroyDevice(device);
854 return VK_ERROR_EXTENSION_NOT_PRESENT;
855 }
856 }
857
Jesse Halla7ac76d2016-01-08 22:29:42 -0800858 VkDeviceCreateInfo driver_create_info = *create_info;
859 driver_create_info.enabledLayerCount = 0;
860 driver_create_info.ppEnabledLayerNames = nullptr;
861 // TODO(jessehall): As soon as we enumerate device extensions supported by
862 // the driver, we need to filter the requested extension list to those
863 // supported by the driver here. Also, add the VK_ANDROID_native_buffer
864 // extension to the list iff the VK_KHR_swapchain extension was requested,
865 // instead of adding it unconditionally like we do now.
Jesse Hallb1471272016-01-17 21:36:58 -0800866 driver_create_info.enabledExtensionCount = num_driver_extensions;
867 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800868
Jesse Hall04f4f472015-08-16 19:51:04 -0700869 VkDevice drv_device;
Jesse Hallb1471272016-01-17 21:36:58 -0800870 result = instance.drv.dispatch.CreateDevice(gpu, &driver_create_info,
871 allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700872 if (result != VK_SUCCESS) {
873 DestroyDevice(device);
874 return result;
875 }
876
Jesse Hall1f91d392015-12-11 16:28:44 -0800877 hwvulkan_dispatch_t* drv_dispatch =
Jesse Hall04f4f472015-08-16 19:51:04 -0700878 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800879 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
880 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
881 drv_dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500882 PFN_vkDestroyDevice destroy_device =
883 reinterpret_cast<PFN_vkDestroyDevice>(
Jesse Hall1f91d392015-12-11 16:28:44 -0800884 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
885 "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800886 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700887 DestroyDevice(device);
888 return VK_ERROR_INITIALIZATION_FAILED;
889 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800890 drv_dispatch->vtbl = &device->dispatch;
891 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
892 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
893 "vkGetDeviceProcAddr"));
Jesse Hall04f4f472015-08-16 19:51:04 -0700894
Michael Lentine03c64b02015-08-26 18:27:26 -0500895 void* base_object = static_cast<void*>(drv_device);
896 void* next_object = base_object;
897 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800898 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetDeviceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500899 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500900 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500901 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
902
903 for (size_t i = elem_list.size(); i > 0; i--) {
904 size_t idx = i - 1;
905 next_element = &elem_list[idx];
906 next_element->get_proc_addr =
907 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
908 next_element->base_object = base_object;
909 next_element->next_element = next_object;
910 next_object = static_cast<void*>(next_element);
911
Jesse Hall80523e22016-01-06 16:47:54 -0800912 next_get_proc_addr = device->active_layers[idx].GetGetDeviceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500913 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800914 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500915 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800916 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500917 }
918 }
919
Jesse Hall1f91d392015-12-11 16:28:44 -0800920 // This is the magic call that initializes all the layer devices and
921 // allows them to create their device_handle -> device_data mapping.
922 next_get_proc_addr(static_cast<VkDevice>(next_object),
923 "vkGetDeviceProcAddr");
924
925 // We must create all the layer devices *before* retrieving the device
926 // procaddrs, so that the layers know which extensions are enabled and
927 // therefore which functions to return procaddrs for.
928 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
929 next_get_proc_addr(drv_device, "vkCreateDevice"));
Jesse Hallb1471272016-01-17 21:36:58 -0800930 create_device(gpu, create_info, allocator, &drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800931
932 if (!LoadDeviceDispatchTable(static_cast<VkDevice>(base_object),
933 next_get_proc_addr, device->dispatch)) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500934 DestroyDevice(device);
935 return VK_ERROR_INITIALIZATION_FAILED;
936 }
937
Jesse Hall1f91d392015-12-11 16:28:44 -0800938 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700939 return VK_SUCCESS;
940}
941
Jesse Hall1f91d392015-12-11 16:28:44 -0800942void DestroyInstance_Bottom(VkInstance vkinstance,
943 const VkAllocationCallbacks* allocator) {
944 Instance& instance = GetDispatchParent(vkinstance);
945
946 // These checks allow us to call DestroyInstance_Bottom from any error
947 // path in CreateInstance_Bottom, before the driver instance is fully
948 // initialized.
949 if (instance.drv.instance != VK_NULL_HANDLE &&
950 instance.drv.dispatch.DestroyInstance) {
951 instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
952 }
953 if (instance.message) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800954 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback;
955 destroy_debug_report_callback =
956 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
957 vkGetInstanceProcAddr(vkinstance,
958 "vkDestroyDebugReportCallbackEXT"));
959 destroy_debug_report_callback(vkinstance, instance.message, allocator);
Jesse Hall1f91d392015-12-11 16:28:44 -0800960 }
Jesse Hall80523e22016-01-06 16:47:54 -0800961 instance.active_layers.clear();
Jesse Hall1f91d392015-12-11 16:28:44 -0800962 const VkAllocationCallbacks* alloc = instance.alloc;
963 instance.~Instance();
964 alloc->pfnFree(alloc->pUserData, &instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700965}
966
Jesse Hall1f91d392015-12-11 16:28:44 -0800967PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
968 const char* name) {
969 if (strcmp(name, "vkCreateDevice") == 0) {
970 // TODO(jessehall): Blegh, having this here is disgusting. The current
971 // layer init process can't call through the instance dispatch table's
972 // vkCreateDevice, because that goes through the instance layers rather
973 // than through the device layers. So we need to be able to get the
974 // vkCreateDevice pointer through the *device* layer chain.
975 //
976 // Because we've already created the driver device before calling
977 // through the layer vkCreateDevice functions, the loader bottom proc
978 // is a no-op.
Michael Lentine03c64b02015-08-26 18:27:26 -0500979 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
980 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800981
982 // VK_ANDROID_native_buffer should be hidden from applications and layers.
983 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
984 PFN_vkVoidFunction pfn;
985 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
986 strcmp(name, "vkAcquireImageANDROID") == 0 ||
987 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
988 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500989 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800990 if ((pfn = GetLoaderBottomProcAddr(name)))
991 return pfn;
992 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700993}
994
Jesse Hall04f4f472015-08-16 19:51:04 -0700995// -----------------------------------------------------------------------------
Jesse Hall1f91d392015-12-11 16:28:44 -0800996// Loader top functions. These are called directly from the loader entry
997// points or from the application (via vkGetInstanceProcAddr) without going
998// through a dispatch table.
Jesse Hall04f4f472015-08-16 19:51:04 -0700999
Jesse Hall1f91d392015-12-11 16:28:44 -08001000VkResult EnumerateInstanceExtensionProperties_Top(
Jesse Hall80523e22016-01-06 16:47:54 -08001001 const char* layer_name,
1002 uint32_t* properties_count,
1003 VkExtensionProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001004 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001005 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001006
Jesse Hall80523e22016-01-06 16:47:54 -08001007 const VkExtensionProperties* extensions = nullptr;
1008 uint32_t num_extensions = 0;
1009 if (layer_name) {
Jesse Hallaa410942016-01-17 13:07:10 -08001010 GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions);
Jesse Hall80523e22016-01-06 16:47:54 -08001011 } else {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -08001012 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
1013 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
1014 available[num_extensions++] = VkExtensionProperties{
1015 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
1016 available[num_extensions++] =
1017 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
1018 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
1019 if (g_driver_instance_extensions[kEXT_debug_report]) {
1020 available[num_extensions++] =
1021 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
1022 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
1023 }
Jesse Hall80523e22016-01-06 16:47:54 -08001024 // TODO(jessehall): We need to also enumerate extensions supported by
1025 // implicitly-enabled layers. Currently we don't have that list of
1026 // layers until instance creation.
Jesse Hall6bd5dfa2016-01-16 17:13:30 -08001027 extensions = available;
Jesse Hall80523e22016-01-06 16:47:54 -08001028 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001029
Jesse Hall80523e22016-01-06 16:47:54 -08001030 if (!properties || *properties_count > num_extensions)
1031 *properties_count = num_extensions;
1032 if (properties)
1033 std::copy(extensions, extensions + *properties_count, properties);
1034 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001035}
1036
Jesse Hall80523e22016-01-06 16:47:54 -08001037VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
1038 VkLayerProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001039 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001040 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001041
Jesse Hall80523e22016-01-06 16:47:54 -08001042 uint32_t layer_count =
Jesse Hallaa410942016-01-17 13:07:10 -08001043 EnumerateInstanceLayers(properties ? *properties_count : 0, properties);
Jesse Hall80523e22016-01-06 16:47:54 -08001044 if (!properties || *properties_count > layer_count)
1045 *properties_count = layer_count;
1046 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001047}
1048
Jesse Hall1f91d392015-12-11 16:28:44 -08001049VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
1050 const VkAllocationCallbacks* allocator,
1051 VkInstance* instance_out) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001052 VkResult result;
1053
1054 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001055 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001056
Jesse Hall03b6fe12015-11-24 12:44:21 -08001057 if (!allocator)
1058 allocator = &kDefaultAllocCallbacks;
1059
Jesse Hall04f4f472015-08-16 19:51:04 -07001060 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -07001061 create_info = &local_create_info;
1062
Jesse Hall3fbc8562015-11-29 22:10:52 -08001063 void* instance_mem = allocator->pfnAllocation(
1064 allocator->pUserData, sizeof(Instance), alignof(Instance),
1065 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -07001066 if (!instance_mem)
1067 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001068 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -07001069
Jesse Hall9a16f972015-10-28 15:59:53 -07001070 result = ActivateAllLayers(create_info, instance, instance);
1071 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001072 DestroyInstance_Bottom(instance->handle, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -07001073 return result;
1074 }
Michael Lentine03c64b02015-08-26 18:27:26 -05001075
Jesse Hall1f91d392015-12-11 16:28:44 -08001076 void* base_object = static_cast<void*>(instance->handle);
Michael Lentine03c64b02015-08-26 18:27:26 -05001077 void* next_object = base_object;
1078 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -08001079 PFN_vkGetInstanceProcAddr next_get_proc_addr = GetInstanceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -05001080 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -07001081 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -05001082 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
1083
1084 for (size_t i = elem_list.size(); i > 0; i--) {
1085 size_t idx = i - 1;
1086 next_element = &elem_list[idx];
1087 next_element->get_proc_addr =
1088 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
1089 next_element->base_object = base_object;
1090 next_element->next_element = next_object;
1091 next_object = static_cast<void*>(next_element);
1092
Jesse Hall80523e22016-01-06 16:47:54 -08001093 next_get_proc_addr =
1094 instance->active_layers[idx].GetGetInstanceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -05001095 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -08001096 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -05001097 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -08001098 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -05001099 }
1100 }
1101
Jesse Hall1f91d392015-12-11 16:28:44 -08001102 // This is the magic call that initializes all the layer instances and
1103 // allows them to create their instance_handle -> instance_data mapping.
1104 next_get_proc_addr(static_cast<VkInstance>(next_object),
1105 "vkGetInstanceProcAddr");
1106
1107 if (!LoadInstanceDispatchTable(static_cast<VkInstance>(base_object),
1108 next_get_proc_addr, instance->dispatch)) {
1109 DestroyInstance_Bottom(instance->handle, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -05001110 return VK_ERROR_INITIALIZATION_FAILED;
1111 }
1112
Michael Lentine950bb4f2015-09-14 13:26:30 -05001113 // Force enable callback extension if required
Jesse Hall21597662015-12-18 13:48:24 -08001114 bool enable_callback = false;
1115 bool enable_logging = false;
1116 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
1117 enable_callback =
1118 property_get_bool("debug.vulkan.enable_callback", false);
1119 enable_logging = enable_callback;
1120 if (enable_callback) {
1121 enable_callback = AddExtensionToCreateInfo(
Jesse Hall715b86a2016-01-16 16:34:29 -08001122 local_create_info, "VK_EXT_debug_report", instance->alloc);
Jesse Hall21597662015-12-18 13:48:24 -08001123 }
Michael Lentine950bb4f2015-09-14 13:26:30 -05001124 }
1125
Jesse Hallc55fa942016-01-17 21:44:16 -08001126 VkInstance handle = instance->handle;
Jesse Hall1f91d392015-12-11 16:28:44 -08001127 PFN_vkCreateInstance create_instance =
1128 reinterpret_cast<PFN_vkCreateInstance>(
1129 next_get_proc_addr(instance->handle, "vkCreateInstance"));
Jesse Hallc55fa942016-01-17 21:44:16 -08001130 result = create_instance(create_info, allocator, &handle);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001131 if (enable_callback)
1132 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall993849c2016-01-18 00:59:45 -08001133 if (result >= 0) {
1134 *instance_out = instance->handle;
1135 } else {
Jesse Hall04f4f472015-08-16 19:51:04 -07001136 // For every layer, including the loader top and bottom layers:
1137 // - If a call to the next CreateInstance fails, the layer must clean
1138 // up anything it has successfully done so far, and propagate the
1139 // error upwards.
1140 // - If a layer successfully calls the next layer's CreateInstance, and
1141 // afterwards must fail for some reason, it must call the next layer's
1142 // DestroyInstance before returning.
1143 // - The layer must not call the next layer's DestroyInstance if that
1144 // layer's CreateInstance wasn't called, or returned failure.
1145
Jesse Hall1f91d392015-12-11 16:28:44 -08001146 // On failure, CreateInstance_Bottom frees the instance struct, so it's
Jesse Hall04f4f472015-08-16 19:51:04 -07001147 // already gone at this point. Nothing to do.
1148 }
1149
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001150 if (enable_logging) {
Jesse Hall715b86a2016-01-16 16:34:29 -08001151 const VkDebugReportCallbackCreateInfoEXT callback_create_info = {
1152 .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
1153 .flags =
1154 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT,
1155 .pfnCallback = LogDebugMessageCallback,
1156 };
1157 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
1158 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
1159 GetInstanceProcAddr_Top(instance->handle,
1160 "vkCreateDebugReportCallbackEXT"));
1161 create_debug_report_callback(instance->handle, &callback_create_info,
1162 allocator, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001163 }
1164
Jesse Hall04f4f472015-08-16 19:51:04 -07001165 return result;
1166}
1167
Jesse Hall1f91d392015-12-11 16:28:44 -08001168PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
1169 const char* name) {
1170 // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
1171 if (!vkinstance)
1172 return GetLoaderGlobalProcAddr(name);
1173
1174 const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
1175 PFN_vkVoidFunction pfn;
1176 // Always go through the loader-top function if there is one.
1177 if ((pfn = GetLoaderTopProcAddr(name)))
1178 return pfn;
1179 // Otherwise, look up the handler in the instance dispatch table
1180 if ((pfn = GetDispatchProcAddr(dispatch, name)))
1181 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -08001182 // Anything not handled already must be a device-dispatched function
1183 // without a loader-top. We must return a function that will dispatch based
1184 // on the dispatchable object parameter -- which is exactly what the
1185 // exported functions do. So just return them here.
1186 return GetLoaderExportProcAddr(name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001187}
1188
Jesse Hall1f91d392015-12-11 16:28:44 -08001189void DestroyInstance_Top(VkInstance instance,
1190 const VkAllocationCallbacks* allocator) {
1191 if (!instance)
1192 return;
1193 GetDispatchTable(instance).DestroyInstance(instance, allocator);
1194}
1195
1196PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
1197 PFN_vkVoidFunction pfn;
Jesse Hall04f4f472015-08-16 19:51:04 -07001198 if (!device)
Jesse Hall1f91d392015-12-11 16:28:44 -08001199 return nullptr;
1200 if ((pfn = GetLoaderTopProcAddr(name)))
1201 return pfn;
1202 return GetDispatchProcAddr(GetDispatchTable(device), name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001203}
1204
Jesse Hall1f91d392015-12-11 16:28:44 -08001205void GetDeviceQueue_Top(VkDevice vkdevice,
1206 uint32_t family,
1207 uint32_t index,
1208 VkQueue* queue_out) {
1209 const auto& table = GetDispatchTable(vkdevice);
1210 table.GetDeviceQueue(vkdevice, family, index, queue_out);
1211 hwvulkan_dispatch_t* queue_dispatch =
1212 reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
1213 if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
1214 queue_dispatch->vtbl != &table)
1215 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
1216 queue_dispatch->magic);
1217 queue_dispatch->vtbl = &table;
Jesse Hall04f4f472015-08-16 19:51:04 -07001218}
1219
Jesse Hall1f91d392015-12-11 16:28:44 -08001220VkResult AllocateCommandBuffers_Top(
1221 VkDevice vkdevice,
1222 const VkCommandBufferAllocateInfo* alloc_info,
1223 VkCommandBuffer* cmdbufs) {
1224 const auto& table = GetDispatchTable(vkdevice);
1225 VkResult result =
1226 table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001227 if (result != VK_SUCCESS)
1228 return result;
Jesse Hall3dd678a2016-01-08 21:52:01 -08001229 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001230 hwvulkan_dispatch_t* cmdbuf_dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001231 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hall1f91d392015-12-11 16:28:44 -08001232 ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001233 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -08001234 cmdbuf_dispatch->magic);
1235 cmdbuf_dispatch->vtbl = &table;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001236 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001237 return VK_SUCCESS;
1238}
1239
Jesse Hall1f91d392015-12-11 16:28:44 -08001240void DestroyDevice_Top(VkDevice vkdevice,
1241 const VkAllocationCallbacks* /*allocator*/) {
1242 if (!vkdevice)
1243 return;
1244 Device& device = GetDispatchParent(vkdevice);
Jesse Hall1f91d392015-12-11 16:28:44 -08001245 device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
1246 DestroyDevice(&device);
Jesse Hall04f4f472015-08-16 19:51:04 -07001247}
1248
Jesse Hall1f91d392015-12-11 16:28:44 -08001249// -----------------------------------------------------------------------------
1250
1251const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
1252 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001253}
1254
Jesse Hall1f91d392015-12-11 16:28:44 -08001255const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
1256 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001257}
1258
Jesse Hall715b86a2016-01-16 16:34:29 -08001259VkInstance GetDriverInstance(VkInstance instance) {
1260 return GetDispatchParent(instance).drv.instance;
1261}
1262
1263const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
1264 return GetDispatchParent(instance).drv.dispatch;
1265}
1266
Jesse Hall1f91d392015-12-11 16:28:44 -08001267const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
1268 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001269}
1270
Jesse Hall1f91d392015-12-11 16:28:44 -08001271const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
1272 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001273}
1274
Jesse Hall715b86a2016-01-16 16:34:29 -08001275DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
1276 return GetDispatchParent(instance).debug_report_callbacks;
1277}
1278
Jesse Hall04f4f472015-08-16 19:51:04 -07001279} // namespace vulkan