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
, },
126 { .format
= VK_FORMAT_B8G8R8A8_UNORM
, },
129 static const VkPresentModeKHR present_modes
[] = {
130 VK_PRESENT_MODE_MAILBOX_KHR
,
133 static xcb_screen_t
*
134 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
136 xcb_screen_iterator_t screen_iter
=
137 xcb_setup_roots_iterator(xcb_get_setup(conn
));
139 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
140 if (screen_iter
.data
->root
== root
)
141 return screen_iter
.data
;
147 static xcb_visualtype_t
*
148 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
151 xcb_depth_iterator_t depth_iter
=
152 xcb_screen_allowed_depths_iterator(screen
);
154 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
155 xcb_visualtype_iterator_t visual_iter
=
156 xcb_depth_visuals_iterator (depth_iter
.data
);
158 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
159 if (visual_iter
.data
->visual_id
== visual_id
) {
161 *depth
= depth_iter
.data
->depth
;
162 return visual_iter
.data
;
170 static xcb_visualtype_t
*
171 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
174 xcb_screen_iterator_t screen_iter
=
175 xcb_setup_roots_iterator(xcb_get_setup(conn
));
177 /* For this we have to iterate over all of the screens which is rather
178 * annoying. Fortunately, there is probably only 1.
180 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
181 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
190 static xcb_visualtype_t
*
191 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
194 xcb_query_tree_cookie_t tree_cookie
;
195 xcb_get_window_attributes_cookie_t attrib_cookie
;
196 xcb_query_tree_reply_t
*tree
;
197 xcb_get_window_attributes_reply_t
*attrib
;
199 tree_cookie
= xcb_query_tree(conn
, window
);
200 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
202 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
203 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
204 if (attrib
== NULL
|| tree
== NULL
) {
210 xcb_window_t root
= tree
->root
;
211 xcb_visualid_t visual_id
= attrib
->visual
;
215 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
219 return screen_get_visualtype(screen
, visual_id
, depth
);
223 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
225 uint32_t rgb_mask
= visual
->red_mask
|
229 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
231 /* Do we have bits left over after RGB? */
232 return (all_mask
& ~rgb_mask
) != 0;
235 VkBool32
anv_GetPhysicalDeviceXcbPresentationSupportKHR(
236 VkPhysicalDevice physicalDevice
,
237 uint32_t queueFamilyIndex
,
238 xcb_connection_t
* connection
,
239 xcb_visualid_t visual_id
)
241 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
243 struct wsi_x11_connection
*wsi_conn
=
244 wsi_x11_get_connection(device
, 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_GetPhysicalDeviceXlibPresentationSupportKHR(
262 VkPhysicalDevice physicalDevice
,
263 uint32_t queueFamilyIndex
,
267 return anv_GetPhysicalDeviceXcbPresentationSupportKHR(physicalDevice
,
269 XGetXCBConnection(dpy
),
273 static xcb_connection_t
*
274 x11_surface_get_connection(VkIcdSurfaceBase
*icd_surface
)
276 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
277 return XGetXCBConnection(((VkIcdSurfaceXlib
*)icd_surface
)->dpy
);
279 return ((VkIcdSurfaceXcb
*)icd_surface
)->connection
;
283 x11_surface_get_window(VkIcdSurfaceBase
*icd_surface
)
285 if (icd_surface
->platform
== VK_ICD_WSI_PLATFORM_XLIB
)
286 return ((VkIcdSurfaceXlib
*)icd_surface
)->window
;
288 return ((VkIcdSurfaceXcb
*)icd_surface
)->window
;
292 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
293 struct anv_physical_device
*device
,
294 uint32_t queueFamilyIndex
,
295 VkBool32
* pSupported
)
297 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
298 xcb_window_t window
= x11_surface_get_window(icd_surface
);
300 struct wsi_x11_connection
*wsi_conn
=
301 wsi_x11_get_connection(device
, conn
);
303 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
305 if (!wsi_conn
->has_dri3
) {
306 fprintf(stderr
, "vulkan: No DRI3 support\n");
311 unsigned visual_depth
;
312 if (!get_visualtype_for_window(conn
, window
, &visual_depth
)) {
317 if (visual_depth
!= 24 && visual_depth
!= 32) {
327 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
328 struct anv_physical_device
*device
,
329 VkSurfaceCapabilitiesKHR
*caps
)
331 xcb_connection_t
*conn
= x11_surface_get_connection(icd_surface
);
332 xcb_window_t window
= x11_surface_get_window(icd_surface
);
333 xcb_get_geometry_cookie_t geom_cookie
;
334 xcb_generic_error_t
*err
;
335 xcb_get_geometry_reply_t
*geom
;
336 unsigned visual_depth
;
338 geom_cookie
= xcb_get_geometry(conn
, window
);
340 /* This does a round-trip. This is why we do get_geometry first and
341 * wait to read the reply until after we have a visual.
343 xcb_visualtype_t
*visual
=
344 get_visualtype_for_window(conn
, window
, &visual_depth
);
346 geom
= xcb_get_geometry_reply(conn
, geom_cookie
, &err
);
348 VkExtent2D extent
= { geom
->width
, geom
->height
};
349 caps
->currentExtent
= extent
;
350 caps
->minImageExtent
= extent
;
351 caps
->maxImageExtent
= extent
;
353 /* This can happen if the client didn't wait for the configure event
354 * to come back from the compositor. In that case, we don't know the
355 * size of the window so we just return valid "I don't know" stuff.
357 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
358 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
359 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
364 if (visual_has_alpha(visual
, visual_depth
)) {
365 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
366 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
368 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
369 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
372 caps
->minImageCount
= 2;
373 caps
->maxImageCount
= 4;
374 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
375 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
376 caps
->maxImageArrayLayers
= 1;
377 caps
->supportedUsageFlags
=
378 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
379 VK_IMAGE_USAGE_SAMPLED_BIT
|
380 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
381 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
387 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
388 struct anv_physical_device
*device
,
389 uint32_t *pSurfaceFormatCount
,
390 VkSurfaceFormatKHR
*pSurfaceFormats
)
392 if (pSurfaceFormats
== NULL
) {
393 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
397 assert(*pSurfaceFormatCount
>= ARRAY_SIZE(formats
));
398 typed_memcpy(pSurfaceFormats
, formats
, *pSurfaceFormatCount
);
399 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
405 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
406 struct anv_physical_device
*device
,
407 uint32_t *pPresentModeCount
,
408 VkPresentModeKHR
*pPresentModes
)
410 if (pPresentModes
== NULL
) {
411 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
415 assert(*pPresentModeCount
>= ARRAY_SIZE(present_modes
));
416 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
417 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
423 x11_surface_create_swapchain(VkIcdSurfaceBase
*surface
,
424 struct anv_device
*device
,
425 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
426 const VkAllocationCallbacks
* pAllocator
,
427 struct anv_swapchain
**swapchain
);
429 VkResult
anv_CreateXcbSurfaceKHR(
430 VkInstance _instance
,
431 const VkXcbSurfaceCreateInfoKHR
* pCreateInfo
,
432 const VkAllocationCallbacks
* pAllocator
,
433 VkSurfaceKHR
* pSurface
)
435 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
437 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR
);
439 VkIcdSurfaceXcb
*surface
;
441 surface
= anv_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
442 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
444 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
446 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
447 surface
->connection
= pCreateInfo
->connection
;
448 surface
->window
= pCreateInfo
->window
;
450 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
455 VkResult
anv_CreateXlibSurfaceKHR(
456 VkInstance _instance
,
457 const VkXlibSurfaceCreateInfoKHR
* pCreateInfo
,
458 const VkAllocationCallbacks
* pAllocator
,
459 VkSurfaceKHR
* pSurface
)
461 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
463 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR
);
465 VkIcdSurfaceXlib
*surface
;
467 surface
= anv_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
468 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
470 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
472 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XLIB
;
473 surface
->dpy
= pCreateInfo
->dpy
;
474 surface
->window
= pCreateInfo
->window
;
476 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
482 struct anv_image
* image
;
483 struct anv_device_memory
* memory
;
486 struct xshmfence
* shm_fence
;
490 struct x11_swapchain
{
491 struct anv_swapchain base
;
493 xcb_connection_t
* conn
;
497 uint32_t image_count
;
499 xcb_present_event_t event_id
;
500 xcb_special_event_t
* special_event
;
504 struct x11_image images
[0];
508 x11_get_images(struct anv_swapchain
*anv_chain
,
509 uint32_t* pCount
, VkImage
*pSwapchainImages
)
511 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
513 if (pSwapchainImages
== NULL
) {
514 *pCount
= chain
->image_count
;
518 assert(chain
->image_count
<= *pCount
);
519 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
520 pSwapchainImages
[i
] = anv_image_to_handle(chain
->images
[i
].image
);
522 *pCount
= chain
->image_count
;
528 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
529 xcb_present_generic_event_t
*event
)
531 switch (event
->evtype
) {
532 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
533 xcb_present_configure_notify_event_t
*config
= (void *) event
;
535 if (config
->width
!= chain
->extent
.width
||
536 config
->height
!= chain
->extent
.height
)
537 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
542 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
543 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
545 for (unsigned i
= 0; i
< chain
->image_count
; i
++) {
546 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
547 chain
->images
[i
].busy
= false;
555 case XCB_PRESENT_COMPLETE_NOTIFY
:
564 x11_acquire_next_image(struct anv_swapchain
*anv_chain
,
566 VkSemaphore semaphore
,
567 uint32_t *image_index
)
569 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
572 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
573 if (!chain
->images
[i
].busy
) {
574 /* We found a non-busy image */
575 xshmfence_await(chain
->images
[i
].shm_fence
);
581 xcb_flush(chain
->conn
);
582 xcb_generic_event_t
*event
=
583 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
585 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
587 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
589 if (result
!= VK_SUCCESS
)
595 x11_queue_present(struct anv_swapchain
*anv_chain
,
596 struct anv_queue
*queue
,
597 uint32_t image_index
)
599 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
600 struct x11_image
*image
= &chain
->images
[image_index
];
602 assert(image_index
< chain
->image_count
);
604 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
606 int64_t target_msc
= 0;
608 int64_t remainder
= 0;
610 options
|= XCB_PRESENT_OPTION_ASYNC
;
612 xshmfence_reset(image
->shm_fence
);
614 xcb_void_cookie_t cookie
=
615 xcb_present_pixmap(chain
->conn
,
618 (uint32_t) chain
->send_sbc
,
623 XCB_NONE
, /* target_crtc */
630 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
633 xcb_flush(chain
->conn
);
639 x11_image_init(struct anv_device
*device
, struct x11_swapchain
*chain
,
640 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
641 const VkAllocationCallbacks
* pAllocator
,
642 struct x11_image
*image
)
644 xcb_void_cookie_t cookie
;
648 result
= anv_image_create(anv_device_to_handle(device
),
649 &(struct anv_image_create_info
) {
650 .isl_tiling_flags
= ISL_TILING_X_BIT
,
653 &(VkImageCreateInfo
) {
654 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
655 .imageType
= VK_IMAGE_TYPE_2D
,
656 .format
= pCreateInfo
->imageFormat
,
658 .width
= pCreateInfo
->imageExtent
.width
,
659 .height
= pCreateInfo
->imageExtent
.height
,
665 /* FIXME: Need a way to use X tiling to allow scanout */
666 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
667 .usage
= (pCreateInfo
->imageUsage
|
668 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
),
673 if (result
!= VK_SUCCESS
)
676 image
->image
= anv_image_from_handle(image_h
);
677 assert(vk_format_is_color(image
->image
->vk_format
));
679 VkDeviceMemory memory_h
;
680 result
= anv_AllocateMemory(anv_device_to_handle(device
),
681 &(VkMemoryAllocateInfo
) {
682 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
683 .allocationSize
= image
->image
->size
,
684 .memoryTypeIndex
= 0,
686 NULL
/* XXX: pAllocator */,
688 if (result
!= VK_SUCCESS
)
689 goto fail_create_image
;
691 image
->memory
= anv_device_memory_from_handle(memory_h
);
692 image
->memory
->bo
.is_winsys_bo
= true;
694 anv_BindImageMemory(VK_NULL_HANDLE
, image_h
, memory_h
, 0);
696 struct anv_surface
*surface
= &image
->image
->color_surface
;
697 assert(surface
->isl
.tiling
== ISL_TILING_X
);
699 int ret
= anv_gem_set_tiling(device
, image
->memory
->bo
.gem_handle
,
700 surface
->isl
.row_pitch
, I915_TILING_X
);
702 /* FINISHME: Choose a better error. */
703 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
704 "set_tiling failed: %m");
705 goto fail_alloc_memory
;
708 int fd
= anv_gem_handle_to_fd(device
, image
->memory
->bo
.gem_handle
);
710 /* FINISHME: Choose a better error. */
711 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
712 "handle_to_fd failed: %m");
713 goto fail_alloc_memory
;
718 image
->pixmap
= xcb_generate_id(chain
->conn
);
721 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
725 pCreateInfo
->imageExtent
.width
,
726 pCreateInfo
->imageExtent
.height
,
727 surface
->isl
.row_pitch
,
729 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
731 int fence_fd
= xshmfence_alloc_shm();
735 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
736 if (image
->shm_fence
== NULL
)
737 goto fail_shmfence_alloc
;
739 image
->sync_fence
= xcb_generate_id(chain
->conn
);
740 xcb_dri3_fence_from_fd(chain
->conn
,
747 xshmfence_trigger(image
->shm_fence
);
755 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
756 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
759 anv_FreeMemory(anv_device_to_handle(chain
->base
.device
),
760 anv_device_memory_to_handle(image
->memory
), pAllocator
);
763 anv_DestroyImage(anv_device_to_handle(chain
->base
.device
),
764 anv_image_to_handle(image
->image
), pAllocator
);
770 x11_image_finish(struct x11_swapchain
*chain
,
771 const VkAllocationCallbacks
* pAllocator
,
772 struct x11_image
*image
)
774 xcb_void_cookie_t cookie
;
776 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
777 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
778 xshmfence_unmap_shm(image
->shm_fence
);
780 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
781 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
783 anv_DestroyImage(anv_device_to_handle(chain
->base
.device
),
784 anv_image_to_handle(image
->image
), pAllocator
);
786 anv_FreeMemory(anv_device_to_handle(chain
->base
.device
),
787 anv_device_memory_to_handle(image
->memory
), pAllocator
);
791 x11_swapchain_destroy(struct anv_swapchain
*anv_chain
,
792 const VkAllocationCallbacks
*pAllocator
)
794 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
796 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
797 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
799 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
801 anv_free2(&chain
->base
.device
->alloc
, pAllocator
, chain
);
807 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
808 struct anv_device
*device
,
809 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
810 const VkAllocationCallbacks
* pAllocator
,
811 struct anv_swapchain
**swapchain_out
)
813 struct x11_swapchain
*chain
;
814 xcb_void_cookie_t cookie
;
817 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
819 int num_images
= pCreateInfo
->minImageCount
;
821 /* For true mailbox mode, we need at least 4 images:
822 * 1) One to scan out from
823 * 2) One to have queued for scan-out
824 * 3) One to be currently held by the Wayland compositor
825 * 4) One to render to
827 if (pCreateInfo
->presentMode
== VK_PRESENT_MODE_MAILBOX_KHR
)
828 num_images
= MAX2(num_images
, 4);
830 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
831 chain
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
832 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
834 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
836 chain
->base
.device
= device
;
837 chain
->base
.destroy
= x11_swapchain_destroy
;
838 chain
->base
.get_images
= x11_get_images
;
839 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
840 chain
->base
.queue_present
= x11_queue_present
;
842 chain
->conn
= x11_surface_get_connection(icd_surface
);
843 chain
->window
= x11_surface_get_window(icd_surface
);
844 chain
->extent
= pCreateInfo
->imageExtent
;
845 chain
->image_count
= num_images
;
847 chain
->event_id
= xcb_generate_id(chain
->conn
);
848 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
849 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
850 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
851 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
853 /* Create an XCB event queue to hold present events outside of the usual
854 * application event queue
856 chain
->special_event
=
857 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
858 chain
->event_id
, NULL
);
860 chain
->gc
= xcb_generate_id(chain
->conn
);
862 /* FINISHME: Choose a better error. */
863 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
867 cookie
= xcb_create_gc(chain
->conn
,
870 XCB_GC_GRAPHICS_EXPOSURES
,
871 (uint32_t []) { 0 });
872 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
875 for (; image
< chain
->image_count
; image
++) {
876 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
877 &chain
->images
[image
]);
878 if (result
!= VK_SUCCESS
)
879 goto fail_init_images
;
882 *swapchain_out
= &chain
->base
;
887 for (uint32_t j
= 0; j
< image
; j
++)
888 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
891 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
893 anv_free2(&device
->alloc
, pAllocator
, chain
);
899 anv_x11_init_wsi(struct anv_physical_device
*device
)
904 wsi
= anv_alloc(&device
->instance
->alloc
, sizeof(*wsi
), 8,
905 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
907 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
911 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
914 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
916 /* FINISHME: Choose a better error. */
917 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
923 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
924 _mesa_key_pointer_equal
);
925 if (!wsi
->connections
) {
926 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
930 wsi
->base
.get_support
= x11_surface_get_support
;
931 wsi
->base
.get_capabilities
= x11_surface_get_capabilities
;
932 wsi
->base
.get_formats
= x11_surface_get_formats
;
933 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
934 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
936 device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
937 device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = &wsi
->base
;
942 pthread_mutex_destroy(&wsi
->mutex
);
944 anv_free(&device
->instance
->alloc
, wsi
);
946 device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
947 device
->wsi
[VK_ICD_WSI_PLATFORM_XLIB
] = NULL
;
953 anv_x11_finish_wsi(struct anv_physical_device
*device
)
955 struct wsi_x11
*wsi
=
956 (struct wsi_x11
*)device
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
959 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
961 pthread_mutex_destroy(&wsi
->mutex
);
963 anv_free(&device
->instance
->alloc
, wsi
);