blob: 65b09db1459ec0e2e1d163314753013338e45c86 [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
Jesse Hall26cecff2016-01-21 19:52:25 -080042// #define ENABLE_ALLOC_CALLSTACKS 1
43#if ENABLE_ALLOC_CALLSTACKS
44#include <utils/CallStack.h>
45#define ALOGD_CALLSTACK(...) \
46 do { \
47 ALOGD(__VA_ARGS__); \
48 android::CallStack callstack; \
49 callstack.update(); \
50 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
51 } while (false)
52#else
53#define ALOGD_CALLSTACK(...) \
54 do { \
55 } while (false)
56#endif
57
Jesse Hall04f4f472015-08-16 19:51:04 -070058using namespace vulkan;
59
60static const uint32_t kMaxPhysicalDevices = 4;
61
Michael Lentine03c64b02015-08-26 18:27:26 -050062namespace {
63
Jesse Hall1f91d392015-12-11 16:28:44 -080064// ----------------------------------------------------------------------------
Michael Lentine03c64b02015-08-26 18:27:26 -050065
Jesse Hall3fbc8562015-11-29 22:10:52 -080066// Standard-library allocator that delegates to VkAllocationCallbacks.
Jesse Hall03b6fe12015-11-24 12:44:21 -080067//
68// TODO(jessehall): This class currently always uses
Jesse Hall3fbc8562015-11-29 22:10:52 -080069// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
Jesse Hall03b6fe12015-11-24 12:44:21 -080070// parameter or a constructor parameter. The former would help catch bugs
71// where we use the wrong scope, e.g. adding a command-scope string to an
72// instance-scope vector. But that might also be pretty annoying to deal with.
Michael Lentine03c64b02015-08-26 18:27:26 -050073template <class T>
74class CallbackAllocator {
75 public:
76 typedef T value_type;
77
Jesse Hall3fbc8562015-11-29 22:10:52 -080078 CallbackAllocator(const VkAllocationCallbacks* alloc_input)
Michael Lentine03c64b02015-08-26 18:27:26 -050079 : alloc(alloc_input) {}
80
81 template <class T2>
82 CallbackAllocator(const CallbackAllocator<T2>& other)
83 : alloc(other.alloc) {}
84
85 T* allocate(std::size_t n) {
Jesse Hall3fbc8562015-11-29 22:10:52 -080086 void* mem =
87 alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
88 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall26cecff2016-01-21 19:52:25 -080089 if (!mem)
90 throw std::bad_alloc();
Michael Lentine03c64b02015-08-26 18:27:26 -050091 return static_cast<T*>(mem);
92 }
93
Jesse Hall26cecff2016-01-21 19:52:25 -080094 void deallocate(T* array, std::size_t /*n*/) noexcept {
Michael Lentine03c64b02015-08-26 18:27:26 -050095 alloc->pfnFree(alloc->pUserData, array);
96 }
97
Jesse Hall3fbc8562015-11-29 22:10:52 -080098 const VkAllocationCallbacks* alloc;
Michael Lentine03c64b02015-08-26 18:27:26 -050099};
100// These are needed in order to move Strings
101template <class T>
102bool operator==(const CallbackAllocator<T>& alloc1,
103 const CallbackAllocator<T>& alloc2) {
104 return alloc1.alloc == alloc2.alloc;
105}
106template <class T>
107bool operator!=(const CallbackAllocator<T>& alloc1,
108 const CallbackAllocator<T>& alloc2) {
109 return !(alloc1 == alloc2);
110}
111
Michael Lentine03c64b02015-08-26 18:27:26 -0500112template <class T>
Jesse Hall1f91d392015-12-11 16:28:44 -0800113using Vector = std::vector<T, CallbackAllocator<T>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500114
Jesse Hall1f91d392015-12-11 16:28:44 -0800115typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
116 String;
Michael Lentine03c64b02015-08-26 18:27:26 -0500117
Jesse Hall1f91d392015-12-11 16:28:44 -0800118// ----------------------------------------------------------------------------
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500119
Jesse Halle1b12782015-11-30 11:27:32 -0800120VKAPI_ATTR void* DefaultAllocate(void*,
121 size_t size,
122 size_t alignment,
123 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800124 void* ptr = nullptr;
125 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
126 // additionally requires that it be at least sizeof(void*).
Jesse Hall26cecff2016-01-21 19:52:25 -0800127 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
128 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
129 ret, ptr);
130 return ret == 0 ? ptr : nullptr;
Jesse Hall03b6fe12015-11-24 12:44:21 -0800131}
132
Jesse Halle1b12782015-11-30 11:27:32 -0800133VKAPI_ATTR void* DefaultReallocate(void*,
134 void* ptr,
135 size_t size,
136 size_t alignment,
137 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800138 if (size == 0) {
139 free(ptr);
140 return nullptr;
141 }
142
143 // TODO(jessehall): Right now we never shrink allocations; if the new
144 // request is smaller than the existing chunk, we just continue using it.
145 // Right now the loader never reallocs, so this doesn't matter. If that
146 // changes, or if this code is copied into some other project, this should
147 // probably have a heuristic to allocate-copy-free when doing so will save
148 // "enough" space.
149 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
150 if (size <= old_size)
151 return ptr;
152
153 void* new_ptr = nullptr;
Chia-I Wu0c203242016-03-15 13:44:51 +0800154 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
Jesse Hall03b6fe12015-11-24 12:44:21 -0800155 return nullptr;
156 if (ptr) {
157 memcpy(new_ptr, ptr, std::min(old_size, size));
158 free(ptr);
159 }
160 return new_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700161}
162
Jesse Hall26cecff2016-01-21 19:52:25 -0800163VKAPI_ATTR void DefaultFree(void*, void* ptr) {
164 ALOGD_CALLSTACK("Free: %p", ptr);
165 free(ptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700166}
167
Jesse Hall3fbc8562015-11-29 22:10:52 -0800168const VkAllocationCallbacks kDefaultAllocCallbacks = {
Jesse Hall04f4f472015-08-16 19:51:04 -0700169 .pUserData = nullptr,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800170 .pfnAllocation = DefaultAllocate,
171 .pfnReallocation = DefaultReallocate,
Jesse Hall04f4f472015-08-16 19:51:04 -0700172 .pfnFree = DefaultFree,
173};
174
Jesse Hall1f91d392015-12-11 16:28:44 -0800175// ----------------------------------------------------------------------------
Jesse Hall80523e22016-01-06 16:47:54 -0800176// Global Data and Initialization
Jesse Hall1f91d392015-12-11 16:28:44 -0800177
Jesse Hall80523e22016-01-06 16:47:54 -0800178hwvulkan_device_t* g_hwdevice = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800179InstanceExtensionSet g_driver_instance_extensions;
180
Jesse Hall80523e22016-01-06 16:47:54 -0800181void LoadVulkanHAL() {
182 static const hwvulkan_module_t* module;
183 int result =
184 hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
185 if (result != 0) {
186 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result);
187 return;
188 }
189 result = module->common.methods->open(
190 &module->common, HWVULKAN_DEVICE_0,
191 reinterpret_cast<hw_device_t**>(&g_hwdevice));
192 if (result != 0) {
193 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
194 result);
195 module = nullptr;
196 return;
197 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800198
199 VkResult vkresult;
200 uint32_t count;
201 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
202 nullptr, &count, nullptr)) != VK_SUCCESS) {
203 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
204 vkresult);
205 g_hwdevice->common.close(&g_hwdevice->common);
206 g_hwdevice = nullptr;
207 module = nullptr;
208 return;
209 }
210 VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
211 alloca(count * sizeof(VkExtensionProperties)));
212 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
213 nullptr, &count, extensions)) != VK_SUCCESS) {
214 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
215 vkresult);
216 g_hwdevice->common.close(&g_hwdevice->common);
217 g_hwdevice = nullptr;
218 module = nullptr;
219 return;
220 }
221 ALOGV_IF(count > 0, "Driver-supported instance extensions:");
222 for (uint32_t i = 0; i < count; i++) {
223 ALOGV(" %s (v%u)", extensions[i].extensionName,
224 extensions[i].specVersion);
225 InstanceExtension id =
226 InstanceExtensionFromName(extensions[i].extensionName);
227 if (id != kInstanceExtensionCount)
228 g_driver_instance_extensions.set(id);
229 }
230 // Ignore driver attempts to support loader extensions
231 g_driver_instance_extensions.reset(kKHR_surface);
232 g_driver_instance_extensions.reset(kKHR_android_surface);
Jesse Hall80523e22016-01-06 16:47:54 -0800233}
234
Jesse Hall1f91d392015-12-11 16:28:44 -0800235// -----------------------------------------------------------------------------
236
237struct Instance {
238 Instance(const VkAllocationCallbacks* alloc_callbacks)
Chia-I Wu0c203242016-03-15 13:44:51 +0800239 : base{{}, *alloc_callbacks},
240 alloc(&base.allocator),
241 num_physical_devices(0) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800242 memset(physical_devices, 0, sizeof(physical_devices));
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600243 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800244 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
Jesse Hall1f91d392015-12-11 16:28:44 -0800245 }
246
Jesse Hall80523e22016-01-06 16:47:54 -0800247 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800248
Chia-I Wu0c203242016-03-15 13:44:51 +0800249 driver::InstanceData base;
Jesse Hall1f91d392015-12-11 16:28:44 -0800250
Jesse Hall1f91d392015-12-11 16:28:44 -0800251 const VkAllocationCallbacks* alloc;
252 uint32_t num_physical_devices;
Courtney Goeltzenleuchter84cd4c22016-03-16 12:20:13 -0600253 VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800254 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800255 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800256
Jesse Hall715b86a2016-01-16 16:34:29 -0800257 DebugReportCallbackList debug_report_callbacks;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600258 InstanceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800259
260 struct {
Jesse Hall1f91d392015-12-11 16:28:44 -0800261 DriverDispatchTable dispatch;
Jesse Hall1f91d392015-12-11 16:28:44 -0800262 } drv; // may eventually be an array
263};
264
265struct Device {
266 Device(Instance* instance_)
Chia-I Wu0c203242016-03-15 13:44:51 +0800267 : base{{}, *instance_->alloc}, instance(instance_) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600268 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800269 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800270
271 driver::DeviceData base;
272
Jesse Hall1f91d392015-12-11 16:28:44 -0800273 Instance* instance;
274 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600275 DeviceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800276};
277
278template <typename THandle>
279struct HandleTraits {};
280template <>
281struct HandleTraits<VkInstance> {
282 typedef Instance LoaderObjectType;
283};
284template <>
285struct HandleTraits<VkPhysicalDevice> {
286 typedef Instance LoaderObjectType;
287};
288template <>
289struct HandleTraits<VkDevice> {
290 typedef Device LoaderObjectType;
291};
292template <>
293struct HandleTraits<VkQueue> {
294 typedef Device LoaderObjectType;
295};
296template <>
297struct HandleTraits<VkCommandBuffer> {
298 typedef Device LoaderObjectType;
299};
300
301template <typename THandle>
302typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
303 THandle handle) {
304 // TODO(jessehall): Make Instance and Device POD types (by removing the
305 // non-default constructors), so that offsetof is actually legal to use.
306 // The specific case we're using here is safe in gcc/clang (and probably
307 // most other C++ compilers), but isn't guaranteed by C++.
308 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
309#pragma clang diagnostic push
310#pragma clang diagnostic ignored "-Winvalid-offsetof"
Chia-I Wu0c203242016-03-15 13:44:51 +0800311 const size_t kBaseOffset = offsetof(ObjectType, base);
Jesse Hall1f91d392015-12-11 16:28:44 -0800312#pragma clang diagnostic pop
313
Chia-I Wu0c203242016-03-15 13:44:51 +0800314 const auto& base = driver::GetData(handle);
315 uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base);
316 uintptr_t object_addr = base_addr - kBaseOffset;
Jesse Hall1f91d392015-12-11 16:28:44 -0800317 return *reinterpret_cast<ObjectType*>(object_addr);
318}
319
320// -----------------------------------------------------------------------------
321
Chia-I Wu0c203242016-03-15 13:44:51 +0800322void DestroyDevice(Device* device, VkDevice vkdevice) {
323 const auto& instance = *device->instance;
324
325 if (vkdevice != VK_NULL_HANDLE)
326 instance.drv.dispatch.DestroyDevice(vkdevice, instance.alloc);
327
Jesse Hall04f4f472015-08-16 19:51:04 -0700328 device->~Device();
Chia-I Wu0c203242016-03-15 13:44:51 +0800329 instance.alloc->pfnFree(instance.alloc->pUserData, device);
Michael Lentine03c64b02015-08-26 18:27:26 -0500330}
331
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700332/*
333 * This function will return the pNext pointer of any
334 * CreateInfo extensions that are not loader extensions.
335 * This is used to skip past the loader extensions prepended
336 * to the list during CreateInstance and CreateDevice.
337 */
338void* StripCreateExtensions(const void* pNext) {
339 VkLayerInstanceCreateInfo* create_info =
340 const_cast<VkLayerInstanceCreateInfo*>(
341 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
342
343 while (
344 create_info &&
345 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
346 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
347 create_info = const_cast<VkLayerInstanceCreateInfo*>(
348 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
349 }
350
351 return create_info;
352}
353
Jesse Hallfee71432016-03-05 22:27:02 -0800354// Clean up and deallocate an Instance; called from both the failure paths in
355// CreateInstance_Top as well as from DestroyInstance_Top. This function does
356// not call down the dispatch chain; that should be done before calling this
357// function, iff the lower vkCreateInstance call has been made and returned
358// successfully.
359void DestroyInstance(Instance* instance,
Chia-I Wu0c203242016-03-15 13:44:51 +0800360 const VkAllocationCallbacks* allocator,
361 VkInstance vkinstance) {
362 if (vkinstance != VK_NULL_HANDLE && instance->drv.dispatch.DestroyInstance)
363 instance->drv.dispatch.DestroyInstance(vkinstance, allocator);
364
Jesse Hallfee71432016-03-05 22:27:02 -0800365 instance->~Instance();
366 allocator->pfnFree(allocator->pUserData, instance);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700367}
368
Jesse Hall1f91d392015-12-11 16:28:44 -0800369} // anonymous namespace
370
371namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500372
Jesse Hall04f4f472015-08-16 19:51:04 -0700373// -----------------------------------------------------------------------------
374// "Bottom" functions. These are called at the end of the instance dispatch
375// chain.
376
Jesse Hall1f91d392015-12-11 16:28:44 -0800377VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
378 const VkAllocationCallbacks* allocator,
379 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700380 VkResult result;
381
Chia-I Wu0c203242016-03-15 13:44:51 +0800382 if (!allocator)
383 allocator = &kDefaultAllocCallbacks;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700384
Chia-I Wu0c203242016-03-15 13:44:51 +0800385 void* instance_mem = allocator->pfnAllocation(
386 allocator->pUserData, sizeof(Instance), alignof(Instance),
387 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
388 if (!instance_mem)
389 return VK_ERROR_OUT_OF_HOST_MEMORY;
390 Instance& instance = *new (instance_mem) Instance(allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700391
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800392 // Check that all enabled extensions are supported
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800393 uint32_t num_driver_extensions = 0;
394 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
395 const char* name = create_info->ppEnabledExtensionNames[i];
396 InstanceExtension id = InstanceExtensionFromName(name);
397 if (id != kInstanceExtensionCount) {
398 if (g_driver_instance_extensions[id]) {
399 num_driver_extensions++;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600400 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800401 continue;
402 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700403 if (id == kKHR_surface || id == kKHR_android_surface) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600404 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800405 continue;
406 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700407 // The loader natively supports debug report.
408 if (id == kEXT_debug_report) {
409 continue;
410 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800411 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800412 }
413
Jesse Halla7ac76d2016-01-08 22:29:42 -0800414 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700415 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800416 driver_create_info.enabledLayerCount = 0;
417 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800418 driver_create_info.enabledExtensionCount = 0;
419 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800420 if (num_driver_extensions > 0) {
421 const char** names = static_cast<const char**>(
422 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800423 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800424 const char* name = create_info->ppEnabledExtensionNames[i];
425 InstanceExtension id = InstanceExtensionFromName(name);
426 if (id != kInstanceExtensionCount) {
427 if (g_driver_instance_extensions[id]) {
428 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800429 continue;
430 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800431 }
432 }
433 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800434 ALOG_ASSERT(
435 driver_create_info.enabledExtensionCount == num_driver_extensions,
436 "counted enabled driver instance extensions twice and got "
437 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800438 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800439
Chia-I Wu0c203242016-03-15 13:44:51 +0800440 VkInstance drv_instance;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800441 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Chia-I Wu0c203242016-03-15 13:44:51 +0800442 &drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700443 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800444 DestroyInstance(&instance, allocator, VK_NULL_HANDLE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700445 return result;
446 }
447
Chia-I Wu0c203242016-03-15 13:44:51 +0800448 if (!driver::SetData(drv_instance, instance.base)) {
449 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700450 return VK_ERROR_INITIALIZATION_FAILED;
451 }
452
Chia-I Wu0c203242016-03-15 13:44:51 +0800453 if (!LoadDriverDispatchTable(drv_instance, g_hwdevice->GetInstanceProcAddr,
454 instance.enabled_extensions,
455 instance.drv.dispatch)) {
456 DestroyInstance(&instance, allocator, drv_instance);
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700457 return VK_ERROR_INITIALIZATION_FAILED;
458 }
459
Jesse Hall04f4f472015-08-16 19:51:04 -0700460 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800461 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800462 drv_instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700463 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800464 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700465 return VK_ERROR_INITIALIZATION_FAILED;
466 }
467 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800468 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800469 drv_instance, &num_physical_devices, instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700470 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800471 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700472 return VK_ERROR_INITIALIZATION_FAILED;
473 }
Jesse Hallb1471272016-01-17 21:36:58 -0800474
475 Vector<VkExtensionProperties> extensions(
476 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700477 for (uint32_t i = 0; i < num_physical_devices; i++) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800478 if (!driver::SetData(instance.physical_devices[i], instance.base)) {
479 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700480 return VK_ERROR_INITIALIZATION_FAILED;
481 }
Jesse Hallb1471272016-01-17 21:36:58 -0800482
483 uint32_t count;
484 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
485 instance.physical_devices[i], nullptr, &count, nullptr)) !=
486 VK_SUCCESS) {
487 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
488 result);
489 continue;
490 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800491 try {
492 extensions.resize(count);
493 } catch (std::bad_alloc&) {
494 ALOGE("instance creation failed: out of memory");
Chia-I Wu0c203242016-03-15 13:44:51 +0800495 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall26cecff2016-01-21 19:52:25 -0800496 return VK_ERROR_OUT_OF_HOST_MEMORY;
497 }
Jesse Hallb1471272016-01-17 21:36:58 -0800498 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
499 instance.physical_devices[i], nullptr, &count,
500 extensions.data())) != VK_SUCCESS) {
501 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
502 result);
503 continue;
504 }
505 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
506 for (const auto& extension : extensions) {
507 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
508 DeviceExtension id =
509 DeviceExtensionFromName(extension.extensionName);
510 if (id == kDeviceExtensionCount) {
511 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
512 extension.extensionName);
513 } else {
514 instance.physical_device_driver_extensions[i].set(id);
515 }
516 }
517 // Ignore driver attempts to support loader extensions
518 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700519 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800520 instance.num_physical_devices = num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800521
Chia-I Wu0c203242016-03-15 13:44:51 +0800522 *vkinstance = drv_instance;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700523
Jesse Hall04f4f472015-08-16 19:51:04 -0700524 return VK_SUCCESS;
525}
526
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600527VkResult CreateAndroidSurfaceKHR_Disabled(VkInstance,
528 const VkAndroidSurfaceCreateInfoKHR*,
529 const VkAllocationCallbacks*,
530 VkSurfaceKHR*) {
531 ALOGE(
532 "VK_KHR_android_surface not enabled. vkCreateAndroidSurfaceKHR not "
533 "executed.");
534
535 return VK_SUCCESS;
536}
537
538void DestroySurfaceKHR_Disabled(VkInstance,
539 VkSurfaceKHR,
540 const VkAllocationCallbacks*) {
541 ALOGE("VK_KHR_surface not enabled. vkDestroySurfaceKHR not executed.");
542}
543
544VkResult GetPhysicalDeviceSurfaceSupportKHR_Disabled(VkPhysicalDevice,
545 uint32_t,
546 VkSurfaceKHR,
547 VkBool32*) {
548 ALOGE(
549 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceSupportKHR not "
550 "executed.");
551
552 return VK_SUCCESS;
553}
554
555VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled(
556 VkPhysicalDevice,
557 VkSurfaceKHR,
558 VkSurfaceCapabilitiesKHR*) {
559 ALOGE(
560 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceapabilitiesKHR "
561 "not executed.");
562
563 return VK_SUCCESS;
564}
565
566VkResult GetPhysicalDeviceSurfaceFormatsKHR_Disabled(VkPhysicalDevice,
567 VkSurfaceKHR,
568 uint32_t*,
569 VkSurfaceFormatKHR*) {
570 ALOGE(
571 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceFormatsKHR not "
572 "executed.");
573
574 return VK_SUCCESS;
575}
576
577VkResult GetPhysicalDeviceSurfacePresentModesKHR_Disabled(VkPhysicalDevice,
578 VkSurfaceKHR,
579 uint32_t*,
580 VkPresentModeKHR*) {
581 ALOGE(
582 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfacePresentModesKHR "
583 "not executed.");
584
585 return VK_SUCCESS;
586}
587
588PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance vkinstance,
589 const char* name) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800590 PFN_vkVoidFunction pfn;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600591
592 if (vkinstance) {
593 Instance& instance = GetDispatchParent(vkinstance);
594 if (!instance.enabled_extensions[kKHR_android_surface]) {
595 // KHR_android_surface is not enabled, use error stubs instead
596 if (strcmp(name, "vkCreateAndroidSurfaceKHR") == 0) {
597 return reinterpret_cast<PFN_vkVoidFunction>(
598 CreateAndroidSurfaceKHR_Disabled);
599 }
600 }
601 if (!instance.enabled_extensions[kKHR_surface]) {
602 // KHR_surface is not enabled, use error stubs instead
603 if (strcmp(name, "vkDestroySurfaceKHR") == 0) {
604 return reinterpret_cast<PFN_vkVoidFunction>(
605 DestroySurfaceKHR_Disabled);
606 }
607 if (strcmp(name, "vkGetPhysicalDeviceSurfaceSupportKHR") == 0) {
608 return reinterpret_cast<PFN_vkVoidFunction>(
609 GetPhysicalDeviceSurfaceSupportKHR_Disabled);
610 }
611 if (strcmp(name, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR") ==
612 0) {
613 return reinterpret_cast<PFN_vkVoidFunction>(
614 GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled);
615 }
616 if (strcmp(name, "vkGetPhysicalDeviceSurfaceFormatsKHR") == 0) {
617 return reinterpret_cast<PFN_vkVoidFunction>(
618 GetPhysicalDeviceSurfaceFormatsKHR_Disabled);
619 }
620 if (strcmp(name, "vkGetPhysicalDeviceSurfacePresentModesKHR") ==
621 0) {
622 return reinterpret_cast<PFN_vkVoidFunction>(
623 GetPhysicalDeviceSurfacePresentModesKHR_Disabled);
624 }
625 }
626 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800627 if ((pfn = GetLoaderBottomProcAddr(name)))
628 return pfn;
Chia-I Wu0c203242016-03-15 13:44:51 +0800629 return g_hwdevice->GetInstanceProcAddr(vkinstance, name);
Jesse Hall1f91d392015-12-11 16:28:44 -0800630}
631
632VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
633 uint32_t* pdev_count,
634 VkPhysicalDevice* pdevs) {
635 Instance& instance = GetDispatchParent(vkinstance);
636 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700637 if (pdevs) {
638 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800639 std::copy(instance.physical_devices, instance.physical_devices + count,
640 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700641 }
642 *pdev_count = count;
643 return VK_SUCCESS;
644}
645
Jesse Hall1f91d392015-12-11 16:28:44 -0800646void GetPhysicalDeviceProperties_Bottom(
647 VkPhysicalDevice pdev,
648 VkPhysicalDeviceProperties* properties) {
649 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
650 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700651}
652
Jesse Hall1f91d392015-12-11 16:28:44 -0800653void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
654 VkPhysicalDeviceFeatures* features) {
655 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
656 features);
657}
658
659void GetPhysicalDeviceMemoryProperties_Bottom(
660 VkPhysicalDevice pdev,
661 VkPhysicalDeviceMemoryProperties* properties) {
662 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
663 pdev, properties);
664}
665
666void GetPhysicalDeviceQueueFamilyProperties_Bottom(
667 VkPhysicalDevice pdev,
668 uint32_t* pCount,
669 VkQueueFamilyProperties* properties) {
670 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
671 pdev, pCount, properties);
672}
673
674void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
675 VkFormat format,
676 VkFormatProperties* properties) {
677 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700678 pdev, format, properties);
679}
680
Jesse Hall1f91d392015-12-11 16:28:44 -0800681VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700682 VkPhysicalDevice pdev,
683 VkFormat format,
684 VkImageType type,
685 VkImageTiling tiling,
686 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700687 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700688 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800689 return GetDispatchParent(pdev)
690 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800691 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700692}
693
Jesse Hall1f91d392015-12-11 16:28:44 -0800694void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700695 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800696 VkFormat format,
697 VkImageType type,
698 VkSampleCountFlagBits samples,
699 VkImageUsageFlags usage,
700 VkImageTiling tiling,
701 uint32_t* properties_count,
702 VkSparseImageFormatProperties* properties) {
703 GetDispatchParent(pdev)
704 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
705 pdev, format, type, samples, usage, tiling, properties_count,
706 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700707}
708
Jesse Halle1b12782015-11-30 11:27:32 -0800709VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800710VkResult EnumerateDeviceExtensionProperties_Bottom(
Chia-I Wu0c203242016-03-15 13:44:51 +0800711 VkPhysicalDevice pdev,
712 const char* layer_name,
713 uint32_t* properties_count,
714 VkExtensionProperties* properties) {
715 (void)layer_name;
716
717 Instance& instance = GetDispatchParent(pdev);
718
719 size_t gpu_idx = 0;
720 while (instance.physical_devices[gpu_idx] != pdev)
721 gpu_idx++;
722 const DeviceExtensionSet driver_extensions =
723 instance.physical_device_driver_extensions[gpu_idx];
724
725 // We only support VK_KHR_swapchain if the GPU supports
726 // VK_ANDROID_native_buffer
727 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
728 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
729 uint32_t num_extensions = 0;
730 if (driver_extensions[kANDROID_native_buffer]) {
731 available[num_extensions++] = VkExtensionProperties{
732 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
733 }
734
735 if (!properties || *properties_count > num_extensions)
736 *properties_count = num_extensions;
737 if (properties)
738 std::copy(available, available + *properties_count, properties);
739
740 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700741}
742
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700743// This is a no-op, the Top function returns the aggregate layer property
744// data. This is to keep the dispatch generator happy.
Jesse Halle1b12782015-11-30 11:27:32 -0800745VKAPI_ATTR
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700746VkResult EnumerateDeviceLayerProperties_Bottom(
747 VkPhysicalDevice /*pdev*/,
748 uint32_t* /*properties_count*/,
749 VkLayerProperties* /*properties*/) {
750 return VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800751}
752
753VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800754VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800755 const VkDeviceCreateInfo* create_info,
756 const VkAllocationCallbacks* allocator,
757 VkDevice* device_out) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700758 Instance& instance = GetDispatchParent(gpu);
Chia-I Wu0c203242016-03-15 13:44:51 +0800759
760 // FIXME(jessehall): We don't have good conventions or infrastructure yet to
761 // do better than just using the instance allocator and scope for
762 // everything. See b/26732122.
763 if (true /*!allocator*/)
764 allocator = instance.alloc;
765
766 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
767 alignof(Device),
768 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
769 if (!mem)
770 return VK_ERROR_OUT_OF_HOST_MEMORY;
771 Device* device = new (mem) Device(&instance);
772
Jesse Hallb1471272016-01-17 21:36:58 -0800773 size_t gpu_idx = 0;
774 while (instance.physical_devices[gpu_idx] != gpu)
775 gpu_idx++;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700776
777 VkDeviceCreateInfo driver_create_info = *create_info;
778 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
779 driver_create_info.enabledLayerCount = 0;
780 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800781
782 uint32_t num_driver_extensions = 0;
783 const char** driver_extensions = static_cast<const char**>(
784 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
785 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
786 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800787 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800788 if (id != kDeviceExtensionCount) {
789 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
790 driver_extensions[num_driver_extensions++] = name;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600791 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800792 continue;
793 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700794 // Add the VK_ANDROID_native_buffer extension to the list iff
795 // the VK_KHR_swapchain extension was requested
Jesse Hallae3b70d2016-01-17 22:05:29 -0800796 if (id == kKHR_swapchain &&
797 instance.physical_device_driver_extensions
798 [gpu_idx][kANDROID_native_buffer]) {
799 driver_extensions[num_driver_extensions++] =
800 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600801 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800802 continue;
803 }
Jesse Hallb1471272016-01-17 21:36:58 -0800804 }
Jesse Hallb1471272016-01-17 21:36:58 -0800805 }
806
Jesse Hallb1471272016-01-17 21:36:58 -0800807 driver_create_info.enabledExtensionCount = num_driver_extensions;
808 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Hall04f4f472015-08-16 19:51:04 -0700809 VkDevice drv_device;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700810 VkResult result = instance.drv.dispatch.CreateDevice(
811 gpu, &driver_create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700812 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800813 DestroyDevice(device, VK_NULL_HANDLE);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700814 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700815 }
816
Chia-I Wu0c203242016-03-15 13:44:51 +0800817 if (!driver::SetData(drv_device, device->base)) {
818 DestroyDevice(device, drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700819 return VK_ERROR_INITIALIZATION_FAILED;
820 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700821
Jesse Hall1f91d392015-12-11 16:28:44 -0800822 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
823 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
824 "vkGetDeviceProcAddr"));
Jesse Hall1f91d392015-12-11 16:28:44 -0800825 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700826 return VK_SUCCESS;
827}
828
Jesse Hall1f91d392015-12-11 16:28:44 -0800829void DestroyInstance_Bottom(VkInstance vkinstance,
830 const VkAllocationCallbacks* allocator) {
831 Instance& instance = GetDispatchParent(vkinstance);
832
Chia-I Wu0c203242016-03-15 13:44:51 +0800833 VkAllocationCallbacks local_allocator;
834 if (!allocator) {
835 local_allocator = *instance.alloc;
836 allocator = &local_allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800837 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800838
839 DestroyInstance(&instance, allocator, vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700840}
841
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600842VkResult CreateSwapchainKHR_Disabled(VkDevice,
843 const VkSwapchainCreateInfoKHR*,
844 const VkAllocationCallbacks*,
845 VkSwapchainKHR*) {
846 ALOGE("VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
847
848 return VK_SUCCESS;
849}
850
851void DestroySwapchainKHR_Disabled(VkDevice,
852 VkSwapchainKHR,
853 const VkAllocationCallbacks*) {
854 ALOGE("VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
855}
856
857VkResult GetSwapchainImagesKHR_Disabled(VkDevice,
858 VkSwapchainKHR,
859 uint32_t*,
860 VkImage*) {
861 ALOGE(
862 "VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
863
864 return VK_SUCCESS;
865}
866
867VkResult AcquireNextImageKHR_Disabled(VkDevice,
868 VkSwapchainKHR,
869 uint64_t,
870 VkSemaphore,
871 VkFence,
872 uint32_t*) {
873 ALOGE("VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
874
875 return VK_SUCCESS;
876}
877
878VkResult QueuePresentKHR_Disabled(VkQueue, const VkPresentInfoKHR*) {
879 ALOGE("VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
880
881 return VK_SUCCESS;
882}
883
Jesse Hall1f91d392015-12-11 16:28:44 -0800884PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
885 const char* name) {
886 if (strcmp(name, "vkCreateDevice") == 0) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700887 return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom);
Michael Lentine03c64b02015-08-26 18:27:26 -0500888 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800889
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600890 Device& device = GetDispatchParent(vkdevice);
891 if (!device.enabled_extensions[kKHR_swapchain]) {
892 if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
893 return reinterpret_cast<PFN_vkVoidFunction>(
894 CreateSwapchainKHR_Disabled);
895 }
896 if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
897 return reinterpret_cast<PFN_vkVoidFunction>(
898 DestroySwapchainKHR_Disabled);
899 }
900 if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
901 return reinterpret_cast<PFN_vkVoidFunction>(
902 GetSwapchainImagesKHR_Disabled);
903 }
904 if (strcmp(name, "vkAcquireNextSwapchainImageKHR") == 0) {
905 return reinterpret_cast<PFN_vkVoidFunction>(
906 AcquireNextImageKHR_Disabled);
907 }
908 if (strcmp(name, "vkQueuePresentKHR") == 0) {
909 return reinterpret_cast<PFN_vkVoidFunction>(
910 QueuePresentKHR_Disabled);
911 }
912 }
913
Jesse Hall1f91d392015-12-11 16:28:44 -0800914 // VK_ANDROID_native_buffer should be hidden from applications and layers.
915 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
916 PFN_vkVoidFunction pfn;
917 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
918 strcmp(name, "vkAcquireImageANDROID") == 0 ||
919 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
920 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500921 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800922 if ((pfn = GetLoaderBottomProcAddr(name)))
923 return pfn;
924 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700925}
926
Chia-I Wu0c203242016-03-15 13:44:51 +0800927void DestroyDevice_Bottom(VkDevice vkdevice, const VkAllocationCallbacks*) {
928 DestroyDevice(&GetDispatchParent(vkdevice), vkdevice);
Jesse Hall04f4f472015-08-16 19:51:04 -0700929}
930
Chia-I Wu0c203242016-03-15 13:44:51 +0800931void GetDeviceQueue_Bottom(VkDevice vkdevice,
932 uint32_t family,
933 uint32_t index,
934 VkQueue* queue_out) {
935 const auto& device = GetDispatchParent(vkdevice);
936 const auto& instance = *device.instance;
Jesse Hall04f4f472015-08-16 19:51:04 -0700937
Chia-I Wu0c203242016-03-15 13:44:51 +0800938 instance.drv.dispatch.GetDeviceQueue(vkdevice, family, index, queue_out);
939 driver::SetData(*queue_out, device.base);
Jesse Hall04f4f472015-08-16 19:51:04 -0700940}
941
Chia-I Wu0c203242016-03-15 13:44:51 +0800942VkResult AllocateCommandBuffers_Bottom(
Jesse Hall1f91d392015-12-11 16:28:44 -0800943 VkDevice vkdevice,
944 const VkCommandBufferAllocateInfo* alloc_info,
945 VkCommandBuffer* cmdbufs) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800946 const auto& device = GetDispatchParent(vkdevice);
947 const auto& instance = *device.instance;
Jesse Hallc7a6eb52015-08-31 12:52:03 -0700948
Chia-I Wu0c203242016-03-15 13:44:51 +0800949 VkResult result = instance.drv.dispatch.AllocateCommandBuffers(
950 vkdevice, alloc_info, cmdbufs);
951 if (result == VK_SUCCESS) {
952 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++)
953 driver::SetData(cmdbufs[i], device.base);
954 }
955
956 return result;
Jesse Hall04f4f472015-08-16 19:51:04 -0700957}
958
Jesse Hall1f91d392015-12-11 16:28:44 -0800959// -----------------------------------------------------------------------------
960
961const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
962 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800963}
964
Jesse Hall1f91d392015-12-11 16:28:44 -0800965const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
966 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800967}
968
Jesse Hall715b86a2016-01-16 16:34:29 -0800969VkInstance GetDriverInstance(VkInstance instance) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800970 return instance;
Jesse Hall715b86a2016-01-16 16:34:29 -0800971}
972
973const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
974 return GetDispatchParent(instance).drv.dispatch;
975}
976
Jesse Hall1f91d392015-12-11 16:28:44 -0800977const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
978 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700979}
980
Jesse Hall1f91d392015-12-11 16:28:44 -0800981const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
982 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700983}
984
Jesse Hall715b86a2016-01-16 16:34:29 -0800985DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
986 return GetDispatchParent(instance).debug_report_callbacks;
987}
988
Chia-I Wu0c203242016-03-15 13:44:51 +0800989namespace driver {
990
991bool Debuggable() {
992 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
993}
994
995bool OpenHAL() {
996 if (!g_hwdevice)
997 LoadVulkanHAL();
998
999 return (g_hwdevice != nullptr);
1000}
1001
1002const VkAllocationCallbacks& GetDefaultAllocator() {
1003 return kDefaultAllocCallbacks;
1004}
1005
1006PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
1007 return GetInstanceProcAddr_Bottom(instance, pName);
1008}
1009
1010PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
1011 return GetDeviceProcAddr_Bottom(device, pName);
1012}
1013
1014VkResult EnumerateInstanceExtensionProperties(
1015 const char* pLayerName,
1016 uint32_t* pPropertyCount,
1017 VkExtensionProperties* pProperties) {
1018 (void)pLayerName;
1019
1020 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
1021 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
1022 uint32_t num_extensions = 0;
1023
1024 available[num_extensions++] = VkExtensionProperties{
1025 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
1026 available[num_extensions++] =
1027 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
1028 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
1029 if (g_driver_instance_extensions[kEXT_debug_report]) {
1030 available[num_extensions++] =
1031 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
1032 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
1033 }
1034
1035 if (!pProperties || *pPropertyCount > num_extensions)
1036 *pPropertyCount = num_extensions;
1037 if (pProperties)
1038 std::copy(available, available + *pPropertyCount, pProperties);
1039
1040 return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
1041}
1042
1043} // namespace driver
1044
Jesse Hall04f4f472015-08-16 19:51:04 -07001045} // namespace vulkan