blob: a0c142e7639898668e9ae3ff6d7a397df847f716 [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 Hall04f4f472015-08-16 19:51:04 -070017// module header
18#include "loader.h"
19// standard C headers
Michael Lentine03c64b02015-08-26 18:27:26 -050020#include <dirent.h>
21#include <dlfcn.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070022#include <inttypes.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070023#include <pthread.h>
Jesse Hall03b6fe12015-11-24 12:44:21 -080024#include <stdlib.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070025#include <string.h>
Jesse Hall21597662015-12-18 13:48:24 -080026#include <sys/prctl.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070027// standard C++ headers
28#include <algorithm>
29#include <mutex>
Michael Lentine03c64b02015-08-26 18:27:26 -050030#include <sstream>
31#include <string>
32#include <unordered_map>
33#include <vector>
Jesse Hall04f4f472015-08-16 19:51:04 -070034// platform/library headers
Michael Lentine03c64b02015-08-26 18:27:26 -050035#include <cutils/properties.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070036#include <hardware/hwvulkan.h>
37#include <log/log.h>
Michael Lentine1c69b9e2015-09-14 13:26:59 -050038#include <vulkan/vulkan_loader_data.h>
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -070039#include <vulkan/vk_layer_interface.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070040
Jesse Hall26cecff2016-01-21 19:52:25 -080041// #define ENABLE_ALLOC_CALLSTACKS 1
42#if ENABLE_ALLOC_CALLSTACKS
43#include <utils/CallStack.h>
44#define ALOGD_CALLSTACK(...) \
45 do { \
46 ALOGD(__VA_ARGS__); \
47 android::CallStack callstack; \
48 callstack.update(); \
49 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
50 } while (false)
51#else
52#define ALOGD_CALLSTACK(...) \
53 do { \
54 } while (false)
55#endif
56
Jesse Hall04f4f472015-08-16 19:51:04 -070057using namespace vulkan;
58
59static const uint32_t kMaxPhysicalDevices = 4;
60
Michael Lentine03c64b02015-08-26 18:27:26 -050061namespace {
62
Jesse Hall1f91d392015-12-11 16:28:44 -080063// ----------------------------------------------------------------------------
Michael Lentine03c64b02015-08-26 18:27:26 -050064
Jesse Hall3fbc8562015-11-29 22:10:52 -080065// Standard-library allocator that delegates to VkAllocationCallbacks.
Jesse Hall03b6fe12015-11-24 12:44:21 -080066//
67// TODO(jessehall): This class currently always uses
Jesse Hall3fbc8562015-11-29 22:10:52 -080068// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
Jesse Hall03b6fe12015-11-24 12:44:21 -080069// parameter or a constructor parameter. The former would help catch bugs
70// where we use the wrong scope, e.g. adding a command-scope string to an
71// instance-scope vector. But that might also be pretty annoying to deal with.
Michael Lentine03c64b02015-08-26 18:27:26 -050072template <class T>
73class CallbackAllocator {
74 public:
75 typedef T value_type;
76
Jesse Hall3fbc8562015-11-29 22:10:52 -080077 CallbackAllocator(const VkAllocationCallbacks* alloc_input)
Michael Lentine03c64b02015-08-26 18:27:26 -050078 : alloc(alloc_input) {}
79
80 template <class T2>
81 CallbackAllocator(const CallbackAllocator<T2>& other)
82 : alloc(other.alloc) {}
83
84 T* allocate(std::size_t n) {
Jesse Hall3fbc8562015-11-29 22:10:52 -080085 void* mem =
86 alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
87 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall26cecff2016-01-21 19:52:25 -080088 if (!mem)
89 throw std::bad_alloc();
Michael Lentine03c64b02015-08-26 18:27:26 -050090 return static_cast<T*>(mem);
91 }
92
Jesse Hall26cecff2016-01-21 19:52:25 -080093 void deallocate(T* array, std::size_t /*n*/) noexcept {
Michael Lentine03c64b02015-08-26 18:27:26 -050094 alloc->pfnFree(alloc->pUserData, array);
95 }
96
Jesse Hall3fbc8562015-11-29 22:10:52 -080097 const VkAllocationCallbacks* alloc;
Michael Lentine03c64b02015-08-26 18:27:26 -050098};
99// These are needed in order to move Strings
100template <class T>
101bool operator==(const CallbackAllocator<T>& alloc1,
102 const CallbackAllocator<T>& alloc2) {
103 return alloc1.alloc == alloc2.alloc;
104}
105template <class T>
106bool operator!=(const CallbackAllocator<T>& alloc1,
107 const CallbackAllocator<T>& alloc2) {
108 return !(alloc1 == alloc2);
109}
110
Michael Lentine03c64b02015-08-26 18:27:26 -0500111template <class T>
Jesse Hall1f91d392015-12-11 16:28:44 -0800112using Vector = std::vector<T, CallbackAllocator<T>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500113
Jesse Hall1f91d392015-12-11 16:28:44 -0800114typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
115 String;
Michael Lentine03c64b02015-08-26 18:27:26 -0500116
Jesse Hall1f91d392015-12-11 16:28:44 -0800117// ----------------------------------------------------------------------------
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500118
Jesse Halle1b12782015-11-30 11:27:32 -0800119VKAPI_ATTR void* DefaultAllocate(void*,
120 size_t size,
121 size_t alignment,
122 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800123 void* ptr = nullptr;
124 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
125 // additionally requires that it be at least sizeof(void*).
Jesse Hall26cecff2016-01-21 19:52:25 -0800126 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
127 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
128 ret, ptr);
129 return ret == 0 ? ptr : nullptr;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800130}
131
Jesse Halle1b12782015-11-30 11:27:32 -0800132VKAPI_ATTR void* DefaultReallocate(void*,
133 void* ptr,
134 size_t size,
135 size_t alignment,
136 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800137 if (size == 0) {
138 free(ptr);
139 return nullptr;
140 }
141
142 // TODO(jessehall): Right now we never shrink allocations; if the new
143 // request is smaller than the existing chunk, we just continue using it.
144 // Right now the loader never reallocs, so this doesn't matter. If that
145 // changes, or if this code is copied into some other project, this should
146 // probably have a heuristic to allocate-copy-free when doing so will save
147 // "enough" space.
148 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
149 if (size <= old_size)
150 return ptr;
151
152 void* new_ptr = nullptr;
153 if (posix_memalign(&new_ptr, alignment, size) != 0)
154 return nullptr;
155 if (ptr) {
156 memcpy(new_ptr, ptr, std::min(old_size, size));
157 free(ptr);
158 }
159 return new_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700160}
161
Jesse Hall26cecff2016-01-21 19:52:25 -0800162VKAPI_ATTR void DefaultFree(void*, void* ptr) {
163 ALOGD_CALLSTACK("Free: %p", ptr);
164 free(ptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700165}
166
Jesse Hall3fbc8562015-11-29 22:10:52 -0800167const VkAllocationCallbacks kDefaultAllocCallbacks = {
Jesse Hall04f4f472015-08-16 19:51:04 -0700168 .pUserData = nullptr,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800169 .pfnAllocation = DefaultAllocate,
170 .pfnReallocation = DefaultReallocate,
Jesse Hall04f4f472015-08-16 19:51:04 -0700171 .pfnFree = DefaultFree,
172};
173
Jesse Hall1f91d392015-12-11 16:28:44 -0800174// ----------------------------------------------------------------------------
Jesse Hall80523e22016-01-06 16:47:54 -0800175// Global Data and Initialization
Jesse Hall1f91d392015-12-11 16:28:44 -0800176
Jesse Hall80523e22016-01-06 16:47:54 -0800177hwvulkan_device_t* g_hwdevice = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800178InstanceExtensionSet g_driver_instance_extensions;
179
Jesse Hall80523e22016-01-06 16:47:54 -0800180void LoadVulkanHAL() {
181 static const hwvulkan_module_t* module;
182 int result =
183 hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
184 if (result != 0) {
185 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result);
186 return;
187 }
188 result = module->common.methods->open(
189 &module->common, HWVULKAN_DEVICE_0,
190 reinterpret_cast<hw_device_t**>(&g_hwdevice));
191 if (result != 0) {
192 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
193 result);
194 module = nullptr;
195 return;
196 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800197
198 VkResult vkresult;
199 uint32_t count;
200 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
201 nullptr, &count, nullptr)) != VK_SUCCESS) {
202 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
203 vkresult);
204 g_hwdevice->common.close(&g_hwdevice->common);
205 g_hwdevice = nullptr;
206 module = nullptr;
207 return;
208 }
209 VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
210 alloca(count * sizeof(VkExtensionProperties)));
211 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
212 nullptr, &count, extensions)) != VK_SUCCESS) {
213 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
214 vkresult);
215 g_hwdevice->common.close(&g_hwdevice->common);
216 g_hwdevice = nullptr;
217 module = nullptr;
218 return;
219 }
220 ALOGV_IF(count > 0, "Driver-supported instance extensions:");
221 for (uint32_t i = 0; i < count; i++) {
222 ALOGV(" %s (v%u)", extensions[i].extensionName,
223 extensions[i].specVersion);
224 InstanceExtension id =
225 InstanceExtensionFromName(extensions[i].extensionName);
226 if (id != kInstanceExtensionCount)
227 g_driver_instance_extensions.set(id);
228 }
229 // Ignore driver attempts to support loader extensions
230 g_driver_instance_extensions.reset(kKHR_surface);
231 g_driver_instance_extensions.reset(kKHR_android_surface);
Jesse Hall80523e22016-01-06 16:47:54 -0800232}
233
Jesse Hall04f4f472015-08-16 19:51:04 -0700234bool EnsureInitialized() {
235 static std::once_flag once_flag;
Jesse Hall04f4f472015-08-16 19:51:04 -0700236 std::call_once(once_flag, []() {
Jesse Hall80523e22016-01-06 16:47:54 -0800237 LoadVulkanHAL();
238 DiscoverLayers();
Jesse Hall04f4f472015-08-16 19:51:04 -0700239 });
Jesse Hall80523e22016-01-06 16:47:54 -0800240 return g_hwdevice != nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700241}
242
Jesse Hall1f91d392015-12-11 16:28:44 -0800243// -----------------------------------------------------------------------------
244
245struct Instance {
246 Instance(const VkAllocationCallbacks* alloc_callbacks)
247 : dispatch_ptr(&dispatch),
248 handle(reinterpret_cast<VkInstance>(&dispatch_ptr)),
Jesse Hall1f91d392015-12-11 16:28:44 -0800249 alloc(alloc_callbacks),
250 num_physical_devices(0),
Jesse Hall80523e22016-01-06 16:47:54 -0800251 active_layers(CallbackAllocator<LayerRef>(alloc)),
Jesse Hall1f91d392015-12-11 16:28:44 -0800252 message(VK_NULL_HANDLE) {
253 memset(&dispatch, 0, sizeof(dispatch));
254 memset(physical_devices, 0, sizeof(physical_devices));
Jesse Hall1f91d392015-12-11 16:28:44 -0800255 drv.instance = VK_NULL_HANDLE;
256 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
257 drv.num_physical_devices = 0;
258 }
259
Jesse Hall80523e22016-01-06 16:47:54 -0800260 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800261
262 const InstanceDispatchTable* dispatch_ptr;
263 const VkInstance handle;
264 InstanceDispatchTable dispatch;
265
Jesse Hall1f91d392015-12-11 16:28:44 -0800266 const VkAllocationCallbacks* alloc;
267 uint32_t num_physical_devices;
268 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800269 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800270
Jesse Hall80523e22016-01-06 16:47:54 -0800271 Vector<LayerRef> active_layers;
Jesse Hall715b86a2016-01-16 16:34:29 -0800272 VkDebugReportCallbackEXT message;
273 DebugReportCallbackList debug_report_callbacks;
Jesse Hall1f91d392015-12-11 16:28:44 -0800274
275 struct {
276 VkInstance instance;
277 DriverDispatchTable dispatch;
278 uint32_t num_physical_devices;
279 } drv; // may eventually be an array
280};
281
282struct Device {
283 Device(Instance* instance_)
284 : instance(instance_),
Jesse Hall80523e22016-01-06 16:47:54 -0800285 active_layers(CallbackAllocator<LayerRef>(instance->alloc)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800286 memset(&dispatch, 0, sizeof(dispatch));
287 }
288 DeviceDispatchTable dispatch;
289 Instance* instance;
290 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Jesse Hall80523e22016-01-06 16:47:54 -0800291 Vector<LayerRef> active_layers;
Jesse Hall1f91d392015-12-11 16:28:44 -0800292};
293
294template <typename THandle>
295struct HandleTraits {};
296template <>
297struct HandleTraits<VkInstance> {
298 typedef Instance LoaderObjectType;
299};
300template <>
301struct HandleTraits<VkPhysicalDevice> {
302 typedef Instance LoaderObjectType;
303};
304template <>
305struct HandleTraits<VkDevice> {
306 typedef Device LoaderObjectType;
307};
308template <>
309struct HandleTraits<VkQueue> {
310 typedef Device LoaderObjectType;
311};
312template <>
313struct HandleTraits<VkCommandBuffer> {
314 typedef Device LoaderObjectType;
315};
316
317template <typename THandle>
318typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
319 THandle handle) {
320 // TODO(jessehall): Make Instance and Device POD types (by removing the
321 // non-default constructors), so that offsetof is actually legal to use.
322 // The specific case we're using here is safe in gcc/clang (and probably
323 // most other C++ compilers), but isn't guaranteed by C++.
324 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
325#pragma clang diagnostic push
326#pragma clang diagnostic ignored "-Winvalid-offsetof"
327 const size_t kDispatchOffset = offsetof(ObjectType, dispatch);
328#pragma clang diagnostic pop
329
330 const auto& dispatch = GetDispatchTable(handle);
331 uintptr_t dispatch_addr = reinterpret_cast<uintptr_t>(&dispatch);
332 uintptr_t object_addr = dispatch_addr - kDispatchOffset;
333 return *reinterpret_cast<ObjectType*>(object_addr);
334}
335
336// -----------------------------------------------------------------------------
337
Jesse Hall04f4f472015-08-16 19:51:04 -0700338void DestroyDevice(Device* device) {
Jesse Hall3fbc8562015-11-29 22:10:52 -0800339 const VkAllocationCallbacks* alloc = device->instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700340 device->~Device();
341 alloc->pfnFree(alloc->pUserData, device);
342}
343
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500344template <class TObject>
Jesse Hallaa410942016-01-17 13:07:10 -0800345LayerRef GetLayerRef(const char* name);
346template <>
347LayerRef GetLayerRef<Instance>(const char* name) {
348 return GetInstanceLayerRef(name);
349}
350template <>
351LayerRef GetLayerRef<Device>(const char* name) {
352 return GetDeviceLayerRef(name);
353}
354
355template <class TObject>
Jesse Hall80523e22016-01-06 16:47:54 -0800356bool ActivateLayer(TObject* object, const char* name) {
Jesse Hallaa410942016-01-17 13:07:10 -0800357 LayerRef layer(GetLayerRef<TObject>(name));
Jesse Hall80523e22016-01-06 16:47:54 -0800358 if (!layer)
359 return false;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500360 if (std::find(object->active_layers.begin(), object->active_layers.end(),
Jesse Hall26cecff2016-01-21 19:52:25 -0800361 layer) == object->active_layers.end()) {
362 try {
363 object->active_layers.push_back(std::move(layer));
364 } catch (std::bad_alloc&) {
365 // TODO(jessehall): We should fail with VK_ERROR_OUT_OF_MEMORY
366 // if we can't enable a requested layer. Callers currently ignore
367 // ActivateLayer's return value.
368 ALOGW("failed to activate layer '%s': out of memory", name);
369 return false;
370 }
371 }
Jesse Hall80523e22016-01-06 16:47:54 -0800372 ALOGV("activated layer '%s'", name);
373 return true;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500374}
375
Michael Lentine9da191b2015-10-13 11:08:45 -0500376struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500377 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500378 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500379};
380
Michael Lentine9da191b2015-10-13 11:08:45 -0500381void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500382 const char* value,
383 void* data) {
Jesse Hall26cecff2016-01-21 19:52:25 -0800384 try {
385 const char prefix[] = "debug.vulkan.layer.";
386 const size_t prefixlen = sizeof(prefix) - 1;
387 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
388 return;
389 const char* number_str = name + prefixlen;
390 long layer_number = strtol(number_str, nullptr, 10);
391 if (layer_number <= 0 || layer_number == LONG_MAX) {
392 ALOGW("Cannot use a layer at number %ld from string %s",
393 layer_number, number_str);
394 return;
395 }
396 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
397 Vector<String>* layer_names = instance_names_pair->layer_names;
398 Instance* instance = instance_names_pair->instance;
399 size_t layer_size = static_cast<size_t>(layer_number);
400 if (layer_size > layer_names->size()) {
401 layer_names->resize(
402 layer_size, String(CallbackAllocator<char>(instance->alloc)));
403 }
404 (*layer_names)[layer_size - 1] = value;
405 } catch (std::bad_alloc&) {
406 ALOGW("failed to handle property '%s'='%s': out of memory", name,
407 value);
Michael Lentine9da191b2015-10-13 11:08:45 -0500408 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500409 }
410}
411
412template <class TInfo, class TObject>
Jesse Hall1f91d392015-12-11 16:28:44 -0800413VkResult ActivateAllLayers(TInfo create_info,
414 Instance* instance,
415 TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500416 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
417 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
418 "Cannot activate layers for unknown object %p", object);
419 CallbackAllocator<char> string_allocator(instance->alloc);
420 // Load system layers
Jesse Hall21597662015-12-18 13:48:24 -0800421 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500422 char layer_prop[PROPERTY_VALUE_MAX];
423 property_get("debug.vulkan.layers", layer_prop, "");
Jesse Hall26cecff2016-01-21 19:52:25 -0800424 char* strtok_state;
425 char* layer_name = nullptr;
426 while ((layer_name = strtok_r(layer_name ? nullptr : layer_prop, ":",
427 &strtok_state))) {
428 ActivateLayer(object, layer_name);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500429 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500430 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
431 InstanceNamesPair instance_names_pair = {.instance = instance,
432 .layer_names = &layer_names};
433 property_list(SetLayerNamesFromProperty,
434 static_cast<void*>(&instance_names_pair));
435 for (auto layer_name_element : layer_names) {
Jesse Hall80523e22016-01-06 16:47:54 -0800436 ActivateLayer(object, layer_name_element.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500437 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500438 }
439 // Load app layers
Jesse Hall3dd678a2016-01-08 21:52:01 -0800440 for (uint32_t i = 0; i < create_info->enabledLayerCount; ++i) {
Jesse Hall80523e22016-01-06 16:47:54 -0800441 if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700442 ALOGE("requested %s layer '%s' not present",
Jesse Hall1f91d392015-12-11 16:28:44 -0800443 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
444 ? "instance"
445 : "device",
Jesse Hall80523e22016-01-06 16:47:54 -0800446 create_info->ppEnabledLayerNames[i]);
Jesse Hall9a16f972015-10-28 15:59:53 -0700447 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500448 }
449 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700450 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500451}
452
453template <class TCreateInfo>
454bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
455 const char* extension_name,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800456 const VkAllocationCallbacks* alloc) {
Jesse Hall3dd678a2016-01-08 21:52:01 -0800457 for (uint32_t i = 0; i < local_create_info.enabledExtensionCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500458 if (!strcmp(extension_name,
459 local_create_info.ppEnabledExtensionNames[i])) {
460 return false;
461 }
462 }
Jesse Hall3dd678a2016-01-08 21:52:01 -0800463 uint32_t extension_count = local_create_info.enabledExtensionCount;
464 local_create_info.enabledExtensionCount++;
Jesse Hall3fbc8562015-11-29 22:10:52 -0800465 void* mem = alloc->pfnAllocation(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800466 alloc->pUserData,
Jesse Hall3dd678a2016-01-08 21:52:01 -0800467 local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*),
468 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500469 if (mem) {
470 const char** enabled_extensions = static_cast<const char**>(mem);
471 for (uint32_t i = 0; i < extension_count; ++i) {
472 enabled_extensions[i] =
473 local_create_info.ppEnabledExtensionNames[i];
474 }
475 enabled_extensions[extension_count] = extension_name;
476 local_create_info.ppEnabledExtensionNames = enabled_extensions;
477 } else {
478 ALOGW("%s extension cannot be enabled: memory allocation failed",
479 extension_name);
Jesse Hall3dd678a2016-01-08 21:52:01 -0800480 local_create_info.enabledExtensionCount--;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500481 return false;
482 }
483 return true;
484}
485
486template <class T>
487void FreeAllocatedCreateInfo(T& local_create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800488 const VkAllocationCallbacks* alloc) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500489 alloc->pfnFree(
490 alloc->pUserData,
491 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
492}
493
Jesse Halle1b12782015-11-30 11:27:32 -0800494VKAPI_ATTR
Jesse Hall715b86a2016-01-16 16:34:29 -0800495VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags,
496 VkDebugReportObjectTypeEXT /*objectType*/,
497 uint64_t /*object*/,
Michael Lentineeb970862015-10-15 12:42:22 -0500498 size_t /*location*/,
499 int32_t message_code,
500 const char* layer_prefix,
501 const char* message,
502 void* /*user_data*/) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800503 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500504 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
Jesse Hall715b86a2016-01-16 16:34:29 -0800505 } else if (flags & VK_DEBUG_REPORT_WARN_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500506 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
507 }
Michael Lentineeb970862015-10-15 12:42:22 -0500508 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500509}
510
Jesse Hall06193802015-12-03 16:12:51 -0800511VkResult Noop() {
Michael Lentine03c64b02015-08-26 18:27:26 -0500512 return VK_SUCCESS;
513}
514
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700515/*
516 * This function will return the pNext pointer of any
517 * CreateInfo extensions that are not loader extensions.
518 * This is used to skip past the loader extensions prepended
519 * to the list during CreateInstance and CreateDevice.
520 */
521void* StripCreateExtensions(const void* pNext) {
522 VkLayerInstanceCreateInfo* create_info =
523 const_cast<VkLayerInstanceCreateInfo*>(
524 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
525
526 while (
527 create_info &&
528 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
529 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
530 create_info = const_cast<VkLayerInstanceCreateInfo*>(
531 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
532 }
533
534 return create_info;
535}
536
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700537// Separate out cleaning up the layers and instance storage
538// to avoid code duplication in the many failure cases in
539// in CreateInstance_Top
540void TeardownInstance(
541 VkInstance vkinstance,
542 const VkAllocationCallbacks* /* allocator */) {
543 Instance& instance = GetDispatchParent(vkinstance);
544 instance.active_layers.clear();
545 const VkAllocationCallbacks* alloc = instance.alloc;
546 instance.~Instance();
547 alloc->pfnFree(alloc->pUserData, &instance);
548}
549
Jesse Hall1f91d392015-12-11 16:28:44 -0800550} // anonymous namespace
551
552namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500553
Jesse Hall04f4f472015-08-16 19:51:04 -0700554// -----------------------------------------------------------------------------
555// "Bottom" functions. These are called at the end of the instance dispatch
556// chain.
557
Jesse Hall1f91d392015-12-11 16:28:44 -0800558VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
559 const VkAllocationCallbacks* allocator,
560 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700561 VkResult result;
562
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700563 VkLayerInstanceCreateInfo* chain_info =
564 const_cast<VkLayerInstanceCreateInfo*>(
565 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
566 while (
567 chain_info &&
568 !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO &&
569 chain_info->function == VK_LAYER_FUNCTION_INSTANCE)) {
570 chain_info = const_cast<VkLayerInstanceCreateInfo*>(
571 static_cast<const VkLayerInstanceCreateInfo*>(chain_info->pNext));
572 }
573 ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!");
574
575 Instance& instance = GetDispatchParent(
576 static_cast<VkInstance>(chain_info->u.instanceInfo.instance_info));
577
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800578 // Check that all enabled extensions are supported
579 InstanceExtensionSet enabled_extensions;
580 uint32_t num_driver_extensions = 0;
581 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
582 const char* name = create_info->ppEnabledExtensionNames[i];
583 InstanceExtension id = InstanceExtensionFromName(name);
584 if (id != kInstanceExtensionCount) {
585 if (g_driver_instance_extensions[id]) {
586 num_driver_extensions++;
587 enabled_extensions.set(id);
588 continue;
589 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700590 if (id == kKHR_surface || id == kKHR_android_surface) {
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800591 enabled_extensions.set(id);
592 continue;
593 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700594 // The loader natively supports debug report.
595 if (id == kEXT_debug_report) {
596 continue;
597 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800598 }
599 bool supported = false;
600 for (const auto& layer : instance.active_layers) {
601 if (layer.SupportsExtension(name))
602 supported = true;
603 }
604 if (!supported) {
605 ALOGE(
606 "requested instance extension '%s' not supported by "
607 "loader, driver, or any active layers",
608 name);
609 DestroyInstance_Bottom(instance.handle, allocator);
610 return VK_ERROR_EXTENSION_NOT_PRESENT;
611 }
612 }
613
Jesse Halla7ac76d2016-01-08 22:29:42 -0800614 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700615 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800616 driver_create_info.enabledLayerCount = 0;
617 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800618 driver_create_info.enabledExtensionCount = 0;
619 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800620 if (num_driver_extensions > 0) {
621 const char** names = static_cast<const char**>(
622 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800623 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800624 const char* name = create_info->ppEnabledExtensionNames[i];
625 InstanceExtension id = InstanceExtensionFromName(name);
626 if (id != kInstanceExtensionCount) {
627 if (g_driver_instance_extensions[id]) {
628 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800629 continue;
630 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800631 }
632 }
633 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800634 ALOG_ASSERT(
635 driver_create_info.enabledExtensionCount == num_driver_extensions,
636 "counted enabled driver instance extensions twice and got "
637 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800638 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800639
640 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Jesse Hall1f91d392015-12-11 16:28:44 -0800641 &instance.drv.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700642 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800643 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700644 return result;
645 }
646
Jesse Hall1f91d392015-12-11 16:28:44 -0800647 hwvulkan_dispatch_t* drv_dispatch =
648 reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700649 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700650 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800651 drv_dispatch->magic);
652 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700653 return VK_ERROR_INITIALIZATION_FAILED;
654 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700655 // Skip setting drv_dispatch->vtbl, since we never call through it;
656 // we go through instance.drv.dispatch instead.
Jesse Hall04f4f472015-08-16 19:51:04 -0700657
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700658 if (!LoadDriverDispatchTable(instance.drv.instance,
659 g_hwdevice->GetInstanceProcAddr,
660 enabled_extensions, instance.drv.dispatch)) {
661 DestroyInstance_Bottom(instance.handle, allocator);
662 return VK_ERROR_INITIALIZATION_FAILED;
663 }
664
Jesse Hall04f4f472015-08-16 19:51:04 -0700665 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800666 result = instance.drv.dispatch.EnumeratePhysicalDevices(
667 instance.drv.instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700668 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800669 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700670 return VK_ERROR_INITIALIZATION_FAILED;
671 }
672 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800673 result = instance.drv.dispatch.EnumeratePhysicalDevices(
674 instance.drv.instance, &num_physical_devices,
675 instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700676 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800677 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700678 return VK_ERROR_INITIALIZATION_FAILED;
679 }
Jesse Hallb1471272016-01-17 21:36:58 -0800680
681 Vector<VkExtensionProperties> extensions(
682 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700683 for (uint32_t i = 0; i < num_physical_devices; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800684 hwvulkan_dispatch_t* pdev_dispatch =
685 reinterpret_cast<hwvulkan_dispatch_t*>(
686 instance.physical_devices[i]);
687 if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700688 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800689 pdev_dispatch->magic);
690 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700691 return VK_ERROR_INITIALIZATION_FAILED;
692 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800693 pdev_dispatch->vtbl = instance.dispatch_ptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800694
695 uint32_t count;
696 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
697 instance.physical_devices[i], nullptr, &count, nullptr)) !=
698 VK_SUCCESS) {
699 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
700 result);
701 continue;
702 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800703 try {
704 extensions.resize(count);
705 } catch (std::bad_alloc&) {
706 ALOGE("instance creation failed: out of memory");
707 DestroyInstance_Bottom(instance.handle, allocator);
708 return VK_ERROR_OUT_OF_HOST_MEMORY;
709 }
Jesse Hallb1471272016-01-17 21:36:58 -0800710 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
711 instance.physical_devices[i], nullptr, &count,
712 extensions.data())) != VK_SUCCESS) {
713 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
714 result);
715 continue;
716 }
717 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
718 for (const auto& extension : extensions) {
719 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
720 DeviceExtension id =
721 DeviceExtensionFromName(extension.extensionName);
722 if (id == kDeviceExtensionCount) {
723 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
724 extension.extensionName);
725 } else {
726 instance.physical_device_driver_extensions[i].set(id);
727 }
728 }
729 // Ignore driver attempts to support loader extensions
730 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700731 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800732 instance.drv.num_physical_devices = num_physical_devices;
Jesse Hall1f91d392015-12-11 16:28:44 -0800733 instance.num_physical_devices = instance.drv.num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800734
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700735 *vkinstance = instance.handle;
736
Jesse Hall04f4f472015-08-16 19:51:04 -0700737 return VK_SUCCESS;
738}
739
Jesse Hall1f91d392015-12-11 16:28:44 -0800740PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) {
741 PFN_vkVoidFunction pfn;
742 if ((pfn = GetLoaderBottomProcAddr(name)))
743 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -0800744 return nullptr;
745}
746
747VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
748 uint32_t* pdev_count,
749 VkPhysicalDevice* pdevs) {
750 Instance& instance = GetDispatchParent(vkinstance);
751 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700752 if (pdevs) {
753 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800754 std::copy(instance.physical_devices, instance.physical_devices + count,
755 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700756 }
757 *pdev_count = count;
758 return VK_SUCCESS;
759}
760
Jesse Hall1f91d392015-12-11 16:28:44 -0800761void GetPhysicalDeviceProperties_Bottom(
762 VkPhysicalDevice pdev,
763 VkPhysicalDeviceProperties* properties) {
764 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
765 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700766}
767
Jesse Hall1f91d392015-12-11 16:28:44 -0800768void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
769 VkPhysicalDeviceFeatures* features) {
770 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
771 features);
772}
773
774void GetPhysicalDeviceMemoryProperties_Bottom(
775 VkPhysicalDevice pdev,
776 VkPhysicalDeviceMemoryProperties* properties) {
777 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
778 pdev, properties);
779}
780
781void GetPhysicalDeviceQueueFamilyProperties_Bottom(
782 VkPhysicalDevice pdev,
783 uint32_t* pCount,
784 VkQueueFamilyProperties* properties) {
785 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
786 pdev, pCount, properties);
787}
788
789void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
790 VkFormat format,
791 VkFormatProperties* properties) {
792 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700793 pdev, format, properties);
794}
795
Jesse Hall1f91d392015-12-11 16:28:44 -0800796VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700797 VkPhysicalDevice pdev,
798 VkFormat format,
799 VkImageType type,
800 VkImageTiling tiling,
801 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700802 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700803 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800804 return GetDispatchParent(pdev)
805 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800806 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700807}
808
Jesse Hall1f91d392015-12-11 16:28:44 -0800809void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700810 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800811 VkFormat format,
812 VkImageType type,
813 VkSampleCountFlagBits samples,
814 VkImageUsageFlags usage,
815 VkImageTiling tiling,
816 uint32_t* properties_count,
817 VkSparseImageFormatProperties* properties) {
818 GetDispatchParent(pdev)
819 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
820 pdev, format, type, samples, usage, tiling, properties_count,
821 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700822}
823
Jesse Halle1b12782015-11-30 11:27:32 -0800824VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800825VkResult EnumerateDeviceExtensionProperties_Bottom(
Jesse Hallb1471272016-01-17 21:36:58 -0800826 VkPhysicalDevice gpu,
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800827 const char* layer_name,
Jesse Hall1f91d392015-12-11 16:28:44 -0800828 uint32_t* properties_count,
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800829 VkExtensionProperties* properties) {
830 const VkExtensionProperties* extensions = nullptr;
831 uint32_t num_extensions = 0;
832 if (layer_name) {
833 GetDeviceLayerExtensions(layer_name, &extensions, &num_extensions);
834 } else {
Jesse Hallb1471272016-01-17 21:36:58 -0800835 Instance& instance = GetDispatchParent(gpu);
836 size_t gpu_idx = 0;
837 while (instance.physical_devices[gpu_idx] != gpu)
838 gpu_idx++;
839 const DeviceExtensionSet driver_extensions =
840 instance.physical_device_driver_extensions[gpu_idx];
841
842 // We only support VK_KHR_swapchain if the GPU supports
843 // VK_ANDROID_native_buffer
844 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
845 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
846 if (driver_extensions[kANDROID_native_buffer]) {
847 available[num_extensions++] = VkExtensionProperties{
848 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
849 }
850
851 // TODO(jessehall): We need to also enumerate extensions supported by
852 // implicitly-enabled layers. Currently we don't have that list of
853 // layers until instance creation.
854 extensions = available;
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800855 }
856
857 if (!properties || *properties_count > num_extensions)
858 *properties_count = num_extensions;
859 if (properties)
860 std::copy(extensions, extensions + *properties_count, properties);
861 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700862}
863
Jesse Halle1b12782015-11-30 11:27:32 -0800864VKAPI_ATTR
Jesse Hall80523e22016-01-06 16:47:54 -0800865VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800866 uint32_t* properties_count,
Jesse Hallaa410942016-01-17 13:07:10 -0800867 VkLayerProperties* properties) {
868 uint32_t layer_count =
869 EnumerateDeviceLayers(properties ? *properties_count : 0, properties);
870 if (!properties || *properties_count > layer_count)
871 *properties_count = layer_count;
872 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800873}
874
875VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800876VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800877 const VkDeviceCreateInfo* create_info,
878 const VkAllocationCallbacks* allocator,
879 VkDevice* device_out) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700880 VkLayerDeviceCreateInfo* chain_info = const_cast<VkLayerDeviceCreateInfo*>(
881 static_cast<const VkLayerDeviceCreateInfo*>(create_info->pNext));
882 while (chain_info &&
883 !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO &&
884 chain_info->function == VK_LAYER_FUNCTION_DEVICE)) {
885 chain_info = const_cast<VkLayerDeviceCreateInfo*>(
886 static_cast<const VkLayerDeviceCreateInfo*>(chain_info->pNext));
Jesse Hall9a16f972015-10-28 15:59:53 -0700887 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700888 ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!");
Jesse Hall9a16f972015-10-28 15:59:53 -0700889
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700890 Instance& instance = GetDispatchParent(gpu);
Jesse Hallb1471272016-01-17 21:36:58 -0800891 size_t gpu_idx = 0;
892 while (instance.physical_devices[gpu_idx] != gpu)
893 gpu_idx++;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700894 Device* device = static_cast<Device*>(chain_info->u.deviceInfo.device_info);
895 PFN_vkGetInstanceProcAddr get_instance_proc_addr =
896 chain_info->u.deviceInfo.pfnNextGetInstanceProcAddr;
897
898 VkDeviceCreateInfo driver_create_info = *create_info;
899 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
900 driver_create_info.enabledLayerCount = 0;
901 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800902
903 uint32_t num_driver_extensions = 0;
904 const char** driver_extensions = static_cast<const char**>(
905 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
906 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
907 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800908 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800909 if (id != kDeviceExtensionCount) {
910 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
911 driver_extensions[num_driver_extensions++] = name;
912 continue;
913 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700914 // Add the VK_ANDROID_native_buffer extension to the list iff
915 // the VK_KHR_swapchain extension was requested
Jesse Hallae3b70d2016-01-17 22:05:29 -0800916 if (id == kKHR_swapchain &&
917 instance.physical_device_driver_extensions
918 [gpu_idx][kANDROID_native_buffer]) {
919 driver_extensions[num_driver_extensions++] =
920 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
921 continue;
922 }
Jesse Hallb1471272016-01-17 21:36:58 -0800923 }
Jesse Hallb1471272016-01-17 21:36:58 -0800924 bool supported = false;
925 for (const auto& layer : device->active_layers) {
926 if (layer.SupportsExtension(name))
927 supported = true;
928 }
929 if (!supported) {
930 ALOGE(
Jesse Hallae3b70d2016-01-17 22:05:29 -0800931 "requested device extension '%s' not supported by loader, "
932 "driver, or any active layers",
Jesse Hallb1471272016-01-17 21:36:58 -0800933 name);
Jesse Hallb1471272016-01-17 21:36:58 -0800934 return VK_ERROR_EXTENSION_NOT_PRESENT;
935 }
936 }
937
Jesse Hallb1471272016-01-17 21:36:58 -0800938 driver_create_info.enabledExtensionCount = num_driver_extensions;
939 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Hall04f4f472015-08-16 19:51:04 -0700940 VkDevice drv_device;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700941 VkResult result = instance.drv.dispatch.CreateDevice(
942 gpu, &driver_create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700943 if (result != VK_SUCCESS) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700944 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700945 }
946
Jesse Hall1f91d392015-12-11 16:28:44 -0800947 hwvulkan_dispatch_t* drv_dispatch =
Jesse Hall04f4f472015-08-16 19:51:04 -0700948 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800949 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
950 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
951 drv_dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500952 PFN_vkDestroyDevice destroy_device =
953 reinterpret_cast<PFN_vkDestroyDevice>(
Jesse Hall1f91d392015-12-11 16:28:44 -0800954 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
955 "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800956 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700957 return VK_ERROR_INITIALIZATION_FAILED;
958 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700959
960 // Set dispatch table for newly created Device
961 // CreateDevice_Top will fill in the details
Jesse Hall1f91d392015-12-11 16:28:44 -0800962 drv_dispatch->vtbl = &device->dispatch;
963 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
964 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
965 "vkGetDeviceProcAddr"));
Jesse Hall1f91d392015-12-11 16:28:44 -0800966 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700967 return VK_SUCCESS;
968}
969
Jesse Hall1f91d392015-12-11 16:28:44 -0800970void DestroyInstance_Bottom(VkInstance vkinstance,
971 const VkAllocationCallbacks* allocator) {
972 Instance& instance = GetDispatchParent(vkinstance);
973
974 // These checks allow us to call DestroyInstance_Bottom from any error
975 // path in CreateInstance_Bottom, before the driver instance is fully
976 // initialized.
977 if (instance.drv.instance != VK_NULL_HANDLE &&
978 instance.drv.dispatch.DestroyInstance) {
979 instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
980 }
981 if (instance.message) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800982 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback;
983 destroy_debug_report_callback =
984 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
985 vkGetInstanceProcAddr(vkinstance,
986 "vkDestroyDebugReportCallbackEXT"));
987 destroy_debug_report_callback(vkinstance, instance.message, allocator);
Jesse Hall1f91d392015-12-11 16:28:44 -0800988 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700989}
990
Jesse Hall1f91d392015-12-11 16:28:44 -0800991PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
992 const char* name) {
993 if (strcmp(name, "vkCreateDevice") == 0) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700994 return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom);
Michael Lentine03c64b02015-08-26 18:27:26 -0500995 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800996
997 // VK_ANDROID_native_buffer should be hidden from applications and layers.
998 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
999 PFN_vkVoidFunction pfn;
1000 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
1001 strcmp(name, "vkAcquireImageANDROID") == 0 ||
1002 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
1003 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -05001004 }
Jesse Hall1f91d392015-12-11 16:28:44 -08001005 if ((pfn = GetLoaderBottomProcAddr(name)))
1006 return pfn;
1007 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001008}
1009
Jesse Hall04f4f472015-08-16 19:51:04 -07001010// -----------------------------------------------------------------------------
Jesse Hall1f91d392015-12-11 16:28:44 -08001011// Loader top functions. These are called directly from the loader entry
1012// points or from the application (via vkGetInstanceProcAddr) without going
1013// through a dispatch table.
Jesse Hall04f4f472015-08-16 19:51:04 -07001014
Jesse Hall1f91d392015-12-11 16:28:44 -08001015VkResult EnumerateInstanceExtensionProperties_Top(
Jesse Hall80523e22016-01-06 16:47:54 -08001016 const char* layer_name,
1017 uint32_t* properties_count,
1018 VkExtensionProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001019 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001020 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001021
Jesse Hall80523e22016-01-06 16:47:54 -08001022 const VkExtensionProperties* extensions = nullptr;
1023 uint32_t num_extensions = 0;
1024 if (layer_name) {
Jesse Hallaa410942016-01-17 13:07:10 -08001025 GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions);
Jesse Hall80523e22016-01-06 16:47:54 -08001026 } else {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -08001027 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
1028 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
1029 available[num_extensions++] = VkExtensionProperties{
1030 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
1031 available[num_extensions++] =
1032 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
1033 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
1034 if (g_driver_instance_extensions[kEXT_debug_report]) {
1035 available[num_extensions++] =
1036 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
1037 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
1038 }
Jesse Hall80523e22016-01-06 16:47:54 -08001039 // TODO(jessehall): We need to also enumerate extensions supported by
1040 // implicitly-enabled layers. Currently we don't have that list of
1041 // layers until instance creation.
Jesse Hall6bd5dfa2016-01-16 17:13:30 -08001042 extensions = available;
Jesse Hall80523e22016-01-06 16:47:54 -08001043 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001044
Jesse Hall80523e22016-01-06 16:47:54 -08001045 if (!properties || *properties_count > num_extensions)
1046 *properties_count = num_extensions;
1047 if (properties)
1048 std::copy(extensions, extensions + *properties_count, properties);
1049 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001050}
1051
Jesse Hall80523e22016-01-06 16:47:54 -08001052VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
1053 VkLayerProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001054 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001055 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001056
Jesse Hall80523e22016-01-06 16:47:54 -08001057 uint32_t layer_count =
Jesse Hallaa410942016-01-17 13:07:10 -08001058 EnumerateInstanceLayers(properties ? *properties_count : 0, properties);
Jesse Hall80523e22016-01-06 16:47:54 -08001059 if (!properties || *properties_count > layer_count)
1060 *properties_count = layer_count;
1061 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001062}
1063
Jesse Hall1f91d392015-12-11 16:28:44 -08001064VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
1065 const VkAllocationCallbacks* allocator,
1066 VkInstance* instance_out) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001067 VkResult result;
1068
1069 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001070 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001071
Jesse Hall03b6fe12015-11-24 12:44:21 -08001072 if (!allocator)
1073 allocator = &kDefaultAllocCallbacks;
1074
Jesse Hall04f4f472015-08-16 19:51:04 -07001075 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -07001076 create_info = &local_create_info;
1077
Jesse Hall3fbc8562015-11-29 22:10:52 -08001078 void* instance_mem = allocator->pfnAllocation(
1079 allocator->pUserData, sizeof(Instance), alignof(Instance),
1080 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -07001081 if (!instance_mem)
1082 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001083 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -07001084
Jesse Hall9a16f972015-10-28 15:59:53 -07001085 result = ActivateAllLayers(create_info, instance, instance);
1086 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001087 DestroyInstance_Bottom(instance->handle, allocator);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001088 TeardownInstance(instance->handle, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -07001089 return result;
1090 }
Michael Lentine03c64b02015-08-26 18:27:26 -05001091
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001092 uint32_t activated_layers = 0;
1093 VkLayerInstanceCreateInfo chain_info;
1094 VkLayerInstanceLink* layer_instance_link_info = nullptr;
1095 PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom;
1096 VkInstance local_instance = nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -05001097
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001098 if (instance->active_layers.size() > 0) {
1099 chain_info.u.pLayerInfo = nullptr;
1100 chain_info.pNext = create_info->pNext;
1101 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
1102 chain_info.function = VK_LAYER_FUNCTION_LINK;
1103 local_create_info.pNext = &chain_info;
Michael Lentine03c64b02015-08-26 18:27:26 -05001104
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001105 layer_instance_link_info = static_cast<VkLayerInstanceLink*>(alloca(
1106 sizeof(VkLayerInstanceLink) * instance->active_layers.size()));
1107 if (!layer_instance_link_info) {
1108 ALOGE("Failed to alloc Instance objects for layers");
1109 DestroyInstance_Bottom(instance->handle, allocator);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001110 TeardownInstance(instance->handle, allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001111 return VK_ERROR_OUT_OF_HOST_MEMORY;
1112 }
1113
1114 /* Create instance chain of enabled layers */
1115 for (auto rit = instance->active_layers.rbegin();
1116 rit != instance->active_layers.rend(); ++rit) {
1117 LayerRef& layer = *rit;
1118 layer_instance_link_info[activated_layers].pNext =
1119 chain_info.u.pLayerInfo;
1120 layer_instance_link_info[activated_layers]
1121 .pfnNextGetInstanceProcAddr = next_gipa;
1122 chain_info.u.pLayerInfo =
1123 &layer_instance_link_info[activated_layers];
1124 next_gipa = layer.GetGetInstanceProcAddr();
1125
1126 ALOGV("Insert instance layer %s (v%u)", layer.GetName(),
1127 layer.GetSpecVersion());
1128
1129 activated_layers++;
Michael Lentine03c64b02015-08-26 18:27:26 -05001130 }
1131 }
1132
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001133 PFN_vkCreateInstance create_instance =
1134 reinterpret_cast<PFN_vkCreateInstance>(
1135 next_gipa(VK_NULL_HANDLE, "vkCreateInstance"));
1136 if (!create_instance) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001137 DestroyInstance_Bottom(instance->handle, allocator);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001138 TeardownInstance(instance->handle, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -05001139 return VK_ERROR_INITIALIZATION_FAILED;
1140 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001141 VkLayerInstanceCreateInfo instance_create_info;
1142
1143 instance_create_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
1144 instance_create_info.function = VK_LAYER_FUNCTION_INSTANCE;
1145
1146 instance_create_info.u.instanceInfo.instance_info = instance;
1147 instance_create_info.u.instanceInfo.pfnNextGetInstanceProcAddr = next_gipa;
1148
1149 instance_create_info.pNext = local_create_info.pNext;
1150 local_create_info.pNext = &instance_create_info;
1151
1152 result = create_instance(&local_create_info, allocator, &local_instance);
1153
1154 if (result != VK_SUCCESS) {
1155 DestroyInstance_Bottom(instance->handle, allocator);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001156 TeardownInstance(instance->handle, allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001157 return result;
1158 }
1159
1160 const InstanceDispatchTable& instance_dispatch =
1161 GetDispatchTable(local_instance);
1162 if (!LoadInstanceDispatchTable(
1163 local_instance, next_gipa,
1164 const_cast<InstanceDispatchTable&>(instance_dispatch))) {
1165 ALOGV("Failed to initialize instance dispatch table");
1166 PFN_vkDestroyInstance destroy_instance =
1167 reinterpret_cast<PFN_vkDestroyInstance>(
1168 next_gipa(VK_NULL_HANDLE, "vkDestroyInstance"));
1169 if (!destroy_instance) {
1170 ALOGD("Loader unable to find DestroyInstance");
1171 return VK_ERROR_INITIALIZATION_FAILED;
1172 }
1173 destroy_instance(local_instance, allocator);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001174 DestroyInstance_Bottom(instance->handle, allocator);
1175 TeardownInstance(instance->handle, allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001176 return VK_ERROR_INITIALIZATION_FAILED;
1177 }
1178 *instance_out = local_instance;
Michael Lentine03c64b02015-08-26 18:27:26 -05001179
Michael Lentine950bb4f2015-09-14 13:26:30 -05001180 // Force enable callback extension if required
Jesse Hall21597662015-12-18 13:48:24 -08001181 bool enable_callback = false;
1182 bool enable_logging = false;
1183 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
1184 enable_callback =
1185 property_get_bool("debug.vulkan.enable_callback", false);
1186 enable_logging = enable_callback;
1187 if (enable_callback) {
1188 enable_callback = AddExtensionToCreateInfo(
Jesse Hall715b86a2016-01-16 16:34:29 -08001189 local_create_info, "VK_EXT_debug_report", instance->alloc);
Jesse Hall21597662015-12-18 13:48:24 -08001190 }
Michael Lentine950bb4f2015-09-14 13:26:30 -05001191 }
1192
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001193 if (enable_logging) {
Jesse Hall715b86a2016-01-16 16:34:29 -08001194 const VkDebugReportCallbackCreateInfoEXT callback_create_info = {
1195 .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
1196 .flags =
1197 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT,
1198 .pfnCallback = LogDebugMessageCallback,
1199 };
1200 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
1201 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
1202 GetInstanceProcAddr_Top(instance->handle,
1203 "vkCreateDebugReportCallbackEXT"));
1204 create_debug_report_callback(instance->handle, &callback_create_info,
1205 allocator, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001206 }
1207
Jesse Hall04f4f472015-08-16 19:51:04 -07001208 return result;
1209}
1210
Jesse Hall1f91d392015-12-11 16:28:44 -08001211PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
1212 const char* name) {
1213 // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
1214 if (!vkinstance)
1215 return GetLoaderGlobalProcAddr(name);
1216
1217 const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
1218 PFN_vkVoidFunction pfn;
1219 // Always go through the loader-top function if there is one.
1220 if ((pfn = GetLoaderTopProcAddr(name)))
1221 return pfn;
1222 // Otherwise, look up the handler in the instance dispatch table
1223 if ((pfn = GetDispatchProcAddr(dispatch, name)))
1224 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -08001225 // Anything not handled already must be a device-dispatched function
1226 // without a loader-top. We must return a function that will dispatch based
1227 // on the dispatchable object parameter -- which is exactly what the
1228 // exported functions do. So just return them here.
1229 return GetLoaderExportProcAddr(name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001230}
1231
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001232void DestroyInstance_Top(VkInstance vkinstance,
Jesse Hall1f91d392015-12-11 16:28:44 -08001233 const VkAllocationCallbacks* allocator) {
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001234 if (!vkinstance)
Jesse Hall1f91d392015-12-11 16:28:44 -08001235 return;
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001236 GetDispatchTable(vkinstance).DestroyInstance(vkinstance, allocator);
1237
1238 TeardownInstance(vkinstance, allocator);
Jesse Hall1f91d392015-12-11 16:28:44 -08001239}
1240
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001241VKAPI_ATTR
1242VkResult CreateDevice_Top(VkPhysicalDevice gpu,
1243 const VkDeviceCreateInfo* create_info,
1244 const VkAllocationCallbacks* allocator,
1245 VkDevice* device_out) {
1246 Instance& instance = GetDispatchParent(gpu);
1247 VkResult result;
1248
1249 // FIXME(jessehall): We don't have good conventions or infrastructure yet to
1250 // do better than just using the instance allocator and scope for
1251 // everything. See b/26732122.
1252 if (true /*!allocator*/)
1253 allocator = instance.alloc;
1254
1255 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
1256 alignof(Device),
1257 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1258 if (!mem)
1259 return VK_ERROR_OUT_OF_HOST_MEMORY;
1260 Device* device = new (mem) Device(&instance);
1261
1262 result = ActivateAllLayers(create_info, &instance, device);
1263 if (result != VK_SUCCESS) {
1264 DestroyDevice(device);
1265 return result;
1266 }
1267
1268 size_t gpu_idx = 0;
1269 while (instance.physical_devices[gpu_idx] != gpu)
1270 gpu_idx++;
1271
1272 uint32_t activated_layers = 0;
1273 VkLayerDeviceCreateInfo chain_info;
1274 VkLayerDeviceLink* layer_device_link_info = nullptr;
1275 PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom;
1276 PFN_vkGetDeviceProcAddr next_gdpa = GetDeviceProcAddr_Bottom;
1277 VkDeviceCreateInfo local_create_info = *create_info;
1278 VkDevice local_device = nullptr;
1279
1280 if (device->active_layers.size() > 0) {
1281 chain_info.u.pLayerInfo = nullptr;
1282 chain_info.pNext = local_create_info.pNext;
1283 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
1284 chain_info.function = VK_LAYER_FUNCTION_LINK;
1285 local_create_info.pNext = &chain_info;
1286
1287 layer_device_link_info = static_cast<VkLayerDeviceLink*>(
1288 alloca(sizeof(VkLayerDeviceLink) * device->active_layers.size()));
1289 if (!layer_device_link_info) {
1290 ALOGE("Failed to alloc Device objects for layers");
1291 DestroyDevice(device);
1292 return VK_ERROR_OUT_OF_HOST_MEMORY;
1293 }
1294
1295 /* Create device chain of enabled layers */
1296 for (auto rit = device->active_layers.rbegin();
1297 rit != device->active_layers.rend(); ++rit) {
1298 LayerRef& layer = *rit;
1299 layer_device_link_info[activated_layers].pNext =
1300 chain_info.u.pLayerInfo;
1301 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
1302 next_gdpa;
1303 layer_device_link_info[activated_layers]
1304 .pfnNextGetInstanceProcAddr = next_gipa;
1305 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
1306
1307 next_gipa = layer.GetGetInstanceProcAddr();
1308 next_gdpa = layer.GetGetDeviceProcAddr();
1309
1310 ALOGV("Insert device layer %s (v%u)", layer.GetName(),
1311 layer.GetSpecVersion());
1312
1313 activated_layers++;
1314 }
1315 }
1316
1317 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
1318 next_gipa(VK_NULL_HANDLE, "vkCreateDevice"));
1319 if (!create_device) {
1320 ALOGE("Unable to find vkCreateDevice for driver");
1321 DestroyDevice(device);
1322 return VK_ERROR_INITIALIZATION_FAILED;
1323 }
1324
1325 VkLayerDeviceCreateInfo device_create_info;
1326
1327 device_create_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
1328 device_create_info.function = VK_LAYER_FUNCTION_DEVICE;
1329
1330 device_create_info.u.deviceInfo.device_info = device;
1331 device_create_info.u.deviceInfo.pfnNextGetInstanceProcAddr = next_gipa;
1332
1333 device_create_info.pNext = local_create_info.pNext;
1334 local_create_info.pNext = &device_create_info;
1335
1336 result = create_device(gpu, &local_create_info, allocator, &local_device);
1337
1338 if (result != VK_SUCCESS) {
1339 DestroyDevice(device);
1340 return result;
1341 }
1342
1343 // Set dispatch table for newly created Device
1344 hwvulkan_dispatch_t* vulkan_dispatch =
1345 reinterpret_cast<hwvulkan_dispatch_t*>(local_device);
1346 vulkan_dispatch->vtbl = &device->dispatch;
1347
1348 const DeviceDispatchTable& device_dispatch = GetDispatchTable(local_device);
1349 if (!LoadDeviceDispatchTable(
1350 local_device, next_gdpa,
1351 const_cast<DeviceDispatchTable&>(device_dispatch))) {
1352 ALOGV("Failed to initialize device dispatch table");
1353 PFN_vkDestroyDevice destroy_device =
1354 reinterpret_cast<PFN_vkDestroyDevice>(
1355 next_gipa(VK_NULL_HANDLE, "vkDestroyDevice"));
1356 ALOG_ASSERT(destroy_device != nullptr,
1357 "Loader unable to find DestroyDevice");
1358 destroy_device(local_device, allocator);
1359 return VK_ERROR_INITIALIZATION_FAILED;
1360 }
1361 *device_out = local_device;
1362
1363 return VK_SUCCESS;
1364}
1365
Jesse Hall1f91d392015-12-11 16:28:44 -08001366PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
1367 PFN_vkVoidFunction pfn;
Jesse Hall04f4f472015-08-16 19:51:04 -07001368 if (!device)
Jesse Hall1f91d392015-12-11 16:28:44 -08001369 return nullptr;
1370 if ((pfn = GetLoaderTopProcAddr(name)))
1371 return pfn;
1372 return GetDispatchProcAddr(GetDispatchTable(device), name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001373}
1374
Jesse Hall1f91d392015-12-11 16:28:44 -08001375void GetDeviceQueue_Top(VkDevice vkdevice,
1376 uint32_t family,
1377 uint32_t index,
1378 VkQueue* queue_out) {
1379 const auto& table = GetDispatchTable(vkdevice);
1380 table.GetDeviceQueue(vkdevice, family, index, queue_out);
1381 hwvulkan_dispatch_t* queue_dispatch =
1382 reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
1383 if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
1384 queue_dispatch->vtbl != &table)
1385 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
1386 queue_dispatch->magic);
1387 queue_dispatch->vtbl = &table;
Jesse Hall04f4f472015-08-16 19:51:04 -07001388}
1389
Jesse Hall1f91d392015-12-11 16:28:44 -08001390VkResult AllocateCommandBuffers_Top(
1391 VkDevice vkdevice,
1392 const VkCommandBufferAllocateInfo* alloc_info,
1393 VkCommandBuffer* cmdbufs) {
1394 const auto& table = GetDispatchTable(vkdevice);
1395 VkResult result =
1396 table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001397 if (result != VK_SUCCESS)
1398 return result;
Jesse Hall3dd678a2016-01-08 21:52:01 -08001399 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001400 hwvulkan_dispatch_t* cmdbuf_dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001401 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hall1f91d392015-12-11 16:28:44 -08001402 ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001403 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -08001404 cmdbuf_dispatch->magic);
1405 cmdbuf_dispatch->vtbl = &table;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001406 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001407 return VK_SUCCESS;
1408}
1409
Jesse Hall1f91d392015-12-11 16:28:44 -08001410void DestroyDevice_Top(VkDevice vkdevice,
1411 const VkAllocationCallbacks* /*allocator*/) {
1412 if (!vkdevice)
1413 return;
1414 Device& device = GetDispatchParent(vkdevice);
Jesse Hall1f91d392015-12-11 16:28:44 -08001415 device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
1416 DestroyDevice(&device);
Jesse Hall04f4f472015-08-16 19:51:04 -07001417}
1418
Jesse Hall1f91d392015-12-11 16:28:44 -08001419// -----------------------------------------------------------------------------
1420
1421const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
1422 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001423}
1424
Jesse Hall1f91d392015-12-11 16:28:44 -08001425const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
1426 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001427}
1428
Jesse Hall715b86a2016-01-16 16:34:29 -08001429VkInstance GetDriverInstance(VkInstance instance) {
1430 return GetDispatchParent(instance).drv.instance;
1431}
1432
1433const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
1434 return GetDispatchParent(instance).drv.dispatch;
1435}
1436
Jesse Hall1f91d392015-12-11 16:28:44 -08001437const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
1438 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001439}
1440
Jesse Hall1f91d392015-12-11 16:28:44 -08001441const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
1442 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001443}
1444
Jesse Hall715b86a2016-01-16 16:34:29 -08001445DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
1446 return GetDispatchParent(instance).debug_report_callbacks;
1447}
1448
Jesse Hall04f4f472015-08-16 19:51:04 -07001449} // namespace vulkan