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