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 "drm-uapi/drm_fourcc.h"
40 #include "util/hash_table.h"
41 #include "util/xmlconfig.h"
44 #include "wsi_common_private.h"
45 #include "wsi_common_x11.h"
46 #include "wsi_common_queue.h"
48 #define typed_memcpy(dest, src, count) ({ \
49 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
50 memcpy((dest), (src), (count) * sizeof(*(src))); \
53 struct wsi_x11_connection
{
55 bool has_dri3_modifiers
;
57 bool is_proprietary_x11
;
61 struct wsi_interface base
;
63 pthread_mutex_t mutex
;
64 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
65 struct hash_table
*connections
;
71 * Wrapper around xcb_dri3_open
74 wsi_dri3_open(xcb_connection_t
*conn
,
78 xcb_dri3_open_cookie_t cookie
;
79 xcb_dri3_open_reply_t
*reply
;
82 cookie
= xcb_dri3_open(conn
,
86 reply
= xcb_dri3_open_reply(conn
, cookie
, NULL
);
90 if (reply
->nfd
!= 1) {
95 fd
= xcb_dri3_open_reply_fds(conn
, reply
)[0];
97 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
103 wsi_x11_check_dri3_compatible(const struct wsi_device
*wsi_dev
,
104 xcb_connection_t
*conn
)
106 xcb_screen_iterator_t screen_iter
=
107 xcb_setup_roots_iterator(xcb_get_setup(conn
));
108 xcb_screen_t
*screen
= screen_iter
.data
;
110 int dri3_fd
= wsi_dri3_open(conn
, screen
->root
, None
);
114 bool match
= wsi_device_matches_drm_fd(wsi_dev
, dri3_fd
);
121 static struct wsi_x11_connection
*
122 wsi_x11_connection_create(struct wsi_device
*wsi_dev
,
123 xcb_connection_t
*conn
)
125 xcb_query_extension_cookie_t dri3_cookie
, pres_cookie
, amd_cookie
, nv_cookie
;
126 xcb_query_extension_reply_t
*dri3_reply
, *pres_reply
, *amd_reply
, *nv_reply
;
127 bool has_dri3_v1_2
= false;
128 bool has_present_v1_2
= false;
130 struct wsi_x11_connection
*wsi_conn
=
131 vk_alloc(&wsi_dev
->instance_alloc
, sizeof(*wsi_conn
), 8,
132 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
136 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
137 pres_cookie
= xcb_query_extension(conn
, 7, "Present");
139 /* We try to be nice to users and emit a warning if they try to use a
140 * Vulkan application on a system without DRI3 enabled. However, this ends
141 * up spewing the warning when a user has, for example, both Intel
142 * integrated graphics and a discrete card with proprietary drivers and are
143 * running on the discrete card with the proprietary DDX. In this case, we
144 * really don't want to print the warning because it just confuses users.
145 * As a heuristic to detect this case, we check for a couple of proprietary
148 amd_cookie
= xcb_query_extension(conn
, 11, "ATIFGLRXDRI");
149 nv_cookie
= xcb_query_extension(conn
, 10, "NV-CONTROL");
151 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
152 pres_reply
= xcb_query_extension_reply(conn
, pres_cookie
, NULL
);
153 amd_reply
= xcb_query_extension_reply(conn
, amd_cookie
, NULL
);
154 nv_reply
= xcb_query_extension_reply(conn
, nv_cookie
, NULL
);
155 if (!dri3_reply
|| !pres_reply
) {
160 vk_free(&wsi_dev
->instance_alloc
, wsi_conn
);
164 wsi_conn
->has_dri3
= dri3_reply
->present
!= 0;
165 #ifdef HAVE_DRI3_MODIFIERS
166 if (wsi_conn
->has_dri3
) {
167 xcb_dri3_query_version_cookie_t ver_cookie
;
168 xcb_dri3_query_version_reply_t
*ver_reply
;
170 ver_cookie
= xcb_dri3_query_version(conn
, 1, 2);
171 ver_reply
= xcb_dri3_query_version_reply(conn
, ver_cookie
, NULL
);
173 (ver_reply
->major_version
> 1 || ver_reply
->minor_version
>= 2);
178 wsi_conn
->has_present
= pres_reply
->present
!= 0;
179 #ifdef HAVE_DRI3_MODIFIERS
180 if (wsi_conn
->has_present
) {
181 xcb_present_query_version_cookie_t ver_cookie
;
182 xcb_present_query_version_reply_t
*ver_reply
;
184 ver_cookie
= xcb_present_query_version(conn
, 1, 2);
185 ver_reply
= xcb_present_query_version_reply(conn
, ver_cookie
, NULL
);
187 (ver_reply
->major_version
> 1 || ver_reply
->minor_version
>= 2);
192 wsi_conn
->has_dri3_modifiers
= has_dri3_v1_2
&& has_present_v1_2
;
193 wsi_conn
->is_proprietary_x11
= false;
194 if (amd_reply
&& amd_reply
->present
)
195 wsi_conn
->is_proprietary_x11
= true;
196 if (nv_reply
&& nv_reply
->present
)
197 wsi_conn
->is_proprietary_x11
= true;
208 wsi_x11_connection_destroy(struct wsi_device
*wsi_dev
,
209 struct wsi_x11_connection
*conn
)
211 vk_free(&wsi_dev
->instance_alloc
, conn
);
215 wsi_x11_check_for_dri3(struct wsi_x11_connection
*wsi_conn
)
217 if (wsi_conn
->has_dri3
)
219 if (!wsi_conn
->is_proprietary_x11
) {
220 fprintf(stderr
, "vulkan: No DRI3 support detected - required for presentation\n"
221 "Note: you can probably enable DRI3 in your Xorg config\n");
226 static struct wsi_x11_connection
*
227 wsi_x11_get_connection(struct wsi_device
*wsi_dev
,
228 xcb_connection_t
*conn
)
230 struct wsi_x11
*wsi
=
231 (struct wsi_x11
*)wsi_dev
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
233 pthread_mutex_lock(&wsi
->mutex
);
235 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
237 /* We're about to make a bunch of blocking calls. Let's drop the
238 * mutex for now so we don't block up too badly.
240 pthread_mutex_unlock(&wsi
->mutex
);
242 struct wsi_x11_connection
*wsi_conn
=
243 wsi_x11_connection_create(wsi_dev
, conn
);
247 pthread_mutex_lock(&wsi
->mutex
);
249 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
251 /* Oops, someone raced us to it */
252 wsi_x11_connection_destroy(wsi_dev
, wsi_conn
);
254 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
258 pthread_mutex_unlock(&wsi
->mutex
);
263 static const VkFormat formats
[] = {
264 VK_FORMAT_B8G8R8A8_SRGB
,
265 VK_FORMAT_B8G8R8A8_UNORM
,
268 static const VkPresentModeKHR present_modes
[] = {
269 VK_PRESENT_MODE_IMMEDIATE_KHR
,
270 VK_PRESENT_MODE_MAILBOX_KHR
,
271 VK_PRESENT_MODE_FIFO_KHR
,
274 static xcb_screen_t
*
275 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
277 xcb_screen_iterator_t screen_iter
=
278 xcb_setup_roots_iterator(xcb_get_setup(conn
));
280 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
281 if (screen_iter
.data
->root
== root
)
282 return screen_iter
.data
;
288 static xcb_visualtype_t
*
289 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
292 xcb_depth_iterator_t depth_iter
=
293 xcb_screen_allowed_depths_iterator(screen
);
295 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
296 xcb_visualtype_iterator_t visual_iter
=
297 xcb_depth_visuals_iterator (depth_iter
.data
);
299 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
300 if (visual_iter
.data
->visual_id
== visual_id
) {
302 *depth
= depth_iter
.data
->depth
;
303 return visual_iter
.data
;
311 static xcb_visualtype_t
*
312 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
315 xcb_screen_iterator_t screen_iter
=
316 xcb_setup_roots_iterator(xcb_get_setup(conn
));
318 /* For this we have to iterate over all of the screens which is rather
319 * annoying. Fortunately, there is probably only 1.
321 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
322 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
331 static xcb_visualtype_t
*
332 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
335 xcb_query_tree_cookie_t tree_cookie
;
336 xcb_get_window_attributes_cookie_t attrib_cookie
;
337 xcb_query_tree_reply_t
*tree
;
338 xcb_get_window_attributes_reply_t
*attrib
;
340 tree_cookie
= xcb_query_tree(conn
, window
);
341 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
343 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
344 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
345 if (attrib
== NULL
|| tree
== NULL
) {
351 xcb_window_t root
= tree
->root
;
352 xcb_visualid_t visual_id
= attrib
->visual
;
356 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
360 return screen_get_visualtype(screen
, visual_id
, depth
);
364 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
366 uint32_t rgb_mask
= visual
->red_mask
|
370 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
372 /* Do we have bits left over after RGB? */
373 return (all_mask
& ~rgb_mask
) != 0;
376 VkBool32
wsi_get_physical_device_xcb_presentation_support(
377 struct wsi_device
*wsi_device
,
378 uint32_t queueFamilyIndex
,
379 xcb_connection_t
* connection
,
380 xcb_visualid_t visual_id
)
382 struct wsi_x11_connection
*wsi_conn
=
383 wsi_x11_get_connection(wsi_device
, connection
);
388 if (!wsi_x11_check_for_dri3(wsi_conn
))
391 unsigned visual_depth
;
392 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
395 if (visual_depth
!= 24 && visual_depth
!= 32)
401 static xcb_connection_t
*
402 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
404 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
405 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
407 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
411 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
413 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
414 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
416 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
420 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
421 struct wsi_device
*wsi_device
,
422 uint32_t queueFamilyIndex
,
423 VkBool32
* pSupported
)
425 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
426 xcb_window_t window
= x11_surface_get_window(icd_surface
);
428 struct wsi_x11_connection
*wsi_conn
=
429 wsi_x11_get_connection(wsi_device
, conn
);
431 return VK_ERROR_OUT_OF_HOST_MEMORY
;
433 if (!wsi_x11_check_for_dri3(wsi_conn
)) {
438 unsigned visual_depth
;
439 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
444 if (visual_depth
!= 24 && visual_depth
!= 32) {
454 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
455 struct wsi_device
*wsi_device
,
456 VkSurfaceCapabilitiesKHR
*caps
)
458 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
459 xcb_window_t window
= x11_surface_get_window(icd_surface
);
460 xcb_get_geometry_cookie_t geom_cookie
;
461 xcb_generic_error_t
*err
;
462 xcb_get_geometry_reply_t
*geom
;
463 unsigned visual_depth
;
465 geom_cookie
= xcb_get_geometry(conn
, window
);
467 /* This does a round-trip. This is why we do get_geometry first and
468 * wait to read the reply until after we have a visual.
470 xcb_visualtype_t
*visual
=
471 get_visualtype_for_window(conn
, window
, &visual_depth
);
474 return VK_ERROR_SURFACE_LOST_KHR
;
476 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
478 VkExtent2D extent
= { geom
->width
, geom
->height
};
479 caps
->currentExtent
= extent
;
480 caps
->minImageExtent
= extent
;
481 caps
->maxImageExtent
= extent
;
483 /* This can happen if the client didn't wait for the configure event
484 * to come back from the compositor. In that case, we don't know the
485 * size of the window so we just return valid "I don't know" stuff.
487 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
488 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
489 caps
->maxImageExtent
= (VkExtent2D
) {
490 wsi_device
->maxImageDimension2D
,
491 wsi_device
->maxImageDimension2D
,
497 if (visual_has_alpha(visual
, visual_depth
)) {
498 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
499 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
501 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
502 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
505 /* For IMMEDIATE and FIFO, most games work in a pipelined manner where the
506 * can produce frames at a rate of 1/MAX(CPU duration, GPU duration), but
507 * the render latency is CPU duration + GPU duration.
509 * This means that with scanout from pageflipping we need 3 frames to run
511 * 1) CPU rendering work
512 * 2) GPU rendering work
515 * Once we have a nonblocking acquire that returns a semaphore we can merge
516 * 1 and 3. Hence the ideal implementation needs only 2 images, but games
517 * cannot tellwe currently do not have an ideal implementation and that
518 * hence they need to allocate 3 images. So let us do it for them.
520 * This is a tradeoff as it uses more memory than needed for non-fullscreen
521 * and non-performance intensive applications.
523 caps
->minImageCount
= 3;
524 /* There is no real maximum */
525 caps
->maxImageCount
= 0;
527 if (wsi_device
->x11
.override_minImageCount
)
528 caps
->minImageCount
= wsi_device
->x11
.override_minImageCount
;
530 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
531 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
532 caps
->maxImageArrayLayers
= 1;
533 caps
->supportedUsageFlags
=
534 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
535 VK_IMAGE_USAGE_SAMPLED_BIT
|
536 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
537 VK_IMAGE_USAGE_STORAGE_BIT
|
538 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
544 x11_surface_get_capabilities2(VkIcdSurfaceBase
*icd_surface
,
545 struct wsi_device
*wsi_device
,
546 const void *info_next
,
547 VkSurfaceCapabilities2KHR
*caps
)
549 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
552 x11_surface_get_capabilities(icd_surface
, wsi_device
,
553 &caps
->surfaceCapabilities
);
555 vk_foreach_struct(ext
, caps
->pNext
) {
556 switch (ext
->sType
) {
557 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR
: {
558 VkSurfaceProtectedCapabilitiesKHR
*protected = (void *)ext
;
559 protected->supportsProtected
= VK_FALSE
;
573 get_sorted_vk_formats(struct wsi_device
*wsi_device
, VkFormat
*sorted_formats
)
575 memcpy(sorted_formats
, formats
, sizeof(formats
));
577 if (wsi_device
->force_bgra8_unorm_first
) {
578 for (unsigned i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
579 if (sorted_formats
[i
] == VK_FORMAT_B8G8R8A8_UNORM
) {
580 sorted_formats
[i
] = sorted_formats
[0];
581 sorted_formats
[0] = VK_FORMAT_B8G8R8A8_UNORM
;
589 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
590 struct wsi_device
*wsi_device
,
591 uint32_t *pSurfaceFormatCount
,
592 VkSurfaceFormatKHR
*pSurfaceFormats
)
594 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
596 VkFormat sorted_formats
[ARRAY_SIZE(formats
)];
597 get_sorted_vk_formats(wsi_device
, sorted_formats
);
599 for (unsigned i
= 0; i
< ARRAY_SIZE(sorted_formats
); i
++) {
600 vk_outarray_append(&out
, f
) {
601 f
->format
= sorted_formats
[i
];
602 f
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
606 return vk_outarray_status(&out
);
610 x11_surface_get_formats2(VkIcdSurfaceBase
*surface
,
611 struct wsi_device
*wsi_device
,
612 const void *info_next
,
613 uint32_t *pSurfaceFormatCount
,
614 VkSurfaceFormat2KHR
*pSurfaceFormats
)
616 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
618 VkFormat sorted_formats
[ARRAY_SIZE(formats
)];
619 get_sorted_vk_formats(wsi_device
, sorted_formats
);
621 for (unsigned i
= 0; i
< ARRAY_SIZE(sorted_formats
); i
++) {
622 vk_outarray_append(&out
, f
) {
623 assert(f
->sType
== VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR
);
624 f
->surfaceFormat
.format
= sorted_formats
[i
];
625 f
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
629 return vk_outarray_status(&out
);
633 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
634 uint32_t *pPresentModeCount
,
635 VkPresentModeKHR
*pPresentModes
)
637 if (pPresentModes
== NULL
) {
638 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
642 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
643 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
645 return *pPresentModeCount
< ARRAY_SIZE(present_modes
) ?
646 VK_INCOMPLETE
: VK_SUCCESS
;
650 x11_surface_is_local_to_gpu(struct wsi_device
*wsi_dev
,
651 xcb_connection_t
*conn
)
653 struct wsi_x11_connection
*wsi_conn
=
654 wsi_x11_get_connection(wsi_dev
, conn
);
659 if (!wsi_x11_check_for_dri3(wsi_conn
))
662 if (!wsi_x11_check_dri3_compatible(wsi_dev
, conn
))
669 x11_surface_get_present_rectangles(VkIcdSurfaceBase
*icd_surface
,
670 struct wsi_device
*wsi_device
,
671 uint32_t* pRectCount
,
674 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
675 xcb_window_t window
= x11_surface_get_window(icd_surface
);
676 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
678 if (x11_surface_is_local_to_gpu(wsi_device
, conn
)) {
679 vk_outarray_append(&out
, rect
) {
680 xcb_generic_error_t
*err
= NULL
;
681 xcb_get_geometry_cookie_t geom_cookie
= xcb_get_geometry(conn
, window
);
682 xcb_get_geometry_reply_t
*geom
=
683 xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
688 .extent
= { geom
->width
, geom
->height
},
691 /* This can happen if the client didn't wait for the configure event
692 * to come back from the compositor. In that case, we don't know the
693 * size of the window so we just return valid "I don't know" stuff.
697 .extent
= { -1, -1 },
704 return vk_outarray_status(&out
);
707 VkResult
wsi_create_xcb_surface(const VkAllocationCallbacks
*pAllocator
,
708 const VkXcbSurfaceCreateInfoKHR
*pCreateInfo
,
709 VkSurfaceKHR
*pSurface
)
711 VkIcdSurfaceXcb
*surface
;
713 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
714 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
716 return VK_ERROR_OUT_OF_HOST_MEMORY
;
718 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
719 surface
->connection
= pCreateInfo
->connection
;
720 surface
->window
= pCreateInfo
->window
;
722 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
726 VkResult
wsi_create_xlib_surface(const VkAllocationCallbacks
*pAllocator
,
727 const VkXlibSurfaceCreateInfoKHR
*pCreateInfo
,
728 VkSurfaceKHR
*pSurface
)
730 VkIcdSurfaceXlib
*surface
;
732 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
733 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
735 return VK_ERROR_OUT_OF_HOST_MEMORY
;
737 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
738 surface
->dpy
= pCreateInfo
->dpy
;
739 surface
->window
= pCreateInfo
->window
;
741 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
746 struct wsi_image base
;
749 struct xshmfence
* shm_fence
;
753 struct x11_swapchain
{
754 struct wsi_swapchain base
;
756 bool has_dri3_modifiers
;
758 xcb_connection_t
* conn
;
764 xcb_present_event_t event_id
;
765 xcb_special_event_t
* special_event
;
767 uint64_t last_present_msc
;
770 bool has_present_queue
;
771 bool has_acquire_queue
;
773 xcb_present_complete_mode_t last_present_mode
;
774 struct wsi_queue present_queue
;
775 struct wsi_queue acquire_queue
;
776 pthread_t queue_manager
;
778 struct x11_image images
[0];
780 VK_DEFINE_NONDISP_HANDLE_CASTS(x11_swapchain
, base
.base
, VkSwapchainKHR
,
781 VK_OBJECT_TYPE_SWAPCHAIN_KHR
)
784 * Update the swapchain status with the result of an operation, and return
785 * the combined status. The chain status will eventually be returned from
786 * AcquireNextImage and QueuePresent.
788 * We make sure to 'stick' more pessimistic statuses: an out-of-date error
789 * is permanent once seen, and every subsequent call will return this. If
790 * this has not been seen, success will be returned.
793 x11_swapchain_result(struct x11_swapchain
*chain
, VkResult result
)
795 /* Prioritise returning existing errors for consistency. */
796 if (chain
->status
< 0)
797 return chain
->status
;
799 /* If we have a new error, mark it as permanent on the chain and return. */
801 chain
->status
= result
;
805 /* Return temporary errors, but don't persist them. */
806 if (result
== VK_TIMEOUT
|| result
== VK_NOT_READY
)
809 /* Suboptimal isn't an error, but is a status which sticks to the swapchain
810 * and is always returned rather than success.
812 if (result
== VK_SUBOPTIMAL_KHR
) {
813 chain
->status
= result
;
817 /* No changes, so return the last status. */
818 return chain
->status
;
821 static struct wsi_image
*
822 x11_get_wsi_image(struct wsi_swapchain
*wsi_chain
, uint32_t image_index
)
824 struct x11_swapchain
*chain
= (struct x11_swapchain
*)wsi_chain
;
825 return &chain
->images
[image_index
].base
;
829 * Process an X11 Present event. Does not update chain->status.
832 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
833 xcb_present_generic_event_t
*event
)
835 switch (event
->evtype
) {
836 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
837 xcb_present_configure_notify_event_t
*config
= (void *) event
;
839 if (config
->width
!= chain
->extent
.width
||
840 config
->height
!= chain
->extent
.height
)
841 return VK_ERROR_OUT_OF_DATE_KHR
;
846 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
847 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
849 for (unsigned i
= 0; i
< chain
->base
.image_count
; i
++) {
850 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
851 chain
->images
[i
].busy
= false;
852 if (chain
->has_acquire_queue
)
853 wsi_queue_push(&chain
->acquire_queue
, i
);
861 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY
: {
862 xcb_present_complete_notify_event_t
*complete
= (void *) event
;
863 if (complete
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
)
864 chain
->last_present_msc
= complete
->msc
;
866 VkResult result
= VK_SUCCESS
;
868 /* The winsys is now trying to flip directly and cannot due to our
869 * configuration. Request the user reallocate.
871 #ifdef HAVE_DRI3_MODIFIERS
872 if (complete
->mode
== XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY
&&
873 chain
->last_present_mode
!= XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY
)
874 result
= VK_SUBOPTIMAL_KHR
;
877 /* When we go from flipping to copying, the odds are very likely that
878 * we could reallocate in a more optimal way if we didn't have to care
879 * about scanout, so we always do this.
881 if (complete
->mode
== XCB_PRESENT_COMPLETE_MODE_COPY
&&
882 chain
->last_present_mode
== XCB_PRESENT_COMPLETE_MODE_FLIP
)
883 result
= VK_SUBOPTIMAL_KHR
;
885 chain
->last_present_mode
= complete
->mode
;
897 static uint64_t wsi_get_absolute_timeout(uint64_t timeout
)
899 uint64_t current_time
= wsi_common_get_current_time();
901 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
903 return current_time
+ timeout
;
907 x11_acquire_next_image_poll_x11(struct x11_swapchain
*chain
,
908 uint32_t *image_index
, uint64_t timeout
)
910 xcb_generic_event_t
*event
;
914 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
915 if (!chain
->images
[i
].busy
) {
916 /* We found a non-busy image */
917 xshmfence_await(chain
->images
[i
].shm_fence
);
919 chain
->images
[i
].busy
= true;
920 return x11_swapchain_result(chain
, VK_SUCCESS
);
924 xcb_flush(chain
->conn
);
926 if (timeout
== UINT64_MAX
) {
927 event
= xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
929 return x11_swapchain_result(chain
, VK_ERROR_OUT_OF_DATE_KHR
);
931 event
= xcb_poll_for_special_event(chain
->conn
, chain
->special_event
);
935 return x11_swapchain_result(chain
, VK_NOT_READY
);
937 atimeout
= wsi_get_absolute_timeout(timeout
);
939 pfds
.fd
= xcb_get_file_descriptor(chain
->conn
);
940 pfds
.events
= POLLIN
;
941 ret
= poll(&pfds
, 1, timeout
/ 1000 / 1000);
943 return x11_swapchain_result(chain
, VK_TIMEOUT
);
945 return x11_swapchain_result(chain
, VK_ERROR_OUT_OF_DATE_KHR
);
947 /* If a non-special event happens, the fd will still
948 * poll. So recalculate the timeout now just in case.
950 uint64_t current_time
= wsi_common_get_current_time();
951 if (atimeout
> current_time
)
952 timeout
= atimeout
- current_time
;
959 /* Update the swapchain status here. We may catch non-fatal errors here,
960 * in which case we need to update the status and continue.
962 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
965 return x11_swapchain_result(chain
, result
);
970 x11_acquire_next_image_from_queue(struct x11_swapchain
*chain
,
971 uint32_t *image_index_out
, uint64_t timeout
)
973 assert(chain
->has_acquire_queue
);
975 uint32_t image_index
;
976 VkResult result
= wsi_queue_pull(&chain
->acquire_queue
,
977 &image_index
, timeout
);
978 if (result
< 0 || result
== VK_TIMEOUT
) {
979 /* On error, the thread has shut down, so safe to update chain->status.
980 * Calling x11_swapchain_result with VK_TIMEOUT won't modify
981 * chain->status so that is also safe.
983 return x11_swapchain_result(chain
, result
);
984 } else if (chain
->status
< 0) {
985 return chain
->status
;
988 assert(image_index
< chain
->base
.image_count
);
989 xshmfence_await(chain
->images
[image_index
].shm_fence
);
991 *image_index_out
= image_index
;
993 return chain
->status
;
997 x11_present_to_x11(struct x11_swapchain
*chain
, uint32_t image_index
,
1000 struct x11_image
*image
= &chain
->images
[image_index
];
1002 assert(image_index
< chain
->base
.image_count
);
1004 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
1006 int64_t divisor
= 0;
1007 int64_t remainder
= 0;
1009 if (chain
->base
.present_mode
== VK_PRESENT_MODE_IMMEDIATE_KHR
)
1010 options
|= XCB_PRESENT_OPTION_ASYNC
;
1012 #ifdef HAVE_DRI3_MODIFIERS
1013 if (chain
->has_dri3_modifiers
)
1014 options
|= XCB_PRESENT_OPTION_SUBOPTIMAL
;
1017 /* Poll for any available event and update the swapchain status. This could
1018 * update the status of the swapchain to SUBOPTIMAL or OUT_OF_DATE if the
1019 * associated X11 surface has been resized.
1021 xcb_generic_event_t
*event
;
1022 while ((event
= xcb_poll_for_special_event(chain
->conn
, chain
->special_event
))) {
1023 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
1026 return x11_swapchain_result(chain
, result
);
1027 x11_swapchain_result(chain
, result
);
1030 xshmfence_reset(image
->shm_fence
);
1033 xcb_void_cookie_t cookie
=
1034 xcb_present_pixmap(chain
->conn
,
1037 (uint32_t) chain
->send_sbc
,
1042 XCB_NONE
, /* target_crtc */
1048 remainder
, 0, NULL
);
1049 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1051 xcb_flush(chain
->conn
);
1053 return x11_swapchain_result(chain
, VK_SUCCESS
);
1057 x11_acquire_next_image(struct wsi_swapchain
*anv_chain
,
1058 const VkAcquireNextImageInfoKHR
*info
,
1059 uint32_t *image_index
)
1061 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
1062 uint64_t timeout
= info
->timeout
;
1064 /* If the swapchain is in an error state, don't go any further. */
1065 if (chain
->status
< 0)
1066 return chain
->status
;
1068 if (chain
->has_acquire_queue
) {
1069 return x11_acquire_next_image_from_queue(chain
, image_index
, timeout
);
1071 return x11_acquire_next_image_poll_x11(chain
, image_index
, timeout
);
1076 x11_queue_present(struct wsi_swapchain
*anv_chain
,
1077 uint32_t image_index
,
1078 const VkPresentRegionKHR
*damage
)
1080 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
1082 /* If the swapchain is in an error state, don't go any further. */
1083 if (chain
->status
< 0)
1084 return chain
->status
;
1086 chain
->images
[image_index
].busy
= true;
1087 if (chain
->has_present_queue
) {
1088 wsi_queue_push(&chain
->present_queue
, image_index
);
1089 return chain
->status
;
1091 return x11_present_to_x11(chain
, image_index
, 0);
1096 x11_manage_fifo_queues(void *state
)
1098 struct x11_swapchain
*chain
= state
;
1099 VkResult result
= VK_SUCCESS
;
1101 assert(chain
->has_present_queue
);
1102 while (chain
->status
>= 0) {
1103 /* It should be safe to unconditionally block here. Later in the loop
1104 * we blocks until the previous present has landed on-screen. At that
1105 * point, we should have received IDLE_NOTIFY on all images presented
1106 * before that point so the client should be able to acquire any image
1107 * other than the currently presented one.
1109 uint32_t image_index
= 0;
1110 result
= wsi_queue_pull(&chain
->present_queue
, &image_index
, INT64_MAX
);
1111 assert(result
!= VK_TIMEOUT
);
1114 } else if (chain
->status
< 0) {
1115 /* The status can change underneath us if the swapchain is destroyed
1116 * from another thread.
1121 if (chain
->base
.present_mode
== VK_PRESENT_MODE_MAILBOX_KHR
) {
1122 result
= chain
->base
.wsi
->WaitForFences(chain
->base
.device
, 1,
1123 &chain
->base
.fences
[image_index
],
1125 if (result
!= VK_SUCCESS
) {
1126 result
= VK_ERROR_OUT_OF_DATE_KHR
;
1131 uint64_t target_msc
= 0;
1132 if (chain
->has_acquire_queue
)
1133 target_msc
= chain
->last_present_msc
+ 1;
1135 result
= x11_present_to_x11(chain
, image_index
, target_msc
);
1139 if (chain
->has_acquire_queue
) {
1140 while (chain
->last_present_msc
< target_msc
) {
1141 xcb_generic_event_t
*event
=
1142 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
1144 result
= VK_ERROR_OUT_OF_DATE_KHR
;
1148 result
= x11_handle_dri3_present_event(chain
, (void *)event
);
1157 x11_swapchain_result(chain
, result
);
1158 if (chain
->has_acquire_queue
)
1159 wsi_queue_push(&chain
->acquire_queue
, UINT32_MAX
);
1165 x11_image_init(VkDevice device_h
, struct x11_swapchain
*chain
,
1166 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
1167 const VkAllocationCallbacks
* pAllocator
,
1168 const uint64_t *const *modifiers
,
1169 const uint32_t *num_modifiers
,
1170 int num_tranches
, struct x11_image
*image
)
1172 xcb_void_cookie_t cookie
;
1176 if (chain
->base
.use_prime_blit
) {
1177 bool use_modifier
= num_tranches
> 0;
1178 result
= wsi_create_prime_image(&chain
->base
, pCreateInfo
, use_modifier
, &image
->base
);
1180 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
1181 num_tranches
, num_modifiers
, modifiers
,
1187 image
->pixmap
= xcb_generate_id(chain
->conn
);
1189 #ifdef HAVE_DRI3_MODIFIERS
1190 if (image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
) {
1191 /* If the image has a modifier, we must have DRI3 v1.2. */
1192 assert(chain
->has_dri3_modifiers
);
1195 xcb_dri3_pixmap_from_buffers_checked(chain
->conn
,
1198 image
->base
.num_planes
,
1199 pCreateInfo
->imageExtent
.width
,
1200 pCreateInfo
->imageExtent
.height
,
1201 image
->base
.row_pitches
[0],
1202 image
->base
.offsets
[0],
1203 image
->base
.row_pitches
[1],
1204 image
->base
.offsets
[1],
1205 image
->base
.row_pitches
[2],
1206 image
->base
.offsets
[2],
1207 image
->base
.row_pitches
[3],
1208 image
->base
.offsets
[3],
1210 image
->base
.drm_modifier
,
1215 /* Without passing modifiers, we can't have multi-plane RGB images. */
1216 assert(image
->base
.num_planes
== 1);
1219 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
1222 image
->base
.sizes
[0],
1223 pCreateInfo
->imageExtent
.width
,
1224 pCreateInfo
->imageExtent
.height
,
1225 image
->base
.row_pitches
[0],
1227 image
->base
.fds
[0]);
1230 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1232 /* XCB has now taken ownership of the FDs. */
1233 for (int i
= 0; i
< image
->base
.num_planes
; i
++)
1234 image
->base
.fds
[i
] = -1;
1236 int fence_fd
= xshmfence_alloc_shm();
1240 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
1241 if (image
->shm_fence
== NULL
)
1242 goto fail_shmfence_alloc
;
1244 image
->sync_fence
= xcb_generate_id(chain
->conn
);
1245 xcb_dri3_fence_from_fd(chain
->conn
,
1251 image
->busy
= false;
1252 xshmfence_trigger(image
->shm_fence
);
1256 fail_shmfence_alloc
:
1260 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
1261 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1263 wsi_destroy_image(&chain
->base
, &image
->base
);
1269 x11_image_finish(struct x11_swapchain
*chain
,
1270 const VkAllocationCallbacks
* pAllocator
,
1271 struct x11_image
*image
)
1273 xcb_void_cookie_t cookie
;
1275 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
1276 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1277 xshmfence_unmap_shm(image
->shm_fence
);
1279 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
1280 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1282 wsi_destroy_image(&chain
->base
, &image
->base
);
1286 wsi_x11_get_dri3_modifiers(struct wsi_x11_connection
*wsi_conn
,
1287 xcb_connection_t
*conn
, xcb_window_t window
,
1288 uint8_t depth
, uint8_t bpp
,
1289 VkCompositeAlphaFlagsKHR vk_alpha
,
1290 uint64_t **modifiers_in
, uint32_t *num_modifiers_in
,
1291 uint32_t *num_tranches_in
,
1292 const VkAllocationCallbacks
*pAllocator
)
1294 if (!wsi_conn
->has_dri3_modifiers
)
1297 #ifdef HAVE_DRI3_MODIFIERS
1298 xcb_generic_error_t
*error
= NULL
;
1299 xcb_dri3_get_supported_modifiers_cookie_t mod_cookie
=
1300 xcb_dri3_get_supported_modifiers(conn
, window
, depth
, bpp
);
1301 xcb_dri3_get_supported_modifiers_reply_t
*mod_reply
=
1302 xcb_dri3_get_supported_modifiers_reply(conn
, mod_cookie
, &error
);
1305 if (!mod_reply
|| (mod_reply
->num_window_modifiers
== 0 &&
1306 mod_reply
->num_screen_modifiers
== 0)) {
1313 uint64_t *modifiers
[2];
1315 if (mod_reply
->num_window_modifiers
) {
1316 counts
[n
] = mod_reply
->num_window_modifiers
;
1317 modifiers
[n
] = vk_alloc(pAllocator
,
1318 counts
[n
] * sizeof(uint64_t),
1319 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1320 if (!modifiers
[n
]) {
1325 memcpy(modifiers
[n
],
1326 xcb_dri3_get_supported_modifiers_window_modifiers(mod_reply
),
1327 counts
[n
] * sizeof(uint64_t));
1331 if (mod_reply
->num_screen_modifiers
) {
1332 counts
[n
] = mod_reply
->num_screen_modifiers
;
1333 modifiers
[n
] = vk_alloc(pAllocator
,
1334 counts
[n
] * sizeof(uint64_t),
1335 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1336 if (!modifiers
[n
]) {
1338 vk_free(pAllocator
, modifiers
[0]);
1343 memcpy(modifiers
[n
],
1344 xcb_dri3_get_supported_modifiers_screen_modifiers(mod_reply
),
1345 counts
[n
] * sizeof(uint64_t));
1349 for (int i
= 0; i
< n
; i
++) {
1350 modifiers_in
[i
] = modifiers
[i
];
1351 num_modifiers_in
[i
] = counts
[i
];
1353 *num_tranches_in
= n
;
1359 *num_tranches_in
= 0;
1363 x11_swapchain_destroy(struct wsi_swapchain
*anv_chain
,
1364 const VkAllocationCallbacks
*pAllocator
)
1366 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
1367 xcb_void_cookie_t cookie
;
1369 if (chain
->has_present_queue
) {
1370 chain
->status
= VK_ERROR_OUT_OF_DATE_KHR
;
1371 /* Push a UINT32_MAX to wake up the manager */
1372 wsi_queue_push(&chain
->present_queue
, UINT32_MAX
);
1373 pthread_join(chain
->queue_manager
, NULL
);
1375 if (chain
->has_acquire_queue
)
1376 wsi_queue_destroy(&chain
->acquire_queue
);
1377 wsi_queue_destroy(&chain
->present_queue
);
1380 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++)
1381 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
1383 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
1384 cookie
= xcb_present_select_input_checked(chain
->conn
, chain
->event_id
,
1386 XCB_PRESENT_EVENT_MASK_NO_EVENT
);
1387 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1389 wsi_swapchain_finish(&chain
->base
);
1391 vk_free(pAllocator
, chain
);
1397 wsi_x11_set_adaptive_sync_property(xcb_connection_t
*conn
,
1398 xcb_drawable_t drawable
,
1401 static char const name
[] = "_VARIABLE_REFRESH";
1402 xcb_intern_atom_cookie_t cookie
;
1403 xcb_intern_atom_reply_t
* reply
;
1404 xcb_void_cookie_t check
;
1406 cookie
= xcb_intern_atom(conn
, 0, strlen(name
), name
);
1407 reply
= xcb_intern_atom_reply(conn
, cookie
, NULL
);
1412 check
= xcb_change_property_checked(conn
, XCB_PROP_MODE_REPLACE
,
1413 drawable
, reply
->atom
,
1414 XCB_ATOM_CARDINAL
, 32, 1, &state
);
1416 check
= xcb_delete_property_checked(conn
, drawable
, reply
->atom
);
1418 xcb_discard_reply(conn
, check
.sequence
);
1424 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
1426 struct wsi_device
*wsi_device
,
1427 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
1428 const VkAllocationCallbacks
* pAllocator
,
1429 struct wsi_swapchain
**swapchain_out
)
1431 struct x11_swapchain
*chain
;
1432 xcb_void_cookie_t cookie
;
1434 VkPresentModeKHR present_mode
= wsi_swapchain_get_present_mode(wsi_device
, pCreateInfo
);
1436 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
1438 unsigned num_images
= pCreateInfo
->minImageCount
;
1439 if (wsi_device
->x11
.strict_imageCount
)
1440 num_images
= pCreateInfo
->minImageCount
;
1441 else if (present_mode
== VK_PRESENT_MODE_MAILBOX_KHR
)
1442 num_images
= MAX2(num_images
, 5);
1444 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
1445 struct wsi_x11_connection
*wsi_conn
=
1446 wsi_x11_get_connection(wsi_device
, conn
);
1448 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1450 /* Check for whether or not we have a window up-front */
1451 xcb_window_t window
= x11_surface_get_window(icd_surface
);
1452 xcb_get_geometry_reply_t
*geometry
=
1453 xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, window
), NULL
);
1454 if (geometry
== NULL
)
1455 return VK_ERROR_SURFACE_LOST_KHR
;
1456 const uint32_t bit_depth
= geometry
->depth
;
1459 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
1460 chain
= vk_alloc(pAllocator
, size
, 8,
1461 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1463 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1465 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
1466 pCreateInfo
, pAllocator
);
1467 if (result
!= VK_SUCCESS
)
1470 chain
->base
.destroy
= x11_swapchain_destroy
;
1471 chain
->base
.get_wsi_image
= x11_get_wsi_image
;
1472 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
1473 chain
->base
.queue_present
= x11_queue_present
;
1474 chain
->base
.present_mode
= present_mode
;
1475 chain
->base
.image_count
= num_images
;
1477 chain
->window
= window
;
1478 chain
->depth
= bit_depth
;
1479 chain
->extent
= pCreateInfo
->imageExtent
;
1480 chain
->send_sbc
= 0;
1481 chain
->last_present_msc
= 0;
1482 chain
->has_acquire_queue
= false;
1483 chain
->has_present_queue
= false;
1484 chain
->status
= VK_SUCCESS
;
1485 chain
->has_dri3_modifiers
= wsi_conn
->has_dri3_modifiers
;
1487 /* If we are reallocating from an old swapchain, then we inherit its
1488 * last completion mode, to ensure we don't get into reallocation
1489 * cycles. If we are starting anew, we set 'COPY', as that is the only
1490 * mode which provokes reallocation when anything changes, to make
1491 * sure we have the most optimal allocation.
1493 VK_FROM_HANDLE(x11_swapchain
, old_chain
, pCreateInfo
->oldSwapchain
);
1495 chain
->last_present_mode
= old_chain
->last_present_mode
;
1497 chain
->last_present_mode
= XCB_PRESENT_COMPLETE_MODE_COPY
;
1499 if (!wsi_x11_check_dri3_compatible(wsi_device
, conn
))
1500 chain
->base
.use_prime_blit
= true;
1502 chain
->event_id
= xcb_generate_id(chain
->conn
);
1503 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
1504 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
1505 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
1506 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
1508 /* Create an XCB event queue to hold present events outside of the usual
1509 * application event queue
1511 chain
->special_event
=
1512 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
1513 chain
->event_id
, NULL
);
1515 chain
->gc
= xcb_generate_id(chain
->conn
);
1517 /* FINISHME: Choose a better error. */
1518 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1522 cookie
= xcb_create_gc(chain
->conn
,
1525 XCB_GC_GRAPHICS_EXPOSURES
,
1526 (uint32_t []) { 0 });
1527 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1529 uint64_t *modifiers
[2] = {NULL
, NULL
};
1530 uint32_t num_modifiers
[2] = {0, 0};
1531 uint32_t num_tranches
= 0;
1532 if (wsi_device
->supports_modifiers
)
1533 wsi_x11_get_dri3_modifiers(wsi_conn
, conn
, window
, chain
->depth
, 32,
1534 pCreateInfo
->compositeAlpha
,
1535 modifiers
, num_modifiers
, &num_tranches
,
1539 for (; image
< chain
->base
.image_count
; image
++) {
1540 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
1541 (const uint64_t *const *)modifiers
,
1542 num_modifiers
, num_tranches
,
1543 &chain
->images
[image
]);
1544 if (result
!= VK_SUCCESS
)
1545 goto fail_init_images
;
1548 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
||
1549 chain
->base
.present_mode
== VK_PRESENT_MODE_MAILBOX_KHR
) {
1550 chain
->has_present_queue
= true;
1552 /* Initialize our queues. We make them base.image_count + 1 because we will
1553 * occasionally use UINT32_MAX to signal the other thread that an error
1554 * has occurred and we don't want an overflow.
1557 ret
= wsi_queue_init(&chain
->present_queue
, chain
->base
.image_count
+ 1);
1559 goto fail_init_images
;
1562 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
1563 chain
->has_acquire_queue
= true;
1565 ret
= wsi_queue_init(&chain
->acquire_queue
, chain
->base
.image_count
+ 1);
1567 wsi_queue_destroy(&chain
->present_queue
);
1568 goto fail_init_images
;
1571 for (unsigned i
= 0; i
< chain
->base
.image_count
; i
++)
1572 wsi_queue_push(&chain
->acquire_queue
, i
);
1575 ret
= pthread_create(&chain
->queue_manager
, NULL
,
1576 x11_manage_fifo_queues
, chain
);
1578 wsi_queue_destroy(&chain
->present_queue
);
1579 if (chain
->has_acquire_queue
)
1580 wsi_queue_destroy(&chain
->acquire_queue
);
1582 goto fail_init_images
;
1586 assert(chain
->has_present_queue
|| !chain
->has_acquire_queue
);
1588 for (int i
= 0; i
< ARRAY_SIZE(modifiers
); i
++)
1589 vk_free(pAllocator
, modifiers
[i
]);
1591 /* It is safe to set it here as only one swapchain can be associated with
1592 * the window, and swapchain creation does the association. At this point
1593 * we know the creation is going to succeed. */
1594 wsi_x11_set_adaptive_sync_property(conn
, window
,
1595 wsi_device
->enable_adaptive_sync
);
1597 *swapchain_out
= &chain
->base
;
1602 for (uint32_t j
= 0; j
< image
; j
++)
1603 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
1605 for (int i
= 0; i
< ARRAY_SIZE(modifiers
); i
++)
1606 vk_free(pAllocator
, modifiers
[i
]);
1609 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
1611 wsi_swapchain_finish(&chain
->base
);
1614 vk_free(pAllocator
, chain
);
1620 wsi_x11_init_wsi(struct wsi_device
*wsi_device
,
1621 const VkAllocationCallbacks
*alloc
,
1622 const struct driOptionCache
*dri_options
)
1624 struct wsi_x11
*wsi
;
1627 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1628 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1630 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1634 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
1636 if (ret
== ENOMEM
) {
1637 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1639 /* FINISHME: Choose a better error. */
1640 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1646 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1647 _mesa_key_pointer_equal
);
1648 if (!wsi
->connections
) {
1649 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1654 if (driCheckOption(dri_options
, "vk_x11_override_min_image_count", DRI_INT
)) {
1655 wsi_device
->x11
.override_minImageCount
=
1656 driQueryOptioni(dri_options
, "vk_x11_override_min_image_count");
1658 if (driCheckOption(dri_options
, "vk_x11_strict_image_count", DRI_BOOL
)) {
1659 wsi_device
->x11
.strict_imageCount
=
1660 driQueryOptionb(dri_options
, "vk_x11_strict_image_count");
1664 wsi
->base
.get_support
= x11_surface_get_support
;
1665 wsi
->base
.get_capabilities2
= x11_surface_get_capabilities2
;
1666 wsi
->base
.get_formats
= x11_surface_get_formats
;
1667 wsi
->base
.get_formats2
= x11_surface_get_formats2
;
1668 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
1669 wsi
->base
.get_present_rectangles
= x11_surface_get_present_rectangles
;
1670 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
1672 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
1673 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
1678 pthread_mutex_destroy(&wsi
->mutex
);
1680 vk_free(alloc
, wsi
);
1682 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
1683 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
1689 wsi_x11_finish_wsi(struct wsi_device
*wsi_device
,
1690 const VkAllocationCallbacks
*alloc
)
1692 struct wsi_x11
*wsi
=
1693 (struct wsi_x11
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
1696 hash_table_foreach(wsi
->connections
, entry
)
1697 wsi_x11_connection_destroy(wsi_device
, entry
->data
);
1699 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
1701 pthread_mutex_destroy(&wsi
->mutex
);
1703 vk_free(alloc
, wsi
);