vulkan/wsi/x11: don't crash on null visual
[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
37 #include <poll.h>
38 #include "util/hash_table.h"
39
40 #include "wsi_common.h"
41 #include "wsi_common_x11.h"
42 #include "wsi_common_queue.h"
43
44 #define typed_memcpy(dest, src, count) ({ \
45 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
46 memcpy((dest), (src), (count) * sizeof(*(src))); \
47 })
48
49 struct wsi_x11_connection {
50 bool has_dri3;
51 bool has_present;
52 };
53
54 struct wsi_x11 {
55 struct wsi_interface base;
56
57 pthread_mutex_t mutex;
58 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
59 struct hash_table *connections;
60 };
61
62 static struct wsi_x11_connection *
63 wsi_x11_connection_create(const VkAllocationCallbacks *alloc,
64 xcb_connection_t *conn)
65 {
66 xcb_query_extension_cookie_t dri3_cookie, pres_cookie;
67 xcb_query_extension_reply_t *dri3_reply, *pres_reply;
68
69 struct wsi_x11_connection *wsi_conn =
70 vk_alloc(alloc, sizeof(*wsi_conn), 8,
71 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
72 if (!wsi_conn)
73 return NULL;
74
75 dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
76 pres_cookie = xcb_query_extension(conn, 7, "PRESENT");
77
78 dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
79 pres_reply = xcb_query_extension_reply(conn, pres_cookie, NULL);
80 if (dri3_reply == NULL || pres_reply == NULL) {
81 free(dri3_reply);
82 free(pres_reply);
83 vk_free(alloc, wsi_conn);
84 return NULL;
85 }
86
87 wsi_conn->has_dri3 = dri3_reply->present != 0;
88 wsi_conn->has_present = pres_reply->present != 0;
89
90 free(dri3_reply);
91 free(pres_reply);
92
93 return wsi_conn;
94 }
95
96 static void
97 wsi_x11_connection_destroy(const VkAllocationCallbacks *alloc,
98 struct wsi_x11_connection *conn)
99 {
100 vk_free(alloc, conn);
101 }
102
103 static struct wsi_x11_connection *
104 wsi_x11_get_connection(struct wsi_device *wsi_dev,
105 const VkAllocationCallbacks *alloc,
106 xcb_connection_t *conn)
107 {
108 struct wsi_x11 *wsi =
109 (struct wsi_x11 *)wsi_dev->wsi[VK_ICD_WSI_PLATFORM_XCB];
110
111 pthread_mutex_lock(&wsi->mutex);
112
113 struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
114 if (!entry) {
115 /* We're about to make a bunch of blocking calls. Let's drop the
116 * mutex for now so we don't block up too badly.
117 */
118 pthread_mutex_unlock(&wsi->mutex);
119
120 struct wsi_x11_connection *wsi_conn =
121 wsi_x11_connection_create(alloc, conn);
122 if (!wsi_conn)
123 return NULL;
124
125 pthread_mutex_lock(&wsi->mutex);
126
127 entry = _mesa_hash_table_search(wsi->connections, conn);
128 if (entry) {
129 /* Oops, someone raced us to it */
130 wsi_x11_connection_destroy(alloc, wsi_conn);
131 } else {
132 entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
133 }
134 }
135
136 pthread_mutex_unlock(&wsi->mutex);
137
138 return entry->data;
139 }
140
141 static const VkSurfaceFormatKHR formats[] = {
142 { .format = VK_FORMAT_B8G8R8A8_SRGB, },
143 { .format = VK_FORMAT_B8G8R8A8_UNORM, },
144 };
145
146 static const VkPresentModeKHR present_modes[] = {
147 VK_PRESENT_MODE_IMMEDIATE_KHR,
148 VK_PRESENT_MODE_MAILBOX_KHR,
149 VK_PRESENT_MODE_FIFO_KHR,
150 };
151
152 static xcb_screen_t *
153 get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
154 {
155 xcb_screen_iterator_t screen_iter =
156 xcb_setup_roots_iterator(xcb_get_setup(conn));
157
158 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
159 if (screen_iter.data->root == root)
160 return screen_iter.data;
161 }
162
163 return NULL;
164 }
165
166 static xcb_visualtype_t *
167 screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
168 unsigned *depth)
169 {
170 xcb_depth_iterator_t depth_iter =
171 xcb_screen_allowed_depths_iterator(screen);
172
173 for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
174 xcb_visualtype_iterator_t visual_iter =
175 xcb_depth_visuals_iterator (depth_iter.data);
176
177 for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
178 if (visual_iter.data->visual_id == visual_id) {
179 if (depth)
180 *depth = depth_iter.data->depth;
181 return visual_iter.data;
182 }
183 }
184 }
185
186 return NULL;
187 }
188
189 static xcb_visualtype_t *
190 connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id,
191 unsigned *depth)
192 {
193 xcb_screen_iterator_t screen_iter =
194 xcb_setup_roots_iterator(xcb_get_setup(conn));
195
196 /* For this we have to iterate over all of the screens which is rather
197 * annoying. Fortunately, there is probably only 1.
198 */
199 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
200 xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
201 visual_id, depth);
202 if (visual)
203 return visual;
204 }
205
206 return NULL;
207 }
208
209 static xcb_visualtype_t *
210 get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
211 unsigned *depth)
212 {
213 xcb_query_tree_cookie_t tree_cookie;
214 xcb_get_window_attributes_cookie_t attrib_cookie;
215 xcb_query_tree_reply_t *tree;
216 xcb_get_window_attributes_reply_t *attrib;
217
218 tree_cookie = xcb_query_tree(conn, window);
219 attrib_cookie = xcb_get_window_attributes(conn, window);
220
221 tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
222 attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
223 if (attrib == NULL || tree == NULL) {
224 free(attrib);
225 free(tree);
226 return NULL;
227 }
228
229 xcb_window_t root = tree->root;
230 xcb_visualid_t visual_id = attrib->visual;
231 free(attrib);
232 free(tree);
233
234 xcb_screen_t *screen = get_screen_for_root(conn, root);
235 if (screen == NULL)
236 return NULL;
237
238 return screen_get_visualtype(screen, visual_id, depth);
239 }
240
241 static bool
242 visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
243 {
244 uint32_t rgb_mask = visual->red_mask |
245 visual->green_mask |
246 visual->blue_mask;
247
248 uint32_t all_mask = 0xffffffff >> (32 - depth);
249
250 /* Do we have bits left over after RGB? */
251 return (all_mask & ~rgb_mask) != 0;
252 }
253
254 VkBool32 wsi_get_physical_device_xcb_presentation_support(
255 struct wsi_device *wsi_device,
256 VkAllocationCallbacks *alloc,
257 uint32_t queueFamilyIndex,
258 xcb_connection_t* connection,
259 xcb_visualid_t visual_id)
260 {
261 struct wsi_x11_connection *wsi_conn =
262 wsi_x11_get_connection(wsi_device, alloc, connection);
263
264 if (!wsi_conn->has_dri3) {
265 fprintf(stderr, "vulkan: No DRI3 support\n");
266 return false;
267 }
268
269 unsigned visual_depth;
270 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
271 return false;
272
273 if (visual_depth != 24 && visual_depth != 32)
274 return false;
275
276 return true;
277 }
278
279 static xcb_connection_t*
280 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
281 {
282 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
283 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
284 else
285 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
286 }
287
288 static xcb_window_t
289 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
290 {
291 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
292 return ((VkIcdSurfaceXlib *)icd_surface)->window;
293 else
294 return ((VkIcdSurfaceXcb *)icd_surface)->window;
295 }
296
297 static VkResult
298 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
299 struct wsi_device *wsi_device,
300 const VkAllocationCallbacks *alloc,
301 uint32_t queueFamilyIndex,
302 VkBool32* pSupported)
303 {
304 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
305 xcb_window_t window = x11_surface_get_window(icd_surface);
306
307 struct wsi_x11_connection *wsi_conn =
308 wsi_x11_get_connection(wsi_device, alloc, conn);
309 if (!wsi_conn)
310 return VK_ERROR_OUT_OF_HOST_MEMORY;
311
312 if (!wsi_conn->has_dri3) {
313 fprintf(stderr, "vulkan: No DRI3 support\n");
314 *pSupported = false;
315 return VK_SUCCESS;
316 }
317
318 unsigned visual_depth;
319 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
320 *pSupported = false;
321 return VK_SUCCESS;
322 }
323
324 if (visual_depth != 24 && visual_depth != 32) {
325 *pSupported = false;
326 return VK_SUCCESS;
327 }
328
329 *pSupported = true;
330 return VK_SUCCESS;
331 }
332
333 static VkResult
334 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
335 VkSurfaceCapabilitiesKHR *caps)
336 {
337 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
338 xcb_window_t window = x11_surface_get_window(icd_surface);
339 xcb_get_geometry_cookie_t geom_cookie;
340 xcb_generic_error_t *err;
341 xcb_get_geometry_reply_t *geom;
342 unsigned visual_depth;
343
344 geom_cookie = xcb_get_geometry(conn, window);
345
346 /* This does a round-trip. This is why we do get_geometry first and
347 * wait to read the reply until after we have a visual.
348 */
349 xcb_visualtype_t *visual =
350 get_visualtype_for_window(conn, window, &visual_depth);
351
352 if (!visual)
353 return VK_ERROR_SURFACE_LOST_KHR;
354
355 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
356 if (geom) {
357 VkExtent2D extent = { geom->width, geom->height };
358 caps->currentExtent = extent;
359 caps->minImageExtent = extent;
360 caps->maxImageExtent = extent;
361 } else {
362 /* This can happen if the client didn't wait for the configure event
363 * to come back from the compositor. In that case, we don't know the
364 * size of the window so we just return valid "I don't know" stuff.
365 */
366 caps->currentExtent = (VkExtent2D) { -1, -1 };
367 caps->minImageExtent = (VkExtent2D) { 1, 1 };
368 caps->maxImageExtent = (VkExtent2D) { INT16_MAX, INT16_MAX };
369 }
370 free(err);
371 free(geom);
372
373 if (visual_has_alpha(visual, visual_depth)) {
374 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
375 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
376 } else {
377 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
378 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
379 }
380
381 /* For true mailbox mode, we need at least 4 images:
382 * 1) One to scan out from
383 * 2) One to have queued for scan-out
384 * 3) One to be currently held by the X server
385 * 4) One to render to
386 */
387 caps->minImageCount = 2;
388 /* There is no real maximum */
389 caps->maxImageCount = 0;
390
391 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
392 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
393 caps->maxImageArrayLayers = 1;
394 caps->supportedUsageFlags =
395 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
396 VK_IMAGE_USAGE_SAMPLED_BIT |
397 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
398 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
399
400 return VK_SUCCESS;
401 }
402
403 static VkResult
404 x11_surface_get_formats(VkIcdSurfaceBase *surface,
405 struct wsi_device *wsi_device,
406 uint32_t *pSurfaceFormatCount,
407 VkSurfaceFormatKHR *pSurfaceFormats)
408 {
409 if (pSurfaceFormats == NULL) {
410 *pSurfaceFormatCount = ARRAY_SIZE(formats);
411 return VK_SUCCESS;
412 }
413
414 *pSurfaceFormatCount = MIN2(*pSurfaceFormatCount, ARRAY_SIZE(formats));
415 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
416
417 return *pSurfaceFormatCount < ARRAY_SIZE(formats) ?
418 VK_INCOMPLETE : VK_SUCCESS;
419 }
420
421 static VkResult
422 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
423 uint32_t *pPresentModeCount,
424 VkPresentModeKHR *pPresentModes)
425 {
426 if (pPresentModes == NULL) {
427 *pPresentModeCount = ARRAY_SIZE(present_modes);
428 return VK_SUCCESS;
429 }
430
431 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
432 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
433
434 return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
435 VK_INCOMPLETE : VK_SUCCESS;
436 }
437
438 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
439 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
440 VkSurfaceKHR *pSurface)
441 {
442 VkIcdSurfaceXcb *surface;
443
444 surface = vk_alloc(pAllocator, sizeof *surface, 8,
445 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
446 if (surface == NULL)
447 return VK_ERROR_OUT_OF_HOST_MEMORY;
448
449 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
450 surface->connection = pCreateInfo->connection;
451 surface->window = pCreateInfo->window;
452
453 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
454 return VK_SUCCESS;
455 }
456
457 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
458 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
459 VkSurfaceKHR *pSurface)
460 {
461 VkIcdSurfaceXlib *surface;
462
463 surface = vk_alloc(pAllocator, sizeof *surface, 8,
464 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
465 if (surface == NULL)
466 return VK_ERROR_OUT_OF_HOST_MEMORY;
467
468 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
469 surface->dpy = pCreateInfo->dpy;
470 surface->window = pCreateInfo->window;
471
472 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
473 return VK_SUCCESS;
474 }
475
476 struct x11_image {
477 VkImage image;
478 VkDeviceMemory memory;
479 xcb_pixmap_t pixmap;
480 bool busy;
481 struct xshmfence * shm_fence;
482 uint32_t sync_fence;
483 };
484
485 struct x11_swapchain {
486 struct wsi_swapchain base;
487
488 xcb_connection_t * conn;
489 xcb_window_t window;
490 xcb_gc_t gc;
491 uint32_t depth;
492 VkExtent2D extent;
493 uint32_t image_count;
494
495 xcb_present_event_t event_id;
496 xcb_special_event_t * special_event;
497 uint64_t send_sbc;
498 uint64_t last_present_msc;
499 uint32_t stamp;
500
501 bool threaded;
502 VkResult status;
503 struct wsi_queue present_queue;
504 struct wsi_queue acquire_queue;
505 pthread_t queue_manager;
506
507 struct x11_image images[0];
508 };
509
510 static VkResult
511 x11_get_images(struct wsi_swapchain *anv_chain,
512 uint32_t* pCount, VkImage *pSwapchainImages)
513 {
514 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
515 uint32_t ret_count;
516 VkResult result;
517
518 if (pSwapchainImages == NULL) {
519 *pCount = chain->image_count;
520 return VK_SUCCESS;
521 }
522
523 result = VK_SUCCESS;
524 ret_count = chain->image_count;
525 if (chain->image_count > *pCount) {
526 ret_count = *pCount;
527 result = VK_INCOMPLETE;
528 }
529
530 for (uint32_t i = 0; i < ret_count; i++)
531 pSwapchainImages[i] = chain->images[i].image;
532
533 return result;
534 }
535
536 static VkResult
537 x11_handle_dri3_present_event(struct x11_swapchain *chain,
538 xcb_present_generic_event_t *event)
539 {
540 switch (event->evtype) {
541 case XCB_PRESENT_CONFIGURE_NOTIFY: {
542 xcb_present_configure_notify_event_t *config = (void *) event;
543
544 if (config->width != chain->extent.width ||
545 config->height != chain->extent.height)
546 return VK_ERROR_OUT_OF_DATE_KHR;
547
548 break;
549 }
550
551 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
552 xcb_present_idle_notify_event_t *idle = (void *) event;
553
554 for (unsigned i = 0; i < chain->image_count; i++) {
555 if (chain->images[i].pixmap == idle->pixmap) {
556 chain->images[i].busy = false;
557 if (chain->threaded)
558 wsi_queue_push(&chain->acquire_queue, i);
559 break;
560 }
561 }
562
563 break;
564 }
565
566 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
567 xcb_present_complete_notify_event_t *complete = (void *) event;
568 if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
569 chain->last_present_msc = complete->msc;
570 break;
571 }
572
573 default:
574 break;
575 }
576
577 return VK_SUCCESS;
578 }
579
580
581 static uint64_t wsi_get_current_time(void)
582 {
583 uint64_t current_time;
584 struct timespec tv;
585
586 clock_gettime(CLOCK_MONOTONIC, &tv);
587 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
588 return current_time;
589 }
590
591 static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
592 {
593 uint64_t current_time = wsi_get_current_time();
594
595 timeout = MIN2(UINT64_MAX - current_time, timeout);
596
597 return current_time + timeout;
598 }
599
600 static VkResult
601 x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
602 uint32_t *image_index, uint64_t timeout)
603 {
604 xcb_generic_event_t *event;
605 struct pollfd pfds;
606 uint64_t atimeout;
607 while (1) {
608 for (uint32_t i = 0; i < chain->image_count; i++) {
609 if (!chain->images[i].busy) {
610 /* We found a non-busy image */
611 xshmfence_await(chain->images[i].shm_fence);
612 *image_index = i;
613 chain->images[i].busy = true;
614 return VK_SUCCESS;
615 }
616 }
617
618 xcb_flush(chain->conn);
619
620 if (timeout == UINT64_MAX) {
621 event = xcb_wait_for_special_event(chain->conn, chain->special_event);
622 if (!event)
623 return VK_ERROR_OUT_OF_DATE_KHR;
624 } else {
625 event = xcb_poll_for_special_event(chain->conn, chain->special_event);
626 if (!event) {
627 int ret;
628 if (timeout == 0)
629 return VK_NOT_READY;
630
631 atimeout = wsi_get_absolute_timeout(timeout);
632
633 pfds.fd = xcb_get_file_descriptor(chain->conn);
634 pfds.events = POLLIN;
635 ret = poll(&pfds, 1, timeout / 1000 / 1000);
636 if (ret == 0)
637 return VK_TIMEOUT;
638 if (ret == -1)
639 return VK_ERROR_OUT_OF_DATE_KHR;
640
641 /* If a non-special event happens, the fd will still
642 * poll. So recalculate the timeout now just in case.
643 */
644 uint64_t current_time = wsi_get_current_time();
645 if (atimeout > current_time)
646 timeout = atimeout - current_time;
647 else
648 timeout = 0;
649 continue;
650 }
651 }
652
653 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
654 free(event);
655 if (result != VK_SUCCESS)
656 return result;
657 }
658 }
659
660 static VkResult
661 x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
662 uint32_t *image_index_out, uint64_t timeout)
663 {
664 assert(chain->threaded);
665
666 uint32_t image_index;
667 VkResult result = wsi_queue_pull(&chain->acquire_queue,
668 &image_index, timeout);
669 if (result != VK_SUCCESS) {
670 return result;
671 } else if (chain->status != VK_SUCCESS) {
672 return chain->status;
673 }
674
675 assert(image_index < chain->image_count);
676 xshmfence_await(chain->images[image_index].shm_fence);
677
678 *image_index_out = image_index;
679
680 return VK_SUCCESS;
681 }
682
683 static VkResult
684 x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
685 uint32_t target_msc)
686 {
687 struct x11_image *image = &chain->images[image_index];
688
689 assert(image_index < chain->image_count);
690
691 uint32_t options = XCB_PRESENT_OPTION_NONE;
692
693 int64_t divisor = 0;
694 int64_t remainder = 0;
695
696 if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
697 options |= XCB_PRESENT_OPTION_ASYNC;
698
699 xshmfence_reset(image->shm_fence);
700
701 ++chain->send_sbc;
702 xcb_void_cookie_t cookie =
703 xcb_present_pixmap(chain->conn,
704 chain->window,
705 image->pixmap,
706 (uint32_t) chain->send_sbc,
707 0, /* valid */
708 0, /* update */
709 0, /* x_off */
710 0, /* y_off */
711 XCB_NONE, /* target_crtc */
712 XCB_NONE,
713 image->sync_fence,
714 options,
715 target_msc,
716 divisor,
717 remainder, 0, NULL);
718 xcb_discard_reply(chain->conn, cookie.sequence);
719 image->busy = true;
720
721 xcb_flush(chain->conn);
722
723 return VK_SUCCESS;
724 }
725
726 static VkResult
727 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
728 uint64_t timeout,
729 VkSemaphore semaphore,
730 uint32_t *image_index)
731 {
732 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
733
734 if (chain->threaded) {
735 return x11_acquire_next_image_from_queue(chain, image_index, timeout);
736 } else {
737 return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
738 }
739 }
740
741 static VkResult
742 x11_queue_present(struct wsi_swapchain *anv_chain,
743 uint32_t image_index)
744 {
745 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
746
747 if (chain->threaded) {
748 wsi_queue_push(&chain->present_queue, image_index);
749 return chain->status;
750 } else {
751 return x11_present_to_x11(chain, image_index, 0);
752 }
753 }
754
755 static void *
756 x11_manage_fifo_queues(void *state)
757 {
758 struct x11_swapchain *chain = state;
759 VkResult result;
760
761 assert(chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR);
762
763 while (chain->status == VK_SUCCESS) {
764 /* It should be safe to unconditionally block here. Later in the loop
765 * we blocks until the previous present has landed on-screen. At that
766 * point, we should have received IDLE_NOTIFY on all images presented
767 * before that point so the client should be able to acquire any image
768 * other than the currently presented one.
769 */
770 uint32_t image_index;
771 result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
772 if (result != VK_SUCCESS) {
773 goto fail;
774 } else if (chain->status != VK_SUCCESS) {
775 return NULL;
776 }
777
778 uint64_t target_msc = chain->last_present_msc + 1;
779 result = x11_present_to_x11(chain, image_index, target_msc);
780 if (result != VK_SUCCESS)
781 goto fail;
782
783 while (chain->last_present_msc < target_msc) {
784 xcb_generic_event_t *event =
785 xcb_wait_for_special_event(chain->conn, chain->special_event);
786 if (!event)
787 goto fail;
788
789 result = x11_handle_dri3_present_event(chain, (void *)event);
790 if (result != VK_SUCCESS)
791 goto fail;
792 }
793 }
794
795 fail:
796 chain->status = result;
797 wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
798
799 return NULL;
800 }
801
802 static VkResult
803 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
804 const VkSwapchainCreateInfoKHR *pCreateInfo,
805 const VkAllocationCallbacks* pAllocator,
806 struct x11_image *image)
807 {
808 xcb_void_cookie_t cookie;
809 VkResult result;
810 uint32_t row_pitch;
811 uint32_t offset;
812 uint32_t bpp = 32;
813 int fd;
814 uint32_t size;
815
816 result = chain->base.image_fns->create_wsi_image(device_h,
817 pCreateInfo,
818 pAllocator,
819 &image->image,
820 &image->memory,
821 &size,
822 &offset,
823 &row_pitch,
824 &fd);
825 if (result != VK_SUCCESS)
826 return result;
827
828 image->pixmap = xcb_generate_id(chain->conn);
829
830 cookie =
831 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
832 image->pixmap,
833 chain->window,
834 size,
835 pCreateInfo->imageExtent.width,
836 pCreateInfo->imageExtent.height,
837 row_pitch,
838 chain->depth, bpp, fd);
839 xcb_discard_reply(chain->conn, cookie.sequence);
840
841 int fence_fd = xshmfence_alloc_shm();
842 if (fence_fd < 0)
843 goto fail_pixmap;
844
845 image->shm_fence = xshmfence_map_shm(fence_fd);
846 if (image->shm_fence == NULL)
847 goto fail_shmfence_alloc;
848
849 image->sync_fence = xcb_generate_id(chain->conn);
850 xcb_dri3_fence_from_fd(chain->conn,
851 image->pixmap,
852 image->sync_fence,
853 false,
854 fence_fd);
855
856 image->busy = false;
857 xshmfence_trigger(image->shm_fence);
858
859 return VK_SUCCESS;
860
861 fail_shmfence_alloc:
862 close(fence_fd);
863
864 fail_pixmap:
865 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
866 xcb_discard_reply(chain->conn, cookie.sequence);
867
868 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
869 image->image, image->memory);
870
871 return result;
872 }
873
874 static void
875 x11_image_finish(struct x11_swapchain *chain,
876 const VkAllocationCallbacks* pAllocator,
877 struct x11_image *image)
878 {
879 xcb_void_cookie_t cookie;
880
881 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
882 xcb_discard_reply(chain->conn, cookie.sequence);
883 xshmfence_unmap_shm(image->shm_fence);
884
885 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
886 xcb_discard_reply(chain->conn, cookie.sequence);
887
888 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
889 image->image, image->memory);
890 }
891
892 static VkResult
893 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
894 const VkAllocationCallbacks *pAllocator)
895 {
896 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
897 xcb_void_cookie_t cookie;
898
899 for (uint32_t i = 0; i < chain->image_count; i++)
900 x11_image_finish(chain, pAllocator, &chain->images[i]);
901
902 if (chain->threaded) {
903 chain->status = VK_ERROR_OUT_OF_DATE_KHR;
904 /* Push a UINT32_MAX to wake up the manager */
905 wsi_queue_push(&chain->present_queue, UINT32_MAX);
906 pthread_join(chain->queue_manager, NULL);
907 wsi_queue_destroy(&chain->acquire_queue);
908 wsi_queue_destroy(&chain->present_queue);
909 }
910
911 xcb_unregister_for_special_event(chain->conn, chain->special_event);
912 cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
913 chain->window,
914 XCB_PRESENT_EVENT_MASK_NO_EVENT);
915 xcb_discard_reply(chain->conn, cookie.sequence);
916
917 vk_free(pAllocator, chain);
918
919 return VK_SUCCESS;
920 }
921
922 static VkResult
923 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
924 VkDevice device,
925 struct wsi_device *wsi_device,
926 const VkSwapchainCreateInfoKHR *pCreateInfo,
927 const VkAllocationCallbacks* pAllocator,
928 const struct wsi_image_fns *image_fns,
929 struct wsi_swapchain **swapchain_out)
930 {
931 struct x11_swapchain *chain;
932 xcb_void_cookie_t cookie;
933 VkResult result;
934
935 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
936
937 const unsigned num_images = pCreateInfo->minImageCount;
938
939 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
940 chain = vk_alloc(pAllocator, size, 8,
941 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
942 if (chain == NULL)
943 return VK_ERROR_OUT_OF_HOST_MEMORY;
944
945 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
946 xcb_window_t window = x11_surface_get_window(icd_surface);
947 xcb_get_geometry_reply_t *geometry =
948 xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
949
950 if (geometry == NULL)
951 return VK_ERROR_SURFACE_LOST_KHR;
952
953 chain->base.device = device;
954 chain->base.destroy = x11_swapchain_destroy;
955 chain->base.get_images = x11_get_images;
956 chain->base.acquire_next_image = x11_acquire_next_image;
957 chain->base.queue_present = x11_queue_present;
958 chain->base.image_fns = image_fns;
959 chain->base.present_mode = pCreateInfo->presentMode;
960 chain->conn = conn;
961 chain->window = window;
962 chain->depth = geometry->depth;
963 chain->extent = pCreateInfo->imageExtent;
964 chain->image_count = num_images;
965 chain->send_sbc = 0;
966 chain->last_present_msc = 0;
967 chain->threaded = false;
968 chain->status = VK_SUCCESS;
969
970 free(geometry);
971
972 chain->event_id = xcb_generate_id(chain->conn);
973 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
974 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
975 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
976 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
977
978 /* Create an XCB event queue to hold present events outside of the usual
979 * application event queue
980 */
981 chain->special_event =
982 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
983 chain->event_id, NULL);
984
985 chain->gc = xcb_generate_id(chain->conn);
986 if (!chain->gc) {
987 /* FINISHME: Choose a better error. */
988 result = VK_ERROR_OUT_OF_HOST_MEMORY;
989 goto fail_register;
990 }
991
992 cookie = xcb_create_gc(chain->conn,
993 chain->gc,
994 chain->window,
995 XCB_GC_GRAPHICS_EXPOSURES,
996 (uint32_t []) { 0 });
997 xcb_discard_reply(chain->conn, cookie.sequence);
998
999 uint32_t image = 0;
1000 for (; image < chain->image_count; image++) {
1001 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1002 &chain->images[image]);
1003 if (result != VK_SUCCESS)
1004 goto fail_init_images;
1005 }
1006
1007 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
1008 chain->threaded = true;
1009
1010 /* Initialize our queues. We make them image_count + 1 because we will
1011 * occasionally use UINT32_MAX to signal the other thread that an error
1012 * has occurred and we don't want an overflow.
1013 */
1014 int ret;
1015 ret = wsi_queue_init(&chain->acquire_queue, chain->image_count + 1);
1016 if (ret) {
1017 goto fail_init_images;
1018 }
1019
1020 ret = wsi_queue_init(&chain->present_queue, chain->image_count + 1);
1021 if (ret) {
1022 wsi_queue_destroy(&chain->acquire_queue);
1023 goto fail_init_images;
1024 }
1025
1026 for (unsigned i = 0; i < chain->image_count; i++)
1027 wsi_queue_push(&chain->acquire_queue, i);
1028
1029 ret = pthread_create(&chain->queue_manager, NULL,
1030 x11_manage_fifo_queues, chain);
1031 if (ret) {
1032 wsi_queue_destroy(&chain->present_queue);
1033 wsi_queue_destroy(&chain->acquire_queue);
1034 goto fail_init_images;
1035 }
1036 }
1037
1038 *swapchain_out = &chain->base;
1039
1040 return VK_SUCCESS;
1041
1042 fail_init_images:
1043 for (uint32_t j = 0; j < image; j++)
1044 x11_image_finish(chain, pAllocator, &chain->images[j]);
1045
1046 fail_register:
1047 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1048
1049 vk_free(pAllocator, chain);
1050
1051 return result;
1052 }
1053
1054 VkResult
1055 wsi_x11_init_wsi(struct wsi_device *wsi_device,
1056 const VkAllocationCallbacks *alloc)
1057 {
1058 struct wsi_x11 *wsi;
1059 VkResult result;
1060
1061 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1062 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1063 if (!wsi) {
1064 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1065 goto fail;
1066 }
1067
1068 int ret = pthread_mutex_init(&wsi->mutex, NULL);
1069 if (ret != 0) {
1070 if (ret == ENOMEM) {
1071 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1072 } else {
1073 /* FINISHME: Choose a better error. */
1074 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1075 }
1076
1077 goto fail_alloc;
1078 }
1079
1080 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1081 _mesa_key_pointer_equal);
1082 if (!wsi->connections) {
1083 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1084 goto fail_mutex;
1085 }
1086
1087 wsi->base.get_support = x11_surface_get_support;
1088 wsi->base.get_capabilities = x11_surface_get_capabilities;
1089 wsi->base.get_formats = x11_surface_get_formats;
1090 wsi->base.get_present_modes = x11_surface_get_present_modes;
1091 wsi->base.create_swapchain = x11_surface_create_swapchain;
1092
1093 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1094 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1095
1096 return VK_SUCCESS;
1097
1098 fail_mutex:
1099 pthread_mutex_destroy(&wsi->mutex);
1100 fail_alloc:
1101 vk_free(alloc, wsi);
1102 fail:
1103 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1104 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1105
1106 return result;
1107 }
1108
1109 void
1110 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1111 const VkAllocationCallbacks *alloc)
1112 {
1113 struct wsi_x11 *wsi =
1114 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1115
1116 if (wsi) {
1117 struct hash_entry *entry;
1118 hash_table_foreach(wsi->connections, entry)
1119 wsi_x11_connection_destroy(alloc, entry->data);
1120
1121 _mesa_hash_table_destroy(wsi->connections, NULL);
1122
1123 pthread_mutex_destroy(&wsi->mutex);
1124
1125 vk_free(alloc, wsi);
1126 }
1127 }