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