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