xref: /aosp_15_r20/external/mesa3d/src/loader/loader_wayland_helper.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2022 Red Hat, Inc.
3*61046927SAndroid Build Coastguard Worker  *
4*61046927SAndroid Build Coastguard Worker  * Permission to use, copy, modify, distribute, and sell this software and its
5*61046927SAndroid Build Coastguard Worker  * documentation for any purpose is hereby granted without fee, provided that
6*61046927SAndroid Build Coastguard Worker  * the above copyright notice appear in all copies and that both that copyright
7*61046927SAndroid Build Coastguard Worker  * notice and this permission notice appear in supporting documentation, and
8*61046927SAndroid Build Coastguard Worker  * that the name of the copyright holders not be used in advertising or
9*61046927SAndroid Build Coastguard Worker  * publicity pertaining to distribution of the software without specific,
10*61046927SAndroid Build Coastguard Worker  * written prior permission.  The copyright holders make no representations
11*61046927SAndroid Build Coastguard Worker  * about the suitability of this software for any purpose.  It is provided "as
12*61046927SAndroid Build Coastguard Worker  * is" without express or implied warranty.
13*61046927SAndroid Build Coastguard Worker  *
14*61046927SAndroid Build Coastguard Worker  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*61046927SAndroid Build Coastguard Worker  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*61046927SAndroid Build Coastguard Worker  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*61046927SAndroid Build Coastguard Worker  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*61046927SAndroid Build Coastguard Worker  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*61046927SAndroid Build Coastguard Worker  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20*61046927SAndroid Build Coastguard Worker  * OF THIS SOFTWARE.
21*61046927SAndroid Build Coastguard Worker  */
22*61046927SAndroid Build Coastguard Worker 
23*61046927SAndroid Build Coastguard Worker #include <poll.h>
24*61046927SAndroid Build Coastguard Worker #include <errno.h>
25*61046927SAndroid Build Coastguard Worker 
26*61046927SAndroid Build Coastguard Worker #include "util/perf/cpu_trace.h"
27*61046927SAndroid Build Coastguard Worker 
28*61046927SAndroid Build Coastguard Worker #include "loader_wayland_helper.h"
29*61046927SAndroid Build Coastguard Worker 
30*61046927SAndroid Build Coastguard Worker #ifndef HAVE_WL_DISPATCH_QUEUE_TIMEOUT
31*61046927SAndroid Build Coastguard Worker static int
wl_display_poll(struct wl_display * display,short int events,const struct timespec * timeout)32*61046927SAndroid Build Coastguard Worker wl_display_poll(struct wl_display *display,
33*61046927SAndroid Build Coastguard Worker                 short int events,
34*61046927SAndroid Build Coastguard Worker                 const struct timespec *timeout)
35*61046927SAndroid Build Coastguard Worker {
36*61046927SAndroid Build Coastguard Worker    int ret;
37*61046927SAndroid Build Coastguard Worker    struct pollfd pfd[1];
38*61046927SAndroid Build Coastguard Worker    struct timespec now;
39*61046927SAndroid Build Coastguard Worker    struct timespec deadline = {0};
40*61046927SAndroid Build Coastguard Worker    struct timespec result;
41*61046927SAndroid Build Coastguard Worker    struct timespec *remaining_timeout = NULL;
42*61046927SAndroid Build Coastguard Worker 
43*61046927SAndroid Build Coastguard Worker    if (timeout) {
44*61046927SAndroid Build Coastguard Worker       clock_gettime(CLOCK_MONOTONIC, &now);
45*61046927SAndroid Build Coastguard Worker       timespec_add(&deadline, &now, timeout);
46*61046927SAndroid Build Coastguard Worker    }
47*61046927SAndroid Build Coastguard Worker 
48*61046927SAndroid Build Coastguard Worker    pfd[0].fd = wl_display_get_fd(display);
49*61046927SAndroid Build Coastguard Worker    pfd[0].events = events;
50*61046927SAndroid Build Coastguard Worker    do {
51*61046927SAndroid Build Coastguard Worker       if (timeout) {
52*61046927SAndroid Build Coastguard Worker          clock_gettime(CLOCK_MONOTONIC, &now);
53*61046927SAndroid Build Coastguard Worker          timespec_sub_saturate(&result, &deadline, &now);
54*61046927SAndroid Build Coastguard Worker          remaining_timeout = &result;
55*61046927SAndroid Build Coastguard Worker       }
56*61046927SAndroid Build Coastguard Worker       ret = ppoll(pfd, 1, remaining_timeout, NULL);
57*61046927SAndroid Build Coastguard Worker    } while (ret == -1 && errno == EINTR);
58*61046927SAndroid Build Coastguard Worker 
59*61046927SAndroid Build Coastguard Worker    return ret;
60*61046927SAndroid Build Coastguard Worker }
61*61046927SAndroid Build Coastguard Worker 
62*61046927SAndroid Build Coastguard Worker int
wl_display_dispatch_queue_timeout(struct wl_display * display,struct wl_event_queue * queue,const struct timespec * timeout)63*61046927SAndroid Build Coastguard Worker wl_display_dispatch_queue_timeout(struct wl_display *display,
64*61046927SAndroid Build Coastguard Worker                                   struct wl_event_queue *queue,
65*61046927SAndroid Build Coastguard Worker                                   const struct timespec *timeout)
66*61046927SAndroid Build Coastguard Worker {
67*61046927SAndroid Build Coastguard Worker    int ret;
68*61046927SAndroid Build Coastguard Worker    struct timespec now;
69*61046927SAndroid Build Coastguard Worker    struct timespec deadline = {0};
70*61046927SAndroid Build Coastguard Worker    struct timespec result;
71*61046927SAndroid Build Coastguard Worker    struct timespec *remaining_timeout = NULL;
72*61046927SAndroid Build Coastguard Worker 
73*61046927SAndroid Build Coastguard Worker    if (timeout) {
74*61046927SAndroid Build Coastguard Worker       clock_gettime(CLOCK_MONOTONIC, &now);
75*61046927SAndroid Build Coastguard Worker       timespec_add(&deadline, &now, timeout);
76*61046927SAndroid Build Coastguard Worker    }
77*61046927SAndroid Build Coastguard Worker 
78*61046927SAndroid Build Coastguard Worker    if (wl_display_prepare_read_queue(display, queue) == -1)
79*61046927SAndroid Build Coastguard Worker       return wl_display_dispatch_queue_pending(display, queue);
80*61046927SAndroid Build Coastguard Worker 
81*61046927SAndroid Build Coastguard Worker    while (true) {
82*61046927SAndroid Build Coastguard Worker       ret = wl_display_flush(display);
83*61046927SAndroid Build Coastguard Worker 
84*61046927SAndroid Build Coastguard Worker       if (ret != -1 || errno != EAGAIN)
85*61046927SAndroid Build Coastguard Worker          break;
86*61046927SAndroid Build Coastguard Worker 
87*61046927SAndroid Build Coastguard Worker       if (timeout) {
88*61046927SAndroid Build Coastguard Worker          clock_gettime(CLOCK_MONOTONIC, &now);
89*61046927SAndroid Build Coastguard Worker          timespec_sub_saturate(&result, &deadline, &now);
90*61046927SAndroid Build Coastguard Worker          remaining_timeout = &result;
91*61046927SAndroid Build Coastguard Worker       }
92*61046927SAndroid Build Coastguard Worker       ret = wl_display_poll(display, POLLOUT, remaining_timeout);
93*61046927SAndroid Build Coastguard Worker 
94*61046927SAndroid Build Coastguard Worker       if (ret <= 0) {
95*61046927SAndroid Build Coastguard Worker          wl_display_cancel_read(display);
96*61046927SAndroid Build Coastguard Worker          return ret;
97*61046927SAndroid Build Coastguard Worker       }
98*61046927SAndroid Build Coastguard Worker    }
99*61046927SAndroid Build Coastguard Worker 
100*61046927SAndroid Build Coastguard Worker    /* Don't stop if flushing hits an EPIPE; continue so we can read any
101*61046927SAndroid Build Coastguard Worker     * protocol error that may have triggered it. */
102*61046927SAndroid Build Coastguard Worker    if (ret < 0 && errno != EPIPE) {
103*61046927SAndroid Build Coastguard Worker       wl_display_cancel_read(display);
104*61046927SAndroid Build Coastguard Worker       return -1;
105*61046927SAndroid Build Coastguard Worker    }
106*61046927SAndroid Build Coastguard Worker 
107*61046927SAndroid Build Coastguard Worker    while (true) {
108*61046927SAndroid Build Coastguard Worker       if (timeout) {
109*61046927SAndroid Build Coastguard Worker          clock_gettime(CLOCK_MONOTONIC, &now);
110*61046927SAndroid Build Coastguard Worker          timespec_sub_saturate(&result, &deadline, &now);
111*61046927SAndroid Build Coastguard Worker          remaining_timeout = &result;
112*61046927SAndroid Build Coastguard Worker       }
113*61046927SAndroid Build Coastguard Worker 
114*61046927SAndroid Build Coastguard Worker       ret = wl_display_poll(display, POLLIN, remaining_timeout);
115*61046927SAndroid Build Coastguard Worker       if (ret <= 0) {
116*61046927SAndroid Build Coastguard Worker          wl_display_cancel_read(display);
117*61046927SAndroid Build Coastguard Worker          break;
118*61046927SAndroid Build Coastguard Worker       }
119*61046927SAndroid Build Coastguard Worker 
120*61046927SAndroid Build Coastguard Worker       ret = wl_display_read_events(display);
121*61046927SAndroid Build Coastguard Worker       if (ret == -1)
122*61046927SAndroid Build Coastguard Worker          break;
123*61046927SAndroid Build Coastguard Worker 
124*61046927SAndroid Build Coastguard Worker       ret = wl_display_dispatch_queue_pending(display, queue);
125*61046927SAndroid Build Coastguard Worker       if (ret != 0)
126*61046927SAndroid Build Coastguard Worker          break;
127*61046927SAndroid Build Coastguard Worker 
128*61046927SAndroid Build Coastguard Worker       /* wl_display_dispatch_queue_pending can return 0 if we ended up reading
129*61046927SAndroid Build Coastguard Worker        * from WL fd, but there was no complete event to dispatch yet.
130*61046927SAndroid Build Coastguard Worker        * Try reading again. */
131*61046927SAndroid Build Coastguard Worker       if (wl_display_prepare_read_queue(display, queue) == -1)
132*61046927SAndroid Build Coastguard Worker          return wl_display_dispatch_queue_pending(display, queue);
133*61046927SAndroid Build Coastguard Worker    }
134*61046927SAndroid Build Coastguard Worker 
135*61046927SAndroid Build Coastguard Worker    return ret;
136*61046927SAndroid Build Coastguard Worker }
137*61046927SAndroid Build Coastguard Worker #endif
138*61046927SAndroid Build Coastguard Worker 
139*61046927SAndroid Build Coastguard Worker #ifndef HAVE_WL_CREATE_QUEUE_WITH_NAME
140*61046927SAndroid Build Coastguard Worker struct wl_event_queue *
wl_display_create_queue_with_name(struct wl_display * display,const char * name)141*61046927SAndroid Build Coastguard Worker wl_display_create_queue_with_name(struct wl_display *display, const char *name)
142*61046927SAndroid Build Coastguard Worker {
143*61046927SAndroid Build Coastguard Worker    return wl_display_create_queue(display);
144*61046927SAndroid Build Coastguard Worker }
145*61046927SAndroid Build Coastguard Worker #endif
146*61046927SAndroid Build Coastguard Worker 
147*61046927SAndroid Build Coastguard Worker int
loader_wayland_dispatch(struct wl_display * wl_display,struct wl_event_queue * queue,struct timespec * end_time)148*61046927SAndroid Build Coastguard Worker loader_wayland_dispatch(struct wl_display *wl_display,
149*61046927SAndroid Build Coastguard Worker                         struct wl_event_queue *queue,
150*61046927SAndroid Build Coastguard Worker                         struct timespec *end_time)
151*61046927SAndroid Build Coastguard Worker {
152*61046927SAndroid Build Coastguard Worker    struct timespec current_time;
153*61046927SAndroid Build Coastguard Worker    struct timespec remaining_timeout;
154*61046927SAndroid Build Coastguard Worker 
155*61046927SAndroid Build Coastguard Worker    MESA_TRACE_FUNC();
156*61046927SAndroid Build Coastguard Worker 
157*61046927SAndroid Build Coastguard Worker    if (!end_time)
158*61046927SAndroid Build Coastguard Worker       return wl_display_dispatch_queue(wl_display, queue);
159*61046927SAndroid Build Coastguard Worker 
160*61046927SAndroid Build Coastguard Worker    clock_gettime(CLOCK_MONOTONIC, &current_time);
161*61046927SAndroid Build Coastguard Worker    timespec_sub_saturate(&remaining_timeout, end_time, &current_time);
162*61046927SAndroid Build Coastguard Worker    return wl_display_dispatch_queue_timeout(wl_display,
163*61046927SAndroid Build Coastguard Worker                                             queue,
164*61046927SAndroid Build Coastguard Worker                                             &remaining_timeout);
165*61046927SAndroid Build Coastguard Worker }
166