vulkan/wsi: create wl_drm wrapper as needed
[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, 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 struct wsi_device *wsi_device,
484 VkSurfaceCapabilitiesKHR* caps)
485 {
486 /* For true mailbox mode, we need at least 4 images:
487 * 1) One to scan out from
488 * 2) One to have queued for scan-out
489 * 3) One to be currently held by the Wayland compositor
490 * 4) One to render to
491 */
492 caps->minImageCount = 4;
493 /* There is no real maximum */
494 caps->maxImageCount = 0;
495
496 caps->currentExtent = (VkExtent2D) { -1, -1 };
497 caps->minImageExtent = (VkExtent2D) { 1, 1 };
498 caps->maxImageExtent = (VkExtent2D) {
499 wsi_device->maxImageDimension2D,
500 wsi_device->maxImageDimension2D,
501 };
502
503 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
504 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
505 caps->maxImageArrayLayers = 1;
506
507 caps->supportedCompositeAlpha =
508 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
509 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
510
511 caps->supportedUsageFlags =
512 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
513 VK_IMAGE_USAGE_SAMPLED_BIT |
514 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
515 VK_IMAGE_USAGE_STORAGE_BIT |
516 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
517
518 return VK_SUCCESS;
519 }
520
521 static VkResult
522 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase *surface,
523 struct wsi_device *wsi_device,
524 const void *info_next,
525 VkSurfaceCapabilities2KHR* caps)
526 {
527 assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
528
529 VkResult result =
530 wsi_wl_surface_get_capabilities(surface, wsi_device,
531 &caps->surfaceCapabilities);
532
533 vk_foreach_struct(ext, caps->pNext) {
534 switch (ext->sType) {
535 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
536 VkSurfaceProtectedCapabilitiesKHR *protected = (void *)ext;
537 protected->supportsProtected = VK_FALSE;
538 break;
539 }
540
541 default:
542 /* Ignored */
543 break;
544 }
545 }
546
547 return result;
548 }
549
550 static VkResult
551 wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
552 struct wsi_device *wsi_device,
553 uint32_t* pSurfaceFormatCount,
554 VkSurfaceFormatKHR* pSurfaceFormats)
555 {
556 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
557 struct wsi_wayland *wsi =
558 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
559
560 struct wsi_wl_display display;
561 if (wsi_wl_display_init(wsi, &display, surface->display, true))
562 return VK_ERROR_SURFACE_LOST_KHR;
563
564 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
565
566 VkFormat *disp_fmt;
567 u_vector_foreach(disp_fmt, &display.formats) {
568 vk_outarray_append(&out, out_fmt) {
569 out_fmt->format = *disp_fmt;
570 out_fmt->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
571 }
572 }
573
574 wsi_wl_display_finish(&display);
575
576 return vk_outarray_status(&out);
577 }
578
579 static VkResult
580 wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
581 struct wsi_device *wsi_device,
582 const void *info_next,
583 uint32_t* pSurfaceFormatCount,
584 VkSurfaceFormat2KHR* pSurfaceFormats)
585 {
586 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
587 struct wsi_wayland *wsi =
588 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
589
590 struct wsi_wl_display display;
591 if (wsi_wl_display_init(wsi, &display, surface->display, true))
592 return VK_ERROR_SURFACE_LOST_KHR;
593
594 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
595
596 VkFormat *disp_fmt;
597 u_vector_foreach(disp_fmt, &display.formats) {
598 vk_outarray_append(&out, out_fmt) {
599 out_fmt->surfaceFormat.format = *disp_fmt;
600 out_fmt->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
601 }
602 }
603
604 wsi_wl_display_finish(&display);
605
606 return vk_outarray_status(&out);
607 }
608
609 static VkResult
610 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface,
611 uint32_t* pPresentModeCount,
612 VkPresentModeKHR* pPresentModes)
613 {
614 if (pPresentModes == NULL) {
615 *pPresentModeCount = ARRAY_SIZE(present_modes);
616 return VK_SUCCESS;
617 }
618
619 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
620 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
621
622 if (*pPresentModeCount < ARRAY_SIZE(present_modes))
623 return VK_INCOMPLETE;
624 else
625 return VK_SUCCESS;
626 }
627
628 static VkResult
629 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase *surface,
630 struct wsi_device *wsi_device,
631 uint32_t* pRectCount,
632 VkRect2D* pRects)
633 {
634 VK_OUTARRAY_MAKE(out, pRects, pRectCount);
635
636 vk_outarray_append(&out, rect) {
637 /* We don't know a size so just return the usual "I don't know." */
638 *rect = (VkRect2D) {
639 .offset = { 0, 0 },
640 .extent = { -1, -1 },
641 };
642 }
643
644 return vk_outarray_status(&out);
645 }
646
647 VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator,
648 const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
649 VkSurfaceKHR *pSurface)
650 {
651 VkIcdSurfaceWayland *surface;
652
653 surface = vk_alloc(pAllocator, sizeof *surface, 8,
654 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
655 if (surface == NULL)
656 return VK_ERROR_OUT_OF_HOST_MEMORY;
657
658 surface->base.platform = VK_ICD_WSI_PLATFORM_WAYLAND;
659 surface->display = pCreateInfo->display;
660 surface->surface = pCreateInfo->surface;
661
662 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
663
664 return VK_SUCCESS;
665 }
666
667 struct wsi_wl_image {
668 struct wsi_image base;
669 struct wl_buffer * buffer;
670 bool busy;
671 };
672
673 struct wsi_wl_swapchain {
674 struct wsi_swapchain base;
675
676 struct wsi_wl_display *display;
677
678 struct wl_surface * surface;
679 uint32_t surface_version;
680
681 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
682 * zwp_linux_dmabuf_v1 should be used.
683 */
684 struct wl_drm * drm_wrapper;
685
686 struct wl_callback * frame;
687
688 VkExtent2D extent;
689 VkFormat vk_format;
690 uint32_t drm_format;
691
692 uint32_t num_drm_modifiers;
693 const uint64_t * drm_modifiers;
694
695 VkPresentModeKHR present_mode;
696 bool fifo_ready;
697
698 struct wsi_wl_image images[0];
699 };
700 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain, VkSwapchainKHR)
701
702 static struct wsi_image *
703 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain *wsi_chain,
704 uint32_t image_index)
705 {
706 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
707 return &chain->images[image_index].base;
708 }
709
710 static VkResult
711 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
712 const VkAcquireNextImageInfoKHR *info,
713 uint32_t *image_index)
714 {
715 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
716
717 int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
718 chain->display->queue);
719 /* XXX: I'm not sure if out-of-date is the right error here. If
720 * wl_display_dispatch_queue_pending fails it most likely means we got
721 * kicked by the server so this seems more-or-less correct.
722 */
723 if (ret < 0)
724 return VK_ERROR_OUT_OF_DATE_KHR;
725
726 while (1) {
727 for (uint32_t i = 0; i < chain->base.image_count; i++) {
728 if (!chain->images[i].busy) {
729 /* We found a non-busy image */
730 *image_index = i;
731 chain->images[i].busy = true;
732 return VK_SUCCESS;
733 }
734 }
735
736 /* This time we do a blocking dispatch because we can't go
737 * anywhere until we get an event.
738 */
739 int ret = wl_display_roundtrip_queue(chain->display->wl_display,
740 chain->display->queue);
741 if (ret < 0)
742 return VK_ERROR_OUT_OF_DATE_KHR;
743 }
744 }
745
746 static void
747 frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
748 {
749 struct wsi_wl_swapchain *chain = data;
750
751 chain->frame = NULL;
752 chain->fifo_ready = true;
753
754 wl_callback_destroy(callback);
755 }
756
757 static const struct wl_callback_listener frame_listener = {
758 frame_handle_done,
759 };
760
761 static VkResult
762 wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
763 uint32_t image_index,
764 const VkPresentRegionKHR *damage)
765 {
766 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
767
768 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
769 while (!chain->fifo_ready) {
770 int ret = wl_display_dispatch_queue(chain->display->wl_display,
771 chain->display->queue);
772 if (ret < 0)
773 return VK_ERROR_OUT_OF_DATE_KHR;
774 }
775 }
776
777 assert(image_index < chain->base.image_count);
778 wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
779
780 if (chain->surface_version >= 4 && damage &&
781 damage->pRectangles && damage->rectangleCount > 0) {
782 for (unsigned i = 0; i < damage->rectangleCount; i++) {
783 const VkRectLayerKHR *rect = &damage->pRectangles[i];
784 assert(rect->layer == 0);
785 wl_surface_damage_buffer(chain->surface,
786 rect->offset.x, rect->offset.y,
787 rect->extent.width, rect->extent.height);
788 }
789 } else {
790 wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
791 }
792
793 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
794 chain->frame = wl_surface_frame(chain->surface);
795 wl_callback_add_listener(chain->frame, &frame_listener, chain);
796 chain->fifo_ready = false;
797 }
798
799 chain->images[image_index].busy = true;
800 wl_surface_commit(chain->surface);
801 wl_display_flush(chain->display->wl_display);
802
803 return VK_SUCCESS;
804 }
805
806 static void
807 buffer_handle_release(void *data, struct wl_buffer *buffer)
808 {
809 struct wsi_wl_image *image = data;
810
811 assert(image->buffer == buffer);
812
813 image->busy = false;
814 }
815
816 static const struct wl_buffer_listener buffer_listener = {
817 buffer_handle_release,
818 };
819
820 static VkResult
821 wsi_wl_image_init(struct wsi_wl_swapchain *chain,
822 struct wsi_wl_image *image,
823 const VkSwapchainCreateInfoKHR *pCreateInfo,
824 const VkAllocationCallbacks* pAllocator)
825 {
826 struct wsi_wl_display *display = chain->display;
827 VkResult result;
828
829 result = wsi_create_native_image(&chain->base, pCreateInfo,
830 chain->num_drm_modifiers > 0 ? 1 : 0,
831 &chain->num_drm_modifiers,
832 &chain->drm_modifiers, &image->base);
833
834 if (result != VK_SUCCESS)
835 return result;
836
837 if (!chain->drm_wrapper) {
838 /* Only request modifiers if we have dmabuf, else it must be implicit. */
839 assert(display->dmabuf);
840 assert(image->base.drm_modifier != DRM_FORMAT_MOD_INVALID);
841
842 struct zwp_linux_buffer_params_v1 *params =
843 zwp_linux_dmabuf_v1_create_params(display->dmabuf);
844 wl_proxy_set_queue((struct wl_proxy *) params, chain->display->queue);
845
846 for (int i = 0; i < image->base.num_planes; i++) {
847 zwp_linux_buffer_params_v1_add(params,
848 image->base.fds[i],
849 i,
850 image->base.offsets[i],
851 image->base.row_pitches[i],
852 image->base.drm_modifier >> 32,
853 image->base.drm_modifier & 0xffffffff);
854 close(image->base.fds[i]);
855 }
856
857 image->buffer =
858 zwp_linux_buffer_params_v1_create_immed(params,
859 chain->extent.width,
860 chain->extent.height,
861 chain->drm_format,
862 0);
863 zwp_linux_buffer_params_v1_destroy(params);
864 } else {
865 /* Without passing modifiers, we can't have multi-plane RGB images. */
866 assert(image->base.num_planes == 1);
867 assert(image->base.drm_modifier == DRM_FORMAT_MOD_INVALID);
868
869 image->buffer =
870 wl_drm_create_prime_buffer(chain->drm_wrapper,
871 image->base.fds[0], /* name */
872 chain->extent.width,
873 chain->extent.height,
874 chain->drm_format,
875 image->base.offsets[0],
876 image->base.row_pitches[0],
877 0, 0, 0, 0 /* unused */);
878 close(image->base.fds[0]);
879 }
880
881 if (!image->buffer)
882 goto fail_image;
883
884 wl_buffer_add_listener(image->buffer, &buffer_listener, image);
885
886 return VK_SUCCESS;
887
888 fail_image:
889 wsi_destroy_image(&chain->base, &image->base);
890
891 return result;
892 }
893
894 static VkResult
895 wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
896 const VkAllocationCallbacks *pAllocator)
897 {
898 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
899
900 for (uint32_t i = 0; i < chain->base.image_count; i++) {
901 if (chain->images[i].buffer) {
902 wl_buffer_destroy(chain->images[i].buffer);
903 wsi_destroy_image(&chain->base, &chain->images[i].base);
904 }
905 }
906
907 if (chain->frame)
908 wl_callback_destroy(chain->frame);
909 if (chain->surface)
910 wl_proxy_wrapper_destroy(chain->surface);
911 if (chain->drm_wrapper)
912 wl_proxy_wrapper_destroy(chain->drm_wrapper);
913
914 if (chain->display)
915 wsi_wl_display_unref(chain->display);
916
917 wsi_swapchain_finish(&chain->base);
918
919 vk_free(pAllocator, chain);
920
921 return VK_SUCCESS;
922 }
923
924 static VkResult
925 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
926 VkDevice device,
927 struct wsi_device *wsi_device,
928 const VkSwapchainCreateInfoKHR* pCreateInfo,
929 const VkAllocationCallbacks* pAllocator,
930 struct wsi_swapchain **swapchain_out)
931 {
932 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
933 struct wsi_wayland *wsi =
934 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
935 struct wsi_wl_swapchain *chain;
936 VkResult result;
937
938 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
939
940 int num_images = pCreateInfo->minImageCount;
941
942 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
943 chain = vk_alloc(pAllocator, size, 8,
944 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
945 if (chain == NULL)
946 return VK_ERROR_OUT_OF_HOST_MEMORY;
947
948 result = wsi_swapchain_init(wsi_device, &chain->base, device,
949 pCreateInfo, pAllocator);
950 if (result != VK_SUCCESS) {
951 vk_free(pAllocator, chain);
952 return result;
953 }
954
955 /* Mark a bunch of stuff as NULL. This way we can just call
956 * destroy_swapchain for cleanup.
957 */
958 for (uint32_t i = 0; i < num_images; i++)
959 chain->images[i].buffer = NULL;
960 chain->surface = NULL;
961 chain->drm_wrapper = NULL;
962 chain->frame = NULL;
963
964 bool alpha = pCreateInfo->compositeAlpha ==
965 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
966
967 chain->base.destroy = wsi_wl_swapchain_destroy;
968 chain->base.get_wsi_image = wsi_wl_swapchain_get_wsi_image;
969 chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
970 chain->base.queue_present = wsi_wl_swapchain_queue_present;
971 chain->base.present_mode = pCreateInfo->presentMode;
972 chain->base.image_count = num_images;
973 chain->extent = pCreateInfo->imageExtent;
974 chain->vk_format = pCreateInfo->imageFormat;
975 chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
976
977 if (pCreateInfo->oldSwapchain) {
978 /* If we have an oldSwapchain parameter, copy the display struct over
979 * from the old one so we don't have to fully re-initialize it.
980 */
981 WSI_FROM_HANDLE(wsi_wl_swapchain, old_chain, pCreateInfo->oldSwapchain);
982 chain->display = wsi_wl_display_ref(old_chain->display);
983 } else {
984 chain->display = NULL;
985 result = wsi_wl_display_create(wsi, surface->display, &chain->display);
986 if (result != VK_SUCCESS)
987 goto fail;
988 }
989
990 chain->surface = wl_proxy_create_wrapper(surface->surface);
991 if (!chain->surface) {
992 result = VK_ERROR_OUT_OF_HOST_MEMORY;
993 goto fail;
994 }
995 wl_proxy_set_queue((struct wl_proxy *) chain->surface,
996 chain->display->queue);
997 chain->surface_version = wl_proxy_get_version((void *)surface->surface);
998
999 chain->num_drm_modifiers = 0;
1000 chain->drm_modifiers = 0;
1001
1002 /* Use explicit DRM format modifiers when both the server and the driver
1003 * support them.
1004 */
1005 if (chain->display->dmabuf && chain->base.wsi->supports_modifiers) {
1006 struct u_vector *modifiers;
1007 switch (chain->drm_format) {
1008 case WL_DRM_FORMAT_ARGB8888:
1009 modifiers = &chain->display->modifiers.argb8888;
1010 break;
1011 case WL_DRM_FORMAT_XRGB8888:
1012 modifiers = &chain->display->modifiers.xrgb8888;
1013 break;
1014 default:
1015 modifiers = NULL;
1016 break;
1017 }
1018
1019 if (modifiers) {
1020 chain->drm_modifiers = u_vector_tail(modifiers);
1021 chain->num_drm_modifiers = u_vector_length(modifiers);
1022 }
1023 }
1024
1025 /* When there are explicit DRM format modifiers, we must use
1026 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1027 * wl_drm.
1028 */
1029 if (!chain->num_drm_modifiers) {
1030 chain->drm_wrapper = wl_proxy_create_wrapper(chain->display->drm);
1031 if (!chain->drm_wrapper) {
1032 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1033 goto fail;
1034 }
1035 wl_proxy_set_queue((struct wl_proxy *) chain->drm_wrapper,
1036 chain->display->queue);
1037 }
1038
1039 chain->fifo_ready = true;
1040
1041 for (uint32_t i = 0; i < chain->base.image_count; i++) {
1042 result = wsi_wl_image_init(chain, &chain->images[i],
1043 pCreateInfo, pAllocator);
1044 if (result != VK_SUCCESS)
1045 goto fail;
1046 chain->images[i].busy = false;
1047 }
1048
1049 *swapchain_out = &chain->base;
1050
1051 return VK_SUCCESS;
1052
1053 fail:
1054 wsi_wl_swapchain_destroy(&chain->base, pAllocator);
1055
1056 return result;
1057 }
1058
1059 VkResult
1060 wsi_wl_init_wsi(struct wsi_device *wsi_device,
1061 const VkAllocationCallbacks *alloc,
1062 VkPhysicalDevice physical_device)
1063 {
1064 struct wsi_wayland *wsi;
1065 VkResult result;
1066
1067 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1068 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1069 if (!wsi) {
1070 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1071 goto fail;
1072 }
1073
1074 wsi->physical_device = physical_device;
1075 wsi->alloc = alloc;
1076 wsi->wsi = wsi_device;
1077
1078 wsi->base.get_support = wsi_wl_surface_get_support;
1079 wsi->base.get_capabilities2 = wsi_wl_surface_get_capabilities2;
1080 wsi->base.get_formats = wsi_wl_surface_get_formats;
1081 wsi->base.get_formats2 = wsi_wl_surface_get_formats2;
1082 wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
1083 wsi->base.get_present_rectangles = wsi_wl_surface_get_present_rectangles;
1084 wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
1085
1086 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
1087
1088 return VK_SUCCESS;
1089
1090 fail:
1091 wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
1092
1093 return result;
1094 }
1095
1096 void
1097 wsi_wl_finish_wsi(struct wsi_device *wsi_device,
1098 const VkAllocationCallbacks *alloc)
1099 {
1100 struct wsi_wayland *wsi =
1101 (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
1102 if (!wsi)
1103 return;
1104
1105 vk_free(alloc, wsi);
1106 }