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