blob: 01cbf399ff15ac91ad80e8321a966061cdb19fcf [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>
Jesse Hall53457db2016-12-14 16:54:06 -080031#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090032#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070033#include <log/log.h>
Yiwei Zhange40dd732019-08-05 16:41:03 -070034#include <nativeloader/dlext_namespaces.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070035#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080036#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080037#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080038
Yiwei Zhang5e862202019-06-21 14:59:16 -070039#include <algorithm>
40#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070041#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070042#include <new>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070043#include <string_view>
44#include <sstream>
Yiwei Zhang5e862202019-06-21 14:59:16 -070045#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070046
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070047#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080048
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080049using namespace android::hardware::configstore;
50using namespace android::hardware::configstore::V1_0;
51
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080052// #define ENABLE_ALLOC_CALLSTACKS 1
53#if ENABLE_ALLOC_CALLSTACKS
54#include <utils/CallStack.h>
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 ALOGD(__VA_ARGS__); \
58 android::CallStack callstack; \
59 callstack.update(); \
60 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
61 } while (false)
62#else
63#define ALOGD_CALLSTACK(...) \
64 do { \
65 } while (false)
66#endif
67
Chia-I Wu9d518162016-03-24 14:55:27 +080068namespace vulkan {
69namespace driver {
70
Chia-I Wu136b8eb2016-03-24 15:01:52 +080071namespace {
72
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080073class Hal {
74 public:
75 static bool Open();
76
77 static const Hal& Get() { return hal_; }
78 static const hwvulkan_device_t& Device() { return *Get().dev_; }
79
Chia-I Wu31938252016-05-23 15:31:02 +080080 int GetDebugReportIndex() const { return debug_report_index_; }
81
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080082 private:
Chia-I Wu31938252016-05-23 15:31:02 +080083 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080084 Hal(const Hal&) = delete;
85 Hal& operator=(const Hal&) = delete;
86
Chia-I Wu31938252016-05-23 15:31:02 +080087 bool InitDebugReportIndex();
88
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 static Hal hal_;
90
91 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080092 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093};
94
Chia-I Wu4901db72016-03-24 16:38:58 +080095class CreateInfoWrapper {
96 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080097 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080098 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080099 CreateInfoWrapper(VkPhysicalDevice physical_dev,
100 const VkDeviceCreateInfo& create_info,
101 const VkAllocationCallbacks& allocator);
102 ~CreateInfoWrapper();
103
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800104 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600105 void DowngradeApiVersion();
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700106 void UpgradeDeviceCoreApiVersion(uint32_t api_version);
Chia-I Wu4901db72016-03-24 16:38:58 +0800107
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
109 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800110
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800111 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800112 explicit operator const VkDeviceCreateInfo*() const;
113
114 private:
115 struct ExtensionFilter {
116 VkExtensionProperties* exts;
117 uint32_t ext_count;
118
119 const char** names;
120 uint32_t name_count;
121 };
122
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800123 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800124
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800125 VkResult SanitizeLayers();
126 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800127
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800128 VkResult QueryExtensionCount(uint32_t& count) const;
129 VkResult EnumerateExtensions(uint32_t& count,
130 VkExtensionProperties* props) const;
131 VkResult InitExtensionFilter();
132 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800133
134 const bool is_instance_;
135 const VkAllocationCallbacks& allocator_;
136
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800137 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800138
139 union {
140 VkInstanceCreateInfo instance_info_;
141 VkDeviceCreateInfo dev_info_;
142 };
143
Ian Elliottf3e872d2017-11-02 10:15:13 -0600144 VkApplicationInfo application_info_;
145
Chia-I Wu4901db72016-03-24 16:38:58 +0800146 ExtensionFilter extension_filter_;
147
148 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
149 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
150};
151
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800152Hal Hal::hal_;
153
Jesse Hall53457db2016-12-14 16:54:06 -0800154void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniersa7044712019-10-21 16:36:19 -0700155 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800156 ATRACE_CALL();
157
Nick Desaulniersa7044712019-10-21 16:36:19 -0700158 std::stringstream ss;
159 ss << "vulkan." << subname << ".so";
160 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800161}
162
163const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700164 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800165 "ro.board.platform",
166}};
167
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800168// LoadDriver returns:
169// * 0 when succeed, or
170// * -ENOENT when fail to open binary libraries, or
171// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
172// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700173int LoadDriver(android_namespace_t* library_namespace,
174 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800175 ATRACE_CALL();
176
Jesse Hall53457db2016-12-14 16:54:06 -0800177 const android_dlextinfo dlextinfo = {
178 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800180 };
Jesse Hall53457db2016-12-14 16:54:06 -0800181 void* so = nullptr;
182 char prop[PROPERTY_VALUE_MAX];
183 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
184 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniersa7044712019-10-21 16:36:19 -0700185 if (prop_len > 0 && prop_len <= UINT_MAX) {
186 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
187 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800188 if (so)
189 break;
190 }
191 }
192 if (!so)
193 return -ENOENT;
194
Jesse Hall00e61ff2017-04-07 16:48:02 -0700195 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800196 if (!hmi) {
197 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
198 dlclose(so);
199 return -EINVAL;
200 }
201 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
202 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
203 dlclose(so);
204 return -EINVAL;
205 }
206 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700207 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800208 return 0;
209}
210
Jesse Hall00e61ff2017-04-07 16:48:02 -0700211int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800212 ATRACE_CALL();
213
Jesse Hall00e61ff2017-04-07 16:48:02 -0700214 auto ns = android_get_exported_namespace("sphal");
215 if (!ns)
216 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700218 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700219 return LoadDriver(ns, module);
220}
221
222int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800223 ATRACE_CALL();
224
Jesse Hall00e61ff2017-04-07 16:48:02 -0700225 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
226 if (!ns)
227 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700229 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800230 int result = LoadDriver(ns, module);
231 if (result != 0) {
232 LOG_ALWAYS_FATAL(
233 "couldn't find an updated Vulkan implementation from %s",
234 android::GraphicsEnv::getInstance().getDriverPath().c_str());
235 }
236 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700237}
238
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800239bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800240 ATRACE_CALL();
241
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 const nsecs_t openTime = systemTime();
243
Jesse Halldc225072016-05-30 22:40:14 -0700244 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800245
246 // Use a stub device unless we successfully open a real HAL device.
247 hal_.dev_ = &stubhal::kDevice;
248
Jesse Hall53457db2016-12-14 16:54:06 -0800249 int result;
250 const hwvulkan_module_t* module = nullptr;
251
Jesse Hall00e61ff2017-04-07 16:48:02 -0700252 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800253 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700254 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800255 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800256 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800257 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700258 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800259 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800260 return true;
261 }
262
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800263
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800264 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800265 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800266 result =
267 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
268 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800269 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800270 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800271 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700272 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800273 // Any device with a Vulkan HAL should be able to open the device.
274 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
275 result);
276 return false;
277 }
278
279 hal_.dev_ = device;
280
Chia-I Wu31938252016-05-23 15:31:02 +0800281 hal_.InitDebugReportIndex();
282
Yiwei Zhangd9861812019-02-13 11:51:55 -0800283 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700284 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800285
Chia-I Wu31938252016-05-23 15:31:02 +0800286 return true;
287}
288
289bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800290 ATRACE_CALL();
291
Chia-I Wu31938252016-05-23 15:31:02 +0800292 uint32_t count;
293 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
294 VK_SUCCESS) {
295 ALOGE("failed to get HAL instance extension count");
296 return false;
297 }
298
299 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
300 malloc(sizeof(VkExtensionProperties) * count));
301 if (!exts) {
302 ALOGE("failed to allocate HAL instance extension array");
303 return false;
304 }
305
306 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
307 VK_SUCCESS) {
308 ALOGE("failed to enumerate HAL instance extensions");
309 free(exts);
310 return false;
311 }
312
313 for (uint32_t i = 0; i < count; i++) {
314 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
315 0) {
316 debug_report_index_ = static_cast<int>(i);
317 break;
318 }
319 }
320
321 free(exts);
322
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800323 return true;
324}
325
326CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800327 const VkAllocationCallbacks& allocator)
328 : is_instance_(true),
329 allocator_(allocator),
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 Zhangec6c5052019-10-17 15:53:00 -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 Zhangec6c5052019-10-17 15:53:00 -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 Zhangec6c5052019-10-17 15:53:00 -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 Zhangec6c5052019-10-17 15:53:00 -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 Zhangec6c5052019-10-17 15:53:00 -0700648void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
649 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700650
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700651#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
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700655
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700656 // 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;
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700659
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700660 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
Yiwei Zhanga885c062019-10-24 12:07:57 -0700696 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800697 // 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 Wu136b8eb2016-03-24 15:01:52 +0800756bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800757 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800758}
759
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800760const VkAllocationCallbacks& GetDefaultAllocator() {
761 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
762 .pUserData = nullptr,
763 .pfnAllocation = DefaultAllocate,
764 .pfnReallocation = DefaultReallocate,
765 .pfnFree = DefaultFree,
766 };
767
768 return kDefaultAllocCallbacks;
769}
770
Chia-I Wueb7db122016-03-24 09:11:06 +0800771PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
772 const ProcHook* hook = GetProcHook(pName);
773 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800774 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800775
776 if (!instance) {
777 if (hook->type == ProcHook::GLOBAL)
778 return hook->proc;
779
Chia-I Wu109f8982016-04-22 06:40:40 +0800780 // v0 layers expect
781 //
782 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
783 //
784 // to work.
785 if (strcmp(pName, "vkCreateDevice") == 0)
786 return hook->proc;
787
Chia-I Wueb7db122016-03-24 09:11:06 +0800788 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800789 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800790 pName);
791
Chia-I Wu109f8982016-04-22 06:40:40 +0800792 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800793 }
794
795 PFN_vkVoidFunction proc;
796
797 switch (hook->type) {
798 case ProcHook::INSTANCE:
799 proc = (GetData(instance).hook_extensions[hook->extension])
800 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800801 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800802 break;
803 case ProcHook::DEVICE:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700804 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800805 ? hook->proc
806 : hook->checked_proc;
807 break;
808 default:
809 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800810 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800811 pName);
812 proc = nullptr;
813 break;
814 }
815
816 return proc;
817}
818
819PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
820 const ProcHook* hook = GetProcHook(pName);
821 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800822 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800823
824 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800825 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800826 return nullptr;
827 }
828
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800829 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
830 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800831}
832
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800833VkResult EnumerateInstanceExtensionProperties(
834 const char* pLayerName,
835 uint32_t* pPropertyCount,
836 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700837 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600838 loader_extensions.push_back({
839 VK_KHR_SURFACE_EXTENSION_NAME,
840 VK_KHR_SURFACE_SPEC_VERSION});
841 loader_extensions.push_back({
842 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
843 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
844 loader_extensions.push_back({
845 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
846 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700847 loader_extensions.push_back({
848 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
849 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600850
Chia-I Wu31938252016-05-23 15:31:02 +0800851 static const VkExtensionProperties loader_debug_report_extension = {
852 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
853 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800854
855 // enumerate our extensions first
856 if (!pLayerName && pProperties) {
857 uint32_t count = std::min(
858 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
859
Yiwei Zhang5e862202019-06-21 14:59:16 -0700860 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800861
862 if (count < loader_extensions.size()) {
863 *pPropertyCount = count;
864 return VK_INCOMPLETE;
865 }
866
867 pProperties += count;
868 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800869
870 if (Hal::Get().GetDebugReportIndex() < 0) {
871 if (!*pPropertyCount) {
872 *pPropertyCount = count;
873 return VK_INCOMPLETE;
874 }
875
876 pProperties[0] = loader_debug_report_extension;
877 pProperties += 1;
878 *pPropertyCount -= 1;
879 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800880 }
881
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800882 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800883 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800884 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800885 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800886
Chia-I Wu31938252016-05-23 15:31:02 +0800887 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
888 int idx = Hal::Get().GetDebugReportIndex();
889 if (idx < 0) {
890 *pPropertyCount += 1;
891 } else if (pProperties &&
892 static_cast<uint32_t>(idx) < *pPropertyCount) {
893 pProperties[idx].specVersion =
894 std::min(pProperties[idx].specVersion,
895 loader_debug_report_extension.specVersion);
896 }
897
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800898 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800899 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800900
901 return result;
902}
903
Chris Forbesfa25e632017-02-22 12:36:02 +1300904bool QueryPresentationProperties(
905 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700906 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300907 const InstanceData& data = GetData(physicalDevice);
908
909 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700910 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
911 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300912 return false;
913
914 // Request the android-specific presentation properties via GPDP2
915 VkPhysicalDeviceProperties2KHR properties = {
916 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
917 presentation_properties,
918 {}
919 };
920
921#pragma clang diagnostic push
922#pragma clang diagnostic ignored "-Wold-style-cast"
923 presentation_properties->sType =
924 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
925#pragma clang diagnostic pop
926 presentation_properties->pNext = nullptr;
927 presentation_properties->sharedImage = VK_FALSE;
928
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700929 if (data.driver.GetPhysicalDeviceProperties2KHR) {
930 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
931 &properties);
932 } else {
933 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
934 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300935
936 return true;
937}
938
Chia-I Wu01cf3052016-03-24 16:16:21 +0800939VkResult EnumerateDeviceExtensionProperties(
940 VkPhysicalDevice physicalDevice,
941 const char* pLayerName,
942 uint32_t* pPropertyCount,
943 VkExtensionProperties* pProperties) {
944 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300945 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700946 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300947 loader_extensions.push_back({
948 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
949 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300950
Sundong Ahnbc37dd52020-04-23 21:21:00 +0900951 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800952 if (hdrBoardConfig) {
953 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
954 VK_EXT_HDR_METADATA_SPEC_VERSION});
955 }
956
Chris Forbes16095002017-05-05 15:33:29 -0700957 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
958 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
959 presentation_properties.sharedImage) {
960 loader_extensions.push_back({
961 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
962 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300963 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700964
Ian Elliott5c34de22017-04-10 14:42:30 -0600965 // conditionally add VK_GOOGLE_display_timing if present timestamps are
966 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -0700967 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600968 loader_extensions.push_back({
969 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
970 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
971 }
972
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700973 // enumerate our extensions first
974 if (!pLayerName && pProperties) {
975 uint32_t count = std::min(
976 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
977
Yiwei Zhang5e862202019-06-21 14:59:16 -0700978 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700979
980 if (count < loader_extensions.size()) {
981 *pPropertyCount = count;
982 return VK_INCOMPLETE;
983 }
984
985 pProperties += count;
986 *pPropertyCount -= count;
987 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800988
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800989 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800990 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
991 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800992 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800993
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700994 if (pProperties) {
995 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
996 for (uint32_t i = 0; i < *pPropertyCount; i++) {
997 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800998
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700999 if (strcmp(prop.extensionName,
1000 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1001 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001002
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001003 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1004 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001005
1006 if (prop.specVersion >= 8) {
1007 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1008 } else {
1009 prop.specVersion = 68;
1010 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001011 }
1012 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001013
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001014 // restore loader extension count
1015 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1016 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001017 }
1018
1019 return result;
1020}
1021
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001022VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1023 const VkAllocationCallbacks* pAllocator,
1024 VkInstance* pInstance) {
1025 const VkAllocationCallbacks& data_allocator =
1026 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1027
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001028 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001029 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001030 if (result != VK_SUCCESS)
1031 return result;
1032
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001033 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001034 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001035 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001036 if (!data)
1037 return VK_ERROR_OUT_OF_HOST_MEMORY;
1038
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001039 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001040
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001041 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001042#pragma clang diagnostic push
1043#pragma clang diagnostic ignored "-Wold-style-cast"
1044 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1045 ? pCreateInfo->pApplicationInfo->apiVersion
1046 : VK_API_VERSION_1_0);
1047 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1048 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1049 uint32_t icd_api_version;
1050 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1051 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001052 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001053 "vkEnumerateInstanceVersion"));
1054 if (!pfn_enumerate_instance_version) {
1055 icd_api_version = VK_API_VERSION_1_0;
1056 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001057 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001058 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001059 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001060 }
1061 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1062 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1063
1064 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1065 ((api_major_version > 1) || (api_minor_version > 0))) {
1066 api_version = VK_API_VERSION_1_0;
1067 wrapper.DowngradeApiVersion();
1068 }
1069#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001070 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001071
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001072 // call into the driver
1073 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001074 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001075 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001076 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1077 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001078 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001079 if (result != VK_SUCCESS) {
1080 FreeInstanceData(data, data_allocator);
1081 return result;
1082 }
1083
1084 // initialize InstanceDriverTable
1085 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001086 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001087 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001088 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001089 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001090 if (data->driver.DestroyInstance)
1091 data->driver.DestroyInstance(instance, pAllocator);
1092
1093 FreeInstanceData(data, data_allocator);
1094
1095 return VK_ERROR_INCOMPATIBLE_DRIVER;
1096 }
1097
1098 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001099 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001100 if (!data->get_device_proc_addr) {
1101 data->driver.DestroyInstance(instance, pAllocator);
1102 FreeInstanceData(data, data_allocator);
1103
1104 return VK_ERROR_INCOMPATIBLE_DRIVER;
1105 }
1106
1107 *pInstance = instance;
1108
1109 return VK_SUCCESS;
1110}
1111
1112void DestroyInstance(VkInstance instance,
1113 const VkAllocationCallbacks* pAllocator) {
1114 InstanceData& data = GetData(instance);
1115 data.driver.DestroyInstance(instance, pAllocator);
1116
1117 VkAllocationCallbacks local_allocator;
1118 if (!pAllocator) {
1119 local_allocator = data.allocator;
1120 pAllocator = &local_allocator;
1121 }
1122
1123 FreeInstanceData(&data, *pAllocator);
1124}
1125
Chia-I Wu4901db72016-03-24 16:38:58 +08001126VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1127 const VkDeviceCreateInfo* pCreateInfo,
1128 const VkAllocationCallbacks* pAllocator,
1129 VkDevice* pDevice) {
1130 const InstanceData& instance_data = GetData(physicalDevice);
1131 const VkAllocationCallbacks& data_allocator =
1132 (pAllocator) ? *pAllocator : instance_data.allocator;
1133
1134 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001135 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001136 if (result != VK_SUCCESS)
1137 return result;
1138
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001139 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001140 DeviceData* data = AllocateDeviceData(data_allocator,
1141 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001142 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001143 if (!data)
1144 return VK_ERROR_OUT_OF_HOST_MEMORY;
1145
Yiwei Zhangec6c5052019-10-17 15:53:00 -07001146 VkPhysicalDeviceProperties properties;
1147 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1148 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1149 &properties);
1150 ATRACE_END();
1151
1152 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001153 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001154
1155 // call into the driver
1156 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001157 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001158 result = instance_data.driver.CreateDevice(
1159 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1160 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001161 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001162 if (result != VK_SUCCESS) {
1163 FreeDeviceData(data, data_allocator);
1164 return result;
1165 }
1166
1167 // initialize DeviceDriverTable
1168 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001169 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1170 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001171 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1172 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1173 if (data->driver.DestroyDevice)
1174 data->driver.DestroyDevice(dev, pAllocator);
1175
1176 FreeDeviceData(data, data_allocator);
1177
1178 return VK_ERROR_INCOMPATIBLE_DRIVER;
1179 }
Chris Forbesd8277912017-02-10 14:59:59 +13001180
1181 // sanity check ANDROID_native_buffer implementation, whose set of
1182 // entrypoints varies according to the spec version.
1183 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1184 !data->driver.GetSwapchainGrallocUsageANDROID &&
1185 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1186 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1187 " must expose at least one of "
1188 "vkGetSwapchainGrallocUsageANDROID or "
1189 "vkGetSwapchainGrallocUsage2ANDROID");
1190
1191 data->driver.DestroyDevice(dev, pAllocator);
1192 FreeDeviceData(data, data_allocator);
1193
1194 return VK_ERROR_INCOMPATIBLE_DRIVER;
1195 }
1196
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001197 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1198 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangb1c1a372019-07-03 13:39:32 -07001199 android::GraphicsEnv::getInstance().setTargetStats(
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001200 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001201 }
1202
Jesse Halldc225072016-05-30 22:40:14 -07001203 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001204
1205 *pDevice = dev;
1206
1207 return VK_SUCCESS;
1208}
1209
1210void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1211 DeviceData& data = GetData(device);
1212 data.driver.DestroyDevice(device, pAllocator);
1213
1214 VkAllocationCallbacks local_allocator;
1215 if (!pAllocator) {
1216 local_allocator = data.allocator;
1217 pAllocator = &local_allocator;
1218 }
1219
1220 FreeDeviceData(&data, *pAllocator);
1221}
1222
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001223VkResult EnumeratePhysicalDevices(VkInstance instance,
1224 uint32_t* pPhysicalDeviceCount,
1225 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001226 ATRACE_CALL();
1227
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001228 const auto& data = GetData(instance);
1229
1230 VkResult result = data.driver.EnumeratePhysicalDevices(
1231 instance, pPhysicalDeviceCount, pPhysicalDevices);
1232 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1233 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1234 SetData(pPhysicalDevices[i], data);
1235 }
1236
1237 return result;
1238}
1239
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001240VkResult EnumeratePhysicalDeviceGroups(
1241 VkInstance instance,
1242 uint32_t* pPhysicalDeviceGroupCount,
1243 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001244 ATRACE_CALL();
1245
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001246 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001247 const auto& data = GetData(instance);
1248
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001249 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1250 uint32_t device_count = 0;
1251 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1252 if (result < 0)
1253 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001254
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001255 if (!pPhysicalDeviceGroupProperties) {
1256 *pPhysicalDeviceGroupCount = device_count;
1257 return result;
1258 }
1259
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001260 if (!device_count) {
1261 *pPhysicalDeviceGroupCount = 0;
1262 return result;
1263 }
Chad Versace32c087f2018-09-09 07:28:05 -07001264 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1265 if (!device_count)
1266 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001267
Yiwei Zhang5e862202019-06-21 14:59:16 -07001268 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001269 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001270 result =
1271 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001272 if (result < 0)
1273 return result;
1274
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001275 for (uint32_t i = 0; i < device_count; ++i) {
1276 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1277 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1278 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1279 }
1280 } else {
1281 result = data.driver.EnumeratePhysicalDeviceGroups(
1282 instance, pPhysicalDeviceGroupCount,
1283 pPhysicalDeviceGroupProperties);
1284 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1285 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1286 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1287 for (uint32_t j = 0;
1288 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1289 j++) {
1290 SetData(
1291 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001292 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001293 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001294 }
1295 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001296 }
1297
1298 return result;
1299}
1300
Chia-I Wuba0be412016-03-24 16:24:40 +08001301void GetDeviceQueue(VkDevice device,
1302 uint32_t queueFamilyIndex,
1303 uint32_t queueIndex,
1304 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001305 ATRACE_CALL();
1306
Chia-I Wuba0be412016-03-24 16:24:40 +08001307 const auto& data = GetData(device);
1308
1309 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1310 SetData(*pQueue, data);
1311}
1312
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001313void GetDeviceQueue2(VkDevice device,
1314 const VkDeviceQueueInfo2* pQueueInfo,
1315 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001316 ATRACE_CALL();
1317
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001318 const auto& data = GetData(device);
1319
1320 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001321 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001322}
1323
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001324VKAPI_ATTR VkResult
1325AllocateCommandBuffers(VkDevice device,
1326 const VkCommandBufferAllocateInfo* pAllocateInfo,
1327 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001328 ATRACE_CALL();
1329
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001330 const auto& data = GetData(device);
1331
1332 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1333 pCommandBuffers);
1334 if (result == VK_SUCCESS) {
1335 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1336 SetData(pCommandBuffers[i], data);
1337 }
1338
1339 return result;
1340}
1341
Yiwei Zhang899d1752019-09-23 16:05:35 -07001342VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1343 uint32_t submitCount,
1344 const VkSubmitInfo* pSubmits,
1345 VkFence fence) {
1346 ATRACE_CALL();
1347
1348 const auto& data = GetData(queue);
1349
1350 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1351}
1352
Chia-I Wu9d518162016-03-24 14:55:27 +08001353} // namespace driver
1354} // namespace vulkan