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>
32 #include "vk_format_info.h"
33 #include "util/hash_table.h"
35 struct wsi_x11_connection
{
41 struct anv_wsi_interface base
;
43 pthread_mutex_t mutex
;
44 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
45 struct hash_table
*connections
;
48 static struct wsi_x11_connection
*
49 wsi_x11_connection_create(const VkAllocationCallbacks
*alloc
,
50 xcb_connection_t
*conn
)
52 xcb_query_extension_cookie_t dri3_cookie
, pres_cookie
;
53 xcb_query_extension_reply_t
*dri3_reply
, *pres_reply
;
55 struct wsi_x11_connection
*wsi_conn
=
56 vk_alloc(alloc
, sizeof(*wsi_conn
), 8,
57 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
61 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
62 pres_cookie
= xcb_query_extension(conn
, 7, "PRESENT");
64 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
65 pres_reply
= xcb_query_extension_reply(conn
, pres_cookie
, NULL
);
66 if (dri3_reply
== NULL
|| pres_reply
== NULL
) {
69 vk_free(alloc
, wsi_conn
);
73 wsi_conn
->has_dri3
= dri3_reply
->present
!= 0;
74 wsi_conn
->has_present
= pres_reply
->present
!= 0;
83 wsi_x11_connection_destroy(const VkAllocationCallbacks
*alloc
,
84 struct wsi_x11_connection
*conn
)
89 static struct wsi_x11_connection
*
90 wsi_x11_get_connection(struct anv_wsi_device
*wsi_dev
,
91 const VkAllocationCallbacks
*alloc
,
92 xcb_connection_t
*conn
)
95 (struct wsi_x11
*)wsi_dev
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
97 pthread_mutex_lock(&wsi
->mutex
);
99 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
101 /* We're about to make a bunch of blocking calls. Let's drop the
102 * mutex for now so we don't block up too badly.
104 pthread_mutex_unlock(&wsi
->mutex
);
106 struct wsi_x11_connection
*wsi_conn
=
107 wsi_x11_connection_create(alloc
, conn
);
109 pthread_mutex_lock(&wsi
->mutex
);
111 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
113 /* Oops, someone raced us to it */
114 wsi_x11_connection_destroy(alloc
, wsi_conn
);
116 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
120 pthread_mutex_unlock(&wsi
->mutex
);
125 static const VkSurfaceFormatKHR formats
[] = {
126 { .format
= VK_FORMAT_B8G8R8A8_SRGB
, },
127 { .format
= VK_FORMAT_B8G8R8A8_UNORM
, },
130 static const VkPresentModeKHR present_modes
[] = {
131 VK_PRESENT_MODE_MAILBOX_KHR
,
134 static xcb_screen_t
*
135 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
137 xcb_screen_iterator_t screen_iter
=
138 xcb_setup_roots_iterator(xcb_get_setup(conn
));
140 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
141 if (screen_iter
.data
->root
== root
)
142 return screen_iter
.data
;
148 static xcb_visualtype_t
*
149 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
152 xcb_depth_iterator_t depth_iter
=
153 xcb_screen_allowed_depths_iterator(screen
);
155 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
156 xcb_visualtype_iterator_t visual_iter
=
157 xcb_depth_visuals_iterator (depth_iter
.data
);
159 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
160 if (visual_iter
.data
->visual_id
== visual_id
) {
162 *depth
= depth_iter
.data
->depth
;
163 return visual_iter
.data
;
171 static xcb_visualtype_t
*
172 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
175 xcb_screen_iterator_t screen_iter
=
176 xcb_setup_roots_iterator(xcb_get_setup(conn
));
178 /* For this we have to iterate over all of the screens which is rather
179 * annoying. Fortunately, there is probably only 1.
181 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
182 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
191 static xcb_visualtype_t
*
192 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
195 xcb_query_tree_cookie_t tree_cookie
;
196 xcb_get_window_attributes_cookie_t attrib_cookie
;
197 xcb_query_tree_reply_t
*tree
;
198 xcb_get_window_attributes_reply_t
*attrib
;
200 tree_cookie
= xcb_query_tree(conn
, window
);
201 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
203 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
204 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
205 if (attrib
== NULL
|| tree
== NULL
) {
211 xcb_window_t root
= tree
->root
;
212 xcb_visualid_t visual_id
= attrib
->visual
;
216 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
220 return screen_get_visualtype(screen
, visual_id
, depth
);
224 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
226 uint32_t rgb_mask
= visual
->red_mask
|
230 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
232 /* Do we have bits left over after RGB? */
233 return (all_mask
& ~rgb_mask
) != 0;
236 static VkBool32
anv_get_physical_device_xcb_presentation_support(
237 struct anv_wsi_device
*wsi_device
,
238 VkAllocationCallbacks
*alloc
,
239 uint32_t queueFamilyIndex
,
240 xcb_connection_t
* connection
,
241 xcb_visualid_t visual_id
)
243 struct wsi_x11_connection
*wsi_conn
=
244 wsi_x11_get_connection(wsi_device
, alloc
, connection
);
246 if (!wsi_conn
->has_dri3
) {
247 fprintf(stderr
, "vulkan: No DRI3 support\n");
251 unsigned visual_depth
;
252 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
255 if (visual_depth
!= 24 && visual_depth
!= 32)
261 VkBool32
anv_GetPhysicalDeviceXcbPresentationSupportKHR(
262 VkPhysicalDevice physicalDevice
,
263 uint32_t queueFamilyIndex
,
264 xcb_connection_t
* connection
,
265 xcb_visualid_t visual_id
)
267 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
269 return anv_get_physical_device_xcb_presentation_support(
271 &device
->instance
->alloc
,
272 queueFamilyIndex
, connection
, visual_id
);
275 VkBool32
anv_GetPhysicalDeviceXlibPresentationSupportKHR(
276 VkPhysicalDevice physicalDevice
,
277 uint32_t queueFamilyIndex
,
281 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
283 return anv_get_physical_device_xcb_presentation_support(
285 &device
->instance
->alloc
,
286 queueFamilyIndex
, XGetXCBConnection(dpy
), visualID
);
289 static xcb_connection_t
*
290 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
292 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
293 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
295 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
299 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
301 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
302 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
304 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
308 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
309 struct anv_wsi_device
*wsi_device
,
310 const VkAllocationCallbacks
*alloc
,
311 uint32_t queueFamilyIndex
,
312 VkBool32
* pSupported
)
314 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
315 xcb_window_t window
= x11_surface_get_window(icd_surface
);
317 struct wsi_x11_connection
*wsi_conn
=
318 wsi_x11_get_connection(wsi_device
, alloc
, conn
);
320 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
322 if (!wsi_conn
->has_dri3
) {
323 fprintf(stderr
, "vulkan: No DRI3 support\n");
328 unsigned visual_depth
;
329 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
334 if (visual_depth
!= 24 && visual_depth
!= 32) {
344 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
345 VkSurfaceCapabilitiesKHR
*caps
)
347 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
348 xcb_window_t window
= x11_surface_get_window(icd_surface
);
349 xcb_get_geometry_cookie_t geom_cookie
;
350 xcb_generic_error_t
*err
;
351 xcb_get_geometry_reply_t
*geom
;
352 unsigned visual_depth
;
354 geom_cookie
= xcb_get_geometry(conn
, window
);
356 /* This does a round-trip. This is why we do get_geometry first and
357 * wait to read the reply until after we have a visual.
359 xcb_visualtype_t
*visual
=
360 get_visualtype_for_window(conn
, window
, &visual_depth
);
362 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
364 VkExtent2D extent
= { geom
->width
, geom
->height
};
365 caps
->currentExtent
= extent
;
366 caps
->minImageExtent
= extent
;
367 caps
->maxImageExtent
= extent
;
369 /* This can happen if the client didn't wait for the configure event
370 * to come back from the compositor. In that case, we don't know the
371 * size of the window so we just return valid "I don't know" stuff.
373 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
374 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
375 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
380 if (visual_has_alpha(visual
, visual_depth
)) {
381 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
382 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
384 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
385 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
388 caps
->minImageCount
= 2;
389 caps
->maxImageCount
= 4;
390 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
391 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
392 caps
->maxImageArrayLayers
= 1;
393 caps
->supportedUsageFlags
=
394 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
395 VK_IMAGE_USAGE_SAMPLED_BIT
|
396 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
397 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
403 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
404 struct anv_wsi_device
*wsi_device
,
405 uint32_t *pSurfaceFormatCount
,
406 VkSurfaceFormatKHR
*pSurfaceFormats
)
408 if (pSurfaceFormats
== NULL
) {
409 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
413 assert(*pSurfaceFormatCount
>= ARRAY_SIZE(formats
));
414 typed_memcpy(pSurfaceFormats
, formats
, *pSurfaceFormatCount
);
415 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
421 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
422 uint32_t *pPresentModeCount
,
423 VkPresentModeKHR
*pPresentModes
)
425 if (pPresentModes
== NULL
) {
426 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
430 assert(*pPresentModeCount
>= ARRAY_SIZE(present_modes
));
431 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
432 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
438 x11_surface_create_swapchain(VkIcdSurfaceBase
*surface
,
439 struct anv_device
*device
,
440 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
441 const VkAllocationCallbacks
* pAllocator
,
442 const struct anv_wsi_image_fns
*image_fns
,
443 struct anv_swapchain
**swapchain
);
445 VkResult
anv_CreateXcbSurfaceKHR(
446 VkInstance _instance
,
447 const VkXcbSurfaceCreateInfoKHR
* pCreateInfo
,
448 const VkAllocationCallbacks
* pAllocator
,
449 VkSurfaceKHR
* pSurface
)
451 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
453 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR
);
455 VkIcdSurfaceXcb
*surface
;
457 surface
= vk_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
458 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
460 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
462 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
463 surface
->connection
= pCreateInfo
->connection
;
464 surface
->window
= pCreateInfo
->window
;
466 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
471 VkResult
anv_CreateXlibSurfaceKHR(
472 VkInstance _instance
,
473 const VkXlibSurfaceCreateInfoKHR
* pCreateInfo
,
474 const VkAllocationCallbacks
* pAllocator
,
475 VkSurfaceKHR
* pSurface
)
477 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
479 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR
);
481 VkIcdSurfaceXlib
*surface
;
483 surface
= vk_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
484 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
486 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
488 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
489 surface
->dpy
= pCreateInfo
->dpy
;
490 surface
->window
= pCreateInfo
->window
;
492 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
499 VkDeviceMemory memory
;
502 struct xshmfence
* shm_fence
;
506 struct x11_swapchain
{
507 struct anv_swapchain base
;
509 xcb_connection_t
* conn
;
513 uint32_t image_count
;
515 xcb_present_event_t event_id
;
516 xcb_special_event_t
* special_event
;
520 struct x11_image images
[0];
524 x11_get_images(struct anv_swapchain
*anv_chain
,
525 uint32_t* pCount
, VkImage
*pSwapchainImages
)
527 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
529 if (pSwapchainImages
== NULL
) {
530 *pCount
= chain
->image_count
;
534 assert(chain
->image_count
<= *pCount
);
535 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
536 pSwapchainImages
[i
] = chain
->images
[i
].image
;
538 *pCount
= chain
->image_count
;
544 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
545 xcb_present_generic_event_t
*event
)
547 switch (event
->evtype
) {
548 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
549 xcb_present_configure_notify_event_t
*config
= (void *) event
;
551 if (config
->width
!= chain
->extent
.width
||
552 config
->height
!= chain
->extent
.height
)
553 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
558 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
559 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
561 for (unsigned i
= 0; i
< chain
->image_count
; i
++) {
562 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
563 chain
->images
[i
].busy
= false;
571 case XCB_PRESENT_COMPLETE_NOTIFY
:
580 x11_acquire_next_image(struct anv_swapchain
*anv_chain
,
582 VkSemaphore semaphore
,
583 uint32_t *image_index
)
585 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
588 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
589 if (!chain
->images
[i
].busy
) {
590 /* We found a non-busy image */
591 xshmfence_await(chain
->images
[i
].shm_fence
);
593 chain
->images
[i
].busy
= true;
598 xcb_flush(chain
->conn
);
599 xcb_generic_event_t
*event
=
600 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
602 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
604 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
606 if (result
!= VK_SUCCESS
)
612 x11_queue_present(struct anv_swapchain
*anv_chain
,
613 uint32_t image_index
)
615 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
616 struct x11_image
*image
= &chain
->images
[image_index
];
618 assert(image_index
< chain
->image_count
);
620 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
622 int64_t target_msc
= 0;
624 int64_t remainder
= 0;
626 options
|= XCB_PRESENT_OPTION_ASYNC
;
628 xshmfence_reset(image
->shm_fence
);
631 xcb_void_cookie_t cookie
=
632 xcb_present_pixmap(chain
->conn
,
635 (uint32_t) chain
->send_sbc
,
640 XCB_NONE
, /* target_crtc */
647 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
650 xcb_flush(chain
->conn
);
656 x11_image_init(VkDevice device_h
, struct x11_swapchain
*chain
,
657 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
658 const VkAllocationCallbacks
* pAllocator
,
659 struct x11_image
*image
)
661 xcb_void_cookie_t cookie
;
670 result
= chain
->base
.image_fns
->create_wsi_image(device_h
,
679 if (result
!= VK_SUCCESS
)
682 image
->pixmap
= xcb_generate_id(chain
->conn
);
685 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
689 pCreateInfo
->imageExtent
.width
,
690 pCreateInfo
->imageExtent
.height
,
693 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
695 int fence_fd
= xshmfence_alloc_shm();
699 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
700 if (image
->shm_fence
== NULL
)
701 goto fail_shmfence_alloc
;
703 image
->sync_fence
= xcb_generate_id(chain
->conn
);
704 xcb_dri3_fence_from_fd(chain
->conn
,
711 xshmfence_trigger(image
->shm_fence
);
719 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
720 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
722 chain
->base
.image_fns
->free_wsi_image(device_h
, pAllocator
,
723 image
->image
, image
->memory
);
728 x11_image_finish(struct x11_swapchain
*chain
,
729 const VkAllocationCallbacks
* pAllocator
,
730 struct x11_image
*image
)
732 xcb_void_cookie_t cookie
;
734 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
735 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
736 xshmfence_unmap_shm(image
->shm_fence
);
738 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
739 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
741 chain
->base
.image_fns
->free_wsi_image(chain
->base
.device
, pAllocator
,
742 image
->image
, image
->memory
);
746 x11_swapchain_destroy(struct anv_swapchain
*anv_chain
,
747 const VkAllocationCallbacks
*pAllocator
)
749 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
750 struct anv_device
*device
= anv_device_from_handle(chain
->base
.device
);
751 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
752 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
754 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
756 vk_free2(&device
->alloc
, pAllocator
, chain
);
762 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
763 struct anv_device
*device
,
764 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
765 const VkAllocationCallbacks
* pAllocator
,
766 const struct anv_wsi_image_fns
*image_fns
,
767 struct anv_swapchain
**swapchain_out
)
769 struct x11_swapchain
*chain
;
770 xcb_void_cookie_t cookie
;
773 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
775 int num_images
= pCreateInfo
->minImageCount
;
777 /* For true mailbox mode, we need at least 4 images:
778 * 1) One to scan out from
779 * 2) One to have queued for scan-out
780 * 3) One to be currently held by the Wayland compositor
781 * 4) One to render to
783 if (pCreateInfo
->presentMode
== VK_PRESENT_MODE_MAILBOX_KHR
)
784 num_images
= MAX2(num_images
, 4);
786 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
787 chain
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
788 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
790 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
792 chain
->base
.device
= anv_device_to_handle(device
);
793 chain
->base
.destroy
= x11_swapchain_destroy
;
794 chain
->base
.get_images
= x11_get_images
;
795 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
796 chain
->base
.queue_present
= x11_queue_present
;
797 chain
->base
.image_fns
= image_fns
;
798 chain
->conn
= x11_surface_get_connection(icd_surface
);
799 chain
->window
= x11_surface_get_window(icd_surface
);
800 chain
->extent
= pCreateInfo
->imageExtent
;
801 chain
->image_count
= num_images
;
804 chain
->event_id
= xcb_generate_id(chain
->conn
);
805 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
806 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
807 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
808 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
810 /* Create an XCB event queue to hold present events outside of the usual
811 * application event queue
813 chain
->special_event
=
814 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
815 chain
->event_id
, NULL
);
817 chain
->gc
= xcb_generate_id(chain
->conn
);
819 /* FINISHME: Choose a better error. */
820 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
824 cookie
= xcb_create_gc(chain
->conn
,
827 XCB_GC_GRAPHICS_EXPOSURES
,
828 (uint32_t []) { 0 });
829 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
832 for (; image
< chain
->image_count
; image
++) {
833 result
= x11_image_init(anv_device_to_handle(device
), chain
, pCreateInfo
, pAllocator
,
834 &chain
->images
[image
]);
835 if (result
!= VK_SUCCESS
)
836 goto fail_init_images
;
839 *swapchain_out
= &chain
->base
;
844 for (uint32_t j
= 0; j
< image
; j
++)
845 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
848 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
850 vk_free2(&device
->alloc
, pAllocator
, chain
);
856 anv_x11_init_wsi(struct anv_wsi_device
*wsi_device
,
857 const VkAllocationCallbacks
*alloc
)
862 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
863 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
865 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
869 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
872 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
874 /* FINISHME: Choose a better error. */
875 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
881 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
882 _mesa_key_pointer_equal
);
883 if (!wsi
->connections
) {
884 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
888 wsi
->base
.get_support
= x11_surface_get_support
;
889 wsi
->base
.get_capabilities
= x11_surface_get_capabilities
;
890 wsi
->base
.get_formats
= x11_surface_get_formats
;
891 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
892 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
894 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
895 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
900 pthread_mutex_destroy(&wsi
->mutex
);
904 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
905 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
911 anv_x11_finish_wsi(struct anv_wsi_device
*wsi_device
,
912 const VkAllocationCallbacks
*alloc
)
914 struct wsi_x11
*wsi
=
915 (struct wsi_x11
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
918 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
920 pthread_mutex_destroy(&wsi
->mutex
);