blob: b413ac93751b6f84eeae1f0a0f05b62089dc93b4 [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
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang5e862202019-06-21 14:59:16 -070019#include "driver.h"
20
21#include <dlfcn.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <stdlib.h>
24#include <string.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080025
Jesse Hall53457db2016-12-14 16:54:06 -080026#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080027#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070028#include <android-base/properties.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080030#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
Yiwei Zhange40dd732019-08-05 16:41:03 -070033#include <nativeloader/dlext_namespaces.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070034#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080035#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080036#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037
Yiwei Zhang5e862202019-06-21 14:59:16 -070038#include <algorithm>
39#include <array>
40#include <new>
41#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070042
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070043#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080044
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080045using namespace android::hardware::configstore;
46using namespace android::hardware::configstore::V1_0;
47
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080048// #define ENABLE_ALLOC_CALLSTACKS 1
49#if ENABLE_ALLOC_CALLSTACKS
50#include <utils/CallStack.h>
51#define ALOGD_CALLSTACK(...) \
52 do { \
53 ALOGD(__VA_ARGS__); \
54 android::CallStack callstack; \
55 callstack.update(); \
56 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
57 } while (false)
58#else
59#define ALOGD_CALLSTACK(...) \
60 do { \
61 } while (false)
62#endif
63
Chia-I Wu9d518162016-03-24 14:55:27 +080064namespace vulkan {
65namespace driver {
66
Chia-I Wu136b8eb2016-03-24 15:01:52 +080067namespace {
68
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080069class Hal {
70 public:
71 static bool Open();
72
73 static const Hal& Get() { return hal_; }
74 static const hwvulkan_device_t& Device() { return *Get().dev_; }
75
Chia-I Wu31938252016-05-23 15:31:02 +080076 int GetDebugReportIndex() const { return debug_report_index_; }
77
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080078 private:
Chia-I Wu31938252016-05-23 15:31:02 +080079 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080080 Hal(const Hal&) = delete;
81 Hal& operator=(const Hal&) = delete;
82
Chia-I Wu31938252016-05-23 15:31:02 +080083 bool InitDebugReportIndex();
84
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080085 static Hal hal_;
86
87 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080088 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089};
90
Chia-I Wu4901db72016-03-24 16:38:58 +080091class CreateInfoWrapper {
92 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080094 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080095 CreateInfoWrapper(VkPhysicalDevice physical_dev,
96 const VkDeviceCreateInfo& create_info,
97 const VkAllocationCallbacks& allocator);
98 ~CreateInfoWrapper();
99
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800100 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600101 void DowngradeApiVersion();
Chia-I Wu4901db72016-03-24 16:38:58 +0800102
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800103 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
104 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800105
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800106 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800107 explicit operator const VkDeviceCreateInfo*() const;
108
109 private:
110 struct ExtensionFilter {
111 VkExtensionProperties* exts;
112 uint32_t ext_count;
113
114 const char** names;
115 uint32_t name_count;
116 };
117
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800118 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800119
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800120 VkResult SanitizeLayers();
121 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800122
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800123 VkResult QueryExtensionCount(uint32_t& count) const;
124 VkResult EnumerateExtensions(uint32_t& count,
125 VkExtensionProperties* props) const;
126 VkResult InitExtensionFilter();
127 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800128
129 const bool is_instance_;
130 const VkAllocationCallbacks& allocator_;
131
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800132 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800133
134 union {
135 VkInstanceCreateInfo instance_info_;
136 VkDeviceCreateInfo dev_info_;
137 };
138
Ian Elliottf3e872d2017-11-02 10:15:13 -0600139 VkApplicationInfo application_info_;
140
Chia-I Wu4901db72016-03-24 16:38:58 +0800141 ExtensionFilter extension_filter_;
142
143 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
144 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
145};
146
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800147Hal Hal::hal_;
148
Jesse Hall53457db2016-12-14 16:54:06 -0800149void* LoadLibrary(const android_dlextinfo& dlextinfo,
150 const char* subname,
151 int subname_len) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800152 ATRACE_CALL();
153
Jesse Hall53457db2016-12-14 16:54:06 -0800154 const char kLibFormat[] = "vulkan.%*s.so";
155 char* name = static_cast<char*>(
156 alloca(sizeof(kLibFormat) + static_cast<size_t>(subname_len)));
157 sprintf(name, kLibFormat, subname_len, subname);
158 return android_dlopen_ext(name, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
159}
160
161const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
162 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
163 "ro.board.platform",
164}};
165
Jesse Hall00e61ff2017-04-07 16:48:02 -0700166int LoadDriver(android_namespace_t* library_namespace,
167 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800168 ATRACE_CALL();
169
Jesse Hall53457db2016-12-14 16:54:06 -0800170 const android_dlextinfo dlextinfo = {
171 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700172 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800173 };
Jesse Hall53457db2016-12-14 16:54:06 -0800174 void* so = nullptr;
175 char prop[PROPERTY_VALUE_MAX];
176 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
177 int prop_len = property_get(key, prop, nullptr);
178 if (prop_len > 0) {
179 so = LoadLibrary(dlextinfo, prop, prop_len);
180 if (so)
181 break;
182 }
183 }
184 if (!so)
185 return -ENOENT;
186
Jesse Hall00e61ff2017-04-07 16:48:02 -0700187 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800188 if (!hmi) {
189 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
190 dlclose(so);
191 return -EINVAL;
192 }
193 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
194 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
195 dlclose(so);
196 return -EINVAL;
197 }
198 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700199 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800200 return 0;
201}
202
Jesse Hall00e61ff2017-04-07 16:48:02 -0700203int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800204 ATRACE_CALL();
205
Jesse Hall00e61ff2017-04-07 16:48:02 -0700206 auto ns = android_get_exported_namespace("sphal");
207 if (!ns)
208 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800209 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700210 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700211 return LoadDriver(ns, module);
212}
213
214int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800215 ATRACE_CALL();
216
Jesse Hall00e61ff2017-04-07 16:48:02 -0700217 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
218 if (!ns)
219 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800220 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700221 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700222 return LoadDriver(ns, module);
223}
224
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800225bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800226 ATRACE_CALL();
227
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 const nsecs_t openTime = systemTime();
229
Jesse Halldc225072016-05-30 22:40:14 -0700230 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800231
232 // Use a stub device unless we successfully open a real HAL device.
233 hal_.dev_ = &stubhal::kDevice;
234
Jesse Hall53457db2016-12-14 16:54:06 -0800235 int result;
236 const hwvulkan_module_t* module = nullptr;
237
Jesse Hall00e61ff2017-04-07 16:48:02 -0700238 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800239 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700240 result = LoadBuiltinDriver(&module);
241 if (result != 0) {
242 // -ENOENT means the sphal namespace doesn't exist, not that there
243 // is a problem with the driver.
244 ALOGW_IF(
245 result != -ENOENT,
246 "Failed to load Vulkan driver into sphal namespace. This "
247 "usually means the driver has forbidden library dependencies."
248 "Please fix, this will soon stop working.");
249 result =
250 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
251 reinterpret_cast<const hw_module_t**>(&module));
252 }
Jesse Hall53457db2016-12-14 16:54:06 -0800253 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800254 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800255 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700256 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800257 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800258 return true;
259 }
260
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800261
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800262 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800263 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800264 result =
265 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
266 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800267 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800268 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800269 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700270 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800271 // Any device with a Vulkan HAL should be able to open the device.
272 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
273 result);
274 return false;
275 }
276
277 hal_.dev_ = device;
278
Chia-I Wu31938252016-05-23 15:31:02 +0800279 hal_.InitDebugReportIndex();
280
Yiwei Zhangd9861812019-02-13 11:51:55 -0800281 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700282 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800283
Chia-I Wu31938252016-05-23 15:31:02 +0800284 return true;
285}
286
287bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800288 ATRACE_CALL();
289
Chia-I Wu31938252016-05-23 15:31:02 +0800290 uint32_t count;
291 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
292 VK_SUCCESS) {
293 ALOGE("failed to get HAL instance extension count");
294 return false;
295 }
296
297 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
298 malloc(sizeof(VkExtensionProperties) * count));
299 if (!exts) {
300 ALOGE("failed to allocate HAL instance extension array");
301 return false;
302 }
303
304 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
305 VK_SUCCESS) {
306 ALOGE("failed to enumerate HAL instance extensions");
307 free(exts);
308 return false;
309 }
310
311 for (uint32_t i = 0; i < count; i++) {
312 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
313 0) {
314 debug_report_index_ = static_cast<int>(i);
315 break;
316 }
317 }
318
319 free(exts);
320
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800321 return true;
322}
323
324CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800325 const VkAllocationCallbacks& allocator)
326 : is_instance_(true),
327 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800328 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800329 instance_info_(create_info),
330 extension_filter_() {
331 hook_extensions_.set(ProcHook::EXTENSION_CORE);
332 hal_extensions_.set(ProcHook::EXTENSION_CORE);
333}
334
Chia-I Wu4901db72016-03-24 16:38:58 +0800335CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
336 const VkDeviceCreateInfo& create_info,
337 const VkAllocationCallbacks& allocator)
338 : is_instance_(false),
339 allocator_(allocator),
340 physical_dev_(physical_dev),
341 dev_info_(create_info),
342 extension_filter_() {
343 hook_extensions_.set(ProcHook::EXTENSION_CORE);
344 hal_extensions_.set(ProcHook::EXTENSION_CORE);
345}
346
347CreateInfoWrapper::~CreateInfoWrapper() {
348 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
349 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
350}
351
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800352VkResult CreateInfoWrapper::Validate() {
353 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800354 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800355 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800356 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800357 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800358
359 return result;
360}
361
362const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800363CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800364 return hook_extensions_;
365}
366
367const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800368CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800369 return hal_extensions_;
370}
371
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800372CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
373 return &instance_info_;
374}
375
Chia-I Wu4901db72016-03-24 16:38:58 +0800376CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
377 return &dev_info_;
378}
379
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800380VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800381 const struct StructHeader {
382 VkStructureType type;
383 const void* next;
384 } * header;
385
386 if (is_instance_) {
387 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
388
389 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
390 while (header &&
391 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
392 header = reinterpret_cast<const StructHeader*>(header->next);
393
394 instance_info_.pNext = header;
395 } else {
396 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
397
398 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
399 while (header &&
400 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
401 header = reinterpret_cast<const StructHeader*>(header->next);
402
403 dev_info_.pNext = header;
404 }
405
406 return VK_SUCCESS;
407}
408
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800409VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800410 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
411 : dev_info_.ppEnabledLayerNames;
412 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
413 : dev_info_.enabledLayerCount;
414
415 // remove all layers
416 layer_names = nullptr;
417 layer_count = 0;
418
419 return VK_SUCCESS;
420}
421
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800422VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800423 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
424 : dev_info_.ppEnabledExtensionNames;
425 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
426 : dev_info_.enabledExtensionCount;
427 if (!ext_count)
428 return VK_SUCCESS;
429
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800430 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800431 if (result != VK_SUCCESS)
432 return result;
433
434 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800435 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800436
Jesse Halld3d887a2018-03-05 13:34:45 -0800437 // Enable device extensions that contain physical-device commands, so that
438 // vkGetInstanceProcAddr will return those physical-device commands.
439 if (is_instance_) {
440 hook_extensions_.set(ProcHook::KHR_swapchain);
441 }
442
Chia-I Wu4901db72016-03-24 16:38:58 +0800443 ext_names = extension_filter_.names;
444 ext_count = extension_filter_.name_count;
445
446 return VK_SUCCESS;
447}
448
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800449VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800450 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800451 return Hal::Device().EnumerateInstanceExtensionProperties(
452 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800453 } else {
454 const auto& driver = GetData(physical_dev_).driver;
455 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
456 &count, nullptr);
457 }
458}
459
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800460VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800461 uint32_t& count,
462 VkExtensionProperties* props) const {
463 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800464 return Hal::Device().EnumerateInstanceExtensionProperties(
465 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800466 } else {
467 const auto& driver = GetData(physical_dev_).driver;
468 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
469 &count, props);
470 }
471}
472
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800473VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800474 // query extension count
475 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800476 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800477 if (result != VK_SUCCESS || count == 0)
478 return result;
479
480 auto& filter = extension_filter_;
481 filter.exts =
482 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
483 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
484 alignof(VkExtensionProperties),
485 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
486 if (!filter.exts)
487 return VK_ERROR_OUT_OF_HOST_MEMORY;
488
489 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800490 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800491 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
492 return result;
493
494 if (!count)
495 return VK_SUCCESS;
496
497 filter.ext_count = count;
498
499 // allocate name array
500 uint32_t enabled_ext_count = (is_instance_)
501 ? instance_info_.enabledExtensionCount
502 : dev_info_.enabledExtensionCount;
503 count = std::min(filter.ext_count, enabled_ext_count);
504 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
505 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
506 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
507 if (!filter.names)
508 return VK_ERROR_OUT_OF_HOST_MEMORY;
509
510 return VK_SUCCESS;
511}
512
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800513void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800514 auto& filter = extension_filter_;
515
516 ProcHook::Extension ext_bit = GetProcHookExtension(name);
517 if (is_instance_) {
518 switch (ext_bit) {
519 case ProcHook::KHR_android_surface:
520 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700521 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300522 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800523 hook_extensions_.set(ext_bit);
524 // return now as these extensions do not require HAL support
525 return;
526 case ProcHook::EXT_debug_report:
527 // both we and HAL can take part in
528 hook_extensions_.set(ext_bit);
529 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300530 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700531 case ProcHook::EXTENSION_UNKNOWN:
532 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800533 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700534
Yiwei Zhang23143102019-04-10 18:24:05 -0700535 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700536 case ProcHook::KHR_incremental_present:
537 case ProcHook::KHR_shared_presentable_image:
538 case ProcHook::KHR_swapchain:
539 case ProcHook::EXT_hdr_metadata:
540 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
541 case ProcHook::ANDROID_native_buffer:
542 case ProcHook::GOOGLE_display_timing:
543 case ProcHook::EXTENSION_CORE:
544 case ProcHook::EXTENSION_COUNT:
545 // Device and meta extensions. If we ever get here it's a bug in
546 // our code. But enumerating them lets us avoid having a default
547 // case, and default hides other bugs.
548 ALOGE(
549 "CreateInfoWrapper::FilterExtension: invalid instance "
550 "extension '%s'. FIX ME",
551 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800552 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700553
554 // Don't use a default case. Without it, -Wswitch will tell us
555 // at compile time if someone adds a new ProcHook extension but
556 // doesn't handle it above. That's a real bug that has
557 // not-immediately-obvious effects.
558 //
559 // default:
560 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800561 }
562 } else {
563 switch (ext_bit) {
564 case ProcHook::KHR_swapchain:
565 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
566 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
567 ext_bit = ProcHook::ANDROID_native_buffer;
568 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700569 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700570 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300571 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700572 hook_extensions_.set(ext_bit);
573 // return now as these extensions do not require HAL support
574 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700575 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700576 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700577 hook_extensions_.set(ext_bit);
578 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700579 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800580 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700581 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800582 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700583
584 case ProcHook::KHR_android_surface:
585 case ProcHook::KHR_get_physical_device_properties2:
586 case ProcHook::KHR_get_surface_capabilities2:
587 case ProcHook::KHR_surface:
588 case ProcHook::EXT_debug_report:
589 case ProcHook::EXT_swapchain_colorspace:
590 case ProcHook::ANDROID_native_buffer:
591 case ProcHook::EXTENSION_CORE:
592 case ProcHook::EXTENSION_COUNT:
593 // Instance and meta extensions. If we ever get here it's a bug
594 // in our code. But enumerating them lets us avoid having a
595 // default case, and default hides other bugs.
596 ALOGE(
597 "CreateInfoWrapper::FilterExtension: invalid device "
598 "extension '%s'. FIX ME",
599 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800600 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700601
602 // Don't use a default case. Without it, -Wswitch will tell us
603 // at compile time if someone adds a new ProcHook extension but
604 // doesn't handle it above. That's a real bug that has
605 // not-immediately-obvious effects.
606 //
607 // default:
608 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800609 }
610 }
611
612 for (uint32_t i = 0; i < filter.ext_count; i++) {
613 const VkExtensionProperties& props = filter.exts[i];
614 // ignore unknown extensions
615 if (strcmp(name, props.extensionName) != 0)
616 continue;
617
Chia-I Wu4901db72016-03-24 16:38:58 +0800618 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800619 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
620 if (ext_bit == ProcHook::ANDROID_native_buffer)
621 hook_extensions_.set(ProcHook::KHR_swapchain);
622
623 hal_extensions_.set(ext_bit);
624 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800625
626 break;
627 }
628}
629
Ian Elliottf3e872d2017-11-02 10:15:13 -0600630void CreateInfoWrapper::DowngradeApiVersion() {
631 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
632 if (instance_info_.pApplicationInfo) {
633 application_info_ = *instance_info_.pApplicationInfo;
634 instance_info_.pApplicationInfo = &application_info_;
635 application_info_.apiVersion = VK_API_VERSION_1_0;
636 }
637}
638
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800639VKAPI_ATTR void* DefaultAllocate(void*,
640 size_t size,
641 size_t alignment,
642 VkSystemAllocationScope) {
643 void* ptr = nullptr;
644 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
645 // additionally requires that it be at least sizeof(void*).
646 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
647 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
648 ret, ptr);
649 return ret == 0 ? ptr : nullptr;
650}
651
652VKAPI_ATTR void* DefaultReallocate(void*,
653 void* ptr,
654 size_t size,
655 size_t alignment,
656 VkSystemAllocationScope) {
657 if (size == 0) {
658 free(ptr);
659 return nullptr;
660 }
661
662 // TODO(jessehall): Right now we never shrink allocations; if the new
663 // request is smaller than the existing chunk, we just continue using it.
664 // Right now the loader never reallocs, so this doesn't matter. If that
665 // changes, or if this code is copied into some other project, this should
666 // probably have a heuristic to allocate-copy-free when doing so will save
667 // "enough" space.
668 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
669 if (size <= old_size)
670 return ptr;
671
672 void* new_ptr = nullptr;
673 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
674 return nullptr;
675 if (ptr) {
676 memcpy(new_ptr, ptr, std::min(old_size, size));
677 free(ptr);
678 }
679 return new_ptr;
680}
681
682VKAPI_ATTR void DefaultFree(void*, void* ptr) {
683 ALOGD_CALLSTACK("Free: %p", ptr);
684 free(ptr);
685}
686
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800687InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
688 void* data_mem = allocator.pfnAllocation(
689 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
690 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
691 if (!data_mem)
692 return nullptr;
693
694 return new (data_mem) InstanceData(allocator);
695}
696
697void FreeInstanceData(InstanceData* data,
698 const VkAllocationCallbacks& allocator) {
699 data->~InstanceData();
700 allocator.pfnFree(allocator.pUserData, data);
701}
702
Chia-I Wu950d6e12016-05-03 09:12:35 +0800703DeviceData* AllocateDeviceData(
704 const VkAllocationCallbacks& allocator,
705 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800706 void* data_mem = allocator.pfnAllocation(
707 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
708 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
709 if (!data_mem)
710 return nullptr;
711
Chia-I Wu950d6e12016-05-03 09:12:35 +0800712 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800713}
714
715void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
716 data->~DeviceData();
717 allocator.pfnFree(allocator.pUserData, data);
718}
719
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800720} // anonymous namespace
721
Chia-I Wu9d518162016-03-24 14:55:27 +0800722bool Debuggable() {
723 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
724}
725
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800726bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800727 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800728}
729
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800730const VkAllocationCallbacks& GetDefaultAllocator() {
731 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
732 .pUserData = nullptr,
733 .pfnAllocation = DefaultAllocate,
734 .pfnReallocation = DefaultReallocate,
735 .pfnFree = DefaultFree,
736 };
737
738 return kDefaultAllocCallbacks;
739}
740
Chia-I Wueb7db122016-03-24 09:11:06 +0800741PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
742 const ProcHook* hook = GetProcHook(pName);
743 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800744 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800745
746 if (!instance) {
747 if (hook->type == ProcHook::GLOBAL)
748 return hook->proc;
749
Chia-I Wu109f8982016-04-22 06:40:40 +0800750 // v0 layers expect
751 //
752 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
753 //
754 // to work.
755 if (strcmp(pName, "vkCreateDevice") == 0)
756 return hook->proc;
757
Chia-I Wueb7db122016-03-24 09:11:06 +0800758 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800759 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800760 pName);
761
Chia-I Wu109f8982016-04-22 06:40:40 +0800762 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800763 }
764
765 PFN_vkVoidFunction proc;
766
767 switch (hook->type) {
768 case ProcHook::INSTANCE:
769 proc = (GetData(instance).hook_extensions[hook->extension])
770 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800771 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800772 break;
773 case ProcHook::DEVICE:
774 proc = (hook->extension == ProcHook::EXTENSION_CORE)
775 ? hook->proc
776 : hook->checked_proc;
777 break;
778 default:
779 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800780 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800781 pName);
782 proc = nullptr;
783 break;
784 }
785
786 return proc;
787}
788
789PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
790 const ProcHook* hook = GetProcHook(pName);
791 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800792 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800793
794 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800795 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800796 return nullptr;
797 }
798
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800799 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
800 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800801}
802
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800803VkResult EnumerateInstanceExtensionProperties(
804 const char* pLayerName,
805 uint32_t* pPropertyCount,
806 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700807 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600808 loader_extensions.push_back({
809 VK_KHR_SURFACE_EXTENSION_NAME,
810 VK_KHR_SURFACE_SPEC_VERSION});
811 loader_extensions.push_back({
812 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
813 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
814 loader_extensions.push_back({
815 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
816 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700817 loader_extensions.push_back({
818 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
819 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600820
Chia-I Wu31938252016-05-23 15:31:02 +0800821 static const VkExtensionProperties loader_debug_report_extension = {
822 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
823 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800824
825 // enumerate our extensions first
826 if (!pLayerName && pProperties) {
827 uint32_t count = std::min(
828 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
829
Yiwei Zhang5e862202019-06-21 14:59:16 -0700830 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800831
832 if (count < loader_extensions.size()) {
833 *pPropertyCount = count;
834 return VK_INCOMPLETE;
835 }
836
837 pProperties += count;
838 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800839
840 if (Hal::Get().GetDebugReportIndex() < 0) {
841 if (!*pPropertyCount) {
842 *pPropertyCount = count;
843 return VK_INCOMPLETE;
844 }
845
846 pProperties[0] = loader_debug_report_extension;
847 pProperties += 1;
848 *pPropertyCount -= 1;
849 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800850 }
851
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800852 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800853 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800854 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800855 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800856
Chia-I Wu31938252016-05-23 15:31:02 +0800857 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
858 int idx = Hal::Get().GetDebugReportIndex();
859 if (idx < 0) {
860 *pPropertyCount += 1;
861 } else if (pProperties &&
862 static_cast<uint32_t>(idx) < *pPropertyCount) {
863 pProperties[idx].specVersion =
864 std::min(pProperties[idx].specVersion,
865 loader_debug_report_extension.specVersion);
866 }
867
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800868 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800869 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800870
871 return result;
872}
873
Chris Forbesfa25e632017-02-22 12:36:02 +1300874bool QueryPresentationProperties(
875 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700876 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300877 const InstanceData& data = GetData(physicalDevice);
878
879 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700880 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
881 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300882 return false;
883
884 // Request the android-specific presentation properties via GPDP2
885 VkPhysicalDeviceProperties2KHR properties = {
886 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
887 presentation_properties,
888 {}
889 };
890
891#pragma clang diagnostic push
892#pragma clang diagnostic ignored "-Wold-style-cast"
893 presentation_properties->sType =
894 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
895#pragma clang diagnostic pop
896 presentation_properties->pNext = nullptr;
897 presentation_properties->sharedImage = VK_FALSE;
898
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700899 if (data.driver.GetPhysicalDeviceProperties2KHR) {
900 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
901 &properties);
902 } else {
903 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
904 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300905
906 return true;
907}
908
Chia-I Wu01cf3052016-03-24 16:16:21 +0800909VkResult EnumerateDeviceExtensionProperties(
910 VkPhysicalDevice physicalDevice,
911 const char* pLayerName,
912 uint32_t* pPropertyCount,
913 VkExtensionProperties* pProperties) {
914 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300915 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700916 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300917 loader_extensions.push_back({
918 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
919 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300920
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800921 bool hdrBoardConfig =
922 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
923 false);
924 if (hdrBoardConfig) {
925 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
926 VK_EXT_HDR_METADATA_SPEC_VERSION});
927 }
928
Chris Forbes16095002017-05-05 15:33:29 -0700929 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
930 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
931 presentation_properties.sharedImage) {
932 loader_extensions.push_back({
933 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
934 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300935 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700936
Ian Elliott5c34de22017-04-10 14:42:30 -0600937 // conditionally add VK_GOOGLE_display_timing if present timestamps are
938 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700939 const std::string timestamp_property("service.sf.present_timestamp");
940 android::base::WaitForPropertyCreation(timestamp_property);
941 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600942 loader_extensions.push_back({
943 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
944 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
945 }
946
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700947 // enumerate our extensions first
948 if (!pLayerName && pProperties) {
949 uint32_t count = std::min(
950 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
951
Yiwei Zhang5e862202019-06-21 14:59:16 -0700952 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700953
954 if (count < loader_extensions.size()) {
955 *pPropertyCount = count;
956 return VK_INCOMPLETE;
957 }
958
959 pProperties += count;
960 *pPropertyCount -= count;
961 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800962
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800963 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800964 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
965 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800966 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800967
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700968 if (pProperties) {
969 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
970 for (uint32_t i = 0; i < *pPropertyCount; i++) {
971 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800972
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700973 if (strcmp(prop.extensionName,
974 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
975 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +0800976
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700977 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
978 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -0700979
980 if (prop.specVersion >= 8) {
981 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
982 } else {
983 prop.specVersion = 68;
984 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700985 }
986 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800987
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700988 // restore loader extension count
989 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
990 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800991 }
992
993 return result;
994}
995
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800996VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
997 const VkAllocationCallbacks* pAllocator,
998 VkInstance* pInstance) {
999 const VkAllocationCallbacks& data_allocator =
1000 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1001
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001002 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001003 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001004 if (result != VK_SUCCESS)
1005 return result;
1006
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001007 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001008 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001009 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001010 if (!data)
1011 return VK_ERROR_OUT_OF_HOST_MEMORY;
1012
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001013 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001014
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001015 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001016#pragma clang diagnostic push
1017#pragma clang diagnostic ignored "-Wold-style-cast"
1018 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1019 ? pCreateInfo->pApplicationInfo->apiVersion
1020 : VK_API_VERSION_1_0);
1021 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1022 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1023 uint32_t icd_api_version;
1024 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1025 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001026 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001027 "vkEnumerateInstanceVersion"));
1028 if (!pfn_enumerate_instance_version) {
1029 icd_api_version = VK_API_VERSION_1_0;
1030 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001031 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001032 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001033 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001034 }
1035 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1036 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1037
1038 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1039 ((api_major_version > 1) || (api_minor_version > 0))) {
1040 api_version = VK_API_VERSION_1_0;
1041 wrapper.DowngradeApiVersion();
1042 }
1043#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001044 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001045
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001046 // call into the driver
1047 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001048 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001049 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001050 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1051 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001052 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001053 if (result != VK_SUCCESS) {
1054 FreeInstanceData(data, data_allocator);
1055 return result;
1056 }
1057
1058 // initialize InstanceDriverTable
1059 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001060 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001061 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001062 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001063 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001064 if (data->driver.DestroyInstance)
1065 data->driver.DestroyInstance(instance, pAllocator);
1066
1067 FreeInstanceData(data, data_allocator);
1068
1069 return VK_ERROR_INCOMPATIBLE_DRIVER;
1070 }
1071
1072 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001073 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001074 if (!data->get_device_proc_addr) {
1075 data->driver.DestroyInstance(instance, pAllocator);
1076 FreeInstanceData(data, data_allocator);
1077
1078 return VK_ERROR_INCOMPATIBLE_DRIVER;
1079 }
1080
1081 *pInstance = instance;
1082
1083 return VK_SUCCESS;
1084}
1085
1086void DestroyInstance(VkInstance instance,
1087 const VkAllocationCallbacks* pAllocator) {
1088 InstanceData& data = GetData(instance);
1089 data.driver.DestroyInstance(instance, pAllocator);
1090
1091 VkAllocationCallbacks local_allocator;
1092 if (!pAllocator) {
1093 local_allocator = data.allocator;
1094 pAllocator = &local_allocator;
1095 }
1096
1097 FreeInstanceData(&data, *pAllocator);
1098}
1099
Chia-I Wu4901db72016-03-24 16:38:58 +08001100VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1101 const VkDeviceCreateInfo* pCreateInfo,
1102 const VkAllocationCallbacks* pAllocator,
1103 VkDevice* pDevice) {
1104 const InstanceData& instance_data = GetData(physicalDevice);
1105 const VkAllocationCallbacks& data_allocator =
1106 (pAllocator) ? *pAllocator : instance_data.allocator;
1107
1108 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001109 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001110 if (result != VK_SUCCESS)
1111 return result;
1112
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001113 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001114 DeviceData* data = AllocateDeviceData(data_allocator,
1115 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001116 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001117 if (!data)
1118 return VK_ERROR_OUT_OF_HOST_MEMORY;
1119
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001120 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001121
1122 // call into the driver
1123 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001124 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001125 result = instance_data.driver.CreateDevice(
1126 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1127 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001128 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001129 if (result != VK_SUCCESS) {
1130 FreeDeviceData(data, data_allocator);
1131 return result;
1132 }
1133
1134 // initialize DeviceDriverTable
1135 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001136 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1137 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001138 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1139 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1140 if (data->driver.DestroyDevice)
1141 data->driver.DestroyDevice(dev, pAllocator);
1142
1143 FreeDeviceData(data, data_allocator);
1144
1145 return VK_ERROR_INCOMPATIBLE_DRIVER;
1146 }
Chris Forbesd8277912017-02-10 14:59:59 +13001147
1148 // sanity check ANDROID_native_buffer implementation, whose set of
1149 // entrypoints varies according to the spec version.
1150 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1151 !data->driver.GetSwapchainGrallocUsageANDROID &&
1152 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1153 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1154 " must expose at least one of "
1155 "vkGetSwapchainGrallocUsageANDROID or "
1156 "vkGetSwapchainGrallocUsage2ANDROID");
1157
1158 data->driver.DestroyDevice(dev, pAllocator);
1159 FreeDeviceData(data, data_allocator);
1160
1161 return VK_ERROR_INCOMPATIBLE_DRIVER;
1162 }
1163
Jesse Hall85bb0c52017-02-09 22:13:02 -08001164 VkPhysicalDeviceProperties properties;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001165 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
Jesse Hall85bb0c52017-02-09 22:13:02 -08001166 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1167 &properties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001168 ATRACE_END();
Jesse Hall85bb0c52017-02-09 22:13:02 -08001169
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001170 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1171 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001172 android::GraphicsEnv::getInstance().setTargetStats(
1173 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001174 }
1175
Jesse Halldc225072016-05-30 22:40:14 -07001176 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001177 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001178
1179 *pDevice = dev;
1180
1181 return VK_SUCCESS;
1182}
1183
1184void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1185 DeviceData& data = GetData(device);
1186 data.driver.DestroyDevice(device, pAllocator);
1187
1188 VkAllocationCallbacks local_allocator;
1189 if (!pAllocator) {
1190 local_allocator = data.allocator;
1191 pAllocator = &local_allocator;
1192 }
1193
1194 FreeDeviceData(&data, *pAllocator);
1195}
1196
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001197VkResult EnumeratePhysicalDevices(VkInstance instance,
1198 uint32_t* pPhysicalDeviceCount,
1199 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001200 ATRACE_CALL();
1201
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001202 const auto& data = GetData(instance);
1203
1204 VkResult result = data.driver.EnumeratePhysicalDevices(
1205 instance, pPhysicalDeviceCount, pPhysicalDevices);
1206 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1207 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1208 SetData(pPhysicalDevices[i], data);
1209 }
1210
1211 return result;
1212}
1213
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001214VkResult EnumeratePhysicalDeviceGroups(
1215 VkInstance instance,
1216 uint32_t* pPhysicalDeviceGroupCount,
1217 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001218 ATRACE_CALL();
1219
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001220 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001221 const auto& data = GetData(instance);
1222
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001223 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1224 uint32_t device_count = 0;
1225 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1226 if (result < 0)
1227 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001228
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001229 if (!pPhysicalDeviceGroupProperties) {
1230 *pPhysicalDeviceGroupCount = device_count;
1231 return result;
1232 }
1233
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001234 if (!device_count) {
1235 *pPhysicalDeviceGroupCount = 0;
1236 return result;
1237 }
Chad Versace32c087f2018-09-09 07:28:05 -07001238 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1239 if (!device_count)
1240 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001241
Yiwei Zhang5e862202019-06-21 14:59:16 -07001242 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001243 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001244 result =
1245 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001246 if (result < 0)
1247 return result;
1248
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001249 for (uint32_t i = 0; i < device_count; ++i) {
1250 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1251 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1252 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1253 }
1254 } else {
1255 result = data.driver.EnumeratePhysicalDeviceGroups(
1256 instance, pPhysicalDeviceGroupCount,
1257 pPhysicalDeviceGroupProperties);
1258 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1259 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1260 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1261 for (uint32_t j = 0;
1262 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1263 j++) {
1264 SetData(
1265 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001266 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001267 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001268 }
1269 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001270 }
1271
1272 return result;
1273}
1274
Chia-I Wuba0be412016-03-24 16:24:40 +08001275void GetDeviceQueue(VkDevice device,
1276 uint32_t queueFamilyIndex,
1277 uint32_t queueIndex,
1278 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001279 ATRACE_CALL();
1280
Chia-I Wuba0be412016-03-24 16:24:40 +08001281 const auto& data = GetData(device);
1282
1283 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1284 SetData(*pQueue, data);
1285}
1286
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001287void GetDeviceQueue2(VkDevice device,
1288 const VkDeviceQueueInfo2* pQueueInfo,
1289 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001290 ATRACE_CALL();
1291
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001292 const auto& data = GetData(device);
1293
1294 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001295 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001296}
1297
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001298VKAPI_ATTR VkResult
1299AllocateCommandBuffers(VkDevice device,
1300 const VkCommandBufferAllocateInfo* pAllocateInfo,
1301 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001302 ATRACE_CALL();
1303
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001304 const auto& data = GetData(device);
1305
1306 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1307 pCommandBuffers);
1308 if (result == VK_SUCCESS) {
1309 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1310 SetData(pCommandBuffers[i], data);
1311 }
1312
1313 return result;
1314}
1315
Yiwei Zhang899d1752019-09-23 16:05:35 -07001316VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1317 uint32_t submitCount,
1318 const VkSubmitInfo* pSubmits,
1319 VkFence fence) {
1320 ATRACE_CALL();
1321
1322 const auto& data = GetData(queue);
1323
1324 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1325}
1326
Chia-I Wu9d518162016-03-24 14:55:27 +08001327} // namespace driver
1328} // namespace vulkan