blob: 0313f7e87ebf64a74ca2ad4927eb015bbe024556 [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
Jesse Hall1f91d392015-12-11 16:28:44 -0800166template <typename THandle>
167struct HandleTraits {};
168template <>
169struct HandleTraits<VkInstance> {
170 typedef Instance LoaderObjectType;
171};
172template <>
173struct HandleTraits<VkPhysicalDevice> {
174 typedef Instance LoaderObjectType;
175};
Jesse Hall1f91d392015-12-11 16:28:44 -0800176
177template <typename THandle>
178typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
179 THandle handle) {
180 // TODO(jessehall): Make Instance and Device POD types (by removing the
181 // non-default constructors), so that offsetof is actually legal to use.
182 // The specific case we're using here is safe in gcc/clang (and probably
183 // most other C++ compilers), but isn't guaranteed by C++.
184 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
185#pragma clang diagnostic push
186#pragma clang diagnostic ignored "-Winvalid-offsetof"
Chia-I Wu0c203242016-03-15 13:44:51 +0800187 const size_t kBaseOffset = offsetof(ObjectType, base);
Jesse Hall1f91d392015-12-11 16:28:44 -0800188#pragma clang diagnostic pop
189
Chia-I Wu0c203242016-03-15 13:44:51 +0800190 const auto& base = driver::GetData(handle);
191 uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base);
192 uintptr_t object_addr = base_addr - kBaseOffset;
Jesse Hall1f91d392015-12-11 16:28:44 -0800193 return *reinterpret_cast<ObjectType*>(object_addr);
194}
195
196// -----------------------------------------------------------------------------
197
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700198/*
199 * This function will return the pNext pointer of any
200 * CreateInfo extensions that are not loader extensions.
201 * This is used to skip past the loader extensions prepended
202 * to the list during CreateInstance and CreateDevice.
203 */
204void* StripCreateExtensions(const void* pNext) {
205 VkLayerInstanceCreateInfo* create_info =
206 const_cast<VkLayerInstanceCreateInfo*>(
207 static_cast<const VkLayerInstanceCreateInfo*>(pNext));
208
209 while (
210 create_info &&
211 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
212 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
213 create_info = const_cast<VkLayerInstanceCreateInfo*>(
214 static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
215 }
216
217 return create_info;
218}
219
Jesse Hallfee71432016-03-05 22:27:02 -0800220// Clean up and deallocate an Instance; called from both the failure paths in
221// CreateInstance_Top as well as from DestroyInstance_Top. This function does
222// not call down the dispatch chain; that should be done before calling this
223// function, iff the lower vkCreateInstance call has been made and returned
224// successfully.
225void DestroyInstance(Instance* instance,
Chia-I Wu0c203242016-03-15 13:44:51 +0800226 const VkAllocationCallbacks* allocator,
227 VkInstance vkinstance) {
Chia-I Wucc5e2762016-03-24 13:01:16 +0800228 if (vkinstance != VK_NULL_HANDLE && instance->base.driver.DestroyInstance)
229 instance->base.driver.DestroyInstance(vkinstance, allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +0800230
Jesse Hallfee71432016-03-05 22:27:02 -0800231 instance->~Instance();
232 allocator->pfnFree(allocator->pUserData, instance);
Courtney Goeltzenleuchtere6e69682016-01-28 17:26:17 -0700233}
234
Chia-I Wueb7db122016-03-24 09:11:06 +0800235driver::ProcHook::Extension InstanceExtensionToProcHookExtension(
236 InstanceExtension id) {
237 switch (id) {
238 case kKHR_surface:
239 return driver::ProcHook::KHR_surface;
240 case kKHR_android_surface:
241 return driver::ProcHook::KHR_android_surface;
242 case kEXT_debug_report:
243 return driver::ProcHook::EXT_debug_report;
244 default:
245 return driver::ProcHook::EXTENSION_UNKNOWN;
246 }
247}
248
249driver::ProcHook::Extension DeviceExtensionToProcHookExtension(
250 DeviceExtension id) {
251 switch (id) {
252 case kKHR_swapchain:
253 return driver::ProcHook::KHR_swapchain;
254 case kANDROID_native_buffer:
255 return driver::ProcHook::ANDROID_native_buffer;
256 default:
257 return driver::ProcHook::EXTENSION_UNKNOWN;
258 }
259}
260
Jesse Hall1f91d392015-12-11 16:28:44 -0800261} // anonymous namespace
262
263namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500264
Jesse Hall04f4f472015-08-16 19:51:04 -0700265// -----------------------------------------------------------------------------
266// "Bottom" functions. These are called at the end of the instance dispatch
267// chain.
268
Jesse Hall1f91d392015-12-11 16:28:44 -0800269VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
270 const VkAllocationCallbacks* allocator,
271 VkInstance* vkinstance) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700272 VkResult result;
273
Chia-I Wu0c203242016-03-15 13:44:51 +0800274 if (!allocator)
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800275 allocator = &driver::GetDefaultAllocator();
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700276
Chia-I Wu0c203242016-03-15 13:44:51 +0800277 void* instance_mem = allocator->pfnAllocation(
278 allocator->pUserData, sizeof(Instance), alignof(Instance),
279 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
280 if (!instance_mem)
281 return VK_ERROR_OUT_OF_HOST_MEMORY;
282 Instance& instance = *new (instance_mem) Instance(allocator);
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700283
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800284 // Check that all enabled extensions are supported
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800285 uint32_t num_driver_extensions = 0;
Chia-I Wueb7db122016-03-24 09:11:06 +0800286 bool enable_kEXT_debug_report = false;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800287 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
288 const char* name = create_info->ppEnabledExtensionNames[i];
289 InstanceExtension id = InstanceExtensionFromName(name);
290 if (id != kInstanceExtensionCount) {
291 if (g_driver_instance_extensions[id]) {
292 num_driver_extensions++;
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600293 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800294 continue;
295 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700296 if (id == kKHR_surface || id == kKHR_android_surface) {
Courtney Goeltzenleuchtera76e8ff2016-03-22 08:09:58 -0600297 instance.enabled_extensions.set(id);
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800298 continue;
299 }
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700300 // The loader natively supports debug report.
301 if (id == kEXT_debug_report) {
Chia-I Wueb7db122016-03-24 09:11:06 +0800302 enable_kEXT_debug_report = true;
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700303 continue;
304 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800305 }
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800306 }
307
Chia-I Wueb7db122016-03-24 09:11:06 +0800308 auto& hal_exts = instance.base.hal_extensions;
309 for (size_t i = 0; i < instance.enabled_extensions.size(); i++) {
310 if (instance.enabled_extensions[i]) {
311 auto bit = InstanceExtensionToProcHookExtension(
312 static_cast<InstanceExtension>(i));
313 if (bit != driver::ProcHook::EXTENSION_UNKNOWN)
314 hal_exts.set(bit);
315 }
316 }
317
318 auto& hook_exts = instance.base.hook_extensions;
319 hook_exts = hal_exts;
320 if (enable_kEXT_debug_report)
321 hook_exts.set(driver::ProcHook::EXT_debug_report);
322
Jesse Halla7ac76d2016-01-08 22:29:42 -0800323 VkInstanceCreateInfo driver_create_info = *create_info;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700324 driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800325 driver_create_info.enabledLayerCount = 0;
326 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800327 driver_create_info.enabledExtensionCount = 0;
328 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800329 if (num_driver_extensions > 0) {
330 const char** names = static_cast<const char**>(
331 alloca(num_driver_extensions * sizeof(char*)));
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800332 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
Jesse Hallae3b70d2016-01-17 22:05:29 -0800333 const char* name = create_info->ppEnabledExtensionNames[i];
334 InstanceExtension id = InstanceExtensionFromName(name);
335 if (id != kInstanceExtensionCount) {
336 if (g_driver_instance_extensions[id]) {
337 names[driver_create_info.enabledExtensionCount++] = name;
Jesse Hallae3b70d2016-01-17 22:05:29 -0800338 continue;
339 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800340 }
341 }
342 driver_create_info.ppEnabledExtensionNames = names;
Jesse Hall4b62e4f2016-01-21 09:49:49 -0800343 ALOG_ASSERT(
344 driver_create_info.enabledExtensionCount == num_driver_extensions,
345 "counted enabled driver instance extensions twice and got "
346 "different answers!");
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800347 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800348
Chia-I Wu0c203242016-03-15 13:44:51 +0800349 VkInstance drv_instance;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800350 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Chia-I Wu0c203242016-03-15 13:44:51 +0800351 &drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700352 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800353 DestroyInstance(&instance, allocator, VK_NULL_HANDLE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700354 return result;
355 }
356
Chia-I Wu0c203242016-03-15 13:44:51 +0800357 if (!driver::SetData(drv_instance, instance.base)) {
358 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700359 return VK_ERROR_INITIALIZATION_FAILED;
360 }
361
Chia-I Wucc5e2762016-03-24 13:01:16 +0800362 if (!driver::InitDriverTable(drv_instance,
363 g_hwdevice->GetInstanceProcAddr)) {
364 DestroyInstance(&instance, allocator, drv_instance);
365 return VK_ERROR_INITIALIZATION_FAILED;
366 }
367
368 instance.base.get_device_proc_addr =
369 reinterpret_cast<PFN_vkGetDeviceProcAddr>(
370 g_hwdevice->GetInstanceProcAddr(drv_instance,
371 "vkGetDeviceProcAddr"));
372 if (!instance.base.get_device_proc_addr) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800373 DestroyInstance(&instance, allocator, drv_instance);
Courtney Goeltzenleuchteraa6c8722016-01-29 08:57:16 -0700374 return VK_ERROR_INITIALIZATION_FAILED;
375 }
376
Jesse Hall04f4f472015-08-16 19:51:04 -0700377 uint32_t num_physical_devices = 0;
Chia-I Wucc5e2762016-03-24 13:01:16 +0800378 result = instance.base.driver.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800379 drv_instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700380 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800381 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700382 return VK_ERROR_INITIALIZATION_FAILED;
383 }
384 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Chia-I Wucc5e2762016-03-24 13:01:16 +0800385 result = instance.base.driver.EnumeratePhysicalDevices(
Chia-I Wu0c203242016-03-15 13:44:51 +0800386 drv_instance, &num_physical_devices, instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700387 if (result != VK_SUCCESS) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800388 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700389 return VK_ERROR_INITIALIZATION_FAILED;
390 }
Jesse Hallb1471272016-01-17 21:36:58 -0800391
392 Vector<VkExtensionProperties> extensions(
393 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700394 for (uint32_t i = 0; i < num_physical_devices; i++) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800395 if (!driver::SetData(instance.physical_devices[i], instance.base)) {
396 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700397 return VK_ERROR_INITIALIZATION_FAILED;
398 }
Jesse Hallb1471272016-01-17 21:36:58 -0800399
400 uint32_t count;
Chia-I Wucc5e2762016-03-24 13:01:16 +0800401 if ((result = instance.base.driver.EnumerateDeviceExtensionProperties(
Jesse Hallb1471272016-01-17 21:36:58 -0800402 instance.physical_devices[i], nullptr, &count, nullptr)) !=
403 VK_SUCCESS) {
404 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
405 result);
406 continue;
407 }
Jesse Hall26cecff2016-01-21 19:52:25 -0800408 try {
409 extensions.resize(count);
410 } catch (std::bad_alloc&) {
411 ALOGE("instance creation failed: out of memory");
Chia-I Wu0c203242016-03-15 13:44:51 +0800412 DestroyInstance(&instance, allocator, drv_instance);
Jesse Hall26cecff2016-01-21 19:52:25 -0800413 return VK_ERROR_OUT_OF_HOST_MEMORY;
414 }
Chia-I Wucc5e2762016-03-24 13:01:16 +0800415 if ((result = instance.base.driver.EnumerateDeviceExtensionProperties(
Jesse Hallb1471272016-01-17 21:36:58 -0800416 instance.physical_devices[i], nullptr, &count,
417 extensions.data())) != VK_SUCCESS) {
418 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
419 result);
420 continue;
421 }
422 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
423 for (const auto& extension : extensions) {
424 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
425 DeviceExtension id =
426 DeviceExtensionFromName(extension.extensionName);
427 if (id == kDeviceExtensionCount) {
428 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
429 extension.extensionName);
430 } else {
431 instance.physical_device_driver_extensions[i].set(id);
432 }
433 }
434 // Ignore driver attempts to support loader extensions
435 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700436 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800437 instance.num_physical_devices = num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800438
Chia-I Wu0c203242016-03-15 13:44:51 +0800439 *vkinstance = drv_instance;
Courtney Goeltzenleuchtera90ce612016-02-08 20:48:05 -0700440
Jesse Hall04f4f472015-08-16 19:51:04 -0700441 return VK_SUCCESS;
442}
443
Jesse Hall1f91d392015-12-11 16:28:44 -0800444VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
445 uint32_t* pdev_count,
446 VkPhysicalDevice* pdevs) {
447 Instance& instance = GetDispatchParent(vkinstance);
448 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700449 if (pdevs) {
450 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800451 std::copy(instance.physical_devices, instance.physical_devices + count,
452 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700453 }
454 *pdev_count = count;
455 return VK_SUCCESS;
456}
457
Jesse Halle1b12782015-11-30 11:27:32 -0800458VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800459VkResult EnumerateDeviceExtensionProperties_Bottom(
Chia-I Wu0c203242016-03-15 13:44:51 +0800460 VkPhysicalDevice pdev,
461 const char* layer_name,
462 uint32_t* properties_count,
463 VkExtensionProperties* properties) {
464 (void)layer_name;
465
466 Instance& instance = GetDispatchParent(pdev);
467
468 size_t gpu_idx = 0;
469 while (instance.physical_devices[gpu_idx] != pdev)
470 gpu_idx++;
471 const DeviceExtensionSet driver_extensions =
472 instance.physical_device_driver_extensions[gpu_idx];
473
474 // We only support VK_KHR_swapchain if the GPU supports
475 // VK_ANDROID_native_buffer
476 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
477 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
478 uint32_t num_extensions = 0;
479 if (driver_extensions[kANDROID_native_buffer]) {
480 available[num_extensions++] = VkExtensionProperties{
481 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
482 }
483
484 if (!properties || *properties_count > num_extensions)
485 *properties_count = num_extensions;
486 if (properties)
487 std::copy(available, available + *properties_count, properties);
488
489 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700490}
491
Jesse Hall1f91d392015-12-11 16:28:44 -0800492void DestroyInstance_Bottom(VkInstance vkinstance,
493 const VkAllocationCallbacks* allocator) {
494 Instance& instance = GetDispatchParent(vkinstance);
495
Chia-I Wu0c203242016-03-15 13:44:51 +0800496 VkAllocationCallbacks local_allocator;
497 if (!allocator) {
498 local_allocator = *instance.alloc;
499 allocator = &local_allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800500 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800501
502 DestroyInstance(&instance, allocator, vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700503}
504
Jesse Hall1f91d392015-12-11 16:28:44 -0800505// -----------------------------------------------------------------------------
506
507const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
508 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800509}
510
Jesse Hall1f91d392015-12-11 16:28:44 -0800511const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800512 return &driver::GetData(vkdevice).allocator;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800513}
514
Jesse Hall715b86a2016-01-16 16:34:29 -0800515VkInstance GetDriverInstance(VkInstance instance) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800516 return instance;
Jesse Hall715b86a2016-01-16 16:34:29 -0800517}
518
Chia-I Wucc5e2762016-03-24 13:01:16 +0800519const driver::InstanceDriverTable& GetDriverDispatch(VkInstance instance) {
520 return driver::GetData(instance).driver;
Jesse Hall715b86a2016-01-16 16:34:29 -0800521}
522
Chia-I Wucc5e2762016-03-24 13:01:16 +0800523const driver::DeviceDriverTable& GetDriverDispatch(VkDevice device) {
524 return driver::GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700525}
526
Chia-I Wucc5e2762016-03-24 13:01:16 +0800527const driver::DeviceDriverTable& GetDriverDispatch(VkQueue queue) {
528 return driver::GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700529}
530
Jesse Hall715b86a2016-01-16 16:34:29 -0800531DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
532 return GetDispatchParent(instance).debug_report_callbacks;
533}
534
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800535bool InitLoader(hwvulkan_device_t* dev) {
536 if (!g_hwdevice) {
537 g_hwdevice = dev;
538 if (!LoadVulkanHAL())
539 g_hwdevice = nullptr;
540 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800541
542 return (g_hwdevice != nullptr);
543}
544
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800545namespace driver {
546
Chia-I Wu0c203242016-03-15 13:44:51 +0800547VkResult EnumerateInstanceExtensionProperties(
548 const char* pLayerName,
549 uint32_t* pPropertyCount,
550 VkExtensionProperties* pProperties) {
551 (void)pLayerName;
552
553 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
554 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
555 uint32_t num_extensions = 0;
556
557 available[num_extensions++] = VkExtensionProperties{
558 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
559 available[num_extensions++] =
560 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
561 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
562 if (g_driver_instance_extensions[kEXT_debug_report]) {
563 available[num_extensions++] =
564 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
565 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
566 }
567
568 if (!pProperties || *pPropertyCount > num_extensions)
569 *pPropertyCount = num_extensions;
570 if (pProperties)
571 std::copy(available, available + *pPropertyCount, pProperties);
572
573 return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
574}
575
576} // namespace driver
577
Jesse Hall04f4f472015-08-16 19:51:04 -0700578} // namespace vulkan