blob: e3fc67e35f602e8476d1b629258606a9d3e6297e [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,
Yiwei Zhang2cefa732020-07-10 21:07:30 -070098 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080099 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800100 CreateInfoWrapper(VkPhysicalDevice physical_dev,
101 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700102 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800103 const VkAllocationCallbacks& allocator);
104 ~CreateInfoWrapper();
105
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800106 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800107
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
109 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800110
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800111 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800112 explicit operator const VkDeviceCreateInfo*() const;
113
114 private:
115 struct ExtensionFilter {
116 VkExtensionProperties* exts;
117 uint32_t ext_count;
118
119 const char** names;
120 uint32_t name_count;
121 };
122
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700123 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800124 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800125 VkResult SanitizeLayers();
126 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800127
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800128 VkResult QueryExtensionCount(uint32_t& count) const;
129 VkResult EnumerateExtensions(uint32_t& count,
130 VkExtensionProperties* props) const;
131 VkResult InitExtensionFilter();
132 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800133
134 const bool is_instance_;
135 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700136 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700137 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800138
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800139 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800140
141 union {
142 VkInstanceCreateInfo instance_info_;
143 VkDeviceCreateInfo dev_info_;
144 };
145
Ian Elliottf3e872d2017-11-02 10:15:13 -0600146 VkApplicationInfo application_info_;
147
Chia-I Wu4901db72016-03-24 16:38:58 +0800148 ExtensionFilter extension_filter_;
149
150 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
151 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
152};
153
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800154Hal Hal::hal_;
155
Jesse Hall53457db2016-12-14 16:54:06 -0800156void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700157 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800158 ATRACE_CALL();
159
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700160 std::stringstream ss;
161 ss << "vulkan." << subname << ".so";
162 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800163}
164
165const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700166 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800167 "ro.board.platform",
168}};
169
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800170// LoadDriver returns:
171// * 0 when succeed, or
172// * -ENOENT when fail to open binary libraries, or
173// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
174// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700175int LoadDriver(android_namespace_t* library_namespace,
176 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800177 ATRACE_CALL();
178
Jesse Hall53457db2016-12-14 16:54:06 -0800179 const android_dlextinfo dlextinfo = {
180 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700181 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800182 };
Jesse Hall53457db2016-12-14 16:54:06 -0800183 void* so = nullptr;
184 char prop[PROPERTY_VALUE_MAX];
185 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
186 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700187 if (prop_len > 0 && prop_len <= UINT_MAX) {
188 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
189 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800190 if (so)
191 break;
192 }
193 }
194 if (!so)
195 return -ENOENT;
196
Jesse Hall00e61ff2017-04-07 16:48:02 -0700197 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800198 if (!hmi) {
199 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
200 dlclose(so);
201 return -EINVAL;
202 }
203 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
204 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
205 dlclose(so);
206 return -EINVAL;
207 }
208 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700209 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800210 return 0;
211}
212
Jesse Hall00e61ff2017-04-07 16:48:02 -0700213int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800214 ATRACE_CALL();
215
Jesse Hall00e61ff2017-04-07 16:48:02 -0700216 auto ns = android_get_exported_namespace("sphal");
217 if (!ns)
218 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800219 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700220 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700221 return LoadDriver(ns, module);
222}
223
224int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800225 ATRACE_CALL();
226
Jesse Hall00e61ff2017-04-07 16:48:02 -0700227 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
228 if (!ns)
229 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800230 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700231 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800232 int result = LoadDriver(ns, module);
233 if (result != 0) {
234 LOG_ALWAYS_FATAL(
235 "couldn't find an updated Vulkan implementation from %s",
236 android::GraphicsEnv::getInstance().getDriverPath().c_str());
237 }
238 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700239}
240
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800241bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800242 ATRACE_CALL();
243
Yiwei Zhangd9861812019-02-13 11:51:55 -0800244 const nsecs_t openTime = systemTime();
245
Jesse Halldc225072016-05-30 22:40:14 -0700246 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800247
248 // Use a stub device unless we successfully open a real HAL device.
249 hal_.dev_ = &stubhal::kDevice;
250
Jesse Hall53457db2016-12-14 16:54:06 -0800251 int result;
252 const hwvulkan_module_t* module = nullptr;
253
Jesse Hall00e61ff2017-04-07 16:48:02 -0700254 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800255 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700256 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800257 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800258 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800259 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700260 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800261 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800262 return true;
263 }
264
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800265
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800266 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800267 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800268 result =
269 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
270 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800271 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800272 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700274 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 // Any device with a Vulkan HAL should be able to open the device.
276 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
277 result);
278 return false;
279 }
280
281 hal_.dev_ = device;
282
Chia-I Wu31938252016-05-23 15:31:02 +0800283 hal_.InitDebugReportIndex();
284
Yiwei Zhangd9861812019-02-13 11:51:55 -0800285 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700286 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800287
Chia-I Wu31938252016-05-23 15:31:02 +0800288 return true;
289}
290
291bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800292 ATRACE_CALL();
293
Chia-I Wu31938252016-05-23 15:31:02 +0800294 uint32_t count;
295 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
296 VK_SUCCESS) {
297 ALOGE("failed to get HAL instance extension count");
298 return false;
299 }
300
301 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
302 malloc(sizeof(VkExtensionProperties) * count));
303 if (!exts) {
304 ALOGE("failed to allocate HAL instance extension array");
305 return false;
306 }
307
308 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
309 VK_SUCCESS) {
310 ALOGE("failed to enumerate HAL instance extensions");
311 free(exts);
312 return false;
313 }
314
315 for (uint32_t i = 0; i < count; i++) {
316 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
317 0) {
318 debug_report_index_ = static_cast<int>(i);
319 break;
320 }
321 }
322
323 free(exts);
324
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800325 return true;
326}
327
328CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700329 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800330 const VkAllocationCallbacks& allocator)
331 : is_instance_(true),
332 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700333 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700334 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800335 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800336 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700337 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800338
Chia-I Wu4901db72016-03-24 16:38:58 +0800339CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
340 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700341 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800342 const VkAllocationCallbacks& allocator)
343 : is_instance_(false),
344 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700345 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700346 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800347 physical_dev_(physical_dev),
348 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700349 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800350
351CreateInfoWrapper::~CreateInfoWrapper() {
352 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
353 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
354}
355
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800356VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700357 VkResult result = SanitizeApiVersion();
358 if (result == VK_SUCCESS)
359 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800360 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800361 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800362 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800363 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800364
365 return result;
366}
367
368const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800369CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800370 return hook_extensions_;
371}
372
373const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800374CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800375 return hal_extensions_;
376}
377
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800378CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
379 return &instance_info_;
380}
381
Chia-I Wu4901db72016-03-24 16:38:58 +0800382CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
383 return &dev_info_;
384}
385
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700386VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700387 if (!is_instance_ || !instance_info_.pApplicationInfo)
388 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700389
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700390 if (icd_api_version_ > VK_API_VERSION_1_0 ||
391 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
392 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700393
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700394 // override apiVersion to avoid error return from 1.0 icd
395 application_info_ = *instance_info_.pApplicationInfo;
396 application_info_.apiVersion = VK_API_VERSION_1_0;
397 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700398
399 return VK_SUCCESS;
400}
401
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800402VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800403 const struct StructHeader {
404 VkStructureType type;
405 const void* next;
406 } * header;
407
408 if (is_instance_) {
409 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
410
411 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
412 while (header &&
413 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
414 header = reinterpret_cast<const StructHeader*>(header->next);
415
416 instance_info_.pNext = header;
417 } else {
418 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
419
420 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
421 while (header &&
422 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
423 header = reinterpret_cast<const StructHeader*>(header->next);
424
425 dev_info_.pNext = header;
426 }
427
428 return VK_SUCCESS;
429}
430
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800431VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800432 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
433 : dev_info_.ppEnabledLayerNames;
434 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
435 : dev_info_.enabledLayerCount;
436
437 // remove all layers
438 layer_names = nullptr;
439 layer_count = 0;
440
441 return VK_SUCCESS;
442}
443
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800444VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800445 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
446 : dev_info_.ppEnabledExtensionNames;
447 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
448 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800449
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800450 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800451 if (result != VK_SUCCESS)
452 return result;
453
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700454 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700455 for (uint32_t i = 0; i < ext_count; i++) {
456 // Upon api downgrade, skip the promoted instance extensions in the
457 // first pass to avoid duplicate extensions.
458 const std::optional<uint32_t> version =
459 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700460 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700461 *version <= loader_api_version_)
462 continue;
463
464 FilterExtension(ext_names[i]);
465 }
466
467 // Enable the required extensions to support core functionalities.
468 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700469 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700470 for (const auto& promoted_extension : promoted_extensions)
471 FilterExtension(promoted_extension);
472 } else {
473 for (uint32_t i = 0; i < ext_count; i++)
474 FilterExtension(ext_names[i]);
475 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800476
Jesse Halld3d887a2018-03-05 13:34:45 -0800477 // Enable device extensions that contain physical-device commands, so that
478 // vkGetInstanceProcAddr will return those physical-device commands.
479 if (is_instance_) {
480 hook_extensions_.set(ProcHook::KHR_swapchain);
481 }
482
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700483 const uint32_t api_version =
484 is_instance_ ? loader_api_version_
485 : std::min(icd_api_version_, loader_api_version_);
486 switch (api_version) {
487 case VK_API_VERSION_1_1:
488 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
489 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
490 [[clang::fallthrough]];
491 case VK_API_VERSION_1_0:
492 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
493 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
494 break;
495 default:
496 ALOGE("Unknown API version[%u]", api_version);
497 break;
498 }
499
Chia-I Wu4901db72016-03-24 16:38:58 +0800500 ext_names = extension_filter_.names;
501 ext_count = extension_filter_.name_count;
502
503 return VK_SUCCESS;
504}
505
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800506VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800507 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800508 return Hal::Device().EnumerateInstanceExtensionProperties(
509 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800510 } else {
511 const auto& driver = GetData(physical_dev_).driver;
512 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
513 &count, nullptr);
514 }
515}
516
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800517VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800518 uint32_t& count,
519 VkExtensionProperties* props) const {
520 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800521 return Hal::Device().EnumerateInstanceExtensionProperties(
522 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800523 } else {
524 const auto& driver = GetData(physical_dev_).driver;
525 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
526 &count, props);
527 }
528}
529
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800530VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800531 // query extension count
532 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800533 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800534 if (result != VK_SUCCESS || count == 0)
535 return result;
536
537 auto& filter = extension_filter_;
538 filter.exts =
539 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
540 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
541 alignof(VkExtensionProperties),
542 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
543 if (!filter.exts)
544 return VK_ERROR_OUT_OF_HOST_MEMORY;
545
546 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800547 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800548 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
549 return result;
550
551 if (!count)
552 return VK_SUCCESS;
553
554 filter.ext_count = count;
555
556 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700557 if (is_instance_) {
558 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
559
560 // It requires enabling additional promoted extensions to downgrade api,
561 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700562 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700563 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700564 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700565 }
566
567 count = std::min(filter.ext_count, enabled_ext_count);
568 } else {
569 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
570 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800571 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
572 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
573 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
574 if (!filter.names)
575 return VK_ERROR_OUT_OF_HOST_MEMORY;
576
577 return VK_SUCCESS;
578}
579
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800580void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800581 auto& filter = extension_filter_;
582
583 ProcHook::Extension ext_bit = GetProcHookExtension(name);
584 if (is_instance_) {
585 switch (ext_bit) {
586 case ProcHook::KHR_android_surface:
587 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700588 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300589 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800590 hook_extensions_.set(ext_bit);
591 // return now as these extensions do not require HAL support
592 return;
593 case ProcHook::EXT_debug_report:
594 // both we and HAL can take part in
595 hook_extensions_.set(ext_bit);
596 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300597 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700598 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700599 case ProcHook::KHR_external_memory_capabilities:
600 case ProcHook::KHR_external_semaphore_capabilities:
601 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700602 case ProcHook::EXTENSION_UNKNOWN:
603 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800604 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700605
Yiwei Zhang23143102019-04-10 18:24:05 -0700606 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700607 case ProcHook::KHR_incremental_present:
608 case ProcHook::KHR_shared_presentable_image:
609 case ProcHook::KHR_swapchain:
610 case ProcHook::EXT_hdr_metadata:
611 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
612 case ProcHook::ANDROID_native_buffer:
613 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700614 case ProcHook::EXTENSION_CORE_1_0:
615 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700616 case ProcHook::EXTENSION_COUNT:
617 // Device and meta extensions. If we ever get here it's a bug in
618 // our code. But enumerating them lets us avoid having a default
619 // case, and default hides other bugs.
620 ALOGE(
621 "CreateInfoWrapper::FilterExtension: invalid instance "
622 "extension '%s'. FIX ME",
623 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800624 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700625
626 // Don't use a default case. Without it, -Wswitch will tell us
627 // at compile time if someone adds a new ProcHook extension but
628 // doesn't handle it above. That's a real bug that has
629 // not-immediately-obvious effects.
630 //
631 // default:
632 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800633 }
634 } else {
635 switch (ext_bit) {
636 case ProcHook::KHR_swapchain:
637 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
638 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
639 ext_bit = ProcHook::ANDROID_native_buffer;
640 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700641 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700642 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300643 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700644 hook_extensions_.set(ext_bit);
645 // return now as these extensions do not require HAL support
646 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700647 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700648 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700649 hook_extensions_.set(ext_bit);
650 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700651 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800652 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700653 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800654 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700655
656 case ProcHook::KHR_android_surface:
657 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700658 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700659 case ProcHook::KHR_external_memory_capabilities:
660 case ProcHook::KHR_external_semaphore_capabilities:
661 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700662 case ProcHook::KHR_get_surface_capabilities2:
663 case ProcHook::KHR_surface:
664 case ProcHook::EXT_debug_report:
665 case ProcHook::EXT_swapchain_colorspace:
666 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700667 case ProcHook::EXTENSION_CORE_1_0:
668 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700669 case ProcHook::EXTENSION_COUNT:
670 // Instance and meta extensions. If we ever get here it's a bug
671 // in our code. But enumerating them lets us avoid having a
672 // default case, and default hides other bugs.
673 ALOGE(
674 "CreateInfoWrapper::FilterExtension: invalid device "
675 "extension '%s'. FIX ME",
676 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800677 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700678
679 // Don't use a default case. Without it, -Wswitch will tell us
680 // at compile time if someone adds a new ProcHook extension but
681 // doesn't handle it above. That's a real bug that has
682 // not-immediately-obvious effects.
683 //
684 // default:
685 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800686 }
687 }
688
689 for (uint32_t i = 0; i < filter.ext_count; i++) {
690 const VkExtensionProperties& props = filter.exts[i];
691 // ignore unknown extensions
692 if (strcmp(name, props.extensionName) != 0)
693 continue;
694
Chia-I Wu4901db72016-03-24 16:38:58 +0800695 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800696 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
697 if (ext_bit == ProcHook::ANDROID_native_buffer)
698 hook_extensions_.set(ProcHook::KHR_swapchain);
699
700 hal_extensions_.set(ext_bit);
701 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800702
703 break;
704 }
705}
706
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800707VKAPI_ATTR void* DefaultAllocate(void*,
708 size_t size,
709 size_t alignment,
710 VkSystemAllocationScope) {
711 void* ptr = nullptr;
712 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
713 // additionally requires that it be at least sizeof(void*).
714 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
715 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
716 ret, ptr);
717 return ret == 0 ? ptr : nullptr;
718}
719
720VKAPI_ATTR void* DefaultReallocate(void*,
721 void* ptr,
722 size_t size,
723 size_t alignment,
724 VkSystemAllocationScope) {
725 if (size == 0) {
726 free(ptr);
727 return nullptr;
728 }
729
Yiwei Zhanga885c062019-10-24 12:07:57 -0700730 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800731 // request is smaller than the existing chunk, we just continue using it.
732 // Right now the loader never reallocs, so this doesn't matter. If that
733 // changes, or if this code is copied into some other project, this should
734 // probably have a heuristic to allocate-copy-free when doing so will save
735 // "enough" space.
736 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
737 if (size <= old_size)
738 return ptr;
739
740 void* new_ptr = nullptr;
741 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
742 return nullptr;
743 if (ptr) {
744 memcpy(new_ptr, ptr, std::min(old_size, size));
745 free(ptr);
746 }
747 return new_ptr;
748}
749
750VKAPI_ATTR void DefaultFree(void*, void* ptr) {
751 ALOGD_CALLSTACK("Free: %p", ptr);
752 free(ptr);
753}
754
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800755InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
756 void* data_mem = allocator.pfnAllocation(
757 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
758 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
759 if (!data_mem)
760 return nullptr;
761
762 return new (data_mem) InstanceData(allocator);
763}
764
765void FreeInstanceData(InstanceData* data,
766 const VkAllocationCallbacks& allocator) {
767 data->~InstanceData();
768 allocator.pfnFree(allocator.pUserData, data);
769}
770
Chia-I Wu950d6e12016-05-03 09:12:35 +0800771DeviceData* AllocateDeviceData(
772 const VkAllocationCallbacks& allocator,
773 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800774 void* data_mem = allocator.pfnAllocation(
775 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
776 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
777 if (!data_mem)
778 return nullptr;
779
Chia-I Wu950d6e12016-05-03 09:12:35 +0800780 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800781}
782
783void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
784 data->~DeviceData();
785 allocator.pfnFree(allocator.pUserData, data);
786}
787
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800788} // anonymous namespace
789
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800790bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800791 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800792}
793
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800794const VkAllocationCallbacks& GetDefaultAllocator() {
795 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
796 .pUserData = nullptr,
797 .pfnAllocation = DefaultAllocate,
798 .pfnReallocation = DefaultReallocate,
799 .pfnFree = DefaultFree,
800 };
801
802 return kDefaultAllocCallbacks;
803}
804
Chia-I Wueb7db122016-03-24 09:11:06 +0800805PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
806 const ProcHook* hook = GetProcHook(pName);
807 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800808 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800809
810 if (!instance) {
811 if (hook->type == ProcHook::GLOBAL)
812 return hook->proc;
813
Chia-I Wu109f8982016-04-22 06:40:40 +0800814 // v0 layers expect
815 //
816 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
817 //
818 // to work.
819 if (strcmp(pName, "vkCreateDevice") == 0)
820 return hook->proc;
821
Chia-I Wueb7db122016-03-24 09:11:06 +0800822 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800823 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800824 pName);
825
Chia-I Wu109f8982016-04-22 06:40:40 +0800826 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800827 }
828
829 PFN_vkVoidFunction proc;
830
831 switch (hook->type) {
832 case ProcHook::INSTANCE:
833 proc = (GetData(instance).hook_extensions[hook->extension])
834 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800835 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800836 break;
837 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700838 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800839 ? hook->proc
840 : hook->checked_proc;
841 break;
842 default:
843 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800844 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800845 pName);
846 proc = nullptr;
847 break;
848 }
849
850 return proc;
851}
852
853PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
854 const ProcHook* hook = GetProcHook(pName);
855 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800856 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800857
858 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800859 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800860 return nullptr;
861 }
862
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800863 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
864 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800865}
866
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800867VkResult EnumerateInstanceExtensionProperties(
868 const char* pLayerName,
869 uint32_t* pPropertyCount,
870 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700871 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600872 loader_extensions.push_back({
873 VK_KHR_SURFACE_EXTENSION_NAME,
874 VK_KHR_SURFACE_SPEC_VERSION});
875 loader_extensions.push_back({
876 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
877 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
878 loader_extensions.push_back({
879 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
880 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700881 loader_extensions.push_back({
882 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
883 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600884
Chia-I Wu31938252016-05-23 15:31:02 +0800885 static const VkExtensionProperties loader_debug_report_extension = {
886 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
887 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800888
889 // enumerate our extensions first
890 if (!pLayerName && pProperties) {
891 uint32_t count = std::min(
892 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
893
Yiwei Zhang5e862202019-06-21 14:59:16 -0700894 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800895
896 if (count < loader_extensions.size()) {
897 *pPropertyCount = count;
898 return VK_INCOMPLETE;
899 }
900
901 pProperties += count;
902 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800903
904 if (Hal::Get().GetDebugReportIndex() < 0) {
905 if (!*pPropertyCount) {
906 *pPropertyCount = count;
907 return VK_INCOMPLETE;
908 }
909
910 pProperties[0] = loader_debug_report_extension;
911 pProperties += 1;
912 *pPropertyCount -= 1;
913 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800914 }
915
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800916 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800917 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800918 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800919 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800920
Chia-I Wu31938252016-05-23 15:31:02 +0800921 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
922 int idx = Hal::Get().GetDebugReportIndex();
923 if (idx < 0) {
924 *pPropertyCount += 1;
925 } else if (pProperties &&
926 static_cast<uint32_t>(idx) < *pPropertyCount) {
927 pProperties[idx].specVersion =
928 std::min(pProperties[idx].specVersion,
929 loader_debug_report_extension.specVersion);
930 }
931
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800932 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800933 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800934
935 return result;
936}
937
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700938void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +1300939 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700940 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300941 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700942 VkPhysicalDeviceProperties2 properties = {
943 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +1300944 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700945 {},
Chris Forbesfa25e632017-02-22 12:36:02 +1300946 };
947
948#pragma clang diagnostic push
949#pragma clang diagnostic ignored "-Wold-style-cast"
950 presentation_properties->sType =
951 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
952#pragma clang diagnostic pop
953 presentation_properties->pNext = nullptr;
954 presentation_properties->sharedImage = VK_FALSE;
955
Yiwei Zhanga55624b2020-07-05 16:05:26 -0700956 GetPhysicalDeviceProperties2(physicalDevice, &properties);
Chris Forbesfa25e632017-02-22 12:36:02 +1300957}
958
Chia-I Wu01cf3052016-03-24 16:16:21 +0800959VkResult EnumerateDeviceExtensionProperties(
960 VkPhysicalDevice physicalDevice,
961 const char* pLayerName,
962 uint32_t* pPropertyCount,
963 VkExtensionProperties* pProperties) {
964 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300965 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700966 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300967 loader_extensions.push_back({
968 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
969 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300970
Sundong Ahnbc37dd52020-04-23 21:21:00 +0900971 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800972 if (hdrBoardConfig) {
973 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
974 VK_EXT_HDR_METADATA_SPEC_VERSION});
975 }
976
Chris Forbes16095002017-05-05 15:33:29 -0700977 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700978 QueryPresentationProperties(physicalDevice, &presentation_properties);
979 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -0700980 loader_extensions.push_back({
981 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
982 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300983 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700984
Ian Elliott5c34de22017-04-10 14:42:30 -0600985 // conditionally add VK_GOOGLE_display_timing if present timestamps are
986 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700987 const std::string timestamp_property("service.sf.present_timestamp");
988 android::base::WaitForPropertyCreation(timestamp_property);
989 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600990 loader_extensions.push_back({
991 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
992 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
993 }
994
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700995 // enumerate our extensions first
996 if (!pLayerName && pProperties) {
997 uint32_t count = std::min(
998 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
999
Yiwei Zhang5e862202019-06-21 14:59:16 -07001000 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001001
1002 if (count < loader_extensions.size()) {
1003 *pPropertyCount = count;
1004 return VK_INCOMPLETE;
1005 }
1006
1007 pProperties += count;
1008 *pPropertyCount -= count;
1009 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001010
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001011 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001012 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1013 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001014 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001015
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001016 if (pProperties) {
1017 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1018 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1019 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001020
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001021 if (strcmp(prop.extensionName,
1022 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1023 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001024
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001025 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1026 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001027
1028 if (prop.specVersion >= 8) {
1029 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1030 } else {
1031 prop.specVersion = 68;
1032 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001033 }
1034 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001035
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001036 // restore loader extension count
1037 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1038 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001039 }
1040
1041 return result;
1042}
1043
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001044VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1045 const VkAllocationCallbacks* pAllocator,
1046 VkInstance* pInstance) {
1047 const VkAllocationCallbacks& data_allocator =
1048 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1049
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001050 VkResult result = VK_SUCCESS;
1051 uint32_t icd_api_version = VK_API_VERSION_1_0;
1052 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1053 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1054 Hal::Device().GetInstanceProcAddr(nullptr,
1055 "vkEnumerateInstanceVersion"));
1056 if (pfn_enumerate_instance_version) {
1057 ATRACE_BEGIN("pfn_enumerate_instance_version");
1058 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1059 ATRACE_END();
1060 if (result != VK_SUCCESS)
1061 return result;
1062
1063 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
1064 }
1065
1066 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1067 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001068 if (result != VK_SUCCESS)
1069 return result;
1070
1071 InstanceData* data = AllocateInstanceData(data_allocator);
1072 if (!data)
1073 return VK_ERROR_OUT_OF_HOST_MEMORY;
1074
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001075 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001076
1077 // call into the driver
1078 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001079 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001080 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001081 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1082 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001083 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001084 if (result != VK_SUCCESS) {
1085 FreeInstanceData(data, data_allocator);
1086 return result;
1087 }
1088
1089 // initialize InstanceDriverTable
1090 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001091 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001092 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001093 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001094 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001095 if (data->driver.DestroyInstance)
1096 data->driver.DestroyInstance(instance, pAllocator);
1097
1098 FreeInstanceData(data, data_allocator);
1099
1100 return VK_ERROR_INCOMPATIBLE_DRIVER;
1101 }
1102
1103 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001104 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001105 if (!data->get_device_proc_addr) {
1106 data->driver.DestroyInstance(instance, pAllocator);
1107 FreeInstanceData(data, data_allocator);
1108
1109 return VK_ERROR_INCOMPATIBLE_DRIVER;
1110 }
1111
1112 *pInstance = instance;
1113
1114 return VK_SUCCESS;
1115}
1116
1117void DestroyInstance(VkInstance instance,
1118 const VkAllocationCallbacks* pAllocator) {
1119 InstanceData& data = GetData(instance);
1120 data.driver.DestroyInstance(instance, pAllocator);
1121
1122 VkAllocationCallbacks local_allocator;
1123 if (!pAllocator) {
1124 local_allocator = data.allocator;
1125 pAllocator = &local_allocator;
1126 }
1127
1128 FreeInstanceData(&data, *pAllocator);
1129}
1130
Chia-I Wu4901db72016-03-24 16:38:58 +08001131VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1132 const VkDeviceCreateInfo* pCreateInfo,
1133 const VkAllocationCallbacks* pAllocator,
1134 VkDevice* pDevice) {
1135 const InstanceData& instance_data = GetData(physicalDevice);
1136 const VkAllocationCallbacks& data_allocator =
1137 (pAllocator) ? *pAllocator : instance_data.allocator;
1138
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001139 VkPhysicalDeviceProperties properties;
1140 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1141 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1142 &properties);
1143 ATRACE_END();
1144
1145 CreateInfoWrapper wrapper(
1146 physicalDevice, *pCreateInfo,
1147 properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion),
1148 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001149 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001150 if (result != VK_SUCCESS)
1151 return result;
1152
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001153 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001154 DeviceData* data = AllocateDeviceData(data_allocator,
1155 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001156 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001157 if (!data)
1158 return VK_ERROR_OUT_OF_HOST_MEMORY;
1159
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001160 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001161
1162 // call into the driver
1163 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001164 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001165 result = instance_data.driver.CreateDevice(
1166 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1167 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001168 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001169 if (result != VK_SUCCESS) {
1170 FreeDeviceData(data, data_allocator);
1171 return result;
1172 }
1173
1174 // initialize DeviceDriverTable
1175 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001176 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1177 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001178 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1179 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1180 if (data->driver.DestroyDevice)
1181 data->driver.DestroyDevice(dev, pAllocator);
1182
1183 FreeDeviceData(data, data_allocator);
1184
1185 return VK_ERROR_INCOMPATIBLE_DRIVER;
1186 }
Chris Forbesd8277912017-02-10 14:59:59 +13001187
1188 // sanity check ANDROID_native_buffer implementation, whose set of
1189 // entrypoints varies according to the spec version.
1190 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1191 !data->driver.GetSwapchainGrallocUsageANDROID &&
1192 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1193 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1194 " must expose at least one of "
1195 "vkGetSwapchainGrallocUsageANDROID or "
1196 "vkGetSwapchainGrallocUsage2ANDROID");
1197
1198 data->driver.DestroyDevice(dev, pAllocator);
1199 FreeDeviceData(data, data_allocator);
1200
1201 return VK_ERROR_INCOMPATIBLE_DRIVER;
1202 }
1203
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001204 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1205 // Log that the app is hitting software Vulkan implementation
1206 android::GraphicsEnv::getInstance().setTargetStats(
1207 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1208 }
1209
Jesse Halldc225072016-05-30 22:40:14 -07001210 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001211
1212 *pDevice = dev;
1213
1214 return VK_SUCCESS;
1215}
1216
1217void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1218 DeviceData& data = GetData(device);
1219 data.driver.DestroyDevice(device, pAllocator);
1220
1221 VkAllocationCallbacks local_allocator;
1222 if (!pAllocator) {
1223 local_allocator = data.allocator;
1224 pAllocator = &local_allocator;
1225 }
1226
1227 FreeDeviceData(&data, *pAllocator);
1228}
1229
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001230VkResult EnumeratePhysicalDevices(VkInstance instance,
1231 uint32_t* pPhysicalDeviceCount,
1232 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001233 ATRACE_CALL();
1234
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001235 const auto& data = GetData(instance);
1236
1237 VkResult result = data.driver.EnumeratePhysicalDevices(
1238 instance, pPhysicalDeviceCount, pPhysicalDevices);
1239 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1240 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1241 SetData(pPhysicalDevices[i], data);
1242 }
1243
1244 return result;
1245}
1246
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001247VkResult EnumeratePhysicalDeviceGroups(
1248 VkInstance instance,
1249 uint32_t* pPhysicalDeviceGroupCount,
1250 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001251 ATRACE_CALL();
1252
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001253 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001254 const auto& data = GetData(instance);
1255
Yiwei Zhange4f64172020-07-05 15:17:32 -07001256 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1257 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001258 uint32_t device_count = 0;
1259 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1260 if (result < 0)
1261 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001262
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001263 if (!pPhysicalDeviceGroupProperties) {
1264 *pPhysicalDeviceGroupCount = device_count;
1265 return result;
1266 }
1267
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001268 if (!device_count) {
1269 *pPhysicalDeviceGroupCount = 0;
1270 return result;
1271 }
Chad Versace32c087f2018-09-09 07:28:05 -07001272 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1273 if (!device_count)
1274 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001275
Yiwei Zhang5e862202019-06-21 14:59:16 -07001276 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001277 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001278 result =
1279 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001280 if (result < 0)
1281 return result;
1282
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001283 for (uint32_t i = 0; i < device_count; ++i) {
1284 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1285 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1286 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1287 }
1288 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001289 if (data.driver.EnumeratePhysicalDeviceGroups) {
1290 result = data.driver.EnumeratePhysicalDeviceGroups(
1291 instance, pPhysicalDeviceGroupCount,
1292 pPhysicalDeviceGroupProperties);
1293 } else {
1294 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1295 instance, pPhysicalDeviceGroupCount,
1296 pPhysicalDeviceGroupProperties);
1297 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001298 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1299 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1300 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1301 for (uint32_t j = 0;
1302 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1303 j++) {
1304 SetData(
1305 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001306 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001307 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001308 }
1309 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001310 }
1311
1312 return result;
1313}
1314
Chia-I Wuba0be412016-03-24 16:24:40 +08001315void GetDeviceQueue(VkDevice device,
1316 uint32_t queueFamilyIndex,
1317 uint32_t queueIndex,
1318 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001319 ATRACE_CALL();
1320
Chia-I Wuba0be412016-03-24 16:24:40 +08001321 const auto& data = GetData(device);
1322
1323 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1324 SetData(*pQueue, data);
1325}
1326
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001327void GetDeviceQueue2(VkDevice device,
1328 const VkDeviceQueueInfo2* pQueueInfo,
1329 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001330 ATRACE_CALL();
1331
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001332 const auto& data = GetData(device);
1333
1334 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001335 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001336}
1337
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001338VkResult AllocateCommandBuffers(
1339 VkDevice device,
1340 const VkCommandBufferAllocateInfo* pAllocateInfo,
1341 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001342 ATRACE_CALL();
1343
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001344 const auto& data = GetData(device);
1345
1346 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1347 pCommandBuffers);
1348 if (result == VK_SUCCESS) {
1349 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1350 SetData(pCommandBuffers[i], data);
1351 }
1352
1353 return result;
1354}
1355
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001356VkResult QueueSubmit(VkQueue queue,
1357 uint32_t submitCount,
1358 const VkSubmitInfo* pSubmits,
1359 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001360 ATRACE_CALL();
1361
1362 const auto& data = GetData(queue);
1363
1364 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1365}
1366
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001367void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1368 VkPhysicalDeviceFeatures2* pFeatures) {
1369 ATRACE_CALL();
1370
1371 const auto& driver = GetData(physicalDevice).driver;
1372
1373 if (driver.GetPhysicalDeviceFeatures2) {
1374 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1375 return;
1376 }
1377
1378 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1379}
1380
1381void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1382 VkPhysicalDeviceProperties2* pProperties) {
1383 ATRACE_CALL();
1384
1385 const auto& driver = GetData(physicalDevice).driver;
1386
1387 if (driver.GetPhysicalDeviceProperties2) {
1388 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1389 return;
1390 }
1391
1392 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1393}
1394
1395void GetPhysicalDeviceFormatProperties2(
1396 VkPhysicalDevice physicalDevice,
1397 VkFormat format,
1398 VkFormatProperties2* pFormatProperties) {
1399 ATRACE_CALL();
1400
1401 const auto& driver = GetData(physicalDevice).driver;
1402
1403 if (driver.GetPhysicalDeviceFormatProperties2) {
1404 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1405 pFormatProperties);
1406 return;
1407 }
1408
1409 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1410 pFormatProperties);
1411}
1412
1413VkResult GetPhysicalDeviceImageFormatProperties2(
1414 VkPhysicalDevice physicalDevice,
1415 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1416 VkImageFormatProperties2* pImageFormatProperties) {
1417 ATRACE_CALL();
1418
1419 const auto& driver = GetData(physicalDevice).driver;
1420
1421 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1422 return driver.GetPhysicalDeviceImageFormatProperties2(
1423 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1424 }
1425
1426 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1427 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1428}
1429
1430void GetPhysicalDeviceQueueFamilyProperties2(
1431 VkPhysicalDevice physicalDevice,
1432 uint32_t* pQueueFamilyPropertyCount,
1433 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1434 ATRACE_CALL();
1435
1436 const auto& driver = GetData(physicalDevice).driver;
1437
1438 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1439 driver.GetPhysicalDeviceQueueFamilyProperties2(
1440 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1441 return;
1442 }
1443
1444 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1445 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1446}
1447
1448void GetPhysicalDeviceMemoryProperties2(
1449 VkPhysicalDevice physicalDevice,
1450 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1451 ATRACE_CALL();
1452
1453 const auto& driver = GetData(physicalDevice).driver;
1454
1455 if (driver.GetPhysicalDeviceMemoryProperties2) {
1456 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1457 pMemoryProperties);
1458 return;
1459 }
1460
1461 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1462 pMemoryProperties);
1463}
1464
1465void GetPhysicalDeviceSparseImageFormatProperties2(
1466 VkPhysicalDevice physicalDevice,
1467 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1468 uint32_t* pPropertyCount,
1469 VkSparseImageFormatProperties2* pProperties) {
1470 ATRACE_CALL();
1471
1472 const auto& driver = GetData(physicalDevice).driver;
1473
1474 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1475 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1476 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1477 return;
1478 }
1479
1480 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1481 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1482}
1483
Yiwei Zhange1f35012020-07-05 22:52:04 -07001484void GetPhysicalDeviceExternalBufferProperties(
1485 VkPhysicalDevice physicalDevice,
1486 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1487 VkExternalBufferProperties* pExternalBufferProperties) {
1488 ATRACE_CALL();
1489
1490 const auto& driver = GetData(physicalDevice).driver;
1491
1492 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1493 driver.GetPhysicalDeviceExternalBufferProperties(
1494 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1495 return;
1496 }
1497
1498 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1499 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1500 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1501 return;
1502 }
1503
1504 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1505 sizeof(VkExternalMemoryProperties));
1506}
1507
1508void GetPhysicalDeviceExternalSemaphoreProperties(
1509 VkPhysicalDevice physicalDevice,
1510 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1511 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1512 ATRACE_CALL();
1513
1514 const auto& driver = GetData(physicalDevice).driver;
1515
1516 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1517 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1518 physicalDevice, pExternalSemaphoreInfo,
1519 pExternalSemaphoreProperties);
1520 return;
1521 }
1522
1523 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1524 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1525 physicalDevice, pExternalSemaphoreInfo,
1526 pExternalSemaphoreProperties);
1527 return;
1528 }
1529
1530 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1531 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1532 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1533}
1534
1535void GetPhysicalDeviceExternalFenceProperties(
1536 VkPhysicalDevice physicalDevice,
1537 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1538 VkExternalFenceProperties* pExternalFenceProperties) {
1539 ATRACE_CALL();
1540
1541 const auto& driver = GetData(physicalDevice).driver;
1542
1543 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1544 driver.GetPhysicalDeviceExternalFenceProperties(
1545 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1546 return;
1547 }
1548
1549 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1550 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1551 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1552 return;
1553 }
1554
1555 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1556 pExternalFenceProperties->compatibleHandleTypes = 0;
1557 pExternalFenceProperties->externalFenceFeatures = 0;
1558}
1559
Chia-I Wu9d518162016-03-24 14:55:27 +08001560} // namespace driver
1561} // namespace vulkan