meson: only build imgui when needed
[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 /* We need prime support for wl_drm */
425 if (display->drm.wl_drm &&
426 (display->drm.capabilities & WL_DRM_CAPABILITY_PRIME)) {
427 display->formats = &display->drm.formats;
428 } else if (display->dmabuf.wl_dmabuf) {
429 display->formats = &display->dmabuf.formats;
430 }
431
432 if (!display->formats) {
433 result = VK_ERROR_SURFACE_LOST_KHR;
434 goto fail_registry;
435 }
436
437 /* We don't need this anymore */
438 wl_registry_destroy(registry);
439
440 display->refcount = 0;
441
442 return VK_SUCCESS;
443
444 fail_registry:
445 if (registry)
446 wl_registry_destroy(registry);
447
448 fail:
449 wsi_wl_display_finish(display);
450 return result;
451 }
452
453 static VkResult
454 wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display,
455 struct wsi_wl_display **display_out)
456 {
457 struct wsi_wl_display *display =
458 vk_alloc(wsi->alloc, sizeof(*display), 8,
459 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
460 if (!display)
461 return VK_ERROR_OUT_OF_HOST_MEMORY;
462
463 VkResult result = wsi_wl_display_init(wsi, display, wl_display, true);
464 if (result != VK_SUCCESS) {
465 vk_free(wsi->alloc, display);
466 return result;
467 }
468
469 display->refcount++;
470 *display_out = display;
471
472 return result;
473 }
474
475 static struct wsi_wl_display *
476 wsi_wl_display_ref(struct wsi_wl_display *display)
477 {
478 display->refcount++;
479 return display;
480 }
481
482 static void
483 wsi_wl_display_unref(struct wsi_wl_display *display)
484 {
485 if (display->refcount-- > 1)
486 return;
487
488 struct wsi_wayland *wsi = display->wsi_wl;
489 wsi_wl_display_finish(display);
490 vk_free(wsi->alloc, display);
491 }
492
493 VkBool32
494 wsi_wl_get_presentation_support(struct wsi_device *wsi_device,
495 struct wl_display *wl_display)
496 {
497 struct wsi_wayland *wsi =
498 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
499
500 struct wsi_wl_display display;
501 VkResult ret = wsi_wl_display_init(wsi, &display, wl_display, false);
502 if (ret == VK_SUCCESS)
503 wsi_wl_display_finish(&display);
504
505 return ret == VK_SUCCESS;
506 }
507
508 static VkResult
509 wsi_wl_surface_get_support(VkIcdSurfaceBase *surface,
510 struct wsi_device *wsi_device,
511 uint32_t queueFamilyIndex,
512 VkBool32* pSupported)
513 {
514 *pSupported = true;
515
516 return VK_SUCCESS;
517 }
518
519 static const VkPresentModeKHR present_modes[] = {
520 VK_PRESENT_MODE_MAILBOX_KHR,
521 VK_PRESENT_MODE_FIFO_KHR,
522 };
523
524 static VkResult
525 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase *surface,
526 struct wsi_device *wsi_device,
527 VkSurfaceCapabilitiesKHR* caps)
528 {
529 /* For true mailbox mode, we need at least 4 images:
530 * 1) One to scan out from
531 * 2) One to have queued for scan-out
532 * 3) One to be currently held by the Wayland compositor
533 * 4) One to render to
534 */
535 caps->minImageCount = 4;
536 /* There is no real maximum */
537 caps->maxImageCount = 0;
538
539 caps->currentExtent = (VkExtent2D) { -1, -1 };
540 caps->minImageExtent = (VkExtent2D) { 1, 1 };
541 caps->maxImageExtent = (VkExtent2D) {
542 wsi_device->maxImageDimension2D,
543 wsi_device->maxImageDimension2D,
544 };
545
546 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
547 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
548 caps->maxImageArrayLayers = 1;
549
550 caps->supportedCompositeAlpha =
551 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
552 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
553
554 caps->supportedUsageFlags =
555 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
556 VK_IMAGE_USAGE_SAMPLED_BIT |
557 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
558 VK_IMAGE_USAGE_STORAGE_BIT |
559 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
560
561 return VK_SUCCESS;
562 }
563
564 static VkResult
565 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase *surface,
566 struct wsi_device *wsi_device,
567 const void *info_next,
568 VkSurfaceCapabilities2KHR* caps)
569 {
570 assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
571
572 VkResult result =
573 wsi_wl_surface_get_capabilities(surface, wsi_device,
574 &caps->surfaceCapabilities);
575
576 vk_foreach_struct(ext, caps->pNext) {
577 switch (ext->sType) {
578 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
579 VkSurfaceProtectedCapabilitiesKHR *protected = (void *)ext;
580 protected->supportsProtected = VK_FALSE;
581 break;
582 }
583
584 default:
585 /* Ignored */
586 break;
587 }
588 }
589
590 return result;
591 }
592
593 static VkResult
594 wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
595 struct wsi_device *wsi_device,
596 uint32_t* pSurfaceFormatCount,
597 VkSurfaceFormatKHR* pSurfaceFormats)
598 {
599 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
600 struct wsi_wayland *wsi =
601 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
602
603 struct wsi_wl_display display;
604 if (wsi_wl_display_init(wsi, &display, surface->display, true))
605 return VK_ERROR_SURFACE_LOST_KHR;
606
607 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
608
609 VkFormat *disp_fmt;
610 u_vector_foreach(disp_fmt, display.formats) {
611 vk_outarray_append(&out, out_fmt) {
612 out_fmt->format = *disp_fmt;
613 out_fmt->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
614 }
615 }
616
617 wsi_wl_display_finish(&display);
618
619 return vk_outarray_status(&out);
620 }
621
622 static VkResult
623 wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
624 struct wsi_device *wsi_device,
625 const void *info_next,
626 uint32_t* pSurfaceFormatCount,
627 VkSurfaceFormat2KHR* pSurfaceFormats)
628 {
629 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
630 struct wsi_wayland *wsi =
631 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
632
633 struct wsi_wl_display display;
634 if (wsi_wl_display_init(wsi, &display, surface->display, true))
635 return VK_ERROR_SURFACE_LOST_KHR;
636
637 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
638
639 VkFormat *disp_fmt;
640 u_vector_foreach(disp_fmt, display.formats) {
641 vk_outarray_append(&out, out_fmt) {
642 out_fmt->surfaceFormat.format = *disp_fmt;
643 out_fmt->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
644 }
645 }
646
647 wsi_wl_display_finish(&display);
648
649 return vk_outarray_status(&out);
650 }
651
652 static VkResult
653 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface,
654 uint32_t* pPresentModeCount,
655 VkPresentModeKHR* pPresentModes)
656 {
657 if (pPresentModes == NULL) {
658 *pPresentModeCount = ARRAY_SIZE(present_modes);
659 return VK_SUCCESS;
660 }
661
662 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
663 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
664
665 if (*pPresentModeCount < ARRAY_SIZE(present_modes))
666 return VK_INCOMPLETE;
667 else
668 return VK_SUCCESS;
669 }
670
671 static VkResult
672 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase *surface,
673 struct wsi_device *wsi_device,
674 uint32_t* pRectCount,
675 VkRect2D* pRects)
676 {
677 VK_OUTARRAY_MAKE(out, pRects, pRectCount);
678
679 vk_outarray_append(&out, rect) {
680 /* We don't know a size so just return the usual "I don't know." */
681 *rect = (VkRect2D) {
682 .offset = { 0, 0 },
683 .extent = { -1, -1 },
684 };
685 }
686
687 return vk_outarray_status(&out);
688 }
689
690 VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator,
691 const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
692 VkSurfaceKHR *pSurface)
693 {
694 VkIcdSurfaceWayland *surface;
695
696 surface = vk_alloc(pAllocator, sizeof *surface, 8,
697 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
698 if (surface == NULL)
699 return VK_ERROR_OUT_OF_HOST_MEMORY;
700
701 surface->base.platform = VK_ICD_WSI_PLATFORM_WAYLAND;
702 surface->display = pCreateInfo->display;
703 surface->surface = pCreateInfo->surface;
704
705 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
706
707 return VK_SUCCESS;
708 }
709
710 struct wsi_wl_image {
711 struct wsi_image base;
712 struct wl_buffer * buffer;
713 bool busy;
714 };
715
716 struct wsi_wl_swapchain {
717 struct wsi_swapchain base;
718
719 struct wsi_wl_display *display;
720
721 struct wl_surface * surface;
722 uint32_t surface_version;
723
724 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
725 * zwp_linux_dmabuf_v1 should be used.
726 */
727 struct wl_drm * drm_wrapper;
728
729 struct wl_callback * frame;
730
731 VkExtent2D extent;
732 VkFormat vk_format;
733 uint32_t drm_format;
734
735 uint32_t num_drm_modifiers;
736 const uint64_t * drm_modifiers;
737
738 VkPresentModeKHR present_mode;
739 bool fifo_ready;
740
741 struct wsi_wl_image images[0];
742 };
743 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain, VkSwapchainKHR)
744
745 static struct wsi_image *
746 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain *wsi_chain,
747 uint32_t image_index)
748 {
749 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
750 return &chain->images[image_index].base;
751 }
752
753 static VkResult
754 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
755 const VkAcquireNextImageInfoKHR *info,
756 uint32_t *image_index)
757 {
758 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
759 struct timespec start_time, end_time;
760 struct timespec rel_timeout;
761 int wl_fd = wl_display_get_fd(chain->display->wl_display);
762
763 timespec_from_nsec(&rel_timeout, info->timeout);
764
765 clock_gettime(CLOCK_MONOTONIC, &start_time);
766 timespec_add(&end_time, &rel_timeout, &start_time);
767
768 while (1) {
769 /* Try to dispatch potential events. */
770 int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
771 chain->display->queue);
772 if (ret < 0)
773 return VK_ERROR_OUT_OF_DATE_KHR;
774
775 /* Try to find a free image. */
776 for (uint32_t i = 0; i < chain->base.image_count; i++) {
777 if (!chain->images[i].busy) {
778 /* We found a non-busy image */
779 *image_index = i;
780 chain->images[i].busy = true;
781 return VK_SUCCESS;
782 }
783 }
784
785 /* Check for timeout. */
786 struct timespec current_time;
787 clock_gettime(CLOCK_MONOTONIC, &current_time);
788 if (timespec_after(&current_time, &end_time))
789 return VK_NOT_READY;
790
791 /* Try to read events from the server. */
792 ret = wl_display_prepare_read_queue(chain->display->wl_display,
793 chain->display->queue);
794 if (ret < 0) {
795 /* Another thread might have read events for our queue already. Go
796 * back to dispatch them.
797 */
798 if (errno == EAGAIN)
799 continue;
800 return VK_ERROR_OUT_OF_DATE_KHR;
801 }
802
803 struct pollfd pollfd = {
804 .fd = wl_fd,
805 .events = POLLIN
806 };
807 timespec_sub(&rel_timeout, &end_time, &current_time);
808 ret = ppoll(&pollfd, 1, &rel_timeout, NULL);
809 if (ret <= 0) {
810 int lerrno = errno;
811 wl_display_cancel_read(chain->display->wl_display);
812 if (ret < 0) {
813 /* If ppoll() was interrupted, try again. */
814 if (lerrno == EINTR || lerrno == EAGAIN)
815 continue;
816 return VK_ERROR_OUT_OF_DATE_KHR;
817 }
818 assert(ret == 0);
819 continue;
820 }
821
822 ret = wl_display_read_events(chain->display->wl_display);
823 if (ret < 0)
824 return VK_ERROR_OUT_OF_DATE_KHR;
825 }
826 }
827
828 static void
829 frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
830 {
831 struct wsi_wl_swapchain *chain = data;
832
833 chain->frame = NULL;
834 chain->fifo_ready = true;
835
836 wl_callback_destroy(callback);
837 }
838
839 static const struct wl_callback_listener frame_listener = {
840 frame_handle_done,
841 };
842
843 static VkResult
844 wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
845 uint32_t image_index,
846 const VkPresentRegionKHR *damage)
847 {
848 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
849
850 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
851 while (!chain->fifo_ready) {
852 int ret = wl_display_dispatch_queue(chain->display->wl_display,
853 chain->display->queue);
854 if (ret < 0)
855 return VK_ERROR_OUT_OF_DATE_KHR;
856 }
857 }
858
859 assert(image_index < chain->base.image_count);
860 wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
861
862 if (chain->surface_version >= 4 && damage &&
863 damage->pRectangles && damage->rectangleCount > 0) {
864 for (unsigned i = 0; i < damage->rectangleCount; i++) {
865 const VkRectLayerKHR *rect = &damage->pRectangles[i];
866 assert(rect->layer == 0);
867 wl_surface_damage_buffer(chain->surface,
868 rect->offset.x, rect->offset.y,
869 rect->extent.width, rect->extent.height);
870 }
871 } else {
872 wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
873 }
874
875 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
876 chain->frame = wl_surface_frame(chain->surface);
877 wl_callback_add_listener(chain->frame, &frame_listener, chain);
878 chain->fifo_ready = false;
879 }
880
881 chain->images[image_index].busy = true;
882 wl_surface_commit(chain->surface);
883 wl_display_flush(chain->display->wl_display);
884
885 return VK_SUCCESS;
886 }
887
888 static void
889 buffer_handle_release(void *data, struct wl_buffer *buffer)
890 {
891 struct wsi_wl_image *image = data;
892
893 assert(image->buffer == buffer);
894
895 image->busy = false;
896 }
897
898 static const struct wl_buffer_listener buffer_listener = {
899 buffer_handle_release,
900 };
901
902 static VkResult
903 wsi_wl_image_init(struct wsi_wl_swapchain *chain,
904 struct wsi_wl_image *image,
905 const VkSwapchainCreateInfoKHR *pCreateInfo,
906 const VkAllocationCallbacks* pAllocator)
907 {
908 struct wsi_wl_display *display = chain->display;
909 VkResult result;
910
911 result = wsi_create_native_image(&chain->base, pCreateInfo,
912 chain->num_drm_modifiers > 0 ? 1 : 0,
913 &chain->num_drm_modifiers,
914 &chain->drm_modifiers, &image->base);
915
916 if (result != VK_SUCCESS)
917 return result;
918
919 if (!chain->drm_wrapper) {
920 /* Only request modifiers if we have dmabuf, else it must be implicit. */
921 assert(display->dmabuf.wl_dmabuf);
922 assert(image->base.drm_modifier != DRM_FORMAT_MOD_INVALID);
923
924 struct zwp_linux_buffer_params_v1 *params =
925 zwp_linux_dmabuf_v1_create_params(display->dmabuf.wl_dmabuf);
926 wl_proxy_set_queue((struct wl_proxy *) params, chain->display->queue);
927
928 for (int i = 0; i < image->base.num_planes; i++) {
929 zwp_linux_buffer_params_v1_add(params,
930 image->base.fds[i],
931 i,
932 image->base.offsets[i],
933 image->base.row_pitches[i],
934 image->base.drm_modifier >> 32,
935 image->base.drm_modifier & 0xffffffff);
936 close(image->base.fds[i]);
937 }
938
939 image->buffer =
940 zwp_linux_buffer_params_v1_create_immed(params,
941 chain->extent.width,
942 chain->extent.height,
943 chain->drm_format,
944 0);
945 zwp_linux_buffer_params_v1_destroy(params);
946 } else {
947 /* Without passing modifiers, we can't have multi-plane RGB images. */
948 assert(image->base.num_planes == 1);
949 assert(image->base.drm_modifier == DRM_FORMAT_MOD_INVALID);
950
951 image->buffer =
952 wl_drm_create_prime_buffer(chain->drm_wrapper,
953 image->base.fds[0], /* name */
954 chain->extent.width,
955 chain->extent.height,
956 chain->drm_format,
957 image->base.offsets[0],
958 image->base.row_pitches[0],
959 0, 0, 0, 0 /* unused */);
960 close(image->base.fds[0]);
961 }
962
963 if (!image->buffer)
964 goto fail_image;
965
966 wl_buffer_add_listener(image->buffer, &buffer_listener, image);
967
968 return VK_SUCCESS;
969
970 fail_image:
971 wsi_destroy_image(&chain->base, &image->base);
972
973 return result;
974 }
975
976 static VkResult
977 wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
978 const VkAllocationCallbacks *pAllocator)
979 {
980 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
981
982 for (uint32_t i = 0; i < chain->base.image_count; i++) {
983 if (chain->images[i].buffer) {
984 wl_buffer_destroy(chain->images[i].buffer);
985 wsi_destroy_image(&chain->base, &chain->images[i].base);
986 }
987 }
988
989 if (chain->frame)
990 wl_callback_destroy(chain->frame);
991 if (chain->surface)
992 wl_proxy_wrapper_destroy(chain->surface);
993 if (chain->drm_wrapper)
994 wl_proxy_wrapper_destroy(chain->drm_wrapper);
995
996 if (chain->display)
997 wsi_wl_display_unref(chain->display);
998
999 wsi_swapchain_finish(&chain->base);
1000
1001 vk_free(pAllocator, chain);
1002
1003 return VK_SUCCESS;
1004 }
1005
1006 static VkResult
1007 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
1008 VkDevice device,
1009 struct wsi_device *wsi_device,
1010 const VkSwapchainCreateInfoKHR* pCreateInfo,
1011 const VkAllocationCallbacks* pAllocator,
1012 struct wsi_swapchain **swapchain_out)
1013 {
1014 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
1015 struct wsi_wayland *wsi =
1016 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1017 struct wsi_wl_swapchain *chain;
1018 VkResult result;
1019
1020 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1021
1022 int num_images = pCreateInfo->minImageCount;
1023
1024 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1025 chain = vk_alloc(pAllocator, size, 8,
1026 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1027 if (chain == NULL)
1028 return VK_ERROR_OUT_OF_HOST_MEMORY;
1029
1030 result = wsi_swapchain_init(wsi_device, &chain->base, device,
1031 pCreateInfo, pAllocator);
1032 if (result != VK_SUCCESS) {
1033 vk_free(pAllocator, chain);
1034 return result;
1035 }
1036
1037 /* Mark a bunch of stuff as NULL. This way we can just call
1038 * destroy_swapchain for cleanup.
1039 */
1040 for (uint32_t i = 0; i < num_images; i++)
1041 chain->images[i].buffer = NULL;
1042 chain->surface = NULL;
1043 chain->drm_wrapper = NULL;
1044 chain->frame = NULL;
1045
1046 bool alpha = pCreateInfo->compositeAlpha ==
1047 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
1048
1049 chain->base.destroy = wsi_wl_swapchain_destroy;
1050 chain->base.get_wsi_image = wsi_wl_swapchain_get_wsi_image;
1051 chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
1052 chain->base.queue_present = wsi_wl_swapchain_queue_present;
1053 chain->base.present_mode = wsi_swapchain_get_present_mode(wsi_device, pCreateInfo);
1054 chain->base.image_count = num_images;
1055 chain->extent = pCreateInfo->imageExtent;
1056 chain->vk_format = pCreateInfo->imageFormat;
1057 chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
1058
1059 if (pCreateInfo->oldSwapchain) {
1060 /* If we have an oldSwapchain parameter, copy the display struct over
1061 * from the old one so we don't have to fully re-initialize it.
1062 */
1063 WSI_FROM_HANDLE(wsi_wl_swapchain, old_chain, pCreateInfo->oldSwapchain);
1064 chain->display = wsi_wl_display_ref(old_chain->display);
1065 } else {
1066 chain->display = NULL;
1067 result = wsi_wl_display_create(wsi, surface->display, &chain->display);
1068 if (result != VK_SUCCESS)
1069 goto fail;
1070 }
1071
1072 chain->surface = wl_proxy_create_wrapper(surface->surface);
1073 if (!chain->surface) {
1074 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1075 goto fail;
1076 }
1077 wl_proxy_set_queue((struct wl_proxy *) chain->surface,
1078 chain->display->queue);
1079 chain->surface_version = wl_proxy_get_version((void *)surface->surface);
1080
1081 chain->num_drm_modifiers = 0;
1082 chain->drm_modifiers = 0;
1083
1084 /* Use explicit DRM format modifiers when both the server and the driver
1085 * support them.
1086 */
1087 if (chain->display->dmabuf.wl_dmabuf &&
1088 chain->base.wsi->supports_modifiers) {
1089 struct u_vector *modifiers;
1090 switch (chain->drm_format) {
1091 case WL_DRM_FORMAT_ARGB8888:
1092 modifiers = &chain->display->dmabuf.modifiers.argb8888;
1093 break;
1094 case WL_DRM_FORMAT_XRGB8888:
1095 modifiers = &chain->display->dmabuf.modifiers.xrgb8888;
1096 break;
1097 default:
1098 modifiers = NULL;
1099 break;
1100 }
1101
1102 if (modifiers) {
1103 chain->drm_modifiers = u_vector_tail(modifiers);
1104 chain->num_drm_modifiers = u_vector_length(modifiers);
1105 }
1106 }
1107
1108 /* When there are explicit DRM format modifiers, we must use
1109 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1110 * wl_drm.
1111 */
1112 if (!chain->num_drm_modifiers) {
1113 assert(chain->display->drm.wl_drm);
1114
1115 chain->drm_wrapper =
1116 wl_proxy_create_wrapper(chain->display->drm.wl_drm);
1117 if (!chain->drm_wrapper) {
1118 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1119 goto fail;
1120 }
1121 wl_proxy_set_queue((struct wl_proxy *) chain->drm_wrapper,
1122 chain->display->queue);
1123 }
1124
1125 chain->fifo_ready = true;
1126
1127 for (uint32_t i = 0; i < chain->base.image_count; i++) {
1128 result = wsi_wl_image_init(chain, &chain->images[i],
1129 pCreateInfo, pAllocator);
1130 if (result != VK_SUCCESS)
1131 goto fail;
1132 chain->images[i].busy = false;
1133 }
1134
1135 *swapchain_out = &chain->base;
1136
1137 return VK_SUCCESS;
1138
1139 fail:
1140 wsi_wl_swapchain_destroy(&chain->base, pAllocator);
1141
1142 return result;
1143 }
1144
1145 VkResult
1146 wsi_wl_init_wsi(struct wsi_device *wsi_device,
1147 const VkAllocationCallbacks *alloc,
1148 VkPhysicalDevice physical_device)
1149 {
1150 struct wsi_wayland *wsi;
1151 VkResult result;
1152
1153 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1154 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1155 if (!wsi) {
1156 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1157 goto fail;
1158 }
1159
1160 wsi->physical_device = physical_device;
1161 wsi->alloc = alloc;
1162 wsi->wsi = wsi_device;
1163
1164 wsi->base.get_support = wsi_wl_surface_get_support;
1165 wsi->base.get_capabilities2 = wsi_wl_surface_get_capabilities2;
1166 wsi->base.get_formats = wsi_wl_surface_get_formats;
1167 wsi->base.get_formats2 = wsi_wl_surface_get_formats2;
1168 wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
1169 wsi->base.get_present_rectangles = wsi_wl_surface_get_present_rectangles;
1170 wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
1171
1172 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
1173
1174 return VK_SUCCESS;
1175
1176 fail:
1177 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
1178
1179 return result;
1180 }
1181
1182 void
1183 wsi_wl_finish_wsi(struct wsi_device *wsi_device,
1184 const VkAllocationCallbacks *alloc)
1185 {
1186 struct wsi_wayland *wsi =
1187 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1188 if (!wsi)
1189 return;
1190
1191 vk_free(alloc, wsi);
1192 }