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