configure.ac: split the wayland client/server confusion
[mesa.git] / src / amd / vulkan / radv_wsi.c
1 /*
2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 #include "radv_private.h"
27 #include "radv_meta.h"
28 #include "wsi_common.h"
29 #include "vk_util.h"
30 #include "util/macros.h"
31
32 MAYBE_UNUSED static const struct wsi_callbacks wsi_cbs = {
33 .get_phys_device_format_properties = radv_GetPhysicalDeviceFormatProperties,
34 };
35
36 VkResult
37 radv_init_wsi(struct radv_physical_device *physical_device)
38 {
39 VkResult result;
40
41 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
42
43 #ifdef VK_USE_PLATFORM_XCB_KHR
44 result = wsi_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
45 if (result != VK_SUCCESS)
46 return result;
47 #endif
48
49 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
50 result = wsi_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
51 radv_physical_device_to_handle(physical_device),
52 &wsi_cbs);
53 if (result != VK_SUCCESS) {
54 #ifdef VK_USE_PLATFORM_XCB_KHR
55 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
56 #endif
57 return result;
58 }
59 #endif
60
61 return VK_SUCCESS;
62 }
63
64 void
65 radv_finish_wsi(struct radv_physical_device *physical_device)
66 {
67 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
68 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
69 #endif
70 #ifdef VK_USE_PLATFORM_XCB_KHR
71 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
72 #endif
73 }
74
75 void radv_DestroySurfaceKHR(
76 VkInstance _instance,
77 VkSurfaceKHR _surface,
78 const VkAllocationCallbacks* pAllocator)
79 {
80 RADV_FROM_HANDLE(radv_instance, instance, _instance);
81 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
82
83 vk_free2(&instance->alloc, pAllocator, surface);
84 }
85
86 VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
87 VkPhysicalDevice physicalDevice,
88 uint32_t queueFamilyIndex,
89 VkSurfaceKHR _surface,
90 VkBool32* pSupported)
91 {
92 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
93 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
94 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
95
96 return iface->get_support(surface, &device->wsi_device,
97 &device->instance->alloc,
98 queueFamilyIndex, device->local_fd, true, pSupported);
99 }
100
101 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
102 VkPhysicalDevice physicalDevice,
103 VkSurfaceKHR _surface,
104 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
105 {
106 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
107 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
108 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
109
110 return iface->get_capabilities(surface, pSurfaceCapabilities);
111 }
112
113 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
114 VkPhysicalDevice physicalDevice,
115 VkSurfaceKHR _surface,
116 uint32_t* pSurfaceFormatCount,
117 VkSurfaceFormatKHR* pSurfaceFormats)
118 {
119 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
120 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
121 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
122
123 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
124 pSurfaceFormats);
125 }
126
127 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
128 VkPhysicalDevice physicalDevice,
129 VkSurfaceKHR _surface,
130 uint32_t* pPresentModeCount,
131 VkPresentModeKHR* pPresentModes)
132 {
133 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
134 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
135 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
136
137 return iface->get_present_modes(surface, pPresentModeCount,
138 pPresentModes);
139 }
140
141 static VkResult
142 radv_wsi_image_create(VkDevice device_h,
143 const VkSwapchainCreateInfoKHR *pCreateInfo,
144 const VkAllocationCallbacks* pAllocator,
145 bool needs_linear_copy,
146 bool linear,
147 VkImage *image_p,
148 VkDeviceMemory *memory_p,
149 uint32_t *size,
150 uint32_t *offset,
151 uint32_t *row_pitch, int *fd_p)
152 {
153 VkResult result = VK_SUCCESS;
154 struct radeon_surf *surface;
155 VkImage image_h;
156 struct radv_image *image;
157 int fd;
158 RADV_FROM_HANDLE(radv_device, device, device_h);
159
160 result = radv_image_create(device_h,
161 &(struct radv_image_create_info) {
162 .vk_info =
163 &(VkImageCreateInfo) {
164 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
165 .imageType = VK_IMAGE_TYPE_2D,
166 .format = pCreateInfo->imageFormat,
167 .extent = {
168 .width = pCreateInfo->imageExtent.width,
169 .height = pCreateInfo->imageExtent.height,
170 .depth = 1
171 },
172 .mipLevels = 1,
173 .arrayLayers = 1,
174 .samples = 1,
175 /* FIXME: Need a way to use X tiling to allow scanout */
176 .tiling = linear ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL,
177 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
178 .flags = 0,
179 },
180 .scanout = true},
181 NULL,
182 &image_h);
183 if (result != VK_SUCCESS)
184 return result;
185
186 image = radv_image_from_handle(image_h);
187
188 VkDeviceMemory memory_h;
189
190 const VkMemoryDedicatedAllocateInfoKHR ded_alloc = {
191 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
192 .pNext = NULL,
193 .buffer = VK_NULL_HANDLE,
194 .image = image_h
195 };
196
197 result = radv_AllocateMemory(device_h,
198 &(VkMemoryAllocateInfo) {
199 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
200 .pNext = &ded_alloc,
201 .allocationSize = image->size,
202 .memoryTypeIndex = linear ? 1 : 0,
203 },
204 NULL /* XXX: pAllocator */,
205 &memory_h);
206 if (result != VK_SUCCESS)
207 goto fail_create_image;
208
209 radv_BindImageMemory(device_h, image_h, memory_h, 0);
210
211 /*
212 * return the fd for the image in the no copy mode,
213 * or the fd for the linear image if a copy is required.
214 */
215 if (!needs_linear_copy || (needs_linear_copy && linear)) {
216 RADV_FROM_HANDLE(radv_device_memory, memory, memory_h);
217 if (!radv_get_memory_fd(device, memory, &fd))
218 goto fail_alloc_memory;
219 *fd_p = fd;
220 }
221
222 surface = &image->surface;
223
224 *image_p = image_h;
225 *memory_p = memory_h;
226 *size = image->size;
227 *offset = image->offset;
228
229 if (device->physical_device->rad_info.chip_class >= GFX9)
230 *row_pitch = surface->u.gfx9.surf_pitch * surface->bpe;
231 else
232 *row_pitch = surface->u.legacy.level[0].nblk_x * surface->bpe;
233 return VK_SUCCESS;
234 fail_alloc_memory:
235 radv_FreeMemory(device_h, memory_h, pAllocator);
236
237 fail_create_image:
238 radv_DestroyImage(device_h, image_h, pAllocator);
239
240 return result;
241 }
242
243 static void
244 radv_wsi_image_free(VkDevice device,
245 const VkAllocationCallbacks* pAllocator,
246 VkImage image_h,
247 VkDeviceMemory memory_h)
248 {
249 radv_DestroyImage(device, image_h, pAllocator);
250
251 radv_FreeMemory(device, memory_h, pAllocator);
252 }
253
254 static const struct wsi_image_fns radv_wsi_image_fns = {
255 .create_wsi_image = radv_wsi_image_create,
256 .free_wsi_image = radv_wsi_image_free,
257 };
258
259 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
260 static void
261 radv_wsi_free_prime_command_buffers(struct radv_device *device,
262 struct wsi_swapchain *swapchain)
263 {
264 const int num_pools = NUM_PRIME_POOLS;
265 const int num_images = swapchain->image_count;
266 int i;
267 for (i = 0; i < num_pools; i++) {
268 radv_FreeCommandBuffers(radv_device_to_handle(device),
269 swapchain->cmd_pools[i],
270 swapchain->image_count,
271 &swapchain->cmd_buffers[i * num_images]);
272
273 radv_DestroyCommandPool(radv_device_to_handle(device),
274 swapchain->cmd_pools[i],
275 &swapchain->alloc);
276 }
277 }
278
279 static VkResult
280 radv_wsi_create_prime_command_buffers(struct radv_device *device,
281 const VkAllocationCallbacks *alloc,
282 struct wsi_swapchain *swapchain)
283 {
284 const int num_pools = NUM_PRIME_POOLS;
285 const int num_images = swapchain->image_count;
286 int num_cmd_buffers = num_images * num_pools; //TODO bump to MAX_QUEUE_FAMILIES
287 VkResult result;
288 int i, j;
289
290 swapchain->cmd_buffers = vk_alloc(alloc, (sizeof(VkCommandBuffer) * num_cmd_buffers), 8,
291 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
292 if (!swapchain->cmd_buffers)
293 return VK_ERROR_OUT_OF_HOST_MEMORY;
294
295 memset(swapchain->cmd_buffers, 0, sizeof(VkCommandBuffer) * num_cmd_buffers);
296 memset(swapchain->cmd_pools, 0, sizeof(VkCommandPool) * num_pools);
297 for (i = 0; i < num_pools; i++) {
298 VkCommandPoolCreateInfo pool_create_info;
299
300 pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
301 pool_create_info.pNext = NULL;
302 pool_create_info.flags = 0;
303 pool_create_info.queueFamilyIndex = i;
304
305 result = radv_CreateCommandPool(radv_device_to_handle(device),
306 &pool_create_info, alloc,
307 &swapchain->cmd_pools[i]);
308 if (result != VK_SUCCESS)
309 goto fail;
310
311 VkCommandBufferAllocateInfo cmd_buffer_info;
312 cmd_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
313 cmd_buffer_info.pNext = NULL;
314 cmd_buffer_info.commandPool = swapchain->cmd_pools[i];
315 cmd_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
316 cmd_buffer_info.commandBufferCount = num_images;
317
318 result = radv_AllocateCommandBuffers(radv_device_to_handle(device),
319 &cmd_buffer_info,
320 &swapchain->cmd_buffers[i * num_images]);
321 if (result != VK_SUCCESS)
322 goto fail;
323 for (j = 0; j < num_images; j++) {
324 VkImage image, linear_image;
325 int idx = (i * num_images) + j;
326
327 swapchain->get_image_and_linear(swapchain, j, &image, &linear_image);
328 VkCommandBufferBeginInfo begin_info = {0};
329
330 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
331
332 radv_BeginCommandBuffer(swapchain->cmd_buffers[idx], &begin_info);
333
334 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx]),
335 radv_image_from_handle(image),
336 radv_image_from_handle(linear_image));
337
338 radv_EndCommandBuffer(swapchain->cmd_buffers[idx]);
339 }
340 }
341 return VK_SUCCESS;
342 fail:
343 radv_wsi_free_prime_command_buffers(device, swapchain);
344 return result;
345 }
346
347 VkResult radv_CreateSwapchainKHR(
348 VkDevice _device,
349 const VkSwapchainCreateInfoKHR* pCreateInfo,
350 const VkAllocationCallbacks* pAllocator,
351 VkSwapchainKHR* pSwapchain)
352 {
353 RADV_FROM_HANDLE(radv_device, device, _device);
354 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
355 struct wsi_interface *iface =
356 device->physical_device->wsi_device.wsi[surface->platform];
357 struct wsi_swapchain *swapchain;
358 const VkAllocationCallbacks *alloc;
359 if (pAllocator)
360 alloc = pAllocator;
361 else
362 alloc = &device->alloc;
363 VkResult result = iface->create_swapchain(surface, _device,
364 &device->physical_device->wsi_device,
365 device->physical_device->local_fd,
366 pCreateInfo,
367 alloc, &radv_wsi_image_fns,
368 &swapchain);
369 if (result != VK_SUCCESS)
370 return result;
371
372 if (pAllocator)
373 swapchain->alloc = *pAllocator;
374 else
375 swapchain->alloc = device->alloc;
376
377 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
378 swapchain->fences[i] = VK_NULL_HANDLE;
379
380 if (swapchain->needs_linear_copy) {
381 result = radv_wsi_create_prime_command_buffers(device, alloc,
382 swapchain);
383 if (result != VK_SUCCESS)
384 return result;
385 }
386
387 *pSwapchain = wsi_swapchain_to_handle(swapchain);
388
389 return VK_SUCCESS;
390 }
391
392 void radv_DestroySwapchainKHR(
393 VkDevice _device,
394 VkSwapchainKHR _swapchain,
395 const VkAllocationCallbacks* pAllocator)
396 {
397 RADV_FROM_HANDLE(radv_device, device, _device);
398 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
399 const VkAllocationCallbacks *alloc;
400
401 if (!_swapchain)
402 return;
403
404 if (pAllocator)
405 alloc = pAllocator;
406 else
407 alloc = &device->alloc;
408
409 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
410 if (swapchain->fences[i] != VK_NULL_HANDLE)
411 radv_DestroyFence(_device, swapchain->fences[i], pAllocator);
412 }
413
414 if (swapchain->needs_linear_copy)
415 radv_wsi_free_prime_command_buffers(device, swapchain);
416
417 swapchain->destroy(swapchain, alloc);
418 }
419
420 VkResult radv_GetSwapchainImagesKHR(
421 VkDevice device,
422 VkSwapchainKHR _swapchain,
423 uint32_t* pSwapchainImageCount,
424 VkImage* pSwapchainImages)
425 {
426 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
427
428 return swapchain->get_images(swapchain, pSwapchainImageCount,
429 pSwapchainImages);
430 }
431
432 VkResult radv_AcquireNextImageKHR(
433 VkDevice device,
434 VkSwapchainKHR _swapchain,
435 uint64_t timeout,
436 VkSemaphore semaphore,
437 VkFence _fence,
438 uint32_t* pImageIndex)
439 {
440 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
441 RADV_FROM_HANDLE(radv_fence, fence, _fence);
442
443 VkResult result = swapchain->acquire_next_image(swapchain, timeout, semaphore,
444 pImageIndex);
445
446 if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
447 fence->submitted = true;
448 fence->signalled = true;
449 }
450 return result;
451 }
452
453 VkResult radv_QueuePresentKHR(
454 VkQueue _queue,
455 const VkPresentInfoKHR* pPresentInfo)
456 {
457 RADV_FROM_HANDLE(radv_queue, queue, _queue);
458 VkResult result = VK_SUCCESS;
459 const VkPresentRegionsKHR *regions =
460 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
461
462 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
463 RADV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
464 struct radeon_winsys_cs *cs;
465 const VkPresentRegionKHR *region = NULL;
466 VkResult item_result;
467 struct radv_winsys_sem_info sem_info;
468
469 item_result = radv_alloc_sem_info(&sem_info,
470 pPresentInfo->waitSemaphoreCount,
471 pPresentInfo->pWaitSemaphores,
472 0,
473 NULL);
474 if (pPresentInfo->pResults != NULL)
475 pPresentInfo->pResults[i] = item_result;
476 result = result == VK_SUCCESS ? item_result : result;
477 if (item_result != VK_SUCCESS) {
478 radv_free_sem_info(&sem_info);
479 continue;
480 }
481
482 assert(radv_device_from_handle(swapchain->device) == queue->device);
483 if (swapchain->fences[0] == VK_NULL_HANDLE) {
484 item_result = radv_CreateFence(radv_device_to_handle(queue->device),
485 &(VkFenceCreateInfo) {
486 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
487 .flags = 0,
488 }, &swapchain->alloc, &swapchain->fences[0]);
489 if (pPresentInfo->pResults != NULL)
490 pPresentInfo->pResults[i] = item_result;
491 result = result == VK_SUCCESS ? item_result : result;
492 if (item_result != VK_SUCCESS) {
493 radv_free_sem_info(&sem_info);
494 continue;
495 }
496 } else {
497 radv_ResetFences(radv_device_to_handle(queue->device),
498 1, &swapchain->fences[0]);
499 }
500
501 if (swapchain->needs_linear_copy) {
502 int idx = (queue->queue_family_index * swapchain->image_count) + pPresentInfo->pImageIndices[i];
503 cs = radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx])->cs;
504 } else
505 cs = queue->device->empty_cs[queue->queue_family_index];
506 RADV_FROM_HANDLE(radv_fence, fence, swapchain->fences[0]);
507 struct radeon_winsys_fence *base_fence = fence->fence;
508 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
509
510 queue->device->ws->cs_submit(ctx, queue->queue_idx,
511 &cs,
512 1, NULL, NULL,
513 &sem_info,
514 false, base_fence);
515 fence->submitted = true;
516
517 if (regions && regions->pRegions)
518 region = &regions->pRegions[i];
519
520 item_result = swapchain->queue_present(swapchain,
521 pPresentInfo->pImageIndices[i],
522 region);
523 /* TODO: What if one of them returns OUT_OF_DATE? */
524 if (pPresentInfo->pResults != NULL)
525 pPresentInfo->pResults[i] = item_result;
526 result = result == VK_SUCCESS ? item_result : result;
527 if (item_result != VK_SUCCESS) {
528 radv_free_sem_info(&sem_info);
529 continue;
530 }
531
532 VkFence last = swapchain->fences[2];
533 swapchain->fences[2] = swapchain->fences[1];
534 swapchain->fences[1] = swapchain->fences[0];
535 swapchain->fences[0] = last;
536
537 if (last != VK_NULL_HANDLE) {
538 radv_WaitForFences(radv_device_to_handle(queue->device),
539 1, &last, true, 1);
540 }
541
542 radv_free_sem_info(&sem_info);
543 }
544
545 return VK_SUCCESS;
546 }