blob: 2d48fda688e9a7eafc86f3ad3bbdcc510854d761 [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"
Chia-I Wu0c203242016-03-15 13:44:51 +080019#include "driver.h"
Jesse Hall04f4f472015-08-16 19:51:04 -070020// standard C headers
Michael Lentine03c64b02015-08-26 18:27:26 -050021#include <dirent.h>
22#include <dlfcn.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070023#include <inttypes.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070024#include <pthread.h>
Jesse Hall03b6fe12015-11-24 12:44:21 -080025#include <stdlib.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070026#include <string.h>
Jesse Hall21597662015-12-18 13:48:24 -080027#include <sys/prctl.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070028// standard C++ headers
29#include <algorithm>
30#include <mutex>
Michael Lentine03c64b02015-08-26 18:27:26 -050031#include <sstream>
32#include <string>
33#include <unordered_map>
34#include <vector>
Jesse Hall04f4f472015-08-16 19:51:04 -070035// platform/library headers
Michael Lentine03c64b02015-08-26 18:27:26 -050036#include <cutils/properties.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070037#include <hardware/hwvulkan.h>
38#include <log/log.h>
Michael Lentine1c69b9e2015-09-14 13:26:59 -050039#include <vulkan/vulkan_loader_data.h>
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -070040#include <vulkan/vk_layer_interface.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070041
42using namespace vulkan;
43
44static const uint32_t kMaxPhysicalDevices = 4;
45
Michael Lentine03c64b02015-08-26 18:27:26 -050046namespace {
47
Jesse Hall1f91d392015-12-11 16:28:44 -080048// ----------------------------------------------------------------------------
Michael Lentine03c64b02015-08-26 18:27:26 -050049
Jesse Hall3fbc8562015-11-29 22:10:52 -080050// Standard-library allocator that delegates to VkAllocationCallbacks.
Jesse Hall03b6fe12015-11-24 12:44:21 -080051//
52// TODO(jessehall): This class currently always uses
Jesse Hall3fbc8562015-11-29 22:10:52 -080053// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
Jesse Hall03b6fe12015-11-24 12:44:21 -080054// parameter or a constructor parameter. The former would help catch bugs
55// where we use the wrong scope, e.g. adding a command-scope string to an
56// instance-scope vector. But that might also be pretty annoying to deal with.
Michael Lentine03c64b02015-08-26 18:27:26 -050057template <class T>
58class CallbackAllocator {
59 public:
60 typedef T value_type;
61
Jesse Hall3fbc8562015-11-29 22:10:52 -080062 CallbackAllocator(const VkAllocationCallbacks* alloc_input)
Michael Lentine03c64b02015-08-26 18:27:26 -050063 : alloc(alloc_input) {}
64
65 template <class T2>
66 CallbackAllocator(const CallbackAllocator<T2>& other)
67 : alloc(other.alloc) {}
68
69 T* allocate(std::size_t n) {
Jesse Hall3fbc8562015-11-29 22:10:52 -080070 void* mem =
71 alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
72 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall26cecff2016-01-21 19:52:25 -080073 if (!mem)
74 throw std::bad_alloc();
Michael Lentine03c64b02015-08-26 18:27:26 -050075 return static_cast<T*>(mem);
76 }
77
Jesse Hall26cecff2016-01-21 19:52:25 -080078 void deallocate(T* array, std::size_t /*n*/) noexcept {
Michael Lentine03c64b02015-08-26 18:27:26 -050079 alloc->pfnFree(alloc->pUserData, array);
80 }
81
Jesse Hall3fbc8562015-11-29 22:10:52 -080082 const VkAllocationCallbacks* alloc;
Michael Lentine03c64b02015-08-26 18:27:26 -050083};
84// These are needed in order to move Strings
85template <class T>
86bool operator==(const CallbackAllocator<T>& alloc1,
87 const CallbackAllocator<T>& alloc2) {
88 return alloc1.alloc == alloc2.alloc;
89}
90template <class T>
91bool operator!=(const CallbackAllocator<T>& alloc1,
92 const CallbackAllocator<T>& alloc2) {
93 return !(alloc1 == alloc2);
94}
95
Michael Lentine03c64b02015-08-26 18:27:26 -050096template <class T>
Jesse Hall1f91d392015-12-11 16:28:44 -080097using Vector = std::vector<T, CallbackAllocator<T>>;
Michael Lentine03c64b02015-08-26 18:27:26 -050098
Jesse Hall1f91d392015-12-11 16:28:44 -080099typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
100 String;
Michael Lentine03c64b02015-08-26 18:27:26 -0500101
Jesse Hall1f91d392015-12-11 16:28:44 -0800102// ----------------------------------------------------------------------------
Jesse Hall80523e22016-01-06 16:47:54 -0800103// Global Data and Initialization
Jesse Hall1f91d392015-12-11 16:28:44 -0800104
Jesse Hall80523e22016-01-06 16:47:54 -0800105hwvulkan_device_t* g_hwdevice = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800106InstanceExtensionSet g_driver_instance_extensions;
107
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800108bool LoadVulkanHAL() {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800109 VkResult vkresult;
110 uint32_t count;
111 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
112 nullptr, &count, nullptr)) != VK_SUCCESS) {
113 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
114 vkresult);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800115 return false;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800116 }
117 VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
118 alloca(count * sizeof(VkExtensionProperties)));
119 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
120 nullptr, &count, extensions)) != VK_SUCCESS) {
121 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
122 vkresult);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800123 return false;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800124 }
125 ALOGV_IF(count > 0, "Driver-supported instance extensions:");
126 for (uint32_t i = 0; i < count; i++) {
127 ALOGV(" %s (v%u)", extensions[i].extensionName,
128 extensions[i].specVersion);
129 InstanceExtension id =
130 InstanceExtensionFromName(extensions[i].extensionName);
131 if (id != kInstanceExtensionCount)
132 g_driver_instance_extensions.set(id);
133 }
134 // Ignore driver attempts to support loader extensions
135 g_driver_instance_extensions.reset(kKHR_surface);
136 g_driver_instance_extensions.reset(kKHR_android_surface);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800137
138 return true;
Jesse Hall80523e22016-01-06 16:47:54 -0800139}
140
Jesse Hall1f91d392015-12-11 16:28:44 -0800141// -----------------------------------------------------------------------------
142
143struct Instance {
144 Instance(const VkAllocationCallbacks* alloc_callbacks)
Chia-I Wu0c203242016-03-15 13:44:51 +0800145 : base{{}, *alloc_callbacks},
146 alloc(&base.allocator),
147 num_physical_devices(0) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800148 memset(physical_devices, 0, sizeof(physical_devices));
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600149 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800150 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
Jesse Hall1f91d392015-12-11 16:28:44 -0800151 }
152
Jesse Hall80523e22016-01-06 16:47:54 -0800153 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800154
Chia-I Wu0c203242016-03-15 13:44:51 +0800155 driver::InstanceData base;
Jesse Hall1f91d392015-12-11 16:28:44 -0800156
Jesse Hall1f91d392015-12-11 16:28:44 -0800157 const VkAllocationCallbacks* alloc;
158 uint32_t num_physical_devices;
Courtney Goeltzenleuchter84cd4c22016-03-16 12:20:13 -0600159 VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800160 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800161 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800162
Jesse Hall715b86a2016-01-16 16:34:29 -0800163 DebugReportCallbackList debug_report_callbacks;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600164 InstanceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800165
166 struct {
Jesse Hall1f91d392015-12-11 16:28:44 -0800167 DriverDispatchTable dispatch;
Jesse Hall1f91d392015-12-11 16:28:44 -0800168 } drv; // may eventually be an array
169};
170
171struct Device {
172 Device(Instance* instance_)
Chia-I Wu0c203242016-03-15 13:44:51 +0800173 : base{{}, *instance_->alloc}, instance(instance_) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600174 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800175 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800176
177 driver::DeviceData base;
178
Jesse Hall1f91d392015-12-11 16:28:44 -0800179 Instance* instance;
180 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600181 DeviceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800182};
183
184template <typename THandle>
185struct HandleTraits {};
186template <>
187struct HandleTraits<VkInstance> {
188 typedef Instance LoaderObjectType;
189};
190template <>
191struct HandleTraits<VkPhysicalDevice> {
192 typedef Instance LoaderObjectType;
193};
194template <>
195struct HandleTraits<VkDevice> {
196 typedef Device LoaderObjectType;
197};
198template <>
199struct HandleTraits<VkQueue> {
200 typedef Device LoaderObjectType;
201};
202template <>
203struct HandleTraits<VkCommandBuffer> {
204 typedef Device LoaderObjectType;
205};
206
207template <typename THandle>
208typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
209 THandle handle) {
210 // TODO(jessehall): Make Instance and Device POD types (by removing the
211 // non-default constructors), so that offsetof is actually legal to use.
212 // The specific case we're using here is safe in gcc/clang (and probably
213 // most other C++ compilers), but isn't guaranteed by C++.
214 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
215#pragma clang diagnostic push
216#pragma clang diagnostic ignored "-Winvalid-offsetof"
Chia-I Wu0c203242016-03-15 13:44:51 +0800217 const size_t kBaseOffset = offsetof(ObjectType, base);
Jesse Hall1f91d392015-12-11 16:28:44 -0800218#pragma clang diagnostic pop
219
Chia-I Wu0c203242016-03-15 13:44:51 +0800220 const auto& base = driver::GetData(handle);
221 uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base);
222 uintptr_t object_addr = base_addr - kBaseOffset;
Jesse Hall1f91d392015-12-11 16:28:44 -0800223 return *reinterpret_cast<ObjectType*>(object_addr);
224}
225
226// -----------------------------------------------------------------------------
227
Chia-I Wu0c203242016-03-15 13:44:51 +0800228void DestroyDevice(Device* device, VkDevice vkdevice) {
229 const auto& instance = *device->instance;
230
231 if (vkdevice != VK_NULL_HANDLE)
232 instance.drv.dispatch.DestroyDevice(vkdevice, instance.alloc);
233
Jesse Hall04f4f472015-08-16 19:51:04 -0700234 device->~Device();
Chia-I Wu0c203242016-03-15 13:44:51 +0800235 instance.alloc->pfnFree(instance.alloc->pUserData, device);
Michael Lentine03c64b02015-08-26 18:27:26 -0500236}
237
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700238/*
239 * This function will return the pNext pointer of any
240 * CreateInfo extensions that are not loader extensions.
241 * This is used to skip past the loader extensions prepended
242 * to the list during CreateInstance and CreateDevice.
243 */
244void* StripCreateExtensions(const void* pNext) {
245 VkLayerInstanceCreateInfo* create_info =
246 const_cast<VkLayerInstanceCreateInfo*>(
247 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
248
249 while (
250 create_info &&
251 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
252 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
253 create_info = const_cast<VkLayerInstanceCreateInfo*>(
254 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
255 }
256
257 return create_info;
258}
259
Jesse Hallfee71432016-03-05 22:27:02 -0800260// Clean up and deallocate an Instance; called from both the failure paths in
261// CreateInstance_Top as well as from DestroyInstance_Top. This function does
262// not call down the dispatch chain; that should be done before calling this
263// function, iff the lower vkCreateInstance call has been made and returned
264// successfully.
265void DestroyInstance(Instance* instance,
Chia-I Wu0c203242016-03-15 13:44:51 +0800266 const VkAllocationCallbacks* allocator,
267 VkInstance vkinstance) {
268 if (vkinstance != VK_NULL_HANDLE && instance->drv.dispatch.DestroyInstance)
269 instance->drv.dispatch.DestroyInstance(vkinstance, allocator);
270
Jesse Hallfee71432016-03-05 22:27:02 -0800271 instance->~Instance();
272 allocator->pfnFree(allocator->pUserData, instance);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700273}
274
Jesse Hall1f91d392015-12-11 16:28:44 -0800275} // anonymous namespace
276
277namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500278
Jesse Hall04f4f472015-08-16 19:51:04 -0700279// -----------------------------------------------------------------------------
280// "Bottom" functions. These are called at the end of the instance dispatch
281// chain.
282
Jesse Hall1f91d392015-12-11 16:28:44 -0800283VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
284 const VkAllocationCallbacks* allocator,
285 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700286 VkResult result;
287
Chia-I Wu0c203242016-03-15 13:44:51 +0800288 if (!allocator)
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800289 allocator = &driver::GetDefaultAllocator();
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700290
Chia-I Wu0c203242016-03-15 13:44:51 +0800291 void* instance_mem = allocator->pfnAllocation(
292 allocator->pUserData, sizeof(Instance), alignof(Instance),
293 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
294 if (!instance_mem)
295 return VK_ERROR_OUT_OF_HOST_MEMORY;
296 Instance& instance = *new (instance_mem) Instance(allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700297
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800298 // Check that all enabled extensions are supported
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800299 uint32_t num_driver_extensions = 0;
300 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
301 const char* name = create_info->ppEnabledExtensionNames[i];
302 InstanceExtension id = InstanceExtensionFromName(name);
303 if (id != kInstanceExtensionCount) {
304 if (g_driver_instance_extensions[id]) {
305 num_driver_extensions++;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600306 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800307 continue;
308 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700309 if (id == kKHR_surface || id == kKHR_android_surface) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600310 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800311 continue;
312 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700313 // The loader natively supports debug report.
314 if (id == kEXT_debug_report) {
315 continue;
316 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800317 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800318 }
319
Jesse Halla7ac76d2016-01-08 22:29:42 -0800320 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700321 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800322 driver_create_info.enabledLayerCount = 0;
323 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800324 driver_create_info.enabledExtensionCount = 0;
325 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800326 if (num_driver_extensions > 0) {
327 const char** names = static_cast<const char**>(
328 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800329 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800330 const char* name = create_info->ppEnabledExtensionNames[i];
331 InstanceExtension id = InstanceExtensionFromName(name);
332 if (id != kInstanceExtensionCount) {
333 if (g_driver_instance_extensions[id]) {
334 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800335 continue;
336 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800337 }
338 }
339 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800340 ALOG_ASSERT(
341 driver_create_info.enabledExtensionCount == num_driver_extensions,
342 "counted enabled driver instance extensions twice and got "
343 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800344 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800345
Chia-I Wu0c203242016-03-15 13:44:51 +0800346 VkInstance drv_instance;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800347 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Chia-I Wu0c203242016-03-15 13:44:51 +0800348 &drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700349 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800350 DestroyInstance(&instance, allocator, VK_NULL_HANDLE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700351 return result;
352 }
353
Chia-I Wu0c203242016-03-15 13:44:51 +0800354 if (!driver::SetData(drv_instance, instance.base)) {
355 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700356 return VK_ERROR_INITIALIZATION_FAILED;
357 }
358
Chia-I Wu0c203242016-03-15 13:44:51 +0800359 if (!LoadDriverDispatchTable(drv_instance, g_hwdevice->GetInstanceProcAddr,
360 instance.enabled_extensions,
361 instance.drv.dispatch)) {
362 DestroyInstance(&instance, allocator, drv_instance);
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700363 return VK_ERROR_INITIALIZATION_FAILED;
364 }
365
Jesse Hall04f4f472015-08-16 19:51:04 -0700366 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800367 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800368 drv_instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700369 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800370 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700371 return VK_ERROR_INITIALIZATION_FAILED;
372 }
373 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800374 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800375 drv_instance, &num_physical_devices, instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700376 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800377 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700378 return VK_ERROR_INITIALIZATION_FAILED;
379 }
Jesse Hallb1471272016-01-17 21:36:58 -0800380
381 Vector<VkExtensionProperties> extensions(
382 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700383 for (uint32_t i = 0; i < num_physical_devices; i++) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800384 if (!driver::SetData(instance.physical_devices[i], instance.base)) {
385 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700386 return VK_ERROR_INITIALIZATION_FAILED;
387 }
Jesse Hallb1471272016-01-17 21:36:58 -0800388
389 uint32_t count;
390 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
391 instance.physical_devices[i], nullptr, &count, nullptr)) !=
392 VK_SUCCESS) {
393 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
394 result);
395 continue;
396 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800397 try {
398 extensions.resize(count);
399 } catch (std::bad_alloc&) {
400 ALOGE("instance creation failed: out of memory");
Chia-I Wu0c203242016-03-15 13:44:51 +0800401 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall26cecff2016-01-21 19:52:25 -0800402 return VK_ERROR_OUT_OF_HOST_MEMORY;
403 }
Jesse Hallb1471272016-01-17 21:36:58 -0800404 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
405 instance.physical_devices[i], nullptr, &count,
406 extensions.data())) != VK_SUCCESS) {
407 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
408 result);
409 continue;
410 }
411 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
412 for (const auto& extension : extensions) {
413 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
414 DeviceExtension id =
415 DeviceExtensionFromName(extension.extensionName);
416 if (id == kDeviceExtensionCount) {
417 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
418 extension.extensionName);
419 } else {
420 instance.physical_device_driver_extensions[i].set(id);
421 }
422 }
423 // Ignore driver attempts to support loader extensions
424 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700425 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800426 instance.num_physical_devices = num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800427
Chia-I Wu0c203242016-03-15 13:44:51 +0800428 *vkinstance = drv_instance;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700429
Jesse Hall04f4f472015-08-16 19:51:04 -0700430 return VK_SUCCESS;
431}
432
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600433VkResult CreateAndroidSurfaceKHR_Disabled(VkInstance,
434 const VkAndroidSurfaceCreateInfoKHR*,
435 const VkAllocationCallbacks*,
436 VkSurfaceKHR*) {
437 ALOGE(
438 "VK_KHR_android_surface not enabled. vkCreateAndroidSurfaceKHR not "
439 "executed.");
440
441 return VK_SUCCESS;
442}
443
444void DestroySurfaceKHR_Disabled(VkInstance,
445 VkSurfaceKHR,
446 const VkAllocationCallbacks*) {
447 ALOGE("VK_KHR_surface not enabled. vkDestroySurfaceKHR not executed.");
448}
449
450VkResult GetPhysicalDeviceSurfaceSupportKHR_Disabled(VkPhysicalDevice,
451 uint32_t,
452 VkSurfaceKHR,
453 VkBool32*) {
454 ALOGE(
455 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceSupportKHR not "
456 "executed.");
457
458 return VK_SUCCESS;
459}
460
461VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled(
462 VkPhysicalDevice,
463 VkSurfaceKHR,
464 VkSurfaceCapabilitiesKHR*) {
465 ALOGE(
466 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceapabilitiesKHR "
467 "not executed.");
468
469 return VK_SUCCESS;
470}
471
472VkResult GetPhysicalDeviceSurfaceFormatsKHR_Disabled(VkPhysicalDevice,
473 VkSurfaceKHR,
474 uint32_t*,
475 VkSurfaceFormatKHR*) {
476 ALOGE(
477 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceFormatsKHR not "
478 "executed.");
479
480 return VK_SUCCESS;
481}
482
483VkResult GetPhysicalDeviceSurfacePresentModesKHR_Disabled(VkPhysicalDevice,
484 VkSurfaceKHR,
485 uint32_t*,
486 VkPresentModeKHR*) {
487 ALOGE(
488 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfacePresentModesKHR "
489 "not executed.");
490
491 return VK_SUCCESS;
492}
493
494PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance vkinstance,
495 const char* name) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800496 PFN_vkVoidFunction pfn;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600497
498 if (vkinstance) {
499 Instance& instance = GetDispatchParent(vkinstance);
500 if (!instance.enabled_extensions[kKHR_android_surface]) {
501 // KHR_android_surface is not enabled, use error stubs instead
502 if (strcmp(name, "vkCreateAndroidSurfaceKHR") == 0) {
503 return reinterpret_cast<PFN_vkVoidFunction>(
504 CreateAndroidSurfaceKHR_Disabled);
505 }
506 }
507 if (!instance.enabled_extensions[kKHR_surface]) {
508 // KHR_surface is not enabled, use error stubs instead
509 if (strcmp(name, "vkDestroySurfaceKHR") == 0) {
510 return reinterpret_cast<PFN_vkVoidFunction>(
511 DestroySurfaceKHR_Disabled);
512 }
513 if (strcmp(name, "vkGetPhysicalDeviceSurfaceSupportKHR") == 0) {
514 return reinterpret_cast<PFN_vkVoidFunction>(
515 GetPhysicalDeviceSurfaceSupportKHR_Disabled);
516 }
517 if (strcmp(name, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR") ==
518 0) {
519 return reinterpret_cast<PFN_vkVoidFunction>(
520 GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled);
521 }
522 if (strcmp(name, "vkGetPhysicalDeviceSurfaceFormatsKHR") == 0) {
523 return reinterpret_cast<PFN_vkVoidFunction>(
524 GetPhysicalDeviceSurfaceFormatsKHR_Disabled);
525 }
526 if (strcmp(name, "vkGetPhysicalDeviceSurfacePresentModesKHR") ==
527 0) {
528 return reinterpret_cast<PFN_vkVoidFunction>(
529 GetPhysicalDeviceSurfacePresentModesKHR_Disabled);
530 }
531 }
532 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800533 if ((pfn = GetLoaderBottomProcAddr(name)))
534 return pfn;
Chia-I Wu0c203242016-03-15 13:44:51 +0800535 return g_hwdevice->GetInstanceProcAddr(vkinstance, name);
Jesse Hall1f91d392015-12-11 16:28:44 -0800536}
537
538VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
539 uint32_t* pdev_count,
540 VkPhysicalDevice* pdevs) {
541 Instance& instance = GetDispatchParent(vkinstance);
542 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700543 if (pdevs) {
544 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800545 std::copy(instance.physical_devices, instance.physical_devices + count,
546 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700547 }
548 *pdev_count = count;
549 return VK_SUCCESS;
550}
551
Jesse Hall1f91d392015-12-11 16:28:44 -0800552void GetPhysicalDeviceProperties_Bottom(
553 VkPhysicalDevice pdev,
554 VkPhysicalDeviceProperties* properties) {
555 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
556 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700557}
558
Jesse Hall1f91d392015-12-11 16:28:44 -0800559void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
560 VkPhysicalDeviceFeatures* features) {
561 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
562 features);
563}
564
565void GetPhysicalDeviceMemoryProperties_Bottom(
566 VkPhysicalDevice pdev,
567 VkPhysicalDeviceMemoryProperties* properties) {
568 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
569 pdev, properties);
570}
571
572void GetPhysicalDeviceQueueFamilyProperties_Bottom(
573 VkPhysicalDevice pdev,
574 uint32_t* pCount,
575 VkQueueFamilyProperties* properties) {
576 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
577 pdev, pCount, properties);
578}
579
580void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
581 VkFormat format,
582 VkFormatProperties* properties) {
583 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700584 pdev, format, properties);
585}
586
Jesse Hall1f91d392015-12-11 16:28:44 -0800587VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700588 VkPhysicalDevice pdev,
589 VkFormat format,
590 VkImageType type,
591 VkImageTiling tiling,
592 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700593 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700594 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800595 return GetDispatchParent(pdev)
596 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800597 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700598}
599
Jesse Hall1f91d392015-12-11 16:28:44 -0800600void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700601 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800602 VkFormat format,
603 VkImageType type,
604 VkSampleCountFlagBits samples,
605 VkImageUsageFlags usage,
606 VkImageTiling tiling,
607 uint32_t* properties_count,
608 VkSparseImageFormatProperties* properties) {
609 GetDispatchParent(pdev)
610 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
611 pdev, format, type, samples, usage, tiling, properties_count,
612 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700613}
614
Jesse Halle1b12782015-11-30 11:27:32 -0800615VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800616VkResult EnumerateDeviceExtensionProperties_Bottom(
Chia-I Wu0c203242016-03-15 13:44:51 +0800617 VkPhysicalDevice pdev,
618 const char* layer_name,
619 uint32_t* properties_count,
620 VkExtensionProperties* properties) {
621 (void)layer_name;
622
623 Instance& instance = GetDispatchParent(pdev);
624
625 size_t gpu_idx = 0;
626 while (instance.physical_devices[gpu_idx] != pdev)
627 gpu_idx++;
628 const DeviceExtensionSet driver_extensions =
629 instance.physical_device_driver_extensions[gpu_idx];
630
631 // We only support VK_KHR_swapchain if the GPU supports
632 // VK_ANDROID_native_buffer
633 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
634 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
635 uint32_t num_extensions = 0;
636 if (driver_extensions[kANDROID_native_buffer]) {
637 available[num_extensions++] = VkExtensionProperties{
638 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
639 }
640
641 if (!properties || *properties_count > num_extensions)
642 *properties_count = num_extensions;
643 if (properties)
644 std::copy(available, available + *properties_count, properties);
645
646 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700647}
648
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700649// This is a no-op, the Top function returns the aggregate layer property
650// data. This is to keep the dispatch generator happy.
Jesse Halle1b12782015-11-30 11:27:32 -0800651VKAPI_ATTR
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700652VkResult EnumerateDeviceLayerProperties_Bottom(
653 VkPhysicalDevice /*pdev*/,
654 uint32_t* /*properties_count*/,
655 VkLayerProperties* /*properties*/) {
656 return VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800657}
658
659VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800660VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800661 const VkDeviceCreateInfo* create_info,
662 const VkAllocationCallbacks* allocator,
663 VkDevice* device_out) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700664 Instance& instance = GetDispatchParent(gpu);
Chia-I Wu0c203242016-03-15 13:44:51 +0800665
666 // FIXME(jessehall): We don't have good conventions or infrastructure yet to
667 // do better than just using the instance allocator and scope for
668 // everything. See b/26732122.
669 if (true /*!allocator*/)
670 allocator = instance.alloc;
671
672 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
673 alignof(Device),
674 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
675 if (!mem)
676 return VK_ERROR_OUT_OF_HOST_MEMORY;
677 Device* device = new (mem) Device(&instance);
678
Jesse Hallb1471272016-01-17 21:36:58 -0800679 size_t gpu_idx = 0;
680 while (instance.physical_devices[gpu_idx] != gpu)
681 gpu_idx++;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700682
683 VkDeviceCreateInfo driver_create_info = *create_info;
684 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
685 driver_create_info.enabledLayerCount = 0;
686 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800687
688 uint32_t num_driver_extensions = 0;
689 const char** driver_extensions = static_cast<const char**>(
690 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
691 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
692 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800693 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800694 if (id != kDeviceExtensionCount) {
695 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
696 driver_extensions[num_driver_extensions++] = name;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600697 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800698 continue;
699 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700700 // Add the VK_ANDROID_native_buffer extension to the list iff
701 // the VK_KHR_swapchain extension was requested
Jesse Hallae3b70d2016-01-17 22:05:29 -0800702 if (id == kKHR_swapchain &&
703 instance.physical_device_driver_extensions
704 [gpu_idx][kANDROID_native_buffer]) {
705 driver_extensions[num_driver_extensions++] =
706 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600707 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800708 continue;
709 }
Jesse Hallb1471272016-01-17 21:36:58 -0800710 }
Jesse Hallb1471272016-01-17 21:36:58 -0800711 }
712
Jesse Hallb1471272016-01-17 21:36:58 -0800713 driver_create_info.enabledExtensionCount = num_driver_extensions;
714 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Hall04f4f472015-08-16 19:51:04 -0700715 VkDevice drv_device;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700716 VkResult result = instance.drv.dispatch.CreateDevice(
717 gpu, &driver_create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700718 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800719 DestroyDevice(device, VK_NULL_HANDLE);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700720 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700721 }
722
Chia-I Wu0c203242016-03-15 13:44:51 +0800723 if (!driver::SetData(drv_device, device->base)) {
724 DestroyDevice(device, drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700725 return VK_ERROR_INITIALIZATION_FAILED;
726 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700727
Jesse Hall1f91d392015-12-11 16:28:44 -0800728 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
729 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
730 "vkGetDeviceProcAddr"));
Jesse Hall1f91d392015-12-11 16:28:44 -0800731 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700732 return VK_SUCCESS;
733}
734
Jesse Hall1f91d392015-12-11 16:28:44 -0800735void DestroyInstance_Bottom(VkInstance vkinstance,
736 const VkAllocationCallbacks* allocator) {
737 Instance& instance = GetDispatchParent(vkinstance);
738
Chia-I Wu0c203242016-03-15 13:44:51 +0800739 VkAllocationCallbacks local_allocator;
740 if (!allocator) {
741 local_allocator = *instance.alloc;
742 allocator = &local_allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800743 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800744
745 DestroyInstance(&instance, allocator, vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700746}
747
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600748VkResult CreateSwapchainKHR_Disabled(VkDevice,
749 const VkSwapchainCreateInfoKHR*,
750 const VkAllocationCallbacks*,
751 VkSwapchainKHR*) {
752 ALOGE("VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
753
754 return VK_SUCCESS;
755}
756
757void DestroySwapchainKHR_Disabled(VkDevice,
758 VkSwapchainKHR,
759 const VkAllocationCallbacks*) {
760 ALOGE("VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
761}
762
763VkResult GetSwapchainImagesKHR_Disabled(VkDevice,
764 VkSwapchainKHR,
765 uint32_t*,
766 VkImage*) {
767 ALOGE(
768 "VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
769
770 return VK_SUCCESS;
771}
772
773VkResult AcquireNextImageKHR_Disabled(VkDevice,
774 VkSwapchainKHR,
775 uint64_t,
776 VkSemaphore,
777 VkFence,
778 uint32_t*) {
779 ALOGE("VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
780
781 return VK_SUCCESS;
782}
783
784VkResult QueuePresentKHR_Disabled(VkQueue, const VkPresentInfoKHR*) {
785 ALOGE("VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
786
787 return VK_SUCCESS;
788}
789
Jesse Hall1f91d392015-12-11 16:28:44 -0800790PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
791 const char* name) {
792 if (strcmp(name, "vkCreateDevice") == 0) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700793 return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom);
Michael Lentine03c64b02015-08-26 18:27:26 -0500794 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800795
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600796 Device& device = GetDispatchParent(vkdevice);
797 if (!device.enabled_extensions[kKHR_swapchain]) {
798 if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
799 return reinterpret_cast<PFN_vkVoidFunction>(
800 CreateSwapchainKHR_Disabled);
801 }
802 if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
803 return reinterpret_cast<PFN_vkVoidFunction>(
804 DestroySwapchainKHR_Disabled);
805 }
806 if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
807 return reinterpret_cast<PFN_vkVoidFunction>(
808 GetSwapchainImagesKHR_Disabled);
809 }
810 if (strcmp(name, "vkAcquireNextSwapchainImageKHR") == 0) {
811 return reinterpret_cast<PFN_vkVoidFunction>(
812 AcquireNextImageKHR_Disabled);
813 }
814 if (strcmp(name, "vkQueuePresentKHR") == 0) {
815 return reinterpret_cast<PFN_vkVoidFunction>(
816 QueuePresentKHR_Disabled);
817 }
818 }
819
Jesse Hall1f91d392015-12-11 16:28:44 -0800820 // VK_ANDROID_native_buffer should be hidden from applications and layers.
821 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
822 PFN_vkVoidFunction pfn;
823 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
824 strcmp(name, "vkAcquireImageANDROID") == 0 ||
825 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
826 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500827 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800828 if ((pfn = GetLoaderBottomProcAddr(name)))
829 return pfn;
830 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700831}
832
Chia-I Wu0c203242016-03-15 13:44:51 +0800833void DestroyDevice_Bottom(VkDevice vkdevice, const VkAllocationCallbacks*) {
834 DestroyDevice(&GetDispatchParent(vkdevice), vkdevice);
Jesse Hall04f4f472015-08-16 19:51:04 -0700835}
836
Chia-I Wu0c203242016-03-15 13:44:51 +0800837void GetDeviceQueue_Bottom(VkDevice vkdevice,
838 uint32_t family,
839 uint32_t index,
840 VkQueue* queue_out) {
841 const auto& device = GetDispatchParent(vkdevice);
842 const auto& instance = *device.instance;
Jesse Hall04f4f472015-08-16 19:51:04 -0700843
Chia-I Wu0c203242016-03-15 13:44:51 +0800844 instance.drv.dispatch.GetDeviceQueue(vkdevice, family, index, queue_out);
845 driver::SetData(*queue_out, device.base);
Jesse Hall04f4f472015-08-16 19:51:04 -0700846}
847
Chia-I Wu0c203242016-03-15 13:44:51 +0800848VkResult AllocateCommandBuffers_Bottom(
Jesse Hall1f91d392015-12-11 16:28:44 -0800849 VkDevice vkdevice,
850 const VkCommandBufferAllocateInfo* alloc_info,
851 VkCommandBuffer* cmdbufs) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800852 const auto& device = GetDispatchParent(vkdevice);
853 const auto& instance = *device.instance;
Jesse Hallc7a6eb52015-08-31 12:52:03 -0700854
Chia-I Wu0c203242016-03-15 13:44:51 +0800855 VkResult result = instance.drv.dispatch.AllocateCommandBuffers(
856 vkdevice, alloc_info, cmdbufs);
857 if (result == VK_SUCCESS) {
858 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++)
859 driver::SetData(cmdbufs[i], device.base);
860 }
861
862 return result;
Jesse Hall04f4f472015-08-16 19:51:04 -0700863}
864
Jesse Hall1f91d392015-12-11 16:28:44 -0800865// -----------------------------------------------------------------------------
866
867const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
868 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800869}
870
Jesse Hall1f91d392015-12-11 16:28:44 -0800871const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
872 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800873}
874
Jesse Hall715b86a2016-01-16 16:34:29 -0800875VkInstance GetDriverInstance(VkInstance instance) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800876 return instance;
Jesse Hall715b86a2016-01-16 16:34:29 -0800877}
878
879const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
880 return GetDispatchParent(instance).drv.dispatch;
881}
882
Jesse Hall1f91d392015-12-11 16:28:44 -0800883const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
884 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700885}
886
Jesse Hall1f91d392015-12-11 16:28:44 -0800887const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
888 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700889}
890
Jesse Hall715b86a2016-01-16 16:34:29 -0800891DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
892 return GetDispatchParent(instance).debug_report_callbacks;
893}
894
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800895bool InitLoader(hwvulkan_device_t* dev) {
896 if (!g_hwdevice) {
897 g_hwdevice = dev;
898 if (!LoadVulkanHAL())
899 g_hwdevice = nullptr;
900 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800901
902 return (g_hwdevice != nullptr);
903}
904
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800905namespace driver {
906
Chia-I Wu0c203242016-03-15 13:44:51 +0800907PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
908 return GetInstanceProcAddr_Bottom(instance, pName);
909}
910
911PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
912 return GetDeviceProcAddr_Bottom(device, pName);
913}
914
915VkResult EnumerateInstanceExtensionProperties(
916 const char* pLayerName,
917 uint32_t* pPropertyCount,
918 VkExtensionProperties* pProperties) {
919 (void)pLayerName;
920
921 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
922 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
923 uint32_t num_extensions = 0;
924
925 available[num_extensions++] = VkExtensionProperties{
926 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
927 available[num_extensions++] =
928 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
929 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
930 if (g_driver_instance_extensions[kEXT_debug_report]) {
931 available[num_extensions++] =
932 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
933 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
934 }
935
936 if (!pProperties || *pPropertyCount > num_extensions)
937 *pPropertyCount = num_extensions;
938 if (pProperties)
939 std::copy(available, available + *pPropertyCount, pProperties);
940
941 return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
942}
943
944} // namespace driver
945
Jesse Hall04f4f472015-08-16 19:51:04 -0700946} // namespace vulkan