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