blob: 5d4717200defc1ceb8c33e3718a623fcf73df5e1 [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>
Mark Salyzyn7823e122016-09-29 08:08:05 -070025
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>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
33#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080034#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080035#include <utils/Trace.h>
Yiwei Zhang42406702020-10-14 08:42:26 -070036#include <vndksupport/linker.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>
42#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070043
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070044#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080045
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080046using namespace android::hardware::configstore;
47using namespace android::hardware::configstore::V1_0;
48
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080049// #define ENABLE_ALLOC_CALLSTACKS 1
50#if ENABLE_ALLOC_CALLSTACKS
51#include <utils/CallStack.h>
52#define ALOGD_CALLSTACK(...) \
53 do { \
54 ALOGD(__VA_ARGS__); \
55 android::CallStack callstack; \
56 callstack.update(); \
57 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
58 } while (false)
59#else
60#define ALOGD_CALLSTACK(...) \
61 do { \
62 } while (false)
63#endif
64
Chia-I Wu9d518162016-03-24 14:55:27 +080065namespace vulkan {
66namespace driver {
67
Chia-I Wu136b8eb2016-03-24 15:01:52 +080068namespace {
69
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080070class Hal {
71 public:
72 static bool Open();
73
74 static const Hal& Get() { return hal_; }
75 static const hwvulkan_device_t& Device() { return *Get().dev_; }
76
Chia-I Wu31938252016-05-23 15:31:02 +080077 int GetDebugReportIndex() const { return debug_report_index_; }
78
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080079 private:
Chia-I Wu31938252016-05-23 15:31:02 +080080 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 Hal(const Hal&) = delete;
82 Hal& operator=(const Hal&) = delete;
83
Chia-I Wu31938252016-05-23 15:31:02 +080084 bool InitDebugReportIndex();
85
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080086 static Hal hal_;
87
88 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080089 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080090};
91
Chia-I Wu4901db72016-03-24 16:38:58 +080092class CreateInfoWrapper {
93 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080094 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080095 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080096 CreateInfoWrapper(VkPhysicalDevice physical_dev,
97 const VkDeviceCreateInfo& create_info,
98 const VkAllocationCallbacks& allocator);
99 ~CreateInfoWrapper();
100
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800101 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600102 void DowngradeApiVersion();
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700103 void UpgradeDeviceCoreApiVersion(uint32_t api_version);
Chia-I Wu4901db72016-03-24 16:38:58 +0800104
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800105 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
106 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800107
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800108 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800109 explicit operator const VkDeviceCreateInfo*() const;
110
111 private:
112 struct ExtensionFilter {
113 VkExtensionProperties* exts;
114 uint32_t ext_count;
115
116 const char** names;
117 uint32_t name_count;
118 };
119
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800120 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800121
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800122 VkResult SanitizeLayers();
123 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800124
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800125 VkResult QueryExtensionCount(uint32_t& count) const;
126 VkResult EnumerateExtensions(uint32_t& count,
127 VkExtensionProperties* props) const;
128 VkResult InitExtensionFilter();
129 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800130
131 const bool is_instance_;
132 const VkAllocationCallbacks& allocator_;
133
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800134 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800135
136 union {
137 VkInstanceCreateInfo instance_info_;
138 VkDeviceCreateInfo dev_info_;
139 };
140
Ian Elliottf3e872d2017-11-02 10:15:13 -0600141 VkApplicationInfo application_info_;
142
Chia-I Wu4901db72016-03-24 16:38:58 +0800143 ExtensionFilter extension_filter_;
144
145 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
146 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
147};
148
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800149Hal Hal::hal_;
150
Jesse Hall53457db2016-12-14 16:54:06 -0800151const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Yiwei Zhang42406702020-10-14 08:42:26 -0700152 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
153 "ro.board.platform"
Jesse Hall53457db2016-12-14 16:54:06 -0800154}};
Yiwei Zhang42406702020-10-14 08:42:26 -0700155constexpr int LIB_DL_FLAGS = RTLD_LOCAL | RTLD_NOW;
Jesse Hall53457db2016-12-14 16:54:06 -0800156
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800157// LoadDriver returns:
158// * 0 when succeed, or
159// * -ENOENT when fail to open binary libraries, or
160// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
161// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700162int LoadDriver(android_namespace_t* library_namespace,
163 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800164 ATRACE_CALL();
165
Jesse Hall53457db2016-12-14 16:54:06 -0800166 void* so = nullptr;
Jesse Hall53457db2016-12-14 16:54:06 -0800167 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Yiwei Zhang42406702020-10-14 08:42:26 -0700168 std::string lib_name = android::base::GetProperty(key, "");
169 if (lib_name.empty())
170 continue;
171
172 lib_name = "vulkan." + lib_name + ".so";
173 if (library_namespace) {
174 // load updated driver
175 const android_dlextinfo dlextinfo = {
176 .flags = ANDROID_DLEXT_USE_NAMESPACE,
177 .library_namespace = library_namespace,
178 };
179 so = android_dlopen_ext(lib_name.c_str(), LIB_DL_FLAGS, &dlextinfo);
180 } else {
181 // load built-in driver
182 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800183 }
Yiwei Zhang42406702020-10-14 08:42:26 -0700184 if (so)
185 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800186 }
Yiwei Zhang42406702020-10-14 08:42:26 -0700187 if (!so) {
Jesse Hall53457db2016-12-14 16:54:06 -0800188 return -ENOENT;
Yiwei Zhang42406702020-10-14 08:42:26 -0700189 }
Jesse Hall53457db2016-12-14 16:54:06 -0800190
Jesse Hall00e61ff2017-04-07 16:48:02 -0700191 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800192 if (!hmi) {
193 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
194 dlclose(so);
195 return -EINVAL;
196 }
197 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
198 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
199 dlclose(so);
200 return -EINVAL;
201 }
202 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700203 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800204 return 0;
205}
206
Jesse Hall00e61ff2017-04-07 16:48:02 -0700207int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800208 ATRACE_CALL();
209
Yiwei Zhangd9861812019-02-13 11:51:55 -0800210 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700211 android::GpuStatsInfo::Driver::VULKAN);
Yiwei Zhang42406702020-10-14 08:42:26 -0700212 return LoadDriver(nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700213}
214
215int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800216 ATRACE_CALL();
217
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
219 if (!ns)
220 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800221 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700222 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800223 int result = LoadDriver(ns, module);
224 if (result != 0) {
225 LOG_ALWAYS_FATAL(
226 "couldn't find an updated Vulkan implementation from %s",
227 android::GraphicsEnv::getInstance().getDriverPath().c_str());
228 }
229 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700230}
231
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800232bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800233 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800234 const nsecs_t openTime = systemTime();
235
Jesse Halldc225072016-05-30 22:40:14 -0700236 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800237
238 // Use a stub device unless we successfully open a real HAL device.
239 hal_.dev_ = &stubhal::kDevice;
240
Jesse Hall53457db2016-12-14 16:54:06 -0800241 int result;
242 const hwvulkan_module_t* module = nullptr;
243
Jesse Hall00e61ff2017-04-07 16:48:02 -0700244 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800245 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700246 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800247 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800248 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800249 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700250 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800251 return true;
252 }
253
254 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800255 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800256 result =
257 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
258 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhang42406702020-10-14 08:42:26 -0700259
260
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800261 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800262 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800263 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700264 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 // Any device with a Vulkan HAL should be able to open the device.
266 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
267 result);
268 return false;
269 }
270
271 hal_.dev_ = device;
272
Chia-I Wu31938252016-05-23 15:31:02 +0800273 hal_.InitDebugReportIndex();
274
Yiwei Zhangd9861812019-02-13 11:51:55 -0800275 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700276 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800277
Chia-I Wu31938252016-05-23 15:31:02 +0800278 return true;
279}
280
281bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800282 ATRACE_CALL();
283
Chia-I Wu31938252016-05-23 15:31:02 +0800284 uint32_t count;
285 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
286 VK_SUCCESS) {
287 ALOGE("failed to get HAL instance extension count");
288 return false;
289 }
290
291 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
292 malloc(sizeof(VkExtensionProperties) * count));
293 if (!exts) {
294 ALOGE("failed to allocate HAL instance extension array");
295 return false;
296 }
297
298 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
299 VK_SUCCESS) {
300 ALOGE("failed to enumerate HAL instance extensions");
301 free(exts);
302 return false;
303 }
304
305 for (uint32_t i = 0; i < count; i++) {
306 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
307 0) {
308 debug_report_index_ = static_cast<int>(i);
309 break;
310 }
311 }
312
313 free(exts);
314
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800315 return true;
316}
317
318CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800319 const VkAllocationCallbacks& allocator)
320 : is_instance_(true),
321 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800322 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800323 instance_info_(create_info),
324 extension_filter_() {
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700325 // instance core versions need to match the loader api version
326 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
327 i != ProcHook::EXTENSION_COUNT; ++i) {
328 hook_extensions_.set(i);
329 hal_extensions_.set(i);
330 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800331}
332
Chia-I Wu4901db72016-03-24 16:38:58 +0800333CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
334 const VkDeviceCreateInfo& create_info,
335 const VkAllocationCallbacks& allocator)
336 : is_instance_(false),
337 allocator_(allocator),
338 physical_dev_(physical_dev),
339 dev_info_(create_info),
340 extension_filter_() {
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700341 // initialize with baseline core API version
342 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
343 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
Chia-I Wu4901db72016-03-24 16:38:58 +0800344}
345
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() {
352 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800353 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800354 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800355 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800356 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800357
358 return result;
359}
360
361const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800362CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800363 return hook_extensions_;
364}
365
366const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800367CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800368 return hal_extensions_;
369}
370
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800371CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
372 return &instance_info_;
373}
374
Chia-I Wu4901db72016-03-24 16:38:58 +0800375CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
376 return &dev_info_;
377}
378
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800379VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800380 const struct StructHeader {
381 VkStructureType type;
382 const void* next;
383 } * header;
384
385 if (is_instance_) {
386 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
387
388 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
389 while (header &&
390 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
391 header = reinterpret_cast<const StructHeader*>(header->next);
392
393 instance_info_.pNext = header;
394 } else {
395 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
396
397 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
398 while (header &&
399 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
400 header = reinterpret_cast<const StructHeader*>(header->next);
401
402 dev_info_.pNext = header;
403 }
404
405 return VK_SUCCESS;
406}
407
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800408VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800409 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
410 : dev_info_.ppEnabledLayerNames;
411 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
412 : dev_info_.enabledLayerCount;
413
414 // remove all layers
415 layer_names = nullptr;
416 layer_count = 0;
417
418 return VK_SUCCESS;
419}
420
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800421VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800422 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
423 : dev_info_.ppEnabledExtensionNames;
424 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
425 : dev_info_.enabledExtensionCount;
426 if (!ext_count)
427 return VK_SUCCESS;
428
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800429 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800430 if (result != VK_SUCCESS)
431 return result;
432
433 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800434 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800435
Jesse Halld3d887a2018-03-05 13:34:45 -0800436 // Enable device extensions that contain physical-device commands, so that
437 // vkGetInstanceProcAddr will return those physical-device commands.
438 if (is_instance_) {
439 hook_extensions_.set(ProcHook::KHR_swapchain);
440 }
441
Chia-I Wu4901db72016-03-24 16:38:58 +0800442 ext_names = extension_filter_.names;
443 ext_count = extension_filter_.name_count;
444
445 return VK_SUCCESS;
446}
447
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800448VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800449 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800450 return Hal::Device().EnumerateInstanceExtensionProperties(
451 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800452 } else {
453 const auto& driver = GetData(physical_dev_).driver;
454 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
455 &count, nullptr);
456 }
457}
458
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800459VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800460 uint32_t& count,
461 VkExtensionProperties* props) const {
462 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800463 return Hal::Device().EnumerateInstanceExtensionProperties(
464 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800465 } else {
466 const auto& driver = GetData(physical_dev_).driver;
467 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
468 &count, props);
469 }
470}
471
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800472VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800473 // query extension count
474 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800475 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800476 if (result != VK_SUCCESS || count == 0)
477 return result;
478
479 auto& filter = extension_filter_;
480 filter.exts =
481 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
482 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
483 alignof(VkExtensionProperties),
484 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
485 if (!filter.exts)
486 return VK_ERROR_OUT_OF_HOST_MEMORY;
487
488 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800489 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800490 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
491 return result;
492
493 if (!count)
494 return VK_SUCCESS;
495
496 filter.ext_count = count;
497
498 // allocate name array
499 uint32_t enabled_ext_count = (is_instance_)
500 ? instance_info_.enabledExtensionCount
501 : dev_info_.enabledExtensionCount;
502 count = std::min(filter.ext_count, enabled_ext_count);
503 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
504 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
505 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
506 if (!filter.names)
507 return VK_ERROR_OUT_OF_HOST_MEMORY;
508
509 return VK_SUCCESS;
510}
511
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800512void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800513 auto& filter = extension_filter_;
514
515 ProcHook::Extension ext_bit = GetProcHookExtension(name);
516 if (is_instance_) {
517 switch (ext_bit) {
518 case ProcHook::KHR_android_surface:
519 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700520 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300521 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800522 hook_extensions_.set(ext_bit);
523 // return now as these extensions do not require HAL support
524 return;
525 case ProcHook::EXT_debug_report:
526 // both we and HAL can take part in
527 hook_extensions_.set(ext_bit);
528 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300529 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700530 case ProcHook::EXTENSION_UNKNOWN:
531 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800532 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700533
Yiwei Zhang23143102019-04-10 18:24:05 -0700534 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700535 case ProcHook::KHR_incremental_present:
536 case ProcHook::KHR_shared_presentable_image:
537 case ProcHook::KHR_swapchain:
538 case ProcHook::EXT_hdr_metadata:
539 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
540 case ProcHook::ANDROID_native_buffer:
541 case ProcHook::GOOGLE_display_timing:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700542 case ProcHook::EXTENSION_CORE_1_0:
543 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700544 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700545 case ProcHook::EXTENSION_COUNT:
546 // Device and meta extensions. If we ever get here it's a bug in
547 // our code. But enumerating them lets us avoid having a default
548 // case, and default hides other bugs.
549 ALOGE(
550 "CreateInfoWrapper::FilterExtension: invalid instance "
551 "extension '%s'. FIX ME",
552 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800553 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700554
555 // Don't use a default case. Without it, -Wswitch will tell us
556 // at compile time if someone adds a new ProcHook extension but
557 // doesn't handle it above. That's a real bug that has
558 // not-immediately-obvious effects.
559 //
560 // default:
561 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800562 }
563 } else {
564 switch (ext_bit) {
565 case ProcHook::KHR_swapchain:
566 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
567 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
568 ext_bit = ProcHook::ANDROID_native_buffer;
569 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700570 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700571 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300572 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700573 hook_extensions_.set(ext_bit);
574 // return now as these extensions do not require HAL support
575 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700576 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700577 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700578 hook_extensions_.set(ext_bit);
579 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700580 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800581 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700582 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800583 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700584
585 case ProcHook::KHR_android_surface:
586 case ProcHook::KHR_get_physical_device_properties2:
587 case ProcHook::KHR_get_surface_capabilities2:
588 case ProcHook::KHR_surface:
589 case ProcHook::EXT_debug_report:
590 case ProcHook::EXT_swapchain_colorspace:
591 case ProcHook::ANDROID_native_buffer:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700592 case ProcHook::EXTENSION_CORE_1_0:
593 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700594 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700595 case ProcHook::EXTENSION_COUNT:
596 // Instance and meta extensions. If we ever get here it's a bug
597 // in our code. But enumerating them lets us avoid having a
598 // default case, and default hides other bugs.
599 ALOGE(
600 "CreateInfoWrapper::FilterExtension: invalid device "
601 "extension '%s'. FIX ME",
602 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800603 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700604
605 // Don't use a default case. Without it, -Wswitch will tell us
606 // at compile time if someone adds a new ProcHook extension but
607 // doesn't handle it above. That's a real bug that has
608 // not-immediately-obvious effects.
609 //
610 // default:
611 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800612 }
613 }
614
615 for (uint32_t i = 0; i < filter.ext_count; i++) {
616 const VkExtensionProperties& props = filter.exts[i];
617 // ignore unknown extensions
618 if (strcmp(name, props.extensionName) != 0)
619 continue;
620
Chia-I Wu4901db72016-03-24 16:38:58 +0800621 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800622 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
623 if (ext_bit == ProcHook::ANDROID_native_buffer)
624 hook_extensions_.set(ProcHook::KHR_swapchain);
625
626 hal_extensions_.set(ext_bit);
627 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800628
629 break;
630 }
631}
632
Ian Elliottf3e872d2017-11-02 10:15:13 -0600633void CreateInfoWrapper::DowngradeApiVersion() {
634 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
635 if (instance_info_.pApplicationInfo) {
636 application_info_ = *instance_info_.pApplicationInfo;
637 instance_info_.pApplicationInfo = &application_info_;
638 application_info_.apiVersion = VK_API_VERSION_1_0;
639 }
640}
641
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700642void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
643 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700644
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700645#pragma clang diagnostic push
646#pragma clang diagnostic ignored "-Wold-style-cast"
647 api_version ^= VK_VERSION_PATCH(api_version);
648#pragma clang diagnostic pop
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700649
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700650 // cap the API version to the loader supported highest version
651 if (api_version > VK_API_VERSION_1_1)
652 api_version = VK_API_VERSION_1_1;
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700653
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700654 switch (api_version) {
655 case VK_API_VERSION_1_1:
656 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
657 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
658 [[clang::fallthrough]];
659 case VK_API_VERSION_1_0:
660 break;
661 default:
662 ALOGD("Unknown upgrade API version[%u]", api_version);
663 break;
664 }
665}
666
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800667VKAPI_ATTR void* DefaultAllocate(void*,
668 size_t size,
669 size_t alignment,
670 VkSystemAllocationScope) {
671 void* ptr = nullptr;
672 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
673 // additionally requires that it be at least sizeof(void*).
674 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
675 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
676 ret, ptr);
677 return ret == 0 ? ptr : nullptr;
678}
679
680VKAPI_ATTR void* DefaultReallocate(void*,
681 void* ptr,
682 size_t size,
683 size_t alignment,
684 VkSystemAllocationScope) {
685 if (size == 0) {
686 free(ptr);
687 return nullptr;
688 }
689
Yiwei Zhanga885c062019-10-24 12:07:57 -0700690 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800691 // request is smaller than the existing chunk, we just continue using it.
692 // Right now the loader never reallocs, so this doesn't matter. If that
693 // changes, or if this code is copied into some other project, this should
694 // probably have a heuristic to allocate-copy-free when doing so will save
695 // "enough" space.
696 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
697 if (size <= old_size)
698 return ptr;
699
700 void* new_ptr = nullptr;
701 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
702 return nullptr;
703 if (ptr) {
704 memcpy(new_ptr, ptr, std::min(old_size, size));
705 free(ptr);
706 }
707 return new_ptr;
708}
709
710VKAPI_ATTR void DefaultFree(void*, void* ptr) {
711 ALOGD_CALLSTACK("Free: %p", ptr);
712 free(ptr);
713}
714
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800715InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
716 void* data_mem = allocator.pfnAllocation(
717 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
718 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
719 if (!data_mem)
720 return nullptr;
721
722 return new (data_mem) InstanceData(allocator);
723}
724
725void FreeInstanceData(InstanceData* data,
726 const VkAllocationCallbacks& allocator) {
727 data->~InstanceData();
728 allocator.pfnFree(allocator.pUserData, data);
729}
730
Chia-I Wu950d6e12016-05-03 09:12:35 +0800731DeviceData* AllocateDeviceData(
732 const VkAllocationCallbacks& allocator,
733 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800734 void* data_mem = allocator.pfnAllocation(
735 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
736 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
737 if (!data_mem)
738 return nullptr;
739
Chia-I Wu950d6e12016-05-03 09:12:35 +0800740 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800741}
742
743void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
744 data->~DeviceData();
745 allocator.pfnFree(allocator.pUserData, data);
746}
747
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800748} // anonymous namespace
749
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800750bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800751 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800752}
753
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800754const VkAllocationCallbacks& GetDefaultAllocator() {
755 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
756 .pUserData = nullptr,
757 .pfnAllocation = DefaultAllocate,
758 .pfnReallocation = DefaultReallocate,
759 .pfnFree = DefaultFree,
760 };
761
762 return kDefaultAllocCallbacks;
763}
764
Chia-I Wueb7db122016-03-24 09:11:06 +0800765PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
766 const ProcHook* hook = GetProcHook(pName);
767 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800768 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800769
770 if (!instance) {
771 if (hook->type == ProcHook::GLOBAL)
772 return hook->proc;
773
Chia-I Wu109f8982016-04-22 06:40:40 +0800774 // v0 layers expect
775 //
776 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
777 //
778 // to work.
779 if (strcmp(pName, "vkCreateDevice") == 0)
780 return hook->proc;
781
Chia-I Wueb7db122016-03-24 09:11:06 +0800782 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800783 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800784 pName);
785
Chia-I Wu109f8982016-04-22 06:40:40 +0800786 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800787 }
788
789 PFN_vkVoidFunction proc;
790
791 switch (hook->type) {
792 case ProcHook::INSTANCE:
793 proc = (GetData(instance).hook_extensions[hook->extension])
794 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800795 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800796 break;
797 case ProcHook::DEVICE:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700798 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800799 ? hook->proc
800 : hook->checked_proc;
801 break;
802 default:
803 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800804 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800805 pName);
806 proc = nullptr;
807 break;
808 }
809
810 return proc;
811}
812
813PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
814 const ProcHook* hook = GetProcHook(pName);
815 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800816 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800817
818 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800819 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800820 return nullptr;
821 }
822
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800823 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
824 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800825}
826
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800827VkResult EnumerateInstanceExtensionProperties(
828 const char* pLayerName,
829 uint32_t* pPropertyCount,
830 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700831 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600832 loader_extensions.push_back({
833 VK_KHR_SURFACE_EXTENSION_NAME,
834 VK_KHR_SURFACE_SPEC_VERSION});
835 loader_extensions.push_back({
836 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
837 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
838 loader_extensions.push_back({
839 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
840 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700841 loader_extensions.push_back({
842 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
843 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600844
Chia-I Wu31938252016-05-23 15:31:02 +0800845 static const VkExtensionProperties loader_debug_report_extension = {
846 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
847 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800848
849 // enumerate our extensions first
850 if (!pLayerName && pProperties) {
851 uint32_t count = std::min(
852 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
853
Yiwei Zhang5e862202019-06-21 14:59:16 -0700854 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800855
856 if (count < loader_extensions.size()) {
857 *pPropertyCount = count;
858 return VK_INCOMPLETE;
859 }
860
861 pProperties += count;
862 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800863
864 if (Hal::Get().GetDebugReportIndex() < 0) {
865 if (!*pPropertyCount) {
866 *pPropertyCount = count;
867 return VK_INCOMPLETE;
868 }
869
870 pProperties[0] = loader_debug_report_extension;
871 pProperties += 1;
872 *pPropertyCount -= 1;
873 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800874 }
875
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800876 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800877 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800878 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800879 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800880
Chia-I Wu31938252016-05-23 15:31:02 +0800881 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
882 int idx = Hal::Get().GetDebugReportIndex();
883 if (idx < 0) {
884 *pPropertyCount += 1;
885 } else if (pProperties &&
886 static_cast<uint32_t>(idx) < *pPropertyCount) {
887 pProperties[idx].specVersion =
888 std::min(pProperties[idx].specVersion,
889 loader_debug_report_extension.specVersion);
890 }
891
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800892 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800893 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800894
895 return result;
896}
897
Chris Forbesfa25e632017-02-22 12:36:02 +1300898bool QueryPresentationProperties(
899 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700900 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300901 const InstanceData& data = GetData(physicalDevice);
902
903 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700904 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
905 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300906 return false;
907
908 // Request the android-specific presentation properties via GPDP2
909 VkPhysicalDeviceProperties2KHR properties = {
910 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
911 presentation_properties,
912 {}
913 };
914
915#pragma clang diagnostic push
916#pragma clang diagnostic ignored "-Wold-style-cast"
917 presentation_properties->sType =
918 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
919#pragma clang diagnostic pop
920 presentation_properties->pNext = nullptr;
921 presentation_properties->sharedImage = VK_FALSE;
922
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700923 if (data.driver.GetPhysicalDeviceProperties2KHR) {
924 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
925 &properties);
926 } else {
927 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
928 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300929
930 return true;
931}
932
Chia-I Wu01cf3052016-03-24 16:16:21 +0800933VkResult EnumerateDeviceExtensionProperties(
934 VkPhysicalDevice physicalDevice,
935 const char* pLayerName,
936 uint32_t* pPropertyCount,
937 VkExtensionProperties* pProperties) {
938 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300939 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700940 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300941 loader_extensions.push_back({
942 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
943 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300944
Sundong Ahnbc37dd52020-04-23 21:21:00 +0900945 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800946 if (hdrBoardConfig) {
947 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
948 VK_EXT_HDR_METADATA_SPEC_VERSION});
949 }
950
Chris Forbes16095002017-05-05 15:33:29 -0700951 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
952 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
953 presentation_properties.sharedImage) {
954 loader_extensions.push_back({
955 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
956 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300957 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700958
Ian Elliott5c34de22017-04-10 14:42:30 -0600959 // conditionally add VK_GOOGLE_display_timing if present timestamps are
960 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -0700961 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600962 loader_extensions.push_back({
963 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
964 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
965 }
966
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700967 // enumerate our extensions first
968 if (!pLayerName && pProperties) {
969 uint32_t count = std::min(
970 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
971
Yiwei Zhang5e862202019-06-21 14:59:16 -0700972 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700973
974 if (count < loader_extensions.size()) {
975 *pPropertyCount = count;
976 return VK_INCOMPLETE;
977 }
978
979 pProperties += count;
980 *pPropertyCount -= count;
981 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800982
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800983 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800984 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
985 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800986 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800987
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700988 if (pProperties) {
989 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
990 for (uint32_t i = 0; i < *pPropertyCount; i++) {
991 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800992
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700993 if (strcmp(prop.extensionName,
994 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
995 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +0800996
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700997 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
998 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -0700999
1000 if (prop.specVersion >= 8) {
1001 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1002 } else {
1003 prop.specVersion = 68;
1004 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001005 }
1006 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001007
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001008 // restore loader extension count
1009 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1010 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001011 }
1012
1013 return result;
1014}
1015
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001016VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1017 const VkAllocationCallbacks* pAllocator,
1018 VkInstance* pInstance) {
1019 const VkAllocationCallbacks& data_allocator =
1020 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1021
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001022 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001023 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001024 if (result != VK_SUCCESS)
1025 return result;
1026
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001027 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001028 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001029 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001030 if (!data)
1031 return VK_ERROR_OUT_OF_HOST_MEMORY;
1032
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001033 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001034
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001035 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001036#pragma clang diagnostic push
1037#pragma clang diagnostic ignored "-Wold-style-cast"
1038 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1039 ? pCreateInfo->pApplicationInfo->apiVersion
1040 : VK_API_VERSION_1_0);
1041 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1042 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1043 uint32_t icd_api_version;
1044 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1045 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001046 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001047 "vkEnumerateInstanceVersion"));
1048 if (!pfn_enumerate_instance_version) {
1049 icd_api_version = VK_API_VERSION_1_0;
1050 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001051 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001052 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001053 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001054 }
1055 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1056 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1057
1058 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1059 ((api_major_version > 1) || (api_minor_version > 0))) {
1060 api_version = VK_API_VERSION_1_0;
1061 wrapper.DowngradeApiVersion();
1062 }
1063#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001064 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001065
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001066 // call into the driver
1067 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001068 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001069 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001070 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1071 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001072 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001073 if (result != VK_SUCCESS) {
1074 FreeInstanceData(data, data_allocator);
1075 return result;
1076 }
1077
1078 // initialize InstanceDriverTable
1079 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001080 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001081 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001082 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001083 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001084 if (data->driver.DestroyInstance)
1085 data->driver.DestroyInstance(instance, pAllocator);
1086
1087 FreeInstanceData(data, data_allocator);
1088
1089 return VK_ERROR_INCOMPATIBLE_DRIVER;
1090 }
1091
1092 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001093 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001094 if (!data->get_device_proc_addr) {
1095 data->driver.DestroyInstance(instance, pAllocator);
1096 FreeInstanceData(data, data_allocator);
1097
1098 return VK_ERROR_INCOMPATIBLE_DRIVER;
1099 }
1100
1101 *pInstance = instance;
1102
1103 return VK_SUCCESS;
1104}
1105
1106void DestroyInstance(VkInstance instance,
1107 const VkAllocationCallbacks* pAllocator) {
1108 InstanceData& data = GetData(instance);
1109 data.driver.DestroyInstance(instance, pAllocator);
1110
1111 VkAllocationCallbacks local_allocator;
1112 if (!pAllocator) {
1113 local_allocator = data.allocator;
1114 pAllocator = &local_allocator;
1115 }
1116
1117 FreeInstanceData(&data, *pAllocator);
1118}
1119
Chia-I Wu4901db72016-03-24 16:38:58 +08001120VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1121 const VkDeviceCreateInfo* pCreateInfo,
1122 const VkAllocationCallbacks* pAllocator,
1123 VkDevice* pDevice) {
1124 const InstanceData& instance_data = GetData(physicalDevice);
1125 const VkAllocationCallbacks& data_allocator =
1126 (pAllocator) ? *pAllocator : instance_data.allocator;
1127
1128 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001129 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001130 if (result != VK_SUCCESS)
1131 return result;
1132
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001133 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001134 DeviceData* data = AllocateDeviceData(data_allocator,
1135 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001136 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001137 if (!data)
1138 return VK_ERROR_OUT_OF_HOST_MEMORY;
1139
Yiwei Zhangec6c5052019-10-17 15:53:00 -07001140 VkPhysicalDeviceProperties properties;
1141 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1142 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1143 &properties);
1144 ATRACE_END();
1145
1146 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001147 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001148
1149 // call into the driver
1150 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001151 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001152 result = instance_data.driver.CreateDevice(
1153 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1154 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001155 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001156 if (result != VK_SUCCESS) {
1157 FreeDeviceData(data, data_allocator);
1158 return result;
1159 }
1160
1161 // initialize DeviceDriverTable
1162 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001163 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1164 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001165 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1166 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1167 if (data->driver.DestroyDevice)
1168 data->driver.DestroyDevice(dev, pAllocator);
1169
1170 FreeDeviceData(data, data_allocator);
1171
1172 return VK_ERROR_INCOMPATIBLE_DRIVER;
1173 }
Chris Forbesd8277912017-02-10 14:59:59 +13001174
1175 // sanity check ANDROID_native_buffer implementation, whose set of
1176 // entrypoints varies according to the spec version.
1177 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1178 !data->driver.GetSwapchainGrallocUsageANDROID &&
1179 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1180 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1181 " must expose at least one of "
1182 "vkGetSwapchainGrallocUsageANDROID or "
1183 "vkGetSwapchainGrallocUsage2ANDROID");
1184
1185 data->driver.DestroyDevice(dev, pAllocator);
1186 FreeDeviceData(data, data_allocator);
1187
1188 return VK_ERROR_INCOMPATIBLE_DRIVER;
1189 }
1190
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001191 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1192 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangb1c1a372019-07-03 13:39:32 -07001193 android::GraphicsEnv::getInstance().setTargetStats(
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001194 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001195 }
1196
Jesse Halldc225072016-05-30 22:40:14 -07001197 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001198
1199 *pDevice = dev;
1200
1201 return VK_SUCCESS;
1202}
1203
1204void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1205 DeviceData& data = GetData(device);
1206 data.driver.DestroyDevice(device, pAllocator);
1207
1208 VkAllocationCallbacks local_allocator;
1209 if (!pAllocator) {
1210 local_allocator = data.allocator;
1211 pAllocator = &local_allocator;
1212 }
1213
1214 FreeDeviceData(&data, *pAllocator);
1215}
1216
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001217VkResult EnumeratePhysicalDevices(VkInstance instance,
1218 uint32_t* pPhysicalDeviceCount,
1219 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001220 ATRACE_CALL();
1221
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001222 const auto& data = GetData(instance);
1223
1224 VkResult result = data.driver.EnumeratePhysicalDevices(
1225 instance, pPhysicalDeviceCount, pPhysicalDevices);
1226 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1227 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1228 SetData(pPhysicalDevices[i], data);
1229 }
1230
1231 return result;
1232}
1233
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001234VkResult EnumeratePhysicalDeviceGroups(
1235 VkInstance instance,
1236 uint32_t* pPhysicalDeviceGroupCount,
1237 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001238 ATRACE_CALL();
1239
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001240 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001241 const auto& data = GetData(instance);
1242
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001243 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1244 uint32_t device_count = 0;
1245 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1246 if (result < 0)
1247 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001248
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001249 if (!pPhysicalDeviceGroupProperties) {
1250 *pPhysicalDeviceGroupCount = device_count;
1251 return result;
1252 }
1253
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001254 if (!device_count) {
1255 *pPhysicalDeviceGroupCount = 0;
1256 return result;
1257 }
Chad Versace32c087f2018-09-09 07:28:05 -07001258 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1259 if (!device_count)
1260 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001261
Yiwei Zhang5e862202019-06-21 14:59:16 -07001262 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001263 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001264 result =
1265 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001266 if (result < 0)
1267 return result;
1268
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001269 for (uint32_t i = 0; i < device_count; ++i) {
1270 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1271 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1272 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1273 }
1274 } else {
1275 result = data.driver.EnumeratePhysicalDeviceGroups(
1276 instance, pPhysicalDeviceGroupCount,
1277 pPhysicalDeviceGroupProperties);
1278 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1279 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1280 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1281 for (uint32_t j = 0;
1282 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1283 j++) {
1284 SetData(
1285 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001286 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001287 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001288 }
1289 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001290 }
1291
1292 return result;
1293}
1294
Chia-I Wuba0be412016-03-24 16:24:40 +08001295void GetDeviceQueue(VkDevice device,
1296 uint32_t queueFamilyIndex,
1297 uint32_t queueIndex,
1298 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001299 ATRACE_CALL();
1300
Chia-I Wuba0be412016-03-24 16:24:40 +08001301 const auto& data = GetData(device);
1302
1303 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1304 SetData(*pQueue, data);
1305}
1306
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001307void GetDeviceQueue2(VkDevice device,
1308 const VkDeviceQueueInfo2* pQueueInfo,
1309 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001310 ATRACE_CALL();
1311
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001312 const auto& data = GetData(device);
1313
1314 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001315 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001316}
1317
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001318VKAPI_ATTR VkResult
1319AllocateCommandBuffers(VkDevice device,
1320 const VkCommandBufferAllocateInfo* pAllocateInfo,
1321 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001322 ATRACE_CALL();
1323
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001324 const auto& data = GetData(device);
1325
1326 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1327 pCommandBuffers);
1328 if (result == VK_SUCCESS) {
1329 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1330 SetData(pCommandBuffers[i], data);
1331 }
1332
1333 return result;
1334}
1335
Yiwei Zhang899d1752019-09-23 16:05:35 -07001336VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1337 uint32_t submitCount,
1338 const VkSubmitInfo* pSubmits,
1339 VkFence fence) {
1340 ATRACE_CALL();
1341
1342 const auto& data = GetData(queue);
1343
1344 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1345}
1346
Chia-I Wu9d518162016-03-24 14:55:27 +08001347} // namespace driver
1348} // namespace vulkan