meson: inline `inc_common`
[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 #include <poll.h>
34
35 #include "drm-uapi/drm_fourcc.h"
36
37 #include "vk_util.h"
38 #include "wsi_common_private.h"
39 #include "wsi_common_wayland.h"
40 #include "wayland-drm-client-protocol.h"
41 #include "linux-dmabuf-unstable-v1-client-protocol.h"
42
43 #include <util/hash_table.h>
44 #include <util/timespec.h>
45 #include <util/u_vector.h>
46
47 #define typed_memcpy(dest, src, count) ({ \
48 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
49 memcpy((dest), (src), (count) * sizeof(*(src))); \
50 })
51
52 struct wsi_wayland;
53
54 struct wsi_wl_display_drm {
55 struct wl_drm * wl_drm;
56 struct u_vector formats;
57 uint32_t capabilities;
58 };
59
60 struct wsi_wl_display_dmabuf {
61 struct zwp_linux_dmabuf_v1 * wl_dmabuf;
62 struct u_vector formats;
63 struct {
64 struct u_vector argb8888;
65 struct u_vector xrgb8888;
66 } modifiers;
67 };
68
69 struct wsi_wl_display {
70 /* The real wl_display */
71 struct wl_display * wl_display;
72 /* Actually a proxy wrapper around the event queue */
73 struct wl_display * wl_display_wrapper;
74 struct wl_event_queue * queue;
75
76 struct wsi_wl_display_drm drm;
77 struct wsi_wl_display_dmabuf dmabuf;
78
79 struct wsi_wayland *wsi_wl;
80
81 /* Points to formats in wsi_wl_display_drm or wsi_wl_display_dmabuf */
82 struct u_vector * formats;
83
84 /* Only used for displays created by wsi_wl_display_create */
85 uint32_t refcount;
86 };
87
88 struct wsi_wayland {
89 struct wsi_interface base;
90
91 struct wsi_device *wsi;
92
93 const VkAllocationCallbacks *alloc;
94 VkPhysicalDevice physical_device;
95 };
96
97 static void
98 wsi_wl_display_add_vk_format(struct wsi_wl_display *display,
99 struct u_vector *formats, VkFormat format)
100 {
101 /* Don't add a format that's already in the list */
102 VkFormat *f;
103 u_vector_foreach(f, formats)
104 if (*f == format)
105 return;
106
107 /* Don't add formats that aren't renderable. */
108 VkFormatProperties props;
109
110 display->wsi_wl->wsi->GetPhysicalDeviceFormatProperties(display->wsi_wl->physical_device,
111 format, &props);
112 if (!(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
113 return;
114
115 f = u_vector_add(formats);
116 if (f)
117 *f = format;
118 }
119
120 static void
121 wsi_wl_display_add_wl_format(struct wsi_wl_display *display,
122 struct u_vector *formats, uint32_t wl_format)
123 {
124 switch (wl_format) {
125 #if 0
126 case WL_DRM_FORMAT_ABGR4444:
127 case WL_DRM_FORMAT_XBGR4444:
128 wsi_wl_display_add_vk_format(display, formats,
129 VK_FORMAT_R4G4B4A4_UNORM);
130 break;
131 case WL_DRM_FORMAT_BGR565:
132 wsi_wl_display_add_vk_format(display, formats,
133 VK_FORMAT_R5G6B5_UNORM);
134 break;
135 case WL_DRM_FORMAT_ABGR1555:
136 case WL_DRM_FORMAT_XBGR1555:
137 wsi_wl_display_add_vk_format(display, formats,
138 VK_FORMAT_R5G5B5A1_UNORM);
139 break;
140 case WL_DRM_FORMAT_XBGR8888:
141 wsi_wl_display_add_vk_format(display, formats,
142 VK_FORMAT_R8G8B8_UNORM);
143 /* fallthrough */
144 case WL_DRM_FORMAT_ABGR8888:
145 wsi_wl_display_add_vk_format(display, formats,
146 VK_FORMAT_R8G8B8A8_UNORM);
147 break;
148 case WL_DRM_FORMAT_ABGR2101010:
149 case WL_DRM_FORMAT_XBGR2101010:
150 wsi_wl_display_add_vk_format(display, formats,
151 VK_FORMAT_R10G10B10A2_UNORM);
152 break;
153 case WL_DRM_FORMAT_ARGB4444:
154 case WL_DRM_FORMAT_XRGB4444:
155 wsi_wl_display_add_vk_format(display, formats,
156 VK_FORMAT_B4G4R4A4_UNORM);
157 break;
158 case WL_DRM_FORMAT_RGB565:
159 wsi_wl_display_add_vk_format(display, formats,
160 VK_FORMAT_B5G6R5_UNORM);
161 break;
162 case WL_DRM_FORMAT_ARGB1555:
163 case WL_DRM_FORMAT_XRGB1555:
164 wsi_wl_display_add_vk_format(display, formats,
165 VK_FORMAT_B5G5R5A1_UNORM);
166 break;
167 #endif
168 case WL_DRM_FORMAT_XRGB8888:
169 wsi_wl_display_add_vk_format(display, formats,
170 VK_FORMAT_B8G8R8_SRGB);
171 wsi_wl_display_add_vk_format(display, formats,
172 VK_FORMAT_B8G8R8_UNORM);
173 /* fallthrough */
174 case WL_DRM_FORMAT_ARGB8888:
175 wsi_wl_display_add_vk_format(display, formats,
176 VK_FORMAT_B8G8R8A8_SRGB);
177 wsi_wl_display_add_vk_format(display, formats,
178 VK_FORMAT_B8G8R8A8_UNORM);
179 break;
180 #if 0
181 case WL_DRM_FORMAT_ARGB2101010:
182 case WL_DRM_FORMAT_XRGB2101010:
183 wsi_wl_display_add_vk_format(display, formats,
184 VK_FORMAT_B10G10R10A2_UNORM);
185 break;
186 #endif
187 }
188 }
189
190 static void
191 drm_handle_device(void *data, struct wl_drm *drm, const char *name)
192 {
193 }
194
195 static uint32_t
196 wl_drm_format_for_vk_format(VkFormat vk_format, bool alpha)
197 {
198 switch (vk_format) {
199 /* TODO: Figure out what all the formats mean and make this table
200 * correct.
201 */
202 #if 0
203 case VK_FORMAT_R4G4B4A4_UNORM:
204 return alpha ? WL_DRM_FORMAT_ABGR4444 : WL_DRM_FORMAT_XBGR4444;
205 case VK_FORMAT_R5G6B5_UNORM:
206 return WL_DRM_FORMAT_BGR565;
207 case VK_FORMAT_R5G5B5A1_UNORM:
208 return alpha ? WL_DRM_FORMAT_ABGR1555 : WL_DRM_FORMAT_XBGR1555;
209 case VK_FORMAT_R8G8B8_UNORM:
210 return WL_DRM_FORMAT_XBGR8888;
211 case VK_FORMAT_R8G8B8A8_UNORM:
212 return alpha ? WL_DRM_FORMAT_ABGR8888 : WL_DRM_FORMAT_XBGR8888;
213 case VK_FORMAT_R10G10B10A2_UNORM:
214 return alpha ? WL_DRM_FORMAT_ABGR2101010 : WL_DRM_FORMAT_XBGR2101010;
215 case VK_FORMAT_B4G4R4A4_UNORM:
216 return alpha ? WL_DRM_FORMAT_ARGB4444 : WL_DRM_FORMAT_XRGB4444;
217 case VK_FORMAT_B5G6R5_UNORM:
218 return WL_DRM_FORMAT_RGB565;
219 case VK_FORMAT_B5G5R5A1_UNORM:
220 return alpha ? WL_DRM_FORMAT_XRGB1555 : WL_DRM_FORMAT_XRGB1555;
221 #endif
222 case VK_FORMAT_B8G8R8_UNORM:
223 case VK_FORMAT_B8G8R8_SRGB:
224 return WL_DRM_FORMAT_BGRX8888;
225 case VK_FORMAT_B8G8R8A8_UNORM:
226 case VK_FORMAT_B8G8R8A8_SRGB:
227 return alpha ? WL_DRM_FORMAT_ARGB8888 : WL_DRM_FORMAT_XRGB8888;
228 #if 0
229 case VK_FORMAT_B10G10R10A2_UNORM:
230 return alpha ? WL_DRM_FORMAT_ARGB2101010 : WL_DRM_FORMAT_XRGB2101010;
231 #endif
232
233 default:
234 assert(!"Unsupported Vulkan format");
235 return 0;
236 }
237 }
238
239 static void
240 drm_handle_format(void *data, struct wl_drm *drm, uint32_t wl_format)
241 {
242 struct wsi_wl_display *display = data;
243 if (display->drm.formats.element_size == 0)
244 return;
245
246 wsi_wl_display_add_wl_format(display, &display->drm.formats, wl_format);
247 }
248
249 static void
250 drm_handle_authenticated(void *data, struct wl_drm *drm)
251 {
252 }
253
254 static void
255 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t capabilities)
256 {
257 struct wsi_wl_display *display = data;
258
259 display->drm.capabilities = capabilities;
260 }
261
262 static const struct wl_drm_listener drm_listener = {
263 drm_handle_device,
264 drm_handle_format,
265 drm_handle_authenticated,
266 drm_handle_capabilities,
267 };
268
269 static void
270 dmabuf_handle_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
271 uint32_t format)
272 {
273 /* Formats are implicitly advertised by the modifier event, so we ignore
274 * them here. */
275 }
276
277 static void
278 dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
279 uint32_t format, uint32_t modifier_hi,
280 uint32_t modifier_lo)
281 {
282 struct wsi_wl_display *display = data;
283 uint64_t *mod = NULL;
284
285 /* If we're not fetching formats, don't fetch modifiers either. */
286 if (display->dmabuf.formats.element_size == 0)
287 return;
288
289 if (modifier_hi == (DRM_FORMAT_MOD_INVALID >> 32) &&
290 modifier_lo == (DRM_FORMAT_MOD_INVALID & 0xffffffff))
291 return;
292
293 switch (format) {
294 case WL_DRM_FORMAT_ARGB8888:
295 wsi_wl_display_add_wl_format(display, &display->dmabuf.formats, format);
296 mod = u_vector_add(&display->dmabuf.modifiers.argb8888);
297 break;
298 case WL_DRM_FORMAT_XRGB8888:
299 wsi_wl_display_add_wl_format(display, &display->dmabuf.formats, format);
300 mod = u_vector_add(&display->dmabuf.modifiers.xrgb8888);
301 break;
302 default:
303 break;
304 }
305
306 if (!mod)
307 return;
308
309 *mod = (uint64_t) modifier_hi << 32;
310 *mod |= (uint64_t) (modifier_lo & 0xffffffff);
311 }
312
313 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
314 dmabuf_handle_format,
315 dmabuf_handle_modifier,
316 };
317
318 static void
319 registry_handle_global(void *data, struct wl_registry *registry,
320 uint32_t name, const char *interface, uint32_t version)
321 {
322 struct wsi_wl_display *display = data;
323
324 if (strcmp(interface, "wl_drm") == 0) {
325 assert(display->drm.wl_drm == NULL);
326
327 assert(version >= 2);
328 display->drm.wl_drm =
329 wl_registry_bind(registry, name, &wl_drm_interface, 2);
330 wl_drm_add_listener(display->drm.wl_drm, &drm_listener, display);
331 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3 &&
332 display->wsi_wl->wsi->supports_modifiers) {
333 display->dmabuf.wl_dmabuf =
334 wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface, 3);
335 zwp_linux_dmabuf_v1_add_listener(display->dmabuf.wl_dmabuf,
336 &dmabuf_listener, display);
337 }
338 }
339
340 static void
341 registry_handle_global_remove(void *data, struct wl_registry *registry,
342 uint32_t name)
343 { /* No-op */ }
344
345 static const struct wl_registry_listener registry_listener = {
346 registry_handle_global,
347 registry_handle_global_remove
348 };
349
350 static void
351 wsi_wl_display_finish(struct wsi_wl_display *display)
352 {
353 assert(display->refcount == 0);
354
355 u_vector_finish(&display->drm.formats);
356 u_vector_finish(&display->dmabuf.formats);
357 u_vector_finish(&display->dmabuf.modifiers.argb8888);
358 u_vector_finish(&display->dmabuf.modifiers.xrgb8888);
359 if (display->drm.wl_drm)
360 wl_drm_destroy(display->drm.wl_drm);
361 if (display->dmabuf.wl_dmabuf)
362 zwp_linux_dmabuf_v1_destroy(display->dmabuf.wl_dmabuf);
363 if (display->wl_display_wrapper)
364 wl_proxy_wrapper_destroy(display->wl_display_wrapper);
365 if (display->queue)
366 wl_event_queue_destroy(display->queue);
367 }
368
369 static VkResult
370 wsi_wl_display_init(struct wsi_wayland *wsi_wl,
371 struct wsi_wl_display *display,
372 struct wl_display *wl_display,
373 bool get_format_list)
374 {
375 VkResult result = VK_SUCCESS;
376 memset(display, 0, sizeof(*display));
377
378 display->wsi_wl = wsi_wl;
379 display->wl_display = wl_display;
380
381 if (get_format_list) {
382 if (!u_vector_init(&display->drm.formats, sizeof(VkFormat), 8) ||
383 !u_vector_init(&display->dmabuf.formats, sizeof(VkFormat), 8) ||
384 !u_vector_init(&display->dmabuf.modifiers.argb8888,
385 sizeof(uint64_t), 32) ||
386 !u_vector_init(&display->dmabuf.modifiers.xrgb8888,
387 sizeof(uint64_t), 32)) {
388 result = VK_ERROR_OUT_OF_HOST_MEMORY;
389 goto fail;
390 }
391 }
392
393 display->queue = wl_display_create_queue(wl_display);
394 if (!display->queue) {
395 result = VK_ERROR_OUT_OF_HOST_MEMORY;
396 goto fail;
397 }
398
399 display->wl_display_wrapper = wl_proxy_create_wrapper(wl_display);
400 if (!display->wl_display_wrapper) {
401 result = VK_ERROR_OUT_OF_HOST_MEMORY;
402 goto fail;
403 }
404
405 wl_proxy_set_queue((struct wl_proxy *) display->wl_display_wrapper,
406 display->queue);
407
408 struct wl_registry *registry =
409 wl_display_get_registry(display->wl_display_wrapper);
410 if (!registry) {
411 result = VK_ERROR_OUT_OF_HOST_MEMORY;
412 goto fail;
413 }
414
415 wl_registry_add_listener(registry, &registry_listener, display);
416
417 /* Round-trip to get wl_drms and zwp_linux_dmabuf_v1 globals */
418 wl_display_roundtrip_queue(display->wl_display, display->queue);
419
420 /* Round-trip again to get formats, modifiers and capabilities */
421 if (display->drm.wl_drm || display->dmabuf.wl_dmabuf)
422 wl_display_roundtrip_queue(display->wl_display, display->queue);
423
424 if (wsi_wl->wsi->force_bgra8_unorm_first) {
425 /* Find BGRA8_UNORM in the list and swap it to the first position if we
426 * can find it. Some apps get confused if SRGB is first in the list.
427 */
428 VkFormat *first_fmt = u_vector_head(display->formats);
429 VkFormat *iter_fmt;
430 u_vector_foreach(iter_fmt, display->formats) {
431 if (*iter_fmt == VK_FORMAT_B8G8R8A8_UNORM) {
432 *iter_fmt = *first_fmt;
433 *first_fmt = VK_FORMAT_B8G8R8A8_UNORM;
434 break;
435 }
436 }
437 }
438
439 /* We need prime support for wl_drm */
440 if (display->drm.wl_drm &&
441 (display->drm.capabilities & WL_DRM_CAPABILITY_PRIME)) {
442 display->formats = &display->drm.formats;
443 } else if (display->dmabuf.wl_dmabuf) {
444 display->formats = &display->dmabuf.formats;
445 }
446
447 if (!display->formats) {
448 result = VK_ERROR_SURFACE_LOST_KHR;
449 goto fail_registry;
450 }
451
452 /* We don't need this anymore */
453 wl_registry_destroy(registry);
454
455 display->refcount = 0;
456
457 return VK_SUCCESS;
458
459 fail_registry:
460 if (registry)
461 wl_registry_destroy(registry);
462
463 fail:
464 wsi_wl_display_finish(display);
465 return result;
466 }
467
468 static VkResult
469 wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display,
470 struct wsi_wl_display **display_out)
471 {
472 struct wsi_wl_display *display =
473 vk_alloc(wsi->alloc, sizeof(*display), 8,
474 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
475 if (!display)
476 return VK_ERROR_OUT_OF_HOST_MEMORY;
477
478 VkResult result = wsi_wl_display_init(wsi, display, wl_display, true);
479 if (result != VK_SUCCESS) {
480 vk_free(wsi->alloc, display);
481 return result;
482 }
483
484 display->refcount++;
485 *display_out = display;
486
487 return result;
488 }
489
490 static struct wsi_wl_display *
491 wsi_wl_display_ref(struct wsi_wl_display *display)
492 {
493 display->refcount++;
494 return display;
495 }
496
497 static void
498 wsi_wl_display_unref(struct wsi_wl_display *display)
499 {
500 if (display->refcount-- > 1)
501 return;
502
503 struct wsi_wayland *wsi = display->wsi_wl;
504 wsi_wl_display_finish(display);
505 vk_free(wsi->alloc, display);
506 }
507
508 VkBool32
509 wsi_wl_get_presentation_support(struct wsi_device *wsi_device,
510 struct wl_display *wl_display)
511 {
512 struct wsi_wayland *wsi =
513 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
514
515 struct wsi_wl_display display;
516 VkResult ret = wsi_wl_display_init(wsi, &display, wl_display, false);
517 if (ret == VK_SUCCESS)
518 wsi_wl_display_finish(&display);
519
520 return ret == VK_SUCCESS;
521 }
522
523 static VkResult
524 wsi_wl_surface_get_support(VkIcdSurfaceBase *surface,
525 struct wsi_device *wsi_device,
526 uint32_t queueFamilyIndex,
527 VkBool32* pSupported)
528 {
529 *pSupported = true;
530
531 return VK_SUCCESS;
532 }
533
534 static const VkPresentModeKHR present_modes[] = {
535 VK_PRESENT_MODE_MAILBOX_KHR,
536 VK_PRESENT_MODE_FIFO_KHR,
537 };
538
539 static VkResult
540 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase *surface,
541 struct wsi_device *wsi_device,
542 VkSurfaceCapabilitiesKHR* caps)
543 {
544 /* For true mailbox mode, we need at least 4 images:
545 * 1) One to scan out from
546 * 2) One to have queued for scan-out
547 * 3) One to be currently held by the Wayland compositor
548 * 4) One to render to
549 */
550 caps->minImageCount = 4;
551 /* There is no real maximum */
552 caps->maxImageCount = 0;
553
554 caps->currentExtent = (VkExtent2D) { -1, -1 };
555 caps->minImageExtent = (VkExtent2D) { 1, 1 };
556 caps->maxImageExtent = (VkExtent2D) {
557 wsi_device->maxImageDimension2D,
558 wsi_device->maxImageDimension2D,
559 };
560
561 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
562 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
563 caps->maxImageArrayLayers = 1;
564
565 caps->supportedCompositeAlpha =
566 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
567 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
568
569 caps->supportedUsageFlags =
570 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
571 VK_IMAGE_USAGE_SAMPLED_BIT |
572 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
573 VK_IMAGE_USAGE_STORAGE_BIT |
574 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
575
576 return VK_SUCCESS;
577 }
578
579 static VkResult
580 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase *surface,
581 struct wsi_device *wsi_device,
582 const void *info_next,
583 VkSurfaceCapabilities2KHR* caps)
584 {
585 assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
586
587 VkResult result =
588 wsi_wl_surface_get_capabilities(surface, wsi_device,
589 &caps->surfaceCapabilities);
590
591 vk_foreach_struct(ext, caps->pNext) {
592 switch (ext->sType) {
593 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
594 VkSurfaceProtectedCapabilitiesKHR *protected = (void *)ext;
595 protected->supportsProtected = VK_FALSE;
596 break;
597 }
598
599 default:
600 /* Ignored */
601 break;
602 }
603 }
604
605 return result;
606 }
607
608 static VkResult
609 wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
610 struct wsi_device *wsi_device,
611 uint32_t* pSurfaceFormatCount,
612 VkSurfaceFormatKHR* pSurfaceFormats)
613 {
614 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
615 struct wsi_wayland *wsi =
616 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
617
618 struct wsi_wl_display display;
619 if (wsi_wl_display_init(wsi, &display, surface->display, true))
620 return VK_ERROR_SURFACE_LOST_KHR;
621
622 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
623
624 VkFormat *disp_fmt;
625 u_vector_foreach(disp_fmt, display.formats) {
626 vk_outarray_append(&out, out_fmt) {
627 out_fmt->format = *disp_fmt;
628 out_fmt->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
629 }
630 }
631
632 wsi_wl_display_finish(&display);
633
634 return vk_outarray_status(&out);
635 }
636
637 static VkResult
638 wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
639 struct wsi_device *wsi_device,
640 const void *info_next,
641 uint32_t* pSurfaceFormatCount,
642 VkSurfaceFormat2KHR* pSurfaceFormats)
643 {
644 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
645 struct wsi_wayland *wsi =
646 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
647
648 struct wsi_wl_display display;
649 if (wsi_wl_display_init(wsi, &display, surface->display, true))
650 return VK_ERROR_SURFACE_LOST_KHR;
651
652 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
653
654 VkFormat *disp_fmt;
655 u_vector_foreach(disp_fmt, display.formats) {
656 vk_outarray_append(&out, out_fmt) {
657 out_fmt->surfaceFormat.format = *disp_fmt;
658 out_fmt->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
659 }
660 }
661
662 wsi_wl_display_finish(&display);
663
664 return vk_outarray_status(&out);
665 }
666
667 static VkResult
668 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface,
669 uint32_t* pPresentModeCount,
670 VkPresentModeKHR* pPresentModes)
671 {
672 if (pPresentModes == NULL) {
673 *pPresentModeCount = ARRAY_SIZE(present_modes);
674 return VK_SUCCESS;
675 }
676
677 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
678 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
679
680 if (*pPresentModeCount < ARRAY_SIZE(present_modes))
681 return VK_INCOMPLETE;
682 else
683 return VK_SUCCESS;
684 }
685
686 static VkResult
687 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase *surface,
688 struct wsi_device *wsi_device,
689 uint32_t* pRectCount,
690 VkRect2D* pRects)
691 {
692 VK_OUTARRAY_MAKE(out, pRects, pRectCount);
693
694 vk_outarray_append(&out, rect) {
695 /* We don't know a size so just return the usual "I don't know." */
696 *rect = (VkRect2D) {
697 .offset = { 0, 0 },
698 .extent = { -1, -1 },
699 };
700 }
701
702 return vk_outarray_status(&out);
703 }
704
705 VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator,
706 const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
707 VkSurfaceKHR *pSurface)
708 {
709 VkIcdSurfaceWayland *surface;
710
711 surface = vk_alloc(pAllocator, sizeof *surface, 8,
712 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
713 if (surface == NULL)
714 return VK_ERROR_OUT_OF_HOST_MEMORY;
715
716 surface->base.platform = VK_ICD_WSI_PLATFORM_WAYLAND;
717 surface->display = pCreateInfo->display;
718 surface->surface = pCreateInfo->surface;
719
720 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
721
722 return VK_SUCCESS;
723 }
724
725 struct wsi_wl_image {
726 struct wsi_image base;
727 struct wl_buffer * buffer;
728 bool busy;
729 };
730
731 struct wsi_wl_swapchain {
732 struct wsi_swapchain base;
733
734 struct wsi_wl_display *display;
735
736 struct wl_surface * surface;
737 uint32_t surface_version;
738
739 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
740 * zwp_linux_dmabuf_v1 should be used.
741 */
742 struct wl_drm * drm_wrapper;
743
744 struct wl_callback * frame;
745
746 VkExtent2D extent;
747 VkFormat vk_format;
748 uint32_t drm_format;
749
750 uint32_t num_drm_modifiers;
751 const uint64_t * drm_modifiers;
752
753 VkPresentModeKHR present_mode;
754 bool fifo_ready;
755
756 struct wsi_wl_image images[0];
757 };
758 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain, VkSwapchainKHR)
759
760 static struct wsi_image *
761 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain *wsi_chain,
762 uint32_t image_index)
763 {
764 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
765 return &chain->images[image_index].base;
766 }
767
768 static VkResult
769 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
770 const VkAcquireNextImageInfoKHR *info,
771 uint32_t *image_index)
772 {
773 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
774 struct timespec start_time, end_time;
775 struct timespec rel_timeout;
776 int wl_fd = wl_display_get_fd(chain->display->wl_display);
777
778 timespec_from_nsec(&rel_timeout, info->timeout);
779
780 clock_gettime(CLOCK_MONOTONIC, &start_time);
781 timespec_add(&end_time, &rel_timeout, &start_time);
782
783 while (1) {
784 /* Try to dispatch potential events. */
785 int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
786 chain->display->queue);
787 if (ret < 0)
788 return VK_ERROR_OUT_OF_DATE_KHR;
789
790 /* Try to find a free image. */
791 for (uint32_t i = 0; i < chain->base.image_count; i++) {
792 if (!chain->images[i].busy) {
793 /* We found a non-busy image */
794 *image_index = i;
795 chain->images[i].busy = true;
796 return VK_SUCCESS;
797 }
798 }
799
800 /* Check for timeout. */
801 struct timespec current_time;
802 clock_gettime(CLOCK_MONOTONIC, &current_time);
803 if (timespec_after(&current_time, &end_time))
804 return VK_NOT_READY;
805
806 /* Try to read events from the server. */
807 ret = wl_display_prepare_read_queue(chain->display->wl_display,
808 chain->display->queue);
809 if (ret < 0) {
810 /* Another thread might have read events for our queue already. Go
811 * back to dispatch them.
812 */
813 if (errno == EAGAIN)
814 continue;
815 return VK_ERROR_OUT_OF_DATE_KHR;
816 }
817
818 struct pollfd pollfd = {
819 .fd = wl_fd,
820 .events = POLLIN
821 };
822 timespec_sub(&rel_timeout, &end_time, &current_time);
823 ret = ppoll(&pollfd, 1, &rel_timeout, NULL);
824 if (ret <= 0) {
825 int lerrno = errno;
826 wl_display_cancel_read(chain->display->wl_display);
827 if (ret < 0) {
828 /* If ppoll() was interrupted, try again. */
829 if (lerrno == EINTR || lerrno == EAGAIN)
830 continue;
831 return VK_ERROR_OUT_OF_DATE_KHR;
832 }
833 assert(ret == 0);
834 continue;
835 }
836
837 ret = wl_display_read_events(chain->display->wl_display);
838 if (ret < 0)
839 return VK_ERROR_OUT_OF_DATE_KHR;
840 }
841 }
842
843 static void
844 frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
845 {
846 struct wsi_wl_swapchain *chain = data;
847
848 chain->frame = NULL;
849 chain->fifo_ready = true;
850
851 wl_callback_destroy(callback);
852 }
853
854 static const struct wl_callback_listener frame_listener = {
855 frame_handle_done,
856 };
857
858 static VkResult
859 wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
860 uint32_t image_index,
861 const VkPresentRegionKHR *damage)
862 {
863 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
864
865 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
866 while (!chain->fifo_ready) {
867 int ret = wl_display_dispatch_queue(chain->display->wl_display,
868 chain->display->queue);
869 if (ret < 0)
870 return VK_ERROR_OUT_OF_DATE_KHR;
871 }
872 }
873
874 assert(image_index < chain->base.image_count);
875 wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
876
877 if (chain->surface_version >= 4 && damage &&
878 damage->pRectangles && damage->rectangleCount > 0) {
879 for (unsigned i = 0; i < damage->rectangleCount; i++) {
880 const VkRectLayerKHR *rect = &damage->pRectangles[i];
881 assert(rect->layer == 0);
882 wl_surface_damage_buffer(chain->surface,
883 rect->offset.x, rect->offset.y,
884 rect->extent.width, rect->extent.height);
885 }
886 } else {
887 wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
888 }
889
890 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
891 chain->frame = wl_surface_frame(chain->surface);
892 wl_callback_add_listener(chain->frame, &frame_listener, chain);
893 chain->fifo_ready = false;
894 }
895
896 chain->images[image_index].busy = true;
897 wl_surface_commit(chain->surface);
898 wl_display_flush(chain->display->wl_display);
899
900 return VK_SUCCESS;
901 }
902
903 static void
904 buffer_handle_release(void *data, struct wl_buffer *buffer)
905 {
906 struct wsi_wl_image *image = data;
907
908 assert(image->buffer == buffer);
909
910 image->busy = false;
911 }
912
913 static const struct wl_buffer_listener buffer_listener = {
914 buffer_handle_release,
915 };
916
917 static VkResult
918 wsi_wl_image_init(struct wsi_wl_swapchain *chain,
919 struct wsi_wl_image *image,
920 const VkSwapchainCreateInfoKHR *pCreateInfo,
921 const VkAllocationCallbacks* pAllocator)
922 {
923 struct wsi_wl_display *display = chain->display;
924 VkResult result;
925
926 result = wsi_create_native_image(&chain->base, pCreateInfo,
927 chain->num_drm_modifiers > 0 ? 1 : 0,
928 &chain->num_drm_modifiers,
929 &chain->drm_modifiers, &image->base);
930
931 if (result != VK_SUCCESS)
932 return result;
933
934 if (!chain->drm_wrapper) {
935 /* Only request modifiers if we have dmabuf, else it must be implicit. */
936 assert(display->dmabuf.wl_dmabuf);
937 assert(image->base.drm_modifier != DRM_FORMAT_MOD_INVALID);
938
939 struct zwp_linux_buffer_params_v1 *params =
940 zwp_linux_dmabuf_v1_create_params(display->dmabuf.wl_dmabuf);
941 wl_proxy_set_queue((struct wl_proxy *) params, chain->display->queue);
942
943 for (int i = 0; i < image->base.num_planes; i++) {
944 zwp_linux_buffer_params_v1_add(params,
945 image->base.fds[i],
946 i,
947 image->base.offsets[i],
948 image->base.row_pitches[i],
949 image->base.drm_modifier >> 32,
950 image->base.drm_modifier & 0xffffffff);
951 close(image->base.fds[i]);
952 }
953
954 image->buffer =
955 zwp_linux_buffer_params_v1_create_immed(params,
956 chain->extent.width,
957 chain->extent.height,
958 chain->drm_format,
959 0);
960 zwp_linux_buffer_params_v1_destroy(params);
961 } else {
962 /* Without passing modifiers, we can't have multi-plane RGB images. */
963 assert(image->base.num_planes == 1);
964 assert(image->base.drm_modifier == DRM_FORMAT_MOD_INVALID);
965
966 image->buffer =
967 wl_drm_create_prime_buffer(chain->drm_wrapper,
968 image->base.fds[0], /* name */
969 chain->extent.width,
970 chain->extent.height,
971 chain->drm_format,
972 image->base.offsets[0],
973 image->base.row_pitches[0],
974 0, 0, 0, 0 /* unused */);
975 close(image->base.fds[0]);
976 }
977
978 if (!image->buffer)
979 goto fail_image;
980
981 wl_buffer_add_listener(image->buffer, &buffer_listener, image);
982
983 return VK_SUCCESS;
984
985 fail_image:
986 wsi_destroy_image(&chain->base, &image->base);
987
988 return result;
989 }
990
991 static VkResult
992 wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
993 const VkAllocationCallbacks *pAllocator)
994 {
995 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
996
997 for (uint32_t i = 0; i < chain->base.image_count; i++) {
998 if (chain->images[i].buffer) {
999 wl_buffer_destroy(chain->images[i].buffer);
1000 wsi_destroy_image(&chain->base, &chain->images[i].base);
1001 }
1002 }
1003
1004 if (chain->frame)
1005 wl_callback_destroy(chain->frame);
1006 if (chain->surface)
1007 wl_proxy_wrapper_destroy(chain->surface);
1008 if (chain->drm_wrapper)
1009 wl_proxy_wrapper_destroy(chain->drm_wrapper);
1010
1011 if (chain->display)
1012 wsi_wl_display_unref(chain->display);
1013
1014 wsi_swapchain_finish(&chain->base);
1015
1016 vk_free(pAllocator, chain);
1017
1018 return VK_SUCCESS;
1019 }
1020
1021 static VkResult
1022 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
1023 VkDevice device,
1024 struct wsi_device *wsi_device,
1025 const VkSwapchainCreateInfoKHR* pCreateInfo,
1026 const VkAllocationCallbacks* pAllocator,
1027 struct wsi_swapchain **swapchain_out)
1028 {
1029 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
1030 struct wsi_wayland *wsi =
1031 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1032 struct wsi_wl_swapchain *chain;
1033 VkResult result;
1034
1035 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1036
1037 int num_images = pCreateInfo->minImageCount;
1038
1039 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1040 chain = vk_alloc(pAllocator, size, 8,
1041 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1042 if (chain == NULL)
1043 return VK_ERROR_OUT_OF_HOST_MEMORY;
1044
1045 result = wsi_swapchain_init(wsi_device, &chain->base, device,
1046 pCreateInfo, pAllocator);
1047 if (result != VK_SUCCESS) {
1048 vk_free(pAllocator, chain);
1049 return result;
1050 }
1051
1052 /* Mark a bunch of stuff as NULL. This way we can just call
1053 * destroy_swapchain for cleanup.
1054 */
1055 for (uint32_t i = 0; i < num_images; i++)
1056 chain->images[i].buffer = NULL;
1057 chain->surface = NULL;
1058 chain->drm_wrapper = NULL;
1059 chain->frame = NULL;
1060
1061 bool alpha = pCreateInfo->compositeAlpha ==
1062 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
1063
1064 chain->base.destroy = wsi_wl_swapchain_destroy;
1065 chain->base.get_wsi_image = wsi_wl_swapchain_get_wsi_image;
1066 chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
1067 chain->base.queue_present = wsi_wl_swapchain_queue_present;
1068 chain->base.present_mode = wsi_swapchain_get_present_mode(wsi_device, pCreateInfo);
1069 chain->base.image_count = num_images;
1070 chain->extent = pCreateInfo->imageExtent;
1071 chain->vk_format = pCreateInfo->imageFormat;
1072 chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
1073
1074 if (pCreateInfo->oldSwapchain) {
1075 /* If we have an oldSwapchain parameter, copy the display struct over
1076 * from the old one so we don't have to fully re-initialize it.
1077 */
1078 WSI_FROM_HANDLE(wsi_wl_swapchain, old_chain, pCreateInfo->oldSwapchain);
1079 chain->display = wsi_wl_display_ref(old_chain->display);
1080 } else {
1081 chain->display = NULL;
1082 result = wsi_wl_display_create(wsi, surface->display, &chain->display);
1083 if (result != VK_SUCCESS)
1084 goto fail;
1085 }
1086
1087 chain->surface = wl_proxy_create_wrapper(surface->surface);
1088 if (!chain->surface) {
1089 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1090 goto fail;
1091 }
1092 wl_proxy_set_queue((struct wl_proxy *) chain->surface,
1093 chain->display->queue);
1094 chain->surface_version = wl_proxy_get_version((void *)surface->surface);
1095
1096 chain->num_drm_modifiers = 0;
1097 chain->drm_modifiers = 0;
1098
1099 /* Use explicit DRM format modifiers when both the server and the driver
1100 * support them.
1101 */
1102 if (chain->display->dmabuf.wl_dmabuf &&
1103 chain->base.wsi->supports_modifiers) {
1104 struct u_vector *modifiers;
1105 switch (chain->drm_format) {
1106 case WL_DRM_FORMAT_ARGB8888:
1107 modifiers = &chain->display->dmabuf.modifiers.argb8888;
1108 break;
1109 case WL_DRM_FORMAT_XRGB8888:
1110 modifiers = &chain->display->dmabuf.modifiers.xrgb8888;
1111 break;
1112 default:
1113 modifiers = NULL;
1114 break;
1115 }
1116
1117 if (modifiers) {
1118 chain->drm_modifiers = u_vector_tail(modifiers);
1119 chain->num_drm_modifiers = u_vector_length(modifiers);
1120 }
1121 }
1122
1123 /* When there are explicit DRM format modifiers, we must use
1124 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1125 * wl_drm.
1126 */
1127 if (!chain->num_drm_modifiers) {
1128 assert(chain->display->drm.wl_drm);
1129
1130 chain->drm_wrapper =
1131 wl_proxy_create_wrapper(chain->display->drm.wl_drm);
1132 if (!chain->drm_wrapper) {
1133 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1134 goto fail;
1135 }
1136 wl_proxy_set_queue((struct wl_proxy *) chain->drm_wrapper,
1137 chain->display->queue);
1138 }
1139
1140 chain->fifo_ready = true;
1141
1142 for (uint32_t i = 0; i < chain->base.image_count; i++) {
1143 result = wsi_wl_image_init(chain, &chain->images[i],
1144 pCreateInfo, pAllocator);
1145 if (result != VK_SUCCESS)
1146 goto fail;
1147 chain->images[i].busy = false;
1148 }
1149
1150 *swapchain_out = &chain->base;
1151
1152 return VK_SUCCESS;
1153
1154 fail:
1155 wsi_wl_swapchain_destroy(&chain->base, pAllocator);
1156
1157 return result;
1158 }
1159
1160 VkResult
1161 wsi_wl_init_wsi(struct wsi_device *wsi_device,
1162 const VkAllocationCallbacks *alloc,
1163 VkPhysicalDevice physical_device)
1164 {
1165 struct wsi_wayland *wsi;
1166 VkResult result;
1167
1168 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1169 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1170 if (!wsi) {
1171 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1172 goto fail;
1173 }
1174
1175 wsi->physical_device = physical_device;
1176 wsi->alloc = alloc;
1177 wsi->wsi = wsi_device;
1178
1179 wsi->base.get_support = wsi_wl_surface_get_support;
1180 wsi->base.get_capabilities2 = wsi_wl_surface_get_capabilities2;
1181 wsi->base.get_formats = wsi_wl_surface_get_formats;
1182 wsi->base.get_formats2 = wsi_wl_surface_get_formats2;
1183 wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
1184 wsi->base.get_present_rectangles = wsi_wl_surface_get_present_rectangles;
1185 wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
1186
1187 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
1188
1189 return VK_SUCCESS;
1190
1191 fail:
1192 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
1193
1194 return result;
1195 }
1196
1197 void
1198 wsi_wl_finish_wsi(struct wsi_device *wsi_device,
1199 const VkAllocationCallbacks *alloc)
1200 {
1201 struct wsi_wayland *wsi =
1202 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1203 if (!wsi)
1204 return;
1205
1206 vk_free(alloc, wsi);
1207 }