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