layer: improve how layer handles Vulkan extensions
[platform/core/uifw/vulkan-wsi-tizen.git] / layer / surface_api.cpp
1 /*
2  * Copyright (c) 2016-2017, 2019, 2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
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
22  * SOFTWARE.
23  */
24
25 #include <cassert>
26 #include <wsi/wsi_factory.hpp>
27 #include "private_data.hpp"
28 #include "surface_api.hpp"
29
30 extern "C" {
31
32 /**
33  * @brief Implements vkGetPhysicalDeviceSurfaceCapabilitiesKHR Vulkan entrypoint.
34  */
35 VKAPI_ATTR VkResult wsi_layer_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
36                                                                         VkSurfaceKHR surface,
37                                                                         VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
38 {
39    auto &instance = layer::instance_private_data::get(physicalDevice);
40    if (instance.should_layer_handle_surface(physicalDevice, surface))
41    {
42       wsi::surface_properties *props = wsi::get_surface_properties(surface);
43       assert(props != nullptr);
44       return props->get_surface_capabilities(physicalDevice, surface, pSurfaceCapabilities);
45    }
46
47    /* If the layer cannot handle this surface, then necessarily the surface must have been created by the ICDs (or a
48     * layer below us.) So it is safe to assume that the ICDs (or layers below us) support VK_KHR_surface and therefore
49     * it is safe to can call down. This holds for other entrypoints below.
50     */
51    return instance.disp.GetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, pSurfaceCapabilities);
52 }
53
54 /**
55  * @brief Implements vkGetPhysicalDeviceSurfaceFormatsKHR Vulkan entrypoint.
56  */
57 VKAPI_ATTR VkResult wsi_layer_vkGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice,
58                                                                    VkSurfaceKHR surface, uint32_t *pSurfaceFormatCount,
59                                                                    VkSurfaceFormatKHR *pSurfaceFormats)
60 {
61    auto &instance = layer::instance_private_data::get(physicalDevice);
62    if (instance.should_layer_handle_surface(physicalDevice, surface))
63    {
64       wsi::surface_properties *props = wsi::get_surface_properties(surface);
65       assert(props != nullptr);
66       return props->get_surface_formats(physicalDevice, surface, pSurfaceFormatCount, pSurfaceFormats);
67    }
68
69    return instance.disp.GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, pSurfaceFormatCount,
70                                                            pSurfaceFormats);
71 }
72
73 /**
74  * @brief Implements vkGetPhysicalDeviceSurfacePresentModesKHR Vulkan entrypoint.
75  */
76 VKAPI_ATTR VkResult wsi_layer_vkGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
77                                                                         VkSurfaceKHR surface,
78                                                                         uint32_t *pPresentModeCount,
79                                                                         VkPresentModeKHR *pPresentModes)
80 {
81    auto &instance = layer::instance_private_data::get(physicalDevice);
82    if (instance.should_layer_handle_surface(physicalDevice, surface))
83    {
84       wsi::surface_properties *props = wsi::get_surface_properties(surface);
85       assert(props != nullptr);
86       return props->get_surface_present_modes(physicalDevice, surface, pPresentModeCount, pPresentModes);
87    }
88
89    return instance.disp.GetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, pPresentModeCount,
90                                                                 pPresentModes);
91 }
92
93 /**
94  * @brief Implements vkGetPhysicalDeviceSurfaceSupportKHR Vulkan entrypoint.
95  */
96 VKAPI_ATTR VkResult wsi_layer_vkGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice physicalDevice,
97                                                                    uint32_t queueFamilyIndex, VkSurfaceKHR surface,
98                                                                    VkBool32 *pSupported)
99 {
100    auto &instance = layer::instance_private_data::get(physicalDevice);
101    if (instance.should_layer_handle_surface(physicalDevice, surface))
102    {
103       *pSupported = VK_TRUE;
104       return VK_SUCCESS;
105    }
106
107    return instance.disp.GetPhysicalDeviceSurfaceSupportKHR(physicalDevice, queueFamilyIndex, surface, pSupported);
108 }
109
110 } /* extern "C" */