vulkan/wsi/x11: add support to detect if we can support rendering (v3)
[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 #include <fcntl.h>
37 #include <poll.h>
38 #include <xf86drm.h>
39 #include "util/hash_table.h"
40
41 #include "wsi_common.h"
42 #include "wsi_common_x11.h"
43 #include "wsi_common_queue.h"
44
45 #define typed_memcpy(dest, src, count) ({ \
46 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
47 memcpy((dest), (src), (count) * sizeof(*(src))); \
48 })
49
50 struct wsi_x11_connection {
51 bool has_dri3;
52 bool has_present;
53 };
54
55 struct wsi_x11 {
56 struct wsi_interface base;
57
58 pthread_mutex_t mutex;
59 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
60 struct hash_table *connections;
61 };
62
63
64 /** wsi_dri3_open
65 *
66 * Wrapper around xcb_dri3_open
67 */
68 static int
69 wsi_dri3_open(xcb_connection_t *conn,
70 xcb_window_t root,
71 uint32_t provider)
72 {
73 xcb_dri3_open_cookie_t cookie;
74 xcb_dri3_open_reply_t *reply;
75 int fd;
76
77 cookie = xcb_dri3_open(conn,
78 root,
79 provider);
80
81 reply = xcb_dri3_open_reply(conn, cookie, NULL);
82 if (!reply)
83 return -1;
84
85 if (reply->nfd != 1) {
86 free(reply);
87 return -1;
88 }
89
90 fd = xcb_dri3_open_reply_fds(conn, reply)[0];
91 free(reply);
92 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
93
94 return fd;
95 }
96
97 static bool
98 wsi_x11_check_dri3_compatible(xcb_connection_t *conn, int local_fd)
99 {
100 xcb_screen_iterator_t screen_iter =
101 xcb_setup_roots_iterator(xcb_get_setup(conn));
102 xcb_screen_t *screen = screen_iter.data;
103
104 int dri3_fd = wsi_dri3_open(conn, screen->root, None);
105 if (dri3_fd != -1) {
106 char *local_dev = drmGetRenderDeviceNameFromFd(local_fd);
107 char *dri3_dev = drmGetRenderDeviceNameFromFd(dri3_fd);
108 int ret;
109
110 close(dri3_fd);
111
112 ret = strcmp(local_dev, dri3_dev);
113
114 free(local_dev);
115 free(dri3_dev);
116
117 if (ret != 0)
118 return false;
119 }
120 return true;
121 }
122
123 static struct wsi_x11_connection *
124 wsi_x11_connection_create(const VkAllocationCallbacks *alloc,
125 xcb_connection_t *conn)
126 {
127 xcb_query_extension_cookie_t dri3_cookie, pres_cookie;
128 xcb_query_extension_reply_t *dri3_reply, *pres_reply;
129
130 struct wsi_x11_connection *wsi_conn =
131 vk_alloc(alloc, sizeof(*wsi_conn), 8,
132 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
133 if (!wsi_conn)
134 return NULL;
135
136 dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
137 pres_cookie = xcb_query_extension(conn, 7, "PRESENT");
138
139 dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
140 pres_reply = xcb_query_extension_reply(conn, pres_cookie, NULL);
141 if (dri3_reply == NULL || pres_reply == NULL) {
142 free(dri3_reply);
143 free(pres_reply);
144 vk_free(alloc, wsi_conn);
145 return NULL;
146 }
147
148 wsi_conn->has_dri3 = dri3_reply->present != 0;
149 wsi_conn->has_present = pres_reply->present != 0;
150
151 free(dri3_reply);
152 free(pres_reply);
153
154 return wsi_conn;
155 }
156
157 static void
158 wsi_x11_connection_destroy(const VkAllocationCallbacks *alloc,
159 struct wsi_x11_connection *conn)
160 {
161 vk_free(alloc, conn);
162 }
163
164 static struct wsi_x11_connection *
165 wsi_x11_get_connection(struct wsi_device *wsi_dev,
166 const VkAllocationCallbacks *alloc,
167 xcb_connection_t *conn)
168 {
169 struct wsi_x11 *wsi =
170 (struct wsi_x11 *)wsi_dev->wsi[VK_ICD_WSI_PLATFORM_XCB];
171
172 pthread_mutex_lock(&wsi->mutex);
173
174 struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
175 if (!entry) {
176 /* We're about to make a bunch of blocking calls. Let's drop the
177 * mutex for now so we don't block up too badly.
178 */
179 pthread_mutex_unlock(&wsi->mutex);
180
181 struct wsi_x11_connection *wsi_conn =
182 wsi_x11_connection_create(alloc, conn);
183 if (!wsi_conn)
184 return NULL;
185
186 pthread_mutex_lock(&wsi->mutex);
187
188 entry = _mesa_hash_table_search(wsi->connections, conn);
189 if (entry) {
190 /* Oops, someone raced us to it */
191 wsi_x11_connection_destroy(alloc, wsi_conn);
192 } else {
193 entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
194 }
195 }
196
197 pthread_mutex_unlock(&wsi->mutex);
198
199 return entry->data;
200 }
201
202 static const VkSurfaceFormatKHR formats[] = {
203 { .format = VK_FORMAT_B8G8R8A8_SRGB, },
204 { .format = VK_FORMAT_B8G8R8A8_UNORM, },
205 };
206
207 static const VkPresentModeKHR present_modes[] = {
208 VK_PRESENT_MODE_IMMEDIATE_KHR,
209 VK_PRESENT_MODE_MAILBOX_KHR,
210 VK_PRESENT_MODE_FIFO_KHR,
211 };
212
213 static xcb_screen_t *
214 get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
215 {
216 xcb_screen_iterator_t screen_iter =
217 xcb_setup_roots_iterator(xcb_get_setup(conn));
218
219 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
220 if (screen_iter.data->root == root)
221 return screen_iter.data;
222 }
223
224 return NULL;
225 }
226
227 static xcb_visualtype_t *
228 screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
229 unsigned *depth)
230 {
231 xcb_depth_iterator_t depth_iter =
232 xcb_screen_allowed_depths_iterator(screen);
233
234 for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
235 xcb_visualtype_iterator_t visual_iter =
236 xcb_depth_visuals_iterator (depth_iter.data);
237
238 for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
239 if (visual_iter.data->visual_id == visual_id) {
240 if (depth)
241 *depth = depth_iter.data->depth;
242 return visual_iter.data;
243 }
244 }
245 }
246
247 return NULL;
248 }
249
250 static xcb_visualtype_t *
251 connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id,
252 unsigned *depth)
253 {
254 xcb_screen_iterator_t screen_iter =
255 xcb_setup_roots_iterator(xcb_get_setup(conn));
256
257 /* For this we have to iterate over all of the screens which is rather
258 * annoying. Fortunately, there is probably only 1.
259 */
260 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
261 xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
262 visual_id, depth);
263 if (visual)
264 return visual;
265 }
266
267 return NULL;
268 }
269
270 static xcb_visualtype_t *
271 get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
272 unsigned *depth)
273 {
274 xcb_query_tree_cookie_t tree_cookie;
275 xcb_get_window_attributes_cookie_t attrib_cookie;
276 xcb_query_tree_reply_t *tree;
277 xcb_get_window_attributes_reply_t *attrib;
278
279 tree_cookie = xcb_query_tree(conn, window);
280 attrib_cookie = xcb_get_window_attributes(conn, window);
281
282 tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
283 attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
284 if (attrib == NULL || tree == NULL) {
285 free(attrib);
286 free(tree);
287 return NULL;
288 }
289
290 xcb_window_t root = tree->root;
291 xcb_visualid_t visual_id = attrib->visual;
292 free(attrib);
293 free(tree);
294
295 xcb_screen_t *screen = get_screen_for_root(conn, root);
296 if (screen == NULL)
297 return NULL;
298
299 return screen_get_visualtype(screen, visual_id, depth);
300 }
301
302 static bool
303 visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
304 {
305 uint32_t rgb_mask = visual->red_mask |
306 visual->green_mask |
307 visual->blue_mask;
308
309 uint32_t all_mask = 0xffffffff >> (32 - depth);
310
311 /* Do we have bits left over after RGB? */
312 return (all_mask & ~rgb_mask) != 0;
313 }
314
315 VkBool32 wsi_get_physical_device_xcb_presentation_support(
316 struct wsi_device *wsi_device,
317 VkAllocationCallbacks *alloc,
318 uint32_t queueFamilyIndex,
319 int fd,
320 xcb_connection_t* connection,
321 xcb_visualid_t visual_id)
322 {
323 struct wsi_x11_connection *wsi_conn =
324 wsi_x11_get_connection(wsi_device, alloc, connection);
325
326 if (!wsi_conn)
327 return false;
328
329 if (!wsi_conn->has_dri3) {
330 fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n");
331 fprintf(stderr, "Note: Buggy applications may crash, if they do please report to vendor\n");
332 return false;
333 }
334
335 if (!wsi_x11_check_dri3_compatible(connection, fd))
336 return false;
337
338 unsigned visual_depth;
339 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
340 return false;
341
342 if (visual_depth != 24 && visual_depth != 32)
343 return false;
344
345 return true;
346 }
347
348 static xcb_connection_t*
349 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
350 {
351 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
352 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
353 else
354 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
355 }
356
357 static xcb_window_t
358 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
359 {
360 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
361 return ((VkIcdSurfaceXlib *)icd_surface)->window;
362 else
363 return ((VkIcdSurfaceXcb *)icd_surface)->window;
364 }
365
366 static VkResult
367 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
368 struct wsi_device *wsi_device,
369 const VkAllocationCallbacks *alloc,
370 uint32_t queueFamilyIndex,
371 int local_fd,
372 VkBool32* pSupported)
373 {
374 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
375 xcb_window_t window = x11_surface_get_window(icd_surface);
376
377 struct wsi_x11_connection *wsi_conn =
378 wsi_x11_get_connection(wsi_device, alloc, conn);
379 if (!wsi_conn)
380 return VK_ERROR_OUT_OF_HOST_MEMORY;
381
382 if (!wsi_conn->has_dri3) {
383 fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n");
384 fprintf(stderr, "Note: Buggy applications may crash, if they do please report to vendor\n");
385 *pSupported = false;
386 return VK_SUCCESS;
387 }
388
389 if (!wsi_x11_check_dri3_compatible(conn, local_fd))
390 return false;
391
392 unsigned visual_depth;
393 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
394 *pSupported = false;
395 return VK_SUCCESS;
396 }
397
398 if (visual_depth != 24 && visual_depth != 32) {
399 *pSupported = false;
400 return VK_SUCCESS;
401 }
402
403 *pSupported = true;
404 return VK_SUCCESS;
405 }
406
407 static VkResult
408 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
409 VkSurfaceCapabilitiesKHR *caps)
410 {
411 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
412 xcb_window_t window = x11_surface_get_window(icd_surface);
413 xcb_get_geometry_cookie_t geom_cookie;
414 xcb_generic_error_t *err;
415 xcb_get_geometry_reply_t *geom;
416 unsigned visual_depth;
417
418 geom_cookie = xcb_get_geometry(conn, window);
419
420 /* This does a round-trip. This is why we do get_geometry first and
421 * wait to read the reply until after we have a visual.
422 */
423 xcb_visualtype_t *visual =
424 get_visualtype_for_window(conn, window, &visual_depth);
425
426 if (!visual)
427 return VK_ERROR_SURFACE_LOST_KHR;
428
429 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
430 if (geom) {
431 VkExtent2D extent = { geom->width, geom->height };
432 caps->currentExtent = extent;
433 caps->minImageExtent = extent;
434 caps->maxImageExtent = extent;
435 } else {
436 /* This can happen if the client didn't wait for the configure event
437 * to come back from the compositor. In that case, we don't know the
438 * size of the window so we just return valid "I don't know" stuff.
439 */
440 caps->currentExtent = (VkExtent2D) { -1, -1 };
441 caps->minImageExtent = (VkExtent2D) { 1, 1 };
442 /* This is the maximum supported size on Intel */
443 caps->maxImageExtent = (VkExtent2D) { 1 << 14, 1 << 14 };
444 }
445 free(err);
446 free(geom);
447
448 if (visual_has_alpha(visual, visual_depth)) {
449 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
450 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
451 } else {
452 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
453 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
454 }
455
456 /* For true mailbox mode, we need at least 4 images:
457 * 1) One to scan out from
458 * 2) One to have queued for scan-out
459 * 3) One to be currently held by the X server
460 * 4) One to render to
461 */
462 caps->minImageCount = 2;
463 /* There is no real maximum */
464 caps->maxImageCount = 0;
465
466 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
467 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
468 caps->maxImageArrayLayers = 1;
469 caps->supportedUsageFlags =
470 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
471 VK_IMAGE_USAGE_SAMPLED_BIT |
472 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
473 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
474
475 return VK_SUCCESS;
476 }
477
478 static VkResult
479 x11_surface_get_formats(VkIcdSurfaceBase *surface,
480 struct wsi_device *wsi_device,
481 uint32_t *pSurfaceFormatCount,
482 VkSurfaceFormatKHR *pSurfaceFormats)
483 {
484 if (pSurfaceFormats == NULL) {
485 *pSurfaceFormatCount = ARRAY_SIZE(formats);
486 return VK_SUCCESS;
487 }
488
489 *pSurfaceFormatCount = MIN2(*pSurfaceFormatCount, ARRAY_SIZE(formats));
490 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
491
492 return *pSurfaceFormatCount < ARRAY_SIZE(formats) ?
493 VK_INCOMPLETE : VK_SUCCESS;
494 }
495
496 static VkResult
497 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
498 uint32_t *pPresentModeCount,
499 VkPresentModeKHR *pPresentModes)
500 {
501 if (pPresentModes == NULL) {
502 *pPresentModeCount = ARRAY_SIZE(present_modes);
503 return VK_SUCCESS;
504 }
505
506 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
507 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
508
509 return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
510 VK_INCOMPLETE : VK_SUCCESS;
511 }
512
513 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
514 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
515 VkSurfaceKHR *pSurface)
516 {
517 VkIcdSurfaceXcb *surface;
518
519 surface = vk_alloc(pAllocator, sizeof *surface, 8,
520 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
521 if (surface == NULL)
522 return VK_ERROR_OUT_OF_HOST_MEMORY;
523
524 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
525 surface->connection = pCreateInfo->connection;
526 surface->window = pCreateInfo->window;
527
528 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
529 return VK_SUCCESS;
530 }
531
532 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
533 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
534 VkSurfaceKHR *pSurface)
535 {
536 VkIcdSurfaceXlib *surface;
537
538 surface = vk_alloc(pAllocator, sizeof *surface, 8,
539 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
540 if (surface == NULL)
541 return VK_ERROR_OUT_OF_HOST_MEMORY;
542
543 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
544 surface->dpy = pCreateInfo->dpy;
545 surface->window = pCreateInfo->window;
546
547 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
548 return VK_SUCCESS;
549 }
550
551 struct x11_image {
552 VkImage image;
553 VkDeviceMemory memory;
554 xcb_pixmap_t pixmap;
555 bool busy;
556 struct xshmfence * shm_fence;
557 uint32_t sync_fence;
558 };
559
560 struct x11_swapchain {
561 struct wsi_swapchain base;
562
563 xcb_connection_t * conn;
564 xcb_window_t window;
565 xcb_gc_t gc;
566 uint32_t depth;
567 VkExtent2D extent;
568 uint32_t image_count;
569
570 xcb_present_event_t event_id;
571 xcb_special_event_t * special_event;
572 uint64_t send_sbc;
573 uint64_t last_present_msc;
574 uint32_t stamp;
575
576 bool threaded;
577 VkResult status;
578 struct wsi_queue present_queue;
579 struct wsi_queue acquire_queue;
580 pthread_t queue_manager;
581
582 struct x11_image images[0];
583 };
584
585 static VkResult
586 x11_get_images(struct wsi_swapchain *anv_chain,
587 uint32_t* pCount, VkImage *pSwapchainImages)
588 {
589 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
590 uint32_t ret_count;
591 VkResult result;
592
593 if (pSwapchainImages == NULL) {
594 *pCount = chain->image_count;
595 return VK_SUCCESS;
596 }
597
598 result = VK_SUCCESS;
599 ret_count = chain->image_count;
600 if (chain->image_count > *pCount) {
601 ret_count = *pCount;
602 result = VK_INCOMPLETE;
603 }
604
605 for (uint32_t i = 0; i < ret_count; i++)
606 pSwapchainImages[i] = chain->images[i].image;
607
608 return result;
609 }
610
611 static VkResult
612 x11_handle_dri3_present_event(struct x11_swapchain *chain,
613 xcb_present_generic_event_t *event)
614 {
615 switch (event->evtype) {
616 case XCB_PRESENT_CONFIGURE_NOTIFY: {
617 xcb_present_configure_notify_event_t *config = (void *) event;
618
619 if (config->width != chain->extent.width ||
620 config->height != chain->extent.height)
621 return VK_ERROR_OUT_OF_DATE_KHR;
622
623 break;
624 }
625
626 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
627 xcb_present_idle_notify_event_t *idle = (void *) event;
628
629 for (unsigned i = 0; i < chain->image_count; i++) {
630 if (chain->images[i].pixmap == idle->pixmap) {
631 chain->images[i].busy = false;
632 if (chain->threaded)
633 wsi_queue_push(&chain->acquire_queue, i);
634 break;
635 }
636 }
637
638 break;
639 }
640
641 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
642 xcb_present_complete_notify_event_t *complete = (void *) event;
643 if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
644 chain->last_present_msc = complete->msc;
645 break;
646 }
647
648 default:
649 break;
650 }
651
652 return VK_SUCCESS;
653 }
654
655
656 static uint64_t wsi_get_current_time(void)
657 {
658 uint64_t current_time;
659 struct timespec tv;
660
661 clock_gettime(CLOCK_MONOTONIC, &tv);
662 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
663 return current_time;
664 }
665
666 static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
667 {
668 uint64_t current_time = wsi_get_current_time();
669
670 timeout = MIN2(UINT64_MAX - current_time, timeout);
671
672 return current_time + timeout;
673 }
674
675 static VkResult
676 x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
677 uint32_t *image_index, uint64_t timeout)
678 {
679 xcb_generic_event_t *event;
680 struct pollfd pfds;
681 uint64_t atimeout;
682 while (1) {
683 for (uint32_t i = 0; i < chain->image_count; i++) {
684 if (!chain->images[i].busy) {
685 /* We found a non-busy image */
686 xshmfence_await(chain->images[i].shm_fence);
687 *image_index = i;
688 chain->images[i].busy = true;
689 return VK_SUCCESS;
690 }
691 }
692
693 xcb_flush(chain->conn);
694
695 if (timeout == UINT64_MAX) {
696 event = xcb_wait_for_special_event(chain->conn, chain->special_event);
697 if (!event)
698 return VK_ERROR_OUT_OF_DATE_KHR;
699 } else {
700 event = xcb_poll_for_special_event(chain->conn, chain->special_event);
701 if (!event) {
702 int ret;
703 if (timeout == 0)
704 return VK_NOT_READY;
705
706 atimeout = wsi_get_absolute_timeout(timeout);
707
708 pfds.fd = xcb_get_file_descriptor(chain->conn);
709 pfds.events = POLLIN;
710 ret = poll(&pfds, 1, timeout / 1000 / 1000);
711 if (ret == 0)
712 return VK_TIMEOUT;
713 if (ret == -1)
714 return VK_ERROR_OUT_OF_DATE_KHR;
715
716 /* If a non-special event happens, the fd will still
717 * poll. So recalculate the timeout now just in case.
718 */
719 uint64_t current_time = wsi_get_current_time();
720 if (atimeout > current_time)
721 timeout = atimeout - current_time;
722 else
723 timeout = 0;
724 continue;
725 }
726 }
727
728 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
729 free(event);
730 if (result != VK_SUCCESS)
731 return result;
732 }
733 }
734
735 static VkResult
736 x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
737 uint32_t *image_index_out, uint64_t timeout)
738 {
739 assert(chain->threaded);
740
741 uint32_t image_index;
742 VkResult result = wsi_queue_pull(&chain->acquire_queue,
743 &image_index, timeout);
744 if (result != VK_SUCCESS) {
745 return result;
746 } else if (chain->status != VK_SUCCESS) {
747 return chain->status;
748 }
749
750 assert(image_index < chain->image_count);
751 xshmfence_await(chain->images[image_index].shm_fence);
752
753 *image_index_out = image_index;
754
755 return VK_SUCCESS;
756 }
757
758 static VkResult
759 x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
760 uint32_t target_msc)
761 {
762 struct x11_image *image = &chain->images[image_index];
763
764 assert(image_index < chain->image_count);
765
766 uint32_t options = XCB_PRESENT_OPTION_NONE;
767
768 int64_t divisor = 0;
769 int64_t remainder = 0;
770
771 if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
772 options |= XCB_PRESENT_OPTION_ASYNC;
773
774 xshmfence_reset(image->shm_fence);
775
776 ++chain->send_sbc;
777 xcb_void_cookie_t cookie =
778 xcb_present_pixmap(chain->conn,
779 chain->window,
780 image->pixmap,
781 (uint32_t) chain->send_sbc,
782 0, /* valid */
783 0, /* update */
784 0, /* x_off */
785 0, /* y_off */
786 XCB_NONE, /* target_crtc */
787 XCB_NONE,
788 image->sync_fence,
789 options,
790 target_msc,
791 divisor,
792 remainder, 0, NULL);
793 xcb_discard_reply(chain->conn, cookie.sequence);
794 image->busy = true;
795
796 xcb_flush(chain->conn);
797
798 return VK_SUCCESS;
799 }
800
801 static VkResult
802 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
803 uint64_t timeout,
804 VkSemaphore semaphore,
805 uint32_t *image_index)
806 {
807 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
808
809 if (chain->threaded) {
810 return x11_acquire_next_image_from_queue(chain, image_index, timeout);
811 } else {
812 return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
813 }
814 }
815
816 static VkResult
817 x11_queue_present(struct wsi_swapchain *anv_chain,
818 uint32_t image_index)
819 {
820 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
821
822 if (chain->threaded) {
823 wsi_queue_push(&chain->present_queue, image_index);
824 return chain->status;
825 } else {
826 return x11_present_to_x11(chain, image_index, 0);
827 }
828 }
829
830 static void *
831 x11_manage_fifo_queues(void *state)
832 {
833 struct x11_swapchain *chain = state;
834 VkResult result;
835
836 assert(chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR);
837
838 while (chain->status == VK_SUCCESS) {
839 /* It should be safe to unconditionally block here. Later in the loop
840 * we blocks until the previous present has landed on-screen. At that
841 * point, we should have received IDLE_NOTIFY on all images presented
842 * before that point so the client should be able to acquire any image
843 * other than the currently presented one.
844 */
845 uint32_t image_index;
846 result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
847 if (result != VK_SUCCESS) {
848 goto fail;
849 } else if (chain->status != VK_SUCCESS) {
850 return NULL;
851 }
852
853 uint64_t target_msc = chain->last_present_msc + 1;
854 result = x11_present_to_x11(chain, image_index, target_msc);
855 if (result != VK_SUCCESS)
856 goto fail;
857
858 while (chain->last_present_msc < target_msc) {
859 xcb_generic_event_t *event =
860 xcb_wait_for_special_event(chain->conn, chain->special_event);
861 if (!event)
862 goto fail;
863
864 result = x11_handle_dri3_present_event(chain, (void *)event);
865 if (result != VK_SUCCESS)
866 goto fail;
867 }
868 }
869
870 fail:
871 chain->status = result;
872 wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
873
874 return NULL;
875 }
876
877 static VkResult
878 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
879 const VkSwapchainCreateInfoKHR *pCreateInfo,
880 const VkAllocationCallbacks* pAllocator,
881 struct x11_image *image)
882 {
883 xcb_void_cookie_t cookie;
884 VkResult result;
885 uint32_t row_pitch;
886 uint32_t offset;
887 uint32_t bpp = 32;
888 int fd;
889 uint32_t size;
890
891 result = chain->base.image_fns->create_wsi_image(device_h,
892 pCreateInfo,
893 pAllocator,
894 &image->image,
895 &image->memory,
896 &size,
897 &offset,
898 &row_pitch,
899 &fd);
900 if (result != VK_SUCCESS)
901 return result;
902
903 image->pixmap = xcb_generate_id(chain->conn);
904
905 cookie =
906 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
907 image->pixmap,
908 chain->window,
909 size,
910 pCreateInfo->imageExtent.width,
911 pCreateInfo->imageExtent.height,
912 row_pitch,
913 chain->depth, bpp, fd);
914 xcb_discard_reply(chain->conn, cookie.sequence);
915
916 int fence_fd = xshmfence_alloc_shm();
917 if (fence_fd < 0)
918 goto fail_pixmap;
919
920 image->shm_fence = xshmfence_map_shm(fence_fd);
921 if (image->shm_fence == NULL)
922 goto fail_shmfence_alloc;
923
924 image->sync_fence = xcb_generate_id(chain->conn);
925 xcb_dri3_fence_from_fd(chain->conn,
926 image->pixmap,
927 image->sync_fence,
928 false,
929 fence_fd);
930
931 image->busy = false;
932 xshmfence_trigger(image->shm_fence);
933
934 return VK_SUCCESS;
935
936 fail_shmfence_alloc:
937 close(fence_fd);
938
939 fail_pixmap:
940 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
941 xcb_discard_reply(chain->conn, cookie.sequence);
942
943 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
944 image->image, image->memory);
945
946 return result;
947 }
948
949 static void
950 x11_image_finish(struct x11_swapchain *chain,
951 const VkAllocationCallbacks* pAllocator,
952 struct x11_image *image)
953 {
954 xcb_void_cookie_t cookie;
955
956 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
957 xcb_discard_reply(chain->conn, cookie.sequence);
958 xshmfence_unmap_shm(image->shm_fence);
959
960 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
961 xcb_discard_reply(chain->conn, cookie.sequence);
962
963 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
964 image->image, image->memory);
965 }
966
967 static VkResult
968 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
969 const VkAllocationCallbacks *pAllocator)
970 {
971 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
972 xcb_void_cookie_t cookie;
973
974 for (uint32_t i = 0; i < chain->image_count; i++)
975 x11_image_finish(chain, pAllocator, &chain->images[i]);
976
977 if (chain->threaded) {
978 chain->status = VK_ERROR_OUT_OF_DATE_KHR;
979 /* Push a UINT32_MAX to wake up the manager */
980 wsi_queue_push(&chain->present_queue, UINT32_MAX);
981 pthread_join(chain->queue_manager, NULL);
982 wsi_queue_destroy(&chain->acquire_queue);
983 wsi_queue_destroy(&chain->present_queue);
984 }
985
986 xcb_unregister_for_special_event(chain->conn, chain->special_event);
987 cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
988 chain->window,
989 XCB_PRESENT_EVENT_MASK_NO_EVENT);
990 xcb_discard_reply(chain->conn, cookie.sequence);
991
992 vk_free(pAllocator, chain);
993
994 return VK_SUCCESS;
995 }
996
997 static VkResult
998 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
999 VkDevice device,
1000 struct wsi_device *wsi_device,
1001 const VkSwapchainCreateInfoKHR *pCreateInfo,
1002 const VkAllocationCallbacks* pAllocator,
1003 const struct wsi_image_fns *image_fns,
1004 struct wsi_swapchain **swapchain_out)
1005 {
1006 struct x11_swapchain *chain;
1007 xcb_void_cookie_t cookie;
1008 VkResult result;
1009
1010 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1011
1012 const unsigned num_images = pCreateInfo->minImageCount;
1013
1014 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1015 chain = vk_alloc(pAllocator, size, 8,
1016 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1017 if (chain == NULL)
1018 return VK_ERROR_OUT_OF_HOST_MEMORY;
1019
1020 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
1021 xcb_window_t window = x11_surface_get_window(icd_surface);
1022 xcb_get_geometry_reply_t *geometry =
1023 xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
1024
1025 if (geometry == NULL)
1026 return VK_ERROR_SURFACE_LOST_KHR;
1027
1028 chain->base.device = device;
1029 chain->base.destroy = x11_swapchain_destroy;
1030 chain->base.get_images = x11_get_images;
1031 chain->base.acquire_next_image = x11_acquire_next_image;
1032 chain->base.queue_present = x11_queue_present;
1033 chain->base.image_fns = image_fns;
1034 chain->base.present_mode = pCreateInfo->presentMode;
1035 chain->conn = conn;
1036 chain->window = window;
1037 chain->depth = geometry->depth;
1038 chain->extent = pCreateInfo->imageExtent;
1039 chain->image_count = num_images;
1040 chain->send_sbc = 0;
1041 chain->last_present_msc = 0;
1042 chain->threaded = false;
1043 chain->status = VK_SUCCESS;
1044
1045 free(geometry);
1046
1047 chain->event_id = xcb_generate_id(chain->conn);
1048 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
1049 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1050 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1051 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1052
1053 /* Create an XCB event queue to hold present events outside of the usual
1054 * application event queue
1055 */
1056 chain->special_event =
1057 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
1058 chain->event_id, NULL);
1059
1060 chain->gc = xcb_generate_id(chain->conn);
1061 if (!chain->gc) {
1062 /* FINISHME: Choose a better error. */
1063 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1064 goto fail_register;
1065 }
1066
1067 cookie = xcb_create_gc(chain->conn,
1068 chain->gc,
1069 chain->window,
1070 XCB_GC_GRAPHICS_EXPOSURES,
1071 (uint32_t []) { 0 });
1072 xcb_discard_reply(chain->conn, cookie.sequence);
1073
1074 uint32_t image = 0;
1075 for (; image < chain->image_count; image++) {
1076 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1077 &chain->images[image]);
1078 if (result != VK_SUCCESS)
1079 goto fail_init_images;
1080 }
1081
1082 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
1083 chain->threaded = true;
1084
1085 /* Initialize our queues. We make them image_count + 1 because we will
1086 * occasionally use UINT32_MAX to signal the other thread that an error
1087 * has occurred and we don't want an overflow.
1088 */
1089 int ret;
1090 ret = wsi_queue_init(&chain->acquire_queue, chain->image_count + 1);
1091 if (ret) {
1092 goto fail_init_images;
1093 }
1094
1095 ret = wsi_queue_init(&chain->present_queue, chain->image_count + 1);
1096 if (ret) {
1097 wsi_queue_destroy(&chain->acquire_queue);
1098 goto fail_init_images;
1099 }
1100
1101 for (unsigned i = 0; i < chain->image_count; i++)
1102 wsi_queue_push(&chain->acquire_queue, i);
1103
1104 ret = pthread_create(&chain->queue_manager, NULL,
1105 x11_manage_fifo_queues, chain);
1106 if (ret) {
1107 wsi_queue_destroy(&chain->present_queue);
1108 wsi_queue_destroy(&chain->acquire_queue);
1109 goto fail_init_images;
1110 }
1111 }
1112
1113 *swapchain_out = &chain->base;
1114
1115 return VK_SUCCESS;
1116
1117 fail_init_images:
1118 for (uint32_t j = 0; j < image; j++)
1119 x11_image_finish(chain, pAllocator, &chain->images[j]);
1120
1121 fail_register:
1122 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1123
1124 vk_free(pAllocator, chain);
1125
1126 return result;
1127 }
1128
1129 VkResult
1130 wsi_x11_init_wsi(struct wsi_device *wsi_device,
1131 const VkAllocationCallbacks *alloc)
1132 {
1133 struct wsi_x11 *wsi;
1134 VkResult result;
1135
1136 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1137 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1138 if (!wsi) {
1139 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1140 goto fail;
1141 }
1142
1143 int ret = pthread_mutex_init(&wsi->mutex, NULL);
1144 if (ret != 0) {
1145 if (ret == ENOMEM) {
1146 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1147 } else {
1148 /* FINISHME: Choose a better error. */
1149 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1150 }
1151
1152 goto fail_alloc;
1153 }
1154
1155 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1156 _mesa_key_pointer_equal);
1157 if (!wsi->connections) {
1158 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1159 goto fail_mutex;
1160 }
1161
1162 wsi->base.get_support = x11_surface_get_support;
1163 wsi->base.get_capabilities = x11_surface_get_capabilities;
1164 wsi->base.get_formats = x11_surface_get_formats;
1165 wsi->base.get_present_modes = x11_surface_get_present_modes;
1166 wsi->base.create_swapchain = x11_surface_create_swapchain;
1167
1168 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1169 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1170
1171 return VK_SUCCESS;
1172
1173 fail_mutex:
1174 pthread_mutex_destroy(&wsi->mutex);
1175 fail_alloc:
1176 vk_free(alloc, wsi);
1177 fail:
1178 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1179 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1180
1181 return result;
1182 }
1183
1184 void
1185 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1186 const VkAllocationCallbacks *alloc)
1187 {
1188 struct wsi_x11 *wsi =
1189 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1190
1191 if (wsi) {
1192 struct hash_entry *entry;
1193 hash_table_foreach(wsi->connections, entry)
1194 wsi_x11_connection_destroy(alloc, entry->data);
1195
1196 _mesa_hash_table_destroy(wsi->connections, NULL);
1197
1198 pthread_mutex_destroy(&wsi->mutex);
1199
1200 vk_free(alloc, wsi);
1201 }
1202 }