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