vulkan/wsi: Only use LINEAR modifier for prime if supported.
[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 goto fail;
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 goto fail;
92 #endif
93
94 return VK_SUCCESS;
95
96 fail:
97 wsi_device_finish(wsi, alloc);
98 return result;
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 bool use_modifier,
446 struct wsi_image *image)
447 {
448 const struct wsi_device *wsi = chain->wsi;
449 VkResult result;
450
451 memset(image, 0, sizeof(*image));
452
453 const uint32_t cpp = vk_format_size(pCreateInfo->imageFormat);
454 const uint32_t linear_stride = align_u32(pCreateInfo->imageExtent.width * cpp,
455 WSI_PRIME_LINEAR_STRIDE_ALIGN);
456
457 uint32_t linear_size = linear_stride * pCreateInfo->imageExtent.height;
458 linear_size = align_u32(linear_size, 4096);
459
460 const VkExternalMemoryBufferCreateInfoKHR prime_buffer_external_info = {
461 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR,
462 .pNext = NULL,
463 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
464 };
465 const VkBufferCreateInfo prime_buffer_info = {
466 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
467 .pNext = &prime_buffer_external_info,
468 .size = linear_size,
469 .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
470 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
471 };
472 result = wsi->CreateBuffer(chain->device, &prime_buffer_info,
473 &chain->alloc, &image->prime.buffer);
474 if (result != VK_SUCCESS)
475 goto fail;
476
477 VkMemoryRequirements reqs;
478 wsi->GetBufferMemoryRequirements(chain->device, image->prime.buffer, &reqs);
479 assert(reqs.size <= linear_size);
480
481 const struct wsi_memory_allocate_info memory_wsi_info = {
482 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
483 .pNext = NULL,
484 .implicit_sync = true,
485 };
486 const VkExportMemoryAllocateInfoKHR prime_memory_export_info = {
487 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
488 .pNext = &memory_wsi_info,
489 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
490 };
491 const VkMemoryDedicatedAllocateInfoKHR prime_memory_dedicated_info = {
492 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
493 .pNext = &prime_memory_export_info,
494 .image = VK_NULL_HANDLE,
495 .buffer = image->prime.buffer,
496 };
497 const VkMemoryAllocateInfo prime_memory_info = {
498 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
499 .pNext = &prime_memory_dedicated_info,
500 .allocationSize = linear_size,
501 .memoryTypeIndex = select_memory_type(wsi, 0, reqs.memoryTypeBits),
502 };
503 result = wsi->AllocateMemory(chain->device, &prime_memory_info,
504 &chain->alloc, &image->prime.memory);
505 if (result != VK_SUCCESS)
506 goto fail;
507
508 result = wsi->BindBufferMemory(chain->device, image->prime.buffer,
509 image->prime.memory, 0);
510 if (result != VK_SUCCESS)
511 goto fail;
512
513 const VkImageCreateInfo image_info = {
514 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
515 .pNext = NULL,
516 .flags = 0,
517 .imageType = VK_IMAGE_TYPE_2D,
518 .format = pCreateInfo->imageFormat,
519 .extent = {
520 .width = pCreateInfo->imageExtent.width,
521 .height = pCreateInfo->imageExtent.height,
522 .depth = 1,
523 },
524 .mipLevels = 1,
525 .arrayLayers = 1,
526 .samples = VK_SAMPLE_COUNT_1_BIT,
527 .tiling = VK_IMAGE_TILING_OPTIMAL,
528 .usage = pCreateInfo->imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
529 .sharingMode = pCreateInfo->imageSharingMode,
530 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
531 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
532 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
533 };
534 result = wsi->CreateImage(chain->device, &image_info,
535 &chain->alloc, &image->image);
536 if (result != VK_SUCCESS)
537 goto fail;
538
539 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
540
541 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info = {
542 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
543 .pNext = NULL,
544 .image = image->image,
545 .buffer = VK_NULL_HANDLE,
546 };
547 const VkMemoryAllocateInfo memory_info = {
548 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
549 .pNext = &memory_dedicated_info,
550 .allocationSize = reqs.size,
551 .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
552 reqs.memoryTypeBits),
553 };
554 result = wsi->AllocateMemory(chain->device, &memory_info,
555 &chain->alloc, &image->memory);
556 if (result != VK_SUCCESS)
557 goto fail;
558
559 result = wsi->BindImageMemory(chain->device, image->image,
560 image->memory, 0);
561 if (result != VK_SUCCESS)
562 goto fail;
563
564 image->prime.blit_cmd_buffers =
565 vk_zalloc(&chain->alloc,
566 sizeof(VkCommandBuffer) * wsi->queue_family_count, 8,
567 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
568 if (!image->prime.blit_cmd_buffers) {
569 result = VK_ERROR_OUT_OF_HOST_MEMORY;
570 goto fail;
571 }
572
573 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
574 const VkCommandBufferAllocateInfo cmd_buffer_info = {
575 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
576 .pNext = NULL,
577 .commandPool = chain->cmd_pools[i],
578 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
579 .commandBufferCount = 1,
580 };
581 result = wsi->AllocateCommandBuffers(chain->device, &cmd_buffer_info,
582 &image->prime.blit_cmd_buffers[i]);
583 if (result != VK_SUCCESS)
584 goto fail;
585
586 const VkCommandBufferBeginInfo begin_info = {
587 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
588 };
589 wsi->BeginCommandBuffer(image->prime.blit_cmd_buffers[i], &begin_info);
590
591 struct VkBufferImageCopy buffer_image_copy = {
592 .bufferOffset = 0,
593 .bufferRowLength = linear_stride / cpp,
594 .bufferImageHeight = 0,
595 .imageSubresource = {
596 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
597 .mipLevel = 0,
598 .baseArrayLayer = 0,
599 .layerCount = 1,
600 },
601 .imageOffset = { .x = 0, .y = 0, .z = 0 },
602 .imageExtent = {
603 .width = pCreateInfo->imageExtent.width,
604 .height = pCreateInfo->imageExtent.height,
605 .depth = 1,
606 },
607 };
608 wsi->CmdCopyImageToBuffer(image->prime.blit_cmd_buffers[i],
609 image->image,
610 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
611 image->prime.buffer,
612 1, &buffer_image_copy);
613
614 result = wsi->EndCommandBuffer(image->prime.blit_cmd_buffers[i]);
615 if (result != VK_SUCCESS)
616 goto fail;
617 }
618
619 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info = {
620 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
621 .pNext = NULL,
622 .memory = image->prime.memory,
623 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
624 };
625 int fd;
626 result = wsi->GetMemoryFdKHR(chain->device, &linear_memory_get_fd_info, &fd);
627 if (result != VK_SUCCESS)
628 goto fail;
629
630 image->drm_modifier = use_modifier ? DRM_FORMAT_MOD_LINEAR : DRM_FORMAT_MOD_INVALID;
631 image->num_planes = 1;
632 image->sizes[0] = linear_size;
633 image->row_pitches[0] = linear_stride;
634 image->offsets[0] = 0;
635 image->fds[0] = fd;
636
637 return VK_SUCCESS;
638
639 fail:
640 wsi_destroy_image(chain, image);
641
642 return result;
643 }
644
645 void
646 wsi_destroy_image(const struct wsi_swapchain *chain,
647 struct wsi_image *image)
648 {
649 const struct wsi_device *wsi = chain->wsi;
650
651 if (image->prime.blit_cmd_buffers) {
652 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
653 wsi->FreeCommandBuffers(chain->device, chain->cmd_pools[i],
654 1, &image->prime.blit_cmd_buffers[i]);
655 }
656 vk_free(&chain->alloc, image->prime.blit_cmd_buffers);
657 }
658
659 wsi->FreeMemory(chain->device, image->memory, &chain->alloc);
660 wsi->DestroyImage(chain->device, image->image, &chain->alloc);
661 wsi->FreeMemory(chain->device, image->prime.memory, &chain->alloc);
662 wsi->DestroyBuffer(chain->device, image->prime.buffer, &chain->alloc);
663 }
664
665 VkResult
666 wsi_common_get_surface_support(struct wsi_device *wsi_device,
667 int local_fd,
668 uint32_t queueFamilyIndex,
669 VkSurfaceKHR _surface,
670 const VkAllocationCallbacks *alloc,
671 VkBool32* pSupported)
672 {
673 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
674 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
675
676 return iface->get_support(surface, wsi_device, alloc,
677 queueFamilyIndex, local_fd, pSupported);
678 }
679
680 VkResult
681 wsi_common_get_surface_capabilities(struct wsi_device *wsi_device,
682 VkSurfaceKHR _surface,
683 VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
684 {
685 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
686 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
687
688 return iface->get_capabilities(surface, pSurfaceCapabilities);
689 }
690
691 VkResult
692 wsi_common_get_surface_capabilities2(struct wsi_device *wsi_device,
693 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
694 VkSurfaceCapabilities2KHR *pSurfaceCapabilities)
695 {
696 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pSurfaceInfo->surface);
697 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
698
699 return iface->get_capabilities2(surface, pSurfaceInfo->pNext,
700 pSurfaceCapabilities);
701 }
702
703 VkResult
704 wsi_common_get_surface_formats(struct wsi_device *wsi_device,
705 VkSurfaceKHR _surface,
706 uint32_t *pSurfaceFormatCount,
707 VkSurfaceFormatKHR *pSurfaceFormats)
708 {
709 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
710 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
711
712 return iface->get_formats(surface, wsi_device,
713 pSurfaceFormatCount, pSurfaceFormats);
714 }
715
716 VkResult
717 wsi_common_get_surface_formats2(struct wsi_device *wsi_device,
718 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
719 uint32_t *pSurfaceFormatCount,
720 VkSurfaceFormat2KHR *pSurfaceFormats)
721 {
722 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pSurfaceInfo->surface);
723 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
724
725 return iface->get_formats2(surface, wsi_device, pSurfaceInfo->pNext,
726 pSurfaceFormatCount, pSurfaceFormats);
727 }
728
729 VkResult
730 wsi_common_get_surface_present_modes(struct wsi_device *wsi_device,
731 VkSurfaceKHR _surface,
732 uint32_t *pPresentModeCount,
733 VkPresentModeKHR *pPresentModes)
734 {
735 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
736 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
737
738 return iface->get_present_modes(surface, pPresentModeCount,
739 pPresentModes);
740 }
741
742 VkResult
743 wsi_common_create_swapchain(struct wsi_device *wsi,
744 VkDevice device,
745 int fd,
746 const VkSwapchainCreateInfoKHR *pCreateInfo,
747 const VkAllocationCallbacks *pAllocator,
748 VkSwapchainKHR *pSwapchain)
749 {
750 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
751 struct wsi_interface *iface = wsi->wsi[surface->platform];
752 struct wsi_swapchain *swapchain;
753
754 VkResult result = iface->create_swapchain(surface, device, wsi, fd,
755 pCreateInfo, pAllocator,
756 &swapchain);
757 if (result != VK_SUCCESS)
758 return result;
759
760 *pSwapchain = wsi_swapchain_to_handle(swapchain);
761
762 return VK_SUCCESS;
763 }
764
765 void
766 wsi_common_destroy_swapchain(VkDevice device,
767 VkSwapchainKHR _swapchain,
768 const VkAllocationCallbacks *pAllocator)
769 {
770 WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
771 if (!swapchain)
772 return;
773
774 swapchain->destroy(swapchain, pAllocator);
775 }
776
777 VkResult
778 wsi_common_get_images(VkSwapchainKHR _swapchain,
779 uint32_t *pSwapchainImageCount,
780 VkImage *pSwapchainImages)
781 {
782 WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
783 VK_OUTARRAY_MAKE(images, pSwapchainImages, pSwapchainImageCount);
784
785 for (uint32_t i = 0; i < swapchain->image_count; i++) {
786 vk_outarray_append(&images, image) {
787 *image = swapchain->get_wsi_image(swapchain, i)->image;
788 }
789 }
790
791 return vk_outarray_status(&images);
792 }
793
794 VkResult
795 wsi_common_acquire_next_image(const struct wsi_device *wsi,
796 VkDevice device,
797 VkSwapchainKHR _swapchain,
798 uint64_t timeout,
799 VkSemaphore semaphore,
800 uint32_t *pImageIndex)
801 {
802 WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
803
804 return swapchain->acquire_next_image(swapchain, timeout,
805 semaphore, pImageIndex);
806 }
807
808 VkResult
809 wsi_common_queue_present(const struct wsi_device *wsi,
810 VkDevice device,
811 VkQueue queue,
812 int queue_family_index,
813 const VkPresentInfoKHR *pPresentInfo)
814 {
815 VkResult final_result = VK_SUCCESS;
816
817 const VkPresentRegionsKHR *regions =
818 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
819
820 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
821 WSI_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
822 VkResult result;
823
824 if (swapchain->fences[0] == VK_NULL_HANDLE) {
825 const VkFenceCreateInfo fence_info = {
826 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
827 .pNext = NULL,
828 .flags = 0,
829 };
830 result = wsi->CreateFence(device, &fence_info,
831 &swapchain->alloc,
832 &swapchain->fences[0]);
833 if (result != VK_SUCCESS)
834 goto fail_present;
835 } else {
836 wsi->ResetFences(device, 1, &swapchain->fences[0]);
837 }
838
839 VkSubmitInfo submit_info = {
840 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
841 .pNext = NULL,
842 };
843
844 VkPipelineStageFlags *stage_flags = NULL;
845 if (i == 0) {
846 /* We only need/want to wait on semaphores once. After that, we're
847 * guaranteed ordering since it all happens on the same queue.
848 */
849 submit_info.waitSemaphoreCount = pPresentInfo->waitSemaphoreCount,
850 submit_info.pWaitSemaphores = pPresentInfo->pWaitSemaphores,
851
852 /* Set up the pWaitDstStageMasks */
853 stage_flags = vk_alloc(&swapchain->alloc,
854 sizeof(VkPipelineStageFlags) *
855 pPresentInfo->waitSemaphoreCount,
856 8,
857 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
858 if (!stage_flags) {
859 result = VK_ERROR_OUT_OF_HOST_MEMORY;
860 goto fail_present;
861 }
862 for (uint32_t s = 0; s < pPresentInfo->waitSemaphoreCount; s++)
863 stage_flags[s] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
864
865 submit_info.pWaitDstStageMask = stage_flags;
866 }
867
868 if (swapchain->use_prime_blit) {
869 /* If we are using prime blits, we need to perform the blit now. The
870 * command buffer is attached to the image.
871 */
872 struct wsi_image *image =
873 swapchain->get_wsi_image(swapchain, pPresentInfo->pImageIndices[i]);
874 submit_info.commandBufferCount = 1;
875 submit_info.pCommandBuffers =
876 &image->prime.blit_cmd_buffers[queue_family_index];
877 }
878
879 result = wsi->QueueSubmit(queue, 1, &submit_info, swapchain->fences[0]);
880 vk_free(&swapchain->alloc, stage_flags);
881 if (result != VK_SUCCESS)
882 goto fail_present;
883
884 const VkPresentRegionKHR *region = NULL;
885 if (regions && regions->pRegions)
886 region = &regions->pRegions[i];
887
888 result = swapchain->queue_present(swapchain,
889 pPresentInfo->pImageIndices[i],
890 region);
891 if (result != VK_SUCCESS)
892 goto fail_present;
893
894 VkFence last = swapchain->fences[2];
895 swapchain->fences[2] = swapchain->fences[1];
896 swapchain->fences[1] = swapchain->fences[0];
897 swapchain->fences[0] = last;
898
899 if (last != VK_NULL_HANDLE) {
900 wsi->WaitForFences(device, 1, &last, true, 1);
901 }
902
903 fail_present:
904 if (pPresentInfo->pResults != NULL)
905 pPresentInfo->pResults[i] = result;
906
907 /* Let the final result be our first unsuccessful result */
908 if (final_result == VK_SUCCESS)
909 final_result = result;
910 }
911
912 return final_result;
913 }