1d453c22dbd8bfd090cc26d7e687927539d787fb
[platform/core/uifw/vulkan-wsi-tizen.git] / wsi / wayland / wl_helpers.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 #include "wl_helpers.hpp"
26
27 #include <cstring>
28 #include <memory>
29 #include <poll.h>
30 #include <errno.h>
31 #include <cassert>
32
33 #include "wl_object_owner.hpp"
34
35 #include "util/log.hpp"
36
37 struct formats_vector
38 {
39    util::vector<drm_format_pair> *formats{nullptr};
40    bool is_out_of_memory{false};
41 };
42
43 namespace
44 {
45    /* Handler for format event of the zwp_linux_dmabuf_v1 interface. */
46    extern "C" void dma_buf_format_handler(void *data,
47                                           struct zwp_linux_dmabuf_v1 *dma_buf,
48                                           uint32_t drm_format) {}
49
50    /* Handler for modifier event of the zwp_linux_dmabuf_v1 interface. */
51    extern "C" void dma_buf_modifier_handler(void *data,
52                                             struct zwp_linux_dmabuf_v1 *dma_buf,
53                                             uint32_t drm_format, uint32_t modifier_hi,
54                                             uint32_t modifier_low)
55    {
56       auto *drm_supported_formats = reinterpret_cast<formats_vector *>(data);
57
58       drm_format_pair format = {};
59       format.fourcc = drm_format;
60       format.modifier = (static_cast<uint64_t>(modifier_hi) << 32) | modifier_low;
61
62       if (!drm_supported_formats->formats->try_push_back(format))
63       {
64          drm_supported_formats->is_out_of_memory = true;
65       }
66    }
67 }
68
69 VkResult get_supported_formats_and_modifiers(
70    wl_display* display, zwp_linux_dmabuf_v1 *dmabuf_interface,
71    util::vector<drm_format_pair> &supported_formats)
72 {
73    formats_vector drm_supported_formats;
74    drm_supported_formats.formats = &supported_formats;
75
76    const zwp_linux_dmabuf_v1_listener dma_buf_listener = {
77       .format = dma_buf_format_handler, .modifier = dma_buf_modifier_handler,
78    };
79    int res = zwp_linux_dmabuf_v1_add_listener(dmabuf_interface, &dma_buf_listener,
80                                               &drm_supported_formats);
81    if (res < 0)
82    {
83       WSI_LOG_ERROR("Failed to add zwp_linux_dmabuf_v1 listener.");
84       return VK_ERROR_UNKNOWN;
85    }
86
87    /* Get all modifier events. */
88    res = wl_display_roundtrip(display);
89    if (res < 0)
90    {
91       WSI_LOG_ERROR("Roundtrip failed.");
92       return VK_ERROR_UNKNOWN;
93    }
94
95    if (drm_supported_formats.is_out_of_memory)
96    {
97       WSI_LOG_ERROR("Host got out of memory.");
98       return VK_ERROR_OUT_OF_HOST_MEMORY;
99    }
100
101    return VK_SUCCESS;
102 }
103
104 extern "C" {
105
106    void registry_handler(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface,
107                          uint32_t version)
108    {
109       auto dmabuf_interface = reinterpret_cast<wsi::wayland::zwp_linux_dmabuf_v1_owner* >(data);
110
111       if (!strcmp(interface, "zwp_linux_dmabuf_v1"))
112       {
113          version = ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION;
114          zwp_linux_dmabuf_v1 *dmabuf_interface_obj =
115             reinterpret_cast<zwp_linux_dmabuf_v1 *>(wl_registry_bind(
116                wl_registry, name, &zwp_linux_dmabuf_v1_interface, version));
117
118          if (dmabuf_interface_obj == nullptr)
119          {
120             WSI_LOG_ERROR("Failed to get zwp_linux_dmabuf_v1 interface.");
121             return;
122          }
123
124          dmabuf_interface->reset(dmabuf_interface_obj);
125       }
126    }
127
128    int dispatch_queue(struct wl_display *display, struct wl_event_queue *queue, int timeout)
129    {
130       int err;
131       struct pollfd pfd = {};
132       int retval;
133
134       /* Before we sleep, dispatch any pending events. prepare_read_queue will return 0 whilst there are pending
135        * events to dispatch on the queue. */
136       while (0 != wl_display_prepare_read_queue(display, queue))
137       {
138          /* dispatch_queue_pending returns -1 on error, or the number of events dispatched otherwise. If we
139           * already dispatched some events, then we might not need to sleep, as we might have just dispatched
140           * the event we want, so return immediately. */
141          err = wl_display_dispatch_queue_pending(display, queue);
142          if (err)
143          {
144             return (0 > err) ? -1 : 1;
145          }
146       }
147
148       /* wl_display_read_events performs a non-blocking read. */
149       pfd.fd = wl_display_get_fd(display);
150       pfd.events = POLLIN;
151       while (true)
152       {
153          /* Timeout is given in milliseconds. A return value of 0, or -1 with errno set to EINTR means that we
154           * should retry as the timeout was exceeded or we were interrupted by a signal, respectively. A
155           * return value of 1 means that something happened, and we should inspect the pollfd structure to see
156           * just what that was.
157           */
158          err = poll(&pfd, 1, timeout);
159          if (0 == err)
160          {
161             /* Timeout. */
162             wl_display_cancel_read(display);
163             return 0;
164          }
165          else if (-1 == err)
166          {
167             if (EINTR == errno)
168             {
169                /* Interrupted by a signal; restart. This resets the timeout. */
170                continue;
171             }
172             else
173             {
174                /* Something else bad happened; abort. */
175                wl_display_cancel_read(display);
176                return -1;
177             }
178          }
179          else
180          {
181             if (POLLIN == pfd.revents)
182             {
183                /* We have data to read, and no errors; proceed to read_events. */
184                break;
185             }
186             else
187             {
188                /* An error occurred, e.g. file descriptor was closed from underneath us. */
189                wl_display_cancel_read(display);
190                return -1;
191             }
192          }
193       }
194
195       /* Actually read the events from the display. A failure in read_events calls cancel_read internally for us,
196        * so we don't need to do that here. */
197       err = wl_display_read_events(display);
198       if (0 != err)
199       {
200          return -1;
201       }
202
203       /* Finally, if we read any events relevant to our queue, we can dispatch them. */
204       err = wl_display_dispatch_queue_pending(display, queue);
205       retval = err < 0 ? -1 : 1;
206
207       return retval;
208    }
209 }