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"
38 #include "util/hash_table.h"
40 #include "wsi_common.h"
41 #include "wsi_common_x11.h"
42 #include "wsi_common_queue.h"
44 #define typed_memcpy(dest, src, count) ({ \
45 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
46 memcpy((dest), (src), (count) * sizeof(*(src))); \
49 struct wsi_x11_connection
{
55 struct wsi_interface base
;
57 pthread_mutex_t mutex
;
58 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
59 struct hash_table
*connections
;
62 static struct wsi_x11_connection
*
63 wsi_x11_connection_create(const VkAllocationCallbacks
*alloc
,
64 xcb_connection_t
*conn
)
66 xcb_query_extension_cookie_t dri3_cookie
, pres_cookie
;
67 xcb_query_extension_reply_t
*dri3_reply
, *pres_reply
;
69 struct wsi_x11_connection
*wsi_conn
=
70 vk_alloc(alloc
, sizeof(*wsi_conn
), 8,
71 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
75 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
76 pres_cookie
= xcb_query_extension(conn
, 7, "PRESENT");
78 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
79 pres_reply
= xcb_query_extension_reply(conn
, pres_cookie
, NULL
);
80 if (dri3_reply
== NULL
|| pres_reply
== NULL
) {
83 vk_free(alloc
, wsi_conn
);
87 wsi_conn
->has_dri3
= dri3_reply
->present
!= 0;
88 wsi_conn
->has_present
= pres_reply
->present
!= 0;
97 wsi_x11_connection_destroy(const VkAllocationCallbacks
*alloc
,
98 struct wsi_x11_connection
*conn
)
100 vk_free(alloc
, conn
);
103 static struct wsi_x11_connection
*
104 wsi_x11_get_connection(struct wsi_device
*wsi_dev
,
105 const VkAllocationCallbacks
*alloc
,
106 xcb_connection_t
*conn
)
108 struct wsi_x11
*wsi
=
109 (struct wsi_x11
*)wsi_dev
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
111 pthread_mutex_lock(&wsi
->mutex
);
113 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
115 /* We're about to make a bunch of blocking calls. Let's drop the
116 * mutex for now so we don't block up too badly.
118 pthread_mutex_unlock(&wsi
->mutex
);
120 struct wsi_x11_connection
*wsi_conn
=
121 wsi_x11_connection_create(alloc
, conn
);
125 pthread_mutex_lock(&wsi
->mutex
);
127 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
129 /* Oops, someone raced us to it */
130 wsi_x11_connection_destroy(alloc
, wsi_conn
);
132 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
136 pthread_mutex_unlock(&wsi
->mutex
);
141 static const VkSurfaceFormatKHR formats
[] = {
142 { .format
= VK_FORMAT_B8G8R8A8_SRGB
, },
143 { .format
= VK_FORMAT_B8G8R8A8_UNORM
, },
146 static const VkPresentModeKHR present_modes
[] = {
147 VK_PRESENT_MODE_IMMEDIATE_KHR
,
148 VK_PRESENT_MODE_MAILBOX_KHR
,
149 VK_PRESENT_MODE_FIFO_KHR
,
152 static xcb_screen_t
*
153 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
155 xcb_screen_iterator_t screen_iter
=
156 xcb_setup_roots_iterator(xcb_get_setup(conn
));
158 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
159 if (screen_iter
.data
->root
== root
)
160 return screen_iter
.data
;
166 static xcb_visualtype_t
*
167 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
170 xcb_depth_iterator_t depth_iter
=
171 xcb_screen_allowed_depths_iterator(screen
);
173 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
174 xcb_visualtype_iterator_t visual_iter
=
175 xcb_depth_visuals_iterator (depth_iter
.data
);
177 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
178 if (visual_iter
.data
->visual_id
== visual_id
) {
180 *depth
= depth_iter
.data
->depth
;
181 return visual_iter
.data
;
189 static xcb_visualtype_t
*
190 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
193 xcb_screen_iterator_t screen_iter
=
194 xcb_setup_roots_iterator(xcb_get_setup(conn
));
196 /* For this we have to iterate over all of the screens which is rather
197 * annoying. Fortunately, there is probably only 1.
199 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
200 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
209 static xcb_visualtype_t
*
210 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
213 xcb_query_tree_cookie_t tree_cookie
;
214 xcb_get_window_attributes_cookie_t attrib_cookie
;
215 xcb_query_tree_reply_t
*tree
;
216 xcb_get_window_attributes_reply_t
*attrib
;
218 tree_cookie
= xcb_query_tree(conn
, window
);
219 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
221 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
222 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
223 if (attrib
== NULL
|| tree
== NULL
) {
229 xcb_window_t root
= tree
->root
;
230 xcb_visualid_t visual_id
= attrib
->visual
;
234 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
238 return screen_get_visualtype(screen
, visual_id
, depth
);
242 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
244 uint32_t rgb_mask
= visual
->red_mask
|
248 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
250 /* Do we have bits left over after RGB? */
251 return (all_mask
& ~rgb_mask
) != 0;
254 VkBool32
wsi_get_physical_device_xcb_presentation_support(
255 struct wsi_device
*wsi_device
,
256 VkAllocationCallbacks
*alloc
,
257 uint32_t queueFamilyIndex
,
258 xcb_connection_t
* connection
,
259 xcb_visualid_t visual_id
)
261 struct wsi_x11_connection
*wsi_conn
=
262 wsi_x11_get_connection(wsi_device
, alloc
, connection
);
264 if (!wsi_conn
->has_dri3
) {
265 fprintf(stderr
, "vulkan: No DRI3 support\n");
269 unsigned visual_depth
;
270 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
273 if (visual_depth
!= 24 && visual_depth
!= 32)
279 static xcb_connection_t
*
280 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
282 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
283 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
285 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
289 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
291 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
292 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
294 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
298 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
299 struct wsi_device
*wsi_device
,
300 const VkAllocationCallbacks
*alloc
,
301 uint32_t queueFamilyIndex
,
302 VkBool32
* pSupported
)
304 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
305 xcb_window_t window
= x11_surface_get_window(icd_surface
);
307 struct wsi_x11_connection
*wsi_conn
=
308 wsi_x11_get_connection(wsi_device
, alloc
, conn
);
310 return VK_ERROR_OUT_OF_HOST_MEMORY
;
312 if (!wsi_conn
->has_dri3
) {
313 fprintf(stderr
, "vulkan: No DRI3 support\n");
318 unsigned visual_depth
;
319 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
324 if (visual_depth
!= 24 && visual_depth
!= 32) {
334 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
335 VkSurfaceCapabilitiesKHR
*caps
)
337 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
338 xcb_window_t window
= x11_surface_get_window(icd_surface
);
339 xcb_get_geometry_cookie_t geom_cookie
;
340 xcb_generic_error_t
*err
;
341 xcb_get_geometry_reply_t
*geom
;
342 unsigned visual_depth
;
344 geom_cookie
= xcb_get_geometry(conn
, window
);
346 /* This does a round-trip. This is why we do get_geometry first and
347 * wait to read the reply until after we have a visual.
349 xcb_visualtype_t
*visual
=
350 get_visualtype_for_window(conn
, window
, &visual_depth
);
353 return VK_ERROR_SURFACE_LOST_KHR
;
355 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
357 VkExtent2D extent
= { geom
->width
, geom
->height
};
358 caps
->currentExtent
= extent
;
359 caps
->minImageExtent
= extent
;
360 caps
->maxImageExtent
= extent
;
362 /* This can happen if the client didn't wait for the configure event
363 * to come back from the compositor. In that case, we don't know the
364 * size of the window so we just return valid "I don't know" stuff.
366 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
367 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
368 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
373 if (visual_has_alpha(visual
, visual_depth
)) {
374 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
375 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
377 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
378 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
381 /* For true mailbox mode, we need at least 4 images:
382 * 1) One to scan out from
383 * 2) One to have queued for scan-out
384 * 3) One to be currently held by the X server
385 * 4) One to render to
387 caps
->minImageCount
= 2;
388 /* There is no real maximum */
389 caps
->maxImageCount
= 0;
391 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
392 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
393 caps
->maxImageArrayLayers
= 1;
394 caps
->supportedUsageFlags
=
395 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
396 VK_IMAGE_USAGE_SAMPLED_BIT
|
397 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
398 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
404 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
405 struct wsi_device
*wsi_device
,
406 uint32_t *pSurfaceFormatCount
,
407 VkSurfaceFormatKHR
*pSurfaceFormats
)
409 if (pSurfaceFormats
== NULL
) {
410 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
414 *pSurfaceFormatCount
= MIN2(*pSurfaceFormatCount
, ARRAY_SIZE(formats
));
415 typed_memcpy(pSurfaceFormats
, formats
, *pSurfaceFormatCount
);
417 return *pSurfaceFormatCount
< ARRAY_SIZE(formats
) ?
418 VK_INCOMPLETE
: VK_SUCCESS
;
422 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
423 uint32_t *pPresentModeCount
,
424 VkPresentModeKHR
*pPresentModes
)
426 if (pPresentModes
== NULL
) {
427 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
431 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
432 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
434 return *pPresentModeCount
< ARRAY_SIZE(present_modes
) ?
435 VK_INCOMPLETE
: VK_SUCCESS
;
438 VkResult
wsi_create_xcb_surface(const VkAllocationCallbacks
*pAllocator
,
439 const VkXcbSurfaceCreateInfoKHR
*pCreateInfo
,
440 VkSurfaceKHR
*pSurface
)
442 VkIcdSurfaceXcb
*surface
;
444 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
445 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
447 return VK_ERROR_OUT_OF_HOST_MEMORY
;
449 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
450 surface
->connection
= pCreateInfo
->connection
;
451 surface
->window
= pCreateInfo
->window
;
453 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
457 VkResult
wsi_create_xlib_surface(const VkAllocationCallbacks
*pAllocator
,
458 const VkXlibSurfaceCreateInfoKHR
*pCreateInfo
,
459 VkSurfaceKHR
*pSurface
)
461 VkIcdSurfaceXlib
*surface
;
463 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
464 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
466 return VK_ERROR_OUT_OF_HOST_MEMORY
;
468 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
469 surface
->dpy
= pCreateInfo
->dpy
;
470 surface
->window
= pCreateInfo
->window
;
472 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
478 VkDeviceMemory memory
;
481 struct xshmfence
* shm_fence
;
485 struct x11_swapchain
{
486 struct wsi_swapchain base
;
488 xcb_connection_t
* conn
;
493 uint32_t image_count
;
495 xcb_present_event_t event_id
;
496 xcb_special_event_t
* special_event
;
498 uint64_t last_present_msc
;
503 struct wsi_queue present_queue
;
504 struct wsi_queue acquire_queue
;
505 pthread_t queue_manager
;
507 struct x11_image images
[0];
511 x11_get_images(struct wsi_swapchain
*anv_chain
,
512 uint32_t* pCount
, VkImage
*pSwapchainImages
)
514 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
518 if (pSwapchainImages
== NULL
) {
519 *pCount
= chain
->image_count
;
524 ret_count
= chain
->image_count
;
525 if (chain
->image_count
> *pCount
) {
527 result
= VK_INCOMPLETE
;
530 for (uint32_t i
= 0; i
< ret_count
; i
++)
531 pSwapchainImages
[i
] = chain
->images
[i
].image
;
537 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
538 xcb_present_generic_event_t
*event
)
540 switch (event
->evtype
) {
541 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
542 xcb_present_configure_notify_event_t
*config
= (void *) event
;
544 if (config
->width
!= chain
->extent
.width
||
545 config
->height
!= chain
->extent
.height
)
546 return VK_ERROR_OUT_OF_DATE_KHR
;
551 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
552 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
554 for (unsigned i
= 0; i
< chain
->image_count
; i
++) {
555 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
556 chain
->images
[i
].busy
= false;
558 wsi_queue_push(&chain
->acquire_queue
, i
);
566 case XCB_PRESENT_EVENT_COMPLETE_NOTIFY
: {
567 xcb_present_complete_notify_event_t
*complete
= (void *) event
;
568 if (complete
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
)
569 chain
->last_present_msc
= complete
->msc
;
581 static uint64_t wsi_get_current_time(void)
583 uint64_t current_time
;
586 clock_gettime(CLOCK_MONOTONIC
, &tv
);
587 current_time
= tv
.tv_nsec
+ tv
.tv_sec
*1000000000ull;
591 static uint64_t wsi_get_absolute_timeout(uint64_t timeout
)
593 uint64_t current_time
= wsi_get_current_time();
595 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
597 return current_time
+ timeout
;
601 x11_acquire_next_image_poll_x11(struct x11_swapchain
*chain
,
602 uint32_t *image_index
, uint64_t timeout
)
604 xcb_generic_event_t
*event
;
608 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
609 if (!chain
->images
[i
].busy
) {
610 /* We found a non-busy image */
611 xshmfence_await(chain
->images
[i
].shm_fence
);
613 chain
->images
[i
].busy
= true;
618 xcb_flush(chain
->conn
);
620 if (timeout
== UINT64_MAX
) {
621 event
= xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
623 return VK_ERROR_OUT_OF_DATE_KHR
;
625 event
= xcb_poll_for_special_event(chain
->conn
, chain
->special_event
);
631 atimeout
= wsi_get_absolute_timeout(timeout
);
633 pfds
.fd
= xcb_get_file_descriptor(chain
->conn
);
634 pfds
.events
= POLLIN
;
635 ret
= poll(&pfds
, 1, timeout
/ 1000 / 1000);
639 return VK_ERROR_OUT_OF_DATE_KHR
;
641 /* If a non-special event happens, the fd will still
642 * poll. So recalculate the timeout now just in case.
644 uint64_t current_time
= wsi_get_current_time();
645 if (atimeout
> current_time
)
646 timeout
= atimeout
- current_time
;
653 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
655 if (result
!= VK_SUCCESS
)
661 x11_acquire_next_image_from_queue(struct x11_swapchain
*chain
,
662 uint32_t *image_index_out
, uint64_t timeout
)
664 assert(chain
->threaded
);
666 uint32_t image_index
;
667 VkResult result
= wsi_queue_pull(&chain
->acquire_queue
,
668 &image_index
, timeout
);
669 if (result
!= VK_SUCCESS
) {
671 } else if (chain
->status
!= VK_SUCCESS
) {
672 return chain
->status
;
675 assert(image_index
< chain
->image_count
);
676 xshmfence_await(chain
->images
[image_index
].shm_fence
);
678 *image_index_out
= image_index
;
684 x11_present_to_x11(struct x11_swapchain
*chain
, uint32_t image_index
,
687 struct x11_image
*image
= &chain
->images
[image_index
];
689 assert(image_index
< chain
->image_count
);
691 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
694 int64_t remainder
= 0;
696 if (chain
->base
.present_mode
== VK_PRESENT_MODE_IMMEDIATE_KHR
)
697 options
|= XCB_PRESENT_OPTION_ASYNC
;
699 xshmfence_reset(image
->shm_fence
);
702 xcb_void_cookie_t cookie
=
703 xcb_present_pixmap(chain
->conn
,
706 (uint32_t) chain
->send_sbc
,
711 XCB_NONE
, /* target_crtc */
718 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
721 xcb_flush(chain
->conn
);
727 x11_acquire_next_image(struct wsi_swapchain
*anv_chain
,
729 VkSemaphore semaphore
,
730 uint32_t *image_index
)
732 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
734 if (chain
->threaded
) {
735 return x11_acquire_next_image_from_queue(chain
, image_index
, timeout
);
737 return x11_acquire_next_image_poll_x11(chain
, image_index
, timeout
);
742 x11_queue_present(struct wsi_swapchain
*anv_chain
,
743 uint32_t image_index
)
745 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
747 if (chain
->threaded
) {
748 wsi_queue_push(&chain
->present_queue
, image_index
);
749 return chain
->status
;
751 return x11_present_to_x11(chain
, image_index
, 0);
756 x11_manage_fifo_queues(void *state
)
758 struct x11_swapchain
*chain
= state
;
761 assert(chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
);
763 while (chain
->status
== VK_SUCCESS
) {
764 /* It should be safe to unconditionally block here. Later in the loop
765 * we blocks until the previous present has landed on-screen. At that
766 * point, we should have received IDLE_NOTIFY on all images presented
767 * before that point so the client should be able to acquire any image
768 * other than the currently presented one.
770 uint32_t image_index
;
771 result
= wsi_queue_pull(&chain
->present_queue
, &image_index
, INT64_MAX
);
772 if (result
!= VK_SUCCESS
) {
774 } else if (chain
->status
!= VK_SUCCESS
) {
778 uint64_t target_msc
= chain
->last_present_msc
+ 1;
779 result
= x11_present_to_x11(chain
, image_index
, target_msc
);
780 if (result
!= VK_SUCCESS
)
783 while (chain
->last_present_msc
< target_msc
) {
784 xcb_generic_event_t
*event
=
785 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
789 result
= x11_handle_dri3_present_event(chain
, (void *)event
);
790 if (result
!= VK_SUCCESS
)
796 chain
->status
= result
;
797 wsi_queue_push(&chain
->acquire_queue
, UINT32_MAX
);
803 x11_image_init(VkDevice device_h
, struct x11_swapchain
*chain
,
804 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
805 const VkAllocationCallbacks
* pAllocator
,
806 struct x11_image
*image
)
808 xcb_void_cookie_t cookie
;
816 result
= chain
->base
.image_fns
->create_wsi_image(device_h
,
825 if (result
!= VK_SUCCESS
)
828 image
->pixmap
= xcb_generate_id(chain
->conn
);
831 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
835 pCreateInfo
->imageExtent
.width
,
836 pCreateInfo
->imageExtent
.height
,
838 chain
->depth
, bpp
, fd
);
839 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
841 int fence_fd
= xshmfence_alloc_shm();
845 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
846 if (image
->shm_fence
== NULL
)
847 goto fail_shmfence_alloc
;
849 image
->sync_fence
= xcb_generate_id(chain
->conn
);
850 xcb_dri3_fence_from_fd(chain
->conn
,
857 xshmfence_trigger(image
->shm_fence
);
865 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
866 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
868 chain
->base
.image_fns
->free_wsi_image(device_h
, pAllocator
,
869 image
->image
, image
->memory
);
875 x11_image_finish(struct x11_swapchain
*chain
,
876 const VkAllocationCallbacks
* pAllocator
,
877 struct x11_image
*image
)
879 xcb_void_cookie_t cookie
;
881 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
882 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
883 xshmfence_unmap_shm(image
->shm_fence
);
885 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
886 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
888 chain
->base
.image_fns
->free_wsi_image(chain
->base
.device
, pAllocator
,
889 image
->image
, image
->memory
);
893 x11_swapchain_destroy(struct wsi_swapchain
*anv_chain
,
894 const VkAllocationCallbacks
*pAllocator
)
896 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
897 xcb_void_cookie_t cookie
;
899 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
900 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
902 if (chain
->threaded
) {
903 chain
->status
= VK_ERROR_OUT_OF_DATE_KHR
;
904 /* Push a UINT32_MAX to wake up the manager */
905 wsi_queue_push(&chain
->present_queue
, UINT32_MAX
);
906 pthread_join(chain
->queue_manager
, NULL
);
907 wsi_queue_destroy(&chain
->acquire_queue
);
908 wsi_queue_destroy(&chain
->present_queue
);
911 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
912 cookie
= xcb_present_select_input_checked(chain
->conn
, chain
->event_id
,
914 XCB_PRESENT_EVENT_MASK_NO_EVENT
);
915 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
917 vk_free(pAllocator
, chain
);
923 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
925 struct wsi_device
*wsi_device
,
926 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
927 const VkAllocationCallbacks
* pAllocator
,
928 const struct wsi_image_fns
*image_fns
,
929 struct wsi_swapchain
**swapchain_out
)
931 struct x11_swapchain
*chain
;
932 xcb_void_cookie_t cookie
;
935 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
937 const unsigned num_images
= pCreateInfo
->minImageCount
;
939 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
940 chain
= vk_alloc(pAllocator
, size
, 8,
941 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
943 return VK_ERROR_OUT_OF_HOST_MEMORY
;
945 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
946 xcb_window_t window
= x11_surface_get_window(icd_surface
);
947 xcb_get_geometry_reply_t
*geometry
=
948 xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, window
), NULL
);
950 if (geometry
== NULL
)
951 return VK_ERROR_SURFACE_LOST_KHR
;
953 chain
->base
.device
= device
;
954 chain
->base
.destroy
= x11_swapchain_destroy
;
955 chain
->base
.get_images
= x11_get_images
;
956 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
957 chain
->base
.queue_present
= x11_queue_present
;
958 chain
->base
.image_fns
= image_fns
;
959 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
961 chain
->window
= window
;
962 chain
->depth
= geometry
->depth
;
963 chain
->extent
= pCreateInfo
->imageExtent
;
964 chain
->image_count
= num_images
;
966 chain
->last_present_msc
= 0;
967 chain
->threaded
= false;
968 chain
->status
= VK_SUCCESS
;
972 chain
->event_id
= xcb_generate_id(chain
->conn
);
973 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
974 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
975 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
976 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
978 /* Create an XCB event queue to hold present events outside of the usual
979 * application event queue
981 chain
->special_event
=
982 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
983 chain
->event_id
, NULL
);
985 chain
->gc
= xcb_generate_id(chain
->conn
);
987 /* FINISHME: Choose a better error. */
988 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
992 cookie
= xcb_create_gc(chain
->conn
,
995 XCB_GC_GRAPHICS_EXPOSURES
,
996 (uint32_t []) { 0 });
997 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
1000 for (; image
< chain
->image_count
; image
++) {
1001 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
1002 &chain
->images
[image
]);
1003 if (result
!= VK_SUCCESS
)
1004 goto fail_init_images
;
1007 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
1008 chain
->threaded
= true;
1010 /* Initialize our queues. We make them image_count + 1 because we will
1011 * occasionally use UINT32_MAX to signal the other thread that an error
1012 * has occurred and we don't want an overflow.
1015 ret
= wsi_queue_init(&chain
->acquire_queue
, chain
->image_count
+ 1);
1017 goto fail_init_images
;
1020 ret
= wsi_queue_init(&chain
->present_queue
, chain
->image_count
+ 1);
1022 wsi_queue_destroy(&chain
->acquire_queue
);
1023 goto fail_init_images
;
1026 for (unsigned i
= 0; i
< chain
->image_count
; i
++)
1027 wsi_queue_push(&chain
->acquire_queue
, i
);
1029 ret
= pthread_create(&chain
->queue_manager
, NULL
,
1030 x11_manage_fifo_queues
, chain
);
1032 wsi_queue_destroy(&chain
->present_queue
);
1033 wsi_queue_destroy(&chain
->acquire_queue
);
1034 goto fail_init_images
;
1038 *swapchain_out
= &chain
->base
;
1043 for (uint32_t j
= 0; j
< image
; j
++)
1044 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
1047 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
1049 vk_free(pAllocator
, chain
);
1055 wsi_x11_init_wsi(struct wsi_device
*wsi_device
,
1056 const VkAllocationCallbacks
*alloc
)
1058 struct wsi_x11
*wsi
;
1061 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1062 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1064 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1068 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
1070 if (ret
== ENOMEM
) {
1071 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1073 /* FINISHME: Choose a better error. */
1074 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1080 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1081 _mesa_key_pointer_equal
);
1082 if (!wsi
->connections
) {
1083 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1087 wsi
->base
.get_support
= x11_surface_get_support
;
1088 wsi
->base
.get_capabilities
= x11_surface_get_capabilities
;
1089 wsi
->base
.get_formats
= x11_surface_get_formats
;
1090 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
1091 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
1093 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
1094 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
1099 pthread_mutex_destroy(&wsi
->mutex
);
1101 vk_free(alloc
, wsi
);
1103 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
1104 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
1110 wsi_x11_finish_wsi(struct wsi_device
*wsi_device
,
1111 const VkAllocationCallbacks
*alloc
)
1113 struct wsi_x11
*wsi
=
1114 (struct wsi_x11
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
1117 struct hash_entry
*entry
;
1118 hash_table_foreach(wsi
->connections
, entry
)
1119 wsi_x11_connection_destroy(alloc
, entry
->data
);
1121 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
1123 pthread_mutex_destroy(&wsi
->mutex
);
1125 vk_free(alloc
, wsi
);