blob: b27d1b7cb64eae7f3300e2e53d9c16e27299d755 [file] [log] [blame]
Chia-I Wu9d518162016-03-24 14:55:27 +08001/*
2 * Copyright 2016 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
Mark Salyzyn7823e122016-09-29 08:08:05 -070017#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080018#include <stdlib.h>
19#include <string.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070020#include <sys/prctl.h>
21
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -080022#include <dlfcn.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <algorithm>
Chia-I Wuff4a6c72016-03-24 16:05:56 +080024#include <array>
Chia-I Wu4901db72016-03-24 16:38:58 +080025#include <new>
Mark Salyzyn7823e122016-09-29 08:08:05 -070026
27#include <log/log.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080028
Jesse Hall53457db2016-12-14 16:54:06 -080029#include <android/dlext.h>
30#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Chris Forbesfa25e632017-02-22 12:36:02 +130032#include <utils/Vector.h>
Jesse Hall53457db2016-12-14 16:54:06 -080033
Wei Wangf9b05ee2017-07-19 20:59:39 -070034#include "android-base/properties.h"
35
Chia-I Wu9d518162016-03-24 14:55:27 +080036#include "driver.h"
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070037#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080038
Jesse Hall00e61ff2017-04-07 16:48:02 -070039// TODO(b/37049319) Get this from a header once one exists
40extern "C" {
41android_namespace_t* android_get_exported_namespace(const char*);
42}
43
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080044// #define ENABLE_ALLOC_CALLSTACKS 1
45#if ENABLE_ALLOC_CALLSTACKS
46#include <utils/CallStack.h>
47#define ALOGD_CALLSTACK(...) \
48 do { \
49 ALOGD(__VA_ARGS__); \
50 android::CallStack callstack; \
51 callstack.update(); \
52 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
53 } while (false)
54#else
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 } while (false)
58#endif
59
Chia-I Wu9d518162016-03-24 14:55:27 +080060namespace vulkan {
61namespace driver {
62
Chia-I Wu136b8eb2016-03-24 15:01:52 +080063namespace {
64
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080065class Hal {
66 public:
67 static bool Open();
68
69 static const Hal& Get() { return hal_; }
70 static const hwvulkan_device_t& Device() { return *Get().dev_; }
71
Chia-I Wu31938252016-05-23 15:31:02 +080072 int GetDebugReportIndex() const { return debug_report_index_; }
73
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080074 private:
Chia-I Wu31938252016-05-23 15:31:02 +080075 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080076 Hal(const Hal&) = delete;
77 Hal& operator=(const Hal&) = delete;
78
Chia-I Wu31938252016-05-23 15:31:02 +080079 bool InitDebugReportIndex();
80
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 static Hal hal_;
82
83 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080084 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080085};
86
Chia-I Wu4901db72016-03-24 16:38:58 +080087class CreateInfoWrapper {
88 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080090 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080091 CreateInfoWrapper(VkPhysicalDevice physical_dev,
92 const VkDeviceCreateInfo& create_info,
93 const VkAllocationCallbacks& allocator);
94 ~CreateInfoWrapper();
95
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080096 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -060097 void DowngradeApiVersion();
Chia-I Wu4901db72016-03-24 16:38:58 +080098
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080099 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
100 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800101
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800102 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800103 explicit operator const VkDeviceCreateInfo*() const;
104
105 private:
106 struct ExtensionFilter {
107 VkExtensionProperties* exts;
108 uint32_t ext_count;
109
110 const char** names;
111 uint32_t name_count;
112 };
113
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800114 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800115
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800116 VkResult SanitizeLayers();
117 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800118
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800119 VkResult QueryExtensionCount(uint32_t& count) const;
120 VkResult EnumerateExtensions(uint32_t& count,
121 VkExtensionProperties* props) const;
122 VkResult InitExtensionFilter();
123 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800124
125 const bool is_instance_;
126 const VkAllocationCallbacks& allocator_;
127
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800128 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800129
130 union {
131 VkInstanceCreateInfo instance_info_;
132 VkDeviceCreateInfo dev_info_;
133 };
134
Ian Elliottf3e872d2017-11-02 10:15:13 -0600135 VkApplicationInfo application_info_;
136
Chia-I Wu4901db72016-03-24 16:38:58 +0800137 ExtensionFilter extension_filter_;
138
139 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
140 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
141};
142
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800143Hal Hal::hal_;
144
Jesse Hall53457db2016-12-14 16:54:06 -0800145void* LoadLibrary(const android_dlextinfo& dlextinfo,
146 const char* subname,
147 int subname_len) {
148 const char kLibFormat[] = "vulkan.%*s.so";
149 char* name = static_cast<char*>(
150 alloca(sizeof(kLibFormat) + static_cast<size_t>(subname_len)));
151 sprintf(name, kLibFormat, subname_len, subname);
152 return android_dlopen_ext(name, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
153}
154
155const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
156 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
157 "ro.board.platform",
158}};
159
Jesse Hall00e61ff2017-04-07 16:48:02 -0700160int LoadDriver(android_namespace_t* library_namespace,
161 const hwvulkan_module_t** module) {
Jesse Hall53457db2016-12-14 16:54:06 -0800162 const android_dlextinfo dlextinfo = {
163 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700164 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800165 };
Jesse Hall53457db2016-12-14 16:54:06 -0800166 void* so = nullptr;
167 char prop[PROPERTY_VALUE_MAX];
168 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
169 int prop_len = property_get(key, prop, nullptr);
170 if (prop_len > 0) {
171 so = LoadLibrary(dlextinfo, prop, prop_len);
172 if (so)
173 break;
174 }
175 }
176 if (!so)
177 return -ENOENT;
178
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800180 if (!hmi) {
181 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
182 dlclose(so);
183 return -EINVAL;
184 }
185 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
186 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
187 dlclose(so);
188 return -EINVAL;
189 }
190 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700191 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800192 return 0;
193}
194
Jesse Hall00e61ff2017-04-07 16:48:02 -0700195int LoadBuiltinDriver(const hwvulkan_module_t** module) {
196 auto ns = android_get_exported_namespace("sphal");
197 if (!ns)
198 return -ENOENT;
199 return LoadDriver(ns, module);
200}
201
202int LoadUpdatedDriver(const hwvulkan_module_t** module) {
203 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
204 if (!ns)
205 return -ENOENT;
206 return LoadDriver(ns, module);
207}
208
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800209bool Hal::Open() {
Jesse Halldc225072016-05-30 22:40:14 -0700210 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800211
212 // Use a stub device unless we successfully open a real HAL device.
213 hal_.dev_ = &stubhal::kDevice;
214
Jesse Hall53457db2016-12-14 16:54:06 -0800215 int result;
216 const hwvulkan_module_t* module = nullptr;
217
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800219 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700220 result = LoadBuiltinDriver(&module);
221 if (result != 0) {
222 // -ENOENT means the sphal namespace doesn't exist, not that there
223 // is a problem with the driver.
224 ALOGW_IF(
225 result != -ENOENT,
226 "Failed to load Vulkan driver into sphal namespace. This "
227 "usually means the driver has forbidden library dependencies."
228 "Please fix, this will soon stop working.");
229 result =
230 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
231 reinterpret_cast<const hw_module_t**>(&module));
232 }
Jesse Hall53457db2016-12-14 16:54:06 -0800233 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800234 if (result != 0) {
Jesse Hall53457db2016-12-14 16:54:06 -0800235 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800236 return true;
237 }
238
239 hwvulkan_device_t* device;
240 result =
241 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
242 reinterpret_cast<hw_device_t**>(&device));
243 if (result != 0) {
244 // Any device with a Vulkan HAL should be able to open the device.
245 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
246 result);
247 return false;
248 }
249
250 hal_.dev_ = device;
251
Chia-I Wu31938252016-05-23 15:31:02 +0800252 hal_.InitDebugReportIndex();
253
254 return true;
255}
256
257bool Hal::InitDebugReportIndex() {
258 uint32_t count;
259 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
260 VK_SUCCESS) {
261 ALOGE("failed to get HAL instance extension count");
262 return false;
263 }
264
265 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
266 malloc(sizeof(VkExtensionProperties) * count));
267 if (!exts) {
268 ALOGE("failed to allocate HAL instance extension array");
269 return false;
270 }
271
272 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
273 VK_SUCCESS) {
274 ALOGE("failed to enumerate HAL instance extensions");
275 free(exts);
276 return false;
277 }
278
279 for (uint32_t i = 0; i < count; i++) {
280 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
281 0) {
282 debug_report_index_ = static_cast<int>(i);
283 break;
284 }
285 }
286
287 free(exts);
288
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800289 return true;
290}
291
292CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800293 const VkAllocationCallbacks& allocator)
294 : is_instance_(true),
295 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800296 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800297 instance_info_(create_info),
298 extension_filter_() {
299 hook_extensions_.set(ProcHook::EXTENSION_CORE);
300 hal_extensions_.set(ProcHook::EXTENSION_CORE);
301}
302
Chia-I Wu4901db72016-03-24 16:38:58 +0800303CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
304 const VkDeviceCreateInfo& create_info,
305 const VkAllocationCallbacks& allocator)
306 : is_instance_(false),
307 allocator_(allocator),
308 physical_dev_(physical_dev),
309 dev_info_(create_info),
310 extension_filter_() {
311 hook_extensions_.set(ProcHook::EXTENSION_CORE);
312 hal_extensions_.set(ProcHook::EXTENSION_CORE);
313}
314
315CreateInfoWrapper::~CreateInfoWrapper() {
316 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
317 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
318}
319
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800320VkResult CreateInfoWrapper::Validate() {
321 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800322 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800323 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800324 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800325 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800326
327 return result;
328}
329
330const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800331CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800332 return hook_extensions_;
333}
334
335const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800336CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800337 return hal_extensions_;
338}
339
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800340CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
341 return &instance_info_;
342}
343
Chia-I Wu4901db72016-03-24 16:38:58 +0800344CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
345 return &dev_info_;
346}
347
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800348VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800349 const struct StructHeader {
350 VkStructureType type;
351 const void* next;
352 } * header;
353
354 if (is_instance_) {
355 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
356
357 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
358 while (header &&
359 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
360 header = reinterpret_cast<const StructHeader*>(header->next);
361
362 instance_info_.pNext = header;
363 } else {
364 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
365
366 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
367 while (header &&
368 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
369 header = reinterpret_cast<const StructHeader*>(header->next);
370
371 dev_info_.pNext = header;
372 }
373
374 return VK_SUCCESS;
375}
376
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800377VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800378 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
379 : dev_info_.ppEnabledLayerNames;
380 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
381 : dev_info_.enabledLayerCount;
382
383 // remove all layers
384 layer_names = nullptr;
385 layer_count = 0;
386
387 return VK_SUCCESS;
388}
389
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800390VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800391 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
392 : dev_info_.ppEnabledExtensionNames;
393 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
394 : dev_info_.enabledExtensionCount;
395 if (!ext_count)
396 return VK_SUCCESS;
397
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800398 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800399 if (result != VK_SUCCESS)
400 return result;
401
402 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800403 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800404
405 ext_names = extension_filter_.names;
406 ext_count = extension_filter_.name_count;
407
408 return VK_SUCCESS;
409}
410
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800411VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800412 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800413 return Hal::Device().EnumerateInstanceExtensionProperties(
414 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800415 } else {
416 const auto& driver = GetData(physical_dev_).driver;
417 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
418 &count, nullptr);
419 }
420}
421
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800422VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800423 uint32_t& count,
424 VkExtensionProperties* props) const {
425 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800426 return Hal::Device().EnumerateInstanceExtensionProperties(
427 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800428 } else {
429 const auto& driver = GetData(physical_dev_).driver;
430 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
431 &count, props);
432 }
433}
434
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800435VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800436 // query extension count
437 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800438 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800439 if (result != VK_SUCCESS || count == 0)
440 return result;
441
442 auto& filter = extension_filter_;
443 filter.exts =
444 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
445 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
446 alignof(VkExtensionProperties),
447 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
448 if (!filter.exts)
449 return VK_ERROR_OUT_OF_HOST_MEMORY;
450
451 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800452 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800453 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
454 return result;
455
456 if (!count)
457 return VK_SUCCESS;
458
459 filter.ext_count = count;
460
461 // allocate name array
462 uint32_t enabled_ext_count = (is_instance_)
463 ? instance_info_.enabledExtensionCount
464 : dev_info_.enabledExtensionCount;
465 count = std::min(filter.ext_count, enabled_ext_count);
466 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
467 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
468 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
469 if (!filter.names)
470 return VK_ERROR_OUT_OF_HOST_MEMORY;
471
472 return VK_SUCCESS;
473}
474
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800475void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800476 auto& filter = extension_filter_;
477
478 ProcHook::Extension ext_bit = GetProcHookExtension(name);
479 if (is_instance_) {
480 switch (ext_bit) {
481 case ProcHook::KHR_android_surface:
482 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700483 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300484 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800485 hook_extensions_.set(ext_bit);
486 // return now as these extensions do not require HAL support
487 return;
488 case ProcHook::EXT_debug_report:
489 // both we and HAL can take part in
490 hook_extensions_.set(ext_bit);
491 break;
492 case ProcHook::EXTENSION_UNKNOWN:
Chris Forbes6aa30db2017-02-20 17:12:53 +1300493 case ProcHook::KHR_get_physical_device_properties2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800494 // HAL's extensions
495 break;
496 default:
497 ALOGW("Ignored invalid instance extension %s", name);
498 return;
499 }
500 } else {
501 switch (ext_bit) {
502 case ProcHook::KHR_swapchain:
503 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
504 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
505 ext_bit = ProcHook::ANDROID_native_buffer;
506 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700507 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700508 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300509 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700510 hook_extensions_.set(ext_bit);
511 // return now as these extensions do not require HAL support
512 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700513 case ProcHook::EXT_hdr_metadata:
514 hook_extensions_.set(ext_bit);
515 break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800516 case ProcHook::EXTENSION_UNKNOWN:
517 // HAL's extensions
518 break;
519 default:
520 ALOGW("Ignored invalid device extension %s", name);
521 return;
522 }
523 }
524
525 for (uint32_t i = 0; i < filter.ext_count; i++) {
526 const VkExtensionProperties& props = filter.exts[i];
527 // ignore unknown extensions
528 if (strcmp(name, props.extensionName) != 0)
529 continue;
530
Chia-I Wu4901db72016-03-24 16:38:58 +0800531 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800532 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
533 if (ext_bit == ProcHook::ANDROID_native_buffer)
534 hook_extensions_.set(ProcHook::KHR_swapchain);
535
536 hal_extensions_.set(ext_bit);
537 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800538
539 break;
540 }
541}
542
Ian Elliottf3e872d2017-11-02 10:15:13 -0600543void CreateInfoWrapper::DowngradeApiVersion() {
544 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
545 if (instance_info_.pApplicationInfo) {
546 application_info_ = *instance_info_.pApplicationInfo;
547 instance_info_.pApplicationInfo = &application_info_;
548 application_info_.apiVersion = VK_API_VERSION_1_0;
549 }
550}
551
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800552VKAPI_ATTR void* DefaultAllocate(void*,
553 size_t size,
554 size_t alignment,
555 VkSystemAllocationScope) {
556 void* ptr = nullptr;
557 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
558 // additionally requires that it be at least sizeof(void*).
559 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
560 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
561 ret, ptr);
562 return ret == 0 ? ptr : nullptr;
563}
564
565VKAPI_ATTR void* DefaultReallocate(void*,
566 void* ptr,
567 size_t size,
568 size_t alignment,
569 VkSystemAllocationScope) {
570 if (size == 0) {
571 free(ptr);
572 return nullptr;
573 }
574
575 // TODO(jessehall): Right now we never shrink allocations; if the new
576 // request is smaller than the existing chunk, we just continue using it.
577 // Right now the loader never reallocs, so this doesn't matter. If that
578 // changes, or if this code is copied into some other project, this should
579 // probably have a heuristic to allocate-copy-free when doing so will save
580 // "enough" space.
581 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
582 if (size <= old_size)
583 return ptr;
584
585 void* new_ptr = nullptr;
586 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
587 return nullptr;
588 if (ptr) {
589 memcpy(new_ptr, ptr, std::min(old_size, size));
590 free(ptr);
591 }
592 return new_ptr;
593}
594
595VKAPI_ATTR void DefaultFree(void*, void* ptr) {
596 ALOGD_CALLSTACK("Free: %p", ptr);
597 free(ptr);
598}
599
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800600InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
601 void* data_mem = allocator.pfnAllocation(
602 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
603 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
604 if (!data_mem)
605 return nullptr;
606
607 return new (data_mem) InstanceData(allocator);
608}
609
610void FreeInstanceData(InstanceData* data,
611 const VkAllocationCallbacks& allocator) {
612 data->~InstanceData();
613 allocator.pfnFree(allocator.pUserData, data);
614}
615
Chia-I Wu950d6e12016-05-03 09:12:35 +0800616DeviceData* AllocateDeviceData(
617 const VkAllocationCallbacks& allocator,
618 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800619 void* data_mem = allocator.pfnAllocation(
620 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
621 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
622 if (!data_mem)
623 return nullptr;
624
Chia-I Wu950d6e12016-05-03 09:12:35 +0800625 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800626}
627
628void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
629 data->~DeviceData();
630 allocator.pfnFree(allocator.pUserData, data);
631}
632
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800633} // anonymous namespace
634
Chia-I Wu9d518162016-03-24 14:55:27 +0800635bool Debuggable() {
636 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
637}
638
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800639bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800640 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800641}
642
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800643const VkAllocationCallbacks& GetDefaultAllocator() {
644 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
645 .pUserData = nullptr,
646 .pfnAllocation = DefaultAllocate,
647 .pfnReallocation = DefaultReallocate,
648 .pfnFree = DefaultFree,
649 };
650
651 return kDefaultAllocCallbacks;
652}
653
Chia-I Wueb7db122016-03-24 09:11:06 +0800654PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
655 const ProcHook* hook = GetProcHook(pName);
656 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800657 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800658
659 if (!instance) {
660 if (hook->type == ProcHook::GLOBAL)
661 return hook->proc;
662
Chia-I Wu109f8982016-04-22 06:40:40 +0800663 // v0 layers expect
664 //
665 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
666 //
667 // to work.
668 if (strcmp(pName, "vkCreateDevice") == 0)
669 return hook->proc;
670
Chia-I Wueb7db122016-03-24 09:11:06 +0800671 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800672 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800673 pName);
674
Chia-I Wu109f8982016-04-22 06:40:40 +0800675 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800676 }
677
678 PFN_vkVoidFunction proc;
679
680 switch (hook->type) {
681 case ProcHook::INSTANCE:
682 proc = (GetData(instance).hook_extensions[hook->extension])
683 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800684 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800685 break;
686 case ProcHook::DEVICE:
687 proc = (hook->extension == ProcHook::EXTENSION_CORE)
688 ? hook->proc
689 : hook->checked_proc;
690 break;
691 default:
692 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800693 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800694 pName);
695 proc = nullptr;
696 break;
697 }
698
699 return proc;
700}
701
702PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
703 const ProcHook* hook = GetProcHook(pName);
704 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800705 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800706
707 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800708 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800709 return nullptr;
710 }
711
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800712 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
713 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800714}
715
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800716VkResult EnumerateInstanceExtensionProperties(
717 const char* pLayerName,
718 uint32_t* pPropertyCount,
719 VkExtensionProperties* pProperties) {
Ian Elliott34a327b2017-03-28 13:20:35 -0600720
721 android::Vector<VkExtensionProperties> loader_extensions;
722 loader_extensions.push_back({
723 VK_KHR_SURFACE_EXTENSION_NAME,
724 VK_KHR_SURFACE_SPEC_VERSION});
725 loader_extensions.push_back({
726 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
727 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
728 loader_extensions.push_back({
729 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
730 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700731 loader_extensions.push_back({
732 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
733 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600734
Chia-I Wu31938252016-05-23 15:31:02 +0800735 static const VkExtensionProperties loader_debug_report_extension = {
736 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
737 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800738
739 // enumerate our extensions first
740 if (!pLayerName && pProperties) {
741 uint32_t count = std::min(
742 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
743
744 std::copy_n(loader_extensions.begin(), count, pProperties);
745
746 if (count < loader_extensions.size()) {
747 *pPropertyCount = count;
748 return VK_INCOMPLETE;
749 }
750
751 pProperties += count;
752 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800753
754 if (Hal::Get().GetDebugReportIndex() < 0) {
755 if (!*pPropertyCount) {
756 *pPropertyCount = count;
757 return VK_INCOMPLETE;
758 }
759
760 pProperties[0] = loader_debug_report_extension;
761 pProperties += 1;
762 *pPropertyCount -= 1;
763 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800764 }
765
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800766 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800767 pLayerName, pPropertyCount, pProperties);
768
Chia-I Wu31938252016-05-23 15:31:02 +0800769 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
770 int idx = Hal::Get().GetDebugReportIndex();
771 if (idx < 0) {
772 *pPropertyCount += 1;
773 } else if (pProperties &&
774 static_cast<uint32_t>(idx) < *pPropertyCount) {
775 pProperties[idx].specVersion =
776 std::min(pProperties[idx].specVersion,
777 loader_debug_report_extension.specVersion);
778 }
779
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800780 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800781 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800782
783 return result;
784}
785
Chris Forbesfa25e632017-02-22 12:36:02 +1300786bool QueryPresentationProperties(
787 VkPhysicalDevice physicalDevice,
788 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties)
789{
790 const InstanceData& data = GetData(physicalDevice);
791
792 // GPDP2 must be present and enabled on the instance.
793 if (!data.driver.GetPhysicalDeviceProperties2KHR)
794 return false;
795
796 // Request the android-specific presentation properties via GPDP2
797 VkPhysicalDeviceProperties2KHR properties = {
798 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
799 presentation_properties,
800 {}
801 };
802
803#pragma clang diagnostic push
804#pragma clang diagnostic ignored "-Wold-style-cast"
805 presentation_properties->sType =
806 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
807#pragma clang diagnostic pop
808 presentation_properties->pNext = nullptr;
809 presentation_properties->sharedImage = VK_FALSE;
810
811 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
812 &properties);
813
814 return true;
815}
816
Chia-I Wu01cf3052016-03-24 16:16:21 +0800817VkResult EnumerateDeviceExtensionProperties(
818 VkPhysicalDevice physicalDevice,
819 const char* pLayerName,
820 uint32_t* pPropertyCount,
821 VkExtensionProperties* pProperties) {
822 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300823 // extensions that are unconditionally exposed by the loader
824 android::Vector<VkExtensionProperties> loader_extensions;
825 loader_extensions.push_back({
826 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
827 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300828
Chris Forbes16095002017-05-05 15:33:29 -0700829 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
830 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
831 presentation_properties.sharedImage) {
832 loader_extensions.push_back({
833 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
834 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300835 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700836
Ian Elliott5c34de22017-04-10 14:42:30 -0600837 // conditionally add VK_GOOGLE_display_timing if present timestamps are
838 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700839 const std::string timestamp_property("service.sf.present_timestamp");
840 android::base::WaitForPropertyCreation(timestamp_property);
841 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600842 loader_extensions.push_back({
843 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
844 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
845 }
846
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700847 // enumerate our extensions first
848 if (!pLayerName && pProperties) {
849 uint32_t count = std::min(
850 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
851
852 std::copy_n(loader_extensions.begin(), count, pProperties);
853
854 if (count < loader_extensions.size()) {
855 *pPropertyCount = count;
856 return VK_INCOMPLETE;
857 }
858
859 pProperties += count;
860 *pPropertyCount -= count;
861 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800862
863 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
864 physicalDevice, pLayerName, pPropertyCount, pProperties);
Chia-I Wu01cf3052016-03-24 16:16:21 +0800865
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700866 if (pProperties) {
867 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
868 for (uint32_t i = 0; i < *pPropertyCount; i++) {
869 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800870
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700871 if (strcmp(prop.extensionName,
872 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
873 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +0800874
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700875 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
876 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
877 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
878 }
879 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800880
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700881 // restore loader extension count
882 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
883 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800884 }
885
886 return result;
887}
888
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800889VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
890 const VkAllocationCallbacks* pAllocator,
891 VkInstance* pInstance) {
892 const VkAllocationCallbacks& data_allocator =
893 (pAllocator) ? *pAllocator : GetDefaultAllocator();
894
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800895 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800896 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800897 if (result != VK_SUCCESS)
898 return result;
899
900 InstanceData* data = AllocateInstanceData(data_allocator);
901 if (!data)
902 return VK_ERROR_OUT_OF_HOST_MEMORY;
903
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800904 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800905
Ian Elliottf3e872d2017-11-02 10:15:13 -0600906#pragma clang diagnostic push
907#pragma clang diagnostic ignored "-Wold-style-cast"
908 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
909 ? pCreateInfo->pApplicationInfo->apiVersion
910 : VK_API_VERSION_1_0);
911 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
912 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
913 uint32_t icd_api_version;
914 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
915 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
916 Hal::Device().GetInstanceProcAddr(NULL,
917 "vkEnumerateInstanceVersion"));
918 if (!pfn_enumerate_instance_version) {
919 icd_api_version = VK_API_VERSION_1_0;
920 } else {
921 result = (*pfn_enumerate_instance_version)(&icd_api_version);
922 }
923 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
924 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
925
926 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
927 ((api_major_version > 1) || (api_minor_version > 0))) {
928 api_version = VK_API_VERSION_1_0;
929 wrapper.DowngradeApiVersion();
930 }
931#pragma clang diagnostic pop
932
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800933 // call into the driver
934 VkInstance instance;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800935 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800936 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
937 &instance);
938 if (result != VK_SUCCESS) {
939 FreeInstanceData(data, data_allocator);
940 return result;
941 }
942
943 // initialize InstanceDriverTable
944 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800945 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +0800946 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800947 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800948 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800949 if (data->driver.DestroyInstance)
950 data->driver.DestroyInstance(instance, pAllocator);
951
952 FreeInstanceData(data, data_allocator);
953
954 return VK_ERROR_INCOMPATIBLE_DRIVER;
955 }
956
957 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800958 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800959 if (!data->get_device_proc_addr) {
960 data->driver.DestroyInstance(instance, pAllocator);
961 FreeInstanceData(data, data_allocator);
962
963 return VK_ERROR_INCOMPATIBLE_DRIVER;
964 }
965
966 *pInstance = instance;
967
968 return VK_SUCCESS;
969}
970
971void DestroyInstance(VkInstance instance,
972 const VkAllocationCallbacks* pAllocator) {
973 InstanceData& data = GetData(instance);
974 data.driver.DestroyInstance(instance, pAllocator);
975
976 VkAllocationCallbacks local_allocator;
977 if (!pAllocator) {
978 local_allocator = data.allocator;
979 pAllocator = &local_allocator;
980 }
981
982 FreeInstanceData(&data, *pAllocator);
983}
984
Chia-I Wu4901db72016-03-24 16:38:58 +0800985VkResult CreateDevice(VkPhysicalDevice physicalDevice,
986 const VkDeviceCreateInfo* pCreateInfo,
987 const VkAllocationCallbacks* pAllocator,
988 VkDevice* pDevice) {
989 const InstanceData& instance_data = GetData(physicalDevice);
990 const VkAllocationCallbacks& data_allocator =
991 (pAllocator) ? *pAllocator : instance_data.allocator;
992
993 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800994 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800995 if (result != VK_SUCCESS)
996 return result;
997
Chia-I Wu950d6e12016-05-03 09:12:35 +0800998 DeviceData* data = AllocateDeviceData(data_allocator,
999 instance_data.debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +08001000 if (!data)
1001 return VK_ERROR_OUT_OF_HOST_MEMORY;
1002
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001003 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001004
1005 // call into the driver
1006 VkDevice dev;
1007 result = instance_data.driver.CreateDevice(
1008 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1009 pAllocator, &dev);
1010 if (result != VK_SUCCESS) {
1011 FreeDeviceData(data, data_allocator);
1012 return result;
1013 }
1014
1015 // initialize DeviceDriverTable
1016 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001017 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1018 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001019 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1020 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1021 if (data->driver.DestroyDevice)
1022 data->driver.DestroyDevice(dev, pAllocator);
1023
1024 FreeDeviceData(data, data_allocator);
1025
1026 return VK_ERROR_INCOMPATIBLE_DRIVER;
1027 }
Chris Forbesd8277912017-02-10 14:59:59 +13001028
1029 // sanity check ANDROID_native_buffer implementation, whose set of
1030 // entrypoints varies according to the spec version.
1031 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1032 !data->driver.GetSwapchainGrallocUsageANDROID &&
1033 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1034 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1035 " must expose at least one of "
1036 "vkGetSwapchainGrallocUsageANDROID or "
1037 "vkGetSwapchainGrallocUsage2ANDROID");
1038
1039 data->driver.DestroyDevice(dev, pAllocator);
1040 FreeDeviceData(data, data_allocator);
1041
1042 return VK_ERROR_INCOMPATIBLE_DRIVER;
1043 }
1044
Jesse Hall85bb0c52017-02-09 22:13:02 -08001045 VkPhysicalDeviceProperties properties;
1046 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1047 &properties);
1048
Jesse Halldc225072016-05-30 22:40:14 -07001049 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001050 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001051
1052 *pDevice = dev;
1053
1054 return VK_SUCCESS;
1055}
1056
1057void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1058 DeviceData& data = GetData(device);
1059 data.driver.DestroyDevice(device, pAllocator);
1060
1061 VkAllocationCallbacks local_allocator;
1062 if (!pAllocator) {
1063 local_allocator = data.allocator;
1064 pAllocator = &local_allocator;
1065 }
1066
1067 FreeDeviceData(&data, *pAllocator);
1068}
1069
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001070VkResult EnumeratePhysicalDevices(VkInstance instance,
1071 uint32_t* pPhysicalDeviceCount,
1072 VkPhysicalDevice* pPhysicalDevices) {
1073 const auto& data = GetData(instance);
1074
1075 VkResult result = data.driver.EnumeratePhysicalDevices(
1076 instance, pPhysicalDeviceCount, pPhysicalDevices);
1077 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1078 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1079 SetData(pPhysicalDevices[i], data);
1080 }
1081
1082 return result;
1083}
1084
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001085VkResult EnumeratePhysicalDeviceGroups(
1086 VkInstance instance,
1087 uint32_t* pPhysicalDeviceGroupCount,
1088 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001089 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001090 const auto& data = GetData(instance);
1091
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001092 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1093 uint32_t device_count = 0;
1094 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1095 if (result < 0)
1096 return result;
1097 if (!pPhysicalDeviceGroupProperties) {
1098 *pPhysicalDeviceGroupCount = device_count;
1099 return result;
1100 }
1101
1102 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1103 if (!device_count) {
1104 *pPhysicalDeviceGroupCount = 0;
1105 return result;
1106 }
1107
1108 android::Vector<VkPhysicalDevice> devices;
1109 devices.resize(device_count);
1110
1111 result = EnumeratePhysicalDevices(instance, &device_count,
1112 devices.editArray());
1113 if (result < 0)
1114 return result;
1115
1116 devices.resize(device_count);
1117 *pPhysicalDeviceGroupCount = device_count;
1118 for (uint32_t i = 0; i < device_count; ++i) {
1119 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1120 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1121 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1122 }
1123 } else {
1124 result = data.driver.EnumeratePhysicalDeviceGroups(
1125 instance, pPhysicalDeviceGroupCount,
1126 pPhysicalDeviceGroupProperties);
1127 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1128 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1129 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1130 for (uint32_t j = 0;
1131 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1132 j++) {
1133 SetData(
1134 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001135 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001136 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001137 }
1138 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001139 }
1140
1141 return result;
1142}
1143
Chia-I Wuba0be412016-03-24 16:24:40 +08001144void GetDeviceQueue(VkDevice device,
1145 uint32_t queueFamilyIndex,
1146 uint32_t queueIndex,
1147 VkQueue* pQueue) {
1148 const auto& data = GetData(device);
1149
1150 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1151 SetData(*pQueue, data);
1152}
1153
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001154void GetDeviceQueue2(VkDevice device,
1155 const VkDeviceQueueInfo2* pQueueInfo,
1156 VkQueue* pQueue) {
1157 const auto& data = GetData(device);
1158
1159 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
1160 SetData(*pQueue, data);
1161}
1162
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001163VKAPI_ATTR VkResult
1164AllocateCommandBuffers(VkDevice device,
1165 const VkCommandBufferAllocateInfo* pAllocateInfo,
1166 VkCommandBuffer* pCommandBuffers) {
1167 const auto& data = GetData(device);
1168
1169 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1170 pCommandBuffers);
1171 if (result == VK_SUCCESS) {
1172 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1173 SetData(pCommandBuffers[i], data);
1174 }
1175
1176 return result;
1177}
1178
Chia-I Wu9d518162016-03-24 14:55:27 +08001179} // namespace driver
1180} // namespace vulkan