blob: 1212f96dd4e282431de73888e5043c1dadec754e [file] [log] [blame]
Jesse Halld02edcb2015-09-08 07:44:48 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jesse Hall04f4f472015-08-16 19:51:04 -070017// module header
18#include "loader.h"
Chia-I Wu0c203242016-03-15 13:44:51 +080019#include "driver.h"
Jesse Hall04f4f472015-08-16 19:51:04 -070020// standard C headers
Michael Lentine03c64b02015-08-26 18:27:26 -050021#include <dirent.h>
22#include <dlfcn.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070023#include <inttypes.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070024#include <pthread.h>
Jesse Hall03b6fe12015-11-24 12:44:21 -080025#include <stdlib.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070026#include <string.h>
Jesse Hall21597662015-12-18 13:48:24 -080027#include <sys/prctl.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070028// standard C++ headers
29#include <algorithm>
30#include <mutex>
Michael Lentine03c64b02015-08-26 18:27:26 -050031#include <sstream>
32#include <string>
33#include <unordered_map>
34#include <vector>
Jesse Hall04f4f472015-08-16 19:51:04 -070035// platform/library headers
Michael Lentine03c64b02015-08-26 18:27:26 -050036#include <cutils/properties.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070037#include <hardware/hwvulkan.h>
38#include <log/log.h>
Michael Lentine1c69b9e2015-09-14 13:26:59 -050039#include <vulkan/vulkan_loader_data.h>
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -070040#include <vulkan/vk_layer_interface.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070041
42using namespace vulkan;
43
44static const uint32_t kMaxPhysicalDevices = 4;
45
Michael Lentine03c64b02015-08-26 18:27:26 -050046namespace {
47
Jesse Hall1f91d392015-12-11 16:28:44 -080048// ----------------------------------------------------------------------------
Michael Lentine03c64b02015-08-26 18:27:26 -050049
Jesse Hall3fbc8562015-11-29 22:10:52 -080050// Standard-library allocator that delegates to VkAllocationCallbacks.
Jesse Hall03b6fe12015-11-24 12:44:21 -080051//
52// TODO(jessehall): This class currently always uses
Jesse Hall3fbc8562015-11-29 22:10:52 -080053// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
Jesse Hall03b6fe12015-11-24 12:44:21 -080054// parameter or a constructor parameter. The former would help catch bugs
55// where we use the wrong scope, e.g. adding a command-scope string to an
56// instance-scope vector. But that might also be pretty annoying to deal with.
Michael Lentine03c64b02015-08-26 18:27:26 -050057template <class T>
58class CallbackAllocator {
59 public:
60 typedef T value_type;
61
Jesse Hall3fbc8562015-11-29 22:10:52 -080062 CallbackAllocator(const VkAllocationCallbacks* alloc_input)
Michael Lentine03c64b02015-08-26 18:27:26 -050063 : alloc(alloc_input) {}
64
65 template <class T2>
66 CallbackAllocator(const CallbackAllocator<T2>& other)
67 : alloc(other.alloc) {}
68
69 T* allocate(std::size_t n) {
Jesse Hall3fbc8562015-11-29 22:10:52 -080070 void* mem =
71 alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
72 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall26cecff2016-01-21 19:52:25 -080073 if (!mem)
74 throw std::bad_alloc();
Michael Lentine03c64b02015-08-26 18:27:26 -050075 return static_cast<T*>(mem);
76 }
77
Jesse Hall26cecff2016-01-21 19:52:25 -080078 void deallocate(T* array, std::size_t /*n*/) noexcept {
Michael Lentine03c64b02015-08-26 18:27:26 -050079 alloc->pfnFree(alloc->pUserData, array);
80 }
81
Jesse Hall3fbc8562015-11-29 22:10:52 -080082 const VkAllocationCallbacks* alloc;
Michael Lentine03c64b02015-08-26 18:27:26 -050083};
84// These are needed in order to move Strings
85template <class T>
86bool operator==(const CallbackAllocator<T>& alloc1,
87 const CallbackAllocator<T>& alloc2) {
88 return alloc1.alloc == alloc2.alloc;
89}
90template <class T>
91bool operator!=(const CallbackAllocator<T>& alloc1,
92 const CallbackAllocator<T>& alloc2) {
93 return !(alloc1 == alloc2);
94}
95
Michael Lentine03c64b02015-08-26 18:27:26 -050096template <class T>
Jesse Hall1f91d392015-12-11 16:28:44 -080097using Vector = std::vector<T, CallbackAllocator<T>>;
Michael Lentine03c64b02015-08-26 18:27:26 -050098
Jesse Hall1f91d392015-12-11 16:28:44 -080099typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
100 String;
Michael Lentine03c64b02015-08-26 18:27:26 -0500101
Jesse Hall1f91d392015-12-11 16:28:44 -0800102// ----------------------------------------------------------------------------
Jesse Hall80523e22016-01-06 16:47:54 -0800103// Global Data and Initialization
Jesse Hall1f91d392015-12-11 16:28:44 -0800104
Jesse Hall80523e22016-01-06 16:47:54 -0800105hwvulkan_device_t* g_hwdevice = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800106InstanceExtensionSet g_driver_instance_extensions;
107
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800108bool LoadVulkanHAL() {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800109 VkResult vkresult;
110 uint32_t count;
111 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
112 nullptr, &count, nullptr)) != VK_SUCCESS) {
113 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
114 vkresult);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800115 return false;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800116 }
117 VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
118 alloca(count * sizeof(VkExtensionProperties)));
119 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
120 nullptr, &count, extensions)) != VK_SUCCESS) {
121 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
122 vkresult);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800123 return false;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800124 }
125 ALOGV_IF(count > 0, "Driver-supported instance extensions:");
126 for (uint32_t i = 0; i < count; i++) {
127 ALOGV(" %s (v%u)", extensions[i].extensionName,
128 extensions[i].specVersion);
129 InstanceExtension id =
130 InstanceExtensionFromName(extensions[i].extensionName);
131 if (id != kInstanceExtensionCount)
132 g_driver_instance_extensions.set(id);
133 }
134 // Ignore driver attempts to support loader extensions
135 g_driver_instance_extensions.reset(kKHR_surface);
136 g_driver_instance_extensions.reset(kKHR_android_surface);
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800137
138 return true;
Jesse Hall80523e22016-01-06 16:47:54 -0800139}
140
Jesse Hall1f91d392015-12-11 16:28:44 -0800141// -----------------------------------------------------------------------------
142
143struct Instance {
144 Instance(const VkAllocationCallbacks* alloc_callbacks)
Chia-I Wueb7db122016-03-24 09:11:06 +0800145 : base(*alloc_callbacks),
Chia-I Wu0c203242016-03-15 13:44:51 +0800146 alloc(&base.allocator),
147 num_physical_devices(0) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800148 memset(physical_devices, 0, sizeof(physical_devices));
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600149 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800150 }
151
Jesse Hall80523e22016-01-06 16:47:54 -0800152 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800153
Chia-I Wu0c203242016-03-15 13:44:51 +0800154 driver::InstanceData base;
Jesse Hall1f91d392015-12-11 16:28:44 -0800155
Jesse Hall1f91d392015-12-11 16:28:44 -0800156 const VkAllocationCallbacks* alloc;
157 uint32_t num_physical_devices;
Courtney Goeltzenleuchter84cd4c22016-03-16 12:20:13 -0600158 VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800159 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800160 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800161
Jesse Hall715b86a2016-01-16 16:34:29 -0800162 DebugReportCallbackList debug_report_callbacks;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600163 InstanceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800164};
165
166struct Device {
Chia-I Wueb7db122016-03-24 09:11:06 +0800167 Device(Instance* instance_) : base(*instance_->alloc), instance(instance_) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600168 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800169 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800170
171 driver::DeviceData base;
172
Jesse Hall1f91d392015-12-11 16:28:44 -0800173 Instance* instance;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600174 DeviceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800175};
176
177template <typename THandle>
178struct HandleTraits {};
179template <>
180struct HandleTraits<VkInstance> {
181 typedef Instance LoaderObjectType;
182};
183template <>
184struct HandleTraits<VkPhysicalDevice> {
185 typedef Instance LoaderObjectType;
186};
187template <>
188struct HandleTraits<VkDevice> {
189 typedef Device LoaderObjectType;
190};
191template <>
192struct HandleTraits<VkQueue> {
193 typedef Device LoaderObjectType;
194};
195template <>
196struct HandleTraits<VkCommandBuffer> {
197 typedef Device LoaderObjectType;
198};
199
200template <typename THandle>
201typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
202 THandle handle) {
203 // TODO(jessehall): Make Instance and Device POD types (by removing the
204 // non-default constructors), so that offsetof is actually legal to use.
205 // The specific case we're using here is safe in gcc/clang (and probably
206 // most other C++ compilers), but isn't guaranteed by C++.
207 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
208#pragma clang diagnostic push
209#pragma clang diagnostic ignored "-Winvalid-offsetof"
Chia-I Wu0c203242016-03-15 13:44:51 +0800210 const size_t kBaseOffset = offsetof(ObjectType, base);
Jesse Hall1f91d392015-12-11 16:28:44 -0800211#pragma clang diagnostic pop
212
Chia-I Wu0c203242016-03-15 13:44:51 +0800213 const auto& base = driver::GetData(handle);
214 uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base);
215 uintptr_t object_addr = base_addr - kBaseOffset;
Jesse Hall1f91d392015-12-11 16:28:44 -0800216 return *reinterpret_cast<ObjectType*>(object_addr);
217}
218
219// -----------------------------------------------------------------------------
220
Chia-I Wu0c203242016-03-15 13:44:51 +0800221void DestroyDevice(Device* device, VkDevice vkdevice) {
222 const auto& instance = *device->instance;
223
Chia-I Wucc5e2762016-03-24 13:01:16 +0800224 if (vkdevice != VK_NULL_HANDLE && device->base.driver.DestroyDevice)
225 device->base.driver.DestroyDevice(vkdevice, instance.alloc);
Chia-I Wu0c203242016-03-15 13:44:51 +0800226
Jesse Hall04f4f472015-08-16 19:51:04 -0700227 device->~Device();
Chia-I Wu0c203242016-03-15 13:44:51 +0800228 instance.alloc->pfnFree(instance.alloc->pUserData, device);
Michael Lentine03c64b02015-08-26 18:27:26 -0500229}
230
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700231/*
232 * This function will return the pNext pointer of any
233 * CreateInfo extensions that are not loader extensions.
234 * This is used to skip past the loader extensions prepended
235 * to the list during CreateInstance and CreateDevice.
236 */
237void* StripCreateExtensions(const void* pNext) {
238 VkLayerInstanceCreateInfo* create_info =
239 const_cast<VkLayerInstanceCreateInfo*>(
240 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
241
242 while (
243 create_info &&
244 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
245 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
246 create_info = const_cast<VkLayerInstanceCreateInfo*>(
247 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
248 }
249
250 return create_info;
251}
252
Jesse Hallfee71432016-03-05 22:27:02 -0800253// Clean up and deallocate an Instance; called from both the failure paths in
254// CreateInstance_Top as well as from DestroyInstance_Top. This function does
255// not call down the dispatch chain; that should be done before calling this
256// function, iff the lower vkCreateInstance call has been made and returned
257// successfully.
258void DestroyInstance(Instance* instance,
Chia-I Wu0c203242016-03-15 13:44:51 +0800259 const VkAllocationCallbacks* allocator,
260 VkInstance vkinstance) {
Chia-I Wucc5e2762016-03-24 13:01:16 +0800261 if (vkinstance != VK_NULL_HANDLE && instance->base.driver.DestroyInstance)
262 instance->base.driver.DestroyInstance(vkinstance, allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +0800263
Jesse Hallfee71432016-03-05 22:27:02 -0800264 instance->~Instance();
265 allocator->pfnFree(allocator->pUserData, instance);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700266}
267
Chia-I Wueb7db122016-03-24 09:11:06 +0800268driver::ProcHook::Extension InstanceExtensionToProcHookExtension(
269 InstanceExtension id) {
270 switch (id) {
271 case kKHR_surface:
272 return driver::ProcHook::KHR_surface;
273 case kKHR_android_surface:
274 return driver::ProcHook::KHR_android_surface;
275 case kEXT_debug_report:
276 return driver::ProcHook::EXT_debug_report;
277 default:
278 return driver::ProcHook::EXTENSION_UNKNOWN;
279 }
280}
281
282driver::ProcHook::Extension DeviceExtensionToProcHookExtension(
283 DeviceExtension id) {
284 switch (id) {
285 case kKHR_swapchain:
286 return driver::ProcHook::KHR_swapchain;
287 case kANDROID_native_buffer:
288 return driver::ProcHook::ANDROID_native_buffer;
289 default:
290 return driver::ProcHook::EXTENSION_UNKNOWN;
291 }
292}
293
Jesse Hall1f91d392015-12-11 16:28:44 -0800294} // anonymous namespace
295
296namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500297
Jesse Hall04f4f472015-08-16 19:51:04 -0700298// -----------------------------------------------------------------------------
299// "Bottom" functions. These are called at the end of the instance dispatch
300// chain.
301
Jesse Hall1f91d392015-12-11 16:28:44 -0800302VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
303 const VkAllocationCallbacks* allocator,
304 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700305 VkResult result;
306
Chia-I Wu0c203242016-03-15 13:44:51 +0800307 if (!allocator)
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800308 allocator = &driver::GetDefaultAllocator();
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700309
Chia-I Wu0c203242016-03-15 13:44:51 +0800310 void* instance_mem = allocator->pfnAllocation(
311 allocator->pUserData, sizeof(Instance), alignof(Instance),
312 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
313 if (!instance_mem)
314 return VK_ERROR_OUT_OF_HOST_MEMORY;
315 Instance& instance = *new (instance_mem) Instance(allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700316
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800317 // Check that all enabled extensions are supported
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800318 uint32_t num_driver_extensions = 0;
Chia-I Wueb7db122016-03-24 09:11:06 +0800319 bool enable_kEXT_debug_report = false;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800320 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
321 const char* name = create_info->ppEnabledExtensionNames[i];
322 InstanceExtension id = InstanceExtensionFromName(name);
323 if (id != kInstanceExtensionCount) {
324 if (g_driver_instance_extensions[id]) {
325 num_driver_extensions++;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600326 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800327 continue;
328 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700329 if (id == kKHR_surface || id == kKHR_android_surface) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600330 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800331 continue;
332 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700333 // The loader natively supports debug report.
334 if (id == kEXT_debug_report) {
Chia-I Wueb7db122016-03-24 09:11:06 +0800335 enable_kEXT_debug_report = true;
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700336 continue;
337 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800338 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800339 }
340
Chia-I Wueb7db122016-03-24 09:11:06 +0800341 auto& hal_exts = instance.base.hal_extensions;
342 for (size_t i = 0; i < instance.enabled_extensions.size(); i++) {
343 if (instance.enabled_extensions[i]) {
344 auto bit = InstanceExtensionToProcHookExtension(
345 static_cast<InstanceExtension>(i));
346 if (bit != driver::ProcHook::EXTENSION_UNKNOWN)
347 hal_exts.set(bit);
348 }
349 }
350
351 auto& hook_exts = instance.base.hook_extensions;
352 hook_exts = hal_exts;
353 if (enable_kEXT_debug_report)
354 hook_exts.set(driver::ProcHook::EXT_debug_report);
355
Jesse Halla7ac76d2016-01-08 22:29:42 -0800356 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700357 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800358 driver_create_info.enabledLayerCount = 0;
359 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800360 driver_create_info.enabledExtensionCount = 0;
361 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800362 if (num_driver_extensions > 0) {
363 const char** names = static_cast<const char**>(
364 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800365 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800366 const char* name = create_info->ppEnabledExtensionNames[i];
367 InstanceExtension id = InstanceExtensionFromName(name);
368 if (id != kInstanceExtensionCount) {
369 if (g_driver_instance_extensions[id]) {
370 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800371 continue;
372 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800373 }
374 }
375 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800376 ALOG_ASSERT(
377 driver_create_info.enabledExtensionCount == num_driver_extensions,
378 "counted enabled driver instance extensions twice and got "
379 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800380 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800381
Chia-I Wu0c203242016-03-15 13:44:51 +0800382 VkInstance drv_instance;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800383 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Chia-I Wu0c203242016-03-15 13:44:51 +0800384 &drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700385 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800386 DestroyInstance(&instance, allocator, VK_NULL_HANDLE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700387 return result;
388 }
389
Chia-I Wu0c203242016-03-15 13:44:51 +0800390 if (!driver::SetData(drv_instance, instance.base)) {
391 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700392 return VK_ERROR_INITIALIZATION_FAILED;
393 }
394
Chia-I Wucc5e2762016-03-24 13:01:16 +0800395 if (!driver::InitDriverTable(drv_instance,
396 g_hwdevice->GetInstanceProcAddr)) {
397 DestroyInstance(&instance, allocator, drv_instance);
398 return VK_ERROR_INITIALIZATION_FAILED;
399 }
400
401 instance.base.get_device_proc_addr =
402 reinterpret_cast<PFN_vkGetDeviceProcAddr>(
403 g_hwdevice->GetInstanceProcAddr(drv_instance,
404 "vkGetDeviceProcAddr"));
405 if (!instance.base.get_device_proc_addr) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800406 DestroyInstance(&instance, allocator, drv_instance);
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700407 return VK_ERROR_INITIALIZATION_FAILED;
408 }
409
Jesse Hall04f4f472015-08-16 19:51:04 -0700410 uint32_t num_physical_devices = 0;
Chia-I Wucc5e2762016-03-24 13:01:16 +0800411 result = instance.base.driver.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800412 drv_instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700413 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800414 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700415 return VK_ERROR_INITIALIZATION_FAILED;
416 }
417 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Chia-I Wucc5e2762016-03-24 13:01:16 +0800418 result = instance.base.driver.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800419 drv_instance, &num_physical_devices, instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700420 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800421 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700422 return VK_ERROR_INITIALIZATION_FAILED;
423 }
Jesse Hallb1471272016-01-17 21:36:58 -0800424
425 Vector<VkExtensionProperties> extensions(
426 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700427 for (uint32_t i = 0; i < num_physical_devices; i++) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800428 if (!driver::SetData(instance.physical_devices[i], instance.base)) {
429 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700430 return VK_ERROR_INITIALIZATION_FAILED;
431 }
Jesse Hallb1471272016-01-17 21:36:58 -0800432
433 uint32_t count;
Chia-I Wucc5e2762016-03-24 13:01:16 +0800434 if ((result = instance.base.driver.EnumerateDeviceExtensionProperties(
Jesse Hallb1471272016-01-17 21:36:58 -0800435 instance.physical_devices[i], nullptr, &count, nullptr)) !=
436 VK_SUCCESS) {
437 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
438 result);
439 continue;
440 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800441 try {
442 extensions.resize(count);
443 } catch (std::bad_alloc&) {
444 ALOGE("instance creation failed: out of memory");
Chia-I Wu0c203242016-03-15 13:44:51 +0800445 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall26cecff2016-01-21 19:52:25 -0800446 return VK_ERROR_OUT_OF_HOST_MEMORY;
447 }
Chia-I Wucc5e2762016-03-24 13:01:16 +0800448 if ((result = instance.base.driver.EnumerateDeviceExtensionProperties(
Jesse Hallb1471272016-01-17 21:36:58 -0800449 instance.physical_devices[i], nullptr, &count,
450 extensions.data())) != VK_SUCCESS) {
451 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
452 result);
453 continue;
454 }
455 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
456 for (const auto& extension : extensions) {
457 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
458 DeviceExtension id =
459 DeviceExtensionFromName(extension.extensionName);
460 if (id == kDeviceExtensionCount) {
461 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
462 extension.extensionName);
463 } else {
464 instance.physical_device_driver_extensions[i].set(id);
465 }
466 }
467 // Ignore driver attempts to support loader extensions
468 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700469 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800470 instance.num_physical_devices = num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800471
Chia-I Wu0c203242016-03-15 13:44:51 +0800472 *vkinstance = drv_instance;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700473
Jesse Hall04f4f472015-08-16 19:51:04 -0700474 return VK_SUCCESS;
475}
476
Jesse Hall1f91d392015-12-11 16:28:44 -0800477VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
478 uint32_t* pdev_count,
479 VkPhysicalDevice* pdevs) {
480 Instance& instance = GetDispatchParent(vkinstance);
481 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700482 if (pdevs) {
483 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800484 std::copy(instance.physical_devices, instance.physical_devices + count,
485 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700486 }
487 *pdev_count = count;
488 return VK_SUCCESS;
489}
490
Jesse Halle1b12782015-11-30 11:27:32 -0800491VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800492VkResult EnumerateDeviceExtensionProperties_Bottom(
Chia-I Wu0c203242016-03-15 13:44:51 +0800493 VkPhysicalDevice pdev,
494 const char* layer_name,
495 uint32_t* properties_count,
496 VkExtensionProperties* properties) {
497 (void)layer_name;
498
499 Instance& instance = GetDispatchParent(pdev);
500
501 size_t gpu_idx = 0;
502 while (instance.physical_devices[gpu_idx] != pdev)
503 gpu_idx++;
504 const DeviceExtensionSet driver_extensions =
505 instance.physical_device_driver_extensions[gpu_idx];
506
507 // We only support VK_KHR_swapchain if the GPU supports
508 // VK_ANDROID_native_buffer
509 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
510 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
511 uint32_t num_extensions = 0;
512 if (driver_extensions[kANDROID_native_buffer]) {
513 available[num_extensions++] = VkExtensionProperties{
514 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
515 }
516
517 if (!properties || *properties_count > num_extensions)
518 *properties_count = num_extensions;
519 if (properties)
520 std::copy(available, available + *properties_count, properties);
521
522 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700523}
524
Jesse Hall1f91d392015-12-11 16:28:44 -0800525VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800526VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800527 const VkDeviceCreateInfo* create_info,
528 const VkAllocationCallbacks* allocator,
529 VkDevice* device_out) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700530 Instance& instance = GetDispatchParent(gpu);
Chia-I Wu0c203242016-03-15 13:44:51 +0800531
532 // FIXME(jessehall): We don't have good conventions or infrastructure yet to
533 // do better than just using the instance allocator and scope for
534 // everything. See b/26732122.
535 if (true /*!allocator*/)
536 allocator = instance.alloc;
537
538 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
539 alignof(Device),
540 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
541 if (!mem)
542 return VK_ERROR_OUT_OF_HOST_MEMORY;
543 Device* device = new (mem) Device(&instance);
544
Jesse Hallb1471272016-01-17 21:36:58 -0800545 size_t gpu_idx = 0;
546 while (instance.physical_devices[gpu_idx] != gpu)
547 gpu_idx++;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700548
549 VkDeviceCreateInfo driver_create_info = *create_info;
550 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
551 driver_create_info.enabledLayerCount = 0;
552 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800553
554 uint32_t num_driver_extensions = 0;
555 const char** driver_extensions = static_cast<const char**>(
556 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
557 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
558 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800559 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800560 if (id != kDeviceExtensionCount) {
561 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
562 driver_extensions[num_driver_extensions++] = name;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600563 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800564 continue;
565 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700566 // Add the VK_ANDROID_native_buffer extension to the list iff
567 // the VK_KHR_swapchain extension was requested
Jesse Hallae3b70d2016-01-17 22:05:29 -0800568 if (id == kKHR_swapchain &&
569 instance.physical_device_driver_extensions
570 [gpu_idx][kANDROID_native_buffer]) {
571 driver_extensions[num_driver_extensions++] =
572 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600573 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800574 continue;
575 }
Jesse Hallb1471272016-01-17 21:36:58 -0800576 }
Jesse Hallb1471272016-01-17 21:36:58 -0800577 }
578
Chia-I Wueb7db122016-03-24 09:11:06 +0800579 // Unlike instance->enabled_extensions, device->enabled_extensions maps to
580 // hook extensions.
581 auto& hook_exts = device->base.hook_extensions;
582 for (size_t i = 0; i < device->enabled_extensions.size(); i++) {
583 if (device->enabled_extensions[i]) {
584 auto bit = DeviceExtensionToProcHookExtension(
585 static_cast<DeviceExtension>(i));
586 if (bit != driver::ProcHook::EXTENSION_UNKNOWN)
587 hook_exts.set(bit);
588 }
589 }
590
591 auto& hal_exts = device->base.hal_extensions;
592 hal_exts = hook_exts;
593 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
594 if (hal_exts[driver::ProcHook::KHR_swapchain]) {
595 hal_exts.reset(driver::ProcHook::KHR_swapchain);
596 hal_exts.set(driver::ProcHook::ANDROID_native_buffer);
597 }
598
Jesse Hallb1471272016-01-17 21:36:58 -0800599 driver_create_info.enabledExtensionCount = num_driver_extensions;
600 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Hall04f4f472015-08-16 19:51:04 -0700601 VkDevice drv_device;
Chia-I Wucc5e2762016-03-24 13:01:16 +0800602 VkResult result = instance.base.driver.CreateDevice(
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700603 gpu, &driver_create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700604 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800605 DestroyDevice(device, VK_NULL_HANDLE);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700606 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700607 }
608
Chia-I Wu0c203242016-03-15 13:44:51 +0800609 if (!driver::SetData(drv_device, device->base)) {
610 DestroyDevice(device, drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700611 return VK_ERROR_INITIALIZATION_FAILED;
612 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700613
Chia-I Wucc5e2762016-03-24 13:01:16 +0800614 if (!driver::InitDriverTable(drv_device,
615 instance.base.get_device_proc_addr)) {
616 DestroyDevice(device, drv_device);
617 return VK_ERROR_INITIALIZATION_FAILED;
618 }
Chia-I Wueb7db122016-03-24 09:11:06 +0800619
Jesse Hall1f91d392015-12-11 16:28:44 -0800620 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700621 return VK_SUCCESS;
622}
623
Jesse Hall1f91d392015-12-11 16:28:44 -0800624void DestroyInstance_Bottom(VkInstance vkinstance,
625 const VkAllocationCallbacks* allocator) {
626 Instance& instance = GetDispatchParent(vkinstance);
627
Chia-I Wu0c203242016-03-15 13:44:51 +0800628 VkAllocationCallbacks local_allocator;
629 if (!allocator) {
630 local_allocator = *instance.alloc;
631 allocator = &local_allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800632 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800633
634 DestroyInstance(&instance, allocator, vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700635}
636
Chia-I Wu0c203242016-03-15 13:44:51 +0800637void DestroyDevice_Bottom(VkDevice vkdevice, const VkAllocationCallbacks*) {
638 DestroyDevice(&GetDispatchParent(vkdevice), vkdevice);
Jesse Hall04f4f472015-08-16 19:51:04 -0700639}
640
Jesse Hall1f91d392015-12-11 16:28:44 -0800641// -----------------------------------------------------------------------------
642
643const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
644 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800645}
646
Jesse Hall1f91d392015-12-11 16:28:44 -0800647const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
648 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800649}
650
Jesse Hall715b86a2016-01-16 16:34:29 -0800651VkInstance GetDriverInstance(VkInstance instance) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800652 return instance;
Jesse Hall715b86a2016-01-16 16:34:29 -0800653}
654
Chia-I Wucc5e2762016-03-24 13:01:16 +0800655const driver::InstanceDriverTable& GetDriverDispatch(VkInstance instance) {
656 return driver::GetData(instance).driver;
Jesse Hall715b86a2016-01-16 16:34:29 -0800657}
658
Chia-I Wucc5e2762016-03-24 13:01:16 +0800659const driver::DeviceDriverTable& GetDriverDispatch(VkDevice device) {
660 return driver::GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700661}
662
Chia-I Wucc5e2762016-03-24 13:01:16 +0800663const driver::DeviceDriverTable& GetDriverDispatch(VkQueue queue) {
664 return driver::GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700665}
666
Jesse Hall715b86a2016-01-16 16:34:29 -0800667DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
668 return GetDispatchParent(instance).debug_report_callbacks;
669}
670
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800671bool InitLoader(hwvulkan_device_t* dev) {
672 if (!g_hwdevice) {
673 g_hwdevice = dev;
674 if (!LoadVulkanHAL())
675 g_hwdevice = nullptr;
676 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800677
678 return (g_hwdevice != nullptr);
679}
680
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800681namespace driver {
682
Chia-I Wu0c203242016-03-15 13:44:51 +0800683VkResult EnumerateInstanceExtensionProperties(
684 const char* pLayerName,
685 uint32_t* pPropertyCount,
686 VkExtensionProperties* pProperties) {
687 (void)pLayerName;
688
689 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
690 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
691 uint32_t num_extensions = 0;
692
693 available[num_extensions++] = VkExtensionProperties{
694 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
695 available[num_extensions++] =
696 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
697 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
698 if (g_driver_instance_extensions[kEXT_debug_report]) {
699 available[num_extensions++] =
700 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
701 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
702 }
703
704 if (!pProperties || *pPropertyCount > num_extensions)
705 *pPropertyCount = num_extensions;
706 if (pProperties)
707 std::copy(available, available + *pPropertyCount, pProperties);
708
709 return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
710}
711
712} // namespace driver
713
Jesse Hall04f4f472015-08-16 19:51:04 -0700714} // namespace vulkan