blob: c63fdf596959d976d7026099c6b1f3a6c3f19d45 [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:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700552 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700553 case ProcHook::EXTENSION_COUNT:
554 // Device and meta extensions. If we ever get here it's a bug in
555 // our code. But enumerating them lets us avoid having a default
556 // case, and default hides other bugs.
557 ALOGE(
558 "CreateInfoWrapper::FilterExtension: invalid instance "
559 "extension '%s'. FIX ME",
560 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800561 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700562
563 // Don't use a default case. Without it, -Wswitch will tell us
564 // at compile time if someone adds a new ProcHook extension but
565 // doesn't handle it above. That's a real bug that has
566 // not-immediately-obvious effects.
567 //
568 // default:
569 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800570 }
571 } else {
572 switch (ext_bit) {
573 case ProcHook::KHR_swapchain:
574 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
575 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
576 ext_bit = ProcHook::ANDROID_native_buffer;
577 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700578 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700579 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300580 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700581 hook_extensions_.set(ext_bit);
582 // return now as these extensions do not require HAL support
583 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700584 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700585 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700586 hook_extensions_.set(ext_bit);
587 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700588 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800589 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700590 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800591 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700592
593 case ProcHook::KHR_android_surface:
594 case ProcHook::KHR_get_physical_device_properties2:
595 case ProcHook::KHR_get_surface_capabilities2:
596 case ProcHook::KHR_surface:
597 case ProcHook::EXT_debug_report:
598 case ProcHook::EXT_swapchain_colorspace:
599 case ProcHook::ANDROID_native_buffer:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700600 case ProcHook::EXTENSION_CORE_1_0:
601 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700602 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700603 case ProcHook::EXTENSION_COUNT:
604 // Instance and meta extensions. If we ever get here it's a bug
605 // in our code. But enumerating them lets us avoid having a
606 // default case, and default hides other bugs.
607 ALOGE(
608 "CreateInfoWrapper::FilterExtension: invalid device "
609 "extension '%s'. FIX ME",
610 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800611 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700612
613 // Don't use a default case. Without it, -Wswitch will tell us
614 // at compile time if someone adds a new ProcHook extension but
615 // doesn't handle it above. That's a real bug that has
616 // not-immediately-obvious effects.
617 //
618 // default:
619 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800620 }
621 }
622
623 for (uint32_t i = 0; i < filter.ext_count; i++) {
624 const VkExtensionProperties& props = filter.exts[i];
625 // ignore unknown extensions
626 if (strcmp(name, props.extensionName) != 0)
627 continue;
628
Chia-I Wu4901db72016-03-24 16:38:58 +0800629 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800630 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
631 if (ext_bit == ProcHook::ANDROID_native_buffer)
632 hook_extensions_.set(ProcHook::KHR_swapchain);
633
634 hal_extensions_.set(ext_bit);
635 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800636
637 break;
638 }
639}
640
Ian Elliottf3e872d2017-11-02 10:15:13 -0600641void CreateInfoWrapper::DowngradeApiVersion() {
642 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
643 if (instance_info_.pApplicationInfo) {
644 application_info_ = *instance_info_.pApplicationInfo;
645 instance_info_.pApplicationInfo = &application_info_;
646 application_info_.apiVersion = VK_API_VERSION_1_0;
647 }
648}
649
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700650void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
651 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700652
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700653#pragma clang diagnostic push
654#pragma clang diagnostic ignored "-Wold-style-cast"
655 api_version ^= VK_VERSION_PATCH(api_version);
656#pragma clang diagnostic pop
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700657
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700658 // cap the API version to the loader supported highest version
659 if (api_version > VK_API_VERSION_1_1)
660 api_version = VK_API_VERSION_1_1;
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700661
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700662 switch (api_version) {
663 case VK_API_VERSION_1_1:
664 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
665 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
666 [[clang::fallthrough]];
667 case VK_API_VERSION_1_0:
668 break;
669 default:
670 ALOGD("Unknown upgrade API version[%u]", api_version);
671 break;
672 }
673}
674
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800675VKAPI_ATTR void* DefaultAllocate(void*,
676 size_t size,
677 size_t alignment,
678 VkSystemAllocationScope) {
679 void* ptr = nullptr;
680 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
681 // additionally requires that it be at least sizeof(void*).
682 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
683 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
684 ret, ptr);
685 return ret == 0 ? ptr : nullptr;
686}
687
688VKAPI_ATTR void* DefaultReallocate(void*,
689 void* ptr,
690 size_t size,
691 size_t alignment,
692 VkSystemAllocationScope) {
693 if (size == 0) {
694 free(ptr);
695 return nullptr;
696 }
697
Yiwei Zhanga885c062019-10-24 12:07:57 -0700698 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800699 // request is smaller than the existing chunk, we just continue using it.
700 // Right now the loader never reallocs, so this doesn't matter. If that
701 // changes, or if this code is copied into some other project, this should
702 // probably have a heuristic to allocate-copy-free when doing so will save
703 // "enough" space.
704 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
705 if (size <= old_size)
706 return ptr;
707
708 void* new_ptr = nullptr;
709 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
710 return nullptr;
711 if (ptr) {
712 memcpy(new_ptr, ptr, std::min(old_size, size));
713 free(ptr);
714 }
715 return new_ptr;
716}
717
718VKAPI_ATTR void DefaultFree(void*, void* ptr) {
719 ALOGD_CALLSTACK("Free: %p", ptr);
720 free(ptr);
721}
722
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800723InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
724 void* data_mem = allocator.pfnAllocation(
725 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
726 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
727 if (!data_mem)
728 return nullptr;
729
730 return new (data_mem) InstanceData(allocator);
731}
732
733void FreeInstanceData(InstanceData* data,
734 const VkAllocationCallbacks& allocator) {
735 data->~InstanceData();
736 allocator.pfnFree(allocator.pUserData, data);
737}
738
Chia-I Wu950d6e12016-05-03 09:12:35 +0800739DeviceData* AllocateDeviceData(
740 const VkAllocationCallbacks& allocator,
741 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800742 void* data_mem = allocator.pfnAllocation(
743 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
744 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
745 if (!data_mem)
746 return nullptr;
747
Chia-I Wu950d6e12016-05-03 09:12:35 +0800748 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800749}
750
751void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
752 data->~DeviceData();
753 allocator.pfnFree(allocator.pUserData, data);
754}
755
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800756} // anonymous namespace
757
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800758bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800759 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800760}
761
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800762const VkAllocationCallbacks& GetDefaultAllocator() {
763 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
764 .pUserData = nullptr,
765 .pfnAllocation = DefaultAllocate,
766 .pfnReallocation = DefaultReallocate,
767 .pfnFree = DefaultFree,
768 };
769
770 return kDefaultAllocCallbacks;
771}
772
Chia-I Wueb7db122016-03-24 09:11:06 +0800773PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
774 const ProcHook* hook = GetProcHook(pName);
775 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800776 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800777
778 if (!instance) {
779 if (hook->type == ProcHook::GLOBAL)
780 return hook->proc;
781
Chia-I Wu109f8982016-04-22 06:40:40 +0800782 // v0 layers expect
783 //
784 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
785 //
786 // to work.
787 if (strcmp(pName, "vkCreateDevice") == 0)
788 return hook->proc;
789
Chia-I Wueb7db122016-03-24 09:11:06 +0800790 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800791 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800792 pName);
793
Chia-I Wu109f8982016-04-22 06:40:40 +0800794 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800795 }
796
797 PFN_vkVoidFunction proc;
798
799 switch (hook->type) {
800 case ProcHook::INSTANCE:
801 proc = (GetData(instance).hook_extensions[hook->extension])
802 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800803 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800804 break;
805 case ProcHook::DEVICE:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700806 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800807 ? hook->proc
808 : hook->checked_proc;
809 break;
810 default:
811 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800812 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800813 pName);
814 proc = nullptr;
815 break;
816 }
817
818 return proc;
819}
820
821PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
822 const ProcHook* hook = GetProcHook(pName);
823 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800824 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800825
826 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800827 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800828 return nullptr;
829 }
830
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800831 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
832 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800833}
834
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800835VkResult EnumerateInstanceExtensionProperties(
836 const char* pLayerName,
837 uint32_t* pPropertyCount,
838 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700839 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600840 loader_extensions.push_back({
841 VK_KHR_SURFACE_EXTENSION_NAME,
842 VK_KHR_SURFACE_SPEC_VERSION});
843 loader_extensions.push_back({
844 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
845 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
846 loader_extensions.push_back({
847 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
848 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700849 loader_extensions.push_back({
850 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
851 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600852
Chia-I Wu31938252016-05-23 15:31:02 +0800853 static const VkExtensionProperties loader_debug_report_extension = {
854 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
855 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800856
857 // enumerate our extensions first
858 if (!pLayerName && pProperties) {
859 uint32_t count = std::min(
860 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
861
Yiwei Zhang5e862202019-06-21 14:59:16 -0700862 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800863
864 if (count < loader_extensions.size()) {
865 *pPropertyCount = count;
866 return VK_INCOMPLETE;
867 }
868
869 pProperties += count;
870 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800871
872 if (Hal::Get().GetDebugReportIndex() < 0) {
873 if (!*pPropertyCount) {
874 *pPropertyCount = count;
875 return VK_INCOMPLETE;
876 }
877
878 pProperties[0] = loader_debug_report_extension;
879 pProperties += 1;
880 *pPropertyCount -= 1;
881 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800882 }
883
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800884 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800885 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800886 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800887 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800888
Chia-I Wu31938252016-05-23 15:31:02 +0800889 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
890 int idx = Hal::Get().GetDebugReportIndex();
891 if (idx < 0) {
892 *pPropertyCount += 1;
893 } else if (pProperties &&
894 static_cast<uint32_t>(idx) < *pPropertyCount) {
895 pProperties[idx].specVersion =
896 std::min(pProperties[idx].specVersion,
897 loader_debug_report_extension.specVersion);
898 }
899
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800900 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800901 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800902
903 return result;
904}
905
Chris Forbesfa25e632017-02-22 12:36:02 +1300906bool QueryPresentationProperties(
907 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700908 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300909 const InstanceData& data = GetData(physicalDevice);
910
911 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700912 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
913 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300914 return false;
915
916 // Request the android-specific presentation properties via GPDP2
917 VkPhysicalDeviceProperties2KHR properties = {
918 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
919 presentation_properties,
920 {}
921 };
922
923#pragma clang diagnostic push
924#pragma clang diagnostic ignored "-Wold-style-cast"
925 presentation_properties->sType =
926 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
927#pragma clang diagnostic pop
928 presentation_properties->pNext = nullptr;
929 presentation_properties->sharedImage = VK_FALSE;
930
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700931 if (data.driver.GetPhysicalDeviceProperties2KHR) {
932 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
933 &properties);
934 } else {
935 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
936 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300937
938 return true;
939}
940
Chia-I Wu01cf3052016-03-24 16:16:21 +0800941VkResult EnumerateDeviceExtensionProperties(
942 VkPhysicalDevice physicalDevice,
943 const char* pLayerName,
944 uint32_t* pPropertyCount,
945 VkExtensionProperties* pProperties) {
946 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300947 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700948 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300949 loader_extensions.push_back({
950 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
951 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300952
Sundong Ahnbc37dd52020-04-23 21:21:00 +0900953 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800954 if (hdrBoardConfig) {
955 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
956 VK_EXT_HDR_METADATA_SPEC_VERSION});
957 }
958
Chris Forbes16095002017-05-05 15:33:29 -0700959 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
960 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
961 presentation_properties.sharedImage) {
962 loader_extensions.push_back({
963 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
964 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300965 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700966
Ian Elliott5c34de22017-04-10 14:42:30 -0600967 // conditionally add VK_GOOGLE_display_timing if present timestamps are
968 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -0700969 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600970 loader_extensions.push_back({
971 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
972 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
973 }
974
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700975 // enumerate our extensions first
976 if (!pLayerName && pProperties) {
977 uint32_t count = std::min(
978 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
979
Yiwei Zhang5e862202019-06-21 14:59:16 -0700980 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700981
982 if (count < loader_extensions.size()) {
983 *pPropertyCount = count;
984 return VK_INCOMPLETE;
985 }
986
987 pProperties += count;
988 *pPropertyCount -= count;
989 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800990
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800991 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800992 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
993 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800994 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800995
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700996 if (pProperties) {
997 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
998 for (uint32_t i = 0; i < *pPropertyCount; i++) {
999 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001000
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001001 if (strcmp(prop.extensionName,
1002 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1003 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001004
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001005 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1006 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001007
1008 if (prop.specVersion >= 8) {
1009 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1010 } else {
1011 prop.specVersion = 68;
1012 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001013 }
1014 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001015
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001016 // restore loader extension count
1017 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1018 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001019 }
1020
1021 return result;
1022}
1023
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001024VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1025 const VkAllocationCallbacks* pAllocator,
1026 VkInstance* pInstance) {
1027 const VkAllocationCallbacks& data_allocator =
1028 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1029
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001030 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001031 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001032 if (result != VK_SUCCESS)
1033 return result;
1034
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001035 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001036 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001037 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001038 if (!data)
1039 return VK_ERROR_OUT_OF_HOST_MEMORY;
1040
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001041 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001042
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001043 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001044#pragma clang diagnostic push
1045#pragma clang diagnostic ignored "-Wold-style-cast"
1046 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1047 ? pCreateInfo->pApplicationInfo->apiVersion
1048 : VK_API_VERSION_1_0);
1049 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1050 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1051 uint32_t icd_api_version;
1052 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1053 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001054 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001055 "vkEnumerateInstanceVersion"));
1056 if (!pfn_enumerate_instance_version) {
1057 icd_api_version = VK_API_VERSION_1_0;
1058 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001059 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001060 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001061 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001062 }
1063 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1064 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1065
1066 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1067 ((api_major_version > 1) || (api_minor_version > 0))) {
1068 api_version = VK_API_VERSION_1_0;
1069 wrapper.DowngradeApiVersion();
1070 }
1071#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001072 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001073
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001074 // call into the driver
1075 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001076 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001077 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001078 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1079 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001080 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001081 if (result != VK_SUCCESS) {
1082 FreeInstanceData(data, data_allocator);
1083 return result;
1084 }
1085
1086 // initialize InstanceDriverTable
1087 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001088 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001089 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001090 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001091 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001092 if (data->driver.DestroyInstance)
1093 data->driver.DestroyInstance(instance, pAllocator);
1094
1095 FreeInstanceData(data, data_allocator);
1096
1097 return VK_ERROR_INCOMPATIBLE_DRIVER;
1098 }
1099
1100 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001101 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001102 if (!data->get_device_proc_addr) {
1103 data->driver.DestroyInstance(instance, pAllocator);
1104 FreeInstanceData(data, data_allocator);
1105
1106 return VK_ERROR_INCOMPATIBLE_DRIVER;
1107 }
1108
1109 *pInstance = instance;
1110
1111 return VK_SUCCESS;
1112}
1113
1114void DestroyInstance(VkInstance instance,
1115 const VkAllocationCallbacks* pAllocator) {
1116 InstanceData& data = GetData(instance);
1117 data.driver.DestroyInstance(instance, pAllocator);
1118
1119 VkAllocationCallbacks local_allocator;
1120 if (!pAllocator) {
1121 local_allocator = data.allocator;
1122 pAllocator = &local_allocator;
1123 }
1124
1125 FreeInstanceData(&data, *pAllocator);
1126}
1127
Chia-I Wu4901db72016-03-24 16:38:58 +08001128VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1129 const VkDeviceCreateInfo* pCreateInfo,
1130 const VkAllocationCallbacks* pAllocator,
1131 VkDevice* pDevice) {
1132 const InstanceData& instance_data = GetData(physicalDevice);
1133 const VkAllocationCallbacks& data_allocator =
1134 (pAllocator) ? *pAllocator : instance_data.allocator;
1135
1136 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001137 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001138 if (result != VK_SUCCESS)
1139 return result;
1140
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001141 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001142 DeviceData* data = AllocateDeviceData(data_allocator,
1143 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001144 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001145 if (!data)
1146 return VK_ERROR_OUT_OF_HOST_MEMORY;
1147
Yiwei Zhangec6c5052019-10-17 15:53:00 -07001148 VkPhysicalDeviceProperties properties;
1149 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1150 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1151 &properties);
1152 ATRACE_END();
1153
1154 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001155 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001156
1157 // call into the driver
1158 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001159 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001160 result = instance_data.driver.CreateDevice(
1161 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1162 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001163 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001164 if (result != VK_SUCCESS) {
1165 FreeDeviceData(data, data_allocator);
1166 return result;
1167 }
1168
1169 // initialize DeviceDriverTable
1170 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001171 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1172 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001173 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1174 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1175 if (data->driver.DestroyDevice)
1176 data->driver.DestroyDevice(dev, pAllocator);
1177
1178 FreeDeviceData(data, data_allocator);
1179
1180 return VK_ERROR_INCOMPATIBLE_DRIVER;
1181 }
Chris Forbesd8277912017-02-10 14:59:59 +13001182
1183 // sanity check ANDROID_native_buffer implementation, whose set of
1184 // entrypoints varies according to the spec version.
1185 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1186 !data->driver.GetSwapchainGrallocUsageANDROID &&
1187 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1188 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1189 " must expose at least one of "
1190 "vkGetSwapchainGrallocUsageANDROID or "
1191 "vkGetSwapchainGrallocUsage2ANDROID");
1192
1193 data->driver.DestroyDevice(dev, pAllocator);
1194 FreeDeviceData(data, data_allocator);
1195
1196 return VK_ERROR_INCOMPATIBLE_DRIVER;
1197 }
1198
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001199 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1200 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangb1c1a372019-07-03 13:39:32 -07001201 android::GraphicsEnv::getInstance().setTargetStats(
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001202 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001203 }
1204
Jesse Halldc225072016-05-30 22:40:14 -07001205 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001206
1207 *pDevice = dev;
1208
1209 return VK_SUCCESS;
1210}
1211
1212void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1213 DeviceData& data = GetData(device);
1214 data.driver.DestroyDevice(device, pAllocator);
1215
1216 VkAllocationCallbacks local_allocator;
1217 if (!pAllocator) {
1218 local_allocator = data.allocator;
1219 pAllocator = &local_allocator;
1220 }
1221
1222 FreeDeviceData(&data, *pAllocator);
1223}
1224
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001225VkResult EnumeratePhysicalDevices(VkInstance instance,
1226 uint32_t* pPhysicalDeviceCount,
1227 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001228 ATRACE_CALL();
1229
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001230 const auto& data = GetData(instance);
1231
1232 VkResult result = data.driver.EnumeratePhysicalDevices(
1233 instance, pPhysicalDeviceCount, pPhysicalDevices);
1234 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1235 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1236 SetData(pPhysicalDevices[i], data);
1237 }
1238
1239 return result;
1240}
1241
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001242VkResult EnumeratePhysicalDeviceGroups(
1243 VkInstance instance,
1244 uint32_t* pPhysicalDeviceGroupCount,
1245 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001246 ATRACE_CALL();
1247
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001248 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001249 const auto& data = GetData(instance);
1250
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001251 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1252 uint32_t device_count = 0;
1253 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1254 if (result < 0)
1255 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001256
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001257 if (!pPhysicalDeviceGroupProperties) {
1258 *pPhysicalDeviceGroupCount = device_count;
1259 return result;
1260 }
1261
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001262 if (!device_count) {
1263 *pPhysicalDeviceGroupCount = 0;
1264 return result;
1265 }
Chad Versace32c087f2018-09-09 07:28:05 -07001266 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1267 if (!device_count)
1268 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001269
Yiwei Zhang5e862202019-06-21 14:59:16 -07001270 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001271 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001272 result =
1273 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001274 if (result < 0)
1275 return result;
1276
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001277 for (uint32_t i = 0; i < device_count; ++i) {
1278 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1279 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1280 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1281 }
1282 } else {
1283 result = data.driver.EnumeratePhysicalDeviceGroups(
1284 instance, pPhysicalDeviceGroupCount,
1285 pPhysicalDeviceGroupProperties);
1286 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1287 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1288 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1289 for (uint32_t j = 0;
1290 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1291 j++) {
1292 SetData(
1293 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001294 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001295 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001296 }
1297 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001298 }
1299
1300 return result;
1301}
1302
Chia-I Wuba0be412016-03-24 16:24:40 +08001303void GetDeviceQueue(VkDevice device,
1304 uint32_t queueFamilyIndex,
1305 uint32_t queueIndex,
1306 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001307 ATRACE_CALL();
1308
Chia-I Wuba0be412016-03-24 16:24:40 +08001309 const auto& data = GetData(device);
1310
1311 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1312 SetData(*pQueue, data);
1313}
1314
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001315void GetDeviceQueue2(VkDevice device,
1316 const VkDeviceQueueInfo2* pQueueInfo,
1317 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001318 ATRACE_CALL();
1319
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001320 const auto& data = GetData(device);
1321
1322 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001323 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001324}
1325
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001326VKAPI_ATTR VkResult
1327AllocateCommandBuffers(VkDevice device,
1328 const VkCommandBufferAllocateInfo* pAllocateInfo,
1329 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001330 ATRACE_CALL();
1331
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001332 const auto& data = GetData(device);
1333
1334 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1335 pCommandBuffers);
1336 if (result == VK_SUCCESS) {
1337 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1338 SetData(pCommandBuffers[i], data);
1339 }
1340
1341 return result;
1342}
1343
Yiwei Zhang899d1752019-09-23 16:05:35 -07001344VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1345 uint32_t submitCount,
1346 const VkSubmitInfo* pSubmits,
1347 VkFence fence) {
1348 ATRACE_CALL();
1349
1350 const auto& data = GetData(queue);
1351
1352 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1353}
1354
Chia-I Wu9d518162016-03-24 14:55:27 +08001355} // namespace driver
1356} // namespace vulkan