wsi/wayland: only finish() a successfully init()ed display
[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 u_vector_finish(&display->modifiers.argb8888);
321 u_vector_finish(&display->modifiers.xrgb8888);
322 if (display->drm)
323 wl_drm_destroy(display->drm);
324 if (display->dmabuf)
325 zwp_linux_dmabuf_v1_destroy(display->dmabuf);
326 if (display->wl_display_wrapper)
327 wl_proxy_wrapper_destroy(display->wl_display_wrapper);
328 if (display->queue)
329 wl_event_queue_destroy(display->queue);
330 }
331
332 static VkResult
333 wsi_wl_display_init(struct wsi_wayland *wsi_wl,
334 struct wsi_wl_display *display,
335 struct wl_display *wl_display,
336 bool get_format_list)
337 {
338 VkResult result = VK_SUCCESS;
339 memset(display, 0, sizeof(*display));
340
341 display->wsi_wl = wsi_wl;
342 display->wl_display = wl_display;
343
344 if (get_format_list) {
345 if (!u_vector_init(&display->formats, sizeof(VkFormat), 8) ||
346 !u_vector_init(&display->modifiers.argb8888, sizeof(uint64_t), 32) ||
347 !u_vector_init(&display->modifiers.xrgb8888, sizeof(uint64_t), 32)) {
348 result = VK_ERROR_OUT_OF_HOST_MEMORY;
349 goto fail;
350 }
351 }
352
353 display->queue = wl_display_create_queue(wl_display);
354 if (!display->queue) {
355 result = VK_ERROR_OUT_OF_HOST_MEMORY;
356 goto fail;
357 }
358
359 display->wl_display_wrapper = wl_proxy_create_wrapper(wl_display);
360 if (!display->wl_display_wrapper) {
361 result = VK_ERROR_OUT_OF_HOST_MEMORY;
362 goto fail;
363 }
364
365 wl_proxy_set_queue((struct wl_proxy *) display->wl_display_wrapper,
366 display->queue);
367
368 struct wl_registry *registry =
369 wl_display_get_registry(display->wl_display_wrapper);
370 if (!registry) {
371 result = VK_ERROR_OUT_OF_HOST_MEMORY;
372 goto fail;
373 }
374
375 wl_registry_add_listener(registry, &registry_listener, display);
376
377 /* Round-trip to get the wl_drm global */
378 wl_display_roundtrip_queue(display->wl_display, display->queue);
379
380 if (!display->drm) {
381 result = VK_ERROR_SURFACE_LOST_KHR;
382 goto fail_registry;
383 }
384
385 /* Round-trip to get wl_drm formats and capabilities */
386 wl_display_roundtrip_queue(display->wl_display, display->queue);
387
388 /* We need prime support */
389 if (!(display->capabilities & WL_DRM_CAPABILITY_PRIME)) {
390 result = VK_ERROR_SURFACE_LOST_KHR;
391 goto fail_registry;
392 }
393
394 /* We don't need this anymore */
395 wl_registry_destroy(registry);
396
397 display->refcount = 0;
398
399 return VK_SUCCESS;
400
401 fail_registry:
402 if (registry)
403 wl_registry_destroy(registry);
404
405 fail:
406 wsi_wl_display_finish(display);
407 return result;
408 }
409
410 static VkResult
411 wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display,
412 struct wsi_wl_display **display_out)
413 {
414 struct wsi_wl_display *display =
415 vk_alloc(wsi->alloc, sizeof(*display), 8,
416 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
417 if (!display)
418 return VK_ERROR_OUT_OF_HOST_MEMORY;
419
420 VkResult result = wsi_wl_display_init(wsi, display, wl_display, true);
421 if (result != VK_SUCCESS) {
422 vk_free(wsi->alloc, display);
423 return result;
424 }
425
426 display->refcount++;
427 *display_out = display;
428
429 return result;
430 }
431
432 static struct wsi_wl_display *
433 wsi_wl_display_ref(struct wsi_wl_display *display)
434 {
435 display->refcount++;
436 return display;
437 }
438
439 static void
440 wsi_wl_display_unref(struct wsi_wl_display *display)
441 {
442 if (display->refcount-- > 1)
443 return;
444
445 struct wsi_wayland *wsi = display->wsi_wl;
446 wsi_wl_display_finish(display);
447 vk_free(wsi->alloc, display);
448 }
449
450 VkBool32
451 wsi_wl_get_presentation_support(struct wsi_device *wsi_device,
452 struct wl_display *wl_display)
453 {
454 struct wsi_wayland *wsi =
455 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
456
457 struct wsi_wl_display display;
458 VkResult ret = wsi_wl_display_init(wsi, &display, wl_display, false);
459 if (ret == VK_SUCCESS)
460 wsi_wl_display_finish(&display);
461
462 return ret == VK_SUCCESS;
463 }
464
465 static VkResult
466 wsi_wl_surface_get_support(VkIcdSurfaceBase *surface,
467 struct wsi_device *wsi_device,
468 uint32_t queueFamilyIndex,
469 VkBool32* pSupported)
470 {
471 *pSupported = true;
472
473 return VK_SUCCESS;
474 }
475
476 static const VkPresentModeKHR present_modes[] = {
477 VK_PRESENT_MODE_MAILBOX_KHR,
478 VK_PRESENT_MODE_FIFO_KHR,
479 };
480
481 static VkResult
482 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase *surface,
483 VkSurfaceCapabilitiesKHR* caps)
484 {
485 /* For true mailbox mode, we need at least 4 images:
486 * 1) One to scan out from
487 * 2) One to have queued for scan-out
488 * 3) One to be currently held by the Wayland compositor
489 * 4) One to render to
490 */
491 caps->minImageCount = 4;
492 /* There is no real maximum */
493 caps->maxImageCount = 0;
494
495 caps->currentExtent = (VkExtent2D) { -1, -1 };
496 caps->minImageExtent = (VkExtent2D) { 1, 1 };
497 /* This is the maximum supported size on Intel */
498 caps->maxImageExtent = (VkExtent2D) { 1 << 14, 1 << 14 };
499 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
500 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
501 caps->maxImageArrayLayers = 1;
502
503 caps->supportedCompositeAlpha =
504 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
505 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
506
507 caps->supportedUsageFlags =
508 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
509 VK_IMAGE_USAGE_SAMPLED_BIT |
510 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
511 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
512
513 return VK_SUCCESS;
514 }
515
516 static VkResult
517 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase *surface,
518 const void *info_next,
519 VkSurfaceCapabilities2KHR* caps)
520 {
521 assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
522
523 return wsi_wl_surface_get_capabilities(surface, &caps->surfaceCapabilities);
524 }
525
526 static VkResult
527 wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
528 struct wsi_device *wsi_device,
529 uint32_t* pSurfaceFormatCount,
530 VkSurfaceFormatKHR* pSurfaceFormats)
531 {
532 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
533 struct wsi_wayland *wsi =
534 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
535
536 struct wsi_wl_display display;
537 if (wsi_wl_display_init(wsi, &display, surface->display, true))
538 return VK_ERROR_SURFACE_LOST_KHR;
539
540 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
541
542 VkFormat *disp_fmt;
543 u_vector_foreach(disp_fmt, &display.formats) {
544 vk_outarray_append(&out, out_fmt) {
545 out_fmt->format = *disp_fmt;
546 out_fmt->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
547 }
548 }
549
550 wsi_wl_display_finish(&display);
551
552 return vk_outarray_status(&out);
553 }
554
555 static VkResult
556 wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
557 struct wsi_device *wsi_device,
558 const void *info_next,
559 uint32_t* pSurfaceFormatCount,
560 VkSurfaceFormat2KHR* pSurfaceFormats)
561 {
562 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
563 struct wsi_wayland *wsi =
564 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
565
566 struct wsi_wl_display display;
567 if (wsi_wl_display_init(wsi, &display, surface->display, true))
568 return VK_ERROR_SURFACE_LOST_KHR;
569
570 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
571
572 VkFormat *disp_fmt;
573 u_vector_foreach(disp_fmt, &display.formats) {
574 vk_outarray_append(&out, out_fmt) {
575 out_fmt->surfaceFormat.format = *disp_fmt;
576 out_fmt->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
577 }
578 }
579
580 wsi_wl_display_finish(&display);
581
582 return vk_outarray_status(&out);
583 }
584
585 static VkResult
586 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface,
587 uint32_t* pPresentModeCount,
588 VkPresentModeKHR* pPresentModes)
589 {
590 if (pPresentModes == NULL) {
591 *pPresentModeCount = ARRAY_SIZE(present_modes);
592 return VK_SUCCESS;
593 }
594
595 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
596 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
597
598 if (*pPresentModeCount < ARRAY_SIZE(present_modes))
599 return VK_INCOMPLETE;
600 else
601 return VK_SUCCESS;
602 }
603
604 static VkResult
605 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase *surface,
606 struct wsi_device *wsi_device,
607 uint32_t* pRectCount,
608 VkRect2D* pRects)
609 {
610 VK_OUTARRAY_MAKE(out, pRects, pRectCount);
611
612 vk_outarray_append(&out, rect) {
613 /* We don't know a size so just return the usual "I don't know." */
614 *rect = (VkRect2D) {
615 .offset = { 0, 0 },
616 .extent = { -1, -1 },
617 };
618 }
619
620 return vk_outarray_status(&out);
621 }
622
623 VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator,
624 const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
625 VkSurfaceKHR *pSurface)
626 {
627 VkIcdSurfaceWayland *surface;
628
629 surface = vk_alloc(pAllocator, sizeof *surface, 8,
630 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
631 if (surface == NULL)
632 return VK_ERROR_OUT_OF_HOST_MEMORY;
633
634 surface->base.platform = VK_ICD_WSI_PLATFORM_WAYLAND;
635 surface->display = pCreateInfo->display;
636 surface->surface = pCreateInfo->surface;
637
638 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
639
640 return VK_SUCCESS;
641 }
642
643 struct wsi_wl_image {
644 struct wsi_image base;
645 struct wl_buffer * buffer;
646 bool busy;
647 };
648
649 struct wsi_wl_swapchain {
650 struct wsi_swapchain base;
651
652 struct wsi_wl_display *display;
653
654 struct wl_surface * surface;
655 uint32_t surface_version;
656 struct wl_drm * drm_wrapper;
657 struct wl_callback * frame;
658
659 VkExtent2D extent;
660 VkFormat vk_format;
661 uint32_t drm_format;
662
663 VkPresentModeKHR present_mode;
664 bool fifo_ready;
665
666 struct wsi_wl_image images[0];
667 };
668
669 static struct wsi_image *
670 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain *wsi_chain,
671 uint32_t image_index)
672 {
673 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
674 return &chain->images[image_index].base;
675 }
676
677 static VkResult
678 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
679 const VkAcquireNextImageInfoKHR *info,
680 uint32_t *image_index)
681 {
682 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
683
684 int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
685 chain->display->queue);
686 /* XXX: I'm not sure if out-of-date is the right error here. If
687 * wl_display_dispatch_queue_pending fails it most likely means we got
688 * kicked by the server so this seems more-or-less correct.
689 */
690 if (ret < 0)
691 return VK_ERROR_OUT_OF_DATE_KHR;
692
693 while (1) {
694 for (uint32_t i = 0; i < chain->base.image_count; i++) {
695 if (!chain->images[i].busy) {
696 /* We found a non-busy image */
697 *image_index = i;
698 chain->images[i].busy = true;
699 return VK_SUCCESS;
700 }
701 }
702
703 /* This time we do a blocking dispatch because we can't go
704 * anywhere until we get an event.
705 */
706 int ret = wl_display_roundtrip_queue(chain->display->wl_display,
707 chain->display->queue);
708 if (ret < 0)
709 return VK_ERROR_OUT_OF_DATE_KHR;
710 }
711 }
712
713 static void
714 frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
715 {
716 struct wsi_wl_swapchain *chain = data;
717
718 chain->frame = NULL;
719 chain->fifo_ready = true;
720
721 wl_callback_destroy(callback);
722 }
723
724 static const struct wl_callback_listener frame_listener = {
725 frame_handle_done,
726 };
727
728 static VkResult
729 wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
730 uint32_t image_index,
731 const VkPresentRegionKHR *damage)
732 {
733 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
734
735 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
736 while (!chain->fifo_ready) {
737 int ret = wl_display_dispatch_queue(chain->display->wl_display,
738 chain->display->queue);
739 if (ret < 0)
740 return VK_ERROR_OUT_OF_DATE_KHR;
741 }
742 }
743
744 assert(image_index < chain->base.image_count);
745 wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
746
747 if (chain->surface_version >= 4 && damage &&
748 damage->pRectangles && damage->rectangleCount > 0) {
749 for (unsigned i = 0; i < damage->rectangleCount; i++) {
750 const VkRectLayerKHR *rect = &damage->pRectangles[i];
751 assert(rect->layer == 0);
752 wl_surface_damage_buffer(chain->surface,
753 rect->offset.x, rect->offset.y,
754 rect->extent.width, rect->extent.height);
755 }
756 } else {
757 wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
758 }
759
760 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
761 chain->frame = wl_surface_frame(chain->surface);
762 wl_callback_add_listener(chain->frame, &frame_listener, chain);
763 chain->fifo_ready = false;
764 }
765
766 chain->images[image_index].busy = true;
767 wl_surface_commit(chain->surface);
768 wl_display_flush(chain->display->wl_display);
769
770 return VK_SUCCESS;
771 }
772
773 static void
774 buffer_handle_release(void *data, struct wl_buffer *buffer)
775 {
776 struct wsi_wl_image *image = data;
777
778 assert(image->buffer == buffer);
779
780 image->busy = false;
781 }
782
783 static const struct wl_buffer_listener buffer_listener = {
784 buffer_handle_release,
785 };
786
787 static VkResult
788 wsi_wl_image_init(struct wsi_wl_swapchain *chain,
789 struct wsi_wl_image *image,
790 const VkSwapchainCreateInfoKHR *pCreateInfo,
791 const VkAllocationCallbacks* pAllocator)
792 {
793 struct wsi_wl_display *display = chain->display;
794 const uint64_t *modifiers = NULL;
795 uint32_t num_modifiers = 0;
796 VkResult result;
797
798 if (display->dmabuf && chain->base.wsi->supports_modifiers) {
799 switch (chain->drm_format) {
800 case WL_DRM_FORMAT_ARGB8888:
801 modifiers = u_vector_tail(&display->modifiers.argb8888);
802 num_modifiers = u_vector_length(&display->modifiers.argb8888);
803 break;
804 case WL_DRM_FORMAT_XRGB8888:
805 modifiers = u_vector_tail(&display->modifiers.xrgb8888);
806 num_modifiers = u_vector_length(&display->modifiers.xrgb8888);
807 break;
808 default:
809 break;
810 }
811 }
812
813 result = wsi_create_native_image(&chain->base, pCreateInfo,
814 num_modifiers > 0 ? 1 : 0,
815 &num_modifiers, &modifiers,
816 &image->base);
817
818 if (result != VK_SUCCESS)
819 return result;
820
821 if (image->base.drm_modifier != DRM_FORMAT_MOD_INVALID) {
822 /* Only request modifiers if we have dmabuf, else it must be implicit. */
823 assert(display->dmabuf);
824
825 struct zwp_linux_buffer_params_v1 *params =
826 zwp_linux_dmabuf_v1_create_params(display->dmabuf);
827 wl_proxy_set_queue((struct wl_proxy *) params, chain->display->queue);
828
829 for (int i = 0; i < image->base.num_planes; i++) {
830 zwp_linux_buffer_params_v1_add(params,
831 image->base.fds[i],
832 i,
833 image->base.offsets[i],
834 image->base.row_pitches[i],
835 image->base.drm_modifier >> 32,
836 image->base.drm_modifier & 0xffffffff);
837 close(image->base.fds[i]);
838 }
839
840 image->buffer =
841 zwp_linux_buffer_params_v1_create_immed(params,
842 chain->extent.width,
843 chain->extent.height,
844 chain->drm_format,
845 0);
846 zwp_linux_buffer_params_v1_destroy(params);
847 } else {
848 /* Without passing modifiers, we can't have multi-plane RGB images. */
849 assert(image->base.num_planes == 1);
850
851 image->buffer =
852 wl_drm_create_prime_buffer(chain->drm_wrapper,
853 image->base.fds[0], /* name */
854 chain->extent.width,
855 chain->extent.height,
856 chain->drm_format,
857 image->base.offsets[0],
858 image->base.row_pitches[0],
859 0, 0, 0, 0 /* unused */);
860 close(image->base.fds[0]);
861 }
862
863 if (!image->buffer)
864 goto fail_image;
865
866 wl_buffer_add_listener(image->buffer, &buffer_listener, image);
867
868 return VK_SUCCESS;
869
870 fail_image:
871 wsi_destroy_image(&chain->base, &image->base);
872
873 return result;
874 }
875
876 static VkResult
877 wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
878 const VkAllocationCallbacks *pAllocator)
879 {
880 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
881
882 for (uint32_t i = 0; i < chain->base.image_count; i++) {
883 if (chain->images[i].buffer) {
884 wl_buffer_destroy(chain->images[i].buffer);
885 wsi_destroy_image(&chain->base, &chain->images[i].base);
886 }
887 }
888
889 if (chain->frame)
890 wl_callback_destroy(chain->frame);
891 if (chain->surface)
892 wl_proxy_wrapper_destroy(chain->surface);
893 if (chain->drm_wrapper)
894 wl_proxy_wrapper_destroy(chain->drm_wrapper);
895
896 if (chain->display)
897 wsi_wl_display_unref(chain->display);
898
899 wsi_swapchain_finish(&chain->base);
900
901 vk_free(pAllocator, chain);
902
903 return VK_SUCCESS;
904 }
905
906 static VkResult
907 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
908 VkDevice device,
909 struct wsi_device *wsi_device,
910 const VkSwapchainCreateInfoKHR* pCreateInfo,
911 const VkAllocationCallbacks* pAllocator,
912 struct wsi_swapchain **swapchain_out)
913 {
914 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
915 struct wsi_wayland *wsi =
916 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
917 struct wsi_wl_swapchain *chain;
918 VkResult result;
919
920 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
921
922 int num_images = pCreateInfo->minImageCount;
923
924 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
925 chain = vk_alloc(pAllocator, size, 8,
926 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
927 if (chain == NULL)
928 return VK_ERROR_OUT_OF_HOST_MEMORY;
929
930 result = wsi_swapchain_init(wsi_device, &chain->base, device,
931 pCreateInfo, pAllocator);
932 if (result != VK_SUCCESS) {
933 vk_free(pAllocator, chain);
934 return result;
935 }
936
937 /* Mark a bunch of stuff as NULL. This way we can just call
938 * destroy_swapchain for cleanup.
939 */
940 for (uint32_t i = 0; i < num_images; i++)
941 chain->images[i].buffer = NULL;
942 chain->surface = NULL;
943 chain->drm_wrapper = NULL;
944 chain->frame = NULL;
945
946 bool alpha = pCreateInfo->compositeAlpha ==
947 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
948
949 chain->base.destroy = wsi_wl_swapchain_destroy;
950 chain->base.get_wsi_image = wsi_wl_swapchain_get_wsi_image;
951 chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
952 chain->base.queue_present = wsi_wl_swapchain_queue_present;
953 chain->base.present_mode = pCreateInfo->presentMode;
954 chain->base.image_count = num_images;
955 chain->extent = pCreateInfo->imageExtent;
956 chain->vk_format = pCreateInfo->imageFormat;
957 chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
958
959 if (pCreateInfo->oldSwapchain) {
960 /* If we have an oldSwapchain parameter, copy the display struct over
961 * from the old one so we don't have to fully re-initialize it.
962 */
963 struct wsi_wl_swapchain *old_chain = (void *)pCreateInfo->oldSwapchain;
964 chain->display = wsi_wl_display_ref(old_chain->display);
965 } else {
966 chain->display = NULL;
967 result = wsi_wl_display_create(wsi, surface->display, &chain->display);
968 if (result != VK_SUCCESS)
969 goto fail;
970 }
971
972 chain->surface = wl_proxy_create_wrapper(surface->surface);
973 if (!chain->surface) {
974 result = VK_ERROR_OUT_OF_HOST_MEMORY;
975 goto fail;
976 }
977 wl_proxy_set_queue((struct wl_proxy *) chain->surface,
978 chain->display->queue);
979 chain->surface_version = wl_proxy_get_version((void *)surface->surface);
980
981 chain->drm_wrapper = wl_proxy_create_wrapper(chain->display->drm);
982 if (!chain->drm_wrapper) {
983 result = VK_ERROR_OUT_OF_HOST_MEMORY;
984 goto fail;
985 }
986 wl_proxy_set_queue((struct wl_proxy *) chain->drm_wrapper,
987 chain->display->queue);
988
989 chain->fifo_ready = true;
990
991 for (uint32_t i = 0; i < chain->base.image_count; i++) {
992 result = wsi_wl_image_init(chain, &chain->images[i],
993 pCreateInfo, pAllocator);
994 if (result != VK_SUCCESS)
995 goto fail;
996 chain->images[i].busy = false;
997 }
998
999 *swapchain_out = &chain->base;
1000
1001 return VK_SUCCESS;
1002
1003 fail:
1004 wsi_wl_swapchain_destroy(&chain->base, pAllocator);
1005
1006 return result;
1007 }
1008
1009 VkResult
1010 wsi_wl_init_wsi(struct wsi_device *wsi_device,
1011 const VkAllocationCallbacks *alloc,
1012 VkPhysicalDevice physical_device)
1013 {
1014 struct wsi_wayland *wsi;
1015 VkResult result;
1016
1017 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1018 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1019 if (!wsi) {
1020 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1021 goto fail;
1022 }
1023
1024 wsi->physical_device = physical_device;
1025 wsi->alloc = alloc;
1026 wsi->wsi = wsi_device;
1027
1028 wsi->base.get_support = wsi_wl_surface_get_support;
1029 wsi->base.get_capabilities2 = wsi_wl_surface_get_capabilities2;
1030 wsi->base.get_formats = wsi_wl_surface_get_formats;
1031 wsi->base.get_formats2 = wsi_wl_surface_get_formats2;
1032 wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
1033 wsi->base.get_present_rectangles = wsi_wl_surface_get_present_rectangles;
1034 wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
1035
1036 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
1037
1038 return VK_SUCCESS;
1039
1040 fail:
1041 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
1042
1043 return result;
1044 }
1045
1046 void
1047 wsi_wl_finish_wsi(struct wsi_device *wsi_device,
1048 const VkAllocationCallbacks *alloc)
1049 {
1050 struct wsi_wayland *wsi =
1051 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1052 if (!wsi)
1053 return;
1054
1055 vk_free(alloc, wsi);
1056 }