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"
43 #define typed_memcpy(dest, src, count) ({ \
44 static_assert(sizeof(*src) == sizeof(*dest), ""); \
45 memcpy((dest), (src), (count) * sizeof(*(src))); \
48 struct wsi_x11_connection
{
54 struct wsi_interface base
;
56 pthread_mutex_t mutex
;
57 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
58 struct hash_table
*connections
;
61 static struct wsi_x11_connection
*
62 wsi_x11_connection_create(const VkAllocationCallbacks
*alloc
,
63 xcb_connection_t
*conn
)
65 xcb_query_extension_cookie_t dri3_cookie
, pres_cookie
;
66 xcb_query_extension_reply_t
*dri3_reply
, *pres_reply
;
68 struct wsi_x11_connection
*wsi_conn
=
69 vk_alloc(alloc
, sizeof(*wsi_conn
), 8,
70 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
74 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
75 pres_cookie
= xcb_query_extension(conn
, 7, "PRESENT");
77 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
78 pres_reply
= xcb_query_extension_reply(conn
, pres_cookie
, NULL
);
79 if (dri3_reply
== NULL
|| pres_reply
== NULL
) {
82 vk_free(alloc
, wsi_conn
);
86 wsi_conn
->has_dri3
= dri3_reply
->present
!= 0;
87 wsi_conn
->has_present
= pres_reply
->present
!= 0;
96 wsi_x11_connection_destroy(const VkAllocationCallbacks
*alloc
,
97 struct wsi_x11_connection
*conn
)
102 static struct wsi_x11_connection
*
103 wsi_x11_get_connection(struct wsi_device
*wsi_dev
,
104 const VkAllocationCallbacks
*alloc
,
105 xcb_connection_t
*conn
)
107 struct wsi_x11
*wsi
=
108 (struct wsi_x11
*)wsi_dev
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
110 pthread_mutex_lock(&wsi
->mutex
);
112 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
114 /* We're about to make a bunch of blocking calls. Let's drop the
115 * mutex for now so we don't block up too badly.
117 pthread_mutex_unlock(&wsi
->mutex
);
119 struct wsi_x11_connection
*wsi_conn
=
120 wsi_x11_connection_create(alloc
, conn
);
122 pthread_mutex_lock(&wsi
->mutex
);
124 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
126 /* Oops, someone raced us to it */
127 wsi_x11_connection_destroy(alloc
, wsi_conn
);
129 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
133 pthread_mutex_unlock(&wsi
->mutex
);
138 static const VkSurfaceFormatKHR formats
[] = {
139 { .format
= VK_FORMAT_B8G8R8A8_SRGB
, },
140 { .format
= VK_FORMAT_B8G8R8A8_UNORM
, },
143 static const VkPresentModeKHR present_modes
[] = {
144 VK_PRESENT_MODE_IMMEDIATE_KHR
,
145 VK_PRESENT_MODE_MAILBOX_KHR
,
148 static xcb_screen_t
*
149 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
151 xcb_screen_iterator_t screen_iter
=
152 xcb_setup_roots_iterator(xcb_get_setup(conn
));
154 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
155 if (screen_iter
.data
->root
== root
)
156 return screen_iter
.data
;
162 static xcb_visualtype_t
*
163 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
166 xcb_depth_iterator_t depth_iter
=
167 xcb_screen_allowed_depths_iterator(screen
);
169 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
170 xcb_visualtype_iterator_t visual_iter
=
171 xcb_depth_visuals_iterator (depth_iter
.data
);
173 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
174 if (visual_iter
.data
->visual_id
== visual_id
) {
176 *depth
= depth_iter
.data
->depth
;
177 return visual_iter
.data
;
185 static xcb_visualtype_t
*
186 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
189 xcb_screen_iterator_t screen_iter
=
190 xcb_setup_roots_iterator(xcb_get_setup(conn
));
192 /* For this we have to iterate over all of the screens which is rather
193 * annoying. Fortunately, there is probably only 1.
195 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
196 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
205 static xcb_visualtype_t
*
206 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
209 xcb_query_tree_cookie_t tree_cookie
;
210 xcb_get_window_attributes_cookie_t attrib_cookie
;
211 xcb_query_tree_reply_t
*tree
;
212 xcb_get_window_attributes_reply_t
*attrib
;
214 tree_cookie
= xcb_query_tree(conn
, window
);
215 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
217 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
218 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
219 if (attrib
== NULL
|| tree
== NULL
) {
225 xcb_window_t root
= tree
->root
;
226 xcb_visualid_t visual_id
= attrib
->visual
;
230 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
234 return screen_get_visualtype(screen
, visual_id
, depth
);
238 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
240 uint32_t rgb_mask
= visual
->red_mask
|
244 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
246 /* Do we have bits left over after RGB? */
247 return (all_mask
& ~rgb_mask
) != 0;
250 VkBool32
wsi_get_physical_device_xcb_presentation_support(
251 struct wsi_device
*wsi_device
,
252 VkAllocationCallbacks
*alloc
,
253 uint32_t queueFamilyIndex
,
254 xcb_connection_t
* connection
,
255 xcb_visualid_t visual_id
)
257 struct wsi_x11_connection
*wsi_conn
=
258 wsi_x11_get_connection(wsi_device
, alloc
, connection
);
260 if (!wsi_conn
->has_dri3
) {
261 fprintf(stderr
, "vulkan: No DRI3 support\n");
265 unsigned visual_depth
;
266 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
269 if (visual_depth
!= 24 && visual_depth
!= 32)
275 static xcb_connection_t
*
276 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
278 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
279 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
281 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
285 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
287 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
288 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
290 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
294 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
295 struct wsi_device
*wsi_device
,
296 const VkAllocationCallbacks
*alloc
,
297 uint32_t queueFamilyIndex
,
298 VkBool32
* pSupported
)
300 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
301 xcb_window_t window
= x11_surface_get_window(icd_surface
);
303 struct wsi_x11_connection
*wsi_conn
=
304 wsi_x11_get_connection(wsi_device
, alloc
, conn
);
306 return VK_ERROR_OUT_OF_HOST_MEMORY
;
308 if (!wsi_conn
->has_dri3
) {
309 fprintf(stderr
, "vulkan: No DRI3 support\n");
314 unsigned visual_depth
;
315 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
320 if (visual_depth
!= 24 && visual_depth
!= 32) {
330 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
331 VkSurfaceCapabilitiesKHR
*caps
)
333 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
334 xcb_window_t window
= x11_surface_get_window(icd_surface
);
335 xcb_get_geometry_cookie_t geom_cookie
;
336 xcb_generic_error_t
*err
;
337 xcb_get_geometry_reply_t
*geom
;
338 unsigned visual_depth
;
340 geom_cookie
= xcb_get_geometry(conn
, window
);
342 /* This does a round-trip. This is why we do get_geometry first and
343 * wait to read the reply until after we have a visual.
345 xcb_visualtype_t
*visual
=
346 get_visualtype_for_window(conn
, window
, &visual_depth
);
348 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
350 VkExtent2D extent
= { geom
->width
, geom
->height
};
351 caps
->currentExtent
= extent
;
352 caps
->minImageExtent
= extent
;
353 caps
->maxImageExtent
= extent
;
355 /* This can happen if the client didn't wait for the configure event
356 * to come back from the compositor. In that case, we don't know the
357 * size of the window so we just return valid "I don't know" stuff.
359 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
360 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
361 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
366 if (visual_has_alpha(visual
, visual_depth
)) {
367 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
368 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
370 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
371 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
374 caps
->minImageCount
= 2;
375 caps
->maxImageCount
= 4;
376 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
377 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
378 caps
->maxImageArrayLayers
= 1;
379 caps
->supportedUsageFlags
=
380 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
381 VK_IMAGE_USAGE_SAMPLED_BIT
|
382 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
383 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
389 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
390 struct wsi_device
*wsi_device
,
391 uint32_t *pSurfaceFormatCount
,
392 VkSurfaceFormatKHR
*pSurfaceFormats
)
394 if (pSurfaceFormats
== NULL
) {
395 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
399 *pSurfaceFormatCount
= MIN2(*pSurfaceFormatCount
, ARRAY_SIZE(formats
));
400 typed_memcpy(pSurfaceFormats
, formats
, *pSurfaceFormatCount
);
402 return *pSurfaceFormatCount
< ARRAY_SIZE(formats
) ?
403 VK_INCOMPLETE
: VK_SUCCESS
;
407 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
408 uint32_t *pPresentModeCount
,
409 VkPresentModeKHR
*pPresentModes
)
411 if (pPresentModes
== NULL
) {
412 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
416 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
417 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
419 return *pPresentModeCount
< ARRAY_SIZE(present_modes
) ?
420 VK_INCOMPLETE
: VK_SUCCESS
;
423 VkResult
wsi_create_xcb_surface(const VkAllocationCallbacks
*pAllocator
,
424 const VkXcbSurfaceCreateInfoKHR
*pCreateInfo
,
425 VkSurfaceKHR
*pSurface
)
427 VkIcdSurfaceXcb
*surface
;
429 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
430 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
432 return VK_ERROR_OUT_OF_HOST_MEMORY
;
434 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
435 surface
->connection
= pCreateInfo
->connection
;
436 surface
->window
= pCreateInfo
->window
;
438 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
442 VkResult
wsi_create_xlib_surface(const VkAllocationCallbacks
*pAllocator
,
443 const VkXlibSurfaceCreateInfoKHR
*pCreateInfo
,
444 VkSurfaceKHR
*pSurface
)
446 VkIcdSurfaceXlib
*surface
;
448 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
449 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
451 return VK_ERROR_OUT_OF_HOST_MEMORY
;
453 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
454 surface
->dpy
= pCreateInfo
->dpy
;
455 surface
->window
= pCreateInfo
->window
;
457 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
463 VkDeviceMemory memory
;
466 struct xshmfence
* shm_fence
;
470 struct x11_swapchain
{
471 struct wsi_swapchain base
;
473 xcb_connection_t
* conn
;
478 uint32_t image_count
;
480 xcb_present_event_t event_id
;
481 xcb_special_event_t
* special_event
;
485 struct x11_image images
[0];
489 x11_get_images(struct wsi_swapchain
*anv_chain
,
490 uint32_t* pCount
, VkImage
*pSwapchainImages
)
492 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
494 if (pSwapchainImages
== NULL
) {
495 *pCount
= chain
->image_count
;
499 assert(chain
->image_count
<= *pCount
);
500 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
501 pSwapchainImages
[i
] = chain
->images
[i
].image
;
503 *pCount
= chain
->image_count
;
509 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
510 xcb_present_generic_event_t
*event
)
512 switch (event
->evtype
) {
513 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
514 xcb_present_configure_notify_event_t
*config
= (void *) event
;
516 if (config
->width
!= chain
->extent
.width
||
517 config
->height
!= chain
->extent
.height
)
518 return VK_ERROR_OUT_OF_DATE_KHR
;
523 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
524 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
526 for (unsigned i
= 0; i
< chain
->image_count
; i
++) {
527 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
528 chain
->images
[i
].busy
= false;
536 case XCB_PRESENT_COMPLETE_NOTIFY
:
545 static uint64_t wsi_get_current_time(void)
547 uint64_t current_time
;
550 clock_gettime(CLOCK_MONOTONIC
, &tv
);
551 current_time
= tv
.tv_nsec
+ tv
.tv_sec
*1000000000ull;
555 static uint64_t wsi_get_absolute_timeout(uint64_t timeout
)
557 uint64_t current_time
= wsi_get_current_time();
559 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
561 return current_time
+ timeout
;
565 x11_acquire_next_image(struct wsi_swapchain
*anv_chain
,
567 VkSemaphore semaphore
,
568 uint32_t *image_index
)
570 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
571 xcb_generic_event_t
*event
;
575 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
576 if (!chain
->images
[i
].busy
) {
577 /* We found a non-busy image */
578 xshmfence_await(chain
->images
[i
].shm_fence
);
580 chain
->images
[i
].busy
= true;
585 xcb_flush(chain
->conn
);
587 if (timeout
== UINT64_MAX
) {
588 event
= xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
590 return VK_ERROR_OUT_OF_DATE_KHR
;
592 event
= xcb_poll_for_special_event(chain
->conn
, chain
->special_event
);
598 atimeout
= wsi_get_absolute_timeout(timeout
);
600 pfds
.fd
= xcb_get_file_descriptor(chain
->conn
);
601 pfds
.events
= POLLIN
;
602 ret
= poll(&pfds
, 1, timeout
/ 1000 / 1000);
606 return VK_ERROR_OUT_OF_DATE_KHR
;
608 /* If a non-special event happens, the fd will still
609 * poll. So recalculate the timeout now just in case.
611 uint64_t current_time
= wsi_get_current_time();
612 if (atimeout
> current_time
)
613 timeout
= atimeout
- current_time
;
620 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
622 if (result
!= VK_SUCCESS
)
628 x11_queue_present(struct wsi_swapchain
*anv_chain
,
629 uint32_t image_index
)
631 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
632 struct x11_image
*image
= &chain
->images
[image_index
];
634 assert(image_index
< chain
->image_count
);
636 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
638 int64_t target_msc
= 0;
640 int64_t remainder
= 0;
642 if (chain
->base
.present_mode
== VK_PRESENT_MODE_IMMEDIATE_KHR
)
643 options
|= XCB_PRESENT_OPTION_ASYNC
;
645 xshmfence_reset(image
->shm_fence
);
648 xcb_void_cookie_t cookie
=
649 xcb_present_pixmap(chain
->conn
,
652 (uint32_t) chain
->send_sbc
,
657 XCB_NONE
, /* target_crtc */
664 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
667 xcb_flush(chain
->conn
);
673 x11_image_init(VkDevice device_h
, struct x11_swapchain
*chain
,
674 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
675 const VkAllocationCallbacks
* pAllocator
,
676 struct x11_image
*image
)
678 xcb_void_cookie_t cookie
;
686 result
= chain
->base
.image_fns
->create_wsi_image(device_h
,
695 if (result
!= VK_SUCCESS
)
698 image
->pixmap
= xcb_generate_id(chain
->conn
);
701 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
705 pCreateInfo
->imageExtent
.width
,
706 pCreateInfo
->imageExtent
.height
,
708 chain
->depth
, bpp
, fd
);
709 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
711 int fence_fd
= xshmfence_alloc_shm();
715 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
716 if (image
->shm_fence
== NULL
)
717 goto fail_shmfence_alloc
;
719 image
->sync_fence
= xcb_generate_id(chain
->conn
);
720 xcb_dri3_fence_from_fd(chain
->conn
,
727 xshmfence_trigger(image
->shm_fence
);
735 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
736 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
738 chain
->base
.image_fns
->free_wsi_image(device_h
, pAllocator
,
739 image
->image
, image
->memory
);
745 x11_image_finish(struct x11_swapchain
*chain
,
746 const VkAllocationCallbacks
* pAllocator
,
747 struct x11_image
*image
)
749 xcb_void_cookie_t cookie
;
751 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
752 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
753 xshmfence_unmap_shm(image
->shm_fence
);
755 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
756 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
758 chain
->base
.image_fns
->free_wsi_image(chain
->base
.device
, pAllocator
,
759 image
->image
, image
->memory
);
763 x11_swapchain_destroy(struct wsi_swapchain
*anv_chain
,
764 const VkAllocationCallbacks
*pAllocator
)
766 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
767 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
768 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
770 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
772 vk_free(pAllocator
, chain
);
778 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
780 struct wsi_device
*wsi_device
,
781 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
782 const VkAllocationCallbacks
* pAllocator
,
783 const struct wsi_image_fns
*image_fns
,
784 struct wsi_swapchain
**swapchain_out
)
786 struct x11_swapchain
*chain
;
787 xcb_void_cookie_t cookie
;
790 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
792 int num_images
= pCreateInfo
->minImageCount
;
794 /* For true mailbox mode, we need at least 4 images:
795 * 1) One to scan out from
796 * 2) One to have queued for scan-out
797 * 3) One to be currently held by the Wayland compositor
798 * 4) One to render to
800 if (pCreateInfo
->presentMode
== VK_PRESENT_MODE_MAILBOX_KHR
)
801 num_images
= MAX2(num_images
, 4);
803 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
804 chain
= vk_alloc(pAllocator
, size
, 8,
805 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
807 return VK_ERROR_OUT_OF_HOST_MEMORY
;
809 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
810 xcb_window_t window
= x11_surface_get_window(icd_surface
);
811 xcb_get_geometry_reply_t
*geometry
=
812 xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, window
), NULL
);
814 if (geometry
== NULL
)
815 return VK_ERROR_SURFACE_LOST_KHR
;
817 chain
->base
.device
= device
;
818 chain
->base
.destroy
= x11_swapchain_destroy
;
819 chain
->base
.get_images
= x11_get_images
;
820 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
821 chain
->base
.queue_present
= x11_queue_present
;
822 chain
->base
.image_fns
= image_fns
;
823 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
825 chain
->window
= window
;
826 chain
->depth
= geometry
->depth
;
827 chain
->extent
= pCreateInfo
->imageExtent
;
828 chain
->image_count
= num_images
;
833 chain
->event_id
= xcb_generate_id(chain
->conn
);
834 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
835 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
836 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
837 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
839 /* Create an XCB event queue to hold present events outside of the usual
840 * application event queue
842 chain
->special_event
=
843 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
844 chain
->event_id
, NULL
);
846 chain
->gc
= xcb_generate_id(chain
->conn
);
848 /* FINISHME: Choose a better error. */
849 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
853 cookie
= xcb_create_gc(chain
->conn
,
856 XCB_GC_GRAPHICS_EXPOSURES
,
857 (uint32_t []) { 0 });
858 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
861 for (; image
< chain
->image_count
; image
++) {
862 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
863 &chain
->images
[image
]);
864 if (result
!= VK_SUCCESS
)
865 goto fail_init_images
;
868 *swapchain_out
= &chain
->base
;
873 for (uint32_t j
= 0; j
< image
; j
++)
874 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
877 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
879 vk_free(pAllocator
, chain
);
885 wsi_x11_init_wsi(struct wsi_device
*wsi_device
,
886 const VkAllocationCallbacks
*alloc
)
891 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
892 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
894 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
898 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
901 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
903 /* FINISHME: Choose a better error. */
904 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
910 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
911 _mesa_key_pointer_equal
);
912 if (!wsi
->connections
) {
913 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
917 wsi
->base
.get_support
= x11_surface_get_support
;
918 wsi
->base
.get_capabilities
= x11_surface_get_capabilities
;
919 wsi
->base
.get_formats
= x11_surface_get_formats
;
920 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
921 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
923 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
924 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
929 pthread_mutex_destroy(&wsi
->mutex
);
933 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
934 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
940 wsi_x11_finish_wsi(struct wsi_device
*wsi_device
,
941 const VkAllocationCallbacks
*alloc
)
943 struct wsi_x11
*wsi
=
944 (struct wsi_x11
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
947 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
949 pthread_mutex_destroy(&wsi
->mutex
);