wsi: Move common objects to the wayland wsi::surface
[platform/core/uifw/vulkan-wsi-tizen.git] / wsi / wayland / swapchain.hpp
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 #pragma once
26
27 #include "wsi/swapchain_base.hpp"
28
29 extern "C" {
30 #include <vulkan/vk_icd.h>
31 }
32
33 #include <wayland-client.h>
34 #include <linux-dmabuf-unstable-v1-client-protocol.h>
35 #include "util/wsialloc/wsialloc.h"
36 #include "wl_object_owner.hpp"
37 #include "surface.hpp"
38
39 namespace wsi
40 {
41 namespace wayland
42 {
43
44 class swapchain : public wsi::swapchain_base
45 {
46 public:
47    explicit swapchain(layer::device_private_data &dev_data, const VkAllocationCallbacks *allocator,
48                       surface &wsi_surface);
49
50    ~swapchain();
51
52    /* TODO: make the buffer destructor a friend? so this can be protected */
53    void release_buffer(struct wl_buffer *wl_buffer);
54
55 protected:
56    /**
57     * @brief Initialize platform specifics.
58     */
59    VkResult init_platform(VkDevice device, const VkSwapchainCreateInfoKHR *pSwapchainCreateInfo) override;
60
61    /**
62     * @brief Creates a new swapchain image.
63     *
64     * @param image_create_info Data to be used to create the image.
65     *
66     * @param image Handle to the image.
67     *
68     * @return If image creation is successful returns VK_SUCCESS, otherwise
69     * will return VK_ERROR_OUT_OF_DEVICE_MEMORY or VK_ERROR_INITIALIZATION_FAILED
70     * depending on the error that occurred.
71     */
72    VkResult create_image(VkImageCreateInfo image_create_info, swapchain_image &image) override;
73
74    /**
75     * @brief Method to present an image
76     *
77     * @param pendingIndex Index of the pending image to be presented.
78     */
79    void present_image(uint32_t pendingIndex) override;
80
81    /**
82     * @brief Method to release a swapchain image
83     *
84     * @param image Handle to the image about to be released.
85     */
86    void destroy_image(swapchain_image &image) override;
87
88    /**
89     * @brief Method to check if there are any free images
90     *
91     * @return true if any images are free, otherwise false.
92     */
93    bool free_image_found();
94
95    /**
96     * @brief Hook for any actions to free up a buffer for acquire
97     *
98     * @param[in,out] timeout time to wait, in nanoseconds. 0 doesn't block,
99     *                        UINT64_MAX waits indefinitely. The timeout should
100     *                        be updated if a sleep is required - this can
101     *                        be set to 0 if the semaphore is now not expected
102     *                        block.
103     */
104    VkResult get_free_buffer(uint64_t *timeout) override;
105
106 private:
107    struct wayland_image_data;
108
109    VkResult allocate_image(VkImageCreateInfo &image_create_info, wayland_image_data *image_data, VkImage *image);
110
111    struct wl_display *m_display;
112    struct wl_surface *m_surface;
113    struct zwp_linux_dmabuf_v1 *m_dmabuf_interface;
114    /* The queue on which we dispatch the swapchain related events, mostly frame completion */
115    struct wl_event_queue *m_swapchain_queue;
116    /* The queue on which we dispatch buffer related events, mostly buffer_release */
117    struct wl_event_queue *m_buffer_queue;
118
119    /**
120     * @brief Handle to the WSI allocator.
121     */
122    wsialloc_allocator *m_wsi_allocator;
123
124    /**
125     * @brief true when waiting for the server hint to present a buffer
126     *
127     * true if a buffer has been presented and we've not had a wl_surface::frame
128     * callback to indicate the server is ready for the next buffer.
129     */
130    bool m_present_pending;
131
132    /*
133     * @brief Allocate memory for an image plane.
134     *
135     * Allocates a VkDeviceMemory object from a given fd for an image plane. First
136     * it makes a call to get_fd_mem_type_index() to acquire the memory type for
137     * the given fd and then it allocates device memory by calling vkAllocateMemory().
138     *
139     * @param      fd     The plane's fd.
140     * @param[out] memory The allocated VkDeviceMemory object.
141     *
142     * @return VK_SUCCESS on success. If one of the functions that are being called
143     * fails its return value is returned. VK_ERROR_OUT_OF_HOST_MEMORY is returned
144     * when the host gets out of memory.
145     */
146    VkResult allocate_plane_memory(int fd, VkDeviceMemory *memory);
147
148    /*
149     * @brief Get the memory type which the specified file descriptor can be
150     * imported as.
151     *
152     * @param      fd      The given fd.
153     * @param[out] mem_idx The index of the supported memory type.
154     *
155     * @return VK_SUCCESS on success. On failure the error value of
156     * vkGetMemoryFdPropertiesKHR is returned.
157     */
158    VkResult get_fd_mem_type_index(int fd, uint32_t &mem_idx);
159
160    /*
161     * @brief Get the properties a format has when combined with a DRM modifier.
162     *
163     * @param      format            The target format.
164     * @param[out] format_props_list A vector which will store the supported properties
165     *                               for every modifier.
166     *
167     * @return VK_SUCCESS on success. VK_ERROR_OUT_OF_HOST_MEMORY is returned when
168     * the host gets out of memory.
169     */
170    VkResult get_drm_format_properties(
171       VkFormat format, util::vector<VkDrmFormatModifierPropertiesEXT> &format_props_list);
172 };
173 } // namespace wayland
174 } // namespace wsi