blob: 4068a16d80b2691a928988df258cfd51d8caedb0 [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
Yiwei Zhang901f8ee2020-07-31 13:18:49 -070087 bool ShouldUnloadBuiltinDriver();
88 void UnloadBuiltinDriver();
Chia-I Wu31938252016-05-23 15:31:02 +080089 bool InitDebugReportIndex();
90
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080091 static Hal hal_;
92
93 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080094 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080095};
96
Chia-I Wu4901db72016-03-24 16:38:58 +080097class CreateInfoWrapper {
98 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080099 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700100 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800101 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800102 CreateInfoWrapper(VkPhysicalDevice physical_dev,
103 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700104 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800105 const VkAllocationCallbacks& allocator);
106 ~CreateInfoWrapper();
107
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800108 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800109
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800110 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
111 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800112
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800113 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800114 explicit operator const VkDeviceCreateInfo*() const;
115
116 private:
117 struct ExtensionFilter {
118 VkExtensionProperties* exts;
119 uint32_t ext_count;
120
121 const char** names;
122 uint32_t name_count;
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700123 ExtensionFilter()
124 : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800125 };
126
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700127 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800128 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800129 VkResult SanitizeLayers();
130 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800131
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800132 VkResult QueryExtensionCount(uint32_t& count) const;
133 VkResult EnumerateExtensions(uint32_t& count,
134 VkExtensionProperties* props) const;
135 VkResult InitExtensionFilter();
136 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800137
138 const bool is_instance_;
139 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700140 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700141 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800142
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800143 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800144
145 union {
146 VkInstanceCreateInfo instance_info_;
147 VkDeviceCreateInfo dev_info_;
148 };
149
Ian Elliottf3e872d2017-11-02 10:15:13 -0600150 VkApplicationInfo application_info_;
151
Chia-I Wu4901db72016-03-24 16:38:58 +0800152 ExtensionFilter extension_filter_;
153
154 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
155 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
156};
157
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800158Hal Hal::hal_;
159
Jesse Hall53457db2016-12-14 16:54:06 -0800160void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700161 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800162 ATRACE_CALL();
163
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700164 std::stringstream ss;
165 ss << "vulkan." << subname << ".so";
166 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800167}
168
169const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700170 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800171 "ro.board.platform",
172}};
173
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800174// LoadDriver returns:
175// * 0 when succeed, or
176// * -ENOENT when fail to open binary libraries, or
177// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
178// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179int LoadDriver(android_namespace_t* library_namespace,
180 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800181 ATRACE_CALL();
182
Jesse Hall53457db2016-12-14 16:54:06 -0800183 const android_dlextinfo dlextinfo = {
184 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700185 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800186 };
Jesse Hall53457db2016-12-14 16:54:06 -0800187 void* so = nullptr;
188 char prop[PROPERTY_VALUE_MAX];
189 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
190 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700191 if (prop_len > 0 && prop_len <= UINT_MAX) {
192 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
193 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800194 if (so)
195 break;
196 }
197 }
198 if (!so)
199 return -ENOENT;
200
Jesse Hall00e61ff2017-04-07 16:48:02 -0700201 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800202 if (!hmi) {
203 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
204 dlclose(so);
205 return -EINVAL;
206 }
207 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
208 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
209 dlclose(so);
210 return -EINVAL;
211 }
212 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700213 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800214 return 0;
215}
216
Jesse Hall00e61ff2017-04-07 16:48:02 -0700217int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800218 ATRACE_CALL();
219
Jesse Hall00e61ff2017-04-07 16:48:02 -0700220 auto ns = android_get_exported_namespace("sphal");
221 if (!ns)
222 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800223 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700224 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700225 return LoadDriver(ns, module);
226}
227
228int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800229 ATRACE_CALL();
230
Jesse Hall00e61ff2017-04-07 16:48:02 -0700231 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
232 if (!ns)
233 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800234 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700235 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800236 int result = LoadDriver(ns, module);
237 if (result != 0) {
238 LOG_ALWAYS_FATAL(
239 "couldn't find an updated Vulkan implementation from %s",
240 android::GraphicsEnv::getInstance().getDriverPath().c_str());
241 }
242 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700243}
244
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800245bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800246 ATRACE_CALL();
247
Yiwei Zhangd9861812019-02-13 11:51:55 -0800248 const nsecs_t openTime = systemTime();
249
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700250 if (hal_.ShouldUnloadBuiltinDriver()) {
251 hal_.UnloadBuiltinDriver();
252 }
253
254 if (hal_.dev_)
255 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800256
257 // Use a stub device unless we successfully open a real HAL device.
258 hal_.dev_ = &stubhal::kDevice;
259
Jesse Hall53457db2016-12-14 16:54:06 -0800260 int result;
261 const hwvulkan_module_t* module = nullptr;
262
Jesse Hall00e61ff2017-04-07 16:48:02 -0700263 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800264 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700265 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800266 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800267 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800268 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700269 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800270 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800271 return true;
272 }
273
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800274
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800276 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800277 result =
278 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
279 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800280 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800281 if (result != 0) {
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, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800284 // Any device with a Vulkan HAL should be able to open the device.
285 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
286 result);
287 return false;
288 }
289
290 hal_.dev_ = device;
291
Chia-I Wu31938252016-05-23 15:31:02 +0800292 hal_.InitDebugReportIndex();
293
Yiwei Zhangd9861812019-02-13 11:51:55 -0800294 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700295 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800296
Chia-I Wu31938252016-05-23 15:31:02 +0800297 return true;
298}
299
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700300bool Hal::ShouldUnloadBuiltinDriver() {
301 // Should not unload since the driver was not loaded
302 if (!hal_.dev_)
303 return false;
304
305 // Should not unload if stubhal is used on the device
306 if (hal_.dev_ == &stubhal::kDevice)
307 return false;
308
309 // Unload the driver if updated driver is chosen
310 if (android::GraphicsEnv::getInstance().getDriverNamespace())
311 return true;
312
313 return false;
314}
315
316void Hal::UnloadBuiltinDriver() {
317 ATRACE_CALL();
318
319 ALOGD("Unload builtin Vulkan driver.");
320
321 // Close the opened device
322 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
323 "hw_device_t::close() failed.");
324
325 // Close the opened shared library in the hw_module_t
326 dlclose(hal_.dev_->common.module->dso);
327
328 hal_.dev_ = nullptr;
329 hal_.debug_report_index_ = -1;
330}
331
Chia-I Wu31938252016-05-23 15:31:02 +0800332bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800333 ATRACE_CALL();
334
Chia-I Wu31938252016-05-23 15:31:02 +0800335 uint32_t count;
336 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
337 VK_SUCCESS) {
338 ALOGE("failed to get HAL instance extension count");
339 return false;
340 }
341
342 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
343 malloc(sizeof(VkExtensionProperties) * count));
344 if (!exts) {
345 ALOGE("failed to allocate HAL instance extension array");
346 return false;
347 }
348
349 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
350 VK_SUCCESS) {
351 ALOGE("failed to enumerate HAL instance extensions");
352 free(exts);
353 return false;
354 }
355
356 for (uint32_t i = 0; i < count; i++) {
357 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
358 0) {
359 debug_report_index_ = static_cast<int>(i);
360 break;
361 }
362 }
363
364 free(exts);
365
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800366 return true;
367}
368
369CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700370 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800371 const VkAllocationCallbacks& allocator)
372 : is_instance_(true),
373 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700374 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700375 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800376 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800377 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700378 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800379
Chia-I Wu4901db72016-03-24 16:38:58 +0800380CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
381 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700382 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800383 const VkAllocationCallbacks& allocator)
384 : is_instance_(false),
385 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700386 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700387 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800388 physical_dev_(physical_dev),
389 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700390 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800391
392CreateInfoWrapper::~CreateInfoWrapper() {
393 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
394 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
395}
396
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800397VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700398 VkResult result = SanitizeApiVersion();
399 if (result == VK_SUCCESS)
400 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800401 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800402 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800403 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800404 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800405
406 return result;
407}
408
409const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800410CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800411 return hook_extensions_;
412}
413
414const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800415CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800416 return hal_extensions_;
417}
418
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800419CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
420 return &instance_info_;
421}
422
Chia-I Wu4901db72016-03-24 16:38:58 +0800423CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
424 return &dev_info_;
425}
426
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700427VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700428 if (!is_instance_ || !instance_info_.pApplicationInfo)
429 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700430
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700431 if (icd_api_version_ > VK_API_VERSION_1_0 ||
432 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
433 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700434
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700435 // override apiVersion to avoid error return from 1.0 icd
436 application_info_ = *instance_info_.pApplicationInfo;
437 application_info_.apiVersion = VK_API_VERSION_1_0;
438 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700439
440 return VK_SUCCESS;
441}
442
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800443VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800444 const struct StructHeader {
445 VkStructureType type;
446 const void* next;
447 } * header;
448
449 if (is_instance_) {
450 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
451
452 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
453 while (header &&
454 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
455 header = reinterpret_cast<const StructHeader*>(header->next);
456
457 instance_info_.pNext = header;
458 } else {
459 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
460
461 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
462 while (header &&
463 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
464 header = reinterpret_cast<const StructHeader*>(header->next);
465
466 dev_info_.pNext = header;
467 }
468
469 return VK_SUCCESS;
470}
471
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800472VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800473 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
474 : dev_info_.ppEnabledLayerNames;
475 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
476 : dev_info_.enabledLayerCount;
477
478 // remove all layers
479 layer_names = nullptr;
480 layer_count = 0;
481
482 return VK_SUCCESS;
483}
484
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800485VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800486 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
487 : dev_info_.ppEnabledExtensionNames;
488 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
489 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800490
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800491 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800492 if (result != VK_SUCCESS)
493 return result;
494
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700495 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700496 for (uint32_t i = 0; i < ext_count; i++) {
497 // Upon api downgrade, skip the promoted instance extensions in the
498 // first pass to avoid duplicate extensions.
499 const std::optional<uint32_t> version =
500 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700501 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700502 *version <= loader_api_version_)
503 continue;
504
505 FilterExtension(ext_names[i]);
506 }
507
508 // Enable the required extensions to support core functionalities.
509 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700510 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700511 for (const auto& promoted_extension : promoted_extensions)
512 FilterExtension(promoted_extension);
513 } else {
514 for (uint32_t i = 0; i < ext_count; i++)
515 FilterExtension(ext_names[i]);
516 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800517
Jesse Halld3d887a2018-03-05 13:34:45 -0800518 // Enable device extensions that contain physical-device commands, so that
519 // vkGetInstanceProcAddr will return those physical-device commands.
520 if (is_instance_) {
521 hook_extensions_.set(ProcHook::KHR_swapchain);
522 }
523
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700524 const uint32_t api_version =
525 is_instance_ ? loader_api_version_
526 : std::min(icd_api_version_, loader_api_version_);
527 switch (api_version) {
528 case VK_API_VERSION_1_1:
529 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
530 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
531 [[clang::fallthrough]];
532 case VK_API_VERSION_1_0:
533 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
534 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
535 break;
536 default:
537 ALOGE("Unknown API version[%u]", api_version);
538 break;
539 }
540
Chia-I Wu4901db72016-03-24 16:38:58 +0800541 ext_names = extension_filter_.names;
542 ext_count = extension_filter_.name_count;
543
544 return VK_SUCCESS;
545}
546
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800547VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800548 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800549 return Hal::Device().EnumerateInstanceExtensionProperties(
550 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800551 } else {
552 const auto& driver = GetData(physical_dev_).driver;
553 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
554 &count, nullptr);
555 }
556}
557
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800558VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800559 uint32_t& count,
560 VkExtensionProperties* props) const {
561 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800562 return Hal::Device().EnumerateInstanceExtensionProperties(
563 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800564 } else {
565 const auto& driver = GetData(physical_dev_).driver;
566 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
567 &count, props);
568 }
569}
570
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800571VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800572 // query extension count
573 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800574 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800575 if (result != VK_SUCCESS || count == 0)
576 return result;
577
578 auto& filter = extension_filter_;
579 filter.exts =
580 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
581 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
582 alignof(VkExtensionProperties),
583 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
584 if (!filter.exts)
585 return VK_ERROR_OUT_OF_HOST_MEMORY;
586
587 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800588 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800589 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
590 return result;
591
592 if (!count)
593 return VK_SUCCESS;
594
595 filter.ext_count = count;
596
597 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700598 if (is_instance_) {
599 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
600
601 // It requires enabling additional promoted extensions to downgrade api,
602 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700603 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700604 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700605 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700606 }
607
608 count = std::min(filter.ext_count, enabled_ext_count);
609 } else {
610 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
611 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700612
613 if (!count)
614 return VK_SUCCESS;
615
Chia-I Wu4901db72016-03-24 16:38:58 +0800616 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
617 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
618 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
619 if (!filter.names)
620 return VK_ERROR_OUT_OF_HOST_MEMORY;
621
622 return VK_SUCCESS;
623}
624
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800625void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800626 auto& filter = extension_filter_;
627
628 ProcHook::Extension ext_bit = GetProcHookExtension(name);
629 if (is_instance_) {
630 switch (ext_bit) {
631 case ProcHook::KHR_android_surface:
632 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700633 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300634 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800635 hook_extensions_.set(ext_bit);
636 // return now as these extensions do not require HAL support
637 return;
638 case ProcHook::EXT_debug_report:
639 // both we and HAL can take part in
640 hook_extensions_.set(ext_bit);
641 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300642 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700643 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700644 case ProcHook::KHR_external_memory_capabilities:
645 case ProcHook::KHR_external_semaphore_capabilities:
646 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700647 case ProcHook::EXTENSION_UNKNOWN:
648 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800649 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700650
Yiwei Zhang23143102019-04-10 18:24:05 -0700651 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700652 case ProcHook::KHR_incremental_present:
653 case ProcHook::KHR_shared_presentable_image:
654 case ProcHook::KHR_swapchain:
655 case ProcHook::EXT_hdr_metadata:
656 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
657 case ProcHook::ANDROID_native_buffer:
658 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700659 case ProcHook::EXTENSION_CORE_1_0:
660 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700661 case ProcHook::EXTENSION_COUNT:
662 // Device and meta extensions. If we ever get here it's a bug in
663 // our code. But enumerating them lets us avoid having a default
664 // case, and default hides other bugs.
665 ALOGE(
666 "CreateInfoWrapper::FilterExtension: invalid instance "
667 "extension '%s'. FIX ME",
668 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800669 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700670
671 // Don't use a default case. Without it, -Wswitch will tell us
672 // at compile time if someone adds a new ProcHook extension but
673 // doesn't handle it above. That's a real bug that has
674 // not-immediately-obvious effects.
675 //
676 // default:
677 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800678 }
679 } else {
680 switch (ext_bit) {
681 case ProcHook::KHR_swapchain:
682 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
683 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
684 ext_bit = ProcHook::ANDROID_native_buffer;
685 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700686 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700687 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300688 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700689 hook_extensions_.set(ext_bit);
690 // return now as these extensions do not require HAL support
691 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700692 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700693 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700694 hook_extensions_.set(ext_bit);
695 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700696 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800697 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700698 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800699 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700700
701 case ProcHook::KHR_android_surface:
702 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700703 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700704 case ProcHook::KHR_external_memory_capabilities:
705 case ProcHook::KHR_external_semaphore_capabilities:
706 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700707 case ProcHook::KHR_get_surface_capabilities2:
708 case ProcHook::KHR_surface:
709 case ProcHook::EXT_debug_report:
710 case ProcHook::EXT_swapchain_colorspace:
711 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700712 case ProcHook::EXTENSION_CORE_1_0:
713 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700714 case ProcHook::EXTENSION_COUNT:
715 // Instance and meta extensions. If we ever get here it's a bug
716 // in our code. But enumerating them lets us avoid having a
717 // default case, and default hides other bugs.
718 ALOGE(
719 "CreateInfoWrapper::FilterExtension: invalid device "
720 "extension '%s'. FIX ME",
721 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800722 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700723
724 // Don't use a default case. Without it, -Wswitch will tell us
725 // at compile time if someone adds a new ProcHook extension but
726 // doesn't handle it above. That's a real bug that has
727 // not-immediately-obvious effects.
728 //
729 // default:
730 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800731 }
732 }
733
734 for (uint32_t i = 0; i < filter.ext_count; i++) {
735 const VkExtensionProperties& props = filter.exts[i];
736 // ignore unknown extensions
737 if (strcmp(name, props.extensionName) != 0)
738 continue;
739
Chia-I Wu4901db72016-03-24 16:38:58 +0800740 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800741 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
742 if (ext_bit == ProcHook::ANDROID_native_buffer)
743 hook_extensions_.set(ProcHook::KHR_swapchain);
744
745 hal_extensions_.set(ext_bit);
746 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800747
748 break;
749 }
750}
751
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800752VKAPI_ATTR void* DefaultAllocate(void*,
753 size_t size,
754 size_t alignment,
755 VkSystemAllocationScope) {
756 void* ptr = nullptr;
757 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
758 // additionally requires that it be at least sizeof(void*).
759 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
760 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
761 ret, ptr);
762 return ret == 0 ? ptr : nullptr;
763}
764
765VKAPI_ATTR void* DefaultReallocate(void*,
766 void* ptr,
767 size_t size,
768 size_t alignment,
769 VkSystemAllocationScope) {
770 if (size == 0) {
771 free(ptr);
772 return nullptr;
773 }
774
Yiwei Zhanga885c062019-10-24 12:07:57 -0700775 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800776 // request is smaller than the existing chunk, we just continue using it.
777 // Right now the loader never reallocs, so this doesn't matter. If that
778 // changes, or if this code is copied into some other project, this should
779 // probably have a heuristic to allocate-copy-free when doing so will save
780 // "enough" space.
781 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
782 if (size <= old_size)
783 return ptr;
784
785 void* new_ptr = nullptr;
786 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
787 return nullptr;
788 if (ptr) {
789 memcpy(new_ptr, ptr, std::min(old_size, size));
790 free(ptr);
791 }
792 return new_ptr;
793}
794
795VKAPI_ATTR void DefaultFree(void*, void* ptr) {
796 ALOGD_CALLSTACK("Free: %p", ptr);
797 free(ptr);
798}
799
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800800InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
801 void* data_mem = allocator.pfnAllocation(
802 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
803 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
804 if (!data_mem)
805 return nullptr;
806
807 return new (data_mem) InstanceData(allocator);
808}
809
810void FreeInstanceData(InstanceData* data,
811 const VkAllocationCallbacks& allocator) {
812 data->~InstanceData();
813 allocator.pfnFree(allocator.pUserData, data);
814}
815
Chia-I Wu950d6e12016-05-03 09:12:35 +0800816DeviceData* AllocateDeviceData(
817 const VkAllocationCallbacks& allocator,
818 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800819 void* data_mem = allocator.pfnAllocation(
820 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
821 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
822 if (!data_mem)
823 return nullptr;
824
Chia-I Wu950d6e12016-05-03 09:12:35 +0800825 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800826}
827
828void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
829 data->~DeviceData();
830 allocator.pfnFree(allocator.pUserData, data);
831}
832
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800833} // anonymous namespace
834
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800835bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800836 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800837}
838
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800839const VkAllocationCallbacks& GetDefaultAllocator() {
840 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
841 .pUserData = nullptr,
842 .pfnAllocation = DefaultAllocate,
843 .pfnReallocation = DefaultReallocate,
844 .pfnFree = DefaultFree,
845 };
846
847 return kDefaultAllocCallbacks;
848}
849
Chia-I Wueb7db122016-03-24 09:11:06 +0800850PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
851 const ProcHook* hook = GetProcHook(pName);
852 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800853 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800854
855 if (!instance) {
856 if (hook->type == ProcHook::GLOBAL)
857 return hook->proc;
858
Chia-I Wu109f8982016-04-22 06:40:40 +0800859 // v0 layers expect
860 //
861 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
862 //
863 // to work.
864 if (strcmp(pName, "vkCreateDevice") == 0)
865 return hook->proc;
866
Chia-I Wueb7db122016-03-24 09:11:06 +0800867 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800868 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800869 pName);
870
Chia-I Wu109f8982016-04-22 06:40:40 +0800871 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800872 }
873
874 PFN_vkVoidFunction proc;
875
876 switch (hook->type) {
877 case ProcHook::INSTANCE:
878 proc = (GetData(instance).hook_extensions[hook->extension])
879 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800880 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800881 break;
882 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700883 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800884 ? hook->proc
885 : hook->checked_proc;
886 break;
887 default:
888 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800889 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800890 pName);
891 proc = nullptr;
892 break;
893 }
894
895 return proc;
896}
897
898PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
899 const ProcHook* hook = GetProcHook(pName);
900 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800901 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800902
903 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800904 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800905 return nullptr;
906 }
907
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800908 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
909 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800910}
911
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800912VkResult EnumerateInstanceExtensionProperties(
913 const char* pLayerName,
914 uint32_t* pPropertyCount,
915 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700916 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600917 loader_extensions.push_back({
918 VK_KHR_SURFACE_EXTENSION_NAME,
919 VK_KHR_SURFACE_SPEC_VERSION});
920 loader_extensions.push_back({
921 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
922 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
923 loader_extensions.push_back({
924 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
925 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700926 loader_extensions.push_back({
927 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
928 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600929
Chia-I Wu31938252016-05-23 15:31:02 +0800930 static const VkExtensionProperties loader_debug_report_extension = {
931 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
932 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800933
934 // enumerate our extensions first
935 if (!pLayerName && pProperties) {
936 uint32_t count = std::min(
937 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
938
Yiwei Zhang5e862202019-06-21 14:59:16 -0700939 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800940
941 if (count < loader_extensions.size()) {
942 *pPropertyCount = count;
943 return VK_INCOMPLETE;
944 }
945
946 pProperties += count;
947 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800948
949 if (Hal::Get().GetDebugReportIndex() < 0) {
950 if (!*pPropertyCount) {
951 *pPropertyCount = count;
952 return VK_INCOMPLETE;
953 }
954
955 pProperties[0] = loader_debug_report_extension;
956 pProperties += 1;
957 *pPropertyCount -= 1;
958 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800959 }
960
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800961 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800962 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800963 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800964 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800965
Chia-I Wu31938252016-05-23 15:31:02 +0800966 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
967 int idx = Hal::Get().GetDebugReportIndex();
968 if (idx < 0) {
969 *pPropertyCount += 1;
970 } else if (pProperties &&
971 static_cast<uint32_t>(idx) < *pPropertyCount) {
972 pProperties[idx].specVersion =
973 std::min(pProperties[idx].specVersion,
974 loader_debug_report_extension.specVersion);
975 }
976
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800977 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800978 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800979
980 return result;
981}
982
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700983void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +1300984 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700985 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300986 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700987 VkPhysicalDeviceProperties2 properties = {
988 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +1300989 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700990 {},
Chris Forbesfa25e632017-02-22 12:36:02 +1300991 };
992
993#pragma clang diagnostic push
994#pragma clang diagnostic ignored "-Wold-style-cast"
995 presentation_properties->sType =
996 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
997#pragma clang diagnostic pop
998 presentation_properties->pNext = nullptr;
999 presentation_properties->sharedImage = VK_FALSE;
1000
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001001 GetPhysicalDeviceProperties2(physicalDevice, &properties);
Chris Forbesfa25e632017-02-22 12:36:02 +13001002}
1003
Chia-I Wu01cf3052016-03-24 16:16:21 +08001004VkResult EnumerateDeviceExtensionProperties(
1005 VkPhysicalDevice physicalDevice,
1006 const char* pLayerName,
1007 uint32_t* pPropertyCount,
1008 VkExtensionProperties* pProperties) {
1009 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001010 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001011 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001012 loader_extensions.push_back({
1013 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1014 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001015
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001016 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001017 if (hdrBoardConfig) {
1018 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1019 VK_EXT_HDR_METADATA_SPEC_VERSION});
1020 }
1021
Chris Forbes16095002017-05-05 15:33:29 -07001022 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001023 QueryPresentationProperties(physicalDevice, &presentation_properties);
1024 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001025 loader_extensions.push_back({
1026 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1027 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001028 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001029
Ian Elliott5c34de22017-04-10 14:42:30 -06001030 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1031 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001032 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001033 loader_extensions.push_back({
1034 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1035 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1036 }
1037
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001038 // enumerate our extensions first
1039 if (!pLayerName && pProperties) {
1040 uint32_t count = std::min(
1041 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1042
Yiwei Zhang5e862202019-06-21 14:59:16 -07001043 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001044
1045 if (count < loader_extensions.size()) {
1046 *pPropertyCount = count;
1047 return VK_INCOMPLETE;
1048 }
1049
1050 pProperties += count;
1051 *pPropertyCount -= count;
1052 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001053
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001054 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001055 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1056 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001057 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001058
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001059 if (pProperties) {
1060 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1061 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1062 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001063
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001064 if (strcmp(prop.extensionName,
1065 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1066 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001067
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001068 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1069 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001070
1071 if (prop.specVersion >= 8) {
1072 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1073 } else {
1074 prop.specVersion = 68;
1075 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001076 }
1077 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001078
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001079 // restore loader extension count
1080 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1081 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001082 }
1083
1084 return result;
1085}
1086
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001087VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1088 const VkAllocationCallbacks* pAllocator,
1089 VkInstance* pInstance) {
1090 const VkAllocationCallbacks& data_allocator =
1091 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1092
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001093 VkResult result = VK_SUCCESS;
1094 uint32_t icd_api_version = VK_API_VERSION_1_0;
1095 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1096 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1097 Hal::Device().GetInstanceProcAddr(nullptr,
1098 "vkEnumerateInstanceVersion"));
1099 if (pfn_enumerate_instance_version) {
1100 ATRACE_BEGIN("pfn_enumerate_instance_version");
1101 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1102 ATRACE_END();
1103 if (result != VK_SUCCESS)
1104 return result;
1105
1106 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
1107 }
1108
1109 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1110 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001111 if (result != VK_SUCCESS)
1112 return result;
1113
1114 InstanceData* data = AllocateInstanceData(data_allocator);
1115 if (!data)
1116 return VK_ERROR_OUT_OF_HOST_MEMORY;
1117
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001118 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001119
1120 // call into the driver
1121 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001122 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001123 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001124 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1125 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001126 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001127 if (result != VK_SUCCESS) {
1128 FreeInstanceData(data, data_allocator);
1129 return result;
1130 }
1131
1132 // initialize InstanceDriverTable
1133 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001134 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001135 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001136 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001137 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001138 if (data->driver.DestroyInstance)
1139 data->driver.DestroyInstance(instance, pAllocator);
1140
1141 FreeInstanceData(data, data_allocator);
1142
1143 return VK_ERROR_INCOMPATIBLE_DRIVER;
1144 }
1145
1146 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001147 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001148 if (!data->get_device_proc_addr) {
1149 data->driver.DestroyInstance(instance, pAllocator);
1150 FreeInstanceData(data, data_allocator);
1151
1152 return VK_ERROR_INCOMPATIBLE_DRIVER;
1153 }
1154
1155 *pInstance = instance;
1156
1157 return VK_SUCCESS;
1158}
1159
1160void DestroyInstance(VkInstance instance,
1161 const VkAllocationCallbacks* pAllocator) {
1162 InstanceData& data = GetData(instance);
1163 data.driver.DestroyInstance(instance, pAllocator);
1164
1165 VkAllocationCallbacks local_allocator;
1166 if (!pAllocator) {
1167 local_allocator = data.allocator;
1168 pAllocator = &local_allocator;
1169 }
1170
1171 FreeInstanceData(&data, *pAllocator);
1172}
1173
Chia-I Wu4901db72016-03-24 16:38:58 +08001174VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1175 const VkDeviceCreateInfo* pCreateInfo,
1176 const VkAllocationCallbacks* pAllocator,
1177 VkDevice* pDevice) {
1178 const InstanceData& instance_data = GetData(physicalDevice);
1179 const VkAllocationCallbacks& data_allocator =
1180 (pAllocator) ? *pAllocator : instance_data.allocator;
1181
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001182 VkPhysicalDeviceProperties properties;
1183 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1184 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1185 &properties);
1186 ATRACE_END();
1187
1188 CreateInfoWrapper wrapper(
1189 physicalDevice, *pCreateInfo,
1190 properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion),
1191 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001192 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001193 if (result != VK_SUCCESS)
1194 return result;
1195
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001196 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001197 DeviceData* data = AllocateDeviceData(data_allocator,
1198 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001199 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001200 if (!data)
1201 return VK_ERROR_OUT_OF_HOST_MEMORY;
1202
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001203 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001204
1205 // call into the driver
1206 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001207 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001208 result = instance_data.driver.CreateDevice(
1209 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1210 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001211 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001212 if (result != VK_SUCCESS) {
1213 FreeDeviceData(data, data_allocator);
1214 return result;
1215 }
1216
1217 // initialize DeviceDriverTable
1218 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001219 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1220 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001221 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1222 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1223 if (data->driver.DestroyDevice)
1224 data->driver.DestroyDevice(dev, pAllocator);
1225
1226 FreeDeviceData(data, data_allocator);
1227
1228 return VK_ERROR_INCOMPATIBLE_DRIVER;
1229 }
Chris Forbesd8277912017-02-10 14:59:59 +13001230
1231 // sanity check ANDROID_native_buffer implementation, whose set of
1232 // entrypoints varies according to the spec version.
1233 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1234 !data->driver.GetSwapchainGrallocUsageANDROID &&
1235 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1236 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1237 " must expose at least one of "
1238 "vkGetSwapchainGrallocUsageANDROID or "
1239 "vkGetSwapchainGrallocUsage2ANDROID");
1240
1241 data->driver.DestroyDevice(dev, pAllocator);
1242 FreeDeviceData(data, data_allocator);
1243
1244 return VK_ERROR_INCOMPATIBLE_DRIVER;
1245 }
1246
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001247 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1248 // Log that the app is hitting software Vulkan implementation
1249 android::GraphicsEnv::getInstance().setTargetStats(
1250 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1251 }
1252
Jesse Halldc225072016-05-30 22:40:14 -07001253 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001254
1255 *pDevice = dev;
1256
1257 return VK_SUCCESS;
1258}
1259
1260void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1261 DeviceData& data = GetData(device);
1262 data.driver.DestroyDevice(device, pAllocator);
1263
1264 VkAllocationCallbacks local_allocator;
1265 if (!pAllocator) {
1266 local_allocator = data.allocator;
1267 pAllocator = &local_allocator;
1268 }
1269
1270 FreeDeviceData(&data, *pAllocator);
1271}
1272
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001273VkResult EnumeratePhysicalDevices(VkInstance instance,
1274 uint32_t* pPhysicalDeviceCount,
1275 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001276 ATRACE_CALL();
1277
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001278 const auto& data = GetData(instance);
1279
1280 VkResult result = data.driver.EnumeratePhysicalDevices(
1281 instance, pPhysicalDeviceCount, pPhysicalDevices);
1282 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1283 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1284 SetData(pPhysicalDevices[i], data);
1285 }
1286
1287 return result;
1288}
1289
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001290VkResult EnumeratePhysicalDeviceGroups(
1291 VkInstance instance,
1292 uint32_t* pPhysicalDeviceGroupCount,
1293 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001294 ATRACE_CALL();
1295
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001296 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001297 const auto& data = GetData(instance);
1298
Yiwei Zhange4f64172020-07-05 15:17:32 -07001299 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1300 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001301 uint32_t device_count = 0;
1302 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1303 if (result < 0)
1304 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001305
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001306 if (!pPhysicalDeviceGroupProperties) {
1307 *pPhysicalDeviceGroupCount = device_count;
1308 return result;
1309 }
1310
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001311 if (!device_count) {
1312 *pPhysicalDeviceGroupCount = 0;
1313 return result;
1314 }
Chad Versace32c087f2018-09-09 07:28:05 -07001315 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1316 if (!device_count)
1317 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001318
Yiwei Zhang5e862202019-06-21 14:59:16 -07001319 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001320 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001321 result =
1322 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001323 if (result < 0)
1324 return result;
1325
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001326 for (uint32_t i = 0; i < device_count; ++i) {
1327 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1328 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1329 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1330 }
1331 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001332 if (data.driver.EnumeratePhysicalDeviceGroups) {
1333 result = data.driver.EnumeratePhysicalDeviceGroups(
1334 instance, pPhysicalDeviceGroupCount,
1335 pPhysicalDeviceGroupProperties);
1336 } else {
1337 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1338 instance, pPhysicalDeviceGroupCount,
1339 pPhysicalDeviceGroupProperties);
1340 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001341 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1342 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1343 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1344 for (uint32_t j = 0;
1345 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1346 j++) {
1347 SetData(
1348 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001349 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001350 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001351 }
1352 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001353 }
1354
1355 return result;
1356}
1357
Chia-I Wuba0be412016-03-24 16:24:40 +08001358void GetDeviceQueue(VkDevice device,
1359 uint32_t queueFamilyIndex,
1360 uint32_t queueIndex,
1361 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001362 ATRACE_CALL();
1363
Chia-I Wuba0be412016-03-24 16:24:40 +08001364 const auto& data = GetData(device);
1365
1366 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1367 SetData(*pQueue, data);
1368}
1369
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001370void GetDeviceQueue2(VkDevice device,
1371 const VkDeviceQueueInfo2* pQueueInfo,
1372 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001373 ATRACE_CALL();
1374
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001375 const auto& data = GetData(device);
1376
1377 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001378 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001379}
1380
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001381VkResult AllocateCommandBuffers(
1382 VkDevice device,
1383 const VkCommandBufferAllocateInfo* pAllocateInfo,
1384 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001385 ATRACE_CALL();
1386
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001387 const auto& data = GetData(device);
1388
1389 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1390 pCommandBuffers);
1391 if (result == VK_SUCCESS) {
1392 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1393 SetData(pCommandBuffers[i], data);
1394 }
1395
1396 return result;
1397}
1398
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001399VkResult QueueSubmit(VkQueue queue,
1400 uint32_t submitCount,
1401 const VkSubmitInfo* pSubmits,
1402 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001403 ATRACE_CALL();
1404
1405 const auto& data = GetData(queue);
1406
1407 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1408}
1409
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001410void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1411 VkPhysicalDeviceFeatures2* pFeatures) {
1412 ATRACE_CALL();
1413
1414 const auto& driver = GetData(physicalDevice).driver;
1415
1416 if (driver.GetPhysicalDeviceFeatures2) {
1417 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1418 return;
1419 }
1420
1421 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1422}
1423
1424void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1425 VkPhysicalDeviceProperties2* pProperties) {
1426 ATRACE_CALL();
1427
1428 const auto& driver = GetData(physicalDevice).driver;
1429
1430 if (driver.GetPhysicalDeviceProperties2) {
1431 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1432 return;
1433 }
1434
1435 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1436}
1437
1438void GetPhysicalDeviceFormatProperties2(
1439 VkPhysicalDevice physicalDevice,
1440 VkFormat format,
1441 VkFormatProperties2* pFormatProperties) {
1442 ATRACE_CALL();
1443
1444 const auto& driver = GetData(physicalDevice).driver;
1445
1446 if (driver.GetPhysicalDeviceFormatProperties2) {
1447 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1448 pFormatProperties);
1449 return;
1450 }
1451
1452 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1453 pFormatProperties);
1454}
1455
1456VkResult GetPhysicalDeviceImageFormatProperties2(
1457 VkPhysicalDevice physicalDevice,
1458 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1459 VkImageFormatProperties2* pImageFormatProperties) {
1460 ATRACE_CALL();
1461
1462 const auto& driver = GetData(physicalDevice).driver;
1463
1464 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1465 return driver.GetPhysicalDeviceImageFormatProperties2(
1466 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1467 }
1468
1469 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1470 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1471}
1472
1473void GetPhysicalDeviceQueueFamilyProperties2(
1474 VkPhysicalDevice physicalDevice,
1475 uint32_t* pQueueFamilyPropertyCount,
1476 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1477 ATRACE_CALL();
1478
1479 const auto& driver = GetData(physicalDevice).driver;
1480
1481 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1482 driver.GetPhysicalDeviceQueueFamilyProperties2(
1483 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1484 return;
1485 }
1486
1487 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1488 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1489}
1490
1491void GetPhysicalDeviceMemoryProperties2(
1492 VkPhysicalDevice physicalDevice,
1493 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1494 ATRACE_CALL();
1495
1496 const auto& driver = GetData(physicalDevice).driver;
1497
1498 if (driver.GetPhysicalDeviceMemoryProperties2) {
1499 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1500 pMemoryProperties);
1501 return;
1502 }
1503
1504 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1505 pMemoryProperties);
1506}
1507
1508void GetPhysicalDeviceSparseImageFormatProperties2(
1509 VkPhysicalDevice physicalDevice,
1510 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1511 uint32_t* pPropertyCount,
1512 VkSparseImageFormatProperties2* pProperties) {
1513 ATRACE_CALL();
1514
1515 const auto& driver = GetData(physicalDevice).driver;
1516
1517 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1518 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1519 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1520 return;
1521 }
1522
1523 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1524 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1525}
1526
Yiwei Zhange1f35012020-07-05 22:52:04 -07001527void GetPhysicalDeviceExternalBufferProperties(
1528 VkPhysicalDevice physicalDevice,
1529 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1530 VkExternalBufferProperties* pExternalBufferProperties) {
1531 ATRACE_CALL();
1532
1533 const auto& driver = GetData(physicalDevice).driver;
1534
1535 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1536 driver.GetPhysicalDeviceExternalBufferProperties(
1537 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1538 return;
1539 }
1540
1541 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1542 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1543 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1544 return;
1545 }
1546
1547 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1548 sizeof(VkExternalMemoryProperties));
1549}
1550
1551void GetPhysicalDeviceExternalSemaphoreProperties(
1552 VkPhysicalDevice physicalDevice,
1553 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1554 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1555 ATRACE_CALL();
1556
1557 const auto& driver = GetData(physicalDevice).driver;
1558
1559 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1560 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1561 physicalDevice, pExternalSemaphoreInfo,
1562 pExternalSemaphoreProperties);
1563 return;
1564 }
1565
1566 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1567 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1568 physicalDevice, pExternalSemaphoreInfo,
1569 pExternalSemaphoreProperties);
1570 return;
1571 }
1572
1573 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1574 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1575 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1576}
1577
1578void GetPhysicalDeviceExternalFenceProperties(
1579 VkPhysicalDevice physicalDevice,
1580 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1581 VkExternalFenceProperties* pExternalFenceProperties) {
1582 ATRACE_CALL();
1583
1584 const auto& driver = GetData(physicalDevice).driver;
1585
1586 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1587 driver.GetPhysicalDeviceExternalFenceProperties(
1588 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1589 return;
1590 }
1591
1592 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1593 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1594 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1595 return;
1596 }
1597
1598 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1599 pExternalFenceProperties->compatibleHandleTypes = 0;
1600 pExternalFenceProperties->externalFenceFeatures = 0;
1601}
1602
Chia-I Wu9d518162016-03-24 14:55:27 +08001603} // namespace driver
1604} // namespace vulkan