vulkan/wsi: Store the instance allocator in wsi_device
[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->instance_alloc = *alloc;
43 wsi->pdevice = pdevice;
44
45 #define WSI_GET_CB(func) \
46 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
47 WSI_GET_CB(GetPhysicalDeviceMemoryProperties);
48 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties);
49 #undef WSI_GET_CB
50
51 GetPhysicalDeviceMemoryProperties(pdevice, &wsi->memory_props);
52 GetPhysicalDeviceQueueFamilyProperties(pdevice, &wsi->queue_family_count, NULL);
53
54 #define WSI_GET_CB(func) \
55 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
56 WSI_GET_CB(AllocateMemory);
57 WSI_GET_CB(AllocateCommandBuffers);
58 WSI_GET_CB(BindBufferMemory);
59 WSI_GET_CB(BindImageMemory);
60 WSI_GET_CB(BeginCommandBuffer);
61 WSI_GET_CB(CmdCopyImageToBuffer);
62 WSI_GET_CB(CreateBuffer);
63 WSI_GET_CB(CreateCommandPool);
64 WSI_GET_CB(CreateFence);
65 WSI_GET_CB(CreateImage);
66 WSI_GET_CB(DestroyBuffer);
67 WSI_GET_CB(DestroyCommandPool);
68 WSI_GET_CB(DestroyFence);
69 WSI_GET_CB(DestroyImage);
70 WSI_GET_CB(EndCommandBuffer);
71 WSI_GET_CB(FreeMemory);
72 WSI_GET_CB(FreeCommandBuffers);
73 WSI_GET_CB(GetBufferMemoryRequirements);
74 WSI_GET_CB(GetImageMemoryRequirements);
75 WSI_GET_CB(GetImageSubresourceLayout);
76 WSI_GET_CB(GetMemoryFdKHR);
77 WSI_GET_CB(GetPhysicalDeviceFormatProperties);
78 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR);
79 WSI_GET_CB(ResetFences);
80 WSI_GET_CB(QueueSubmit);
81 WSI_GET_CB(WaitForFences);
82 #undef WSI_GET_CB
83
84 #ifdef VK_USE_PLATFORM_XCB_KHR
85 result = wsi_x11_init_wsi(wsi, alloc);
86 if (result != VK_SUCCESS)
87 goto fail;
88 #endif
89
90 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
91 result = wsi_wl_init_wsi(wsi, alloc, pdevice);
92 if (result != VK_SUCCESS)
93 goto fail;
94 #endif
95
96 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
97 result = wsi_display_init_wsi(wsi, alloc, display_fd);
98 if (result != VK_SUCCESS)
99 goto fail;
100 #endif
101
102 return VK_SUCCESS;
103
104 fail:
105 wsi_device_finish(wsi, alloc);
106 return result;
107 }
108
109 void
110 wsi_device_finish(struct wsi_device *wsi,
111 const VkAllocationCallbacks *alloc)
112 {
113 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
114 wsi_display_finish_wsi(wsi, alloc);
115 #endif
116 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
117 wsi_wl_finish_wsi(wsi, alloc);
118 #endif
119 #ifdef VK_USE_PLATFORM_XCB_KHR
120 wsi_x11_finish_wsi(wsi, alloc);
121 #endif
122 }
123
124 VkResult
125 wsi_swapchain_init(const struct wsi_device *wsi,
126 struct wsi_swapchain *chain,
127 VkDevice device,
128 const VkSwapchainCreateInfoKHR *pCreateInfo,
129 const VkAllocationCallbacks *pAllocator)
130 {
131 VkResult result;
132
133 memset(chain, 0, sizeof(*chain));
134
135 chain->wsi = wsi;
136 chain->device = device;
137 chain->alloc = *pAllocator;
138 chain->use_prime_blit = false;
139
140 chain->cmd_pools =
141 vk_zalloc(pAllocator, sizeof(VkCommandPool) * wsi->queue_family_count, 8,
142 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
143 if (!chain->cmd_pools)
144 return VK_ERROR_OUT_OF_HOST_MEMORY;
145
146 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
147 const VkCommandPoolCreateInfo cmd_pool_info = {
148 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
149 .pNext = NULL,
150 .flags = 0,
151 .queueFamilyIndex = i,
152 };
153 result = wsi->CreateCommandPool(device, &cmd_pool_info, &chain->alloc,
154 &chain->cmd_pools[i]);
155 if (result != VK_SUCCESS)
156 goto fail;
157 }
158
159 return VK_SUCCESS;
160
161 fail:
162 wsi_swapchain_finish(chain);
163 return result;
164 }
165
166 void
167 wsi_swapchain_finish(struct wsi_swapchain *chain)
168 {
169 for (unsigned i = 0; i < ARRAY_SIZE(chain->fences); i++)
170 chain->wsi->DestroyFence(chain->device, chain->fences[i], &chain->alloc);
171
172 for (uint32_t i = 0; i < chain->wsi->queue_family_count; i++) {
173 chain->wsi->DestroyCommandPool(chain->device, chain->cmd_pools[i],
174 &chain->alloc);
175 }
176 vk_free(&chain->alloc, chain->cmd_pools);
177 }
178
179 static uint32_t
180 select_memory_type(const struct wsi_device *wsi,
181 VkMemoryPropertyFlags props,
182 uint32_t type_bits)
183 {
184 for (uint32_t i = 0; i < wsi->memory_props.memoryTypeCount; i++) {
185 const VkMemoryType type = wsi->memory_props.memoryTypes[i];
186 if ((type_bits & (1 << i)) && (type.propertyFlags & props) == props)
187 return i;
188 }
189
190 unreachable("No memory type found");
191 }
192
193 static uint32_t
194 vk_format_size(VkFormat format)
195 {
196 switch (format) {
197 case VK_FORMAT_B8G8R8A8_UNORM:
198 case VK_FORMAT_B8G8R8A8_SRGB:
199 return 4;
200 default:
201 unreachable("Unknown WSI Format");
202 }
203 }
204
205 static inline uint32_t
206 align_u32(uint32_t v, uint32_t a)
207 {
208 assert(a != 0 && a == (a & -a));
209 return (v + a - 1) & ~(a - 1);
210 }
211
212 VkResult
213 wsi_create_native_image(const struct wsi_swapchain *chain,
214 const VkSwapchainCreateInfoKHR *pCreateInfo,
215 uint32_t num_modifier_lists,
216 const uint32_t *num_modifiers,
217 const uint64_t *const *modifiers,
218 struct wsi_image *image)
219 {
220 const struct wsi_device *wsi = chain->wsi;
221 VkResult result;
222
223 memset(image, 0, sizeof(*image));
224 for (int i = 0; i < ARRAY_SIZE(image->fds); i++)
225 image->fds[i] = -1;
226
227 struct wsi_image_create_info image_wsi_info = {
228 .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
229 .pNext = NULL,
230 };
231
232 uint32_t image_modifier_count = 0, modifier_prop_count = 0;
233 struct wsi_format_modifier_properties *modifier_props = NULL;
234 uint64_t *image_modifiers = NULL;
235 if (num_modifier_lists == 0) {
236 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
237 image_wsi_info.scanout = true;
238 } else {
239 /* The winsys can't request modifiers if we don't support them. */
240 assert(wsi->supports_modifiers);
241 struct wsi_format_modifier_properties_list modifier_props_list = {
242 .sType = VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA,
243 .pNext = NULL,
244 };
245 VkFormatProperties2KHR format_props = {
246 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2_KHR,
247 .pNext = &modifier_props_list,
248 };
249 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
250 pCreateInfo->imageFormat,
251 &format_props);
252 assert(modifier_props_list.modifier_count > 0);
253 modifier_props = vk_alloc(&chain->alloc,
254 sizeof(*modifier_props) *
255 modifier_props_list.modifier_count,
256 8,
257 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
258 if (!modifier_props) {
259 result = VK_ERROR_OUT_OF_HOST_MEMORY;
260 goto fail;
261 }
262
263 modifier_props_list.modifier_properties = modifier_props;
264 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
265 pCreateInfo->imageFormat,
266 &format_props);
267 modifier_prop_count = modifier_props_list.modifier_count;
268
269 uint32_t max_modifier_count = 0;
270 for (uint32_t l = 0; l < num_modifier_lists; l++)
271 max_modifier_count = MAX2(max_modifier_count, num_modifiers[l]);
272
273 image_modifiers = vk_alloc(&chain->alloc,
274 sizeof(*image_modifiers) *
275 max_modifier_count,
276 8,
277 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
278 if (!image_modifiers) {
279 result = VK_ERROR_OUT_OF_HOST_MEMORY;
280 goto fail;
281 }
282
283 image_modifier_count = 0;
284 for (uint32_t l = 0; l < num_modifier_lists; l++) {
285 /* Walk the modifier lists and construct a list of supported
286 * modifiers.
287 */
288 for (uint32_t i = 0; i < num_modifiers[l]; i++) {
289 for (uint32_t j = 0; j < modifier_prop_count; j++) {
290 if (modifier_props[j].modifier == modifiers[l][i])
291 image_modifiers[image_modifier_count++] = modifiers[l][i];
292 }
293 }
294
295 /* We only want to take the modifiers from the first list */
296 if (image_modifier_count > 0)
297 break;
298 }
299
300 if (image_modifier_count > 0) {
301 image_wsi_info.modifier_count = image_modifier_count;
302 image_wsi_info.modifiers = image_modifiers;
303 } else {
304 /* TODO: Add a proper error here */
305 assert(!"Failed to find a supported modifier! This should never "
306 "happen because LINEAR should always be available");
307 result = VK_ERROR_OUT_OF_HOST_MEMORY;
308 goto fail;
309 }
310 }
311
312 const VkImageCreateInfo image_info = {
313 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
314 .pNext = &image_wsi_info,
315 .flags = 0,
316 .imageType = VK_IMAGE_TYPE_2D,
317 .format = pCreateInfo->imageFormat,
318 .extent = {
319 .width = pCreateInfo->imageExtent.width,
320 .height = pCreateInfo->imageExtent.height,
321 .depth = 1,
322 },
323 .mipLevels = 1,
324 .arrayLayers = 1,
325 .samples = VK_SAMPLE_COUNT_1_BIT,
326 .tiling = VK_IMAGE_TILING_OPTIMAL,
327 .usage = pCreateInfo->imageUsage,
328 .sharingMode = pCreateInfo->imageSharingMode,
329 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
330 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
331 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
332 };
333 result = wsi->CreateImage(chain->device, &image_info,
334 &chain->alloc, &image->image);
335 if (result != VK_SUCCESS)
336 goto fail;
337
338 VkMemoryRequirements reqs;
339 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
340
341 const struct wsi_memory_allocate_info memory_wsi_info = {
342 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
343 .pNext = NULL,
344 .implicit_sync = true,
345 };
346 const VkExportMemoryAllocateInfoKHR memory_export_info = {
347 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
348 .pNext = &memory_wsi_info,
349 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
350 };
351 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info = {
352 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
353 .pNext = &memory_export_info,
354 .image = image->image,
355 .buffer = VK_NULL_HANDLE,
356 };
357 const VkMemoryAllocateInfo memory_info = {
358 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
359 .pNext = &memory_dedicated_info,
360 .allocationSize = reqs.size,
361 .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
362 reqs.memoryTypeBits),
363 };
364 result = wsi->AllocateMemory(chain->device, &memory_info,
365 &chain->alloc, &image->memory);
366 if (result != VK_SUCCESS)
367 goto fail;
368
369 result = wsi->BindImageMemory(chain->device, image->image,
370 image->memory, 0);
371 if (result != VK_SUCCESS)
372 goto fail;
373
374 const VkMemoryGetFdInfoKHR memory_get_fd_info = {
375 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
376 .pNext = NULL,
377 .memory = image->memory,
378 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
379 };
380 int fd;
381 result = wsi->GetMemoryFdKHR(chain->device, &memory_get_fd_info, &fd);
382 if (result != VK_SUCCESS)
383 goto fail;
384
385 if (num_modifier_lists > 0) {
386 image->drm_modifier = wsi->image_get_modifier(image->image);
387 assert(image->drm_modifier != DRM_FORMAT_MOD_INVALID);
388
389 for (uint32_t j = 0; j < modifier_prop_count; j++) {
390 if (modifier_props[j].modifier == image->drm_modifier) {
391 image->num_planes = modifier_props[j].modifier_plane_count;
392 break;
393 }
394 }
395
396 for (uint32_t p = 0; p < image->num_planes; p++) {
397 const VkImageSubresource image_subresource = {
398 .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT_KHR << p,
399 .mipLevel = 0,
400 .arrayLayer = 0,
401 };
402 VkSubresourceLayout image_layout;
403 wsi->GetImageSubresourceLayout(chain->device, image->image,
404 &image_subresource, &image_layout);
405 image->sizes[p] = image_layout.size;
406 image->row_pitches[p] = image_layout.rowPitch;
407 image->offsets[p] = image_layout.offset;
408 if (p == 0) {
409 image->fds[p] = fd;
410 } else {
411 image->fds[p] = dup(fd);
412 if (image->fds[p] == -1) {
413 for (uint32_t i = 0; i < p; i++)
414 close(image->fds[p]);
415
416 goto fail;
417 }
418 }
419 }
420 } else {
421 const VkImageSubresource image_subresource = {
422 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
423 .mipLevel = 0,
424 .arrayLayer = 0,
425 };
426 VkSubresourceLayout image_layout;
427 wsi->GetImageSubresourceLayout(chain->device, image->image,
428 &image_subresource, &image_layout);
429
430 image->drm_modifier = DRM_FORMAT_MOD_INVALID;
431 image->num_planes = 1;
432 image->sizes[0] = reqs.size;
433 image->row_pitches[0] = image_layout.rowPitch;
434 image->offsets[0] = 0;
435 image->fds[0] = fd;
436 }
437
438 vk_free(&chain->alloc, modifier_props);
439 vk_free(&chain->alloc, image_modifiers);
440
441 return VK_SUCCESS;
442
443 fail:
444 vk_free(&chain->alloc, modifier_props);
445 vk_free(&chain->alloc, image_modifiers);
446 wsi_destroy_image(chain, image);
447
448 return result;
449 }
450
451 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
452
453 VkResult
454 wsi_create_prime_image(const struct wsi_swapchain *chain,
455 const VkSwapchainCreateInfoKHR *pCreateInfo,
456 bool use_modifier,
457 struct wsi_image *image)
458 {
459 const struct wsi_device *wsi = chain->wsi;
460 VkResult result;
461
462 memset(image, 0, sizeof(*image));
463
464 const uint32_t cpp = vk_format_size(pCreateInfo->imageFormat);
465 const uint32_t linear_stride = align_u32(pCreateInfo->imageExtent.width * cpp,
466 WSI_PRIME_LINEAR_STRIDE_ALIGN);
467
468 uint32_t linear_size = linear_stride * pCreateInfo->imageExtent.height;
469 linear_size = align_u32(linear_size, 4096);
470
471 const VkExternalMemoryBufferCreateInfoKHR prime_buffer_external_info = {
472 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR,
473 .pNext = NULL,
474 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
475 };
476 const VkBufferCreateInfo prime_buffer_info = {
477 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
478 .pNext = &prime_buffer_external_info,
479 .size = linear_size,
480 .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
481 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
482 };
483 result = wsi->CreateBuffer(chain->device, &prime_buffer_info,
484 &chain->alloc, &image->prime.buffer);
485 if (result != VK_SUCCESS)
486 goto fail;
487
488 VkMemoryRequirements reqs;
489 wsi->GetBufferMemoryRequirements(chain->device, image->prime.buffer, &reqs);
490 assert(reqs.size <= linear_size);
491
492 const struct wsi_memory_allocate_info memory_wsi_info = {
493 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
494 .pNext = NULL,
495 .implicit_sync = true,
496 };
497 const VkExportMemoryAllocateInfoKHR prime_memory_export_info = {
498 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
499 .pNext = &memory_wsi_info,
500 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
501 };
502 const VkMemoryDedicatedAllocateInfoKHR prime_memory_dedicated_info = {
503 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
504 .pNext = &prime_memory_export_info,
505 .image = VK_NULL_HANDLE,
506 .buffer = image->prime.buffer,
507 };
508 const VkMemoryAllocateInfo prime_memory_info = {
509 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
510 .pNext = &prime_memory_dedicated_info,
511 .allocationSize = linear_size,
512 .memoryTypeIndex = select_memory_type(wsi, 0, reqs.memoryTypeBits),
513 };
514 result = wsi->AllocateMemory(chain->device, &prime_memory_info,
515 &chain->alloc, &image->prime.memory);
516 if (result != VK_SUCCESS)
517 goto fail;
518
519 result = wsi->BindBufferMemory(chain->device, image->prime.buffer,
520 image->prime.memory, 0);
521 if (result != VK_SUCCESS)
522 goto fail;
523
524 const VkImageCreateInfo image_info = {
525 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
526 .pNext = NULL,
527 .flags = 0,
528 .imageType = VK_IMAGE_TYPE_2D,
529 .format = pCreateInfo->imageFormat,
530 .extent = {
531 .width = pCreateInfo->imageExtent.width,
532 .height = pCreateInfo->imageExtent.height,
533 .depth = 1,
534 },
535 .mipLevels = 1,
536 .arrayLayers = 1,
537 .samples = VK_SAMPLE_COUNT_1_BIT,
538 .tiling = VK_IMAGE_TILING_OPTIMAL,
539 .usage = pCreateInfo->imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
540 .sharingMode = pCreateInfo->imageSharingMode,
541 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
542 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
543 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
544 };
545 result = wsi->CreateImage(chain->device, &image_info,
546 &chain->alloc, &image->image);
547 if (result != VK_SUCCESS)
548 goto fail;
549
550 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
551
552 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info = {
553 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
554 .pNext = NULL,
555 .image = image->image,
556 .buffer = VK_NULL_HANDLE,
557 };
558 const VkMemoryAllocateInfo memory_info = {
559 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
560 .pNext = &memory_dedicated_info,
561 .allocationSize = reqs.size,
562 .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
563 reqs.memoryTypeBits),
564 };
565 result = wsi->AllocateMemory(chain->device, &memory_info,
566 &chain->alloc, &image->memory);
567 if (result != VK_SUCCESS)
568 goto fail;
569
570 result = wsi->BindImageMemory(chain->device, image->image,
571 image->memory, 0);
572 if (result != VK_SUCCESS)
573 goto fail;
574
575 image->prime.blit_cmd_buffers =
576 vk_zalloc(&chain->alloc,
577 sizeof(VkCommandBuffer) * wsi->queue_family_count, 8,
578 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
579 if (!image->prime.blit_cmd_buffers) {
580 result = VK_ERROR_OUT_OF_HOST_MEMORY;
581 goto fail;
582 }
583
584 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
585 const VkCommandBufferAllocateInfo cmd_buffer_info = {
586 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
587 .pNext = NULL,
588 .commandPool = chain->cmd_pools[i],
589 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
590 .commandBufferCount = 1,
591 };
592 result = wsi->AllocateCommandBuffers(chain->device, &cmd_buffer_info,
593 &image->prime.blit_cmd_buffers[i]);
594 if (result != VK_SUCCESS)
595 goto fail;
596
597 const VkCommandBufferBeginInfo begin_info = {
598 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
599 };
600 wsi->BeginCommandBuffer(image->prime.blit_cmd_buffers[i], &begin_info);
601
602 struct VkBufferImageCopy buffer_image_copy = {
603 .bufferOffset = 0,
604 .bufferRowLength = linear_stride / cpp,
605 .bufferImageHeight = 0,
606 .imageSubresource = {
607 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
608 .mipLevel = 0,
609 .baseArrayLayer = 0,
610 .layerCount = 1,
611 },
612 .imageOffset = { .x = 0, .y = 0, .z = 0 },
613 .imageExtent = {
614 .width = pCreateInfo->imageExtent.width,
615 .height = pCreateInfo->imageExtent.height,
616 .depth = 1,
617 },
618 };
619 wsi->CmdCopyImageToBuffer(image->prime.blit_cmd_buffers[i],
620 image->image,
621 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
622 image->prime.buffer,
623 1, &buffer_image_copy);
624
625 result = wsi->EndCommandBuffer(image->prime.blit_cmd_buffers[i]);
626 if (result != VK_SUCCESS)
627 goto fail;
628 }
629
630 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info = {
631 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
632 .pNext = NULL,
633 .memory = image->prime.memory,
634 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
635 };
636 int fd;
637 result = wsi->GetMemoryFdKHR(chain->device, &linear_memory_get_fd_info, &fd);
638 if (result != VK_SUCCESS)
639 goto fail;
640
641 image->drm_modifier = use_modifier ? DRM_FORMAT_MOD_LINEAR : DRM_FORMAT_MOD_INVALID;
642 image->num_planes = 1;
643 image->sizes[0] = linear_size;
644 image->row_pitches[0] = linear_stride;
645 image->offsets[0] = 0;
646 image->fds[0] = fd;
647
648 return VK_SUCCESS;
649
650 fail:
651 wsi_destroy_image(chain, image);
652
653 return result;
654 }
655
656 void
657 wsi_destroy_image(const struct wsi_swapchain *chain,
658 struct wsi_image *image)
659 {
660 const struct wsi_device *wsi = chain->wsi;
661
662 if (image->prime.blit_cmd_buffers) {
663 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
664 wsi->FreeCommandBuffers(chain->device, chain->cmd_pools[i],
665 1, &image->prime.blit_cmd_buffers[i]);
666 }
667 vk_free(&chain->alloc, image->prime.blit_cmd_buffers);
668 }
669
670 wsi->FreeMemory(chain->device, image->memory, &chain->alloc);
671 wsi->DestroyImage(chain->device, image->image, &chain->alloc);
672 wsi->FreeMemory(chain->device, image->prime.memory, &chain->alloc);
673 wsi->DestroyBuffer(chain->device, image->prime.buffer, &chain->alloc);
674 }
675
676 VkResult
677 wsi_common_get_surface_support(struct wsi_device *wsi_device,
678 int local_fd,
679 uint32_t queueFamilyIndex,
680 VkSurfaceKHR _surface,
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,
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_image2(const struct wsi_device *wsi,
860 VkDevice device,
861 const VkAcquireNextImageInfoKHR *pAcquireInfo,
862 uint32_t *pImageIndex)
863 {
864 WSI_FROM_HANDLE(wsi_swapchain, swapchain, pAcquireInfo->swapchain);
865
866 return swapchain->acquire_next_image(swapchain, pAcquireInfo, pImageIndex);
867 }
868
869 VkResult
870 wsi_common_queue_present(const struct wsi_device *wsi,
871 VkDevice device,
872 VkQueue queue,
873 int queue_family_index,
874 const VkPresentInfoKHR *pPresentInfo)
875 {
876 VkResult final_result = VK_SUCCESS;
877
878 const VkPresentRegionsKHR *regions =
879 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
880
881 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
882 WSI_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
883 VkResult result;
884
885 if (swapchain->fences[0] == VK_NULL_HANDLE) {
886 const VkFenceCreateInfo fence_info = {
887 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
888 .pNext = NULL,
889 .flags = 0,
890 };
891 result = wsi->CreateFence(device, &fence_info,
892 &swapchain->alloc,
893 &swapchain->fences[0]);
894 if (result != VK_SUCCESS)
895 goto fail_present;
896 } else {
897 wsi->ResetFences(device, 1, &swapchain->fences[0]);
898 }
899
900 VkSubmitInfo submit_info = {
901 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
902 .pNext = NULL,
903 };
904
905 VkPipelineStageFlags *stage_flags = NULL;
906 if (i == 0) {
907 /* We only need/want to wait on semaphores once. After that, we're
908 * guaranteed ordering since it all happens on the same queue.
909 */
910 submit_info.waitSemaphoreCount = pPresentInfo->waitSemaphoreCount,
911 submit_info.pWaitSemaphores = pPresentInfo->pWaitSemaphores,
912
913 /* Set up the pWaitDstStageMasks */
914 stage_flags = vk_alloc(&swapchain->alloc,
915 sizeof(VkPipelineStageFlags) *
916 pPresentInfo->waitSemaphoreCount,
917 8,
918 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
919 if (!stage_flags) {
920 result = VK_ERROR_OUT_OF_HOST_MEMORY;
921 goto fail_present;
922 }
923 for (uint32_t s = 0; s < pPresentInfo->waitSemaphoreCount; s++)
924 stage_flags[s] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
925
926 submit_info.pWaitDstStageMask = stage_flags;
927 }
928
929 if (swapchain->use_prime_blit) {
930 /* If we are using prime blits, we need to perform the blit now. The
931 * command buffer is attached to the image.
932 */
933 struct wsi_image *image =
934 swapchain->get_wsi_image(swapchain, pPresentInfo->pImageIndices[i]);
935 submit_info.commandBufferCount = 1;
936 submit_info.pCommandBuffers =
937 &image->prime.blit_cmd_buffers[queue_family_index];
938 }
939
940 result = wsi->QueueSubmit(queue, 1, &submit_info, swapchain->fences[0]);
941 vk_free(&swapchain->alloc, stage_flags);
942 if (result != VK_SUCCESS)
943 goto fail_present;
944
945 const VkPresentRegionKHR *region = NULL;
946 if (regions && regions->pRegions)
947 region = &regions->pRegions[i];
948
949 result = swapchain->queue_present(swapchain,
950 pPresentInfo->pImageIndices[i],
951 region);
952 if (result != VK_SUCCESS)
953 goto fail_present;
954
955 VkFence last = swapchain->fences[2];
956 swapchain->fences[2] = swapchain->fences[1];
957 swapchain->fences[1] = swapchain->fences[0];
958 swapchain->fences[0] = last;
959
960 if (last != VK_NULL_HANDLE) {
961 wsi->WaitForFences(device, 1, &last, true, 1);
962 }
963
964 fail_present:
965 if (pPresentInfo->pResults != NULL)
966 pPresentInfo->pResults[i] = result;
967
968 /* Let the final result be our first unsuccessful result */
969 if (final_result == VK_SUCCESS)
970 final_result = result;
971 }
972
973 return final_result;
974 }