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>
34 #include "util/vk_util.h"
35 #include "wsi_common_wayland.h"
36 #include "wayland-drm-client-protocol.h"
38 #include <util/hash_table.h>
39 #include <util/u_vector.h>
41 #define typed_memcpy(dest, src, count) ({ \
42 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
43 memcpy((dest), (src), (count) * sizeof(*(src))); \
48 struct wsi_wl_display
{
49 struct wl_display
* display
;
52 struct wsi_wayland
*wsi_wl
;
53 /* Vector of VkFormats supported */
54 struct u_vector formats
;
56 uint32_t capabilities
;
60 struct wsi_interface base
;
62 const VkAllocationCallbacks
*alloc
;
63 VkPhysicalDevice physical_device
;
65 pthread_mutex_t mutex
;
66 /* Hash table of wl_display -> wsi_wl_display mappings */
67 struct hash_table
* displays
;
69 const struct wsi_callbacks
*cbs
;
73 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
, VkFormat format
)
75 /* Don't add a format that's already in the list */
77 u_vector_foreach(f
, &display
->formats
)
81 /* Don't add formats that aren't renderable. */
82 VkFormatProperties props
;
84 display
->wsi_wl
->cbs
->get_phys_device_format_properties(display
->wsi_wl
->physical_device
,
86 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
89 f
= u_vector_add(&display
->formats
);
95 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
97 fprintf(stderr
, "wl_drm.device(%s)\n", name
);
101 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
104 /* TODO: Figure out what all the formats mean and make this table
108 case VK_FORMAT_R4G4B4A4_UNORM
:
109 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
110 case VK_FORMAT_R5G6B5_UNORM
:
111 return WL_DRM_FORMAT_BGR565
;
112 case VK_FORMAT_R5G5B5A1_UNORM
:
113 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
114 case VK_FORMAT_R8G8B8_UNORM
:
115 return WL_DRM_FORMAT_XBGR8888
;
116 case VK_FORMAT_R8G8B8A8_UNORM
:
117 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
118 case VK_FORMAT_R10G10B10A2_UNORM
:
119 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
120 case VK_FORMAT_B4G4R4A4_UNORM
:
121 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
122 case VK_FORMAT_B5G6R5_UNORM
:
123 return WL_DRM_FORMAT_RGB565
;
124 case VK_FORMAT_B5G5R5A1_UNORM
:
125 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
127 case VK_FORMAT_B8G8R8_UNORM
:
128 case VK_FORMAT_B8G8R8_SRGB
:
129 return WL_DRM_FORMAT_BGRX8888
;
130 case VK_FORMAT_B8G8R8A8_UNORM
:
131 case VK_FORMAT_B8G8R8A8_SRGB
:
132 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
134 case VK_FORMAT_B10G10R10A2_UNORM
:
135 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
139 assert(!"Unsupported Vulkan format");
145 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
147 struct wsi_wl_display
*display
= data
;
151 case WL_DRM_FORMAT_ABGR4444
:
152 case WL_DRM_FORMAT_XBGR4444
:
153 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R4G4B4A4_UNORM
);
155 case WL_DRM_FORMAT_BGR565
:
156 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G6B5_UNORM
);
158 case WL_DRM_FORMAT_ABGR1555
:
159 case WL_DRM_FORMAT_XBGR1555
:
160 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G5B5A1_UNORM
);
162 case WL_DRM_FORMAT_XBGR8888
:
163 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8_UNORM
);
165 case WL_DRM_FORMAT_ABGR8888
:
166 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8A8_UNORM
);
168 case WL_DRM_FORMAT_ABGR2101010
:
169 case WL_DRM_FORMAT_XBGR2101010
:
170 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R10G10B10A2_UNORM
);
172 case WL_DRM_FORMAT_ARGB4444
:
173 case WL_DRM_FORMAT_XRGB4444
:
174 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B4G4R4A4_UNORM
);
176 case WL_DRM_FORMAT_RGB565
:
177 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G6R5_UNORM
);
179 case WL_DRM_FORMAT_ARGB1555
:
180 case WL_DRM_FORMAT_XRGB1555
:
181 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G5R5A1_UNORM
);
184 case WL_DRM_FORMAT_XRGB8888
:
185 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_SRGB
);
186 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_UNORM
);
188 case WL_DRM_FORMAT_ARGB8888
:
189 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_SRGB
);
190 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_UNORM
);
193 case WL_DRM_FORMAT_ARGB2101010
:
194 case WL_DRM_FORMAT_XRGB2101010
:
195 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B10G10R10A2_UNORM
);
202 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
207 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
209 struct wsi_wl_display
*display
= data
;
211 display
->capabilities
= capabilities
;
214 static const struct wl_drm_listener drm_listener
= {
217 drm_handle_authenticated
,
218 drm_handle_capabilities
,
222 registry_handle_global(void *data
, struct wl_registry
*registry
,
223 uint32_t name
, const char *interface
, uint32_t version
)
225 struct wsi_wl_display
*display
= data
;
227 if (strcmp(interface
, "wl_drm") == 0) {
228 assert(display
->drm
== NULL
);
230 assert(version
>= 2);
231 display
->drm
= wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
234 wl_drm_add_listener(display
->drm
, &drm_listener
, display
);
239 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
243 static const struct wl_registry_listener registry_listener
= {
244 registry_handle_global
,
245 registry_handle_global_remove
249 wsi_wl_display_destroy(struct wsi_wayland
*wsi
, struct wsi_wl_display
*display
)
251 u_vector_finish(&display
->formats
);
253 wl_drm_destroy(display
->drm
);
254 vk_free(wsi
->alloc
, display
);
257 static struct wsi_wl_display
*
258 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
)
260 struct wsi_wl_display
*display
=
261 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
262 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
266 memset(display
, 0, sizeof(*display
));
268 display
->display
= wl_display
;
269 display
->wsi_wl
= wsi
;
271 if (!u_vector_init(&display
->formats
, sizeof(VkFormat
), 8))
274 struct wl_registry
*registry
= wl_display_get_registry(wl_display
);
278 wl_registry_add_listener(registry
, ®istry_listener
, display
);
280 /* Round-rip to get the wl_drm global */
281 wl_display_roundtrip(wl_display
);
286 /* Round-rip to get wl_drm formats and capabilities */
287 wl_display_roundtrip(wl_display
);
289 /* We need prime support */
290 if (!(display
->capabilities
& WL_DRM_CAPABILITY_PRIME
))
293 /* We don't need this anymore */
294 wl_registry_destroy(registry
);
300 wl_registry_destroy(registry
);
302 wsi_wl_display_destroy(wsi
, display
);
306 static struct wsi_wl_display
*
307 wsi_wl_get_display(struct wsi_device
*wsi_device
,
308 struct wl_display
*wl_display
)
310 struct wsi_wayland
*wsi
=
311 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
313 pthread_mutex_lock(&wsi
->mutex
);
315 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->displays
,
318 /* We're about to make a bunch of blocking calls. Let's drop the
319 * mutex for now so we don't block up too badly.
321 pthread_mutex_unlock(&wsi
->mutex
);
323 struct wsi_wl_display
*display
= wsi_wl_display_create(wsi
, wl_display
);
327 pthread_mutex_lock(&wsi
->mutex
);
329 entry
= _mesa_hash_table_search(wsi
->displays
, wl_display
);
331 /* Oops, someone raced us to it */
332 wsi_wl_display_destroy(wsi
, display
);
334 entry
= _mesa_hash_table_insert(wsi
->displays
, wl_display
, display
);
338 pthread_mutex_unlock(&wsi
->mutex
);
344 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
345 struct wl_display
*wl_display
)
347 return wsi_wl_get_display(wsi_device
, wl_display
) != NULL
;
351 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
352 struct wsi_device
*wsi_device
,
353 const VkAllocationCallbacks
*alloc
,
354 uint32_t queueFamilyIndex
,
356 bool can_handle_different_gpu
,
357 VkBool32
* pSupported
)
364 static const VkPresentModeKHR present_modes
[] = {
365 VK_PRESENT_MODE_MAILBOX_KHR
,
366 VK_PRESENT_MODE_FIFO_KHR
,
370 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
371 VkSurfaceCapabilitiesKHR
* caps
)
373 /* For true mailbox mode, we need at least 4 images:
374 * 1) One to scan out from
375 * 2) One to have queued for scan-out
376 * 3) One to be currently held by the Wayland compositor
377 * 4) One to render to
379 caps
->minImageCount
= 4;
380 /* There is no real maximum */
381 caps
->maxImageCount
= 0;
383 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
384 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
385 /* This is the maximum supported size on Intel */
386 caps
->maxImageExtent
= (VkExtent2D
) { 1 << 14, 1 << 14 };
387 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
388 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
389 caps
->maxImageArrayLayers
= 1;
391 caps
->supportedCompositeAlpha
=
392 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
393 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
395 caps
->supportedUsageFlags
=
396 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
397 VK_IMAGE_USAGE_SAMPLED_BIT
|
398 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
399 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
405 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
406 struct wsi_device
*wsi_device
,
407 uint32_t* pSurfaceFormatCount
,
408 VkSurfaceFormatKHR
* pSurfaceFormats
)
410 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
411 struct wsi_wl_display
*display
=
412 wsi_wl_get_display(wsi_device
, surface
->display
);
414 return VK_ERROR_OUT_OF_HOST_MEMORY
;
416 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
419 u_vector_foreach(disp_fmt
, &display
->formats
) {
420 vk_outarray_append(&out
, out_fmt
) {
421 out_fmt
->format
= *disp_fmt
;
422 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
426 return vk_outarray_status(&out
);
430 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
431 uint32_t* pPresentModeCount
,
432 VkPresentModeKHR
* pPresentModes
)
434 if (pPresentModes
== NULL
) {
435 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
439 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
440 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
442 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
443 return VK_INCOMPLETE
;
448 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
449 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
450 VkSurfaceKHR
*pSurface
)
452 VkIcdSurfaceWayland
*surface
;
454 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
455 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
457 return VK_ERROR_OUT_OF_HOST_MEMORY
;
459 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
460 surface
->display
= pCreateInfo
->display
;
461 surface
->surface
= pCreateInfo
->surface
;
463 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
468 struct wsi_wl_image
{
470 VkDeviceMemory memory
;
471 struct wl_buffer
* buffer
;
475 struct wsi_wl_swapchain
{
476 struct wsi_swapchain base
;
478 struct wsi_wl_display
* display
;
479 struct wl_event_queue
* queue
;
480 struct wl_surface
* surface
;
481 uint32_t surface_version
;
487 VkPresentModeKHR present_mode
;
490 struct wsi_wl_image images
[0];
494 wsi_wl_swapchain_get_images(struct wsi_swapchain
*wsi_chain
,
495 uint32_t *pCount
, VkImage
*pSwapchainImages
)
497 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
501 if (pSwapchainImages
== NULL
) {
502 *pCount
= chain
->base
.image_count
;
507 ret_count
= chain
->base
.image_count
;
508 if (chain
->base
.image_count
> *pCount
) {
510 result
= VK_INCOMPLETE
;
513 for (uint32_t i
= 0; i
< ret_count
; i
++)
514 pSwapchainImages
[i
] = chain
->images
[i
].image
;
520 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
522 VkSemaphore semaphore
,
523 uint32_t *image_index
)
525 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
527 int ret
= wl_display_dispatch_queue_pending(chain
->display
->display
,
529 /* XXX: I'm not sure if out-of-date is the right error here. If
530 * wl_display_dispatch_queue_pending fails it most likely means we got
531 * kicked by the server so this seems more-or-less correct.
534 return VK_ERROR_OUT_OF_DATE_KHR
;
537 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
538 if (!chain
->images
[i
].busy
) {
539 /* We found a non-busy image */
541 chain
->images
[i
].busy
= true;
546 /* This time we do a blocking dispatch because we can't go
547 * anywhere until we get an event.
549 int ret
= wl_display_roundtrip_queue(chain
->display
->display
,
552 return VK_ERROR_OUT_OF_DATE_KHR
;
557 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
559 struct wsi_wl_swapchain
*chain
= data
;
561 chain
->fifo_ready
= true;
563 wl_callback_destroy(callback
);
566 static const struct wl_callback_listener frame_listener
= {
571 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
572 uint32_t image_index
,
573 const VkPresentRegionKHR
*damage
)
575 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
577 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
578 while (!chain
->fifo_ready
) {
579 int ret
= wl_display_dispatch_queue(chain
->display
->display
,
582 return VK_ERROR_OUT_OF_DATE_KHR
;
586 assert(image_index
< chain
->base
.image_count
);
587 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
589 if (chain
->surface_version
>= 4 && damage
&&
590 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
591 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
592 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
593 assert(rect
->layer
== 0);
594 wl_surface_damage_buffer(chain
->surface
,
595 rect
->offset
.x
, rect
->offset
.y
,
596 rect
->extent
.width
, rect
->extent
.height
);
599 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
602 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
603 struct wl_callback
*frame
= wl_surface_frame(chain
->surface
);
604 wl_proxy_set_queue((struct wl_proxy
*)frame
, chain
->queue
);
605 wl_callback_add_listener(frame
, &frame_listener
, chain
);
606 chain
->fifo_ready
= false;
609 chain
->images
[image_index
].busy
= true;
610 wl_surface_commit(chain
->surface
);
611 wl_display_flush(chain
->display
->display
);
617 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
619 struct wsi_wl_image
*image
= data
;
621 assert(image
->buffer
== buffer
);
626 static const struct wl_buffer_listener buffer_listener
= {
627 buffer_handle_release
,
631 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
632 struct wsi_wl_image
*image
,
633 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
634 const VkAllocationCallbacks
* pAllocator
)
636 VkDevice vk_device
= chain
->base
.device
;
642 result
= chain
->base
.image_fns
->create_wsi_image(vk_device
,
653 if (result
!= VK_SUCCESS
)
656 image
->buffer
= wl_drm_create_prime_buffer(chain
->display
->drm
,
659 chain
->extent
.height
,
663 0, 0, 0, 0 /* unused */);
664 wl_display_roundtrip(chain
->display
->display
);
670 wl_proxy_set_queue((struct wl_proxy
*)image
->buffer
, chain
->queue
);
671 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
676 chain
->base
.image_fns
->free_wsi_image(vk_device
, pAllocator
,
677 image
->image
, image
->memory
);
683 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
684 const VkAllocationCallbacks
*pAllocator
)
686 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
688 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
689 if (chain
->images
[i
].buffer
)
690 chain
->base
.image_fns
->free_wsi_image(chain
->base
.device
, pAllocator
,
691 chain
->images
[i
].image
,
692 chain
->images
[i
].memory
);
695 vk_free(pAllocator
, chain
);
701 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
703 struct wsi_device
*wsi_device
,
705 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
706 const VkAllocationCallbacks
* pAllocator
,
707 const struct wsi_image_fns
*image_fns
,
708 struct wsi_swapchain
**swapchain_out
)
710 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
711 struct wsi_wl_swapchain
*chain
;
714 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
716 int num_images
= pCreateInfo
->minImageCount
;
718 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
719 chain
= vk_alloc(pAllocator
, size
, 8,
720 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
722 return VK_ERROR_OUT_OF_HOST_MEMORY
;
724 bool alpha
= pCreateInfo
->compositeAlpha
==
725 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
727 chain
->base
.device
= device
;
728 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
729 chain
->base
.get_images
= wsi_wl_swapchain_get_images
;
730 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
731 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
732 chain
->base
.image_fns
= image_fns
;
733 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
734 chain
->base
.image_count
= num_images
;
735 chain
->base
.needs_linear_copy
= false;
736 chain
->surface
= surface
->surface
;
737 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
738 chain
->extent
= pCreateInfo
->imageExtent
;
739 chain
->vk_format
= pCreateInfo
->imageFormat
;
740 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
742 chain
->fifo_ready
= true;
744 /* Mark a bunch of stuff as NULL. This way we can just call
745 * destroy_swapchain for cleanup.
747 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++)
748 chain
->images
[i
].buffer
= NULL
;
751 chain
->display
= wsi_wl_get_display(wsi_device
,
753 if (!chain
->display
) {
754 result
= VK_ERROR_INITIALIZATION_FAILED
;
758 chain
->queue
= wl_display_create_queue(chain
->display
->display
);
760 result
= VK_ERROR_INITIALIZATION_FAILED
;
764 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
765 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
766 pCreateInfo
, pAllocator
);
767 if (result
!= VK_SUCCESS
)
769 chain
->images
[i
].busy
= false;
772 *swapchain_out
= &chain
->base
;
777 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
783 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
784 const VkAllocationCallbacks
*alloc
,
785 VkPhysicalDevice physical_device
,
786 const struct wsi_callbacks
*cbs
)
788 struct wsi_wayland
*wsi
;
791 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
792 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
794 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
798 wsi
->physical_device
= physical_device
;
801 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
804 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
806 /* FINISHME: Choose a better error. */
807 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
813 wsi
->displays
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
814 _mesa_key_pointer_equal
);
815 if (!wsi
->displays
) {
816 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
820 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
821 wsi
->base
.get_capabilities
= wsi_wl_surface_get_capabilities
;
822 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
823 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
824 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
826 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
831 pthread_mutex_destroy(&wsi
->mutex
);
836 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
842 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
843 const VkAllocationCallbacks
*alloc
)
845 struct wsi_wayland
*wsi
=
846 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
849 struct hash_entry
*entry
;
850 hash_table_foreach(wsi
->displays
, entry
)
851 wsi_wl_display_destroy(wsi
, entry
->data
);
853 _mesa_hash_table_destroy(wsi
->displays
, NULL
);
855 pthread_mutex_destroy(&wsi
->mutex
);