drm-uapi: use local files, not system libdrm
[mesa.git] / src / vulkan / wsi / wsi_common.c
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 "drm-uapi/drm_fourcc.h"
26 #include "util/macros.h"
27 #include "vk_util.h"
28
29 #include <unistd.h>
30 #include <xf86drm.h>
31
32 VkResult
33 wsi_device_init(struct wsi_device *wsi,
34 VkPhysicalDevice pdevice,
35 WSI_FN_GetPhysicalDeviceProcAddr proc_addr,
36 const VkAllocationCallbacks *alloc,
37 int display_fd)
38 {
39 VkResult result;
40
41 memset(wsi, 0, sizeof(*wsi));
42
43 wsi->instance_alloc = *alloc;
44 wsi->pdevice = pdevice;
45
46 #define WSI_GET_CB(func) \
47 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
48 WSI_GET_CB(GetPhysicalDeviceProperties2);
49 WSI_GET_CB(GetPhysicalDeviceMemoryProperties);
50 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties);
51 #undef WSI_GET_CB
52
53 wsi->pci_bus_info.sType =
54 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT;
55 VkPhysicalDeviceProperties2 pdp2 = {
56 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
57 .pNext = &wsi->pci_bus_info,
58 };
59 GetPhysicalDeviceProperties2(pdevice, &pdp2);
60
61 wsi->maxImageDimension2D = pdp2.properties.limits.maxImageDimension2D;
62
63 GetPhysicalDeviceMemoryProperties(pdevice, &wsi->memory_props);
64 GetPhysicalDeviceQueueFamilyProperties(pdevice, &wsi->queue_family_count, NULL);
65
66 #define WSI_GET_CB(func) \
67 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
68 WSI_GET_CB(AllocateMemory);
69 WSI_GET_CB(AllocateCommandBuffers);
70 WSI_GET_CB(BindBufferMemory);
71 WSI_GET_CB(BindImageMemory);
72 WSI_GET_CB(BeginCommandBuffer);
73 WSI_GET_CB(CmdCopyImageToBuffer);
74 WSI_GET_CB(CreateBuffer);
75 WSI_GET_CB(CreateCommandPool);
76 WSI_GET_CB(CreateFence);
77 WSI_GET_CB(CreateImage);
78 WSI_GET_CB(DestroyBuffer);
79 WSI_GET_CB(DestroyCommandPool);
80 WSI_GET_CB(DestroyFence);
81 WSI_GET_CB(DestroyImage);
82 WSI_GET_CB(EndCommandBuffer);
83 WSI_GET_CB(FreeMemory);
84 WSI_GET_CB(FreeCommandBuffers);
85 WSI_GET_CB(GetBufferMemoryRequirements);
86 WSI_GET_CB(GetImageMemoryRequirements);
87 WSI_GET_CB(GetImageSubresourceLayout);
88 WSI_GET_CB(GetMemoryFdKHR);
89 WSI_GET_CB(GetPhysicalDeviceFormatProperties);
90 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR);
91 WSI_GET_CB(ResetFences);
92 WSI_GET_CB(QueueSubmit);
93 WSI_GET_CB(WaitForFences);
94 #undef WSI_GET_CB
95
96 #ifdef VK_USE_PLATFORM_XCB_KHR
97 result = wsi_x11_init_wsi(wsi, alloc);
98 if (result != VK_SUCCESS)
99 goto fail;
100 #endif
101
102 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
103 result = wsi_wl_init_wsi(wsi, alloc, pdevice);
104 if (result != VK_SUCCESS)
105 goto fail;
106 #endif
107
108 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
109 result = wsi_display_init_wsi(wsi, alloc, display_fd);
110 if (result != VK_SUCCESS)
111 goto fail;
112 #endif
113
114 return VK_SUCCESS;
115
116 fail:
117 wsi_device_finish(wsi, alloc);
118 return result;
119 }
120
121 void
122 wsi_device_finish(struct wsi_device *wsi,
123 const VkAllocationCallbacks *alloc)
124 {
125 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
126 wsi_display_finish_wsi(wsi, alloc);
127 #endif
128 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
129 wsi_wl_finish_wsi(wsi, alloc);
130 #endif
131 #ifdef VK_USE_PLATFORM_XCB_KHR
132 wsi_x11_finish_wsi(wsi, alloc);
133 #endif
134 }
135
136 bool
137 wsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd)
138 {
139 drmDevicePtr fd_device;
140 int ret = drmGetDevice2(drm_fd, 0, &fd_device);
141 if (ret)
142 return false;
143
144 bool match = false;
145 switch (fd_device->bustype) {
146 case DRM_BUS_PCI:
147 match = wsi->pci_bus_info.pciDomain == fd_device->businfo.pci->domain &&
148 wsi->pci_bus_info.pciBus == fd_device->businfo.pci->bus &&
149 wsi->pci_bus_info.pciDevice == fd_device->businfo.pci->dev &&
150 wsi->pci_bus_info.pciFunction == fd_device->businfo.pci->func;
151 break;
152
153 default:
154 break;
155 }
156
157 drmFreeDevice(&fd_device);
158
159 return match;
160 }
161
162 VkResult
163 wsi_swapchain_init(const struct wsi_device *wsi,
164 struct wsi_swapchain *chain,
165 VkDevice device,
166 const VkSwapchainCreateInfoKHR *pCreateInfo,
167 const VkAllocationCallbacks *pAllocator)
168 {
169 VkResult result;
170
171 memset(chain, 0, sizeof(*chain));
172
173 chain->wsi = wsi;
174 chain->device = device;
175 chain->alloc = *pAllocator;
176 chain->use_prime_blit = false;
177
178 chain->cmd_pools =
179 vk_zalloc(pAllocator, sizeof(VkCommandPool) * wsi->queue_family_count, 8,
180 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
181 if (!chain->cmd_pools)
182 return VK_ERROR_OUT_OF_HOST_MEMORY;
183
184 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
185 const VkCommandPoolCreateInfo cmd_pool_info = {
186 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
187 .pNext = NULL,
188 .flags = 0,
189 .queueFamilyIndex = i,
190 };
191 result = wsi->CreateCommandPool(device, &cmd_pool_info, &chain->alloc,
192 &chain->cmd_pools[i]);
193 if (result != VK_SUCCESS)
194 goto fail;
195 }
196
197 return VK_SUCCESS;
198
199 fail:
200 wsi_swapchain_finish(chain);
201 return result;
202 }
203
204 void
205 wsi_swapchain_finish(struct wsi_swapchain *chain)
206 {
207 for (unsigned i = 0; i < ARRAY_SIZE(chain->fences); i++)
208 chain->wsi->DestroyFence(chain->device, chain->fences[i], &chain->alloc);
209
210 for (uint32_t i = 0; i < chain->wsi->queue_family_count; i++) {
211 chain->wsi->DestroyCommandPool(chain->device, chain->cmd_pools[i],
212 &chain->alloc);
213 }
214 vk_free(&chain->alloc, chain->cmd_pools);
215 }
216
217 static uint32_t
218 select_memory_type(const struct wsi_device *wsi,
219 VkMemoryPropertyFlags props,
220 uint32_t type_bits)
221 {
222 for (uint32_t i = 0; i < wsi->memory_props.memoryTypeCount; i++) {
223 const VkMemoryType type = wsi->memory_props.memoryTypes[i];
224 if ((type_bits & (1 << i)) && (type.propertyFlags & props) == props)
225 return i;
226 }
227
228 unreachable("No memory type found");
229 }
230
231 static uint32_t
232 vk_format_size(VkFormat format)
233 {
234 switch (format) {
235 case VK_FORMAT_B8G8R8A8_UNORM:
236 case VK_FORMAT_B8G8R8A8_SRGB:
237 return 4;
238 default:
239 unreachable("Unknown WSI Format");
240 }
241 }
242
243 static inline uint32_t
244 align_u32(uint32_t v, uint32_t a)
245 {
246 assert(a != 0 && a == (a & -a));
247 return (v + a - 1) & ~(a - 1);
248 }
249
250 VkResult
251 wsi_create_native_image(const struct wsi_swapchain *chain,
252 const VkSwapchainCreateInfoKHR *pCreateInfo,
253 uint32_t num_modifier_lists,
254 const uint32_t *num_modifiers,
255 const uint64_t *const *modifiers,
256 struct wsi_image *image)
257 {
258 const struct wsi_device *wsi = chain->wsi;
259 VkResult result;
260
261 memset(image, 0, sizeof(*image));
262 for (int i = 0; i < ARRAY_SIZE(image->fds); i++)
263 image->fds[i] = -1;
264
265 struct wsi_image_create_info image_wsi_info = {
266 .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
267 .pNext = NULL,
268 };
269
270 uint32_t image_modifier_count = 0, modifier_prop_count = 0;
271 struct wsi_format_modifier_properties *modifier_props = NULL;
272 uint64_t *image_modifiers = NULL;
273 if (num_modifier_lists == 0) {
274 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
275 image_wsi_info.scanout = true;
276 } else {
277 /* The winsys can't request modifiers if we don't support them. */
278 assert(wsi->supports_modifiers);
279 struct wsi_format_modifier_properties_list modifier_props_list = {
280 .sType = VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA,
281 .pNext = NULL,
282 };
283 VkFormatProperties2 format_props = {
284 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
285 .pNext = &modifier_props_list,
286 };
287 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
288 pCreateInfo->imageFormat,
289 &format_props);
290 assert(modifier_props_list.modifier_count > 0);
291 modifier_props = vk_alloc(&chain->alloc,
292 sizeof(*modifier_props) *
293 modifier_props_list.modifier_count,
294 8,
295 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
296 if (!modifier_props) {
297 result = VK_ERROR_OUT_OF_HOST_MEMORY;
298 goto fail;
299 }
300
301 modifier_props_list.modifier_properties = modifier_props;
302 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
303 pCreateInfo->imageFormat,
304 &format_props);
305 modifier_prop_count = modifier_props_list.modifier_count;
306
307 uint32_t max_modifier_count = 0;
308 for (uint32_t l = 0; l < num_modifier_lists; l++)
309 max_modifier_count = MAX2(max_modifier_count, num_modifiers[l]);
310
311 image_modifiers = vk_alloc(&chain->alloc,
312 sizeof(*image_modifiers) *
313 max_modifier_count,
314 8,
315 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
316 if (!image_modifiers) {
317 result = VK_ERROR_OUT_OF_HOST_MEMORY;
318 goto fail;
319 }
320
321 image_modifier_count = 0;
322 for (uint32_t l = 0; l < num_modifier_lists; l++) {
323 /* Walk the modifier lists and construct a list of supported
324 * modifiers.
325 */
326 for (uint32_t i = 0; i < num_modifiers[l]; i++) {
327 for (uint32_t j = 0; j < modifier_prop_count; j++) {
328 if (modifier_props[j].modifier == modifiers[l][i])
329 image_modifiers[image_modifier_count++] = modifiers[l][i];
330 }
331 }
332
333 /* We only want to take the modifiers from the first list */
334 if (image_modifier_count > 0)
335 break;
336 }
337
338 if (image_modifier_count > 0) {
339 image_wsi_info.modifier_count = image_modifier_count;
340 image_wsi_info.modifiers = image_modifiers;
341 } else {
342 /* TODO: Add a proper error here */
343 assert(!"Failed to find a supported modifier! This should never "
344 "happen because LINEAR should always be available");
345 result = VK_ERROR_OUT_OF_HOST_MEMORY;
346 goto fail;
347 }
348 }
349
350 const VkImageCreateInfo image_info = {
351 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
352 .pNext = &image_wsi_info,
353 .flags = 0,
354 .imageType = VK_IMAGE_TYPE_2D,
355 .format = pCreateInfo->imageFormat,
356 .extent = {
357 .width = pCreateInfo->imageExtent.width,
358 .height = pCreateInfo->imageExtent.height,
359 .depth = 1,
360 },
361 .mipLevels = 1,
362 .arrayLayers = 1,
363 .samples = VK_SAMPLE_COUNT_1_BIT,
364 .tiling = VK_IMAGE_TILING_OPTIMAL,
365 .usage = pCreateInfo->imageUsage,
366 .sharingMode = pCreateInfo->imageSharingMode,
367 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
368 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
369 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
370 };
371 result = wsi->CreateImage(chain->device, &image_info,
372 &chain->alloc, &image->image);
373 if (result != VK_SUCCESS)
374 goto fail;
375
376 VkMemoryRequirements reqs;
377 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
378
379 const struct wsi_memory_allocate_info memory_wsi_info = {
380 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
381 .pNext = NULL,
382 .implicit_sync = true,
383 };
384 const VkExportMemoryAllocateInfo memory_export_info = {
385 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
386 .pNext = &memory_wsi_info,
387 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
388 };
389 const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
390 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
391 .pNext = &memory_export_info,
392 .image = image->image,
393 .buffer = VK_NULL_HANDLE,
394 };
395 const VkMemoryAllocateInfo memory_info = {
396 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
397 .pNext = &memory_dedicated_info,
398 .allocationSize = reqs.size,
399 .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
400 reqs.memoryTypeBits),
401 };
402 result = wsi->AllocateMemory(chain->device, &memory_info,
403 &chain->alloc, &image->memory);
404 if (result != VK_SUCCESS)
405 goto fail;
406
407 result = wsi->BindImageMemory(chain->device, image->image,
408 image->memory, 0);
409 if (result != VK_SUCCESS)
410 goto fail;
411
412 const VkMemoryGetFdInfoKHR memory_get_fd_info = {
413 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
414 .pNext = NULL,
415 .memory = image->memory,
416 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
417 };
418 int fd;
419 result = wsi->GetMemoryFdKHR(chain->device, &memory_get_fd_info, &fd);
420 if (result != VK_SUCCESS)
421 goto fail;
422
423 if (num_modifier_lists > 0) {
424 image->drm_modifier = wsi->image_get_modifier(image->image);
425 assert(image->drm_modifier != DRM_FORMAT_MOD_INVALID);
426
427 for (uint32_t j = 0; j < modifier_prop_count; j++) {
428 if (modifier_props[j].modifier == image->drm_modifier) {
429 image->num_planes = modifier_props[j].modifier_plane_count;
430 break;
431 }
432 }
433
434 for (uint32_t p = 0; p < image->num_planes; p++) {
435 const VkImageSubresource image_subresource = {
436 .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT << p,
437 .mipLevel = 0,
438 .arrayLayer = 0,
439 };
440 VkSubresourceLayout image_layout;
441 wsi->GetImageSubresourceLayout(chain->device, image->image,
442 &image_subresource, &image_layout);
443 image->sizes[p] = image_layout.size;
444 image->row_pitches[p] = image_layout.rowPitch;
445 image->offsets[p] = image_layout.offset;
446 if (p == 0) {
447 image->fds[p] = fd;
448 } else {
449 image->fds[p] = dup(fd);
450 if (image->fds[p] == -1) {
451 for (uint32_t i = 0; i < p; i++)
452 close(image->fds[p]);
453
454 goto fail;
455 }
456 }
457 }
458 } else {
459 const VkImageSubresource image_subresource = {
460 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
461 .mipLevel = 0,
462 .arrayLayer = 0,
463 };
464 VkSubresourceLayout image_layout;
465 wsi->GetImageSubresourceLayout(chain->device, image->image,
466 &image_subresource, &image_layout);
467
468 image->drm_modifier = DRM_FORMAT_MOD_INVALID;
469 image->num_planes = 1;
470 image->sizes[0] = reqs.size;
471 image->row_pitches[0] = image_layout.rowPitch;
472 image->offsets[0] = 0;
473 image->fds[0] = fd;
474 }
475
476 vk_free(&chain->alloc, modifier_props);
477 vk_free(&chain->alloc, image_modifiers);
478
479 return VK_SUCCESS;
480
481 fail:
482 vk_free(&chain->alloc, modifier_props);
483 vk_free(&chain->alloc, image_modifiers);
484 wsi_destroy_image(chain, image);
485
486 return result;
487 }
488
489 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
490
491 VkResult
492 wsi_create_prime_image(const struct wsi_swapchain *chain,
493 const VkSwapchainCreateInfoKHR *pCreateInfo,
494 bool use_modifier,
495 struct wsi_image *image)
496 {
497 const struct wsi_device *wsi = chain->wsi;
498 VkResult result;
499
500 memset(image, 0, sizeof(*image));
501
502 const uint32_t cpp = vk_format_size(pCreateInfo->imageFormat);
503 const uint32_t linear_stride = align_u32(pCreateInfo->imageExtent.width * cpp,
504 WSI_PRIME_LINEAR_STRIDE_ALIGN);
505
506 uint32_t linear_size = linear_stride * pCreateInfo->imageExtent.height;
507 linear_size = align_u32(linear_size, 4096);
508
509 const VkExternalMemoryBufferCreateInfo prime_buffer_external_info = {
510 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
511 .pNext = NULL,
512 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
513 };
514 const VkBufferCreateInfo prime_buffer_info = {
515 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
516 .pNext = &prime_buffer_external_info,
517 .size = linear_size,
518 .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
519 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
520 };
521 result = wsi->CreateBuffer(chain->device, &prime_buffer_info,
522 &chain->alloc, &image->prime.buffer);
523 if (result != VK_SUCCESS)
524 goto fail;
525
526 VkMemoryRequirements reqs;
527 wsi->GetBufferMemoryRequirements(chain->device, image->prime.buffer, &reqs);
528 assert(reqs.size <= linear_size);
529
530 const struct wsi_memory_allocate_info memory_wsi_info = {
531 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
532 .pNext = NULL,
533 .implicit_sync = true,
534 };
535 const VkExportMemoryAllocateInfo prime_memory_export_info = {
536 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
537 .pNext = &memory_wsi_info,
538 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
539 };
540 const VkMemoryDedicatedAllocateInfo prime_memory_dedicated_info = {
541 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
542 .pNext = &prime_memory_export_info,
543 .image = VK_NULL_HANDLE,
544 .buffer = image->prime.buffer,
545 };
546 const VkMemoryAllocateInfo prime_memory_info = {
547 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
548 .pNext = &prime_memory_dedicated_info,
549 .allocationSize = linear_size,
550 .memoryTypeIndex = select_memory_type(wsi, 0, reqs.memoryTypeBits),
551 };
552 result = wsi->AllocateMemory(chain->device, &prime_memory_info,
553 &chain->alloc, &image->prime.memory);
554 if (result != VK_SUCCESS)
555 goto fail;
556
557 result = wsi->BindBufferMemory(chain->device, image->prime.buffer,
558 image->prime.memory, 0);
559 if (result != VK_SUCCESS)
560 goto fail;
561
562 const VkImageCreateInfo image_info = {
563 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
564 .pNext = NULL,
565 .flags = 0,
566 .imageType = VK_IMAGE_TYPE_2D,
567 .format = pCreateInfo->imageFormat,
568 .extent = {
569 .width = pCreateInfo->imageExtent.width,
570 .height = pCreateInfo->imageExtent.height,
571 .depth = 1,
572 },
573 .mipLevels = 1,
574 .arrayLayers = 1,
575 .samples = VK_SAMPLE_COUNT_1_BIT,
576 .tiling = VK_IMAGE_TILING_OPTIMAL,
577 .usage = pCreateInfo->imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
578 .sharingMode = pCreateInfo->imageSharingMode,
579 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
580 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
581 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
582 };
583 result = wsi->CreateImage(chain->device, &image_info,
584 &chain->alloc, &image->image);
585 if (result != VK_SUCCESS)
586 goto fail;
587
588 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
589
590 const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
591 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
592 .pNext = NULL,
593 .image = image->image,
594 .buffer = VK_NULL_HANDLE,
595 };
596 const VkMemoryAllocateInfo memory_info = {
597 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
598 .pNext = &memory_dedicated_info,
599 .allocationSize = reqs.size,
600 .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
601 reqs.memoryTypeBits),
602 };
603 result = wsi->AllocateMemory(chain->device, &memory_info,
604 &chain->alloc, &image->memory);
605 if (result != VK_SUCCESS)
606 goto fail;
607
608 result = wsi->BindImageMemory(chain->device, image->image,
609 image->memory, 0);
610 if (result != VK_SUCCESS)
611 goto fail;
612
613 image->prime.blit_cmd_buffers =
614 vk_zalloc(&chain->alloc,
615 sizeof(VkCommandBuffer) * wsi->queue_family_count, 8,
616 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
617 if (!image->prime.blit_cmd_buffers) {
618 result = VK_ERROR_OUT_OF_HOST_MEMORY;
619 goto fail;
620 }
621
622 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
623 const VkCommandBufferAllocateInfo cmd_buffer_info = {
624 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
625 .pNext = NULL,
626 .commandPool = chain->cmd_pools[i],
627 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
628 .commandBufferCount = 1,
629 };
630 result = wsi->AllocateCommandBuffers(chain->device, &cmd_buffer_info,
631 &image->prime.blit_cmd_buffers[i]);
632 if (result != VK_SUCCESS)
633 goto fail;
634
635 const VkCommandBufferBeginInfo begin_info = {
636 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
637 };
638 wsi->BeginCommandBuffer(image->prime.blit_cmd_buffers[i], &begin_info);
639
640 struct VkBufferImageCopy buffer_image_copy = {
641 .bufferOffset = 0,
642 .bufferRowLength = linear_stride / cpp,
643 .bufferImageHeight = 0,
644 .imageSubresource = {
645 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
646 .mipLevel = 0,
647 .baseArrayLayer = 0,
648 .layerCount = 1,
649 },
650 .imageOffset = { .x = 0, .y = 0, .z = 0 },
651 .imageExtent = {
652 .width = pCreateInfo->imageExtent.width,
653 .height = pCreateInfo->imageExtent.height,
654 .depth = 1,
655 },
656 };
657 wsi->CmdCopyImageToBuffer(image->prime.blit_cmd_buffers[i],
658 image->image,
659 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
660 image->prime.buffer,
661 1, &buffer_image_copy);
662
663 result = wsi->EndCommandBuffer(image->prime.blit_cmd_buffers[i]);
664 if (result != VK_SUCCESS)
665 goto fail;
666 }
667
668 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info = {
669 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
670 .pNext = NULL,
671 .memory = image->prime.memory,
672 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
673 };
674 int fd;
675 result = wsi->GetMemoryFdKHR(chain->device, &linear_memory_get_fd_info, &fd);
676 if (result != VK_SUCCESS)
677 goto fail;
678
679 image->drm_modifier = use_modifier ? DRM_FORMAT_MOD_LINEAR : DRM_FORMAT_MOD_INVALID;
680 image->num_planes = 1;
681 image->sizes[0] = linear_size;
682 image->row_pitches[0] = linear_stride;
683 image->offsets[0] = 0;
684 image->fds[0] = fd;
685
686 return VK_SUCCESS;
687
688 fail:
689 wsi_destroy_image(chain, image);
690
691 return result;
692 }
693
694 void
695 wsi_destroy_image(const struct wsi_swapchain *chain,
696 struct wsi_image *image)
697 {
698 const struct wsi_device *wsi = chain->wsi;
699
700 if (image->prime.blit_cmd_buffers) {
701 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
702 wsi->FreeCommandBuffers(chain->device, chain->cmd_pools[i],
703 1, &image->prime.blit_cmd_buffers[i]);
704 }
705 vk_free(&chain->alloc, image->prime.blit_cmd_buffers);
706 }
707
708 wsi->FreeMemory(chain->device, image->memory, &chain->alloc);
709 wsi->DestroyImage(chain->device, image->image, &chain->alloc);
710 wsi->FreeMemory(chain->device, image->prime.memory, &chain->alloc);
711 wsi->DestroyBuffer(chain->device, image->prime.buffer, &chain->alloc);
712 }
713
714 VkResult
715 wsi_common_get_surface_support(struct wsi_device *wsi_device,
716 uint32_t queueFamilyIndex,
717 VkSurfaceKHR _surface,
718 VkBool32* pSupported)
719 {
720 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
721 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
722
723 return iface->get_support(surface, wsi_device,
724 queueFamilyIndex, pSupported);
725 }
726
727 VkResult
728 wsi_common_get_surface_capabilities(struct wsi_device *wsi_device,
729 VkSurfaceKHR _surface,
730 VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
731 {
732 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
733 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
734
735 VkSurfaceCapabilities2KHR caps2 = {
736 .sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
737 };
738
739 VkResult result = iface->get_capabilities2(surface, wsi_device, NULL, &caps2);
740
741 if (result == VK_SUCCESS)
742 *pSurfaceCapabilities = caps2.surfaceCapabilities;
743
744 return result;
745 }
746
747 VkResult
748 wsi_common_get_surface_capabilities2(struct wsi_device *wsi_device,
749 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
750 VkSurfaceCapabilities2KHR *pSurfaceCapabilities)
751 {
752 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pSurfaceInfo->surface);
753 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
754
755 return iface->get_capabilities2(surface, wsi_device, pSurfaceInfo->pNext,
756 pSurfaceCapabilities);
757 }
758
759 VkResult
760 wsi_common_get_surface_capabilities2ext(
761 struct wsi_device *wsi_device,
762 VkSurfaceKHR _surface,
763 VkSurfaceCapabilities2EXT *pSurfaceCapabilities)
764 {
765 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
766 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
767
768 assert(pSurfaceCapabilities->sType ==
769 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT);
770
771 struct wsi_surface_supported_counters counters = {
772 .sType = VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA,
773 .pNext = pSurfaceCapabilities->pNext,
774 .supported_surface_counters = 0,
775 };
776
777 VkSurfaceCapabilities2KHR caps2 = {
778 .sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
779 .pNext = &counters,
780 };
781
782 VkResult result = iface->get_capabilities2(surface, wsi_device, NULL, &caps2);
783
784 if (result == VK_SUCCESS) {
785 VkSurfaceCapabilities2EXT *ext_caps = pSurfaceCapabilities;
786 VkSurfaceCapabilitiesKHR khr_caps = caps2.surfaceCapabilities;
787
788 ext_caps->minImageCount = khr_caps.minImageCount;
789 ext_caps->maxImageCount = khr_caps.maxImageCount;
790 ext_caps->currentExtent = khr_caps.currentExtent;
791 ext_caps->minImageExtent = khr_caps.minImageExtent;
792 ext_caps->maxImageExtent = khr_caps.maxImageExtent;
793 ext_caps->maxImageArrayLayers = khr_caps.maxImageArrayLayers;
794 ext_caps->supportedTransforms = khr_caps.supportedTransforms;
795 ext_caps->currentTransform = khr_caps.currentTransform;
796 ext_caps->supportedCompositeAlpha = khr_caps.supportedCompositeAlpha;
797 ext_caps->supportedUsageFlags = khr_caps.supportedUsageFlags;
798 ext_caps->supportedSurfaceCounters = counters.supported_surface_counters;
799 }
800
801 return result;
802 }
803
804 VkResult
805 wsi_common_get_surface_formats(struct wsi_device *wsi_device,
806 VkSurfaceKHR _surface,
807 uint32_t *pSurfaceFormatCount,
808 VkSurfaceFormatKHR *pSurfaceFormats)
809 {
810 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
811 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
812
813 return iface->get_formats(surface, wsi_device,
814 pSurfaceFormatCount, pSurfaceFormats);
815 }
816
817 VkResult
818 wsi_common_get_surface_formats2(struct wsi_device *wsi_device,
819 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
820 uint32_t *pSurfaceFormatCount,
821 VkSurfaceFormat2KHR *pSurfaceFormats)
822 {
823 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pSurfaceInfo->surface);
824 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
825
826 return iface->get_formats2(surface, wsi_device, pSurfaceInfo->pNext,
827 pSurfaceFormatCount, pSurfaceFormats);
828 }
829
830 VkResult
831 wsi_common_get_surface_present_modes(struct wsi_device *wsi_device,
832 VkSurfaceKHR _surface,
833 uint32_t *pPresentModeCount,
834 VkPresentModeKHR *pPresentModes)
835 {
836 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
837 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
838
839 return iface->get_present_modes(surface, pPresentModeCount,
840 pPresentModes);
841 }
842
843 VkResult
844 wsi_common_get_present_rectangles(struct wsi_device *wsi_device,
845 VkSurfaceKHR _surface,
846 uint32_t* pRectCount,
847 VkRect2D* pRects)
848 {
849 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
850 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
851
852 return iface->get_present_rectangles(surface, wsi_device,
853 pRectCount, pRects);
854 }
855
856 VkResult
857 wsi_common_create_swapchain(struct wsi_device *wsi,
858 VkDevice device,
859 const VkSwapchainCreateInfoKHR *pCreateInfo,
860 const VkAllocationCallbacks *pAllocator,
861 VkSwapchainKHR *pSwapchain)
862 {
863 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
864 struct wsi_interface *iface = wsi->wsi[surface->platform];
865 struct wsi_swapchain *swapchain;
866
867 VkResult result = iface->create_swapchain(surface, device, wsi,
868 pCreateInfo, pAllocator,
869 &swapchain);
870 if (result != VK_SUCCESS)
871 return result;
872
873 *pSwapchain = wsi_swapchain_to_handle(swapchain);
874
875 return VK_SUCCESS;
876 }
877
878 void
879 wsi_common_destroy_swapchain(VkDevice device,
880 VkSwapchainKHR _swapchain,
881 const VkAllocationCallbacks *pAllocator)
882 {
883 WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
884 if (!swapchain)
885 return;
886
887 swapchain->destroy(swapchain, pAllocator);
888 }
889
890 VkResult
891 wsi_common_get_images(VkSwapchainKHR _swapchain,
892 uint32_t *pSwapchainImageCount,
893 VkImage *pSwapchainImages)
894 {
895 WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
896 VK_OUTARRAY_MAKE(images, pSwapchainImages, pSwapchainImageCount);
897
898 for (uint32_t i = 0; i < swapchain->image_count; i++) {
899 vk_outarray_append(&images, image) {
900 *image = swapchain->get_wsi_image(swapchain, i)->image;
901 }
902 }
903
904 return vk_outarray_status(&images);
905 }
906
907 VkResult
908 wsi_common_acquire_next_image2(const struct wsi_device *wsi,
909 VkDevice device,
910 const VkAcquireNextImageInfoKHR *pAcquireInfo,
911 uint32_t *pImageIndex)
912 {
913 WSI_FROM_HANDLE(wsi_swapchain, swapchain, pAcquireInfo->swapchain);
914
915 return swapchain->acquire_next_image(swapchain, pAcquireInfo, pImageIndex);
916 }
917
918 VkResult
919 wsi_common_queue_present(const struct wsi_device *wsi,
920 VkDevice device,
921 VkQueue queue,
922 int queue_family_index,
923 const VkPresentInfoKHR *pPresentInfo)
924 {
925 VkResult final_result = VK_SUCCESS;
926
927 const VkPresentRegionsKHR *regions =
928 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
929
930 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
931 WSI_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
932 VkResult result;
933
934 if (swapchain->fences[0] == VK_NULL_HANDLE) {
935 const VkFenceCreateInfo fence_info = {
936 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
937 .pNext = NULL,
938 .flags = 0,
939 };
940 result = wsi->CreateFence(device, &fence_info,
941 &swapchain->alloc,
942 &swapchain->fences[0]);
943 if (result != VK_SUCCESS)
944 goto fail_present;
945 } else {
946 wsi->ResetFences(device, 1, &swapchain->fences[0]);
947 }
948
949 VkSubmitInfo submit_info = {
950 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
951 .pNext = NULL,
952 };
953
954 VkPipelineStageFlags *stage_flags = NULL;
955 if (i == 0) {
956 /* We only need/want to wait on semaphores once. After that, we're
957 * guaranteed ordering since it all happens on the same queue.
958 */
959 submit_info.waitSemaphoreCount = pPresentInfo->waitSemaphoreCount;
960 submit_info.pWaitSemaphores = pPresentInfo->pWaitSemaphores;
961
962 /* Set up the pWaitDstStageMasks */
963 stage_flags = vk_alloc(&swapchain->alloc,
964 sizeof(VkPipelineStageFlags) *
965 pPresentInfo->waitSemaphoreCount,
966 8,
967 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
968 if (!stage_flags) {
969 result = VK_ERROR_OUT_OF_HOST_MEMORY;
970 goto fail_present;
971 }
972 for (uint32_t s = 0; s < pPresentInfo->waitSemaphoreCount; s++)
973 stage_flags[s] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
974
975 submit_info.pWaitDstStageMask = stage_flags;
976 }
977
978 if (swapchain->use_prime_blit) {
979 /* If we are using prime blits, we need to perform the blit now. The
980 * command buffer is attached to the image.
981 */
982 struct wsi_image *image =
983 swapchain->get_wsi_image(swapchain, pPresentInfo->pImageIndices[i]);
984 submit_info.commandBufferCount = 1;
985 submit_info.pCommandBuffers =
986 &image->prime.blit_cmd_buffers[queue_family_index];
987 }
988
989 result = wsi->QueueSubmit(queue, 1, &submit_info, swapchain->fences[0]);
990 vk_free(&swapchain->alloc, stage_flags);
991 if (result != VK_SUCCESS)
992 goto fail_present;
993
994 const VkPresentRegionKHR *region = NULL;
995 if (regions && regions->pRegions)
996 region = &regions->pRegions[i];
997
998 result = swapchain->queue_present(swapchain,
999 pPresentInfo->pImageIndices[i],
1000 region);
1001 if (result != VK_SUCCESS)
1002 goto fail_present;
1003
1004 VkFence last = swapchain->fences[2];
1005 swapchain->fences[2] = swapchain->fences[1];
1006 swapchain->fences[1] = swapchain->fences[0];
1007 swapchain->fences[0] = last;
1008
1009 if (last != VK_NULL_HANDLE) {
1010 wsi->WaitForFences(device, 1, &last, true, 1);
1011 }
1012
1013 fail_present:
1014 if (pPresentInfo->pResults != NULL)
1015 pPresentInfo->pResults[i] = result;
1016
1017 /* Let the final result be our first unsuccessful result */
1018 if (final_result == VK_SUCCESS)
1019 final_result = result;
1020 }
1021
1022 return final_result;
1023 }