2 * Copyright (c) 2018-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
25 #include "private_data.hpp"
27 #include "wsi/wsi_factory.hpp"
29 #include <unordered_map>
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;
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)
42 PFN_vkVoidFunction ret = get_proc(obj, proc_name);
43 if (nullptr == ret && required)
50 VkResult instance_dispatch_table::populate(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc)
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);
58 return ok ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED;
61 VkResult device_dispatch_table::populate(VkDevice device, PFN_vkGetDeviceProcAddr get_proc)
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);
69 return ok ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED;
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)
76 , SetInstanceLoaderData(set_loader_data)
77 , enabled_layer_platforms(enabled_layer_platforms)
81 template <typename dispatchable_type>
82 static inline void *get_key(dispatchable_type dispatchable_object)
84 return *reinterpret_cast<void **>(dispatchable_object);
87 void instance_private_data::set(VkInstance inst, std::unique_ptr<instance_private_data> inst_data)
89 scoped_mutex lock(g_data_lock);
90 g_instance_data[get_key(inst)] = std::move(inst_data);
93 template <typename dispatchable_type>
94 static instance_private_data &get_instance_private_data(dispatchable_type dispatchable_object)
96 scoped_mutex lock(g_data_lock);
97 return *g_instance_data[get_key(dispatchable_object)];
100 instance_private_data &instance_private_data::get(VkInstance instance)
102 return get_instance_private_data(instance);
105 instance_private_data &instance_private_data::get(VkPhysicalDevice phys_dev)
107 return get_instance_private_data(phys_dev);
110 static VkIcdWsiPlatform get_platform_of_surface(VkSurfaceKHR surface)
112 VkIcdSurfaceBase *surface_base = reinterpret_cast<VkIcdSurfaceBase *>(surface);
113 return surface_base->platform;
116 bool instance_private_data::does_layer_support_surface(VkSurfaceKHR surface)
118 return enabled_layer_platforms.contains(get_platform_of_surface(surface));
121 bool instance_private_data::do_icds_support_surface(VkPhysicalDevice, VkSurfaceKHR)
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.
130 bool instance_private_data::should_layer_handle_surface(VkPhysicalDevice phys_dev, VkSurfaceKHR surface)
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
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;
144 void instance_private_data::destroy(VkInstance inst)
146 scoped_mutex lock(g_data_lock);
147 g_instance_data.erase(get_key(inst));
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)
153 , instance_data{inst_data}
154 , SetDeviceLoaderData{set_loader_data}
155 , physical_device{phys_dev}
160 void device_private_data::set(VkDevice dev, std::unique_ptr<device_private_data> dev_data)
162 scoped_mutex lock(g_data_lock);
163 g_device_data[get_key(dev)] = std::move(dev_data);
166 template <typename dispatchable_type>
167 static device_private_data &get_device_private_data(dispatchable_type dispatchable_object)
169 scoped_mutex lock(g_data_lock);
170 return *g_device_data[get_key(dispatchable_object)];
173 device_private_data &device_private_data::get(VkDevice device)
175 return get_device_private_data(device);
178 device_private_data &device_private_data::get(VkQueue queue)
180 return get_device_private_data(queue);
183 void device_private_data::add_layer_swapchain(VkSwapchainKHR swapchain)
185 scoped_mutex lock(swapchains_lock);
186 swapchains.insert(swapchain);
189 bool device_private_data::layer_owns_all_swapchains(const VkSwapchainKHR *swapchain, uint32_t swapchain_count) const
191 scoped_mutex lock(swapchains_lock);
192 for (uint32_t i = 0; i < swapchain_count; i++)
194 if (swapchains.find(swapchain[i]) == swapchains.end())
202 bool device_private_data::should_layer_create_swapchain(VkSurfaceKHR vk_surface)
204 return instance_data.should_layer_handle_surface(physical_device, vk_surface);
207 bool device_private_data::can_icds_create_swapchain(VkSurfaceKHR vk_surface)
209 return disp.CreateSwapchainKHR != nullptr;
212 void device_private_data::destroy(VkDevice dev)
214 scoped_mutex lock(g_data_lock);
215 g_device_data.erase(get_key(dev));
217 } /* namespace layer */