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(struct anv_physical_device
*device
,
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 anv_alloc(&device
->instance
->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 anv_free(&device
->instance
->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(struct anv_physical_device
*device
,
84 struct wsi_x11_connection
*conn
)
86 anv_free(&device
->instance
->alloc
, conn
);
89 static struct wsi_x11_connection
*
90 wsi_x11_get_connection(struct anv_physical_device
*device
,
91 xcb_connection_t
*conn
)
94 (struct wsi_x11
*)device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
96 pthread_mutex_lock(&wsi
->mutex
);
98 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
100 /* We're about to make a bunch of blocking calls. Let's drop the
101 * mutex for now so we don't block up too badly.
103 pthread_mutex_unlock(&wsi
->mutex
);
105 struct wsi_x11_connection
*wsi_conn
=
106 wsi_x11_connection_create(device
, conn
);
108 pthread_mutex_lock(&wsi
->mutex
);
110 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
112 /* Oops, someone raced us to it */
113 wsi_x11_connection_destroy(device
, wsi_conn
);
115 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
119 pthread_mutex_unlock(&wsi
->mutex
);
124 static const VkSurfaceFormatKHR formats
[] = {
125 { .format
= VK_FORMAT_B8G8R8A8_SRGB
, },
128 static const VkPresentModeKHR present_modes
[] = {
129 VK_PRESENT_MODE_MAILBOX_KHR
,
132 static xcb_screen_t
*
133 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
135 xcb_screen_iterator_t screen_iter
=
136 xcb_setup_roots_iterator(xcb_get_setup(conn
));
138 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
139 if (screen_iter
.data
->root
== root
)
140 return screen_iter
.data
;
146 static xcb_visualtype_t
*
147 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
150 xcb_depth_iterator_t depth_iter
=
151 xcb_screen_allowed_depths_iterator(screen
);
153 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
154 xcb_visualtype_iterator_t visual_iter
=
155 xcb_depth_visuals_iterator (depth_iter
.data
);
157 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
158 if (visual_iter
.data
->visual_id
== visual_id
) {
160 *depth
= depth_iter
.data
->depth
;
161 return visual_iter
.data
;
169 static xcb_visualtype_t
*
170 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
173 xcb_screen_iterator_t screen_iter
=
174 xcb_setup_roots_iterator(xcb_get_setup(conn
));
176 /* For this we have to iterate over all of the screens which is rather
177 * annoying. Fortunately, there is probably only 1.
179 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
180 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
189 static xcb_visualtype_t
*
190 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
193 xcb_query_tree_cookie_t tree_cookie
;
194 xcb_get_window_attributes_cookie_t attrib_cookie
;
195 xcb_query_tree_reply_t
*tree
;
196 xcb_get_window_attributes_reply_t
*attrib
;
198 tree_cookie
= xcb_query_tree(conn
, window
);
199 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
201 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
202 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
203 if (attrib
== NULL
|| tree
== NULL
) {
209 xcb_window_t root
= tree
->root
;
210 xcb_visualid_t visual_id
= attrib
->visual
;
214 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
218 return screen_get_visualtype(screen
, visual_id
, depth
);
222 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
224 uint32_t rgb_mask
= visual
->red_mask
|
228 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
230 /* Do we have bits left over after RGB? */
231 return (all_mask
& ~rgb_mask
) != 0;
234 VkBool32
anv_GetPhysicalDeviceXcbPresentationSupportKHR(
235 VkPhysicalDevice physicalDevice
,
236 uint32_t queueFamilyIndex
,
237 xcb_connection_t
* connection
,
238 xcb_visualid_t visual_id
)
240 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
242 struct wsi_x11_connection
*wsi_conn
=
243 wsi_x11_get_connection(device
, connection
);
245 if (!wsi_conn
->has_dri3
) {
246 fprintf(stderr
, "vulkan: No DRI3 support\n");
250 unsigned visual_depth
;
251 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
254 if (visual_depth
!= 24 && visual_depth
!= 32)
260 VkBool32
anv_GetPhysicalDeviceXlibPresentationSupportKHR(
261 VkPhysicalDevice physicalDevice
,
262 uint32_t queueFamilyIndex
,
266 return anv_GetPhysicalDeviceXcbPresentationSupportKHR(physicalDevice
,
268 XGetXCBConnection(dpy
),
272 static xcb_connection_t
*
273 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
275 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
276 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
278 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
282 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
284 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
285 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
287 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
291 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
292 struct anv_physical_device
*device
,
293 uint32_t queueFamilyIndex
,
294 VkBool32
* pSupported
)
296 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
297 xcb_window_t window
= x11_surface_get_window(icd_surface
);
299 struct wsi_x11_connection
*wsi_conn
=
300 wsi_x11_get_connection(device
, conn
);
302 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
304 if (!wsi_conn
->has_dri3
) {
305 fprintf(stderr
, "vulkan: No DRI3 support\n");
310 unsigned visual_depth
;
311 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
316 if (visual_depth
!= 24 && visual_depth
!= 32) {
326 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
327 struct anv_physical_device
*device
,
328 VkSurfaceCapabilitiesKHR
*caps
)
330 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
331 xcb_window_t window
= x11_surface_get_window(icd_surface
);
332 xcb_get_geometry_cookie_t geom_cookie
;
333 xcb_generic_error_t
*err
;
334 xcb_get_geometry_reply_t
*geom
;
335 unsigned visual_depth
;
337 geom_cookie
= xcb_get_geometry(conn
, window
);
339 /* This does a round-trip. This is why we do get_geometry first and
340 * wait to read the reply until after we have a visual.
342 xcb_visualtype_t
*visual
=
343 get_visualtype_for_window(conn
, window
, &visual_depth
);
345 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
347 VkExtent2D extent
= { geom
->width
, geom
->height
};
348 caps
->currentExtent
= extent
;
349 caps
->minImageExtent
= extent
;
350 caps
->maxImageExtent
= extent
;
352 /* This can happen if the client didn't wait for the configure event
353 * to come back from the compositor. In that case, we don't know the
354 * size of the window so we just return valid "I don't know" stuff.
356 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
357 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
358 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
363 if (visual_has_alpha(visual
, visual_depth
)) {
364 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
365 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
367 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
368 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
371 caps
->minImageCount
= 2;
372 caps
->maxImageCount
= 4;
373 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
374 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
375 caps
->maxImageArrayLayers
= 1;
376 caps
->supportedUsageFlags
=
377 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
378 VK_IMAGE_USAGE_SAMPLED_BIT
|
379 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
380 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
386 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
387 struct anv_physical_device
*device
,
388 uint32_t *pSurfaceFormatCount
,
389 VkSurfaceFormatKHR
*pSurfaceFormats
)
391 if (pSurfaceFormats
== NULL
) {
392 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
396 assert(*pSurfaceFormatCount
>= ARRAY_SIZE(formats
));
397 typed_memcpy(pSurfaceFormats
, formats
, *pSurfaceFormatCount
);
398 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
404 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
405 struct anv_physical_device
*device
,
406 uint32_t *pPresentModeCount
,
407 VkPresentModeKHR
*pPresentModes
)
409 if (pPresentModes
== NULL
) {
410 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
414 assert(*pPresentModeCount
>= ARRAY_SIZE(present_modes
));
415 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
416 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
422 x11_surface_create_swapchain(VkIcdSurfaceBase
*surface
,
423 struct anv_device
*device
,
424 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
425 const VkAllocationCallbacks
* pAllocator
,
426 struct anv_swapchain
**swapchain
);
428 VkResult
anv_CreateXcbSurfaceKHR(
429 VkInstance _instance
,
430 const VkXcbSurfaceCreateInfoKHR
* pCreateInfo
,
431 const VkAllocationCallbacks
* pAllocator
,
432 VkSurfaceKHR
* pSurface
)
434 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
436 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR
);
438 VkIcdSurfaceXcb
*surface
;
440 surface
= anv_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
441 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
443 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
445 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
446 surface
->connection
= pCreateInfo
->connection
;
447 surface
->window
= pCreateInfo
->window
;
449 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
454 VkResult
anv_CreateXlibSurfaceKHR(
455 VkInstance _instance
,
456 const VkXlibSurfaceCreateInfoKHR
* pCreateInfo
,
457 const VkAllocationCallbacks
* pAllocator
,
458 VkSurfaceKHR
* pSurface
)
460 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
462 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR
);
464 VkIcdSurfaceXlib
*surface
;
466 surface
= anv_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
467 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
469 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
471 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
472 surface
->dpy
= pCreateInfo
->dpy
;
473 surface
->window
= pCreateInfo
->window
;
475 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
481 struct anv_image
* image
;
482 struct anv_device_memory
* memory
;
485 struct xshmfence
* shm_fence
;
489 struct x11_swapchain
{
490 struct anv_swapchain base
;
492 xcb_connection_t
* conn
;
496 uint32_t image_count
;
498 xcb_present_event_t event_id
;
499 xcb_special_event_t
* special_event
;
503 struct x11_image images
[0];
507 x11_get_images(struct anv_swapchain
*anv_chain
,
508 uint32_t* pCount
, VkImage
*pSwapchainImages
)
510 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
512 if (pSwapchainImages
== NULL
) {
513 *pCount
= chain
->image_count
;
517 assert(chain
->image_count
<= *pCount
);
518 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
519 pSwapchainImages
[i
] = anv_image_to_handle(chain
->images
[i
].image
);
521 *pCount
= chain
->image_count
;
527 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
528 xcb_present_generic_event_t
*event
)
530 switch (event
->evtype
) {
531 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
532 xcb_present_configure_notify_event_t
*config
= (void *) event
;
534 if (config
->width
!= chain
->extent
.width
||
535 config
->height
!= chain
->extent
.height
)
536 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
541 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
542 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
544 for (unsigned i
= 0; i
< chain
->image_count
; i
++) {
545 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
546 chain
->images
[i
].busy
= false;
554 case XCB_PRESENT_COMPLETE_NOTIFY
:
563 x11_acquire_next_image(struct anv_swapchain
*anv_chain
,
565 VkSemaphore semaphore
,
566 uint32_t *image_index
)
568 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
571 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
572 if (!chain
->images
[i
].busy
) {
573 /* We found a non-busy image */
574 xshmfence_await(chain
->images
[i
].shm_fence
);
580 xcb_flush(chain
->conn
);
581 xcb_generic_event_t
*event
=
582 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
584 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
586 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
588 if (result
!= VK_SUCCESS
)
594 x11_queue_present(struct anv_swapchain
*anv_chain
,
595 struct anv_queue
*queue
,
596 uint32_t image_index
)
598 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
599 struct x11_image
*image
= &chain
->images
[image_index
];
601 assert(image_index
< chain
->image_count
);
603 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
605 int64_t target_msc
= 0;
607 int64_t remainder
= 0;
609 options
|= XCB_PRESENT_OPTION_ASYNC
;
611 xshmfence_reset(image
->shm_fence
);
613 xcb_void_cookie_t cookie
=
614 xcb_present_pixmap(chain
->conn
,
617 (uint32_t) chain
->send_sbc
,
622 XCB_NONE
, /* target_crtc */
629 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
632 xcb_flush(chain
->conn
);
638 x11_image_init(struct anv_device
*device
, struct x11_swapchain
*chain
,
639 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
640 const VkAllocationCallbacks
* pAllocator
,
641 struct x11_image
*image
)
643 xcb_void_cookie_t cookie
;
647 result
= anv_image_create(anv_device_to_handle(device
),
648 &(struct anv_image_create_info
) {
649 .isl_tiling_flags
= ISL_TILING_X_BIT
,
652 &(VkImageCreateInfo
) {
653 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
654 .imageType
= VK_IMAGE_TYPE_2D
,
655 .format
= pCreateInfo
->imageFormat
,
657 .width
= pCreateInfo
->imageExtent
.width
,
658 .height
= pCreateInfo
->imageExtent
.height
,
664 /* FIXME: Need a way to use X tiling to allow scanout */
665 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
666 .usage
= (pCreateInfo
->imageUsage
|
667 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
),
672 if (result
!= VK_SUCCESS
)
675 image
->image
= anv_image_from_handle(image_h
);
676 assert(vk_format_is_color(image
->image
->vk_format
));
678 VkDeviceMemory memory_h
;
679 result
= anv_AllocateMemory(anv_device_to_handle(device
),
680 &(VkMemoryAllocateInfo
) {
681 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
682 .allocationSize
= image
->image
->size
,
683 .memoryTypeIndex
= 0,
685 NULL
/* XXX: pAllocator */,
687 if (result
!= VK_SUCCESS
)
688 goto fail_create_image
;
690 image
->memory
= anv_device_memory_from_handle(memory_h
);
691 image
->memory
->bo
.is_winsys_bo
= true;
693 anv_BindImageMemory(VK_NULL_HANDLE
, image_h
, memory_h
, 0);
695 struct anv_surface
*surface
= &image
->image
->color_surface
;
696 assert(surface
->isl
.tiling
== ISL_TILING_X
);
698 int ret
= anv_gem_set_tiling(device
, image
->memory
->bo
.gem_handle
,
699 surface
->isl
.row_pitch
, I915_TILING_X
);
701 /* FINISHME: Choose a better error. */
702 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
703 "set_tiling failed: %m");
704 goto fail_alloc_memory
;
707 int fd
= anv_gem_handle_to_fd(device
, image
->memory
->bo
.gem_handle
);
709 /* FINISHME: Choose a better error. */
710 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
711 "handle_to_fd failed: %m");
712 goto fail_alloc_memory
;
717 image
->pixmap
= xcb_generate_id(chain
->conn
);
720 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
724 pCreateInfo
->imageExtent
.width
,
725 pCreateInfo
->imageExtent
.height
,
726 surface
->isl
.row_pitch
,
728 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
730 int fence_fd
= xshmfence_alloc_shm();
734 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
735 if (image
->shm_fence
== NULL
)
736 goto fail_shmfence_alloc
;
738 image
->sync_fence
= xcb_generate_id(chain
->conn
);
739 xcb_dri3_fence_from_fd(chain
->conn
,
746 xshmfence_trigger(image
->shm_fence
);
754 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
755 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
758 anv_FreeMemory(anv_device_to_handle(chain
->base
.device
),
759 anv_device_memory_to_handle(image
->memory
), pAllocator
);
762 anv_DestroyImage(anv_device_to_handle(chain
->base
.device
),
763 anv_image_to_handle(image
->image
), pAllocator
);
769 x11_image_finish(struct x11_swapchain
*chain
,
770 const VkAllocationCallbacks
* pAllocator
,
771 struct x11_image
*image
)
773 xcb_void_cookie_t cookie
;
775 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
776 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
777 xshmfence_unmap_shm(image
->shm_fence
);
779 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
780 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
782 anv_DestroyImage(anv_device_to_handle(chain
->base
.device
),
783 anv_image_to_handle(image
->image
), pAllocator
);
785 anv_FreeMemory(anv_device_to_handle(chain
->base
.device
),
786 anv_device_memory_to_handle(image
->memory
), pAllocator
);
790 x11_swapchain_destroy(struct anv_swapchain
*anv_chain
,
791 const VkAllocationCallbacks
*pAllocator
)
793 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
795 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
796 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
798 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
800 anv_free2(&chain
->base
.device
->alloc
, pAllocator
, chain
);
806 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
807 struct anv_device
*device
,
808 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
809 const VkAllocationCallbacks
* pAllocator
,
810 struct anv_swapchain
**swapchain_out
)
812 struct x11_swapchain
*chain
;
813 xcb_void_cookie_t cookie
;
816 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
818 int num_images
= pCreateInfo
->minImageCount
;
820 /* For true mailbox mode, we need at least 4 images:
821 * 1) One to scan out from
822 * 2) One to have queued for scan-out
823 * 3) One to be currently held by the Wayland compositor
824 * 4) One to render to
826 if (pCreateInfo
->presentMode
== VK_PRESENT_MODE_MAILBOX_KHR
)
827 num_images
= MAX2(num_images
, 4);
829 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
830 chain
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
831 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
833 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
835 chain
->base
.device
= device
;
836 chain
->base
.destroy
= x11_swapchain_destroy
;
837 chain
->base
.get_images
= x11_get_images
;
838 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
839 chain
->base
.queue_present
= x11_queue_present
;
841 chain
->conn
= x11_surface_get_connection(icd_surface
);
842 chain
->window
= x11_surface_get_window(icd_surface
);
843 chain
->extent
= pCreateInfo
->imageExtent
;
844 chain
->image_count
= num_images
;
846 chain
->event_id
= xcb_generate_id(chain
->conn
);
847 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
848 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
849 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
850 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
852 /* Create an XCB event queue to hold present events outside of the usual
853 * application event queue
855 chain
->special_event
=
856 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
857 chain
->event_id
, NULL
);
859 chain
->gc
= xcb_generate_id(chain
->conn
);
861 /* FINISHME: Choose a better error. */
862 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
866 cookie
= xcb_create_gc(chain
->conn
,
869 XCB_GC_GRAPHICS_EXPOSURES
,
870 (uint32_t []) { 0 });
871 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
874 for (; image
< chain
->image_count
; image
++) {
875 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
876 &chain
->images
[image
]);
877 if (result
!= VK_SUCCESS
)
878 goto fail_init_images
;
881 *swapchain_out
= &chain
->base
;
886 for (uint32_t j
= 0; j
< image
; j
++)
887 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
890 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
892 anv_free2(&device
->alloc
, pAllocator
, chain
);
898 anv_x11_init_wsi(struct anv_physical_device
*device
)
903 wsi
= anv_alloc(&device
->instance
->alloc
, sizeof(*wsi
), 8,
904 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
906 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
910 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
913 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
915 /* FINISHME: Choose a better error. */
916 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
922 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
923 _mesa_key_pointer_equal
);
924 if (!wsi
->connections
) {
925 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
929 wsi
->base
.get_support
= x11_surface_get_support
;
930 wsi
->base
.get_capabilities
= x11_surface_get_capabilities
;
931 wsi
->base
.get_formats
= x11_surface_get_formats
;
932 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
933 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
935 device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
936 device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
941 pthread_mutex_destroy(&wsi
->mutex
);
943 anv_free(&device
->instance
->alloc
, wsi
);
945 device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
946 device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
952 anv_x11_finish_wsi(struct anv_physical_device
*device
)
954 struct wsi_x11
*wsi
=
955 (struct wsi_x11
*)device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
958 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
960 pthread_mutex_destroy(&wsi
->mutex
);
962 anv_free(&device
->instance
->alloc
, wsi
);