From: Alexandros Frantzis alexandros.frantzis@collabora.com
If the target HWND is no longer valid, report VK_ERROR_SURFACE_LOST_KHR.
If the VkSwapchainKHR extent does not match the target window client area, report VK_ERROR_OUT_OF_DATE_KHR, to notify the app that it should recreate the swapchain. --- dlls/winewayland.drv/vulkan.c | 131 +++++++++++++++++++++++++++++- dlls/winewayland.drv/waylanddrv.h | 1 + 2 files changed, 131 insertions(+), 1 deletion(-)
diff --git a/dlls/winewayland.drv/vulkan.c b/dlls/winewayland.drv/vulkan.c index 7f73286ec5f..ea7a886b7f1 100644 --- a/dlls/winewayland.drv/vulkan.c +++ b/dlls/winewayland.drv/vulkan.c @@ -73,12 +73,23 @@ static VkResult (*pvkQueuePresentKHR)(VkQueue, const VkPresentInfoKHR *); static void *vulkan_handle; static const struct vulkan_funcs vulkan_funcs;
+static pthread_mutex_t wine_vk_swapchain_mutex = PTHREAD_MUTEX_INITIALIZER; +static struct list wine_vk_swapchain_list = LIST_INIT(wine_vk_swapchain_list); + struct wine_vk_surface { struct wayland_client_surface *client; VkSurfaceKHR native; };
+struct wine_vk_swapchain +{ + struct list entry; + VkSwapchainKHR native; + HWND hwnd; + VkExtent2D extent; +}; + static struct wine_vk_surface *wine_vk_surface_from_handle(VkSurfaceKHR handle) { return (struct wine_vk_surface *)(uintptr_t)handle; @@ -122,6 +133,25 @@ static BOOL wine_vk_surface_is_valid(struct wine_vk_surface *wine_vk_surface) return FALSE; }
+static struct wine_vk_swapchain *wine_vk_swapchain_from_handle(VkSwapchainKHR handle) +{ + struct wine_vk_swapchain *wine_vk_swapchain; + + pthread_mutex_lock(&wine_vk_swapchain_mutex); + LIST_FOR_EACH_ENTRY(wine_vk_swapchain, &wine_vk_swapchain_list, + struct wine_vk_swapchain, entry) + { + if (wine_vk_swapchain->native == handle) + { + pthread_mutex_unlock(&wine_vk_swapchain_mutex); + return wine_vk_swapchain; + } + } + pthread_mutex_unlock(&wine_vk_swapchain_mutex); + + return NULL; +} + /* Helper function for converting between win32 and Wayland compatible VkInstanceCreateInfo. * Caller is responsible for allocation and cleanup of 'dst'. */ static VkResult wine_vk_instance_convert_create_info(const VkInstanceCreateInfo *src, @@ -206,6 +236,76 @@ static VkResult wine_vk_surface_update_caps(struct wine_vk_surface *wine_vk_surf return VK_SUCCESS; }
+/* Rate the severity of a VkResult according to Vulkan spec rules that + * specify which result to return for vkQueuePresentKHR calls with + * multiple swapchains. */ +static int vk_result_severity(VkResult res) +{ + switch(res) + { + case VK_SUCCESS: return 0; + case VK_SUBOPTIMAL_KHR: return 1; + case VK_ERROR_OUT_OF_DATE_KHR: return 3; + case VK_ERROR_SURFACE_LOST_KHR: return 4; + case VK_ERROR_DEVICE_LOST: return 5; + case VK_ERROR_OUT_OF_DEVICE_MEMORY: return 6; + case VK_ERROR_OUT_OF_HOST_MEMORY: return 7; + /* For unhandled results return the lowest non-success severity. */ + default: return 2; + } +} + +static void vk_result_update_severity(VkResult *res1, VkResult res2) +{ + if (vk_result_severity(res2) > vk_result_severity(*res1)) *res1 = res2; +} + +static VkResult check_queue_present(const VkPresentInfoKHR *present_info, + VkResult present_res) +{ + VkResult res = present_res; + uint32_t i; + + for (i = 0; i < present_info->swapchainCount; ++i) + { + struct wine_vk_swapchain *wine_vk_swapchain = + wine_vk_swapchain_from_handle(present_info->pSwapchains[i]); + HWND hwnd = wine_vk_swapchain->hwnd; + struct wayland_surface *wayland_surface; + VkResult swap_res; + + if ((wayland_surface = wayland_surface_lock_hwnd(hwnd))) + { + int client_width = wayland_surface->window.client_rect.right - + wayland_surface->window.client_rect.left; + int client_height = wayland_surface->window.client_rect.bottom - + wayland_surface->window.client_rect.top; + + pthread_mutex_unlock(&wayland_surface->mutex); + + if (client_width != wine_vk_swapchain->extent.width || + client_height != wine_vk_swapchain->extent.height) + { + swap_res = VK_ERROR_OUT_OF_DATE_KHR; + } + else + { + swap_res = VK_SUCCESS; + } + } + else + { + swap_res = VK_ERROR_SURFACE_LOST_KHR; + } + + if (present_info->pResults) + vk_result_update_severity(&present_info->pResults[i], swap_res); + vk_result_update_severity(&res, swap_res); + } + + return res; +} + static VkResult wayland_vkCreateInstance(const VkInstanceCreateInfo *create_info, const VkAllocationCallbacks *allocator, VkInstance *instance) @@ -242,6 +342,7 @@ static VkResult wayland_vkCreateSwapchainKHR(VkDevice device, VkResult res; VkSwapchainCreateInfoKHR create_info_host; struct wine_vk_surface *wine_vk_surface; + struct wine_vk_swapchain *wine_vk_swapchain;
TRACE("%p %p %p %p\n", device, create_info, allocator, swapchain);
@@ -252,6 +353,13 @@ static VkResult wayland_vkCreateSwapchainKHR(VkDevice device, if (!wine_vk_surface_is_valid(wine_vk_surface)) return VK_ERROR_SURFACE_LOST_KHR;
+ wine_vk_swapchain = calloc(1, sizeof(*wine_vk_swapchain)); + if (!wine_vk_swapchain) + return VK_ERROR_OUT_OF_HOST_MEMORY; + + wine_vk_swapchain->hwnd = wine_vk_surface_get_hwnd(wine_vk_surface); + wine_vk_swapchain->extent = create_info->imageExtent; + create_info_host = *create_info; create_info_host.surface = wine_vk_surface->native;
@@ -270,9 +378,16 @@ static VkResult wayland_vkCreateSwapchainKHR(VkDevice device, if (res != VK_SUCCESS) { ERR("Failed to create vulkan wayland swapchain, res=%d\n", res); + free(wine_vk_swapchain); return res; }
+ wine_vk_swapchain->native = *swapchain; + + pthread_mutex_lock(&wine_vk_swapchain_mutex); + list_add_head(&wine_vk_swapchain_list, &wine_vk_swapchain->entry); + pthread_mutex_unlock(&wine_vk_swapchain_mutex); + TRACE("Created swapchain=0x%s\n", wine_dbgstr_longlong(*swapchain)); return res; } @@ -377,12 +492,22 @@ static void wayland_vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *allocator) { + struct wine_vk_swapchain *wine_vk_swapchain; + TRACE("%p, 0x%s %p\n", device, wine_dbgstr_longlong(swapchain), allocator);
if (allocator) FIXME("Support for allocation callbacks not implemented yet\n");
pvkDestroySwapchainKHR(device, swapchain, NULL /* allocator */); + + if ((wine_vk_swapchain = wine_vk_swapchain_from_handle(swapchain))) + { + pthread_mutex_lock(&wine_vk_swapchain_mutex); + list_remove(&wine_vk_swapchain->entry); + pthread_mutex_unlock(&wine_vk_swapchain_mutex); + free(wine_vk_swapchain); + } }
static VkResult wayland_vkEnumerateInstanceExtensionProperties(const char *layer_name, @@ -636,9 +761,13 @@ static VkResult wayland_vkGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR
static VkResult wayland_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *present_info) { + VkResult res; + TRACE("%p, %p\n", queue, present_info);
- return pvkQueuePresentKHR(queue, present_info); + res = pvkQueuePresentKHR(queue, present_info); + + return check_queue_present(present_info, res); }
static VkSurfaceKHR wayland_wine_get_native_surface(VkSurfaceKHR surface) diff --git a/dlls/winewayland.drv/waylanddrv.h b/dlls/winewayland.drv/waylanddrv.h index 00c9112d5de..c7d730fbd3e 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -36,6 +36,7 @@ #include "winbase.h" #include "ntgdi.h" #include "wine/gdi_driver.h" +#include "wine/list.h" #include "wine/rbtree.h"
#include "unixlib.h"