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