2 * Copyright (c) 2017, 2019, 2021 Arm Limited.
4 * SPDX-License-Identifier: MIT
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * @file swapchain_api.cpp
28 * @brief Contains the Vulkan entrypoints for the swapchain.
35 #include <wsi/wsi_factory.hpp>
37 #include "private_data.hpp"
38 #include "swapchain_api.hpp"
42 VKAPI_ATTR VkResult wsi_layer_vkCreateSwapchainKHR(VkDevice device,
43 const VkSwapchainCreateInfoKHR *pSwapchainCreateInfo,
44 const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain)
46 assert(pSwapchain != nullptr);
47 layer::device_private_data &device_data = layer::device_private_data::get(device);
48 VkSurfaceKHR surface = pSwapchainCreateInfo->surface;
50 if (!device_data.should_layer_create_swapchain(surface))
52 if (!device_data.can_icds_create_swapchain(surface))
54 return VK_ERROR_INITIALIZATION_FAILED;
56 return device_data.disp.CreateSwapchainKHR(device_data.device, pSwapchainCreateInfo, pAllocator, pSwapchain);
59 wsi::swapchain_base *sc = wsi::allocate_surface_swapchain(surface, device_data, pAllocator);
62 return VK_ERROR_OUT_OF_HOST_MEMORY;
65 VkResult result = sc->init(device, pSwapchainCreateInfo);
66 if (result != VK_SUCCESS)
68 /* Error occured during initialization, need to free allocated memory. */
69 wsi::destroy_surface_swapchain(sc, pAllocator);
73 *pSwapchain = reinterpret_cast<VkSwapchainKHR>(sc);
74 device_data.add_layer_swapchain(*pSwapchain);
78 VKAPI_ATTR void wsi_layer_vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapc,
79 const VkAllocationCallbacks *pAllocator)
81 layer::device_private_data &device_data = layer::device_private_data::get(device);
83 if (!device_data.layer_owns_swapchain(swapc))
85 return device_data.disp.DestroySwapchainKHR(device_data.device, swapc, pAllocator);
88 assert(swapc != VK_NULL_HANDLE);
89 wsi::swapchain_base *sc = reinterpret_cast<wsi::swapchain_base *>(swapc);
90 wsi::destroy_surface_swapchain(sc, pAllocator);
93 VKAPI_ATTR VkResult wsi_layer_vkGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapc,
94 uint32_t *pSwapchainImageCount, VkImage *pSwapchainImages)
96 layer::device_private_data &device_data = layer::device_private_data::get(device);
98 if (!device_data.layer_owns_swapchain(swapc))
100 return device_data.disp.GetSwapchainImagesKHR(device_data.device, swapc, pSwapchainImageCount, pSwapchainImages);
103 assert(pSwapchainImageCount != nullptr);
104 assert(swapc != VK_NULL_HANDLE);
105 wsi::swapchain_base *sc = reinterpret_cast<wsi::swapchain_base *>(swapc);
106 return sc->get_swapchain_images(pSwapchainImageCount, pSwapchainImages);
109 VKAPI_ATTR VkResult wsi_layer_vkAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapc, uint64_t timeout,
110 VkSemaphore semaphore, VkFence fence, uint32_t *pImageIndex)
112 layer::device_private_data &device_data = layer::device_private_data::get(device);
114 if (!device_data.layer_owns_swapchain(swapc))
116 return device_data.disp.AcquireNextImageKHR(device_data.device, swapc, timeout, semaphore, fence, pImageIndex);
119 assert(swapc != VK_NULL_HANDLE);
120 assert(semaphore != VK_NULL_HANDLE || fence != VK_NULL_HANDLE);
121 assert(pImageIndex != nullptr);
122 wsi::swapchain_base *sc = reinterpret_cast<wsi::swapchain_base *>(swapc);
123 return sc->acquire_next_image(timeout, semaphore, fence, pImageIndex);
126 VKAPI_ATTR VkResult wsi_layer_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo)
128 assert(queue != VK_NULL_HANDLE);
129 assert(pPresentInfo != nullptr);
131 layer::device_private_data &device_data = layer::device_private_data::get(queue);
133 if (!device_data.layer_owns_all_swapchains(pPresentInfo->pSwapchains, pPresentInfo->swapchainCount))
135 return device_data.disp.QueuePresentKHR(queue, pPresentInfo);
138 VkResult ret = VK_SUCCESS;
139 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i)
141 VkSwapchainKHR swapc = pPresentInfo->pSwapchains[i];
143 wsi::swapchain_base *sc = reinterpret_cast<wsi::swapchain_base *>(swapc);
144 assert(sc != nullptr);
146 VkResult res = sc->queue_present(queue, pPresentInfo, pPresentInfo->pImageIndices[i]);
148 if (pPresentInfo->pResults != nullptr)
150 pPresentInfo->pResults[i] = res;
153 if (res != VK_SUCCESS && ret == VK_SUCCESS)