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