2da5e1d90bdb78f5461d5e955d5be8e8ce04e036
[platform/core/uifw/vulkan-wsi-tizen.git] / wsi / wayland / surface_properties.cpp
1 /*
2  * Copyright (c) 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 #define VK_USE_PLATFORM_WAYLAND_KHR 1
26
27 #include <wayland-client.h>
28 #include <linux-dmabuf-unstable-v1-client-protocol.h>
29
30 #include <cassert>
31 #include <cstdlib>
32 #include <algorithm>
33 #include <array>
34 #include <cstring>
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"
42
43 #define NELEMS(x) (sizeof(x) / sizeof(x[0]))
44
45 namespace wsi
46 {
47 namespace wayland
48 {
49
50 struct vk_format_hasher
51 {
52    size_t operator()(const VkFormat format) const
53    {
54       return std::hash<uint64_t>()(static_cast<uint64_t>(format));
55    }
56 };
57
58 using vk_format_set = std::unordered_set<VkFormat, vk_format_hasher>;
59
60 surface_properties &surface_properties::get_instance()
61 {
62    static surface_properties instance;
63    return instance;
64 }
65
66 VkResult surface_properties::get_surface_capabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
67                                                       VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
68 {
69    /* Image count limits */
70    pSurfaceCapabilities->minImageCount = 2;
71    /* There is no maximum theoretically speaking */
72    pSurfaceCapabilities->maxImageCount = UINT32_MAX;
73
74    /* Surface extents */
75    pSurfaceCapabilities->currentExtent = { 0xffffffff, 0xffffffff };
76    pSurfaceCapabilities->minImageExtent = { 1, 1 };
77
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);
81
82    pSurfaceCapabilities->maxImageExtent = { dev_props.limits.maxImageDimension2D,
83                                             dev_props.limits.maxImageDimension2D };
84    pSurfaceCapabilities->maxImageArrayLayers = 1;
85
86    /* Surface transforms */
87    pSurfaceCapabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
88    pSurfaceCapabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
89
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);
94
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;
99
100    return VK_SUCCESS;
101 }
102
103 static void get_vk_supported_formats(const util::vector<drm_format_pair> &drm_supported_formats,
104                                      vk_format_set &vk_supported_formats)
105 {
106    for (const auto &drm_format : drm_supported_formats)
107    {
108       const VkFormat vk_format = util::drm::drm_to_vk_format(drm_format.fourcc);
109       if (vk_format != VK_FORMAT_UNDEFINED)
110       {
111          const VkFormat srgb_vk_format = util::drm::drm_to_vk_srgb_format(drm_format.fourcc);
112          if (srgb_vk_format != VK_FORMAT_UNDEFINED)
113          {
114             vk_supported_formats.insert({srgb_vk_format, vk_format});
115          }
116          else
117          {
118             vk_supported_formats.insert(vk_format);
119          }
120       }
121    }
122 }
123
124 /*
125  * @brief Query a surface's supported formats from the compositor.
126  *
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.
130  *
131  * @param[in]  surface                  The surface, which the supported formats
132  *                                      are for.
133  * @param[out] vk_supported_formats     unordered_set which will store the supported
134  *                                      formats.
135  *
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.
139  */
140 static VkResult query_supported_formats(
141    const VkSurfaceKHR surface, vk_format_set &vk_supported_formats, const util::allocator& allocator)
142 {
143    const VkIcdSurfaceWayland *vk_surf = reinterpret_cast<VkIcdSurfaceWayland *>(surface);
144    wl_display *display = vk_surf->display;
145
146    auto registry = registry_owner{wl_display_get_registry(display)};
147    if (registry.get() == nullptr)
148    {
149       WSI_LOG_ERROR("Failed to get wl display registry.");
150       return VK_ERROR_SURFACE_LOST_KHR;
151    }
152
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(), &registry_listener, &dmabuf_interface);
156    if (res < 0)
157    {
158       WSI_LOG_ERROR("Failed to add registry listener.");
159       return VK_ERROR_SURFACE_LOST_KHR;
160    }
161
162    /* Get the dma buf interface. */
163    res = wl_display_roundtrip(display);
164    if (res < 0)
165    {
166       WSI_LOG_ERROR("Roundtrip failed.");
167       return VK_ERROR_SURFACE_LOST_KHR;
168    }
169
170    if (dmabuf_interface.get() == nullptr)
171    {
172       return VK_ERROR_SURFACE_LOST_KHR;
173    }
174
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)
178    {
179       return ret == VK_ERROR_UNKNOWN ? VK_ERROR_SURFACE_LOST_KHR : ret;
180    }
181
182    get_vk_supported_formats(drm_supported_formats, vk_supported_formats);
183
184    return ret;
185 }
186
187 VkResult surface_properties::get_surface_formats(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
188                                                  uint32_t *surfaceFormatCount, VkSurfaceFormatKHR *surfaceFormats)
189 {
190    vk_format_set formats;
191
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)
195    {
196       return query_res;
197    }
198
199    assert(surfaceFormatCount != nullptr);
200    if (nullptr == surfaceFormats)
201    {
202       *surfaceFormatCount = formats.size();
203       return VK_SUCCESS;
204    }
205
206    VkResult res = VK_SUCCESS;
207
208    if (formats.size() > *surfaceFormatCount)
209    {
210       res = VK_INCOMPLETE;
211    }
212
213    uint32_t format_count = 0;
214    for (const auto &format : formats)
215    {
216       if (format_count >= *surfaceFormatCount)
217       {
218          break;
219       }
220       surfaceFormats[format_count].format = format;
221       surfaceFormats[format_count++].colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
222    }
223    *surfaceFormatCount = format_count;
224
225    return res;
226 }
227
228 VkResult surface_properties::get_surface_present_modes(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
229                                                        uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes)
230 {
231
232    VkResult res = VK_SUCCESS;
233
234    static std::array<const VkPresentModeKHR, 2> modes = {
235       VK_PRESENT_MODE_FIFO_KHR,
236       VK_PRESENT_MODE_MAILBOX_KHR,
237    };
238
239    assert(pPresentModeCount != nullptr);
240
241    if (nullptr == pPresentModes)
242    {
243       *pPresentModeCount = modes.size();
244    }
245    else
246    {
247       if (modes.size() > *pPresentModeCount)
248       {
249          res = VK_INCOMPLETE;
250       }
251       *pPresentModeCount = std::min(*pPresentModeCount, static_cast<uint32_t>(modes.size()));
252       for (uint32_t i = 0; i < *pPresentModeCount; ++i)
253       {
254          pPresentModes[i] = modes[i];
255       }
256    }
257
258    return res;
259 }
260
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,
271 };
272
273 VkResult surface_properties::get_required_device_extensions(util::extension_list &extension_list)
274 {
275    return extension_list.add(required_device_extensions, NELEMS(required_device_extensions));
276 }
277
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)
281 {
282    return VK_TRUE;
283 }
284
285 extern "C" VkResult CreateWaylandSurfaceKHR(VkInstance instance, const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
286                                             const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
287 {
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)
292    {
293       return VK_ERROR_OUT_OF_HOST_MEMORY;
294    }
295    VkResult res = instance_data.disp.CreateWaylandSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
296    if (res == VK_SUCCESS)
297    {
298       res = instance_data.add_surface(*pSurface, wsi_surface);
299       if (res != VK_SUCCESS)
300       {
301          instance_data.disp.DestroySurfaceKHR(instance, *pSurface, pAllocator);
302       }
303    }
304    return res;
305 }
306
307 PFN_vkVoidFunction surface_properties::get_proc_addr(const char *name)
308 {
309    if (strcmp(name, "vkGetPhysicalDeviceWaylandPresentationSupportKHR") == 0)
310    {
311       return reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceWaylandPresentationSupportKHR);
312    }
313    else if (strcmp(name, "vkCreateWaylandSurfaceKHR") == 0)
314    {
315       return reinterpret_cast<PFN_vkVoidFunction>(CreateWaylandSurfaceKHR);
316    }
317    return nullptr;
318 }
319
320 } // namespace wayland
321 } // namespace wsi