vulkan/wsi: move image count to shared structure.
[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
569 xcb_present_event_t event_id;
570 xcb_special_event_t * special_event;
571 uint64_t send_sbc;
572 uint64_t last_present_msc;
573 uint32_t stamp;
574
575 bool threaded;
576 VkResult status;
577 struct wsi_queue present_queue;
578 struct wsi_queue acquire_queue;
579 pthread_t queue_manager;
580
581 struct x11_image images[0];
582 };
583
584 static VkResult
585 x11_get_images(struct wsi_swapchain *anv_chain,
586 uint32_t* pCount, VkImage *pSwapchainImages)
587 {
588 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
589 uint32_t ret_count;
590 VkResult result;
591
592 if (pSwapchainImages == NULL) {
593 *pCount = chain->base.image_count;
594 return VK_SUCCESS;
595 }
596
597 result = VK_SUCCESS;
598 ret_count = chain->base.image_count;
599 if (chain->base.image_count > *pCount) {
600 ret_count = *pCount;
601 result = VK_INCOMPLETE;
602 }
603
604 for (uint32_t i = 0; i < ret_count; i++)
605 pSwapchainImages[i] = chain->images[i].image;
606
607 return result;
608 }
609
610 static VkResult
611 x11_handle_dri3_present_event(struct x11_swapchain *chain,
612 xcb_present_generic_event_t *event)
613 {
614 switch (event->evtype) {
615 case XCB_PRESENT_CONFIGURE_NOTIFY: {
616 xcb_present_configure_notify_event_t *config = (void *) event;
617
618 if (config->width != chain->extent.width ||
619 config->height != chain->extent.height)
620 return VK_ERROR_OUT_OF_DATE_KHR;
621
622 break;
623 }
624
625 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
626 xcb_present_idle_notify_event_t *idle = (void *) event;
627
628 for (unsigned i = 0; i < chain->base.image_count; i++) {
629 if (chain->images[i].pixmap == idle->pixmap) {
630 chain->images[i].busy = false;
631 if (chain->threaded)
632 wsi_queue_push(&chain->acquire_queue, i);
633 break;
634 }
635 }
636
637 break;
638 }
639
640 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
641 xcb_present_complete_notify_event_t *complete = (void *) event;
642 if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
643 chain->last_present_msc = complete->msc;
644 break;
645 }
646
647 default:
648 break;
649 }
650
651 return VK_SUCCESS;
652 }
653
654
655 static uint64_t wsi_get_current_time(void)
656 {
657 uint64_t current_time;
658 struct timespec tv;
659
660 clock_gettime(CLOCK_MONOTONIC, &tv);
661 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
662 return current_time;
663 }
664
665 static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
666 {
667 uint64_t current_time = wsi_get_current_time();
668
669 timeout = MIN2(UINT64_MAX - current_time, timeout);
670
671 return current_time + timeout;
672 }
673
674 static VkResult
675 x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
676 uint32_t *image_index, uint64_t timeout)
677 {
678 xcb_generic_event_t *event;
679 struct pollfd pfds;
680 uint64_t atimeout;
681 while (1) {
682 for (uint32_t i = 0; i < chain->base.image_count; i++) {
683 if (!chain->images[i].busy) {
684 /* We found a non-busy image */
685 xshmfence_await(chain->images[i].shm_fence);
686 *image_index = i;
687 chain->images[i].busy = true;
688 return VK_SUCCESS;
689 }
690 }
691
692 xcb_flush(chain->conn);
693
694 if (timeout == UINT64_MAX) {
695 event = xcb_wait_for_special_event(chain->conn, chain->special_event);
696 if (!event)
697 return VK_ERROR_OUT_OF_DATE_KHR;
698 } else {
699 event = xcb_poll_for_special_event(chain->conn, chain->special_event);
700 if (!event) {
701 int ret;
702 if (timeout == 0)
703 return VK_NOT_READY;
704
705 atimeout = wsi_get_absolute_timeout(timeout);
706
707 pfds.fd = xcb_get_file_descriptor(chain->conn);
708 pfds.events = POLLIN;
709 ret = poll(&pfds, 1, timeout / 1000 / 1000);
710 if (ret == 0)
711 return VK_TIMEOUT;
712 if (ret == -1)
713 return VK_ERROR_OUT_OF_DATE_KHR;
714
715 /* If a non-special event happens, the fd will still
716 * poll. So recalculate the timeout now just in case.
717 */
718 uint64_t current_time = wsi_get_current_time();
719 if (atimeout > current_time)
720 timeout = atimeout - current_time;
721 else
722 timeout = 0;
723 continue;
724 }
725 }
726
727 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
728 free(event);
729 if (result != VK_SUCCESS)
730 return result;
731 }
732 }
733
734 static VkResult
735 x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
736 uint32_t *image_index_out, uint64_t timeout)
737 {
738 assert(chain->threaded);
739
740 uint32_t image_index;
741 VkResult result = wsi_queue_pull(&chain->acquire_queue,
742 &image_index, timeout);
743 if (result != VK_SUCCESS) {
744 return result;
745 } else if (chain->status != VK_SUCCESS) {
746 return chain->status;
747 }
748
749 assert(image_index < chain->base.image_count);
750 xshmfence_await(chain->images[image_index].shm_fence);
751
752 *image_index_out = image_index;
753
754 return VK_SUCCESS;
755 }
756
757 static VkResult
758 x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
759 uint32_t target_msc)
760 {
761 struct x11_image *image = &chain->images[image_index];
762
763 assert(image_index < chain->base.image_count);
764
765 uint32_t options = XCB_PRESENT_OPTION_NONE;
766
767 int64_t divisor = 0;
768 int64_t remainder = 0;
769
770 if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
771 options |= XCB_PRESENT_OPTION_ASYNC;
772
773 xshmfence_reset(image->shm_fence);
774
775 ++chain->send_sbc;
776 xcb_void_cookie_t cookie =
777 xcb_present_pixmap(chain->conn,
778 chain->window,
779 image->pixmap,
780 (uint32_t) chain->send_sbc,
781 0, /* valid */
782 0, /* update */
783 0, /* x_off */
784 0, /* y_off */
785 XCB_NONE, /* target_crtc */
786 XCB_NONE,
787 image->sync_fence,
788 options,
789 target_msc,
790 divisor,
791 remainder, 0, NULL);
792 xcb_discard_reply(chain->conn, cookie.sequence);
793 image->busy = true;
794
795 xcb_flush(chain->conn);
796
797 return VK_SUCCESS;
798 }
799
800 static VkResult
801 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
802 uint64_t timeout,
803 VkSemaphore semaphore,
804 uint32_t *image_index)
805 {
806 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
807
808 if (chain->threaded) {
809 return x11_acquire_next_image_from_queue(chain, image_index, timeout);
810 } else {
811 return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
812 }
813 }
814
815 static VkResult
816 x11_queue_present(struct wsi_swapchain *anv_chain,
817 uint32_t image_index)
818 {
819 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
820
821 if (chain->threaded) {
822 wsi_queue_push(&chain->present_queue, image_index);
823 return chain->status;
824 } else {
825 return x11_present_to_x11(chain, image_index, 0);
826 }
827 }
828
829 static void *
830 x11_manage_fifo_queues(void *state)
831 {
832 struct x11_swapchain *chain = state;
833 VkResult result;
834
835 assert(chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR);
836
837 while (chain->status == VK_SUCCESS) {
838 /* It should be safe to unconditionally block here. Later in the loop
839 * we blocks until the previous present has landed on-screen. At that
840 * point, we should have received IDLE_NOTIFY on all images presented
841 * before that point so the client should be able to acquire any image
842 * other than the currently presented one.
843 */
844 uint32_t image_index;
845 result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
846 if (result != VK_SUCCESS) {
847 goto fail;
848 } else if (chain->status != VK_SUCCESS) {
849 return NULL;
850 }
851
852 uint64_t target_msc = chain->last_present_msc + 1;
853 result = x11_present_to_x11(chain, image_index, target_msc);
854 if (result != VK_SUCCESS)
855 goto fail;
856
857 while (chain->last_present_msc < target_msc) {
858 xcb_generic_event_t *event =
859 xcb_wait_for_special_event(chain->conn, chain->special_event);
860 if (!event)
861 goto fail;
862
863 result = x11_handle_dri3_present_event(chain, (void *)event);
864 if (result != VK_SUCCESS)
865 goto fail;
866 }
867 }
868
869 fail:
870 chain->status = result;
871 wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
872
873 return NULL;
874 }
875
876 static VkResult
877 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
878 const VkSwapchainCreateInfoKHR *pCreateInfo,
879 const VkAllocationCallbacks* pAllocator,
880 struct x11_image *image)
881 {
882 xcb_void_cookie_t cookie;
883 VkResult result;
884 uint32_t row_pitch;
885 uint32_t offset;
886 uint32_t bpp = 32;
887 int fd;
888 uint32_t size;
889
890 result = chain->base.image_fns->create_wsi_image(device_h,
891 pCreateInfo,
892 pAllocator,
893 &image->image,
894 &image->memory,
895 &size,
896 &offset,
897 &row_pitch,
898 &fd);
899 if (result != VK_SUCCESS)
900 return result;
901
902 image->pixmap = xcb_generate_id(chain->conn);
903
904 cookie =
905 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
906 image->pixmap,
907 chain->window,
908 size,
909 pCreateInfo->imageExtent.width,
910 pCreateInfo->imageExtent.height,
911 row_pitch,
912 chain->depth, bpp, fd);
913 xcb_discard_reply(chain->conn, cookie.sequence);
914
915 int fence_fd = xshmfence_alloc_shm();
916 if (fence_fd < 0)
917 goto fail_pixmap;
918
919 image->shm_fence = xshmfence_map_shm(fence_fd);
920 if (image->shm_fence == NULL)
921 goto fail_shmfence_alloc;
922
923 image->sync_fence = xcb_generate_id(chain->conn);
924 xcb_dri3_fence_from_fd(chain->conn,
925 image->pixmap,
926 image->sync_fence,
927 false,
928 fence_fd);
929
930 image->busy = false;
931 xshmfence_trigger(image->shm_fence);
932
933 return VK_SUCCESS;
934
935 fail_shmfence_alloc:
936 close(fence_fd);
937
938 fail_pixmap:
939 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
940 xcb_discard_reply(chain->conn, cookie.sequence);
941
942 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
943 image->image, image->memory);
944
945 return result;
946 }
947
948 static void
949 x11_image_finish(struct x11_swapchain *chain,
950 const VkAllocationCallbacks* pAllocator,
951 struct x11_image *image)
952 {
953 xcb_void_cookie_t cookie;
954
955 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
956 xcb_discard_reply(chain->conn, cookie.sequence);
957 xshmfence_unmap_shm(image->shm_fence);
958
959 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
960 xcb_discard_reply(chain->conn, cookie.sequence);
961
962 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
963 image->image, image->memory);
964 }
965
966 static VkResult
967 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
968 const VkAllocationCallbacks *pAllocator)
969 {
970 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
971 xcb_void_cookie_t cookie;
972
973 for (uint32_t i = 0; i < chain->base.image_count; i++)
974 x11_image_finish(chain, pAllocator, &chain->images[i]);
975
976 if (chain->threaded) {
977 chain->status = VK_ERROR_OUT_OF_DATE_KHR;
978 /* Push a UINT32_MAX to wake up the manager */
979 wsi_queue_push(&chain->present_queue, UINT32_MAX);
980 pthread_join(chain->queue_manager, NULL);
981 wsi_queue_destroy(&chain->acquire_queue);
982 wsi_queue_destroy(&chain->present_queue);
983 }
984
985 xcb_unregister_for_special_event(chain->conn, chain->special_event);
986 cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
987 chain->window,
988 XCB_PRESENT_EVENT_MASK_NO_EVENT);
989 xcb_discard_reply(chain->conn, cookie.sequence);
990
991 vk_free(pAllocator, chain);
992
993 return VK_SUCCESS;
994 }
995
996 static VkResult
997 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
998 VkDevice device,
999 struct wsi_device *wsi_device,
1000 const VkSwapchainCreateInfoKHR *pCreateInfo,
1001 const VkAllocationCallbacks* pAllocator,
1002 const struct wsi_image_fns *image_fns,
1003 struct wsi_swapchain **swapchain_out)
1004 {
1005 struct x11_swapchain *chain;
1006 xcb_void_cookie_t cookie;
1007 VkResult result;
1008
1009 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1010
1011 const unsigned num_images = pCreateInfo->minImageCount;
1012
1013 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1014 chain = vk_alloc(pAllocator, size, 8,
1015 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1016 if (chain == NULL)
1017 return VK_ERROR_OUT_OF_HOST_MEMORY;
1018
1019 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
1020 xcb_window_t window = x11_surface_get_window(icd_surface);
1021 xcb_get_geometry_reply_t *geometry =
1022 xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
1023
1024 if (geometry == NULL)
1025 return VK_ERROR_SURFACE_LOST_KHR;
1026
1027 chain->base.device = device;
1028 chain->base.destroy = x11_swapchain_destroy;
1029 chain->base.get_images = x11_get_images;
1030 chain->base.acquire_next_image = x11_acquire_next_image;
1031 chain->base.queue_present = x11_queue_present;
1032 chain->base.image_fns = image_fns;
1033 chain->base.present_mode = pCreateInfo->presentMode;
1034 chain->base.image_count = num_images;
1035 chain->conn = conn;
1036 chain->window = window;
1037 chain->depth = geometry->depth;
1038 chain->extent = pCreateInfo->imageExtent;
1039 chain->send_sbc = 0;
1040 chain->last_present_msc = 0;
1041 chain->threaded = false;
1042 chain->status = VK_SUCCESS;
1043
1044 free(geometry);
1045
1046 chain->event_id = xcb_generate_id(chain->conn);
1047 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
1048 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1049 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1050 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1051
1052 /* Create an XCB event queue to hold present events outside of the usual
1053 * application event queue
1054 */
1055 chain->special_event =
1056 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
1057 chain->event_id, NULL);
1058
1059 chain->gc = xcb_generate_id(chain->conn);
1060 if (!chain->gc) {
1061 /* FINISHME: Choose a better error. */
1062 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1063 goto fail_register;
1064 }
1065
1066 cookie = xcb_create_gc(chain->conn,
1067 chain->gc,
1068 chain->window,
1069 XCB_GC_GRAPHICS_EXPOSURES,
1070 (uint32_t []) { 0 });
1071 xcb_discard_reply(chain->conn, cookie.sequence);
1072
1073 uint32_t image = 0;
1074 for (; image < chain->base.image_count; image++) {
1075 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1076 &chain->images[image]);
1077 if (result != VK_SUCCESS)
1078 goto fail_init_images;
1079 }
1080
1081 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
1082 chain->threaded = true;
1083
1084 /* Initialize our queues. We make them base.image_count + 1 because we will
1085 * occasionally use UINT32_MAX to signal the other thread that an error
1086 * has occurred and we don't want an overflow.
1087 */
1088 int ret;
1089 ret = wsi_queue_init(&chain->acquire_queue, chain->base.image_count + 1);
1090 if (ret) {
1091 goto fail_init_images;
1092 }
1093
1094 ret = wsi_queue_init(&chain->present_queue, chain->base.image_count + 1);
1095 if (ret) {
1096 wsi_queue_destroy(&chain->acquire_queue);
1097 goto fail_init_images;
1098 }
1099
1100 for (unsigned i = 0; i < chain->base.image_count; i++)
1101 wsi_queue_push(&chain->acquire_queue, i);
1102
1103 ret = pthread_create(&chain->queue_manager, NULL,
1104 x11_manage_fifo_queues, chain);
1105 if (ret) {
1106 wsi_queue_destroy(&chain->present_queue);
1107 wsi_queue_destroy(&chain->acquire_queue);
1108 goto fail_init_images;
1109 }
1110 }
1111
1112 *swapchain_out = &chain->base;
1113
1114 return VK_SUCCESS;
1115
1116 fail_init_images:
1117 for (uint32_t j = 0; j < image; j++)
1118 x11_image_finish(chain, pAllocator, &chain->images[j]);
1119
1120 fail_register:
1121 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1122
1123 vk_free(pAllocator, chain);
1124
1125 return result;
1126 }
1127
1128 VkResult
1129 wsi_x11_init_wsi(struct wsi_device *wsi_device,
1130 const VkAllocationCallbacks *alloc)
1131 {
1132 struct wsi_x11 *wsi;
1133 VkResult result;
1134
1135 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1136 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1137 if (!wsi) {
1138 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1139 goto fail;
1140 }
1141
1142 int ret = pthread_mutex_init(&wsi->mutex, NULL);
1143 if (ret != 0) {
1144 if (ret == ENOMEM) {
1145 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1146 } else {
1147 /* FINISHME: Choose a better error. */
1148 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1149 }
1150
1151 goto fail_alloc;
1152 }
1153
1154 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1155 _mesa_key_pointer_equal);
1156 if (!wsi->connections) {
1157 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1158 goto fail_mutex;
1159 }
1160
1161 wsi->base.get_support = x11_surface_get_support;
1162 wsi->base.get_capabilities = x11_surface_get_capabilities;
1163 wsi->base.get_formats = x11_surface_get_formats;
1164 wsi->base.get_present_modes = x11_surface_get_present_modes;
1165 wsi->base.create_swapchain = x11_surface_create_swapchain;
1166
1167 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1168 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1169
1170 return VK_SUCCESS;
1171
1172 fail_mutex:
1173 pthread_mutex_destroy(&wsi->mutex);
1174 fail_alloc:
1175 vk_free(alloc, wsi);
1176 fail:
1177 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1178 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1179
1180 return result;
1181 }
1182
1183 void
1184 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1185 const VkAllocationCallbacks *alloc)
1186 {
1187 struct wsi_x11 *wsi =
1188 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1189
1190 if (wsi) {
1191 struct hash_entry *entry;
1192 hash_table_foreach(wsi->connections, entry)
1193 wsi_x11_connection_destroy(alloc, entry->data);
1194
1195 _mesa_hash_table_destroy(wsi->connections, NULL);
1196
1197 pthread_mutex_destroy(&wsi->mutex);
1198
1199 vk_free(alloc, wsi);
1200 }
1201 }