vulkan/wsi/wayland: Copy wl_proxy objects from oldSwapchain if available
[mesa.git] / src / vulkan / wsi / wsi_common_wayland.c
1 /*
2 * Copyright © 2015 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 <wayland-client.h>
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <pthread.h>
33
34 #include "vk_util.h"
35 #include "wsi_common_wayland.h"
36 #include "wayland-drm-client-protocol.h"
37
38 #include <util/hash_table.h>
39 #include <util/u_vector.h>
40
41 #define typed_memcpy(dest, src, count) ({ \
42 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
43 memcpy((dest), (src), (count) * sizeof(*(src))); \
44 })
45
46 struct wsi_wayland;
47
48 struct wsi_wl_display {
49 /* The real wl_display */
50 struct wl_display * wl_display;
51 /* Actually a proxy wrapper around the event queue */
52 struct wl_display * wl_display_wrapper;
53 struct wl_event_queue * queue;
54 struct wl_drm * drm;
55
56 struct wsi_wayland *wsi_wl;
57 /* Vector of VkFormats supported */
58 struct u_vector formats;
59
60 uint32_t capabilities;
61
62 /* Only used for displays created by wsi_wl_display_create */
63 uint32_t refcount;
64 };
65
66 struct wsi_wayland {
67 struct wsi_interface base;
68
69 const VkAllocationCallbacks *alloc;
70 VkPhysicalDevice physical_device;
71
72 const struct wsi_callbacks *cbs;
73 };
74
75 static void
76 wsi_wl_display_add_vk_format(struct wsi_wl_display *display, VkFormat format)
77 {
78 /* Don't add a format that's already in the list */
79 VkFormat *f;
80 u_vector_foreach(f, &display->formats)
81 if (*f == format)
82 return;
83
84 /* Don't add formats that aren't renderable. */
85 VkFormatProperties props;
86
87 display->wsi_wl->cbs->get_phys_device_format_properties(display->wsi_wl->physical_device,
88 format, &props);
89 if (!(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
90 return;
91
92 f = u_vector_add(&display->formats);
93 if (f)
94 *f = format;
95 }
96
97 static void
98 drm_handle_device(void *data, struct wl_drm *drm, const char *name)
99 {
100 }
101
102 static uint32_t
103 wl_drm_format_for_vk_format(VkFormat vk_format, bool alpha)
104 {
105 switch (vk_format) {
106 /* TODO: Figure out what all the formats mean and make this table
107 * correct.
108 */
109 #if 0
110 case VK_FORMAT_R4G4B4A4_UNORM:
111 return alpha ? WL_DRM_FORMAT_ABGR4444 : WL_DRM_FORMAT_XBGR4444;
112 case VK_FORMAT_R5G6B5_UNORM:
113 return WL_DRM_FORMAT_BGR565;
114 case VK_FORMAT_R5G5B5A1_UNORM:
115 return alpha ? WL_DRM_FORMAT_ABGR1555 : WL_DRM_FORMAT_XBGR1555;
116 case VK_FORMAT_R8G8B8_UNORM:
117 return WL_DRM_FORMAT_XBGR8888;
118 case VK_FORMAT_R8G8B8A8_UNORM:
119 return alpha ? WL_DRM_FORMAT_ABGR8888 : WL_DRM_FORMAT_XBGR8888;
120 case VK_FORMAT_R10G10B10A2_UNORM:
121 return alpha ? WL_DRM_FORMAT_ABGR2101010 : WL_DRM_FORMAT_XBGR2101010;
122 case VK_FORMAT_B4G4R4A4_UNORM:
123 return alpha ? WL_DRM_FORMAT_ARGB4444 : WL_DRM_FORMAT_XRGB4444;
124 case VK_FORMAT_B5G6R5_UNORM:
125 return WL_DRM_FORMAT_RGB565;
126 case VK_FORMAT_B5G5R5A1_UNORM:
127 return alpha ? WL_DRM_FORMAT_XRGB1555 : WL_DRM_FORMAT_XRGB1555;
128 #endif
129 case VK_FORMAT_B8G8R8_UNORM:
130 case VK_FORMAT_B8G8R8_SRGB:
131 return WL_DRM_FORMAT_BGRX8888;
132 case VK_FORMAT_B8G8R8A8_UNORM:
133 case VK_FORMAT_B8G8R8A8_SRGB:
134 return alpha ? WL_DRM_FORMAT_ARGB8888 : WL_DRM_FORMAT_XRGB8888;
135 #if 0
136 case VK_FORMAT_B10G10R10A2_UNORM:
137 return alpha ? WL_DRM_FORMAT_ARGB2101010 : WL_DRM_FORMAT_XRGB2101010;
138 #endif
139
140 default:
141 assert(!"Unsupported Vulkan format");
142 return 0;
143 }
144 }
145
146 static void
147 drm_handle_format(void *data, struct wl_drm *drm, uint32_t wl_format)
148 {
149 struct wsi_wl_display *display = data;
150 if (display->formats.element_size == 0)
151 return;
152
153 switch (wl_format) {
154 #if 0
155 case WL_DRM_FORMAT_ABGR4444:
156 case WL_DRM_FORMAT_XBGR4444:
157 wsi_wl_display_add_vk_format(display, VK_FORMAT_R4G4B4A4_UNORM);
158 break;
159 case WL_DRM_FORMAT_BGR565:
160 wsi_wl_display_add_vk_format(display, VK_FORMAT_R5G6B5_UNORM);
161 break;
162 case WL_DRM_FORMAT_ABGR1555:
163 case WL_DRM_FORMAT_XBGR1555:
164 wsi_wl_display_add_vk_format(display, VK_FORMAT_R5G5B5A1_UNORM);
165 break;
166 case WL_DRM_FORMAT_XBGR8888:
167 wsi_wl_display_add_vk_format(display, VK_FORMAT_R8G8B8_UNORM);
168 /* fallthrough */
169 case WL_DRM_FORMAT_ABGR8888:
170 wsi_wl_display_add_vk_format(display, VK_FORMAT_R8G8B8A8_UNORM);
171 break;
172 case WL_DRM_FORMAT_ABGR2101010:
173 case WL_DRM_FORMAT_XBGR2101010:
174 wsi_wl_display_add_vk_format(display, VK_FORMAT_R10G10B10A2_UNORM);
175 break;
176 case WL_DRM_FORMAT_ARGB4444:
177 case WL_DRM_FORMAT_XRGB4444:
178 wsi_wl_display_add_vk_format(display, VK_FORMAT_B4G4R4A4_UNORM);
179 break;
180 case WL_DRM_FORMAT_RGB565:
181 wsi_wl_display_add_vk_format(display, VK_FORMAT_B5G6R5_UNORM);
182 break;
183 case WL_DRM_FORMAT_ARGB1555:
184 case WL_DRM_FORMAT_XRGB1555:
185 wsi_wl_display_add_vk_format(display, VK_FORMAT_B5G5R5A1_UNORM);
186 break;
187 #endif
188 case WL_DRM_FORMAT_XRGB8888:
189 wsi_wl_display_add_vk_format(display, VK_FORMAT_B8G8R8_SRGB);
190 wsi_wl_display_add_vk_format(display, VK_FORMAT_B8G8R8_UNORM);
191 /* fallthrough */
192 case WL_DRM_FORMAT_ARGB8888:
193 wsi_wl_display_add_vk_format(display, VK_FORMAT_B8G8R8A8_SRGB);
194 wsi_wl_display_add_vk_format(display, VK_FORMAT_B8G8R8A8_UNORM);
195 break;
196 #if 0
197 case WL_DRM_FORMAT_ARGB2101010:
198 case WL_DRM_FORMAT_XRGB2101010:
199 wsi_wl_display_add_vk_format(display, VK_FORMAT_B10G10R10A2_UNORM);
200 break;
201 #endif
202 }
203 }
204
205 static void
206 drm_handle_authenticated(void *data, struct wl_drm *drm)
207 {
208 }
209
210 static void
211 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t capabilities)
212 {
213 struct wsi_wl_display *display = data;
214
215 display->capabilities = capabilities;
216 }
217
218 static const struct wl_drm_listener drm_listener = {
219 drm_handle_device,
220 drm_handle_format,
221 drm_handle_authenticated,
222 drm_handle_capabilities,
223 };
224
225 static void
226 registry_handle_global(void *data, struct wl_registry *registry,
227 uint32_t name, const char *interface, uint32_t version)
228 {
229 struct wsi_wl_display *display = data;
230
231 if (strcmp(interface, "wl_drm") == 0) {
232 assert(display->drm == NULL);
233
234 assert(version >= 2);
235 display->drm = wl_registry_bind(registry, name, &wl_drm_interface, 2);
236
237 if (display->drm)
238 wl_drm_add_listener(display->drm, &drm_listener, display);
239 }
240 }
241
242 static void
243 registry_handle_global_remove(void *data, struct wl_registry *registry,
244 uint32_t name)
245 { /* No-op */ }
246
247 static const struct wl_registry_listener registry_listener = {
248 registry_handle_global,
249 registry_handle_global_remove
250 };
251
252 static void
253 wsi_wl_display_finish(struct wsi_wl_display *display)
254 {
255 assert(display->refcount == 0);
256
257 u_vector_finish(&display->formats);
258 if (display->drm)
259 wl_drm_destroy(display->drm);
260 if (display->wl_display_wrapper)
261 wl_proxy_wrapper_destroy(display->wl_display_wrapper);
262 if (display->queue)
263 wl_event_queue_destroy(display->queue);
264 }
265
266 static int
267 wsi_wl_display_init(struct wsi_wayland *wsi_wl,
268 struct wsi_wl_display *display,
269 struct wl_display *wl_display,
270 bool get_format_list)
271 {
272 memset(display, 0, sizeof(*display));
273
274 display->wsi_wl = wsi_wl;
275 display->wl_display = wl_display;
276
277 if (get_format_list) {
278 if (!u_vector_init(&display->formats, sizeof(VkFormat), 8))
279 goto fail;
280 }
281
282 display->queue = wl_display_create_queue(wl_display);
283 if (!display->queue)
284 goto fail;
285
286 display->wl_display_wrapper = wl_proxy_create_wrapper(wl_display);
287 if (!display->wl_display_wrapper)
288 goto fail;
289
290 wl_proxy_set_queue((struct wl_proxy *) display->wl_display_wrapper,
291 display->queue);
292
293 struct wl_registry *registry =
294 wl_display_get_registry(display->wl_display_wrapper);
295 if (!registry)
296 goto fail;
297
298 wl_registry_add_listener(registry, &registry_listener, display);
299
300 /* Round-trip to get the wl_drm global */
301 wl_display_roundtrip_queue(display->wl_display, display->queue);
302
303 if (!display->drm)
304 goto fail_registry;
305
306 /* Round-trip to get wl_drm formats and capabilities */
307 wl_display_roundtrip_queue(display->wl_display, display->queue);
308
309 /* We need prime support */
310 if (!(display->capabilities & WL_DRM_CAPABILITY_PRIME))
311 goto fail_registry;
312
313 /* We don't need this anymore */
314 wl_registry_destroy(registry);
315
316 display->refcount = 0;
317
318 return 0;
319
320 fail_registry:
321 if (registry)
322 wl_registry_destroy(registry);
323
324 fail:
325 wsi_wl_display_finish(display);
326 return -1;
327 }
328
329 static struct wsi_wl_display *
330 wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display)
331 {
332 struct wsi_wl_display *display =
333 vk_alloc(wsi->alloc, sizeof(*display), 8,
334 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
335 if (!display)
336 return NULL;
337
338 if (wsi_wl_display_init(wsi, display, wl_display, true)) {
339 vk_free(wsi->alloc, display);
340 return NULL;
341 }
342
343 display->refcount++;
344
345 return display;
346 }
347
348 static struct wsi_wl_display *
349 wsi_wl_display_ref(struct wsi_wl_display *display)
350 {
351 display->refcount++;
352 return display;
353 }
354
355 static void
356 wsi_wl_display_unref(struct wsi_wl_display *display)
357 {
358 if (display->refcount-- > 1)
359 return;
360
361 struct wsi_wayland *wsi = display->wsi_wl;
362 wsi_wl_display_finish(display);
363 vk_free(wsi->alloc, display);
364 }
365
366 VkBool32
367 wsi_wl_get_presentation_support(struct wsi_device *wsi_device,
368 struct wl_display *wl_display)
369 {
370 struct wsi_wayland *wsi =
371 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
372
373 struct wsi_wl_display display;
374 int ret = wsi_wl_display_init(wsi, &display, wl_display, false);
375 wsi_wl_display_finish(&display);
376
377 return ret == 0;
378 }
379
380 static VkResult
381 wsi_wl_surface_get_support(VkIcdSurfaceBase *surface,
382 struct wsi_device *wsi_device,
383 const VkAllocationCallbacks *alloc,
384 uint32_t queueFamilyIndex,
385 int local_fd,
386 bool can_handle_different_gpu,
387 VkBool32* pSupported)
388 {
389 *pSupported = true;
390
391 return VK_SUCCESS;
392 }
393
394 static const VkPresentModeKHR present_modes[] = {
395 VK_PRESENT_MODE_MAILBOX_KHR,
396 VK_PRESENT_MODE_FIFO_KHR,
397 };
398
399 static VkResult
400 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase *surface,
401 VkSurfaceCapabilitiesKHR* caps)
402 {
403 /* For true mailbox mode, we need at least 4 images:
404 * 1) One to scan out from
405 * 2) One to have queued for scan-out
406 * 3) One to be currently held by the Wayland compositor
407 * 4) One to render to
408 */
409 caps->minImageCount = 4;
410 /* There is no real maximum */
411 caps->maxImageCount = 0;
412
413 caps->currentExtent = (VkExtent2D) { -1, -1 };
414 caps->minImageExtent = (VkExtent2D) { 1, 1 };
415 /* This is the maximum supported size on Intel */
416 caps->maxImageExtent = (VkExtent2D) { 1 << 14, 1 << 14 };
417 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
418 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
419 caps->maxImageArrayLayers = 1;
420
421 caps->supportedCompositeAlpha =
422 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
423 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
424
425 caps->supportedUsageFlags =
426 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
427 VK_IMAGE_USAGE_SAMPLED_BIT |
428 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
429 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
430
431 return VK_SUCCESS;
432 }
433
434 static VkResult
435 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase *surface,
436 const void *info_next,
437 VkSurfaceCapabilities2KHR* caps)
438 {
439 assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
440
441 return wsi_wl_surface_get_capabilities(surface, &caps->surfaceCapabilities);
442 }
443
444 static VkResult
445 wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
446 struct wsi_device *wsi_device,
447 uint32_t* pSurfaceFormatCount,
448 VkSurfaceFormatKHR* pSurfaceFormats)
449 {
450 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
451 struct wsi_wayland *wsi =
452 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
453
454 struct wsi_wl_display display;
455 if (wsi_wl_display_init(wsi, &display, surface->display, true))
456 return VK_ERROR_SURFACE_LOST_KHR;
457
458 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
459
460 VkFormat *disp_fmt;
461 u_vector_foreach(disp_fmt, &display.formats) {
462 vk_outarray_append(&out, out_fmt) {
463 out_fmt->format = *disp_fmt;
464 out_fmt->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
465 }
466 }
467
468 wsi_wl_display_finish(&display);
469
470 return vk_outarray_status(&out);
471 }
472
473 static VkResult
474 wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
475 struct wsi_device *wsi_device,
476 const void *info_next,
477 uint32_t* pSurfaceFormatCount,
478 VkSurfaceFormat2KHR* pSurfaceFormats)
479 {
480 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
481 struct wsi_wayland *wsi =
482 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
483
484 struct wsi_wl_display display;
485 if (wsi_wl_display_init(wsi, &display, surface->display, true))
486 return VK_ERROR_SURFACE_LOST_KHR;
487
488 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
489
490 VkFormat *disp_fmt;
491 u_vector_foreach(disp_fmt, &display.formats) {
492 vk_outarray_append(&out, out_fmt) {
493 out_fmt->surfaceFormat.format = *disp_fmt;
494 out_fmt->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
495 }
496 }
497
498 wsi_wl_display_finish(&display);
499
500 return vk_outarray_status(&out);
501 }
502
503 static VkResult
504 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface,
505 uint32_t* pPresentModeCount,
506 VkPresentModeKHR* pPresentModes)
507 {
508 if (pPresentModes == NULL) {
509 *pPresentModeCount = ARRAY_SIZE(present_modes);
510 return VK_SUCCESS;
511 }
512
513 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
514 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
515
516 if (*pPresentModeCount < ARRAY_SIZE(present_modes))
517 return VK_INCOMPLETE;
518 else
519 return VK_SUCCESS;
520 }
521
522 VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator,
523 const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
524 VkSurfaceKHR *pSurface)
525 {
526 VkIcdSurfaceWayland *surface;
527
528 surface = vk_alloc(pAllocator, sizeof *surface, 8,
529 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
530 if (surface == NULL)
531 return VK_ERROR_OUT_OF_HOST_MEMORY;
532
533 surface->base.platform = VK_ICD_WSI_PLATFORM_WAYLAND;
534 surface->display = pCreateInfo->display;
535 surface->surface = pCreateInfo->surface;
536
537 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
538
539 return VK_SUCCESS;
540 }
541
542 struct wsi_wl_image {
543 VkImage image;
544 VkDeviceMemory memory;
545 struct wl_buffer * buffer;
546 bool busy;
547 };
548
549 struct wsi_wl_swapchain {
550 struct wsi_swapchain base;
551
552 struct wsi_wl_display *display;
553
554 struct wl_surface * surface;
555 uint32_t surface_version;
556 struct wl_drm * drm_wrapper;
557 struct wl_callback * frame;
558
559 VkExtent2D extent;
560 VkFormat vk_format;
561 uint32_t drm_format;
562
563 VkPresentModeKHR present_mode;
564 bool fifo_ready;
565
566 struct wsi_wl_image images[0];
567 };
568
569 static VkResult
570 wsi_wl_swapchain_get_images(struct wsi_swapchain *wsi_chain,
571 uint32_t *pCount, VkImage *pSwapchainImages)
572 {
573 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
574 uint32_t ret_count;
575 VkResult result;
576
577 if (pSwapchainImages == NULL) {
578 *pCount = chain->base.image_count;
579 return VK_SUCCESS;
580 }
581
582 result = VK_SUCCESS;
583 ret_count = chain->base.image_count;
584 if (chain->base.image_count > *pCount) {
585 ret_count = *pCount;
586 result = VK_INCOMPLETE;
587 }
588
589 for (uint32_t i = 0; i < ret_count; i++)
590 pSwapchainImages[i] = chain->images[i].image;
591
592 return result;
593 }
594
595 static VkResult
596 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
597 uint64_t timeout,
598 VkSemaphore semaphore,
599 uint32_t *image_index)
600 {
601 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
602
603 int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
604 chain->display->queue);
605 /* XXX: I'm not sure if out-of-date is the right error here. If
606 * wl_display_dispatch_queue_pending fails it most likely means we got
607 * kicked by the server so this seems more-or-less correct.
608 */
609 if (ret < 0)
610 return VK_ERROR_OUT_OF_DATE_KHR;
611
612 while (1) {
613 for (uint32_t i = 0; i < chain->base.image_count; i++) {
614 if (!chain->images[i].busy) {
615 /* We found a non-busy image */
616 *image_index = i;
617 chain->images[i].busy = true;
618 return VK_SUCCESS;
619 }
620 }
621
622 /* This time we do a blocking dispatch because we can't go
623 * anywhere until we get an event.
624 */
625 int ret = wl_display_roundtrip_queue(chain->display->wl_display,
626 chain->display->queue);
627 if (ret < 0)
628 return VK_ERROR_OUT_OF_DATE_KHR;
629 }
630 }
631
632 static void
633 frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
634 {
635 struct wsi_wl_swapchain *chain = data;
636
637 chain->frame = NULL;
638 chain->fifo_ready = true;
639
640 wl_callback_destroy(callback);
641 }
642
643 static const struct wl_callback_listener frame_listener = {
644 frame_handle_done,
645 };
646
647 static VkResult
648 wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
649 uint32_t image_index,
650 const VkPresentRegionKHR *damage)
651 {
652 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
653
654 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
655 while (!chain->fifo_ready) {
656 int ret = wl_display_dispatch_queue(chain->display->wl_display,
657 chain->display->queue);
658 if (ret < 0)
659 return VK_ERROR_OUT_OF_DATE_KHR;
660 }
661 }
662
663 assert(image_index < chain->base.image_count);
664 wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
665
666 if (chain->surface_version >= 4 && damage &&
667 damage->pRectangles && damage->rectangleCount > 0) {
668 for (unsigned i = 0; i < damage->rectangleCount; i++) {
669 const VkRectLayerKHR *rect = &damage->pRectangles[i];
670 assert(rect->layer == 0);
671 wl_surface_damage_buffer(chain->surface,
672 rect->offset.x, rect->offset.y,
673 rect->extent.width, rect->extent.height);
674 }
675 } else {
676 wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
677 }
678
679 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
680 chain->frame = wl_surface_frame(chain->surface);
681 wl_callback_add_listener(chain->frame, &frame_listener, chain);
682 chain->fifo_ready = false;
683 }
684
685 chain->images[image_index].busy = true;
686 wl_surface_commit(chain->surface);
687 wl_display_flush(chain->display->wl_display);
688
689 return VK_SUCCESS;
690 }
691
692 static void
693 buffer_handle_release(void *data, struct wl_buffer *buffer)
694 {
695 struct wsi_wl_image *image = data;
696
697 assert(image->buffer == buffer);
698
699 image->busy = false;
700 }
701
702 static const struct wl_buffer_listener buffer_listener = {
703 buffer_handle_release,
704 };
705
706 static VkResult
707 wsi_wl_image_init(struct wsi_wl_swapchain *chain,
708 struct wsi_wl_image *image,
709 const VkSwapchainCreateInfoKHR *pCreateInfo,
710 const VkAllocationCallbacks* pAllocator)
711 {
712 VkDevice vk_device = chain->base.device;
713 VkResult result;
714 int fd;
715 uint32_t size;
716 uint32_t row_pitch;
717 uint32_t offset;
718 result = chain->base.image_fns->create_wsi_image(vk_device,
719 pCreateInfo,
720 pAllocator,
721 false,
722 false,
723 &image->image,
724 &image->memory,
725 &size,
726 &offset,
727 &row_pitch,
728 &fd);
729 if (result != VK_SUCCESS)
730 return result;
731
732 image->buffer = wl_drm_create_prime_buffer(chain->drm_wrapper,
733 fd, /* name */
734 chain->extent.width,
735 chain->extent.height,
736 chain->drm_format,
737 offset,
738 row_pitch,
739 0, 0, 0, 0 /* unused */);
740 close(fd);
741
742 if (!image->buffer)
743 goto fail_image;
744
745 wl_buffer_add_listener(image->buffer, &buffer_listener, image);
746
747 return VK_SUCCESS;
748
749 fail_image:
750 chain->base.image_fns->free_wsi_image(vk_device, pAllocator,
751 image->image, image->memory);
752
753 return result;
754 }
755
756 static VkResult
757 wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
758 const VkAllocationCallbacks *pAllocator)
759 {
760 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
761
762 for (uint32_t i = 0; i < chain->base.image_count; i++) {
763 if (chain->images[i].buffer) {
764 wl_buffer_destroy(chain->images[i].buffer);
765 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
766 chain->images[i].image,
767 chain->images[i].memory);
768 }
769 }
770
771 if (chain->frame)
772 wl_callback_destroy(chain->frame);
773 if (chain->surface)
774 wl_proxy_wrapper_destroy(chain->surface);
775 if (chain->drm_wrapper)
776 wl_proxy_wrapper_destroy(chain->drm_wrapper);
777
778 if (chain->display)
779 wsi_wl_display_unref(chain->display);
780
781 vk_free(pAllocator, chain);
782
783 return VK_SUCCESS;
784 }
785
786 static VkResult
787 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
788 VkDevice device,
789 struct wsi_device *wsi_device,
790 int local_fd,
791 const VkSwapchainCreateInfoKHR* pCreateInfo,
792 const VkAllocationCallbacks* pAllocator,
793 const struct wsi_image_fns *image_fns,
794 struct wsi_swapchain **swapchain_out)
795 {
796 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
797 struct wsi_wayland *wsi =
798 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
799 struct wsi_wl_swapchain *chain;
800 VkResult result;
801
802 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
803
804 int num_images = pCreateInfo->minImageCount;
805
806 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
807 chain = vk_alloc(pAllocator, size, 8,
808 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
809 if (chain == NULL)
810 return VK_ERROR_OUT_OF_HOST_MEMORY;
811
812 /* Mark a bunch of stuff as NULL. This way we can just call
813 * destroy_swapchain for cleanup.
814 */
815 for (uint32_t i = 0; i < num_images; i++)
816 chain->images[i].buffer = NULL;
817 chain->surface = NULL;
818 chain->drm_wrapper = NULL;
819 chain->frame = NULL;
820
821 bool alpha = pCreateInfo->compositeAlpha ==
822 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
823
824 chain->base.device = device;
825 chain->base.destroy = wsi_wl_swapchain_destroy;
826 chain->base.get_images = wsi_wl_swapchain_get_images;
827 chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
828 chain->base.queue_present = wsi_wl_swapchain_queue_present;
829 chain->base.image_fns = image_fns;
830 chain->base.present_mode = pCreateInfo->presentMode;
831 chain->base.image_count = num_images;
832 chain->base.needs_linear_copy = false;
833 chain->extent = pCreateInfo->imageExtent;
834 chain->vk_format = pCreateInfo->imageFormat;
835 chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
836
837 if (pCreateInfo->oldSwapchain) {
838 /* If we have an oldSwapchain parameter, copy the display struct over
839 * from the old one so we don't have to fully re-initialize it.
840 */
841 struct wsi_wl_swapchain *old_chain = (void *)pCreateInfo->oldSwapchain;
842 chain->display = wsi_wl_display_ref(old_chain->display);
843 } else {
844 chain->display = wsi_wl_display_create(wsi, surface->display);
845 if (!chain->display) {
846 result = VK_ERROR_INITIALIZATION_FAILED;
847 goto fail;
848 }
849 }
850
851 chain->surface = wl_proxy_create_wrapper(surface->surface);
852 if (!chain->surface) {
853 result = VK_ERROR_INITIALIZATION_FAILED;
854 goto fail;
855 }
856 wl_proxy_set_queue((struct wl_proxy *) chain->surface,
857 chain->display->queue);
858 chain->surface_version = wl_proxy_get_version((void *)surface->surface);
859
860 chain->drm_wrapper = wl_proxy_create_wrapper(chain->display->drm);
861 if (!chain->drm_wrapper) {
862 result = VK_ERROR_INITIALIZATION_FAILED;
863 goto fail;
864 }
865 wl_proxy_set_queue((struct wl_proxy *) chain->drm_wrapper,
866 chain->display->queue);
867
868 chain->fifo_ready = true;
869
870 for (uint32_t i = 0; i < chain->base.image_count; i++) {
871 result = wsi_wl_image_init(chain, &chain->images[i],
872 pCreateInfo, pAllocator);
873 if (result != VK_SUCCESS)
874 goto fail;
875 chain->images[i].busy = false;
876 }
877
878 *swapchain_out = &chain->base;
879
880 return VK_SUCCESS;
881
882 fail:
883 wsi_wl_swapchain_destroy(&chain->base, pAllocator);
884
885 return result;
886 }
887
888 VkResult
889 wsi_wl_init_wsi(struct wsi_device *wsi_device,
890 const VkAllocationCallbacks *alloc,
891 VkPhysicalDevice physical_device,
892 const struct wsi_callbacks *cbs)
893 {
894 struct wsi_wayland *wsi;
895 VkResult result;
896
897 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
898 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
899 if (!wsi) {
900 result = VK_ERROR_OUT_OF_HOST_MEMORY;
901 goto fail;
902 }
903
904 wsi->physical_device = physical_device;
905 wsi->alloc = alloc;
906 wsi->cbs = cbs;
907
908 wsi->base.get_support = wsi_wl_surface_get_support;
909 wsi->base.get_capabilities = wsi_wl_surface_get_capabilities;
910 wsi->base.get_capabilities2 = wsi_wl_surface_get_capabilities2;
911 wsi->base.get_formats = wsi_wl_surface_get_formats;
912 wsi->base.get_formats2 = wsi_wl_surface_get_formats2;
913 wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
914 wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
915
916 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
917
918 return VK_SUCCESS;
919
920 fail:
921 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
922
923 return result;
924 }
925
926 void
927 wsi_wl_finish_wsi(struct wsi_device *wsi_device,
928 const VkAllocationCallbacks *alloc)
929 {
930 struct wsi_wayland *wsi =
931 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
932 if (!wsi)
933 return;
934
935 vk_free(alloc, wsi);
936 }