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 <wayland-client.h>
25 #include <wayland-drm-client-protocol.h>
29 #include <util/hash_table.h>
31 #define MIN_NUM_IMAGES 2
33 struct wsi_wl_display
{
34 struct wl_display
* display
;
37 /* Vector of VkFormats supported */
38 struct anv_vector formats
;
40 uint32_t capabilities
;
44 struct anv_wsi_interface base
;
46 struct anv_instance
* instance
;
48 pthread_mutex_t mutex
;
49 /* Hash table of wl_display -> wsi_wl_display mappings */
50 struct hash_table
* displays
;
54 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
, VkFormat format
)
56 /* Don't add a format that's already in the list */
58 anv_vector_foreach(f
, &display
->formats
)
62 /* Don't add formats which aren't supported by the driver */
63 if (anv_format_for_vk_format(format
)->isl_format
==
64 ISL_FORMAT_UNSUPPORTED
) {
68 f
= anv_vector_add(&display
->formats
);
74 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
76 fprintf(stderr
, "wl_drm.device(%s)\n", name
);
80 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
83 /* TODO: Figure out what all the formats mean and make this table
87 case VK_FORMAT_R4G4B4A4_UNORM
:
88 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
89 case VK_FORMAT_R5G6B5_UNORM
:
90 return WL_DRM_FORMAT_BGR565
;
91 case VK_FORMAT_R5G5B5A1_UNORM
:
92 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
93 case VK_FORMAT_R8G8B8_UNORM
:
94 return WL_DRM_FORMAT_XBGR8888
;
95 case VK_FORMAT_R8G8B8A8_UNORM
:
96 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
97 case VK_FORMAT_R10G10B10A2_UNORM
:
98 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
99 case VK_FORMAT_B4G4R4A4_UNORM
:
100 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
101 case VK_FORMAT_B5G6R5_UNORM
:
102 return WL_DRM_FORMAT_RGB565
;
103 case VK_FORMAT_B5G5R5A1_UNORM
:
104 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
106 case VK_FORMAT_B8G8R8_SRGB
:
107 return WL_DRM_FORMAT_BGRX8888
;
108 case VK_FORMAT_B8G8R8A8_SRGB
:
109 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
111 case VK_FORMAT_B10G10R10A2_UNORM
:
112 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
116 assert("!Unsupported Vulkan format");
122 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
124 struct wsi_wl_display
*display
= data
;
128 case WL_DRM_FORMAT_ABGR4444
:
129 case WL_DRM_FORMAT_XBGR4444
:
130 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R4G4B4A4_UNORM
);
132 case WL_DRM_FORMAT_BGR565
:
133 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G6B5_UNORM
);
135 case WL_DRM_FORMAT_ABGR1555
:
136 case WL_DRM_FORMAT_XBGR1555
:
137 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G5B5A1_UNORM
);
139 case WL_DRM_FORMAT_XBGR8888
:
140 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8_UNORM
);
142 case WL_DRM_FORMAT_ABGR8888
:
143 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8A8_UNORM
);
145 case WL_DRM_FORMAT_ABGR2101010
:
146 case WL_DRM_FORMAT_XBGR2101010
:
147 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R10G10B10A2_UNORM
);
149 case WL_DRM_FORMAT_ARGB4444
:
150 case WL_DRM_FORMAT_XRGB4444
:
151 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B4G4R4A4_UNORM
);
153 case WL_DRM_FORMAT_RGB565
:
154 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G6R5_UNORM
);
156 case WL_DRM_FORMAT_ARGB1555
:
157 case WL_DRM_FORMAT_XRGB1555
:
158 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G5R5A1_UNORM
);
161 case WL_DRM_FORMAT_XRGB8888
:
162 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_SRGB
);
164 case WL_DRM_FORMAT_ARGB8888
:
165 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_SRGB
);
168 case WL_DRM_FORMAT_ARGB2101010
:
169 case WL_DRM_FORMAT_XRGB2101010
:
170 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B10G10R10A2_UNORM
);
177 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
182 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
184 struct wsi_wl_display
*display
= data
;
186 display
->capabilities
= capabilities
;
189 static const struct wl_drm_listener drm_listener
= {
192 drm_handle_authenticated
,
193 drm_handle_capabilities
,
197 registry_handle_global(void *data
, struct wl_registry
*registry
,
198 uint32_t name
, const char *interface
, uint32_t version
)
200 struct wsi_wl_display
*display
= data
;
202 if (strcmp(interface
, "wl_drm") == 0) {
203 assert(display
->drm
== NULL
);
205 assert(version
>= 2);
206 display
->drm
= wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
209 wl_drm_add_listener(display
->drm
, &drm_listener
, display
);
214 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
218 static const struct wl_registry_listener registry_listener
= {
219 registry_handle_global
,
220 registry_handle_global_remove
224 wsi_wl_display_destroy(struct wsi_wayland
*wsi
, struct wsi_wl_display
*display
)
226 anv_vector_finish(&display
->formats
);
228 wl_drm_destroy(display
->drm
);
229 anv_free(&wsi
->instance
->alloc
, display
);
232 static struct wsi_wl_display
*
233 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
)
235 struct wsi_wl_display
*display
=
236 anv_alloc(&wsi
->instance
->alloc
, sizeof(*display
), 8,
237 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
241 memset(display
, 0, sizeof(*display
));
243 display
->display
= wl_display
;
245 if (!anv_vector_init(&display
->formats
, sizeof(VkFormat
), 8))
248 struct wl_registry
*registry
= wl_display_get_registry(wl_display
);
252 wl_registry_add_listener(registry
, ®istry_listener
, display
);
254 /* Round-rip to get the wl_drm global */
255 wl_display_roundtrip(wl_display
);
260 /* Round-rip to get wl_drm formats and capabilities */
261 wl_display_roundtrip(wl_display
);
263 /* We need prime support */
264 if (!(display
->capabilities
& WL_DRM_CAPABILITY_PRIME
))
267 /* We don't need this anymore */
268 wl_registry_destroy(registry
);
274 wl_registry_destroy(registry
);
276 wsi_wl_display_destroy(wsi
, display
);
280 static struct wsi_wl_display
*
281 wsi_wl_get_display(struct anv_instance
*instance
, struct wl_display
*wl_display
)
283 struct wsi_wayland
*wsi
=
284 (struct wsi_wayland
*)instance
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
286 pthread_mutex_lock(&wsi
->mutex
);
288 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->displays
,
291 /* We're about to make a bunch of blocking calls. Let's drop the
292 * mutex for now so we don't block up too badly.
294 pthread_mutex_unlock(&wsi
->mutex
);
296 struct wsi_wl_display
*display
= wsi_wl_display_create(wsi
, wl_display
);
298 pthread_mutex_lock(&wsi
->mutex
);
300 entry
= _mesa_hash_table_search(wsi
->displays
, wl_display
);
302 /* Oops, someone raced us to it */
303 wsi_wl_display_destroy(wsi
, display
);
305 entry
= _mesa_hash_table_insert(wsi
->displays
, wl_display
, display
);
309 pthread_mutex_unlock(&wsi
->mutex
);
314 VkBool32
anv_GetPhysicalDeviceWaylandPresentationSupportKHR(
315 VkPhysicalDevice physicalDevice
,
316 uint32_t queueFamilyIndex
,
317 struct wl_display
* display
)
319 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
321 return wsi_wl_get_display(physical_device
->instance
, display
) != NULL
;
325 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
326 struct anv_physical_device
*device
,
327 uint32_t queueFamilyIndex
,
328 VkBool32
* pSupported
)
335 static const VkPresentModeKHR present_modes
[] = {
336 VK_PRESENT_MODE_MAILBOX_KHR
,
337 VK_PRESENT_MODE_FIFO_KHR
,
341 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
342 struct anv_physical_device
*device
,
343 VkSurfaceCapabilitiesKHR
* caps
)
345 caps
->minImageCount
= MIN_NUM_IMAGES
;
346 caps
->maxImageCount
= 4;
347 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
348 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
349 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
350 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
351 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
352 caps
->maxImageArrayLayers
= 1;
354 caps
->supportedCompositeAlpha
=
355 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
356 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
358 caps
->supportedUsageFlags
=
359 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
360 VK_IMAGE_USAGE_SAMPLED_BIT
|
361 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
362 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
368 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
369 struct anv_physical_device
*device
,
370 uint32_t* pSurfaceFormatCount
,
371 VkSurfaceFormatKHR
* pSurfaceFormats
)
373 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
374 struct wsi_wl_display
*display
=
375 wsi_wl_get_display(device
->instance
, surface
->display
);
377 uint32_t count
= anv_vector_length(&display
->formats
);
379 if (pSurfaceFormats
== NULL
) {
380 *pSurfaceFormatCount
= count
;
384 assert(*pSurfaceFormatCount
>= count
);
385 *pSurfaceFormatCount
= count
;
388 anv_vector_foreach(f
, &display
->formats
) {
389 *(pSurfaceFormats
++) = (VkSurfaceFormatKHR
) {
391 /* TODO: We should get this from the compositor somehow */
392 .colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
,
400 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
401 struct anv_physical_device
*device
,
402 uint32_t* pPresentModeCount
,
403 VkPresentModeKHR
* pPresentModes
)
405 if (pPresentModes
== NULL
) {
406 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
410 assert(*pPresentModeCount
>= ARRAY_SIZE(present_modes
));
411 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
412 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
418 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*surface
,
419 struct anv_device
*device
,
420 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
421 const VkAllocationCallbacks
* pAllocator
,
422 struct anv_swapchain
**swapchain
);
424 VkResult
anv_CreateWaylandSurfaceKHR(
425 VkInstance _instance
,
426 const VkWaylandSurfaceCreateInfoKHR
* pCreateInfo
,
427 const VkAllocationCallbacks
* pAllocator
,
428 VkSurfaceKHR
* pSurface
)
430 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
432 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR
);
434 VkIcdSurfaceWayland
*surface
;
436 surface
= anv_alloc2(&instance
->alloc
, pAllocator
, sizeof *surface
, 8,
437 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
439 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
441 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
442 surface
->display
= pCreateInfo
->display
;
443 surface
->surface
= pCreateInfo
->surface
;
445 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
450 struct wsi_wl_image
{
451 struct anv_image
* image
;
452 struct anv_device_memory
* memory
;
453 struct wl_buffer
* buffer
;
457 struct wsi_wl_swapchain
{
458 struct anv_swapchain base
;
460 struct wsi_wl_display
* display
;
461 struct wl_event_queue
* queue
;
462 struct wl_surface
* surface
;
468 VkPresentModeKHR present_mode
;
471 uint32_t image_count
;
472 struct wsi_wl_image images
[0];
476 wsi_wl_swapchain_get_images(struct anv_swapchain
*anv_chain
,
477 uint32_t *pCount
, VkImage
*pSwapchainImages
)
479 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
481 if (pSwapchainImages
== NULL
) {
482 *pCount
= chain
->image_count
;
486 assert(chain
->image_count
<= *pCount
);
487 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
488 pSwapchainImages
[i
] = anv_image_to_handle(chain
->images
[i
].image
);
490 *pCount
= chain
->image_count
;
496 wsi_wl_swapchain_acquire_next_image(struct anv_swapchain
*anv_chain
,
498 VkSemaphore semaphore
,
499 uint32_t *image_index
)
501 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
503 int ret
= wl_display_dispatch_queue_pending(chain
->display
->display
,
505 /* XXX: I'm not sure if out-of-date is the right error here. If
506 * wl_display_dispatch_queue_pending fails it most likely means we got
507 * kicked by the server so this seems more-or-less correct.
510 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
513 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
514 if (!chain
->images
[i
].busy
) {
515 /* We found a non-busy image */
521 /* This time we do a blocking dispatch because we can't go
522 * anywhere until we get an event.
524 int ret
= wl_display_roundtrip_queue(chain
->display
->display
,
527 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
532 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
534 struct wsi_wl_swapchain
*chain
= data
;
536 chain
->fifo_ready
= true;
538 wl_callback_destroy(callback
);
541 static const struct wl_callback_listener frame_listener
= {
546 wsi_wl_swapchain_queue_present(struct anv_swapchain
*anv_chain
,
547 struct anv_queue
*queue
,
548 uint32_t image_index
)
550 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
552 if (chain
->present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
553 while (!chain
->fifo_ready
) {
554 int ret
= wl_display_dispatch_queue(chain
->display
->display
,
557 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
561 assert(image_index
< chain
->image_count
);
562 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
563 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
565 if (chain
->present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
566 struct wl_callback
*frame
= wl_surface_frame(chain
->surface
);
567 wl_proxy_set_queue((struct wl_proxy
*)frame
, chain
->queue
);
568 wl_callback_add_listener(frame
, &frame_listener
, chain
);
569 chain
->fifo_ready
= false;
572 chain
->images
[image_index
].busy
= true;
573 wl_surface_commit(chain
->surface
);
574 wl_display_flush(chain
->display
->display
);
580 wsi_wl_image_finish(struct wsi_wl_swapchain
*chain
, struct wsi_wl_image
*image
,
581 const VkAllocationCallbacks
* pAllocator
)
583 VkDevice vk_device
= anv_device_to_handle(chain
->base
.device
);
584 anv_FreeMemory(vk_device
, anv_device_memory_to_handle(image
->memory
),
586 anv_DestroyImage(vk_device
, anv_image_to_handle(image
->image
),
591 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
593 struct wsi_wl_image
*image
= data
;
595 assert(image
->buffer
== buffer
);
600 static const struct wl_buffer_listener buffer_listener
= {
601 buffer_handle_release
,
605 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
, struct wsi_wl_image
*image
,
606 const VkAllocationCallbacks
* pAllocator
)
608 VkDevice vk_device
= anv_device_to_handle(chain
->base
.device
);
612 result
= anv_image_create(vk_device
,
613 &(struct anv_image_create_info
) {
614 .isl_tiling_flags
= ISL_TILING_X_BIT
,
617 &(VkImageCreateInfo
) {
618 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
619 .imageType
= VK_IMAGE_TYPE_2D
,
620 .format
= chain
->vk_format
,
622 .width
= chain
->extent
.width
,
623 .height
= chain
->extent
.height
,
629 /* FIXME: Need a way to use X tiling to allow scanout */
630 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
631 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
637 if (result
!= VK_SUCCESS
)
640 image
->image
= anv_image_from_handle(vk_image
);
641 assert(anv_format_is_color(image
->image
->format
));
643 struct anv_surface
*surface
= &image
->image
->color_surface
;
645 VkDeviceMemory vk_memory
;
646 result
= anv_AllocateMemory(vk_device
,
647 &(VkMemoryAllocateInfo
) {
648 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
649 .allocationSize
= image
->image
->size
,
650 .memoryTypeIndex
= 0,
655 if (result
!= VK_SUCCESS
)
658 image
->memory
= anv_device_memory_from_handle(vk_memory
);
659 image
->memory
->bo
.is_winsys_bo
= true;
661 result
= anv_BindImageMemory(vk_device
, vk_image
, vk_memory
, 0);
663 if (result
!= VK_SUCCESS
)
666 int ret
= anv_gem_set_tiling(chain
->base
.device
,
667 image
->memory
->bo
.gem_handle
,
668 surface
->isl
.row_pitch
, I915_TILING_X
);
670 /* FINISHME: Choose a better error. */
671 result
= vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
675 int fd
= anv_gem_handle_to_fd(chain
->base
.device
,
676 image
->memory
->bo
.gem_handle
);
678 /* FINISHME: Choose a better error. */
679 result
= vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
683 image
->buffer
= wl_drm_create_prime_buffer(chain
->display
->drm
,
686 chain
->extent
.height
,
689 surface
->isl
.row_pitch
,
690 0, 0, 0, 0 /* unused */);
691 wl_display_roundtrip(chain
->display
->display
);
694 wl_proxy_set_queue((struct wl_proxy
*)image
->buffer
, chain
->queue
);
695 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
700 anv_FreeMemory(vk_device
, vk_memory
, pAllocator
);
702 anv_DestroyImage(vk_device
, vk_image
, pAllocator
);
708 wsi_wl_swapchain_destroy(struct anv_swapchain
*anv_chain
,
709 const VkAllocationCallbacks
*pAllocator
)
711 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
713 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
714 if (chain
->images
[i
].buffer
)
715 wsi_wl_image_finish(chain
, &chain
->images
[i
], pAllocator
);
718 anv_free2(&chain
->base
.device
->alloc
, pAllocator
, chain
);
724 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
725 struct anv_device
*device
,
726 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
727 const VkAllocationCallbacks
* pAllocator
,
728 struct anv_swapchain
**swapchain_out
)
730 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
731 struct wsi_wl_swapchain
*chain
;
734 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
736 int num_images
= pCreateInfo
->minImageCount
;
738 assert(num_images
>= MIN_NUM_IMAGES
);
740 /* For true mailbox mode, we need at least 4 images:
741 * 1) One to scan out from
742 * 2) One to have queued for scan-out
743 * 3) One to be currently held by the Wayland compositor
744 * 4) One to render to
746 if (pCreateInfo
->presentMode
== VK_PRESENT_MODE_MAILBOX_KHR
)
747 num_images
= MAX2(num_images
, 4);
749 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
750 chain
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
751 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
753 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
755 chain
->base
.device
= device
;
756 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
757 chain
->base
.get_images
= wsi_wl_swapchain_get_images
;
758 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
759 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
761 chain
->surface
= surface
->surface
;
762 chain
->extent
= pCreateInfo
->imageExtent
;
763 chain
->vk_format
= pCreateInfo
->imageFormat
;
764 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, false);
766 chain
->present_mode
= pCreateInfo
->presentMode
;
767 chain
->fifo_ready
= true;
769 chain
->image_count
= num_images
;
771 /* Mark a bunch of stuff as NULL. This way we can just call
772 * destroy_swapchain for cleanup.
774 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
775 chain
->images
[i
].buffer
= NULL
;
778 chain
->display
= wsi_wl_get_display(device
->instance
, surface
->display
);
782 chain
->queue
= wl_display_create_queue(chain
->display
->display
);
786 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
787 result
= wsi_wl_image_init(chain
, &chain
->images
[i
], pAllocator
);
788 if (result
!= VK_SUCCESS
)
790 chain
->images
[i
].busy
= false;
793 *swapchain_out
= &chain
->base
;
798 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
804 anv_wl_init_wsi(struct anv_instance
*instance
)
806 struct wsi_wayland
*wsi
;
809 wsi
= anv_alloc(&instance
->alloc
, sizeof(*wsi
), 8,
810 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
812 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
816 wsi
->instance
= instance
;
818 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
821 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
823 /* FINISHME: Choose a better error. */
824 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
830 wsi
->displays
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
831 _mesa_key_pointer_equal
);
832 if (!wsi
->displays
) {
833 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
837 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
838 wsi
->base
.get_capabilities
= wsi_wl_surface_get_capabilities
;
839 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
840 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
841 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
843 instance
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
848 pthread_mutex_destroy(&wsi
->mutex
);
851 anv_free(&instance
->alloc
, wsi
);
853 instance
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
859 anv_wl_finish_wsi(struct anv_instance
*instance
)
861 struct wsi_wayland
*wsi
=
862 (struct wsi_wayland
*)instance
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
865 _mesa_hash_table_destroy(wsi
->displays
, NULL
);
867 pthread_mutex_destroy(&wsi
->mutex
);
869 anv_free(&instance
->alloc
, wsi
);