blob: 8c68efa70ae5e3f7130ace5b563fe51e7a301985 [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 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
Jesse Hall1f91d392015-12-11 16:28:44 -0800151 }
152
Jesse Hall80523e22016-01-06 16:47:54 -0800153 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800154
Chia-I Wu0c203242016-03-15 13:44:51 +0800155 driver::InstanceData base;
Jesse Hall1f91d392015-12-11 16:28:44 -0800156
Jesse Hall1f91d392015-12-11 16:28:44 -0800157 const VkAllocationCallbacks* alloc;
158 uint32_t num_physical_devices;
Courtney Goeltzenleuchter84cd4c22016-03-16 12:20:13 -0600159 VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800160 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800161 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800162
Jesse Hall715b86a2016-01-16 16:34:29 -0800163 DebugReportCallbackList debug_report_callbacks;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600164 InstanceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800165
166 struct {
Jesse Hall1f91d392015-12-11 16:28:44 -0800167 DriverDispatchTable dispatch;
Jesse Hall1f91d392015-12-11 16:28:44 -0800168 } drv; // may eventually be an array
169};
170
171struct Device {
Chia-I Wueb7db122016-03-24 09:11:06 +0800172 Device(Instance* instance_) : base(*instance_->alloc), instance(instance_) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600173 enabled_extensions.reset();
Jesse Hall1f91d392015-12-11 16:28:44 -0800174 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800175
176 driver::DeviceData base;
177
Jesse Hall1f91d392015-12-11 16:28:44 -0800178 Instance* instance;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600179 DeviceExtensionSet enabled_extensions;
Jesse Hall1f91d392015-12-11 16:28:44 -0800180};
181
182template <typename THandle>
183struct HandleTraits {};
184template <>
185struct HandleTraits<VkInstance> {
186 typedef Instance LoaderObjectType;
187};
188template <>
189struct HandleTraits<VkPhysicalDevice> {
190 typedef Instance LoaderObjectType;
191};
192template <>
193struct HandleTraits<VkDevice> {
194 typedef Device LoaderObjectType;
195};
196template <>
197struct HandleTraits<VkQueue> {
198 typedef Device LoaderObjectType;
199};
200template <>
201struct HandleTraits<VkCommandBuffer> {
202 typedef Device LoaderObjectType;
203};
204
205template <typename THandle>
206typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
207 THandle handle) {
208 // TODO(jessehall): Make Instance and Device POD types (by removing the
209 // non-default constructors), so that offsetof is actually legal to use.
210 // The specific case we're using here is safe in gcc/clang (and probably
211 // most other C++ compilers), but isn't guaranteed by C++.
212 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
213#pragma clang diagnostic push
214#pragma clang diagnostic ignored "-Winvalid-offsetof"
Chia-I Wu0c203242016-03-15 13:44:51 +0800215 const size_t kBaseOffset = offsetof(ObjectType, base);
Jesse Hall1f91d392015-12-11 16:28:44 -0800216#pragma clang diagnostic pop
217
Chia-I Wu0c203242016-03-15 13:44:51 +0800218 const auto& base = driver::GetData(handle);
219 uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base);
220 uintptr_t object_addr = base_addr - kBaseOffset;
Jesse Hall1f91d392015-12-11 16:28:44 -0800221 return *reinterpret_cast<ObjectType*>(object_addr);
222}
223
224// -----------------------------------------------------------------------------
225
Chia-I Wu0c203242016-03-15 13:44:51 +0800226void DestroyDevice(Device* device, VkDevice vkdevice) {
227 const auto& instance = *device->instance;
228
229 if (vkdevice != VK_NULL_HANDLE)
230 instance.drv.dispatch.DestroyDevice(vkdevice, instance.alloc);
231
Jesse Hall04f4f472015-08-16 19:51:04 -0700232 device->~Device();
Chia-I Wu0c203242016-03-15 13:44:51 +0800233 instance.alloc->pfnFree(instance.alloc->pUserData, device);
Michael Lentine03c64b02015-08-26 18:27:26 -0500234}
235
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700236/*
237 * This function will return the pNext pointer of any
238 * CreateInfo extensions that are not loader extensions.
239 * This is used to skip past the loader extensions prepended
240 * to the list during CreateInstance and CreateDevice.
241 */
242void* StripCreateExtensions(const void* pNext) {
243 VkLayerInstanceCreateInfo* create_info =
244 const_cast<VkLayerInstanceCreateInfo*>(
245 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
246
247 while (
248 create_info &&
249 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
250 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
251 create_info = const_cast<VkLayerInstanceCreateInfo*>(
252 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
253 }
254
255 return create_info;
256}
257
Jesse Hallfee71432016-03-05 22:27:02 -0800258// Clean up and deallocate an Instance; called from both the failure paths in
259// CreateInstance_Top as well as from DestroyInstance_Top. This function does
260// not call down the dispatch chain; that should be done before calling this
261// function, iff the lower vkCreateInstance call has been made and returned
262// successfully.
263void DestroyInstance(Instance* instance,
Chia-I Wu0c203242016-03-15 13:44:51 +0800264 const VkAllocationCallbacks* allocator,
265 VkInstance vkinstance) {
266 if (vkinstance != VK_NULL_HANDLE && instance->drv.dispatch.DestroyInstance)
267 instance->drv.dispatch.DestroyInstance(vkinstance, allocator);
268
Jesse Hallfee71432016-03-05 22:27:02 -0800269 instance->~Instance();
270 allocator->pfnFree(allocator->pUserData, instance);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700271}
272
Chia-I Wueb7db122016-03-24 09:11:06 +0800273driver::ProcHook::Extension InstanceExtensionToProcHookExtension(
274 InstanceExtension id) {
275 switch (id) {
276 case kKHR_surface:
277 return driver::ProcHook::KHR_surface;
278 case kKHR_android_surface:
279 return driver::ProcHook::KHR_android_surface;
280 case kEXT_debug_report:
281 return driver::ProcHook::EXT_debug_report;
282 default:
283 return driver::ProcHook::EXTENSION_UNKNOWN;
284 }
285}
286
287driver::ProcHook::Extension DeviceExtensionToProcHookExtension(
288 DeviceExtension id) {
289 switch (id) {
290 case kKHR_swapchain:
291 return driver::ProcHook::KHR_swapchain;
292 case kANDROID_native_buffer:
293 return driver::ProcHook::ANDROID_native_buffer;
294 default:
295 return driver::ProcHook::EXTENSION_UNKNOWN;
296 }
297}
298
Jesse Hall1f91d392015-12-11 16:28:44 -0800299} // anonymous namespace
300
301namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500302
Jesse Hall04f4f472015-08-16 19:51:04 -0700303// -----------------------------------------------------------------------------
304// "Bottom" functions. These are called at the end of the instance dispatch
305// chain.
306
Jesse Hall1f91d392015-12-11 16:28:44 -0800307VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
308 const VkAllocationCallbacks* allocator,
309 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700310 VkResult result;
311
Chia-I Wu0c203242016-03-15 13:44:51 +0800312 if (!allocator)
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800313 allocator = &driver::GetDefaultAllocator();
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700314
Chia-I Wu0c203242016-03-15 13:44:51 +0800315 void* instance_mem = allocator->pfnAllocation(
316 allocator->pUserData, sizeof(Instance), alignof(Instance),
317 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
318 if (!instance_mem)
319 return VK_ERROR_OUT_OF_HOST_MEMORY;
320 Instance& instance = *new (instance_mem) Instance(allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700321
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800322 // Check that all enabled extensions are supported
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800323 uint32_t num_driver_extensions = 0;
Chia-I Wueb7db122016-03-24 09:11:06 +0800324 bool enable_kEXT_debug_report = false;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800325 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
326 const char* name = create_info->ppEnabledExtensionNames[i];
327 InstanceExtension id = InstanceExtensionFromName(name);
328 if (id != kInstanceExtensionCount) {
329 if (g_driver_instance_extensions[id]) {
330 num_driver_extensions++;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600331 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800332 continue;
333 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700334 if (id == kKHR_surface || id == kKHR_android_surface) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600335 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800336 continue;
337 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700338 // The loader natively supports debug report.
339 if (id == kEXT_debug_report) {
Chia-I Wueb7db122016-03-24 09:11:06 +0800340 enable_kEXT_debug_report = true;
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700341 continue;
342 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800343 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800344 }
345
Chia-I Wueb7db122016-03-24 09:11:06 +0800346 auto& hal_exts = instance.base.hal_extensions;
347 for (size_t i = 0; i < instance.enabled_extensions.size(); i++) {
348 if (instance.enabled_extensions[i]) {
349 auto bit = InstanceExtensionToProcHookExtension(
350 static_cast<InstanceExtension>(i));
351 if (bit != driver::ProcHook::EXTENSION_UNKNOWN)
352 hal_exts.set(bit);
353 }
354 }
355
356 auto& hook_exts = instance.base.hook_extensions;
357 hook_exts = hal_exts;
358 if (enable_kEXT_debug_report)
359 hook_exts.set(driver::ProcHook::EXT_debug_report);
360
Jesse Halla7ac76d2016-01-08 22:29:42 -0800361 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700362 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800363 driver_create_info.enabledLayerCount = 0;
364 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800365 driver_create_info.enabledExtensionCount = 0;
366 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800367 if (num_driver_extensions > 0) {
368 const char** names = static_cast<const char**>(
369 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800370 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800371 const char* name = create_info->ppEnabledExtensionNames[i];
372 InstanceExtension id = InstanceExtensionFromName(name);
373 if (id != kInstanceExtensionCount) {
374 if (g_driver_instance_extensions[id]) {
375 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800376 continue;
377 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800378 }
379 }
380 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800381 ALOG_ASSERT(
382 driver_create_info.enabledExtensionCount == num_driver_extensions,
383 "counted enabled driver instance extensions twice and got "
384 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800385 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800386
Chia-I Wu0c203242016-03-15 13:44:51 +0800387 VkInstance drv_instance;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800388 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Chia-I Wu0c203242016-03-15 13:44:51 +0800389 &drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700390 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800391 DestroyInstance(&instance, allocator, VK_NULL_HANDLE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700392 return result;
393 }
394
Chia-I Wu0c203242016-03-15 13:44:51 +0800395 if (!driver::SetData(drv_instance, instance.base)) {
396 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700397 return VK_ERROR_INITIALIZATION_FAILED;
398 }
399
Chia-I Wu0c203242016-03-15 13:44:51 +0800400 if (!LoadDriverDispatchTable(drv_instance, g_hwdevice->GetInstanceProcAddr,
401 instance.enabled_extensions,
402 instance.drv.dispatch)) {
403 DestroyInstance(&instance, allocator, drv_instance);
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700404 return VK_ERROR_INITIALIZATION_FAILED;
405 }
406
Jesse Hall04f4f472015-08-16 19:51:04 -0700407 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800408 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800409 drv_instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700410 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800411 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700412 return VK_ERROR_INITIALIZATION_FAILED;
413 }
414 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800415 result = instance.drv.dispatch.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800416 drv_instance, &num_physical_devices, instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700417 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800418 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700419 return VK_ERROR_INITIALIZATION_FAILED;
420 }
Jesse Hallb1471272016-01-17 21:36:58 -0800421
422 Vector<VkExtensionProperties> extensions(
423 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700424 for (uint32_t i = 0; i < num_physical_devices; i++) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800425 if (!driver::SetData(instance.physical_devices[i], instance.base)) {
426 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700427 return VK_ERROR_INITIALIZATION_FAILED;
428 }
Jesse Hallb1471272016-01-17 21:36:58 -0800429
430 uint32_t count;
431 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
432 instance.physical_devices[i], nullptr, &count, nullptr)) !=
433 VK_SUCCESS) {
434 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
435 result);
436 continue;
437 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800438 try {
439 extensions.resize(count);
440 } catch (std::bad_alloc&) {
441 ALOGE("instance creation failed: out of memory");
Chia-I Wu0c203242016-03-15 13:44:51 +0800442 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall26cecff2016-01-21 19:52:25 -0800443 return VK_ERROR_OUT_OF_HOST_MEMORY;
444 }
Jesse Hallb1471272016-01-17 21:36:58 -0800445 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
446 instance.physical_devices[i], nullptr, &count,
447 extensions.data())) != VK_SUCCESS) {
448 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
449 result);
450 continue;
451 }
452 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
453 for (const auto& extension : extensions) {
454 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
455 DeviceExtension id =
456 DeviceExtensionFromName(extension.extensionName);
457 if (id == kDeviceExtensionCount) {
458 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
459 extension.extensionName);
460 } else {
461 instance.physical_device_driver_extensions[i].set(id);
462 }
463 }
464 // Ignore driver attempts to support loader extensions
465 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700466 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800467 instance.num_physical_devices = num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800468
Chia-I Wu0c203242016-03-15 13:44:51 +0800469 *vkinstance = drv_instance;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700470
Jesse Hall04f4f472015-08-16 19:51:04 -0700471 return VK_SUCCESS;
472}
473
Jesse Hall1f91d392015-12-11 16:28:44 -0800474VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
475 uint32_t* pdev_count,
476 VkPhysicalDevice* pdevs) {
477 Instance& instance = GetDispatchParent(vkinstance);
478 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700479 if (pdevs) {
480 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800481 std::copy(instance.physical_devices, instance.physical_devices + count,
482 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700483 }
484 *pdev_count = count;
485 return VK_SUCCESS;
486}
487
Jesse Halle1b12782015-11-30 11:27:32 -0800488VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800489VkResult EnumerateDeviceExtensionProperties_Bottom(
Chia-I Wu0c203242016-03-15 13:44:51 +0800490 VkPhysicalDevice pdev,
491 const char* layer_name,
492 uint32_t* properties_count,
493 VkExtensionProperties* properties) {
494 (void)layer_name;
495
496 Instance& instance = GetDispatchParent(pdev);
497
498 size_t gpu_idx = 0;
499 while (instance.physical_devices[gpu_idx] != pdev)
500 gpu_idx++;
501 const DeviceExtensionSet driver_extensions =
502 instance.physical_device_driver_extensions[gpu_idx];
503
504 // We only support VK_KHR_swapchain if the GPU supports
505 // VK_ANDROID_native_buffer
506 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
507 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
508 uint32_t num_extensions = 0;
509 if (driver_extensions[kANDROID_native_buffer]) {
510 available[num_extensions++] = VkExtensionProperties{
511 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
512 }
513
514 if (!properties || *properties_count > num_extensions)
515 *properties_count = num_extensions;
516 if (properties)
517 std::copy(available, available + *properties_count, properties);
518
519 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700520}
521
Jesse Hall1f91d392015-12-11 16:28:44 -0800522VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800523VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800524 const VkDeviceCreateInfo* create_info,
525 const VkAllocationCallbacks* allocator,
526 VkDevice* device_out) {
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700527 Instance& instance = GetDispatchParent(gpu);
Chia-I Wu0c203242016-03-15 13:44:51 +0800528
529 // FIXME(jessehall): We don't have good conventions or infrastructure yet to
530 // do better than just using the instance allocator and scope for
531 // everything. See b/26732122.
532 if (true /*!allocator*/)
533 allocator = instance.alloc;
534
535 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
536 alignof(Device),
537 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
538 if (!mem)
539 return VK_ERROR_OUT_OF_HOST_MEMORY;
540 Device* device = new (mem) Device(&instance);
541
Jesse Hallb1471272016-01-17 21:36:58 -0800542 size_t gpu_idx = 0;
543 while (instance.physical_devices[gpu_idx] != gpu)
544 gpu_idx++;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700545
546 VkDeviceCreateInfo driver_create_info = *create_info;
547 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
548 driver_create_info.enabledLayerCount = 0;
549 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800550
551 uint32_t num_driver_extensions = 0;
552 const char** driver_extensions = static_cast<const char**>(
553 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
554 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
555 const char* name = create_info->ppEnabledExtensionNames[i];
Jesse Hallb1471272016-01-17 21:36:58 -0800556 DeviceExtension id = DeviceExtensionFromName(name);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800557 if (id != kDeviceExtensionCount) {
558 if (instance.physical_device_driver_extensions[gpu_idx][id]) {
559 driver_extensions[num_driver_extensions++] = name;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600560 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800561 continue;
562 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700563 // Add the VK_ANDROID_native_buffer extension to the list iff
564 // the VK_KHR_swapchain extension was requested
Jesse Hallae3b70d2016-01-17 22:05:29 -0800565 if (id == kKHR_swapchain &&
566 instance.physical_device_driver_extensions
567 [gpu_idx][kANDROID_native_buffer]) {
568 driver_extensions[num_driver_extensions++] =
569 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600570 device->enabled_extensions.set(id);
Jesse Hallae3b70d2016-01-17 22:05:29 -0800571 continue;
572 }
Jesse Hallb1471272016-01-17 21:36:58 -0800573 }
Jesse Hallb1471272016-01-17 21:36:58 -0800574 }
575
Chia-I Wueb7db122016-03-24 09:11:06 +0800576 // Unlike instance->enabled_extensions, device->enabled_extensions maps to
577 // hook extensions.
578 auto& hook_exts = device->base.hook_extensions;
579 for (size_t i = 0; i < device->enabled_extensions.size(); i++) {
580 if (device->enabled_extensions[i]) {
581 auto bit = DeviceExtensionToProcHookExtension(
582 static_cast<DeviceExtension>(i));
583 if (bit != driver::ProcHook::EXTENSION_UNKNOWN)
584 hook_exts.set(bit);
585 }
586 }
587
588 auto& hal_exts = device->base.hal_extensions;
589 hal_exts = hook_exts;
590 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
591 if (hal_exts[driver::ProcHook::KHR_swapchain]) {
592 hal_exts.reset(driver::ProcHook::KHR_swapchain);
593 hal_exts.set(driver::ProcHook::ANDROID_native_buffer);
594 }
595
Jesse Hallb1471272016-01-17 21:36:58 -0800596 driver_create_info.enabledExtensionCount = num_driver_extensions;
597 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Hall04f4f472015-08-16 19:51:04 -0700598 VkDevice drv_device;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700599 VkResult result = instance.drv.dispatch.CreateDevice(
600 gpu, &driver_create_info, allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700601 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800602 DestroyDevice(device, VK_NULL_HANDLE);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700603 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700604 }
605
Chia-I Wu0c203242016-03-15 13:44:51 +0800606 if (!driver::SetData(drv_device, device->base)) {
607 DestroyDevice(device, drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700608 return VK_ERROR_INITIALIZATION_FAILED;
609 }
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700610
Chia-I Wueb7db122016-03-24 09:11:06 +0800611 device->base.get_device_proc_addr =
612 reinterpret_cast<PFN_vkGetDeviceProcAddr>(
613 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
614 "vkGetDeviceProcAddr"));
615
Jesse Hall1f91d392015-12-11 16:28:44 -0800616 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700617 return VK_SUCCESS;
618}
619
Jesse Hall1f91d392015-12-11 16:28:44 -0800620void DestroyInstance_Bottom(VkInstance vkinstance,
621 const VkAllocationCallbacks* allocator) {
622 Instance& instance = GetDispatchParent(vkinstance);
623
Chia-I Wu0c203242016-03-15 13:44:51 +0800624 VkAllocationCallbacks local_allocator;
625 if (!allocator) {
626 local_allocator = *instance.alloc;
627 allocator = &local_allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800628 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800629
630 DestroyInstance(&instance, allocator, vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700631}
632
Chia-I Wu0c203242016-03-15 13:44:51 +0800633void DestroyDevice_Bottom(VkDevice vkdevice, const VkAllocationCallbacks*) {
634 DestroyDevice(&GetDispatchParent(vkdevice), vkdevice);
Jesse Hall04f4f472015-08-16 19:51:04 -0700635}
636
Chia-I Wu0c203242016-03-15 13:44:51 +0800637void GetDeviceQueue_Bottom(VkDevice vkdevice,
638 uint32_t family,
639 uint32_t index,
640 VkQueue* queue_out) {
641 const auto& device = GetDispatchParent(vkdevice);
642 const auto& instance = *device.instance;
Jesse Hall04f4f472015-08-16 19:51:04 -0700643
Chia-I Wu0c203242016-03-15 13:44:51 +0800644 instance.drv.dispatch.GetDeviceQueue(vkdevice, family, index, queue_out);
645 driver::SetData(*queue_out, device.base);
Jesse Hall04f4f472015-08-16 19:51:04 -0700646}
647
Chia-I Wu0c203242016-03-15 13:44:51 +0800648VkResult AllocateCommandBuffers_Bottom(
Jesse Hall1f91d392015-12-11 16:28:44 -0800649 VkDevice vkdevice,
650 const VkCommandBufferAllocateInfo* alloc_info,
651 VkCommandBuffer* cmdbufs) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800652 const auto& device = GetDispatchParent(vkdevice);
653 const auto& instance = *device.instance;
Jesse Hallc7a6eb52015-08-31 12:52:03 -0700654
Chia-I Wu0c203242016-03-15 13:44:51 +0800655 VkResult result = instance.drv.dispatch.AllocateCommandBuffers(
656 vkdevice, alloc_info, cmdbufs);
657 if (result == VK_SUCCESS) {
658 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++)
659 driver::SetData(cmdbufs[i], device.base);
660 }
661
662 return result;
Jesse Hall04f4f472015-08-16 19:51:04 -0700663}
664
Jesse Hall1f91d392015-12-11 16:28:44 -0800665// -----------------------------------------------------------------------------
666
667const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
668 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800669}
670
Jesse Hall1f91d392015-12-11 16:28:44 -0800671const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
672 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800673}
674
Jesse Hall715b86a2016-01-16 16:34:29 -0800675VkInstance GetDriverInstance(VkInstance instance) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800676 return instance;
Jesse Hall715b86a2016-01-16 16:34:29 -0800677}
678
679const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
680 return GetDispatchParent(instance).drv.dispatch;
681}
682
Jesse Hall1f91d392015-12-11 16:28:44 -0800683const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
684 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700685}
686
Jesse Hall1f91d392015-12-11 16:28:44 -0800687const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
688 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -0700689}
690
Jesse Hall715b86a2016-01-16 16:34:29 -0800691DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
692 return GetDispatchParent(instance).debug_report_callbacks;
693}
694
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800695bool InitLoader(hwvulkan_device_t* dev) {
696 if (!g_hwdevice) {
697 g_hwdevice = dev;
698 if (!LoadVulkanHAL())
699 g_hwdevice = nullptr;
700 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800701
702 return (g_hwdevice != nullptr);
703}
704
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800705namespace driver {
706
Chia-I Wu0c203242016-03-15 13:44:51 +0800707VkResult EnumerateInstanceExtensionProperties(
708 const char* pLayerName,
709 uint32_t* pPropertyCount,
710 VkExtensionProperties* pProperties) {
711 (void)pLayerName;
712
713 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
714 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
715 uint32_t num_extensions = 0;
716
717 available[num_extensions++] = VkExtensionProperties{
718 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
719 available[num_extensions++] =
720 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
721 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
722 if (g_driver_instance_extensions[kEXT_debug_report]) {
723 available[num_extensions++] =
724 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
725 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
726 }
727
728 if (!pProperties || *pPropertyCount > num_extensions)
729 *pPropertyCount = num_extensions;
730 if (pProperties)
731 std::copy(available, available + *pPropertyCount, pProperties);
732
733 return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
734}
735
736} // namespace driver
737
Jesse Hall04f4f472015-08-16 19:51:04 -0700738} // namespace vulkan