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/xshmfence.h>
27 #include <xcb/present.h>
31 #include "util/hash_table.h"
33 struct wsi_x11_connection
{
39 struct anv_wsi_interface base
;
41 pthread_mutex_t mutex
;
42 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
43 struct hash_table
*connections
;
46 static struct wsi_x11_connection
*
47 wsi_x11_connection_create(struct anv_instance
*instance
, xcb_connection_t
*conn
)
49 xcb_query_extension_cookie_t dri3_cookie
, pres_cookie
;
50 xcb_query_extension_reply_t
*dri3_reply
, *pres_reply
;
52 struct wsi_x11_connection
*wsi_conn
=
53 anv_alloc(&instance
->alloc
, sizeof(*wsi_conn
), 8,
54 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
58 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
59 pres_cookie
= xcb_query_extension(conn
, 7, "PRESENT");
61 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
62 pres_reply
= xcb_query_extension_reply(conn
, pres_cookie
, NULL
);
63 if (dri3_reply
== NULL
|| pres_reply
== NULL
) {
66 anv_free(&instance
->alloc
, wsi_conn
);
70 wsi_conn
->has_dri3
= dri3_reply
->present
!= 0;
71 wsi_conn
->has_present
= pres_reply
->present
!= 0;
80 wsi_x11_connection_destroy(struct anv_instance
*instance
,
81 struct wsi_x11_connection
*conn
)
83 anv_free(&instance
->alloc
, conn
);
86 static struct wsi_x11_connection
*
87 wsi_x11_get_connection(struct anv_instance
*instance
, xcb_connection_t
*conn
)
90 (struct wsi_x11
*)instance
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
92 pthread_mutex_lock(&wsi
->mutex
);
94 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
96 /* We're about to make a bunch of blocking calls. Let's drop the
97 * mutex for now so we don't block up too badly.
99 pthread_mutex_unlock(&wsi
->mutex
);
101 struct wsi_x11_connection
*wsi_conn
=
102 wsi_x11_connection_create(instance
, conn
);
104 pthread_mutex_lock(&wsi
->mutex
);
106 entry
= _mesa_hash_table_search(wsi
->connections
, conn
);
108 /* Oops, someone raced us to it */
109 wsi_x11_connection_destroy(instance
, wsi_conn
);
111 entry
= _mesa_hash_table_insert(wsi
->connections
, conn
, wsi_conn
);
115 pthread_mutex_unlock(&wsi
->mutex
);
120 static const VkSurfaceFormatKHR formats
[] = {
121 { .format
= VK_FORMAT_B8G8R8A8_SRGB
, },
124 static const VkPresentModeKHR present_modes
[] = {
125 VK_PRESENT_MODE_MAILBOX_KHR
,
128 static xcb_screen_t
*
129 get_screen_for_root(xcb_connection_t
*conn
, xcb_window_t root
)
131 xcb_screen_iterator_t screen_iter
=
132 xcb_setup_roots_iterator(xcb_get_setup(conn
));
134 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
135 if (screen_iter
.data
->root
== root
)
136 return screen_iter
.data
;
142 static xcb_visualtype_t
*
143 screen_get_visualtype(xcb_screen_t
*screen
, xcb_visualid_t visual_id
,
146 xcb_depth_iterator_t depth_iter
=
147 xcb_screen_allowed_depths_iterator(screen
);
149 for (; depth_iter
.rem
; xcb_depth_next (&depth_iter
)) {
150 xcb_visualtype_iterator_t visual_iter
=
151 xcb_depth_visuals_iterator (depth_iter
.data
);
153 for (; visual_iter
.rem
; xcb_visualtype_next (&visual_iter
)) {
154 if (visual_iter
.data
->visual_id
== visual_id
) {
156 *depth
= depth_iter
.data
->depth
;
157 return visual_iter
.data
;
165 static xcb_visualtype_t
*
166 connection_get_visualtype(xcb_connection_t
*conn
, xcb_visualid_t visual_id
,
169 xcb_screen_iterator_t screen_iter
=
170 xcb_setup_roots_iterator(xcb_get_setup(conn
));
172 /* For this we have to iterate over all of the screens which is rather
173 * annoying. Fortunately, there is probably only 1.
175 for (; screen_iter
.rem
; xcb_screen_next (&screen_iter
)) {
176 xcb_visualtype_t
*visual
= screen_get_visualtype(screen_iter
.data
,
185 static xcb_visualtype_t
*
186 get_visualtype_for_window(xcb_connection_t
*conn
, xcb_window_t window
,
189 xcb_query_tree_cookie_t tree_cookie
;
190 xcb_get_window_attributes_cookie_t attrib_cookie
;
191 xcb_query_tree_reply_t
*tree
;
192 xcb_get_window_attributes_reply_t
*attrib
;
194 tree_cookie
= xcb_query_tree(conn
, window
);
195 attrib_cookie
= xcb_get_window_attributes(conn
, window
);
197 tree
= xcb_query_tree_reply(conn
, tree_cookie
, NULL
);
198 attrib
= xcb_get_window_attributes_reply(conn
, attrib_cookie
, NULL
);
199 if (attrib
== NULL
|| tree
== NULL
) {
205 xcb_window_t root
= tree
->root
;
206 xcb_visualid_t visual_id
= attrib
->visual
;
210 xcb_screen_t
*screen
= get_screen_for_root(conn
, root
);
214 return screen_get_visualtype(screen
, visual_id
, depth
);
218 visual_has_alpha(xcb_visualtype_t
*visual
, unsigned depth
)
220 uint32_t rgb_mask
= visual
->red_mask
|
224 uint32_t all_mask
= 0xffffffff >> (32 - depth
);
226 /* Do we have bits left over after RGB? */
227 return (all_mask
& ~rgb_mask
) != 0;
230 VkBool32
anv_GetPhysicalDeviceXcbPresentationSupportKHR(
231 VkPhysicalDevice physicalDevice
,
232 uint32_t queueFamilyIndex
,
233 xcb_connection_t
* connection
,
234 xcb_visualid_t visual_id
)
236 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
238 struct wsi_x11_connection
*wsi_conn
=
239 wsi_x11_get_connection(device
->instance
, connection
);
241 if (!wsi_conn
->has_dri3
) {
242 fprintf(stderr
, "vulkan: No DRI3 support\n");
246 unsigned visual_depth
;
247 if (!connection_get_visualtype(connection
, visual_id
, &visual_depth
))
250 if (visual_depth
!= 24 && visual_depth
!= 32)
257 x11_surface_get_support(VkIcdSurfaceBase
*icd_surface
,
258 struct anv_physical_device
*device
,
259 uint32_t queueFamilyIndex
,
260 VkBool32
* pSupported
)
262 VkIcdSurfaceXcb
*surface
= (VkIcdSurfaceXcb
*)icd_surface
;
264 struct wsi_x11_connection
*wsi_conn
=
265 wsi_x11_get_connection(device
->instance
, surface
->connection
);
267 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
269 if (!wsi_conn
->has_dri3
) {
270 fprintf(stderr
, "vulkan: No DRI3 support\n");
275 unsigned visual_depth
;
276 if (!get_visualtype_for_window(surface
->connection
, surface
->window
,
282 if (visual_depth
!= 24 && visual_depth
!= 32) {
292 x11_surface_get_capabilities(VkIcdSurfaceBase
*icd_surface
,
293 struct anv_physical_device
*device
,
294 VkSurfaceCapabilitiesKHR
*caps
)
296 VkIcdSurfaceXcb
*surface
= (VkIcdSurfaceXcb
*)icd_surface
;
297 xcb_get_geometry_cookie_t geom_cookie
;
298 xcb_generic_error_t
*err
;
299 xcb_get_geometry_reply_t
*geom
;
300 unsigned visual_depth
;
302 geom_cookie
= xcb_get_geometry(surface
->connection
, surface
->window
);
304 /* This does a round-trip. This is why we do get_geometry first and
305 * wait to read the reply until after we have a visual.
307 xcb_visualtype_t
*visual
=
308 get_visualtype_for_window(surface
->connection
, surface
->window
,
311 geom
= xcb_get_geometry_reply(surface
->connection
, geom_cookie
, &err
);
313 VkExtent2D extent
= { geom
->width
, geom
->height
};
314 caps
->currentExtent
= extent
;
315 caps
->minImageExtent
= extent
;
316 caps
->maxImageExtent
= extent
;
318 /* This can happen if the client didn't wait for the configure event
319 * to come back from the compositor. In that case, we don't know the
320 * size of the window so we just return valid "I don't know" stuff.
322 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
323 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
324 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
329 if (visual_has_alpha(visual
, visual_depth
)) {
330 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
331 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
333 caps
->supportedCompositeAlpha
= VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
|
334 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
337 caps
->minImageCount
= 2;
338 caps
->maxImageCount
= 4;
339 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
340 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
341 caps
->maxImageArrayLayers
= 1;
342 caps
->supportedUsageFlags
=
343 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
344 VK_IMAGE_USAGE_SAMPLED_BIT
|
345 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
346 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
352 x11_surface_get_formats(VkIcdSurfaceBase
*surface
,
353 struct anv_physical_device
*device
,
354 uint32_t *pSurfaceFormatCount
,
355 VkSurfaceFormatKHR
*pSurfaceFormats
)
357 if (pSurfaceFormats
== NULL
) {
358 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
362 assert(*pSurfaceFormatCount
>= ARRAY_SIZE(formats
));
363 typed_memcpy(pSurfaceFormats
, formats
, *pSurfaceFormatCount
);
364 *pSurfaceFormatCount
= ARRAY_SIZE(formats
);
370 x11_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
371 struct anv_physical_device
*device
,
372 uint32_t *pPresentModeCount
,
373 VkPresentModeKHR
*pPresentModes
)
375 if (pPresentModes
== NULL
) {
376 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
380 assert(*pPresentModeCount
>= ARRAY_SIZE(present_modes
));
381 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
382 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
388 x11_surface_create_swapchain(VkIcdSurfaceBase
*surface
,
389 struct anv_device
*device
,
390 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
391 const VkAllocationCallbacks
* pAllocator
,
392 struct anv_swapchain
**swapchain
);
394 VkResult
anv_CreateXcbSurfaceKHR(
395 VkInstance _instance
,
396 const VkXcbSurfaceCreateInfoKHR
* pCreateInfo
,
397 const VkAllocationCallbacks
* pAllocator
,
398 VkSurfaceKHR
* pSurface
)
400 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
402 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR
);
404 VkIcdSurfaceXcb
*surface
;
406 surface
= anv_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
407 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
409 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
411 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_XCB
;
412 surface
->connection
= pCreateInfo
->connection
;
413 surface
->window
= pCreateInfo
->window
;
415 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
421 struct anv_image
* image
;
422 struct anv_device_memory
* memory
;
425 struct xshmfence
* shm_fence
;
429 struct x11_swapchain
{
430 struct anv_swapchain base
;
432 xcb_connection_t
* conn
;
436 uint32_t image_count
;
438 xcb_present_event_t event_id
;
439 xcb_special_event_t
* special_event
;
443 struct x11_image images
[0];
447 x11_get_images(struct anv_swapchain
*anv_chain
,
448 uint32_t* pCount
, VkImage
*pSwapchainImages
)
450 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
452 if (pSwapchainImages
== NULL
) {
453 *pCount
= chain
->image_count
;
457 assert(chain
->image_count
<= *pCount
);
458 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
459 pSwapchainImages
[i
] = anv_image_to_handle(chain
->images
[i
].image
);
461 *pCount
= chain
->image_count
;
467 x11_handle_dri3_present_event(struct x11_swapchain
*chain
,
468 xcb_present_generic_event_t
*event
)
470 switch (event
->evtype
) {
471 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
472 xcb_present_configure_notify_event_t
*config
= (void *) event
;
474 if (config
->width
!= chain
->extent
.width
||
475 config
->height
!= chain
->extent
.height
)
476 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
481 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
482 xcb_present_idle_notify_event_t
*idle
= (void *) event
;
484 for (unsigned i
= 0; i
< chain
->image_count
; i
++) {
485 if (chain
->images
[i
].pixmap
== idle
->pixmap
) {
486 chain
->images
[i
].busy
= false;
494 case XCB_PRESENT_COMPLETE_NOTIFY
:
503 x11_acquire_next_image(struct anv_swapchain
*anv_chain
,
505 VkSemaphore semaphore
,
506 uint32_t *image_index
)
508 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
511 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
512 if (!chain
->images
[i
].busy
) {
513 /* We found a non-busy image */
514 xshmfence_await(chain
->images
[i
].shm_fence
);
520 xcb_flush(chain
->conn
);
521 xcb_generic_event_t
*event
=
522 xcb_wait_for_special_event(chain
->conn
, chain
->special_event
);
524 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
526 VkResult result
= x11_handle_dri3_present_event(chain
, (void *)event
);
528 if (result
!= VK_SUCCESS
)
534 x11_queue_present(struct anv_swapchain
*anv_chain
,
535 struct anv_queue
*queue
,
536 uint32_t image_index
)
538 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
539 struct x11_image
*image
= &chain
->images
[image_index
];
541 assert(image_index
< chain
->image_count
);
543 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
545 int64_t target_msc
= 0;
547 int64_t remainder
= 0;
549 options
|= XCB_PRESENT_OPTION_ASYNC
;
551 xshmfence_reset(image
->shm_fence
);
553 xcb_void_cookie_t cookie
=
554 xcb_present_pixmap(chain
->conn
,
557 (uint32_t) chain
->send_sbc
,
562 XCB_NONE
, /* target_crtc */
569 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
572 xcb_flush(chain
->conn
);
578 x11_image_init(struct anv_device
*device
, struct x11_swapchain
*chain
,
579 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
580 const VkAllocationCallbacks
* pAllocator
,
581 struct x11_image
*image
)
583 xcb_void_cookie_t cookie
;
587 result
= anv_image_create(anv_device_to_handle(device
),
588 &(struct anv_image_create_info
) {
589 .isl_tiling_flags
= ISL_TILING_X_BIT
,
592 &(VkImageCreateInfo
) {
593 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
594 .imageType
= VK_IMAGE_TYPE_2D
,
595 .format
= pCreateInfo
->imageFormat
,
597 .width
= pCreateInfo
->imageExtent
.width
,
598 .height
= pCreateInfo
->imageExtent
.height
,
604 /* FIXME: Need a way to use X tiling to allow scanout */
605 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
606 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
611 if (result
!= VK_SUCCESS
)
614 image
->image
= anv_image_from_handle(image_h
);
615 assert(anv_format_is_color(image
->image
->format
));
617 VkDeviceMemory memory_h
;
618 result
= anv_AllocateMemory(anv_device_to_handle(device
),
619 &(VkMemoryAllocateInfo
) {
620 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
621 .allocationSize
= image
->image
->size
,
622 .memoryTypeIndex
= 0,
624 NULL
/* XXX: pAllocator */,
626 if (result
!= VK_SUCCESS
)
627 goto fail_create_image
;
629 image
->memory
= anv_device_memory_from_handle(memory_h
);
630 image
->memory
->bo
.is_winsys_bo
= true;
632 anv_BindImageMemory(VK_NULL_HANDLE
, image_h
, memory_h
, 0);
634 struct anv_surface
*surface
= &image
->image
->color_surface
;
635 assert(surface
->isl
.tiling
== ISL_TILING_X
);
637 int ret
= anv_gem_set_tiling(device
, image
->memory
->bo
.gem_handle
,
638 surface
->isl
.row_pitch
, I915_TILING_X
);
640 /* FINISHME: Choose a better error. */
641 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
642 "set_tiling failed: %m");
643 goto fail_alloc_memory
;
646 int fd
= anv_gem_handle_to_fd(device
, image
->memory
->bo
.gem_handle
);
648 /* FINISHME: Choose a better error. */
649 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
650 "handle_to_fd failed: %m");
651 goto fail_alloc_memory
;
656 image
->pixmap
= xcb_generate_id(chain
->conn
);
659 xcb_dri3_pixmap_from_buffer_checked(chain
->conn
,
663 pCreateInfo
->imageExtent
.width
,
664 pCreateInfo
->imageExtent
.height
,
665 surface
->isl
.row_pitch
,
667 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
669 int fence_fd
= xshmfence_alloc_shm();
673 image
->shm_fence
= xshmfence_map_shm(fence_fd
);
674 if (image
->shm_fence
== NULL
)
675 goto fail_shmfence_alloc
;
677 image
->sync_fence
= xcb_generate_id(chain
->conn
);
678 xcb_dri3_fence_from_fd(chain
->conn
,
685 xshmfence_trigger(image
->shm_fence
);
693 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
694 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
697 anv_FreeMemory(anv_device_to_handle(chain
->base
.device
),
698 anv_device_memory_to_handle(image
->memory
), pAllocator
);
701 anv_DestroyImage(anv_device_to_handle(chain
->base
.device
),
702 anv_image_to_handle(image
->image
), pAllocator
);
708 x11_image_finish(struct x11_swapchain
*chain
,
709 const VkAllocationCallbacks
* pAllocator
,
710 struct x11_image
*image
)
712 xcb_void_cookie_t cookie
;
714 cookie
= xcb_sync_destroy_fence(chain
->conn
, image
->sync_fence
);
715 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
716 xshmfence_unmap_shm(image
->shm_fence
);
718 cookie
= xcb_free_pixmap(chain
->conn
, image
->pixmap
);
719 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
721 anv_DestroyImage(anv_device_to_handle(chain
->base
.device
),
722 anv_image_to_handle(image
->image
), pAllocator
);
724 anv_FreeMemory(anv_device_to_handle(chain
->base
.device
),
725 anv_device_memory_to_handle(image
->memory
), pAllocator
);
729 x11_swapchain_destroy(struct anv_swapchain
*anv_chain
,
730 const VkAllocationCallbacks
*pAllocator
)
732 struct x11_swapchain
*chain
= (struct x11_swapchain
*)anv_chain
;
734 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
735 x11_image_finish(chain
, pAllocator
, &chain
->images
[i
]);
737 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
739 anv_free2(&chain
->base
.device
->alloc
, pAllocator
, chain
);
745 x11_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
746 struct anv_device
*device
,
747 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
748 const VkAllocationCallbacks
* pAllocator
,
749 struct anv_swapchain
**swapchain_out
)
751 VkIcdSurfaceXcb
*surface
= (VkIcdSurfaceXcb
*)icd_surface
;
752 struct x11_swapchain
*chain
;
753 xcb_void_cookie_t cookie
;
756 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
758 int num_images
= pCreateInfo
->minImageCount
;
760 /* For true mailbox mode, we need at least 4 images:
761 * 1) One to scan out from
762 * 2) One to have queued for scan-out
763 * 3) One to be currently held by the Wayland compositor
764 * 4) One to render to
766 if (pCreateInfo
->presentMode
== VK_PRESENT_MODE_MAILBOX_KHR
)
767 num_images
= MAX2(num_images
, 4);
769 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
770 chain
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
771 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
773 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
775 chain
->base
.device
= device
;
776 chain
->base
.destroy
= x11_swapchain_destroy
;
777 chain
->base
.get_images
= x11_get_images
;
778 chain
->base
.acquire_next_image
= x11_acquire_next_image
;
779 chain
->base
.queue_present
= x11_queue_present
;
781 chain
->conn
= surface
->connection
;
782 chain
->window
= surface
->window
;
783 chain
->extent
= pCreateInfo
->imageExtent
;
784 chain
->image_count
= num_images
;
786 chain
->event_id
= xcb_generate_id(chain
->conn
);
787 xcb_present_select_input(chain
->conn
, chain
->event_id
, chain
->window
,
788 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
789 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
790 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
792 /* Create an XCB event queue to hold present events outside of the usual
793 * application event queue
795 chain
->special_event
=
796 xcb_register_for_special_xge(chain
->conn
, &xcb_present_id
,
797 chain
->event_id
, NULL
);
799 chain
->gc
= xcb_generate_id(chain
->conn
);
801 /* FINISHME: Choose a better error. */
802 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
806 cookie
= xcb_create_gc(chain
->conn
,
809 XCB_GC_GRAPHICS_EXPOSURES
,
810 (uint32_t []) { 0 });
811 xcb_discard_reply(chain
->conn
, cookie
.sequence
);
814 for (; image
< chain
->image_count
; image
++) {
815 result
= x11_image_init(device
, chain
, pCreateInfo
, pAllocator
,
816 &chain
->images
[image
]);
817 if (result
!= VK_SUCCESS
)
818 goto fail_init_images
;
821 *swapchain_out
= &chain
->base
;
826 for (uint32_t j
= 0; j
< image
; j
++)
827 x11_image_finish(chain
, pAllocator
, &chain
->images
[j
]);
830 xcb_unregister_for_special_event(chain
->conn
, chain
->special_event
);
832 anv_free2(&device
->alloc
, pAllocator
, chain
);
838 anv_x11_init_wsi(struct anv_instance
*instance
)
843 wsi
= anv_alloc(&instance
->alloc
, sizeof(*wsi
), 8,
844 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
846 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
850 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
853 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
855 /* FINISHME: Choose a better error. */
856 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
862 wsi
->connections
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
863 _mesa_key_pointer_equal
);
864 if (!wsi
->connections
) {
865 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
869 wsi
->base
.get_support
= x11_surface_get_support
;
870 wsi
->base
.get_capabilities
= x11_surface_get_capabilities
;
871 wsi
->base
.get_formats
= x11_surface_get_formats
;
872 wsi
->base
.get_present_modes
= x11_surface_get_present_modes
;
873 wsi
->base
.create_swapchain
= x11_surface_create_swapchain
;
875 instance
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = &wsi
->base
;
880 pthread_mutex_destroy(&wsi
->mutex
);
882 anv_free(&instance
->alloc
, wsi
);
884 instance
->wsi
[VK_ICD_WSI_PLATFORM_XCB
] = NULL
;
890 anv_x11_finish_wsi(struct anv_instance
*instance
)
892 struct wsi_x11
*wsi
=
893 (struct wsi_x11
*)instance
->wsi
[VK_ICD_WSI_PLATFORM_XCB
];
896 _mesa_hash_table_destroy(wsi
->connections
, NULL
);
898 pthread_mutex_destroy(&wsi
->mutex
);
900 anv_free(&instance
->alloc
, wsi
);