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
25 #define VK_USE_PLATFORM_WAYLAND_KHR 1
27 #include <wayland-client.h>
28 #include <linux-dmabuf-unstable-v1-client-protocol.h>
35 #include "surface_properties.hpp"
36 #include "surface.hpp"
37 #include "layer/private_data.hpp"
38 #include "wl_helpers.hpp"
39 #include "wl_object_owner.hpp"
40 #include "util/drm/drm_utils.hpp"
41 #include "util/log.hpp"
43 #define NELEMS(x) (sizeof(x) / sizeof(x[0]))
50 struct vk_format_hasher
52 size_t operator()(const VkFormat format) const
54 return std::hash<uint64_t>()(static_cast<uint64_t>(format));
58 using vk_format_set = std::unordered_set<VkFormat, vk_format_hasher>;
60 surface_properties &surface_properties::get_instance()
62 static surface_properties instance;
66 VkResult surface_properties::get_surface_capabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
67 VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
69 /* Image count limits */
70 pSurfaceCapabilities->minImageCount = 2;
71 /* There is no maximum theoretically speaking */
72 pSurfaceCapabilities->maxImageCount = UINT32_MAX;
75 pSurfaceCapabilities->currentExtent = { 0xffffffff, 0xffffffff };
76 pSurfaceCapabilities->minImageExtent = { 1, 1 };
78 /* TODO: Ask the device for max - for now setting the max from the GPU, may be ask the display somehow*/
79 VkPhysicalDeviceProperties dev_props;
80 layer::instance_private_data::get(physical_device).disp.GetPhysicalDeviceProperties(physical_device, &dev_props);
82 pSurfaceCapabilities->maxImageExtent = { dev_props.limits.maxImageDimension2D,
83 dev_props.limits.maxImageDimension2D };
84 pSurfaceCapabilities->maxImageArrayLayers = 1;
86 /* Surface transforms */
87 pSurfaceCapabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
88 pSurfaceCapabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
90 /* TODO: Composite alpha */
91 pSurfaceCapabilities->supportedCompositeAlpha = static_cast<VkCompositeAlphaFlagBitsKHR>(
92 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR | VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
93 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR | VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR);
95 /* Image usage flags */
96 pSurfaceCapabilities->supportedUsageFlags =
97 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
98 VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
103 static void get_vk_supported_formats(const util::vector<drm_format_pair> &drm_supported_formats,
104 vk_format_set &vk_supported_formats)
106 for (const auto &drm_format : drm_supported_formats)
108 const VkFormat vk_format = util::drm::drm_to_vk_format(drm_format.fourcc);
109 if (vk_format != VK_FORMAT_UNDEFINED)
111 const VkFormat srgb_vk_format = util::drm::drm_to_vk_srgb_format(drm_format.fourcc);
112 if (srgb_vk_format != VK_FORMAT_UNDEFINED)
114 vk_supported_formats.insert({srgb_vk_format, vk_format});
118 vk_supported_formats.insert(vk_format);
125 * @brief Query a surface's supported formats from the compositor.
127 * @details A wl_registry is created in order to get a zwp_linux_dmabuf_v1 object.
128 * Then a listener is attached to that object in order to get the supported formats
129 * from the server. The supported formats are stored in @p vk_supported_formats.
131 * @param[in] surface The surface, which the supported formats
133 * @param[out] vk_supported_formats unordered_set which will store the supported
136 * @retval VK_SUCCESS Indicates success.
137 * @retval VK_ERROR_SURFACE_LOST_KHR Indicates one of the Wayland functions failed.
138 * @retval VK_ERROR_OUT_OF_DEVICE_MEMORY Indicates the host went out of memory.
140 static VkResult query_supported_formats(
141 const VkSurfaceKHR surface, vk_format_set &vk_supported_formats, const util::allocator& allocator)
143 const VkIcdSurfaceWayland *vk_surf = reinterpret_cast<VkIcdSurfaceWayland *>(surface);
144 wl_display *display = vk_surf->display;
146 auto registry = registry_owner{wl_display_get_registry(display)};
147 if (registry.get() == nullptr)
149 WSI_LOG_ERROR("Failed to get wl display registry.");
150 return VK_ERROR_SURFACE_LOST_KHR;
153 auto dmabuf_interface = zwp_linux_dmabuf_v1_owner{nullptr};
154 const wl_registry_listener registry_listener = { registry_handler };
155 int res = wl_registry_add_listener(registry.get(), ®istry_listener, &dmabuf_interface);
158 WSI_LOG_ERROR("Failed to add registry listener.");
159 return VK_ERROR_SURFACE_LOST_KHR;
162 /* Get the dma buf interface. */
163 res = wl_display_roundtrip(display);
166 WSI_LOG_ERROR("Roundtrip failed.");
167 return VK_ERROR_SURFACE_LOST_KHR;
170 if (dmabuf_interface.get() == nullptr)
172 return VK_ERROR_SURFACE_LOST_KHR;
175 util::vector<drm_format_pair> drm_supported_formats{allocator};
176 const VkResult ret = get_supported_formats_and_modifiers(display, dmabuf_interface.get(), drm_supported_formats);
177 if (ret != VK_SUCCESS)
179 return ret == VK_ERROR_UNKNOWN ? VK_ERROR_SURFACE_LOST_KHR : ret;
182 get_vk_supported_formats(drm_supported_formats, vk_supported_formats);
187 VkResult surface_properties::get_surface_formats(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
188 uint32_t *surfaceFormatCount, VkSurfaceFormatKHR *surfaceFormats)
190 vk_format_set formats;
192 auto &instance = layer::instance_private_data::get(physical_device);
193 const auto query_res = query_supported_formats(surface, formats, instance.get_allocator());
194 if (query_res != VK_SUCCESS)
199 assert(surfaceFormatCount != nullptr);
200 if (nullptr == surfaceFormats)
202 *surfaceFormatCount = formats.size();
206 VkResult res = VK_SUCCESS;
208 if (formats.size() > *surfaceFormatCount)
213 uint32_t format_count = 0;
214 for (const auto &format : formats)
216 if (format_count >= *surfaceFormatCount)
220 surfaceFormats[format_count].format = format;
221 surfaceFormats[format_count++].colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
223 *surfaceFormatCount = format_count;
228 VkResult surface_properties::get_surface_present_modes(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
229 uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes)
232 VkResult res = VK_SUCCESS;
234 static std::array<const VkPresentModeKHR, 2> modes = {
235 VK_PRESENT_MODE_FIFO_KHR,
236 VK_PRESENT_MODE_MAILBOX_KHR,
239 assert(pPresentModeCount != nullptr);
241 if (nullptr == pPresentModes)
243 *pPresentModeCount = modes.size();
247 if (modes.size() > *pPresentModeCount)
251 *pPresentModeCount = std::min(*pPresentModeCount, static_cast<uint32_t>(modes.size()));
252 for (uint32_t i = 0; i < *pPresentModeCount; ++i)
254 pPresentModes[i] = modes[i];
261 static const char *required_device_extensions[] = {
262 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
263 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
264 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
265 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
266 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
267 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
268 VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
269 VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
270 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
273 VkResult surface_properties::get_required_device_extensions(util::extension_list &extension_list)
275 return extension_list.add(required_device_extensions, NELEMS(required_device_extensions));
278 /* TODO: Check for zwp_linux_dmabuf_v1 protocol in display */
279 VkBool32 GetPhysicalDeviceWaylandPresentationSupportKHR(VkPhysicalDevice physical_device, uint32_t queue_index,
280 struct wl_display *display)
285 extern "C" VkResult CreateWaylandSurfaceKHR(VkInstance instance, const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
286 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
288 auto &instance_data = layer::instance_private_data::get(instance);
289 util::allocator allocator{ instance_data.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT, pAllocator };
290 auto wsi_surface = util::unique_ptr<wsi::surface>(allocator.make_unique<surface>());
291 if (wsi_surface == nullptr)
293 return VK_ERROR_OUT_OF_HOST_MEMORY;
295 VkResult res = instance_data.disp.CreateWaylandSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
296 if (res == VK_SUCCESS)
298 res = instance_data.add_surface(*pSurface, wsi_surface);
299 if (res != VK_SUCCESS)
301 instance_data.disp.DestroySurfaceKHR(instance, *pSurface, pAllocator);
307 PFN_vkVoidFunction surface_properties::get_proc_addr(const char *name)
309 if (strcmp(name, "vkGetPhysicalDeviceWaylandPresentationSupportKHR") == 0)
311 return reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceWaylandPresentationSupportKHR);
313 else if (strcmp(name, "vkCreateWaylandSurfaceKHR") == 0)
315 return reinterpret_cast<PFN_vkVoidFunction>(CreateWaylandSurfaceKHR);
320 } // namespace wayland