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