blob: 618bd930ccaa6095e9a0d6fa7047150d2e96770a [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];
271
Jesse Hall80523e22016-01-06 16:47:54 -0800272 Vector<LayerRef> active_layers;
Jesse Hall715b86a2016-01-16 16:34:29 -0800273 VkDebugReportCallbackEXT message;
274 DebugReportCallbackList debug_report_callbacks;
Jesse Hall1f91d392015-12-11 16:28:44 -0800275
276 struct {
277 VkInstance instance;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800278 InstanceExtensionSet supported_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800279 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 Hall80523e22016-01-06 16:47:54 -0800347bool ActivateLayer(TObject* object, const char* name) {
348 LayerRef layer(GetLayerRef(name));
349 if (!layer)
350 return false;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500351 if (std::find(object->active_layers.begin(), object->active_layers.end(),
Jesse Hall80523e22016-01-06 16:47:54 -0800352 layer) == object->active_layers.end())
353 object->active_layers.push_back(std::move(layer));
354 ALOGV("activated layer '%s'", name);
355 return true;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500356}
357
Michael Lentine9da191b2015-10-13 11:08:45 -0500358struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500359 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500360 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500361};
362
Michael Lentine9da191b2015-10-13 11:08:45 -0500363void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500364 const char* value,
365 void* data) {
366 const char prefix[] = "debug.vulkan.layer.";
367 const size_t prefixlen = sizeof(prefix) - 1;
368 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
369 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500370 const char* number_str = name + prefixlen;
371 long layer_number = strtol(number_str, nullptr, 10);
372 if (layer_number <= 0 || layer_number == LONG_MAX) {
373 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
374 number_str);
375 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500376 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500377 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
378 Vector<String>* layer_names = instance_names_pair->layer_names;
379 Instance* instance = instance_names_pair->instance;
380 size_t layer_size = static_cast<size_t>(layer_number);
381 if (layer_size > layer_names->size()) {
382 layer_names->resize(layer_size,
383 String(CallbackAllocator<char>(instance->alloc)));
384 }
385 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500386}
387
388template <class TInfo, class TObject>
Jesse Hall1f91d392015-12-11 16:28:44 -0800389VkResult ActivateAllLayers(TInfo create_info,
390 Instance* instance,
391 TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500392 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
393 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
394 "Cannot activate layers for unknown object %p", object);
395 CallbackAllocator<char> string_allocator(instance->alloc);
396 // Load system layers
Jesse Hall21597662015-12-18 13:48:24 -0800397 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500398 char layer_prop[PROPERTY_VALUE_MAX];
399 property_get("debug.vulkan.layers", layer_prop, "");
400 String layer_name(string_allocator);
401 String layer_prop_str(layer_prop, string_allocator);
402 size_t end, start = 0;
403 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
404 layer_name = layer_prop_str.substr(start, end - start);
Jesse Hall80523e22016-01-06 16:47:54 -0800405 ActivateLayer(object, layer_name.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500406 start = end + 1;
407 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500408 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
409 InstanceNamesPair instance_names_pair = {.instance = instance,
410 .layer_names = &layer_names};
411 property_list(SetLayerNamesFromProperty,
412 static_cast<void*>(&instance_names_pair));
413 for (auto layer_name_element : layer_names) {
Jesse Hall80523e22016-01-06 16:47:54 -0800414 ActivateLayer(object, layer_name_element.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500415 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500416 }
417 // Load app layers
Jesse Hall3dd678a2016-01-08 21:52:01 -0800418 for (uint32_t i = 0; i < create_info->enabledLayerCount; ++i) {
Jesse Hall80523e22016-01-06 16:47:54 -0800419 if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700420 ALOGE("requested %s layer '%s' not present",
Jesse Hall1f91d392015-12-11 16:28:44 -0800421 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
422 ? "instance"
423 : "device",
Jesse Hall80523e22016-01-06 16:47:54 -0800424 create_info->ppEnabledLayerNames[i]);
Jesse Hall9a16f972015-10-28 15:59:53 -0700425 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500426 }
427 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700428 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500429}
430
431template <class TCreateInfo>
432bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
433 const char* extension_name,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800434 const VkAllocationCallbacks* alloc) {
Jesse Hall3dd678a2016-01-08 21:52:01 -0800435 for (uint32_t i = 0; i < local_create_info.enabledExtensionCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500436 if (!strcmp(extension_name,
437 local_create_info.ppEnabledExtensionNames[i])) {
438 return false;
439 }
440 }
Jesse Hall3dd678a2016-01-08 21:52:01 -0800441 uint32_t extension_count = local_create_info.enabledExtensionCount;
442 local_create_info.enabledExtensionCount++;
Jesse Hall3fbc8562015-11-29 22:10:52 -0800443 void* mem = alloc->pfnAllocation(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800444 alloc->pUserData,
Jesse Hall3dd678a2016-01-08 21:52:01 -0800445 local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*),
446 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500447 if (mem) {
448 const char** enabled_extensions = static_cast<const char**>(mem);
449 for (uint32_t i = 0; i < extension_count; ++i) {
450 enabled_extensions[i] =
451 local_create_info.ppEnabledExtensionNames[i];
452 }
453 enabled_extensions[extension_count] = extension_name;
454 local_create_info.ppEnabledExtensionNames = enabled_extensions;
455 } else {
456 ALOGW("%s extension cannot be enabled: memory allocation failed",
457 extension_name);
Jesse Hall3dd678a2016-01-08 21:52:01 -0800458 local_create_info.enabledExtensionCount--;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500459 return false;
460 }
461 return true;
462}
463
464template <class T>
465void FreeAllocatedCreateInfo(T& local_create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800466 const VkAllocationCallbacks* alloc) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500467 alloc->pfnFree(
468 alloc->pUserData,
469 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
470}
471
Jesse Halle1b12782015-11-30 11:27:32 -0800472VKAPI_ATTR
Jesse Hall715b86a2016-01-16 16:34:29 -0800473VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags,
474 VkDebugReportObjectTypeEXT /*objectType*/,
475 uint64_t /*object*/,
Michael Lentineeb970862015-10-15 12:42:22 -0500476 size_t /*location*/,
477 int32_t message_code,
478 const char* layer_prefix,
479 const char* message,
480 void* /*user_data*/) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800481 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500482 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
Jesse Hall715b86a2016-01-16 16:34:29 -0800483 } else if (flags & VK_DEBUG_REPORT_WARN_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500484 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
485 }
Michael Lentineeb970862015-10-15 12:42:22 -0500486 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500487}
488
Jesse Hall06193802015-12-03 16:12:51 -0800489VkResult Noop() {
Michael Lentine03c64b02015-08-26 18:27:26 -0500490 return VK_SUCCESS;
491}
492
Jesse Hall1f91d392015-12-11 16:28:44 -0800493} // anonymous namespace
494
495namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500496
Jesse Hall04f4f472015-08-16 19:51:04 -0700497// -----------------------------------------------------------------------------
498// "Bottom" functions. These are called at the end of the instance dispatch
499// chain.
500
Jesse Hall1f91d392015-12-11 16:28:44 -0800501VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
502 const VkAllocationCallbacks* allocator,
503 VkInstance* vkinstance) {
504 Instance& instance = GetDispatchParent(*vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700505 VkResult result;
506
Jesse Halla7ac76d2016-01-08 22:29:42 -0800507 VkInstanceCreateInfo driver_create_info = *create_info;
508 driver_create_info.enabledLayerCount = 0;
509 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800510
511 InstanceExtensionSet enabled_extensions;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800512 driver_create_info.enabledExtensionCount = 0;
513 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall715b86a2016-01-16 16:34:29 -0800514 size_t max_names =
515 std::min(static_cast<size_t>(create_info->enabledExtensionCount),
516 g_driver_instance_extensions.count());
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800517 if (max_names > 0) {
518 const char** names =
519 static_cast<const char**>(alloca(max_names * sizeof(char*)));
520 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
521 InstanceExtension id = InstanceExtensionFromName(
522 create_info->ppEnabledExtensionNames[i]);
523 if (id != kInstanceExtensionCount &&
524 g_driver_instance_extensions[id]) {
525 names[driver_create_info.enabledExtensionCount++] =
526 create_info->ppEnabledExtensionNames[i];
527 enabled_extensions.set(id);
528 }
529 }
530 driver_create_info.ppEnabledExtensionNames = names;
531 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800532
533 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Jesse Hall1f91d392015-12-11 16:28:44 -0800534 &instance.drv.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700535 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800536 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700537 return result;
538 }
539
Jesse Hall1f91d392015-12-11 16:28:44 -0800540 if (!LoadDriverDispatchTable(instance.drv.instance,
541 g_hwdevice->GetInstanceProcAddr,
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800542 enabled_extensions, instance.drv.dispatch)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800543 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700544 return VK_ERROR_INITIALIZATION_FAILED;
545 }
546
Jesse Hall1f91d392015-12-11 16:28:44 -0800547 hwvulkan_dispatch_t* drv_dispatch =
548 reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
549 if (drv_dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
550 // Skip setting drv_dispatch->vtbl, since we never call through it;
551 // we go through instance.drv.dispatch instead.
Jesse Hall04f4f472015-08-16 19:51:04 -0700552 } else {
553 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800554 drv_dispatch->magic);
555 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700556 return VK_ERROR_INITIALIZATION_FAILED;
557 }
558
559 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800560 result = instance.drv.dispatch.EnumeratePhysicalDevices(
561 instance.drv.instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700562 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800563 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700564 return VK_ERROR_INITIALIZATION_FAILED;
565 }
566 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800567 result = instance.drv.dispatch.EnumeratePhysicalDevices(
568 instance.drv.instance, &num_physical_devices,
569 instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700570 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800571 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700572 return VK_ERROR_INITIALIZATION_FAILED;
573 }
574 for (uint32_t i = 0; i < num_physical_devices; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800575 hwvulkan_dispatch_t* pdev_dispatch =
576 reinterpret_cast<hwvulkan_dispatch_t*>(
577 instance.physical_devices[i]);
578 if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700579 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800580 pdev_dispatch->magic);
581 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700582 return VK_ERROR_INITIALIZATION_FAILED;
583 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800584 pdev_dispatch->vtbl = instance.dispatch_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700585 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800586 instance.drv.num_physical_devices = num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700587
Jesse Hall1f91d392015-12-11 16:28:44 -0800588 instance.num_physical_devices = instance.drv.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700589 return VK_SUCCESS;
590}
591
Jesse Hall1f91d392015-12-11 16:28:44 -0800592PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) {
593 PFN_vkVoidFunction pfn;
594 if ((pfn = GetLoaderBottomProcAddr(name)))
595 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -0800596 return nullptr;
597}
598
599VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
600 uint32_t* pdev_count,
601 VkPhysicalDevice* pdevs) {
602 Instance& instance = GetDispatchParent(vkinstance);
603 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700604 if (pdevs) {
605 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800606 std::copy(instance.physical_devices, instance.physical_devices + count,
607 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700608 }
609 *pdev_count = count;
610 return VK_SUCCESS;
611}
612
Jesse Hall1f91d392015-12-11 16:28:44 -0800613void GetPhysicalDeviceProperties_Bottom(
614 VkPhysicalDevice pdev,
615 VkPhysicalDeviceProperties* properties) {
616 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
617 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700618}
619
Jesse Hall1f91d392015-12-11 16:28:44 -0800620void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
621 VkPhysicalDeviceFeatures* features) {
622 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
623 features);
624}
625
626void GetPhysicalDeviceMemoryProperties_Bottom(
627 VkPhysicalDevice pdev,
628 VkPhysicalDeviceMemoryProperties* properties) {
629 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
630 pdev, properties);
631}
632
633void GetPhysicalDeviceQueueFamilyProperties_Bottom(
634 VkPhysicalDevice pdev,
635 uint32_t* pCount,
636 VkQueueFamilyProperties* properties) {
637 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
638 pdev, pCount, properties);
639}
640
641void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
642 VkFormat format,
643 VkFormatProperties* properties) {
644 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700645 pdev, format, properties);
646}
647
Jesse Hall1f91d392015-12-11 16:28:44 -0800648VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700649 VkPhysicalDevice pdev,
650 VkFormat format,
651 VkImageType type,
652 VkImageTiling tiling,
653 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700654 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700655 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800656 return GetDispatchParent(pdev)
657 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800658 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700659}
660
Jesse Hall1f91d392015-12-11 16:28:44 -0800661void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700662 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800663 VkFormat format,
664 VkImageType type,
665 VkSampleCountFlagBits samples,
666 VkImageUsageFlags usage,
667 VkImageTiling tiling,
668 uint32_t* properties_count,
669 VkSparseImageFormatProperties* properties) {
670 GetDispatchParent(pdev)
671 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
672 pdev, format, type, samples, usage, tiling, properties_count,
673 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700674}
675
Jesse Halle1b12782015-11-30 11:27:32 -0800676VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800677VkResult EnumerateDeviceExtensionProperties_Bottom(
Jesse Hall80523e22016-01-06 16:47:54 -0800678 VkPhysicalDevice /*pdev*/,
679 const char* /*layer_name*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800680 uint32_t* properties_count,
Jesse Hall80523e22016-01-06 16:47:54 -0800681 VkExtensionProperties* /*properties*/) {
682 // TODO(jessehall): Implement me...
683 *properties_count = 0;
684 return VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700685}
686
Jesse Halle1b12782015-11-30 11:27:32 -0800687VKAPI_ATTR
Jesse Hall80523e22016-01-06 16:47:54 -0800688VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800689 uint32_t* properties_count,
Jesse Hall80523e22016-01-06 16:47:54 -0800690 VkLayerProperties* /*properties*/) {
691 // TODO(jessehall): Implement me...
692 *properties_count = 0;
693 return VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800694}
695
696VKAPI_ATTR
697VkResult CreateDevice_Bottom(VkPhysicalDevice pdev,
698 const VkDeviceCreateInfo* create_info,
699 const VkAllocationCallbacks* allocator,
700 VkDevice* device_out) {
701 Instance& instance = GetDispatchParent(pdev);
Jesse Hall04f4f472015-08-16 19:51:04 -0700702 VkResult result;
703
Jesse Hall03b6fe12015-11-24 12:44:21 -0800704 if (!allocator) {
705 if (instance.alloc)
706 allocator = instance.alloc;
707 else
708 allocator = &kDefaultAllocCallbacks;
709 }
710
Jesse Hall3fbc8562015-11-29 22:10:52 -0800711 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
712 alignof(Device),
713 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700714 if (!mem)
715 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500716 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700717
Jesse Hall9a16f972015-10-28 15:59:53 -0700718 result = ActivateAllLayers(create_info, &instance, device);
719 if (result != VK_SUCCESS) {
720 DestroyDevice(device);
721 return result;
722 }
723
Jesse Halla7ac76d2016-01-08 22:29:42 -0800724 const char* kAndroidNativeBufferExtensionName = "VK_ANDROID_native_buffer";
725 VkDeviceCreateInfo driver_create_info = *create_info;
726 driver_create_info.enabledLayerCount = 0;
727 driver_create_info.ppEnabledLayerNames = nullptr;
728 // TODO(jessehall): As soon as we enumerate device extensions supported by
729 // the driver, we need to filter the requested extension list to those
730 // supported by the driver here. Also, add the VK_ANDROID_native_buffer
731 // extension to the list iff the VK_KHR_swapchain extension was requested,
732 // instead of adding it unconditionally like we do now.
733 driver_create_info.enabledExtensionCount = 1;
734 driver_create_info.ppEnabledExtensionNames = &kAndroidNativeBufferExtensionName;
735
Jesse Hall04f4f472015-08-16 19:51:04 -0700736 VkDevice drv_device;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800737 result = instance.drv.dispatch.CreateDevice(pdev, &driver_create_info, allocator,
Jesse Hall1f91d392015-12-11 16:28:44 -0800738 &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700739 if (result != VK_SUCCESS) {
740 DestroyDevice(device);
741 return result;
742 }
743
Jesse Hall1f91d392015-12-11 16:28:44 -0800744 hwvulkan_dispatch_t* drv_dispatch =
Jesse Hall04f4f472015-08-16 19:51:04 -0700745 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800746 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
747 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
748 drv_dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500749 PFN_vkDestroyDevice destroy_device =
750 reinterpret_cast<PFN_vkDestroyDevice>(
Jesse Hall1f91d392015-12-11 16:28:44 -0800751 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
752 "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800753 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700754 DestroyDevice(device);
755 return VK_ERROR_INITIALIZATION_FAILED;
756 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800757 drv_dispatch->vtbl = &device->dispatch;
758 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
759 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
760 "vkGetDeviceProcAddr"));
Jesse Hall04f4f472015-08-16 19:51:04 -0700761
Michael Lentine03c64b02015-08-26 18:27:26 -0500762 void* base_object = static_cast<void*>(drv_device);
763 void* next_object = base_object;
764 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800765 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetDeviceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500766 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500767 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500768 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
769
770 for (size_t i = elem_list.size(); i > 0; i--) {
771 size_t idx = i - 1;
772 next_element = &elem_list[idx];
773 next_element->get_proc_addr =
774 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
775 next_element->base_object = base_object;
776 next_element->next_element = next_object;
777 next_object = static_cast<void*>(next_element);
778
Jesse Hall80523e22016-01-06 16:47:54 -0800779 next_get_proc_addr = device->active_layers[idx].GetGetDeviceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500780 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800781 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500782 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800783 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500784 }
785 }
786
Jesse Hall1f91d392015-12-11 16:28:44 -0800787 // This is the magic call that initializes all the layer devices and
788 // allows them to create their device_handle -> device_data mapping.
789 next_get_proc_addr(static_cast<VkDevice>(next_object),
790 "vkGetDeviceProcAddr");
791
792 // We must create all the layer devices *before* retrieving the device
793 // procaddrs, so that the layers know which extensions are enabled and
794 // therefore which functions to return procaddrs for.
795 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
796 next_get_proc_addr(drv_device, "vkCreateDevice"));
797 create_device(pdev, create_info, allocator, &drv_device);
798
799 if (!LoadDeviceDispatchTable(static_cast<VkDevice>(base_object),
800 next_get_proc_addr, device->dispatch)) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500801 DestroyDevice(device);
802 return VK_ERROR_INITIALIZATION_FAILED;
803 }
804
Jesse Hall1f91d392015-12-11 16:28:44 -0800805 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700806 return VK_SUCCESS;
807}
808
Jesse Hall1f91d392015-12-11 16:28:44 -0800809void DestroyInstance_Bottom(VkInstance vkinstance,
810 const VkAllocationCallbacks* allocator) {
811 Instance& instance = GetDispatchParent(vkinstance);
812
813 // These checks allow us to call DestroyInstance_Bottom from any error
814 // path in CreateInstance_Bottom, before the driver instance is fully
815 // initialized.
816 if (instance.drv.instance != VK_NULL_HANDLE &&
817 instance.drv.dispatch.DestroyInstance) {
818 instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
819 }
820 if (instance.message) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800821 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback;
822 destroy_debug_report_callback =
823 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
824 vkGetInstanceProcAddr(vkinstance,
825 "vkDestroyDebugReportCallbackEXT"));
826 destroy_debug_report_callback(vkinstance, instance.message, allocator);
Jesse Hall1f91d392015-12-11 16:28:44 -0800827 }
Jesse Hall80523e22016-01-06 16:47:54 -0800828 instance.active_layers.clear();
Jesse Hall1f91d392015-12-11 16:28:44 -0800829 const VkAllocationCallbacks* alloc = instance.alloc;
830 instance.~Instance();
831 alloc->pfnFree(alloc->pUserData, &instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700832}
833
Jesse Hall1f91d392015-12-11 16:28:44 -0800834PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
835 const char* name) {
836 if (strcmp(name, "vkCreateDevice") == 0) {
837 // TODO(jessehall): Blegh, having this here is disgusting. The current
838 // layer init process can't call through the instance dispatch table's
839 // vkCreateDevice, because that goes through the instance layers rather
840 // than through the device layers. So we need to be able to get the
841 // vkCreateDevice pointer through the *device* layer chain.
842 //
843 // Because we've already created the driver device before calling
844 // through the layer vkCreateDevice functions, the loader bottom proc
845 // is a no-op.
Michael Lentine03c64b02015-08-26 18:27:26 -0500846 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
847 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800848
849 // VK_ANDROID_native_buffer should be hidden from applications and layers.
850 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
851 PFN_vkVoidFunction pfn;
852 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
853 strcmp(name, "vkAcquireImageANDROID") == 0 ||
854 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
855 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500856 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800857 if ((pfn = GetLoaderBottomProcAddr(name)))
858 return pfn;
859 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700860}
861
Jesse Hall04f4f472015-08-16 19:51:04 -0700862// -----------------------------------------------------------------------------
Jesse Hall1f91d392015-12-11 16:28:44 -0800863// Loader top functions. These are called directly from the loader entry
864// points or from the application (via vkGetInstanceProcAddr) without going
865// through a dispatch table.
Jesse Hall04f4f472015-08-16 19:51:04 -0700866
Jesse Hall1f91d392015-12-11 16:28:44 -0800867VkResult EnumerateInstanceExtensionProperties_Top(
Jesse Hall80523e22016-01-06 16:47:54 -0800868 const char* layer_name,
869 uint32_t* properties_count,
870 VkExtensionProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700871 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700872 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700873
Jesse Hall80523e22016-01-06 16:47:54 -0800874 const VkExtensionProperties* extensions = nullptr;
875 uint32_t num_extensions = 0;
876 if (layer_name) {
877 GetLayerExtensions(layer_name, &extensions, &num_extensions);
878 } else {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800879 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
880 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
881 available[num_extensions++] = VkExtensionProperties{
882 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
883 available[num_extensions++] =
884 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
885 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
886 if (g_driver_instance_extensions[kEXT_debug_report]) {
887 available[num_extensions++] =
888 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
889 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
890 }
Jesse Hall80523e22016-01-06 16:47:54 -0800891 // TODO(jessehall): We need to also enumerate extensions supported by
892 // implicitly-enabled layers. Currently we don't have that list of
893 // layers until instance creation.
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800894 extensions = available;
Jesse Hall80523e22016-01-06 16:47:54 -0800895 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700896
Jesse Hall80523e22016-01-06 16:47:54 -0800897 if (!properties || *properties_count > num_extensions)
898 *properties_count = num_extensions;
899 if (properties)
900 std::copy(extensions, extensions + *properties_count, properties);
901 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700902}
903
Jesse Hall80523e22016-01-06 16:47:54 -0800904VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
905 VkLayerProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700906 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700907 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700908
Jesse Hall80523e22016-01-06 16:47:54 -0800909 uint32_t layer_count =
910 EnumerateLayers(properties ? *properties_count : 0, properties);
911 if (!properties || *properties_count > layer_count)
912 *properties_count = layer_count;
913 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700914}
915
Jesse Hall1f91d392015-12-11 16:28:44 -0800916VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
917 const VkAllocationCallbacks* allocator,
918 VkInstance* instance_out) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700919 VkResult result;
920
921 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700922 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700923
Jesse Hall03b6fe12015-11-24 12:44:21 -0800924 if (!allocator)
925 allocator = &kDefaultAllocCallbacks;
926
Jesse Hall04f4f472015-08-16 19:51:04 -0700927 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -0700928 create_info = &local_create_info;
929
Jesse Hall3fbc8562015-11-29 22:10:52 -0800930 void* instance_mem = allocator->pfnAllocation(
931 allocator->pUserData, sizeof(Instance), alignof(Instance),
932 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700933 if (!instance_mem)
934 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800935 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700936
Jesse Hall9a16f972015-10-28 15:59:53 -0700937 result = ActivateAllLayers(create_info, instance, instance);
938 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800939 DestroyInstance_Bottom(instance->handle, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -0700940 return result;
941 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500942
Jesse Hall1f91d392015-12-11 16:28:44 -0800943 void* base_object = static_cast<void*>(instance->handle);
Michael Lentine03c64b02015-08-26 18:27:26 -0500944 void* next_object = base_object;
945 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800946 PFN_vkGetInstanceProcAddr next_get_proc_addr = GetInstanceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500947 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700948 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500949 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
950
951 for (size_t i = elem_list.size(); i > 0; i--) {
952 size_t idx = i - 1;
953 next_element = &elem_list[idx];
954 next_element->get_proc_addr =
955 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
956 next_element->base_object = base_object;
957 next_element->next_element = next_object;
958 next_object = static_cast<void*>(next_element);
959
Jesse Hall80523e22016-01-06 16:47:54 -0800960 next_get_proc_addr =
961 instance->active_layers[idx].GetGetInstanceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500962 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800963 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500964 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800965 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500966 }
967 }
968
Jesse Hall1f91d392015-12-11 16:28:44 -0800969 // This is the magic call that initializes all the layer instances and
970 // allows them to create their instance_handle -> instance_data mapping.
971 next_get_proc_addr(static_cast<VkInstance>(next_object),
972 "vkGetInstanceProcAddr");
973
974 if (!LoadInstanceDispatchTable(static_cast<VkInstance>(base_object),
975 next_get_proc_addr, instance->dispatch)) {
976 DestroyInstance_Bottom(instance->handle, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -0500977 return VK_ERROR_INITIALIZATION_FAILED;
978 }
979
Michael Lentine950bb4f2015-09-14 13:26:30 -0500980 // Force enable callback extension if required
Jesse Hall21597662015-12-18 13:48:24 -0800981 bool enable_callback = false;
982 bool enable_logging = false;
983 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
984 enable_callback =
985 property_get_bool("debug.vulkan.enable_callback", false);
986 enable_logging = enable_callback;
987 if (enable_callback) {
988 enable_callback = AddExtensionToCreateInfo(
Jesse Hall715b86a2016-01-16 16:34:29 -0800989 local_create_info, "VK_EXT_debug_report", instance->alloc);
Jesse Hall21597662015-12-18 13:48:24 -0800990 }
Michael Lentine950bb4f2015-09-14 13:26:30 -0500991 }
992
Jesse Hall1f91d392015-12-11 16:28:44 -0800993 *instance_out = instance->handle;
994 PFN_vkCreateInstance create_instance =
995 reinterpret_cast<PFN_vkCreateInstance>(
996 next_get_proc_addr(instance->handle, "vkCreateInstance"));
997 result = create_instance(create_info, allocator, instance_out);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500998 if (enable_callback)
999 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -07001000 if (result <= 0) {
1001 // For every layer, including the loader top and bottom layers:
1002 // - If a call to the next CreateInstance fails, the layer must clean
1003 // up anything it has successfully done so far, and propagate the
1004 // error upwards.
1005 // - If a layer successfully calls the next layer's CreateInstance, and
1006 // afterwards must fail for some reason, it must call the next layer's
1007 // DestroyInstance before returning.
1008 // - The layer must not call the next layer's DestroyInstance if that
1009 // layer's CreateInstance wasn't called, or returned failure.
1010
Jesse Hall1f91d392015-12-11 16:28:44 -08001011 // On failure, CreateInstance_Bottom frees the instance struct, so it's
Jesse Hall04f4f472015-08-16 19:51:04 -07001012 // already gone at this point. Nothing to do.
1013 }
1014
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001015 if (enable_logging) {
Jesse Hall715b86a2016-01-16 16:34:29 -08001016 const VkDebugReportCallbackCreateInfoEXT callback_create_info = {
1017 .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
1018 .flags =
1019 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT,
1020 .pfnCallback = LogDebugMessageCallback,
1021 };
1022 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
1023 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
1024 GetInstanceProcAddr_Top(instance->handle,
1025 "vkCreateDebugReportCallbackEXT"));
1026 create_debug_report_callback(instance->handle, &callback_create_info,
1027 allocator, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001028 }
1029
Jesse Hall04f4f472015-08-16 19:51:04 -07001030 return result;
1031}
1032
Jesse Hall1f91d392015-12-11 16:28:44 -08001033PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
1034 const char* name) {
1035 // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
1036 if (!vkinstance)
1037 return GetLoaderGlobalProcAddr(name);
1038
1039 const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
1040 PFN_vkVoidFunction pfn;
1041 // Always go through the loader-top function if there is one.
1042 if ((pfn = GetLoaderTopProcAddr(name)))
1043 return pfn;
1044 // Otherwise, look up the handler in the instance dispatch table
1045 if ((pfn = GetDispatchProcAddr(dispatch, name)))
1046 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -08001047 // Anything not handled already must be a device-dispatched function
1048 // without a loader-top. We must return a function that will dispatch based
1049 // on the dispatchable object parameter -- which is exactly what the
1050 // exported functions do. So just return them here.
1051 return GetLoaderExportProcAddr(name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001052}
1053
Jesse Hall1f91d392015-12-11 16:28:44 -08001054void DestroyInstance_Top(VkInstance instance,
1055 const VkAllocationCallbacks* allocator) {
1056 if (!instance)
1057 return;
1058 GetDispatchTable(instance).DestroyInstance(instance, allocator);
1059}
1060
1061PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
1062 PFN_vkVoidFunction pfn;
Jesse Hall04f4f472015-08-16 19:51:04 -07001063 if (!device)
Jesse Hall1f91d392015-12-11 16:28:44 -08001064 return nullptr;
1065 if ((pfn = GetLoaderTopProcAddr(name)))
1066 return pfn;
1067 return GetDispatchProcAddr(GetDispatchTable(device), name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001068}
1069
Jesse Hall1f91d392015-12-11 16:28:44 -08001070void GetDeviceQueue_Top(VkDevice vkdevice,
1071 uint32_t family,
1072 uint32_t index,
1073 VkQueue* queue_out) {
1074 const auto& table = GetDispatchTable(vkdevice);
1075 table.GetDeviceQueue(vkdevice, family, index, queue_out);
1076 hwvulkan_dispatch_t* queue_dispatch =
1077 reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
1078 if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
1079 queue_dispatch->vtbl != &table)
1080 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
1081 queue_dispatch->magic);
1082 queue_dispatch->vtbl = &table;
Jesse Hall04f4f472015-08-16 19:51:04 -07001083}
1084
Jesse Hall1f91d392015-12-11 16:28:44 -08001085VkResult AllocateCommandBuffers_Top(
1086 VkDevice vkdevice,
1087 const VkCommandBufferAllocateInfo* alloc_info,
1088 VkCommandBuffer* cmdbufs) {
1089 const auto& table = GetDispatchTable(vkdevice);
1090 VkResult result =
1091 table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001092 if (result != VK_SUCCESS)
1093 return result;
Jesse Hall3dd678a2016-01-08 21:52:01 -08001094 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001095 hwvulkan_dispatch_t* cmdbuf_dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001096 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hall1f91d392015-12-11 16:28:44 -08001097 ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001098 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -08001099 cmdbuf_dispatch->magic);
1100 cmdbuf_dispatch->vtbl = &table;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001101 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001102 return VK_SUCCESS;
1103}
1104
Jesse Hall1f91d392015-12-11 16:28:44 -08001105void DestroyDevice_Top(VkDevice vkdevice,
1106 const VkAllocationCallbacks* /*allocator*/) {
1107 if (!vkdevice)
1108 return;
1109 Device& device = GetDispatchParent(vkdevice);
Jesse Hall1f91d392015-12-11 16:28:44 -08001110 device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
1111 DestroyDevice(&device);
Jesse Hall04f4f472015-08-16 19:51:04 -07001112}
1113
Jesse Hall1f91d392015-12-11 16:28:44 -08001114// -----------------------------------------------------------------------------
1115
1116const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
1117 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001118}
1119
Jesse Hall1f91d392015-12-11 16:28:44 -08001120const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
1121 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001122}
1123
Jesse Hall715b86a2016-01-16 16:34:29 -08001124VkInstance GetDriverInstance(VkInstance instance) {
1125 return GetDispatchParent(instance).drv.instance;
1126}
1127
1128const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
1129 return GetDispatchParent(instance).drv.dispatch;
1130}
1131
Jesse Hall1f91d392015-12-11 16:28:44 -08001132const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
1133 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001134}
1135
Jesse Hall1f91d392015-12-11 16:28:44 -08001136const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
1137 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001138}
1139
Jesse Hall715b86a2016-01-16 16:34:29 -08001140DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
1141 return GetDispatchParent(instance).debug_report_callbacks;
1142}
1143
Jesse Hall04f4f472015-08-16 19:51:04 -07001144} // namespace vulkan