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