57744a7b936117f27ed5f77ef40352b5e91a7f02
[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 surface_properties::surface_properties(surface &wsi_surface, const util::allocator &allocator)
51    : specific_surface(&wsi_surface)
52    , supported_formats(allocator)
53 {
54 }
55
56 surface_properties::surface_properties()
57    : specific_surface(nullptr)
58    , supported_formats(util::allocator::get_generic())
59 {
60 }
61
62 surface_properties &surface_properties::get_instance()
63 {
64    static surface_properties instance;
65    return instance;
66 }
67
68 VkResult surface_properties::get_surface_capabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
69                                                       VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
70 {
71    /* Image count limits */
72    pSurfaceCapabilities->minImageCount = 2;
73    pSurfaceCapabilities->maxImageCount = MAX_SWAPCHAIN_IMAGE_COUNT;
74
75    /* Surface extents */
76    pSurfaceCapabilities->currentExtent = { 0xffffffff, 0xffffffff };
77    pSurfaceCapabilities->minImageExtent = { 1, 1 };
78
79    /* TODO: Ask the device for max - for now setting the max from the GPU, may be ask the display somehow*/
80    VkPhysicalDeviceProperties dev_props;
81    layer::instance_private_data::get(physical_device).disp.GetPhysicalDeviceProperties(physical_device, &dev_props);
82
83    pSurfaceCapabilities->maxImageExtent = { dev_props.limits.maxImageDimension2D,
84                                             dev_props.limits.maxImageDimension2D };
85    pSurfaceCapabilities->maxImageArrayLayers = 1;
86
87    /* Surface transforms */
88    pSurfaceCapabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
89    pSurfaceCapabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
90
91    /* TODO: Composite alpha */
92    pSurfaceCapabilities->supportedCompositeAlpha = static_cast<VkCompositeAlphaFlagBitsKHR>(
93       VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR | VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
94       VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR | VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR);
95
96    /* Image usage flags */
97    pSurfaceCapabilities->supportedUsageFlags =
98       VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
99       VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
100
101    return VK_SUCCESS;
102 }
103
104 static VkResult get_vk_supported_formats(const util::vector<drm_format_pair> &drm_supported_formats,
105                                          vk_format_set &vk_supported_formats)
106 {
107    for (const auto &drm_format : drm_supported_formats)
108    {
109       const VkFormat vk_format = util::drm::drm_to_vk_format(drm_format.fourcc);
110       if (vk_format != VK_FORMAT_UNDEFINED)
111       {
112          auto it = vk_supported_formats.try_insert(vk_format);
113          if (!it.has_value())
114          {
115             return VK_ERROR_OUT_OF_HOST_MEMORY;
116          }
117       }
118       const VkFormat srgb_vk_format = util::drm::drm_to_vk_srgb_format(drm_format.fourcc);
119       if (srgb_vk_format != VK_FORMAT_UNDEFINED)
120       {
121          auto it = vk_supported_formats.try_insert(srgb_vk_format);
122          if (!it.has_value())
123          {
124             return VK_ERROR_OUT_OF_HOST_MEMORY;
125          }
126       }
127    }
128    return VK_SUCCESS;
129 }
130
131 VkResult surface_properties::get_surface_formats(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
132                                                  uint32_t *surfaceFormatCount, VkSurfaceFormatKHR *surfaceFormats)
133 {
134    assert(specific_surface);
135    if (!supported_formats.size())
136    {
137       VkResult res = get_vk_supported_formats(specific_surface->get_formats(), supported_formats);
138       if (res != VK_SUCCESS)
139       {
140          return res;
141       }
142    }
143
144    assert(surfaceFormatCount != nullptr);
145    if (nullptr == surfaceFormats)
146    {
147       *surfaceFormatCount = supported_formats.size();
148       return VK_SUCCESS;
149    }
150
151    VkResult res = VK_SUCCESS;
152
153    if (supported_formats.size() > *surfaceFormatCount)
154    {
155       res = VK_INCOMPLETE;
156    }
157
158    uint32_t format_count = 0;
159    for (const auto &format : supported_formats)
160    {
161       if (format_count >= *surfaceFormatCount)
162       {
163          break;
164       }
165       surfaceFormats[format_count].format = format;
166       surfaceFormats[format_count++].colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
167    }
168    *surfaceFormatCount = format_count;
169
170    return res;
171 }
172
173 VkResult surface_properties::get_surface_present_modes(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
174                                                        uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes)
175 {
176
177    VkResult res = VK_SUCCESS;
178
179    static std::array<const VkPresentModeKHR, 2> modes = {
180       VK_PRESENT_MODE_FIFO_KHR,
181       VK_PRESENT_MODE_MAILBOX_KHR,
182    };
183
184    assert(pPresentModeCount != nullptr);
185
186    if (nullptr == pPresentModes)
187    {
188       *pPresentModeCount = modes.size();
189    }
190    else
191    {
192       if (modes.size() > *pPresentModeCount)
193       {
194          res = VK_INCOMPLETE;
195       }
196       *pPresentModeCount = std::min(*pPresentModeCount, static_cast<uint32_t>(modes.size()));
197       for (uint32_t i = 0; i < *pPresentModeCount; ++i)
198       {
199          pPresentModes[i] = modes[i];
200       }
201    }
202
203    return res;
204 }
205
206 static const char *required_device_extensions[] = {
207    VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
208    VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
209    VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
210    VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
211    VK_KHR_MAINTENANCE1_EXTENSION_NAME,
212    VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
213    VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
214    VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
215    VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
216    VK_KHR_EXTERNAL_FENCE_EXTENSION_NAME,
217    VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME,
218 };
219
220 VkResult surface_properties::get_required_device_extensions(util::extension_list &extension_list)
221 {
222    return extension_list.add(required_device_extensions, NELEMS(required_device_extensions));
223 }
224
225 /* TODO: Check for zwp_linux_dmabuf_v1 protocol in display */
226 VkBool32 GetPhysicalDeviceWaylandPresentationSupportKHR(VkPhysicalDevice physical_device, uint32_t queue_index,
227                                                         struct wl_display *display)
228 {
229    bool dev_supports_sync =
230       sync_fd_fence_sync::is_supported(layer::instance_private_data::get(physical_device), physical_device);
231    if (!dev_supports_sync)
232    {
233       return VK_FALSE;
234    }
235
236    return VK_TRUE;
237 }
238
239 extern "C" VkResult CreateWaylandSurfaceKHR(VkInstance instance, const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
240                                             const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
241 {
242    auto &instance_data = layer::instance_private_data::get(instance);
243    util::allocator allocator{ instance_data.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT, pAllocator };
244    auto wsi_surface = surface::make_surface(allocator, pCreateInfo->display, pCreateInfo->surface);
245    if (wsi_surface == nullptr)
246    {
247       return VK_ERROR_OUT_OF_HOST_MEMORY;
248    }
249
250    VkResult res = instance_data.disp.CreateWaylandSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
251    if (res == VK_SUCCESS)
252    {
253       auto surface_base = util::unique_ptr<wsi::surface>(std::move(wsi_surface));
254       res = instance_data.add_surface(*pSurface, surface_base);
255       if (res != VK_SUCCESS)
256       {
257          instance_data.disp.DestroySurfaceKHR(instance, *pSurface, pAllocator);
258       }
259    }
260    return res;
261 }
262
263 PFN_vkVoidFunction surface_properties::get_proc_addr(const char *name)
264 {
265    if (strcmp(name, "vkGetPhysicalDeviceWaylandPresentationSupportKHR") == 0)
266    {
267       return reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceWaylandPresentationSupportKHR);
268    }
269    else if (strcmp(name, "vkCreateWaylandSurfaceKHR") == 0)
270    {
271       return reinterpret_cast<PFN_vkVoidFunction>(CreateWaylandSurfaceKHR);
272    }
273    return nullptr;
274 }
275
276 } // namespace wayland
277 } // namespace wsi