1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "wsi_common_private.h"
25 #include "util/macros.h"
26 #include "util/os_file.h"
27 #include "util/xmlconfig.h"
28 #include "vk_util.h"
29 #include "drm-uapi/drm_fourcc.h"
30
31 #include <time.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <xf86drm.h>
36
37 bool
wsi_device_matches_drm_fd(const struct wsi_device * wsi,int drm_fd)38 wsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd)
39 {
40 if (wsi->can_present_on_device)
41 return wsi->can_present_on_device(wsi->pdevice, drm_fd);
42
43 drmDevicePtr fd_device;
44 int ret = drmGetDevice2(drm_fd, 0, &fd_device);
45 if (ret)
46 return false;
47
48 bool match = false;
49 switch (fd_device->bustype) {
50 case DRM_BUS_PCI:
51 match = wsi->pci_bus_info.pciDomain == fd_device->businfo.pci->domain &&
52 wsi->pci_bus_info.pciBus == fd_device->businfo.pci->bus &&
53 wsi->pci_bus_info.pciDevice == fd_device->businfo.pci->dev &&
54 wsi->pci_bus_info.pciFunction == fd_device->businfo.pci->func;
55 break;
56
57 default:
58 break;
59 }
60
61 drmFreeDevice(&fd_device);
62
63 return match;
64 }
65
66 static uint32_t
select_memory_type(const struct wsi_device * wsi,bool want_device_local,uint32_t type_bits)67 select_memory_type(const struct wsi_device *wsi,
68 bool want_device_local,
69 uint32_t type_bits)
70 {
71 assert(type_bits);
72
73 bool all_local = true;
74 for (uint32_t i = 0; i < wsi->memory_props.memoryTypeCount; i++) {
75 const VkMemoryType type = wsi->memory_props.memoryTypes[i];
76 bool local = type.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
77
78 if ((type_bits & (1 << i)) && local == want_device_local)
79 return i;
80 all_local &= local;
81 }
82
83 /* ignore want_device_local when all memory types are device-local */
84 if (all_local) {
85 assert(!want_device_local);
86 return ffs(type_bits) - 1;
87 }
88
89 unreachable("No memory type found");
90 }
91
92 static uint32_t
vk_format_size(VkFormat format)93 vk_format_size(VkFormat format)
94 {
95 switch (format) {
96 case VK_FORMAT_B8G8R8A8_UNORM:
97 case VK_FORMAT_B8G8R8A8_SRGB:
98 return 4;
99 default:
100 unreachable("Unknown WSI Format");
101 }
102 }
103
104 VkResult
wsi_create_native_image(const struct wsi_swapchain * chain,const VkSwapchainCreateInfoKHR * pCreateInfo,uint32_t num_modifier_lists,const uint32_t * num_modifiers,const uint64_t * const * modifiers,uint8_t * (alloc_shm)(struct wsi_image * image,unsigned size),struct wsi_image * image)105 wsi_create_native_image(const struct wsi_swapchain *chain,
106 const VkSwapchainCreateInfoKHR *pCreateInfo,
107 uint32_t num_modifier_lists,
108 const uint32_t *num_modifiers,
109 const uint64_t *const *modifiers,
110 uint8_t *(alloc_shm)(struct wsi_image *image, unsigned size),
111 struct wsi_image *image)
112 {
113 const struct wsi_device *wsi = chain->wsi;
114 VkResult result;
115
116 memset(image, 0, sizeof(*image));
117 for (int i = 0; i < ARRAY_SIZE(image->fds); i++)
118 image->fds[i] = -1;
119
120 struct wsi_image_create_info image_wsi_info = {
121 .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
122 };
123 VkExternalMemoryImageCreateInfo ext_mem_image_create_info = {
124 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
125 .pNext = &image_wsi_info,
126 .handleTypes = wsi->sw ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT :
127 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
128 };
129 VkImageCreateInfo image_info = {
130 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
131 .pNext = &ext_mem_image_create_info,
132 .flags = 0,
133 .imageType = VK_IMAGE_TYPE_2D,
134 .format = pCreateInfo->imageFormat,
135 .extent = {
136 .width = pCreateInfo->imageExtent.width,
137 .height = pCreateInfo->imageExtent.height,
138 .depth = 1,
139 },
140 .mipLevels = 1,
141 .arrayLayers = 1,
142 .samples = VK_SAMPLE_COUNT_1_BIT,
143 .tiling = VK_IMAGE_TILING_OPTIMAL,
144 .usage = pCreateInfo->imageUsage,
145 .sharingMode = pCreateInfo->imageSharingMode,
146 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
147 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
148 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
149 };
150
151 VkImageFormatListCreateInfoKHR image_format_list;
152 if (pCreateInfo->flags & VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR) {
153 image_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT |
154 VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR;
155
156 const VkImageFormatListCreateInfoKHR *format_list =
157 vk_find_struct_const(pCreateInfo->pNext,
158 IMAGE_FORMAT_LIST_CREATE_INFO_KHR);
159
160 #ifndef NDEBUG
161 assume(format_list && format_list->viewFormatCount > 0);
162 bool format_found = false;
163 for (int i = 0; i < format_list->viewFormatCount; i++)
164 if (pCreateInfo->imageFormat == format_list->pViewFormats[i])
165 format_found = true;
166 assert(format_found);
167 #endif
168
169 image_format_list = *format_list;
170 image_format_list.pNext = NULL;
171 __vk_append_struct(&image_info, &image_format_list);
172 }
173
174 VkImageDrmFormatModifierListCreateInfoEXT image_modifier_list;
175
176 uint32_t image_modifier_count = 0, modifier_prop_count = 0;
177 struct VkDrmFormatModifierPropertiesEXT *modifier_props = NULL;
178 uint64_t *image_modifiers = NULL;
179 if (num_modifier_lists == 0) {
180 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
181 image_wsi_info.scanout = true;
182 } else {
183 /* The winsys can't request modifiers if we don't support them. */
184 assert(wsi->supports_modifiers);
185 struct VkDrmFormatModifierPropertiesListEXT modifier_props_list = {
186 .sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
187 };
188 VkFormatProperties2 format_props = {
189 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
190 .pNext = &modifier_props_list,
191 };
192 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
193 pCreateInfo->imageFormat,
194 &format_props);
195 assert(modifier_props_list.drmFormatModifierCount > 0);
196 modifier_props = vk_alloc(&chain->alloc,
197 sizeof(*modifier_props) *
198 modifier_props_list.drmFormatModifierCount,
199 8,
200 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
201 if (!modifier_props) {
202 result = VK_ERROR_OUT_OF_HOST_MEMORY;
203 goto fail;
204 }
205
206 modifier_props_list.pDrmFormatModifierProperties = modifier_props;
207 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
208 pCreateInfo->imageFormat,
209 &format_props);
210
211 /* Call GetImageFormatProperties with every modifier and filter the list
212 * down to those that we know work.
213 */
214 modifier_prop_count = 0;
215 for (uint32_t i = 0; i < modifier_props_list.drmFormatModifierCount; i++) {
216 VkPhysicalDeviceImageDrmFormatModifierInfoEXT mod_info = {
217 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT,
218 .drmFormatModifier = modifier_props[i].drmFormatModifier,
219 .sharingMode = pCreateInfo->imageSharingMode,
220 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
221 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
222 };
223 VkPhysicalDeviceImageFormatInfo2 format_info = {
224 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
225 .format = pCreateInfo->imageFormat,
226 .type = VK_IMAGE_TYPE_2D,
227 .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
228 .usage = pCreateInfo->imageUsage,
229 .flags = image_info.flags,
230 };
231
232 VkImageFormatListCreateInfoKHR format_list;
233 if (image_info.flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
234 format_list = image_format_list;
235 format_list.pNext = NULL;
236 __vk_append_struct(&format_info, &format_list);
237 }
238
239 VkImageFormatProperties2 format_props = {
240 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
241 .pNext = NULL,
242 };
243 __vk_append_struct(&format_info, &mod_info);
244 result = wsi->GetPhysicalDeviceImageFormatProperties2(wsi->pdevice,
245 &format_info,
246 &format_props);
247 if (result == VK_SUCCESS)
248 modifier_props[modifier_prop_count++] = modifier_props[i];
249 }
250
251 uint32_t max_modifier_count = 0;
252 for (uint32_t l = 0; l < num_modifier_lists; l++)
253 max_modifier_count = MAX2(max_modifier_count, num_modifiers[l]);
254
255 image_modifiers = vk_alloc(&chain->alloc,
256 sizeof(*image_modifiers) *
257 max_modifier_count,
258 8,
259 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
260 if (!image_modifiers) {
261 result = VK_ERROR_OUT_OF_HOST_MEMORY;
262 goto fail;
263 }
264
265 image_modifier_count = 0;
266 for (uint32_t l = 0; l < num_modifier_lists; l++) {
267 /* Walk the modifier lists and construct a list of supported
268 * modifiers.
269 */
270 for (uint32_t i = 0; i < num_modifiers[l]; i++) {
271 for (uint32_t j = 0; j < modifier_prop_count; j++) {
272 if (modifier_props[j].drmFormatModifier == modifiers[l][i])
273 image_modifiers[image_modifier_count++] = modifiers[l][i];
274 }
275 }
276
277 /* We only want to take the modifiers from the first list */
278 if (image_modifier_count > 0)
279 break;
280 }
281
282 if (image_modifier_count > 0) {
283 image_modifier_list = (VkImageDrmFormatModifierListCreateInfoEXT) {
284 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT,
285 .drmFormatModifierCount = image_modifier_count,
286 .pDrmFormatModifiers = image_modifiers,
287 };
288 image_info.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
289 __vk_append_struct(&image_info, &image_modifier_list);
290 } else {
291 /* TODO: Add a proper error here */
292 assert(!"Failed to find a supported modifier! This should never "
293 "happen because LINEAR should always be available");
294 result = VK_ERROR_OUT_OF_HOST_MEMORY;
295 goto fail;
296 }
297 }
298
299 result = wsi->CreateImage(chain->device, &image_info,
300 &chain->alloc, &image->image);
301 if (result != VK_SUCCESS)
302 goto fail;
303
304 VkMemoryRequirements reqs;
305 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
306
307 void *sw_host_ptr = NULL;
308 if (alloc_shm) {
309 VkSubresourceLayout layout;
310
311 wsi->GetImageSubresourceLayout(chain->device, image->image,
312 &(VkImageSubresource) {
313 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
314 .mipLevel = 0,
315 .arrayLayer = 0,
316 }, &layout);
317 sw_host_ptr = (*alloc_shm)(image, layout.size);
318 }
319
320 const struct wsi_memory_allocate_info memory_wsi_info = {
321 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
322 .pNext = NULL,
323 .implicit_sync = true,
324 };
325 const VkExportMemoryAllocateInfo memory_export_info = {
326 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
327 .pNext = &memory_wsi_info,
328 .handleTypes = wsi->sw ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT :
329 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
330 };
331 const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
332 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
333 .pNext = &memory_export_info,
334 .image = image->image,
335 .buffer = VK_NULL_HANDLE,
336 };
337 const VkImportMemoryHostPointerInfoEXT host_ptr_info = {
338 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT,
339 .pNext = &memory_dedicated_info,
340 .pHostPointer = sw_host_ptr,
341 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
342 };
343 const VkMemoryAllocateInfo memory_info = {
344 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
345 .pNext = sw_host_ptr ? (void *)&host_ptr_info : (void *)&memory_dedicated_info,
346 .allocationSize = reqs.size,
347 .memoryTypeIndex = select_memory_type(wsi, true, reqs.memoryTypeBits),
348 };
349 result = wsi->AllocateMemory(chain->device, &memory_info,
350 &chain->alloc, &image->memory);
351 if (result != VK_SUCCESS)
352 goto fail;
353
354 result = wsi->BindImageMemory(chain->device, image->image,
355 image->memory, 0);
356 if (result != VK_SUCCESS)
357 goto fail;
358
359 int fd = -1;
360 if (!wsi->sw) {
361 const VkMemoryGetFdInfoKHR memory_get_fd_info = {
362 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
363 .pNext = NULL,
364 .memory = image->memory,
365 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
366 };
367
368 result = wsi->GetMemoryFdKHR(chain->device, &memory_get_fd_info, &fd);
369 if (result != VK_SUCCESS)
370 goto fail;
371 }
372
373 if (!wsi->sw && num_modifier_lists > 0) {
374 VkImageDrmFormatModifierPropertiesEXT image_mod_props = {
375 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT,
376 };
377 result = wsi->GetImageDrmFormatModifierPropertiesEXT(chain->device,
378 image->image,
379 &image_mod_props);
380 if (result != VK_SUCCESS) {
381 close(fd);
382 goto fail;
383 }
384 image->drm_modifier = image_mod_props.drmFormatModifier;
385 assert(image->drm_modifier != DRM_FORMAT_MOD_INVALID);
386
387 for (uint32_t j = 0; j < modifier_prop_count; j++) {
388 if (modifier_props[j].drmFormatModifier == image->drm_modifier) {
389 image->num_planes = modifier_props[j].drmFormatModifierPlaneCount;
390 break;
391 }
392 }
393
394 for (uint32_t p = 0; p < image->num_planes; p++) {
395 const VkImageSubresource image_subresource = {
396 .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT << p,
397 .mipLevel = 0,
398 .arrayLayer = 0,
399 };
400 VkSubresourceLayout image_layout;
401 wsi->GetImageSubresourceLayout(chain->device, image->image,
402 &image_subresource, &image_layout);
403 image->sizes[p] = image_layout.size;
404 image->row_pitches[p] = image_layout.rowPitch;
405 image->offsets[p] = image_layout.offset;
406 if (p == 0) {
407 image->fds[p] = fd;
408 } else {
409 image->fds[p] = os_dupfd_cloexec(fd);
410 if (image->fds[p] == -1) {
411 for (uint32_t i = 0; i < p; i++)
412 close(image->fds[i]);
413
414 result = VK_ERROR_OUT_OF_HOST_MEMORY;
415 goto fail;
416 }
417 }
418 }
419 } else {
420 const VkImageSubresource image_subresource = {
421 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
422 .mipLevel = 0,
423 .arrayLayer = 0,
424 };
425 VkSubresourceLayout image_layout;
426 wsi->GetImageSubresourceLayout(chain->device, image->image,
427 &image_subresource, &image_layout);
428
429 image->drm_modifier = DRM_FORMAT_MOD_INVALID;
430 image->num_planes = 1;
431 image->sizes[0] = reqs.size;
432 image->row_pitches[0] = image_layout.rowPitch;
433 image->offsets[0] = 0;
434 image->fds[0] = fd;
435 }
436
437 vk_free(&chain->alloc, modifier_props);
438 vk_free(&chain->alloc, image_modifiers);
439
440 return VK_SUCCESS;
441
442 fail:
443 vk_free(&chain->alloc, modifier_props);
444 vk_free(&chain->alloc, image_modifiers);
445 wsi_destroy_image(chain, image);
446
447 return result;
448 }
449
450 static inline uint32_t
align_u32(uint32_t v,uint32_t a)451 align_u32(uint32_t v, uint32_t a)
452 {
453 assert(a != 0 && a == (a & -a));
454 return (v + a - 1) & ~(a - 1);
455 }
456
457 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
458
459 VkResult
wsi_create_prime_image(const struct wsi_swapchain * chain,const VkSwapchainCreateInfoKHR * pCreateInfo,bool use_modifier,struct wsi_image * image)460 wsi_create_prime_image(const struct wsi_swapchain *chain,
461 const VkSwapchainCreateInfoKHR *pCreateInfo,
462 bool use_modifier,
463 struct wsi_image *image)
464 {
465 const struct wsi_device *wsi = chain->wsi;
466 VkResult result;
467
468 memset(image, 0, sizeof(*image));
469
470 const uint32_t cpp = vk_format_size(pCreateInfo->imageFormat);
471 const uint32_t linear_stride = align_u32(pCreateInfo->imageExtent.width * cpp,
472 WSI_PRIME_LINEAR_STRIDE_ALIGN);
473
474 uint32_t linear_size = linear_stride * pCreateInfo->imageExtent.height;
475 linear_size = align_u32(linear_size, 4096);
476
477 const VkExternalMemoryBufferCreateInfo prime_buffer_external_info = {
478 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
479 .pNext = NULL,
480 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
481 };
482 const VkBufferCreateInfo prime_buffer_info = {
483 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
484 .pNext = &prime_buffer_external_info,
485 .size = linear_size,
486 .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
487 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
488 };
489 result = wsi->CreateBuffer(chain->device, &prime_buffer_info,
490 &chain->alloc, &image->prime.buffer);
491 if (result != VK_SUCCESS)
492 goto fail;
493
494 VkMemoryRequirements reqs;
495 wsi->GetBufferMemoryRequirements(chain->device, image->prime.buffer, &reqs);
496 assert(reqs.size <= linear_size);
497
498 const struct wsi_memory_allocate_info memory_wsi_info = {
499 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
500 .pNext = NULL,
501 .implicit_sync = true,
502 };
503 const VkExportMemoryAllocateInfo prime_memory_export_info = {
504 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
505 .pNext = &memory_wsi_info,
506 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
507 };
508 const VkMemoryDedicatedAllocateInfo prime_memory_dedicated_info = {
509 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
510 .pNext = &prime_memory_export_info,
511 .image = VK_NULL_HANDLE,
512 .buffer = image->prime.buffer,
513 };
514 const VkMemoryAllocateInfo prime_memory_info = {
515 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
516 .pNext = &prime_memory_dedicated_info,
517 .allocationSize = linear_size,
518 .memoryTypeIndex = select_memory_type(wsi, false, reqs.memoryTypeBits),
519 };
520 result = wsi->AllocateMemory(chain->device, &prime_memory_info,
521 &chain->alloc, &image->prime.memory);
522 if (result != VK_SUCCESS)
523 goto fail;
524
525 result = wsi->BindBufferMemory(chain->device, image->prime.buffer,
526 image->prime.memory, 0);
527 if (result != VK_SUCCESS)
528 goto fail;
529
530 const struct wsi_image_create_info image_wsi_info = {
531 .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
532 .prime_blit_src = true,
533 };
534 const VkImageCreateInfo image_info = {
535 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
536 .pNext = &image_wsi_info,
537 .flags = 0,
538 .imageType = VK_IMAGE_TYPE_2D,
539 .format = pCreateInfo->imageFormat,
540 .extent = {
541 .width = pCreateInfo->imageExtent.width,
542 .height = pCreateInfo->imageExtent.height,
543 .depth = 1,
544 },
545 .mipLevels = 1,
546 .arrayLayers = 1,
547 .samples = VK_SAMPLE_COUNT_1_BIT,
548 .tiling = VK_IMAGE_TILING_OPTIMAL,
549 .usage = pCreateInfo->imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
550 .sharingMode = pCreateInfo->imageSharingMode,
551 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
552 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
553 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
554 };
555 result = wsi->CreateImage(chain->device, &image_info,
556 &chain->alloc, &image->image);
557 if (result != VK_SUCCESS)
558 goto fail;
559
560 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
561
562 const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
563 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
564 .pNext = NULL,
565 .image = image->image,
566 .buffer = VK_NULL_HANDLE,
567 };
568 const VkMemoryAllocateInfo memory_info = {
569 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
570 .pNext = &memory_dedicated_info,
571 .allocationSize = reqs.size,
572 .memoryTypeIndex = select_memory_type(wsi, true, reqs.memoryTypeBits),
573 };
574 result = wsi->AllocateMemory(chain->device, &memory_info,
575 &chain->alloc, &image->memory);
576 if (result != VK_SUCCESS)
577 goto fail;
578
579 result = wsi->BindImageMemory(chain->device, image->image,
580 image->memory, 0);
581 if (result != VK_SUCCESS)
582 goto fail;
583
584 image->prime.blit_cmd_buffers =
585 vk_zalloc(&chain->alloc,
586 sizeof(VkCommandBuffer) * wsi->queue_family_count, 8,
587 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
588 if (!image->prime.blit_cmd_buffers) {
589 result = VK_ERROR_OUT_OF_HOST_MEMORY;
590 goto fail;
591 }
592
593 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
594 const VkCommandBufferAllocateInfo cmd_buffer_info = {
595 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
596 .pNext = NULL,
597 .commandPool = chain->cmd_pools[i],
598 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
599 .commandBufferCount = 1,
600 };
601 result = wsi->AllocateCommandBuffers(chain->device, &cmd_buffer_info,
602 &image->prime.blit_cmd_buffers[i]);
603 if (result != VK_SUCCESS)
604 goto fail;
605
606 const VkCommandBufferBeginInfo begin_info = {
607 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
608 };
609 wsi->BeginCommandBuffer(image->prime.blit_cmd_buffers[i], &begin_info);
610
611 struct VkBufferImageCopy buffer_image_copy = {
612 .bufferOffset = 0,
613 .bufferRowLength = linear_stride / cpp,
614 .bufferImageHeight = 0,
615 .imageSubresource = {
616 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
617 .mipLevel = 0,
618 .baseArrayLayer = 0,
619 .layerCount = 1,
620 },
621 .imageOffset = { .x = 0, .y = 0, .z = 0 },
622 .imageExtent = {
623 .width = pCreateInfo->imageExtent.width,
624 .height = pCreateInfo->imageExtent.height,
625 .depth = 1,
626 },
627 };
628 wsi->CmdCopyImageToBuffer(image->prime.blit_cmd_buffers[i],
629 image->image,
630 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
631 image->prime.buffer,
632 1, &buffer_image_copy);
633
634 result = wsi->EndCommandBuffer(image->prime.blit_cmd_buffers[i]);
635 if (result != VK_SUCCESS)
636 goto fail;
637 }
638
639 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info = {
640 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
641 .pNext = NULL,
642 .memory = image->prime.memory,
643 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
644 };
645 int fd;
646 result = wsi->GetMemoryFdKHR(chain->device, &linear_memory_get_fd_info, &fd);
647 if (result != VK_SUCCESS)
648 goto fail;
649
650 image->drm_modifier = use_modifier ? DRM_FORMAT_MOD_LINEAR : DRM_FORMAT_MOD_INVALID;
651 image->num_planes = 1;
652 image->sizes[0] = linear_size;
653 image->row_pitches[0] = linear_stride;
654 image->offsets[0] = 0;
655 image->fds[0] = fd;
656
657 return VK_SUCCESS;
658
659 fail:
660 wsi_destroy_image(chain, image);
661
662 return result;
663 }
664
665