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