c87b9312636223789fa9779d200ab69e58807a15
[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 "drm-uapi/drm_fourcc.h"
40 #include "util/hash_table.h"
41
42 #include "vk_util.h"
43 #include "wsi_common_private.h"
44 #include "wsi_common_x11.h"
45 #include "wsi_common_queue.h"
46
47 #define typed_memcpy(dest, src, count) ({ \
48 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
49 memcpy((dest), (src), (count) * sizeof(*(src))); \
50 })
51
52 struct wsi_x11_connection {
53 bool has_dri3;
54 bool has_dri3_modifiers;
55 bool has_present;
56 bool is_proprietary_x11;
57 };
58
59 struct wsi_x11 {
60 struct wsi_interface base;
61
62 pthread_mutex_t mutex;
63 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
64 struct hash_table *connections;
65 };
66
67
68 /** wsi_dri3_open
69 *
70 * Wrapper around xcb_dri3_open
71 */
72 static int
73 wsi_dri3_open(xcb_connection_t *conn,
74 xcb_window_t root,
75 uint32_t provider)
76 {
77 xcb_dri3_open_cookie_t cookie;
78 xcb_dri3_open_reply_t *reply;
79 int fd;
80
81 cookie = xcb_dri3_open(conn,
82 root,
83 provider);
84
85 reply = xcb_dri3_open_reply(conn, cookie, NULL);
86 if (!reply)
87 return -1;
88
89 if (reply->nfd != 1) {
90 free(reply);
91 return -1;
92 }
93
94 fd = xcb_dri3_open_reply_fds(conn, reply)[0];
95 free(reply);
96 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
97
98 return fd;
99 }
100
101 static bool
102 wsi_x11_check_dri3_compatible(const struct wsi_device *wsi_dev,
103 xcb_connection_t *conn)
104 {
105 xcb_screen_iterator_t screen_iter =
106 xcb_setup_roots_iterator(xcb_get_setup(conn));
107 xcb_screen_t *screen = screen_iter.data;
108
109 int dri3_fd = wsi_dri3_open(conn, screen->root, None);
110 if (dri3_fd == -1)
111 return true;
112
113 bool match = wsi_device_matches_drm_fd(wsi_dev, dri3_fd);
114
115 close(dri3_fd);
116
117 return match;
118 }
119
120 static struct wsi_x11_connection *
121 wsi_x11_connection_create(struct wsi_device *wsi_dev,
122 xcb_connection_t *conn)
123 {
124 xcb_query_extension_cookie_t dri3_cookie, pres_cookie, amd_cookie, nv_cookie;
125 xcb_query_extension_reply_t *dri3_reply, *pres_reply, *amd_reply, *nv_reply;
126 bool has_dri3_v1_2 = false;
127 bool has_present_v1_2 = false;
128
129 struct wsi_x11_connection *wsi_conn =
130 vk_alloc(&wsi_dev->instance_alloc, sizeof(*wsi_conn), 8,
131 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
132 if (!wsi_conn)
133 return NULL;
134
135 dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
136 pres_cookie = xcb_query_extension(conn, 7, "Present");
137
138 /* We try to be nice to users and emit a warning if they try to use a
139 * Vulkan application on a system without DRI3 enabled. However, this ends
140 * up spewing the warning when a user has, for example, both Intel
141 * integrated graphics and a discrete card with proprietary drivers and are
142 * running on the discrete card with the proprietary DDX. In this case, we
143 * really don't want to print the warning because it just confuses users.
144 * As a heuristic to detect this case, we check for a couple of proprietary
145 * X11 extensions.
146 */
147 amd_cookie = xcb_query_extension(conn, 11, "ATIFGLRXDRI");
148 nv_cookie = xcb_query_extension(conn, 10, "NV-CONTROL");
149
150 dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
151 pres_reply = xcb_query_extension_reply(conn, pres_cookie, NULL);
152 amd_reply = xcb_query_extension_reply(conn, amd_cookie, NULL);
153 nv_reply = xcb_query_extension_reply(conn, nv_cookie, NULL);
154 if (!dri3_reply || !pres_reply) {
155 free(dri3_reply);
156 free(pres_reply);
157 free(amd_reply);
158 free(nv_reply);
159 vk_free(&wsi_dev->instance_alloc, wsi_conn);
160 return NULL;
161 }
162
163 wsi_conn->has_dri3 = dri3_reply->present != 0;
164 #ifdef HAVE_DRI3_MODIFIERS
165 if (wsi_conn->has_dri3) {
166 xcb_dri3_query_version_cookie_t ver_cookie;
167 xcb_dri3_query_version_reply_t *ver_reply;
168
169 ver_cookie = xcb_dri3_query_version(conn, 1, 2);
170 ver_reply = xcb_dri3_query_version_reply(conn, ver_cookie, NULL);
171 has_dri3_v1_2 =
172 (ver_reply->major_version > 1 || ver_reply->minor_version >= 2);
173 free(ver_reply);
174 }
175 #endif
176
177 wsi_conn->has_present = pres_reply->present != 0;
178 #ifdef HAVE_DRI3_MODIFIERS
179 if (wsi_conn->has_present) {
180 xcb_present_query_version_cookie_t ver_cookie;
181 xcb_present_query_version_reply_t *ver_reply;
182
183 ver_cookie = xcb_present_query_version(conn, 1, 2);
184 ver_reply = xcb_present_query_version_reply(conn, ver_cookie, NULL);
185 has_present_v1_2 =
186 (ver_reply->major_version > 1 || ver_reply->minor_version >= 2);
187 free(ver_reply);
188 }
189 #endif
190
191 wsi_conn->has_dri3_modifiers = has_dri3_v1_2 && has_present_v1_2;
192 wsi_conn->is_proprietary_x11 = false;
193 if (amd_reply && amd_reply->present)
194 wsi_conn->is_proprietary_x11 = true;
195 if (nv_reply && nv_reply->present)
196 wsi_conn->is_proprietary_x11 = true;
197
198 free(dri3_reply);
199 free(pres_reply);
200 free(amd_reply);
201 free(nv_reply);
202
203 return wsi_conn;
204 }
205
206 static void
207 wsi_x11_connection_destroy(struct wsi_device *wsi_dev,
208 struct wsi_x11_connection *conn)
209 {
210 vk_free(&wsi_dev->instance_alloc, conn);
211 }
212
213 static bool
214 wsi_x11_check_for_dri3(struct wsi_x11_connection *wsi_conn)
215 {
216 if (wsi_conn->has_dri3)
217 return true;
218 if (!wsi_conn->is_proprietary_x11) {
219 fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n"
220 "Note: you can probably enable DRI3 in your Xorg config\n");
221 }
222 return false;
223 }
224
225 static struct wsi_x11_connection *
226 wsi_x11_get_connection(struct wsi_device *wsi_dev,
227 xcb_connection_t *conn)
228 {
229 struct wsi_x11 *wsi =
230 (struct wsi_x11 *)wsi_dev->wsi[VK_ICD_WSI_PLATFORM_XCB];
231
232 pthread_mutex_lock(&wsi->mutex);
233
234 struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
235 if (!entry) {
236 /* We're about to make a bunch of blocking calls. Let's drop the
237 * mutex for now so we don't block up too badly.
238 */
239 pthread_mutex_unlock(&wsi->mutex);
240
241 struct wsi_x11_connection *wsi_conn =
242 wsi_x11_connection_create(wsi_dev, conn);
243 if (!wsi_conn)
244 return NULL;
245
246 pthread_mutex_lock(&wsi->mutex);
247
248 entry = _mesa_hash_table_search(wsi->connections, conn);
249 if (entry) {
250 /* Oops, someone raced us to it */
251 wsi_x11_connection_destroy(wsi_dev, wsi_conn);
252 } else {
253 entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
254 }
255 }
256
257 pthread_mutex_unlock(&wsi->mutex);
258
259 return entry->data;
260 }
261
262 static const VkFormat formats[] = {
263 VK_FORMAT_B8G8R8A8_SRGB,
264 VK_FORMAT_B8G8R8A8_UNORM,
265 };
266
267 static const VkPresentModeKHR present_modes[] = {
268 VK_PRESENT_MODE_IMMEDIATE_KHR,
269 VK_PRESENT_MODE_MAILBOX_KHR,
270 VK_PRESENT_MODE_FIFO_KHR,
271 };
272
273 static xcb_screen_t *
274 get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
275 {
276 xcb_screen_iterator_t screen_iter =
277 xcb_setup_roots_iterator(xcb_get_setup(conn));
278
279 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
280 if (screen_iter.data->root == root)
281 return screen_iter.data;
282 }
283
284 return NULL;
285 }
286
287 static xcb_visualtype_t *
288 screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
289 unsigned *depth)
290 {
291 xcb_depth_iterator_t depth_iter =
292 xcb_screen_allowed_depths_iterator(screen);
293
294 for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
295 xcb_visualtype_iterator_t visual_iter =
296 xcb_depth_visuals_iterator (depth_iter.data);
297
298 for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
299 if (visual_iter.data->visual_id == visual_id) {
300 if (depth)
301 *depth = depth_iter.data->depth;
302 return visual_iter.data;
303 }
304 }
305 }
306
307 return NULL;
308 }
309
310 static xcb_visualtype_t *
311 connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id,
312 unsigned *depth)
313 {
314 xcb_screen_iterator_t screen_iter =
315 xcb_setup_roots_iterator(xcb_get_setup(conn));
316
317 /* For this we have to iterate over all of the screens which is rather
318 * annoying. Fortunately, there is probably only 1.
319 */
320 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
321 xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
322 visual_id, depth);
323 if (visual)
324 return visual;
325 }
326
327 return NULL;
328 }
329
330 static xcb_visualtype_t *
331 get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
332 unsigned *depth)
333 {
334 xcb_query_tree_cookie_t tree_cookie;
335 xcb_get_window_attributes_cookie_t attrib_cookie;
336 xcb_query_tree_reply_t *tree;
337 xcb_get_window_attributes_reply_t *attrib;
338
339 tree_cookie = xcb_query_tree(conn, window);
340 attrib_cookie = xcb_get_window_attributes(conn, window);
341
342 tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
343 attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
344 if (attrib == NULL || tree == NULL) {
345 free(attrib);
346 free(tree);
347 return NULL;
348 }
349
350 xcb_window_t root = tree->root;
351 xcb_visualid_t visual_id = attrib->visual;
352 free(attrib);
353 free(tree);
354
355 xcb_screen_t *screen = get_screen_for_root(conn, root);
356 if (screen == NULL)
357 return NULL;
358
359 return screen_get_visualtype(screen, visual_id, depth);
360 }
361
362 static bool
363 visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
364 {
365 uint32_t rgb_mask = visual->red_mask |
366 visual->green_mask |
367 visual->blue_mask;
368
369 uint32_t all_mask = 0xffffffff >> (32 - depth);
370
371 /* Do we have bits left over after RGB? */
372 return (all_mask & ~rgb_mask) != 0;
373 }
374
375 VkBool32 wsi_get_physical_device_xcb_presentation_support(
376 struct wsi_device *wsi_device,
377 uint32_t queueFamilyIndex,
378 xcb_connection_t* connection,
379 xcb_visualid_t visual_id)
380 {
381 struct wsi_x11_connection *wsi_conn =
382 wsi_x11_get_connection(wsi_device, connection);
383
384 if (!wsi_conn)
385 return false;
386
387 if (!wsi_x11_check_for_dri3(wsi_conn))
388 return false;
389
390 unsigned visual_depth;
391 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
392 return false;
393
394 if (visual_depth != 24 && visual_depth != 32)
395 return false;
396
397 return true;
398 }
399
400 static xcb_connection_t*
401 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
402 {
403 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
404 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
405 else
406 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
407 }
408
409 static xcb_window_t
410 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
411 {
412 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
413 return ((VkIcdSurfaceXlib *)icd_surface)->window;
414 else
415 return ((VkIcdSurfaceXcb *)icd_surface)->window;
416 }
417
418 static VkResult
419 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
420 struct wsi_device *wsi_device,
421 uint32_t queueFamilyIndex,
422 VkBool32* pSupported)
423 {
424 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
425 xcb_window_t window = x11_surface_get_window(icd_surface);
426
427 struct wsi_x11_connection *wsi_conn =
428 wsi_x11_get_connection(wsi_device, conn);
429 if (!wsi_conn)
430 return VK_ERROR_OUT_OF_HOST_MEMORY;
431
432 if (!wsi_x11_check_for_dri3(wsi_conn)) {
433 *pSupported = false;
434 return VK_SUCCESS;
435 }
436
437 unsigned visual_depth;
438 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
439 *pSupported = false;
440 return VK_SUCCESS;
441 }
442
443 if (visual_depth != 24 && visual_depth != 32) {
444 *pSupported = false;
445 return VK_SUCCESS;
446 }
447
448 *pSupported = true;
449 return VK_SUCCESS;
450 }
451
452 static VkResult
453 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
454 struct wsi_device *wsi_device,
455 VkSurfaceCapabilitiesKHR *caps)
456 {
457 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
458 xcb_window_t window = x11_surface_get_window(icd_surface);
459 xcb_get_geometry_cookie_t geom_cookie;
460 xcb_generic_error_t *err;
461 xcb_get_geometry_reply_t *geom;
462 unsigned visual_depth;
463
464 geom_cookie = xcb_get_geometry(conn, window);
465
466 /* This does a round-trip. This is why we do get_geometry first and
467 * wait to read the reply until after we have a visual.
468 */
469 xcb_visualtype_t *visual =
470 get_visualtype_for_window(conn, window, &visual_depth);
471
472 if (!visual)
473 return VK_ERROR_SURFACE_LOST_KHR;
474
475 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
476 if (geom) {
477 VkExtent2D extent = { geom->width, geom->height };
478 caps->currentExtent = extent;
479 caps->minImageExtent = extent;
480 caps->maxImageExtent = extent;
481 } else {
482 /* This can happen if the client didn't wait for the configure event
483 * to come back from the compositor. In that case, we don't know the
484 * size of the window so we just return valid "I don't know" stuff.
485 */
486 caps->currentExtent = (VkExtent2D) { -1, -1 };
487 caps->minImageExtent = (VkExtent2D) { 1, 1 };
488 caps->maxImageExtent = (VkExtent2D) {
489 wsi_device->maxImageDimension2D,
490 wsi_device->maxImageDimension2D,
491 };
492 }
493 free(err);
494 free(geom);
495
496 if (visual_has_alpha(visual, visual_depth)) {
497 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
498 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
499 } else {
500 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
501 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
502 }
503
504 /* For true mailbox mode, we need at least 4 images:
505 * 1) One to scan out from
506 * 2) One to have queued for scan-out
507 * 3) One to be currently held by the X server
508 * 4) One to render to
509 */
510 caps->minImageCount = 2;
511 /* There is no real maximum */
512 caps->maxImageCount = 0;
513
514 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
515 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
516 caps->maxImageArrayLayers = 1;
517 caps->supportedUsageFlags =
518 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
519 VK_IMAGE_USAGE_SAMPLED_BIT |
520 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
521 VK_IMAGE_USAGE_STORAGE_BIT |
522 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
523
524 return VK_SUCCESS;
525 }
526
527 static VkResult
528 x11_surface_get_capabilities2(VkIcdSurfaceBase *icd_surface,
529 struct wsi_device *wsi_device,
530 const void *info_next,
531 VkSurfaceCapabilities2KHR *caps)
532 {
533 assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
534
535 VkResult result =
536 x11_surface_get_capabilities(icd_surface, wsi_device,
537 &caps->surfaceCapabilities);
538
539 vk_foreach_struct(ext, caps->pNext) {
540 switch (ext->sType) {
541 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
542 VkSurfaceProtectedCapabilitiesKHR *protected = (void *)ext;
543 protected->supportsProtected = VK_FALSE;
544 break;
545 }
546
547 default:
548 /* Ignored */
549 break;
550 }
551 }
552
553 return result;
554 }
555
556 static VkResult
557 x11_surface_get_formats(VkIcdSurfaceBase *surface,
558 struct wsi_device *wsi_device,
559 uint32_t *pSurfaceFormatCount,
560 VkSurfaceFormatKHR *pSurfaceFormats)
561 {
562 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
563
564 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
565 vk_outarray_append(&out, f) {
566 f->format = formats[i];
567 f->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
568 }
569 }
570
571 return vk_outarray_status(&out);
572 }
573
574 static VkResult
575 x11_surface_get_formats2(VkIcdSurfaceBase *surface,
576 struct wsi_device *wsi_device,
577 const void *info_next,
578 uint32_t *pSurfaceFormatCount,
579 VkSurfaceFormat2KHR *pSurfaceFormats)
580 {
581 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
582
583 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
584 vk_outarray_append(&out, f) {
585 assert(f->sType == VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR);
586 f->surfaceFormat.format = formats[i];
587 f->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
588 }
589 }
590
591 return vk_outarray_status(&out);
592 }
593
594 static VkResult
595 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
596 uint32_t *pPresentModeCount,
597 VkPresentModeKHR *pPresentModes)
598 {
599 if (pPresentModes == NULL) {
600 *pPresentModeCount = ARRAY_SIZE(present_modes);
601 return VK_SUCCESS;
602 }
603
604 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
605 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
606
607 return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
608 VK_INCOMPLETE : VK_SUCCESS;
609 }
610
611 static bool
612 x11_surface_is_local_to_gpu(struct wsi_device *wsi_dev,
613 xcb_connection_t *conn)
614 {
615 struct wsi_x11_connection *wsi_conn =
616 wsi_x11_get_connection(wsi_dev, conn);
617
618 if (!wsi_conn)
619 return false;
620
621 if (!wsi_x11_check_for_dri3(wsi_conn))
622 return false;
623
624 if (!wsi_x11_check_dri3_compatible(wsi_dev, conn))
625 return false;
626
627 return true;
628 }
629
630 static VkResult
631 x11_surface_get_present_rectangles(VkIcdSurfaceBase *icd_surface,
632 struct wsi_device *wsi_device,
633 uint32_t* pRectCount,
634 VkRect2D* pRects)
635 {
636 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
637 xcb_window_t window = x11_surface_get_window(icd_surface);
638 VK_OUTARRAY_MAKE(out, pRects, pRectCount);
639
640 if (x11_surface_is_local_to_gpu(wsi_device, conn)) {
641 vk_outarray_append(&out, rect) {
642 xcb_generic_error_t *err = NULL;
643 xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, window);
644 xcb_get_geometry_reply_t *geom =
645 xcb_get_geometry_reply(conn, geom_cookie, &err);
646 free(err);
647 if (geom) {
648 *rect = (VkRect2D) {
649 .offset = { 0, 0 },
650 .extent = { geom->width, geom->height },
651 };
652 } else {
653 /* This can happen if the client didn't wait for the configure event
654 * to come back from the compositor. In that case, we don't know the
655 * size of the window so we just return valid "I don't know" stuff.
656 */
657 *rect = (VkRect2D) {
658 .offset = { 0, 0 },
659 .extent = { -1, -1 },
660 };
661 }
662 free(geom);
663 }
664 }
665
666 return vk_outarray_status(&out);
667 }
668
669 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
670 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
671 VkSurfaceKHR *pSurface)
672 {
673 VkIcdSurfaceXcb *surface;
674
675 surface = vk_alloc(pAllocator, sizeof *surface, 8,
676 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
677 if (surface == NULL)
678 return VK_ERROR_OUT_OF_HOST_MEMORY;
679
680 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
681 surface->connection = pCreateInfo->connection;
682 surface->window = pCreateInfo->window;
683
684 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
685 return VK_SUCCESS;
686 }
687
688 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
689 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
690 VkSurfaceKHR *pSurface)
691 {
692 VkIcdSurfaceXlib *surface;
693
694 surface = vk_alloc(pAllocator, sizeof *surface, 8,
695 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
696 if (surface == NULL)
697 return VK_ERROR_OUT_OF_HOST_MEMORY;
698
699 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
700 surface->dpy = pCreateInfo->dpy;
701 surface->window = pCreateInfo->window;
702
703 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
704 return VK_SUCCESS;
705 }
706
707 struct x11_image {
708 struct wsi_image base;
709 xcb_pixmap_t pixmap;
710 bool busy;
711 struct xshmfence * shm_fence;
712 uint32_t sync_fence;
713 };
714
715 struct x11_swapchain {
716 struct wsi_swapchain base;
717
718 bool has_dri3_modifiers;
719
720 xcb_connection_t * conn;
721 xcb_window_t window;
722 xcb_gc_t gc;
723 uint32_t depth;
724 VkExtent2D extent;
725
726 xcb_present_event_t event_id;
727 xcb_special_event_t * special_event;
728 uint64_t send_sbc;
729 uint64_t last_present_msc;
730 uint32_t stamp;
731
732 bool threaded;
733 VkResult status;
734 xcb_present_complete_mode_t last_present_mode;
735 struct wsi_queue present_queue;
736 struct wsi_queue acquire_queue;
737 pthread_t queue_manager;
738
739 struct x11_image images[0];
740 };
741 WSI_DEFINE_NONDISP_HANDLE_CASTS(x11_swapchain, VkSwapchainKHR)
742
743 /**
744 * Update the swapchain status with the result of an operation, and return
745 * the combined status. The chain status will eventually be returned from
746 * AcquireNextImage and QueuePresent.
747 *
748 * We make sure to 'stick' more pessimistic statuses: an out-of-date error
749 * is permanent once seen, and every subsequent call will return this. If
750 * this has not been seen, success will be returned.
751 */
752 static VkResult
753 x11_swapchain_result(struct x11_swapchain *chain, VkResult result)
754 {
755 /* Prioritise returning existing errors for consistency. */
756 if (chain->status < 0)
757 return chain->status;
758
759 /* If we have a new error, mark it as permanent on the chain and return. */
760 if (result < 0) {
761 chain->status = result;
762 return result;
763 }
764
765 /* Return temporary errors, but don't persist them. */
766 if (result == VK_TIMEOUT || result == VK_NOT_READY)
767 return result;
768
769 /* Suboptimal isn't an error, but is a status which sticks to the swapchain
770 * and is always returned rather than success.
771 */
772 if (result == VK_SUBOPTIMAL_KHR) {
773 chain->status = result;
774 return result;
775 }
776
777 /* No changes, so return the last status. */
778 return chain->status;
779 }
780
781 static struct wsi_image *
782 x11_get_wsi_image(struct wsi_swapchain *wsi_chain, uint32_t image_index)
783 {
784 struct x11_swapchain *chain = (struct x11_swapchain *)wsi_chain;
785 return &chain->images[image_index].base;
786 }
787
788 /**
789 * Process an X11 Present event. Does not update chain->status.
790 */
791 static VkResult
792 x11_handle_dri3_present_event(struct x11_swapchain *chain,
793 xcb_present_generic_event_t *event)
794 {
795 switch (event->evtype) {
796 case XCB_PRESENT_CONFIGURE_NOTIFY: {
797 xcb_present_configure_notify_event_t *config = (void *) event;
798
799 if (config->width != chain->extent.width ||
800 config->height != chain->extent.height)
801 return VK_ERROR_OUT_OF_DATE_KHR;
802
803 break;
804 }
805
806 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
807 xcb_present_idle_notify_event_t *idle = (void *) event;
808
809 for (unsigned i = 0; i < chain->base.image_count; i++) {
810 if (chain->images[i].pixmap == idle->pixmap) {
811 chain->images[i].busy = false;
812 if (chain->threaded)
813 wsi_queue_push(&chain->acquire_queue, i);
814 break;
815 }
816 }
817
818 break;
819 }
820
821 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
822 xcb_present_complete_notify_event_t *complete = (void *) event;
823 if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
824 chain->last_present_msc = complete->msc;
825
826 VkResult result = VK_SUCCESS;
827
828 /* The winsys is now trying to flip directly and cannot due to our
829 * configuration. Request the user reallocate.
830 */
831 #ifdef HAVE_DRI3_MODIFIERS
832 if (complete->mode == XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY &&
833 chain->last_present_mode != XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY)
834 result = VK_SUBOPTIMAL_KHR;
835 #endif
836
837 /* When we go from flipping to copying, the odds are very likely that
838 * we could reallocate in a more optimal way if we didn't have to care
839 * about scanout, so we always do this.
840 */
841 if (complete->mode == XCB_PRESENT_COMPLETE_MODE_COPY &&
842 chain->last_present_mode == XCB_PRESENT_COMPLETE_MODE_FLIP)
843 result = VK_SUBOPTIMAL_KHR;
844
845 chain->last_present_mode = complete->mode;
846 return result;
847 }
848
849 default:
850 break;
851 }
852
853 return VK_SUCCESS;
854 }
855
856
857 static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
858 {
859 uint64_t current_time = wsi_common_get_current_time();
860
861 timeout = MIN2(UINT64_MAX - current_time, timeout);
862
863 return current_time + timeout;
864 }
865
866 static VkResult
867 x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
868 uint32_t *image_index, uint64_t timeout)
869 {
870 xcb_generic_event_t *event;
871 struct pollfd pfds;
872 uint64_t atimeout;
873 while (1) {
874 for (uint32_t i = 0; i < chain->base.image_count; i++) {
875 if (!chain->images[i].busy) {
876 /* We found a non-busy image */
877 xshmfence_await(chain->images[i].shm_fence);
878 *image_index = i;
879 chain->images[i].busy = true;
880 return x11_swapchain_result(chain, VK_SUCCESS);
881 }
882 }
883
884 xcb_flush(chain->conn);
885
886 if (timeout == UINT64_MAX) {
887 event = xcb_wait_for_special_event(chain->conn, chain->special_event);
888 if (!event)
889 return x11_swapchain_result(chain, VK_ERROR_OUT_OF_DATE_KHR);
890 } else {
891 event = xcb_poll_for_special_event(chain->conn, chain->special_event);
892 if (!event) {
893 int ret;
894 if (timeout == 0)
895 return x11_swapchain_result(chain, VK_NOT_READY);
896
897 atimeout = wsi_get_absolute_timeout(timeout);
898
899 pfds.fd = xcb_get_file_descriptor(chain->conn);
900 pfds.events = POLLIN;
901 ret = poll(&pfds, 1, timeout / 1000 / 1000);
902 if (ret == 0)
903 return x11_swapchain_result(chain, VK_TIMEOUT);
904 if (ret == -1)
905 return x11_swapchain_result(chain, VK_ERROR_OUT_OF_DATE_KHR);
906
907 /* If a non-special event happens, the fd will still
908 * poll. So recalculate the timeout now just in case.
909 */
910 uint64_t current_time = wsi_common_get_current_time();
911 if (atimeout > current_time)
912 timeout = atimeout - current_time;
913 else
914 timeout = 0;
915 continue;
916 }
917 }
918
919 /* Update the swapchain status here. We may catch non-fatal errors here,
920 * in which case we need to update the status and continue.
921 */
922 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
923 free(event);
924 if (result < 0)
925 return x11_swapchain_result(chain, result);
926 }
927 }
928
929 static VkResult
930 x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
931 uint32_t *image_index_out, uint64_t timeout)
932 {
933 assert(chain->threaded);
934
935 uint32_t image_index;
936 VkResult result = wsi_queue_pull(&chain->acquire_queue,
937 &image_index, timeout);
938 if (result < 0 || result == VK_TIMEOUT) {
939 /* On error, the thread has shut down, so safe to update chain->status.
940 * Calling x11_swapchain_result with VK_TIMEOUT won't modify
941 * chain->status so that is also safe.
942 */
943 return x11_swapchain_result(chain, result);
944 } else if (chain->status < 0) {
945 return chain->status;
946 }
947
948 assert(image_index < chain->base.image_count);
949 xshmfence_await(chain->images[image_index].shm_fence);
950
951 *image_index_out = image_index;
952
953 return chain->status;
954 }
955
956 static VkResult
957 x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
958 uint32_t target_msc)
959 {
960 struct x11_image *image = &chain->images[image_index];
961
962 assert(image_index < chain->base.image_count);
963
964 uint32_t options = XCB_PRESENT_OPTION_NONE;
965
966 int64_t divisor = 0;
967 int64_t remainder = 0;
968
969 if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
970 options |= XCB_PRESENT_OPTION_ASYNC;
971
972 #ifdef HAVE_DRI3_MODIFIERS
973 if (chain->has_dri3_modifiers)
974 options |= XCB_PRESENT_OPTION_SUBOPTIMAL;
975 #endif
976
977 xshmfence_reset(image->shm_fence);
978
979 ++chain->send_sbc;
980 xcb_void_cookie_t cookie =
981 xcb_present_pixmap(chain->conn,
982 chain->window,
983 image->pixmap,
984 (uint32_t) chain->send_sbc,
985 0, /* valid */
986 0, /* update */
987 0, /* x_off */
988 0, /* y_off */
989 XCB_NONE, /* target_crtc */
990 XCB_NONE,
991 image->sync_fence,
992 options,
993 target_msc,
994 divisor,
995 remainder, 0, NULL);
996 xcb_discard_reply(chain->conn, cookie.sequence);
997 image->busy = true;
998
999 xcb_flush(chain->conn);
1000
1001 return x11_swapchain_result(chain, VK_SUCCESS);
1002 }
1003
1004 static VkResult
1005 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
1006 const VkAcquireNextImageInfoKHR *info,
1007 uint32_t *image_index)
1008 {
1009 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1010 uint64_t timeout = info->timeout;
1011
1012 if (chain->threaded) {
1013 return x11_acquire_next_image_from_queue(chain, image_index, timeout);
1014 } else {
1015 return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
1016 }
1017 }
1018
1019 static VkResult
1020 x11_queue_present(struct wsi_swapchain *anv_chain,
1021 uint32_t image_index,
1022 const VkPresentRegionKHR *damage)
1023 {
1024 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1025
1026 if (chain->threaded) {
1027 wsi_queue_push(&chain->present_queue, image_index);
1028 return chain->status;
1029 } else {
1030 return x11_present_to_x11(chain, image_index, 0);
1031 }
1032 }
1033
1034 static void *
1035 x11_manage_fifo_queues(void *state)
1036 {
1037 struct x11_swapchain *chain = state;
1038 VkResult result = VK_SUCCESS;
1039
1040 assert(chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR);
1041
1042 while (chain->status >= 0) {
1043 /* It should be safe to unconditionally block here. Later in the loop
1044 * we blocks until the previous present has landed on-screen. At that
1045 * point, we should have received IDLE_NOTIFY on all images presented
1046 * before that point so the client should be able to acquire any image
1047 * other than the currently presented one.
1048 */
1049 uint32_t image_index = 0;
1050 result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
1051 assert(result != VK_TIMEOUT);
1052 if (result < 0) {
1053 goto fail;
1054 } else if (chain->status < 0) {
1055 /* The status can change underneath us if the swapchain is destroyed
1056 * from another thread.
1057 */
1058 return NULL;
1059 }
1060
1061 uint64_t target_msc = chain->last_present_msc + 1;
1062 result = x11_present_to_x11(chain, image_index, target_msc);
1063 if (result < 0)
1064 goto fail;
1065
1066 while (chain->last_present_msc < target_msc) {
1067 xcb_generic_event_t *event =
1068 xcb_wait_for_special_event(chain->conn, chain->special_event);
1069 if (!event) {
1070 result = VK_ERROR_OUT_OF_DATE_KHR;
1071 goto fail;
1072 }
1073
1074 result = x11_handle_dri3_present_event(chain, (void *)event);
1075 free(event);
1076 if (result < 0)
1077 goto fail;
1078 }
1079 }
1080
1081 fail:
1082 x11_swapchain_result(chain, result);
1083 wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
1084
1085 return NULL;
1086 }
1087
1088 static VkResult
1089 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
1090 const VkSwapchainCreateInfoKHR *pCreateInfo,
1091 const VkAllocationCallbacks* pAllocator,
1092 const uint64_t *const *modifiers,
1093 const uint32_t *num_modifiers,
1094 int num_tranches, struct x11_image *image)
1095 {
1096 xcb_void_cookie_t cookie;
1097 VkResult result;
1098 uint32_t bpp = 32;
1099
1100 if (chain->base.use_prime_blit) {
1101 bool use_modifier = num_tranches > 0;
1102 result = wsi_create_prime_image(&chain->base, pCreateInfo, use_modifier, &image->base);
1103 } else {
1104 result = wsi_create_native_image(&chain->base, pCreateInfo,
1105 num_tranches, num_modifiers, modifiers,
1106 &image->base);
1107 }
1108 if (result < 0)
1109 return result;
1110
1111 image->pixmap = xcb_generate_id(chain->conn);
1112
1113 #ifdef HAVE_DRI3_MODIFIERS
1114 if (image->base.drm_modifier != DRM_FORMAT_MOD_INVALID) {
1115 /* If the image has a modifier, we must have DRI3 v1.2. */
1116 assert(chain->has_dri3_modifiers);
1117
1118 cookie =
1119 xcb_dri3_pixmap_from_buffers_checked(chain->conn,
1120 image->pixmap,
1121 chain->window,
1122 image->base.num_planes,
1123 pCreateInfo->imageExtent.width,
1124 pCreateInfo->imageExtent.height,
1125 image->base.row_pitches[0],
1126 image->base.offsets[0],
1127 image->base.row_pitches[1],
1128 image->base.offsets[1],
1129 image->base.row_pitches[2],
1130 image->base.offsets[2],
1131 image->base.row_pitches[3],
1132 image->base.offsets[3],
1133 chain->depth, bpp,
1134 image->base.drm_modifier,
1135 image->base.fds);
1136 } else
1137 #endif
1138 {
1139 /* Without passing modifiers, we can't have multi-plane RGB images. */
1140 assert(image->base.num_planes == 1);
1141
1142 cookie =
1143 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
1144 image->pixmap,
1145 chain->window,
1146 image->base.sizes[0],
1147 pCreateInfo->imageExtent.width,
1148 pCreateInfo->imageExtent.height,
1149 image->base.row_pitches[0],
1150 chain->depth, bpp,
1151 image->base.fds[0]);
1152 }
1153
1154 xcb_discard_reply(chain->conn, cookie.sequence);
1155
1156 /* XCB has now taken ownership of the FDs. */
1157 for (int i = 0; i < image->base.num_planes; i++)
1158 image->base.fds[i] = -1;
1159
1160 int fence_fd = xshmfence_alloc_shm();
1161 if (fence_fd < 0)
1162 goto fail_pixmap;
1163
1164 image->shm_fence = xshmfence_map_shm(fence_fd);
1165 if (image->shm_fence == NULL)
1166 goto fail_shmfence_alloc;
1167
1168 image->sync_fence = xcb_generate_id(chain->conn);
1169 xcb_dri3_fence_from_fd(chain->conn,
1170 image->pixmap,
1171 image->sync_fence,
1172 false,
1173 fence_fd);
1174
1175 image->busy = false;
1176 xshmfence_trigger(image->shm_fence);
1177
1178 return VK_SUCCESS;
1179
1180 fail_shmfence_alloc:
1181 close(fence_fd);
1182
1183 fail_pixmap:
1184 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
1185 xcb_discard_reply(chain->conn, cookie.sequence);
1186
1187 wsi_destroy_image(&chain->base, &image->base);
1188
1189 return result;
1190 }
1191
1192 static void
1193 x11_image_finish(struct x11_swapchain *chain,
1194 const VkAllocationCallbacks* pAllocator,
1195 struct x11_image *image)
1196 {
1197 xcb_void_cookie_t cookie;
1198
1199 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
1200 xcb_discard_reply(chain->conn, cookie.sequence);
1201 xshmfence_unmap_shm(image->shm_fence);
1202
1203 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
1204 xcb_discard_reply(chain->conn, cookie.sequence);
1205
1206 wsi_destroy_image(&chain->base, &image->base);
1207 }
1208
1209 static void
1210 wsi_x11_get_dri3_modifiers(struct wsi_x11_connection *wsi_conn,
1211 xcb_connection_t *conn, xcb_window_t window,
1212 uint8_t depth, uint8_t bpp,
1213 VkCompositeAlphaFlagsKHR vk_alpha,
1214 uint64_t **modifiers_in, uint32_t *num_modifiers_in,
1215 uint32_t *num_tranches_in,
1216 const VkAllocationCallbacks *pAllocator)
1217 {
1218 if (!wsi_conn->has_dri3_modifiers)
1219 goto out;
1220
1221 #ifdef HAVE_DRI3_MODIFIERS
1222 xcb_generic_error_t *error = NULL;
1223 xcb_dri3_get_supported_modifiers_cookie_t mod_cookie =
1224 xcb_dri3_get_supported_modifiers(conn, window, depth, bpp);
1225 xcb_dri3_get_supported_modifiers_reply_t *mod_reply =
1226 xcb_dri3_get_supported_modifiers_reply(conn, mod_cookie, &error);
1227 free(error);
1228
1229 if (!mod_reply || (mod_reply->num_window_modifiers == 0 &&
1230 mod_reply->num_screen_modifiers == 0)) {
1231 free(mod_reply);
1232 goto out;
1233 }
1234
1235 uint32_t n = 0;
1236 uint32_t counts[2];
1237 uint64_t *modifiers[2];
1238
1239 if (mod_reply->num_window_modifiers) {
1240 counts[n] = mod_reply->num_window_modifiers;
1241 modifiers[n] = vk_alloc(pAllocator,
1242 counts[n] * sizeof(uint64_t),
1243 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1244 if (!modifiers[n]) {
1245 free(mod_reply);
1246 goto out;
1247 }
1248
1249 memcpy(modifiers[n],
1250 xcb_dri3_get_supported_modifiers_window_modifiers(mod_reply),
1251 counts[n] * sizeof(uint64_t));
1252 n++;
1253 }
1254
1255 if (mod_reply->num_screen_modifiers) {
1256 counts[n] = mod_reply->num_screen_modifiers;
1257 modifiers[n] = vk_alloc(pAllocator,
1258 counts[n] * sizeof(uint64_t),
1259 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1260 if (!modifiers[n]) {
1261 if (n > 0)
1262 vk_free(pAllocator, modifiers[0]);
1263 free(mod_reply);
1264 goto out;
1265 }
1266
1267 memcpy(modifiers[n],
1268 xcb_dri3_get_supported_modifiers_screen_modifiers(mod_reply),
1269 counts[n] * sizeof(uint64_t));
1270 n++;
1271 }
1272
1273 for (int i = 0; i < n; i++) {
1274 modifiers_in[i] = modifiers[i];
1275 num_modifiers_in[i] = counts[i];
1276 }
1277 *num_tranches_in = n;
1278
1279 free(mod_reply);
1280 return;
1281 #endif
1282 out:
1283 *num_tranches_in = 0;
1284 }
1285
1286 static VkResult
1287 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
1288 const VkAllocationCallbacks *pAllocator)
1289 {
1290 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1291 xcb_void_cookie_t cookie;
1292
1293 if (chain->threaded) {
1294 chain->status = VK_ERROR_OUT_OF_DATE_KHR;
1295 /* Push a UINT32_MAX to wake up the manager */
1296 wsi_queue_push(&chain->present_queue, UINT32_MAX);
1297 pthread_join(chain->queue_manager, NULL);
1298 wsi_queue_destroy(&chain->acquire_queue);
1299 wsi_queue_destroy(&chain->present_queue);
1300 }
1301
1302 for (uint32_t i = 0; i < chain->base.image_count; i++)
1303 x11_image_finish(chain, pAllocator, &chain->images[i]);
1304
1305 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1306 cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
1307 chain->window,
1308 XCB_PRESENT_EVENT_MASK_NO_EVENT);
1309 xcb_discard_reply(chain->conn, cookie.sequence);
1310
1311 wsi_swapchain_finish(&chain->base);
1312
1313 vk_free(pAllocator, chain);
1314
1315 return VK_SUCCESS;
1316 }
1317
1318 static VkResult
1319 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
1320 VkDevice device,
1321 struct wsi_device *wsi_device,
1322 const VkSwapchainCreateInfoKHR *pCreateInfo,
1323 const VkAllocationCallbacks* pAllocator,
1324 struct wsi_swapchain **swapchain_out)
1325 {
1326 struct x11_swapchain *chain;
1327 xcb_void_cookie_t cookie;
1328 VkResult result;
1329
1330 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1331
1332 const unsigned num_images = pCreateInfo->minImageCount;
1333
1334 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
1335 struct wsi_x11_connection *wsi_conn =
1336 wsi_x11_get_connection(wsi_device, conn);
1337 if (!wsi_conn)
1338 return VK_ERROR_OUT_OF_HOST_MEMORY;
1339
1340 /* Check for whether or not we have a window up-front */
1341 xcb_window_t window = x11_surface_get_window(icd_surface);
1342 xcb_get_geometry_reply_t *geometry =
1343 xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
1344 if (geometry == NULL)
1345 return VK_ERROR_SURFACE_LOST_KHR;
1346 const uint32_t bit_depth = geometry->depth;
1347 free(geometry);
1348
1349 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1350 chain = vk_alloc(pAllocator, size, 8,
1351 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1352 if (chain == NULL)
1353 return VK_ERROR_OUT_OF_HOST_MEMORY;
1354
1355 result = wsi_swapchain_init(wsi_device, &chain->base, device,
1356 pCreateInfo, pAllocator);
1357 if (result != VK_SUCCESS)
1358 goto fail_alloc;
1359
1360 chain->base.destroy = x11_swapchain_destroy;
1361 chain->base.get_wsi_image = x11_get_wsi_image;
1362 chain->base.acquire_next_image = x11_acquire_next_image;
1363 chain->base.queue_present = x11_queue_present;
1364 chain->base.present_mode = pCreateInfo->presentMode;
1365 chain->base.image_count = num_images;
1366 chain->conn = conn;
1367 chain->window = window;
1368 chain->depth = bit_depth;
1369 chain->extent = pCreateInfo->imageExtent;
1370 chain->send_sbc = 0;
1371 chain->last_present_msc = 0;
1372 chain->threaded = false;
1373 chain->status = VK_SUCCESS;
1374 chain->has_dri3_modifiers = wsi_conn->has_dri3_modifiers;
1375
1376 /* If we are reallocating from an old swapchain, then we inherit its
1377 * last completion mode, to ensure we don't get into reallocation
1378 * cycles. If we are starting anew, we set 'COPY', as that is the only
1379 * mode which provokes reallocation when anything changes, to make
1380 * sure we have the most optimal allocation.
1381 */
1382 WSI_FROM_HANDLE(x11_swapchain, old_chain, pCreateInfo->oldSwapchain);
1383 if (old_chain)
1384 chain->last_present_mode = old_chain->last_present_mode;
1385 else
1386 chain->last_present_mode = XCB_PRESENT_COMPLETE_MODE_COPY;
1387
1388 if (!wsi_x11_check_dri3_compatible(wsi_device, conn))
1389 chain->base.use_prime_blit = true;
1390
1391 chain->event_id = xcb_generate_id(chain->conn);
1392 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
1393 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1394 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1395 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1396
1397 /* Create an XCB event queue to hold present events outside of the usual
1398 * application event queue
1399 */
1400 chain->special_event =
1401 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
1402 chain->event_id, NULL);
1403
1404 chain->gc = xcb_generate_id(chain->conn);
1405 if (!chain->gc) {
1406 /* FINISHME: Choose a better error. */
1407 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1408 goto fail_register;
1409 }
1410
1411 cookie = xcb_create_gc(chain->conn,
1412 chain->gc,
1413 chain->window,
1414 XCB_GC_GRAPHICS_EXPOSURES,
1415 (uint32_t []) { 0 });
1416 xcb_discard_reply(chain->conn, cookie.sequence);
1417
1418 uint64_t *modifiers[2] = {NULL, NULL};
1419 uint32_t num_modifiers[2] = {0, 0};
1420 uint32_t num_tranches = 0;
1421 if (wsi_device->supports_modifiers)
1422 wsi_x11_get_dri3_modifiers(wsi_conn, conn, window, chain->depth, 32,
1423 pCreateInfo->compositeAlpha,
1424 modifiers, num_modifiers, &num_tranches,
1425 pAllocator);
1426
1427 uint32_t image = 0;
1428 for (; image < chain->base.image_count; image++) {
1429 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1430 (const uint64_t *const *)modifiers,
1431 num_modifiers, num_tranches,
1432 &chain->images[image]);
1433 if (result != VK_SUCCESS)
1434 goto fail_init_images;
1435 }
1436
1437 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
1438 chain->threaded = true;
1439
1440 /* Initialize our queues. We make them base.image_count + 1 because we will
1441 * occasionally use UINT32_MAX to signal the other thread that an error
1442 * has occurred and we don't want an overflow.
1443 */
1444 int ret;
1445 ret = wsi_queue_init(&chain->acquire_queue, chain->base.image_count + 1);
1446 if (ret) {
1447 goto fail_init_images;
1448 }
1449
1450 ret = wsi_queue_init(&chain->present_queue, chain->base.image_count + 1);
1451 if (ret) {
1452 wsi_queue_destroy(&chain->acquire_queue);
1453 goto fail_init_images;
1454 }
1455
1456 for (unsigned i = 0; i < chain->base.image_count; i++)
1457 wsi_queue_push(&chain->acquire_queue, i);
1458
1459 ret = pthread_create(&chain->queue_manager, NULL,
1460 x11_manage_fifo_queues, chain);
1461 if (ret) {
1462 wsi_queue_destroy(&chain->present_queue);
1463 wsi_queue_destroy(&chain->acquire_queue);
1464 goto fail_init_images;
1465 }
1466 }
1467
1468 for (int i = 0; i < ARRAY_SIZE(modifiers); i++)
1469 vk_free(pAllocator, modifiers[i]);
1470 *swapchain_out = &chain->base;
1471
1472 return VK_SUCCESS;
1473
1474 fail_init_images:
1475 for (uint32_t j = 0; j < image; j++)
1476 x11_image_finish(chain, pAllocator, &chain->images[j]);
1477
1478 for (int i = 0; i < ARRAY_SIZE(modifiers); i++)
1479 vk_free(pAllocator, modifiers[i]);
1480
1481 fail_register:
1482 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1483
1484 wsi_swapchain_finish(&chain->base);
1485
1486 fail_alloc:
1487 vk_free(pAllocator, chain);
1488
1489 return result;
1490 }
1491
1492 VkResult
1493 wsi_x11_init_wsi(struct wsi_device *wsi_device,
1494 const VkAllocationCallbacks *alloc)
1495 {
1496 struct wsi_x11 *wsi;
1497 VkResult result;
1498
1499 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1500 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1501 if (!wsi) {
1502 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1503 goto fail;
1504 }
1505
1506 int ret = pthread_mutex_init(&wsi->mutex, NULL);
1507 if (ret != 0) {
1508 if (ret == ENOMEM) {
1509 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1510 } else {
1511 /* FINISHME: Choose a better error. */
1512 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1513 }
1514
1515 goto fail_alloc;
1516 }
1517
1518 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1519 _mesa_key_pointer_equal);
1520 if (!wsi->connections) {
1521 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1522 goto fail_mutex;
1523 }
1524
1525 wsi->base.get_support = x11_surface_get_support;
1526 wsi->base.get_capabilities2 = x11_surface_get_capabilities2;
1527 wsi->base.get_formats = x11_surface_get_formats;
1528 wsi->base.get_formats2 = x11_surface_get_formats2;
1529 wsi->base.get_present_modes = x11_surface_get_present_modes;
1530 wsi->base.get_present_rectangles = x11_surface_get_present_rectangles;
1531 wsi->base.create_swapchain = x11_surface_create_swapchain;
1532
1533 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1534 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1535
1536 return VK_SUCCESS;
1537
1538 fail_mutex:
1539 pthread_mutex_destroy(&wsi->mutex);
1540 fail_alloc:
1541 vk_free(alloc, wsi);
1542 fail:
1543 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1544 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1545
1546 return result;
1547 }
1548
1549 void
1550 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1551 const VkAllocationCallbacks *alloc)
1552 {
1553 struct wsi_x11 *wsi =
1554 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1555
1556 if (wsi) {
1557 hash_table_foreach(wsi->connections, entry)
1558 wsi_x11_connection_destroy(wsi_device, entry->data);
1559
1560 _mesa_hash_table_destroy(wsi->connections, NULL);
1561
1562 pthread_mutex_destroy(&wsi->mutex);
1563
1564 vk_free(alloc, wsi);
1565 }
1566 }