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