blob: 602336cf61e463b5fac8cec66523a21887117555 [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
Michael Lentine57036832016-03-04 11:03:35 -0600453template <class TCreateInfo, class TObject>
454bool AddLayersToCreateInfo(TCreateInfo& local_create_info,
455 const TObject& object,
456 const VkAllocationCallbacks* alloc,
457 bool& allocatedMemory) {
458 // This should never happen and means there is a likely a bug in layer
459 // tracking
460 if (object->active_layers.size() < local_create_info.enabledLayerCount) {
461 ALOGE("Total number of layers is less than those enabled by the app!");
462 return false;
463 }
464 // Check if the total number of layers enabled is greater than those
465 // enabled by the application. If it is then we have system enabled
466 // layers which need to be added to the list of layers passed in through
467 // create.
468 if (object->active_layers.size() > local_create_info.enabledLayerCount) {
469 void* mem = alloc->pfnAllocation(
470 alloc->pUserData, object->active_layers.size() * sizeof(char*),
471 alignof(char*), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
472 if (mem) {
473 local_create_info.enabledLayerCount = 0;
474 const char** names = static_cast<const char**>(mem);
475 for (const auto& layer : object->active_layers) {
476 const char* name = layer.GetName();
477 names[local_create_info.enabledLayerCount++] = name;
478 }
479 local_create_info.ppEnabledLayerNames = names;
480 } else {
481 ALOGE("System layers cannot be enabled: memory allocation failed");
482 return false;
483 }
484 allocatedMemory = true;
485 } else {
486 allocatedMemory = false;
487 }
488 return true;
489}
490
491template <class T>
492void FreeAllocatedLayerCreateInfo(T& local_create_info,
493 const VkAllocationCallbacks* alloc) {
494 alloc->pfnFree(alloc->pUserData,
495 const_cast<char**>(local_create_info.ppEnabledLayerNames));
496}
497
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500498template <class TCreateInfo>
499bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
500 const char* extension_name,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800501 const VkAllocationCallbacks* alloc) {
Jesse Hall3dd678a2016-01-08 21:52:01 -0800502 uint32_t extension_count = local_create_info.enabledExtensionCount;
503 local_create_info.enabledExtensionCount++;
Jesse Hall3fbc8562015-11-29 22:10:52 -0800504 void* mem = alloc->pfnAllocation(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800505 alloc->pUserData,
Jesse Hall3dd678a2016-01-08 21:52:01 -0800506 local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*),
Michael Lentine57036832016-03-04 11:03:35 -0600507 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500508 if (mem) {
509 const char** enabled_extensions = static_cast<const char**>(mem);
510 for (uint32_t i = 0; i < extension_count; ++i) {
511 enabled_extensions[i] =
512 local_create_info.ppEnabledExtensionNames[i];
513 }
514 enabled_extensions[extension_count] = extension_name;
515 local_create_info.ppEnabledExtensionNames = enabled_extensions;
516 } else {
Michael Lentine57036832016-03-04 11:03:35 -0600517 ALOGE("%s extension cannot be enabled: memory allocation failed",
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500518 extension_name);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500519 return false;
520 }
521 return true;
522}
523
524template <class T>
Michael Lentine57036832016-03-04 11:03:35 -0600525void FreeAllocatedExtensionCreateInfo(T& local_create_info,
526 const VkAllocationCallbacks* alloc) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500527 alloc->pfnFree(
528 alloc->pUserData,
529 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
530}
531
Jesse Halle1b12782015-11-30 11:27:32 -0800532VKAPI_ATTR
Jesse Hall715b86a2016-01-16 16:34:29 -0800533VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags,
534 VkDebugReportObjectTypeEXT /*objectType*/,
535 uint64_t /*object*/,
Michael Lentineeb970862015-10-15 12:42:22 -0500536 size_t /*location*/,
537 int32_t message_code,
538 const char* layer_prefix,
539 const char* message,
540 void* /*user_data*/) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800541 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500542 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
Jesse Halle2948d82016-02-25 04:19:32 -0800543 } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500544 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
545 }
Michael Lentineeb970862015-10-15 12:42:22 -0500546 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500547}
548
Jesse Hall06193802015-12-03 16:12:51 -0800549VkResult Noop() {
Michael Lentine03c64b02015-08-26 18:27:26 -0500550 return VK_SUCCESS;
551}
552
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700553/*
554 * This function will return the pNext pointer of any
555 * CreateInfo extensions that are not loader extensions.
556 * This is used to skip past the loader extensions prepended
557 * to the list during CreateInstance and CreateDevice.
558 */
559void* StripCreateExtensions(const void* pNext) {
560 VkLayerInstanceCreateInfo* create_info =
561 const_cast<VkLayerInstanceCreateInfo*>(
562 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
563
564 while (
565 create_info &&
566 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
567 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
568 create_info = const_cast<VkLayerInstanceCreateInfo*>(
569 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
570 }
571
572 return create_info;
573}
574
Jesse Hallfee71432016-03-05 22:27:02 -0800575// Clean up and deallocate an Instance; called from both the failure paths in
576// CreateInstance_Top as well as from DestroyInstance_Top. This function does
577// not call down the dispatch chain; that should be done before calling this
578// function, iff the lower vkCreateInstance call has been made and returned
579// successfully.
580void DestroyInstance(Instance* instance,
581 const VkAllocationCallbacks* allocator) {
582 if (instance->message) {
583 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback;
584 destroy_debug_report_callback =
585 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
586 GetInstanceProcAddr_Top(instance->handle,
587 "vkDestroyDebugReportCallbackEXT"));
588 destroy_debug_report_callback(instance->handle, instance->message,
589 allocator);
590 }
591 instance->~Instance();
592 allocator->pfnFree(allocator->pUserData, instance);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700593}
594
Jesse Hall1f91d392015-12-11 16:28:44 -0800595} // anonymous namespace
596
597namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500598
Jesse Hall04f4f472015-08-16 19:51:04 -0700599// -----------------------------------------------------------------------------
600// "Bottom" functions. These are called at the end of the instance dispatch
601// chain.
602
Jesse Hall1f91d392015-12-11 16:28:44 -0800603VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
604 const VkAllocationCallbacks* allocator,
605 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700606 VkResult result;
607
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700608 VkLayerInstanceCreateInfo* chain_info =
609 const_cast<VkLayerInstanceCreateInfo*>(
610 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
611 while (
612 chain_info &&
613 !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO &&
614 chain_info->function == VK_LAYER_FUNCTION_INSTANCE)) {
615 chain_info = const_cast<VkLayerInstanceCreateInfo*>(
616 static_cast<const VkLayerInstanceCreateInfo*>(chain_info->pNext));
617 }
618 ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!");
619
620 Instance& instance = GetDispatchParent(
621 static_cast<VkInstance>(chain_info->u.instanceInfo.instance_info));
622
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800623 // Check that all enabled extensions are supported
624 InstanceExtensionSet enabled_extensions;
625 uint32_t num_driver_extensions = 0;
626 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
627 const char* name = create_info->ppEnabledExtensionNames[i];
628 InstanceExtension id = InstanceExtensionFromName(name);
629 if (id != kInstanceExtensionCount) {
630 if (g_driver_instance_extensions[id]) {
631 num_driver_extensions++;
632 enabled_extensions.set(id);
633 continue;
634 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700635 if (id == kKHR_surface || id == kKHR_android_surface) {
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800636 enabled_extensions.set(id);
637 continue;
638 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700639 // The loader natively supports debug report.
640 if (id == kEXT_debug_report) {
641 continue;
642 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800643 }
644 bool supported = false;
645 for (const auto& layer : instance.active_layers) {
646 if (layer.SupportsExtension(name))
647 supported = true;
648 }
649 if (!supported) {
650 ALOGE(
651 "requested instance extension '%s' not supported by "
652 "loader, driver, or any active layers",
653 name);
654 DestroyInstance_Bottom(instance.handle, allocator);
655 return VK_ERROR_EXTENSION_NOT_PRESENT;
656 }
657 }
658
Jesse Halla7ac76d2016-01-08 22:29:42 -0800659 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700660 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800661 driver_create_info.enabledLayerCount = 0;
662 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800663 driver_create_info.enabledExtensionCount = 0;
664 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800665 if (num_driver_extensions > 0) {
666 const char** names = static_cast<const char**>(
667 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800668 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800669 const char* name = create_info->ppEnabledExtensionNames[i];
670 InstanceExtension id = InstanceExtensionFromName(name);
671 if (id != kInstanceExtensionCount) {
672 if (g_driver_instance_extensions[id]) {
673 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800674 continue;
675 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800676 }
677 }
678 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800679 ALOG_ASSERT(
680 driver_create_info.enabledExtensionCount == num_driver_extensions,
681 "counted enabled driver instance extensions twice and got "
682 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800683 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800684
685 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Jesse Hall1f91d392015-12-11 16:28:44 -0800686 &instance.drv.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700687 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800688 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700689 return result;
690 }
691
Jesse Hall1f91d392015-12-11 16:28:44 -0800692 hwvulkan_dispatch_t* drv_dispatch =
693 reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700694 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700695 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800696 drv_dispatch->magic);
697 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700698 return VK_ERROR_INITIALIZATION_FAILED;
699 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700700 // Skip setting drv_dispatch->vtbl, since we never call through it;
701 // we go through instance.drv.dispatch instead.
Jesse Hall04f4f472015-08-16 19:51:04 -0700702
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700703 if (!LoadDriverDispatchTable(instance.drv.instance,
704 g_hwdevice->GetInstanceProcAddr,
705 enabled_extensions, instance.drv.dispatch)) {
706 DestroyInstance_Bottom(instance.handle, allocator);
707 return VK_ERROR_INITIALIZATION_FAILED;
708 }
709
Jesse Hall04f4f472015-08-16 19:51:04 -0700710 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800711 result = instance.drv.dispatch.EnumeratePhysicalDevices(
712 instance.drv.instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700713 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800714 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700715 return VK_ERROR_INITIALIZATION_FAILED;
716 }
717 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800718 result = instance.drv.dispatch.EnumeratePhysicalDevices(
719 instance.drv.instance, &num_physical_devices,
720 instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700721 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800722 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700723 return VK_ERROR_INITIALIZATION_FAILED;
724 }
Jesse Hallb1471272016-01-17 21:36:58 -0800725
726 Vector<VkExtensionProperties> extensions(
727 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700728 for (uint32_t i = 0; i < num_physical_devices; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800729 hwvulkan_dispatch_t* pdev_dispatch =
730 reinterpret_cast<hwvulkan_dispatch_t*>(
731 instance.physical_devices[i]);
732 if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700733 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800734 pdev_dispatch->magic);
735 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700736 return VK_ERROR_INITIALIZATION_FAILED;
737 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800738 pdev_dispatch->vtbl = instance.dispatch_ptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800739
740 uint32_t count;
741 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
742 instance.physical_devices[i], nullptr, &count, nullptr)) !=
743 VK_SUCCESS) {
744 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
745 result);
746 continue;
747 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800748 try {
749 extensions.resize(count);
750 } catch (std::bad_alloc&) {
751 ALOGE("instance creation failed: out of memory");
752 DestroyInstance_Bottom(instance.handle, allocator);
753 return VK_ERROR_OUT_OF_HOST_MEMORY;
754 }
Jesse Hallb1471272016-01-17 21:36:58 -0800755 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
756 instance.physical_devices[i], nullptr, &count,
757 extensions.data())) != VK_SUCCESS) {
758 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
759 result);
760 continue;
761 }
762 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
763 for (const auto& extension : extensions) {
764 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
765 DeviceExtension id =
766 DeviceExtensionFromName(extension.extensionName);
767 if (id == kDeviceExtensionCount) {
768 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
769 extension.extensionName);
770 } else {
771 instance.physical_device_driver_extensions[i].set(id);
772 }
773 }
774 // Ignore driver attempts to support loader extensions
775 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700776 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800777 instance.drv.num_physical_devices = num_physical_devices;
Jesse Hall1f91d392015-12-11 16:28:44 -0800778 instance.num_physical_devices = instance.drv.num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800779
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700780 *vkinstance = instance.handle;
781
Jesse Hall04f4f472015-08-16 19:51:04 -0700782 return VK_SUCCESS;
783}
784
Jesse Hall1f91d392015-12-11 16:28:44 -0800785PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) {
786 PFN_vkVoidFunction pfn;
787 if ((pfn = GetLoaderBottomProcAddr(name)))
788 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -0800789 return nullptr;
790}
791
792VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
793 uint32_t* pdev_count,
794 VkPhysicalDevice* pdevs) {
795 Instance& instance = GetDispatchParent(vkinstance);
796 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700797 if (pdevs) {
798 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800799 std::copy(instance.physical_devices, instance.physical_devices + count,
800 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700801 }
802 *pdev_count = count;
803 return VK_SUCCESS;
804}
805
Jesse Hall1f91d392015-12-11 16:28:44 -0800806void GetPhysicalDeviceProperties_Bottom(
807 VkPhysicalDevice pdev,
808 VkPhysicalDeviceProperties* properties) {
809 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
810 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700811}
812
Jesse Hall1f91d392015-12-11 16:28:44 -0800813void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
814 VkPhysicalDeviceFeatures* features) {
815 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
816 features);
817}
818
819void GetPhysicalDeviceMemoryProperties_Bottom(
820 VkPhysicalDevice pdev,
821 VkPhysicalDeviceMemoryProperties* properties) {
822 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
823 pdev, properties);
824}
825
826void GetPhysicalDeviceQueueFamilyProperties_Bottom(
827 VkPhysicalDevice pdev,
828 uint32_t* pCount,
829 VkQueueFamilyProperties* properties) {
830 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
831 pdev, pCount, properties);
832}
833
834void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
835 VkFormat format,
836 VkFormatProperties* properties) {
837 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700838 pdev, format, properties);
839}
840
Jesse Hall1f91d392015-12-11 16:28:44 -0800841VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700842 VkPhysicalDevice pdev,
843 VkFormat format,
844 VkImageType type,
845 VkImageTiling tiling,
846 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700847 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700848 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800849 return GetDispatchParent(pdev)
850 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800851 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700852}
853
Jesse Hall1f91d392015-12-11 16:28:44 -0800854void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700855 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800856 VkFormat format,
857 VkImageType type,
858 VkSampleCountFlagBits samples,
859 VkImageUsageFlags usage,
860 VkImageTiling tiling,
861 uint32_t* properties_count,
862 VkSparseImageFormatProperties* properties) {
863 GetDispatchParent(pdev)
864 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
865 pdev, format, type, samples, usage, tiling, properties_count,
866 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700867}
868
Courtney Goeltzenleuchter26d394b2016-02-07 10:32:27 -0700869// This is a no-op, the Top function returns the aggregate layer property
870// data. This is to keep the dispatch generator happy.
Jesse Halle1b12782015-11-30 11:27:32 -0800871VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800872VkResult EnumerateDeviceExtensionProperties_Bottom(
Courtney Goeltzenleuchter26d394b2016-02-07 10:32:27 -0700873 VkPhysicalDevice /*pdev*/,
874 const char* /*layer_name*/,
875 uint32_t* /*properties_count*/,
876 VkExtensionProperties* /*properties*/) {
877 return VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700878}
879
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700880// This is a no-op, the Top function returns the aggregate layer property
881// data. This is to keep the dispatch generator happy.
Jesse Halle1b12782015-11-30 11:27:32 -0800882VKAPI_ATTR
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700883VkResult EnumerateDeviceLayerProperties_Bottom(
884 VkPhysicalDevice /*pdev*/,
885 uint32_t* /*properties_count*/,
886 VkLayerProperties* /*properties*/) {
887 return VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800888}
889
890VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800891VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800892 const VkDeviceCreateInfo* create_info,
893 const VkAllocationCallbacks* allocator,
894 VkDevice* device_out) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700895 VkLayerDeviceCreateInfo* chain_info = const_cast<VkLayerDeviceCreateInfo*>(
896 static_cast<const VkLayerDeviceCreateInfo*>(create_info->pNext));
897 while (chain_info &&
898 !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO &&
899 chain_info->function == VK_LAYER_FUNCTION_DEVICE)) {
900 chain_info = const_cast<VkLayerDeviceCreateInfo*>(
901 static_cast<const VkLayerDeviceCreateInfo*>(chain_info->pNext));
Jesse Hall9a16f972015-10-28 15:59:53 -0700902 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700903 ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!");
Jesse Hall9a16f972015-10-28 15:59:53 -0700904
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700905 Instance& instance = GetDispatchParent(gpu);
Jesse Hallb1471272016-01-17 21:36:58 -0800906 size_t gpu_idx = 0;
907 while (instance.physical_devices[gpu_idx] != gpu)
908 gpu_idx++;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700909 Device* device = static_cast<Device*>(chain_info->u.deviceInfo.device_info);
910 PFN_vkGetInstanceProcAddr get_instance_proc_addr =
911 chain_info->u.deviceInfo.pfnNextGetInstanceProcAddr;
912
913 VkDeviceCreateInfo driver_create_info = *create_info;
914 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
915 driver_create_info.enabledLayerCount = 0;
916 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800917
918 uint32_t num_driver_extensions = 0;
919 const char** driver_extensions = static_cast<const char**>(
920 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
921 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
922 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800923 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800924 if (id != kDeviceExtensionCount) {
925 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
926 driver_extensions[num_driver_extensions++] = name;
927 continue;
928 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700929 // Add the VK_ANDROID_native_buffer extension to the list iff
930 // the VK_KHR_swapchain extension was requested
Jesse Hallae3b70d2016-01-17 22:05:29 -0800931 if (id == kKHR_swapchain &&
932 instance.physical_device_driver_extensions
933 [gpu_idx][kANDROID_native_buffer]) {
934 driver_extensions[num_driver_extensions++] =
935 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
936 continue;
937 }
Jesse Hallb1471272016-01-17 21:36:58 -0800938 }
Jesse Hallb1471272016-01-17 21:36:58 -0800939 bool supported = false;
940 for (const auto& layer : device->active_layers) {
941 if (layer.SupportsExtension(name))
942 supported = true;
943 }
944 if (!supported) {
945 ALOGE(
Jesse Hallae3b70d2016-01-17 22:05:29 -0800946 "requested device extension '%s' not supported by loader, "
947 "driver, or any active layers",
Jesse Hallb1471272016-01-17 21:36:58 -0800948 name);
Jesse Hallb1471272016-01-17 21:36:58 -0800949 return VK_ERROR_EXTENSION_NOT_PRESENT;
950 }
951 }
952
Jesse Hallb1471272016-01-17 21:36:58 -0800953 driver_create_info.enabledExtensionCount = num_driver_extensions;
954 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Hall04f4f472015-08-16 19:51:04 -0700955 VkDevice drv_device;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700956 VkResult result = instance.drv.dispatch.CreateDevice(
957 gpu, &driver_create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700958 if (result != VK_SUCCESS) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700959 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700960 }
961
Jesse Hall1f91d392015-12-11 16:28:44 -0800962 hwvulkan_dispatch_t* drv_dispatch =
Jesse Hall04f4f472015-08-16 19:51:04 -0700963 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800964 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
965 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
966 drv_dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500967 PFN_vkDestroyDevice destroy_device =
968 reinterpret_cast<PFN_vkDestroyDevice>(
Jesse Hall1f91d392015-12-11 16:28:44 -0800969 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
970 "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800971 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700972 return VK_ERROR_INITIALIZATION_FAILED;
973 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700974
975 // Set dispatch table for newly created Device
976 // CreateDevice_Top will fill in the details
Jesse Hall1f91d392015-12-11 16:28:44 -0800977 drv_dispatch->vtbl = &device->dispatch;
978 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
979 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
980 "vkGetDeviceProcAddr"));
Jesse Hall1f91d392015-12-11 16:28:44 -0800981 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700982 return VK_SUCCESS;
983}
984
Jesse Hall1f91d392015-12-11 16:28:44 -0800985void DestroyInstance_Bottom(VkInstance vkinstance,
986 const VkAllocationCallbacks* allocator) {
987 Instance& instance = GetDispatchParent(vkinstance);
988
989 // These checks allow us to call DestroyInstance_Bottom from any error
990 // path in CreateInstance_Bottom, before the driver instance is fully
991 // initialized.
992 if (instance.drv.instance != VK_NULL_HANDLE &&
993 instance.drv.dispatch.DestroyInstance) {
994 instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
Jesse Hallfee71432016-03-05 22:27:02 -0800995 instance.drv.instance = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -0800996 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700997}
998
Jesse Hall1f91d392015-12-11 16:28:44 -0800999PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
1000 const char* name) {
1001 if (strcmp(name, "vkCreateDevice") == 0) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001002 return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom);
Michael Lentine03c64b02015-08-26 18:27:26 -05001003 }
Jesse Hall1f91d392015-12-11 16:28:44 -08001004
1005 // VK_ANDROID_native_buffer should be hidden from applications and layers.
1006 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
1007 PFN_vkVoidFunction pfn;
1008 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
1009 strcmp(name, "vkAcquireImageANDROID") == 0 ||
1010 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
1011 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -05001012 }
Jesse Hall1f91d392015-12-11 16:28:44 -08001013 if ((pfn = GetLoaderBottomProcAddr(name)))
1014 return pfn;
1015 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001016}
1017
Jesse Hall04f4f472015-08-16 19:51:04 -07001018// -----------------------------------------------------------------------------
Jesse Hall1f91d392015-12-11 16:28:44 -08001019// Loader top functions. These are called directly from the loader entry
1020// points or from the application (via vkGetInstanceProcAddr) without going
1021// through a dispatch table.
Jesse Hall04f4f472015-08-16 19:51:04 -07001022
Jesse Hall1f91d392015-12-11 16:28:44 -08001023VkResult EnumerateInstanceExtensionProperties_Top(
Jesse Hall80523e22016-01-06 16:47:54 -08001024 const char* layer_name,
1025 uint32_t* properties_count,
1026 VkExtensionProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001027 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001028 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001029
Jesse Hall80523e22016-01-06 16:47:54 -08001030 const VkExtensionProperties* extensions = nullptr;
1031 uint32_t num_extensions = 0;
1032 if (layer_name) {
Jesse Hallaa410942016-01-17 13:07:10 -08001033 GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions);
Jesse Hall80523e22016-01-06 16:47:54 -08001034 } else {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -08001035 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
1036 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
1037 available[num_extensions++] = VkExtensionProperties{
1038 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
1039 available[num_extensions++] =
1040 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
1041 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
1042 if (g_driver_instance_extensions[kEXT_debug_report]) {
1043 available[num_extensions++] =
1044 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
1045 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
1046 }
Jesse Hall80523e22016-01-06 16:47:54 -08001047 // TODO(jessehall): We need to also enumerate extensions supported by
1048 // implicitly-enabled layers. Currently we don't have that list of
1049 // layers until instance creation.
Jesse Hall6bd5dfa2016-01-16 17:13:30 -08001050 extensions = available;
Jesse Hall80523e22016-01-06 16:47:54 -08001051 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001052
Jesse Hall80523e22016-01-06 16:47:54 -08001053 if (!properties || *properties_count > num_extensions)
1054 *properties_count = num_extensions;
1055 if (properties)
1056 std::copy(extensions, extensions + *properties_count, properties);
1057 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001058}
1059
Jesse Hall80523e22016-01-06 16:47:54 -08001060VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
1061 VkLayerProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001062 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001063 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001064
Jesse Hall80523e22016-01-06 16:47:54 -08001065 uint32_t layer_count =
Jesse Hallaa410942016-01-17 13:07:10 -08001066 EnumerateInstanceLayers(properties ? *properties_count : 0, properties);
Jesse Hall80523e22016-01-06 16:47:54 -08001067 if (!properties || *properties_count > layer_count)
1068 *properties_count = layer_count;
1069 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001070}
1071
Courtney Goeltzenleuchter26d394b2016-02-07 10:32:27 -07001072VKAPI_ATTR
1073VkResult EnumerateDeviceExtensionProperties_Top(
1074 VkPhysicalDevice gpu,
1075 const char* layer_name,
1076 uint32_t* properties_count,
1077 VkExtensionProperties* properties) {
1078 const VkExtensionProperties* extensions = nullptr;
1079 uint32_t num_extensions = 0;
1080
1081 ALOGV("EnumerateDeviceExtensionProperties_Top:");
1082 if (layer_name) {
1083 ALOGV(" layer %s", layer_name);
1084 GetDeviceLayerExtensions(layer_name, &extensions, &num_extensions);
1085 } else {
1086 ALOGV(" no layer");
1087 Instance& instance = GetDispatchParent(gpu);
1088 size_t gpu_idx = 0;
1089 while (instance.physical_devices[gpu_idx] != gpu)
1090 gpu_idx++;
1091 const DeviceExtensionSet driver_extensions =
1092 instance.physical_device_driver_extensions[gpu_idx];
1093
1094 // We only support VK_KHR_swapchain if the GPU supports
1095 // VK_ANDROID_native_buffer
1096 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
1097 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
1098 if (driver_extensions[kANDROID_native_buffer]) {
1099 available[num_extensions++] = VkExtensionProperties{
1100 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
1101 }
1102
1103 // TODO(jessehall): We need to also enumerate extensions supported by
1104 // implicitly-enabled layers. Currently we don't have that list of
1105 // layers until instance creation.
1106 extensions = available;
1107 }
1108
1109 ALOGV(" num: %d, extensions: %p", num_extensions, extensions);
1110 if (!properties || *properties_count > num_extensions)
1111 *properties_count = num_extensions;
1112 if (properties)
1113 std::copy(extensions, extensions + *properties_count, properties);
1114 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
1115}
1116
Jesse Hall1f91d392015-12-11 16:28:44 -08001117VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
1118 const VkAllocationCallbacks* allocator,
1119 VkInstance* instance_out) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001120 VkResult result;
1121
1122 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001123 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001124
Jesse Hall03b6fe12015-11-24 12:44:21 -08001125 if (!allocator)
1126 allocator = &kDefaultAllocCallbacks;
1127
Jesse Hall04f4f472015-08-16 19:51:04 -07001128 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -07001129 create_info = &local_create_info;
1130
Jesse Hall3fbc8562015-11-29 22:10:52 -08001131 void* instance_mem = allocator->pfnAllocation(
1132 allocator->pUserData, sizeof(Instance), alignof(Instance),
1133 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -07001134 if (!instance_mem)
1135 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001136 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -07001137
Jesse Hall9a16f972015-10-28 15:59:53 -07001138 result = ActivateAllLayers(create_info, instance, instance);
1139 if (result != VK_SUCCESS) {
Jesse Hallfee71432016-03-05 22:27:02 -08001140 DestroyInstance(instance, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -07001141 return result;
1142 }
Michael Lentine03c64b02015-08-26 18:27:26 -05001143
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001144 uint32_t activated_layers = 0;
1145 VkLayerInstanceCreateInfo chain_info;
1146 VkLayerInstanceLink* layer_instance_link_info = nullptr;
1147 PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom;
1148 VkInstance local_instance = nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -05001149
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001150 if (instance->active_layers.size() > 0) {
1151 chain_info.u.pLayerInfo = nullptr;
1152 chain_info.pNext = create_info->pNext;
1153 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
1154 chain_info.function = VK_LAYER_FUNCTION_LINK;
1155 local_create_info.pNext = &chain_info;
Michael Lentine03c64b02015-08-26 18:27:26 -05001156
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001157 layer_instance_link_info = static_cast<VkLayerInstanceLink*>(alloca(
1158 sizeof(VkLayerInstanceLink) * instance->active_layers.size()));
1159 if (!layer_instance_link_info) {
1160 ALOGE("Failed to alloc Instance objects for layers");
Jesse Hallfee71432016-03-05 22:27:02 -08001161 DestroyInstance(instance, allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001162 return VK_ERROR_OUT_OF_HOST_MEMORY;
1163 }
1164
1165 /* Create instance chain of enabled layers */
1166 for (auto rit = instance->active_layers.rbegin();
1167 rit != instance->active_layers.rend(); ++rit) {
1168 LayerRef& layer = *rit;
1169 layer_instance_link_info[activated_layers].pNext =
1170 chain_info.u.pLayerInfo;
1171 layer_instance_link_info[activated_layers]
1172 .pfnNextGetInstanceProcAddr = next_gipa;
1173 chain_info.u.pLayerInfo =
1174 &layer_instance_link_info[activated_layers];
1175 next_gipa = layer.GetGetInstanceProcAddr();
1176
1177 ALOGV("Insert instance layer %s (v%u)", layer.GetName(),
1178 layer.GetSpecVersion());
1179
1180 activated_layers++;
Michael Lentine03c64b02015-08-26 18:27:26 -05001181 }
1182 }
1183
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001184 PFN_vkCreateInstance create_instance =
1185 reinterpret_cast<PFN_vkCreateInstance>(
1186 next_gipa(VK_NULL_HANDLE, "vkCreateInstance"));
1187 if (!create_instance) {
Jesse Hallfee71432016-03-05 22:27:02 -08001188 DestroyInstance(instance, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -05001189 return VK_ERROR_INITIALIZATION_FAILED;
1190 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001191 VkLayerInstanceCreateInfo instance_create_info;
1192
1193 instance_create_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
1194 instance_create_info.function = VK_LAYER_FUNCTION_INSTANCE;
1195
1196 instance_create_info.u.instanceInfo.instance_info = instance;
1197 instance_create_info.u.instanceInfo.pfnNextGetInstanceProcAddr = next_gipa;
1198
1199 instance_create_info.pNext = local_create_info.pNext;
1200 local_create_info.pNext = &instance_create_info;
1201
Courtney Goeltzenleuchter12086222016-02-12 07:53:12 -07001202 // Force enable callback extension if required
1203 bool enable_callback = false;
1204 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
1205 enable_callback =
1206 property_get_bool("debug.vulkan.enable_callback", false);
1207 if (enable_callback) {
Michael Lentine57036832016-03-04 11:03:35 -06001208 if (!AddExtensionToCreateInfo(local_create_info,
1209 "VK_EXT_debug_report", allocator)) {
1210 DestroyInstance(instance, allocator);
1211 return VK_ERROR_INITIALIZATION_FAILED;
1212 }
Courtney Goeltzenleuchter12086222016-02-12 07:53:12 -07001213 }
1214 }
Michael Lentine57036832016-03-04 11:03:35 -06001215 bool allocatedLayerMem;
1216 if (!AddLayersToCreateInfo(local_create_info, instance, allocator,
1217 allocatedLayerMem)) {
1218 if (enable_callback) {
1219 FreeAllocatedExtensionCreateInfo(local_create_info, allocator);
1220 }
1221 DestroyInstance(instance, allocator);
1222 return VK_ERROR_INITIALIZATION_FAILED;
1223 }
Courtney Goeltzenleuchter12086222016-02-12 07:53:12 -07001224
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001225 result = create_instance(&local_create_info, allocator, &local_instance);
Michael Lentine57036832016-03-04 11:03:35 -06001226
1227 if (allocatedLayerMem) {
1228 FreeAllocatedLayerCreateInfo(local_create_info, allocator);
1229 }
1230 if (enable_callback) {
1231 FreeAllocatedExtensionCreateInfo(local_create_info, allocator);
1232 }
1233
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001234 if (result != VK_SUCCESS) {
Jesse Hallfee71432016-03-05 22:27:02 -08001235 DestroyInstance(instance, allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001236 return result;
1237 }
1238
1239 const InstanceDispatchTable& instance_dispatch =
1240 GetDispatchTable(local_instance);
1241 if (!LoadInstanceDispatchTable(
1242 local_instance, next_gipa,
1243 const_cast<InstanceDispatchTable&>(instance_dispatch))) {
1244 ALOGV("Failed to initialize instance dispatch table");
1245 PFN_vkDestroyInstance destroy_instance =
1246 reinterpret_cast<PFN_vkDestroyInstance>(
1247 next_gipa(VK_NULL_HANDLE, "vkDestroyInstance"));
1248 if (!destroy_instance) {
1249 ALOGD("Loader unable to find DestroyInstance");
1250 return VK_ERROR_INITIALIZATION_FAILED;
1251 }
1252 destroy_instance(local_instance, allocator);
Jesse Hallfee71432016-03-05 22:27:02 -08001253 DestroyInstance(instance, allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001254 return VK_ERROR_INITIALIZATION_FAILED;
1255 }
1256 *instance_out = local_instance;
Michael Lentine03c64b02015-08-26 18:27:26 -05001257
Courtney Goeltzenleuchter12086222016-02-12 07:53:12 -07001258 if (enable_callback) {
Jesse Hall715b86a2016-01-16 16:34:29 -08001259 const VkDebugReportCallbackCreateInfoEXT callback_create_info = {
1260 .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
1261 .flags =
Jesse Halle2948d82016-02-25 04:19:32 -08001262 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT,
Jesse Hall715b86a2016-01-16 16:34:29 -08001263 .pfnCallback = LogDebugMessageCallback,
1264 };
1265 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
1266 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
1267 GetInstanceProcAddr_Top(instance->handle,
1268 "vkCreateDebugReportCallbackEXT"));
1269 create_debug_report_callback(instance->handle, &callback_create_info,
1270 allocator, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001271 }
1272
Jesse Hall04f4f472015-08-16 19:51:04 -07001273 return result;
1274}
1275
Jesse Hall1f91d392015-12-11 16:28:44 -08001276PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
1277 const char* name) {
1278 // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
1279 if (!vkinstance)
1280 return GetLoaderGlobalProcAddr(name);
1281
1282 const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
1283 PFN_vkVoidFunction pfn;
1284 // Always go through the loader-top function if there is one.
1285 if ((pfn = GetLoaderTopProcAddr(name)))
1286 return pfn;
1287 // Otherwise, look up the handler in the instance dispatch table
1288 if ((pfn = GetDispatchProcAddr(dispatch, name)))
1289 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -08001290 // Anything not handled already must be a device-dispatched function
1291 // without a loader-top. We must return a function that will dispatch based
1292 // on the dispatchable object parameter -- which is exactly what the
1293 // exported functions do. So just return them here.
1294 return GetLoaderExportProcAddr(name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001295}
1296
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001297void DestroyInstance_Top(VkInstance vkinstance,
Jesse Hall1f91d392015-12-11 16:28:44 -08001298 const VkAllocationCallbacks* allocator) {
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001299 if (!vkinstance)
Jesse Hall1f91d392015-12-11 16:28:44 -08001300 return;
Jesse Hallfee71432016-03-05 22:27:02 -08001301 if (!allocator)
1302 allocator = &kDefaultAllocCallbacks;
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -07001303 GetDispatchTable(vkinstance).DestroyInstance(vkinstance, allocator);
Jesse Hallfee71432016-03-05 22:27:02 -08001304 DestroyInstance(&(GetDispatchParent(vkinstance)), allocator);
Jesse Hall1f91d392015-12-11 16:28:44 -08001305}
1306
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001307VKAPI_ATTR
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -07001308VkResult EnumerateDeviceLayerProperties_Top(VkPhysicalDevice /*pdev*/,
1309 uint32_t* properties_count,
1310 VkLayerProperties* properties) {
1311 uint32_t layer_count =
1312 EnumerateDeviceLayers(properties ? *properties_count : 0, properties);
1313 if (!properties || *properties_count > layer_count)
1314 *properties_count = layer_count;
1315 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
1316}
1317
1318VKAPI_ATTR
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001319VkResult CreateDevice_Top(VkPhysicalDevice gpu,
1320 const VkDeviceCreateInfo* create_info,
1321 const VkAllocationCallbacks* allocator,
1322 VkDevice* device_out) {
1323 Instance& instance = GetDispatchParent(gpu);
1324 VkResult result;
1325
1326 // FIXME(jessehall): We don't have good conventions or infrastructure yet to
1327 // do better than just using the instance allocator and scope for
1328 // everything. See b/26732122.
1329 if (true /*!allocator*/)
1330 allocator = instance.alloc;
1331
1332 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
1333 alignof(Device),
1334 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1335 if (!mem)
1336 return VK_ERROR_OUT_OF_HOST_MEMORY;
1337 Device* device = new (mem) Device(&instance);
1338
1339 result = ActivateAllLayers(create_info, &instance, device);
1340 if (result != VK_SUCCESS) {
1341 DestroyDevice(device);
1342 return result;
1343 }
1344
1345 size_t gpu_idx = 0;
1346 while (instance.physical_devices[gpu_idx] != gpu)
1347 gpu_idx++;
1348
1349 uint32_t activated_layers = 0;
1350 VkLayerDeviceCreateInfo chain_info;
1351 VkLayerDeviceLink* layer_device_link_info = nullptr;
1352 PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom;
1353 PFN_vkGetDeviceProcAddr next_gdpa = GetDeviceProcAddr_Bottom;
1354 VkDeviceCreateInfo local_create_info = *create_info;
1355 VkDevice local_device = nullptr;
1356
1357 if (device->active_layers.size() > 0) {
1358 chain_info.u.pLayerInfo = nullptr;
1359 chain_info.pNext = local_create_info.pNext;
1360 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
1361 chain_info.function = VK_LAYER_FUNCTION_LINK;
1362 local_create_info.pNext = &chain_info;
1363
1364 layer_device_link_info = static_cast<VkLayerDeviceLink*>(
1365 alloca(sizeof(VkLayerDeviceLink) * device->active_layers.size()));
1366 if (!layer_device_link_info) {
1367 ALOGE("Failed to alloc Device objects for layers");
1368 DestroyDevice(device);
1369 return VK_ERROR_OUT_OF_HOST_MEMORY;
1370 }
1371
1372 /* Create device chain of enabled layers */
1373 for (auto rit = device->active_layers.rbegin();
1374 rit != device->active_layers.rend(); ++rit) {
1375 LayerRef& layer = *rit;
1376 layer_device_link_info[activated_layers].pNext =
1377 chain_info.u.pLayerInfo;
1378 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
1379 next_gdpa;
1380 layer_device_link_info[activated_layers]
1381 .pfnNextGetInstanceProcAddr = next_gipa;
1382 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
1383
1384 next_gipa = layer.GetGetInstanceProcAddr();
1385 next_gdpa = layer.GetGetDeviceProcAddr();
1386
1387 ALOGV("Insert device layer %s (v%u)", layer.GetName(),
1388 layer.GetSpecVersion());
1389
1390 activated_layers++;
1391 }
1392 }
1393
1394 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
1395 next_gipa(VK_NULL_HANDLE, "vkCreateDevice"));
1396 if (!create_device) {
1397 ALOGE("Unable to find vkCreateDevice for driver");
1398 DestroyDevice(device);
1399 return VK_ERROR_INITIALIZATION_FAILED;
1400 }
1401
1402 VkLayerDeviceCreateInfo device_create_info;
1403
1404 device_create_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
1405 device_create_info.function = VK_LAYER_FUNCTION_DEVICE;
1406
1407 device_create_info.u.deviceInfo.device_info = device;
1408 device_create_info.u.deviceInfo.pfnNextGetInstanceProcAddr = next_gipa;
1409
1410 device_create_info.pNext = local_create_info.pNext;
1411 local_create_info.pNext = &device_create_info;
1412
Michael Lentine57036832016-03-04 11:03:35 -06001413 bool allocatedLayerMem;
1414 if (!AddLayersToCreateInfo(local_create_info, device, allocator,
1415 allocatedLayerMem)) {
1416 DestroyDevice(device);
1417 return VK_ERROR_INITIALIZATION_FAILED;
1418 }
1419
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001420 result = create_device(gpu, &local_create_info, allocator, &local_device);
1421
Michael Lentine57036832016-03-04 11:03:35 -06001422 if (allocatedLayerMem) {
1423 FreeAllocatedLayerCreateInfo(local_create_info, allocator);
1424 }
1425
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -07001426 if (result != VK_SUCCESS) {
1427 DestroyDevice(device);
1428 return result;
1429 }
1430
1431 // Set dispatch table for newly created Device
1432 hwvulkan_dispatch_t* vulkan_dispatch =
1433 reinterpret_cast<hwvulkan_dispatch_t*>(local_device);
1434 vulkan_dispatch->vtbl = &device->dispatch;
1435
1436 const DeviceDispatchTable& device_dispatch = GetDispatchTable(local_device);
1437 if (!LoadDeviceDispatchTable(
1438 local_device, next_gdpa,
1439 const_cast<DeviceDispatchTable&>(device_dispatch))) {
1440 ALOGV("Failed to initialize device dispatch table");
1441 PFN_vkDestroyDevice destroy_device =
1442 reinterpret_cast<PFN_vkDestroyDevice>(
1443 next_gipa(VK_NULL_HANDLE, "vkDestroyDevice"));
1444 ALOG_ASSERT(destroy_device != nullptr,
1445 "Loader unable to find DestroyDevice");
1446 destroy_device(local_device, allocator);
1447 return VK_ERROR_INITIALIZATION_FAILED;
1448 }
1449 *device_out = local_device;
1450
1451 return VK_SUCCESS;
1452}
1453
Jesse Hall1f91d392015-12-11 16:28:44 -08001454PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
1455 PFN_vkVoidFunction pfn;
Jesse Hall04f4f472015-08-16 19:51:04 -07001456 if (!device)
Jesse Hall1f91d392015-12-11 16:28:44 -08001457 return nullptr;
1458 if ((pfn = GetLoaderTopProcAddr(name)))
1459 return pfn;
1460 return GetDispatchProcAddr(GetDispatchTable(device), name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001461}
1462
Jesse Hall1f91d392015-12-11 16:28:44 -08001463void GetDeviceQueue_Top(VkDevice vkdevice,
1464 uint32_t family,
1465 uint32_t index,
1466 VkQueue* queue_out) {
1467 const auto& table = GetDispatchTable(vkdevice);
1468 table.GetDeviceQueue(vkdevice, family, index, queue_out);
1469 hwvulkan_dispatch_t* queue_dispatch =
1470 reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
1471 if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
1472 queue_dispatch->vtbl != &table)
1473 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
1474 queue_dispatch->magic);
1475 queue_dispatch->vtbl = &table;
Jesse Hall04f4f472015-08-16 19:51:04 -07001476}
1477
Jesse Hall1f91d392015-12-11 16:28:44 -08001478VkResult AllocateCommandBuffers_Top(
1479 VkDevice vkdevice,
1480 const VkCommandBufferAllocateInfo* alloc_info,
1481 VkCommandBuffer* cmdbufs) {
1482 const auto& table = GetDispatchTable(vkdevice);
1483 VkResult result =
1484 table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001485 if (result != VK_SUCCESS)
1486 return result;
Jesse Hall3dd678a2016-01-08 21:52:01 -08001487 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001488 hwvulkan_dispatch_t* cmdbuf_dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001489 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hall1f91d392015-12-11 16:28:44 -08001490 ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001491 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -08001492 cmdbuf_dispatch->magic);
1493 cmdbuf_dispatch->vtbl = &table;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001494 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001495 return VK_SUCCESS;
1496}
1497
Jesse Hall1f91d392015-12-11 16:28:44 -08001498void DestroyDevice_Top(VkDevice vkdevice,
1499 const VkAllocationCallbacks* /*allocator*/) {
1500 if (!vkdevice)
1501 return;
1502 Device& device = GetDispatchParent(vkdevice);
Jesse Hall1f91d392015-12-11 16:28:44 -08001503 device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
1504 DestroyDevice(&device);
Jesse Hall04f4f472015-08-16 19:51:04 -07001505}
1506
Jesse Hall1f91d392015-12-11 16:28:44 -08001507// -----------------------------------------------------------------------------
1508
1509const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
1510 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001511}
1512
Jesse Hall1f91d392015-12-11 16:28:44 -08001513const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
1514 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001515}
1516
Jesse Hall715b86a2016-01-16 16:34:29 -08001517VkInstance GetDriverInstance(VkInstance instance) {
1518 return GetDispatchParent(instance).drv.instance;
1519}
1520
1521const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
1522 return GetDispatchParent(instance).drv.dispatch;
1523}
1524
Jesse Hall1f91d392015-12-11 16:28:44 -08001525const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
1526 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001527}
1528
Jesse Hall1f91d392015-12-11 16:28:44 -08001529const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
1530 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001531}
1532
Jesse Hall715b86a2016-01-16 16:34:29 -08001533DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
1534 return GetDispatchParent(instance).debug_report_callbacks;
1535}
1536
Jesse Hall04f4f472015-08-16 19:51:04 -07001537} // namespace vulkan