2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include <X11/Xlib-xcb.h>
25 #include <X11/xshmfence.h>
28 #include <xcb/present.h>
30 #include "util/macros.h"
39 #include "util/hash_table.h"
41 #include "wsi_common.h"
42 #include "wsi_common_x11.h"
43 #include "wsi_common_queue.h"
45 #define typed_memcpy(dest, src, count) ({ \
46 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
47 memcpy((dest), (src), (count) * sizeof(*(src))); \
50 struct wsi_x11_connection
{
53 bool is_proprietary_x11
;
57 struct wsi_interface base
;
59 pthread_mutex_t mutex
;
60 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
61 struct hash_table
*connections
;
67 * Wrapper around xcb_dri3_open
70 wsi_dri3_open(xcb_connection_t
*conn
,
74 xcb_dri3_open_cookie_t cookie
;
75 xcb_dri3_open_reply_t
*reply
;
78 cookie
= xcb_dri3_open(conn
,
82 reply
= xcb_dri3_open_reply(conn
, cookie
, NULL
);
86 if (reply
->nfd
!= 1) {
91 fd
= xcb_dri3_open_reply_fds(conn
, reply
)[0];
93 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
99 wsi_x11_check_dri3_compatible(xcb_connection_t
*conn
, int local_fd
)
101 xcb_screen_iterator_t screen_iter
=
102 xcb_setup_roots_iterator(xcb_get_setup(conn
));
103 xcb_screen_t
*screen
= screen_iter
.data
;
105 int dri3_fd
= wsi_dri3_open(conn
, screen
->root
, None
);
107 char *local_dev
= drmGetRenderDeviceNameFromFd(local_fd
);
108 char *dri3_dev
= drmGetRenderDeviceNameFromFd(dri3_fd
);
113 ret
= strcmp(local_dev
, dri3_dev
);
124 static struct wsi_x11_connection
*
125 wsi_x11_connection_create(const VkAllocationCallbacks
*alloc
,
126 xcb_connection_t
*conn
)
128 xcb_query_extension_cookie_t dri3_cookie
, pres_cookie
, amd_cookie
, nv_cookie
;
129 xcb_query_extension_reply_t
*dri3_reply
, *pres_reply
, *amd_reply
, *nv_reply
;
131 struct wsi_x11_connection
*wsi_conn
=
132 vk_alloc(alloc
, sizeof(*wsi_conn
), 8,
133 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
137 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
138 pres_cookie
= xcb_query_extension(conn
, 7, "PRESENT");
140 /* We try to be nice to users and emit a warning if they try to use a
141 * Vulkan application on a system without DRI3 enabled. However, this ends
142 * up spewing the warning when a user has, for example, both Intel
143 * integrated graphics and a discrete card with proprietary drivers and are
144 * running on the discrete card with the proprietary DDX. In this case, we
145 * really don't want to print the warning because it just confuses users.
146 * As a heuristic to detect this case, we check for a couple of proprietary
149 amd_cookie
= xcb_query_extension(conn
, 11, "ATIFGLRXDRI");
150 nv_cookie
= xcb_query_extension(conn
, 10, "NV-CONTROL");
152 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
153 pres_reply
= xcb_query_extension_reply(conn
, pres_cookie
, NULL
);
154 amd_reply
= xcb_query_extension_reply(conn
, amd_cookie
, NULL
);
155 nv_reply
= xcb_query_extension_reply(conn
, nv_cookie
, NULL
);
156 if (!dri3_reply
|| !pres_reply
) {
161 vk_free(alloc
, wsi_conn
);
165 wsi_conn
->has_dri3
= dri3_reply
->present
!= 0;
166 wsi_conn
->has_present
= pres_reply
->present
!= 0;
167 wsi_conn
->is_proprietary_x11
= false;
168 if (amd_reply
&& amd_reply
->present
)
169 wsi_conn
->is_proprietary_x11
= true;
170 if (nv_reply
&& nv_reply
->present
)
171 wsi_conn
->is_proprietary_x11
= true;
182 wsi_x11_connection_destroy(const VkAllocationCallbacks
*alloc
,
183 struct wsi_x11_connection
*conn
)
185 vk_free(alloc
, conn
);
189 wsi_x11_check_for_dri3(struct wsi_x11_connection
*wsi_conn
)
191 if (wsi_conn
->has_dri3
)
193 if (!wsi_conn
->is_proprietary_x11
) {
194 fprintf(stderr
, "vulkan: No DRI3 support detected - required for presentation\n"
195 "Note: you can probably enable DRI3 in your Xorg config\n");
200 static struct wsi_x11_connection
*
201 wsi_x11_get_connection(struct wsi_device
*wsi_dev
,
202 const VkAllocationCallbacks
*alloc
,
203 xcb_connection_t
*conn
)
205 struct wsi_x11
*wsi
=
206 (struct wsi_x11
*)wsi_dev
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
208 pthread_mutex_lock(&wsi
->mutex
);
210 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
212 /* We're about to make a bunch of blocking calls. Let's drop the
213 * mutex for now so we don't block up too badly.
215 pthread_mutex_unlock(&wsi
->mutex
);
217 struct wsi_x11_connection
*wsi_conn
=
218 wsi_x11_connection_create(alloc
, conn
);
222 pthread_mutex_lock(&wsi
->mutex
);
224 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
226 /* Oops, someone raced us to it */
227 wsi_x11_connection_destroy(alloc
, wsi_conn
);
229 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
233 pthread_mutex_unlock(&wsi
->mutex
);
238 static const VkSurfaceFormatKHR formats
[] = {
239 { .format
= VK_FORMAT_B8G8R8A8_SRGB
, },
240 { .format
= VK_FORMAT_B8G8R8A8_UNORM
, },
243 static const VkPresentModeKHR present_modes
[] = {
244 VK_PRESENT_MODE_IMMEDIATE_KHR
,
245 VK_PRESENT_MODE_MAILBOX_KHR
,
246 VK_PRESENT_MODE_FIFO_KHR
,
249 static xcb_screen_t
*
250 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
252 xcb_screen_iterator_t screen_iter
=
253 xcb_setup_roots_iterator(xcb_get_setup(conn
));
255 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
256 if (screen_iter
.data
->root
== root
)
257 return screen_iter
.data
;
263 static xcb_visualtype_t
*
264 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
267 xcb_depth_iterator_t depth_iter
=
268 xcb_screen_allowed_depths_iterator(screen
);
270 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
271 xcb_visualtype_iterator_t visual_iter
=
272 xcb_depth_visuals_iterator (depth_iter
.data
);
274 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
275 if (visual_iter
.data
->visual_id
== visual_id
) {
277 *depth
= depth_iter
.data
->depth
;
278 return visual_iter
.data
;
286 static xcb_visualtype_t
*
287 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
290 xcb_screen_iterator_t screen_iter
=
291 xcb_setup_roots_iterator(xcb_get_setup(conn
));
293 /* For this we have to iterate over all of the screens which is rather
294 * annoying. Fortunately, there is probably only 1.
296 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
297 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
306 static xcb_visualtype_t
*
307 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
310 xcb_query_tree_cookie_t tree_cookie
;
311 xcb_get_window_attributes_cookie_t attrib_cookie
;
312 xcb_query_tree_reply_t
*tree
;
313 xcb_get_window_attributes_reply_t
*attrib
;
315 tree_cookie
= xcb_query_tree(conn
, window
);
316 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
318 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
319 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
320 if (attrib
== NULL
|| tree
== NULL
) {
326 xcb_window_t root
= tree
->root
;
327 xcb_visualid_t visual_id
= attrib
->visual
;
331 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
335 return screen_get_visualtype(screen
, visual_id
, depth
);
339 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
341 uint32_t rgb_mask
= visual
->red_mask
|
345 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
347 /* Do we have bits left over after RGB? */
348 return (all_mask
& ~rgb_mask
) != 0;
351 VkBool32
wsi_get_physical_device_xcb_presentation_support(
352 struct wsi_device
*wsi_device
,
353 VkAllocationCallbacks
*alloc
,
354 uint32_t queueFamilyIndex
,
356 bool can_handle_different_gpu
,
357 xcb_connection_t
* connection
,
358 xcb_visualid_t visual_id
)
360 struct wsi_x11_connection
*wsi_conn
=
361 wsi_x11_get_connection(wsi_device
, alloc
, connection
);
366 if (!wsi_x11_check_for_dri3(wsi_conn
))
369 if (!can_handle_different_gpu
)
370 if (!wsi_x11_check_dri3_compatible(connection
, fd
))
373 unsigned visual_depth
;
374 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
377 if (visual_depth
!= 24 && visual_depth
!= 32)
383 static xcb_connection_t
*
384 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
386 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
387 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
389 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
393 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
395 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
396 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
398 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
402 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
403 struct wsi_device
*wsi_device
,
404 const VkAllocationCallbacks
*alloc
,
405 uint32_t queueFamilyIndex
,
407 bool can_handle_different_gpu
,
408 VkBool32
* pSupported
)
410 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
411 xcb_window_t window
= x11_surface_get_window(icd_surface
);
413 struct wsi_x11_connection
*wsi_conn
=
414 wsi_x11_get_connection(wsi_device
, alloc
, conn
);
416 return VK_ERROR_OUT_OF_HOST_MEMORY
;
418 if (!wsi_x11_check_for_dri3(wsi_conn
)) {
423 if (!can_handle_different_gpu
)
424 if (!wsi_x11_check_dri3_compatible(conn
, local_fd
))
427 unsigned visual_depth
;
428 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
433 if (visual_depth
!= 24 && visual_depth
!= 32) {
443 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
444 VkSurfaceCapabilitiesKHR
*caps
)
446 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
447 xcb_window_t window
= x11_surface_get_window(icd_surface
);
448 xcb_get_geometry_cookie_t geom_cookie
;
449 xcb_generic_error_t
*err
;
450 xcb_get_geometry_reply_t
*geom
;
451 unsigned visual_depth
;
453 geom_cookie
= xcb_get_geometry(conn
, window
);
455 /* This does a round-trip. This is why we do get_geometry first and
456 * wait to read the reply until after we have a visual.
458 xcb_visualtype_t
*visual
=
459 get_visualtype_for_window(conn
, window
, &visual_depth
);
462 return VK_ERROR_SURFACE_LOST_KHR
;
464 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
466 VkExtent2D extent
= { geom
->width
, geom
->height
};
467 caps
->currentExtent
= extent
;
468 caps
->minImageExtent
= extent
;
469 caps
->maxImageExtent
= extent
;
471 /* This can happen if the client didn't wait for the configure event
472 * to come back from the compositor. In that case, we don't know the
473 * size of the window so we just return valid "I don't know" stuff.
475 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
476 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
477 /* This is the maximum supported size on Intel */
478 caps
->maxImageExtent
= (VkExtent2D
) { 1 << 14, 1 << 14 };
483 if (visual_has_alpha(visual
, visual_depth
)) {
484 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
485 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
487 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
488 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
491 /* For true mailbox mode, we need at least 4 images:
492 * 1) One to scan out from
493 * 2) One to have queued for scan-out
494 * 3) One to be currently held by the X server
495 * 4) One to render to
497 caps
->minImageCount
= 2;
498 /* There is no real maximum */
499 caps
->maxImageCount
= 0;
501 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
502 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
503 caps
->maxImageArrayLayers
= 1;
504 caps
->supportedUsageFlags
=
505 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
506 VK_IMAGE_USAGE_SAMPLED_BIT
|
507 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
508 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
514 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
515 struct wsi_device
*wsi_device
,
516 uint32_t *pSurfaceFormatCount
,
517 VkSurfaceFormatKHR
*pSurfaceFormats
)
519 if (pSurfaceFormats
== NULL
) {
520 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
524 *pSurfaceFormatCount
= MIN2(*pSurfaceFormatCount
, ARRAY_SIZE(formats
));
525 typed_memcpy(pSurfaceFormats
, formats
, *pSurfaceFormatCount
);
527 return *pSurfaceFormatCount
< ARRAY_SIZE(formats
) ?
528 VK_INCOMPLETE
: VK_SUCCESS
;
532 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
533 uint32_t *pPresentModeCount
,
534 VkPresentModeKHR
*pPresentModes
)
536 if (pPresentModes
== NULL
) {
537 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
541 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
542 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
544 return *pPresentModeCount
< ARRAY_SIZE(present_modes
) ?
545 VK_INCOMPLETE
: VK_SUCCESS
;
548 VkResult
wsi_create_xcb_surface(const VkAllocationCallbacks
*pAllocator
,
549 const VkXcbSurfaceCreateInfoKHR
*pCreateInfo
,
550 VkSurfaceKHR
*pSurface
)
552 VkIcdSurfaceXcb
*surface
;
554 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
555 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
557 return VK_ERROR_OUT_OF_HOST_MEMORY
;
559 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
560 surface
->connection
= pCreateInfo
->connection
;
561 surface
->window
= pCreateInfo
->window
;
563 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
567 VkResult
wsi_create_xlib_surface(const VkAllocationCallbacks
*pAllocator
,
568 const VkXlibSurfaceCreateInfoKHR
*pCreateInfo
,
569 VkSurfaceKHR
*pSurface
)
571 VkIcdSurfaceXlib
*surface
;
573 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
574 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
576 return VK_ERROR_OUT_OF_HOST_MEMORY
;
578 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
579 surface
->dpy
= pCreateInfo
->dpy
;
580 surface
->window
= pCreateInfo
->window
;
582 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
588 VkImage linear_image
; // for prime
589 VkDeviceMemory memory
;
590 VkDeviceMemory linear_memory
; // for prime
593 struct xshmfence
* shm_fence
;
597 struct x11_swapchain
{
598 struct wsi_swapchain base
;
600 xcb_connection_t
* conn
;
606 xcb_present_event_t event_id
;
607 xcb_special_event_t
* special_event
;
609 uint64_t last_present_msc
;
614 struct wsi_queue present_queue
;
615 struct wsi_queue acquire_queue
;
616 pthread_t queue_manager
;
618 struct x11_image images
[0];
622 x11_get_images(struct wsi_swapchain
*anv_chain
,
623 uint32_t* pCount
, VkImage
*pSwapchainImages
)
625 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
629 if (pSwapchainImages
== NULL
) {
630 *pCount
= chain
->base
.image_count
;
635 ret_count
= chain
->base
.image_count
;
636 if (chain
->base
.image_count
> *pCount
) {
638 result
= VK_INCOMPLETE
;
641 for (uint32_t i
= 0; i
< ret_count
; i
++)
642 pSwapchainImages
[i
] = chain
->images
[i
].image
;
648 x11_get_image_and_linear(struct wsi_swapchain
*drv_chain
,
649 int imageIndex
, VkImage
*image
, VkImage
*linear_image
)
651 struct x11_swapchain
*chain
= (struct x11_swapchain
*)drv_chain
;
652 *image
= chain
->images
[imageIndex
].image
;
653 *linear_image
= chain
->images
[imageIndex
].linear_image
;
657 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
658 xcb_present_generic_event_t
*event
)
660 switch (event
->evtype
) {
661 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
662 xcb_present_configure_notify_event_t
*config
= (void *) event
;
664 if (config
->width
!= chain
->extent
.width
||
665 config
->height
!= chain
->extent
.height
)
666 return VK_ERROR_OUT_OF_DATE_KHR
;
671 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
672 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
674 for (unsigned i
= 0; i
< chain
->base
.image_count
; i
++) {
675 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
676 chain
->images
[i
].busy
= false;
678 wsi_queue_push(&chain
->acquire_queue
, i
);
686 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY
: {
687 xcb_present_complete_notify_event_t
*complete
= (void *) event
;
688 if (complete
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
)
689 chain
->last_present_msc
= complete
->msc
;
701 static uint64_t wsi_get_current_time(void)
703 uint64_t current_time
;
706 clock_gettime(CLOCK_MONOTONIC
, &tv
);
707 current_time
= tv
.tv_nsec
+ tv
.tv_sec
*1000000000ull;
711 static uint64_t wsi_get_absolute_timeout(uint64_t timeout
)
713 uint64_t current_time
= wsi_get_current_time();
715 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
717 return current_time
+ timeout
;
721 x11_acquire_next_image_poll_x11(struct x11_swapchain
*chain
,
722 uint32_t *image_index
, uint64_t timeout
)
724 xcb_generic_event_t
*event
;
728 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
729 if (!chain
->images
[i
].busy
) {
730 /* We found a non-busy image */
731 xshmfence_await(chain
->images
[i
].shm_fence
);
733 chain
->images
[i
].busy
= true;
738 xcb_flush(chain
->conn
);
740 if (timeout
== UINT64_MAX
) {
741 event
= xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
743 return VK_ERROR_OUT_OF_DATE_KHR
;
745 event
= xcb_poll_for_special_event(chain
->conn
, chain
->special_event
);
751 atimeout
= wsi_get_absolute_timeout(timeout
);
753 pfds
.fd
= xcb_get_file_descriptor(chain
->conn
);
754 pfds
.events
= POLLIN
;
755 ret
= poll(&pfds
, 1, timeout
/ 1000 / 1000);
759 return VK_ERROR_OUT_OF_DATE_KHR
;
761 /* If a non-special event happens, the fd will still
762 * poll. So recalculate the timeout now just in case.
764 uint64_t current_time
= wsi_get_current_time();
765 if (atimeout
> current_time
)
766 timeout
= atimeout
- current_time
;
773 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
775 if (result
!= VK_SUCCESS
)
781 x11_acquire_next_image_from_queue(struct x11_swapchain
*chain
,
782 uint32_t *image_index_out
, uint64_t timeout
)
784 assert(chain
->threaded
);
786 uint32_t image_index
;
787 VkResult result
= wsi_queue_pull(&chain
->acquire_queue
,
788 &image_index
, timeout
);
789 if (result
!= VK_SUCCESS
) {
791 } else if (chain
->status
!= VK_SUCCESS
) {
792 return chain
->status
;
795 assert(image_index
< chain
->base
.image_count
);
796 xshmfence_await(chain
->images
[image_index
].shm_fence
);
798 *image_index_out
= image_index
;
804 x11_present_to_x11(struct x11_swapchain
*chain
, uint32_t image_index
,
807 struct x11_image
*image
= &chain
->images
[image_index
];
809 assert(image_index
< chain
->base
.image_count
);
811 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
814 int64_t remainder
= 0;
816 if (chain
->base
.present_mode
== VK_PRESENT_MODE_IMMEDIATE_KHR
)
817 options
|= XCB_PRESENT_OPTION_ASYNC
;
819 xshmfence_reset(image
->shm_fence
);
822 xcb_void_cookie_t cookie
=
823 xcb_present_pixmap(chain
->conn
,
826 (uint32_t) chain
->send_sbc
,
831 XCB_NONE
, /* target_crtc */
838 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
841 xcb_flush(chain
->conn
);
847 x11_acquire_next_image(struct wsi_swapchain
*anv_chain
,
849 VkSemaphore semaphore
,
850 uint32_t *image_index
)
852 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
854 if (chain
->threaded
) {
855 return x11_acquire_next_image_from_queue(chain
, image_index
, timeout
);
857 return x11_acquire_next_image_poll_x11(chain
, image_index
, timeout
);
862 x11_queue_present(struct wsi_swapchain
*anv_chain
,
863 uint32_t image_index
,
864 const VkPresentRegionKHR
*damage
)
866 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
868 if (chain
->threaded
) {
869 wsi_queue_push(&chain
->present_queue
, image_index
);
870 return chain
->status
;
872 return x11_present_to_x11(chain
, image_index
, 0);
877 x11_manage_fifo_queues(void *state
)
879 struct x11_swapchain
*chain
= state
;
882 assert(chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
);
884 while (chain
->status
== VK_SUCCESS
) {
885 /* It should be safe to unconditionally block here. Later in the loop
886 * we blocks until the previous present has landed on-screen. At that
887 * point, we should have received IDLE_NOTIFY on all images presented
888 * before that point so the client should be able to acquire any image
889 * other than the currently presented one.
891 uint32_t image_index
;
892 result
= wsi_queue_pull(&chain
->present_queue
, &image_index
, INT64_MAX
);
893 if (result
!= VK_SUCCESS
) {
895 } else if (chain
->status
!= VK_SUCCESS
) {
899 uint64_t target_msc
= chain
->last_present_msc
+ 1;
900 result
= x11_present_to_x11(chain
, image_index
, target_msc
);
901 if (result
!= VK_SUCCESS
)
904 while (chain
->last_present_msc
< target_msc
) {
905 xcb_generic_event_t
*event
=
906 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
910 result
= x11_handle_dri3_present_event(chain
, (void *)event
);
911 if (result
!= VK_SUCCESS
)
917 chain
->status
= result
;
918 wsi_queue_push(&chain
->acquire_queue
, UINT32_MAX
);
924 x11_image_init(VkDevice device_h
, struct x11_swapchain
*chain
,
925 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
926 const VkAllocationCallbacks
* pAllocator
,
927 struct x11_image
*image
)
929 xcb_void_cookie_t cookie
;
937 result
= chain
->base
.image_fns
->create_wsi_image(device_h
,
940 chain
->base
.needs_linear_copy
,
948 if (result
!= VK_SUCCESS
)
951 if (chain
->base
.needs_linear_copy
) {
952 result
= chain
->base
.image_fns
->create_wsi_image(device_h
,
955 chain
->base
.needs_linear_copy
,
957 &image
->linear_image
,
958 &image
->linear_memory
,
963 if (result
!= VK_SUCCESS
) {
964 chain
->base
.image_fns
->free_wsi_image(device_h
, pAllocator
,
965 image
->image
, image
->memory
);
970 image
->pixmap
= xcb_generate_id(chain
->conn
);
973 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
977 pCreateInfo
->imageExtent
.width
,
978 pCreateInfo
->imageExtent
.height
,
980 chain
->depth
, bpp
, fd
);
981 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
983 int fence_fd
= xshmfence_alloc_shm();
987 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
988 if (image
->shm_fence
== NULL
)
989 goto fail_shmfence_alloc
;
991 image
->sync_fence
= xcb_generate_id(chain
->conn
);
992 xcb_dri3_fence_from_fd(chain
->conn
,
999 xshmfence_trigger(image
->shm_fence
);
1003 fail_shmfence_alloc
:
1007 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
1008 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1010 if (chain
->base
.needs_linear_copy
) {
1011 chain
->base
.image_fns
->free_wsi_image(device_h
, pAllocator
,
1012 image
->linear_image
, image
->linear_memory
);
1014 chain
->base
.image_fns
->free_wsi_image(device_h
, pAllocator
,
1015 image
->image
, image
->memory
);
1021 x11_image_finish(struct x11_swapchain
*chain
,
1022 const VkAllocationCallbacks
* pAllocator
,
1023 struct x11_image
*image
)
1025 xcb_void_cookie_t cookie
;
1027 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
1028 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1029 xshmfence_unmap_shm(image
->shm_fence
);
1031 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
1032 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1034 if (chain
->base
.needs_linear_copy
) {
1035 chain
->base
.image_fns
->free_wsi_image(chain
->base
.device
, pAllocator
,
1036 image
->linear_image
, image
->linear_memory
);
1038 chain
->base
.image_fns
->free_wsi_image(chain
->base
.device
, pAllocator
,
1039 image
->image
, image
->memory
);
1043 x11_swapchain_destroy(struct wsi_swapchain
*anv_chain
,
1044 const VkAllocationCallbacks
*pAllocator
)
1046 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
1047 xcb_void_cookie_t cookie
;
1049 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++)
1050 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
1052 if (chain
->threaded
) {
1053 chain
->status
= VK_ERROR_OUT_OF_DATE_KHR
;
1054 /* Push a UINT32_MAX to wake up the manager */
1055 wsi_queue_push(&chain
->present_queue
, UINT32_MAX
);
1056 pthread_join(chain
->queue_manager
, NULL
);
1057 wsi_queue_destroy(&chain
->acquire_queue
);
1058 wsi_queue_destroy(&chain
->present_queue
);
1061 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
1062 cookie
= xcb_present_select_input_checked(chain
->conn
, chain
->event_id
,
1064 XCB_PRESENT_EVENT_MASK_NO_EVENT
);
1065 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1067 vk_free(pAllocator
, chain
);
1073 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
1075 struct wsi_device
*wsi_device
,
1077 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
1078 const VkAllocationCallbacks
* pAllocator
,
1079 const struct wsi_image_fns
*image_fns
,
1080 struct wsi_swapchain
**swapchain_out
)
1082 struct x11_swapchain
*chain
;
1083 xcb_void_cookie_t cookie
;
1086 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
1088 const unsigned num_images
= pCreateInfo
->minImageCount
;
1090 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
1091 chain
= vk_alloc(pAllocator
, size
, 8,
1092 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1094 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1096 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
1097 xcb_window_t window
= x11_surface_get_window(icd_surface
);
1098 xcb_get_geometry_reply_t
*geometry
=
1099 xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, window
), NULL
);
1101 if (geometry
== NULL
)
1102 return VK_ERROR_SURFACE_LOST_KHR
;
1104 chain
->base
.device
= device
;
1105 chain
->base
.destroy
= x11_swapchain_destroy
;
1106 chain
->base
.get_images
= x11_get_images
;
1107 chain
->base
.get_image_and_linear
= x11_get_image_and_linear
;
1108 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
1109 chain
->base
.queue_present
= x11_queue_present
;
1110 chain
->base
.image_fns
= image_fns
;
1111 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
1112 chain
->base
.image_count
= num_images
;
1114 chain
->window
= window
;
1115 chain
->depth
= geometry
->depth
;
1116 chain
->extent
= pCreateInfo
->imageExtent
;
1117 chain
->send_sbc
= 0;
1118 chain
->last_present_msc
= 0;
1119 chain
->threaded
= false;
1120 chain
->status
= VK_SUCCESS
;
1124 chain
->base
.needs_linear_copy
= false;
1125 if (!wsi_x11_check_dri3_compatible(conn
, local_fd
))
1126 chain
->base
.needs_linear_copy
= true;
1128 chain
->event_id
= xcb_generate_id(chain
->conn
);
1129 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
1130 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
1131 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
1132 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
1134 /* Create an XCB event queue to hold present events outside of the usual
1135 * application event queue
1137 chain
->special_event
=
1138 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
1139 chain
->event_id
, NULL
);
1141 chain
->gc
= xcb_generate_id(chain
->conn
);
1143 /* FINISHME: Choose a better error. */
1144 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1148 cookie
= xcb_create_gc(chain
->conn
,
1151 XCB_GC_GRAPHICS_EXPOSURES
,
1152 (uint32_t []) { 0 });
1153 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1156 for (; image
< chain
->base
.image_count
; image
++) {
1157 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
1158 &chain
->images
[image
]);
1159 if (result
!= VK_SUCCESS
)
1160 goto fail_init_images
;
1163 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
1164 chain
->threaded
= true;
1166 /* Initialize our queues. We make them base.image_count + 1 because we will
1167 * occasionally use UINT32_MAX to signal the other thread that an error
1168 * has occurred and we don't want an overflow.
1171 ret
= wsi_queue_init(&chain
->acquire_queue
, chain
->base
.image_count
+ 1);
1173 goto fail_init_images
;
1176 ret
= wsi_queue_init(&chain
->present_queue
, chain
->base
.image_count
+ 1);
1178 wsi_queue_destroy(&chain
->acquire_queue
);
1179 goto fail_init_images
;
1182 for (unsigned i
= 0; i
< chain
->base
.image_count
; i
++)
1183 wsi_queue_push(&chain
->acquire_queue
, i
);
1185 ret
= pthread_create(&chain
->queue_manager
, NULL
,
1186 x11_manage_fifo_queues
, chain
);
1188 wsi_queue_destroy(&chain
->present_queue
);
1189 wsi_queue_destroy(&chain
->acquire_queue
);
1190 goto fail_init_images
;
1194 *swapchain_out
= &chain
->base
;
1199 for (uint32_t j
= 0; j
< image
; j
++)
1200 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
1203 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
1205 vk_free(pAllocator
, chain
);
1211 wsi_x11_init_wsi(struct wsi_device
*wsi_device
,
1212 const VkAllocationCallbacks
*alloc
)
1214 struct wsi_x11
*wsi
;
1217 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1218 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1220 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1224 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
1226 if (ret
== ENOMEM
) {
1227 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1229 /* FINISHME: Choose a better error. */
1230 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1236 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1237 _mesa_key_pointer_equal
);
1238 if (!wsi
->connections
) {
1239 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1243 wsi
->base
.get_support
= x11_surface_get_support
;
1244 wsi
->base
.get_capabilities
= x11_surface_get_capabilities
;
1245 wsi
->base
.get_formats
= x11_surface_get_formats
;
1246 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
1247 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
1249 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
1250 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
1255 pthread_mutex_destroy(&wsi
->mutex
);
1257 vk_free(alloc
, wsi
);
1259 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
1260 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
1266 wsi_x11_finish_wsi(struct wsi_device
*wsi_device
,
1267 const VkAllocationCallbacks
*alloc
)
1269 struct wsi_x11
*wsi
=
1270 (struct wsi_x11
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
1273 struct hash_entry
*entry
;
1274 hash_table_foreach(wsi
->connections
, entry
)
1275 wsi_x11_connection_destroy(alloc
, entry
->data
);
1277 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
1279 pthread_mutex_destroy(&wsi
->mutex
);
1281 vk_free(alloc
, wsi
);