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 #include <wayland-client.h>
27 #include <linux-dmabuf-unstable-v1-client-protocol.h>
35 #include "surface_properties.hpp"
36 #include "layer/private_data.hpp"
38 #define NELEMS(x) (sizeof(x) / sizeof(x[0]))
45 surface_properties &surface_properties::get_instance()
47 static surface_properties instance;
51 VkResult surface_properties::get_surface_capabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
52 VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
54 /* Image count limits */
55 pSurfaceCapabilities->minImageCount = 2;
56 /* There is no maximum theoretically speaking */
57 pSurfaceCapabilities->maxImageCount = UINT32_MAX;
60 pSurfaceCapabilities->currentExtent = { 0xffffffff, 0xffffffff };
61 pSurfaceCapabilities->minImageExtent = { 1, 1 };
63 /* TODO: Ask the device for max - for now setting the max from the GPU, may be ask the display somehow*/
64 VkPhysicalDeviceProperties dev_props;
65 layer::instance_private_data::get(physical_device).disp.GetPhysicalDeviceProperties(physical_device, &dev_props);
67 pSurfaceCapabilities->maxImageExtent = { dev_props.limits.maxImageDimension2D,
68 dev_props.limits.maxImageDimension2D };
69 pSurfaceCapabilities->maxImageArrayLayers = 1;
71 /* Surface transforms */
72 pSurfaceCapabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
73 pSurfaceCapabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
75 /* TODO: Composite alpha */
76 pSurfaceCapabilities->supportedCompositeAlpha = static_cast<VkCompositeAlphaFlagBitsKHR>(
77 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR | VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
78 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR | VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR);
80 /* Image usage flags */
81 pSurfaceCapabilities->supportedUsageFlags =
82 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
83 VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
88 VkResult surface_properties::get_surface_formats(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
89 uint32_t *surfaceFormatCount, VkSurfaceFormatKHR *surfaceFormats)
92 VkResult res = VK_SUCCESS;
93 /* TODO: Hardcoding a list of sensible formats, may be query it from compositor later. */
94 static std::array<const VkFormat, 2> formats = { VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB };
96 assert(surfaceFormatCount != nullptr);
98 if (nullptr == surfaceFormats)
100 *surfaceFormatCount = formats.size();
104 if (formats.size() > *surfaceFormatCount)
109 *surfaceFormatCount = std::min(*surfaceFormatCount, static_cast<uint32_t>(formats.size()));
110 for (uint32_t i = 0; i < *surfaceFormatCount; ++i)
112 surfaceFormats[i].format = formats[i];
113 surfaceFormats[i].colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
120 VkResult surface_properties::get_surface_present_modes(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
121 uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes)
124 VkResult res = VK_SUCCESS;
125 /* TODO: Check that FIFO is okay on Wayland */
126 static std::array<const VkPresentModeKHR, 2> modes = {
127 VK_PRESENT_MODE_FIFO_KHR,
128 VK_PRESENT_MODE_MAILBOX_KHR,
131 assert(pPresentModeCount != nullptr);
133 if (nullptr == pPresentModes)
135 *pPresentModeCount = modes.size();
139 if (modes.size() > *pPresentModeCount)
143 *pPresentModeCount = std::min(*pPresentModeCount, static_cast<uint32_t>(modes.size()));
144 for (uint32_t i = 0; i < *pPresentModeCount; ++i)
146 pPresentModes[i] = modes[i];
153 static const char *required_device_extensions[] = {
154 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
155 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
156 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
157 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
158 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
159 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
160 VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
161 VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
162 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
165 static std::unique_ptr<util::extension_list> populate_device_extensions()
167 std::unique_ptr<util::extension_list> ret(new util::extension_list(util::allocator::get_generic()));
168 ret->add(required_device_extensions, NELEMS(required_device_extensions));
173 const util::extension_list &surface_properties::get_required_device_extensions()
175 static std::unique_ptr<util::extension_list> device_extensions = populate_device_extensions();
176 return *device_extensions;
179 bool surface_properties::physical_device_supported(VkPhysicalDevice dev)
181 static util::extension_list device_extensions{util::allocator::get_generic()};
182 device_extensions.add(dev);
184 static util::extension_list required_extensions{util::allocator::get_generic()};
185 required_extensions.add(required_device_extensions, NELEMS(required_device_extensions));
187 return device_extensions.contains(required_extensions);
190 /* TODO: Check for zwp_linux_dmabuf_v1 protocol in display */
191 VkBool32 GetPhysicalDeviceWaylandPresentationSupportKHR(VkPhysicalDevice physical_device, uint32_t queue_index,
192 struct wl_display *display)
197 PFN_vkVoidFunction surface_properties::get_proc_addr(const char *name)
199 if (strcmp(name, "vkGetPhysicalDeviceWaylandPresentationSupportKHR") == 0)
201 return reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceWaylandPresentationSupportKHR);