meson: Add mising git_sha1.h dependency.
[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 VK_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain, base.base, VkSwapchainKHR,
759 VK_OBJECT_TYPE_SWAPCHAIN_KHR)
760
761 static struct wsi_image *
762 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain *wsi_chain,
763 uint32_t image_index)
764 {
765 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
766 return &chain->images[image_index].base;
767 }
768
769 static VkResult
770 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
771 const VkAcquireNextImageInfoKHR *info,
772 uint32_t *image_index)
773 {
774 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
775 struct timespec start_time, end_time;
776 struct timespec rel_timeout;
777 int wl_fd = wl_display_get_fd(chain->display->wl_display);
778
779 timespec_from_nsec(&rel_timeout, info->timeout);
780
781 clock_gettime(CLOCK_MONOTONIC, &start_time);
782 timespec_add(&end_time, &rel_timeout, &start_time);
783
784 while (1) {
785 /* Try to dispatch potential events. */
786 int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
787 chain->display->queue);
788 if (ret < 0)
789 return VK_ERROR_OUT_OF_DATE_KHR;
790
791 /* Try to find a free image. */
792 for (uint32_t i = 0; i < chain->base.image_count; i++) {
793 if (!chain->images[i].busy) {
794 /* We found a non-busy image */
795 *image_index = i;
796 chain->images[i].busy = true;
797 return VK_SUCCESS;
798 }
799 }
800
801 /* Check for timeout. */
802 struct timespec current_time;
803 clock_gettime(CLOCK_MONOTONIC, &current_time);
804 if (timespec_after(&current_time, &end_time))
805 return VK_NOT_READY;
806
807 /* Try to read events from the server. */
808 ret = wl_display_prepare_read_queue(chain->display->wl_display,
809 chain->display->queue);
810 if (ret < 0) {
811 /* Another thread might have read events for our queue already. Go
812 * back to dispatch them.
813 */
814 if (errno == EAGAIN)
815 continue;
816 return VK_ERROR_OUT_OF_DATE_KHR;
817 }
818
819 struct pollfd pollfd = {
820 .fd = wl_fd,
821 .events = POLLIN
822 };
823 timespec_sub(&rel_timeout, &end_time, &current_time);
824 ret = ppoll(&pollfd, 1, &rel_timeout, NULL);
825 if (ret <= 0) {
826 int lerrno = errno;
827 wl_display_cancel_read(chain->display->wl_display);
828 if (ret < 0) {
829 /* If ppoll() was interrupted, try again. */
830 if (lerrno == EINTR || lerrno == EAGAIN)
831 continue;
832 return VK_ERROR_OUT_OF_DATE_KHR;
833 }
834 assert(ret == 0);
835 continue;
836 }
837
838 ret = wl_display_read_events(chain->display->wl_display);
839 if (ret < 0)
840 return VK_ERROR_OUT_OF_DATE_KHR;
841 }
842 }
843
844 static void
845 frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
846 {
847 struct wsi_wl_swapchain *chain = data;
848
849 chain->frame = NULL;
850 chain->fifo_ready = true;
851
852 wl_callback_destroy(callback);
853 }
854
855 static const struct wl_callback_listener frame_listener = {
856 frame_handle_done,
857 };
858
859 static VkResult
860 wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
861 uint32_t image_index,
862 const VkPresentRegionKHR *damage)
863 {
864 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
865
866 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
867 while (!chain->fifo_ready) {
868 int ret = wl_display_dispatch_queue(chain->display->wl_display,
869 chain->display->queue);
870 if (ret < 0)
871 return VK_ERROR_OUT_OF_DATE_KHR;
872 }
873 }
874
875 assert(image_index < chain->base.image_count);
876 wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
877
878 if (chain->surface_version >= 4 && damage &&
879 damage->pRectangles && damage->rectangleCount > 0) {
880 for (unsigned i = 0; i < damage->rectangleCount; i++) {
881 const VkRectLayerKHR *rect = &damage->pRectangles[i];
882 assert(rect->layer == 0);
883 wl_surface_damage_buffer(chain->surface,
884 rect->offset.x, rect->offset.y,
885 rect->extent.width, rect->extent.height);
886 }
887 } else {
888 wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
889 }
890
891 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
892 chain->frame = wl_surface_frame(chain->surface);
893 wl_callback_add_listener(chain->frame, &frame_listener, chain);
894 chain->fifo_ready = false;
895 }
896
897 chain->images[image_index].busy = true;
898 wl_surface_commit(chain->surface);
899 wl_display_flush(chain->display->wl_display);
900
901 return VK_SUCCESS;
902 }
903
904 static void
905 buffer_handle_release(void *data, struct wl_buffer *buffer)
906 {
907 struct wsi_wl_image *image = data;
908
909 assert(image->buffer == buffer);
910
911 image->busy = false;
912 }
913
914 static const struct wl_buffer_listener buffer_listener = {
915 buffer_handle_release,
916 };
917
918 static VkResult
919 wsi_wl_image_init(struct wsi_wl_swapchain *chain,
920 struct wsi_wl_image *image,
921 const VkSwapchainCreateInfoKHR *pCreateInfo,
922 const VkAllocationCallbacks* pAllocator)
923 {
924 struct wsi_wl_display *display = chain->display;
925 VkResult result;
926
927 result = wsi_create_native_image(&chain->base, pCreateInfo,
928 chain->num_drm_modifiers > 0 ? 1 : 0,
929 &chain->num_drm_modifiers,
930 &chain->drm_modifiers, &image->base);
931
932 if (result != VK_SUCCESS)
933 return result;
934
935 if (!chain->drm_wrapper) {
936 /* Only request modifiers if we have dmabuf, else it must be implicit. */
937 assert(display->dmabuf.wl_dmabuf);
938 assert(image->base.drm_modifier != DRM_FORMAT_MOD_INVALID);
939
940 struct zwp_linux_buffer_params_v1 *params =
941 zwp_linux_dmabuf_v1_create_params(display->dmabuf.wl_dmabuf);
942 wl_proxy_set_queue((struct wl_proxy *) params, chain->display->queue);
943
944 for (int i = 0; i < image->base.num_planes; i++) {
945 zwp_linux_buffer_params_v1_add(params,
946 image->base.fds[i],
947 i,
948 image->base.offsets[i],
949 image->base.row_pitches[i],
950 image->base.drm_modifier >> 32,
951 image->base.drm_modifier & 0xffffffff);
952 close(image->base.fds[i]);
953 }
954
955 image->buffer =
956 zwp_linux_buffer_params_v1_create_immed(params,
957 chain->extent.width,
958 chain->extent.height,
959 chain->drm_format,
960 0);
961 zwp_linux_buffer_params_v1_destroy(params);
962 } else {
963 /* Without passing modifiers, we can't have multi-plane RGB images. */
964 assert(image->base.num_planes == 1);
965 assert(image->base.drm_modifier == DRM_FORMAT_MOD_INVALID);
966
967 image->buffer =
968 wl_drm_create_prime_buffer(chain->drm_wrapper,
969 image->base.fds[0], /* name */
970 chain->extent.width,
971 chain->extent.height,
972 chain->drm_format,
973 image->base.offsets[0],
974 image->base.row_pitches[0],
975 0, 0, 0, 0 /* unused */);
976 close(image->base.fds[0]);
977 }
978
979 if (!image->buffer)
980 goto fail_image;
981
982 wl_buffer_add_listener(image->buffer, &buffer_listener, image);
983
984 return VK_SUCCESS;
985
986 fail_image:
987 wsi_destroy_image(&chain->base, &image->base);
988
989 return result;
990 }
991
992 static VkResult
993 wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
994 const VkAllocationCallbacks *pAllocator)
995 {
996 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
997
998 for (uint32_t i = 0; i < chain->base.image_count; i++) {
999 if (chain->images[i].buffer) {
1000 wl_buffer_destroy(chain->images[i].buffer);
1001 wsi_destroy_image(&chain->base, &chain->images[i].base);
1002 }
1003 }
1004
1005 if (chain->frame)
1006 wl_callback_destroy(chain->frame);
1007 if (chain->surface)
1008 wl_proxy_wrapper_destroy(chain->surface);
1009 if (chain->drm_wrapper)
1010 wl_proxy_wrapper_destroy(chain->drm_wrapper);
1011
1012 if (chain->display)
1013 wsi_wl_display_unref(chain->display);
1014
1015 wsi_swapchain_finish(&chain->base);
1016
1017 vk_free(pAllocator, chain);
1018
1019 return VK_SUCCESS;
1020 }
1021
1022 static VkResult
1023 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
1024 VkDevice device,
1025 struct wsi_device *wsi_device,
1026 const VkSwapchainCreateInfoKHR* pCreateInfo,
1027 const VkAllocationCallbacks* pAllocator,
1028 struct wsi_swapchain **swapchain_out)
1029 {
1030 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
1031 struct wsi_wayland *wsi =
1032 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1033 struct wsi_wl_swapchain *chain;
1034 VkResult result;
1035
1036 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1037
1038 int num_images = pCreateInfo->minImageCount;
1039
1040 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1041 chain = vk_alloc(pAllocator, size, 8,
1042 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1043 if (chain == NULL)
1044 return VK_ERROR_OUT_OF_HOST_MEMORY;
1045
1046 result = wsi_swapchain_init(wsi_device, &chain->base, device,
1047 pCreateInfo, pAllocator);
1048 if (result != VK_SUCCESS) {
1049 vk_free(pAllocator, chain);
1050 return result;
1051 }
1052
1053 /* Mark a bunch of stuff as NULL. This way we can just call
1054 * destroy_swapchain for cleanup.
1055 */
1056 for (uint32_t i = 0; i < num_images; i++)
1057 chain->images[i].buffer = NULL;
1058 chain->surface = NULL;
1059 chain->drm_wrapper = NULL;
1060 chain->frame = NULL;
1061
1062 bool alpha = pCreateInfo->compositeAlpha ==
1063 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
1064
1065 chain->base.destroy = wsi_wl_swapchain_destroy;
1066 chain->base.get_wsi_image = wsi_wl_swapchain_get_wsi_image;
1067 chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
1068 chain->base.queue_present = wsi_wl_swapchain_queue_present;
1069 chain->base.present_mode = wsi_swapchain_get_present_mode(wsi_device, pCreateInfo);
1070 chain->base.image_count = num_images;
1071 chain->extent = pCreateInfo->imageExtent;
1072 chain->vk_format = pCreateInfo->imageFormat;
1073 chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
1074
1075 if (pCreateInfo->oldSwapchain) {
1076 /* If we have an oldSwapchain parameter, copy the display struct over
1077 * from the old one so we don't have to fully re-initialize it.
1078 */
1079 VK_FROM_HANDLE(wsi_wl_swapchain, old_chain, pCreateInfo->oldSwapchain);
1080 chain->display = wsi_wl_display_ref(old_chain->display);
1081 } else {
1082 chain->display = NULL;
1083 result = wsi_wl_display_create(wsi, surface->display, &chain->display);
1084 if (result != VK_SUCCESS)
1085 goto fail;
1086 }
1087
1088 chain->surface = wl_proxy_create_wrapper(surface->surface);
1089 if (!chain->surface) {
1090 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1091 goto fail;
1092 }
1093 wl_proxy_set_queue((struct wl_proxy *) chain->surface,
1094 chain->display->queue);
1095 chain->surface_version = wl_proxy_get_version((void *)surface->surface);
1096
1097 chain->num_drm_modifiers = 0;
1098 chain->drm_modifiers = 0;
1099
1100 /* Use explicit DRM format modifiers when both the server and the driver
1101 * support them.
1102 */
1103 if (chain->display->dmabuf.wl_dmabuf &&
1104 chain->base.wsi->supports_modifiers) {
1105 struct u_vector *modifiers;
1106 switch (chain->drm_format) {
1107 case WL_DRM_FORMAT_ARGB8888:
1108 modifiers = &chain->display->dmabuf.modifiers.argb8888;
1109 break;
1110 case WL_DRM_FORMAT_XRGB8888:
1111 modifiers = &chain->display->dmabuf.modifiers.xrgb8888;
1112 break;
1113 default:
1114 modifiers = NULL;
1115 break;
1116 }
1117
1118 if (modifiers) {
1119 chain->drm_modifiers = u_vector_tail(modifiers);
1120 chain->num_drm_modifiers = u_vector_length(modifiers);
1121 }
1122 }
1123
1124 /* When there are explicit DRM format modifiers, we must use
1125 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1126 * wl_drm.
1127 */
1128 if (!chain->num_drm_modifiers) {
1129 assert(chain->display->drm.wl_drm);
1130
1131 chain->drm_wrapper =
1132 wl_proxy_create_wrapper(chain->display->drm.wl_drm);
1133 if (!chain->drm_wrapper) {
1134 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1135 goto fail;
1136 }
1137 wl_proxy_set_queue((struct wl_proxy *) chain->drm_wrapper,
1138 chain->display->queue);
1139 }
1140
1141 chain->fifo_ready = true;
1142
1143 for (uint32_t i = 0; i < chain->base.image_count; i++) {
1144 result = wsi_wl_image_init(chain, &chain->images[i],
1145 pCreateInfo, pAllocator);
1146 if (result != VK_SUCCESS)
1147 goto fail;
1148 chain->images[i].busy = false;
1149 }
1150
1151 *swapchain_out = &chain->base;
1152
1153 return VK_SUCCESS;
1154
1155 fail:
1156 wsi_wl_swapchain_destroy(&chain->base, pAllocator);
1157
1158 return result;
1159 }
1160
1161 VkResult
1162 wsi_wl_init_wsi(struct wsi_device *wsi_device,
1163 const VkAllocationCallbacks *alloc,
1164 VkPhysicalDevice physical_device)
1165 {
1166 struct wsi_wayland *wsi;
1167 VkResult result;
1168
1169 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1170 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1171 if (!wsi) {
1172 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1173 goto fail;
1174 }
1175
1176 wsi->physical_device = physical_device;
1177 wsi->alloc = alloc;
1178 wsi->wsi = wsi_device;
1179
1180 wsi->base.get_support = wsi_wl_surface_get_support;
1181 wsi->base.get_capabilities2 = wsi_wl_surface_get_capabilities2;
1182 wsi->base.get_formats = wsi_wl_surface_get_formats;
1183 wsi->base.get_formats2 = wsi_wl_surface_get_formats2;
1184 wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
1185 wsi->base.get_present_rectangles = wsi_wl_surface_get_present_rectangles;
1186 wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
1187
1188 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
1189
1190 return VK_SUCCESS;
1191
1192 fail:
1193 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
1194
1195 return result;
1196 }
1197
1198 void
1199 wsi_wl_finish_wsi(struct wsi_device *wsi_device,
1200 const VkAllocationCallbacks *alloc)
1201 {
1202 struct wsi_wayland *wsi =
1203 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1204 if (!wsi)
1205 return;
1206
1207 vk_free(alloc, wsi);
1208 }