blob: 80727c5b877cf5219c1955711bd20d63c2169daf [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
Mark Salyzyn7823e122016-09-29 08:08:05 -070017#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080018#include <stdlib.h>
19#include <string.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070020#include <sys/prctl.h>
21
Yiwei Zhang170d3702018-01-08 17:55:50 -080022#include <dlfcn.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <algorithm>
Chia-I Wuff4a6c72016-03-24 16:05:56 +080024#include <array>
Chia-I Wu4901db72016-03-24 16:38:58 +080025#include <new>
Mark Salyzyn7823e122016-09-29 08:08:05 -070026
27#include <log/log.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080028
Jesse Hall53457db2016-12-14 16:54:06 -080029#include <android/dlext.h>
30#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Chris Forbesfa25e632017-02-22 12:36:02 +130032#include <utils/Vector.h>
Jesse Hall53457db2016-12-14 16:54:06 -080033
Wei Wangf9b05ee2017-07-19 20:59:39 -070034#include "android-base/properties.h"
35
Chia-I Wu9d518162016-03-24 14:55:27 +080036#include "driver.h"
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070037#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080038
Jesse Hall00e61ff2017-04-07 16:48:02 -070039// TODO(b/37049319) Get this from a header once one exists
40extern "C" {
41android_namespace_t* android_get_exported_namespace(const char*);
42}
43
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080044// #define ENABLE_ALLOC_CALLSTACKS 1
45#if ENABLE_ALLOC_CALLSTACKS
46#include <utils/CallStack.h>
47#define ALOGD_CALLSTACK(...) \
48 do { \
49 ALOGD(__VA_ARGS__); \
50 android::CallStack callstack; \
51 callstack.update(); \
52 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
53 } while (false)
54#else
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 } while (false)
58#endif
59
Chia-I Wu9d518162016-03-24 14:55:27 +080060namespace vulkan {
61namespace driver {
62
Chia-I Wu136b8eb2016-03-24 15:01:52 +080063namespace {
64
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080065class Hal {
66 public:
67 static bool Open();
68
69 static const Hal& Get() { return hal_; }
70 static const hwvulkan_device_t& Device() { return *Get().dev_; }
71
Chia-I Wu31938252016-05-23 15:31:02 +080072 int GetDebugReportIndex() const { return debug_report_index_; }
73
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080074 private:
Chia-I Wu31938252016-05-23 15:31:02 +080075 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080076 Hal(const Hal&) = delete;
77 Hal& operator=(const Hal&) = delete;
78
Chia-I Wu31938252016-05-23 15:31:02 +080079 bool InitDebugReportIndex();
80
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 static Hal hal_;
82
83 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080084 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080085};
86
Chia-I Wu4901db72016-03-24 16:38:58 +080087class CreateInfoWrapper {
88 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080090 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080091 CreateInfoWrapper(VkPhysicalDevice physical_dev,
92 const VkDeviceCreateInfo& create_info,
93 const VkAllocationCallbacks& allocator);
94 ~CreateInfoWrapper();
95
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080096 VkResult Validate();
Ian Elliott1d990b22017-11-02 10:15:13 -060097 void DowngradeApiVersion();
Chia-I Wu4901db72016-03-24 16:38:58 +080098
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080099 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
100 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800101
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800102 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800103 explicit operator const VkDeviceCreateInfo*() const;
104
105 private:
106 struct ExtensionFilter {
107 VkExtensionProperties* exts;
108 uint32_t ext_count;
109
110 const char** names;
111 uint32_t name_count;
112 };
113
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800114 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800115
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800116 VkResult SanitizeLayers();
117 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800118
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800119 VkResult QueryExtensionCount(uint32_t& count) const;
120 VkResult EnumerateExtensions(uint32_t& count,
121 VkExtensionProperties* props) const;
122 VkResult InitExtensionFilter();
123 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800124
125 const bool is_instance_;
126 const VkAllocationCallbacks& allocator_;
127
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800128 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800129
130 union {
131 VkInstanceCreateInfo instance_info_;
132 VkDeviceCreateInfo dev_info_;
133 };
134
Ian Elliott1d990b22017-11-02 10:15:13 -0600135 VkApplicationInfo application_info_;
136
Chia-I Wu4901db72016-03-24 16:38:58 +0800137 ExtensionFilter extension_filter_;
138
139 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
140 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
141};
142
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800143Hal Hal::hal_;
144
Jesse Hall53457db2016-12-14 16:54:06 -0800145void* LoadLibrary(const android_dlextinfo& dlextinfo,
146 const char* subname,
147 int subname_len) {
148 const char kLibFormat[] = "vulkan.%*s.so";
149 char* name = static_cast<char*>(
150 alloca(sizeof(kLibFormat) + static_cast<size_t>(subname_len)));
151 sprintf(name, kLibFormat, subname_len, subname);
152 return android_dlopen_ext(name, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
153}
154
155const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
156 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
157 "ro.board.platform",
158}};
159
Jesse Hall00e61ff2017-04-07 16:48:02 -0700160int LoadDriver(android_namespace_t* library_namespace,
161 const hwvulkan_module_t** module) {
Jesse Hall53457db2016-12-14 16:54:06 -0800162 const android_dlextinfo dlextinfo = {
163 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700164 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800165 };
Jesse Hall53457db2016-12-14 16:54:06 -0800166 void* so = nullptr;
167 char prop[PROPERTY_VALUE_MAX];
168 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
169 int prop_len = property_get(key, prop, nullptr);
170 if (prop_len > 0) {
171 so = LoadLibrary(dlextinfo, prop, prop_len);
172 if (so)
173 break;
174 }
175 }
176 if (!so)
177 return -ENOENT;
178
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800180 if (!hmi) {
181 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
182 dlclose(so);
183 return -EINVAL;
184 }
185 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
186 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
187 dlclose(so);
188 return -EINVAL;
189 }
190 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700191 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800192 return 0;
193}
194
Jesse Hall00e61ff2017-04-07 16:48:02 -0700195int LoadBuiltinDriver(const hwvulkan_module_t** module) {
196 auto ns = android_get_exported_namespace("sphal");
197 if (!ns)
198 return -ENOENT;
199 return LoadDriver(ns, module);
200}
201
202int LoadUpdatedDriver(const hwvulkan_module_t** module) {
203 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
204 if (!ns)
205 return -ENOENT;
206 return LoadDriver(ns, module);
207}
208
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800209bool Hal::Open() {
Jesse Halldc225072016-05-30 22:40:14 -0700210 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800211
212 // Use a stub device unless we successfully open a real HAL device.
213 hal_.dev_ = &stubhal::kDevice;
214
Jesse Hall53457db2016-12-14 16:54:06 -0800215 int result;
216 const hwvulkan_module_t* module = nullptr;
217
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800219 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700220 result = LoadBuiltinDriver(&module);
221 if (result != 0) {
222 // -ENOENT means the sphal namespace doesn't exist, not that there
223 // is a problem with the driver.
224 ALOGW_IF(
225 result != -ENOENT,
226 "Failed to load Vulkan driver into sphal namespace. This "
227 "usually means the driver has forbidden library dependencies."
228 "Please fix, this will soon stop working.");
229 result =
230 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
231 reinterpret_cast<const hw_module_t**>(&module));
232 }
Jesse Hall53457db2016-12-14 16:54:06 -0800233 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800234 if (result != 0) {
Jesse Hall53457db2016-12-14 16:54:06 -0800235 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800236 return true;
237 }
238
239 hwvulkan_device_t* device;
240 result =
241 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
242 reinterpret_cast<hw_device_t**>(&device));
243 if (result != 0) {
244 // Any device with a Vulkan HAL should be able to open the device.
245 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
246 result);
247 return false;
248 }
249
250 hal_.dev_ = device;
251
Chia-I Wu31938252016-05-23 15:31:02 +0800252 hal_.InitDebugReportIndex();
253
254 return true;
255}
256
257bool Hal::InitDebugReportIndex() {
258 uint32_t count;
259 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
260 VK_SUCCESS) {
261 ALOGE("failed to get HAL instance extension count");
262 return false;
263 }
264
265 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
266 malloc(sizeof(VkExtensionProperties) * count));
267 if (!exts) {
268 ALOGE("failed to allocate HAL instance extension array");
269 return false;
270 }
271
272 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
273 VK_SUCCESS) {
274 ALOGE("failed to enumerate HAL instance extensions");
275 free(exts);
276 return false;
277 }
278
279 for (uint32_t i = 0; i < count; i++) {
280 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
281 0) {
282 debug_report_index_ = static_cast<int>(i);
283 break;
284 }
285 }
286
287 free(exts);
288
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800289 return true;
290}
291
292CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800293 const VkAllocationCallbacks& allocator)
294 : is_instance_(true),
295 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800296 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800297 instance_info_(create_info),
298 extension_filter_() {
299 hook_extensions_.set(ProcHook::EXTENSION_CORE);
300 hal_extensions_.set(ProcHook::EXTENSION_CORE);
301}
302
Chia-I Wu4901db72016-03-24 16:38:58 +0800303CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
304 const VkDeviceCreateInfo& create_info,
305 const VkAllocationCallbacks& allocator)
306 : is_instance_(false),
307 allocator_(allocator),
308 physical_dev_(physical_dev),
309 dev_info_(create_info),
310 extension_filter_() {
311 hook_extensions_.set(ProcHook::EXTENSION_CORE);
312 hal_extensions_.set(ProcHook::EXTENSION_CORE);
313}
314
315CreateInfoWrapper::~CreateInfoWrapper() {
316 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
317 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
318}
319
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800320VkResult CreateInfoWrapper::Validate() {
321 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800322 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800323 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800324 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800325 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800326
327 return result;
328}
329
330const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800331CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800332 return hook_extensions_;
333}
334
335const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800336CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800337 return hal_extensions_;
338}
339
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800340CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
341 return &instance_info_;
342}
343
Chia-I Wu4901db72016-03-24 16:38:58 +0800344CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
345 return &dev_info_;
346}
347
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800348VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800349 const struct StructHeader {
350 VkStructureType type;
351 const void* next;
352 } * header;
353
354 if (is_instance_) {
355 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
356
357 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
358 while (header &&
359 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
360 header = reinterpret_cast<const StructHeader*>(header->next);
361
362 instance_info_.pNext = header;
363 } else {
364 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
365
366 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
367 while (header &&
368 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
369 header = reinterpret_cast<const StructHeader*>(header->next);
370
371 dev_info_.pNext = header;
372 }
373
374 return VK_SUCCESS;
375}
376
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800377VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800378 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
379 : dev_info_.ppEnabledLayerNames;
380 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
381 : dev_info_.enabledLayerCount;
382
383 // remove all layers
384 layer_names = nullptr;
385 layer_count = 0;
386
387 return VK_SUCCESS;
388}
389
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800390VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800391 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
392 : dev_info_.ppEnabledExtensionNames;
393 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
394 : dev_info_.enabledExtensionCount;
395 if (!ext_count)
396 return VK_SUCCESS;
397
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800398 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800399 if (result != VK_SUCCESS)
400 return result;
401
402 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800403 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800404
Jesse Hall9f5c65d2018-03-05 13:34:45 -0800405 // Enable device extensions that contain physical-device commands, so that
406 // vkGetInstanceProcAddr will return those physical-device commands.
407 if (is_instance_) {
408 hook_extensions_.set(ProcHook::KHR_swapchain);
409 }
410
Chia-I Wu4901db72016-03-24 16:38:58 +0800411 ext_names = extension_filter_.names;
412 ext_count = extension_filter_.name_count;
413
414 return VK_SUCCESS;
415}
416
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800417VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800418 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800419 return Hal::Device().EnumerateInstanceExtensionProperties(
420 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800421 } else {
422 const auto& driver = GetData(physical_dev_).driver;
423 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
424 &count, nullptr);
425 }
426}
427
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800428VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800429 uint32_t& count,
430 VkExtensionProperties* props) const {
431 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800432 return Hal::Device().EnumerateInstanceExtensionProperties(
433 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800434 } else {
435 const auto& driver = GetData(physical_dev_).driver;
436 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
437 &count, props);
438 }
439}
440
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800441VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800442 // query extension count
443 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800444 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800445 if (result != VK_SUCCESS || count == 0)
446 return result;
447
448 auto& filter = extension_filter_;
449 filter.exts =
450 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
451 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
452 alignof(VkExtensionProperties),
453 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
454 if (!filter.exts)
455 return VK_ERROR_OUT_OF_HOST_MEMORY;
456
457 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800458 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800459 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
460 return result;
461
462 if (!count)
463 return VK_SUCCESS;
464
465 filter.ext_count = count;
466
467 // allocate name array
468 uint32_t enabled_ext_count = (is_instance_)
469 ? instance_info_.enabledExtensionCount
470 : dev_info_.enabledExtensionCount;
471 count = std::min(filter.ext_count, enabled_ext_count);
472 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
473 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
474 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
475 if (!filter.names)
476 return VK_ERROR_OUT_OF_HOST_MEMORY;
477
478 return VK_SUCCESS;
479}
480
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800481void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800482 auto& filter = extension_filter_;
483
484 ProcHook::Extension ext_bit = GetProcHookExtension(name);
485 if (is_instance_) {
486 switch (ext_bit) {
487 case ProcHook::KHR_android_surface:
488 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700489 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300490 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800491 hook_extensions_.set(ext_bit);
492 // return now as these extensions do not require HAL support
493 return;
494 case ProcHook::EXT_debug_report:
495 // both we and HAL can take part in
496 hook_extensions_.set(ext_bit);
497 break;
498 case ProcHook::EXTENSION_UNKNOWN:
Chris Forbes6aa30db2017-02-20 17:12:53 +1300499 case ProcHook::KHR_get_physical_device_properties2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800500 // HAL's extensions
501 break;
502 default:
503 ALOGW("Ignored invalid instance extension %s", name);
504 return;
505 }
506 } else {
507 switch (ext_bit) {
508 case ProcHook::KHR_swapchain:
509 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
510 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
511 ext_bit = ProcHook::ANDROID_native_buffer;
512 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700513 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700514 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300515 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700516 hook_extensions_.set(ext_bit);
517 // return now as these extensions do not require HAL support
518 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700519 case ProcHook::EXT_hdr_metadata:
520 hook_extensions_.set(ext_bit);
521 break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800522 case ProcHook::EXTENSION_UNKNOWN:
523 // HAL's extensions
524 break;
525 default:
526 ALOGW("Ignored invalid device extension %s", name);
527 return;
528 }
529 }
530
531 for (uint32_t i = 0; i < filter.ext_count; i++) {
532 const VkExtensionProperties& props = filter.exts[i];
533 // ignore unknown extensions
534 if (strcmp(name, props.extensionName) != 0)
535 continue;
536
Chia-I Wu4901db72016-03-24 16:38:58 +0800537 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800538 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
539 if (ext_bit == ProcHook::ANDROID_native_buffer)
540 hook_extensions_.set(ProcHook::KHR_swapchain);
541
542 hal_extensions_.set(ext_bit);
543 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800544
545 break;
546 }
547}
548
Ian Elliott1d990b22017-11-02 10:15:13 -0600549void CreateInfoWrapper::DowngradeApiVersion() {
550 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
551 if (instance_info_.pApplicationInfo) {
552 application_info_ = *instance_info_.pApplicationInfo;
553 instance_info_.pApplicationInfo = &application_info_;
554 application_info_.apiVersion = VK_API_VERSION_1_0;
555 }
556}
557
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800558VKAPI_ATTR void* DefaultAllocate(void*,
559 size_t size,
560 size_t alignment,
561 VkSystemAllocationScope) {
562 void* ptr = nullptr;
563 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
564 // additionally requires that it be at least sizeof(void*).
565 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
566 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
567 ret, ptr);
568 return ret == 0 ? ptr : nullptr;
569}
570
571VKAPI_ATTR void* DefaultReallocate(void*,
572 void* ptr,
573 size_t size,
574 size_t alignment,
575 VkSystemAllocationScope) {
576 if (size == 0) {
577 free(ptr);
578 return nullptr;
579 }
580
581 // TODO(jessehall): Right now we never shrink allocations; if the new
582 // request is smaller than the existing chunk, we just continue using it.
583 // Right now the loader never reallocs, so this doesn't matter. If that
584 // changes, or if this code is copied into some other project, this should
585 // probably have a heuristic to allocate-copy-free when doing so will save
586 // "enough" space.
587 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
588 if (size <= old_size)
589 return ptr;
590
591 void* new_ptr = nullptr;
592 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
593 return nullptr;
594 if (ptr) {
595 memcpy(new_ptr, ptr, std::min(old_size, size));
596 free(ptr);
597 }
598 return new_ptr;
599}
600
601VKAPI_ATTR void DefaultFree(void*, void* ptr) {
602 ALOGD_CALLSTACK("Free: %p", ptr);
603 free(ptr);
604}
605
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800606InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
607 void* data_mem = allocator.pfnAllocation(
608 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
609 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
610 if (!data_mem)
611 return nullptr;
612
613 return new (data_mem) InstanceData(allocator);
614}
615
616void FreeInstanceData(InstanceData* data,
617 const VkAllocationCallbacks& allocator) {
618 data->~InstanceData();
619 allocator.pfnFree(allocator.pUserData, data);
620}
621
Chia-I Wu950d6e12016-05-03 09:12:35 +0800622DeviceData* AllocateDeviceData(
623 const VkAllocationCallbacks& allocator,
624 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800625 void* data_mem = allocator.pfnAllocation(
626 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
627 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
628 if (!data_mem)
629 return nullptr;
630
Chia-I Wu950d6e12016-05-03 09:12:35 +0800631 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800632}
633
634void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
635 data->~DeviceData();
636 allocator.pfnFree(allocator.pUserData, data);
637}
638
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800639} // anonymous namespace
640
Chia-I Wu9d518162016-03-24 14:55:27 +0800641bool Debuggable() {
642 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
643}
644
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800645bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800646 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800647}
648
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800649const VkAllocationCallbacks& GetDefaultAllocator() {
650 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
651 .pUserData = nullptr,
652 .pfnAllocation = DefaultAllocate,
653 .pfnReallocation = DefaultReallocate,
654 .pfnFree = DefaultFree,
655 };
656
657 return kDefaultAllocCallbacks;
658}
659
Chia-I Wueb7db122016-03-24 09:11:06 +0800660PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
661 const ProcHook* hook = GetProcHook(pName);
662 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800663 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800664
665 if (!instance) {
666 if (hook->type == ProcHook::GLOBAL)
667 return hook->proc;
668
Chia-I Wu109f8982016-04-22 06:40:40 +0800669 // v0 layers expect
670 //
671 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
672 //
673 // to work.
674 if (strcmp(pName, "vkCreateDevice") == 0)
675 return hook->proc;
676
Chia-I Wueb7db122016-03-24 09:11:06 +0800677 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800678 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800679 pName);
680
Chia-I Wu109f8982016-04-22 06:40:40 +0800681 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800682 }
683
684 PFN_vkVoidFunction proc;
685
686 switch (hook->type) {
687 case ProcHook::INSTANCE:
688 proc = (GetData(instance).hook_extensions[hook->extension])
689 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800690 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800691 break;
692 case ProcHook::DEVICE:
693 proc = (hook->extension == ProcHook::EXTENSION_CORE)
694 ? hook->proc
695 : hook->checked_proc;
696 break;
697 default:
698 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800699 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800700 pName);
701 proc = nullptr;
702 break;
703 }
704
705 return proc;
706}
707
708PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
709 const ProcHook* hook = GetProcHook(pName);
710 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800711 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800712
713 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800714 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800715 return nullptr;
716 }
717
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800718 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
719 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800720}
721
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800722VkResult EnumerateInstanceExtensionProperties(
723 const char* pLayerName,
724 uint32_t* pPropertyCount,
725 VkExtensionProperties* pProperties) {
Ian Elliott34a327b2017-03-28 13:20:35 -0600726
727 android::Vector<VkExtensionProperties> loader_extensions;
728 loader_extensions.push_back({
729 VK_KHR_SURFACE_EXTENSION_NAME,
730 VK_KHR_SURFACE_SPEC_VERSION});
731 loader_extensions.push_back({
732 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
733 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
734 loader_extensions.push_back({
735 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
736 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700737 loader_extensions.push_back({
738 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
739 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600740
Chia-I Wu31938252016-05-23 15:31:02 +0800741 static const VkExtensionProperties loader_debug_report_extension = {
742 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
743 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800744
745 // enumerate our extensions first
746 if (!pLayerName && pProperties) {
747 uint32_t count = std::min(
748 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
749
750 std::copy_n(loader_extensions.begin(), count, pProperties);
751
752 if (count < loader_extensions.size()) {
753 *pPropertyCount = count;
754 return VK_INCOMPLETE;
755 }
756
757 pProperties += count;
758 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800759
760 if (Hal::Get().GetDebugReportIndex() < 0) {
761 if (!*pPropertyCount) {
762 *pPropertyCount = count;
763 return VK_INCOMPLETE;
764 }
765
766 pProperties[0] = loader_debug_report_extension;
767 pProperties += 1;
768 *pPropertyCount -= 1;
769 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800770 }
771
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800772 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800773 pLayerName, pPropertyCount, pProperties);
774
Chia-I Wu31938252016-05-23 15:31:02 +0800775 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
776 int idx = Hal::Get().GetDebugReportIndex();
777 if (idx < 0) {
778 *pPropertyCount += 1;
779 } else if (pProperties &&
780 static_cast<uint32_t>(idx) < *pPropertyCount) {
781 pProperties[idx].specVersion =
782 std::min(pProperties[idx].specVersion,
783 loader_debug_report_extension.specVersion);
784 }
785
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800786 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800787 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800788
789 return result;
790}
791
Chris Forbesfa25e632017-02-22 12:36:02 +1300792bool QueryPresentationProperties(
793 VkPhysicalDevice physicalDevice,
794 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties)
795{
796 const InstanceData& data = GetData(physicalDevice);
797
798 // GPDP2 must be present and enabled on the instance.
799 if (!data.driver.GetPhysicalDeviceProperties2KHR)
800 return false;
801
802 // Request the android-specific presentation properties via GPDP2
803 VkPhysicalDeviceProperties2KHR properties = {
804 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
805 presentation_properties,
806 {}
807 };
808
809#pragma clang diagnostic push
810#pragma clang diagnostic ignored "-Wold-style-cast"
811 presentation_properties->sType =
812 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
813#pragma clang diagnostic pop
814 presentation_properties->pNext = nullptr;
815 presentation_properties->sharedImage = VK_FALSE;
816
817 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
818 &properties);
819
820 return true;
821}
822
Chia-I Wu01cf3052016-03-24 16:16:21 +0800823VkResult EnumerateDeviceExtensionProperties(
824 VkPhysicalDevice physicalDevice,
825 const char* pLayerName,
826 uint32_t* pPropertyCount,
827 VkExtensionProperties* pProperties) {
828 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300829 // extensions that are unconditionally exposed by the loader
830 android::Vector<VkExtensionProperties> loader_extensions;
831 loader_extensions.push_back({
832 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
833 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300834
Chris Forbes16095002017-05-05 15:33:29 -0700835 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
836 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
837 presentation_properties.sharedImage) {
838 loader_extensions.push_back({
839 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
840 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300841 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700842
Ian Elliott5c34de22017-04-10 14:42:30 -0600843 // conditionally add VK_GOOGLE_display_timing if present timestamps are
844 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700845 const std::string timestamp_property("service.sf.present_timestamp");
846 android::base::WaitForPropertyCreation(timestamp_property);
847 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600848 loader_extensions.push_back({
849 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
850 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
851 }
852
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700853 // enumerate our extensions first
854 if (!pLayerName && pProperties) {
855 uint32_t count = std::min(
856 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
857
858 std::copy_n(loader_extensions.begin(), count, pProperties);
859
860 if (count < loader_extensions.size()) {
861 *pPropertyCount = count;
862 return VK_INCOMPLETE;
863 }
864
865 pProperties += count;
866 *pPropertyCount -= count;
867 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800868
869 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
870 physicalDevice, pLayerName, pPropertyCount, pProperties);
Chia-I Wu01cf3052016-03-24 16:16:21 +0800871
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700872 if (pProperties) {
873 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
874 for (uint32_t i = 0; i < *pPropertyCount; i++) {
875 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800876
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700877 if (strcmp(prop.extensionName,
878 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
879 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +0800880
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700881 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
882 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
883 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
884 }
885 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800886
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700887 // restore loader extension count
888 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
889 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800890 }
891
892 return result;
893}
894
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800895VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
896 const VkAllocationCallbacks* pAllocator,
897 VkInstance* pInstance) {
898 const VkAllocationCallbacks& data_allocator =
899 (pAllocator) ? *pAllocator : GetDefaultAllocator();
900
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800901 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800902 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800903 if (result != VK_SUCCESS)
904 return result;
905
906 InstanceData* data = AllocateInstanceData(data_allocator);
907 if (!data)
908 return VK_ERROR_OUT_OF_HOST_MEMORY;
909
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800910 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800911
Ian Elliott1d990b22017-11-02 10:15:13 -0600912#pragma clang diagnostic push
913#pragma clang diagnostic ignored "-Wold-style-cast"
914 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
915 ? pCreateInfo->pApplicationInfo->apiVersion
916 : VK_API_VERSION_1_0);
917 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
918 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
919 uint32_t icd_api_version;
920 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
921 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
922 Hal::Device().GetInstanceProcAddr(NULL,
923 "vkEnumerateInstanceVersion"));
924 if (!pfn_enumerate_instance_version) {
925 icd_api_version = VK_API_VERSION_1_0;
926 } else {
927 result = (*pfn_enumerate_instance_version)(&icd_api_version);
928 }
929 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
930 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
931
932 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
933 ((api_major_version > 1) || (api_minor_version > 0))) {
934 api_version = VK_API_VERSION_1_0;
935 wrapper.DowngradeApiVersion();
936 }
937#pragma clang diagnostic pop
938
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800939 // call into the driver
940 VkInstance instance;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800941 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800942 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
943 &instance);
944 if (result != VK_SUCCESS) {
945 FreeInstanceData(data, data_allocator);
946 return result;
947 }
948
949 // initialize InstanceDriverTable
950 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800951 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +0800952 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800953 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800954 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800955 if (data->driver.DestroyInstance)
956 data->driver.DestroyInstance(instance, pAllocator);
957
958 FreeInstanceData(data, data_allocator);
959
960 return VK_ERROR_INCOMPATIBLE_DRIVER;
961 }
962
963 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800964 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800965 if (!data->get_device_proc_addr) {
966 data->driver.DestroyInstance(instance, pAllocator);
967 FreeInstanceData(data, data_allocator);
968
969 return VK_ERROR_INCOMPATIBLE_DRIVER;
970 }
971
972 *pInstance = instance;
973
974 return VK_SUCCESS;
975}
976
977void DestroyInstance(VkInstance instance,
978 const VkAllocationCallbacks* pAllocator) {
979 InstanceData& data = GetData(instance);
980 data.driver.DestroyInstance(instance, pAllocator);
981
982 VkAllocationCallbacks local_allocator;
983 if (!pAllocator) {
984 local_allocator = data.allocator;
985 pAllocator = &local_allocator;
986 }
987
988 FreeInstanceData(&data, *pAllocator);
989}
990
Chia-I Wu4901db72016-03-24 16:38:58 +0800991VkResult CreateDevice(VkPhysicalDevice physicalDevice,
992 const VkDeviceCreateInfo* pCreateInfo,
993 const VkAllocationCallbacks* pAllocator,
994 VkDevice* pDevice) {
995 const InstanceData& instance_data = GetData(physicalDevice);
996 const VkAllocationCallbacks& data_allocator =
997 (pAllocator) ? *pAllocator : instance_data.allocator;
998
999 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001000 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001001 if (result != VK_SUCCESS)
1002 return result;
1003
Chia-I Wu950d6e12016-05-03 09:12:35 +08001004 DeviceData* data = AllocateDeviceData(data_allocator,
1005 instance_data.debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +08001006 if (!data)
1007 return VK_ERROR_OUT_OF_HOST_MEMORY;
1008
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001009 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001010
1011 // call into the driver
1012 VkDevice dev;
1013 result = instance_data.driver.CreateDevice(
1014 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1015 pAllocator, &dev);
1016 if (result != VK_SUCCESS) {
1017 FreeDeviceData(data, data_allocator);
1018 return result;
1019 }
1020
1021 // initialize DeviceDriverTable
1022 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001023 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1024 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001025 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1026 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1027 if (data->driver.DestroyDevice)
1028 data->driver.DestroyDevice(dev, pAllocator);
1029
1030 FreeDeviceData(data, data_allocator);
1031
1032 return VK_ERROR_INCOMPATIBLE_DRIVER;
1033 }
Chris Forbesd8277912017-02-10 14:59:59 +13001034
1035 // sanity check ANDROID_native_buffer implementation, whose set of
1036 // entrypoints varies according to the spec version.
1037 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1038 !data->driver.GetSwapchainGrallocUsageANDROID &&
1039 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1040 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1041 " must expose at least one of "
1042 "vkGetSwapchainGrallocUsageANDROID or "
1043 "vkGetSwapchainGrallocUsage2ANDROID");
1044
1045 data->driver.DestroyDevice(dev, pAllocator);
1046 FreeDeviceData(data, data_allocator);
1047
1048 return VK_ERROR_INCOMPATIBLE_DRIVER;
1049 }
1050
Jesse Hall85bb0c52017-02-09 22:13:02 -08001051 VkPhysicalDeviceProperties properties;
1052 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1053 &properties);
1054
Jesse Halldc225072016-05-30 22:40:14 -07001055 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001056 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001057
1058 *pDevice = dev;
1059
1060 return VK_SUCCESS;
1061}
1062
1063void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1064 DeviceData& data = GetData(device);
1065 data.driver.DestroyDevice(device, pAllocator);
1066
1067 VkAllocationCallbacks local_allocator;
1068 if (!pAllocator) {
1069 local_allocator = data.allocator;
1070 pAllocator = &local_allocator;
1071 }
1072
1073 FreeDeviceData(&data, *pAllocator);
1074}
1075
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001076VkResult EnumeratePhysicalDevices(VkInstance instance,
1077 uint32_t* pPhysicalDeviceCount,
1078 VkPhysicalDevice* pPhysicalDevices) {
1079 const auto& data = GetData(instance);
1080
1081 VkResult result = data.driver.EnumeratePhysicalDevices(
1082 instance, pPhysicalDeviceCount, pPhysicalDevices);
1083 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1084 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1085 SetData(pPhysicalDevices[i], data);
1086 }
1087
1088 return result;
1089}
1090
Daniel Koch09f7bf92017-10-05 00:26:58 -04001091VkResult EnumeratePhysicalDeviceGroups(
1092 VkInstance instance,
1093 uint32_t* pPhysicalDeviceGroupCount,
1094 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhang170d3702018-01-08 17:55:50 -08001095 VkResult result = VK_SUCCESS;
Daniel Koch09f7bf92017-10-05 00:26:58 -04001096 const auto& data = GetData(instance);
1097
Yiwei Zhang170d3702018-01-08 17:55:50 -08001098 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1099 uint32_t device_count = 0;
1100 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1101 if (result < 0)
1102 return result;
1103 if (!pPhysicalDeviceGroupProperties) {
1104 *pPhysicalDeviceGroupCount = device_count;
1105 return result;
1106 }
1107
1108 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1109 if (!device_count) {
1110 *pPhysicalDeviceGroupCount = 0;
1111 return result;
1112 }
1113
1114 android::Vector<VkPhysicalDevice> devices;
1115 devices.resize(device_count);
1116
1117 result = EnumeratePhysicalDevices(instance, &device_count,
1118 devices.editArray());
1119 if (result < 0)
1120 return result;
1121
1122 devices.resize(device_count);
1123 *pPhysicalDeviceGroupCount = device_count;
1124 for (uint32_t i = 0; i < device_count; ++i) {
1125 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1126 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1127 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1128 }
1129 } else {
1130 result = data.driver.EnumeratePhysicalDeviceGroups(
1131 instance, pPhysicalDeviceGroupCount,
1132 pPhysicalDeviceGroupProperties);
1133 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1134 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1135 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1136 for (uint32_t j = 0;
1137 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1138 j++) {
1139 SetData(
1140 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliott28bd2c32017-10-13 09:21:12 -06001141 data);
Yiwei Zhang170d3702018-01-08 17:55:50 -08001142 }
Ian Elliott28bd2c32017-10-13 09:21:12 -06001143 }
1144 }
Daniel Koch09f7bf92017-10-05 00:26:58 -04001145 }
1146
1147 return result;
1148}
1149
Chia-I Wuba0be412016-03-24 16:24:40 +08001150void GetDeviceQueue(VkDevice device,
1151 uint32_t queueFamilyIndex,
1152 uint32_t queueIndex,
1153 VkQueue* pQueue) {
1154 const auto& data = GetData(device);
1155
1156 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1157 SetData(*pQueue, data);
1158}
1159
Daniel Koch09f7bf92017-10-05 00:26:58 -04001160void GetDeviceQueue2(VkDevice device,
1161 const VkDeviceQueueInfo2* pQueueInfo,
1162 VkQueue* pQueue) {
1163 const auto& data = GetData(device);
1164
1165 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhang5f646a02018-02-02 12:49:28 -08001166 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Koch09f7bf92017-10-05 00:26:58 -04001167}
1168
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001169VKAPI_ATTR VkResult
1170AllocateCommandBuffers(VkDevice device,
1171 const VkCommandBufferAllocateInfo* pAllocateInfo,
1172 VkCommandBuffer* pCommandBuffers) {
1173 const auto& data = GetData(device);
1174
1175 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1176 pCommandBuffers);
1177 if (result == VK_SUCCESS) {
1178 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1179 SetData(pCommandBuffers[i], data);
1180 }
1181
1182 return result;
1183}
1184
Chia-I Wu9d518162016-03-24 14:55:27 +08001185} // namespace driver
1186} // namespace vulkan