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 "vk_format_info.h"
30 #include <util/hash_table.h>
32 #define MIN_NUM_IMAGES 2
34 struct wsi_wl_display
{
35 struct anv_physical_device
*physical_device
;
36 struct wl_display
* display
;
39 /* Vector of VkFormats supported */
40 struct anv_vector formats
;
42 uint32_t capabilities
;
46 struct anv_wsi_interface base
;
48 struct anv_physical_device
* physical_device
;
50 pthread_mutex_t mutex
;
51 /* Hash table of wl_display -> wsi_wl_display mappings */
52 struct hash_table
* displays
;
56 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
, VkFormat format
)
58 /* Don't add a format that's already in the list */
60 anv_vector_foreach(f
, &display
->formats
)
64 /* Don't add formats that aren't renderable. */
65 VkFormatProperties props
;
66 anv_GetPhysicalDeviceFormatProperties(
67 anv_physical_device_to_handle(display
->physical_device
), format
, &props
);
68 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
71 f
= anv_vector_add(&display
->formats
);
77 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
79 fprintf(stderr
, "wl_drm.device(%s)\n", name
);
83 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
86 /* TODO: Figure out what all the formats mean and make this table
90 case VK_FORMAT_R4G4B4A4_UNORM
:
91 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
92 case VK_FORMAT_R5G6B5_UNORM
:
93 return WL_DRM_FORMAT_BGR565
;
94 case VK_FORMAT_R5G5B5A1_UNORM
:
95 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
96 case VK_FORMAT_R8G8B8_UNORM
:
97 return WL_DRM_FORMAT_XBGR8888
;
98 case VK_FORMAT_R8G8B8A8_UNORM
:
99 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
100 case VK_FORMAT_R10G10B10A2_UNORM
:
101 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
102 case VK_FORMAT_B4G4R4A4_UNORM
:
103 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
104 case VK_FORMAT_B5G6R5_UNORM
:
105 return WL_DRM_FORMAT_RGB565
;
106 case VK_FORMAT_B5G5R5A1_UNORM
:
107 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
109 case VK_FORMAT_B8G8R8_SRGB
:
110 return WL_DRM_FORMAT_BGRX8888
;
111 case VK_FORMAT_B8G8R8A8_SRGB
:
112 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
114 case VK_FORMAT_B10G10R10A2_UNORM
:
115 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
119 assert("!Unsupported Vulkan format");
125 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
127 struct wsi_wl_display
*display
= data
;
131 case WL_DRM_FORMAT_ABGR4444
:
132 case WL_DRM_FORMAT_XBGR4444
:
133 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R4G4B4A4_UNORM
);
135 case WL_DRM_FORMAT_BGR565
:
136 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G6B5_UNORM
);
138 case WL_DRM_FORMAT_ABGR1555
:
139 case WL_DRM_FORMAT_XBGR1555
:
140 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G5B5A1_UNORM
);
142 case WL_DRM_FORMAT_XBGR8888
:
143 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8_UNORM
);
145 case WL_DRM_FORMAT_ABGR8888
:
146 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8A8_UNORM
);
148 case WL_DRM_FORMAT_ABGR2101010
:
149 case WL_DRM_FORMAT_XBGR2101010
:
150 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R10G10B10A2_UNORM
);
152 case WL_DRM_FORMAT_ARGB4444
:
153 case WL_DRM_FORMAT_XRGB4444
:
154 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B4G4R4A4_UNORM
);
156 case WL_DRM_FORMAT_RGB565
:
157 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G6R5_UNORM
);
159 case WL_DRM_FORMAT_ARGB1555
:
160 case WL_DRM_FORMAT_XRGB1555
:
161 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G5R5A1_UNORM
);
164 case WL_DRM_FORMAT_XRGB8888
:
165 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_SRGB
);
167 case WL_DRM_FORMAT_ARGB8888
:
168 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_SRGB
);
171 case WL_DRM_FORMAT_ARGB2101010
:
172 case WL_DRM_FORMAT_XRGB2101010
:
173 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B10G10R10A2_UNORM
);
180 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
185 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
187 struct wsi_wl_display
*display
= data
;
189 display
->capabilities
= capabilities
;
192 static const struct wl_drm_listener drm_listener
= {
195 drm_handle_authenticated
,
196 drm_handle_capabilities
,
200 registry_handle_global(void *data
, struct wl_registry
*registry
,
201 uint32_t name
, const char *interface
, uint32_t version
)
203 struct wsi_wl_display
*display
= data
;
205 if (strcmp(interface
, "wl_drm") == 0) {
206 assert(display
->drm
== NULL
);
208 assert(version
>= 2);
209 display
->drm
= wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
212 wl_drm_add_listener(display
->drm
, &drm_listener
, display
);
217 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
221 static const struct wl_registry_listener registry_listener
= {
222 registry_handle_global
,
223 registry_handle_global_remove
227 wsi_wl_display_destroy(struct wsi_wayland
*wsi
, struct wsi_wl_display
*display
)
229 anv_vector_finish(&display
->formats
);
231 wl_drm_destroy(display
->drm
);
232 anv_free(&wsi
->physical_device
->instance
->alloc
, display
);
235 static struct wsi_wl_display
*
236 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
)
238 struct wsi_wl_display
*display
=
239 anv_alloc(&wsi
->physical_device
->instance
->alloc
, sizeof(*display
), 8,
240 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
244 memset(display
, 0, sizeof(*display
));
246 display
->display
= wl_display
;
247 display
->physical_device
= wsi
->physical_device
;
249 if (!anv_vector_init(&display
->formats
, sizeof(VkFormat
), 8))
252 struct wl_registry
*registry
= wl_display_get_registry(wl_display
);
256 wl_registry_add_listener(registry
, ®istry_listener
, display
);
258 /* Round-rip to get the wl_drm global */
259 wl_display_roundtrip(wl_display
);
264 /* Round-rip to get wl_drm formats and capabilities */
265 wl_display_roundtrip(wl_display
);
267 /* We need prime support */
268 if (!(display
->capabilities
& WL_DRM_CAPABILITY_PRIME
))
271 /* We don't need this anymore */
272 wl_registry_destroy(registry
);
278 wl_registry_destroy(registry
);
280 wsi_wl_display_destroy(wsi
, display
);
284 static struct wsi_wl_display
*
285 wsi_wl_get_display(struct anv_physical_device
*device
,
286 struct wl_display
*wl_display
)
288 struct wsi_wayland
*wsi
=
289 (struct wsi_wayland
*)device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
291 pthread_mutex_lock(&wsi
->mutex
);
293 struct hash_entry
*entry
= _mesa_hash_table_search(wsi
->displays
,
296 /* We're about to make a bunch of blocking calls. Let's drop the
297 * mutex for now so we don't block up too badly.
299 pthread_mutex_unlock(&wsi
->mutex
);
301 struct wsi_wl_display
*display
= wsi_wl_display_create(wsi
, wl_display
);
303 pthread_mutex_lock(&wsi
->mutex
);
305 entry
= _mesa_hash_table_search(wsi
->displays
, wl_display
);
307 /* Oops, someone raced us to it */
308 wsi_wl_display_destroy(wsi
, display
);
310 entry
= _mesa_hash_table_insert(wsi
->displays
, wl_display
, display
);
314 pthread_mutex_unlock(&wsi
->mutex
);
319 VkBool32
anv_GetPhysicalDeviceWaylandPresentationSupportKHR(
320 VkPhysicalDevice physicalDevice
,
321 uint32_t queueFamilyIndex
,
322 struct wl_display
* display
)
324 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
326 return wsi_wl_get_display(physical_device
, display
) != NULL
;
330 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
331 struct anv_physical_device
*device
,
332 uint32_t queueFamilyIndex
,
333 VkBool32
* pSupported
)
340 static const VkPresentModeKHR present_modes
[] = {
341 VK_PRESENT_MODE_MAILBOX_KHR
,
342 VK_PRESENT_MODE_FIFO_KHR
,
346 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
347 struct anv_physical_device
*device
,
348 VkSurfaceCapabilitiesKHR
* caps
)
350 caps
->minImageCount
= MIN_NUM_IMAGES
;
351 caps
->maxImageCount
= 4;
352 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
353 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
354 caps
->maxImageExtent
= (VkExtent2D
) { INT16_MAX
, INT16_MAX
};
355 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
356 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
357 caps
->maxImageArrayLayers
= 1;
359 caps
->supportedCompositeAlpha
=
360 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
361 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
363 caps
->supportedUsageFlags
=
364 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
365 VK_IMAGE_USAGE_SAMPLED_BIT
|
366 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
367 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
373 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
374 struct anv_physical_device
*device
,
375 uint32_t* pSurfaceFormatCount
,
376 VkSurfaceFormatKHR
* pSurfaceFormats
)
378 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
379 struct wsi_wl_display
*display
=
380 wsi_wl_get_display(device
, surface
->display
);
382 uint32_t count
= anv_vector_length(&display
->formats
);
384 if (pSurfaceFormats
== NULL
) {
385 *pSurfaceFormatCount
= count
;
389 assert(*pSurfaceFormatCount
>= count
);
390 *pSurfaceFormatCount
= count
;
393 anv_vector_foreach(f
, &display
->formats
) {
394 *(pSurfaceFormats
++) = (VkSurfaceFormatKHR
) {
396 /* TODO: We should get this from the compositor somehow */
397 .colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
,
405 wsi_wl_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 wsi_wl_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_CreateWaylandSurfaceKHR(
430 VkInstance _instance
,
431 const VkWaylandSurfaceCreateInfoKHR
* pCreateInfo
,
432 const VkAllocationCallbacks
* pAllocator
,
433 VkSurfaceKHR
* pSurface
)
435 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
437 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR
);
439 VkIcdSurfaceWayland
*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_WAYLAND
;
447 surface
->display
= pCreateInfo
->display
;
448 surface
->surface
= pCreateInfo
->surface
;
450 *pSurface
= _VkIcdSurfaceBase_to_handle(&surface
->base
);
455 struct wsi_wl_image
{
456 struct anv_image
* image
;
457 struct anv_device_memory
* memory
;
458 struct wl_buffer
* buffer
;
462 struct wsi_wl_swapchain
{
463 struct anv_swapchain base
;
465 struct wsi_wl_display
* display
;
466 struct wl_event_queue
* queue
;
467 struct wl_surface
* surface
;
473 VkPresentModeKHR present_mode
;
476 uint32_t image_count
;
477 struct wsi_wl_image images
[0];
481 wsi_wl_swapchain_get_images(struct anv_swapchain
*anv_chain
,
482 uint32_t *pCount
, VkImage
*pSwapchainImages
)
484 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
486 if (pSwapchainImages
== NULL
) {
487 *pCount
= chain
->image_count
;
491 assert(chain
->image_count
<= *pCount
);
492 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
493 pSwapchainImages
[i
] = anv_image_to_handle(chain
->images
[i
].image
);
495 *pCount
= chain
->image_count
;
501 wsi_wl_swapchain_acquire_next_image(struct anv_swapchain
*anv_chain
,
503 VkSemaphore semaphore
,
504 uint32_t *image_index
)
506 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
508 int ret
= wl_display_dispatch_queue_pending(chain
->display
->display
,
510 /* XXX: I'm not sure if out-of-date is the right error here. If
511 * wl_display_dispatch_queue_pending fails it most likely means we got
512 * kicked by the server so this seems more-or-less correct.
515 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
518 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
519 if (!chain
->images
[i
].busy
) {
520 /* We found a non-busy image */
526 /* This time we do a blocking dispatch because we can't go
527 * anywhere until we get an event.
529 int ret
= wl_display_roundtrip_queue(chain
->display
->display
,
532 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
537 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
539 struct wsi_wl_swapchain
*chain
= data
;
541 chain
->fifo_ready
= true;
543 wl_callback_destroy(callback
);
546 static const struct wl_callback_listener frame_listener
= {
551 wsi_wl_swapchain_queue_present(struct anv_swapchain
*anv_chain
,
552 struct anv_queue
*queue
,
553 uint32_t image_index
)
555 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
557 if (chain
->present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
558 while (!chain
->fifo_ready
) {
559 int ret
= wl_display_dispatch_queue(chain
->display
->display
,
562 return vk_error(VK_ERROR_OUT_OF_DATE_KHR
);
566 assert(image_index
< chain
->image_count
);
567 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
568 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
570 if (chain
->present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
571 struct wl_callback
*frame
= wl_surface_frame(chain
->surface
);
572 wl_proxy_set_queue((struct wl_proxy
*)frame
, chain
->queue
);
573 wl_callback_add_listener(frame
, &frame_listener
, chain
);
574 chain
->fifo_ready
= false;
577 chain
->images
[image_index
].busy
= true;
578 wl_surface_commit(chain
->surface
);
579 wl_display_flush(chain
->display
->display
);
585 wsi_wl_image_finish(struct wsi_wl_swapchain
*chain
, struct wsi_wl_image
*image
,
586 const VkAllocationCallbacks
* pAllocator
)
588 VkDevice vk_device
= anv_device_to_handle(chain
->base
.device
);
589 anv_FreeMemory(vk_device
, anv_device_memory_to_handle(image
->memory
),
591 anv_DestroyImage(vk_device
, anv_image_to_handle(image
->image
),
596 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
598 struct wsi_wl_image
*image
= data
;
600 assert(image
->buffer
== buffer
);
605 static const struct wl_buffer_listener buffer_listener
= {
606 buffer_handle_release
,
610 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
, struct wsi_wl_image
*image
,
611 const VkAllocationCallbacks
* pAllocator
)
613 VkDevice vk_device
= anv_device_to_handle(chain
->base
.device
);
617 result
= anv_image_create(vk_device
,
618 &(struct anv_image_create_info
) {
619 .isl_tiling_flags
= ISL_TILING_X_BIT
,
622 &(VkImageCreateInfo
) {
623 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
624 .imageType
= VK_IMAGE_TYPE_2D
,
625 .format
= chain
->vk_format
,
627 .width
= chain
->extent
.width
,
628 .height
= chain
->extent
.height
,
634 /* FIXME: Need a way to use X tiling to allow scanout */
635 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
636 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
642 if (result
!= VK_SUCCESS
)
645 image
->image
= anv_image_from_handle(vk_image
);
646 assert(vk_format_is_color(image
->image
->vk_format
));
648 struct anv_surface
*surface
= &image
->image
->color_surface
;
650 VkDeviceMemory vk_memory
;
651 result
= anv_AllocateMemory(vk_device
,
652 &(VkMemoryAllocateInfo
) {
653 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
654 .allocationSize
= image
->image
->size
,
655 .memoryTypeIndex
= 0,
660 if (result
!= VK_SUCCESS
)
663 image
->memory
= anv_device_memory_from_handle(vk_memory
);
664 image
->memory
->bo
.is_winsys_bo
= true;
666 result
= anv_BindImageMemory(vk_device
, vk_image
, vk_memory
, 0);
668 if (result
!= VK_SUCCESS
)
671 int ret
= anv_gem_set_tiling(chain
->base
.device
,
672 image
->memory
->bo
.gem_handle
,
673 surface
->isl
.row_pitch
, I915_TILING_X
);
675 /* FINISHME: Choose a better error. */
676 result
= vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
680 int fd
= anv_gem_handle_to_fd(chain
->base
.device
,
681 image
->memory
->bo
.gem_handle
);
683 /* FINISHME: Choose a better error. */
684 result
= vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
688 image
->buffer
= wl_drm_create_prime_buffer(chain
->display
->drm
,
691 chain
->extent
.height
,
694 surface
->isl
.row_pitch
,
695 0, 0, 0, 0 /* unused */);
696 wl_display_roundtrip(chain
->display
->display
);
699 wl_proxy_set_queue((struct wl_proxy
*)image
->buffer
, chain
->queue
);
700 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
705 anv_FreeMemory(vk_device
, vk_memory
, pAllocator
);
707 anv_DestroyImage(vk_device
, vk_image
, pAllocator
);
713 wsi_wl_swapchain_destroy(struct anv_swapchain
*anv_chain
,
714 const VkAllocationCallbacks
*pAllocator
)
716 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)anv_chain
;
718 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
719 if (chain
->images
[i
].buffer
)
720 wsi_wl_image_finish(chain
, &chain
->images
[i
], pAllocator
);
723 anv_free2(&chain
->base
.device
->alloc
, pAllocator
, chain
);
729 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
730 struct anv_device
*device
,
731 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
732 const VkAllocationCallbacks
* pAllocator
,
733 struct anv_swapchain
**swapchain_out
)
735 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
736 struct wsi_wl_swapchain
*chain
;
739 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
741 int num_images
= pCreateInfo
->minImageCount
;
743 assert(num_images
>= MIN_NUM_IMAGES
);
745 /* For true mailbox mode, we need at least 4 images:
746 * 1) One to scan out from
747 * 2) One to have queued for scan-out
748 * 3) One to be currently held by the Wayland compositor
749 * 4) One to render to
751 if (pCreateInfo
->presentMode
== VK_PRESENT_MODE_MAILBOX_KHR
)
752 num_images
= MAX2(num_images
, 4);
754 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
755 chain
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
756 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
758 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
760 chain
->base
.device
= device
;
761 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
762 chain
->base
.get_images
= wsi_wl_swapchain_get_images
;
763 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
764 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
766 chain
->surface
= surface
->surface
;
767 chain
->extent
= pCreateInfo
->imageExtent
;
768 chain
->vk_format
= pCreateInfo
->imageFormat
;
769 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, false);
771 chain
->present_mode
= pCreateInfo
->presentMode
;
772 chain
->fifo_ready
= true;
774 chain
->image_count
= num_images
;
776 /* Mark a bunch of stuff as NULL. This way we can just call
777 * destroy_swapchain for cleanup.
779 for (uint32_t i
= 0; i
< chain
->image_count
; i
++)
780 chain
->images
[i
].buffer
= NULL
;
783 chain
->display
= wsi_wl_get_display(&device
->instance
->physicalDevice
,
788 chain
->queue
= wl_display_create_queue(chain
->display
->display
);
792 for (uint32_t i
= 0; i
< chain
->image_count
; i
++) {
793 result
= wsi_wl_image_init(chain
, &chain
->images
[i
], pAllocator
);
794 if (result
!= VK_SUCCESS
)
796 chain
->images
[i
].busy
= false;
799 *swapchain_out
= &chain
->base
;
804 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
810 anv_wl_init_wsi(struct anv_physical_device
*device
)
812 struct wsi_wayland
*wsi
;
815 wsi
= anv_alloc(&device
->instance
->alloc
, sizeof(*wsi
), 8,
816 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
818 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
822 wsi
->physical_device
= device
;
824 int ret
= pthread_mutex_init(&wsi
->mutex
, NULL
);
827 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
829 /* FINISHME: Choose a better error. */
830 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
836 wsi
->displays
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
837 _mesa_key_pointer_equal
);
838 if (!wsi
->displays
) {
839 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
843 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
844 wsi
->base
.get_capabilities
= wsi_wl_surface_get_capabilities
;
845 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
846 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
847 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
849 device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
854 pthread_mutex_destroy(&wsi
->mutex
);
857 anv_free(&device
->instance
->alloc
, wsi
);
859 device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
865 anv_wl_finish_wsi(struct anv_physical_device
*device
)
867 struct wsi_wayland
*wsi
=
868 (struct wsi_wayland
*)device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
871 _mesa_hash_table_destroy(wsi
->displays
, NULL
);
873 pthread_mutex_destroy(&wsi
->mutex
);
875 anv_free(&device
->instance
->alloc
, wsi
);