blob: 2c63b0643cc3a3a517a2d1b842a23adc0e6f625d [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
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800181bool LoadVulkanHAL() {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800182 VkResult vkresult;
183 uint32_t count;
184 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
185 nullptr, &count, nullptr)) != VK_SUCCESS) {
186 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
187 vkresult);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800188 return false;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800189 }
190 VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
191 alloca(count * sizeof(VkExtensionProperties)));
192 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
193 nullptr, &count, extensions)) != VK_SUCCESS) {
194 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
195 vkresult);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800196 return false;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800197 }
198 ALOGV_IF(count > 0, "Driver-supported instance extensions:");
199 for (uint32_t i = 0; i < count; i++) {
200 ALOGV(" %s (v%u)", extensions[i].extensionName,
201 extensions[i].specVersion);
202 InstanceExtension id =
203 InstanceExtensionFromName(extensions[i].extensionName);
204 if (id != kInstanceExtensionCount)
205 g_driver_instance_extensions.set(id);
206 }
207 // Ignore driver attempts to support loader extensions
208 g_driver_instance_extensions.reset(kKHR_surface);
209 g_driver_instance_extensions.reset(kKHR_android_surface);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800210
211 return true;
Jesse Hall80523e22016-01-06 16:47:54 -0800212}
213
Jesse Hall1f91d392015-12-11 16:28:44 -0800214// -----------------------------------------------------------------------------
215
216struct Instance {
217 Instance(const VkAllocationCallbacks* alloc_callbacks)
Chia-I Wu0c203242016-03-15 13:44:51 +0800218 : base{{}, *alloc_callbacks},
219 alloc(&base.allocator),
220 num_physical_devices(0) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800221 memset(physical_devices, 0, sizeof(physical_devices));
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600222 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800223 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
Jesse Hall1f91d392015-12-11 16:28:44 -0800224 }
225
Jesse Hall80523e22016-01-06 16:47:54 -0800226 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800227
Chia-I Wu0c203242016-03-15 13:44:51 +0800228 driver::InstanceData base;
Jesse Hall1f91d392015-12-11 16:28:44 -0800229
Jesse Hall1f91d392015-12-11 16:28:44 -0800230 const VkAllocationCallbacks* alloc;
231 uint32_t num_physical_devices;
Courtney Goeltzenleuchter84cd4c22016-03-16 12:20:13 -0600232 VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800233 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800234 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800235
Jesse Hall715b86a2016-01-16 16:34:29 -0800236 DebugReportCallbackList debug_report_callbacks;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600237 InstanceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800238
239 struct {
Jesse Hall1f91d392015-12-11 16:28:44 -0800240 DriverDispatchTable dispatch;
Jesse Hall1f91d392015-12-11 16:28:44 -0800241 } drv; // may eventually be an array
242};
243
244struct Device {
245 Device(Instance* instance_)
Chia-I Wu0c203242016-03-15 13:44:51 +0800246 : base{{}, *instance_->alloc}, instance(instance_) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600247 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800248 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800249
250 driver::DeviceData base;
251
Jesse Hall1f91d392015-12-11 16:28:44 -0800252 Instance* instance;
253 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600254 DeviceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800255};
256
257template <typename THandle>
258struct HandleTraits {};
259template <>
260struct HandleTraits<VkInstance> {
261 typedef Instance LoaderObjectType;
262};
263template <>
264struct HandleTraits<VkPhysicalDevice> {
265 typedef Instance LoaderObjectType;
266};
267template <>
268struct HandleTraits<VkDevice> {
269 typedef Device LoaderObjectType;
270};
271template <>
272struct HandleTraits<VkQueue> {
273 typedef Device LoaderObjectType;
274};
275template <>
276struct HandleTraits<VkCommandBuffer> {
277 typedef Device LoaderObjectType;
278};
279
280template <typename THandle>
281typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
282 THandle handle) {
283 // TODO(jessehall): Make Instance and Device POD types (by removing the
284 // non-default constructors), so that offsetof is actually legal to use.
285 // The specific case we're using here is safe in gcc/clang (and probably
286 // most other C++ compilers), but isn't guaranteed by C++.
287 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
288#pragma clang diagnostic push
289#pragma clang diagnostic ignored "-Winvalid-offsetof"
Chia-I Wu0c203242016-03-15 13:44:51 +0800290 const size_t kBaseOffset = offsetof(ObjectType, base);
Jesse Hall1f91d392015-12-11 16:28:44 -0800291#pragma clang diagnostic pop
292
Chia-I Wu0c203242016-03-15 13:44:51 +0800293 const auto& base = driver::GetData(handle);
294 uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base);
295 uintptr_t object_addr = base_addr - kBaseOffset;
Jesse Hall1f91d392015-12-11 16:28:44 -0800296 return *reinterpret_cast<ObjectType*>(object_addr);
297}
298
299// -----------------------------------------------------------------------------
300
Chia-I Wu0c203242016-03-15 13:44:51 +0800301void DestroyDevice(Device* device, VkDevice vkdevice) {
302 const auto& instance = *device->instance;
303
304 if (vkdevice != VK_NULL_HANDLE)
305 instance.drv.dispatch.DestroyDevice(vkdevice, instance.alloc);
306
Jesse Hall04f4f472015-08-16 19:51:04 -0700307 device->~Device();
Chia-I Wu0c203242016-03-15 13:44:51 +0800308 instance.alloc->pfnFree(instance.alloc->pUserData, device);
Michael Lentine03c64b02015-08-26 18:27:26 -0500309}
310
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700311/*
312 * This function will return the pNext pointer of any
313 * CreateInfo extensions that are not loader extensions.
314 * This is used to skip past the loader extensions prepended
315 * to the list during CreateInstance and CreateDevice.
316 */
317void* StripCreateExtensions(const void* pNext) {
318 VkLayerInstanceCreateInfo* create_info =
319 const_cast<VkLayerInstanceCreateInfo*>(
320 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
321
322 while (
323 create_info &&
324 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
325 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
326 create_info = const_cast<VkLayerInstanceCreateInfo*>(
327 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
328 }
329
330 return create_info;
331}
332
Jesse Hallfee71432016-03-05 22:27:02 -0800333// Clean up and deallocate an Instance; called from both the failure paths in
334// CreateInstance_Top as well as from DestroyInstance_Top. This function does
335// not call down the dispatch chain; that should be done before calling this
336// function, iff the lower vkCreateInstance call has been made and returned
337// successfully.
338void DestroyInstance(Instance* instance,
Chia-I Wu0c203242016-03-15 13:44:51 +0800339 const VkAllocationCallbacks* allocator,
340 VkInstance vkinstance) {
341 if (vkinstance != VK_NULL_HANDLE && instance->drv.dispatch.DestroyInstance)
342 instance->drv.dispatch.DestroyInstance(vkinstance, allocator);
343
Jesse Hallfee71432016-03-05 22:27:02 -0800344 instance->~Instance();
345 allocator->pfnFree(allocator->pUserData, instance);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700346}
347
Jesse Hall1f91d392015-12-11 16:28:44 -0800348} // anonymous namespace
349
350namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500351
Jesse Hall04f4f472015-08-16 19:51:04 -0700352// -----------------------------------------------------------------------------
353// "Bottom" functions. These are called at the end of the instance dispatch
354// chain.
355
Jesse Hall1f91d392015-12-11 16:28:44 -0800356VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
357 const VkAllocationCallbacks* allocator,
358 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700359 VkResult result;
360
Chia-I Wu0c203242016-03-15 13:44:51 +0800361 if (!allocator)
362 allocator = &kDefaultAllocCallbacks;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700363
Chia-I Wu0c203242016-03-15 13:44:51 +0800364 void* instance_mem = allocator->pfnAllocation(
365 allocator->pUserData, sizeof(Instance), alignof(Instance),
366 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
367 if (!instance_mem)
368 return VK_ERROR_OUT_OF_HOST_MEMORY;
369 Instance& instance = *new (instance_mem) Instance(allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700370
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800371 // Check that all enabled extensions are supported
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800372 uint32_t num_driver_extensions = 0;
373 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
374 const char* name = create_info->ppEnabledExtensionNames[i];
375 InstanceExtension id = InstanceExtensionFromName(name);
376 if (id != kInstanceExtensionCount) {
377 if (g_driver_instance_extensions[id]) {
378 num_driver_extensions++;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600379 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800380 continue;
381 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700382 if (id == kKHR_surface || id == kKHR_android_surface) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600383 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800384 continue;
385 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700386 // The loader natively supports debug report.
387 if (id == kEXT_debug_report) {
388 continue;
389 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800390 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800391 }
392
Jesse Halla7ac76d2016-01-08 22:29:42 -0800393 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700394 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800395 driver_create_info.enabledLayerCount = 0;
396 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800397 driver_create_info.enabledExtensionCount = 0;
398 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800399 if (num_driver_extensions > 0) {
400 const char** names = static_cast<const char**>(
401 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800402 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800403 const char* name = create_info->ppEnabledExtensionNames[i];
404 InstanceExtension id = InstanceExtensionFromName(name);
405 if (id != kInstanceExtensionCount) {
406 if (g_driver_instance_extensions[id]) {
407 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800408 continue;
409 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800410 }
411 }
412 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800413 ALOG_ASSERT(
414 driver_create_info.enabledExtensionCount == num_driver_extensions,
415 "counted enabled driver instance extensions twice and got "
416 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800417 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800418
Chia-I Wu0c203242016-03-15 13:44:51 +0800419 VkInstance drv_instance;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800420 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Chia-I Wu0c203242016-03-15 13:44:51 +0800421 &drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700422 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800423 DestroyInstance(&instance, allocator, VK_NULL_HANDLE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700424 return result;
425 }
426
Chia-I Wu0c203242016-03-15 13:44:51 +0800427 if (!driver::SetData(drv_instance, instance.base)) {
428 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700429 return VK_ERROR_INITIALIZATION_FAILED;
430 }
431
Chia-I Wu0c203242016-03-15 13:44:51 +0800432 if (!LoadDriverDispatchTable(drv_instance, g_hwdevice->GetInstanceProcAddr,
433 instance.enabled_extensions,
434 instance.drv.dispatch)) {
435 DestroyInstance(&instance, allocator, drv_instance);
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700436 return VK_ERROR_INITIALIZATION_FAILED;
437 }
438
Jesse Hall04f4f472015-08-16 19:51:04 -0700439 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800440 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800441 drv_instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700442 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800443 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700444 return VK_ERROR_INITIALIZATION_FAILED;
445 }
446 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800447 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800448 drv_instance, &num_physical_devices, instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700449 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800450 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700451 return VK_ERROR_INITIALIZATION_FAILED;
452 }
Jesse Hallb1471272016-01-17 21:36:58 -0800453
454 Vector<VkExtensionProperties> extensions(
455 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700456 for (uint32_t i = 0; i < num_physical_devices; i++) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800457 if (!driver::SetData(instance.physical_devices[i], instance.base)) {
458 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700459 return VK_ERROR_INITIALIZATION_FAILED;
460 }
Jesse Hallb1471272016-01-17 21:36:58 -0800461
462 uint32_t count;
463 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
464 instance.physical_devices[i], nullptr, &count, nullptr)) !=
465 VK_SUCCESS) {
466 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
467 result);
468 continue;
469 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800470 try {
471 extensions.resize(count);
472 } catch (std::bad_alloc&) {
473 ALOGE("instance creation failed: out of memory");
Chia-I Wu0c203242016-03-15 13:44:51 +0800474 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall26cecff2016-01-21 19:52:25 -0800475 return VK_ERROR_OUT_OF_HOST_MEMORY;
476 }
Jesse Hallb1471272016-01-17 21:36:58 -0800477 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
478 instance.physical_devices[i], nullptr, &count,
479 extensions.data())) != VK_SUCCESS) {
480 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
481 result);
482 continue;
483 }
484 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
485 for (const auto& extension : extensions) {
486 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
487 DeviceExtension id =
488 DeviceExtensionFromName(extension.extensionName);
489 if (id == kDeviceExtensionCount) {
490 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
491 extension.extensionName);
492 } else {
493 instance.physical_device_driver_extensions[i].set(id);
494 }
495 }
496 // Ignore driver attempts to support loader extensions
497 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700498 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800499 instance.num_physical_devices = num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800500
Chia-I Wu0c203242016-03-15 13:44:51 +0800501 *vkinstance = drv_instance;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700502
Jesse Hall04f4f472015-08-16 19:51:04 -0700503 return VK_SUCCESS;
504}
505
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600506VkResult CreateAndroidSurfaceKHR_Disabled(VkInstance,
507 const VkAndroidSurfaceCreateInfoKHR*,
508 const VkAllocationCallbacks*,
509 VkSurfaceKHR*) {
510 ALOGE(
511 "VK_KHR_android_surface not enabled. vkCreateAndroidSurfaceKHR not "
512 "executed.");
513
514 return VK_SUCCESS;
515}
516
517void DestroySurfaceKHR_Disabled(VkInstance,
518 VkSurfaceKHR,
519 const VkAllocationCallbacks*) {
520 ALOGE("VK_KHR_surface not enabled. vkDestroySurfaceKHR not executed.");
521}
522
523VkResult GetPhysicalDeviceSurfaceSupportKHR_Disabled(VkPhysicalDevice,
524 uint32_t,
525 VkSurfaceKHR,
526 VkBool32*) {
527 ALOGE(
528 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceSupportKHR not "
529 "executed.");
530
531 return VK_SUCCESS;
532}
533
534VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled(
535 VkPhysicalDevice,
536 VkSurfaceKHR,
537 VkSurfaceCapabilitiesKHR*) {
538 ALOGE(
539 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceapabilitiesKHR "
540 "not executed.");
541
542 return VK_SUCCESS;
543}
544
545VkResult GetPhysicalDeviceSurfaceFormatsKHR_Disabled(VkPhysicalDevice,
546 VkSurfaceKHR,
547 uint32_t*,
548 VkSurfaceFormatKHR*) {
549 ALOGE(
550 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceFormatsKHR not "
551 "executed.");
552
553 return VK_SUCCESS;
554}
555
556VkResult GetPhysicalDeviceSurfacePresentModesKHR_Disabled(VkPhysicalDevice,
557 VkSurfaceKHR,
558 uint32_t*,
559 VkPresentModeKHR*) {
560 ALOGE(
561 "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfacePresentModesKHR "
562 "not executed.");
563
564 return VK_SUCCESS;
565}
566
567PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance vkinstance,
568 const char* name) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800569 PFN_vkVoidFunction pfn;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600570
571 if (vkinstance) {
572 Instance& instance = GetDispatchParent(vkinstance);
573 if (!instance.enabled_extensions[kKHR_android_surface]) {
574 // KHR_android_surface is not enabled, use error stubs instead
575 if (strcmp(name, "vkCreateAndroidSurfaceKHR") == 0) {
576 return reinterpret_cast<PFN_vkVoidFunction>(
577 CreateAndroidSurfaceKHR_Disabled);
578 }
579 }
580 if (!instance.enabled_extensions[kKHR_surface]) {
581 // KHR_surface is not enabled, use error stubs instead
582 if (strcmp(name, "vkDestroySurfaceKHR") == 0) {
583 return reinterpret_cast<PFN_vkVoidFunction>(
584 DestroySurfaceKHR_Disabled);
585 }
586 if (strcmp(name, "vkGetPhysicalDeviceSurfaceSupportKHR") == 0) {
587 return reinterpret_cast<PFN_vkVoidFunction>(
588 GetPhysicalDeviceSurfaceSupportKHR_Disabled);
589 }
590 if (strcmp(name, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR") ==
591 0) {
592 return reinterpret_cast<PFN_vkVoidFunction>(
593 GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled);
594 }
595 if (strcmp(name, "vkGetPhysicalDeviceSurfaceFormatsKHR") == 0) {
596 return reinterpret_cast<PFN_vkVoidFunction>(
597 GetPhysicalDeviceSurfaceFormatsKHR_Disabled);
598 }
599 if (strcmp(name, "vkGetPhysicalDeviceSurfacePresentModesKHR") ==
600 0) {
601 return reinterpret_cast<PFN_vkVoidFunction>(
602 GetPhysicalDeviceSurfacePresentModesKHR_Disabled);
603 }
604 }
605 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800606 if ((pfn = GetLoaderBottomProcAddr(name)))
607 return pfn;
Chia-I Wu0c203242016-03-15 13:44:51 +0800608 return g_hwdevice->GetInstanceProcAddr(vkinstance, name);
Jesse Hall1f91d392015-12-11 16:28:44 -0800609}
610
611VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
612 uint32_t* pdev_count,
613 VkPhysicalDevice* pdevs) {
614 Instance& instance = GetDispatchParent(vkinstance);
615 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700616 if (pdevs) {
617 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800618 std::copy(instance.physical_devices, instance.physical_devices + count,
619 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700620 }
621 *pdev_count = count;
622 return VK_SUCCESS;
623}
624
Jesse Hall1f91d392015-12-11 16:28:44 -0800625void GetPhysicalDeviceProperties_Bottom(
626 VkPhysicalDevice pdev,
627 VkPhysicalDeviceProperties* properties) {
628 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
629 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700630}
631
Jesse Hall1f91d392015-12-11 16:28:44 -0800632void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
633 VkPhysicalDeviceFeatures* features) {
634 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
635 features);
636}
637
638void GetPhysicalDeviceMemoryProperties_Bottom(
639 VkPhysicalDevice pdev,
640 VkPhysicalDeviceMemoryProperties* properties) {
641 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
642 pdev, properties);
643}
644
645void GetPhysicalDeviceQueueFamilyProperties_Bottom(
646 VkPhysicalDevice pdev,
647 uint32_t* pCount,
648 VkQueueFamilyProperties* properties) {
649 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
650 pdev, pCount, properties);
651}
652
653void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
654 VkFormat format,
655 VkFormatProperties* properties) {
656 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700657 pdev, format, properties);
658}
659
Jesse Hall1f91d392015-12-11 16:28:44 -0800660VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700661 VkPhysicalDevice pdev,
662 VkFormat format,
663 VkImageType type,
664 VkImageTiling tiling,
665 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700666 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700667 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800668 return GetDispatchParent(pdev)
669 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800670 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700671}
672
Jesse Hall1f91d392015-12-11 16:28:44 -0800673void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700674 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800675 VkFormat format,
676 VkImageType type,
677 VkSampleCountFlagBits samples,
678 VkImageUsageFlags usage,
679 VkImageTiling tiling,
680 uint32_t* properties_count,
681 VkSparseImageFormatProperties* properties) {
682 GetDispatchParent(pdev)
683 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
684 pdev, format, type, samples, usage, tiling, properties_count,
685 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700686}
687
Jesse Halle1b12782015-11-30 11:27:32 -0800688VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800689VkResult EnumerateDeviceExtensionProperties_Bottom(
Chia-I Wu0c203242016-03-15 13:44:51 +0800690 VkPhysicalDevice pdev,
691 const char* layer_name,
692 uint32_t* properties_count,
693 VkExtensionProperties* properties) {
694 (void)layer_name;
695
696 Instance& instance = GetDispatchParent(pdev);
697
698 size_t gpu_idx = 0;
699 while (instance.physical_devices[gpu_idx] != pdev)
700 gpu_idx++;
701 const DeviceExtensionSet driver_extensions =
702 instance.physical_device_driver_extensions[gpu_idx];
703
704 // We only support VK_KHR_swapchain if the GPU supports
705 // VK_ANDROID_native_buffer
706 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
707 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
708 uint32_t num_extensions = 0;
709 if (driver_extensions[kANDROID_native_buffer]) {
710 available[num_extensions++] = VkExtensionProperties{
711 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
712 }
713
714 if (!properties || *properties_count > num_extensions)
715 *properties_count = num_extensions;
716 if (properties)
717 std::copy(available, available + *properties_count, properties);
718
719 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700720}
721
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700722// This is a no-op, the Top function returns the aggregate layer property
723// data. This is to keep the dispatch generator happy.
Jesse Halle1b12782015-11-30 11:27:32 -0800724VKAPI_ATTR
Courtney Goeltzenleuchter1cc0d372016-02-05 17:10:59 -0700725VkResult EnumerateDeviceLayerProperties_Bottom(
726 VkPhysicalDevice /*pdev*/,
727 uint32_t* /*properties_count*/,
728 VkLayerProperties* /*properties*/) {
729 return VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800730}
731
732VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800733VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800734 const VkDeviceCreateInfo* create_info,
735 const VkAllocationCallbacks* allocator,
736 VkDevice* device_out) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700737 Instance& instance = GetDispatchParent(gpu);
Chia-I Wu0c203242016-03-15 13:44:51 +0800738
739 // FIXME(jessehall): We don't have good conventions or infrastructure yet to
740 // do better than just using the instance allocator and scope for
741 // everything. See b/26732122.
742 if (true /*!allocator*/)
743 allocator = instance.alloc;
744
745 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
746 alignof(Device),
747 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
748 if (!mem)
749 return VK_ERROR_OUT_OF_HOST_MEMORY;
750 Device* device = new (mem) Device(&instance);
751
Jesse Hallb1471272016-01-17 21:36:58 -0800752 size_t gpu_idx = 0;
753 while (instance.physical_devices[gpu_idx] != gpu)
754 gpu_idx++;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700755
756 VkDeviceCreateInfo driver_create_info = *create_info;
757 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
758 driver_create_info.enabledLayerCount = 0;
759 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800760
761 uint32_t num_driver_extensions = 0;
762 const char** driver_extensions = static_cast<const char**>(
763 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
764 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
765 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800766 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800767 if (id != kDeviceExtensionCount) {
768 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
769 driver_extensions[num_driver_extensions++] = name;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600770 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800771 continue;
772 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700773 // Add the VK_ANDROID_native_buffer extension to the list iff
774 // the VK_KHR_swapchain extension was requested
Jesse Hallae3b70d2016-01-17 22:05:29 -0800775 if (id == kKHR_swapchain &&
776 instance.physical_device_driver_extensions
777 [gpu_idx][kANDROID_native_buffer]) {
778 driver_extensions[num_driver_extensions++] =
779 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600780 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800781 continue;
782 }
Jesse Hallb1471272016-01-17 21:36:58 -0800783 }
Jesse Hallb1471272016-01-17 21:36:58 -0800784 }
785
Jesse Hallb1471272016-01-17 21:36:58 -0800786 driver_create_info.enabledExtensionCount = num_driver_extensions;
787 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Hall04f4f472015-08-16 19:51:04 -0700788 VkDevice drv_device;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700789 VkResult result = instance.drv.dispatch.CreateDevice(
790 gpu, &driver_create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700791 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800792 DestroyDevice(device, VK_NULL_HANDLE);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700793 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700794 }
795
Chia-I Wu0c203242016-03-15 13:44:51 +0800796 if (!driver::SetData(drv_device, device->base)) {
797 DestroyDevice(device, drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700798 return VK_ERROR_INITIALIZATION_FAILED;
799 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700800
Jesse Hall1f91d392015-12-11 16:28:44 -0800801 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
802 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
803 "vkGetDeviceProcAddr"));
Jesse Hall1f91d392015-12-11 16:28:44 -0800804 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700805 return VK_SUCCESS;
806}
807
Jesse Hall1f91d392015-12-11 16:28:44 -0800808void DestroyInstance_Bottom(VkInstance vkinstance,
809 const VkAllocationCallbacks* allocator) {
810 Instance& instance = GetDispatchParent(vkinstance);
811
Chia-I Wu0c203242016-03-15 13:44:51 +0800812 VkAllocationCallbacks local_allocator;
813 if (!allocator) {
814 local_allocator = *instance.alloc;
815 allocator = &local_allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800816 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800817
818 DestroyInstance(&instance, allocator, vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700819}
820
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600821VkResult CreateSwapchainKHR_Disabled(VkDevice,
822 const VkSwapchainCreateInfoKHR*,
823 const VkAllocationCallbacks*,
824 VkSwapchainKHR*) {
825 ALOGE("VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
826
827 return VK_SUCCESS;
828}
829
830void DestroySwapchainKHR_Disabled(VkDevice,
831 VkSwapchainKHR,
832 const VkAllocationCallbacks*) {
833 ALOGE("VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
834}
835
836VkResult GetSwapchainImagesKHR_Disabled(VkDevice,
837 VkSwapchainKHR,
838 uint32_t*,
839 VkImage*) {
840 ALOGE(
841 "VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
842
843 return VK_SUCCESS;
844}
845
846VkResult AcquireNextImageKHR_Disabled(VkDevice,
847 VkSwapchainKHR,
848 uint64_t,
849 VkSemaphore,
850 VkFence,
851 uint32_t*) {
852 ALOGE("VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
853
854 return VK_SUCCESS;
855}
856
857VkResult QueuePresentKHR_Disabled(VkQueue, const VkPresentInfoKHR*) {
858 ALOGE("VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
859
860 return VK_SUCCESS;
861}
862
Jesse Hall1f91d392015-12-11 16:28:44 -0800863PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
864 const char* name) {
865 if (strcmp(name, "vkCreateDevice") == 0) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700866 return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom);
Michael Lentine03c64b02015-08-26 18:27:26 -0500867 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800868
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600869 Device& device = GetDispatchParent(vkdevice);
870 if (!device.enabled_extensions[kKHR_swapchain]) {
871 if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
872 return reinterpret_cast<PFN_vkVoidFunction>(
873 CreateSwapchainKHR_Disabled);
874 }
875 if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
876 return reinterpret_cast<PFN_vkVoidFunction>(
877 DestroySwapchainKHR_Disabled);
878 }
879 if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
880 return reinterpret_cast<PFN_vkVoidFunction>(
881 GetSwapchainImagesKHR_Disabled);
882 }
883 if (strcmp(name, "vkAcquireNextSwapchainImageKHR") == 0) {
884 return reinterpret_cast<PFN_vkVoidFunction>(
885 AcquireNextImageKHR_Disabled);
886 }
887 if (strcmp(name, "vkQueuePresentKHR") == 0) {
888 return reinterpret_cast<PFN_vkVoidFunction>(
889 QueuePresentKHR_Disabled);
890 }
891 }
892
Jesse Hall1f91d392015-12-11 16:28:44 -0800893 // VK_ANDROID_native_buffer should be hidden from applications and layers.
894 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
895 PFN_vkVoidFunction pfn;
896 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
897 strcmp(name, "vkAcquireImageANDROID") == 0 ||
898 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
899 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500900 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800901 if ((pfn = GetLoaderBottomProcAddr(name)))
902 return pfn;
903 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700904}
905
Chia-I Wu0c203242016-03-15 13:44:51 +0800906void DestroyDevice_Bottom(VkDevice vkdevice, const VkAllocationCallbacks*) {
907 DestroyDevice(&GetDispatchParent(vkdevice), vkdevice);
Jesse Hall04f4f472015-08-16 19:51:04 -0700908}
909
Chia-I Wu0c203242016-03-15 13:44:51 +0800910void GetDeviceQueue_Bottom(VkDevice vkdevice,
911 uint32_t family,
912 uint32_t index,
913 VkQueue* queue_out) {
914 const auto& device = GetDispatchParent(vkdevice);
915 const auto& instance = *device.instance;
Jesse Hall04f4f472015-08-16 19:51:04 -0700916
Chia-I Wu0c203242016-03-15 13:44:51 +0800917 instance.drv.dispatch.GetDeviceQueue(vkdevice, family, index, queue_out);
918 driver::SetData(*queue_out, device.base);
Jesse Hall04f4f472015-08-16 19:51:04 -0700919}
920
Chia-I Wu0c203242016-03-15 13:44:51 +0800921VkResult AllocateCommandBuffers_Bottom(
Jesse Hall1f91d392015-12-11 16:28:44 -0800922 VkDevice vkdevice,
923 const VkCommandBufferAllocateInfo* alloc_info,
924 VkCommandBuffer* cmdbufs) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800925 const auto& device = GetDispatchParent(vkdevice);
926 const auto& instance = *device.instance;
Jesse Hallc7a6eb52015-08-31 12:52:03 -0700927
Chia-I Wu0c203242016-03-15 13:44:51 +0800928 VkResult result = instance.drv.dispatch.AllocateCommandBuffers(
929 vkdevice, alloc_info, cmdbufs);
930 if (result == VK_SUCCESS) {
931 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++)
932 driver::SetData(cmdbufs[i], device.base);
933 }
934
935 return result;
Jesse Hall04f4f472015-08-16 19:51:04 -0700936}
937
Jesse Hall1f91d392015-12-11 16:28:44 -0800938// -----------------------------------------------------------------------------
939
940const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
941 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800942}
943
Jesse Hall1f91d392015-12-11 16:28:44 -0800944const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
945 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800946}
947
Jesse Hall715b86a2016-01-16 16:34:29 -0800948VkInstance GetDriverInstance(VkInstance instance) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800949 return instance;
Jesse Hall715b86a2016-01-16 16:34:29 -0800950}
951
952const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
953 return GetDispatchParent(instance).drv.dispatch;
954}
955
Jesse Hall1f91d392015-12-11 16:28:44 -0800956const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
957 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700958}
959
Jesse Hall1f91d392015-12-11 16:28:44 -0800960const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
961 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700962}
963
Jesse Hall715b86a2016-01-16 16:34:29 -0800964DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
965 return GetDispatchParent(instance).debug_report_callbacks;
966}
967
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800968bool InitLoader(hwvulkan_device_t* dev) {
969 if (!g_hwdevice) {
970 g_hwdevice = dev;
971 if (!LoadVulkanHAL())
972 g_hwdevice = nullptr;
973 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800974
975 return (g_hwdevice != nullptr);
976}
977
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800978namespace driver {
979
Chia-I Wu0c203242016-03-15 13:44:51 +0800980const VkAllocationCallbacks& GetDefaultAllocator() {
981 return kDefaultAllocCallbacks;
982}
983
984PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
985 return GetInstanceProcAddr_Bottom(instance, pName);
986}
987
988PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
989 return GetDeviceProcAddr_Bottom(device, pName);
990}
991
992VkResult EnumerateInstanceExtensionProperties(
993 const char* pLayerName,
994 uint32_t* pPropertyCount,
995 VkExtensionProperties* pProperties) {
996 (void)pLayerName;
997
998 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
999 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
1000 uint32_t num_extensions = 0;
1001
1002 available[num_extensions++] = VkExtensionProperties{
1003 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
1004 available[num_extensions++] =
1005 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
1006 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
1007 if (g_driver_instance_extensions[kEXT_debug_report]) {
1008 available[num_extensions++] =
1009 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
1010 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
1011 }
1012
1013 if (!pProperties || *pPropertyCount > num_extensions)
1014 *pPropertyCount = num_extensions;
1015 if (pProperties)
1016 std::copy(available, available + *pPropertyCount, pProperties);
1017
1018 return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
1019}
1020
1021} // namespace driver
1022
Jesse Hall04f4f472015-08-16 19:51:04 -07001023} // namespace vulkan