548352eec24be3a72ac07f38023e2c4c7e6f1639
[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 VkResult result = VK_SUCCESS;
398
399 if (*pSurfaceFormatCount > ARRAY_SIZE(formats))
400 *pSurfaceFormatCount = ARRAY_SIZE(formats);
401 else if (*pSurfaceFormatCount < ARRAY_SIZE(formats))
402 result = VK_INCOMPLETE;
403
404 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
405
406 return result;
407 }
408
409 static VkResult
410 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
411 uint32_t *pPresentModeCount,
412 VkPresentModeKHR *pPresentModes)
413 {
414 if (pPresentModes == NULL) {
415 *pPresentModeCount = ARRAY_SIZE(present_modes);
416 return VK_SUCCESS;
417 }
418
419 assert(*pPresentModeCount >= ARRAY_SIZE(present_modes));
420 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
421 *pPresentModeCount = ARRAY_SIZE(present_modes);
422
423 return VK_SUCCESS;
424 }
425
426 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
427 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
428 VkSurfaceKHR *pSurface)
429 {
430 VkIcdSurfaceXcb *surface;
431
432 surface = vk_alloc(pAllocator, sizeof *surface, 8,
433 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
434 if (surface == NULL)
435 return VK_ERROR_OUT_OF_HOST_MEMORY;
436
437 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
438 surface->connection = pCreateInfo->connection;
439 surface->window = pCreateInfo->window;
440
441 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
442 return VK_SUCCESS;
443 }
444
445 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
446 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
447 VkSurfaceKHR *pSurface)
448 {
449 VkIcdSurfaceXlib *surface;
450
451 surface = vk_alloc(pAllocator, sizeof *surface, 8,
452 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
453 if (surface == NULL)
454 return VK_ERROR_OUT_OF_HOST_MEMORY;
455
456 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
457 surface->dpy = pCreateInfo->dpy;
458 surface->window = pCreateInfo->window;
459
460 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
461 return VK_SUCCESS;
462 }
463
464 struct x11_image {
465 VkImage image;
466 VkDeviceMemory memory;
467 xcb_pixmap_t pixmap;
468 bool busy;
469 struct xshmfence * shm_fence;
470 uint32_t sync_fence;
471 };
472
473 struct x11_swapchain {
474 struct wsi_swapchain base;
475
476 xcb_connection_t * conn;
477 xcb_window_t window;
478 xcb_gc_t gc;
479 VkExtent2D extent;
480 uint32_t image_count;
481
482 xcb_present_event_t event_id;
483 xcb_special_event_t * special_event;
484 uint64_t send_sbc;
485 uint32_t stamp;
486
487 struct x11_image images[0];
488 };
489
490 static VkResult
491 x11_get_images(struct wsi_swapchain *anv_chain,
492 uint32_t* pCount, VkImage *pSwapchainImages)
493 {
494 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
495
496 if (pSwapchainImages == NULL) {
497 *pCount = chain->image_count;
498 return VK_SUCCESS;
499 }
500
501 assert(chain->image_count <= *pCount);
502 for (uint32_t i = 0; i < chain->image_count; i++)
503 pSwapchainImages[i] = chain->images[i].image;
504
505 *pCount = chain->image_count;
506
507 return VK_SUCCESS;
508 }
509
510 static VkResult
511 x11_handle_dri3_present_event(struct x11_swapchain *chain,
512 xcb_present_generic_event_t *event)
513 {
514 switch (event->evtype) {
515 case XCB_PRESENT_CONFIGURE_NOTIFY: {
516 xcb_present_configure_notify_event_t *config = (void *) event;
517
518 if (config->width != chain->extent.width ||
519 config->height != chain->extent.height)
520 return VK_ERROR_OUT_OF_DATE_KHR;
521
522 break;
523 }
524
525 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
526 xcb_present_idle_notify_event_t *idle = (void *) event;
527
528 for (unsigned i = 0; i < chain->image_count; i++) {
529 if (chain->images[i].pixmap == idle->pixmap) {
530 chain->images[i].busy = false;
531 break;
532 }
533 }
534
535 break;
536 }
537
538 case XCB_PRESENT_COMPLETE_NOTIFY:
539 default:
540 break;
541 }
542
543 return VK_SUCCESS;
544 }
545
546 static VkResult
547 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
548 uint64_t timeout,
549 VkSemaphore semaphore,
550 uint32_t *image_index)
551 {
552 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
553
554 while (1) {
555 for (uint32_t i = 0; i < chain->image_count; i++) {
556 if (!chain->images[i].busy) {
557 /* We found a non-busy image */
558 xshmfence_await(chain->images[i].shm_fence);
559 *image_index = i;
560 chain->images[i].busy = true;
561 return VK_SUCCESS;
562 }
563 }
564
565 xcb_flush(chain->conn);
566 xcb_generic_event_t *event =
567 xcb_wait_for_special_event(chain->conn, chain->special_event);
568 if (!event)
569 return VK_ERROR_OUT_OF_DATE_KHR;
570
571 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
572 free(event);
573 if (result != VK_SUCCESS)
574 return result;
575 }
576 }
577
578 static VkResult
579 x11_queue_present(struct wsi_swapchain *anv_chain,
580 uint32_t image_index)
581 {
582 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
583 struct x11_image *image = &chain->images[image_index];
584
585 assert(image_index < chain->image_count);
586
587 uint32_t options = XCB_PRESENT_OPTION_NONE;
588
589 int64_t target_msc = 0;
590 int64_t divisor = 0;
591 int64_t remainder = 0;
592
593 options |= XCB_PRESENT_OPTION_ASYNC;
594
595 xshmfence_reset(image->shm_fence);
596
597 ++chain->send_sbc;
598 xcb_void_cookie_t cookie =
599 xcb_present_pixmap(chain->conn,
600 chain->window,
601 image->pixmap,
602 (uint32_t) chain->send_sbc,
603 0, /* valid */
604 0, /* update */
605 0, /* x_off */
606 0, /* y_off */
607 XCB_NONE, /* target_crtc */
608 XCB_NONE,
609 image->sync_fence,
610 options,
611 target_msc,
612 divisor,
613 remainder, 0, NULL);
614 xcb_discard_reply(chain->conn, cookie.sequence);
615 image->busy = true;
616
617 xcb_flush(chain->conn);
618
619 return VK_SUCCESS;
620 }
621
622 static VkResult
623 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
624 const VkSwapchainCreateInfoKHR *pCreateInfo,
625 const VkAllocationCallbacks* pAllocator,
626 struct x11_image *image)
627 {
628 xcb_void_cookie_t cookie;
629 VkResult result;
630 uint32_t row_pitch;
631 uint32_t offset;
632 uint32_t bpp = 32;
633 uint32_t depth = 24;
634 int fd;
635 uint32_t size;
636
637 result = chain->base.image_fns->create_wsi_image(device_h,
638 pCreateInfo,
639 pAllocator,
640 &image->image,
641 &image->memory,
642 &size,
643 &offset,
644 &row_pitch,
645 &fd);
646 if (result != VK_SUCCESS)
647 return result;
648
649 image->pixmap = xcb_generate_id(chain->conn);
650
651 cookie =
652 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
653 image->pixmap,
654 chain->window,
655 size,
656 pCreateInfo->imageExtent.width,
657 pCreateInfo->imageExtent.height,
658 row_pitch,
659 depth, bpp, fd);
660 xcb_discard_reply(chain->conn, cookie.sequence);
661
662 int fence_fd = xshmfence_alloc_shm();
663 if (fence_fd < 0)
664 goto fail_pixmap;
665
666 image->shm_fence = xshmfence_map_shm(fence_fd);
667 if (image->shm_fence == NULL)
668 goto fail_shmfence_alloc;
669
670 image->sync_fence = xcb_generate_id(chain->conn);
671 xcb_dri3_fence_from_fd(chain->conn,
672 image->pixmap,
673 image->sync_fence,
674 false,
675 fence_fd);
676
677 image->busy = false;
678 xshmfence_trigger(image->shm_fence);
679
680 return VK_SUCCESS;
681
682 fail_shmfence_alloc:
683 close(fence_fd);
684
685 fail_pixmap:
686 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
687 xcb_discard_reply(chain->conn, cookie.sequence);
688
689 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
690 image->image, image->memory);
691
692 return result;
693 }
694
695 static void
696 x11_image_finish(struct x11_swapchain *chain,
697 const VkAllocationCallbacks* pAllocator,
698 struct x11_image *image)
699 {
700 xcb_void_cookie_t cookie;
701
702 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
703 xcb_discard_reply(chain->conn, cookie.sequence);
704 xshmfence_unmap_shm(image->shm_fence);
705
706 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
707 xcb_discard_reply(chain->conn, cookie.sequence);
708
709 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
710 image->image, image->memory);
711 }
712
713 static VkResult
714 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
715 const VkAllocationCallbacks *pAllocator)
716 {
717 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
718 for (uint32_t i = 0; i < chain->image_count; i++)
719 x11_image_finish(chain, pAllocator, &chain->images[i]);
720
721 xcb_unregister_for_special_event(chain->conn, chain->special_event);
722
723 vk_free(pAllocator, chain);
724
725 return VK_SUCCESS;
726 }
727
728 static VkResult
729 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
730 VkDevice device,
731 struct wsi_device *wsi_device,
732 const VkSwapchainCreateInfoKHR *pCreateInfo,
733 const VkAllocationCallbacks* pAllocator,
734 const struct wsi_image_fns *image_fns,
735 struct wsi_swapchain **swapchain_out)
736 {
737 struct x11_swapchain *chain;
738 xcb_void_cookie_t cookie;
739 VkResult result;
740
741 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
742
743 int num_images = pCreateInfo->minImageCount;
744
745 /* For true mailbox mode, we need at least 4 images:
746 * 1) One to scan out from
747 * 2) One to have queued for scan-out
748 * 3) One to be currently held by the Wayland compositor
749 * 4) One to render to
750 */
751 if (pCreateInfo->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
752 num_images = MAX2(num_images, 4);
753
754 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
755 chain = vk_alloc(pAllocator, size, 8,
756 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
757 if (chain == NULL)
758 return VK_ERROR_OUT_OF_HOST_MEMORY;
759
760 chain->base.device = device;
761 chain->base.destroy = x11_swapchain_destroy;
762 chain->base.get_images = x11_get_images;
763 chain->base.acquire_next_image = x11_acquire_next_image;
764 chain->base.queue_present = x11_queue_present;
765 chain->base.image_fns = image_fns;
766 chain->conn = x11_surface_get_connection(icd_surface);
767 chain->window = x11_surface_get_window(icd_surface);
768 chain->extent = pCreateInfo->imageExtent;
769 chain->image_count = num_images;
770 chain->send_sbc = 0;
771
772 chain->event_id = xcb_generate_id(chain->conn);
773 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
774 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
775 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
776 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
777
778 /* Create an XCB event queue to hold present events outside of the usual
779 * application event queue
780 */
781 chain->special_event =
782 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
783 chain->event_id, NULL);
784
785 chain->gc = xcb_generate_id(chain->conn);
786 if (!chain->gc) {
787 /* FINISHME: Choose a better error. */
788 result = VK_ERROR_OUT_OF_HOST_MEMORY;
789 goto fail_register;
790 }
791
792 cookie = xcb_create_gc(chain->conn,
793 chain->gc,
794 chain->window,
795 XCB_GC_GRAPHICS_EXPOSURES,
796 (uint32_t []) { 0 });
797 xcb_discard_reply(chain->conn, cookie.sequence);
798
799 uint32_t image = 0;
800 for (; image < chain->image_count; image++) {
801 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
802 &chain->images[image]);
803 if (result != VK_SUCCESS)
804 goto fail_init_images;
805 }
806
807 *swapchain_out = &chain->base;
808
809 return VK_SUCCESS;
810
811 fail_init_images:
812 for (uint32_t j = 0; j < image; j++)
813 x11_image_finish(chain, pAllocator, &chain->images[j]);
814
815 fail_register:
816 xcb_unregister_for_special_event(chain->conn, chain->special_event);
817
818 vk_free(pAllocator, chain);
819
820 return result;
821 }
822
823 VkResult
824 wsi_x11_init_wsi(struct wsi_device *wsi_device,
825 const VkAllocationCallbacks *alloc)
826 {
827 struct wsi_x11 *wsi;
828 VkResult result;
829
830 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
831 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
832 if (!wsi) {
833 result = VK_ERROR_OUT_OF_HOST_MEMORY;
834 goto fail;
835 }
836
837 int ret = pthread_mutex_init(&wsi->mutex, NULL);
838 if (ret != 0) {
839 if (ret == ENOMEM) {
840 result = VK_ERROR_OUT_OF_HOST_MEMORY;
841 } else {
842 /* FINISHME: Choose a better error. */
843 result = VK_ERROR_OUT_OF_HOST_MEMORY;
844 }
845
846 goto fail_alloc;
847 }
848
849 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
850 _mesa_key_pointer_equal);
851 if (!wsi->connections) {
852 result = VK_ERROR_OUT_OF_HOST_MEMORY;
853 goto fail_mutex;
854 }
855
856 wsi->base.get_support = x11_surface_get_support;
857 wsi->base.get_capabilities = x11_surface_get_capabilities;
858 wsi->base.get_formats = x11_surface_get_formats;
859 wsi->base.get_present_modes = x11_surface_get_present_modes;
860 wsi->base.create_swapchain = x11_surface_create_swapchain;
861
862 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
863 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
864
865 return VK_SUCCESS;
866
867 fail_mutex:
868 pthread_mutex_destroy(&wsi->mutex);
869 fail_alloc:
870 vk_free(alloc, wsi);
871 fail:
872 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
873 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
874
875 return result;
876 }
877
878 void
879 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
880 const VkAllocationCallbacks *alloc)
881 {
882 struct wsi_x11 *wsi =
883 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
884
885 if (wsi) {
886 _mesa_hash_table_destroy(wsi->connections, NULL);
887
888 pthread_mutex_destroy(&wsi->mutex);
889
890 vk_free(alloc, wsi);
891 }
892 }