blob: c48d1eebf701fe31e60caaa47a59b827f2da6f16 [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:
Jesse Hall7f983a82018-03-29 14:46:45 -0700636 case ProcHook::EXTENSION_UNKNOWN:
637 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800638 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700639
Yiwei Zhang23143102019-04-10 18:24:05 -0700640 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700641 case ProcHook::KHR_incremental_present:
642 case ProcHook::KHR_shared_presentable_image:
643 case ProcHook::KHR_swapchain:
644 case ProcHook::EXT_hdr_metadata:
645 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
646 case ProcHook::ANDROID_native_buffer:
647 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700648 case ProcHook::EXTENSION_CORE_1_0:
649 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700650 case ProcHook::EXTENSION_COUNT:
651 // Device and meta extensions. If we ever get here it's a bug in
652 // our code. But enumerating them lets us avoid having a default
653 // case, and default hides other bugs.
654 ALOGE(
655 "CreateInfoWrapper::FilterExtension: invalid instance "
656 "extension '%s'. FIX ME",
657 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800658 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700659
660 // Don't use a default case. Without it, -Wswitch will tell us
661 // at compile time if someone adds a new ProcHook extension but
662 // doesn't handle it above. That's a real bug that has
663 // not-immediately-obvious effects.
664 //
665 // default:
666 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800667 }
668 } else {
669 switch (ext_bit) {
670 case ProcHook::KHR_swapchain:
671 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
672 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
673 ext_bit = ProcHook::ANDROID_native_buffer;
674 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700675 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700676 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300677 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700678 hook_extensions_.set(ext_bit);
679 // return now as these extensions do not require HAL support
680 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700681 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700682 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700683 hook_extensions_.set(ext_bit);
684 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700685 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800686 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700687 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800688 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700689
690 case ProcHook::KHR_android_surface:
691 case ProcHook::KHR_get_physical_device_properties2:
692 case ProcHook::KHR_get_surface_capabilities2:
693 case ProcHook::KHR_surface:
694 case ProcHook::EXT_debug_report:
695 case ProcHook::EXT_swapchain_colorspace:
696 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700697 case ProcHook::EXTENSION_CORE_1_0:
698 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700699 case ProcHook::EXTENSION_COUNT:
700 // Instance and meta extensions. If we ever get here it's a bug
701 // in our code. But enumerating them lets us avoid having a
702 // default case, and default hides other bugs.
703 ALOGE(
704 "CreateInfoWrapper::FilterExtension: invalid device "
705 "extension '%s'. FIX ME",
706 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800707 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700708
709 // Don't use a default case. Without it, -Wswitch will tell us
710 // at compile time if someone adds a new ProcHook extension but
711 // doesn't handle it above. That's a real bug that has
712 // not-immediately-obvious effects.
713 //
714 // default:
715 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800716 }
717 }
718
719 for (uint32_t i = 0; i < filter.ext_count; i++) {
720 const VkExtensionProperties& props = filter.exts[i];
721 // ignore unknown extensions
722 if (strcmp(name, props.extensionName) != 0)
723 continue;
724
Chia-I Wu4901db72016-03-24 16:38:58 +0800725 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800726 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
727 if (ext_bit == ProcHook::ANDROID_native_buffer)
728 hook_extensions_.set(ProcHook::KHR_swapchain);
729
730 hal_extensions_.set(ext_bit);
731 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800732
733 break;
734 }
735}
736
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800737VKAPI_ATTR void* DefaultAllocate(void*,
738 size_t size,
739 size_t alignment,
740 VkSystemAllocationScope) {
741 void* ptr = nullptr;
742 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
743 // additionally requires that it be at least sizeof(void*).
744 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
745 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
746 ret, ptr);
747 return ret == 0 ? ptr : nullptr;
748}
749
750VKAPI_ATTR void* DefaultReallocate(void*,
751 void* ptr,
752 size_t size,
753 size_t alignment,
754 VkSystemAllocationScope) {
755 if (size == 0) {
756 free(ptr);
757 return nullptr;
758 }
759
Yiwei Zhanga885c062019-10-24 12:07:57 -0700760 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800761 // request is smaller than the existing chunk, we just continue using it.
762 // Right now the loader never reallocs, so this doesn't matter. If that
763 // changes, or if this code is copied into some other project, this should
764 // probably have a heuristic to allocate-copy-free when doing so will save
765 // "enough" space.
766 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
767 if (size <= old_size)
768 return ptr;
769
770 void* new_ptr = nullptr;
771 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
772 return nullptr;
773 if (ptr) {
774 memcpy(new_ptr, ptr, std::min(old_size, size));
775 free(ptr);
776 }
777 return new_ptr;
778}
779
780VKAPI_ATTR void DefaultFree(void*, void* ptr) {
781 ALOGD_CALLSTACK("Free: %p", ptr);
782 free(ptr);
783}
784
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800785InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
786 void* data_mem = allocator.pfnAllocation(
787 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
788 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
789 if (!data_mem)
790 return nullptr;
791
792 return new (data_mem) InstanceData(allocator);
793}
794
795void FreeInstanceData(InstanceData* data,
796 const VkAllocationCallbacks& allocator) {
797 data->~InstanceData();
798 allocator.pfnFree(allocator.pUserData, data);
799}
800
Chia-I Wu950d6e12016-05-03 09:12:35 +0800801DeviceData* AllocateDeviceData(
802 const VkAllocationCallbacks& allocator,
803 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800804 void* data_mem = allocator.pfnAllocation(
805 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
806 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
807 if (!data_mem)
808 return nullptr;
809
Chia-I Wu950d6e12016-05-03 09:12:35 +0800810 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800811}
812
813void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
814 data->~DeviceData();
815 allocator.pfnFree(allocator.pUserData, data);
816}
817
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800818} // anonymous namespace
819
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800820bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800821 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800822}
823
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800824const VkAllocationCallbacks& GetDefaultAllocator() {
825 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
826 .pUserData = nullptr,
827 .pfnAllocation = DefaultAllocate,
828 .pfnReallocation = DefaultReallocate,
829 .pfnFree = DefaultFree,
830 };
831
832 return kDefaultAllocCallbacks;
833}
834
Chia-I Wueb7db122016-03-24 09:11:06 +0800835PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
836 const ProcHook* hook = GetProcHook(pName);
837 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800838 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800839
840 if (!instance) {
841 if (hook->type == ProcHook::GLOBAL)
842 return hook->proc;
843
Chia-I Wu109f8982016-04-22 06:40:40 +0800844 // v0 layers expect
845 //
846 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
847 //
848 // to work.
849 if (strcmp(pName, "vkCreateDevice") == 0)
850 return hook->proc;
851
Chia-I Wueb7db122016-03-24 09:11:06 +0800852 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800853 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800854 pName);
855
Chia-I Wu109f8982016-04-22 06:40:40 +0800856 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800857 }
858
859 PFN_vkVoidFunction proc;
860
861 switch (hook->type) {
862 case ProcHook::INSTANCE:
863 proc = (GetData(instance).hook_extensions[hook->extension])
864 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800865 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800866 break;
867 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700868 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800869 ? hook->proc
870 : hook->checked_proc;
871 break;
872 default:
873 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800874 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800875 pName);
876 proc = nullptr;
877 break;
878 }
879
880 return proc;
881}
882
883PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
884 const ProcHook* hook = GetProcHook(pName);
885 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800886 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800887
888 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800889 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800890 return nullptr;
891 }
892
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800893 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
894 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800895}
896
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800897VkResult EnumerateInstanceExtensionProperties(
898 const char* pLayerName,
899 uint32_t* pPropertyCount,
900 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700901 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600902 loader_extensions.push_back({
903 VK_KHR_SURFACE_EXTENSION_NAME,
904 VK_KHR_SURFACE_SPEC_VERSION});
905 loader_extensions.push_back({
906 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
907 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
908 loader_extensions.push_back({
909 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
910 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700911 loader_extensions.push_back({
912 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
913 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600914
Chia-I Wu31938252016-05-23 15:31:02 +0800915 static const VkExtensionProperties loader_debug_report_extension = {
916 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
917 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800918
919 // enumerate our extensions first
920 if (!pLayerName && pProperties) {
921 uint32_t count = std::min(
922 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
923
Yiwei Zhang5e862202019-06-21 14:59:16 -0700924 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800925
926 if (count < loader_extensions.size()) {
927 *pPropertyCount = count;
928 return VK_INCOMPLETE;
929 }
930
931 pProperties += count;
932 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800933
934 if (Hal::Get().GetDebugReportIndex() < 0) {
935 if (!*pPropertyCount) {
936 *pPropertyCount = count;
937 return VK_INCOMPLETE;
938 }
939
940 pProperties[0] = loader_debug_report_extension;
941 pProperties += 1;
942 *pPropertyCount -= 1;
943 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800944 }
945
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800946 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800947 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800948 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800949 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800950
Chia-I Wu31938252016-05-23 15:31:02 +0800951 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
952 int idx = Hal::Get().GetDebugReportIndex();
953 if (idx < 0) {
954 *pPropertyCount += 1;
955 } else if (pProperties &&
956 static_cast<uint32_t>(idx) < *pPropertyCount) {
957 pProperties[idx].specVersion =
958 std::min(pProperties[idx].specVersion,
959 loader_debug_report_extension.specVersion);
960 }
961
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800962 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800963 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800964
965 return result;
966}
967
Chris Forbesfa25e632017-02-22 12:36:02 +1300968bool QueryPresentationProperties(
969 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700970 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300971 const InstanceData& data = GetData(physicalDevice);
972
973 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700974 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
975 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300976 return false;
977
978 // Request the android-specific presentation properties via GPDP2
979 VkPhysicalDeviceProperties2KHR properties = {
980 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
981 presentation_properties,
982 {}
983 };
984
985#pragma clang diagnostic push
986#pragma clang diagnostic ignored "-Wold-style-cast"
987 presentation_properties->sType =
988 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
989#pragma clang diagnostic pop
990 presentation_properties->pNext = nullptr;
991 presentation_properties->sharedImage = VK_FALSE;
992
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700993 if (data.driver.GetPhysicalDeviceProperties2KHR) {
994 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
995 &properties);
996 } else {
997 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
998 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300999
1000 return true;
1001}
1002
Chia-I Wu01cf3052016-03-24 16:16:21 +08001003VkResult EnumerateDeviceExtensionProperties(
1004 VkPhysicalDevice physicalDevice,
1005 const char* pLayerName,
1006 uint32_t* pPropertyCount,
1007 VkExtensionProperties* pProperties) {
1008 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001009 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001010 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001011 loader_extensions.push_back({
1012 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1013 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001014
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001015 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001016 if (hdrBoardConfig) {
1017 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1018 VK_EXT_HDR_METADATA_SPEC_VERSION});
1019 }
1020
Chris Forbes16095002017-05-05 15:33:29 -07001021 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
1022 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
1023 presentation_properties.sharedImage) {
1024 loader_extensions.push_back({
1025 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1026 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001027 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001028
Ian Elliott5c34de22017-04-10 14:42:30 -06001029 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1030 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -07001031 const std::string timestamp_property("service.sf.present_timestamp");
1032 android::base::WaitForPropertyCreation(timestamp_property);
1033 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001034 loader_extensions.push_back({
1035 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1036 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1037 }
1038
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001039 // enumerate our extensions first
1040 if (!pLayerName && pProperties) {
1041 uint32_t count = std::min(
1042 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1043
Yiwei Zhang5e862202019-06-21 14:59:16 -07001044 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001045
1046 if (count < loader_extensions.size()) {
1047 *pPropertyCount = count;
1048 return VK_INCOMPLETE;
1049 }
1050
1051 pProperties += count;
1052 *pPropertyCount -= count;
1053 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001054
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001055 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001056 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1057 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001058 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001059
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001060 if (pProperties) {
1061 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1062 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1063 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001064
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001065 if (strcmp(prop.extensionName,
1066 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1067 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001068
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001069 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1070 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001071
1072 if (prop.specVersion >= 8) {
1073 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1074 } else {
1075 prop.specVersion = 68;
1076 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001077 }
1078 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001079
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001080 // restore loader extension count
1081 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1082 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001083 }
1084
1085 return result;
1086}
1087
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001088VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1089 const VkAllocationCallbacks* pAllocator,
1090 VkInstance* pInstance) {
1091 const VkAllocationCallbacks& data_allocator =
1092 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1093
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001094 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001095 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001096 if (result != VK_SUCCESS)
1097 return result;
1098
1099 InstanceData* data = AllocateInstanceData(data_allocator);
1100 if (!data)
1101 return VK_ERROR_OUT_OF_HOST_MEMORY;
1102
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001103 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001104
1105 // call into the driver
1106 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001107 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001108 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001109 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1110 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001111 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001112 if (result != VK_SUCCESS) {
1113 FreeInstanceData(data, data_allocator);
1114 return result;
1115 }
1116
1117 // initialize InstanceDriverTable
1118 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001119 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001120 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001121 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001122 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001123 if (data->driver.DestroyInstance)
1124 data->driver.DestroyInstance(instance, pAllocator);
1125
1126 FreeInstanceData(data, data_allocator);
1127
1128 return VK_ERROR_INCOMPATIBLE_DRIVER;
1129 }
1130
1131 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001132 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001133 if (!data->get_device_proc_addr) {
1134 data->driver.DestroyInstance(instance, pAllocator);
1135 FreeInstanceData(data, data_allocator);
1136
1137 return VK_ERROR_INCOMPATIBLE_DRIVER;
1138 }
1139
1140 *pInstance = instance;
1141
1142 return VK_SUCCESS;
1143}
1144
1145void DestroyInstance(VkInstance instance,
1146 const VkAllocationCallbacks* pAllocator) {
1147 InstanceData& data = GetData(instance);
1148 data.driver.DestroyInstance(instance, pAllocator);
1149
1150 VkAllocationCallbacks local_allocator;
1151 if (!pAllocator) {
1152 local_allocator = data.allocator;
1153 pAllocator = &local_allocator;
1154 }
1155
1156 FreeInstanceData(&data, *pAllocator);
1157}
1158
Chia-I Wu4901db72016-03-24 16:38:58 +08001159VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1160 const VkDeviceCreateInfo* pCreateInfo,
1161 const VkAllocationCallbacks* pAllocator,
1162 VkDevice* pDevice) {
1163 const InstanceData& instance_data = GetData(physicalDevice);
1164 const VkAllocationCallbacks& data_allocator =
1165 (pAllocator) ? *pAllocator : instance_data.allocator;
1166
1167 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001168 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001169 if (result != VK_SUCCESS)
1170 return result;
1171
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001172 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001173 DeviceData* data = AllocateDeviceData(data_allocator,
1174 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001175 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001176 if (!data)
1177 return VK_ERROR_OUT_OF_HOST_MEMORY;
1178
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001179 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001180
1181 // call into the driver
1182 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001183 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001184 result = instance_data.driver.CreateDevice(
1185 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1186 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001187 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001188 if (result != VK_SUCCESS) {
1189 FreeDeviceData(data, data_allocator);
1190 return result;
1191 }
1192
1193 // initialize DeviceDriverTable
1194 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001195 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1196 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001197 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1198 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1199 if (data->driver.DestroyDevice)
1200 data->driver.DestroyDevice(dev, pAllocator);
1201
1202 FreeDeviceData(data, data_allocator);
1203
1204 return VK_ERROR_INCOMPATIBLE_DRIVER;
1205 }
Chris Forbesd8277912017-02-10 14:59:59 +13001206
1207 // sanity check ANDROID_native_buffer implementation, whose set of
1208 // entrypoints varies according to the spec version.
1209 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1210 !data->driver.GetSwapchainGrallocUsageANDROID &&
1211 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1212 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1213 " must expose at least one of "
1214 "vkGetSwapchainGrallocUsageANDROID or "
1215 "vkGetSwapchainGrallocUsage2ANDROID");
1216
1217 data->driver.DestroyDevice(dev, pAllocator);
1218 FreeDeviceData(data, data_allocator);
1219
1220 return VK_ERROR_INCOMPATIBLE_DRIVER;
1221 }
1222
Jesse Halldc225072016-05-30 22:40:14 -07001223 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001224
1225 *pDevice = dev;
1226
1227 return VK_SUCCESS;
1228}
1229
1230void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1231 DeviceData& data = GetData(device);
1232 data.driver.DestroyDevice(device, pAllocator);
1233
1234 VkAllocationCallbacks local_allocator;
1235 if (!pAllocator) {
1236 local_allocator = data.allocator;
1237 pAllocator = &local_allocator;
1238 }
1239
1240 FreeDeviceData(&data, *pAllocator);
1241}
1242
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001243VkResult EnumeratePhysicalDevices(VkInstance instance,
1244 uint32_t* pPhysicalDeviceCount,
1245 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001246 ATRACE_CALL();
1247
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001248 const auto& data = GetData(instance);
1249
1250 VkResult result = data.driver.EnumeratePhysicalDevices(
1251 instance, pPhysicalDeviceCount, pPhysicalDevices);
1252 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1253 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1254 SetData(pPhysicalDevices[i], data);
1255 }
1256
1257 return result;
1258}
1259
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001260VkResult EnumeratePhysicalDeviceGroups(
1261 VkInstance instance,
1262 uint32_t* pPhysicalDeviceGroupCount,
1263 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001264 ATRACE_CALL();
1265
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001266 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001267 const auto& data = GetData(instance);
1268
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001269 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1270 uint32_t device_count = 0;
1271 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1272 if (result < 0)
1273 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001274
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001275 if (!pPhysicalDeviceGroupProperties) {
1276 *pPhysicalDeviceGroupCount = device_count;
1277 return result;
1278 }
1279
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001280 if (!device_count) {
1281 *pPhysicalDeviceGroupCount = 0;
1282 return result;
1283 }
Chad Versace32c087f2018-09-09 07:28:05 -07001284 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1285 if (!device_count)
1286 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001287
Yiwei Zhang5e862202019-06-21 14:59:16 -07001288 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001289 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001290 result =
1291 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001292 if (result < 0)
1293 return result;
1294
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001295 for (uint32_t i = 0; i < device_count; ++i) {
1296 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1297 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1298 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1299 }
1300 } else {
1301 result = data.driver.EnumeratePhysicalDeviceGroups(
1302 instance, pPhysicalDeviceGroupCount,
1303 pPhysicalDeviceGroupProperties);
1304 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1305 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1306 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1307 for (uint32_t j = 0;
1308 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1309 j++) {
1310 SetData(
1311 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001312 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001313 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001314 }
1315 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001316 }
1317
1318 return result;
1319}
1320
Chia-I Wuba0be412016-03-24 16:24:40 +08001321void GetDeviceQueue(VkDevice device,
1322 uint32_t queueFamilyIndex,
1323 uint32_t queueIndex,
1324 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001325 ATRACE_CALL();
1326
Chia-I Wuba0be412016-03-24 16:24:40 +08001327 const auto& data = GetData(device);
1328
1329 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1330 SetData(*pQueue, data);
1331}
1332
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001333void GetDeviceQueue2(VkDevice device,
1334 const VkDeviceQueueInfo2* pQueueInfo,
1335 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001336 ATRACE_CALL();
1337
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001338 const auto& data = GetData(device);
1339
1340 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001341 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001342}
1343
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001344VKAPI_ATTR VkResult
1345AllocateCommandBuffers(VkDevice device,
1346 const VkCommandBufferAllocateInfo* pAllocateInfo,
1347 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001348 ATRACE_CALL();
1349
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001350 const auto& data = GetData(device);
1351
1352 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1353 pCommandBuffers);
1354 if (result == VK_SUCCESS) {
1355 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1356 SetData(pCommandBuffers[i], data);
1357 }
1358
1359 return result;
1360}
1361
Yiwei Zhang899d1752019-09-23 16:05:35 -07001362VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1363 uint32_t submitCount,
1364 const VkSubmitInfo* pSubmits,
1365 VkFence fence) {
1366 ATRACE_CALL();
1367
1368 const auto& data = GetData(queue);
1369
1370 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1371}
1372
Chia-I Wu9d518162016-03-24 14:55:27 +08001373} // namespace driver
1374} // namespace vulkan