vulkan/wsi: Use VK_EXT_pci_bus_info for DRM fd matching
[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_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 bool can_handle_different_gpu,
379 xcb_connection_t* connection,
380 xcb_visualid_t visual_id)
381 {
382 struct wsi_x11_connection *wsi_conn =
383 wsi_x11_get_connection(wsi_device, connection);
384
385 if (!wsi_conn)
386 return false;
387
388 if (!wsi_x11_check_for_dri3(wsi_conn))
389 return false;
390
391 if (!can_handle_different_gpu)
392 if (!wsi_x11_check_dri3_compatible(wsi_device, connection))
393 return false;
394
395 unsigned visual_depth;
396 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
397 return false;
398
399 if (visual_depth != 24 && visual_depth != 32)
400 return false;
401
402 return true;
403 }
404
405 static xcb_connection_t*
406 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
407 {
408 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
409 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
410 else
411 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
412 }
413
414 static xcb_window_t
415 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
416 {
417 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
418 return ((VkIcdSurfaceXlib *)icd_surface)->window;
419 else
420 return ((VkIcdSurfaceXcb *)icd_surface)->window;
421 }
422
423 static VkResult
424 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
425 struct wsi_device *wsi_device,
426 uint32_t queueFamilyIndex,
427 VkBool32* pSupported)
428 {
429 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
430 xcb_window_t window = x11_surface_get_window(icd_surface);
431
432 struct wsi_x11_connection *wsi_conn =
433 wsi_x11_get_connection(wsi_device, conn);
434 if (!wsi_conn)
435 return VK_ERROR_OUT_OF_HOST_MEMORY;
436
437 if (!wsi_x11_check_for_dri3(wsi_conn)) {
438 *pSupported = false;
439 return VK_SUCCESS;
440 }
441
442 unsigned visual_depth;
443 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
444 *pSupported = false;
445 return VK_SUCCESS;
446 }
447
448 if (visual_depth != 24 && visual_depth != 32) {
449 *pSupported = false;
450 return VK_SUCCESS;
451 }
452
453 *pSupported = true;
454 return VK_SUCCESS;
455 }
456
457 static VkResult
458 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
459 VkSurfaceCapabilitiesKHR *caps)
460 {
461 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
462 xcb_window_t window = x11_surface_get_window(icd_surface);
463 xcb_get_geometry_cookie_t geom_cookie;
464 xcb_generic_error_t *err;
465 xcb_get_geometry_reply_t *geom;
466 unsigned visual_depth;
467
468 geom_cookie = xcb_get_geometry(conn, window);
469
470 /* This does a round-trip. This is why we do get_geometry first and
471 * wait to read the reply until after we have a visual.
472 */
473 xcb_visualtype_t *visual =
474 get_visualtype_for_window(conn, window, &visual_depth);
475
476 if (!visual)
477 return VK_ERROR_SURFACE_LOST_KHR;
478
479 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
480 if (geom) {
481 VkExtent2D extent = { geom->width, geom->height };
482 caps->currentExtent = extent;
483 caps->minImageExtent = extent;
484 caps->maxImageExtent = extent;
485 } else {
486 /* This can happen if the client didn't wait for the configure event
487 * to come back from the compositor. In that case, we don't know the
488 * size of the window so we just return valid "I don't know" stuff.
489 */
490 caps->currentExtent = (VkExtent2D) { -1, -1 };
491 caps->minImageExtent = (VkExtent2D) { 1, 1 };
492 /* This is the maximum supported size on Intel */
493 caps->maxImageExtent = (VkExtent2D) { 1 << 14, 1 << 14 };
494 }
495 free(err);
496 free(geom);
497
498 if (visual_has_alpha(visual, visual_depth)) {
499 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
500 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
501 } else {
502 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
503 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
504 }
505
506 /* For true mailbox mode, we need at least 4 images:
507 * 1) One to scan out from
508 * 2) One to have queued for scan-out
509 * 3) One to be currently held by the X server
510 * 4) One to render to
511 */
512 caps->minImageCount = 2;
513 /* There is no real maximum */
514 caps->maxImageCount = 0;
515
516 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
517 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
518 caps->maxImageArrayLayers = 1;
519 caps->supportedUsageFlags =
520 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
521 VK_IMAGE_USAGE_SAMPLED_BIT |
522 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
523 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
524
525 return VK_SUCCESS;
526 }
527
528 static VkResult
529 x11_surface_get_capabilities2(VkIcdSurfaceBase *icd_surface,
530 const void *info_next,
531 VkSurfaceCapabilities2KHR *caps)
532 {
533 assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
534
535 return x11_surface_get_capabilities(icd_surface, &caps->surfaceCapabilities);
536 }
537
538 static VkResult
539 x11_surface_get_formats(VkIcdSurfaceBase *surface,
540 struct wsi_device *wsi_device,
541 uint32_t *pSurfaceFormatCount,
542 VkSurfaceFormatKHR *pSurfaceFormats)
543 {
544 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
545
546 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
547 vk_outarray_append(&out, f) {
548 f->format = formats[i];
549 f->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
550 }
551 }
552
553 return vk_outarray_status(&out);
554 }
555
556 static VkResult
557 x11_surface_get_formats2(VkIcdSurfaceBase *surface,
558 struct wsi_device *wsi_device,
559 const void *info_next,
560 uint32_t *pSurfaceFormatCount,
561 VkSurfaceFormat2KHR *pSurfaceFormats)
562 {
563 VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
564
565 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
566 vk_outarray_append(&out, f) {
567 assert(f->sType == VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR);
568 f->surfaceFormat.format = formats[i];
569 f->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
570 }
571 }
572
573 return vk_outarray_status(&out);
574 }
575
576 static VkResult
577 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
578 uint32_t *pPresentModeCount,
579 VkPresentModeKHR *pPresentModes)
580 {
581 if (pPresentModes == NULL) {
582 *pPresentModeCount = ARRAY_SIZE(present_modes);
583 return VK_SUCCESS;
584 }
585
586 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
587 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
588
589 return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
590 VK_INCOMPLETE : VK_SUCCESS;
591 }
592
593 static bool
594 x11_surface_is_local_to_gpu(struct wsi_device *wsi_dev,
595 xcb_connection_t *conn)
596 {
597 struct wsi_x11_connection *wsi_conn =
598 wsi_x11_get_connection(wsi_dev, conn);
599
600 if (!wsi_conn)
601 return false;
602
603 if (!wsi_x11_check_for_dri3(wsi_conn))
604 return false;
605
606 if (!wsi_x11_check_dri3_compatible(wsi_dev, conn))
607 return false;
608
609 return true;
610 }
611
612 static VkResult
613 x11_surface_get_present_rectangles(VkIcdSurfaceBase *icd_surface,
614 struct wsi_device *wsi_device,
615 uint32_t* pRectCount,
616 VkRect2D* pRects)
617 {
618 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
619 xcb_window_t window = x11_surface_get_window(icd_surface);
620 VK_OUTARRAY_MAKE(out, pRects, pRectCount);
621
622 if (x11_surface_is_local_to_gpu(wsi_device, conn)) {
623 vk_outarray_append(&out, rect) {
624 xcb_generic_error_t *err = NULL;
625 xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, window);
626 xcb_get_geometry_reply_t *geom =
627 xcb_get_geometry_reply(conn, geom_cookie, &err);
628 free(err);
629 if (geom) {
630 *rect = (VkRect2D) {
631 .offset = { 0, 0 },
632 .extent = { geom->width, geom->height },
633 };
634 } else {
635 /* This can happen if the client didn't wait for the configure event
636 * to come back from the compositor. In that case, we don't know the
637 * size of the window so we just return valid "I don't know" stuff.
638 */
639 *rect = (VkRect2D) {
640 .offset = { 0, 0 },
641 .extent = { -1, -1 },
642 };
643 }
644 free(geom);
645 }
646 }
647
648 return vk_outarray_status(&out);
649 }
650
651 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
652 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
653 VkSurfaceKHR *pSurface)
654 {
655 VkIcdSurfaceXcb *surface;
656
657 surface = vk_alloc(pAllocator, sizeof *surface, 8,
658 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
659 if (surface == NULL)
660 return VK_ERROR_OUT_OF_HOST_MEMORY;
661
662 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
663 surface->connection = pCreateInfo->connection;
664 surface->window = pCreateInfo->window;
665
666 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
667 return VK_SUCCESS;
668 }
669
670 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
671 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
672 VkSurfaceKHR *pSurface)
673 {
674 VkIcdSurfaceXlib *surface;
675
676 surface = vk_alloc(pAllocator, sizeof *surface, 8,
677 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
678 if (surface == NULL)
679 return VK_ERROR_OUT_OF_HOST_MEMORY;
680
681 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
682 surface->dpy = pCreateInfo->dpy;
683 surface->window = pCreateInfo->window;
684
685 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
686 return VK_SUCCESS;
687 }
688
689 struct x11_image {
690 struct wsi_image base;
691 xcb_pixmap_t pixmap;
692 bool busy;
693 struct xshmfence * shm_fence;
694 uint32_t sync_fence;
695 };
696
697 struct x11_swapchain {
698 struct wsi_swapchain base;
699
700 bool has_dri3_modifiers;
701
702 xcb_connection_t * conn;
703 xcb_window_t window;
704 xcb_gc_t gc;
705 uint32_t depth;
706 VkExtent2D extent;
707
708 xcb_present_event_t event_id;
709 xcb_special_event_t * special_event;
710 uint64_t send_sbc;
711 uint64_t last_present_msc;
712 uint32_t stamp;
713
714 bool threaded;
715 VkResult status;
716 xcb_present_complete_mode_t last_present_mode;
717 struct wsi_queue present_queue;
718 struct wsi_queue acquire_queue;
719 pthread_t queue_manager;
720
721 struct x11_image images[0];
722 };
723
724 /**
725 * Update the swapchain status with the result of an operation, and return
726 * the combined status. The chain status will eventually be returned from
727 * AcquireNextImage and QueuePresent.
728 *
729 * We make sure to 'stick' more pessimistic statuses: an out-of-date error
730 * is permanent once seen, and every subsequent call will return this. If
731 * this has not been seen, success will be returned.
732 */
733 static VkResult
734 x11_swapchain_result(struct x11_swapchain *chain, VkResult result)
735 {
736 /* Prioritise returning existing errors for consistency. */
737 if (chain->status < 0)
738 return chain->status;
739
740 /* If we have a new error, mark it as permanent on the chain and return. */
741 if (result < 0) {
742 chain->status = result;
743 return result;
744 }
745
746 /* Return temporary errors, but don't persist them. */
747 if (result == VK_TIMEOUT || result == VK_NOT_READY)
748 return result;
749
750 /* Suboptimal isn't an error, but is a status which sticks to the swapchain
751 * and is always returned rather than success.
752 */
753 if (result == VK_SUBOPTIMAL_KHR) {
754 chain->status = result;
755 return result;
756 }
757
758 /* No changes, so return the last status. */
759 return chain->status;
760 }
761
762 static struct wsi_image *
763 x11_get_wsi_image(struct wsi_swapchain *wsi_chain, uint32_t image_index)
764 {
765 struct x11_swapchain *chain = (struct x11_swapchain *)wsi_chain;
766 return &chain->images[image_index].base;
767 }
768
769 /**
770 * Process an X11 Present event. Does not update chain->status.
771 */
772 static VkResult
773 x11_handle_dri3_present_event(struct x11_swapchain *chain,
774 xcb_present_generic_event_t *event)
775 {
776 switch (event->evtype) {
777 case XCB_PRESENT_CONFIGURE_NOTIFY: {
778 xcb_present_configure_notify_event_t *config = (void *) event;
779
780 if (config->width != chain->extent.width ||
781 config->height != chain->extent.height)
782 return VK_ERROR_OUT_OF_DATE_KHR;
783
784 break;
785 }
786
787 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
788 xcb_present_idle_notify_event_t *idle = (void *) event;
789
790 for (unsigned i = 0; i < chain->base.image_count; i++) {
791 if (chain->images[i].pixmap == idle->pixmap) {
792 chain->images[i].busy = false;
793 if (chain->threaded)
794 wsi_queue_push(&chain->acquire_queue, i);
795 break;
796 }
797 }
798
799 break;
800 }
801
802 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
803 xcb_present_complete_notify_event_t *complete = (void *) event;
804 if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
805 chain->last_present_msc = complete->msc;
806
807 VkResult result = VK_SUCCESS;
808
809 /* The winsys is now trying to flip directly and cannot due to our
810 * configuration. Request the user reallocate.
811 */
812 #ifdef HAVE_DRI3_MODIFIERS
813 if (complete->mode == XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY &&
814 chain->last_present_mode != XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY)
815 result = VK_SUBOPTIMAL_KHR;
816 #endif
817
818 /* When we go from flipping to copying, the odds are very likely that
819 * we could reallocate in a more optimal way if we didn't have to care
820 * about scanout, so we always do this.
821 */
822 if (complete->mode == XCB_PRESENT_COMPLETE_MODE_COPY &&
823 chain->last_present_mode == XCB_PRESENT_COMPLETE_MODE_FLIP)
824 result = VK_SUBOPTIMAL_KHR;
825
826 chain->last_present_mode = complete->mode;
827 return result;
828 }
829
830 default:
831 break;
832 }
833
834 return VK_SUCCESS;
835 }
836
837
838 static uint64_t wsi_get_current_time(void)
839 {
840 uint64_t current_time;
841 struct timespec tv;
842
843 clock_gettime(CLOCK_MONOTONIC, &tv);
844 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
845 return current_time;
846 }
847
848 static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
849 {
850 uint64_t current_time = wsi_get_current_time();
851
852 timeout = MIN2(UINT64_MAX - current_time, timeout);
853
854 return current_time + timeout;
855 }
856
857 static VkResult
858 x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
859 uint32_t *image_index, uint64_t timeout)
860 {
861 xcb_generic_event_t *event;
862 struct pollfd pfds;
863 uint64_t atimeout;
864 while (1) {
865 for (uint32_t i = 0; i < chain->base.image_count; i++) {
866 if (!chain->images[i].busy) {
867 /* We found a non-busy image */
868 xshmfence_await(chain->images[i].shm_fence);
869 *image_index = i;
870 chain->images[i].busy = true;
871 return x11_swapchain_result(chain, VK_SUCCESS);
872 }
873 }
874
875 xcb_flush(chain->conn);
876
877 if (timeout == UINT64_MAX) {
878 event = xcb_wait_for_special_event(chain->conn, chain->special_event);
879 if (!event)
880 return x11_swapchain_result(chain, VK_ERROR_OUT_OF_DATE_KHR);
881 } else {
882 event = xcb_poll_for_special_event(chain->conn, chain->special_event);
883 if (!event) {
884 int ret;
885 if (timeout == 0)
886 return x11_swapchain_result(chain, VK_NOT_READY);
887
888 atimeout = wsi_get_absolute_timeout(timeout);
889
890 pfds.fd = xcb_get_file_descriptor(chain->conn);
891 pfds.events = POLLIN;
892 ret = poll(&pfds, 1, timeout / 1000 / 1000);
893 if (ret == 0)
894 return x11_swapchain_result(chain, VK_TIMEOUT);
895 if (ret == -1)
896 return x11_swapchain_result(chain, VK_ERROR_OUT_OF_DATE_KHR);
897
898 /* If a non-special event happens, the fd will still
899 * poll. So recalculate the timeout now just in case.
900 */
901 uint64_t current_time = wsi_get_current_time();
902 if (atimeout > current_time)
903 timeout = atimeout - current_time;
904 else
905 timeout = 0;
906 continue;
907 }
908 }
909
910 /* Update the swapchain status here. We may catch non-fatal errors here,
911 * in which case we need to update the status and continue.
912 */
913 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
914 free(event);
915 if (result < 0)
916 return x11_swapchain_result(chain, result);
917 }
918 }
919
920 static VkResult
921 x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
922 uint32_t *image_index_out, uint64_t timeout)
923 {
924 assert(chain->threaded);
925
926 uint32_t image_index;
927 VkResult result = wsi_queue_pull(&chain->acquire_queue,
928 &image_index, timeout);
929 if (result < 0 || result == VK_TIMEOUT) {
930 /* On error, the thread has shut down, so safe to update chain->status.
931 * Calling x11_swapchain_result with VK_TIMEOUT won't modify
932 * chain->status so that is also safe.
933 */
934 return x11_swapchain_result(chain, result);
935 } else if (chain->status < 0) {
936 return chain->status;
937 }
938
939 assert(image_index < chain->base.image_count);
940 xshmfence_await(chain->images[image_index].shm_fence);
941
942 *image_index_out = image_index;
943
944 return chain->status;
945 }
946
947 static VkResult
948 x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
949 uint32_t target_msc)
950 {
951 struct x11_image *image = &chain->images[image_index];
952
953 assert(image_index < chain->base.image_count);
954
955 uint32_t options = XCB_PRESENT_OPTION_NONE;
956
957 int64_t divisor = 0;
958 int64_t remainder = 0;
959
960 if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
961 options |= XCB_PRESENT_OPTION_ASYNC;
962
963 #ifdef HAVE_DRI3_MODIFIERS
964 if (chain->has_dri3_modifiers)
965 options |= XCB_PRESENT_OPTION_SUBOPTIMAL;
966 #endif
967
968 xshmfence_reset(image->shm_fence);
969
970 ++chain->send_sbc;
971 xcb_void_cookie_t cookie =
972 xcb_present_pixmap(chain->conn,
973 chain->window,
974 image->pixmap,
975 (uint32_t) chain->send_sbc,
976 0, /* valid */
977 0, /* update */
978 0, /* x_off */
979 0, /* y_off */
980 XCB_NONE, /* target_crtc */
981 XCB_NONE,
982 image->sync_fence,
983 options,
984 target_msc,
985 divisor,
986 remainder, 0, NULL);
987 xcb_discard_reply(chain->conn, cookie.sequence);
988 image->busy = true;
989
990 xcb_flush(chain->conn);
991
992 return x11_swapchain_result(chain, VK_SUCCESS);
993 }
994
995 static VkResult
996 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
997 const VkAcquireNextImageInfoKHR *info,
998 uint32_t *image_index)
999 {
1000 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1001 uint64_t timeout = info->timeout;
1002
1003 if (chain->threaded) {
1004 return x11_acquire_next_image_from_queue(chain, image_index, timeout);
1005 } else {
1006 return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
1007 }
1008 }
1009
1010 static VkResult
1011 x11_queue_present(struct wsi_swapchain *anv_chain,
1012 uint32_t image_index,
1013 const VkPresentRegionKHR *damage)
1014 {
1015 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1016
1017 if (chain->threaded) {
1018 wsi_queue_push(&chain->present_queue, image_index);
1019 return chain->status;
1020 } else {
1021 return x11_present_to_x11(chain, image_index, 0);
1022 }
1023 }
1024
1025 static void *
1026 x11_manage_fifo_queues(void *state)
1027 {
1028 struct x11_swapchain *chain = state;
1029 VkResult result = VK_SUCCESS;
1030
1031 assert(chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR);
1032
1033 while (chain->status >= 0) {
1034 /* It should be safe to unconditionally block here. Later in the loop
1035 * we blocks until the previous present has landed on-screen. At that
1036 * point, we should have received IDLE_NOTIFY on all images presented
1037 * before that point so the client should be able to acquire any image
1038 * other than the currently presented one.
1039 */
1040 uint32_t image_index = 0;
1041 result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
1042 assert(result != VK_TIMEOUT);
1043 if (result < 0) {
1044 goto fail;
1045 } else if (chain->status < 0) {
1046 /* The status can change underneath us if the swapchain is destroyed
1047 * from another thread.
1048 */
1049 return NULL;
1050 }
1051
1052 uint64_t target_msc = chain->last_present_msc + 1;
1053 result = x11_present_to_x11(chain, image_index, target_msc);
1054 if (result < 0)
1055 goto fail;
1056
1057 while (chain->last_present_msc < target_msc) {
1058 xcb_generic_event_t *event =
1059 xcb_wait_for_special_event(chain->conn, chain->special_event);
1060 if (!event) {
1061 result = VK_ERROR_OUT_OF_DATE_KHR;
1062 goto fail;
1063 }
1064
1065 result = x11_handle_dri3_present_event(chain, (void *)event);
1066 free(event);
1067 if (result < 0)
1068 goto fail;
1069 }
1070 }
1071
1072 fail:
1073 x11_swapchain_result(chain, result);
1074 wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
1075
1076 return NULL;
1077 }
1078
1079 static VkResult
1080 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
1081 const VkSwapchainCreateInfoKHR *pCreateInfo,
1082 const VkAllocationCallbacks* pAllocator,
1083 const uint64_t *const *modifiers,
1084 const uint32_t *num_modifiers,
1085 int num_tranches, struct x11_image *image)
1086 {
1087 xcb_void_cookie_t cookie;
1088 VkResult result;
1089 uint32_t bpp = 32;
1090
1091 if (chain->base.use_prime_blit) {
1092 bool use_modifier = num_tranches > 0;
1093 result = wsi_create_prime_image(&chain->base, pCreateInfo, use_modifier, &image->base);
1094 } else {
1095 result = wsi_create_native_image(&chain->base, pCreateInfo,
1096 num_tranches, num_modifiers, modifiers,
1097 &image->base);
1098 }
1099 if (result < 0)
1100 return result;
1101
1102 image->pixmap = xcb_generate_id(chain->conn);
1103
1104 #ifdef HAVE_DRI3_MODIFIERS
1105 if (image->base.drm_modifier != DRM_FORMAT_MOD_INVALID) {
1106 /* If the image has a modifier, we must have DRI3 v1.2. */
1107 assert(chain->has_dri3_modifiers);
1108
1109 cookie =
1110 xcb_dri3_pixmap_from_buffers_checked(chain->conn,
1111 image->pixmap,
1112 chain->window,
1113 image->base.num_planes,
1114 pCreateInfo->imageExtent.width,
1115 pCreateInfo->imageExtent.height,
1116 image->base.row_pitches[0],
1117 image->base.offsets[0],
1118 image->base.row_pitches[1],
1119 image->base.offsets[1],
1120 image->base.row_pitches[2],
1121 image->base.offsets[2],
1122 image->base.row_pitches[3],
1123 image->base.offsets[3],
1124 chain->depth, bpp,
1125 image->base.drm_modifier,
1126 image->base.fds);
1127 } else
1128 #endif
1129 {
1130 /* Without passing modifiers, we can't have multi-plane RGB images. */
1131 assert(image->base.num_planes == 1);
1132
1133 cookie =
1134 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
1135 image->pixmap,
1136 chain->window,
1137 image->base.sizes[0],
1138 pCreateInfo->imageExtent.width,
1139 pCreateInfo->imageExtent.height,
1140 image->base.row_pitches[0],
1141 chain->depth, bpp,
1142 image->base.fds[0]);
1143 }
1144
1145 xcb_discard_reply(chain->conn, cookie.sequence);
1146
1147 /* XCB has now taken ownership of the FDs. */
1148 for (int i = 0; i < image->base.num_planes; i++)
1149 image->base.fds[i] = -1;
1150
1151 int fence_fd = xshmfence_alloc_shm();
1152 if (fence_fd < 0)
1153 goto fail_pixmap;
1154
1155 image->shm_fence = xshmfence_map_shm(fence_fd);
1156 if (image->shm_fence == NULL)
1157 goto fail_shmfence_alloc;
1158
1159 image->sync_fence = xcb_generate_id(chain->conn);
1160 xcb_dri3_fence_from_fd(chain->conn,
1161 image->pixmap,
1162 image->sync_fence,
1163 false,
1164 fence_fd);
1165
1166 image->busy = false;
1167 xshmfence_trigger(image->shm_fence);
1168
1169 return VK_SUCCESS;
1170
1171 fail_shmfence_alloc:
1172 close(fence_fd);
1173
1174 fail_pixmap:
1175 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
1176 xcb_discard_reply(chain->conn, cookie.sequence);
1177
1178 wsi_destroy_image(&chain->base, &image->base);
1179
1180 return result;
1181 }
1182
1183 static void
1184 x11_image_finish(struct x11_swapchain *chain,
1185 const VkAllocationCallbacks* pAllocator,
1186 struct x11_image *image)
1187 {
1188 xcb_void_cookie_t cookie;
1189
1190 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
1191 xcb_discard_reply(chain->conn, cookie.sequence);
1192 xshmfence_unmap_shm(image->shm_fence);
1193
1194 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
1195 xcb_discard_reply(chain->conn, cookie.sequence);
1196
1197 wsi_destroy_image(&chain->base, &image->base);
1198 }
1199
1200 static void
1201 wsi_x11_get_dri3_modifiers(struct wsi_x11_connection *wsi_conn,
1202 xcb_connection_t *conn, xcb_window_t window,
1203 uint8_t depth, uint8_t bpp,
1204 VkCompositeAlphaFlagsKHR vk_alpha,
1205 uint64_t **modifiers_in, uint32_t *num_modifiers_in,
1206 uint32_t *num_tranches_in,
1207 const VkAllocationCallbacks *pAllocator)
1208 {
1209 if (!wsi_conn->has_dri3_modifiers)
1210 goto out;
1211
1212 #ifdef HAVE_DRI3_MODIFIERS
1213 xcb_generic_error_t *error = NULL;
1214 xcb_dri3_get_supported_modifiers_cookie_t mod_cookie =
1215 xcb_dri3_get_supported_modifiers(conn, window, depth, bpp);
1216 xcb_dri3_get_supported_modifiers_reply_t *mod_reply =
1217 xcb_dri3_get_supported_modifiers_reply(conn, mod_cookie, &error);
1218 free(error);
1219
1220 if (!mod_reply || (mod_reply->num_window_modifiers == 0 &&
1221 mod_reply->num_screen_modifiers == 0)) {
1222 free(mod_reply);
1223 goto out;
1224 }
1225
1226 uint32_t n = 0;
1227 uint32_t counts[2];
1228 uint64_t *modifiers[2];
1229
1230 if (mod_reply->num_window_modifiers) {
1231 counts[n] = mod_reply->num_window_modifiers;
1232 modifiers[n] = vk_alloc(pAllocator,
1233 counts[n] * sizeof(uint64_t),
1234 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1235 if (!modifiers[n]) {
1236 free(mod_reply);
1237 goto out;
1238 }
1239
1240 memcpy(modifiers[n],
1241 xcb_dri3_get_supported_modifiers_window_modifiers(mod_reply),
1242 counts[n] * sizeof(uint64_t));
1243 n++;
1244 }
1245
1246 if (mod_reply->num_screen_modifiers) {
1247 counts[n] = mod_reply->num_screen_modifiers;
1248 modifiers[n] = vk_alloc(pAllocator,
1249 counts[n] * sizeof(uint64_t),
1250 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1251 if (!modifiers[n]) {
1252 if (n > 0)
1253 vk_free(pAllocator, modifiers[0]);
1254 free(mod_reply);
1255 goto out;
1256 }
1257
1258 memcpy(modifiers[n],
1259 xcb_dri3_get_supported_modifiers_screen_modifiers(mod_reply),
1260 counts[n] * sizeof(uint64_t));
1261 n++;
1262 }
1263
1264 for (int i = 0; i < n; i++) {
1265 modifiers_in[i] = modifiers[i];
1266 num_modifiers_in[i] = counts[i];
1267 }
1268 *num_tranches_in = n;
1269
1270 free(mod_reply);
1271 return;
1272 #endif
1273 out:
1274 *num_tranches_in = 0;
1275 }
1276
1277 static VkResult
1278 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
1279 const VkAllocationCallbacks *pAllocator)
1280 {
1281 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1282 xcb_void_cookie_t cookie;
1283
1284 if (chain->threaded) {
1285 chain->status = VK_ERROR_OUT_OF_DATE_KHR;
1286 /* Push a UINT32_MAX to wake up the manager */
1287 wsi_queue_push(&chain->present_queue, UINT32_MAX);
1288 pthread_join(chain->queue_manager, NULL);
1289 wsi_queue_destroy(&chain->acquire_queue);
1290 wsi_queue_destroy(&chain->present_queue);
1291 }
1292
1293 for (uint32_t i = 0; i < chain->base.image_count; i++)
1294 x11_image_finish(chain, pAllocator, &chain->images[i]);
1295
1296 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1297 cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
1298 chain->window,
1299 XCB_PRESENT_EVENT_MASK_NO_EVENT);
1300 xcb_discard_reply(chain->conn, cookie.sequence);
1301
1302 wsi_swapchain_finish(&chain->base);
1303
1304 vk_free(pAllocator, chain);
1305
1306 return VK_SUCCESS;
1307 }
1308
1309 static VkResult
1310 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
1311 VkDevice device,
1312 struct wsi_device *wsi_device,
1313 const VkSwapchainCreateInfoKHR *pCreateInfo,
1314 const VkAllocationCallbacks* pAllocator,
1315 struct wsi_swapchain **swapchain_out)
1316 {
1317 struct x11_swapchain *chain;
1318 xcb_void_cookie_t cookie;
1319 VkResult result;
1320
1321 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1322
1323 const unsigned num_images = pCreateInfo->minImageCount;
1324
1325 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
1326 struct wsi_x11_connection *wsi_conn =
1327 wsi_x11_get_connection(wsi_device, conn);
1328 if (!wsi_conn)
1329 return VK_ERROR_OUT_OF_HOST_MEMORY;
1330
1331 /* Check for whether or not we have a window up-front */
1332 xcb_window_t window = x11_surface_get_window(icd_surface);
1333 xcb_get_geometry_reply_t *geometry =
1334 xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
1335 if (geometry == NULL)
1336 return VK_ERROR_SURFACE_LOST_KHR;
1337 const uint32_t bit_depth = geometry->depth;
1338 free(geometry);
1339
1340 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1341 chain = vk_alloc(pAllocator, size, 8,
1342 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1343 if (chain == NULL)
1344 return VK_ERROR_OUT_OF_HOST_MEMORY;
1345
1346 result = wsi_swapchain_init(wsi_device, &chain->base, device,
1347 pCreateInfo, pAllocator);
1348 if (result != VK_SUCCESS)
1349 goto fail_alloc;
1350
1351 chain->base.destroy = x11_swapchain_destroy;
1352 chain->base.get_wsi_image = x11_get_wsi_image;
1353 chain->base.acquire_next_image = x11_acquire_next_image;
1354 chain->base.queue_present = x11_queue_present;
1355 chain->base.present_mode = pCreateInfo->presentMode;
1356 chain->base.image_count = num_images;
1357 chain->conn = conn;
1358 chain->window = window;
1359 chain->depth = bit_depth;
1360 chain->extent = pCreateInfo->imageExtent;
1361 chain->send_sbc = 0;
1362 chain->last_present_msc = 0;
1363 chain->threaded = false;
1364 chain->status = VK_SUCCESS;
1365 chain->has_dri3_modifiers = wsi_conn->has_dri3_modifiers;
1366
1367 /* If we are reallocating from an old swapchain, then we inherit its
1368 * last completion mode, to ensure we don't get into reallocation
1369 * cycles. If we are starting anew, we set 'COPY', as that is the only
1370 * mode which provokes reallocation when anything changes, to make
1371 * sure we have the most optimal allocation.
1372 */
1373 struct x11_swapchain *old_chain = (void *)(intptr_t) pCreateInfo->oldSwapchain;
1374 if (old_chain)
1375 chain->last_present_mode = old_chain->last_present_mode;
1376 else
1377 chain->last_present_mode = XCB_PRESENT_COMPLETE_MODE_COPY;
1378
1379 if (!wsi_x11_check_dri3_compatible(wsi_device, conn))
1380 chain->base.use_prime_blit = true;
1381
1382 chain->event_id = xcb_generate_id(chain->conn);
1383 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
1384 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1385 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1386 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1387
1388 /* Create an XCB event queue to hold present events outside of the usual
1389 * application event queue
1390 */
1391 chain->special_event =
1392 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
1393 chain->event_id, NULL);
1394
1395 chain->gc = xcb_generate_id(chain->conn);
1396 if (!chain->gc) {
1397 /* FINISHME: Choose a better error. */
1398 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1399 goto fail_register;
1400 }
1401
1402 cookie = xcb_create_gc(chain->conn,
1403 chain->gc,
1404 chain->window,
1405 XCB_GC_GRAPHICS_EXPOSURES,
1406 (uint32_t []) { 0 });
1407 xcb_discard_reply(chain->conn, cookie.sequence);
1408
1409 uint64_t *modifiers[2] = {NULL, NULL};
1410 uint32_t num_modifiers[2] = {0, 0};
1411 uint32_t num_tranches = 0;
1412 if (wsi_device->supports_modifiers)
1413 wsi_x11_get_dri3_modifiers(wsi_conn, conn, window, chain->depth, 32,
1414 pCreateInfo->compositeAlpha,
1415 modifiers, num_modifiers, &num_tranches,
1416 pAllocator);
1417
1418 uint32_t image = 0;
1419 for (; image < chain->base.image_count; image++) {
1420 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1421 (const uint64_t *const *)modifiers,
1422 num_modifiers, num_tranches,
1423 &chain->images[image]);
1424 if (result != VK_SUCCESS)
1425 goto fail_init_images;
1426 }
1427
1428 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
1429 chain->threaded = true;
1430
1431 /* Initialize our queues. We make them base.image_count + 1 because we will
1432 * occasionally use UINT32_MAX to signal the other thread that an error
1433 * has occurred and we don't want an overflow.
1434 */
1435 int ret;
1436 ret = wsi_queue_init(&chain->acquire_queue, chain->base.image_count + 1);
1437 if (ret) {
1438 goto fail_init_images;
1439 }
1440
1441 ret = wsi_queue_init(&chain->present_queue, chain->base.image_count + 1);
1442 if (ret) {
1443 wsi_queue_destroy(&chain->acquire_queue);
1444 goto fail_init_images;
1445 }
1446
1447 for (unsigned i = 0; i < chain->base.image_count; i++)
1448 wsi_queue_push(&chain->acquire_queue, i);
1449
1450 ret = pthread_create(&chain->queue_manager, NULL,
1451 x11_manage_fifo_queues, chain);
1452 if (ret) {
1453 wsi_queue_destroy(&chain->present_queue);
1454 wsi_queue_destroy(&chain->acquire_queue);
1455 goto fail_init_images;
1456 }
1457 }
1458
1459 for (int i = 0; i < ARRAY_SIZE(modifiers); i++)
1460 vk_free(pAllocator, modifiers[i]);
1461 *swapchain_out = &chain->base;
1462
1463 return VK_SUCCESS;
1464
1465 fail_init_images:
1466 for (uint32_t j = 0; j < image; j++)
1467 x11_image_finish(chain, pAllocator, &chain->images[j]);
1468
1469 for (int i = 0; i < ARRAY_SIZE(modifiers); i++)
1470 vk_free(pAllocator, modifiers[i]);
1471
1472 fail_register:
1473 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1474
1475 wsi_swapchain_finish(&chain->base);
1476
1477 fail_alloc:
1478 vk_free(pAllocator, chain);
1479
1480 return result;
1481 }
1482
1483 VkResult
1484 wsi_x11_init_wsi(struct wsi_device *wsi_device,
1485 const VkAllocationCallbacks *alloc)
1486 {
1487 struct wsi_x11 *wsi;
1488 VkResult result;
1489
1490 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1491 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1492 if (!wsi) {
1493 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1494 goto fail;
1495 }
1496
1497 int ret = pthread_mutex_init(&wsi->mutex, NULL);
1498 if (ret != 0) {
1499 if (ret == ENOMEM) {
1500 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1501 } else {
1502 /* FINISHME: Choose a better error. */
1503 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1504 }
1505
1506 goto fail_alloc;
1507 }
1508
1509 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1510 _mesa_key_pointer_equal);
1511 if (!wsi->connections) {
1512 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1513 goto fail_mutex;
1514 }
1515
1516 wsi->base.get_support = x11_surface_get_support;
1517 wsi->base.get_capabilities2 = x11_surface_get_capabilities2;
1518 wsi->base.get_formats = x11_surface_get_formats;
1519 wsi->base.get_formats2 = x11_surface_get_formats2;
1520 wsi->base.get_present_modes = x11_surface_get_present_modes;
1521 wsi->base.get_present_rectangles = x11_surface_get_present_rectangles;
1522 wsi->base.create_swapchain = x11_surface_create_swapchain;
1523
1524 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1525 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1526
1527 return VK_SUCCESS;
1528
1529 fail_mutex:
1530 pthread_mutex_destroy(&wsi->mutex);
1531 fail_alloc:
1532 vk_free(alloc, wsi);
1533 fail:
1534 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1535 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1536
1537 return result;
1538 }
1539
1540 void
1541 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1542 const VkAllocationCallbacks *alloc)
1543 {
1544 struct wsi_x11 *wsi =
1545 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1546
1547 if (wsi) {
1548 struct hash_entry *entry;
1549 hash_table_foreach(wsi->connections, entry)
1550 wsi_x11_connection_destroy(wsi_device, entry->data);
1551
1552 _mesa_hash_table_destroy(wsi->connections, NULL);
1553
1554 pthread_mutex_destroy(&wsi->mutex);
1555
1556 vk_free(alloc, wsi);
1557 }
1558 }