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