blob: f2712d1f997f6412748089486ba0ec53087beebe [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:
Jesse Hall7f983a82018-03-29 14:46:45 -0700637 case ProcHook::EXTENSION_UNKNOWN:
638 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800639 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700640
Yiwei Zhang23143102019-04-10 18:24:05 -0700641 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700642 case ProcHook::KHR_incremental_present:
643 case ProcHook::KHR_shared_presentable_image:
644 case ProcHook::KHR_swapchain:
645 case ProcHook::EXT_hdr_metadata:
646 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
647 case ProcHook::ANDROID_native_buffer:
648 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700649 case ProcHook::EXTENSION_CORE_1_0:
650 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700651 case ProcHook::EXTENSION_COUNT:
652 // Device and meta extensions. If we ever get here it's a bug in
653 // our code. But enumerating them lets us avoid having a default
654 // case, and default hides other bugs.
655 ALOGE(
656 "CreateInfoWrapper::FilterExtension: invalid instance "
657 "extension '%s'. FIX ME",
658 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800659 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700660
661 // Don't use a default case. Without it, -Wswitch will tell us
662 // at compile time if someone adds a new ProcHook extension but
663 // doesn't handle it above. That's a real bug that has
664 // not-immediately-obvious effects.
665 //
666 // default:
667 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800668 }
669 } else {
670 switch (ext_bit) {
671 case ProcHook::KHR_swapchain:
672 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
673 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
674 ext_bit = ProcHook::ANDROID_native_buffer;
675 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700676 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700677 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300678 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700679 hook_extensions_.set(ext_bit);
680 // return now as these extensions do not require HAL support
681 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700682 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700683 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700684 hook_extensions_.set(ext_bit);
685 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700686 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800687 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700688 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800689 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700690
691 case ProcHook::KHR_android_surface:
692 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700693 case ProcHook::KHR_device_group_creation:
Jesse Hall7f983a82018-03-29 14:46:45 -0700694 case ProcHook::KHR_get_surface_capabilities2:
695 case ProcHook::KHR_surface:
696 case ProcHook::EXT_debug_report:
697 case ProcHook::EXT_swapchain_colorspace:
698 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700699 case ProcHook::EXTENSION_CORE_1_0:
700 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700701 case ProcHook::EXTENSION_COUNT:
702 // Instance and meta extensions. If we ever get here it's a bug
703 // in our code. But enumerating them lets us avoid having a
704 // default case, and default hides other bugs.
705 ALOGE(
706 "CreateInfoWrapper::FilterExtension: invalid device "
707 "extension '%s'. FIX ME",
708 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800709 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700710
711 // Don't use a default case. Without it, -Wswitch will tell us
712 // at compile time if someone adds a new ProcHook extension but
713 // doesn't handle it above. That's a real bug that has
714 // not-immediately-obvious effects.
715 //
716 // default:
717 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800718 }
719 }
720
721 for (uint32_t i = 0; i < filter.ext_count; i++) {
722 const VkExtensionProperties& props = filter.exts[i];
723 // ignore unknown extensions
724 if (strcmp(name, props.extensionName) != 0)
725 continue;
726
Chia-I Wu4901db72016-03-24 16:38:58 +0800727 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800728 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
729 if (ext_bit == ProcHook::ANDROID_native_buffer)
730 hook_extensions_.set(ProcHook::KHR_swapchain);
731
732 hal_extensions_.set(ext_bit);
733 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800734
735 break;
736 }
737}
738
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800739VKAPI_ATTR void* DefaultAllocate(void*,
740 size_t size,
741 size_t alignment,
742 VkSystemAllocationScope) {
743 void* ptr = nullptr;
744 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
745 // additionally requires that it be at least sizeof(void*).
746 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
747 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
748 ret, ptr);
749 return ret == 0 ? ptr : nullptr;
750}
751
752VKAPI_ATTR void* DefaultReallocate(void*,
753 void* ptr,
754 size_t size,
755 size_t alignment,
756 VkSystemAllocationScope) {
757 if (size == 0) {
758 free(ptr);
759 return nullptr;
760 }
761
Yiwei Zhanga885c062019-10-24 12:07:57 -0700762 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800763 // request is smaller than the existing chunk, we just continue using it.
764 // Right now the loader never reallocs, so this doesn't matter. If that
765 // changes, or if this code is copied into some other project, this should
766 // probably have a heuristic to allocate-copy-free when doing so will save
767 // "enough" space.
768 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
769 if (size <= old_size)
770 return ptr;
771
772 void* new_ptr = nullptr;
773 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
774 return nullptr;
775 if (ptr) {
776 memcpy(new_ptr, ptr, std::min(old_size, size));
777 free(ptr);
778 }
779 return new_ptr;
780}
781
782VKAPI_ATTR void DefaultFree(void*, void* ptr) {
783 ALOGD_CALLSTACK("Free: %p", ptr);
784 free(ptr);
785}
786
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800787InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
788 void* data_mem = allocator.pfnAllocation(
789 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
790 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
791 if (!data_mem)
792 return nullptr;
793
794 return new (data_mem) InstanceData(allocator);
795}
796
797void FreeInstanceData(InstanceData* data,
798 const VkAllocationCallbacks& allocator) {
799 data->~InstanceData();
800 allocator.pfnFree(allocator.pUserData, data);
801}
802
Chia-I Wu950d6e12016-05-03 09:12:35 +0800803DeviceData* AllocateDeviceData(
804 const VkAllocationCallbacks& allocator,
805 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800806 void* data_mem = allocator.pfnAllocation(
807 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
808 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
809 if (!data_mem)
810 return nullptr;
811
Chia-I Wu950d6e12016-05-03 09:12:35 +0800812 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800813}
814
815void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
816 data->~DeviceData();
817 allocator.pfnFree(allocator.pUserData, data);
818}
819
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800820} // anonymous namespace
821
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800822bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800823 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800824}
825
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800826const VkAllocationCallbacks& GetDefaultAllocator() {
827 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
828 .pUserData = nullptr,
829 .pfnAllocation = DefaultAllocate,
830 .pfnReallocation = DefaultReallocate,
831 .pfnFree = DefaultFree,
832 };
833
834 return kDefaultAllocCallbacks;
835}
836
Chia-I Wueb7db122016-03-24 09:11:06 +0800837PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
838 const ProcHook* hook = GetProcHook(pName);
839 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800840 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800841
842 if (!instance) {
843 if (hook->type == ProcHook::GLOBAL)
844 return hook->proc;
845
Chia-I Wu109f8982016-04-22 06:40:40 +0800846 // v0 layers expect
847 //
848 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
849 //
850 // to work.
851 if (strcmp(pName, "vkCreateDevice") == 0)
852 return hook->proc;
853
Chia-I Wueb7db122016-03-24 09:11:06 +0800854 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800855 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800856 pName);
857
Chia-I Wu109f8982016-04-22 06:40:40 +0800858 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800859 }
860
861 PFN_vkVoidFunction proc;
862
863 switch (hook->type) {
864 case ProcHook::INSTANCE:
865 proc = (GetData(instance).hook_extensions[hook->extension])
866 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800867 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800868 break;
869 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700870 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800871 ? hook->proc
872 : hook->checked_proc;
873 break;
874 default:
875 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800876 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800877 pName);
878 proc = nullptr;
879 break;
880 }
881
882 return proc;
883}
884
885PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
886 const ProcHook* hook = GetProcHook(pName);
887 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800888 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800889
890 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800891 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800892 return nullptr;
893 }
894
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800895 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
896 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800897}
898
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800899VkResult EnumerateInstanceExtensionProperties(
900 const char* pLayerName,
901 uint32_t* pPropertyCount,
902 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700903 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600904 loader_extensions.push_back({
905 VK_KHR_SURFACE_EXTENSION_NAME,
906 VK_KHR_SURFACE_SPEC_VERSION});
907 loader_extensions.push_back({
908 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
909 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
910 loader_extensions.push_back({
911 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
912 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700913 loader_extensions.push_back({
914 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
915 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600916
Chia-I Wu31938252016-05-23 15:31:02 +0800917 static const VkExtensionProperties loader_debug_report_extension = {
918 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
919 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800920
921 // enumerate our extensions first
922 if (!pLayerName && pProperties) {
923 uint32_t count = std::min(
924 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
925
Yiwei Zhang5e862202019-06-21 14:59:16 -0700926 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800927
928 if (count < loader_extensions.size()) {
929 *pPropertyCount = count;
930 return VK_INCOMPLETE;
931 }
932
933 pProperties += count;
934 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800935
936 if (Hal::Get().GetDebugReportIndex() < 0) {
937 if (!*pPropertyCount) {
938 *pPropertyCount = count;
939 return VK_INCOMPLETE;
940 }
941
942 pProperties[0] = loader_debug_report_extension;
943 pProperties += 1;
944 *pPropertyCount -= 1;
945 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800946 }
947
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800948 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800949 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800950 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800951 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800952
Chia-I Wu31938252016-05-23 15:31:02 +0800953 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
954 int idx = Hal::Get().GetDebugReportIndex();
955 if (idx < 0) {
956 *pPropertyCount += 1;
957 } else if (pProperties &&
958 static_cast<uint32_t>(idx) < *pPropertyCount) {
959 pProperties[idx].specVersion =
960 std::min(pProperties[idx].specVersion,
961 loader_debug_report_extension.specVersion);
962 }
963
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800964 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800965 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800966
967 return result;
968}
969
Chris Forbesfa25e632017-02-22 12:36:02 +1300970bool QueryPresentationProperties(
971 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700972 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300973 const InstanceData& data = GetData(physicalDevice);
974
975 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700976 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
977 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300978 return false;
979
980 // Request the android-specific presentation properties via GPDP2
981 VkPhysicalDeviceProperties2KHR properties = {
982 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
983 presentation_properties,
984 {}
985 };
986
987#pragma clang diagnostic push
988#pragma clang diagnostic ignored "-Wold-style-cast"
989 presentation_properties->sType =
990 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
991#pragma clang diagnostic pop
992 presentation_properties->pNext = nullptr;
993 presentation_properties->sharedImage = VK_FALSE;
994
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700995 if (data.driver.GetPhysicalDeviceProperties2KHR) {
996 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
997 &properties);
998 } else {
999 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1000 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001001
1002 return true;
1003}
1004
Chia-I Wu01cf3052016-03-24 16:16:21 +08001005VkResult EnumerateDeviceExtensionProperties(
1006 VkPhysicalDevice physicalDevice,
1007 const char* pLayerName,
1008 uint32_t* pPropertyCount,
1009 VkExtensionProperties* pProperties) {
1010 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001011 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001012 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001013 loader_extensions.push_back({
1014 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1015 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001016
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001017 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001018 if (hdrBoardConfig) {
1019 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1020 VK_EXT_HDR_METADATA_SPEC_VERSION});
1021 }
1022
Chris Forbes16095002017-05-05 15:33:29 -07001023 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
1024 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
1025 presentation_properties.sharedImage) {
1026 loader_extensions.push_back({
1027 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1028 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001029 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001030
Ian Elliott5c34de22017-04-10 14:42:30 -06001031 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1032 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -07001033 const std::string timestamp_property("service.sf.present_timestamp");
1034 android::base::WaitForPropertyCreation(timestamp_property);
1035 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001036 loader_extensions.push_back({
1037 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1038 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1039 }
1040
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001041 // enumerate our extensions first
1042 if (!pLayerName && pProperties) {
1043 uint32_t count = std::min(
1044 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1045
Yiwei Zhang5e862202019-06-21 14:59:16 -07001046 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001047
1048 if (count < loader_extensions.size()) {
1049 *pPropertyCount = count;
1050 return VK_INCOMPLETE;
1051 }
1052
1053 pProperties += count;
1054 *pPropertyCount -= count;
1055 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001056
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001057 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001058 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1059 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001060 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001061
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001062 if (pProperties) {
1063 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1064 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1065 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001066
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001067 if (strcmp(prop.extensionName,
1068 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1069 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001070
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001071 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1072 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001073
1074 if (prop.specVersion >= 8) {
1075 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1076 } else {
1077 prop.specVersion = 68;
1078 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001079 }
1080 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001081
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001082 // restore loader extension count
1083 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1084 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001085 }
1086
1087 return result;
1088}
1089
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001090VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1091 const VkAllocationCallbacks* pAllocator,
1092 VkInstance* pInstance) {
1093 const VkAllocationCallbacks& data_allocator =
1094 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1095
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001096 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001097 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001098 if (result != VK_SUCCESS)
1099 return result;
1100
1101 InstanceData* data = AllocateInstanceData(data_allocator);
1102 if (!data)
1103 return VK_ERROR_OUT_OF_HOST_MEMORY;
1104
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001105 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001106
1107 // call into the driver
1108 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001109 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001110 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001111 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1112 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001113 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001114 if (result != VK_SUCCESS) {
1115 FreeInstanceData(data, data_allocator);
1116 return result;
1117 }
1118
1119 // initialize InstanceDriverTable
1120 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001121 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001122 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001123 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001124 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001125 if (data->driver.DestroyInstance)
1126 data->driver.DestroyInstance(instance, pAllocator);
1127
1128 FreeInstanceData(data, data_allocator);
1129
1130 return VK_ERROR_INCOMPATIBLE_DRIVER;
1131 }
1132
1133 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001134 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001135 if (!data->get_device_proc_addr) {
1136 data->driver.DestroyInstance(instance, pAllocator);
1137 FreeInstanceData(data, data_allocator);
1138
1139 return VK_ERROR_INCOMPATIBLE_DRIVER;
1140 }
1141
1142 *pInstance = instance;
1143
1144 return VK_SUCCESS;
1145}
1146
1147void DestroyInstance(VkInstance instance,
1148 const VkAllocationCallbacks* pAllocator) {
1149 InstanceData& data = GetData(instance);
1150 data.driver.DestroyInstance(instance, pAllocator);
1151
1152 VkAllocationCallbacks local_allocator;
1153 if (!pAllocator) {
1154 local_allocator = data.allocator;
1155 pAllocator = &local_allocator;
1156 }
1157
1158 FreeInstanceData(&data, *pAllocator);
1159}
1160
Chia-I Wu4901db72016-03-24 16:38:58 +08001161VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1162 const VkDeviceCreateInfo* pCreateInfo,
1163 const VkAllocationCallbacks* pAllocator,
1164 VkDevice* pDevice) {
1165 const InstanceData& instance_data = GetData(physicalDevice);
1166 const VkAllocationCallbacks& data_allocator =
1167 (pAllocator) ? *pAllocator : instance_data.allocator;
1168
1169 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001170 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001171 if (result != VK_SUCCESS)
1172 return result;
1173
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001174 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001175 DeviceData* data = AllocateDeviceData(data_allocator,
1176 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001177 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001178 if (!data)
1179 return VK_ERROR_OUT_OF_HOST_MEMORY;
1180
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001181 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001182
1183 // call into the driver
1184 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001185 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001186 result = instance_data.driver.CreateDevice(
1187 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1188 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001189 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001190 if (result != VK_SUCCESS) {
1191 FreeDeviceData(data, data_allocator);
1192 return result;
1193 }
1194
1195 // initialize DeviceDriverTable
1196 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001197 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1198 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001199 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1200 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1201 if (data->driver.DestroyDevice)
1202 data->driver.DestroyDevice(dev, pAllocator);
1203
1204 FreeDeviceData(data, data_allocator);
1205
1206 return VK_ERROR_INCOMPATIBLE_DRIVER;
1207 }
Chris Forbesd8277912017-02-10 14:59:59 +13001208
1209 // sanity check ANDROID_native_buffer implementation, whose set of
1210 // entrypoints varies according to the spec version.
1211 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1212 !data->driver.GetSwapchainGrallocUsageANDROID &&
1213 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1214 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1215 " must expose at least one of "
1216 "vkGetSwapchainGrallocUsageANDROID or "
1217 "vkGetSwapchainGrallocUsage2ANDROID");
1218
1219 data->driver.DestroyDevice(dev, pAllocator);
1220 FreeDeviceData(data, data_allocator);
1221
1222 return VK_ERROR_INCOMPATIBLE_DRIVER;
1223 }
1224
Jesse Halldc225072016-05-30 22:40:14 -07001225 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001226
1227 *pDevice = dev;
1228
1229 return VK_SUCCESS;
1230}
1231
1232void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1233 DeviceData& data = GetData(device);
1234 data.driver.DestroyDevice(device, pAllocator);
1235
1236 VkAllocationCallbacks local_allocator;
1237 if (!pAllocator) {
1238 local_allocator = data.allocator;
1239 pAllocator = &local_allocator;
1240 }
1241
1242 FreeDeviceData(&data, *pAllocator);
1243}
1244
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001245VkResult EnumeratePhysicalDevices(VkInstance instance,
1246 uint32_t* pPhysicalDeviceCount,
1247 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001248 ATRACE_CALL();
1249
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001250 const auto& data = GetData(instance);
1251
1252 VkResult result = data.driver.EnumeratePhysicalDevices(
1253 instance, pPhysicalDeviceCount, pPhysicalDevices);
1254 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1255 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1256 SetData(pPhysicalDevices[i], data);
1257 }
1258
1259 return result;
1260}
1261
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001262VkResult EnumeratePhysicalDeviceGroups(
1263 VkInstance instance,
1264 uint32_t* pPhysicalDeviceGroupCount,
1265 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001266 ATRACE_CALL();
1267
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001268 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001269 const auto& data = GetData(instance);
1270
Yiwei Zhange4f64172020-07-05 15:17:32 -07001271 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1272 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001273 uint32_t device_count = 0;
1274 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1275 if (result < 0)
1276 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001277
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001278 if (!pPhysicalDeviceGroupProperties) {
1279 *pPhysicalDeviceGroupCount = device_count;
1280 return result;
1281 }
1282
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001283 if (!device_count) {
1284 *pPhysicalDeviceGroupCount = 0;
1285 return result;
1286 }
Chad Versace32c087f2018-09-09 07:28:05 -07001287 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1288 if (!device_count)
1289 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001290
Yiwei Zhang5e862202019-06-21 14:59:16 -07001291 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001292 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001293 result =
1294 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001295 if (result < 0)
1296 return result;
1297
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001298 for (uint32_t i = 0; i < device_count; ++i) {
1299 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1300 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1301 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1302 }
1303 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001304 if (data.driver.EnumeratePhysicalDeviceGroups) {
1305 result = data.driver.EnumeratePhysicalDeviceGroups(
1306 instance, pPhysicalDeviceGroupCount,
1307 pPhysicalDeviceGroupProperties);
1308 } else {
1309 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1310 instance, pPhysicalDeviceGroupCount,
1311 pPhysicalDeviceGroupProperties);
1312 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001313 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1314 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1315 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1316 for (uint32_t j = 0;
1317 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1318 j++) {
1319 SetData(
1320 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001321 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001322 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001323 }
1324 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001325 }
1326
1327 return result;
1328}
1329
Chia-I Wuba0be412016-03-24 16:24:40 +08001330void GetDeviceQueue(VkDevice device,
1331 uint32_t queueFamilyIndex,
1332 uint32_t queueIndex,
1333 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001334 ATRACE_CALL();
1335
Chia-I Wuba0be412016-03-24 16:24:40 +08001336 const auto& data = GetData(device);
1337
1338 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1339 SetData(*pQueue, data);
1340}
1341
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001342void GetDeviceQueue2(VkDevice device,
1343 const VkDeviceQueueInfo2* pQueueInfo,
1344 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001345 ATRACE_CALL();
1346
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001347 const auto& data = GetData(device);
1348
1349 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001350 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001351}
1352
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001353VKAPI_ATTR VkResult
1354AllocateCommandBuffers(VkDevice device,
1355 const VkCommandBufferAllocateInfo* pAllocateInfo,
1356 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001357 ATRACE_CALL();
1358
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001359 const auto& data = GetData(device);
1360
1361 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1362 pCommandBuffers);
1363 if (result == VK_SUCCESS) {
1364 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1365 SetData(pCommandBuffers[i], data);
1366 }
1367
1368 return result;
1369}
1370
Yiwei Zhang899d1752019-09-23 16:05:35 -07001371VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1372 uint32_t submitCount,
1373 const VkSubmitInfo* pSubmits,
1374 VkFence fence) {
1375 ATRACE_CALL();
1376
1377 const auto& data = GetData(queue);
1378
1379 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1380}
1381
Chia-I Wu9d518162016-03-24 14:55:27 +08001382} // namespace driver
1383} // namespace vulkan