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