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