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