vulkan/wsi/radv: add initial prime support (v1.1)
[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 bool can_handle_different_gpu,
321 xcb_connection_t* connection,
322 xcb_visualid_t visual_id)
323 {
324 struct wsi_x11_connection *wsi_conn =
325 wsi_x11_get_connection(wsi_device, alloc, connection);
326
327 if (!wsi_conn)
328 return false;
329
330 if (!wsi_conn->has_dri3) {
331 fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n");
332 fprintf(stderr, "Note: Buggy applications may crash, if they do please report to vendor\n");
333 return false;
334 }
335
336 if (!can_handle_different_gpu)
337 if (!wsi_x11_check_dri3_compatible(connection, fd))
338 return false;
339
340 unsigned visual_depth;
341 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
342 return false;
343
344 if (visual_depth != 24 && visual_depth != 32)
345 return false;
346
347 return true;
348 }
349
350 static xcb_connection_t*
351 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
352 {
353 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
354 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
355 else
356 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
357 }
358
359 static xcb_window_t
360 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
361 {
362 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
363 return ((VkIcdSurfaceXlib *)icd_surface)->window;
364 else
365 return ((VkIcdSurfaceXcb *)icd_surface)->window;
366 }
367
368 static VkResult
369 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
370 struct wsi_device *wsi_device,
371 const VkAllocationCallbacks *alloc,
372 uint32_t queueFamilyIndex,
373 int local_fd,
374 bool can_handle_different_gpu,
375 VkBool32* pSupported)
376 {
377 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
378 xcb_window_t window = x11_surface_get_window(icd_surface);
379
380 struct wsi_x11_connection *wsi_conn =
381 wsi_x11_get_connection(wsi_device, alloc, conn);
382 if (!wsi_conn)
383 return VK_ERROR_OUT_OF_HOST_MEMORY;
384
385 if (!wsi_conn->has_dri3) {
386 fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n");
387 fprintf(stderr, "Note: Buggy applications may crash, if they do please report to vendor\n");
388 *pSupported = false;
389 return VK_SUCCESS;
390 }
391
392 if (!can_handle_different_gpu)
393 if (!wsi_x11_check_dri3_compatible(conn, local_fd))
394 return false;
395
396 unsigned visual_depth;
397 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
398 *pSupported = false;
399 return VK_SUCCESS;
400 }
401
402 if (visual_depth != 24 && visual_depth != 32) {
403 *pSupported = false;
404 return VK_SUCCESS;
405 }
406
407 *pSupported = true;
408 return VK_SUCCESS;
409 }
410
411 static VkResult
412 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
413 VkSurfaceCapabilitiesKHR *caps)
414 {
415 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
416 xcb_window_t window = x11_surface_get_window(icd_surface);
417 xcb_get_geometry_cookie_t geom_cookie;
418 xcb_generic_error_t *err;
419 xcb_get_geometry_reply_t *geom;
420 unsigned visual_depth;
421
422 geom_cookie = xcb_get_geometry(conn, window);
423
424 /* This does a round-trip. This is why we do get_geometry first and
425 * wait to read the reply until after we have a visual.
426 */
427 xcb_visualtype_t *visual =
428 get_visualtype_for_window(conn, window, &visual_depth);
429
430 if (!visual)
431 return VK_ERROR_SURFACE_LOST_KHR;
432
433 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
434 if (geom) {
435 VkExtent2D extent = { geom->width, geom->height };
436 caps->currentExtent = extent;
437 caps->minImageExtent = extent;
438 caps->maxImageExtent = extent;
439 } else {
440 /* This can happen if the client didn't wait for the configure event
441 * to come back from the compositor. In that case, we don't know the
442 * size of the window so we just return valid "I don't know" stuff.
443 */
444 caps->currentExtent = (VkExtent2D) { -1, -1 };
445 caps->minImageExtent = (VkExtent2D) { 1, 1 };
446 /* This is the maximum supported size on Intel */
447 caps->maxImageExtent = (VkExtent2D) { 1 << 14, 1 << 14 };
448 }
449 free(err);
450 free(geom);
451
452 if (visual_has_alpha(visual, visual_depth)) {
453 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
454 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
455 } else {
456 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
457 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
458 }
459
460 /* For true mailbox mode, we need at least 4 images:
461 * 1) One to scan out from
462 * 2) One to have queued for scan-out
463 * 3) One to be currently held by the X server
464 * 4) One to render to
465 */
466 caps->minImageCount = 2;
467 /* There is no real maximum */
468 caps->maxImageCount = 0;
469
470 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
471 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
472 caps->maxImageArrayLayers = 1;
473 caps->supportedUsageFlags =
474 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
475 VK_IMAGE_USAGE_SAMPLED_BIT |
476 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
477 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
478
479 return VK_SUCCESS;
480 }
481
482 static VkResult
483 x11_surface_get_formats(VkIcdSurfaceBase *surface,
484 struct wsi_device *wsi_device,
485 uint32_t *pSurfaceFormatCount,
486 VkSurfaceFormatKHR *pSurfaceFormats)
487 {
488 if (pSurfaceFormats == NULL) {
489 *pSurfaceFormatCount = ARRAY_SIZE(formats);
490 return VK_SUCCESS;
491 }
492
493 *pSurfaceFormatCount = MIN2(*pSurfaceFormatCount, ARRAY_SIZE(formats));
494 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
495
496 return *pSurfaceFormatCount < ARRAY_SIZE(formats) ?
497 VK_INCOMPLETE : VK_SUCCESS;
498 }
499
500 static VkResult
501 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
502 uint32_t *pPresentModeCount,
503 VkPresentModeKHR *pPresentModes)
504 {
505 if (pPresentModes == NULL) {
506 *pPresentModeCount = ARRAY_SIZE(present_modes);
507 return VK_SUCCESS;
508 }
509
510 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
511 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
512
513 return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
514 VK_INCOMPLETE : VK_SUCCESS;
515 }
516
517 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
518 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
519 VkSurfaceKHR *pSurface)
520 {
521 VkIcdSurfaceXcb *surface;
522
523 surface = vk_alloc(pAllocator, sizeof *surface, 8,
524 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
525 if (surface == NULL)
526 return VK_ERROR_OUT_OF_HOST_MEMORY;
527
528 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
529 surface->connection = pCreateInfo->connection;
530 surface->window = pCreateInfo->window;
531
532 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
533 return VK_SUCCESS;
534 }
535
536 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
537 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
538 VkSurfaceKHR *pSurface)
539 {
540 VkIcdSurfaceXlib *surface;
541
542 surface = vk_alloc(pAllocator, sizeof *surface, 8,
543 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
544 if (surface == NULL)
545 return VK_ERROR_OUT_OF_HOST_MEMORY;
546
547 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
548 surface->dpy = pCreateInfo->dpy;
549 surface->window = pCreateInfo->window;
550
551 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
552 return VK_SUCCESS;
553 }
554
555 struct x11_image {
556 VkImage image;
557 VkImage linear_image; // for prime
558 VkDeviceMemory memory;
559 VkDeviceMemory linear_memory; // for prime
560 xcb_pixmap_t pixmap;
561 bool busy;
562 struct xshmfence * shm_fence;
563 uint32_t sync_fence;
564 };
565
566 struct x11_swapchain {
567 struct wsi_swapchain base;
568
569 xcb_connection_t * conn;
570 xcb_window_t window;
571 xcb_gc_t gc;
572 uint32_t depth;
573 VkExtent2D extent;
574
575 xcb_present_event_t event_id;
576 xcb_special_event_t * special_event;
577 uint64_t send_sbc;
578 uint64_t last_present_msc;
579 uint32_t stamp;
580
581 bool threaded;
582 VkResult status;
583 struct wsi_queue present_queue;
584 struct wsi_queue acquire_queue;
585 pthread_t queue_manager;
586
587 struct x11_image images[0];
588 };
589
590 static VkResult
591 x11_get_images(struct wsi_swapchain *anv_chain,
592 uint32_t* pCount, VkImage *pSwapchainImages)
593 {
594 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
595 uint32_t ret_count;
596 VkResult result;
597
598 if (pSwapchainImages == NULL) {
599 *pCount = chain->base.image_count;
600 return VK_SUCCESS;
601 }
602
603 result = VK_SUCCESS;
604 ret_count = chain->base.image_count;
605 if (chain->base.image_count > *pCount) {
606 ret_count = *pCount;
607 result = VK_INCOMPLETE;
608 }
609
610 for (uint32_t i = 0; i < ret_count; i++)
611 pSwapchainImages[i] = chain->images[i].image;
612
613 return result;
614 }
615
616 static void
617 x11_get_image_and_linear(struct wsi_swapchain *drv_chain,
618 int imageIndex, VkImage *image, VkImage *linear_image)
619 {
620 struct x11_swapchain *chain = (struct x11_swapchain *)drv_chain;
621 *image = chain->images[imageIndex].image;
622 *linear_image = chain->images[imageIndex].linear_image;
623 }
624
625 static VkResult
626 x11_handle_dri3_present_event(struct x11_swapchain *chain,
627 xcb_present_generic_event_t *event)
628 {
629 switch (event->evtype) {
630 case XCB_PRESENT_CONFIGURE_NOTIFY: {
631 xcb_present_configure_notify_event_t *config = (void *) event;
632
633 if (config->width != chain->extent.width ||
634 config->height != chain->extent.height)
635 return VK_ERROR_OUT_OF_DATE_KHR;
636
637 break;
638 }
639
640 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
641 xcb_present_idle_notify_event_t *idle = (void *) event;
642
643 for (unsigned i = 0; i < chain->base.image_count; i++) {
644 if (chain->images[i].pixmap == idle->pixmap) {
645 chain->images[i].busy = false;
646 if (chain->threaded)
647 wsi_queue_push(&chain->acquire_queue, i);
648 break;
649 }
650 }
651
652 break;
653 }
654
655 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
656 xcb_present_complete_notify_event_t *complete = (void *) event;
657 if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
658 chain->last_present_msc = complete->msc;
659 break;
660 }
661
662 default:
663 break;
664 }
665
666 return VK_SUCCESS;
667 }
668
669
670 static uint64_t wsi_get_current_time(void)
671 {
672 uint64_t current_time;
673 struct timespec tv;
674
675 clock_gettime(CLOCK_MONOTONIC, &tv);
676 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
677 return current_time;
678 }
679
680 static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
681 {
682 uint64_t current_time = wsi_get_current_time();
683
684 timeout = MIN2(UINT64_MAX - current_time, timeout);
685
686 return current_time + timeout;
687 }
688
689 static VkResult
690 x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
691 uint32_t *image_index, uint64_t timeout)
692 {
693 xcb_generic_event_t *event;
694 struct pollfd pfds;
695 uint64_t atimeout;
696 while (1) {
697 for (uint32_t i = 0; i < chain->base.image_count; i++) {
698 if (!chain->images[i].busy) {
699 /* We found a non-busy image */
700 xshmfence_await(chain->images[i].shm_fence);
701 *image_index = i;
702 chain->images[i].busy = true;
703 return VK_SUCCESS;
704 }
705 }
706
707 xcb_flush(chain->conn);
708
709 if (timeout == UINT64_MAX) {
710 event = xcb_wait_for_special_event(chain->conn, chain->special_event);
711 if (!event)
712 return VK_ERROR_OUT_OF_DATE_KHR;
713 } else {
714 event = xcb_poll_for_special_event(chain->conn, chain->special_event);
715 if (!event) {
716 int ret;
717 if (timeout == 0)
718 return VK_NOT_READY;
719
720 atimeout = wsi_get_absolute_timeout(timeout);
721
722 pfds.fd = xcb_get_file_descriptor(chain->conn);
723 pfds.events = POLLIN;
724 ret = poll(&pfds, 1, timeout / 1000 / 1000);
725 if (ret == 0)
726 return VK_TIMEOUT;
727 if (ret == -1)
728 return VK_ERROR_OUT_OF_DATE_KHR;
729
730 /* If a non-special event happens, the fd will still
731 * poll. So recalculate the timeout now just in case.
732 */
733 uint64_t current_time = wsi_get_current_time();
734 if (atimeout > current_time)
735 timeout = atimeout - current_time;
736 else
737 timeout = 0;
738 continue;
739 }
740 }
741
742 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
743 free(event);
744 if (result != VK_SUCCESS)
745 return result;
746 }
747 }
748
749 static VkResult
750 x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
751 uint32_t *image_index_out, uint64_t timeout)
752 {
753 assert(chain->threaded);
754
755 uint32_t image_index;
756 VkResult result = wsi_queue_pull(&chain->acquire_queue,
757 &image_index, timeout);
758 if (result != VK_SUCCESS) {
759 return result;
760 } else if (chain->status != VK_SUCCESS) {
761 return chain->status;
762 }
763
764 assert(image_index < chain->base.image_count);
765 xshmfence_await(chain->images[image_index].shm_fence);
766
767 *image_index_out = image_index;
768
769 return VK_SUCCESS;
770 }
771
772 static VkResult
773 x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
774 uint32_t target_msc)
775 {
776 struct x11_image *image = &chain->images[image_index];
777
778 assert(image_index < chain->base.image_count);
779
780 uint32_t options = XCB_PRESENT_OPTION_NONE;
781
782 int64_t divisor = 0;
783 int64_t remainder = 0;
784
785 if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
786 options |= XCB_PRESENT_OPTION_ASYNC;
787
788 xshmfence_reset(image->shm_fence);
789
790 ++chain->send_sbc;
791 xcb_void_cookie_t cookie =
792 xcb_present_pixmap(chain->conn,
793 chain->window,
794 image->pixmap,
795 (uint32_t) chain->send_sbc,
796 0, /* valid */
797 0, /* update */
798 0, /* x_off */
799 0, /* y_off */
800 XCB_NONE, /* target_crtc */
801 XCB_NONE,
802 image->sync_fence,
803 options,
804 target_msc,
805 divisor,
806 remainder, 0, NULL);
807 xcb_discard_reply(chain->conn, cookie.sequence);
808 image->busy = true;
809
810 xcb_flush(chain->conn);
811
812 return VK_SUCCESS;
813 }
814
815 static VkResult
816 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
817 uint64_t timeout,
818 VkSemaphore semaphore,
819 uint32_t *image_index)
820 {
821 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
822
823 if (chain->threaded) {
824 return x11_acquire_next_image_from_queue(chain, image_index, timeout);
825 } else {
826 return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
827 }
828 }
829
830 static VkResult
831 x11_queue_present(struct wsi_swapchain *anv_chain,
832 uint32_t image_index)
833 {
834 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
835
836 if (chain->threaded) {
837 wsi_queue_push(&chain->present_queue, image_index);
838 return chain->status;
839 } else {
840 return x11_present_to_x11(chain, image_index, 0);
841 }
842 }
843
844 static void *
845 x11_manage_fifo_queues(void *state)
846 {
847 struct x11_swapchain *chain = state;
848 VkResult result;
849
850 assert(chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR);
851
852 while (chain->status == VK_SUCCESS) {
853 /* It should be safe to unconditionally block here. Later in the loop
854 * we blocks until the previous present has landed on-screen. At that
855 * point, we should have received IDLE_NOTIFY on all images presented
856 * before that point so the client should be able to acquire any image
857 * other than the currently presented one.
858 */
859 uint32_t image_index;
860 result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
861 if (result != VK_SUCCESS) {
862 goto fail;
863 } else if (chain->status != VK_SUCCESS) {
864 return NULL;
865 }
866
867 uint64_t target_msc = chain->last_present_msc + 1;
868 result = x11_present_to_x11(chain, image_index, target_msc);
869 if (result != VK_SUCCESS)
870 goto fail;
871
872 while (chain->last_present_msc < target_msc) {
873 xcb_generic_event_t *event =
874 xcb_wait_for_special_event(chain->conn, chain->special_event);
875 if (!event)
876 goto fail;
877
878 result = x11_handle_dri3_present_event(chain, (void *)event);
879 if (result != VK_SUCCESS)
880 goto fail;
881 }
882 }
883
884 fail:
885 chain->status = result;
886 wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
887
888 return NULL;
889 }
890
891 static VkResult
892 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
893 const VkSwapchainCreateInfoKHR *pCreateInfo,
894 const VkAllocationCallbacks* pAllocator,
895 struct x11_image *image)
896 {
897 xcb_void_cookie_t cookie;
898 VkResult result;
899 uint32_t row_pitch;
900 uint32_t offset;
901 uint32_t bpp = 32;
902 int fd;
903 uint32_t size;
904
905 result = chain->base.image_fns->create_wsi_image(device_h,
906 pCreateInfo,
907 pAllocator,
908 chain->base.needs_linear_copy,
909 false,
910 &image->image,
911 &image->memory,
912 &size,
913 &offset,
914 &row_pitch,
915 &fd);
916 if (result != VK_SUCCESS)
917 return result;
918
919 if (chain->base.needs_linear_copy) {
920 result = chain->base.image_fns->create_wsi_image(device_h,
921 pCreateInfo,
922 pAllocator,
923 chain->base.needs_linear_copy,
924 true,
925 &image->linear_image,
926 &image->linear_memory,
927 &size,
928 &offset,
929 &row_pitch,
930 &fd);
931 if (result != VK_SUCCESS) {
932 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
933 image->image, image->memory);
934 return result;
935 }
936 }
937
938 image->pixmap = xcb_generate_id(chain->conn);
939
940 cookie =
941 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
942 image->pixmap,
943 chain->window,
944 size,
945 pCreateInfo->imageExtent.width,
946 pCreateInfo->imageExtent.height,
947 row_pitch,
948 chain->depth, bpp, fd);
949 xcb_discard_reply(chain->conn, cookie.sequence);
950
951 int fence_fd = xshmfence_alloc_shm();
952 if (fence_fd < 0)
953 goto fail_pixmap;
954
955 image->shm_fence = xshmfence_map_shm(fence_fd);
956 if (image->shm_fence == NULL)
957 goto fail_shmfence_alloc;
958
959 image->sync_fence = xcb_generate_id(chain->conn);
960 xcb_dri3_fence_from_fd(chain->conn,
961 image->pixmap,
962 image->sync_fence,
963 false,
964 fence_fd);
965
966 image->busy = false;
967 xshmfence_trigger(image->shm_fence);
968
969 return VK_SUCCESS;
970
971 fail_shmfence_alloc:
972 close(fence_fd);
973
974 fail_pixmap:
975 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
976 xcb_discard_reply(chain->conn, cookie.sequence);
977
978 if (chain->base.needs_linear_copy) {
979 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
980 image->linear_image, image->linear_memory);
981 }
982 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
983 image->image, image->memory);
984
985 return result;
986 }
987
988 static void
989 x11_image_finish(struct x11_swapchain *chain,
990 const VkAllocationCallbacks* pAllocator,
991 struct x11_image *image)
992 {
993 xcb_void_cookie_t cookie;
994
995 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
996 xcb_discard_reply(chain->conn, cookie.sequence);
997 xshmfence_unmap_shm(image->shm_fence);
998
999 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
1000 xcb_discard_reply(chain->conn, cookie.sequence);
1001
1002 if (chain->base.needs_linear_copy) {
1003 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
1004 image->linear_image, image->linear_memory);
1005 }
1006 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
1007 image->image, image->memory);
1008 }
1009
1010 static VkResult
1011 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
1012 const VkAllocationCallbacks *pAllocator)
1013 {
1014 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1015 xcb_void_cookie_t cookie;
1016
1017 for (uint32_t i = 0; i < chain->base.image_count; i++)
1018 x11_image_finish(chain, pAllocator, &chain->images[i]);
1019
1020 if (chain->threaded) {
1021 chain->status = VK_ERROR_OUT_OF_DATE_KHR;
1022 /* Push a UINT32_MAX to wake up the manager */
1023 wsi_queue_push(&chain->present_queue, UINT32_MAX);
1024 pthread_join(chain->queue_manager, NULL);
1025 wsi_queue_destroy(&chain->acquire_queue);
1026 wsi_queue_destroy(&chain->present_queue);
1027 }
1028
1029 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1030 cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
1031 chain->window,
1032 XCB_PRESENT_EVENT_MASK_NO_EVENT);
1033 xcb_discard_reply(chain->conn, cookie.sequence);
1034
1035 vk_free(pAllocator, chain);
1036
1037 return VK_SUCCESS;
1038 }
1039
1040 static VkResult
1041 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
1042 VkDevice device,
1043 struct wsi_device *wsi_device,
1044 int local_fd,
1045 const VkSwapchainCreateInfoKHR *pCreateInfo,
1046 const VkAllocationCallbacks* pAllocator,
1047 const struct wsi_image_fns *image_fns,
1048 struct wsi_swapchain **swapchain_out)
1049 {
1050 struct x11_swapchain *chain;
1051 xcb_void_cookie_t cookie;
1052 VkResult result;
1053
1054 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1055
1056 const unsigned num_images = pCreateInfo->minImageCount;
1057
1058 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1059 chain = vk_alloc(pAllocator, size, 8,
1060 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1061 if (chain == NULL)
1062 return VK_ERROR_OUT_OF_HOST_MEMORY;
1063
1064 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
1065 xcb_window_t window = x11_surface_get_window(icd_surface);
1066 xcb_get_geometry_reply_t *geometry =
1067 xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
1068
1069 if (geometry == NULL)
1070 return VK_ERROR_SURFACE_LOST_KHR;
1071
1072 chain->base.device = device;
1073 chain->base.destroy = x11_swapchain_destroy;
1074 chain->base.get_images = x11_get_images;
1075 chain->base.get_image_and_linear = x11_get_image_and_linear;
1076 chain->base.acquire_next_image = x11_acquire_next_image;
1077 chain->base.queue_present = x11_queue_present;
1078 chain->base.image_fns = image_fns;
1079 chain->base.present_mode = pCreateInfo->presentMode;
1080 chain->base.image_count = num_images;
1081 chain->conn = conn;
1082 chain->window = window;
1083 chain->depth = geometry->depth;
1084 chain->extent = pCreateInfo->imageExtent;
1085 chain->send_sbc = 0;
1086 chain->last_present_msc = 0;
1087 chain->threaded = false;
1088 chain->status = VK_SUCCESS;
1089
1090 free(geometry);
1091
1092 chain->base.needs_linear_copy = false;
1093 if (!wsi_x11_check_dri3_compatible(conn, local_fd))
1094 chain->base.needs_linear_copy = true;
1095
1096 chain->event_id = xcb_generate_id(chain->conn);
1097 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
1098 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1099 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1100 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1101
1102 /* Create an XCB event queue to hold present events outside of the usual
1103 * application event queue
1104 */
1105 chain->special_event =
1106 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
1107 chain->event_id, NULL);
1108
1109 chain->gc = xcb_generate_id(chain->conn);
1110 if (!chain->gc) {
1111 /* FINISHME: Choose a better error. */
1112 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1113 goto fail_register;
1114 }
1115
1116 cookie = xcb_create_gc(chain->conn,
1117 chain->gc,
1118 chain->window,
1119 XCB_GC_GRAPHICS_EXPOSURES,
1120 (uint32_t []) { 0 });
1121 xcb_discard_reply(chain->conn, cookie.sequence);
1122
1123 uint32_t image = 0;
1124 for (; image < chain->base.image_count; image++) {
1125 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1126 &chain->images[image]);
1127 if (result != VK_SUCCESS)
1128 goto fail_init_images;
1129 }
1130
1131 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
1132 chain->threaded = true;
1133
1134 /* Initialize our queues. We make them base.image_count + 1 because we will
1135 * occasionally use UINT32_MAX to signal the other thread that an error
1136 * has occurred and we don't want an overflow.
1137 */
1138 int ret;
1139 ret = wsi_queue_init(&chain->acquire_queue, chain->base.image_count + 1);
1140 if (ret) {
1141 goto fail_init_images;
1142 }
1143
1144 ret = wsi_queue_init(&chain->present_queue, chain->base.image_count + 1);
1145 if (ret) {
1146 wsi_queue_destroy(&chain->acquire_queue);
1147 goto fail_init_images;
1148 }
1149
1150 for (unsigned i = 0; i < chain->base.image_count; i++)
1151 wsi_queue_push(&chain->acquire_queue, i);
1152
1153 ret = pthread_create(&chain->queue_manager, NULL,
1154 x11_manage_fifo_queues, chain);
1155 if (ret) {
1156 wsi_queue_destroy(&chain->present_queue);
1157 wsi_queue_destroy(&chain->acquire_queue);
1158 goto fail_init_images;
1159 }
1160 }
1161
1162 *swapchain_out = &chain->base;
1163
1164 return VK_SUCCESS;
1165
1166 fail_init_images:
1167 for (uint32_t j = 0; j < image; j++)
1168 x11_image_finish(chain, pAllocator, &chain->images[j]);
1169
1170 fail_register:
1171 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1172
1173 vk_free(pAllocator, chain);
1174
1175 return result;
1176 }
1177
1178 VkResult
1179 wsi_x11_init_wsi(struct wsi_device *wsi_device,
1180 const VkAllocationCallbacks *alloc)
1181 {
1182 struct wsi_x11 *wsi;
1183 VkResult result;
1184
1185 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1186 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1187 if (!wsi) {
1188 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1189 goto fail;
1190 }
1191
1192 int ret = pthread_mutex_init(&wsi->mutex, NULL);
1193 if (ret != 0) {
1194 if (ret == ENOMEM) {
1195 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1196 } else {
1197 /* FINISHME: Choose a better error. */
1198 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1199 }
1200
1201 goto fail_alloc;
1202 }
1203
1204 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1205 _mesa_key_pointer_equal);
1206 if (!wsi->connections) {
1207 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1208 goto fail_mutex;
1209 }
1210
1211 wsi->base.get_support = x11_surface_get_support;
1212 wsi->base.get_capabilities = x11_surface_get_capabilities;
1213 wsi->base.get_formats = x11_surface_get_formats;
1214 wsi->base.get_present_modes = x11_surface_get_present_modes;
1215 wsi->base.create_swapchain = x11_surface_create_swapchain;
1216
1217 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1218 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1219
1220 return VK_SUCCESS;
1221
1222 fail_mutex:
1223 pthread_mutex_destroy(&wsi->mutex);
1224 fail_alloc:
1225 vk_free(alloc, wsi);
1226 fail:
1227 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1228 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1229
1230 return result;
1231 }
1232
1233 void
1234 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1235 const VkAllocationCallbacks *alloc)
1236 {
1237 struct wsi_x11 *wsi =
1238 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1239
1240 if (wsi) {
1241 struct hash_entry *entry;
1242 hash_table_foreach(wsi->connections, entry)
1243 wsi_x11_connection_destroy(alloc, entry->data);
1244
1245 _mesa_hash_table_destroy(wsi->connections, NULL);
1246
1247 pthread_mutex_destroy(&wsi->mutex);
1248
1249 vk_free(alloc, wsi);
1250 }
1251 }