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