anv: move to using shared wsi code
[mesa.git] / src / vulkan / wsi / wsi_common_x11.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 <X11/Xlib-xcb.h>
25 #include <X11/xshmfence.h>
26 #include <xcb/xcb.h>
27 #include <xcb/dri3.h>
28 #include <xcb/present.h>
29
30 #include "util/macros.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <string.h>
36
37 #include "util/hash_table.h"
38
39 #include "wsi_common.h"
40 #include "wsi_common_x11.h"
41
42 #define typed_memcpy(dest, src, count) ({ \
43 static_assert(sizeof(*src) == sizeof(*dest), ""); \
44 memcpy((dest), (src), (count) * sizeof(*(src))); \
45 })
46
47 struct wsi_x11_connection {
48 bool has_dri3;
49 bool has_present;
50 };
51
52 struct wsi_x11 {
53 struct wsi_interface base;
54
55 pthread_mutex_t mutex;
56 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
57 struct hash_table *connections;
58 };
59
60 static struct wsi_x11_connection *
61 wsi_x11_connection_create(const VkAllocationCallbacks *alloc,
62 xcb_connection_t *conn)
63 {
64 xcb_query_extension_cookie_t dri3_cookie, pres_cookie;
65 xcb_query_extension_reply_t *dri3_reply, *pres_reply;
66
67 struct wsi_x11_connection *wsi_conn =
68 vk_alloc(alloc, sizeof(*wsi_conn), 8,
69 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
70 if (!wsi_conn)
71 return NULL;
72
73 dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
74 pres_cookie = xcb_query_extension(conn, 7, "PRESENT");
75
76 dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
77 pres_reply = xcb_query_extension_reply(conn, pres_cookie, NULL);
78 if (dri3_reply == NULL || pres_reply == NULL) {
79 free(dri3_reply);
80 free(pres_reply);
81 vk_free(alloc, wsi_conn);
82 return NULL;
83 }
84
85 wsi_conn->has_dri3 = dri3_reply->present != 0;
86 wsi_conn->has_present = pres_reply->present != 0;
87
88 free(dri3_reply);
89 free(pres_reply);
90
91 return wsi_conn;
92 }
93
94 static void
95 wsi_x11_connection_destroy(const VkAllocationCallbacks *alloc,
96 struct wsi_x11_connection *conn)
97 {
98 vk_free(alloc, conn);
99 }
100
101 static struct wsi_x11_connection *
102 wsi_x11_get_connection(struct wsi_device *wsi_dev,
103 const VkAllocationCallbacks *alloc,
104 xcb_connection_t *conn)
105 {
106 struct wsi_x11 *wsi =
107 (struct wsi_x11 *)wsi_dev->wsi[VK_ICD_WSI_PLATFORM_XCB];
108
109 pthread_mutex_lock(&wsi->mutex);
110
111 struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
112 if (!entry) {
113 /* We're about to make a bunch of blocking calls. Let's drop the
114 * mutex for now so we don't block up too badly.
115 */
116 pthread_mutex_unlock(&wsi->mutex);
117
118 struct wsi_x11_connection *wsi_conn =
119 wsi_x11_connection_create(alloc, conn);
120
121 pthread_mutex_lock(&wsi->mutex);
122
123 entry = _mesa_hash_table_search(wsi->connections, conn);
124 if (entry) {
125 /* Oops, someone raced us to it */
126 wsi_x11_connection_destroy(alloc, wsi_conn);
127 } else {
128 entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
129 }
130 }
131
132 pthread_mutex_unlock(&wsi->mutex);
133
134 return entry->data;
135 }
136
137 static const VkSurfaceFormatKHR formats[] = {
138 { .format = VK_FORMAT_B8G8R8A8_SRGB, },
139 { .format = VK_FORMAT_B8G8R8A8_UNORM, },
140 };
141
142 static const VkPresentModeKHR present_modes[] = {
143 VK_PRESENT_MODE_MAILBOX_KHR,
144 };
145
146 static xcb_screen_t *
147 get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
148 {
149 xcb_screen_iterator_t screen_iter =
150 xcb_setup_roots_iterator(xcb_get_setup(conn));
151
152 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
153 if (screen_iter.data->root == root)
154 return screen_iter.data;
155 }
156
157 return NULL;
158 }
159
160 static xcb_visualtype_t *
161 screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
162 unsigned *depth)
163 {
164 xcb_depth_iterator_t depth_iter =
165 xcb_screen_allowed_depths_iterator(screen);
166
167 for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
168 xcb_visualtype_iterator_t visual_iter =
169 xcb_depth_visuals_iterator (depth_iter.data);
170
171 for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
172 if (visual_iter.data->visual_id == visual_id) {
173 if (depth)
174 *depth = depth_iter.data->depth;
175 return visual_iter.data;
176 }
177 }
178 }
179
180 return NULL;
181 }
182
183 static xcb_visualtype_t *
184 connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id,
185 unsigned *depth)
186 {
187 xcb_screen_iterator_t screen_iter =
188 xcb_setup_roots_iterator(xcb_get_setup(conn));
189
190 /* For this we have to iterate over all of the screens which is rather
191 * annoying. Fortunately, there is probably only 1.
192 */
193 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
194 xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
195 visual_id, depth);
196 if (visual)
197 return visual;
198 }
199
200 return NULL;
201 }
202
203 static xcb_visualtype_t *
204 get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
205 unsigned *depth)
206 {
207 xcb_query_tree_cookie_t tree_cookie;
208 xcb_get_window_attributes_cookie_t attrib_cookie;
209 xcb_query_tree_reply_t *tree;
210 xcb_get_window_attributes_reply_t *attrib;
211
212 tree_cookie = xcb_query_tree(conn, window);
213 attrib_cookie = xcb_get_window_attributes(conn, window);
214
215 tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
216 attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
217 if (attrib == NULL || tree == NULL) {
218 free(attrib);
219 free(tree);
220 return NULL;
221 }
222
223 xcb_window_t root = tree->root;
224 xcb_visualid_t visual_id = attrib->visual;
225 free(attrib);
226 free(tree);
227
228 xcb_screen_t *screen = get_screen_for_root(conn, root);
229 if (screen == NULL)
230 return NULL;
231
232 return screen_get_visualtype(screen, visual_id, depth);
233 }
234
235 static bool
236 visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
237 {
238 uint32_t rgb_mask = visual->red_mask |
239 visual->green_mask |
240 visual->blue_mask;
241
242 uint32_t all_mask = 0xffffffff >> (32 - depth);
243
244 /* Do we have bits left over after RGB? */
245 return (all_mask & ~rgb_mask) != 0;
246 }
247
248 VkBool32 wsi_get_physical_device_xcb_presentation_support(
249 struct wsi_device *wsi_device,
250 VkAllocationCallbacks *alloc,
251 uint32_t queueFamilyIndex,
252 xcb_connection_t* connection,
253 xcb_visualid_t visual_id)
254 {
255 struct wsi_x11_connection *wsi_conn =
256 wsi_x11_get_connection(wsi_device, alloc, connection);
257
258 if (!wsi_conn->has_dri3) {
259 fprintf(stderr, "vulkan: No DRI3 support\n");
260 return false;
261 }
262
263 unsigned visual_depth;
264 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
265 return false;
266
267 if (visual_depth != 24 && visual_depth != 32)
268 return false;
269
270 return true;
271 }
272
273 static xcb_connection_t*
274 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
275 {
276 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
277 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
278 else
279 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
280 }
281
282 static xcb_window_t
283 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
284 {
285 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
286 return ((VkIcdSurfaceXlib *)icd_surface)->window;
287 else
288 return ((VkIcdSurfaceXcb *)icd_surface)->window;
289 }
290
291 static VkResult
292 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
293 struct wsi_device *wsi_device,
294 const VkAllocationCallbacks *alloc,
295 uint32_t queueFamilyIndex,
296 VkBool32* pSupported)
297 {
298 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
299 xcb_window_t window = x11_surface_get_window(icd_surface);
300
301 struct wsi_x11_connection *wsi_conn =
302 wsi_x11_get_connection(wsi_device, alloc, conn);
303 if (!wsi_conn)
304 return VK_ERROR_OUT_OF_HOST_MEMORY;
305
306 if (!wsi_conn->has_dri3) {
307 fprintf(stderr, "vulkan: No DRI3 support\n");
308 *pSupported = false;
309 return VK_SUCCESS;
310 }
311
312 unsigned visual_depth;
313 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
314 *pSupported = false;
315 return VK_SUCCESS;
316 }
317
318 if (visual_depth != 24 && visual_depth != 32) {
319 *pSupported = false;
320 return VK_SUCCESS;
321 }
322
323 *pSupported = true;
324 return VK_SUCCESS;
325 }
326
327 static VkResult
328 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
329 VkSurfaceCapabilitiesKHR *caps)
330 {
331 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
332 xcb_window_t window = x11_surface_get_window(icd_surface);
333 xcb_get_geometry_cookie_t geom_cookie;
334 xcb_generic_error_t *err;
335 xcb_get_geometry_reply_t *geom;
336 unsigned visual_depth;
337
338 geom_cookie = xcb_get_geometry(conn, window);
339
340 /* This does a round-trip. This is why we do get_geometry first and
341 * wait to read the reply until after we have a visual.
342 */
343 xcb_visualtype_t *visual =
344 get_visualtype_for_window(conn, window, &visual_depth);
345
346 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
347 if (geom) {
348 VkExtent2D extent = { geom->width, geom->height };
349 caps->currentExtent = extent;
350 caps->minImageExtent = extent;
351 caps->maxImageExtent = extent;
352 } else {
353 /* This can happen if the client didn't wait for the configure event
354 * to come back from the compositor. In that case, we don't know the
355 * size of the window so we just return valid "I don't know" stuff.
356 */
357 caps->currentExtent = (VkExtent2D) { -1, -1 };
358 caps->minImageExtent = (VkExtent2D) { 1, 1 };
359 caps->maxImageExtent = (VkExtent2D) { INT16_MAX, INT16_MAX };
360 }
361 free(err);
362 free(geom);
363
364 if (visual_has_alpha(visual, visual_depth)) {
365 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
366 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
367 } else {
368 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
369 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
370 }
371
372 caps->minImageCount = 2;
373 caps->maxImageCount = 4;
374 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
375 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
376 caps->maxImageArrayLayers = 1;
377 caps->supportedUsageFlags =
378 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
379 VK_IMAGE_USAGE_SAMPLED_BIT |
380 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
381 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
382
383 return VK_SUCCESS;
384 }
385
386 static VkResult
387 x11_surface_get_formats(VkIcdSurfaceBase *surface,
388 struct wsi_device *wsi_device,
389 uint32_t *pSurfaceFormatCount,
390 VkSurfaceFormatKHR *pSurfaceFormats)
391 {
392 if (pSurfaceFormats == NULL) {
393 *pSurfaceFormatCount = ARRAY_SIZE(formats);
394 return VK_SUCCESS;
395 }
396
397 assert(*pSurfaceFormatCount >= ARRAY_SIZE(formats));
398 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
399 *pSurfaceFormatCount = ARRAY_SIZE(formats);
400
401 return VK_SUCCESS;
402 }
403
404 static VkResult
405 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
406 uint32_t *pPresentModeCount,
407 VkPresentModeKHR *pPresentModes)
408 {
409 if (pPresentModes == NULL) {
410 *pPresentModeCount = ARRAY_SIZE(present_modes);
411 return VK_SUCCESS;
412 }
413
414 assert(*pPresentModeCount >= ARRAY_SIZE(present_modes));
415 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
416 *pPresentModeCount = ARRAY_SIZE(present_modes);
417
418 return VK_SUCCESS;
419 }
420
421 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
422 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
423 VkSurfaceKHR *pSurface)
424 {
425 VkIcdSurfaceXcb *surface;
426
427 surface = vk_alloc(pAllocator, sizeof *surface, 8,
428 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
429 if (surface == NULL)
430 return VK_ERROR_OUT_OF_HOST_MEMORY;
431
432 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
433 surface->connection = pCreateInfo->connection;
434 surface->window = pCreateInfo->window;
435
436 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
437 return VK_SUCCESS;
438 }
439
440 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
441 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
442 VkSurfaceKHR *pSurface)
443 {
444 VkIcdSurfaceXlib *surface;
445
446 surface = vk_alloc(pAllocator, sizeof *surface, 8,
447 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
448 if (surface == NULL)
449 return VK_ERROR_OUT_OF_HOST_MEMORY;
450
451 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
452 surface->dpy = pCreateInfo->dpy;
453 surface->window = pCreateInfo->window;
454
455 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
456 return VK_SUCCESS;
457 }
458
459 struct x11_image {
460 VkImage image;
461 VkDeviceMemory memory;
462 xcb_pixmap_t pixmap;
463 bool busy;
464 struct xshmfence * shm_fence;
465 uint32_t sync_fence;
466 };
467
468 struct x11_swapchain {
469 struct wsi_swapchain base;
470
471 xcb_connection_t * conn;
472 xcb_window_t window;
473 xcb_gc_t gc;
474 VkExtent2D extent;
475 uint32_t image_count;
476
477 xcb_present_event_t event_id;
478 xcb_special_event_t * special_event;
479 uint64_t send_sbc;
480 uint32_t stamp;
481
482 struct x11_image images[0];
483 };
484
485 static VkResult
486 x11_get_images(struct wsi_swapchain *anv_chain,
487 uint32_t* pCount, VkImage *pSwapchainImages)
488 {
489 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
490
491 if (pSwapchainImages == NULL) {
492 *pCount = chain->image_count;
493 return VK_SUCCESS;
494 }
495
496 assert(chain->image_count <= *pCount);
497 for (uint32_t i = 0; i < chain->image_count; i++)
498 pSwapchainImages[i] = chain->images[i].image;
499
500 *pCount = chain->image_count;
501
502 return VK_SUCCESS;
503 }
504
505 static VkResult
506 x11_handle_dri3_present_event(struct x11_swapchain *chain,
507 xcb_present_generic_event_t *event)
508 {
509 switch (event->evtype) {
510 case XCB_PRESENT_CONFIGURE_NOTIFY: {
511 xcb_present_configure_notify_event_t *config = (void *) event;
512
513 if (config->width != chain->extent.width ||
514 config->height != chain->extent.height)
515 return VK_ERROR_OUT_OF_DATE_KHR;
516
517 break;
518 }
519
520 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
521 xcb_present_idle_notify_event_t *idle = (void *) event;
522
523 for (unsigned i = 0; i < chain->image_count; i++) {
524 if (chain->images[i].pixmap == idle->pixmap) {
525 chain->images[i].busy = false;
526 break;
527 }
528 }
529
530 break;
531 }
532
533 case XCB_PRESENT_COMPLETE_NOTIFY:
534 default:
535 break;
536 }
537
538 return VK_SUCCESS;
539 }
540
541 static VkResult
542 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
543 uint64_t timeout,
544 VkSemaphore semaphore,
545 uint32_t *image_index)
546 {
547 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
548
549 while (1) {
550 for (uint32_t i = 0; i < chain->image_count; i++) {
551 if (!chain->images[i].busy) {
552 /* We found a non-busy image */
553 xshmfence_await(chain->images[i].shm_fence);
554 *image_index = i;
555 chain->images[i].busy = true;
556 return VK_SUCCESS;
557 }
558 }
559
560 xcb_flush(chain->conn);
561 xcb_generic_event_t *event =
562 xcb_wait_for_special_event(chain->conn, chain->special_event);
563 if (!event)
564 return VK_ERROR_OUT_OF_DATE_KHR;
565
566 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
567 free(event);
568 if (result != VK_SUCCESS)
569 return result;
570 }
571 }
572
573 static VkResult
574 x11_queue_present(struct wsi_swapchain *anv_chain,
575 uint32_t image_index)
576 {
577 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
578 struct x11_image *image = &chain->images[image_index];
579
580 assert(image_index < chain->image_count);
581
582 uint32_t options = XCB_PRESENT_OPTION_NONE;
583
584 int64_t target_msc = 0;
585 int64_t divisor = 0;
586 int64_t remainder = 0;
587
588 options |= XCB_PRESENT_OPTION_ASYNC;
589
590 xshmfence_reset(image->shm_fence);
591
592 ++chain->send_sbc;
593 xcb_void_cookie_t cookie =
594 xcb_present_pixmap(chain->conn,
595 chain->window,
596 image->pixmap,
597 (uint32_t) chain->send_sbc,
598 0, /* valid */
599 0, /* update */
600 0, /* x_off */
601 0, /* y_off */
602 XCB_NONE, /* target_crtc */
603 XCB_NONE,
604 image->sync_fence,
605 options,
606 target_msc,
607 divisor,
608 remainder, 0, NULL);
609 xcb_discard_reply(chain->conn, cookie.sequence);
610 image->busy = true;
611
612 xcb_flush(chain->conn);
613
614 return VK_SUCCESS;
615 }
616
617 static VkResult
618 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
619 const VkSwapchainCreateInfoKHR *pCreateInfo,
620 const VkAllocationCallbacks* pAllocator,
621 struct x11_image *image)
622 {
623 xcb_void_cookie_t cookie;
624 VkResult result;
625 uint32_t row_pitch;
626 uint32_t offset;
627 uint32_t bpp = 32;
628 uint32_t depth = 24;
629 int fd;
630 uint32_t size;
631
632 result = chain->base.image_fns->create_wsi_image(device_h,
633 pCreateInfo,
634 pAllocator,
635 &image->image,
636 &image->memory,
637 &size,
638 &offset,
639 &row_pitch,
640 &fd);
641 if (result != VK_SUCCESS)
642 return result;
643
644 image->pixmap = xcb_generate_id(chain->conn);
645
646 cookie =
647 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
648 image->pixmap,
649 chain->window,
650 size,
651 pCreateInfo->imageExtent.width,
652 pCreateInfo->imageExtent.height,
653 row_pitch,
654 depth, bpp, fd);
655 xcb_discard_reply(chain->conn, cookie.sequence);
656
657 int fence_fd = xshmfence_alloc_shm();
658 if (fence_fd < 0)
659 goto fail_pixmap;
660
661 image->shm_fence = xshmfence_map_shm(fence_fd);
662 if (image->shm_fence == NULL)
663 goto fail_shmfence_alloc;
664
665 image->sync_fence = xcb_generate_id(chain->conn);
666 xcb_dri3_fence_from_fd(chain->conn,
667 image->pixmap,
668 image->sync_fence,
669 false,
670 fence_fd);
671
672 image->busy = false;
673 xshmfence_trigger(image->shm_fence);
674
675 return VK_SUCCESS;
676
677 fail_shmfence_alloc:
678 close(fence_fd);
679
680 fail_pixmap:
681 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
682 xcb_discard_reply(chain->conn, cookie.sequence);
683
684 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
685 image->image, image->memory);
686
687 return result;
688 }
689
690 static void
691 x11_image_finish(struct x11_swapchain *chain,
692 const VkAllocationCallbacks* pAllocator,
693 struct x11_image *image)
694 {
695 xcb_void_cookie_t cookie;
696
697 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
698 xcb_discard_reply(chain->conn, cookie.sequence);
699 xshmfence_unmap_shm(image->shm_fence);
700
701 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
702 xcb_discard_reply(chain->conn, cookie.sequence);
703
704 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
705 image->image, image->memory);
706 }
707
708 static VkResult
709 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
710 const VkAllocationCallbacks *pAllocator)
711 {
712 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
713 for (uint32_t i = 0; i < chain->image_count; i++)
714 x11_image_finish(chain, pAllocator, &chain->images[i]);
715
716 xcb_unregister_for_special_event(chain->conn, chain->special_event);
717
718 vk_free(pAllocator, chain);
719
720 return VK_SUCCESS;
721 }
722
723 static VkResult
724 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
725 VkDevice device,
726 struct wsi_device *wsi_device,
727 const VkSwapchainCreateInfoKHR *pCreateInfo,
728 const VkAllocationCallbacks* pAllocator,
729 const struct wsi_image_fns *image_fns,
730 struct wsi_swapchain **swapchain_out)
731 {
732 struct x11_swapchain *chain;
733 xcb_void_cookie_t cookie;
734 VkResult result;
735
736 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
737
738 int num_images = pCreateInfo->minImageCount;
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 = vk_alloc(pAllocator, size, 8,
751 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
752 if (chain == NULL)
753 return VK_ERROR_OUT_OF_HOST_MEMORY;
754
755 chain->base.device = device;
756 chain->base.destroy = x11_swapchain_destroy;
757 chain->base.get_images = x11_get_images;
758 chain->base.acquire_next_image = x11_acquire_next_image;
759 chain->base.queue_present = x11_queue_present;
760 chain->base.image_fns = image_fns;
761 chain->conn = x11_surface_get_connection(icd_surface);
762 chain->window = x11_surface_get_window(icd_surface);
763 chain->extent = pCreateInfo->imageExtent;
764 chain->image_count = num_images;
765 chain->send_sbc = 0;
766
767 chain->event_id = xcb_generate_id(chain->conn);
768 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
769 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
770 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
771 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
772
773 /* Create an XCB event queue to hold present events outside of the usual
774 * application event queue
775 */
776 chain->special_event =
777 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
778 chain->event_id, NULL);
779
780 chain->gc = xcb_generate_id(chain->conn);
781 if (!chain->gc) {
782 /* FINISHME: Choose a better error. */
783 result = VK_ERROR_OUT_OF_HOST_MEMORY;
784 goto fail_register;
785 }
786
787 cookie = xcb_create_gc(chain->conn,
788 chain->gc,
789 chain->window,
790 XCB_GC_GRAPHICS_EXPOSURES,
791 (uint32_t []) { 0 });
792 xcb_discard_reply(chain->conn, cookie.sequence);
793
794 uint32_t image = 0;
795 for (; image < chain->image_count; image++) {
796 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
797 &chain->images[image]);
798 if (result != VK_SUCCESS)
799 goto fail_init_images;
800 }
801
802 *swapchain_out = &chain->base;
803
804 return VK_SUCCESS;
805
806 fail_init_images:
807 for (uint32_t j = 0; j < image; j++)
808 x11_image_finish(chain, pAllocator, &chain->images[j]);
809
810 fail_register:
811 xcb_unregister_for_special_event(chain->conn, chain->special_event);
812
813 vk_free(pAllocator, chain);
814
815 return result;
816 }
817
818 VkResult
819 wsi_x11_init_wsi(struct wsi_device *wsi_device,
820 const VkAllocationCallbacks *alloc)
821 {
822 struct wsi_x11 *wsi;
823 VkResult result;
824
825 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
826 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
827 if (!wsi) {
828 result = VK_ERROR_OUT_OF_HOST_MEMORY;
829 goto fail;
830 }
831
832 int ret = pthread_mutex_init(&wsi->mutex, NULL);
833 if (ret != 0) {
834 if (ret == ENOMEM) {
835 result = VK_ERROR_OUT_OF_HOST_MEMORY;
836 } else {
837 /* FINISHME: Choose a better error. */
838 result = VK_ERROR_OUT_OF_HOST_MEMORY;
839 }
840
841 goto fail_alloc;
842 }
843
844 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
845 _mesa_key_pointer_equal);
846 if (!wsi->connections) {
847 result = VK_ERROR_OUT_OF_HOST_MEMORY;
848 goto fail_mutex;
849 }
850
851 wsi->base.get_support = x11_surface_get_support;
852 wsi->base.get_capabilities = x11_surface_get_capabilities;
853 wsi->base.get_formats = x11_surface_get_formats;
854 wsi->base.get_present_modes = x11_surface_get_present_modes;
855 wsi->base.create_swapchain = x11_surface_create_swapchain;
856
857 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
858 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
859
860 return VK_SUCCESS;
861
862 fail_mutex:
863 pthread_mutex_destroy(&wsi->mutex);
864 fail_alloc:
865 vk_free(alloc, wsi);
866 fail:
867 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
868 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
869
870 return result;
871 }
872
873 void
874 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
875 const VkAllocationCallbacks *alloc)
876 {
877 struct wsi_x11 *wsi =
878 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
879
880 if (wsi) {
881 _mesa_hash_table_destroy(wsi->connections, NULL);
882
883 pthread_mutex_destroy(&wsi->mutex);
884
885 vk_free(alloc, wsi);
886 }
887 }