layer: improve how layer handles Vulkan extensions
[platform/core/uifw/vulkan-wsi-tizen.git] / layer / private_data.cpp
1 /*
2  * Copyright (c) 2018-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 "private_data.hpp"
26
27 #include "wsi/wsi_factory.hpp"
28
29 #include <unordered_map>
30
31 namespace layer
32 {
33
34 static std::mutex g_data_lock;
35 static std::unordered_map<void *, std::unique_ptr<instance_private_data>> g_instance_data;
36 static std::unordered_map<void *, std::unique_ptr<device_private_data>> g_device_data;
37
38 template <typename object_type, typename get_proc_type>
39 static PFN_vkVoidFunction get_proc_helper(object_type obj, get_proc_type get_proc,
40                                           const char* proc_name, bool required, bool &ok)
41 {
42    PFN_vkVoidFunction ret = get_proc(obj, proc_name);
43    if (nullptr == ret && required)
44    {
45       ok = false;
46    }
47    return ret;
48 }
49
50 VkResult instance_dispatch_table::populate(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc)
51 {
52    bool ok = true;
53 #define REQUIRED(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(instance, get_proc, "vk" #x, true, ok));
54 #define OPTIONAL(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(instance, get_proc, "vk" #x, false, ok));
55    INSTANCE_ENTRYPOINTS_LIST(REQUIRED, OPTIONAL);
56 #undef REQUIRED
57 #undef OPTIONAL
58    return ok ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED;
59 }
60
61 VkResult device_dispatch_table::populate(VkDevice device, PFN_vkGetDeviceProcAddr get_proc)
62 {
63    bool ok = true;
64 #define REQUIRED(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(device, get_proc, "vk" #x, true, ok));
65 #define OPTIONAL(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(device, get_proc, "vk" #x, false, ok));
66    DEVICE_ENTRYPOINTS_LIST(REQUIRED, OPTIONAL);
67 #undef REQUIRED
68 #undef OPTIONAL
69    return ok ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED;
70 }
71
72 instance_private_data::instance_private_data(const instance_dispatch_table& table,
73                                              PFN_vkSetInstanceLoaderData set_loader_data,
74                                              util::wsi_platform_set enabled_layer_platforms)
75    : disp(table)
76    , SetInstanceLoaderData(set_loader_data)
77    , enabled_layer_platforms(enabled_layer_platforms)
78 {
79 }
80
81 template <typename dispatchable_type>
82 static inline void *get_key(dispatchable_type dispatchable_object)
83 {
84    return *reinterpret_cast<void **>(dispatchable_object);
85 }
86
87 void instance_private_data::set(VkInstance inst, std::unique_ptr<instance_private_data> inst_data)
88 {
89    scoped_mutex lock(g_data_lock);
90    g_instance_data[get_key(inst)] = std::move(inst_data);
91 }
92
93 template <typename dispatchable_type>
94 static instance_private_data &get_instance_private_data(dispatchable_type dispatchable_object)
95 {
96    scoped_mutex lock(g_data_lock);
97    return *g_instance_data[get_key(dispatchable_object)];
98 }
99
100 instance_private_data &instance_private_data::get(VkInstance instance)
101 {
102    return get_instance_private_data(instance);
103 }
104
105 instance_private_data &instance_private_data::get(VkPhysicalDevice phys_dev)
106 {
107    return get_instance_private_data(phys_dev);
108 }
109
110 static VkIcdWsiPlatform get_platform_of_surface(VkSurfaceKHR surface)
111 {
112    VkIcdSurfaceBase *surface_base = reinterpret_cast<VkIcdSurfaceBase *>(surface);
113    return surface_base->platform;
114 }
115
116 bool instance_private_data::does_layer_support_surface(VkSurfaceKHR surface)
117 {
118    return enabled_layer_platforms.contains(get_platform_of_surface(surface));
119 }
120
121 bool instance_private_data::do_icds_support_surface(VkPhysicalDevice, VkSurfaceKHR)
122 {
123    /* For now assume ICDs do not support VK_KHR_surface. This means that the layer will handle all the surfaces it can
124     * handle (even if the ICDs can handle the surface) and only call down for surfaces it cannot handle. In the future
125     * we may allow system integrators to configure which ICDs have precedence handling which platforms.
126     */
127    return false;
128 }
129
130 bool instance_private_data::should_layer_handle_surface(VkPhysicalDevice phys_dev, VkSurfaceKHR surface)
131 {
132    /* If the layer cannot handle the surface, then necessarily the ICDs or layers below us must be able to do it:
133     * the fact that the surface exists means that the Vulkan loader created it. In turn, this means that someone
134     * among the ICDs and layers advertised support for it. If it's not us, then it must be one of the layers/ICDs
135     * below us. It is therefore safe to always return false (and therefore call-down) when layer_can_handle_surface
136     * is false.
137     */
138    bool icd_can_handle_surface = do_icds_support_surface(phys_dev, surface);
139    bool layer_can_handle_surface = does_layer_support_surface(surface);
140    bool ret = layer_can_handle_surface && !icd_can_handle_surface;
141    return ret;
142 }
143
144 void instance_private_data::destroy(VkInstance inst)
145 {
146    scoped_mutex lock(g_data_lock);
147    g_instance_data.erase(get_key(inst));
148 }
149
150 device_private_data::device_private_data(instance_private_data &inst_data, VkPhysicalDevice phys_dev, VkDevice dev,
151                                          const device_dispatch_table &table, PFN_vkSetDeviceLoaderData set_loader_data)
152    : disp{table}
153    , instance_data{inst_data}
154    , SetDeviceLoaderData{set_loader_data}
155    , physical_device{phys_dev}
156    , device{dev}
157 {
158 }
159
160 void device_private_data::set(VkDevice dev, std::unique_ptr<device_private_data> dev_data)
161 {
162    scoped_mutex lock(g_data_lock);
163    g_device_data[get_key(dev)] = std::move(dev_data);
164 }
165
166 template <typename dispatchable_type>
167 static device_private_data &get_device_private_data(dispatchable_type dispatchable_object)
168 {
169    scoped_mutex lock(g_data_lock);
170    return *g_device_data[get_key(dispatchable_object)];
171 }
172
173 device_private_data &device_private_data::get(VkDevice device)
174 {
175    return get_device_private_data(device);
176 }
177
178 device_private_data &device_private_data::get(VkQueue queue)
179 {
180    return get_device_private_data(queue);
181 }
182
183 void device_private_data::add_layer_swapchain(VkSwapchainKHR swapchain)
184 {
185    scoped_mutex lock(swapchains_lock);
186    swapchains.insert(swapchain);
187 }
188
189 bool device_private_data::layer_owns_all_swapchains(const VkSwapchainKHR *swapchain, uint32_t swapchain_count) const
190 {
191    scoped_mutex lock(swapchains_lock);
192    for (uint32_t i = 0; i < swapchain_count; i++)
193    {
194       if (swapchains.find(swapchain[i]) == swapchains.end())
195       {
196          return false;
197       }
198    }
199    return true;
200 }
201
202 bool device_private_data::should_layer_create_swapchain(VkSurfaceKHR vk_surface)
203 {
204    return instance_data.should_layer_handle_surface(physical_device, vk_surface);
205 }
206
207 bool device_private_data::can_icds_create_swapchain(VkSurfaceKHR vk_surface)
208 {
209    return disp.CreateSwapchainKHR != nullptr;
210 }
211
212 void device_private_data::destroy(VkDevice dev)
213 {
214    scoped_mutex lock(g_data_lock);
215    g_device_data.erase(get_key(dev));
216 }
217 } /* namespace layer */