vulkan/wsi: Lower the maximum image sizes
[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)
265 return false;
266
267 if (!wsi_conn->has_dri3) {
268 fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n");
269 fprintf(stderr, "Note: Buggy applications may crash, if they do please report to vendor\n");
270 return false;
271 }
272
273 unsigned visual_depth;
274 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
275 return false;
276
277 if (visual_depth != 24 && visual_depth != 32)
278 return false;
279
280 return true;
281 }
282
283 static xcb_connection_t*
284 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
285 {
286 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
287 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
288 else
289 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
290 }
291
292 static xcb_window_t
293 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
294 {
295 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
296 return ((VkIcdSurfaceXlib *)icd_surface)->window;
297 else
298 return ((VkIcdSurfaceXcb *)icd_surface)->window;
299 }
300
301 static VkResult
302 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
303 struct wsi_device *wsi_device,
304 const VkAllocationCallbacks *alloc,
305 uint32_t queueFamilyIndex,
306 VkBool32* pSupported)
307 {
308 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
309 xcb_window_t window = x11_surface_get_window(icd_surface);
310
311 struct wsi_x11_connection *wsi_conn =
312 wsi_x11_get_connection(wsi_device, alloc, conn);
313 if (!wsi_conn)
314 return VK_ERROR_OUT_OF_HOST_MEMORY;
315
316 if (!wsi_conn->has_dri3) {
317 fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n");
318 fprintf(stderr, "Note: Buggy applications may crash, if they do please report to vendor\n");
319 *pSupported = false;
320 return VK_SUCCESS;
321 }
322
323 unsigned visual_depth;
324 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
325 *pSupported = false;
326 return VK_SUCCESS;
327 }
328
329 if (visual_depth != 24 && visual_depth != 32) {
330 *pSupported = false;
331 return VK_SUCCESS;
332 }
333
334 *pSupported = true;
335 return VK_SUCCESS;
336 }
337
338 static VkResult
339 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
340 VkSurfaceCapabilitiesKHR *caps)
341 {
342 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
343 xcb_window_t window = x11_surface_get_window(icd_surface);
344 xcb_get_geometry_cookie_t geom_cookie;
345 xcb_generic_error_t *err;
346 xcb_get_geometry_reply_t *geom;
347 unsigned visual_depth;
348
349 geom_cookie = xcb_get_geometry(conn, window);
350
351 /* This does a round-trip. This is why we do get_geometry first and
352 * wait to read the reply until after we have a visual.
353 */
354 xcb_visualtype_t *visual =
355 get_visualtype_for_window(conn, window, &visual_depth);
356
357 if (!visual)
358 return VK_ERROR_SURFACE_LOST_KHR;
359
360 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
361 if (geom) {
362 VkExtent2D extent = { geom->width, geom->height };
363 caps->currentExtent = extent;
364 caps->minImageExtent = extent;
365 caps->maxImageExtent = extent;
366 } else {
367 /* This can happen if the client didn't wait for the configure event
368 * to come back from the compositor. In that case, we don't know the
369 * size of the window so we just return valid "I don't know" stuff.
370 */
371 caps->currentExtent = (VkExtent2D) { -1, -1 };
372 caps->minImageExtent = (VkExtent2D) { 1, 1 };
373 /* This is the maximum supported size on Intel */
374 caps->maxImageExtent = (VkExtent2D) { 1 << 14, 1 << 14 };
375 }
376 free(err);
377 free(geom);
378
379 if (visual_has_alpha(visual, visual_depth)) {
380 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
381 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
382 } else {
383 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
384 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
385 }
386
387 /* For true mailbox mode, we need at least 4 images:
388 * 1) One to scan out from
389 * 2) One to have queued for scan-out
390 * 3) One to be currently held by the X server
391 * 4) One to render to
392 */
393 caps->minImageCount = 2;
394 /* There is no real maximum */
395 caps->maxImageCount = 0;
396
397 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
398 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
399 caps->maxImageArrayLayers = 1;
400 caps->supportedUsageFlags =
401 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
402 VK_IMAGE_USAGE_SAMPLED_BIT |
403 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
404 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
405
406 return VK_SUCCESS;
407 }
408
409 static VkResult
410 x11_surface_get_formats(VkIcdSurfaceBase *surface,
411 struct wsi_device *wsi_device,
412 uint32_t *pSurfaceFormatCount,
413 VkSurfaceFormatKHR *pSurfaceFormats)
414 {
415 if (pSurfaceFormats == NULL) {
416 *pSurfaceFormatCount = ARRAY_SIZE(formats);
417 return VK_SUCCESS;
418 }
419
420 *pSurfaceFormatCount = MIN2(*pSurfaceFormatCount, ARRAY_SIZE(formats));
421 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
422
423 return *pSurfaceFormatCount < ARRAY_SIZE(formats) ?
424 VK_INCOMPLETE : VK_SUCCESS;
425 }
426
427 static VkResult
428 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
429 uint32_t *pPresentModeCount,
430 VkPresentModeKHR *pPresentModes)
431 {
432 if (pPresentModes == NULL) {
433 *pPresentModeCount = ARRAY_SIZE(present_modes);
434 return VK_SUCCESS;
435 }
436
437 *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
438 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
439
440 return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
441 VK_INCOMPLETE : VK_SUCCESS;
442 }
443
444 VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
445 const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
446 VkSurfaceKHR *pSurface)
447 {
448 VkIcdSurfaceXcb *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_XCB;
456 surface->connection = pCreateInfo->connection;
457 surface->window = pCreateInfo->window;
458
459 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
460 return VK_SUCCESS;
461 }
462
463 VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
464 const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
465 VkSurfaceKHR *pSurface)
466 {
467 VkIcdSurfaceXlib *surface;
468
469 surface = vk_alloc(pAllocator, sizeof *surface, 8,
470 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
471 if (surface == NULL)
472 return VK_ERROR_OUT_OF_HOST_MEMORY;
473
474 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
475 surface->dpy = pCreateInfo->dpy;
476 surface->window = pCreateInfo->window;
477
478 *pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
479 return VK_SUCCESS;
480 }
481
482 struct x11_image {
483 VkImage image;
484 VkDeviceMemory memory;
485 xcb_pixmap_t pixmap;
486 bool busy;
487 struct xshmfence * shm_fence;
488 uint32_t sync_fence;
489 };
490
491 struct x11_swapchain {
492 struct wsi_swapchain base;
493
494 xcb_connection_t * conn;
495 xcb_window_t window;
496 xcb_gc_t gc;
497 uint32_t depth;
498 VkExtent2D extent;
499 uint32_t image_count;
500
501 xcb_present_event_t event_id;
502 xcb_special_event_t * special_event;
503 uint64_t send_sbc;
504 uint64_t last_present_msc;
505 uint32_t stamp;
506
507 bool threaded;
508 VkResult status;
509 struct wsi_queue present_queue;
510 struct wsi_queue acquire_queue;
511 pthread_t queue_manager;
512
513 struct x11_image images[0];
514 };
515
516 static VkResult
517 x11_get_images(struct wsi_swapchain *anv_chain,
518 uint32_t* pCount, VkImage *pSwapchainImages)
519 {
520 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
521 uint32_t ret_count;
522 VkResult result;
523
524 if (pSwapchainImages == NULL) {
525 *pCount = chain->image_count;
526 return VK_SUCCESS;
527 }
528
529 result = VK_SUCCESS;
530 ret_count = chain->image_count;
531 if (chain->image_count > *pCount) {
532 ret_count = *pCount;
533 result = VK_INCOMPLETE;
534 }
535
536 for (uint32_t i = 0; i < ret_count; i++)
537 pSwapchainImages[i] = chain->images[i].image;
538
539 return result;
540 }
541
542 static VkResult
543 x11_handle_dri3_present_event(struct x11_swapchain *chain,
544 xcb_present_generic_event_t *event)
545 {
546 switch (event->evtype) {
547 case XCB_PRESENT_CONFIGURE_NOTIFY: {
548 xcb_present_configure_notify_event_t *config = (void *) event;
549
550 if (config->width != chain->extent.width ||
551 config->height != chain->extent.height)
552 return VK_ERROR_OUT_OF_DATE_KHR;
553
554 break;
555 }
556
557 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
558 xcb_present_idle_notify_event_t *idle = (void *) event;
559
560 for (unsigned i = 0; i < chain->image_count; i++) {
561 if (chain->images[i].pixmap == idle->pixmap) {
562 chain->images[i].busy = false;
563 if (chain->threaded)
564 wsi_queue_push(&chain->acquire_queue, i);
565 break;
566 }
567 }
568
569 break;
570 }
571
572 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
573 xcb_present_complete_notify_event_t *complete = (void *) event;
574 if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
575 chain->last_present_msc = complete->msc;
576 break;
577 }
578
579 default:
580 break;
581 }
582
583 return VK_SUCCESS;
584 }
585
586
587 static uint64_t wsi_get_current_time(void)
588 {
589 uint64_t current_time;
590 struct timespec tv;
591
592 clock_gettime(CLOCK_MONOTONIC, &tv);
593 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
594 return current_time;
595 }
596
597 static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
598 {
599 uint64_t current_time = wsi_get_current_time();
600
601 timeout = MIN2(UINT64_MAX - current_time, timeout);
602
603 return current_time + timeout;
604 }
605
606 static VkResult
607 x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
608 uint32_t *image_index, uint64_t timeout)
609 {
610 xcb_generic_event_t *event;
611 struct pollfd pfds;
612 uint64_t atimeout;
613 while (1) {
614 for (uint32_t i = 0; i < chain->image_count; i++) {
615 if (!chain->images[i].busy) {
616 /* We found a non-busy image */
617 xshmfence_await(chain->images[i].shm_fence);
618 *image_index = i;
619 chain->images[i].busy = true;
620 return VK_SUCCESS;
621 }
622 }
623
624 xcb_flush(chain->conn);
625
626 if (timeout == UINT64_MAX) {
627 event = xcb_wait_for_special_event(chain->conn, chain->special_event);
628 if (!event)
629 return VK_ERROR_OUT_OF_DATE_KHR;
630 } else {
631 event = xcb_poll_for_special_event(chain->conn, chain->special_event);
632 if (!event) {
633 int ret;
634 if (timeout == 0)
635 return VK_NOT_READY;
636
637 atimeout = wsi_get_absolute_timeout(timeout);
638
639 pfds.fd = xcb_get_file_descriptor(chain->conn);
640 pfds.events = POLLIN;
641 ret = poll(&pfds, 1, timeout / 1000 / 1000);
642 if (ret == 0)
643 return VK_TIMEOUT;
644 if (ret == -1)
645 return VK_ERROR_OUT_OF_DATE_KHR;
646
647 /* If a non-special event happens, the fd will still
648 * poll. So recalculate the timeout now just in case.
649 */
650 uint64_t current_time = wsi_get_current_time();
651 if (atimeout > current_time)
652 timeout = atimeout - current_time;
653 else
654 timeout = 0;
655 continue;
656 }
657 }
658
659 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
660 free(event);
661 if (result != VK_SUCCESS)
662 return result;
663 }
664 }
665
666 static VkResult
667 x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
668 uint32_t *image_index_out, uint64_t timeout)
669 {
670 assert(chain->threaded);
671
672 uint32_t image_index;
673 VkResult result = wsi_queue_pull(&chain->acquire_queue,
674 &image_index, timeout);
675 if (result != VK_SUCCESS) {
676 return result;
677 } else if (chain->status != VK_SUCCESS) {
678 return chain->status;
679 }
680
681 assert(image_index < chain->image_count);
682 xshmfence_await(chain->images[image_index].shm_fence);
683
684 *image_index_out = image_index;
685
686 return VK_SUCCESS;
687 }
688
689 static VkResult
690 x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
691 uint32_t target_msc)
692 {
693 struct x11_image *image = &chain->images[image_index];
694
695 assert(image_index < chain->image_count);
696
697 uint32_t options = XCB_PRESENT_OPTION_NONE;
698
699 int64_t divisor = 0;
700 int64_t remainder = 0;
701
702 if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
703 options |= XCB_PRESENT_OPTION_ASYNC;
704
705 xshmfence_reset(image->shm_fence);
706
707 ++chain->send_sbc;
708 xcb_void_cookie_t cookie =
709 xcb_present_pixmap(chain->conn,
710 chain->window,
711 image->pixmap,
712 (uint32_t) chain->send_sbc,
713 0, /* valid */
714 0, /* update */
715 0, /* x_off */
716 0, /* y_off */
717 XCB_NONE, /* target_crtc */
718 XCB_NONE,
719 image->sync_fence,
720 options,
721 target_msc,
722 divisor,
723 remainder, 0, NULL);
724 xcb_discard_reply(chain->conn, cookie.sequence);
725 image->busy = true;
726
727 xcb_flush(chain->conn);
728
729 return VK_SUCCESS;
730 }
731
732 static VkResult
733 x11_acquire_next_image(struct wsi_swapchain *anv_chain,
734 uint64_t timeout,
735 VkSemaphore semaphore,
736 uint32_t *image_index)
737 {
738 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
739
740 if (chain->threaded) {
741 return x11_acquire_next_image_from_queue(chain, image_index, timeout);
742 } else {
743 return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
744 }
745 }
746
747 static VkResult
748 x11_queue_present(struct wsi_swapchain *anv_chain,
749 uint32_t image_index)
750 {
751 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
752
753 if (chain->threaded) {
754 wsi_queue_push(&chain->present_queue, image_index);
755 return chain->status;
756 } else {
757 return x11_present_to_x11(chain, image_index, 0);
758 }
759 }
760
761 static void *
762 x11_manage_fifo_queues(void *state)
763 {
764 struct x11_swapchain *chain = state;
765 VkResult result;
766
767 assert(chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR);
768
769 while (chain->status == VK_SUCCESS) {
770 /* It should be safe to unconditionally block here. Later in the loop
771 * we blocks until the previous present has landed on-screen. At that
772 * point, we should have received IDLE_NOTIFY on all images presented
773 * before that point so the client should be able to acquire any image
774 * other than the currently presented one.
775 */
776 uint32_t image_index;
777 result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
778 if (result != VK_SUCCESS) {
779 goto fail;
780 } else if (chain->status != VK_SUCCESS) {
781 return NULL;
782 }
783
784 uint64_t target_msc = chain->last_present_msc + 1;
785 result = x11_present_to_x11(chain, image_index, target_msc);
786 if (result != VK_SUCCESS)
787 goto fail;
788
789 while (chain->last_present_msc < target_msc) {
790 xcb_generic_event_t *event =
791 xcb_wait_for_special_event(chain->conn, chain->special_event);
792 if (!event)
793 goto fail;
794
795 result = x11_handle_dri3_present_event(chain, (void *)event);
796 if (result != VK_SUCCESS)
797 goto fail;
798 }
799 }
800
801 fail:
802 chain->status = result;
803 wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
804
805 return NULL;
806 }
807
808 static VkResult
809 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
810 const VkSwapchainCreateInfoKHR *pCreateInfo,
811 const VkAllocationCallbacks* pAllocator,
812 struct x11_image *image)
813 {
814 xcb_void_cookie_t cookie;
815 VkResult result;
816 uint32_t row_pitch;
817 uint32_t offset;
818 uint32_t bpp = 32;
819 int fd;
820 uint32_t size;
821
822 result = chain->base.image_fns->create_wsi_image(device_h,
823 pCreateInfo,
824 pAllocator,
825 &image->image,
826 &image->memory,
827 &size,
828 &offset,
829 &row_pitch,
830 &fd);
831 if (result != VK_SUCCESS)
832 return result;
833
834 image->pixmap = xcb_generate_id(chain->conn);
835
836 cookie =
837 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
838 image->pixmap,
839 chain->window,
840 size,
841 pCreateInfo->imageExtent.width,
842 pCreateInfo->imageExtent.height,
843 row_pitch,
844 chain->depth, bpp, fd);
845 xcb_discard_reply(chain->conn, cookie.sequence);
846
847 int fence_fd = xshmfence_alloc_shm();
848 if (fence_fd < 0)
849 goto fail_pixmap;
850
851 image->shm_fence = xshmfence_map_shm(fence_fd);
852 if (image->shm_fence == NULL)
853 goto fail_shmfence_alloc;
854
855 image->sync_fence = xcb_generate_id(chain->conn);
856 xcb_dri3_fence_from_fd(chain->conn,
857 image->pixmap,
858 image->sync_fence,
859 false,
860 fence_fd);
861
862 image->busy = false;
863 xshmfence_trigger(image->shm_fence);
864
865 return VK_SUCCESS;
866
867 fail_shmfence_alloc:
868 close(fence_fd);
869
870 fail_pixmap:
871 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
872 xcb_discard_reply(chain->conn, cookie.sequence);
873
874 chain->base.image_fns->free_wsi_image(device_h, pAllocator,
875 image->image, image->memory);
876
877 return result;
878 }
879
880 static void
881 x11_image_finish(struct x11_swapchain *chain,
882 const VkAllocationCallbacks* pAllocator,
883 struct x11_image *image)
884 {
885 xcb_void_cookie_t cookie;
886
887 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
888 xcb_discard_reply(chain->conn, cookie.sequence);
889 xshmfence_unmap_shm(image->shm_fence);
890
891 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
892 xcb_discard_reply(chain->conn, cookie.sequence);
893
894 chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
895 image->image, image->memory);
896 }
897
898 static VkResult
899 x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
900 const VkAllocationCallbacks *pAllocator)
901 {
902 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
903 xcb_void_cookie_t cookie;
904
905 for (uint32_t i = 0; i < chain->image_count; i++)
906 x11_image_finish(chain, pAllocator, &chain->images[i]);
907
908 if (chain->threaded) {
909 chain->status = VK_ERROR_OUT_OF_DATE_KHR;
910 /* Push a UINT32_MAX to wake up the manager */
911 wsi_queue_push(&chain->present_queue, UINT32_MAX);
912 pthread_join(chain->queue_manager, NULL);
913 wsi_queue_destroy(&chain->acquire_queue);
914 wsi_queue_destroy(&chain->present_queue);
915 }
916
917 xcb_unregister_for_special_event(chain->conn, chain->special_event);
918 cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
919 chain->window,
920 XCB_PRESENT_EVENT_MASK_NO_EVENT);
921 xcb_discard_reply(chain->conn, cookie.sequence);
922
923 vk_free(pAllocator, chain);
924
925 return VK_SUCCESS;
926 }
927
928 static VkResult
929 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
930 VkDevice device,
931 struct wsi_device *wsi_device,
932 const VkSwapchainCreateInfoKHR *pCreateInfo,
933 const VkAllocationCallbacks* pAllocator,
934 const struct wsi_image_fns *image_fns,
935 struct wsi_swapchain **swapchain_out)
936 {
937 struct x11_swapchain *chain;
938 xcb_void_cookie_t cookie;
939 VkResult result;
940
941 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
942
943 const unsigned num_images = pCreateInfo->minImageCount;
944
945 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
946 chain = vk_alloc(pAllocator, size, 8,
947 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
948 if (chain == NULL)
949 return VK_ERROR_OUT_OF_HOST_MEMORY;
950
951 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
952 xcb_window_t window = x11_surface_get_window(icd_surface);
953 xcb_get_geometry_reply_t *geometry =
954 xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
955
956 if (geometry == NULL)
957 return VK_ERROR_SURFACE_LOST_KHR;
958
959 chain->base.device = device;
960 chain->base.destroy = x11_swapchain_destroy;
961 chain->base.get_images = x11_get_images;
962 chain->base.acquire_next_image = x11_acquire_next_image;
963 chain->base.queue_present = x11_queue_present;
964 chain->base.image_fns = image_fns;
965 chain->base.present_mode = pCreateInfo->presentMode;
966 chain->conn = conn;
967 chain->window = window;
968 chain->depth = geometry->depth;
969 chain->extent = pCreateInfo->imageExtent;
970 chain->image_count = num_images;
971 chain->send_sbc = 0;
972 chain->last_present_msc = 0;
973 chain->threaded = false;
974 chain->status = VK_SUCCESS;
975
976 free(geometry);
977
978 chain->event_id = xcb_generate_id(chain->conn);
979 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
980 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
981 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
982 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
983
984 /* Create an XCB event queue to hold present events outside of the usual
985 * application event queue
986 */
987 chain->special_event =
988 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
989 chain->event_id, NULL);
990
991 chain->gc = xcb_generate_id(chain->conn);
992 if (!chain->gc) {
993 /* FINISHME: Choose a better error. */
994 result = VK_ERROR_OUT_OF_HOST_MEMORY;
995 goto fail_register;
996 }
997
998 cookie = xcb_create_gc(chain->conn,
999 chain->gc,
1000 chain->window,
1001 XCB_GC_GRAPHICS_EXPOSURES,
1002 (uint32_t []) { 0 });
1003 xcb_discard_reply(chain->conn, cookie.sequence);
1004
1005 uint32_t image = 0;
1006 for (; image < chain->image_count; image++) {
1007 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1008 &chain->images[image]);
1009 if (result != VK_SUCCESS)
1010 goto fail_init_images;
1011 }
1012
1013 if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
1014 chain->threaded = true;
1015
1016 /* Initialize our queues. We make them image_count + 1 because we will
1017 * occasionally use UINT32_MAX to signal the other thread that an error
1018 * has occurred and we don't want an overflow.
1019 */
1020 int ret;
1021 ret = wsi_queue_init(&chain->acquire_queue, chain->image_count + 1);
1022 if (ret) {
1023 goto fail_init_images;
1024 }
1025
1026 ret = wsi_queue_init(&chain->present_queue, chain->image_count + 1);
1027 if (ret) {
1028 wsi_queue_destroy(&chain->acquire_queue);
1029 goto fail_init_images;
1030 }
1031
1032 for (unsigned i = 0; i < chain->image_count; i++)
1033 wsi_queue_push(&chain->acquire_queue, i);
1034
1035 ret = pthread_create(&chain->queue_manager, NULL,
1036 x11_manage_fifo_queues, chain);
1037 if (ret) {
1038 wsi_queue_destroy(&chain->present_queue);
1039 wsi_queue_destroy(&chain->acquire_queue);
1040 goto fail_init_images;
1041 }
1042 }
1043
1044 *swapchain_out = &chain->base;
1045
1046 return VK_SUCCESS;
1047
1048 fail_init_images:
1049 for (uint32_t j = 0; j < image; j++)
1050 x11_image_finish(chain, pAllocator, &chain->images[j]);
1051
1052 fail_register:
1053 xcb_unregister_for_special_event(chain->conn, chain->special_event);
1054
1055 vk_free(pAllocator, chain);
1056
1057 return result;
1058 }
1059
1060 VkResult
1061 wsi_x11_init_wsi(struct wsi_device *wsi_device,
1062 const VkAllocationCallbacks *alloc)
1063 {
1064 struct wsi_x11 *wsi;
1065 VkResult result;
1066
1067 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1068 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1069 if (!wsi) {
1070 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1071 goto fail;
1072 }
1073
1074 int ret = pthread_mutex_init(&wsi->mutex, NULL);
1075 if (ret != 0) {
1076 if (ret == ENOMEM) {
1077 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1078 } else {
1079 /* FINISHME: Choose a better error. */
1080 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1081 }
1082
1083 goto fail_alloc;
1084 }
1085
1086 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1087 _mesa_key_pointer_equal);
1088 if (!wsi->connections) {
1089 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1090 goto fail_mutex;
1091 }
1092
1093 wsi->base.get_support = x11_surface_get_support;
1094 wsi->base.get_capabilities = x11_surface_get_capabilities;
1095 wsi->base.get_formats = x11_surface_get_formats;
1096 wsi->base.get_present_modes = x11_surface_get_present_modes;
1097 wsi->base.create_swapchain = x11_surface_create_swapchain;
1098
1099 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1100 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1101
1102 return VK_SUCCESS;
1103
1104 fail_mutex:
1105 pthread_mutex_destroy(&wsi->mutex);
1106 fail_alloc:
1107 vk_free(alloc, wsi);
1108 fail:
1109 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1110 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1111
1112 return result;
1113 }
1114
1115 void
1116 wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1117 const VkAllocationCallbacks *alloc)
1118 {
1119 struct wsi_x11 *wsi =
1120 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1121
1122 if (wsi) {
1123 struct hash_entry *entry;
1124 hash_table_foreach(wsi->connections, entry)
1125 wsi_x11_connection_destroy(alloc, entry->data);
1126
1127 _mesa_hash_table_destroy(wsi->connections, NULL);
1128
1129 pthread_mutex_destroy(&wsi->mutex);
1130
1131 vk_free(alloc, wsi);
1132 }
1133 }