wsi: Move common objects to the wayland wsi::surface
[platform/core/uifw/vulkan-wsi-tizen.git] / wsi / wayland / surface.hpp
1 /*
2  * Copyright (c) 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 /** @file
26  * @brief Definitions for a Wayland WSI Surface
27  */
28
29 #pragma once
30
31 #include <wayland-client.h>
32
33 #include "wsi/surface.hpp"
34 #include "surface_properties.hpp"
35 #include "wl_object_owner.hpp"
36
37 namespace wsi
38 {
39 namespace wayland
40 {
41
42 struct drm_format_pair
43 {
44    uint32_t fourcc;
45    uint64_t modifier;
46 };
47
48 class surface : public wsi::surface
49 {
50 public:
51    surface() = delete;
52    struct init_parameters;
53
54    /** Constructor to allow for custom allocation, but require privately defined arguments. */
55    surface(const init_parameters&);
56
57    /**
58     * @brief Allocates and initializes a surface
59     *
60     * @param allocator An allocator to use for host allocations needed for the surface.
61     * @param display   The Wayland display used to create the VkSurface
62     * @param surf      The Wayland surface used to create the VkSurface
63     *
64     * @return A constructed and initalized surface or nullptr on failure
65     */
66    static util::unique_ptr<surface> make_surface(const util::allocator &allocator, wl_display *display,
67                                                  wl_surface *surf);
68
69    /** Destructor */
70    ~surface() override;
71
72    wsi::surface_properties &get_properties() override;
73    util::unique_ptr<swapchain_base> allocate_swapchain(layer::device_private_data &dev_data,
74                                                        const VkAllocationCallbacks *allocator) override;
75
76    /** Returns the Wayland display */
77    wl_display *get_wl_display() const
78    {
79       return wayland_display;
80    }
81
82    /** Returns the Wayland surface */
83    wl_surface *get_wl_surface() const
84    {
85       return wayland_surface;
86    }
87
88    /**
89     * @brief Returns a pointer to the Wayland zwp_linux_dmabuf_v1 interface.
90     *
91     * The raw pointer is valid throughout the lifetime of this surface.
92     */
93    zwp_linux_dmabuf_v1 *get_dmabuf_interface()
94    {
95       return dmabuf_interface.get();
96    }
97
98    /**
99     * @brief Returns a reference to a list of DRM formats supported by the Wayland surface.
100     *
101     * The reference is valid throughout the lifetime of this surface.
102     */
103    const util::vector<drm_format_pair> &get_formats() const
104    {
105       return supported_formats;
106    }
107
108 private:
109    /**
110     * @brief Initialize the WSI surface by creating Wayland queues and linking to Wayland protocols.
111     *
112     * @return true on success, false otherwise.
113     */
114    bool init();
115
116    /** The native Wayland display */
117    wl_display *wayland_display;
118    /** The native Wayland surface */
119    wl_surface *wayland_surface;
120    /** A list of DRM formats supported by the Wayland compositor on this surface */
121    util::vector<drm_format_pair> supported_formats;
122    /** Surface properties specific to the Wayland surface. */
123    surface_properties properties;
124
125    /** Container for the zwp_linux_dmabuf_v1 interface binding */
126    zwp_linux_dmabuf_v1_owner dmabuf_interface;
127    /** Private queue for surface events generated by the layer */
128    wl_event_queue *surface_queue;
129 };
130
131 } // namespace wayland
132 } // namespace wsi