blob: 238429f9577fc252534bdb6c7810ea4a5e5e8d35 [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),
Ian Elliottfa7af492021-07-20 17:40:24 -0600368 loader_api_version_(VK_API_VERSION_1_2),
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),
Ian Elliottfa7af492021-07-20 17:40:24 -0600380 loader_api_version_(VK_API_VERSION_1_2),
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) {
Ian Elliottfa7af492021-07-20 17:40:24 -0600522 case VK_API_VERSION_1_2:
523 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
524 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
525 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700526 case VK_API_VERSION_1_1:
527 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
528 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
529 [[clang::fallthrough]];
530 case VK_API_VERSION_1_0:
531 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
532 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
533 break;
534 default:
535 ALOGE("Unknown API version[%u]", api_version);
536 break;
537 }
538
Chia-I Wu4901db72016-03-24 16:38:58 +0800539 ext_names = extension_filter_.names;
540 ext_count = extension_filter_.name_count;
541
542 return VK_SUCCESS;
543}
544
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800545VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800546 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800547 return Hal::Device().EnumerateInstanceExtensionProperties(
548 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800549 } else {
550 const auto& driver = GetData(physical_dev_).driver;
551 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
552 &count, nullptr);
553 }
554}
555
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800556VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800557 uint32_t& count,
558 VkExtensionProperties* props) const {
559 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800560 return Hal::Device().EnumerateInstanceExtensionProperties(
561 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800562 } else {
563 const auto& driver = GetData(physical_dev_).driver;
564 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
565 &count, props);
566 }
567}
568
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800569VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800570 // query extension count
571 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800572 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800573 if (result != VK_SUCCESS || count == 0)
574 return result;
575
576 auto& filter = extension_filter_;
577 filter.exts =
578 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
579 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
580 alignof(VkExtensionProperties),
581 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
582 if (!filter.exts)
583 return VK_ERROR_OUT_OF_HOST_MEMORY;
584
585 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800586 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800587 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
588 return result;
589
590 if (!count)
591 return VK_SUCCESS;
592
593 filter.ext_count = count;
594
595 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700596 if (is_instance_) {
597 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
598
599 // It requires enabling additional promoted extensions to downgrade api,
600 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700601 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700602 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700603 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700604 }
605
606 count = std::min(filter.ext_count, enabled_ext_count);
607 } else {
608 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
609 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700610
611 if (!count)
612 return VK_SUCCESS;
613
Chia-I Wu4901db72016-03-24 16:38:58 +0800614 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
615 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
616 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
617 if (!filter.names)
618 return VK_ERROR_OUT_OF_HOST_MEMORY;
619
620 return VK_SUCCESS;
621}
622
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800623void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800624 auto& filter = extension_filter_;
625
626 ProcHook::Extension ext_bit = GetProcHookExtension(name);
627 if (is_instance_) {
628 switch (ext_bit) {
629 case ProcHook::KHR_android_surface:
630 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700631 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300632 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800633 hook_extensions_.set(ext_bit);
634 // return now as these extensions do not require HAL support
635 return;
636 case ProcHook::EXT_debug_report:
637 // both we and HAL can take part in
638 hook_extensions_.set(ext_bit);
639 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300640 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700641 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700642 case ProcHook::KHR_external_memory_capabilities:
643 case ProcHook::KHR_external_semaphore_capabilities:
644 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700645 case ProcHook::EXTENSION_UNKNOWN:
646 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800647 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700648
Yiwei Zhang23143102019-04-10 18:24:05 -0700649 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700650 case ProcHook::KHR_incremental_present:
651 case ProcHook::KHR_shared_presentable_image:
652 case ProcHook::KHR_swapchain:
653 case ProcHook::EXT_hdr_metadata:
654 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
655 case ProcHook::ANDROID_native_buffer:
656 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700657 case ProcHook::EXTENSION_CORE_1_0:
658 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700659 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000660 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700661 case ProcHook::EXTENSION_COUNT:
662 // Device and meta extensions. If we ever get here it's a bug in
663 // our code. But enumerating them lets us avoid having a default
664 // case, and default hides other bugs.
665 ALOGE(
666 "CreateInfoWrapper::FilterExtension: invalid instance "
667 "extension '%s'. FIX ME",
668 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800669 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700670
671 // Don't use a default case. Without it, -Wswitch will tell us
672 // at compile time if someone adds a new ProcHook extension but
673 // doesn't handle it above. That's a real bug that has
674 // not-immediately-obvious effects.
675 //
676 // default:
677 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800678 }
679 } else {
680 switch (ext_bit) {
681 case ProcHook::KHR_swapchain:
682 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
683 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
684 ext_bit = ProcHook::ANDROID_native_buffer;
685 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700686 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700687 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300688 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700689 hook_extensions_.set(ext_bit);
690 // return now as these extensions do not require HAL support
691 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700692 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700693 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700694 hook_extensions_.set(ext_bit);
695 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700696 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800697 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700698 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800699 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700700
701 case ProcHook::KHR_android_surface:
702 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700703 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700704 case ProcHook::KHR_external_memory_capabilities:
705 case ProcHook::KHR_external_semaphore_capabilities:
706 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700707 case ProcHook::KHR_get_surface_capabilities2:
708 case ProcHook::KHR_surface:
709 case ProcHook::EXT_debug_report:
710 case ProcHook::EXT_swapchain_colorspace:
711 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700712 case ProcHook::EXTENSION_CORE_1_0:
713 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700714 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000715 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700716 case ProcHook::EXTENSION_COUNT:
717 // Instance and meta extensions. If we ever get here it's a bug
718 // in our code. But enumerating them lets us avoid having a
719 // default case, and default hides other bugs.
720 ALOGE(
721 "CreateInfoWrapper::FilterExtension: invalid device "
722 "extension '%s'. FIX ME",
723 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800724 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700725
726 // Don't use a default case. Without it, -Wswitch will tell us
727 // at compile time if someone adds a new ProcHook extension but
728 // doesn't handle it above. That's a real bug that has
729 // not-immediately-obvious effects.
730 //
731 // default:
732 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800733 }
734 }
735
736 for (uint32_t i = 0; i < filter.ext_count; i++) {
737 const VkExtensionProperties& props = filter.exts[i];
738 // ignore unknown extensions
739 if (strcmp(name, props.extensionName) != 0)
740 continue;
741
Chia-I Wu4901db72016-03-24 16:38:58 +0800742 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800743 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
744 if (ext_bit == ProcHook::ANDROID_native_buffer)
745 hook_extensions_.set(ProcHook::KHR_swapchain);
746
747 hal_extensions_.set(ext_bit);
748 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800749
750 break;
751 }
752}
753
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800754VKAPI_ATTR void* DefaultAllocate(void*,
755 size_t size,
756 size_t alignment,
757 VkSystemAllocationScope) {
758 void* ptr = nullptr;
759 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
760 // additionally requires that it be at least sizeof(void*).
761 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
762 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
763 ret, ptr);
764 return ret == 0 ? ptr : nullptr;
765}
766
767VKAPI_ATTR void* DefaultReallocate(void*,
768 void* ptr,
769 size_t size,
770 size_t alignment,
771 VkSystemAllocationScope) {
772 if (size == 0) {
773 free(ptr);
774 return nullptr;
775 }
776
Yiwei Zhanga885c062019-10-24 12:07:57 -0700777 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800778 // request is smaller than the existing chunk, we just continue using it.
779 // Right now the loader never reallocs, so this doesn't matter. If that
780 // changes, or if this code is copied into some other project, this should
781 // probably have a heuristic to allocate-copy-free when doing so will save
782 // "enough" space.
783 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
784 if (size <= old_size)
785 return ptr;
786
787 void* new_ptr = nullptr;
788 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
789 return nullptr;
790 if (ptr) {
791 memcpy(new_ptr, ptr, std::min(old_size, size));
792 free(ptr);
793 }
794 return new_ptr;
795}
796
797VKAPI_ATTR void DefaultFree(void*, void* ptr) {
798 ALOGD_CALLSTACK("Free: %p", ptr);
799 free(ptr);
800}
801
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800802InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
803 void* data_mem = allocator.pfnAllocation(
804 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
805 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
806 if (!data_mem)
807 return nullptr;
808
809 return new (data_mem) InstanceData(allocator);
810}
811
812void FreeInstanceData(InstanceData* data,
813 const VkAllocationCallbacks& allocator) {
814 data->~InstanceData();
815 allocator.pfnFree(allocator.pUserData, data);
816}
817
Chia-I Wu950d6e12016-05-03 09:12:35 +0800818DeviceData* AllocateDeviceData(
819 const VkAllocationCallbacks& allocator,
820 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800821 void* data_mem = allocator.pfnAllocation(
822 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
823 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
824 if (!data_mem)
825 return nullptr;
826
Chia-I Wu950d6e12016-05-03 09:12:35 +0800827 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800828}
829
830void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
831 data->~DeviceData();
832 allocator.pfnFree(allocator.pUserData, data);
833}
834
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800835} // anonymous namespace
836
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800837bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800838 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800839}
840
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800841const VkAllocationCallbacks& GetDefaultAllocator() {
842 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
843 .pUserData = nullptr,
844 .pfnAllocation = DefaultAllocate,
845 .pfnReallocation = DefaultReallocate,
846 .pfnFree = DefaultFree,
847 };
848
849 return kDefaultAllocCallbacks;
850}
851
Chia-I Wueb7db122016-03-24 09:11:06 +0800852PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
853 const ProcHook* hook = GetProcHook(pName);
854 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800855 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800856
857 if (!instance) {
858 if (hook->type == ProcHook::GLOBAL)
859 return hook->proc;
860
Chia-I Wu109f8982016-04-22 06:40:40 +0800861 // v0 layers expect
862 //
863 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
864 //
865 // to work.
866 if (strcmp(pName, "vkCreateDevice") == 0)
867 return hook->proc;
868
Chia-I Wueb7db122016-03-24 09:11:06 +0800869 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800870 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800871 pName);
872
Chia-I Wu109f8982016-04-22 06:40:40 +0800873 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800874 }
875
876 PFN_vkVoidFunction proc;
877
878 switch (hook->type) {
879 case ProcHook::INSTANCE:
880 proc = (GetData(instance).hook_extensions[hook->extension])
881 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800882 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800883 break;
884 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700885 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800886 ? hook->proc
887 : hook->checked_proc;
888 break;
889 default:
890 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800891 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800892 pName);
893 proc = nullptr;
894 break;
895 }
896
897 return proc;
898}
899
900PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
901 const ProcHook* hook = GetProcHook(pName);
902 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800903 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800904
905 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800906 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800907 return nullptr;
908 }
909
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800910 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
911 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800912}
913
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800914VkResult EnumerateInstanceExtensionProperties(
915 const char* pLayerName,
916 uint32_t* pPropertyCount,
917 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700918 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600919 loader_extensions.push_back({
920 VK_KHR_SURFACE_EXTENSION_NAME,
921 VK_KHR_SURFACE_SPEC_VERSION});
922 loader_extensions.push_back({
923 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
924 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
925 loader_extensions.push_back({
926 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
927 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700928 loader_extensions.push_back({
929 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
930 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600931
Chia-I Wu31938252016-05-23 15:31:02 +0800932 static const VkExtensionProperties loader_debug_report_extension = {
933 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
934 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800935
936 // enumerate our extensions first
937 if (!pLayerName && pProperties) {
938 uint32_t count = std::min(
939 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
940
Yiwei Zhang5e862202019-06-21 14:59:16 -0700941 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800942
943 if (count < loader_extensions.size()) {
944 *pPropertyCount = count;
945 return VK_INCOMPLETE;
946 }
947
948 pProperties += count;
949 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800950
951 if (Hal::Get().GetDebugReportIndex() < 0) {
952 if (!*pPropertyCount) {
953 *pPropertyCount = count;
954 return VK_INCOMPLETE;
955 }
956
957 pProperties[0] = loader_debug_report_extension;
958 pProperties += 1;
959 *pPropertyCount -= 1;
960 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800961 }
962
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800963 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800964 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800965 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800966 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800967
Chia-I Wu31938252016-05-23 15:31:02 +0800968 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
969 int idx = Hal::Get().GetDebugReportIndex();
970 if (idx < 0) {
971 *pPropertyCount += 1;
972 } else if (pProperties &&
973 static_cast<uint32_t>(idx) < *pPropertyCount) {
974 pProperties[idx].specVersion =
975 std::min(pProperties[idx].specVersion,
976 loader_debug_report_extension.specVersion);
977 }
978
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800979 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800980 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800981
982 return result;
983}
984
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700985void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +1300986 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700987 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -0700988 ATRACE_CALL();
989
Chris Forbesfa25e632017-02-22 12:36:02 +1300990 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700991 VkPhysicalDeviceProperties2 properties = {
992 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +1300993 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700994 {},
Chris Forbesfa25e632017-02-22 12:36:02 +1300995 };
996
997#pragma clang diagnostic push
998#pragma clang diagnostic ignored "-Wold-style-cast"
999 presentation_properties->sType =
1000 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1001#pragma clang diagnostic pop
1002 presentation_properties->pNext = nullptr;
1003 presentation_properties->sharedImage = VK_FALSE;
1004
Chris Forbese056c122021-07-22 13:54:04 -07001005 const auto& driver = GetData(physicalDevice).driver;
1006
1007 if (driver.GetPhysicalDeviceProperties2) {
1008 // >= 1.1 driver, supports core GPDP2 entrypoint.
1009 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1010 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1011 // Old driver, but may support presentation properties
1012 // if we have the GPDP2 extension. Otherwise, no presentation
1013 // properties supported.
1014 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1015 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001016}
1017
Chia-I Wu01cf3052016-03-24 16:16:21 +08001018VkResult EnumerateDeviceExtensionProperties(
1019 VkPhysicalDevice physicalDevice,
1020 const char* pLayerName,
1021 uint32_t* pPropertyCount,
1022 VkExtensionProperties* pProperties) {
1023 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001024 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001025 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001026 loader_extensions.push_back({
1027 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1028 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001029
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001030 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001031 if (hdrBoardConfig) {
1032 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1033 VK_EXT_HDR_METADATA_SPEC_VERSION});
1034 }
1035
Chris Forbes16095002017-05-05 15:33:29 -07001036 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001037 QueryPresentationProperties(physicalDevice, &presentation_properties);
1038 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001039 loader_extensions.push_back({
1040 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1041 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001042 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001043
Ian Elliott5c34de22017-04-10 14:42:30 -06001044 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1045 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001046 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001047 loader_extensions.push_back({
1048 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1049 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1050 }
1051
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001052 // enumerate our extensions first
1053 if (!pLayerName && pProperties) {
1054 uint32_t count = std::min(
1055 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1056
Yiwei Zhang5e862202019-06-21 14:59:16 -07001057 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001058
1059 if (count < loader_extensions.size()) {
1060 *pPropertyCount = count;
1061 return VK_INCOMPLETE;
1062 }
1063
1064 pProperties += count;
1065 *pPropertyCount -= count;
1066 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001067
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001068 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001069 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1070 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001071 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001072
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001073 if (pProperties) {
1074 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1075 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1076 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001077
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001078 if (strcmp(prop.extensionName,
1079 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1080 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001081
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001082 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1083 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001084
1085 if (prop.specVersion >= 8) {
1086 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1087 } else {
1088 prop.specVersion = 68;
1089 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001090 }
1091 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001092
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001093 // restore loader extension count
1094 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1095 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001096 }
1097
1098 return result;
1099}
1100
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001101VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1102 const VkAllocationCallbacks* pAllocator,
1103 VkInstance* pInstance) {
1104 const VkAllocationCallbacks& data_allocator =
1105 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1106
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001107 VkResult result = VK_SUCCESS;
1108 uint32_t icd_api_version = VK_API_VERSION_1_0;
1109 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1110 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1111 Hal::Device().GetInstanceProcAddr(nullptr,
1112 "vkEnumerateInstanceVersion"));
1113 if (pfn_enumerate_instance_version) {
1114 ATRACE_BEGIN("pfn_enumerate_instance_version");
1115 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1116 ATRACE_END();
1117 if (result != VK_SUCCESS)
1118 return result;
1119
Trevor David Blackb68a2252021-08-23 16:37:18 +00001120 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001121 }
1122
1123 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1124 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001125 if (result != VK_SUCCESS)
1126 return result;
1127
1128 InstanceData* data = AllocateInstanceData(data_allocator);
1129 if (!data)
1130 return VK_ERROR_OUT_OF_HOST_MEMORY;
1131
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001132 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001133
1134 // call into the driver
1135 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001136 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001137 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001138 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1139 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001140 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001141 if (result != VK_SUCCESS) {
1142 FreeInstanceData(data, data_allocator);
1143 return result;
1144 }
1145
1146 // initialize InstanceDriverTable
1147 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001148 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001149 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001150 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001151 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001152 if (data->driver.DestroyInstance)
1153 data->driver.DestroyInstance(instance, pAllocator);
1154
1155 FreeInstanceData(data, data_allocator);
1156
1157 return VK_ERROR_INCOMPATIBLE_DRIVER;
1158 }
1159
1160 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001161 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001162 if (!data->get_device_proc_addr) {
1163 data->driver.DestroyInstance(instance, pAllocator);
1164 FreeInstanceData(data, data_allocator);
1165
1166 return VK_ERROR_INCOMPATIBLE_DRIVER;
1167 }
1168
1169 *pInstance = instance;
1170
1171 return VK_SUCCESS;
1172}
1173
1174void DestroyInstance(VkInstance instance,
1175 const VkAllocationCallbacks* pAllocator) {
1176 InstanceData& data = GetData(instance);
1177 data.driver.DestroyInstance(instance, pAllocator);
1178
1179 VkAllocationCallbacks local_allocator;
1180 if (!pAllocator) {
1181 local_allocator = data.allocator;
1182 pAllocator = &local_allocator;
1183 }
1184
1185 FreeInstanceData(&data, *pAllocator);
1186}
1187
Chia-I Wu4901db72016-03-24 16:38:58 +08001188VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1189 const VkDeviceCreateInfo* pCreateInfo,
1190 const VkAllocationCallbacks* pAllocator,
1191 VkDevice* pDevice) {
1192 const InstanceData& instance_data = GetData(physicalDevice);
1193 const VkAllocationCallbacks& data_allocator =
1194 (pAllocator) ? *pAllocator : instance_data.allocator;
1195
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001196 VkPhysicalDeviceProperties properties;
1197 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1198 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1199 &properties);
1200 ATRACE_END();
1201
1202 CreateInfoWrapper wrapper(
1203 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001204 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001205 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001206 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001207 if (result != VK_SUCCESS)
1208 return result;
1209
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001210 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001211 DeviceData* data = AllocateDeviceData(data_allocator,
1212 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001213 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001214 if (!data)
1215 return VK_ERROR_OUT_OF_HOST_MEMORY;
1216
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001217 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001218
1219 // call into the driver
1220 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001221 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001222 result = instance_data.driver.CreateDevice(
1223 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1224 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001225 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001226 if (result != VK_SUCCESS) {
1227 FreeDeviceData(data, data_allocator);
1228 return result;
1229 }
1230
1231 // initialize DeviceDriverTable
1232 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001233 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1234 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001235 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1236 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1237 if (data->driver.DestroyDevice)
1238 data->driver.DestroyDevice(dev, pAllocator);
1239
1240 FreeDeviceData(data, data_allocator);
1241
1242 return VK_ERROR_INCOMPATIBLE_DRIVER;
1243 }
Chris Forbesd8277912017-02-10 14:59:59 +13001244
1245 // sanity check ANDROID_native_buffer implementation, whose set of
1246 // entrypoints varies according to the spec version.
1247 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1248 !data->driver.GetSwapchainGrallocUsageANDROID &&
1249 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1250 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1251 " must expose at least one of "
1252 "vkGetSwapchainGrallocUsageANDROID or "
1253 "vkGetSwapchainGrallocUsage2ANDROID");
1254
1255 data->driver.DestroyDevice(dev, pAllocator);
1256 FreeDeviceData(data, data_allocator);
1257
1258 return VK_ERROR_INCOMPATIBLE_DRIVER;
1259 }
1260
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001261 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1262 // Log that the app is hitting software Vulkan implementation
1263 android::GraphicsEnv::getInstance().setTargetStats(
1264 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1265 }
1266
Jesse Halldc225072016-05-30 22:40:14 -07001267 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001268
1269 *pDevice = dev;
1270
1271 return VK_SUCCESS;
1272}
1273
1274void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1275 DeviceData& data = GetData(device);
1276 data.driver.DestroyDevice(device, pAllocator);
1277
1278 VkAllocationCallbacks local_allocator;
1279 if (!pAllocator) {
1280 local_allocator = data.allocator;
1281 pAllocator = &local_allocator;
1282 }
1283
1284 FreeDeviceData(&data, *pAllocator);
1285}
1286
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001287VkResult EnumeratePhysicalDevices(VkInstance instance,
1288 uint32_t* pPhysicalDeviceCount,
1289 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001290 ATRACE_CALL();
1291
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001292 const auto& data = GetData(instance);
1293
1294 VkResult result = data.driver.EnumeratePhysicalDevices(
1295 instance, pPhysicalDeviceCount, pPhysicalDevices);
1296 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1297 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1298 SetData(pPhysicalDevices[i], data);
1299 }
1300
1301 return result;
1302}
1303
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001304VkResult EnumeratePhysicalDeviceGroups(
1305 VkInstance instance,
1306 uint32_t* pPhysicalDeviceGroupCount,
1307 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001308 ATRACE_CALL();
1309
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001310 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001311 const auto& data = GetData(instance);
1312
Yiwei Zhange4f64172020-07-05 15:17:32 -07001313 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1314 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001315 uint32_t device_count = 0;
1316 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1317 if (result < 0)
1318 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001319
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001320 if (!pPhysicalDeviceGroupProperties) {
1321 *pPhysicalDeviceGroupCount = device_count;
1322 return result;
1323 }
1324
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001325 if (!device_count) {
1326 *pPhysicalDeviceGroupCount = 0;
1327 return result;
1328 }
Chad Versace32c087f2018-09-09 07:28:05 -07001329 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1330 if (!device_count)
1331 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001332
Yiwei Zhang5e862202019-06-21 14:59:16 -07001333 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001334 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001335 result =
1336 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001337 if (result < 0)
1338 return result;
1339
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001340 for (uint32_t i = 0; i < device_count; ++i) {
1341 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1342 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1343 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1344 }
1345 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001346 if (data.driver.EnumeratePhysicalDeviceGroups) {
1347 result = data.driver.EnumeratePhysicalDeviceGroups(
1348 instance, pPhysicalDeviceGroupCount,
1349 pPhysicalDeviceGroupProperties);
1350 } else {
1351 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1352 instance, pPhysicalDeviceGroupCount,
1353 pPhysicalDeviceGroupProperties);
1354 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001355 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1356 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1357 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1358 for (uint32_t j = 0;
1359 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1360 j++) {
1361 SetData(
1362 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001363 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001364 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001365 }
1366 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001367 }
1368
1369 return result;
1370}
1371
Chia-I Wuba0be412016-03-24 16:24:40 +08001372void GetDeviceQueue(VkDevice device,
1373 uint32_t queueFamilyIndex,
1374 uint32_t queueIndex,
1375 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001376 ATRACE_CALL();
1377
Chia-I Wuba0be412016-03-24 16:24:40 +08001378 const auto& data = GetData(device);
1379
1380 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1381 SetData(*pQueue, data);
1382}
1383
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001384void GetDeviceQueue2(VkDevice device,
1385 const VkDeviceQueueInfo2* pQueueInfo,
1386 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001387 ATRACE_CALL();
1388
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001389 const auto& data = GetData(device);
1390
1391 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001392 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001393}
1394
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001395VkResult AllocateCommandBuffers(
1396 VkDevice device,
1397 const VkCommandBufferAllocateInfo* pAllocateInfo,
1398 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001399 ATRACE_CALL();
1400
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001401 const auto& data = GetData(device);
1402
1403 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1404 pCommandBuffers);
1405 if (result == VK_SUCCESS) {
1406 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1407 SetData(pCommandBuffers[i], data);
1408 }
1409
1410 return result;
1411}
1412
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001413VkResult QueueSubmit(VkQueue queue,
1414 uint32_t submitCount,
1415 const VkSubmitInfo* pSubmits,
1416 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001417 ATRACE_CALL();
1418
1419 const auto& data = GetData(queue);
1420
1421 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1422}
1423
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001424void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1425 VkPhysicalDeviceFeatures2* pFeatures) {
1426 ATRACE_CALL();
1427
1428 const auto& driver = GetData(physicalDevice).driver;
1429
1430 if (driver.GetPhysicalDeviceFeatures2) {
1431 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1432 return;
1433 }
1434
1435 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1436}
1437
1438void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1439 VkPhysicalDeviceProperties2* pProperties) {
1440 ATRACE_CALL();
1441
1442 const auto& driver = GetData(physicalDevice).driver;
1443
1444 if (driver.GetPhysicalDeviceProperties2) {
1445 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1446 return;
1447 }
1448
1449 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1450}
1451
1452void GetPhysicalDeviceFormatProperties2(
1453 VkPhysicalDevice physicalDevice,
1454 VkFormat format,
1455 VkFormatProperties2* pFormatProperties) {
1456 ATRACE_CALL();
1457
1458 const auto& driver = GetData(physicalDevice).driver;
1459
1460 if (driver.GetPhysicalDeviceFormatProperties2) {
1461 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1462 pFormatProperties);
1463 return;
1464 }
1465
1466 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1467 pFormatProperties);
1468}
1469
1470VkResult GetPhysicalDeviceImageFormatProperties2(
1471 VkPhysicalDevice physicalDevice,
1472 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1473 VkImageFormatProperties2* pImageFormatProperties) {
1474 ATRACE_CALL();
1475
1476 const auto& driver = GetData(physicalDevice).driver;
1477
1478 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1479 return driver.GetPhysicalDeviceImageFormatProperties2(
1480 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1481 }
1482
1483 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1484 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1485}
1486
1487void GetPhysicalDeviceQueueFamilyProperties2(
1488 VkPhysicalDevice physicalDevice,
1489 uint32_t* pQueueFamilyPropertyCount,
1490 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1491 ATRACE_CALL();
1492
1493 const auto& driver = GetData(physicalDevice).driver;
1494
1495 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1496 driver.GetPhysicalDeviceQueueFamilyProperties2(
1497 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1498 return;
1499 }
1500
1501 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1502 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1503}
1504
1505void GetPhysicalDeviceMemoryProperties2(
1506 VkPhysicalDevice physicalDevice,
1507 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1508 ATRACE_CALL();
1509
1510 const auto& driver = GetData(physicalDevice).driver;
1511
1512 if (driver.GetPhysicalDeviceMemoryProperties2) {
1513 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1514 pMemoryProperties);
1515 return;
1516 }
1517
1518 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1519 pMemoryProperties);
1520}
1521
1522void GetPhysicalDeviceSparseImageFormatProperties2(
1523 VkPhysicalDevice physicalDevice,
1524 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1525 uint32_t* pPropertyCount,
1526 VkSparseImageFormatProperties2* pProperties) {
1527 ATRACE_CALL();
1528
1529 const auto& driver = GetData(physicalDevice).driver;
1530
1531 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1532 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1533 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1534 return;
1535 }
1536
1537 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1538 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1539}
1540
Yiwei Zhange1f35012020-07-05 22:52:04 -07001541void GetPhysicalDeviceExternalBufferProperties(
1542 VkPhysicalDevice physicalDevice,
1543 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1544 VkExternalBufferProperties* pExternalBufferProperties) {
1545 ATRACE_CALL();
1546
1547 const auto& driver = GetData(physicalDevice).driver;
1548
1549 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1550 driver.GetPhysicalDeviceExternalBufferProperties(
1551 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1552 return;
1553 }
1554
1555 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1556 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1557 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1558 return;
1559 }
1560
1561 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1562 sizeof(VkExternalMemoryProperties));
1563}
1564
1565void GetPhysicalDeviceExternalSemaphoreProperties(
1566 VkPhysicalDevice physicalDevice,
1567 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1568 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1569 ATRACE_CALL();
1570
1571 const auto& driver = GetData(physicalDevice).driver;
1572
1573 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1574 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1575 physicalDevice, pExternalSemaphoreInfo,
1576 pExternalSemaphoreProperties);
1577 return;
1578 }
1579
1580 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1581 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1582 physicalDevice, pExternalSemaphoreInfo,
1583 pExternalSemaphoreProperties);
1584 return;
1585 }
1586
1587 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1588 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1589 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1590}
1591
1592void GetPhysicalDeviceExternalFenceProperties(
1593 VkPhysicalDevice physicalDevice,
1594 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1595 VkExternalFenceProperties* pExternalFenceProperties) {
1596 ATRACE_CALL();
1597
1598 const auto& driver = GetData(physicalDevice).driver;
1599
1600 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1601 driver.GetPhysicalDeviceExternalFenceProperties(
1602 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1603 return;
1604 }
1605
1606 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1607 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1608 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1609 return;
1610 }
1611
1612 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1613 pExternalFenceProperties->compatibleHandleTypes = 0;
1614 pExternalFenceProperties->externalFenceFeatures = 0;
1615}
1616
Chia-I Wu9d518162016-03-24 14:55:27 +08001617} // namespace driver
1618} // namespace vulkan