blob: 535e19063f2376f16812c773c31e769a75c2072f [file] [log] [blame]
Chia-I Wu9d518162016-03-24 14:55:27 +08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang5e862202019-06-21 14:59:16 -070019#include "driver.h"
20
21#include <dlfcn.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <stdlib.h>
24#include <string.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080025
Sundong Ahnbc37dd52020-04-23 21:21:00 +090026#include <SurfaceFlingerProperties.h>
27#include <android-base/properties.h>
Jesse Hall53457db2016-12-14 16:54:06 -080028#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
30#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080031#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090032#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070033#include <log/log.h>
Yiwei Zhange40dd732019-08-05 16:41:03 -070034#include <nativeloader/dlext_namespaces.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070035#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080036#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080037#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080038
Yiwei Zhang5e862202019-06-21 14:59:16 -070039#include <algorithm>
40#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070041#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070042#include <new>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070043#include <string_view>
44#include <sstream>
Yiwei Zhang5e862202019-06-21 14:59:16 -070045#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070046
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070047#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080048
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080049using namespace android::hardware::configstore;
50using namespace android::hardware::configstore::V1_0;
51
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080052// #define ENABLE_ALLOC_CALLSTACKS 1
53#if ENABLE_ALLOC_CALLSTACKS
54#include <utils/CallStack.h>
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 ALOGD(__VA_ARGS__); \
58 android::CallStack callstack; \
59 callstack.update(); \
60 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
61 } while (false)
62#else
63#define ALOGD_CALLSTACK(...) \
64 do { \
65 } while (false)
66#endif
67
Chia-I Wu9d518162016-03-24 14:55:27 +080068namespace vulkan {
69namespace driver {
70
Chia-I Wu136b8eb2016-03-24 15:01:52 +080071namespace {
72
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080073class Hal {
74 public:
75 static bool Open();
76
77 static const Hal& Get() { return hal_; }
78 static const hwvulkan_device_t& Device() { return *Get().dev_; }
79
Chia-I Wu31938252016-05-23 15:31:02 +080080 int GetDebugReportIndex() const { return debug_report_index_; }
81
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080082 private:
Chia-I Wu31938252016-05-23 15:31:02 +080083 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080084 Hal(const Hal&) = delete;
85 Hal& operator=(const Hal&) = delete;
86
Chia-I Wu31938252016-05-23 15:31:02 +080087 bool InitDebugReportIndex();
88
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 static Hal hal_;
90
91 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080092 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093};
94
Chia-I Wu4901db72016-03-24 16:38:58 +080095class CreateInfoWrapper {
96 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080097 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080098 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080099 CreateInfoWrapper(VkPhysicalDevice physical_dev,
100 const VkDeviceCreateInfo& create_info,
101 const VkAllocationCallbacks& allocator);
102 ~CreateInfoWrapper();
103
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800104 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800105
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800106 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
107 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800108
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800109 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800110 explicit operator const VkDeviceCreateInfo*() const;
111
112 private:
113 struct ExtensionFilter {
114 VkExtensionProperties* exts;
115 uint32_t ext_count;
116
117 const char** names;
118 uint32_t name_count;
119 };
120
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700121 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800122 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800123 VkResult SanitizeLayers();
124 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800125
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800126 VkResult QueryExtensionCount(uint32_t& count) const;
127 VkResult EnumerateExtensions(uint32_t& count,
128 VkExtensionProperties* props) const;
129 VkResult InitExtensionFilter();
130 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800131
132 const bool is_instance_;
133 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700134 const uint32_t loader_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800135
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800136 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800137
138 union {
139 VkInstanceCreateInfo instance_info_;
140 VkDeviceCreateInfo dev_info_;
141 };
142
Ian Elliottf3e872d2017-11-02 10:15:13 -0600143 VkApplicationInfo application_info_;
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700144 uint32_t sanitized_api_version_;
Ian Elliottf3e872d2017-11-02 10:15:13 -0600145
Chia-I Wu4901db72016-03-24 16:38:58 +0800146 ExtensionFilter extension_filter_;
147
148 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
149 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
150};
151
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800152Hal Hal::hal_;
153
Jesse Hall53457db2016-12-14 16:54:06 -0800154void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700155 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800156 ATRACE_CALL();
157
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700158 std::stringstream ss;
159 ss << "vulkan." << subname << ".so";
160 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800161}
162
163const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700164 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800165 "ro.board.platform",
166}};
167
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800168// LoadDriver returns:
169// * 0 when succeed, or
170// * -ENOENT when fail to open binary libraries, or
171// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
172// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700173int LoadDriver(android_namespace_t* library_namespace,
174 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800175 ATRACE_CALL();
176
Jesse Hall53457db2016-12-14 16:54:06 -0800177 const android_dlextinfo dlextinfo = {
178 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800180 };
Jesse Hall53457db2016-12-14 16:54:06 -0800181 void* so = nullptr;
182 char prop[PROPERTY_VALUE_MAX];
183 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
184 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700185 if (prop_len > 0 && prop_len <= UINT_MAX) {
186 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
187 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800188 if (so)
189 break;
190 }
191 }
192 if (!so)
193 return -ENOENT;
194
Jesse Hall00e61ff2017-04-07 16:48:02 -0700195 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800196 if (!hmi) {
197 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
198 dlclose(so);
199 return -EINVAL;
200 }
201 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
202 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
203 dlclose(so);
204 return -EINVAL;
205 }
206 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700207 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800208 return 0;
209}
210
Jesse Hall00e61ff2017-04-07 16:48:02 -0700211int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800212 ATRACE_CALL();
213
Jesse Hall00e61ff2017-04-07 16:48:02 -0700214 auto ns = android_get_exported_namespace("sphal");
215 if (!ns)
216 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700218 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700219 return LoadDriver(ns, module);
220}
221
222int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800223 ATRACE_CALL();
224
Jesse Hall00e61ff2017-04-07 16:48:02 -0700225 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
226 if (!ns)
227 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700229 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800230 int result = LoadDriver(ns, module);
231 if (result != 0) {
232 LOG_ALWAYS_FATAL(
233 "couldn't find an updated Vulkan implementation from %s",
234 android::GraphicsEnv::getInstance().getDriverPath().c_str());
235 }
236 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700237}
238
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800239bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800240 ATRACE_CALL();
241
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 const nsecs_t openTime = systemTime();
243
Jesse Halldc225072016-05-30 22:40:14 -0700244 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800245
246 // Use a stub device unless we successfully open a real HAL device.
247 hal_.dev_ = &stubhal::kDevice;
248
Jesse Hall53457db2016-12-14 16:54:06 -0800249 int result;
250 const hwvulkan_module_t* module = nullptr;
251
Jesse Hall00e61ff2017-04-07 16:48:02 -0700252 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800253 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700254 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800255 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800256 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800257 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700258 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800259 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800260 return true;
261 }
262
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800263
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800264 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800265 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800266 result =
267 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
268 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800269 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800270 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800271 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700272 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800273 // Any device with a Vulkan HAL should be able to open the device.
274 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
275 result);
276 return false;
277 }
278
279 hal_.dev_ = device;
280
Chia-I Wu31938252016-05-23 15:31:02 +0800281 hal_.InitDebugReportIndex();
282
Yiwei Zhangd9861812019-02-13 11:51:55 -0800283 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700284 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800285
Chia-I Wu31938252016-05-23 15:31:02 +0800286 return true;
287}
288
289bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800290 ATRACE_CALL();
291
Chia-I Wu31938252016-05-23 15:31:02 +0800292 uint32_t count;
293 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
294 VK_SUCCESS) {
295 ALOGE("failed to get HAL instance extension count");
296 return false;
297 }
298
299 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
300 malloc(sizeof(VkExtensionProperties) * count));
301 if (!exts) {
302 ALOGE("failed to allocate HAL instance extension array");
303 return false;
304 }
305
306 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
307 VK_SUCCESS) {
308 ALOGE("failed to enumerate HAL instance extensions");
309 free(exts);
310 return false;
311 }
312
313 for (uint32_t i = 0; i < count; i++) {
314 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
315 0) {
316 debug_report_index_ = static_cast<int>(i);
317 break;
318 }
319 }
320
321 free(exts);
322
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800323 return true;
324}
325
326CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800327 const VkAllocationCallbacks& allocator)
328 : is_instance_(true),
329 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700330 loader_api_version_(VK_API_VERSION_1_1),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800331 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800332 instance_info_(create_info),
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700333 sanitized_api_version_(loader_api_version_),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700334 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800335
Chia-I Wu4901db72016-03-24 16:38:58 +0800336CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
337 const VkDeviceCreateInfo& create_info,
338 const VkAllocationCallbacks& allocator)
339 : is_instance_(false),
340 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700341 loader_api_version_(VK_API_VERSION_1_1),
Chia-I Wu4901db72016-03-24 16:38:58 +0800342 physical_dev_(physical_dev),
343 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700344 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800345
346CreateInfoWrapper::~CreateInfoWrapper() {
347 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
348 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
349}
350
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800351VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700352 VkResult result = SanitizeApiVersion();
353 if (result == VK_SUCCESS)
354 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800355 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800356 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800357 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800358 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800359
360 return result;
361}
362
363const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800364CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800365 return hook_extensions_;
366}
367
368const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800369CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800370 return hal_extensions_;
371}
372
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800373CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
374 return &instance_info_;
375}
376
Chia-I Wu4901db72016-03-24 16:38:58 +0800377CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
378 return &dev_info_;
379}
380
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700381VkResult CreateInfoWrapper::SanitizeApiVersion() {
382 if (is_instance_) {
383 // instance core versions need to match the loader api version
384 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
385 i != ProcHook::EXTENSION_COUNT; i++) {
386 hook_extensions_.set(i);
387 hal_extensions_.set(i);
388 }
389
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700390 uint32_t icd_api_version = VK_API_VERSION_1_0;
391 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
392 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
393 Hal::Device().GetInstanceProcAddr(
394 nullptr, "vkEnumerateInstanceVersion"));
395 if (pfn_enumerate_instance_version) {
396 ATRACE_BEGIN("pfn_enumerate_instance_version");
397 VkResult result =
398 (*pfn_enumerate_instance_version)(&icd_api_version);
399 ATRACE_END();
400 if (result != VK_SUCCESS)
401 return result;
402
403 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
404 }
405
406 if (icd_api_version < VK_API_VERSION_1_0)
407 return VK_SUCCESS;
408
409 if (icd_api_version < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700410 sanitized_api_version_ = icd_api_version;
411
412 if (!instance_info_.pApplicationInfo)
413 return VK_SUCCESS;
414
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700415 application_info_ = *instance_info_.pApplicationInfo;
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700416 application_info_.apiVersion = sanitized_api_version_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700417 instance_info_.pApplicationInfo = &application_info_;
418 }
419 } else {
420 const auto& driver = GetData(physical_dev_).driver;
421
422 VkPhysicalDeviceProperties properties;
423 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
424 driver.GetPhysicalDeviceProperties(physical_dev_, &properties);
425 ATRACE_END();
426
427 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
428 // Log that the app is hitting software Vulkan implementation
429 android::GraphicsEnv::getInstance().setTargetStats(
430 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
431 }
432
433 uint32_t api_version = properties.apiVersion;
434 api_version ^= VK_VERSION_PATCH(api_version);
435
436 if (api_version > loader_api_version_)
437 api_version = loader_api_version_;
438
439 switch (api_version) {
440 case VK_API_VERSION_1_1:
441 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
442 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
443 [[clang::fallthrough]];
444 case VK_API_VERSION_1_0:
445 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
446 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
447 break;
448 default:
449 ALOGE("Unknown API version[%u]", api_version);
450 break;
451 }
452 }
453
454 return VK_SUCCESS;
455}
456
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800457VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800458 const struct StructHeader {
459 VkStructureType type;
460 const void* next;
461 } * header;
462
463 if (is_instance_) {
464 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
465
466 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
467 while (header &&
468 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
469 header = reinterpret_cast<const StructHeader*>(header->next);
470
471 instance_info_.pNext = header;
472 } else {
473 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
474
475 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
476 while (header &&
477 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
478 header = reinterpret_cast<const StructHeader*>(header->next);
479
480 dev_info_.pNext = header;
481 }
482
483 return VK_SUCCESS;
484}
485
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800486VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800487 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
488 : dev_info_.ppEnabledLayerNames;
489 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
490 : dev_info_.enabledLayerCount;
491
492 // remove all layers
493 layer_names = nullptr;
494 layer_count = 0;
495
496 return VK_SUCCESS;
497}
498
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800499VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800500 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
501 : dev_info_.ppEnabledExtensionNames;
502 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
503 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800504
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800505 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800506 if (result != VK_SUCCESS)
507 return result;
508
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700509 if (is_instance_ && sanitized_api_version_ < loader_api_version_) {
510 for (uint32_t i = 0; i < ext_count; i++) {
511 // Upon api downgrade, skip the promoted instance extensions in the
512 // first pass to avoid duplicate extensions.
513 const std::optional<uint32_t> version =
514 GetInstanceExtensionPromotedVersion(ext_names[i]);
515 if (version && *version > sanitized_api_version_ &&
516 *version <= loader_api_version_)
517 continue;
518
519 FilterExtension(ext_names[i]);
520 }
521
522 // Enable the required extensions to support core functionalities.
523 const auto promoted_extensions = GetPromotedInstanceExtensions(
524 sanitized_api_version_, loader_api_version_);
525 for (const auto& promoted_extension : promoted_extensions)
526 FilterExtension(promoted_extension);
527 } else {
528 for (uint32_t i = 0; i < ext_count; i++)
529 FilterExtension(ext_names[i]);
530 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800531
Jesse Halld3d887a2018-03-05 13:34:45 -0800532 // Enable device extensions that contain physical-device commands, so that
533 // vkGetInstanceProcAddr will return those physical-device commands.
534 if (is_instance_) {
535 hook_extensions_.set(ProcHook::KHR_swapchain);
536 }
537
Chia-I Wu4901db72016-03-24 16:38:58 +0800538 ext_names = extension_filter_.names;
539 ext_count = extension_filter_.name_count;
540
541 return VK_SUCCESS;
542}
543
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800544VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800545 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800546 return Hal::Device().EnumerateInstanceExtensionProperties(
547 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800548 } else {
549 const auto& driver = GetData(physical_dev_).driver;
550 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
551 &count, nullptr);
552 }
553}
554
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800555VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800556 uint32_t& count,
557 VkExtensionProperties* props) const {
558 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800559 return Hal::Device().EnumerateInstanceExtensionProperties(
560 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800561 } else {
562 const auto& driver = GetData(physical_dev_).driver;
563 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
564 &count, props);
565 }
566}
567
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800568VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800569 // query extension count
570 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800571 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800572 if (result != VK_SUCCESS || count == 0)
573 return result;
574
575 auto& filter = extension_filter_;
576 filter.exts =
577 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
578 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
579 alignof(VkExtensionProperties),
580 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
581 if (!filter.exts)
582 return VK_ERROR_OUT_OF_HOST_MEMORY;
583
584 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800585 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800586 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
587 return result;
588
589 if (!count)
590 return VK_SUCCESS;
591
592 filter.ext_count = count;
593
594 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700595 if (is_instance_) {
596 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
597
598 // It requires enabling additional promoted extensions to downgrade api,
599 // so we reserve enough space here.
600 if (sanitized_api_version_ < loader_api_version_) {
601 enabled_ext_count += CountPromotedInstanceExtensions(
602 sanitized_api_version_, loader_api_version_);
603 }
604
605 count = std::min(filter.ext_count, enabled_ext_count);
606 } else {
607 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
608 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800609 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
610 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
611 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
612 if (!filter.names)
613 return VK_ERROR_OUT_OF_HOST_MEMORY;
614
615 return VK_SUCCESS;
616}
617
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800618void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800619 auto& filter = extension_filter_;
620
621 ProcHook::Extension ext_bit = GetProcHookExtension(name);
622 if (is_instance_) {
623 switch (ext_bit) {
624 case ProcHook::KHR_android_surface:
625 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700626 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300627 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800628 hook_extensions_.set(ext_bit);
629 // return now as these extensions do not require HAL support
630 return;
631 case ProcHook::EXT_debug_report:
632 // both we and HAL can take part in
633 hook_extensions_.set(ext_bit);
634 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300635 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700636 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700637 case ProcHook::KHR_external_memory_capabilities:
638 case ProcHook::KHR_external_semaphore_capabilities:
639 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700640 case ProcHook::EXTENSION_UNKNOWN:
641 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800642 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700643
Yiwei Zhang23143102019-04-10 18:24:05 -0700644 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700645 case ProcHook::KHR_incremental_present:
646 case ProcHook::KHR_shared_presentable_image:
647 case ProcHook::KHR_swapchain:
648 case ProcHook::EXT_hdr_metadata:
649 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
650 case ProcHook::ANDROID_native_buffer:
651 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700652 case ProcHook::EXTENSION_CORE_1_0:
653 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700654 case ProcHook::EXTENSION_COUNT:
655 // Device and meta extensions. If we ever get here it's a bug in
656 // our code. But enumerating them lets us avoid having a default
657 // case, and default hides other bugs.
658 ALOGE(
659 "CreateInfoWrapper::FilterExtension: invalid instance "
660 "extension '%s'. FIX ME",
661 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800662 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700663
664 // Don't use a default case. Without it, -Wswitch will tell us
665 // at compile time if someone adds a new ProcHook extension but
666 // doesn't handle it above. That's a real bug that has
667 // not-immediately-obvious effects.
668 //
669 // default:
670 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800671 }
672 } else {
673 switch (ext_bit) {
674 case ProcHook::KHR_swapchain:
675 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
676 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
677 ext_bit = ProcHook::ANDROID_native_buffer;
678 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700679 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700680 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300681 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700682 hook_extensions_.set(ext_bit);
683 // return now as these extensions do not require HAL support
684 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700685 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700686 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700687 hook_extensions_.set(ext_bit);
688 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700689 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800690 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700691 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800692 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700693
694 case ProcHook::KHR_android_surface:
695 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700696 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700697 case ProcHook::KHR_external_memory_capabilities:
698 case ProcHook::KHR_external_semaphore_capabilities:
699 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700700 case ProcHook::KHR_get_surface_capabilities2:
701 case ProcHook::KHR_surface:
702 case ProcHook::EXT_debug_report:
703 case ProcHook::EXT_swapchain_colorspace:
704 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700705 case ProcHook::EXTENSION_CORE_1_0:
706 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700707 case ProcHook::EXTENSION_COUNT:
708 // Instance and meta extensions. If we ever get here it's a bug
709 // in our code. But enumerating them lets us avoid having a
710 // default case, and default hides other bugs.
711 ALOGE(
712 "CreateInfoWrapper::FilterExtension: invalid device "
713 "extension '%s'. FIX ME",
714 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800715 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700716
717 // Don't use a default case. Without it, -Wswitch will tell us
718 // at compile time if someone adds a new ProcHook extension but
719 // doesn't handle it above. That's a real bug that has
720 // not-immediately-obvious effects.
721 //
722 // default:
723 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800724 }
725 }
726
727 for (uint32_t i = 0; i < filter.ext_count; i++) {
728 const VkExtensionProperties& props = filter.exts[i];
729 // ignore unknown extensions
730 if (strcmp(name, props.extensionName) != 0)
731 continue;
732
Chia-I Wu4901db72016-03-24 16:38:58 +0800733 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800734 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
735 if (ext_bit == ProcHook::ANDROID_native_buffer)
736 hook_extensions_.set(ProcHook::KHR_swapchain);
737
738 hal_extensions_.set(ext_bit);
739 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800740
741 break;
742 }
743}
744
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800745VKAPI_ATTR void* DefaultAllocate(void*,
746 size_t size,
747 size_t alignment,
748 VkSystemAllocationScope) {
749 void* ptr = nullptr;
750 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
751 // additionally requires that it be at least sizeof(void*).
752 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
753 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
754 ret, ptr);
755 return ret == 0 ? ptr : nullptr;
756}
757
758VKAPI_ATTR void* DefaultReallocate(void*,
759 void* ptr,
760 size_t size,
761 size_t alignment,
762 VkSystemAllocationScope) {
763 if (size == 0) {
764 free(ptr);
765 return nullptr;
766 }
767
Yiwei Zhanga885c062019-10-24 12:07:57 -0700768 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800769 // request is smaller than the existing chunk, we just continue using it.
770 // Right now the loader never reallocs, so this doesn't matter. If that
771 // changes, or if this code is copied into some other project, this should
772 // probably have a heuristic to allocate-copy-free when doing so will save
773 // "enough" space.
774 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
775 if (size <= old_size)
776 return ptr;
777
778 void* new_ptr = nullptr;
779 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
780 return nullptr;
781 if (ptr) {
782 memcpy(new_ptr, ptr, std::min(old_size, size));
783 free(ptr);
784 }
785 return new_ptr;
786}
787
788VKAPI_ATTR void DefaultFree(void*, void* ptr) {
789 ALOGD_CALLSTACK("Free: %p", ptr);
790 free(ptr);
791}
792
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800793InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
794 void* data_mem = allocator.pfnAllocation(
795 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
796 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
797 if (!data_mem)
798 return nullptr;
799
800 return new (data_mem) InstanceData(allocator);
801}
802
803void FreeInstanceData(InstanceData* data,
804 const VkAllocationCallbacks& allocator) {
805 data->~InstanceData();
806 allocator.pfnFree(allocator.pUserData, data);
807}
808
Chia-I Wu950d6e12016-05-03 09:12:35 +0800809DeviceData* AllocateDeviceData(
810 const VkAllocationCallbacks& allocator,
811 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800812 void* data_mem = allocator.pfnAllocation(
813 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
814 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
815 if (!data_mem)
816 return nullptr;
817
Chia-I Wu950d6e12016-05-03 09:12:35 +0800818 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800819}
820
821void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
822 data->~DeviceData();
823 allocator.pfnFree(allocator.pUserData, data);
824}
825
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800826} // anonymous namespace
827
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800828bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800829 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800830}
831
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800832const VkAllocationCallbacks& GetDefaultAllocator() {
833 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
834 .pUserData = nullptr,
835 .pfnAllocation = DefaultAllocate,
836 .pfnReallocation = DefaultReallocate,
837 .pfnFree = DefaultFree,
838 };
839
840 return kDefaultAllocCallbacks;
841}
842
Chia-I Wueb7db122016-03-24 09:11:06 +0800843PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
844 const ProcHook* hook = GetProcHook(pName);
845 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800846 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800847
848 if (!instance) {
849 if (hook->type == ProcHook::GLOBAL)
850 return hook->proc;
851
Chia-I Wu109f8982016-04-22 06:40:40 +0800852 // v0 layers expect
853 //
854 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
855 //
856 // to work.
857 if (strcmp(pName, "vkCreateDevice") == 0)
858 return hook->proc;
859
Chia-I Wueb7db122016-03-24 09:11:06 +0800860 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800861 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800862 pName);
863
Chia-I Wu109f8982016-04-22 06:40:40 +0800864 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800865 }
866
867 PFN_vkVoidFunction proc;
868
869 switch (hook->type) {
870 case ProcHook::INSTANCE:
871 proc = (GetData(instance).hook_extensions[hook->extension])
872 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800873 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800874 break;
875 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700876 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800877 ? hook->proc
878 : hook->checked_proc;
879 break;
880 default:
881 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800882 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800883 pName);
884 proc = nullptr;
885 break;
886 }
887
888 return proc;
889}
890
891PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
892 const ProcHook* hook = GetProcHook(pName);
893 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800894 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800895
896 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800897 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800898 return nullptr;
899 }
900
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800901 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
902 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800903}
904
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800905VkResult EnumerateInstanceExtensionProperties(
906 const char* pLayerName,
907 uint32_t* pPropertyCount,
908 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700909 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600910 loader_extensions.push_back({
911 VK_KHR_SURFACE_EXTENSION_NAME,
912 VK_KHR_SURFACE_SPEC_VERSION});
913 loader_extensions.push_back({
914 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
915 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
916 loader_extensions.push_back({
917 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
918 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700919 loader_extensions.push_back({
920 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
921 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600922
Chia-I Wu31938252016-05-23 15:31:02 +0800923 static const VkExtensionProperties loader_debug_report_extension = {
924 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
925 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800926
927 // enumerate our extensions first
928 if (!pLayerName && pProperties) {
929 uint32_t count = std::min(
930 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
931
Yiwei Zhang5e862202019-06-21 14:59:16 -0700932 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800933
934 if (count < loader_extensions.size()) {
935 *pPropertyCount = count;
936 return VK_INCOMPLETE;
937 }
938
939 pProperties += count;
940 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800941
942 if (Hal::Get().GetDebugReportIndex() < 0) {
943 if (!*pPropertyCount) {
944 *pPropertyCount = count;
945 return VK_INCOMPLETE;
946 }
947
948 pProperties[0] = loader_debug_report_extension;
949 pProperties += 1;
950 *pPropertyCount -= 1;
951 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800952 }
953
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800954 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800955 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800956 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800957 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800958
Chia-I Wu31938252016-05-23 15:31:02 +0800959 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
960 int idx = Hal::Get().GetDebugReportIndex();
961 if (idx < 0) {
962 *pPropertyCount += 1;
963 } else if (pProperties &&
964 static_cast<uint32_t>(idx) < *pPropertyCount) {
965 pProperties[idx].specVersion =
966 std::min(pProperties[idx].specVersion,
967 loader_debug_report_extension.specVersion);
968 }
969
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800970 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800971 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800972
973 return result;
974}
975
Chris Forbesfa25e632017-02-22 12:36:02 +1300976bool QueryPresentationProperties(
977 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700978 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300979 // Request the android-specific presentation properties via GPDP2
980 VkPhysicalDeviceProperties2KHR properties = {
981 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
982 presentation_properties,
983 {}
984 };
985
986#pragma clang diagnostic push
987#pragma clang diagnostic ignored "-Wold-style-cast"
988 presentation_properties->sType =
989 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
990#pragma clang diagnostic pop
991 presentation_properties->pNext = nullptr;
992 presentation_properties->sharedImage = VK_FALSE;
993
Yiwei Zhanga55624b2020-07-05 16:05:26 -0700994 GetPhysicalDeviceProperties2(physicalDevice, &properties);
Chris Forbesfa25e632017-02-22 12:36:02 +1300995
996 return true;
997}
998
Chia-I Wu01cf3052016-03-24 16:16:21 +0800999VkResult EnumerateDeviceExtensionProperties(
1000 VkPhysicalDevice physicalDevice,
1001 const char* pLayerName,
1002 uint32_t* pPropertyCount,
1003 VkExtensionProperties* pProperties) {
1004 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001005 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001006 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001007 loader_extensions.push_back({
1008 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1009 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001010
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001011 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001012 if (hdrBoardConfig) {
1013 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1014 VK_EXT_HDR_METADATA_SPEC_VERSION});
1015 }
1016
Chris Forbes16095002017-05-05 15:33:29 -07001017 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
1018 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
1019 presentation_properties.sharedImage) {
1020 loader_extensions.push_back({
1021 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1022 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001023 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001024
Ian Elliott5c34de22017-04-10 14:42:30 -06001025 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1026 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -07001027 const std::string timestamp_property("service.sf.present_timestamp");
1028 android::base::WaitForPropertyCreation(timestamp_property);
1029 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001030 loader_extensions.push_back({
1031 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1032 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1033 }
1034
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001035 // enumerate our extensions first
1036 if (!pLayerName && pProperties) {
1037 uint32_t count = std::min(
1038 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1039
Yiwei Zhang5e862202019-06-21 14:59:16 -07001040 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001041
1042 if (count < loader_extensions.size()) {
1043 *pPropertyCount = count;
1044 return VK_INCOMPLETE;
1045 }
1046
1047 pProperties += count;
1048 *pPropertyCount -= count;
1049 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001050
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001051 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001052 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1053 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001054 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001055
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001056 if (pProperties) {
1057 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1058 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1059 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001060
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001061 if (strcmp(prop.extensionName,
1062 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1063 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001064
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001065 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1066 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001067
1068 if (prop.specVersion >= 8) {
1069 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1070 } else {
1071 prop.specVersion = 68;
1072 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001073 }
1074 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001075
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001076 // restore loader extension count
1077 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1078 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001079 }
1080
1081 return result;
1082}
1083
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001084VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1085 const VkAllocationCallbacks* pAllocator,
1086 VkInstance* pInstance) {
1087 const VkAllocationCallbacks& data_allocator =
1088 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1089
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001090 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001091 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001092 if (result != VK_SUCCESS)
1093 return result;
1094
1095 InstanceData* data = AllocateInstanceData(data_allocator);
1096 if (!data)
1097 return VK_ERROR_OUT_OF_HOST_MEMORY;
1098
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001099 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001100
1101 // call into the driver
1102 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001103 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001104 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001105 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1106 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001107 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001108 if (result != VK_SUCCESS) {
1109 FreeInstanceData(data, data_allocator);
1110 return result;
1111 }
1112
1113 // initialize InstanceDriverTable
1114 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001115 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001116 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001117 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001118 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001119 if (data->driver.DestroyInstance)
1120 data->driver.DestroyInstance(instance, pAllocator);
1121
1122 FreeInstanceData(data, data_allocator);
1123
1124 return VK_ERROR_INCOMPATIBLE_DRIVER;
1125 }
1126
1127 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001128 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001129 if (!data->get_device_proc_addr) {
1130 data->driver.DestroyInstance(instance, pAllocator);
1131 FreeInstanceData(data, data_allocator);
1132
1133 return VK_ERROR_INCOMPATIBLE_DRIVER;
1134 }
1135
1136 *pInstance = instance;
1137
1138 return VK_SUCCESS;
1139}
1140
1141void DestroyInstance(VkInstance instance,
1142 const VkAllocationCallbacks* pAllocator) {
1143 InstanceData& data = GetData(instance);
1144 data.driver.DestroyInstance(instance, pAllocator);
1145
1146 VkAllocationCallbacks local_allocator;
1147 if (!pAllocator) {
1148 local_allocator = data.allocator;
1149 pAllocator = &local_allocator;
1150 }
1151
1152 FreeInstanceData(&data, *pAllocator);
1153}
1154
Chia-I Wu4901db72016-03-24 16:38:58 +08001155VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1156 const VkDeviceCreateInfo* pCreateInfo,
1157 const VkAllocationCallbacks* pAllocator,
1158 VkDevice* pDevice) {
1159 const InstanceData& instance_data = GetData(physicalDevice);
1160 const VkAllocationCallbacks& data_allocator =
1161 (pAllocator) ? *pAllocator : instance_data.allocator;
1162
1163 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001164 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001165 if (result != VK_SUCCESS)
1166 return result;
1167
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001168 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001169 DeviceData* data = AllocateDeviceData(data_allocator,
1170 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001171 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001172 if (!data)
1173 return VK_ERROR_OUT_OF_HOST_MEMORY;
1174
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001175 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001176
1177 // call into the driver
1178 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001179 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001180 result = instance_data.driver.CreateDevice(
1181 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1182 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001183 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001184 if (result != VK_SUCCESS) {
1185 FreeDeviceData(data, data_allocator);
1186 return result;
1187 }
1188
1189 // initialize DeviceDriverTable
1190 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001191 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1192 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001193 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1194 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1195 if (data->driver.DestroyDevice)
1196 data->driver.DestroyDevice(dev, pAllocator);
1197
1198 FreeDeviceData(data, data_allocator);
1199
1200 return VK_ERROR_INCOMPATIBLE_DRIVER;
1201 }
Chris Forbesd8277912017-02-10 14:59:59 +13001202
1203 // sanity check ANDROID_native_buffer implementation, whose set of
1204 // entrypoints varies according to the spec version.
1205 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1206 !data->driver.GetSwapchainGrallocUsageANDROID &&
1207 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1208 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1209 " must expose at least one of "
1210 "vkGetSwapchainGrallocUsageANDROID or "
1211 "vkGetSwapchainGrallocUsage2ANDROID");
1212
1213 data->driver.DestroyDevice(dev, pAllocator);
1214 FreeDeviceData(data, data_allocator);
1215
1216 return VK_ERROR_INCOMPATIBLE_DRIVER;
1217 }
1218
Jesse Halldc225072016-05-30 22:40:14 -07001219 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001220
1221 *pDevice = dev;
1222
1223 return VK_SUCCESS;
1224}
1225
1226void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1227 DeviceData& data = GetData(device);
1228 data.driver.DestroyDevice(device, pAllocator);
1229
1230 VkAllocationCallbacks local_allocator;
1231 if (!pAllocator) {
1232 local_allocator = data.allocator;
1233 pAllocator = &local_allocator;
1234 }
1235
1236 FreeDeviceData(&data, *pAllocator);
1237}
1238
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001239VkResult EnumeratePhysicalDevices(VkInstance instance,
1240 uint32_t* pPhysicalDeviceCount,
1241 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001242 ATRACE_CALL();
1243
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001244 const auto& data = GetData(instance);
1245
1246 VkResult result = data.driver.EnumeratePhysicalDevices(
1247 instance, pPhysicalDeviceCount, pPhysicalDevices);
1248 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1249 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1250 SetData(pPhysicalDevices[i], data);
1251 }
1252
1253 return result;
1254}
1255
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001256VkResult EnumeratePhysicalDeviceGroups(
1257 VkInstance instance,
1258 uint32_t* pPhysicalDeviceGroupCount,
1259 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001260 ATRACE_CALL();
1261
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001262 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001263 const auto& data = GetData(instance);
1264
Yiwei Zhange4f64172020-07-05 15:17:32 -07001265 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1266 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001267 uint32_t device_count = 0;
1268 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1269 if (result < 0)
1270 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001271
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001272 if (!pPhysicalDeviceGroupProperties) {
1273 *pPhysicalDeviceGroupCount = device_count;
1274 return result;
1275 }
1276
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001277 if (!device_count) {
1278 *pPhysicalDeviceGroupCount = 0;
1279 return result;
1280 }
Chad Versace32c087f2018-09-09 07:28:05 -07001281 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1282 if (!device_count)
1283 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001284
Yiwei Zhang5e862202019-06-21 14:59:16 -07001285 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001286 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001287 result =
1288 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001289 if (result < 0)
1290 return result;
1291
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001292 for (uint32_t i = 0; i < device_count; ++i) {
1293 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1294 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1295 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1296 }
1297 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001298 if (data.driver.EnumeratePhysicalDeviceGroups) {
1299 result = data.driver.EnumeratePhysicalDeviceGroups(
1300 instance, pPhysicalDeviceGroupCount,
1301 pPhysicalDeviceGroupProperties);
1302 } else {
1303 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1304 instance, pPhysicalDeviceGroupCount,
1305 pPhysicalDeviceGroupProperties);
1306 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001307 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1308 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1309 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1310 for (uint32_t j = 0;
1311 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1312 j++) {
1313 SetData(
1314 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001315 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001316 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001317 }
1318 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001319 }
1320
1321 return result;
1322}
1323
Chia-I Wuba0be412016-03-24 16:24:40 +08001324void GetDeviceQueue(VkDevice device,
1325 uint32_t queueFamilyIndex,
1326 uint32_t queueIndex,
1327 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001328 ATRACE_CALL();
1329
Chia-I Wuba0be412016-03-24 16:24:40 +08001330 const auto& data = GetData(device);
1331
1332 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1333 SetData(*pQueue, data);
1334}
1335
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001336void GetDeviceQueue2(VkDevice device,
1337 const VkDeviceQueueInfo2* pQueueInfo,
1338 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001339 ATRACE_CALL();
1340
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001341 const auto& data = GetData(device);
1342
1343 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001344 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001345}
1346
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001347VKAPI_ATTR VkResult
1348AllocateCommandBuffers(VkDevice device,
1349 const VkCommandBufferAllocateInfo* pAllocateInfo,
1350 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001351 ATRACE_CALL();
1352
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001353 const auto& data = GetData(device);
1354
1355 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1356 pCommandBuffers);
1357 if (result == VK_SUCCESS) {
1358 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1359 SetData(pCommandBuffers[i], data);
1360 }
1361
1362 return result;
1363}
1364
Yiwei Zhang899d1752019-09-23 16:05:35 -07001365VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1366 uint32_t submitCount,
1367 const VkSubmitInfo* pSubmits,
1368 VkFence fence) {
1369 ATRACE_CALL();
1370
1371 const auto& data = GetData(queue);
1372
1373 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1374}
1375
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001376void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1377 VkPhysicalDeviceFeatures2* pFeatures) {
1378 ATRACE_CALL();
1379
1380 const auto& driver = GetData(physicalDevice).driver;
1381
1382 if (driver.GetPhysicalDeviceFeatures2) {
1383 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1384 return;
1385 }
1386
1387 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1388}
1389
1390void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1391 VkPhysicalDeviceProperties2* pProperties) {
1392 ATRACE_CALL();
1393
1394 const auto& driver = GetData(physicalDevice).driver;
1395
1396 if (driver.GetPhysicalDeviceProperties2) {
1397 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1398 return;
1399 }
1400
1401 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1402}
1403
1404void GetPhysicalDeviceFormatProperties2(
1405 VkPhysicalDevice physicalDevice,
1406 VkFormat format,
1407 VkFormatProperties2* pFormatProperties) {
1408 ATRACE_CALL();
1409
1410 const auto& driver = GetData(physicalDevice).driver;
1411
1412 if (driver.GetPhysicalDeviceFormatProperties2) {
1413 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1414 pFormatProperties);
1415 return;
1416 }
1417
1418 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1419 pFormatProperties);
1420}
1421
1422VkResult GetPhysicalDeviceImageFormatProperties2(
1423 VkPhysicalDevice physicalDevice,
1424 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1425 VkImageFormatProperties2* pImageFormatProperties) {
1426 ATRACE_CALL();
1427
1428 const auto& driver = GetData(physicalDevice).driver;
1429
1430 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1431 return driver.GetPhysicalDeviceImageFormatProperties2(
1432 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1433 }
1434
1435 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1436 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1437}
1438
1439void GetPhysicalDeviceQueueFamilyProperties2(
1440 VkPhysicalDevice physicalDevice,
1441 uint32_t* pQueueFamilyPropertyCount,
1442 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1443 ATRACE_CALL();
1444
1445 const auto& driver = GetData(physicalDevice).driver;
1446
1447 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1448 driver.GetPhysicalDeviceQueueFamilyProperties2(
1449 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1450 return;
1451 }
1452
1453 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1454 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1455}
1456
1457void GetPhysicalDeviceMemoryProperties2(
1458 VkPhysicalDevice physicalDevice,
1459 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1460 ATRACE_CALL();
1461
1462 const auto& driver = GetData(physicalDevice).driver;
1463
1464 if (driver.GetPhysicalDeviceMemoryProperties2) {
1465 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1466 pMemoryProperties);
1467 return;
1468 }
1469
1470 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1471 pMemoryProperties);
1472}
1473
1474void GetPhysicalDeviceSparseImageFormatProperties2(
1475 VkPhysicalDevice physicalDevice,
1476 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1477 uint32_t* pPropertyCount,
1478 VkSparseImageFormatProperties2* pProperties) {
1479 ATRACE_CALL();
1480
1481 const auto& driver = GetData(physicalDevice).driver;
1482
1483 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1484 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1485 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1486 return;
1487 }
1488
1489 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1490 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1491}
1492
Yiwei Zhange1f35012020-07-05 22:52:04 -07001493void GetPhysicalDeviceExternalBufferProperties(
1494 VkPhysicalDevice physicalDevice,
1495 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1496 VkExternalBufferProperties* pExternalBufferProperties) {
1497 ATRACE_CALL();
1498
1499 const auto& driver = GetData(physicalDevice).driver;
1500
1501 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1502 driver.GetPhysicalDeviceExternalBufferProperties(
1503 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1504 return;
1505 }
1506
1507 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1508 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1509 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1510 return;
1511 }
1512
1513 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1514 sizeof(VkExternalMemoryProperties));
1515}
1516
1517void GetPhysicalDeviceExternalSemaphoreProperties(
1518 VkPhysicalDevice physicalDevice,
1519 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1520 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1521 ATRACE_CALL();
1522
1523 const auto& driver = GetData(physicalDevice).driver;
1524
1525 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1526 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1527 physicalDevice, pExternalSemaphoreInfo,
1528 pExternalSemaphoreProperties);
1529 return;
1530 }
1531
1532 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1533 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1534 physicalDevice, pExternalSemaphoreInfo,
1535 pExternalSemaphoreProperties);
1536 return;
1537 }
1538
1539 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1540 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1541 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1542}
1543
1544void GetPhysicalDeviceExternalFenceProperties(
1545 VkPhysicalDevice physicalDevice,
1546 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1547 VkExternalFenceProperties* pExternalFenceProperties) {
1548 ATRACE_CALL();
1549
1550 const auto& driver = GetData(physicalDevice).driver;
1551
1552 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1553 driver.GetPhysicalDeviceExternalFenceProperties(
1554 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1555 return;
1556 }
1557
1558 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1559 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1560 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1561 return;
1562 }
1563
1564 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1565 pExternalFenceProperties->compatibleHandleTypes = 0;
1566 pExternalFenceProperties->externalFenceFeatures = 0;
1567}
1568
Chia-I Wu9d518162016-03-24 14:55:27 +08001569} // namespace driver
1570} // namespace vulkan