Merge remote-tracking branch 'public/master' into vulkan
[mesa.git] / src / intel / 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_interface 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)->isl_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_SRGB:
107 return WL_DRM_FORMAT_BGRX8888;
108 case VK_FORMAT_B8G8R8A8_SRGB:
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_SRGB);
163 /* fallthrough */
164 case WL_DRM_FORMAT_ARGB8888:
165 wsi_wl_display_add_vk_format(display, VK_FORMAT_B8G8R8A8_SRGB);
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_INSTANCE);
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 anv_instance *instance, struct wl_display *wl_display)
282 {
283 struct wsi_wayland *wsi =
284 (struct wsi_wayland *)instance->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
285
286 pthread_mutex_lock(&wsi->mutex);
287
288 struct hash_entry *entry = _mesa_hash_table_search(wsi->displays,
289 wl_display);
290 if (!entry) {
291 /* We're about to make a bunch of blocking calls. Let's drop the
292 * mutex for now so we don't block up too badly.
293 */
294 pthread_mutex_unlock(&wsi->mutex);
295
296 struct wsi_wl_display *display = wsi_wl_display_create(wsi, wl_display);
297
298 pthread_mutex_lock(&wsi->mutex);
299
300 entry = _mesa_hash_table_search(wsi->displays, wl_display);
301 if (entry) {
302 /* Oops, someone raced us to it */
303 wsi_wl_display_destroy(wsi, display);
304 } else {
305 entry = _mesa_hash_table_insert(wsi->displays, wl_display, display);
306 }
307 }
308
309 pthread_mutex_unlock(&wsi->mutex);
310
311 return entry->data;
312 }
313
314 VkBool32 anv_GetPhysicalDeviceWaylandPresentationSupportKHR(
315 VkPhysicalDevice physicalDevice,
316 uint32_t queueFamilyIndex,
317 struct wl_display* display)
318 {
319 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
320
321 return wsi_wl_get_display(physical_device->instance, display) != NULL;
322 }
323
324 static VkResult
325 wsi_wl_surface_get_support(VkIcdSurfaceBase *surface,
326 struct anv_physical_device *device,
327 uint32_t queueFamilyIndex,
328 VkBool32* pSupported)
329 {
330 *pSupported = true;
331
332 return VK_SUCCESS;
333 }
334
335 static const VkPresentModeKHR present_modes[] = {
336 VK_PRESENT_MODE_MAILBOX_KHR,
337 VK_PRESENT_MODE_FIFO_KHR,
338 };
339
340 static VkResult
341 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase *surface,
342 struct anv_physical_device *device,
343 VkSurfaceCapabilitiesKHR* caps)
344 {
345 caps->minImageCount = MIN_NUM_IMAGES;
346 caps->maxImageCount = 4;
347 caps->currentExtent = (VkExtent2D) { -1, -1 };
348 caps->minImageExtent = (VkExtent2D) { 1, 1 };
349 caps->maxImageExtent = (VkExtent2D) { INT16_MAX, INT16_MAX };
350 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
351 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
352 caps->maxImageArrayLayers = 1;
353
354 caps->supportedCompositeAlpha =
355 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
356 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
357
358 caps->supportedUsageFlags =
359 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
360 VK_IMAGE_USAGE_SAMPLED_BIT |
361 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
362 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
363
364 return VK_SUCCESS;
365 }
366
367 static VkResult
368 wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
369 struct anv_physical_device *device,
370 uint32_t* pSurfaceFormatCount,
371 VkSurfaceFormatKHR* pSurfaceFormats)
372 {
373 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
374 struct wsi_wl_display *display =
375 wsi_wl_get_display(device->instance, surface->display);
376
377 uint32_t count = anv_vector_length(&display->formats);
378
379 if (pSurfaceFormats == NULL) {
380 *pSurfaceFormatCount = count;
381 return VK_SUCCESS;
382 }
383
384 assert(*pSurfaceFormatCount >= count);
385 *pSurfaceFormatCount = count;
386
387 VkFormat *f;
388 anv_vector_foreach(f, &display->formats) {
389 *(pSurfaceFormats++) = (VkSurfaceFormatKHR) {
390 .format = *f,
391 /* TODO: We should get this from the compositor somehow */
392 .colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR,
393 };
394 }
395
396 return VK_SUCCESS;
397 }
398
399 static VkResult
400 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface,
401 struct anv_physical_device *device,
402 uint32_t* pPresentModeCount,
403 VkPresentModeKHR* pPresentModes)
404 {
405 if (pPresentModes == NULL) {
406 *pPresentModeCount = ARRAY_SIZE(present_modes);
407 return VK_SUCCESS;
408 }
409
410 assert(*pPresentModeCount >= ARRAY_SIZE(present_modes));
411 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
412 *pPresentModeCount = ARRAY_SIZE(present_modes);
413
414 return VK_SUCCESS;
415 }
416
417 static VkResult
418 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *surface,
419 struct anv_device *device,
420 const VkSwapchainCreateInfoKHR* pCreateInfo,
421 const VkAllocationCallbacks* pAllocator,
422 struct anv_swapchain **swapchain);
423
424 VkResult anv_CreateWaylandSurfaceKHR(
425 VkInstance _instance,
426 const VkWaylandSurfaceCreateInfoKHR* pCreateInfo,
427 const VkAllocationCallbacks* pAllocator,
428 VkSurfaceKHR* pSurface)
429 {
430 ANV_FROM_HANDLE(anv_instance, instance, _instance);
431
432 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR);
433
434 VkIcdSurfaceWayland *surface;
435
436 surface = anv_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
437 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
438 if (surface == NULL)
439 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
440
441 surface->base.platform = VK_ICD_WSI_PLATFORM_WAYLAND;
442 surface->display = pCreateInfo->display;
443 surface->surface = pCreateInfo->surface;
444
445 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
446
447 return VK_SUCCESS;
448 }
449
450 struct wsi_wl_image {
451 struct anv_image * image;
452 struct anv_device_memory * memory;
453 struct wl_buffer * buffer;
454 bool busy;
455 };
456
457 struct wsi_wl_swapchain {
458 struct anv_swapchain base;
459
460 struct wsi_wl_display * display;
461 struct wl_event_queue * queue;
462 struct wl_surface * surface;
463
464 VkExtent2D extent;
465 VkFormat vk_format;
466 uint32_t drm_format;
467
468 VkPresentModeKHR present_mode;
469 bool fifo_ready;
470
471 uint32_t image_count;
472 struct wsi_wl_image images[0];
473 };
474
475 static VkResult
476 wsi_wl_swapchain_get_images(struct anv_swapchain *anv_chain,
477 uint32_t *pCount, VkImage *pSwapchainImages)
478 {
479 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)anv_chain;
480
481 if (pSwapchainImages == NULL) {
482 *pCount = chain->image_count;
483 return VK_SUCCESS;
484 }
485
486 assert(chain->image_count <= *pCount);
487 for (uint32_t i = 0; i < chain->image_count; i++)
488 pSwapchainImages[i] = anv_image_to_handle(chain->images[i].image);
489
490 *pCount = chain->image_count;
491
492 return VK_SUCCESS;
493 }
494
495 static VkResult
496 wsi_wl_swapchain_acquire_next_image(struct anv_swapchain *anv_chain,
497 uint64_t timeout,
498 VkSemaphore semaphore,
499 uint32_t *image_index)
500 {
501 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)anv_chain;
502
503 int ret = wl_display_dispatch_queue_pending(chain->display->display,
504 chain->queue);
505 /* XXX: I'm not sure if out-of-date is the right error here. If
506 * wl_display_dispatch_queue_pending fails it most likely means we got
507 * kicked by the server so this seems more-or-less correct.
508 */
509 if (ret < 0)
510 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
511
512 while (1) {
513 for (uint32_t i = 0; i < chain->image_count; i++) {
514 if (!chain->images[i].busy) {
515 /* We found a non-busy image */
516 *image_index = i;
517 return VK_SUCCESS;
518 }
519 }
520
521 /* This time we do a blocking dispatch because we can't go
522 * anywhere until we get an event.
523 */
524 int ret = wl_display_roundtrip_queue(chain->display->display,
525 chain->queue);
526 if (ret < 0)
527 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
528 }
529 }
530
531 static void
532 frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
533 {
534 struct wsi_wl_swapchain *chain = data;
535
536 chain->fifo_ready = true;
537
538 wl_callback_destroy(callback);
539 }
540
541 static const struct wl_callback_listener frame_listener = {
542 frame_handle_done,
543 };
544
545 static VkResult
546 wsi_wl_swapchain_queue_present(struct anv_swapchain *anv_chain,
547 struct anv_queue *queue,
548 uint32_t image_index)
549 {
550 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)anv_chain;
551
552 if (chain->present_mode == VK_PRESENT_MODE_FIFO_KHR) {
553 while (!chain->fifo_ready) {
554 int ret = wl_display_dispatch_queue(chain->display->display,
555 chain->queue);
556 if (ret < 0)
557 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
558 }
559 }
560
561 assert(image_index < chain->image_count);
562 wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
563 wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
564
565 if (chain->present_mode == VK_PRESENT_MODE_FIFO_KHR) {
566 struct wl_callback *frame = wl_surface_frame(chain->surface);
567 wl_proxy_set_queue((struct wl_proxy *)frame, chain->queue);
568 wl_callback_add_listener(frame, &frame_listener, chain);
569 chain->fifo_ready = false;
570 }
571
572 chain->images[image_index].busy = true;
573 wl_surface_commit(chain->surface);
574 wl_display_flush(chain->display->display);
575
576 return VK_SUCCESS;
577 }
578
579 static void
580 wsi_wl_image_finish(struct wsi_wl_swapchain *chain, struct wsi_wl_image *image,
581 const VkAllocationCallbacks* pAllocator)
582 {
583 VkDevice vk_device = anv_device_to_handle(chain->base.device);
584 anv_FreeMemory(vk_device, anv_device_memory_to_handle(image->memory),
585 pAllocator);
586 anv_DestroyImage(vk_device, anv_image_to_handle(image->image),
587 pAllocator);
588 }
589
590 static void
591 buffer_handle_release(void *data, struct wl_buffer *buffer)
592 {
593 struct wsi_wl_image *image = data;
594
595 assert(image->buffer == buffer);
596
597 image->busy = false;
598 }
599
600 static const struct wl_buffer_listener buffer_listener = {
601 buffer_handle_release,
602 };
603
604 static VkResult
605 wsi_wl_image_init(struct wsi_wl_swapchain *chain, struct wsi_wl_image *image,
606 const VkAllocationCallbacks* pAllocator)
607 {
608 VkDevice vk_device = anv_device_to_handle(chain->base.device);
609 VkResult result;
610
611 VkImage vk_image;
612 result = anv_image_create(vk_device,
613 &(struct anv_image_create_info) {
614 .isl_tiling_flags = ISL_TILING_X_BIT,
615 .stride = 0,
616 .vk_info =
617 &(VkImageCreateInfo) {
618 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
619 .imageType = VK_IMAGE_TYPE_2D,
620 .format = chain->vk_format,
621 .extent = {
622 .width = chain->extent.width,
623 .height = chain->extent.height,
624 .depth = 1
625 },
626 .mipLevels = 1,
627 .arrayLayers = 1,
628 .samples = 1,
629 /* FIXME: Need a way to use X tiling to allow scanout */
630 .tiling = VK_IMAGE_TILING_OPTIMAL,
631 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
632 .flags = 0,
633 }},
634 pAllocator,
635 &vk_image);
636
637 if (result != VK_SUCCESS)
638 return result;
639
640 image->image = anv_image_from_handle(vk_image);
641 assert(anv_format_is_color(image->image->format));
642
643 struct anv_surface *surface = &image->image->color_surface;
644
645 VkDeviceMemory vk_memory;
646 result = anv_AllocateMemory(vk_device,
647 &(VkMemoryAllocateInfo) {
648 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
649 .allocationSize = image->image->size,
650 .memoryTypeIndex = 0,
651 },
652 pAllocator,
653 &vk_memory);
654
655 if (result != VK_SUCCESS)
656 goto fail_image;
657
658 image->memory = anv_device_memory_from_handle(vk_memory);
659 image->memory->bo.is_winsys_bo = true;
660
661 result = anv_BindImageMemory(vk_device, vk_image, vk_memory, 0);
662
663 if (result != VK_SUCCESS)
664 goto fail_mem;
665
666 int ret = anv_gem_set_tiling(chain->base.device,
667 image->memory->bo.gem_handle,
668 surface->isl.row_pitch, I915_TILING_X);
669 if (ret) {
670 /* FINISHME: Choose a better error. */
671 result = vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
672 goto fail_mem;
673 }
674
675 int fd = anv_gem_handle_to_fd(chain->base.device,
676 image->memory->bo.gem_handle);
677 if (fd == -1) {
678 /* FINISHME: Choose a better error. */
679 result = vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
680 goto fail_mem;
681 }
682
683 image->buffer = wl_drm_create_prime_buffer(chain->display->drm,
684 fd, /* name */
685 chain->extent.width,
686 chain->extent.height,
687 chain->drm_format,
688 surface->offset,
689 surface->isl.row_pitch,
690 0, 0, 0, 0 /* unused */);
691 wl_display_roundtrip(chain->display->display);
692 close(fd);
693
694 wl_proxy_set_queue((struct wl_proxy *)image->buffer, chain->queue);
695 wl_buffer_add_listener(image->buffer, &buffer_listener, image);
696
697 return VK_SUCCESS;
698
699 fail_mem:
700 anv_FreeMemory(vk_device, vk_memory, pAllocator);
701 fail_image:
702 anv_DestroyImage(vk_device, vk_image, pAllocator);
703
704 return result;
705 }
706
707 static VkResult
708 wsi_wl_swapchain_destroy(struct anv_swapchain *anv_chain,
709 const VkAllocationCallbacks *pAllocator)
710 {
711 struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)anv_chain;
712
713 for (uint32_t i = 0; i < chain->image_count; i++) {
714 if (chain->images[i].buffer)
715 wsi_wl_image_finish(chain, &chain->images[i], pAllocator);
716 }
717
718 anv_free2(&chain->base.device->alloc, pAllocator, chain);
719
720 return VK_SUCCESS;
721 }
722
723 static VkResult
724 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
725 struct anv_device *device,
726 const VkSwapchainCreateInfoKHR* pCreateInfo,
727 const VkAllocationCallbacks* pAllocator,
728 struct anv_swapchain **swapchain_out)
729 {
730 VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
731 struct wsi_wl_swapchain *chain;
732 VkResult result;
733
734 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
735
736 int num_images = pCreateInfo->minImageCount;
737
738 assert(num_images >= MIN_NUM_IMAGES);
739
740 /* For true mailbox mode, we need at least 4 images:
741 * 1) One to scan out from
742 * 2) One to have queued for scan-out
743 * 3) One to be currently held by the Wayland compositor
744 * 4) One to render to
745 */
746 if (pCreateInfo->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
747 num_images = MAX2(num_images, 4);
748
749 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
750 chain = anv_alloc2(&device->alloc, pAllocator, size, 8,
751 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
752 if (chain == NULL)
753 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
754
755 chain->base.device = device;
756 chain->base.destroy = wsi_wl_swapchain_destroy;
757 chain->base.get_images = wsi_wl_swapchain_get_images;
758 chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
759 chain->base.queue_present = wsi_wl_swapchain_queue_present;
760
761 chain->surface = surface->surface;
762 chain->extent = pCreateInfo->imageExtent;
763 chain->vk_format = pCreateInfo->imageFormat;
764 chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, false);
765
766 chain->present_mode = pCreateInfo->presentMode;
767 chain->fifo_ready = true;
768
769 chain->image_count = num_images;
770
771 /* Mark a bunch of stuff as NULL. This way we can just call
772 * destroy_swapchain for cleanup.
773 */
774 for (uint32_t i = 0; i < chain->image_count; i++)
775 chain->images[i].buffer = NULL;
776 chain->queue = NULL;
777
778 chain->display = wsi_wl_get_display(device->instance, surface->display);
779 if (!chain->display)
780 goto fail;
781
782 chain->queue = wl_display_create_queue(chain->display->display);
783 if (!chain->queue)
784 goto fail;
785
786 for (uint32_t i = 0; i < chain->image_count; i++) {
787 result = wsi_wl_image_init(chain, &chain->images[i], pAllocator);
788 if (result != VK_SUCCESS)
789 goto fail;
790 chain->images[i].busy = false;
791 }
792
793 *swapchain_out = &chain->base;
794
795 return VK_SUCCESS;
796
797 fail:
798 wsi_wl_swapchain_destroy(&chain->base, pAllocator);
799
800 return result;
801 }
802
803 VkResult
804 anv_wl_init_wsi(struct anv_instance *instance)
805 {
806 struct wsi_wayland *wsi;
807 VkResult result;
808
809 wsi = anv_alloc(&instance->alloc, sizeof(*wsi), 8,
810 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
811 if (!wsi) {
812 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
813 goto fail;
814 }
815
816 wsi->instance = instance;
817
818 int ret = pthread_mutex_init(&wsi->mutex, NULL);
819 if (ret != 0) {
820 if (ret == ENOMEM) {
821 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
822 } else {
823 /* FINISHME: Choose a better error. */
824 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
825 }
826
827 goto fail_alloc;
828 }
829
830 wsi->displays = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
831 _mesa_key_pointer_equal);
832 if (!wsi->displays) {
833 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
834 goto fail_mutex;
835 }
836
837 wsi->base.get_support = wsi_wl_surface_get_support;
838 wsi->base.get_capabilities = wsi_wl_surface_get_capabilities;
839 wsi->base.get_formats = wsi_wl_surface_get_formats;
840 wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
841 wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
842
843 instance->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
844
845 return VK_SUCCESS;
846
847 fail_mutex:
848 pthread_mutex_destroy(&wsi->mutex);
849
850 fail_alloc:
851 anv_free(&instance->alloc, wsi);
852 fail:
853 instance->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
854
855 return result;
856 }
857
858 void
859 anv_wl_finish_wsi(struct anv_instance *instance)
860 {
861 struct wsi_wayland *wsi =
862 (struct wsi_wayland *)instance->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
863
864 if (wsi) {
865 _mesa_hash_table_destroy(wsi->displays, NULL);
866
867 pthread_mutex_destroy(&wsi->mutex);
868
869 anv_free(&instance->alloc, wsi);
870 }
871 }