blob: 2f6747dd7034fb91d1740ec6c562837d5b6411e5 [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
Jesse Hall53457db2016-12-14 16:54:06 -080026#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080027#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070028#include <android-base/properties.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080030#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
Yiwei Zhange40dd732019-08-05 16:41:03 -070033#include <nativeloader/dlext_namespaces.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070034#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080035#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080036#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037
Yiwei Zhang5e862202019-06-21 14:59:16 -070038#include <algorithm>
39#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070040#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070041#include <new>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070042#include <string_view>
43#include <sstream>
Yiwei Zhang5e862202019-06-21 14:59:16 -070044#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070045
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070046#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080047
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080048using namespace android::hardware::configstore;
49using namespace android::hardware::configstore::V1_0;
50
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080051// #define ENABLE_ALLOC_CALLSTACKS 1
52#if ENABLE_ALLOC_CALLSTACKS
53#include <utils/CallStack.h>
54#define ALOGD_CALLSTACK(...) \
55 do { \
56 ALOGD(__VA_ARGS__); \
57 android::CallStack callstack; \
58 callstack.update(); \
59 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
60 } while (false)
61#else
62#define ALOGD_CALLSTACK(...) \
63 do { \
64 } while (false)
65#endif
66
Chia-I Wu9d518162016-03-24 14:55:27 +080067namespace vulkan {
68namespace driver {
69
Chia-I Wu136b8eb2016-03-24 15:01:52 +080070namespace {
71
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080072class Hal {
73 public:
74 static bool Open();
75
76 static const Hal& Get() { return hal_; }
77 static const hwvulkan_device_t& Device() { return *Get().dev_; }
78
Chia-I Wu31938252016-05-23 15:31:02 +080079 int GetDebugReportIndex() const { return debug_report_index_; }
80
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 private:
Chia-I Wu31938252016-05-23 15:31:02 +080082 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080083 Hal(const Hal&) = delete;
84 Hal& operator=(const Hal&) = delete;
85
Chia-I Wu31938252016-05-23 15:31:02 +080086 bool InitDebugReportIndex();
87
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080088 static Hal hal_;
89
90 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080091 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080092};
93
Chia-I Wu4901db72016-03-24 16:38:58 +080094class CreateInfoWrapper {
95 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080096 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080097 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080098 CreateInfoWrapper(VkPhysicalDevice physical_dev,
99 const VkDeviceCreateInfo& create_info,
100 const VkAllocationCallbacks& allocator);
101 ~CreateInfoWrapper();
102
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800103 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600104 void DowngradeApiVersion();
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700105 void UpgradeDeviceCoreApiVersion(uint32_t api_version);
Chia-I Wu4901db72016-03-24 16:38:58 +0800106
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800107 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800109
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800110 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800111 explicit operator const VkDeviceCreateInfo*() const;
112
113 private:
114 struct ExtensionFilter {
115 VkExtensionProperties* exts;
116 uint32_t ext_count;
117
118 const char** names;
119 uint32_t name_count;
120 };
121
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800122 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800123
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800124 VkResult SanitizeLayers();
125 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800126
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800127 VkResult QueryExtensionCount(uint32_t& count) const;
128 VkResult EnumerateExtensions(uint32_t& count,
129 VkExtensionProperties* props) const;
130 VkResult InitExtensionFilter();
131 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800132
133 const bool is_instance_;
134 const VkAllocationCallbacks& allocator_;
135
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_;
144
Chia-I Wu4901db72016-03-24 16:38:58 +0800145 ExtensionFilter extension_filter_;
146
147 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
148 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
149};
150
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800151Hal Hal::hal_;
152
Jesse Hall53457db2016-12-14 16:54:06 -0800153void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700154 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800155 ATRACE_CALL();
156
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700157 std::stringstream ss;
158 ss << "vulkan." << subname << ".so";
159 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800160}
161
162const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
163 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
164 "ro.board.platform",
165}};
166
Jesse Hall00e61ff2017-04-07 16:48:02 -0700167int LoadDriver(android_namespace_t* library_namespace,
168 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800169 ATRACE_CALL();
170
Jesse Hall53457db2016-12-14 16:54:06 -0800171 const android_dlextinfo dlextinfo = {
172 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700173 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800174 };
Jesse Hall53457db2016-12-14 16:54:06 -0800175 void* so = nullptr;
176 char prop[PROPERTY_VALUE_MAX];
177 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
178 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700179 if (prop_len > 0 && prop_len <= UINT_MAX) {
180 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
181 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800182 if (so)
183 break;
184 }
185 }
186 if (!so)
187 return -ENOENT;
188
Jesse Hall00e61ff2017-04-07 16:48:02 -0700189 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800190 if (!hmi) {
191 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
192 dlclose(so);
193 return -EINVAL;
194 }
195 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
196 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
197 dlclose(so);
198 return -EINVAL;
199 }
200 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700201 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800202 return 0;
203}
204
Jesse Hall00e61ff2017-04-07 16:48:02 -0700205int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800206 ATRACE_CALL();
207
Jesse Hall00e61ff2017-04-07 16:48:02 -0700208 auto ns = android_get_exported_namespace("sphal");
209 if (!ns)
210 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800211 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700212 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700213 return LoadDriver(ns, module);
214}
215
216int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800217 ATRACE_CALL();
218
Jesse Hall00e61ff2017-04-07 16:48:02 -0700219 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
220 if (!ns)
221 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800222 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700223 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700224 return LoadDriver(ns, module);
225}
226
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800227bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800228 ATRACE_CALL();
229
Yiwei Zhangd9861812019-02-13 11:51:55 -0800230 const nsecs_t openTime = systemTime();
231
Jesse Halldc225072016-05-30 22:40:14 -0700232 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800233
234 // Use a stub device unless we successfully open a real HAL device.
235 hal_.dev_ = &stubhal::kDevice;
236
Jesse Hall53457db2016-12-14 16:54:06 -0800237 int result;
238 const hwvulkan_module_t* module = nullptr;
239
Jesse Hall00e61ff2017-04-07 16:48:02 -0700240 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800241 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700242 result = LoadBuiltinDriver(&module);
243 if (result != 0) {
244 // -ENOENT means the sphal namespace doesn't exist, not that there
245 // is a problem with the driver.
246 ALOGW_IF(
247 result != -ENOENT,
248 "Failed to load Vulkan driver into sphal namespace. This "
249 "usually means the driver has forbidden library dependencies."
250 "Please fix, this will soon stop working.");
251 result =
252 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
253 reinterpret_cast<const hw_module_t**>(&module));
254 }
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),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800330 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800331 instance_info_(create_info),
332 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700333 // instance core versions need to match the loader api version
334 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
335 i != ProcHook::EXTENSION_COUNT; ++i) {
336 hook_extensions_.set(i);
337 hal_extensions_.set(i);
338 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800339}
340
Chia-I Wu4901db72016-03-24 16:38:58 +0800341CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
342 const VkDeviceCreateInfo& create_info,
343 const VkAllocationCallbacks& allocator)
344 : is_instance_(false),
345 allocator_(allocator),
346 physical_dev_(physical_dev),
347 dev_info_(create_info),
348 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700349 // initialize with baseline core API version
350 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
351 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
Chia-I Wu4901db72016-03-24 16:38:58 +0800352}
353
354CreateInfoWrapper::~CreateInfoWrapper() {
355 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
356 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
357}
358
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800359VkResult CreateInfoWrapper::Validate() {
360 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800361 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800362 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800363 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800364 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800365
366 return result;
367}
368
369const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800370CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800371 return hook_extensions_;
372}
373
374const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800375CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800376 return hal_extensions_;
377}
378
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800379CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
380 return &instance_info_;
381}
382
Chia-I Wu4901db72016-03-24 16:38:58 +0800383CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
384 return &dev_info_;
385}
386
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800387VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800388 const struct StructHeader {
389 VkStructureType type;
390 const void* next;
391 } * header;
392
393 if (is_instance_) {
394 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
395
396 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
397 while (header &&
398 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
399 header = reinterpret_cast<const StructHeader*>(header->next);
400
401 instance_info_.pNext = header;
402 } else {
403 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
404
405 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
406 while (header &&
407 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
408 header = reinterpret_cast<const StructHeader*>(header->next);
409
410 dev_info_.pNext = header;
411 }
412
413 return VK_SUCCESS;
414}
415
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800416VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800417 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
418 : dev_info_.ppEnabledLayerNames;
419 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
420 : dev_info_.enabledLayerCount;
421
422 // remove all layers
423 layer_names = nullptr;
424 layer_count = 0;
425
426 return VK_SUCCESS;
427}
428
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800429VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800430 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
431 : dev_info_.ppEnabledExtensionNames;
432 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
433 : dev_info_.enabledExtensionCount;
434 if (!ext_count)
435 return VK_SUCCESS;
436
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800437 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800438 if (result != VK_SUCCESS)
439 return result;
440
441 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800442 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800443
Jesse Halld3d887a2018-03-05 13:34:45 -0800444 // Enable device extensions that contain physical-device commands, so that
445 // vkGetInstanceProcAddr will return those physical-device commands.
446 if (is_instance_) {
447 hook_extensions_.set(ProcHook::KHR_swapchain);
448 }
449
Chia-I Wu4901db72016-03-24 16:38:58 +0800450 ext_names = extension_filter_.names;
451 ext_count = extension_filter_.name_count;
452
453 return VK_SUCCESS;
454}
455
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800456VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800457 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800458 return Hal::Device().EnumerateInstanceExtensionProperties(
459 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800460 } else {
461 const auto& driver = GetData(physical_dev_).driver;
462 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
463 &count, nullptr);
464 }
465}
466
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800467VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800468 uint32_t& count,
469 VkExtensionProperties* props) const {
470 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800471 return Hal::Device().EnumerateInstanceExtensionProperties(
472 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800473 } else {
474 const auto& driver = GetData(physical_dev_).driver;
475 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
476 &count, props);
477 }
478}
479
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800480VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800481 // query extension count
482 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800483 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800484 if (result != VK_SUCCESS || count == 0)
485 return result;
486
487 auto& filter = extension_filter_;
488 filter.exts =
489 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
490 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
491 alignof(VkExtensionProperties),
492 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
493 if (!filter.exts)
494 return VK_ERROR_OUT_OF_HOST_MEMORY;
495
496 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800497 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800498 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
499 return result;
500
501 if (!count)
502 return VK_SUCCESS;
503
504 filter.ext_count = count;
505
506 // allocate name array
507 uint32_t enabled_ext_count = (is_instance_)
508 ? instance_info_.enabledExtensionCount
509 : dev_info_.enabledExtensionCount;
510 count = std::min(filter.ext_count, enabled_ext_count);
511 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
512 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
513 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
514 if (!filter.names)
515 return VK_ERROR_OUT_OF_HOST_MEMORY;
516
517 return VK_SUCCESS;
518}
519
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800520void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800521 auto& filter = extension_filter_;
522
523 ProcHook::Extension ext_bit = GetProcHookExtension(name);
524 if (is_instance_) {
525 switch (ext_bit) {
526 case ProcHook::KHR_android_surface:
527 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700528 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300529 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800530 hook_extensions_.set(ext_bit);
531 // return now as these extensions do not require HAL support
532 return;
533 case ProcHook::EXT_debug_report:
534 // both we and HAL can take part in
535 hook_extensions_.set(ext_bit);
536 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300537 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700538 case ProcHook::EXTENSION_UNKNOWN:
539 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800540 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700541
Yiwei Zhang23143102019-04-10 18:24:05 -0700542 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700543 case ProcHook::KHR_incremental_present:
544 case ProcHook::KHR_shared_presentable_image:
545 case ProcHook::KHR_swapchain:
546 case ProcHook::EXT_hdr_metadata:
547 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
548 case ProcHook::ANDROID_native_buffer:
549 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700550 case ProcHook::EXTENSION_CORE_1_0:
551 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700552 case ProcHook::EXTENSION_COUNT:
553 // Device and meta extensions. If we ever get here it's a bug in
554 // our code. But enumerating them lets us avoid having a default
555 // case, and default hides other bugs.
556 ALOGE(
557 "CreateInfoWrapper::FilterExtension: invalid instance "
558 "extension '%s'. FIX ME",
559 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800560 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700561
562 // Don't use a default case. Without it, -Wswitch will tell us
563 // at compile time if someone adds a new ProcHook extension but
564 // doesn't handle it above. That's a real bug that has
565 // not-immediately-obvious effects.
566 //
567 // default:
568 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800569 }
570 } else {
571 switch (ext_bit) {
572 case ProcHook::KHR_swapchain:
573 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
574 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
575 ext_bit = ProcHook::ANDROID_native_buffer;
576 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700577 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700578 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300579 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700580 hook_extensions_.set(ext_bit);
581 // return now as these extensions do not require HAL support
582 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700583 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700584 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700585 hook_extensions_.set(ext_bit);
586 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700587 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800588 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700589 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800590 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700591
592 case ProcHook::KHR_android_surface:
593 case ProcHook::KHR_get_physical_device_properties2:
594 case ProcHook::KHR_get_surface_capabilities2:
595 case ProcHook::KHR_surface:
596 case ProcHook::EXT_debug_report:
597 case ProcHook::EXT_swapchain_colorspace:
598 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700599 case ProcHook::EXTENSION_CORE_1_0:
600 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700601 case ProcHook::EXTENSION_COUNT:
602 // Instance and meta extensions. If we ever get here it's a bug
603 // in our code. But enumerating them lets us avoid having a
604 // default case, and default hides other bugs.
605 ALOGE(
606 "CreateInfoWrapper::FilterExtension: invalid device "
607 "extension '%s'. FIX ME",
608 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800609 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700610
611 // Don't use a default case. Without it, -Wswitch will tell us
612 // at compile time if someone adds a new ProcHook extension but
613 // doesn't handle it above. That's a real bug that has
614 // not-immediately-obvious effects.
615 //
616 // default:
617 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800618 }
619 }
620
621 for (uint32_t i = 0; i < filter.ext_count; i++) {
622 const VkExtensionProperties& props = filter.exts[i];
623 // ignore unknown extensions
624 if (strcmp(name, props.extensionName) != 0)
625 continue;
626
Chia-I Wu4901db72016-03-24 16:38:58 +0800627 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800628 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
629 if (ext_bit == ProcHook::ANDROID_native_buffer)
630 hook_extensions_.set(ProcHook::KHR_swapchain);
631
632 hal_extensions_.set(ext_bit);
633 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800634
635 break;
636 }
637}
638
Ian Elliottf3e872d2017-11-02 10:15:13 -0600639void CreateInfoWrapper::DowngradeApiVersion() {
640 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
641 if (instance_info_.pApplicationInfo) {
642 application_info_ = *instance_info_.pApplicationInfo;
643 instance_info_.pApplicationInfo = &application_info_;
644 application_info_.apiVersion = VK_API_VERSION_1_0;
645 }
646}
647
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700648void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
649 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
650
651#pragma clang diagnostic push
652#pragma clang diagnostic ignored "-Wold-style-cast"
653 api_version ^= VK_VERSION_PATCH(api_version);
654#pragma clang diagnostic pop
655
656 // cap the API version to the loader supported highest version
657 if (api_version > VK_API_VERSION_1_1)
658 api_version = VK_API_VERSION_1_1;
659
660 switch (api_version) {
661 case VK_API_VERSION_1_1:
662 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
663 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
664 [[clang::fallthrough]];
665 case VK_API_VERSION_1_0:
666 break;
667 default:
668 ALOGD("Unknown upgrade API version[%u]", api_version);
669 break;
670 }
671}
672
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800673VKAPI_ATTR void* DefaultAllocate(void*,
674 size_t size,
675 size_t alignment,
676 VkSystemAllocationScope) {
677 void* ptr = nullptr;
678 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
679 // additionally requires that it be at least sizeof(void*).
680 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
681 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
682 ret, ptr);
683 return ret == 0 ? ptr : nullptr;
684}
685
686VKAPI_ATTR void* DefaultReallocate(void*,
687 void* ptr,
688 size_t size,
689 size_t alignment,
690 VkSystemAllocationScope) {
691 if (size == 0) {
692 free(ptr);
693 return nullptr;
694 }
695
696 // TODO(jessehall): Right now we never shrink allocations; if the new
697 // request is smaller than the existing chunk, we just continue using it.
698 // Right now the loader never reallocs, so this doesn't matter. If that
699 // changes, or if this code is copied into some other project, this should
700 // probably have a heuristic to allocate-copy-free when doing so will save
701 // "enough" space.
702 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
703 if (size <= old_size)
704 return ptr;
705
706 void* new_ptr = nullptr;
707 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
708 return nullptr;
709 if (ptr) {
710 memcpy(new_ptr, ptr, std::min(old_size, size));
711 free(ptr);
712 }
713 return new_ptr;
714}
715
716VKAPI_ATTR void DefaultFree(void*, void* ptr) {
717 ALOGD_CALLSTACK("Free: %p", ptr);
718 free(ptr);
719}
720
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800721InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
722 void* data_mem = allocator.pfnAllocation(
723 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
724 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
725 if (!data_mem)
726 return nullptr;
727
728 return new (data_mem) InstanceData(allocator);
729}
730
731void FreeInstanceData(InstanceData* data,
732 const VkAllocationCallbacks& allocator) {
733 data->~InstanceData();
734 allocator.pfnFree(allocator.pUserData, data);
735}
736
Chia-I Wu950d6e12016-05-03 09:12:35 +0800737DeviceData* AllocateDeviceData(
738 const VkAllocationCallbacks& allocator,
739 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800740 void* data_mem = allocator.pfnAllocation(
741 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
742 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
743 if (!data_mem)
744 return nullptr;
745
Chia-I Wu950d6e12016-05-03 09:12:35 +0800746 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800747}
748
749void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
750 data->~DeviceData();
751 allocator.pfnFree(allocator.pUserData, data);
752}
753
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800754} // anonymous namespace
755
Chia-I Wu9d518162016-03-24 14:55:27 +0800756bool Debuggable() {
757 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
758}
759
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800760bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800761 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800762}
763
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800764const VkAllocationCallbacks& GetDefaultAllocator() {
765 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
766 .pUserData = nullptr,
767 .pfnAllocation = DefaultAllocate,
768 .pfnReallocation = DefaultReallocate,
769 .pfnFree = DefaultFree,
770 };
771
772 return kDefaultAllocCallbacks;
773}
774
Chia-I Wueb7db122016-03-24 09:11:06 +0800775PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
776 const ProcHook* hook = GetProcHook(pName);
777 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800778 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800779
780 if (!instance) {
781 if (hook->type == ProcHook::GLOBAL)
782 return hook->proc;
783
Chia-I Wu109f8982016-04-22 06:40:40 +0800784 // v0 layers expect
785 //
786 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
787 //
788 // to work.
789 if (strcmp(pName, "vkCreateDevice") == 0)
790 return hook->proc;
791
Chia-I Wueb7db122016-03-24 09:11:06 +0800792 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800793 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800794 pName);
795
Chia-I Wu109f8982016-04-22 06:40:40 +0800796 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800797 }
798
799 PFN_vkVoidFunction proc;
800
801 switch (hook->type) {
802 case ProcHook::INSTANCE:
803 proc = (GetData(instance).hook_extensions[hook->extension])
804 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800805 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800806 break;
807 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700808 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800809 ? hook->proc
810 : hook->checked_proc;
811 break;
812 default:
813 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800814 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800815 pName);
816 proc = nullptr;
817 break;
818 }
819
820 return proc;
821}
822
823PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
824 const ProcHook* hook = GetProcHook(pName);
825 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800826 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800827
828 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800829 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800830 return nullptr;
831 }
832
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800833 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
834 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800835}
836
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800837VkResult EnumerateInstanceExtensionProperties(
838 const char* pLayerName,
839 uint32_t* pPropertyCount,
840 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700841 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600842 loader_extensions.push_back({
843 VK_KHR_SURFACE_EXTENSION_NAME,
844 VK_KHR_SURFACE_SPEC_VERSION});
845 loader_extensions.push_back({
846 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
847 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
848 loader_extensions.push_back({
849 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
850 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700851 loader_extensions.push_back({
852 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
853 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600854
Chia-I Wu31938252016-05-23 15:31:02 +0800855 static const VkExtensionProperties loader_debug_report_extension = {
856 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
857 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800858
859 // enumerate our extensions first
860 if (!pLayerName && pProperties) {
861 uint32_t count = std::min(
862 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
863
Yiwei Zhang5e862202019-06-21 14:59:16 -0700864 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800865
866 if (count < loader_extensions.size()) {
867 *pPropertyCount = count;
868 return VK_INCOMPLETE;
869 }
870
871 pProperties += count;
872 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800873
874 if (Hal::Get().GetDebugReportIndex() < 0) {
875 if (!*pPropertyCount) {
876 *pPropertyCount = count;
877 return VK_INCOMPLETE;
878 }
879
880 pProperties[0] = loader_debug_report_extension;
881 pProperties += 1;
882 *pPropertyCount -= 1;
883 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800884 }
885
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800886 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800887 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800888 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800889 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800890
Chia-I Wu31938252016-05-23 15:31:02 +0800891 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
892 int idx = Hal::Get().GetDebugReportIndex();
893 if (idx < 0) {
894 *pPropertyCount += 1;
895 } else if (pProperties &&
896 static_cast<uint32_t>(idx) < *pPropertyCount) {
897 pProperties[idx].specVersion =
898 std::min(pProperties[idx].specVersion,
899 loader_debug_report_extension.specVersion);
900 }
901
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800902 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800903 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800904
905 return result;
906}
907
Chris Forbesfa25e632017-02-22 12:36:02 +1300908bool QueryPresentationProperties(
909 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700910 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300911 const InstanceData& data = GetData(physicalDevice);
912
913 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700914 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
915 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300916 return false;
917
918 // Request the android-specific presentation properties via GPDP2
919 VkPhysicalDeviceProperties2KHR properties = {
920 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
921 presentation_properties,
922 {}
923 };
924
925#pragma clang diagnostic push
926#pragma clang diagnostic ignored "-Wold-style-cast"
927 presentation_properties->sType =
928 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
929#pragma clang diagnostic pop
930 presentation_properties->pNext = nullptr;
931 presentation_properties->sharedImage = VK_FALSE;
932
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700933 if (data.driver.GetPhysicalDeviceProperties2KHR) {
934 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
935 &properties);
936 } else {
937 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
938 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300939
940 return true;
941}
942
Chia-I Wu01cf3052016-03-24 16:16:21 +0800943VkResult EnumerateDeviceExtensionProperties(
944 VkPhysicalDevice physicalDevice,
945 const char* pLayerName,
946 uint32_t* pPropertyCount,
947 VkExtensionProperties* pProperties) {
948 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300949 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700950 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300951 loader_extensions.push_back({
952 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
953 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300954
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800955 bool hdrBoardConfig =
956 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
957 false);
958 if (hdrBoardConfig) {
959 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
960 VK_EXT_HDR_METADATA_SPEC_VERSION});
961 }
962
Chris Forbes16095002017-05-05 15:33:29 -0700963 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
964 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
965 presentation_properties.sharedImage) {
966 loader_extensions.push_back({
967 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
968 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300969 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700970
Ian Elliott5c34de22017-04-10 14:42:30 -0600971 // conditionally add VK_GOOGLE_display_timing if present timestamps are
972 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700973 const std::string timestamp_property("service.sf.present_timestamp");
974 android::base::WaitForPropertyCreation(timestamp_property);
975 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600976 loader_extensions.push_back({
977 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
978 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
979 }
980
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700981 // enumerate our extensions first
982 if (!pLayerName && pProperties) {
983 uint32_t count = std::min(
984 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
985
Yiwei Zhang5e862202019-06-21 14:59:16 -0700986 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700987
988 if (count < loader_extensions.size()) {
989 *pPropertyCount = count;
990 return VK_INCOMPLETE;
991 }
992
993 pProperties += count;
994 *pPropertyCount -= count;
995 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800996
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800997 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800998 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
999 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001000 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001001
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001002 if (pProperties) {
1003 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1004 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1005 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001006
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001007 if (strcmp(prop.extensionName,
1008 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1009 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001010
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001011 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1012 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001013
1014 if (prop.specVersion >= 8) {
1015 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1016 } else {
1017 prop.specVersion = 68;
1018 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001019 }
1020 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001021
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001022 // restore loader extension count
1023 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1024 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001025 }
1026
1027 return result;
1028}
1029
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001030VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1031 const VkAllocationCallbacks* pAllocator,
1032 VkInstance* pInstance) {
1033 const VkAllocationCallbacks& data_allocator =
1034 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1035
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001036 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001037 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001038 if (result != VK_SUCCESS)
1039 return result;
1040
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001041 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001042 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001043 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001044 if (!data)
1045 return VK_ERROR_OUT_OF_HOST_MEMORY;
1046
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001047 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001048
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001049 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001050#pragma clang diagnostic push
1051#pragma clang diagnostic ignored "-Wold-style-cast"
1052 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1053 ? pCreateInfo->pApplicationInfo->apiVersion
1054 : VK_API_VERSION_1_0);
1055 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1056 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1057 uint32_t icd_api_version;
1058 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1059 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001060 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001061 "vkEnumerateInstanceVersion"));
1062 if (!pfn_enumerate_instance_version) {
1063 icd_api_version = VK_API_VERSION_1_0;
1064 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001065 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001066 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001067 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001068 }
1069 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1070 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1071
1072 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1073 ((api_major_version > 1) || (api_minor_version > 0))) {
1074 api_version = VK_API_VERSION_1_0;
1075 wrapper.DowngradeApiVersion();
1076 }
1077#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001078 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001079
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001080 // call into the driver
1081 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001082 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001083 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001084 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1085 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001086 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001087 if (result != VK_SUCCESS) {
1088 FreeInstanceData(data, data_allocator);
1089 return result;
1090 }
1091
1092 // initialize InstanceDriverTable
1093 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001094 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001095 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001096 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001097 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001098 if (data->driver.DestroyInstance)
1099 data->driver.DestroyInstance(instance, pAllocator);
1100
1101 FreeInstanceData(data, data_allocator);
1102
1103 return VK_ERROR_INCOMPATIBLE_DRIVER;
1104 }
1105
1106 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001107 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001108 if (!data->get_device_proc_addr) {
1109 data->driver.DestroyInstance(instance, pAllocator);
1110 FreeInstanceData(data, data_allocator);
1111
1112 return VK_ERROR_INCOMPATIBLE_DRIVER;
1113 }
1114
1115 *pInstance = instance;
1116
1117 return VK_SUCCESS;
1118}
1119
1120void DestroyInstance(VkInstance instance,
1121 const VkAllocationCallbacks* pAllocator) {
1122 InstanceData& data = GetData(instance);
1123 data.driver.DestroyInstance(instance, pAllocator);
1124
1125 VkAllocationCallbacks local_allocator;
1126 if (!pAllocator) {
1127 local_allocator = data.allocator;
1128 pAllocator = &local_allocator;
1129 }
1130
1131 FreeInstanceData(&data, *pAllocator);
1132}
1133
Chia-I Wu4901db72016-03-24 16:38:58 +08001134VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1135 const VkDeviceCreateInfo* pCreateInfo,
1136 const VkAllocationCallbacks* pAllocator,
1137 VkDevice* pDevice) {
1138 const InstanceData& instance_data = GetData(physicalDevice);
1139 const VkAllocationCallbacks& data_allocator =
1140 (pAllocator) ? *pAllocator : instance_data.allocator;
1141
1142 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001143 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001144 if (result != VK_SUCCESS)
1145 return result;
1146
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001147 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001148 DeviceData* data = AllocateDeviceData(data_allocator,
1149 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001150 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001151 if (!data)
1152 return VK_ERROR_OUT_OF_HOST_MEMORY;
1153
Yiwei Zhang7cc36a52019-10-11 19:02:09 -07001154 VkPhysicalDeviceProperties properties;
1155 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1156 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1157 &properties);
1158 ATRACE_END();
1159
1160 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001161 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001162
1163 // call into the driver
1164 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001165 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001166 result = instance_data.driver.CreateDevice(
1167 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1168 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001169 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001170 if (result != VK_SUCCESS) {
1171 FreeDeviceData(data, data_allocator);
1172 return result;
1173 }
1174
1175 // initialize DeviceDriverTable
1176 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001177 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1178 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001179 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1180 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1181 if (data->driver.DestroyDevice)
1182 data->driver.DestroyDevice(dev, pAllocator);
1183
1184 FreeDeviceData(data, data_allocator);
1185
1186 return VK_ERROR_INCOMPATIBLE_DRIVER;
1187 }
Chris Forbesd8277912017-02-10 14:59:59 +13001188
1189 // sanity check ANDROID_native_buffer implementation, whose set of
1190 // entrypoints varies according to the spec version.
1191 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1192 !data->driver.GetSwapchainGrallocUsageANDROID &&
1193 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1194 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1195 " must expose at least one of "
1196 "vkGetSwapchainGrallocUsageANDROID or "
1197 "vkGetSwapchainGrallocUsage2ANDROID");
1198
1199 data->driver.DestroyDevice(dev, pAllocator);
1200 FreeDeviceData(data, data_allocator);
1201
1202 return VK_ERROR_INCOMPATIBLE_DRIVER;
1203 }
1204
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001205 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1206 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001207 android::GraphicsEnv::getInstance().setTargetStats(
1208 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001209 }
1210
Jesse Halldc225072016-05-30 22:40:14 -07001211 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001212 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001213
1214 *pDevice = dev;
1215
1216 return VK_SUCCESS;
1217}
1218
1219void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1220 DeviceData& data = GetData(device);
1221 data.driver.DestroyDevice(device, pAllocator);
1222
1223 VkAllocationCallbacks local_allocator;
1224 if (!pAllocator) {
1225 local_allocator = data.allocator;
1226 pAllocator = &local_allocator;
1227 }
1228
1229 FreeDeviceData(&data, *pAllocator);
1230}
1231
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001232VkResult EnumeratePhysicalDevices(VkInstance instance,
1233 uint32_t* pPhysicalDeviceCount,
1234 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001235 ATRACE_CALL();
1236
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001237 const auto& data = GetData(instance);
1238
1239 VkResult result = data.driver.EnumeratePhysicalDevices(
1240 instance, pPhysicalDeviceCount, pPhysicalDevices);
1241 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1242 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1243 SetData(pPhysicalDevices[i], data);
1244 }
1245
1246 return result;
1247}
1248
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001249VkResult EnumeratePhysicalDeviceGroups(
1250 VkInstance instance,
1251 uint32_t* pPhysicalDeviceGroupCount,
1252 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001253 ATRACE_CALL();
1254
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001255 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001256 const auto& data = GetData(instance);
1257
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001258 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1259 uint32_t device_count = 0;
1260 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1261 if (result < 0)
1262 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001263
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001264 if (!pPhysicalDeviceGroupProperties) {
1265 *pPhysicalDeviceGroupCount = device_count;
1266 return result;
1267 }
1268
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001269 if (!device_count) {
1270 *pPhysicalDeviceGroupCount = 0;
1271 return result;
1272 }
Chad Versace32c087f2018-09-09 07:28:05 -07001273 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1274 if (!device_count)
1275 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001276
Yiwei Zhang5e862202019-06-21 14:59:16 -07001277 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001278 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001279 result =
1280 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001281 if (result < 0)
1282 return result;
1283
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001284 for (uint32_t i = 0; i < device_count; ++i) {
1285 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1286 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1287 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1288 }
1289 } else {
1290 result = data.driver.EnumeratePhysicalDeviceGroups(
1291 instance, pPhysicalDeviceGroupCount,
1292 pPhysicalDeviceGroupProperties);
1293 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1294 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1295 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1296 for (uint32_t j = 0;
1297 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1298 j++) {
1299 SetData(
1300 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001301 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001302 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001303 }
1304 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001305 }
1306
1307 return result;
1308}
1309
Chia-I Wuba0be412016-03-24 16:24:40 +08001310void GetDeviceQueue(VkDevice device,
1311 uint32_t queueFamilyIndex,
1312 uint32_t queueIndex,
1313 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001314 ATRACE_CALL();
1315
Chia-I Wuba0be412016-03-24 16:24:40 +08001316 const auto& data = GetData(device);
1317
1318 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1319 SetData(*pQueue, data);
1320}
1321
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001322void GetDeviceQueue2(VkDevice device,
1323 const VkDeviceQueueInfo2* pQueueInfo,
1324 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001325 ATRACE_CALL();
1326
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001327 const auto& data = GetData(device);
1328
1329 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001330 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001331}
1332
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001333VKAPI_ATTR VkResult
1334AllocateCommandBuffers(VkDevice device,
1335 const VkCommandBufferAllocateInfo* pAllocateInfo,
1336 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001337 ATRACE_CALL();
1338
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001339 const auto& data = GetData(device);
1340
1341 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1342 pCommandBuffers);
1343 if (result == VK_SUCCESS) {
1344 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1345 SetData(pCommandBuffers[i], data);
1346 }
1347
1348 return result;
1349}
1350
Yiwei Zhang899d1752019-09-23 16:05:35 -07001351VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1352 uint32_t submitCount,
1353 const VkSubmitInfo* pSubmits,
1354 VkFence fence) {
1355 ATRACE_CALL();
1356
1357 const auto& data = GetData(queue);
1358
1359 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1360}
1361
Chia-I Wu9d518162016-03-24 14:55:27 +08001362} // namespace driver
1363} // namespace vulkan