b98527ecc0c7d6d772dfa6054b34df07d3c74b5c
[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 /**
49  * Wayland callback for global wl_registry events to handle global objects required by @ref wsi::wayland::surface
50  */
51 extern "C" void surface_registry_handler(void *data, struct wl_registry *wl_registry, uint32_t name,
52                                          const char *interface, uint32_t version);
53
54 class surface : public wsi::surface
55 {
56 public:
57    surface() = delete;
58    struct init_parameters;
59
60    /** Constructor to allow for custom allocation, but require privately defined arguments. */
61    surface(const init_parameters&);
62
63    /**
64     * @brief Allocates and initializes a surface
65     *
66     * @param allocator An allocator to use for host allocations needed for the surface.
67     * @param display   The Wayland display used to create the VkSurface
68     * @param surf      The Wayland surface used to create the VkSurface
69     *
70     * @return A constructed and initalized surface or nullptr on failure
71     */
72    static util::unique_ptr<surface> make_surface(const util::allocator &allocator, wl_display *display,
73                                                  wl_surface *surf);
74
75    /** Destructor */
76    ~surface() override;
77
78    wsi::surface_properties &get_properties() override;
79    util::unique_ptr<swapchain_base> allocate_swapchain(layer::device_private_data &dev_data,
80                                                        const VkAllocationCallbacks *allocator) override;
81
82    /** Returns the Wayland display */
83    wl_display *get_wl_display() const
84    {
85       return wayland_display;
86    }
87
88    /** Returns the Wayland surface */
89    wl_surface *get_wl_surface() const
90    {
91       return wayland_surface;
92    }
93
94    /**
95     * @brief Returns a pointer to the Wayland zwp_linux_dmabuf_v1 interface.
96     *
97     * The raw pointer is valid throughout the lifetime of this surface.
98     */
99    zwp_linux_dmabuf_v1 *get_dmabuf_interface()
100    {
101       return dmabuf_interface.get();
102    }
103
104    /**
105     * @brief Returns a pointer to the Wayland zwp_linux_surface_synchronization_v1 interface obtained for the wayland
106     *        surface.
107     *
108     * The raw pointer is valid for the lifetime of the surface.
109     */
110    zwp_linux_surface_synchronization_v1* get_surface_sync_interface()
111    {
112       return surface_sync_interface.get();
113    }
114
115    /**
116     * @brief Returns a reference to a list of DRM formats supported by the Wayland surface.
117     *
118     * The reference is valid throughout the lifetime of this surface.
119     */
120    const util::vector<drm_format_pair> &get_formats() const
121    {
122       return supported_formats;
123    }
124
125 private:
126    /**
127     * @brief Initialize the WSI surface by creating Wayland queues and linking to Wayland protocols.
128     *
129     * @return true on success, false otherwise.
130     */
131    bool init();
132
133    friend void surface_registry_handler(void *data, struct wl_registry *wl_registry, uint32_t name,
134                                         const char *interface, uint32_t version);
135
136    /** The native Wayland display */
137    wl_display *wayland_display;
138    /** The native Wayland surface */
139    wl_surface *wayland_surface;
140    /** A list of DRM formats supported by the Wayland compositor on this surface */
141    util::vector<drm_format_pair> supported_formats;
142    /** Surface properties specific to the Wayland surface. */
143    surface_properties properties;
144
145    /** Container for the zwp_linux_dmabuf_v1 interface binding */
146    wayland_owner<zwp_linux_dmabuf_v1> dmabuf_interface;
147
148    /** Container for the zwp_linux_explicit_synchronization_v1 interface binding */
149    wayland_owner<zwp_linux_explicit_synchronization_v1> explicit_sync_interface;
150    /** Container for the surface specific zwp_linux_surface_synchronization_v1 interface. */
151    wayland_owner<zwp_linux_surface_synchronization_v1> surface_sync_interface;
152
153    /** Private queue for surface events generated by the layer */
154    wl_event_queue *surface_queue;
155 };
156
157 } // namespace wayland
158 } // namespace wsi