Debian Codegraph @

src/gfxstream/guest/vulkan/gfxstream_vk_device.cpp

raw
1 /*
2 * Copyright 2023 Google LLC
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <errno.h>
7 #include <string.h>
8
9 #include "GfxStreamConnectionManager.h"
10 #include "GfxStreamRenderControl.h"
11 #include "GfxStreamVulkanConnection.h"
12 #include "ResourceTracker.h"
13 #include "VkEncoder.h"
14 #include "gfxstream_vk_entrypoints.h"
15 #include "gfxstream_vk_private.h"
16 #include "util/detect_os.h"
17 #include "util/perf/cpu_trace.h"
18 #include "vk_common_entrypoints.h"
19 #include "vk_sync_dummy.h"
20 #include "vk_util.h"
21
22 uint32_t gSeqno = 0;
23 uint32_t gNoRenderControlEnc = 0;
24
25 static gfxstream::vk::VkEncoder* getVulkanEncoder(GfxStreamConnectionManager* mgr) {
26 if (!gNoRenderControlEnc) {
27 int32_t ret = renderControlInit(mgr, nullptr);
28 if (ret) {
29 mesa_loge("Failed to initialize renderControl when getting VK encoder");
30 return nullptr;
31 }
32 }
33
34 gfxstream::vk::VkEncoder* vkEncoder =
35 (gfxstream::vk::VkEncoder*)mgr->getEncoder(GFXSTREAM_CONNECTION_VULKAN);
36
37 if (vkEncoder == nullptr) {
38 auto stream = mgr->getStream();
39 int32_t ret = mgr->addConnection(GFXSTREAM_CONNECTION_VULKAN,
40 std::make_unique<GfxStreamVulkanConnection>(stream));
41 if (ret) {
42 return nullptr;
43 }
44
45 vkEncoder = (gfxstream::vk::VkEncoder*)mgr->getEncoder(GFXSTREAM_CONNECTION_VULKAN);
46 }
47
48 return vkEncoder;
49 }
50
51 static GfxStreamConnectionManager* getConnectionManager(void) {
52 auto transport = renderControlGetTransport();
53 return GfxStreamConnectionManager::getThreadLocalInstance(transport, kCapsetGfxStreamVulkan);
54 }
55
56 namespace {
57
58 static bool instance_extension_table_initialized = false;
59 static struct vk_instance_extension_table gfxstream_vk_instance_extensions_supported = {};
60
61 // Always provided by guest driver only; never encoded/decoded to/from host
62 static const char* const kGuestEmulatedInstanceExtensions[] = {
63 VK_KHR_SURFACE_EXTENSION_NAME,
64 #if defined(GFXSTREAM_VK_WAYLAND)
65 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
66 #endif
67 #if defined(GFXSTREAM_VK_X11)
68 VK_KHR_XCB_SURFACE_EXTENSION_NAME,
69 #endif
70 VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
71 };
72
73 static bool isGuestEmulatedInstanceExtension(const char* name) {
74 for (auto mesaExt : kGuestEmulatedInstanceExtensions) {
75 if (!strncmp(mesaExt, name, VK_MAX_EXTENSION_NAME_SIZE)) return true;
76 }
77 return false;
78 }
79
80 static VkResult SetupInstanceForProcess(void) {
81 auto mgr = getConnectionManager();
82 if (!mgr) {
83 mesa_logd("vulkan: Failed to get host connection\n");
84 return VK_ERROR_INITIALIZATION_FAILED;
85 }
86
87 gfxstream::vk::ResourceTracker::get()->setupCaps(gNoRenderControlEnc);
88 gfxstream::vk::ResourceTracker::get()->setupPlatformHelpers();
89 // Legacy goldfish path: could be deleted once goldfish not used guest-side.
90 if (!gNoRenderControlEnc) {
91 struct GfxStreamVkFeatureInfo features = {};
92 int32_t ret = renderControlInit(mgr, &features);
93 if (ret) {
94 mesa_loge("Failed to initialize renderControl ");
95 return VK_ERROR_DEVICE_LOST;
96 }
97
98 gfxstream::vk::ResourceTracker::get()->setupFeatures(&features);
99 }
100
101 gfxstream::vk::ResourceTracker::get()->setThreadingCallbacks({
102 .hostConnectionGetFunc = getConnectionManager,
103 .vkEncoderGetFunc = getVulkanEncoder,
104 });
105 gfxstream::vk::ResourceTracker::get()->setSeqnoPtr(&gSeqno);
106 gfxstream::vk::VkEncoder* vkEnc = getVulkanEncoder(mgr);
107 if (!vkEnc) {
108 mesa_loge("vulkan: Failed to get Vulkan encoder\n");
109 return VK_ERROR_DEVICE_LOST;
110 }
111
112 return VK_SUCCESS;
113 }
114
115 // Filtered extension names for encoding
116 static std::vector<const char*> filteredInstanceExtensionNames(uint32_t count,
117 const char* const* extNames) {
118 std::vector<const char*> retList;
119 for (uint32_t i = 0; i < count; ++i) {
120 auto extName = extNames[i];
121 if (!isGuestEmulatedInstanceExtension(extName)) {
122 retList.push_back(extName);
123 }
124 }
125 return retList;
126 }
127
128 // Always provided by guest driver only; never encoded/decoded to/from host
129 static const char* const kGuestEmulatedDeviceExtensions[] = {
130 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
131 VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
132 VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
133 };
134
135 static bool isGuestEmulatedDeviceExtension(const char* name) {
136 for (auto mesaExt : kGuestEmulatedDeviceExtensions) {
137 if (!strncmp(mesaExt, name, VK_MAX_EXTENSION_NAME_SIZE)) return true;
138 }
139 return false;
140 }
141
142 static std::vector<const char*> filteredDeviceExtensionNames(
143 gfxstream_vk_physical_device* physical_device, uint32_t count, const char* const* extNames) {
144 std::vector<const char*> retList;
145 for (uint32_t i = 0; i < count; ++i) {
146 auto extName = extNames[i];
147 // VK_EXT_image_drm_format_modifier
148 if (!strncmp(extName, VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
149 VK_MAX_EXTENSION_NAME_SIZE)) {
150 if (physical_device->doImageDrmFormatModifierEmulation) {
151 // If emulated, drop this exension from the filtered list
152 } else {
153 retList.push_back(extName);
154 }
155 } else if (!isGuestEmulatedDeviceExtension(extName)) {
156 retList.push_back(extName);
157 }
158 }
159 return retList;
160 }
161
162 static void get_device_extensions(VkPhysicalDevice physDevInternal,
163 struct vk_device_extension_table* deviceExts) {
164 VkResult result = (VkResult)0;
165 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
166 auto resources = gfxstream::vk::ResourceTracker::get();
167 uint32_t numDeviceExts = 0;
168 result = resources->on_vkEnumerateDeviceExtensionProperties(vkEnc, VK_SUCCESS, physDevInternal,
169 NULL, &numDeviceExts, NULL);
170 if (result == VK_SUCCESS) {
171 std::vector<VkExtensionProperties> extProps(numDeviceExts);
172 result = resources->on_vkEnumerateDeviceExtensionProperties(
173 vkEnc, VK_SUCCESS, physDevInternal, NULL, &numDeviceExts, extProps.data());
174 if (result == VK_SUCCESS) {
175 // Enable device extensions from the host's physical device
176 for (uint32_t i = 0; i < numDeviceExts; i++) {
177 for (uint32_t j = 0; j < VK_DEVICE_EXTENSION_COUNT; j++) {
178 if (0 == strncmp(extProps[i].extensionName,
179 vk_device_extensions[j].extensionName,
180 VK_MAX_EXTENSION_NAME_SIZE)) {
181 deviceExts->extensions[j] = true;
182 break;
183 }
184 }
185 }
186 // Make sure all guest-emulated device extensions are enabled
187 for (uint32_t j = 0; j < VK_DEVICE_EXTENSION_COUNT; j++) {
188 if (isGuestEmulatedDeviceExtension(vk_device_extensions[j].extensionName)) {
189 deviceExts->extensions[j] = true;
190 }
191 }
192 }
193 }
194 }
195
196 static VkResult gfxstream_vk_physical_device_init(
197 struct gfxstream_vk_physical_device* physical_device, struct gfxstream_vk_instance* instance,
198 VkPhysicalDevice internal_object) {
199 struct vk_device_extension_table supported_extensions = {};
200 get_device_extensions(internal_object, &supported_extensions);
201
202 // VK_EXT_image_drm_format_modifier support is either emulated, or passthrough using
203 // host functionality
204 if (!supported_extensions.EXT_image_drm_format_modifier) {
205 physical_device->doImageDrmFormatModifierEmulation = true;
206 supported_extensions.EXT_image_drm_format_modifier = true;
207 } else {
208 physical_device->doImageDrmFormatModifierEmulation = false;
209 }
210
211 struct vk_physical_device_dispatch_table dispatch_table;
212 memset(&dispatch_table, 0, sizeof(struct vk_physical_device_dispatch_table));
213 vk_physical_device_dispatch_table_from_entrypoints(
214 &dispatch_table, &gfxstream_vk_physical_device_entrypoints, false);
215 #if !DETECT_OS_FUCHSIA
216 vk_physical_device_dispatch_table_from_entrypoints(&dispatch_table,
217 &wsi_physical_device_entrypoints, false);
218 #endif
219
220 // Initialize the mesa object
221 VkResult result = vk_physical_device_init(&physical_device->vk, &instance->vk,
222 &supported_extensions, NULL, NULL, &dispatch_table);
223
224 if (result == VK_SUCCESS) {
225 // Set the gfxstream-internal object
226 physical_device->internal_object = internal_object;
227 physical_device->instance = instance;
228 // Note: Must use dummy_sync for correct sync object path in WSI operations
229 physical_device->sync_types[0] = &vk_sync_dummy_type;
230 physical_device->sync_types[1] = NULL;
231 physical_device->vk.supported_sync_types = physical_device->sync_types;
232
233 result = gfxstream_vk_wsi_init(physical_device);
234 }
235
236 return result;
237 }
238
239 static void gfxstream_vk_physical_device_finish(
240 struct gfxstream_vk_physical_device* physical_device) {
241 gfxstream_vk_wsi_finish(physical_device);
242
243 vk_physical_device_finish(&physical_device->vk);
244 }
245
246 static void gfxstream_vk_destroy_physical_device(struct vk_physical_device* physical_device) {
247 gfxstream_vk_physical_device_finish((struct gfxstream_vk_physical_device*)physical_device);
248 vk_free(&physical_device->instance->alloc, physical_device);
249 }
250
251 static VkResult gfxstream_vk_enumerate_devices(struct vk_instance* vk_instance) {
252 VkResult result = VK_SUCCESS;
253 gfxstream_vk_instance* gfxstream_instance = (gfxstream_vk_instance*)vk_instance;
254
255 if (gfxstream_instance->init_failed) {
256 return VK_SUCCESS;
257 }
258
259 uint32_t deviceCount = 0;
260 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
261 auto resources = gfxstream::vk::ResourceTracker::get();
262 result = resources->on_vkEnumeratePhysicalDevices(
263 vkEnc, VK_SUCCESS, gfxstream_instance->internal_object, &deviceCount, NULL);
264 if (result != VK_SUCCESS) return result;
265 std::vector<VkPhysicalDevice> internal_list(deviceCount);
266 result = resources->on_vkEnumeratePhysicalDevices(
267 vkEnc, VK_SUCCESS, gfxstream_instance->internal_object, &deviceCount, internal_list.data());
268
269 if (result == VK_SUCCESS) {
270 for (uint32_t i = 0; i < deviceCount; i++) {
271 struct gfxstream_vk_physical_device* gfxstream_physicalDevice =
272 (struct gfxstream_vk_physical_device*)vk_zalloc(
273 &gfxstream_instance->vk.alloc, sizeof(struct gfxstream_vk_physical_device), GFXSTREAM_DEFAULT_ALIGN,
274 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
275 if (!gfxstream_physicalDevice) {
276 result = VK_ERROR_OUT_OF_HOST_MEMORY;
277 break;
278 }
279 result = gfxstream_vk_physical_device_init(gfxstream_physicalDevice, gfxstream_instance,
280 internal_list[i]);
281 if (result == VK_SUCCESS) {
282 list_addtail(&gfxstream_physicalDevice->vk.link,
283 &gfxstream_instance->vk.physical_devices.list);
284 } else {
285 vk_free(&gfxstream_instance->vk.alloc, gfxstream_physicalDevice);
286 break;
287 }
288 }
289 }
290
291 return result;
292 }
293
294 static struct vk_instance_extension_table* get_instance_extensions() {
295 struct vk_instance_extension_table* const retTablePtr =
296 &gfxstream_vk_instance_extensions_supported;
297 if (!instance_extension_table_initialized) {
298 VkResult result = SetupInstanceForProcess();
299 if (result == VK_SUCCESS) {
300 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
301 auto resources = gfxstream::vk::ResourceTracker::get();
302 uint32_t numInstanceExts = 0;
303 result = resources->on_vkEnumerateInstanceExtensionProperties(vkEnc, VK_SUCCESS, NULL,
304 &numInstanceExts, NULL);
305 if (result == VK_SUCCESS) {
306 std::vector<VkExtensionProperties> extProps(numInstanceExts);
307 result = resources->on_vkEnumerateInstanceExtensionProperties(
308 vkEnc, VK_SUCCESS, NULL, &numInstanceExts, extProps.data());
309 if (result == VK_SUCCESS) {
310 // Enable instance extensions from gfxstream
311 for (uint32_t i = 0; i < numInstanceExts; i++) {
312 for (uint32_t j = 0; j < VK_INSTANCE_EXTENSION_COUNT; j++) {
313 if (0 == strncmp(extProps[i].extensionName,
314 vk_instance_extensions[j].extensionName,
315 VK_MAX_EXTENSION_NAME_SIZE)) {
316 gfxstream_vk_instance_extensions_supported.extensions[j] = true;
317 break;
318 }
319 }
320 }
321 // Make sure all guest-emulated instance extensions are enabled
322 for (uint32_t j = 0; j < VK_INSTANCE_EXTENSION_COUNT; j++) {
323 if (isGuestEmulatedInstanceExtension(
324 vk_instance_extensions[j].extensionName)) {
325 gfxstream_vk_instance_extensions_supported.extensions[j] = true;
326 }
327 }
328 instance_extension_table_initialized = true;
329 }
330 }
331 }
332 }
333 return retTablePtr;
334 }
335
336 } // namespace
337
338 VkResult gfxstream_vk_CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
339 const VkAllocationCallbacks* pAllocator,
340 VkInstance* pInstance) {
341 MESA_TRACE_SCOPE("vkCreateInstance");
342
343 struct gfxstream_vk_instance* instance;
344 VkResult result = VK_SUCCESS;
345
346 pAllocator = pAllocator ?: vk_default_allocator();
347 instance = (struct gfxstream_vk_instance*)vk_zalloc(
348 pAllocator, sizeof(*instance), GFXSTREAM_DEFAULT_ALIGN, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
349
350 if (!instance) {
351 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
352 }
353
354 instance->init_failed = (SetupInstanceForProcess() == VK_ERROR_INITIALIZATION_FAILED);
355 auto extensions = instance->init_failed ? &gfxstream_vk_instance_extensions_supported
356 : get_instance_extensions();
357 struct vk_instance_dispatch_table dispatch_table;
358 memset(&dispatch_table, 0, sizeof(struct vk_instance_dispatch_table));
359 vk_instance_dispatch_table_from_entrypoints(&dispatch_table, &gfxstream_vk_instance_entrypoints,
360 false);
361 #if !DETECT_OS_FUCHSIA
362 vk_instance_dispatch_table_from_entrypoints(&dispatch_table, &wsi_instance_entrypoints, false);
363 #endif
364
365 result = vk_instance_init(&instance->vk, extensions, &dispatch_table, pCreateInfo, pAllocator);
366
367 if (result != VK_SUCCESS) {
368 vk_free(pAllocator, instance);
369 return vk_error(NULL, result);
370 }
371
372 // Note: Do not support try_create_for_drm. virtio_gpu DRM device opened in
373 // init_renderer above, which can still enumerate multiple physical devices on the host.
374 instance->vk.physical_devices.enumerate = gfxstream_vk_enumerate_devices;
375 instance->vk.physical_devices.destroy = gfxstream_vk_destroy_physical_device;
376
377 if (instance->init_failed) {
378 goto out;
379 }
380
381 /* Encoder call */
382 {
383 // Full local copy of pCreateInfo
384 VkInstanceCreateInfo localCreateInfo = *pCreateInfo;
385 std::vector<const char*> filteredExts = filteredInstanceExtensionNames(
386 localCreateInfo.enabledExtensionCount, localCreateInfo.ppEnabledExtensionNames);
387 localCreateInfo.enabledExtensionCount = static_cast<uint32_t>(filteredExts.size());
388 localCreateInfo.ppEnabledExtensionNames = filteredExts.data();
389
390 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
391 result = vkEnc->vkCreateInstance(&localCreateInfo, nullptr, &instance->internal_object,
392 true /* do lock */);
393 if (result != VK_SUCCESS) {
394 vk_free(pAllocator, instance);
395 return vk_error(NULL, result);
396 }
397 }
398
399 out:
400 *pInstance = gfxstream_vk_instance_to_handle(instance);
401 return VK_SUCCESS;
402 }
403
404 void gfxstream_vk_DestroyInstance(VkInstance _instance, const VkAllocationCallbacks* pAllocator) {
405 MESA_TRACE_SCOPE("vkDestroyInstance");
406 if (_instance == VK_NULL_HANDLE) return;
407
408 VK_FROM_HANDLE(gfxstream_vk_instance, instance, _instance);
409
410 if (!instance->init_failed) {
411 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
412 vkEnc->vkDestroyInstance(instance->internal_object, pAllocator, true /* do lock */);
413 }
414
415 vk_instance_finish(&instance->vk);
416 vk_free(&instance->vk.alloc, instance);
417
418 // To make End2EndTests happy, since now the host connection is statically linked to
419 // libvulkan_ranchu.so [separate HostConnections now].
420 #if defined(END2END_TESTS)
421 GfxStreamConnectionManager::resetThreadLocalInstance();
422 VirtGpuDevice::resetInstance();
423 gSeqno = 0;
424 #endif
425 }
426
427 VkResult gfxstream_vk_EnumerateInstanceExtensionProperties(const char* pLayerName,
428 uint32_t* pPropertyCount,
429 VkExtensionProperties* pProperties) {
430 MESA_TRACE_SCOPE("vkvkEnumerateInstanceExtensionProperties");
431 (void)pLayerName;
432
433 return vk_enumerate_instance_extension_properties(get_instance_extensions(), pPropertyCount,
434 pProperties);
435 }
436
437 VkResult gfxstream_vk_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
438 const char* pLayerName,
439 uint32_t* pPropertyCount,
440 VkExtensionProperties* pProperties) {
441 MESA_TRACE_SCOPE("vkEnumerateDeviceExtensionProperties");
442 (void)pLayerName;
443 VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
444
445 VK_OUTARRAY_MAKE_TYPED(VkExtensionProperties, out, pProperties, pPropertyCount);
446
447 for (int i = 0; i < VK_DEVICE_EXTENSION_COUNT; i++) {
448 if (!pdevice->supported_extensions.extensions[i]) continue;
449
450 vk_outarray_append_typed(VkExtensionProperties, &out, prop) {
451 *prop = vk_device_extensions[i];
452 }
453 }
454
455 return vk_outarray_status(&out);
456 }
457
458 static VkResult gfxstream_vk_queue_init(struct gfxstream_vk_device* dev,
459 struct gfxstream_vk_queue* queue,
460 const VkDeviceQueueCreateInfo* queue_info,
461 uint32_t queue_index) {
462 VkResult result = vk_queue_init(&queue->vk, &dev->vk, queue_info, queue_index);
463 if (result != VK_SUCCESS) return result;
464
465 const VkDeviceQueueInfo2 device_queue_info = {
466 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
467 .pNext = NULL,
468 .flags = queue_info->flags,
469 .queueFamilyIndex = queue_info->queueFamilyIndex,
470 .queueIndex = queue_index,
471 };
472
473 // Make encoder call to host to get VkQueue internal_object
474 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
475 vkEnc->vkGetDeviceQueue2(dev->internal_object, &device_queue_info, &queue->internal_object,
476 true /* do lock */);
477
478 queue->device = dev;
479
480 return VK_SUCCESS;
481 }
482
483 static void gfxstream_vk_queue_fini(struct gfxstream_vk_queue* queue) {
484 vk_queue_finish(&queue->vk);
485 }
486
487 static VkResult gfxstream_vk_device_init_queues(struct gfxstream_vk_device* dev,
488 const VkDeviceCreateInfo* create_info) {
489 const VkAllocationCallbacks* alloc = &dev->vk.alloc;
490
491 uint32_t count = 0;
492 for (uint32_t i = 0; i < create_info->queueCreateInfoCount; i++)
493 count += create_info->pQueueCreateInfos[i].queueCount;
494
495 struct gfxstream_vk_queue* queues = (struct gfxstream_vk_queue*)vk_zalloc(
496 alloc, sizeof(*queues) * count, GFXSTREAM_DEFAULT_ALIGN, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
497 if (!queues) return VK_ERROR_OUT_OF_HOST_MEMORY;
498
499 count = 0;
500 for (uint32_t i = 0; i < create_info->queueCreateInfoCount; i++) {
501 VkResult result;
502
503 const VkDeviceQueueCreateInfo* queue_info = &create_info->pQueueCreateInfos[i];
504 for (uint32_t j = 0; j < queue_info->queueCount; j++) {
505 result = gfxstream_vk_queue_init(dev, &queues[count], queue_info, j);
506 if (result != VK_SUCCESS) {
507 for (uint32_t k = 0; k < count; k++) gfxstream_vk_queue_fini(&queues[k]);
508 vk_free(alloc, queues);
509
510 return result;
511 }
512
513 // Move to next queue allocation
514 count++;
515 }
516 }
517
518 dev->queues = queues;
519 dev->queue_count = count;
520
521 return VK_SUCCESS;
522 }
523
524 static VkResult gfxstream_vk_device_queue_family_init(struct gfxstream_vk_device* dev,
525 const VkDeviceCreateInfo* create_info) {
526 const VkAllocationCallbacks* alloc = &dev->vk.alloc;
527 uint32_t* queue_families = NULL;
528 uint32_t count = 0;
529
530 queue_families =
531 (uint32_t*)vk_zalloc(alloc, sizeof(*queue_families) * create_info->queueCreateInfoCount,
532 GFXSTREAM_DEFAULT_ALIGN, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
533 if (!queue_families) return VK_ERROR_OUT_OF_HOST_MEMORY;
534
535 for (uint32_t i = 0; i < create_info->queueCreateInfoCount; i++) {
536 const uint32_t index = create_info->pQueueCreateInfos[i].queueFamilyIndex;
537 bool new_index = true;
538
539 for (uint32_t j = 0; j < count; j++) {
540 if (queue_families[j] == index) {
541 new_index = false;
542 break;
543 }
544 }
545 if (new_index) queue_families[count++] = index;
546 }
547
548 dev->queue_families = queue_families;
549 dev->queue_family_count = count;
550
551 return VK_SUCCESS;
552 }
553
554 static inline void gfxstream_vk_device_queue_family_fini(struct gfxstream_vk_device* dev) {
555 vk_free(&dev->vk.alloc, dev->queue_families);
556 }
557
558 VkResult gfxstream_vk_CreateDevice(VkPhysicalDevice physicalDevice,
559 const VkDeviceCreateInfo* pCreateInfo,
560 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) {
561 MESA_TRACE_SCOPE("vkCreateDevice");
562 VK_FROM_HANDLE(gfxstream_vk_physical_device, gfxstream_physicalDevice, physicalDevice);
563 VkResult result = (VkResult)0;
564
565 /*
566 * Android's libvulkan implements VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT, but
567 * passes it to the underlying driver anyways. See:
568 *
569 * https://android-review.googlesource.com/c/platform/hardware/google/gfxstream/+/2839438
570 *
571 * and associated bugs. Mesa VK runtime also checks this, so we have to filter out before
572 * reaches it.
573 */
574 VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT* mutableSwapchainMaintenance1Features =
575 vk_find_struct(const_cast<VkDeviceCreateInfo*>(pCreateInfo),
576 PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT);
577 if (mutableSwapchainMaintenance1Features) {
578 mutableSwapchainMaintenance1Features->swapchainMaintenance1 = VK_FALSE;
579 }
580
581 const VkAllocationCallbacks* pMesaAllocator =
582 pAllocator ?: &gfxstream_physicalDevice->instance->vk.alloc;
583 struct gfxstream_vk_device* gfxstream_device = (struct gfxstream_vk_device*)vk_zalloc(
584 pMesaAllocator, sizeof(struct gfxstream_vk_device), GFXSTREAM_DEFAULT_ALIGN, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
585 result = gfxstream_device ? VK_SUCCESS : VK_ERROR_OUT_OF_HOST_MEMORY;
586 if (result == VK_SUCCESS) {
587 // Full local copy of pCreateInfo
588 VkDeviceCreateInfo localCreateInfo = *pCreateInfo;
589
590 std::vector<const char*> filteredExts = filteredDeviceExtensionNames(
591 gfxstream_physicalDevice, localCreateInfo.enabledExtensionCount,
592 localCreateInfo.ppEnabledExtensionNames);
593 localCreateInfo.enabledExtensionCount = static_cast<uint32_t>(filteredExts.size());
594 localCreateInfo.ppEnabledExtensionNames = filteredExts.data();
595
596 /* pNext = VkPhysicalDeviceGroupProperties */
597 std::vector<VkPhysicalDevice> initialPhysicalDeviceList;
598 VkPhysicalDeviceGroupProperties* mutablePhysicalDeviceGroupProperties = vk_find_struct(&localCreateInfo, PHYSICAL_DEVICE_GROUP_PROPERTIES);
599 if (mutablePhysicalDeviceGroupProperties) {
600 // Temporarily modify the VkPhysicalDeviceGroupProperties structure to use translated
601 // VkPhysicalDevice references for the encoder call
602 for (uint32_t physDev = 0;
603 physDev < mutablePhysicalDeviceGroupProperties->physicalDeviceCount; physDev++) {
604 initialPhysicalDeviceList.push_back(
605 mutablePhysicalDeviceGroupProperties->physicalDevices[physDev]);
606 VK_FROM_HANDLE(gfxstream_vk_physical_device, gfxstream_physicalDevice,
607 mutablePhysicalDeviceGroupProperties->physicalDevices[physDev]);
608 mutablePhysicalDeviceGroupProperties->physicalDevices[physDev] =
609 gfxstream_physicalDevice->internal_object;
610 }
611 }
612
613 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
614 result = vkEnc->vkCreateDevice(gfxstream_physicalDevice->internal_object, &localCreateInfo,
615 pAllocator, &gfxstream_device->internal_object,
616 true /* do lock */);
617
618 if (mutablePhysicalDeviceGroupProperties) {
619 // Revert the physicalDevice list in VkPhysicalDeviceGroupProperties to the user-set
620 // data
621 for (uint32_t physDev = 0;
622 physDev < mutablePhysicalDeviceGroupProperties->physicalDeviceCount; physDev++) {
623 initialPhysicalDeviceList.push_back(
624 mutablePhysicalDeviceGroupProperties->physicalDevices[physDev]);
625 mutablePhysicalDeviceGroupProperties->physicalDevices[physDev] =
626 initialPhysicalDeviceList[physDev];
627 }
628 }
629 }
630 if (result == VK_SUCCESS) {
631 struct vk_device_dispatch_table dispatch_table;
632 memset(&dispatch_table, 0, sizeof(struct vk_device_dispatch_table));
633 vk_device_dispatch_table_from_entrypoints(&dispatch_table, &gfxstream_vk_device_entrypoints,
634 false);
635 #if !DETECT_OS_FUCHSIA
636 vk_device_dispatch_table_from_entrypoints(&dispatch_table, &wsi_device_entrypoints, false);
637 #endif
638
639 result = vk_device_init(&gfxstream_device->vk, &gfxstream_physicalDevice->vk,
640 &dispatch_table, pCreateInfo, pMesaAllocator);
641 }
642 if (result == VK_SUCCESS) {
643 gfxstream_device->physical_device = gfxstream_physicalDevice;
644 // TODO: Initialize cmd_dispatch for emulated secondary command buffer support?
645 gfxstream_device->vk.command_dispatch_table = &gfxstream_device->cmd_dispatch;
646 *pDevice = gfxstream_vk_device_to_handle(gfxstream_device);
647 } else {
648 vk_free(pMesaAllocator, gfxstream_device);
649 }
650
651 if (result == VK_SUCCESS) {
652 result = gfxstream_vk_device_queue_family_init(gfxstream_device, pCreateInfo);
653 }
654 if (result == VK_SUCCESS) {
655 result = gfxstream_vk_device_init_queues(gfxstream_device, pCreateInfo);
656 }
657
658 return result;
659 }
660
661 void gfxstream_vk_DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
662 MESA_TRACE_SCOPE("vkDestroyDevice");
663 VK_FROM_HANDLE(gfxstream_vk_device, gfxstream_device, device);
664 if (device == VK_NULL_HANDLE) return;
665
666 const VkAllocationCallbacks* alloc = pAllocator ? pAllocator : &gfxstream_device->vk.alloc;
667
668 for (uint32_t i = 0; i < gfxstream_device->queue_count; i++)
669 gfxstream_vk_queue_fini(&gfxstream_device->queues[i]);
670
671 gfxstream_vk_device_queue_family_fini(gfxstream_device);
672
673 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
674 vkEnc->vkDestroyDevice(gfxstream_device->internal_object, pAllocator, true /* do lock */);
675
676 vk_free(alloc, gfxstream_device->queues);
677
678 vk_device_finish(&gfxstream_device->vk);
679 vk_free(alloc, gfxstream_device);
680 }
681
682 /* The loader wants us to expose a second GetInstanceProcAddr function
683 * to work around certain LD_PRELOAD issues seen in apps.
684 */
685 extern "C" PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
686 vk_icdGetInstanceProcAddr(VkInstance instance, const char* pName);
687
688 extern "C" PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
689 vk_icdGetInstanceProcAddr(VkInstance instance, const char* pName) {
690 return gfxstream_vk_GetInstanceProcAddr(instance, pName);
691 }
692
693 PFN_vkVoidFunction gfxstream_vk_GetInstanceProcAddr(VkInstance _instance, const char* pName) {
694 VK_FROM_HANDLE(gfxstream_vk_instance, instance, _instance);
695 return vk_instance_get_proc_addr(&instance->vk, &gfxstream_vk_instance_entrypoints, pName);
696 }
697
698 PFN_vkVoidFunction gfxstream_vk_GetDeviceProcAddr(VkDevice _device, const char* pName) {
699 MESA_TRACE_SCOPE("vkGetDeviceProcAddr");
700 VK_FROM_HANDLE(gfxstream_vk_device, device, _device);
701 return vk_device_get_proc_addr(&device->vk, pName);
702 }
703
704 VkResult gfxstream_vk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
705 const VkAllocationCallbacks* pAllocator,
706 VkDeviceMemory* pMemory) {
707 MESA_TRACE_SCOPE("vkAllocateMemory");
708 VK_FROM_HANDLE(gfxstream_vk_device, gfxstream_device, device);
709 VkResult vkAllocateMemory_VkResult_return = (VkResult)0;
710 /* VkMemoryDedicatedAllocateInfo */
711 VkMemoryDedicatedAllocateInfo* dedicatedAllocInfoPtr = vk_find_struct(
712 const_cast<VkMemoryAllocateInfo*>(pAllocateInfo), MEMORY_DEDICATED_ALLOCATE_INFO);
713 if (dedicatedAllocInfoPtr) {
714 if (dedicatedAllocInfoPtr->buffer) {
715 VK_FROM_HANDLE(gfxstream_vk_buffer, gfxstream_buffer, dedicatedAllocInfoPtr->buffer);
716 dedicatedAllocInfoPtr->buffer = gfxstream_buffer->internal_object;
717 }
718 }
719 {
720 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
721 auto resources = gfxstream::vk::ResourceTracker::get();
722 vkAllocateMemory_VkResult_return =
723 resources->on_vkAllocateMemory(vkEnc, VK_SUCCESS, gfxstream_device->internal_object,
724 pAllocateInfo, pAllocator, pMemory);
725 }
726 return vkAllocateMemory_VkResult_return;
727 }
728
729 VkResult gfxstream_vk_EnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
730 VkLayerProperties* pProperties) {
731 MESA_TRACE_SCOPE("vkEnumerateInstanceLayerProperties");
732 auto result = SetupInstanceForProcess();
733 if (result != VK_SUCCESS) {
734 return vk_error(NULL, result);
735 }
736
737 VkResult vkEnumerateInstanceLayerProperties_VkResult_return = (VkResult)0;
738 {
739 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
740 vkEnumerateInstanceLayerProperties_VkResult_return =
741 vkEnc->vkEnumerateInstanceLayerProperties(pPropertyCount, pProperties,
742 true /* do lock */);
743 }
744 return vkEnumerateInstanceLayerProperties_VkResult_return;
745 }
746
747 VkResult gfxstream_vk_EnumerateInstanceVersion(uint32_t* pApiVersion) {
748 MESA_TRACE_SCOPE("vkEnumerateInstanceVersion");
749 auto result = SetupInstanceForProcess();
750 if (result != VK_SUCCESS) {
751 return vk_error(NULL, result);
752 }
753
754 VkResult vkEnumerateInstanceVersion_VkResult_return = (VkResult)0;
755 {
756 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
757 vkEnumerateInstanceVersion_VkResult_return =
758 vkEnc->vkEnumerateInstanceVersion(pApiVersion, true /* do lock */);
759 }
760 return vkEnumerateInstanceVersion_VkResult_return;
761 }
762
763 static bool vk_descriptor_type_has_descriptor_buffer(VkDescriptorType type) {
764 switch (type) {
765 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
766 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
767 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
768 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
769 return true;
770 default:
771 return false;
772 }
773 }
774
775 static std::vector<VkWriteDescriptorSet> transformDescriptorSetList(
776 const VkWriteDescriptorSet* pDescriptorSets, uint32_t descriptorSetCount,
777 std::vector<std::vector<VkDescriptorBufferInfo>>& bufferInfos) {
778 std::vector<VkWriteDescriptorSet> outDescriptorSets(descriptorSetCount);
779 bufferInfos.resize(descriptorSetCount);
780 for (uint32_t i = 0; i < descriptorSetCount; ++i) {
781 const auto& srcDescriptorSet = pDescriptorSets[i];
782 const uint32_t descriptorCount = srcDescriptorSet.descriptorCount;
783
784 VkWriteDescriptorSet& outDescriptorSet = outDescriptorSets[i];
785 outDescriptorSet = srcDescriptorSet;
786
787 std::vector<VkDescriptorBufferInfo>& bufferInfo = bufferInfos[i];
788 bufferInfo.resize(descriptorCount);
789 for (uint32_t j = 0; j < descriptorCount; ++j) {
790 const auto* srcBufferInfo = srcDescriptorSet.pBufferInfo;
791 if (srcBufferInfo) {
792 bufferInfo[j] = srcBufferInfo[j];
793 bufferInfo[j].buffer = VK_NULL_HANDLE;
794 if (vk_descriptor_type_has_descriptor_buffer(srcDescriptorSet.descriptorType) &&
795 srcBufferInfo[j].buffer) {
796 VK_FROM_HANDLE(gfxstream_vk_buffer, gfxstreamBuffer, srcBufferInfo[j].buffer);
797 bufferInfo[j].buffer = gfxstreamBuffer->internal_object;
798 }
799 } else {
800 memset(&bufferInfo[j], 0, sizeof(VkDescriptorBufferInfo));
801 }
802 }
803 outDescriptorSet.pBufferInfo = bufferInfos[i].data();
804 }
805 return outDescriptorSets;
806 }
807
808 void gfxstream_vk_UpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
809 const VkWriteDescriptorSet* pDescriptorWrites,
810 uint32_t descriptorCopyCount,
811 const VkCopyDescriptorSet* pDescriptorCopies) {
812 MESA_TRACE_SCOPE("vkUpdateDescriptorSets");
813 VK_FROM_HANDLE(gfxstream_vk_device, gfxstream_device, device);
814 {
815 auto vkEnc = gfxstream::vk::ResourceTracker::getThreadLocalEncoder();
816 std::vector<std::vector<VkDescriptorBufferInfo>> descriptorBufferInfoStorage;
817 std::vector<VkWriteDescriptorSet> internal_pDescriptorWrites = transformDescriptorSetList(
818 pDescriptorWrites, descriptorWriteCount, descriptorBufferInfoStorage);
819 auto resources = gfxstream::vk::ResourceTracker::get();
820 resources->on_vkUpdateDescriptorSets(
821 vkEnc, gfxstream_device->internal_object, descriptorWriteCount,
822 internal_pDescriptorWrites.data(), descriptorCopyCount, pDescriptorCopies);
823 }
824 }
825
826 /*
827 * Override vk_common_SetDebugUtilsObjectNameEXT.
828 *
829 * Mesa's common implementation assumes every Vulkan object handle is a pointer
830 * to a vk_object_base. In gfxstream, only a subset of objects carry a Mesa
831 * wrapper; all other non-dispatchable handles are opaque host handles. Passing
832 * such a handle to the common path causes a null-pointer dereference.
833 *
834 * For wrapped objects we delegate to the common implementation. For unwrapped
835 * objects we return VK_SUCCESS as a no-op; the names would only become useful
836 * once guest-to-host forwarding is implemented.
837 */
838 VkResult gfxstream_vk_SetDebugUtilsObjectNameEXT(VkDevice device,
839 const VkDebugUtilsObjectNameInfoEXT* pNameInfo) {
840 MESA_TRACE_SCOPE("vkSetDebugUtilsObjectNameEXT");
841 /*
842 * These object types have a gfxstream Mesa wrapper containing a
843 * vk_object_base, so the common implementation can safely dereference
844 * the handle. This list corresponds to the gfxstream_vk_* structs
845 * defined in gfxstream_vk_private.h.
846 */
847 switch (pNameInfo->objectType) {
848 case VK_OBJECT_TYPE_INSTANCE:
849 case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
850 case VK_OBJECT_TYPE_DEVICE:
851 case VK_OBJECT_TYPE_QUEUE:
852 case VK_OBJECT_TYPE_COMMAND_BUFFER:
853 case VK_OBJECT_TYPE_COMMAND_POOL:
854 case VK_OBJECT_TYPE_BUFFER:
855 case VK_OBJECT_TYPE_FENCE:
856 case VK_OBJECT_TYPE_SEMAPHORE:
857 return vk_common_SetDebugUtilsObjectNameEXT(device, pNameInfo);
858 default:
859 return VK_SUCCESS;
860 }
861 }