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