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"
43 #include "wsi_common_private.h"
44 #include "wsi_common_x11.h"
45 #include "wsi_common_queue.h"
47 #define typed_memcpy(dest, src, count) ({ \
48 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
49 memcpy((dest), (src), (count) * sizeof(*(src))); \
52 struct wsi_x11_connection
{
54 bool has_dri3_modifiers
;
56 bool is_proprietary_x11
;
60 struct wsi_interface base
;
62 pthread_mutex_t mutex
;
63 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
64 struct hash_table
*connections
;
70 * Wrapper around xcb_dri3_open
73 wsi_dri3_open(xcb_connection_t
*conn
,
77 xcb_dri3_open_cookie_t cookie
;
78 xcb_dri3_open_reply_t
*reply
;
81 cookie
= xcb_dri3_open(conn
,
85 reply
= xcb_dri3_open_reply(conn
, cookie
, NULL
);
89 if (reply
->nfd
!= 1) {
94 fd
= xcb_dri3_open_reply_fds(conn
, reply
)[0];
96 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
102 wsi_x11_check_dri3_compatible(const struct wsi_device
*wsi_dev
,
103 xcb_connection_t
*conn
)
105 xcb_screen_iterator_t screen_iter
=
106 xcb_setup_roots_iterator(xcb_get_setup(conn
));
107 xcb_screen_t
*screen
= screen_iter
.data
;
109 int dri3_fd
= wsi_dri3_open(conn
, screen
->root
, None
);
113 bool match
= wsi_device_matches_drm_fd(wsi_dev
, dri3_fd
);
120 static struct wsi_x11_connection
*
121 wsi_x11_connection_create(struct wsi_device
*wsi_dev
,
122 xcb_connection_t
*conn
)
124 xcb_query_extension_cookie_t dri3_cookie
, pres_cookie
, amd_cookie
, nv_cookie
;
125 xcb_query_extension_reply_t
*dri3_reply
, *pres_reply
, *amd_reply
, *nv_reply
;
126 bool has_dri3_v1_2
= false;
127 bool has_present_v1_2
= false;
129 struct wsi_x11_connection
*wsi_conn
=
130 vk_alloc(&wsi_dev
->instance_alloc
, sizeof(*wsi_conn
), 8,
131 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
135 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
136 pres_cookie
= xcb_query_extension(conn
, 7, "Present");
138 /* We try to be nice to users and emit a warning if they try to use a
139 * Vulkan application on a system without DRI3 enabled. However, this ends
140 * up spewing the warning when a user has, for example, both Intel
141 * integrated graphics and a discrete card with proprietary drivers and are
142 * running on the discrete card with the proprietary DDX. In this case, we
143 * really don't want to print the warning because it just confuses users.
144 * As a heuristic to detect this case, we check for a couple of proprietary
147 amd_cookie
= xcb_query_extension(conn
, 11, "ATIFGLRXDRI");
148 nv_cookie
= xcb_query_extension(conn
, 10, "NV-CONTROL");
150 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
151 pres_reply
= xcb_query_extension_reply(conn
, pres_cookie
, NULL
);
152 amd_reply
= xcb_query_extension_reply(conn
, amd_cookie
, NULL
);
153 nv_reply
= xcb_query_extension_reply(conn
, nv_cookie
, NULL
);
154 if (!dri3_reply
|| !pres_reply
) {
159 vk_free(&wsi_dev
->instance_alloc
, wsi_conn
);
163 wsi_conn
->has_dri3
= dri3_reply
->present
!= 0;
164 #ifdef HAVE_DRI3_MODIFIERS
165 if (wsi_conn
->has_dri3
) {
166 xcb_dri3_query_version_cookie_t ver_cookie
;
167 xcb_dri3_query_version_reply_t
*ver_reply
;
169 ver_cookie
= xcb_dri3_query_version(conn
, 1, 2);
170 ver_reply
= xcb_dri3_query_version_reply(conn
, ver_cookie
, NULL
);
172 (ver_reply
->major_version
> 1 || ver_reply
->minor_version
>= 2);
177 wsi_conn
->has_present
= pres_reply
->present
!= 0;
178 #ifdef HAVE_DRI3_MODIFIERS
179 if (wsi_conn
->has_present
) {
180 xcb_present_query_version_cookie_t ver_cookie
;
181 xcb_present_query_version_reply_t
*ver_reply
;
183 ver_cookie
= xcb_present_query_version(conn
, 1, 2);
184 ver_reply
= xcb_present_query_version_reply(conn
, ver_cookie
, NULL
);
186 (ver_reply
->major_version
> 1 || ver_reply
->minor_version
>= 2);
191 wsi_conn
->has_dri3_modifiers
= has_dri3_v1_2
&& has_present_v1_2
;
192 wsi_conn
->is_proprietary_x11
= false;
193 if (amd_reply
&& amd_reply
->present
)
194 wsi_conn
->is_proprietary_x11
= true;
195 if (nv_reply
&& nv_reply
->present
)
196 wsi_conn
->is_proprietary_x11
= true;
207 wsi_x11_connection_destroy(struct wsi_device
*wsi_dev
,
208 struct wsi_x11_connection
*conn
)
210 vk_free(&wsi_dev
->instance_alloc
, conn
);
214 wsi_x11_check_for_dri3(struct wsi_x11_connection
*wsi_conn
)
216 if (wsi_conn
->has_dri3
)
218 if (!wsi_conn
->is_proprietary_x11
) {
219 fprintf(stderr
, "vulkan: No DRI3 support detected - required for presentation\n"
220 "Note: you can probably enable DRI3 in your Xorg config\n");
225 static struct wsi_x11_connection
*
226 wsi_x11_get_connection(struct wsi_device
*wsi_dev
,
227 xcb_connection_t
*conn
)
229 struct wsi_x11
*wsi
=
230 (struct wsi_x11
*)wsi_dev
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
232 pthread_mutex_lock(&wsi
->mutex
);
234 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
236 /* We're about to make a bunch of blocking calls. Let's drop the
237 * mutex for now so we don't block up too badly.
239 pthread_mutex_unlock(&wsi
->mutex
);
241 struct wsi_x11_connection
*wsi_conn
=
242 wsi_x11_connection_create(wsi_dev
, conn
);
246 pthread_mutex_lock(&wsi
->mutex
);
248 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
250 /* Oops, someone raced us to it */
251 wsi_x11_connection_destroy(wsi_dev
, wsi_conn
);
253 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
257 pthread_mutex_unlock(&wsi
->mutex
);
262 static const VkFormat formats
[] = {
263 VK_FORMAT_B8G8R8A8_SRGB
,
264 VK_FORMAT_B8G8R8A8_UNORM
,
267 static const VkPresentModeKHR present_modes
[] = {
268 VK_PRESENT_MODE_IMMEDIATE_KHR
,
269 VK_PRESENT_MODE_MAILBOX_KHR
,
270 VK_PRESENT_MODE_FIFO_KHR
,
273 static xcb_screen_t
*
274 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
276 xcb_screen_iterator_t screen_iter
=
277 xcb_setup_roots_iterator(xcb_get_setup(conn
));
279 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
280 if (screen_iter
.data
->root
== root
)
281 return screen_iter
.data
;
287 static xcb_visualtype_t
*
288 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
291 xcb_depth_iterator_t depth_iter
=
292 xcb_screen_allowed_depths_iterator(screen
);
294 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
295 xcb_visualtype_iterator_t visual_iter
=
296 xcb_depth_visuals_iterator (depth_iter
.data
);
298 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
299 if (visual_iter
.data
->visual_id
== visual_id
) {
301 *depth
= depth_iter
.data
->depth
;
302 return visual_iter
.data
;
310 static xcb_visualtype_t
*
311 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
314 xcb_screen_iterator_t screen_iter
=
315 xcb_setup_roots_iterator(xcb_get_setup(conn
));
317 /* For this we have to iterate over all of the screens which is rather
318 * annoying. Fortunately, there is probably only 1.
320 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
321 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
330 static xcb_visualtype_t
*
331 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
334 xcb_query_tree_cookie_t tree_cookie
;
335 xcb_get_window_attributes_cookie_t attrib_cookie
;
336 xcb_query_tree_reply_t
*tree
;
337 xcb_get_window_attributes_reply_t
*attrib
;
339 tree_cookie
= xcb_query_tree(conn
, window
);
340 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
342 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
343 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
344 if (attrib
== NULL
|| tree
== NULL
) {
350 xcb_window_t root
= tree
->root
;
351 xcb_visualid_t visual_id
= attrib
->visual
;
355 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
359 return screen_get_visualtype(screen
, visual_id
, depth
);
363 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
365 uint32_t rgb_mask
= visual
->red_mask
|
369 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
371 /* Do we have bits left over after RGB? */
372 return (all_mask
& ~rgb_mask
) != 0;
375 VkBool32
wsi_get_physical_device_xcb_presentation_support(
376 struct wsi_device
*wsi_device
,
377 uint32_t queueFamilyIndex
,
378 xcb_connection_t
* connection
,
379 xcb_visualid_t visual_id
)
381 struct wsi_x11_connection
*wsi_conn
=
382 wsi_x11_get_connection(wsi_device
, connection
);
387 if (!wsi_x11_check_for_dri3(wsi_conn
))
390 unsigned visual_depth
;
391 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
394 if (visual_depth
!= 24 && visual_depth
!= 32)
400 static xcb_connection_t
*
401 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
403 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
404 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
406 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
410 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
412 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
413 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
415 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
419 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
420 struct wsi_device
*wsi_device
,
421 uint32_t queueFamilyIndex
,
422 VkBool32
* pSupported
)
424 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
425 xcb_window_t window
= x11_surface_get_window(icd_surface
);
427 struct wsi_x11_connection
*wsi_conn
=
428 wsi_x11_get_connection(wsi_device
, conn
);
430 return VK_ERROR_OUT_OF_HOST_MEMORY
;
432 if (!wsi_x11_check_for_dri3(wsi_conn
)) {
437 unsigned visual_depth
;
438 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
443 if (visual_depth
!= 24 && visual_depth
!= 32) {
453 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
454 struct wsi_device
*wsi_device
,
455 VkSurfaceCapabilitiesKHR
*caps
)
457 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
458 xcb_window_t window
= x11_surface_get_window(icd_surface
);
459 xcb_get_geometry_cookie_t geom_cookie
;
460 xcb_generic_error_t
*err
;
461 xcb_get_geometry_reply_t
*geom
;
462 unsigned visual_depth
;
464 geom_cookie
= xcb_get_geometry(conn
, window
);
466 /* This does a round-trip. This is why we do get_geometry first and
467 * wait to read the reply until after we have a visual.
469 xcb_visualtype_t
*visual
=
470 get_visualtype_for_window(conn
, window
, &visual_depth
);
473 return VK_ERROR_SURFACE_LOST_KHR
;
475 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
477 VkExtent2D extent
= { geom
->width
, geom
->height
};
478 caps
->currentExtent
= extent
;
479 caps
->minImageExtent
= extent
;
480 caps
->maxImageExtent
= extent
;
482 /* This can happen if the client didn't wait for the configure event
483 * to come back from the compositor. In that case, we don't know the
484 * size of the window so we just return valid "I don't know" stuff.
486 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
487 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
488 caps
->maxImageExtent
= (VkExtent2D
) {
489 wsi_device
->maxImageDimension2D
,
490 wsi_device
->maxImageDimension2D
,
496 if (visual_has_alpha(visual
, visual_depth
)) {
497 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
498 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
500 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
501 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
504 /* For true mailbox mode, we need at least 4 images:
505 * 1) One to scan out from
506 * 2) One to have queued for scan-out
507 * 3) One to be currently held by the X server
508 * 4) One to render to
510 caps
->minImageCount
= 2;
511 /* There is no real maximum */
512 caps
->maxImageCount
= 0;
514 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
515 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
516 caps
->maxImageArrayLayers
= 1;
517 caps
->supportedUsageFlags
=
518 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
519 VK_IMAGE_USAGE_SAMPLED_BIT
|
520 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
521 VK_IMAGE_USAGE_STORAGE_BIT
|
522 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
528 x11_surface_get_capabilities2(VkIcdSurfaceBase
*icd_surface
,
529 struct wsi_device
*wsi_device
,
530 const void *info_next
,
531 VkSurfaceCapabilities2KHR
*caps
)
533 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
535 return x11_surface_get_capabilities(icd_surface
, wsi_device
, &caps
->surfaceCapabilities
);
539 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
540 struct wsi_device
*wsi_device
,
541 uint32_t *pSurfaceFormatCount
,
542 VkSurfaceFormatKHR
*pSurfaceFormats
)
544 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
546 for (unsigned i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
547 vk_outarray_append(&out
, f
) {
548 f
->format
= formats
[i
];
549 f
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
553 return vk_outarray_status(&out
);
557 x11_surface_get_formats2(VkIcdSurfaceBase
*surface
,
558 struct wsi_device
*wsi_device
,
559 const void *info_next
,
560 uint32_t *pSurfaceFormatCount
,
561 VkSurfaceFormat2KHR
*pSurfaceFormats
)
563 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
565 for (unsigned i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
566 vk_outarray_append(&out
, f
) {
567 assert(f
->sType
== VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR
);
568 f
->surfaceFormat
.format
= formats
[i
];
569 f
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
573 return vk_outarray_status(&out
);
577 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
578 uint32_t *pPresentModeCount
,
579 VkPresentModeKHR
*pPresentModes
)
581 if (pPresentModes
== NULL
) {
582 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
586 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
587 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
589 return *pPresentModeCount
< ARRAY_SIZE(present_modes
) ?
590 VK_INCOMPLETE
: VK_SUCCESS
;
594 x11_surface_is_local_to_gpu(struct wsi_device
*wsi_dev
,
595 xcb_connection_t
*conn
)
597 struct wsi_x11_connection
*wsi_conn
=
598 wsi_x11_get_connection(wsi_dev
, conn
);
603 if (!wsi_x11_check_for_dri3(wsi_conn
))
606 if (!wsi_x11_check_dri3_compatible(wsi_dev
, conn
))
613 x11_surface_get_present_rectangles(VkIcdSurfaceBase
*icd_surface
,
614 struct wsi_device
*wsi_device
,
615 uint32_t* pRectCount
,
618 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
619 xcb_window_t window
= x11_surface_get_window(icd_surface
);
620 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
622 if (x11_surface_is_local_to_gpu(wsi_device
, conn
)) {
623 vk_outarray_append(&out
, rect
) {
624 xcb_generic_error_t
*err
= NULL
;
625 xcb_get_geometry_cookie_t geom_cookie
= xcb_get_geometry(conn
, window
);
626 xcb_get_geometry_reply_t
*geom
=
627 xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
632 .extent
= { geom
->width
, geom
->height
},
635 /* This can happen if the client didn't wait for the configure event
636 * to come back from the compositor. In that case, we don't know the
637 * size of the window so we just return valid "I don't know" stuff.
641 .extent
= { -1, -1 },
648 return vk_outarray_status(&out
);
651 VkResult
wsi_create_xcb_surface(const VkAllocationCallbacks
*pAllocator
,
652 const VkXcbSurfaceCreateInfoKHR
*pCreateInfo
,
653 VkSurfaceKHR
*pSurface
)
655 VkIcdSurfaceXcb
*surface
;
657 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
658 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
660 return VK_ERROR_OUT_OF_HOST_MEMORY
;
662 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
663 surface
->connection
= pCreateInfo
->connection
;
664 surface
->window
= pCreateInfo
->window
;
666 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
670 VkResult
wsi_create_xlib_surface(const VkAllocationCallbacks
*pAllocator
,
671 const VkXlibSurfaceCreateInfoKHR
*pCreateInfo
,
672 VkSurfaceKHR
*pSurface
)
674 VkIcdSurfaceXlib
*surface
;
676 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
677 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
679 return VK_ERROR_OUT_OF_HOST_MEMORY
;
681 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
682 surface
->dpy
= pCreateInfo
->dpy
;
683 surface
->window
= pCreateInfo
->window
;
685 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
690 struct wsi_image base
;
693 struct xshmfence
* shm_fence
;
697 struct x11_swapchain
{
698 struct wsi_swapchain base
;
700 bool has_dri3_modifiers
;
702 xcb_connection_t
* conn
;
708 xcb_present_event_t event_id
;
709 xcb_special_event_t
* special_event
;
711 uint64_t last_present_msc
;
716 xcb_present_complete_mode_t last_present_mode
;
717 struct wsi_queue present_queue
;
718 struct wsi_queue acquire_queue
;
719 pthread_t queue_manager
;
721 struct x11_image images
[0];
723 WSI_DEFINE_NONDISP_HANDLE_CASTS(x11_swapchain
, VkSwapchainKHR
)
726 * Update the swapchain status with the result of an operation, and return
727 * the combined status. The chain status will eventually be returned from
728 * AcquireNextImage and QueuePresent.
730 * We make sure to 'stick' more pessimistic statuses: an out-of-date error
731 * is permanent once seen, and every subsequent call will return this. If
732 * this has not been seen, success will be returned.
735 x11_swapchain_result(struct x11_swapchain
*chain
, VkResult result
)
737 /* Prioritise returning existing errors for consistency. */
738 if (chain
->status
< 0)
739 return chain
->status
;
741 /* If we have a new error, mark it as permanent on the chain and return. */
743 chain
->status
= result
;
747 /* Return temporary errors, but don't persist them. */
748 if (result
== VK_TIMEOUT
|| result
== VK_NOT_READY
)
751 /* Suboptimal isn't an error, but is a status which sticks to the swapchain
752 * and is always returned rather than success.
754 if (result
== VK_SUBOPTIMAL_KHR
) {
755 chain
->status
= result
;
759 /* No changes, so return the last status. */
760 return chain
->status
;
763 static struct wsi_image
*
764 x11_get_wsi_image(struct wsi_swapchain
*wsi_chain
, uint32_t image_index
)
766 struct x11_swapchain
*chain
= (struct x11_swapchain
*)wsi_chain
;
767 return &chain
->images
[image_index
].base
;
771 * Process an X11 Present event. Does not update chain->status.
774 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
775 xcb_present_generic_event_t
*event
)
777 switch (event
->evtype
) {
778 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
779 xcb_present_configure_notify_event_t
*config
= (void *) event
;
781 if (config
->width
!= chain
->extent
.width
||
782 config
->height
!= chain
->extent
.height
)
783 return VK_ERROR_OUT_OF_DATE_KHR
;
788 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
789 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
791 for (unsigned i
= 0; i
< chain
->base
.image_count
; i
++) {
792 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
793 chain
->images
[i
].busy
= false;
795 wsi_queue_push(&chain
->acquire_queue
, i
);
803 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY
: {
804 xcb_present_complete_notify_event_t
*complete
= (void *) event
;
805 if (complete
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
)
806 chain
->last_present_msc
= complete
->msc
;
808 VkResult result
= VK_SUCCESS
;
810 /* The winsys is now trying to flip directly and cannot due to our
811 * configuration. Request the user reallocate.
813 #ifdef HAVE_DRI3_MODIFIERS
814 if (complete
->mode
== XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY
&&
815 chain
->last_present_mode
!= XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY
)
816 result
= VK_SUBOPTIMAL_KHR
;
819 /* When we go from flipping to copying, the odds are very likely that
820 * we could reallocate in a more optimal way if we didn't have to care
821 * about scanout, so we always do this.
823 if (complete
->mode
== XCB_PRESENT_COMPLETE_MODE_COPY
&&
824 chain
->last_present_mode
== XCB_PRESENT_COMPLETE_MODE_FLIP
)
825 result
= VK_SUBOPTIMAL_KHR
;
827 chain
->last_present_mode
= complete
->mode
;
839 static uint64_t wsi_get_absolute_timeout(uint64_t timeout
)
841 uint64_t current_time
= wsi_common_get_current_time();
843 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
845 return current_time
+ timeout
;
849 x11_acquire_next_image_poll_x11(struct x11_swapchain
*chain
,
850 uint32_t *image_index
, uint64_t timeout
)
852 xcb_generic_event_t
*event
;
856 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
857 if (!chain
->images
[i
].busy
) {
858 /* We found a non-busy image */
859 xshmfence_await(chain
->images
[i
].shm_fence
);
861 chain
->images
[i
].busy
= true;
862 return x11_swapchain_result(chain
, VK_SUCCESS
);
866 xcb_flush(chain
->conn
);
868 if (timeout
== UINT64_MAX
) {
869 event
= xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
871 return x11_swapchain_result(chain
, VK_ERROR_OUT_OF_DATE_KHR
);
873 event
= xcb_poll_for_special_event(chain
->conn
, chain
->special_event
);
877 return x11_swapchain_result(chain
, VK_NOT_READY
);
879 atimeout
= wsi_get_absolute_timeout(timeout
);
881 pfds
.fd
= xcb_get_file_descriptor(chain
->conn
);
882 pfds
.events
= POLLIN
;
883 ret
= poll(&pfds
, 1, timeout
/ 1000 / 1000);
885 return x11_swapchain_result(chain
, VK_TIMEOUT
);
887 return x11_swapchain_result(chain
, VK_ERROR_OUT_OF_DATE_KHR
);
889 /* If a non-special event happens, the fd will still
890 * poll. So recalculate the timeout now just in case.
892 uint64_t current_time
= wsi_common_get_current_time();
893 if (atimeout
> current_time
)
894 timeout
= atimeout
- current_time
;
901 /* Update the swapchain status here. We may catch non-fatal errors here,
902 * in which case we need to update the status and continue.
904 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
907 return x11_swapchain_result(chain
, result
);
912 x11_acquire_next_image_from_queue(struct x11_swapchain
*chain
,
913 uint32_t *image_index_out
, uint64_t timeout
)
915 assert(chain
->threaded
);
917 uint32_t image_index
;
918 VkResult result
= wsi_queue_pull(&chain
->acquire_queue
,
919 &image_index
, timeout
);
920 if (result
< 0 || result
== VK_TIMEOUT
) {
921 /* On error, the thread has shut down, so safe to update chain->status.
922 * Calling x11_swapchain_result with VK_TIMEOUT won't modify
923 * chain->status so that is also safe.
925 return x11_swapchain_result(chain
, result
);
926 } else if (chain
->status
< 0) {
927 return chain
->status
;
930 assert(image_index
< chain
->base
.image_count
);
931 xshmfence_await(chain
->images
[image_index
].shm_fence
);
933 *image_index_out
= image_index
;
935 return chain
->status
;
939 x11_present_to_x11(struct x11_swapchain
*chain
, uint32_t image_index
,
942 struct x11_image
*image
= &chain
->images
[image_index
];
944 assert(image_index
< chain
->base
.image_count
);
946 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
949 int64_t remainder
= 0;
951 if (chain
->base
.present_mode
== VK_PRESENT_MODE_IMMEDIATE_KHR
)
952 options
|= XCB_PRESENT_OPTION_ASYNC
;
954 #ifdef HAVE_DRI3_MODIFIERS
955 if (chain
->has_dri3_modifiers
)
956 options
|= XCB_PRESENT_OPTION_SUBOPTIMAL
;
959 xshmfence_reset(image
->shm_fence
);
962 xcb_void_cookie_t cookie
=
963 xcb_present_pixmap(chain
->conn
,
966 (uint32_t) chain
->send_sbc
,
971 XCB_NONE
, /* target_crtc */
978 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
981 xcb_flush(chain
->conn
);
983 return x11_swapchain_result(chain
, VK_SUCCESS
);
987 x11_acquire_next_image(struct wsi_swapchain
*anv_chain
,
988 const VkAcquireNextImageInfoKHR
*info
,
989 uint32_t *image_index
)
991 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
992 uint64_t timeout
= info
->timeout
;
994 if (chain
->threaded
) {
995 return x11_acquire_next_image_from_queue(chain
, image_index
, timeout
);
997 return x11_acquire_next_image_poll_x11(chain
, image_index
, timeout
);
1002 x11_queue_present(struct wsi_swapchain
*anv_chain
,
1003 uint32_t image_index
,
1004 const VkPresentRegionKHR
*damage
)
1006 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
1008 if (chain
->threaded
) {
1009 wsi_queue_push(&chain
->present_queue
, image_index
);
1010 return chain
->status
;
1012 return x11_present_to_x11(chain
, image_index
, 0);
1017 x11_manage_fifo_queues(void *state
)
1019 struct x11_swapchain
*chain
= state
;
1020 VkResult result
= VK_SUCCESS
;
1022 assert(chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
);
1024 while (chain
->status
>= 0) {
1025 /* It should be safe to unconditionally block here. Later in the loop
1026 * we blocks until the previous present has landed on-screen. At that
1027 * point, we should have received IDLE_NOTIFY on all images presented
1028 * before that point so the client should be able to acquire any image
1029 * other than the currently presented one.
1031 uint32_t image_index
= 0;
1032 result
= wsi_queue_pull(&chain
->present_queue
, &image_index
, INT64_MAX
);
1033 assert(result
!= VK_TIMEOUT
);
1036 } else if (chain
->status
< 0) {
1037 /* The status can change underneath us if the swapchain is destroyed
1038 * from another thread.
1043 uint64_t target_msc
= chain
->last_present_msc
+ 1;
1044 result
= x11_present_to_x11(chain
, image_index
, target_msc
);
1048 while (chain
->last_present_msc
< target_msc
) {
1049 xcb_generic_event_t
*event
=
1050 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
1052 result
= VK_ERROR_OUT_OF_DATE_KHR
;
1056 result
= x11_handle_dri3_present_event(chain
, (void *)event
);
1064 x11_swapchain_result(chain
, result
);
1065 wsi_queue_push(&chain
->acquire_queue
, UINT32_MAX
);
1071 x11_image_init(VkDevice device_h
, struct x11_swapchain
*chain
,
1072 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
1073 const VkAllocationCallbacks
* pAllocator
,
1074 const uint64_t *const *modifiers
,
1075 const uint32_t *num_modifiers
,
1076 int num_tranches
, struct x11_image
*image
)
1078 xcb_void_cookie_t cookie
;
1082 if (chain
->base
.use_prime_blit
) {
1083 bool use_modifier
= num_tranches
> 0;
1084 result
= wsi_create_prime_image(&chain
->base
, pCreateInfo
, use_modifier
, &image
->base
);
1086 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
1087 num_tranches
, num_modifiers
, modifiers
,
1093 image
->pixmap
= xcb_generate_id(chain
->conn
);
1095 #ifdef HAVE_DRI3_MODIFIERS
1096 if (image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
) {
1097 /* If the image has a modifier, we must have DRI3 v1.2. */
1098 assert(chain
->has_dri3_modifiers
);
1101 xcb_dri3_pixmap_from_buffers_checked(chain
->conn
,
1104 image
->base
.num_planes
,
1105 pCreateInfo
->imageExtent
.width
,
1106 pCreateInfo
->imageExtent
.height
,
1107 image
->base
.row_pitches
[0],
1108 image
->base
.offsets
[0],
1109 image
->base
.row_pitches
[1],
1110 image
->base
.offsets
[1],
1111 image
->base
.row_pitches
[2],
1112 image
->base
.offsets
[2],
1113 image
->base
.row_pitches
[3],
1114 image
->base
.offsets
[3],
1116 image
->base
.drm_modifier
,
1121 /* Without passing modifiers, we can't have multi-plane RGB images. */
1122 assert(image
->base
.num_planes
== 1);
1125 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
1128 image
->base
.sizes
[0],
1129 pCreateInfo
->imageExtent
.width
,
1130 pCreateInfo
->imageExtent
.height
,
1131 image
->base
.row_pitches
[0],
1133 image
->base
.fds
[0]);
1136 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1138 /* XCB has now taken ownership of the FDs. */
1139 for (int i
= 0; i
< image
->base
.num_planes
; i
++)
1140 image
->base
.fds
[i
] = -1;
1142 int fence_fd
= xshmfence_alloc_shm();
1146 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
1147 if (image
->shm_fence
== NULL
)
1148 goto fail_shmfence_alloc
;
1150 image
->sync_fence
= xcb_generate_id(chain
->conn
);
1151 xcb_dri3_fence_from_fd(chain
->conn
,
1157 image
->busy
= false;
1158 xshmfence_trigger(image
->shm_fence
);
1162 fail_shmfence_alloc
:
1166 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
1167 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1169 wsi_destroy_image(&chain
->base
, &image
->base
);
1175 x11_image_finish(struct x11_swapchain
*chain
,
1176 const VkAllocationCallbacks
* pAllocator
,
1177 struct x11_image
*image
)
1179 xcb_void_cookie_t cookie
;
1181 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
1182 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1183 xshmfence_unmap_shm(image
->shm_fence
);
1185 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
1186 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1188 wsi_destroy_image(&chain
->base
, &image
->base
);
1192 wsi_x11_get_dri3_modifiers(struct wsi_x11_connection
*wsi_conn
,
1193 xcb_connection_t
*conn
, xcb_window_t window
,
1194 uint8_t depth
, uint8_t bpp
,
1195 VkCompositeAlphaFlagsKHR vk_alpha
,
1196 uint64_t **modifiers_in
, uint32_t *num_modifiers_in
,
1197 uint32_t *num_tranches_in
,
1198 const VkAllocationCallbacks
*pAllocator
)
1200 if (!wsi_conn
->has_dri3_modifiers
)
1203 #ifdef HAVE_DRI3_MODIFIERS
1204 xcb_generic_error_t
*error
= NULL
;
1205 xcb_dri3_get_supported_modifiers_cookie_t mod_cookie
=
1206 xcb_dri3_get_supported_modifiers(conn
, window
, depth
, bpp
);
1207 xcb_dri3_get_supported_modifiers_reply_t
*mod_reply
=
1208 xcb_dri3_get_supported_modifiers_reply(conn
, mod_cookie
, &error
);
1211 if (!mod_reply
|| (mod_reply
->num_window_modifiers
== 0 &&
1212 mod_reply
->num_screen_modifiers
== 0)) {
1219 uint64_t *modifiers
[2];
1221 if (mod_reply
->num_window_modifiers
) {
1222 counts
[n
] = mod_reply
->num_window_modifiers
;
1223 modifiers
[n
] = vk_alloc(pAllocator
,
1224 counts
[n
] * sizeof(uint64_t),
1225 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1226 if (!modifiers
[n
]) {
1231 memcpy(modifiers
[n
],
1232 xcb_dri3_get_supported_modifiers_window_modifiers(mod_reply
),
1233 counts
[n
] * sizeof(uint64_t));
1237 if (mod_reply
->num_screen_modifiers
) {
1238 counts
[n
] = mod_reply
->num_screen_modifiers
;
1239 modifiers
[n
] = vk_alloc(pAllocator
,
1240 counts
[n
] * sizeof(uint64_t),
1241 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1242 if (!modifiers
[n
]) {
1244 vk_free(pAllocator
, modifiers
[0]);
1249 memcpy(modifiers
[n
],
1250 xcb_dri3_get_supported_modifiers_screen_modifiers(mod_reply
),
1251 counts
[n
] * sizeof(uint64_t));
1255 for (int i
= 0; i
< n
; i
++) {
1256 modifiers_in
[i
] = modifiers
[i
];
1257 num_modifiers_in
[i
] = counts
[i
];
1259 *num_tranches_in
= n
;
1265 *num_tranches_in
= 0;
1269 x11_swapchain_destroy(struct wsi_swapchain
*anv_chain
,
1270 const VkAllocationCallbacks
*pAllocator
)
1272 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
1273 xcb_void_cookie_t cookie
;
1275 if (chain
->threaded
) {
1276 chain
->status
= VK_ERROR_OUT_OF_DATE_KHR
;
1277 /* Push a UINT32_MAX to wake up the manager */
1278 wsi_queue_push(&chain
->present_queue
, UINT32_MAX
);
1279 pthread_join(chain
->queue_manager
, NULL
);
1280 wsi_queue_destroy(&chain
->acquire_queue
);
1281 wsi_queue_destroy(&chain
->present_queue
);
1284 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++)
1285 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
1287 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
1288 cookie
= xcb_present_select_input_checked(chain
->conn
, chain
->event_id
,
1290 XCB_PRESENT_EVENT_MASK_NO_EVENT
);
1291 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1293 wsi_swapchain_finish(&chain
->base
);
1295 vk_free(pAllocator
, chain
);
1301 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
1303 struct wsi_device
*wsi_device
,
1304 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
1305 const VkAllocationCallbacks
* pAllocator
,
1306 struct wsi_swapchain
**swapchain_out
)
1308 struct x11_swapchain
*chain
;
1309 xcb_void_cookie_t cookie
;
1312 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
1314 const unsigned num_images
= pCreateInfo
->minImageCount
;
1316 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
1317 struct wsi_x11_connection
*wsi_conn
=
1318 wsi_x11_get_connection(wsi_device
, conn
);
1320 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1322 /* Check for whether or not we have a window up-front */
1323 xcb_window_t window
= x11_surface_get_window(icd_surface
);
1324 xcb_get_geometry_reply_t
*geometry
=
1325 xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, window
), NULL
);
1326 if (geometry
== NULL
)
1327 return VK_ERROR_SURFACE_LOST_KHR
;
1328 const uint32_t bit_depth
= geometry
->depth
;
1331 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
1332 chain
= vk_alloc(pAllocator
, size
, 8,
1333 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1335 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1337 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
1338 pCreateInfo
, pAllocator
);
1339 if (result
!= VK_SUCCESS
)
1342 chain
->base
.destroy
= x11_swapchain_destroy
;
1343 chain
->base
.get_wsi_image
= x11_get_wsi_image
;
1344 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
1345 chain
->base
.queue_present
= x11_queue_present
;
1346 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
1347 chain
->base
.image_count
= num_images
;
1349 chain
->window
= window
;
1350 chain
->depth
= bit_depth
;
1351 chain
->extent
= pCreateInfo
->imageExtent
;
1352 chain
->send_sbc
= 0;
1353 chain
->last_present_msc
= 0;
1354 chain
->threaded
= false;
1355 chain
->status
= VK_SUCCESS
;
1356 chain
->has_dri3_modifiers
= wsi_conn
->has_dri3_modifiers
;
1358 /* If we are reallocating from an old swapchain, then we inherit its
1359 * last completion mode, to ensure we don't get into reallocation
1360 * cycles. If we are starting anew, we set 'COPY', as that is the only
1361 * mode which provokes reallocation when anything changes, to make
1362 * sure we have the most optimal allocation.
1364 WSI_FROM_HANDLE(x11_swapchain
, old_chain
, pCreateInfo
->oldSwapchain
);
1366 chain
->last_present_mode
= old_chain
->last_present_mode
;
1368 chain
->last_present_mode
= XCB_PRESENT_COMPLETE_MODE_COPY
;
1370 if (!wsi_x11_check_dri3_compatible(wsi_device
, conn
))
1371 chain
->base
.use_prime_blit
= true;
1373 chain
->event_id
= xcb_generate_id(chain
->conn
);
1374 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
1375 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
1376 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
1377 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
1379 /* Create an XCB event queue to hold present events outside of the usual
1380 * application event queue
1382 chain
->special_event
=
1383 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
1384 chain
->event_id
, NULL
);
1386 chain
->gc
= xcb_generate_id(chain
->conn
);
1388 /* FINISHME: Choose a better error. */
1389 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1393 cookie
= xcb_create_gc(chain
->conn
,
1396 XCB_GC_GRAPHICS_EXPOSURES
,
1397 (uint32_t []) { 0 });
1398 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1400 uint64_t *modifiers
[2] = {NULL
, NULL
};
1401 uint32_t num_modifiers
[2] = {0, 0};
1402 uint32_t num_tranches
= 0;
1403 if (wsi_device
->supports_modifiers
)
1404 wsi_x11_get_dri3_modifiers(wsi_conn
, conn
, window
, chain
->depth
, 32,
1405 pCreateInfo
->compositeAlpha
,
1406 modifiers
, num_modifiers
, &num_tranches
,
1410 for (; image
< chain
->base
.image_count
; image
++) {
1411 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
1412 (const uint64_t *const *)modifiers
,
1413 num_modifiers
, num_tranches
,
1414 &chain
->images
[image
]);
1415 if (result
!= VK_SUCCESS
)
1416 goto fail_init_images
;
1419 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
1420 chain
->threaded
= true;
1422 /* Initialize our queues. We make them base.image_count + 1 because we will
1423 * occasionally use UINT32_MAX to signal the other thread that an error
1424 * has occurred and we don't want an overflow.
1427 ret
= wsi_queue_init(&chain
->acquire_queue
, chain
->base
.image_count
+ 1);
1429 goto fail_init_images
;
1432 ret
= wsi_queue_init(&chain
->present_queue
, chain
->base
.image_count
+ 1);
1434 wsi_queue_destroy(&chain
->acquire_queue
);
1435 goto fail_init_images
;
1438 for (unsigned i
= 0; i
< chain
->base
.image_count
; i
++)
1439 wsi_queue_push(&chain
->acquire_queue
, i
);
1441 ret
= pthread_create(&chain
->queue_manager
, NULL
,
1442 x11_manage_fifo_queues
, chain
);
1444 wsi_queue_destroy(&chain
->present_queue
);
1445 wsi_queue_destroy(&chain
->acquire_queue
);
1446 goto fail_init_images
;
1450 for (int i
= 0; i
< ARRAY_SIZE(modifiers
); i
++)
1451 vk_free(pAllocator
, modifiers
[i
]);
1452 *swapchain_out
= &chain
->base
;
1457 for (uint32_t j
= 0; j
< image
; j
++)
1458 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
1460 for (int i
= 0; i
< ARRAY_SIZE(modifiers
); i
++)
1461 vk_free(pAllocator
, modifiers
[i
]);
1464 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
1466 wsi_swapchain_finish(&chain
->base
);
1469 vk_free(pAllocator
, chain
);
1475 wsi_x11_init_wsi(struct wsi_device
*wsi_device
,
1476 const VkAllocationCallbacks
*alloc
)
1478 struct wsi_x11
*wsi
;
1481 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1482 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1484 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1488 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
1490 if (ret
== ENOMEM
) {
1491 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1493 /* FINISHME: Choose a better error. */
1494 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1500 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1501 _mesa_key_pointer_equal
);
1502 if (!wsi
->connections
) {
1503 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1507 wsi
->base
.get_support
= x11_surface_get_support
;
1508 wsi
->base
.get_capabilities2
= x11_surface_get_capabilities2
;
1509 wsi
->base
.get_formats
= x11_surface_get_formats
;
1510 wsi
->base
.get_formats2
= x11_surface_get_formats2
;
1511 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
1512 wsi
->base
.get_present_rectangles
= x11_surface_get_present_rectangles
;
1513 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
1515 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
1516 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
1521 pthread_mutex_destroy(&wsi
->mutex
);
1523 vk_free(alloc
, wsi
);
1525 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
1526 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
1532 wsi_x11_finish_wsi(struct wsi_device
*wsi_device
,
1533 const VkAllocationCallbacks
*alloc
)
1535 struct wsi_x11
*wsi
=
1536 (struct wsi_x11
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
1539 hash_table_foreach(wsi
->connections
, entry
)
1540 wsi_x11_connection_destroy(wsi_device
, entry
->data
);
1542 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
1544 pthread_mutex_destroy(&wsi
->mutex
);
1546 vk_free(alloc
, wsi
);