blob: 3e31a66d7840f7f12a40722892724c7e8e3c8d38 [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
Sundong Ahnbc37dd52020-04-23 21:21:00 +090026#include <SurfaceFlingerProperties.h>
27#include <android-base/properties.h>
Jesse Hall53457db2016-12-14 16:54:06 -080028#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
30#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080031#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090032#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070033#include <log/log.h>
Yiwei Zhange40dd732019-08-05 16:41:03 -070034#include <nativeloader/dlext_namespaces.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070035#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080036#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080037#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080038
Yiwei Zhang5e862202019-06-21 14:59:16 -070039#include <algorithm>
40#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070041#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070042#include <new>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070043#include <string_view>
44#include <sstream>
Yiwei Zhang5e862202019-06-21 14:59:16 -070045#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070046
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070047#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080048
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080049using namespace android::hardware::configstore;
50using namespace android::hardware::configstore::V1_0;
51
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080052// #define ENABLE_ALLOC_CALLSTACKS 1
53#if ENABLE_ALLOC_CALLSTACKS
54#include <utils/CallStack.h>
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 ALOGD(__VA_ARGS__); \
58 android::CallStack callstack; \
59 callstack.update(); \
60 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
61 } while (false)
62#else
63#define ALOGD_CALLSTACK(...) \
64 do { \
65 } while (false)
66#endif
67
Chia-I Wu9d518162016-03-24 14:55:27 +080068namespace vulkan {
69namespace driver {
70
Chia-I Wu136b8eb2016-03-24 15:01:52 +080071namespace {
72
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080073class Hal {
74 public:
75 static bool Open();
76
77 static const Hal& Get() { return hal_; }
78 static const hwvulkan_device_t& Device() { return *Get().dev_; }
79
Chia-I Wu31938252016-05-23 15:31:02 +080080 int GetDebugReportIndex() const { return debug_report_index_; }
81
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080082 private:
Chia-I Wu31938252016-05-23 15:31:02 +080083 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080084 Hal(const Hal&) = delete;
85 Hal& operator=(const Hal&) = delete;
86
Chia-I Wu31938252016-05-23 15:31:02 +080087 bool InitDebugReportIndex();
88
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 static Hal hal_;
90
91 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080092 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093};
94
Chia-I Wu4901db72016-03-24 16:38:58 +080095class CreateInfoWrapper {
96 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080097 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080098 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080099 CreateInfoWrapper(VkPhysicalDevice physical_dev,
100 const VkDeviceCreateInfo& create_info,
101 const VkAllocationCallbacks& allocator);
102 ~CreateInfoWrapper();
103
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800104 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800105
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800106 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
107 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800108
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800109 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800110 explicit operator const VkDeviceCreateInfo*() const;
111
112 private:
113 struct ExtensionFilter {
114 VkExtensionProperties* exts;
115 uint32_t ext_count;
116
117 const char** names;
118 uint32_t name_count;
119 };
120
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700121 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800122 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800123 VkResult SanitizeLayers();
124 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800125
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800126 VkResult QueryExtensionCount(uint32_t& count) const;
127 VkResult EnumerateExtensions(uint32_t& count,
128 VkExtensionProperties* props) const;
129 VkResult InitExtensionFilter();
130 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800131
132 const bool is_instance_;
133 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700134 const uint32_t loader_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800135
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800136 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800137
138 union {
139 VkInstanceCreateInfo instance_info_;
140 VkDeviceCreateInfo dev_info_;
141 };
142
Ian Elliottf3e872d2017-11-02 10:15:13 -0600143 VkApplicationInfo application_info_;
144
Chia-I Wu4901db72016-03-24 16:38:58 +0800145 ExtensionFilter extension_filter_;
146
147 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
148 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
149};
150
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800151Hal Hal::hal_;
152
Jesse Hall53457db2016-12-14 16:54:06 -0800153void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700154 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800155 ATRACE_CALL();
156
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700157 std::stringstream ss;
158 ss << "vulkan." << subname << ".so";
159 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800160}
161
162const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700163 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800164 "ro.board.platform",
165}};
166
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800167// LoadDriver returns:
168// * 0 when succeed, or
169// * -ENOENT when fail to open binary libraries, or
170// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
171// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700172int LoadDriver(android_namespace_t* library_namespace,
173 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800174 ATRACE_CALL();
175
Jesse Hall53457db2016-12-14 16:54:06 -0800176 const android_dlextinfo dlextinfo = {
177 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700178 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800179 };
Jesse Hall53457db2016-12-14 16:54:06 -0800180 void* so = nullptr;
181 char prop[PROPERTY_VALUE_MAX];
182 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
183 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700184 if (prop_len > 0 && prop_len <= UINT_MAX) {
185 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
186 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800187 if (so)
188 break;
189 }
190 }
191 if (!so)
192 return -ENOENT;
193
Jesse Hall00e61ff2017-04-07 16:48:02 -0700194 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800195 if (!hmi) {
196 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
197 dlclose(so);
198 return -EINVAL;
199 }
200 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
201 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
202 dlclose(so);
203 return -EINVAL;
204 }
205 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700206 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800207 return 0;
208}
209
Jesse Hall00e61ff2017-04-07 16:48:02 -0700210int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800211 ATRACE_CALL();
212
Jesse Hall00e61ff2017-04-07 16:48:02 -0700213 auto ns = android_get_exported_namespace("sphal");
214 if (!ns)
215 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800216 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700217 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 return LoadDriver(ns, module);
219}
220
221int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800222 ATRACE_CALL();
223
Jesse Hall00e61ff2017-04-07 16:48:02 -0700224 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
225 if (!ns)
226 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800227 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700228 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800229 int result = LoadDriver(ns, module);
230 if (result != 0) {
231 LOG_ALWAYS_FATAL(
232 "couldn't find an updated Vulkan implementation from %s",
233 android::GraphicsEnv::getInstance().getDriverPath().c_str());
234 }
235 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700236}
237
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800238bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800239 ATRACE_CALL();
240
Yiwei Zhangd9861812019-02-13 11:51:55 -0800241 const nsecs_t openTime = systemTime();
242
Jesse Halldc225072016-05-30 22:40:14 -0700243 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800244
245 // Use a stub device unless we successfully open a real HAL device.
246 hal_.dev_ = &stubhal::kDevice;
247
Jesse Hall53457db2016-12-14 16:54:06 -0800248 int result;
249 const hwvulkan_module_t* module = nullptr;
250
Jesse Hall00e61ff2017-04-07 16:48:02 -0700251 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800252 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700253 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800254 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800255 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800256 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700257 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800258 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800259 return true;
260 }
261
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800262
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800263 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800264 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 result =
266 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
267 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800268 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800270 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700271 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800272 // Any device with a Vulkan HAL should be able to open the device.
273 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
274 result);
275 return false;
276 }
277
278 hal_.dev_ = device;
279
Chia-I Wu31938252016-05-23 15:31:02 +0800280 hal_.InitDebugReportIndex();
281
Yiwei Zhangd9861812019-02-13 11:51:55 -0800282 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700283 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800284
Chia-I Wu31938252016-05-23 15:31:02 +0800285 return true;
286}
287
288bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800289 ATRACE_CALL();
290
Chia-I Wu31938252016-05-23 15:31:02 +0800291 uint32_t count;
292 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
293 VK_SUCCESS) {
294 ALOGE("failed to get HAL instance extension count");
295 return false;
296 }
297
298 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
299 malloc(sizeof(VkExtensionProperties) * count));
300 if (!exts) {
301 ALOGE("failed to allocate HAL instance extension array");
302 return false;
303 }
304
305 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
306 VK_SUCCESS) {
307 ALOGE("failed to enumerate HAL instance extensions");
308 free(exts);
309 return false;
310 }
311
312 for (uint32_t i = 0; i < count; i++) {
313 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
314 0) {
315 debug_report_index_ = static_cast<int>(i);
316 break;
317 }
318 }
319
320 free(exts);
321
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800322 return true;
323}
324
325CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800326 const VkAllocationCallbacks& allocator)
327 : is_instance_(true),
328 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700329 loader_api_version_(VK_API_VERSION_1_1),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800330 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800331 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700332 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800333
Chia-I Wu4901db72016-03-24 16:38:58 +0800334CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
335 const VkDeviceCreateInfo& create_info,
336 const VkAllocationCallbacks& allocator)
337 : is_instance_(false),
338 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700339 loader_api_version_(VK_API_VERSION_1_1),
Chia-I Wu4901db72016-03-24 16:38:58 +0800340 physical_dev_(physical_dev),
341 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700342 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800343
344CreateInfoWrapper::~CreateInfoWrapper() {
345 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
346 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
347}
348
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800349VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700350 VkResult result = SanitizeApiVersion();
351 if (result == VK_SUCCESS)
352 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800353 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800354 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800355 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800356 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800357
358 return result;
359}
360
361const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800362CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800363 return hook_extensions_;
364}
365
366const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800367CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800368 return hal_extensions_;
369}
370
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800371CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
372 return &instance_info_;
373}
374
Chia-I Wu4901db72016-03-24 16:38:58 +0800375CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
376 return &dev_info_;
377}
378
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700379VkResult CreateInfoWrapper::SanitizeApiVersion() {
380 if (is_instance_) {
381 // instance core versions need to match the loader api version
382 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
383 i != ProcHook::EXTENSION_COUNT; i++) {
384 hook_extensions_.set(i);
385 hal_extensions_.set(i);
386 }
387
388 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0
389 if (!instance_info_.pApplicationInfo)
390 return VK_SUCCESS;
391
392 uint32_t icd_api_version = VK_API_VERSION_1_0;
393 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
394 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
395 Hal::Device().GetInstanceProcAddr(
396 nullptr, "vkEnumerateInstanceVersion"));
397 if (pfn_enumerate_instance_version) {
398 ATRACE_BEGIN("pfn_enumerate_instance_version");
399 VkResult result =
400 (*pfn_enumerate_instance_version)(&icd_api_version);
401 ATRACE_END();
402 if (result != VK_SUCCESS)
403 return result;
404
405 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
406 }
407
408 if (icd_api_version < VK_API_VERSION_1_0)
409 return VK_SUCCESS;
410
411 if (icd_api_version < loader_api_version_) {
412 application_info_ = *instance_info_.pApplicationInfo;
413 application_info_.apiVersion = icd_api_version;
414 instance_info_.pApplicationInfo = &application_info_;
415 }
416 } else {
417 const auto& driver = GetData(physical_dev_).driver;
418
419 VkPhysicalDeviceProperties properties;
420 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
421 driver.GetPhysicalDeviceProperties(physical_dev_, &properties);
422 ATRACE_END();
423
424 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
425 // Log that the app is hitting software Vulkan implementation
426 android::GraphicsEnv::getInstance().setTargetStats(
427 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
428 }
429
430 uint32_t api_version = properties.apiVersion;
431 api_version ^= VK_VERSION_PATCH(api_version);
432
433 if (api_version > loader_api_version_)
434 api_version = loader_api_version_;
435
436 switch (api_version) {
437 case VK_API_VERSION_1_1:
438 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
439 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
440 [[clang::fallthrough]];
441 case VK_API_VERSION_1_0:
442 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
443 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
444 break;
445 default:
446 ALOGE("Unknown API version[%u]", api_version);
447 break;
448 }
449 }
450
451 return VK_SUCCESS;
452}
453
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800454VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800455 const struct StructHeader {
456 VkStructureType type;
457 const void* next;
458 } * header;
459
460 if (is_instance_) {
461 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
462
463 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
464 while (header &&
465 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
466 header = reinterpret_cast<const StructHeader*>(header->next);
467
468 instance_info_.pNext = header;
469 } else {
470 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
471
472 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
473 while (header &&
474 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
475 header = reinterpret_cast<const StructHeader*>(header->next);
476
477 dev_info_.pNext = header;
478 }
479
480 return VK_SUCCESS;
481}
482
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800483VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800484 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
485 : dev_info_.ppEnabledLayerNames;
486 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
487 : dev_info_.enabledLayerCount;
488
489 // remove all layers
490 layer_names = nullptr;
491 layer_count = 0;
492
493 return VK_SUCCESS;
494}
495
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800496VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800497 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
498 : dev_info_.ppEnabledExtensionNames;
499 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
500 : dev_info_.enabledExtensionCount;
501 if (!ext_count)
502 return VK_SUCCESS;
503
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800504 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800505 if (result != VK_SUCCESS)
506 return result;
507
508 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800509 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800510
Jesse Halld3d887a2018-03-05 13:34:45 -0800511 // Enable device extensions that contain physical-device commands, so that
512 // vkGetInstanceProcAddr will return those physical-device commands.
513 if (is_instance_) {
514 hook_extensions_.set(ProcHook::KHR_swapchain);
515 }
516
Chia-I Wu4901db72016-03-24 16:38:58 +0800517 ext_names = extension_filter_.names;
518 ext_count = extension_filter_.name_count;
519
520 return VK_SUCCESS;
521}
522
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800523VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800524 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800525 return Hal::Device().EnumerateInstanceExtensionProperties(
526 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800527 } else {
528 const auto& driver = GetData(physical_dev_).driver;
529 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
530 &count, nullptr);
531 }
532}
533
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800534VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800535 uint32_t& count,
536 VkExtensionProperties* props) const {
537 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800538 return Hal::Device().EnumerateInstanceExtensionProperties(
539 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800540 } else {
541 const auto& driver = GetData(physical_dev_).driver;
542 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
543 &count, props);
544 }
545}
546
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800547VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800548 // query extension count
549 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800550 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800551 if (result != VK_SUCCESS || count == 0)
552 return result;
553
554 auto& filter = extension_filter_;
555 filter.exts =
556 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
557 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
558 alignof(VkExtensionProperties),
559 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
560 if (!filter.exts)
561 return VK_ERROR_OUT_OF_HOST_MEMORY;
562
563 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800564 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800565 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
566 return result;
567
568 if (!count)
569 return VK_SUCCESS;
570
571 filter.ext_count = count;
572
573 // allocate name array
574 uint32_t enabled_ext_count = (is_instance_)
575 ? instance_info_.enabledExtensionCount
576 : dev_info_.enabledExtensionCount;
577 count = std::min(filter.ext_count, enabled_ext_count);
578 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
579 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
580 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
581 if (!filter.names)
582 return VK_ERROR_OUT_OF_HOST_MEMORY;
583
584 return VK_SUCCESS;
585}
586
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800587void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800588 auto& filter = extension_filter_;
589
590 ProcHook::Extension ext_bit = GetProcHookExtension(name);
591 if (is_instance_) {
592 switch (ext_bit) {
593 case ProcHook::KHR_android_surface:
594 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700595 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300596 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800597 hook_extensions_.set(ext_bit);
598 // return now as these extensions do not require HAL support
599 return;
600 case ProcHook::EXT_debug_report:
601 // both we and HAL can take part in
602 hook_extensions_.set(ext_bit);
603 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300604 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700605 case ProcHook::EXTENSION_UNKNOWN:
606 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800607 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700608
Yiwei Zhang23143102019-04-10 18:24:05 -0700609 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700610 case ProcHook::KHR_incremental_present:
611 case ProcHook::KHR_shared_presentable_image:
612 case ProcHook::KHR_swapchain:
613 case ProcHook::EXT_hdr_metadata:
614 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
615 case ProcHook::ANDROID_native_buffer:
616 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700617 case ProcHook::EXTENSION_CORE_1_0:
618 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700619 case ProcHook::EXTENSION_COUNT:
620 // Device and meta extensions. If we ever get here it's a bug in
621 // our code. But enumerating them lets us avoid having a default
622 // case, and default hides other bugs.
623 ALOGE(
624 "CreateInfoWrapper::FilterExtension: invalid instance "
625 "extension '%s'. FIX ME",
626 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800627 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700628
629 // Don't use a default case. Without it, -Wswitch will tell us
630 // at compile time if someone adds a new ProcHook extension but
631 // doesn't handle it above. That's a real bug that has
632 // not-immediately-obvious effects.
633 //
634 // default:
635 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800636 }
637 } else {
638 switch (ext_bit) {
639 case ProcHook::KHR_swapchain:
640 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
641 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
642 ext_bit = ProcHook::ANDROID_native_buffer;
643 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700644 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700645 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300646 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700647 hook_extensions_.set(ext_bit);
648 // return now as these extensions do not require HAL support
649 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700650 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700651 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700652 hook_extensions_.set(ext_bit);
653 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700654 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800655 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700656 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800657 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700658
659 case ProcHook::KHR_android_surface:
660 case ProcHook::KHR_get_physical_device_properties2:
661 case ProcHook::KHR_get_surface_capabilities2:
662 case ProcHook::KHR_surface:
663 case ProcHook::EXT_debug_report:
664 case ProcHook::EXT_swapchain_colorspace:
665 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700666 case ProcHook::EXTENSION_CORE_1_0:
667 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700668 case ProcHook::EXTENSION_COUNT:
669 // Instance and meta extensions. If we ever get here it's a bug
670 // in our code. But enumerating them lets us avoid having a
671 // default case, and default hides other bugs.
672 ALOGE(
673 "CreateInfoWrapper::FilterExtension: invalid device "
674 "extension '%s'. FIX ME",
675 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800676 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700677
678 // Don't use a default case. Without it, -Wswitch will tell us
679 // at compile time if someone adds a new ProcHook extension but
680 // doesn't handle it above. That's a real bug that has
681 // not-immediately-obvious effects.
682 //
683 // default:
684 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800685 }
686 }
687
688 for (uint32_t i = 0; i < filter.ext_count; i++) {
689 const VkExtensionProperties& props = filter.exts[i];
690 // ignore unknown extensions
691 if (strcmp(name, props.extensionName) != 0)
692 continue;
693
Chia-I Wu4901db72016-03-24 16:38:58 +0800694 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800695 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
696 if (ext_bit == ProcHook::ANDROID_native_buffer)
697 hook_extensions_.set(ProcHook::KHR_swapchain);
698
699 hal_extensions_.set(ext_bit);
700 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800701
702 break;
703 }
704}
705
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800706VKAPI_ATTR void* DefaultAllocate(void*,
707 size_t size,
708 size_t alignment,
709 VkSystemAllocationScope) {
710 void* ptr = nullptr;
711 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
712 // additionally requires that it be at least sizeof(void*).
713 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
714 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
715 ret, ptr);
716 return ret == 0 ? ptr : nullptr;
717}
718
719VKAPI_ATTR void* DefaultReallocate(void*,
720 void* ptr,
721 size_t size,
722 size_t alignment,
723 VkSystemAllocationScope) {
724 if (size == 0) {
725 free(ptr);
726 return nullptr;
727 }
728
Yiwei Zhanga885c062019-10-24 12:07:57 -0700729 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800730 // request is smaller than the existing chunk, we just continue using it.
731 // Right now the loader never reallocs, so this doesn't matter. If that
732 // changes, or if this code is copied into some other project, this should
733 // probably have a heuristic to allocate-copy-free when doing so will save
734 // "enough" space.
735 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
736 if (size <= old_size)
737 return ptr;
738
739 void* new_ptr = nullptr;
740 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
741 return nullptr;
742 if (ptr) {
743 memcpy(new_ptr, ptr, std::min(old_size, size));
744 free(ptr);
745 }
746 return new_ptr;
747}
748
749VKAPI_ATTR void DefaultFree(void*, void* ptr) {
750 ALOGD_CALLSTACK("Free: %p", ptr);
751 free(ptr);
752}
753
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800754InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
755 void* data_mem = allocator.pfnAllocation(
756 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
757 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
758 if (!data_mem)
759 return nullptr;
760
761 return new (data_mem) InstanceData(allocator);
762}
763
764void FreeInstanceData(InstanceData* data,
765 const VkAllocationCallbacks& allocator) {
766 data->~InstanceData();
767 allocator.pfnFree(allocator.pUserData, data);
768}
769
Chia-I Wu950d6e12016-05-03 09:12:35 +0800770DeviceData* AllocateDeviceData(
771 const VkAllocationCallbacks& allocator,
772 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800773 void* data_mem = allocator.pfnAllocation(
774 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
775 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
776 if (!data_mem)
777 return nullptr;
778
Chia-I Wu950d6e12016-05-03 09:12:35 +0800779 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800780}
781
782void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
783 data->~DeviceData();
784 allocator.pfnFree(allocator.pUserData, data);
785}
786
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800787} // anonymous namespace
788
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800789bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800790 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800791}
792
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800793const VkAllocationCallbacks& GetDefaultAllocator() {
794 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
795 .pUserData = nullptr,
796 .pfnAllocation = DefaultAllocate,
797 .pfnReallocation = DefaultReallocate,
798 .pfnFree = DefaultFree,
799 };
800
801 return kDefaultAllocCallbacks;
802}
803
Chia-I Wueb7db122016-03-24 09:11:06 +0800804PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
805 const ProcHook* hook = GetProcHook(pName);
806 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800807 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800808
809 if (!instance) {
810 if (hook->type == ProcHook::GLOBAL)
811 return hook->proc;
812
Chia-I Wu109f8982016-04-22 06:40:40 +0800813 // v0 layers expect
814 //
815 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
816 //
817 // to work.
818 if (strcmp(pName, "vkCreateDevice") == 0)
819 return hook->proc;
820
Chia-I Wueb7db122016-03-24 09:11:06 +0800821 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800822 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800823 pName);
824
Chia-I Wu109f8982016-04-22 06:40:40 +0800825 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800826 }
827
828 PFN_vkVoidFunction proc;
829
830 switch (hook->type) {
831 case ProcHook::INSTANCE:
832 proc = (GetData(instance).hook_extensions[hook->extension])
833 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800834 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800835 break;
836 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700837 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800838 ? hook->proc
839 : hook->checked_proc;
840 break;
841 default:
842 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800843 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800844 pName);
845 proc = nullptr;
846 break;
847 }
848
849 return proc;
850}
851
852PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
853 const ProcHook* hook = GetProcHook(pName);
854 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800855 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800856
857 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800858 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800859 return nullptr;
860 }
861
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800862 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
863 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800864}
865
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800866VkResult EnumerateInstanceExtensionProperties(
867 const char* pLayerName,
868 uint32_t* pPropertyCount,
869 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700870 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600871 loader_extensions.push_back({
872 VK_KHR_SURFACE_EXTENSION_NAME,
873 VK_KHR_SURFACE_SPEC_VERSION});
874 loader_extensions.push_back({
875 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
876 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
877 loader_extensions.push_back({
878 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
879 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700880 loader_extensions.push_back({
881 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
882 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600883
Chia-I Wu31938252016-05-23 15:31:02 +0800884 static const VkExtensionProperties loader_debug_report_extension = {
885 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
886 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800887
888 // enumerate our extensions first
889 if (!pLayerName && pProperties) {
890 uint32_t count = std::min(
891 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
892
Yiwei Zhang5e862202019-06-21 14:59:16 -0700893 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800894
895 if (count < loader_extensions.size()) {
896 *pPropertyCount = count;
897 return VK_INCOMPLETE;
898 }
899
900 pProperties += count;
901 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800902
903 if (Hal::Get().GetDebugReportIndex() < 0) {
904 if (!*pPropertyCount) {
905 *pPropertyCount = count;
906 return VK_INCOMPLETE;
907 }
908
909 pProperties[0] = loader_debug_report_extension;
910 pProperties += 1;
911 *pPropertyCount -= 1;
912 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800913 }
914
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800915 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800916 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800917 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800918 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800919
Chia-I Wu31938252016-05-23 15:31:02 +0800920 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
921 int idx = Hal::Get().GetDebugReportIndex();
922 if (idx < 0) {
923 *pPropertyCount += 1;
924 } else if (pProperties &&
925 static_cast<uint32_t>(idx) < *pPropertyCount) {
926 pProperties[idx].specVersion =
927 std::min(pProperties[idx].specVersion,
928 loader_debug_report_extension.specVersion);
929 }
930
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800931 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800932 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800933
934 return result;
935}
936
Chris Forbesfa25e632017-02-22 12:36:02 +1300937bool QueryPresentationProperties(
938 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700939 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300940 const InstanceData& data = GetData(physicalDevice);
941
942 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700943 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
944 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300945 return false;
946
947 // Request the android-specific presentation properties via GPDP2
948 VkPhysicalDeviceProperties2KHR properties = {
949 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
950 presentation_properties,
951 {}
952 };
953
954#pragma clang diagnostic push
955#pragma clang diagnostic ignored "-Wold-style-cast"
956 presentation_properties->sType =
957 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
958#pragma clang diagnostic pop
959 presentation_properties->pNext = nullptr;
960 presentation_properties->sharedImage = VK_FALSE;
961
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700962 if (data.driver.GetPhysicalDeviceProperties2KHR) {
963 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
964 &properties);
965 } else {
966 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
967 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300968
969 return true;
970}
971
Chia-I Wu01cf3052016-03-24 16:16:21 +0800972VkResult EnumerateDeviceExtensionProperties(
973 VkPhysicalDevice physicalDevice,
974 const char* pLayerName,
975 uint32_t* pPropertyCount,
976 VkExtensionProperties* pProperties) {
977 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300978 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700979 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300980 loader_extensions.push_back({
981 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
982 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300983
Sundong Ahnbc37dd52020-04-23 21:21:00 +0900984 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800985 if (hdrBoardConfig) {
986 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
987 VK_EXT_HDR_METADATA_SPEC_VERSION});
988 }
989
Chris Forbes16095002017-05-05 15:33:29 -0700990 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
991 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
992 presentation_properties.sharedImage) {
993 loader_extensions.push_back({
994 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
995 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300996 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700997
Ian Elliott5c34de22017-04-10 14:42:30 -0600998 // conditionally add VK_GOOGLE_display_timing if present timestamps are
999 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -07001000 const std::string timestamp_property("service.sf.present_timestamp");
1001 android::base::WaitForPropertyCreation(timestamp_property);
1002 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001003 loader_extensions.push_back({
1004 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1005 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1006 }
1007
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001008 // enumerate our extensions first
1009 if (!pLayerName && pProperties) {
1010 uint32_t count = std::min(
1011 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1012
Yiwei Zhang5e862202019-06-21 14:59:16 -07001013 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001014
1015 if (count < loader_extensions.size()) {
1016 *pPropertyCount = count;
1017 return VK_INCOMPLETE;
1018 }
1019
1020 pProperties += count;
1021 *pPropertyCount -= count;
1022 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001023
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001024 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001025 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1026 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001027 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001028
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001029 if (pProperties) {
1030 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1031 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1032 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001033
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001034 if (strcmp(prop.extensionName,
1035 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1036 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001037
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001038 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1039 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001040
1041 if (prop.specVersion >= 8) {
1042 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1043 } else {
1044 prop.specVersion = 68;
1045 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001046 }
1047 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001048
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001049 // restore loader extension count
1050 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1051 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001052 }
1053
1054 return result;
1055}
1056
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001057VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1058 const VkAllocationCallbacks* pAllocator,
1059 VkInstance* pInstance) {
1060 const VkAllocationCallbacks& data_allocator =
1061 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1062
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001063 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001064 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001065 if (result != VK_SUCCESS)
1066 return result;
1067
1068 InstanceData* data = AllocateInstanceData(data_allocator);
1069 if (!data)
1070 return VK_ERROR_OUT_OF_HOST_MEMORY;
1071
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001072 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001073
1074 // call into the driver
1075 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001076 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001077 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001078 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1079 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001080 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001081 if (result != VK_SUCCESS) {
1082 FreeInstanceData(data, data_allocator);
1083 return result;
1084 }
1085
1086 // initialize InstanceDriverTable
1087 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001088 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001089 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001090 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001091 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001092 if (data->driver.DestroyInstance)
1093 data->driver.DestroyInstance(instance, pAllocator);
1094
1095 FreeInstanceData(data, data_allocator);
1096
1097 return VK_ERROR_INCOMPATIBLE_DRIVER;
1098 }
1099
1100 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001101 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001102 if (!data->get_device_proc_addr) {
1103 data->driver.DestroyInstance(instance, pAllocator);
1104 FreeInstanceData(data, data_allocator);
1105
1106 return VK_ERROR_INCOMPATIBLE_DRIVER;
1107 }
1108
1109 *pInstance = instance;
1110
1111 return VK_SUCCESS;
1112}
1113
1114void DestroyInstance(VkInstance instance,
1115 const VkAllocationCallbacks* pAllocator) {
1116 InstanceData& data = GetData(instance);
1117 data.driver.DestroyInstance(instance, pAllocator);
1118
1119 VkAllocationCallbacks local_allocator;
1120 if (!pAllocator) {
1121 local_allocator = data.allocator;
1122 pAllocator = &local_allocator;
1123 }
1124
1125 FreeInstanceData(&data, *pAllocator);
1126}
1127
Chia-I Wu4901db72016-03-24 16:38:58 +08001128VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1129 const VkDeviceCreateInfo* pCreateInfo,
1130 const VkAllocationCallbacks* pAllocator,
1131 VkDevice* pDevice) {
1132 const InstanceData& instance_data = GetData(physicalDevice);
1133 const VkAllocationCallbacks& data_allocator =
1134 (pAllocator) ? *pAllocator : instance_data.allocator;
1135
1136 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001137 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001138 if (result != VK_SUCCESS)
1139 return result;
1140
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001141 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001142 DeviceData* data = AllocateDeviceData(data_allocator,
1143 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001144 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001145 if (!data)
1146 return VK_ERROR_OUT_OF_HOST_MEMORY;
1147
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001148 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001149
1150 // call into the driver
1151 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001152 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001153 result = instance_data.driver.CreateDevice(
1154 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1155 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001156 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001157 if (result != VK_SUCCESS) {
1158 FreeDeviceData(data, data_allocator);
1159 return result;
1160 }
1161
1162 // initialize DeviceDriverTable
1163 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001164 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1165 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001166 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1167 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1168 if (data->driver.DestroyDevice)
1169 data->driver.DestroyDevice(dev, pAllocator);
1170
1171 FreeDeviceData(data, data_allocator);
1172
1173 return VK_ERROR_INCOMPATIBLE_DRIVER;
1174 }
Chris Forbesd8277912017-02-10 14:59:59 +13001175
1176 // sanity check ANDROID_native_buffer implementation, whose set of
1177 // entrypoints varies according to the spec version.
1178 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1179 !data->driver.GetSwapchainGrallocUsageANDROID &&
1180 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1181 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1182 " must expose at least one of "
1183 "vkGetSwapchainGrallocUsageANDROID or "
1184 "vkGetSwapchainGrallocUsage2ANDROID");
1185
1186 data->driver.DestroyDevice(dev, pAllocator);
1187 FreeDeviceData(data, data_allocator);
1188
1189 return VK_ERROR_INCOMPATIBLE_DRIVER;
1190 }
1191
Jesse Halldc225072016-05-30 22:40:14 -07001192 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001193
1194 *pDevice = dev;
1195
1196 return VK_SUCCESS;
1197}
1198
1199void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1200 DeviceData& data = GetData(device);
1201 data.driver.DestroyDevice(device, pAllocator);
1202
1203 VkAllocationCallbacks local_allocator;
1204 if (!pAllocator) {
1205 local_allocator = data.allocator;
1206 pAllocator = &local_allocator;
1207 }
1208
1209 FreeDeviceData(&data, *pAllocator);
1210}
1211
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001212VkResult EnumeratePhysicalDevices(VkInstance instance,
1213 uint32_t* pPhysicalDeviceCount,
1214 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001215 ATRACE_CALL();
1216
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001217 const auto& data = GetData(instance);
1218
1219 VkResult result = data.driver.EnumeratePhysicalDevices(
1220 instance, pPhysicalDeviceCount, pPhysicalDevices);
1221 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1222 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1223 SetData(pPhysicalDevices[i], data);
1224 }
1225
1226 return result;
1227}
1228
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001229VkResult EnumeratePhysicalDeviceGroups(
1230 VkInstance instance,
1231 uint32_t* pPhysicalDeviceGroupCount,
1232 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001233 ATRACE_CALL();
1234
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001235 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001236 const auto& data = GetData(instance);
1237
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001238 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1239 uint32_t device_count = 0;
1240 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1241 if (result < 0)
1242 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001243
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001244 if (!pPhysicalDeviceGroupProperties) {
1245 *pPhysicalDeviceGroupCount = device_count;
1246 return result;
1247 }
1248
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001249 if (!device_count) {
1250 *pPhysicalDeviceGroupCount = 0;
1251 return result;
1252 }
Chad Versace32c087f2018-09-09 07:28:05 -07001253 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1254 if (!device_count)
1255 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001256
Yiwei Zhang5e862202019-06-21 14:59:16 -07001257 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001258 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001259 result =
1260 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001261 if (result < 0)
1262 return result;
1263
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001264 for (uint32_t i = 0; i < device_count; ++i) {
1265 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1266 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1267 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1268 }
1269 } else {
1270 result = data.driver.EnumeratePhysicalDeviceGroups(
1271 instance, pPhysicalDeviceGroupCount,
1272 pPhysicalDeviceGroupProperties);
1273 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1274 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1275 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1276 for (uint32_t j = 0;
1277 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1278 j++) {
1279 SetData(
1280 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001281 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001282 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001283 }
1284 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001285 }
1286
1287 return result;
1288}
1289
Chia-I Wuba0be412016-03-24 16:24:40 +08001290void GetDeviceQueue(VkDevice device,
1291 uint32_t queueFamilyIndex,
1292 uint32_t queueIndex,
1293 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001294 ATRACE_CALL();
1295
Chia-I Wuba0be412016-03-24 16:24:40 +08001296 const auto& data = GetData(device);
1297
1298 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1299 SetData(*pQueue, data);
1300}
1301
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001302void GetDeviceQueue2(VkDevice device,
1303 const VkDeviceQueueInfo2* pQueueInfo,
1304 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001305 ATRACE_CALL();
1306
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001307 const auto& data = GetData(device);
1308
1309 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001310 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001311}
1312
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001313VKAPI_ATTR VkResult
1314AllocateCommandBuffers(VkDevice device,
1315 const VkCommandBufferAllocateInfo* pAllocateInfo,
1316 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001317 ATRACE_CALL();
1318
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001319 const auto& data = GetData(device);
1320
1321 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1322 pCommandBuffers);
1323 if (result == VK_SUCCESS) {
1324 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1325 SetData(pCommandBuffers[i], data);
1326 }
1327
1328 return result;
1329}
1330
Yiwei Zhang899d1752019-09-23 16:05:35 -07001331VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1332 uint32_t submitCount,
1333 const VkSubmitInfo* pSubmits,
1334 VkFence fence) {
1335 ATRACE_CALL();
1336
1337 const auto& data = GetData(queue);
1338
1339 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1340}
1341
Chia-I Wu9d518162016-03-24 14:55:27 +08001342} // namespace driver
1343} // namespace vulkan