blob: 00eb9b1bcdbd578f2a0643170e7f62f49864368d [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 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++) {
532 InstanceExtension id = InstanceExtensionFromName(
533 create_info->ppEnabledExtensionNames[i]);
534 if (id != kInstanceExtensionCount &&
535 g_driver_instance_extensions[id]) {
536 names[driver_create_info.enabledExtensionCount++] =
537 create_info->ppEnabledExtensionNames[i];
538 enabled_extensions.set(id);
539 }
540 }
541 driver_create_info.ppEnabledExtensionNames = names;
542 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800543
544 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Jesse Hall1f91d392015-12-11 16:28:44 -0800545 &instance.drv.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700546 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800547 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700548 return result;
549 }
550
Jesse Hall1f91d392015-12-11 16:28:44 -0800551 if (!LoadDriverDispatchTable(instance.drv.instance,
552 g_hwdevice->GetInstanceProcAddr,
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800553 enabled_extensions, instance.drv.dispatch)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800554 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700555 return VK_ERROR_INITIALIZATION_FAILED;
556 }
557
Jesse Hall1f91d392015-12-11 16:28:44 -0800558 hwvulkan_dispatch_t* drv_dispatch =
559 reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
560 if (drv_dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
561 // Skip setting drv_dispatch->vtbl, since we never call through it;
562 // we go through instance.drv.dispatch instead.
Jesse Hall04f4f472015-08-16 19:51:04 -0700563 } else {
564 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800565 drv_dispatch->magic);
566 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700567 return VK_ERROR_INITIALIZATION_FAILED;
568 }
569
570 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800571 result = instance.drv.dispatch.EnumeratePhysicalDevices(
572 instance.drv.instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700573 if (result != VK_SUCCESS) {
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 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800578 result = instance.drv.dispatch.EnumeratePhysicalDevices(
579 instance.drv.instance, &num_physical_devices,
580 instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700581 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800582 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700583 return VK_ERROR_INITIALIZATION_FAILED;
584 }
585 for (uint32_t i = 0; i < num_physical_devices; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800586 hwvulkan_dispatch_t* pdev_dispatch =
587 reinterpret_cast<hwvulkan_dispatch_t*>(
588 instance.physical_devices[i]);
589 if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700590 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800591 pdev_dispatch->magic);
592 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700593 return VK_ERROR_INITIALIZATION_FAILED;
594 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800595 pdev_dispatch->vtbl = instance.dispatch_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700596 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800597 instance.drv.num_physical_devices = num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700598
Jesse Hall1f91d392015-12-11 16:28:44 -0800599 instance.num_physical_devices = instance.drv.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700600 return VK_SUCCESS;
601}
602
Jesse Hall1f91d392015-12-11 16:28:44 -0800603PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) {
604 PFN_vkVoidFunction pfn;
605 if ((pfn = GetLoaderBottomProcAddr(name)))
606 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -0800607 return nullptr;
608}
609
610VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
611 uint32_t* pdev_count,
612 VkPhysicalDevice* pdevs) {
613 Instance& instance = GetDispatchParent(vkinstance);
614 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700615 if (pdevs) {
616 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800617 std::copy(instance.physical_devices, instance.physical_devices + count,
618 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700619 }
620 *pdev_count = count;
621 return VK_SUCCESS;
622}
623
Jesse Hall1f91d392015-12-11 16:28:44 -0800624void GetPhysicalDeviceProperties_Bottom(
625 VkPhysicalDevice pdev,
626 VkPhysicalDeviceProperties* properties) {
627 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
628 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700629}
630
Jesse Hall1f91d392015-12-11 16:28:44 -0800631void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
632 VkPhysicalDeviceFeatures* features) {
633 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
634 features);
635}
636
637void GetPhysicalDeviceMemoryProperties_Bottom(
638 VkPhysicalDevice pdev,
639 VkPhysicalDeviceMemoryProperties* properties) {
640 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
641 pdev, properties);
642}
643
644void GetPhysicalDeviceQueueFamilyProperties_Bottom(
645 VkPhysicalDevice pdev,
646 uint32_t* pCount,
647 VkQueueFamilyProperties* properties) {
648 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
649 pdev, pCount, properties);
650}
651
652void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
653 VkFormat format,
654 VkFormatProperties* properties) {
655 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700656 pdev, format, properties);
657}
658
Jesse Hall1f91d392015-12-11 16:28:44 -0800659VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700660 VkPhysicalDevice pdev,
661 VkFormat format,
662 VkImageType type,
663 VkImageTiling tiling,
664 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700665 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700666 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800667 return GetDispatchParent(pdev)
668 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800669 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700670}
671
Jesse Hall1f91d392015-12-11 16:28:44 -0800672void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700673 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800674 VkFormat format,
675 VkImageType type,
676 VkSampleCountFlagBits samples,
677 VkImageUsageFlags usage,
678 VkImageTiling tiling,
679 uint32_t* properties_count,
680 VkSparseImageFormatProperties* properties) {
681 GetDispatchParent(pdev)
682 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
683 pdev, format, type, samples, usage, tiling, properties_count,
684 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700685}
686
Jesse Halle1b12782015-11-30 11:27:32 -0800687VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800688VkResult EnumerateDeviceExtensionProperties_Bottom(
Jesse Hall80523e22016-01-06 16:47:54 -0800689 VkPhysicalDevice /*pdev*/,
690 const char* /*layer_name*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800691 uint32_t* properties_count,
Jesse Hall80523e22016-01-06 16:47:54 -0800692 VkExtensionProperties* /*properties*/) {
693 // TODO(jessehall): Implement me...
694 *properties_count = 0;
695 return VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700696}
697
Jesse Halle1b12782015-11-30 11:27:32 -0800698VKAPI_ATTR
Jesse Hall80523e22016-01-06 16:47:54 -0800699VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800700 uint32_t* properties_count,
Jesse Hallaa410942016-01-17 13:07:10 -0800701 VkLayerProperties* properties) {
702 uint32_t layer_count =
703 EnumerateDeviceLayers(properties ? *properties_count : 0, properties);
704 if (!properties || *properties_count > layer_count)
705 *properties_count = layer_count;
706 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800707}
708
709VKAPI_ATTR
710VkResult CreateDevice_Bottom(VkPhysicalDevice pdev,
711 const VkDeviceCreateInfo* create_info,
712 const VkAllocationCallbacks* allocator,
713 VkDevice* device_out) {
714 Instance& instance = GetDispatchParent(pdev);
Jesse Hall04f4f472015-08-16 19:51:04 -0700715 VkResult result;
716
Jesse Hall03b6fe12015-11-24 12:44:21 -0800717 if (!allocator) {
718 if (instance.alloc)
719 allocator = instance.alloc;
720 else
721 allocator = &kDefaultAllocCallbacks;
722 }
723
Jesse Hall3fbc8562015-11-29 22:10:52 -0800724 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
725 alignof(Device),
726 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700727 if (!mem)
728 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500729 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700730
Jesse Hall9a16f972015-10-28 15:59:53 -0700731 result = ActivateAllLayers(create_info, &instance, device);
732 if (result != VK_SUCCESS) {
733 DestroyDevice(device);
734 return result;
735 }
736
Jesse Halla7ac76d2016-01-08 22:29:42 -0800737 const char* kAndroidNativeBufferExtensionName = "VK_ANDROID_native_buffer";
738 VkDeviceCreateInfo driver_create_info = *create_info;
739 driver_create_info.enabledLayerCount = 0;
740 driver_create_info.ppEnabledLayerNames = nullptr;
741 // TODO(jessehall): As soon as we enumerate device extensions supported by
742 // the driver, we need to filter the requested extension list to those
743 // supported by the driver here. Also, add the VK_ANDROID_native_buffer
744 // extension to the list iff the VK_KHR_swapchain extension was requested,
745 // instead of adding it unconditionally like we do now.
746 driver_create_info.enabledExtensionCount = 1;
747 driver_create_info.ppEnabledExtensionNames = &kAndroidNativeBufferExtensionName;
748
Jesse Hall04f4f472015-08-16 19:51:04 -0700749 VkDevice drv_device;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800750 result = instance.drv.dispatch.CreateDevice(pdev, &driver_create_info, allocator,
Jesse Hall1f91d392015-12-11 16:28:44 -0800751 &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700752 if (result != VK_SUCCESS) {
753 DestroyDevice(device);
754 return result;
755 }
756
Jesse Hall1f91d392015-12-11 16:28:44 -0800757 hwvulkan_dispatch_t* drv_dispatch =
Jesse Hall04f4f472015-08-16 19:51:04 -0700758 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800759 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
760 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
761 drv_dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500762 PFN_vkDestroyDevice destroy_device =
763 reinterpret_cast<PFN_vkDestroyDevice>(
Jesse Hall1f91d392015-12-11 16:28:44 -0800764 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
765 "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800766 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700767 DestroyDevice(device);
768 return VK_ERROR_INITIALIZATION_FAILED;
769 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800770 drv_dispatch->vtbl = &device->dispatch;
771 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
772 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
773 "vkGetDeviceProcAddr"));
Jesse Hall04f4f472015-08-16 19:51:04 -0700774
Michael Lentine03c64b02015-08-26 18:27:26 -0500775 void* base_object = static_cast<void*>(drv_device);
776 void* next_object = base_object;
777 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800778 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetDeviceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500779 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500780 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500781 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
782
783 for (size_t i = elem_list.size(); i > 0; i--) {
784 size_t idx = i - 1;
785 next_element = &elem_list[idx];
786 next_element->get_proc_addr =
787 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
788 next_element->base_object = base_object;
789 next_element->next_element = next_object;
790 next_object = static_cast<void*>(next_element);
791
Jesse Hall80523e22016-01-06 16:47:54 -0800792 next_get_proc_addr = device->active_layers[idx].GetGetDeviceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500793 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800794 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500795 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800796 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500797 }
798 }
799
Jesse Hall1f91d392015-12-11 16:28:44 -0800800 // This is the magic call that initializes all the layer devices and
801 // allows them to create their device_handle -> device_data mapping.
802 next_get_proc_addr(static_cast<VkDevice>(next_object),
803 "vkGetDeviceProcAddr");
804
805 // We must create all the layer devices *before* retrieving the device
806 // procaddrs, so that the layers know which extensions are enabled and
807 // therefore which functions to return procaddrs for.
808 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
809 next_get_proc_addr(drv_device, "vkCreateDevice"));
810 create_device(pdev, create_info, allocator, &drv_device);
811
812 if (!LoadDeviceDispatchTable(static_cast<VkDevice>(base_object),
813 next_get_proc_addr, device->dispatch)) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500814 DestroyDevice(device);
815 return VK_ERROR_INITIALIZATION_FAILED;
816 }
817
Jesse Hall1f91d392015-12-11 16:28:44 -0800818 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700819 return VK_SUCCESS;
820}
821
Jesse Hall1f91d392015-12-11 16:28:44 -0800822void DestroyInstance_Bottom(VkInstance vkinstance,
823 const VkAllocationCallbacks* allocator) {
824 Instance& instance = GetDispatchParent(vkinstance);
825
826 // These checks allow us to call DestroyInstance_Bottom from any error
827 // path in CreateInstance_Bottom, before the driver instance is fully
828 // initialized.
829 if (instance.drv.instance != VK_NULL_HANDLE &&
830 instance.drv.dispatch.DestroyInstance) {
831 instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
832 }
833 if (instance.message) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800834 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback;
835 destroy_debug_report_callback =
836 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
837 vkGetInstanceProcAddr(vkinstance,
838 "vkDestroyDebugReportCallbackEXT"));
839 destroy_debug_report_callback(vkinstance, instance.message, allocator);
Jesse Hall1f91d392015-12-11 16:28:44 -0800840 }
Jesse Hall80523e22016-01-06 16:47:54 -0800841 instance.active_layers.clear();
Jesse Hall1f91d392015-12-11 16:28:44 -0800842 const VkAllocationCallbacks* alloc = instance.alloc;
843 instance.~Instance();
844 alloc->pfnFree(alloc->pUserData, &instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700845}
846
Jesse Hall1f91d392015-12-11 16:28:44 -0800847PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
848 const char* name) {
849 if (strcmp(name, "vkCreateDevice") == 0) {
850 // TODO(jessehall): Blegh, having this here is disgusting. The current
851 // layer init process can't call through the instance dispatch table's
852 // vkCreateDevice, because that goes through the instance layers rather
853 // than through the device layers. So we need to be able to get the
854 // vkCreateDevice pointer through the *device* layer chain.
855 //
856 // Because we've already created the driver device before calling
857 // through the layer vkCreateDevice functions, the loader bottom proc
858 // is a no-op.
Michael Lentine03c64b02015-08-26 18:27:26 -0500859 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
860 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800861
862 // VK_ANDROID_native_buffer should be hidden from applications and layers.
863 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
864 PFN_vkVoidFunction pfn;
865 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
866 strcmp(name, "vkAcquireImageANDROID") == 0 ||
867 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
868 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500869 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800870 if ((pfn = GetLoaderBottomProcAddr(name)))
871 return pfn;
872 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700873}
874
Jesse Hall04f4f472015-08-16 19:51:04 -0700875// -----------------------------------------------------------------------------
Jesse Hall1f91d392015-12-11 16:28:44 -0800876// Loader top functions. These are called directly from the loader entry
877// points or from the application (via vkGetInstanceProcAddr) without going
878// through a dispatch table.
Jesse Hall04f4f472015-08-16 19:51:04 -0700879
Jesse Hall1f91d392015-12-11 16:28:44 -0800880VkResult EnumerateInstanceExtensionProperties_Top(
Jesse Hall80523e22016-01-06 16:47:54 -0800881 const char* layer_name,
882 uint32_t* properties_count,
883 VkExtensionProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700884 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700885 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700886
Jesse Hall80523e22016-01-06 16:47:54 -0800887 const VkExtensionProperties* extensions = nullptr;
888 uint32_t num_extensions = 0;
889 if (layer_name) {
Jesse Hallaa410942016-01-17 13:07:10 -0800890 GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions);
Jesse Hall80523e22016-01-06 16:47:54 -0800891 } else {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800892 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
893 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
894 available[num_extensions++] = VkExtensionProperties{
895 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
896 available[num_extensions++] =
897 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
898 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
899 if (g_driver_instance_extensions[kEXT_debug_report]) {
900 available[num_extensions++] =
901 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
902 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
903 }
Jesse Hall80523e22016-01-06 16:47:54 -0800904 // TODO(jessehall): We need to also enumerate extensions supported by
905 // implicitly-enabled layers. Currently we don't have that list of
906 // layers until instance creation.
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800907 extensions = available;
Jesse Hall80523e22016-01-06 16:47:54 -0800908 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700909
Jesse Hall80523e22016-01-06 16:47:54 -0800910 if (!properties || *properties_count > num_extensions)
911 *properties_count = num_extensions;
912 if (properties)
913 std::copy(extensions, extensions + *properties_count, properties);
914 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700915}
916
Jesse Hall80523e22016-01-06 16:47:54 -0800917VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
918 VkLayerProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700919 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700920 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700921
Jesse Hall80523e22016-01-06 16:47:54 -0800922 uint32_t layer_count =
Jesse Hallaa410942016-01-17 13:07:10 -0800923 EnumerateInstanceLayers(properties ? *properties_count : 0, properties);
Jesse Hall80523e22016-01-06 16:47:54 -0800924 if (!properties || *properties_count > layer_count)
925 *properties_count = layer_count;
926 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700927}
928
Jesse Hall1f91d392015-12-11 16:28:44 -0800929VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
930 const VkAllocationCallbacks* allocator,
931 VkInstance* instance_out) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700932 VkResult result;
933
934 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700935 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700936
Jesse Hall03b6fe12015-11-24 12:44:21 -0800937 if (!allocator)
938 allocator = &kDefaultAllocCallbacks;
939
Jesse Hall04f4f472015-08-16 19:51:04 -0700940 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -0700941 create_info = &local_create_info;
942
Jesse Hall3fbc8562015-11-29 22:10:52 -0800943 void* instance_mem = allocator->pfnAllocation(
944 allocator->pUserData, sizeof(Instance), alignof(Instance),
945 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700946 if (!instance_mem)
947 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800948 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700949
Jesse Hall9a16f972015-10-28 15:59:53 -0700950 result = ActivateAllLayers(create_info, instance, instance);
951 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800952 DestroyInstance_Bottom(instance->handle, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -0700953 return result;
954 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500955
Jesse Hall1f91d392015-12-11 16:28:44 -0800956 void* base_object = static_cast<void*>(instance->handle);
Michael Lentine03c64b02015-08-26 18:27:26 -0500957 void* next_object = base_object;
958 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800959 PFN_vkGetInstanceProcAddr next_get_proc_addr = GetInstanceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500960 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -0700961 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500962 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
963
964 for (size_t i = elem_list.size(); i > 0; i--) {
965 size_t idx = i - 1;
966 next_element = &elem_list[idx];
967 next_element->get_proc_addr =
968 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
969 next_element->base_object = base_object;
970 next_element->next_element = next_object;
971 next_object = static_cast<void*>(next_element);
972
Jesse Hall80523e22016-01-06 16:47:54 -0800973 next_get_proc_addr =
974 instance->active_layers[idx].GetGetInstanceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500975 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800976 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500977 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800978 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500979 }
980 }
981
Jesse Hall1f91d392015-12-11 16:28:44 -0800982 // This is the magic call that initializes all the layer instances and
983 // allows them to create their instance_handle -> instance_data mapping.
984 next_get_proc_addr(static_cast<VkInstance>(next_object),
985 "vkGetInstanceProcAddr");
986
987 if (!LoadInstanceDispatchTable(static_cast<VkInstance>(base_object),
988 next_get_proc_addr, instance->dispatch)) {
989 DestroyInstance_Bottom(instance->handle, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -0500990 return VK_ERROR_INITIALIZATION_FAILED;
991 }
992
Michael Lentine950bb4f2015-09-14 13:26:30 -0500993 // Force enable callback extension if required
Jesse Hall21597662015-12-18 13:48:24 -0800994 bool enable_callback = false;
995 bool enable_logging = false;
996 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
997 enable_callback =
998 property_get_bool("debug.vulkan.enable_callback", false);
999 enable_logging = enable_callback;
1000 if (enable_callback) {
1001 enable_callback = AddExtensionToCreateInfo(
Jesse Hall715b86a2016-01-16 16:34:29 -08001002 local_create_info, "VK_EXT_debug_report", instance->alloc);
Jesse Hall21597662015-12-18 13:48:24 -08001003 }
Michael Lentine950bb4f2015-09-14 13:26:30 -05001004 }
1005
Jesse Hall1f91d392015-12-11 16:28:44 -08001006 *instance_out = instance->handle;
1007 PFN_vkCreateInstance create_instance =
1008 reinterpret_cast<PFN_vkCreateInstance>(
1009 next_get_proc_addr(instance->handle, "vkCreateInstance"));
1010 result = create_instance(create_info, allocator, instance_out);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001011 if (enable_callback)
1012 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -07001013 if (result <= 0) {
1014 // For every layer, including the loader top and bottom layers:
1015 // - If a call to the next CreateInstance fails, the layer must clean
1016 // up anything it has successfully done so far, and propagate the
1017 // error upwards.
1018 // - If a layer successfully calls the next layer's CreateInstance, and
1019 // afterwards must fail for some reason, it must call the next layer's
1020 // DestroyInstance before returning.
1021 // - The layer must not call the next layer's DestroyInstance if that
1022 // layer's CreateInstance wasn't called, or returned failure.
1023
Jesse Hall1f91d392015-12-11 16:28:44 -08001024 // On failure, CreateInstance_Bottom frees the instance struct, so it's
Jesse Hall04f4f472015-08-16 19:51:04 -07001025 // already gone at this point. Nothing to do.
1026 }
1027
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001028 if (enable_logging) {
Jesse Hall715b86a2016-01-16 16:34:29 -08001029 const VkDebugReportCallbackCreateInfoEXT callback_create_info = {
1030 .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
1031 .flags =
1032 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT,
1033 .pfnCallback = LogDebugMessageCallback,
1034 };
1035 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
1036 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
1037 GetInstanceProcAddr_Top(instance->handle,
1038 "vkCreateDebugReportCallbackEXT"));
1039 create_debug_report_callback(instance->handle, &callback_create_info,
1040 allocator, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001041 }
1042
Jesse Hall04f4f472015-08-16 19:51:04 -07001043 return result;
1044}
1045
Jesse Hall1f91d392015-12-11 16:28:44 -08001046PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
1047 const char* name) {
1048 // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
1049 if (!vkinstance)
1050 return GetLoaderGlobalProcAddr(name);
1051
1052 const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
1053 PFN_vkVoidFunction pfn;
1054 // Always go through the loader-top function if there is one.
1055 if ((pfn = GetLoaderTopProcAddr(name)))
1056 return pfn;
1057 // Otherwise, look up the handler in the instance dispatch table
1058 if ((pfn = GetDispatchProcAddr(dispatch, name)))
1059 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -08001060 // Anything not handled already must be a device-dispatched function
1061 // without a loader-top. We must return a function that will dispatch based
1062 // on the dispatchable object parameter -- which is exactly what the
1063 // exported functions do. So just return them here.
1064 return GetLoaderExportProcAddr(name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001065}
1066
Jesse Hall1f91d392015-12-11 16:28:44 -08001067void DestroyInstance_Top(VkInstance instance,
1068 const VkAllocationCallbacks* allocator) {
1069 if (!instance)
1070 return;
1071 GetDispatchTable(instance).DestroyInstance(instance, allocator);
1072}
1073
1074PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
1075 PFN_vkVoidFunction pfn;
Jesse Hall04f4f472015-08-16 19:51:04 -07001076 if (!device)
Jesse Hall1f91d392015-12-11 16:28:44 -08001077 return nullptr;
1078 if ((pfn = GetLoaderTopProcAddr(name)))
1079 return pfn;
1080 return GetDispatchProcAddr(GetDispatchTable(device), name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001081}
1082
Jesse Hall1f91d392015-12-11 16:28:44 -08001083void GetDeviceQueue_Top(VkDevice vkdevice,
1084 uint32_t family,
1085 uint32_t index,
1086 VkQueue* queue_out) {
1087 const auto& table = GetDispatchTable(vkdevice);
1088 table.GetDeviceQueue(vkdevice, family, index, queue_out);
1089 hwvulkan_dispatch_t* queue_dispatch =
1090 reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
1091 if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
1092 queue_dispatch->vtbl != &table)
1093 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
1094 queue_dispatch->magic);
1095 queue_dispatch->vtbl = &table;
Jesse Hall04f4f472015-08-16 19:51:04 -07001096}
1097
Jesse Hall1f91d392015-12-11 16:28:44 -08001098VkResult AllocateCommandBuffers_Top(
1099 VkDevice vkdevice,
1100 const VkCommandBufferAllocateInfo* alloc_info,
1101 VkCommandBuffer* cmdbufs) {
1102 const auto& table = GetDispatchTable(vkdevice);
1103 VkResult result =
1104 table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001105 if (result != VK_SUCCESS)
1106 return result;
Jesse Hall3dd678a2016-01-08 21:52:01 -08001107 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001108 hwvulkan_dispatch_t* cmdbuf_dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001109 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hall1f91d392015-12-11 16:28:44 -08001110 ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001111 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -08001112 cmdbuf_dispatch->magic);
1113 cmdbuf_dispatch->vtbl = &table;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001114 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001115 return VK_SUCCESS;
1116}
1117
Jesse Hall1f91d392015-12-11 16:28:44 -08001118void DestroyDevice_Top(VkDevice vkdevice,
1119 const VkAllocationCallbacks* /*allocator*/) {
1120 if (!vkdevice)
1121 return;
1122 Device& device = GetDispatchParent(vkdevice);
Jesse Hall1f91d392015-12-11 16:28:44 -08001123 device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
1124 DestroyDevice(&device);
Jesse Hall04f4f472015-08-16 19:51:04 -07001125}
1126
Jesse Hall1f91d392015-12-11 16:28:44 -08001127// -----------------------------------------------------------------------------
1128
1129const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
1130 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001131}
1132
Jesse Hall1f91d392015-12-11 16:28:44 -08001133const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
1134 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001135}
1136
Jesse Hall715b86a2016-01-16 16:34:29 -08001137VkInstance GetDriverInstance(VkInstance instance) {
1138 return GetDispatchParent(instance).drv.instance;
1139}
1140
1141const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
1142 return GetDispatchParent(instance).drv.dispatch;
1143}
1144
Jesse Hall1f91d392015-12-11 16:28:44 -08001145const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
1146 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001147}
1148
Jesse Hall1f91d392015-12-11 16:28:44 -08001149const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
1150 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001151}
1152
Jesse Hall715b86a2016-01-16 16:34:29 -08001153DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
1154 return GetDispatchParent(instance).debug_report_callbacks;
1155}
1156
Jesse Hall04f4f472015-08-16 19:51:04 -07001157} // namespace vulkan