blob: cf774fd9b82b6ceb837fa60740929732da91e921 [file] [log] [blame]
Chia-I Wu9d518162016-03-24 14:55:27 +08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang5e862202019-06-21 14:59:16 -070019#include "driver.h"
20
21#include <dlfcn.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <stdlib.h>
24#include <string.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080025
Sundong Ahnbc37dd52020-04-23 21:21:00 +090026#include <SurfaceFlingerProperties.h>
27#include <android-base/properties.h>
Jesse Hall53457db2016-12-14 16:54:06 -080028#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
30#include <configstore/Utils.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
33#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080034#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080035#include <utils/Trace.h>
Yiwei Zhang40e84f12020-10-14 08:42:26 -070036#include <vndksupport/linker.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037
Yiwei Zhang5e862202019-06-21 14:59:16 -070038#include <algorithm>
39#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070040#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070041#include <new>
42#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070043
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070044#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080045
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080046using namespace android::hardware::configstore;
47using namespace android::hardware::configstore::V1_0;
48
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080049// #define ENABLE_ALLOC_CALLSTACKS 1
50#if ENABLE_ALLOC_CALLSTACKS
51#include <utils/CallStack.h>
52#define ALOGD_CALLSTACK(...) \
53 do { \
54 ALOGD(__VA_ARGS__); \
55 android::CallStack callstack; \
56 callstack.update(); \
57 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
58 } while (false)
59#else
60#define ALOGD_CALLSTACK(...) \
61 do { \
62 } while (false)
63#endif
64
Chia-I Wu9d518162016-03-24 14:55:27 +080065namespace vulkan {
66namespace driver {
67
Chia-I Wu136b8eb2016-03-24 15:01:52 +080068namespace {
69
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080070class Hal {
71 public:
72 static bool Open();
73
74 static const Hal& Get() { return hal_; }
75 static const hwvulkan_device_t& Device() { return *Get().dev_; }
76
Chia-I Wu31938252016-05-23 15:31:02 +080077 int GetDebugReportIndex() const { return debug_report_index_; }
78
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080079 private:
Chia-I Wu31938252016-05-23 15:31:02 +080080 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 Hal(const Hal&) = delete;
82 Hal& operator=(const Hal&) = delete;
83
Yiwei Zhang901f8ee2020-07-31 13:18:49 -070084 bool ShouldUnloadBuiltinDriver();
85 void UnloadBuiltinDriver();
Chia-I Wu31938252016-05-23 15:31:02 +080086 bool InitDebugReportIndex();
87
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080088 static Hal hal_;
89
90 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080091 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080092};
93
Chia-I Wu4901db72016-03-24 16:38:58 +080094class CreateInfoWrapper {
95 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080096 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -070097 uint32_t icd_api_version,
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,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700101 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800102 const VkAllocationCallbacks& allocator);
103 ~CreateInfoWrapper();
104
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800105 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800106
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800107 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800109
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800110 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800111 explicit operator const VkDeviceCreateInfo*() const;
112
113 private:
114 struct ExtensionFilter {
115 VkExtensionProperties* exts;
116 uint32_t ext_count;
117
118 const char** names;
119 uint32_t name_count;
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700120 ExtensionFilter()
121 : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800122 };
123
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700124 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800125 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800126 VkResult SanitizeLayers();
127 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800128
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800129 VkResult QueryExtensionCount(uint32_t& count) const;
130 VkResult EnumerateExtensions(uint32_t& count,
131 VkExtensionProperties* props) const;
132 VkResult InitExtensionFilter();
133 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800134
135 const bool is_instance_;
136 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700137 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700138 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800139
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800140 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800141
142 union {
143 VkInstanceCreateInfo instance_info_;
144 VkDeviceCreateInfo dev_info_;
145 };
146
Ian Elliottf3e872d2017-11-02 10:15:13 -0600147 VkApplicationInfo application_info_;
148
Chia-I Wu4901db72016-03-24 16:38:58 +0800149 ExtensionFilter extension_filter_;
150
151 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
152 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
153};
154
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800155Hal Hal::hal_;
156
Jesse Hall53457db2016-12-14 16:54:06 -0800157const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700158 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800159 "ro.board.platform",
160}};
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700161constexpr int LIB_DL_FLAGS = RTLD_LOCAL | RTLD_NOW;
Jesse Hall53457db2016-12-14 16:54:06 -0800162
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800163// LoadDriver returns:
164// * 0 when succeed, or
165// * -ENOENT when fail to open binary libraries, or
166// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
167// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700168int LoadDriver(android_namespace_t* library_namespace,
169 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800170 ATRACE_CALL();
171
Jesse Hall53457db2016-12-14 16:54:06 -0800172 void* so = nullptr;
Jesse Hall53457db2016-12-14 16:54:06 -0800173 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700174 std::string lib_name = android::base::GetProperty(key, "");
175 if (lib_name.empty())
176 continue;
177
178 lib_name = "vulkan." + lib_name + ".so";
179 if (library_namespace) {
180 // load updated driver
181 const android_dlextinfo dlextinfo = {
182 .flags = ANDROID_DLEXT_USE_NAMESPACE,
183 .library_namespace = library_namespace,
184 };
185 so = android_dlopen_ext(lib_name.c_str(), LIB_DL_FLAGS, &dlextinfo);
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000186 ALOGE("Could not load %s from updatable gfx driver namespace: %s.",
187 lib_name.c_str(), dlerror());
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700188 } else {
189 // load built-in driver
190 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800191 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700192 if (so)
193 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800194 }
195 if (!so)
196 return -ENOENT;
197
Jesse Hall00e61ff2017-04-07 16:48:02 -0700198 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800199 if (!hmi) {
200 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
201 dlclose(so);
202 return -EINVAL;
203 }
204 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
205 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
206 dlclose(so);
207 return -EINVAL;
208 }
209 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700210 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800211 return 0;
212}
213
Jesse Hall00e61ff2017-04-07 16:48:02 -0700214int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800215 ATRACE_CALL();
216
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700218 android::GpuStatsInfo::Driver::VULKAN);
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700219 return LoadDriver(nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700220}
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
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700244 if (hal_.ShouldUnloadBuiltinDriver()) {
245 hal_.UnloadBuiltinDriver();
246 }
247
248 if (hal_.dev_)
249 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800250
251 // Use a stub device unless we successfully open a real HAL device.
252 hal_.dev_ = &stubhal::kDevice;
253
Jesse Hall53457db2016-12-14 16:54:06 -0800254 int result;
255 const hwvulkan_module_t* module = nullptr;
256
Jesse Hall00e61ff2017-04-07 16:48:02 -0700257 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800258 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700259 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800260 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800261 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800262 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700263 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800264 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 return true;
266 }
267
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800268
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800270 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800271 result =
272 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
273 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800274 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800276 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700277 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800278 // Any device with a Vulkan HAL should be able to open the device.
279 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
280 result);
281 return false;
282 }
283
284 hal_.dev_ = device;
285
Chia-I Wu31938252016-05-23 15:31:02 +0800286 hal_.InitDebugReportIndex();
287
Yiwei Zhangd9861812019-02-13 11:51:55 -0800288 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700289 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800290
Chia-I Wu31938252016-05-23 15:31:02 +0800291 return true;
292}
293
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700294bool Hal::ShouldUnloadBuiltinDriver() {
295 // Should not unload since the driver was not loaded
296 if (!hal_.dev_)
297 return false;
298
299 // Should not unload if stubhal is used on the device
300 if (hal_.dev_ == &stubhal::kDevice)
301 return false;
302
303 // Unload the driver if updated driver is chosen
304 if (android::GraphicsEnv::getInstance().getDriverNamespace())
305 return true;
306
307 return false;
308}
309
310void Hal::UnloadBuiltinDriver() {
311 ATRACE_CALL();
312
313 ALOGD("Unload builtin Vulkan driver.");
314
315 // Close the opened device
316 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
317 "hw_device_t::close() failed.");
318
319 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700320 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700321
322 hal_.dev_ = nullptr;
323 hal_.debug_report_index_ = -1;
324}
325
Chia-I Wu31938252016-05-23 15:31:02 +0800326bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800327 ATRACE_CALL();
328
Chia-I Wu31938252016-05-23 15:31:02 +0800329 uint32_t count;
330 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
331 VK_SUCCESS) {
332 ALOGE("failed to get HAL instance extension count");
333 return false;
334 }
335
336 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
337 malloc(sizeof(VkExtensionProperties) * count));
338 if (!exts) {
339 ALOGE("failed to allocate HAL instance extension array");
340 return false;
341 }
342
343 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
344 VK_SUCCESS) {
345 ALOGE("failed to enumerate HAL instance extensions");
346 free(exts);
347 return false;
348 }
349
350 for (uint32_t i = 0; i < count; i++) {
351 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
352 0) {
353 debug_report_index_ = static_cast<int>(i);
354 break;
355 }
356 }
357
358 free(exts);
359
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800360 return true;
361}
362
363CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700364 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800365 const VkAllocationCallbacks& allocator)
366 : is_instance_(true),
367 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700368 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700369 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800370 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800371 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700372 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800373
Chia-I Wu4901db72016-03-24 16:38:58 +0800374CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
375 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700376 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800377 const VkAllocationCallbacks& allocator)
378 : is_instance_(false),
379 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700380 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700381 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800382 physical_dev_(physical_dev),
383 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700384 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800385
386CreateInfoWrapper::~CreateInfoWrapper() {
387 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
388 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
389}
390
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800391VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700392 VkResult result = SanitizeApiVersion();
393 if (result == VK_SUCCESS)
394 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800395 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800396 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800397 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800398 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800399
400 return result;
401}
402
403const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800404CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800405 return hook_extensions_;
406}
407
408const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800409CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800410 return hal_extensions_;
411}
412
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800413CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
414 return &instance_info_;
415}
416
Chia-I Wu4901db72016-03-24 16:38:58 +0800417CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
418 return &dev_info_;
419}
420
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700421VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700422 if (!is_instance_ || !instance_info_.pApplicationInfo)
423 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700424
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700425 if (icd_api_version_ > VK_API_VERSION_1_0 ||
426 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
427 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700428
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700429 // override apiVersion to avoid error return from 1.0 icd
430 application_info_ = *instance_info_.pApplicationInfo;
431 application_info_.apiVersion = VK_API_VERSION_1_0;
432 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700433
434 return VK_SUCCESS;
435}
436
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800437VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800438 const struct StructHeader {
439 VkStructureType type;
440 const void* next;
441 } * header;
442
443 if (is_instance_) {
444 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
445
446 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
447 while (header &&
448 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
449 header = reinterpret_cast<const StructHeader*>(header->next);
450
451 instance_info_.pNext = header;
452 } else {
453 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
454
455 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
456 while (header &&
457 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
458 header = reinterpret_cast<const StructHeader*>(header->next);
459
460 dev_info_.pNext = header;
461 }
462
463 return VK_SUCCESS;
464}
465
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800466VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800467 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
468 : dev_info_.ppEnabledLayerNames;
469 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
470 : dev_info_.enabledLayerCount;
471
472 // remove all layers
473 layer_names = nullptr;
474 layer_count = 0;
475
476 return VK_SUCCESS;
477}
478
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800479VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800480 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
481 : dev_info_.ppEnabledExtensionNames;
482 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
483 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800484
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800485 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800486 if (result != VK_SUCCESS)
487 return result;
488
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700489 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700490 for (uint32_t i = 0; i < ext_count; i++) {
491 // Upon api downgrade, skip the promoted instance extensions in the
492 // first pass to avoid duplicate extensions.
493 const std::optional<uint32_t> version =
494 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700495 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700496 *version <= loader_api_version_)
497 continue;
498
499 FilterExtension(ext_names[i]);
500 }
501
502 // Enable the required extensions to support core functionalities.
503 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700504 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700505 for (const auto& promoted_extension : promoted_extensions)
506 FilterExtension(promoted_extension);
507 } else {
508 for (uint32_t i = 0; i < ext_count; i++)
509 FilterExtension(ext_names[i]);
510 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800511
Jesse Halld3d887a2018-03-05 13:34:45 -0800512 // Enable device extensions that contain physical-device commands, so that
513 // vkGetInstanceProcAddr will return those physical-device commands.
514 if (is_instance_) {
515 hook_extensions_.set(ProcHook::KHR_swapchain);
516 }
517
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700518 const uint32_t api_version =
519 is_instance_ ? loader_api_version_
520 : std::min(icd_api_version_, loader_api_version_);
521 switch (api_version) {
522 case VK_API_VERSION_1_1:
523 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
524 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
525 [[clang::fallthrough]];
526 case VK_API_VERSION_1_0:
527 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
528 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
529 break;
530 default:
531 ALOGE("Unknown API version[%u]", api_version);
532 break;
533 }
534
Chia-I Wu4901db72016-03-24 16:38:58 +0800535 ext_names = extension_filter_.names;
536 ext_count = extension_filter_.name_count;
537
538 return VK_SUCCESS;
539}
540
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800541VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800542 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800543 return Hal::Device().EnumerateInstanceExtensionProperties(
544 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800545 } else {
546 const auto& driver = GetData(physical_dev_).driver;
547 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
548 &count, nullptr);
549 }
550}
551
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800552VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800553 uint32_t& count,
554 VkExtensionProperties* props) const {
555 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800556 return Hal::Device().EnumerateInstanceExtensionProperties(
557 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800558 } else {
559 const auto& driver = GetData(physical_dev_).driver;
560 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
561 &count, props);
562 }
563}
564
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800565VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800566 // query extension count
567 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800568 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800569 if (result != VK_SUCCESS || count == 0)
570 return result;
571
572 auto& filter = extension_filter_;
573 filter.exts =
574 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
575 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
576 alignof(VkExtensionProperties),
577 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
578 if (!filter.exts)
579 return VK_ERROR_OUT_OF_HOST_MEMORY;
580
581 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800582 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800583 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
584 return result;
585
586 if (!count)
587 return VK_SUCCESS;
588
589 filter.ext_count = count;
590
591 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700592 if (is_instance_) {
593 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
594
595 // It requires enabling additional promoted extensions to downgrade api,
596 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700597 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700598 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700599 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700600 }
601
602 count = std::min(filter.ext_count, enabled_ext_count);
603 } else {
604 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
605 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700606
607 if (!count)
608 return VK_SUCCESS;
609
Chia-I Wu4901db72016-03-24 16:38:58 +0800610 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
611 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
612 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
613 if (!filter.names)
614 return VK_ERROR_OUT_OF_HOST_MEMORY;
615
616 return VK_SUCCESS;
617}
618
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800619void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800620 auto& filter = extension_filter_;
621
622 ProcHook::Extension ext_bit = GetProcHookExtension(name);
623 if (is_instance_) {
624 switch (ext_bit) {
625 case ProcHook::KHR_android_surface:
626 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700627 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300628 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800629 hook_extensions_.set(ext_bit);
630 // return now as these extensions do not require HAL support
631 return;
632 case ProcHook::EXT_debug_report:
633 // both we and HAL can take part in
634 hook_extensions_.set(ext_bit);
635 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300636 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700637 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700638 case ProcHook::KHR_external_memory_capabilities:
639 case ProcHook::KHR_external_semaphore_capabilities:
640 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700641 case ProcHook::EXTENSION_UNKNOWN:
642 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800643 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700644
Yiwei Zhang23143102019-04-10 18:24:05 -0700645 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700646 case ProcHook::KHR_incremental_present:
647 case ProcHook::KHR_shared_presentable_image:
648 case ProcHook::KHR_swapchain:
649 case ProcHook::EXT_hdr_metadata:
650 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
651 case ProcHook::ANDROID_native_buffer:
652 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700653 case ProcHook::EXTENSION_CORE_1_0:
654 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700655 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700656 case ProcHook::EXTENSION_COUNT:
657 // Device and meta extensions. If we ever get here it's a bug in
658 // our code. But enumerating them lets us avoid having a default
659 // case, and default hides other bugs.
660 ALOGE(
661 "CreateInfoWrapper::FilterExtension: invalid instance "
662 "extension '%s'. FIX ME",
663 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800664 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700665
666 // Don't use a default case. Without it, -Wswitch will tell us
667 // at compile time if someone adds a new ProcHook extension but
668 // doesn't handle it above. That's a real bug that has
669 // not-immediately-obvious effects.
670 //
671 // default:
672 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800673 }
674 } else {
675 switch (ext_bit) {
676 case ProcHook::KHR_swapchain:
677 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
678 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
679 ext_bit = ProcHook::ANDROID_native_buffer;
680 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700681 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700682 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300683 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700684 hook_extensions_.set(ext_bit);
685 // return now as these extensions do not require HAL support
686 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700687 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700688 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700689 hook_extensions_.set(ext_bit);
690 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700691 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800692 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700693 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800694 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700695
696 case ProcHook::KHR_android_surface:
697 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700698 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700699 case ProcHook::KHR_external_memory_capabilities:
700 case ProcHook::KHR_external_semaphore_capabilities:
701 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700702 case ProcHook::KHR_get_surface_capabilities2:
703 case ProcHook::KHR_surface:
704 case ProcHook::EXT_debug_report:
705 case ProcHook::EXT_swapchain_colorspace:
706 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700707 case ProcHook::EXTENSION_CORE_1_0:
708 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700709 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700710 case ProcHook::EXTENSION_COUNT:
711 // Instance and meta extensions. If we ever get here it's a bug
712 // in our code. But enumerating them lets us avoid having a
713 // default case, and default hides other bugs.
714 ALOGE(
715 "CreateInfoWrapper::FilterExtension: invalid device "
716 "extension '%s'. FIX ME",
717 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800718 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700719
720 // Don't use a default case. Without it, -Wswitch will tell us
721 // at compile time if someone adds a new ProcHook extension but
722 // doesn't handle it above. That's a real bug that has
723 // not-immediately-obvious effects.
724 //
725 // default:
726 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800727 }
728 }
729
730 for (uint32_t i = 0; i < filter.ext_count; i++) {
731 const VkExtensionProperties& props = filter.exts[i];
732 // ignore unknown extensions
733 if (strcmp(name, props.extensionName) != 0)
734 continue;
735
Chia-I Wu4901db72016-03-24 16:38:58 +0800736 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800737 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
738 if (ext_bit == ProcHook::ANDROID_native_buffer)
739 hook_extensions_.set(ProcHook::KHR_swapchain);
740
741 hal_extensions_.set(ext_bit);
742 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800743
744 break;
745 }
746}
747
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800748VKAPI_ATTR void* DefaultAllocate(void*,
749 size_t size,
750 size_t alignment,
751 VkSystemAllocationScope) {
752 void* ptr = nullptr;
753 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
754 // additionally requires that it be at least sizeof(void*).
755 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
756 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
757 ret, ptr);
758 return ret == 0 ? ptr : nullptr;
759}
760
761VKAPI_ATTR void* DefaultReallocate(void*,
762 void* ptr,
763 size_t size,
764 size_t alignment,
765 VkSystemAllocationScope) {
766 if (size == 0) {
767 free(ptr);
768 return nullptr;
769 }
770
Yiwei Zhanga885c062019-10-24 12:07:57 -0700771 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800772 // request is smaller than the existing chunk, we just continue using it.
773 // Right now the loader never reallocs, so this doesn't matter. If that
774 // changes, or if this code is copied into some other project, this should
775 // probably have a heuristic to allocate-copy-free when doing so will save
776 // "enough" space.
777 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
778 if (size <= old_size)
779 return ptr;
780
781 void* new_ptr = nullptr;
782 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
783 return nullptr;
784 if (ptr) {
785 memcpy(new_ptr, ptr, std::min(old_size, size));
786 free(ptr);
787 }
788 return new_ptr;
789}
790
791VKAPI_ATTR void DefaultFree(void*, void* ptr) {
792 ALOGD_CALLSTACK("Free: %p", ptr);
793 free(ptr);
794}
795
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800796InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
797 void* data_mem = allocator.pfnAllocation(
798 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
799 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
800 if (!data_mem)
801 return nullptr;
802
803 return new (data_mem) InstanceData(allocator);
804}
805
806void FreeInstanceData(InstanceData* data,
807 const VkAllocationCallbacks& allocator) {
808 data->~InstanceData();
809 allocator.pfnFree(allocator.pUserData, data);
810}
811
Chia-I Wu950d6e12016-05-03 09:12:35 +0800812DeviceData* AllocateDeviceData(
813 const VkAllocationCallbacks& allocator,
814 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800815 void* data_mem = allocator.pfnAllocation(
816 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
817 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
818 if (!data_mem)
819 return nullptr;
820
Chia-I Wu950d6e12016-05-03 09:12:35 +0800821 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800822}
823
824void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
825 data->~DeviceData();
826 allocator.pfnFree(allocator.pUserData, data);
827}
828
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800829} // anonymous namespace
830
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800831bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800832 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800833}
834
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800835const VkAllocationCallbacks& GetDefaultAllocator() {
836 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
837 .pUserData = nullptr,
838 .pfnAllocation = DefaultAllocate,
839 .pfnReallocation = DefaultReallocate,
840 .pfnFree = DefaultFree,
841 };
842
843 return kDefaultAllocCallbacks;
844}
845
Chia-I Wueb7db122016-03-24 09:11:06 +0800846PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
847 const ProcHook* hook = GetProcHook(pName);
848 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800849 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800850
851 if (!instance) {
852 if (hook->type == ProcHook::GLOBAL)
853 return hook->proc;
854
Chia-I Wu109f8982016-04-22 06:40:40 +0800855 // v0 layers expect
856 //
857 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
858 //
859 // to work.
860 if (strcmp(pName, "vkCreateDevice") == 0)
861 return hook->proc;
862
Chia-I Wueb7db122016-03-24 09:11:06 +0800863 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800864 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800865 pName);
866
Chia-I Wu109f8982016-04-22 06:40:40 +0800867 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800868 }
869
870 PFN_vkVoidFunction proc;
871
872 switch (hook->type) {
873 case ProcHook::INSTANCE:
874 proc = (GetData(instance).hook_extensions[hook->extension])
875 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800876 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800877 break;
878 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700879 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800880 ? hook->proc
881 : hook->checked_proc;
882 break;
883 default:
884 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800885 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800886 pName);
887 proc = nullptr;
888 break;
889 }
890
891 return proc;
892}
893
894PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
895 const ProcHook* hook = GetProcHook(pName);
896 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800897 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800898
899 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800900 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800901 return nullptr;
902 }
903
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800904 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
905 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800906}
907
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800908VkResult EnumerateInstanceExtensionProperties(
909 const char* pLayerName,
910 uint32_t* pPropertyCount,
911 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700912 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600913 loader_extensions.push_back({
914 VK_KHR_SURFACE_EXTENSION_NAME,
915 VK_KHR_SURFACE_SPEC_VERSION});
916 loader_extensions.push_back({
917 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
918 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
919 loader_extensions.push_back({
920 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
921 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700922 loader_extensions.push_back({
923 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
924 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600925
Chia-I Wu31938252016-05-23 15:31:02 +0800926 static const VkExtensionProperties loader_debug_report_extension = {
927 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
928 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800929
930 // enumerate our extensions first
931 if (!pLayerName && pProperties) {
932 uint32_t count = std::min(
933 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
934
Yiwei Zhang5e862202019-06-21 14:59:16 -0700935 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800936
937 if (count < loader_extensions.size()) {
938 *pPropertyCount = count;
939 return VK_INCOMPLETE;
940 }
941
942 pProperties += count;
943 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800944
945 if (Hal::Get().GetDebugReportIndex() < 0) {
946 if (!*pPropertyCount) {
947 *pPropertyCount = count;
948 return VK_INCOMPLETE;
949 }
950
951 pProperties[0] = loader_debug_report_extension;
952 pProperties += 1;
953 *pPropertyCount -= 1;
954 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800955 }
956
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800957 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800958 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800959 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800960 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800961
Chia-I Wu31938252016-05-23 15:31:02 +0800962 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
963 int idx = Hal::Get().GetDebugReportIndex();
964 if (idx < 0) {
965 *pPropertyCount += 1;
966 } else if (pProperties &&
967 static_cast<uint32_t>(idx) < *pPropertyCount) {
968 pProperties[idx].specVersion =
969 std::min(pProperties[idx].specVersion,
970 loader_debug_report_extension.specVersion);
971 }
972
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800973 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800974 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800975
976 return result;
977}
978
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700979void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +1300980 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700981 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -0700982 ATRACE_CALL();
983
Chris Forbesfa25e632017-02-22 12:36:02 +1300984 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700985 VkPhysicalDeviceProperties2 properties = {
986 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +1300987 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700988 {},
Chris Forbesfa25e632017-02-22 12:36:02 +1300989 };
990
991#pragma clang diagnostic push
992#pragma clang diagnostic ignored "-Wold-style-cast"
993 presentation_properties->sType =
994 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
995#pragma clang diagnostic pop
996 presentation_properties->pNext = nullptr;
997 presentation_properties->sharedImage = VK_FALSE;
998
Chris Forbese056c122021-07-22 13:54:04 -0700999 const auto& driver = GetData(physicalDevice).driver;
1000
1001 if (driver.GetPhysicalDeviceProperties2) {
1002 // >= 1.1 driver, supports core GPDP2 entrypoint.
1003 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1004 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1005 // Old driver, but may support presentation properties
1006 // if we have the GPDP2 extension. Otherwise, no presentation
1007 // properties supported.
1008 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1009 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001010}
1011
Chia-I Wu01cf3052016-03-24 16:16:21 +08001012VkResult EnumerateDeviceExtensionProperties(
1013 VkPhysicalDevice physicalDevice,
1014 const char* pLayerName,
1015 uint32_t* pPropertyCount,
1016 VkExtensionProperties* pProperties) {
1017 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001018 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001019 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001020 loader_extensions.push_back({
1021 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1022 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001023
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001024 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001025 if (hdrBoardConfig) {
1026 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1027 VK_EXT_HDR_METADATA_SPEC_VERSION});
1028 }
1029
Chris Forbes16095002017-05-05 15:33:29 -07001030 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001031 QueryPresentationProperties(physicalDevice, &presentation_properties);
1032 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001033 loader_extensions.push_back({
1034 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1035 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001036 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001037
Ian Elliott5c34de22017-04-10 14:42:30 -06001038 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1039 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001040 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001041 loader_extensions.push_back({
1042 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1043 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1044 }
1045
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001046 // enumerate our extensions first
1047 if (!pLayerName && pProperties) {
1048 uint32_t count = std::min(
1049 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1050
Yiwei Zhang5e862202019-06-21 14:59:16 -07001051 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001052
1053 if (count < loader_extensions.size()) {
1054 *pPropertyCount = count;
1055 return VK_INCOMPLETE;
1056 }
1057
1058 pProperties += count;
1059 *pPropertyCount -= count;
1060 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001061
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001062 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001063 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1064 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001065 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001066
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001067 if (pProperties) {
1068 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1069 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1070 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001071
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001072 if (strcmp(prop.extensionName,
1073 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1074 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001075
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001076 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1077 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001078
1079 if (prop.specVersion >= 8) {
1080 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1081 } else {
1082 prop.specVersion = 68;
1083 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001084 }
1085 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001086
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001087 // restore loader extension count
1088 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1089 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001090 }
1091
1092 return result;
1093}
1094
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001095VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1096 const VkAllocationCallbacks* pAllocator,
1097 VkInstance* pInstance) {
1098 const VkAllocationCallbacks& data_allocator =
1099 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1100
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001101 VkResult result = VK_SUCCESS;
1102 uint32_t icd_api_version = VK_API_VERSION_1_0;
1103 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1104 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1105 Hal::Device().GetInstanceProcAddr(nullptr,
1106 "vkEnumerateInstanceVersion"));
1107 if (pfn_enumerate_instance_version) {
1108 ATRACE_BEGIN("pfn_enumerate_instance_version");
1109 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1110 ATRACE_END();
1111 if (result != VK_SUCCESS)
1112 return result;
1113
1114 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
1115 }
1116
1117 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1118 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001119 if (result != VK_SUCCESS)
1120 return result;
1121
1122 InstanceData* data = AllocateInstanceData(data_allocator);
1123 if (!data)
1124 return VK_ERROR_OUT_OF_HOST_MEMORY;
1125
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001126 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001127
1128 // call into the driver
1129 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001130 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001131 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001132 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1133 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001134 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001135 if (result != VK_SUCCESS) {
1136 FreeInstanceData(data, data_allocator);
1137 return result;
1138 }
1139
1140 // initialize InstanceDriverTable
1141 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001142 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001143 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001144 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001145 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001146 if (data->driver.DestroyInstance)
1147 data->driver.DestroyInstance(instance, pAllocator);
1148
1149 FreeInstanceData(data, data_allocator);
1150
1151 return VK_ERROR_INCOMPATIBLE_DRIVER;
1152 }
1153
1154 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001155 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001156 if (!data->get_device_proc_addr) {
1157 data->driver.DestroyInstance(instance, pAllocator);
1158 FreeInstanceData(data, data_allocator);
1159
1160 return VK_ERROR_INCOMPATIBLE_DRIVER;
1161 }
1162
1163 *pInstance = instance;
1164
1165 return VK_SUCCESS;
1166}
1167
1168void DestroyInstance(VkInstance instance,
1169 const VkAllocationCallbacks* pAllocator) {
1170 InstanceData& data = GetData(instance);
1171 data.driver.DestroyInstance(instance, pAllocator);
1172
1173 VkAllocationCallbacks local_allocator;
1174 if (!pAllocator) {
1175 local_allocator = data.allocator;
1176 pAllocator = &local_allocator;
1177 }
1178
1179 FreeInstanceData(&data, *pAllocator);
1180}
1181
Chia-I Wu4901db72016-03-24 16:38:58 +08001182VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1183 const VkDeviceCreateInfo* pCreateInfo,
1184 const VkAllocationCallbacks* pAllocator,
1185 VkDevice* pDevice) {
1186 const InstanceData& instance_data = GetData(physicalDevice);
1187 const VkAllocationCallbacks& data_allocator =
1188 (pAllocator) ? *pAllocator : instance_data.allocator;
1189
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001190 VkPhysicalDeviceProperties properties;
1191 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1192 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1193 &properties);
1194 ATRACE_END();
1195
1196 CreateInfoWrapper wrapper(
1197 physicalDevice, *pCreateInfo,
1198 properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion),
1199 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001200 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001201 if (result != VK_SUCCESS)
1202 return result;
1203
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001204 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001205 DeviceData* data = AllocateDeviceData(data_allocator,
1206 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001207 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001208 if (!data)
1209 return VK_ERROR_OUT_OF_HOST_MEMORY;
1210
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001211 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001212
1213 // call into the driver
1214 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001215 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001216 result = instance_data.driver.CreateDevice(
1217 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1218 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001219 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001220 if (result != VK_SUCCESS) {
1221 FreeDeviceData(data, data_allocator);
1222 return result;
1223 }
1224
1225 // initialize DeviceDriverTable
1226 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001227 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1228 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001229 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1230 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1231 if (data->driver.DestroyDevice)
1232 data->driver.DestroyDevice(dev, pAllocator);
1233
1234 FreeDeviceData(data, data_allocator);
1235
1236 return VK_ERROR_INCOMPATIBLE_DRIVER;
1237 }
Chris Forbesd8277912017-02-10 14:59:59 +13001238
1239 // sanity check ANDROID_native_buffer implementation, whose set of
1240 // entrypoints varies according to the spec version.
1241 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1242 !data->driver.GetSwapchainGrallocUsageANDROID &&
1243 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1244 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1245 " must expose at least one of "
1246 "vkGetSwapchainGrallocUsageANDROID or "
1247 "vkGetSwapchainGrallocUsage2ANDROID");
1248
1249 data->driver.DestroyDevice(dev, pAllocator);
1250 FreeDeviceData(data, data_allocator);
1251
1252 return VK_ERROR_INCOMPATIBLE_DRIVER;
1253 }
1254
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001255 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1256 // Log that the app is hitting software Vulkan implementation
1257 android::GraphicsEnv::getInstance().setTargetStats(
1258 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1259 }
1260
Jesse Halldc225072016-05-30 22:40:14 -07001261 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001262
1263 *pDevice = dev;
1264
1265 return VK_SUCCESS;
1266}
1267
1268void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1269 DeviceData& data = GetData(device);
1270 data.driver.DestroyDevice(device, pAllocator);
1271
1272 VkAllocationCallbacks local_allocator;
1273 if (!pAllocator) {
1274 local_allocator = data.allocator;
1275 pAllocator = &local_allocator;
1276 }
1277
1278 FreeDeviceData(&data, *pAllocator);
1279}
1280
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001281VkResult EnumeratePhysicalDevices(VkInstance instance,
1282 uint32_t* pPhysicalDeviceCount,
1283 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001284 ATRACE_CALL();
1285
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001286 const auto& data = GetData(instance);
1287
1288 VkResult result = data.driver.EnumeratePhysicalDevices(
1289 instance, pPhysicalDeviceCount, pPhysicalDevices);
1290 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1291 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1292 SetData(pPhysicalDevices[i], data);
1293 }
1294
1295 return result;
1296}
1297
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001298VkResult EnumeratePhysicalDeviceGroups(
1299 VkInstance instance,
1300 uint32_t* pPhysicalDeviceGroupCount,
1301 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001302 ATRACE_CALL();
1303
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001304 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001305 const auto& data = GetData(instance);
1306
Yiwei Zhange4f64172020-07-05 15:17:32 -07001307 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1308 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001309 uint32_t device_count = 0;
1310 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1311 if (result < 0)
1312 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001313
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001314 if (!pPhysicalDeviceGroupProperties) {
1315 *pPhysicalDeviceGroupCount = device_count;
1316 return result;
1317 }
1318
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001319 if (!device_count) {
1320 *pPhysicalDeviceGroupCount = 0;
1321 return result;
1322 }
Chad Versace32c087f2018-09-09 07:28:05 -07001323 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1324 if (!device_count)
1325 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001326
Yiwei Zhang5e862202019-06-21 14:59:16 -07001327 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001328 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001329 result =
1330 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001331 if (result < 0)
1332 return result;
1333
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001334 for (uint32_t i = 0; i < device_count; ++i) {
1335 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1336 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1337 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1338 }
1339 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001340 if (data.driver.EnumeratePhysicalDeviceGroups) {
1341 result = data.driver.EnumeratePhysicalDeviceGroups(
1342 instance, pPhysicalDeviceGroupCount,
1343 pPhysicalDeviceGroupProperties);
1344 } else {
1345 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1346 instance, pPhysicalDeviceGroupCount,
1347 pPhysicalDeviceGroupProperties);
1348 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001349 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1350 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1351 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1352 for (uint32_t j = 0;
1353 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1354 j++) {
1355 SetData(
1356 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001357 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001358 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001359 }
1360 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001361 }
1362
1363 return result;
1364}
1365
Chia-I Wuba0be412016-03-24 16:24:40 +08001366void GetDeviceQueue(VkDevice device,
1367 uint32_t queueFamilyIndex,
1368 uint32_t queueIndex,
1369 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001370 ATRACE_CALL();
1371
Chia-I Wuba0be412016-03-24 16:24:40 +08001372 const auto& data = GetData(device);
1373
1374 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1375 SetData(*pQueue, data);
1376}
1377
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001378void GetDeviceQueue2(VkDevice device,
1379 const VkDeviceQueueInfo2* pQueueInfo,
1380 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001381 ATRACE_CALL();
1382
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001383 const auto& data = GetData(device);
1384
1385 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001386 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001387}
1388
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001389VkResult AllocateCommandBuffers(
1390 VkDevice device,
1391 const VkCommandBufferAllocateInfo* pAllocateInfo,
1392 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001393 ATRACE_CALL();
1394
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001395 const auto& data = GetData(device);
1396
1397 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1398 pCommandBuffers);
1399 if (result == VK_SUCCESS) {
1400 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1401 SetData(pCommandBuffers[i], data);
1402 }
1403
1404 return result;
1405}
1406
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001407VkResult QueueSubmit(VkQueue queue,
1408 uint32_t submitCount,
1409 const VkSubmitInfo* pSubmits,
1410 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001411 ATRACE_CALL();
1412
1413 const auto& data = GetData(queue);
1414
1415 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1416}
1417
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001418void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1419 VkPhysicalDeviceFeatures2* pFeatures) {
1420 ATRACE_CALL();
1421
1422 const auto& driver = GetData(physicalDevice).driver;
1423
1424 if (driver.GetPhysicalDeviceFeatures2) {
1425 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1426 return;
1427 }
1428
1429 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1430}
1431
1432void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1433 VkPhysicalDeviceProperties2* pProperties) {
1434 ATRACE_CALL();
1435
1436 const auto& driver = GetData(physicalDevice).driver;
1437
1438 if (driver.GetPhysicalDeviceProperties2) {
1439 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1440 return;
1441 }
1442
1443 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1444}
1445
1446void GetPhysicalDeviceFormatProperties2(
1447 VkPhysicalDevice physicalDevice,
1448 VkFormat format,
1449 VkFormatProperties2* pFormatProperties) {
1450 ATRACE_CALL();
1451
1452 const auto& driver = GetData(physicalDevice).driver;
1453
1454 if (driver.GetPhysicalDeviceFormatProperties2) {
1455 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1456 pFormatProperties);
1457 return;
1458 }
1459
1460 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1461 pFormatProperties);
1462}
1463
1464VkResult GetPhysicalDeviceImageFormatProperties2(
1465 VkPhysicalDevice physicalDevice,
1466 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1467 VkImageFormatProperties2* pImageFormatProperties) {
1468 ATRACE_CALL();
1469
1470 const auto& driver = GetData(physicalDevice).driver;
1471
1472 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1473 return driver.GetPhysicalDeviceImageFormatProperties2(
1474 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1475 }
1476
1477 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1478 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1479}
1480
1481void GetPhysicalDeviceQueueFamilyProperties2(
1482 VkPhysicalDevice physicalDevice,
1483 uint32_t* pQueueFamilyPropertyCount,
1484 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1485 ATRACE_CALL();
1486
1487 const auto& driver = GetData(physicalDevice).driver;
1488
1489 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1490 driver.GetPhysicalDeviceQueueFamilyProperties2(
1491 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1492 return;
1493 }
1494
1495 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1496 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1497}
1498
1499void GetPhysicalDeviceMemoryProperties2(
1500 VkPhysicalDevice physicalDevice,
1501 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1502 ATRACE_CALL();
1503
1504 const auto& driver = GetData(physicalDevice).driver;
1505
1506 if (driver.GetPhysicalDeviceMemoryProperties2) {
1507 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1508 pMemoryProperties);
1509 return;
1510 }
1511
1512 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1513 pMemoryProperties);
1514}
1515
1516void GetPhysicalDeviceSparseImageFormatProperties2(
1517 VkPhysicalDevice physicalDevice,
1518 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1519 uint32_t* pPropertyCount,
1520 VkSparseImageFormatProperties2* pProperties) {
1521 ATRACE_CALL();
1522
1523 const auto& driver = GetData(physicalDevice).driver;
1524
1525 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1526 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1527 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1528 return;
1529 }
1530
1531 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1532 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1533}
1534
Yiwei Zhange1f35012020-07-05 22:52:04 -07001535void GetPhysicalDeviceExternalBufferProperties(
1536 VkPhysicalDevice physicalDevice,
1537 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1538 VkExternalBufferProperties* pExternalBufferProperties) {
1539 ATRACE_CALL();
1540
1541 const auto& driver = GetData(physicalDevice).driver;
1542
1543 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1544 driver.GetPhysicalDeviceExternalBufferProperties(
1545 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1546 return;
1547 }
1548
1549 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1550 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1551 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1552 return;
1553 }
1554
1555 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1556 sizeof(VkExternalMemoryProperties));
1557}
1558
1559void GetPhysicalDeviceExternalSemaphoreProperties(
1560 VkPhysicalDevice physicalDevice,
1561 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1562 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1563 ATRACE_CALL();
1564
1565 const auto& driver = GetData(physicalDevice).driver;
1566
1567 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1568 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1569 physicalDevice, pExternalSemaphoreInfo,
1570 pExternalSemaphoreProperties);
1571 return;
1572 }
1573
1574 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1575 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1576 physicalDevice, pExternalSemaphoreInfo,
1577 pExternalSemaphoreProperties);
1578 return;
1579 }
1580
1581 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1582 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1583 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1584}
1585
1586void GetPhysicalDeviceExternalFenceProperties(
1587 VkPhysicalDevice physicalDevice,
1588 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1589 VkExternalFenceProperties* pExternalFenceProperties) {
1590 ATRACE_CALL();
1591
1592 const auto& driver = GetData(physicalDevice).driver;
1593
1594 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1595 driver.GetPhysicalDeviceExternalFenceProperties(
1596 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1597 return;
1598 }
1599
1600 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1601 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1602 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1603 return;
1604 }
1605
1606 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1607 pExternalFenceProperties->compatibleHandleTypes = 0;
1608 pExternalFenceProperties->externalFenceFeatures = 0;
1609}
1610
Chia-I Wu9d518162016-03-24 14:55:27 +08001611} // namespace driver
1612} // namespace vulkan