• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2010  Intel Corporation.
4  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
5  * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  * Authors:
23  *  Matthew Allum
24  *  Robert Bragg
25  *  Kristian Høgsberg
26  */
27 
28 /* code originally from clutter's wayland backend found here
29  * http://git.gnome.org/browse/clutter/tree/clutter/wayland/clutter-event-wayland.c
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <wayland-client.h>
39 
40 #include <gst/vulkan/wayland/gstvkdisplay_wayland.h>
41 
42 #include "wayland_event_source.h"
43 
44 static void
sync_callback(void * data,struct wl_callback * callback,uint32_t serial)45 sync_callback (void *data, struct wl_callback *callback, uint32_t serial)
46 {
47   gboolean *done = data;
48 
49   *done = TRUE;
50   wl_callback_destroy (callback);
51 }
52 
53 static const struct wl_callback_listener sync_listener = {
54   sync_callback
55 };
56 
57 /* only thread safe iff called on the same thread @queue is being dispatched on */
58 gint
gst_vulkan_wl_display_roundtrip_queue(struct wl_display * display,struct wl_event_queue * queue)59 gst_vulkan_wl_display_roundtrip_queue (struct wl_display *display,
60     struct wl_event_queue *queue)
61 {
62   struct wl_callback *callback;
63   gboolean done = FALSE;
64   gint ret = 0;
65 
66   if (queue) {
67     /* creating a wl_proxy and setting the queue is racy with the dispatching
68      * of the default queue */
69     while (wl_display_prepare_read_queue (display, queue) != 0) {
70       if ((ret = wl_display_dispatch_queue_pending (display, queue)) < 0)
71         return ret;
72     }
73   }
74   if (!(callback = wl_display_sync (display))) {
75     return -1;
76   }
77   wl_callback_add_listener (callback, &sync_listener, &done);
78   if (queue) {
79     wl_proxy_set_queue ((struct wl_proxy *) callback, queue);
80     wl_display_cancel_read (display);
81     while (!done && ret >= 0)
82       ret = wl_display_dispatch_queue (display, queue);
83   } else {
84     while (!done && ret >= 0)
85       ret = wl_display_dispatch (display);
86   }
87 
88   if (ret == -1 && !done)
89     wl_callback_destroy (callback);
90 
91   return ret;
92 }
93 
94 typedef struct _WaylandEventSource
95 {
96   GSource source;
97   GPollFD pfd;
98   uint32_t mask;
99   struct wl_display *display;
100   struct wl_event_queue *queue;
101   gboolean reading;
102 } WaylandEventSource;
103 
104 static gboolean
wayland_event_source_prepare(GSource * base,gint * timeout)105 wayland_event_source_prepare (GSource * base, gint * timeout)
106 {
107   WaylandEventSource *source = (WaylandEventSource *) base;
108 
109   *timeout = -1;
110 
111   /* we may be called multiple times for prepare */
112   if (source->reading)
113     wl_display_cancel_read (source->display);
114 
115   if (source->queue) {
116     while (wl_display_prepare_read_queue (source->display, source->queue) != 0) {
117       if (wl_display_dispatch_queue_pending (source->display,
118               source->queue) < 0) {
119         g_critical ("Failed to dispatch pending events\n");
120       }
121     }
122   } else {
123     while (wl_display_prepare_read (source->display) != 0) {
124       if (wl_display_dispatch_pending (source->display) < 0) {
125         g_critical ("Failed to dispatch pending events\n");
126       }
127     }
128   }
129   source->reading = TRUE;
130 
131   /* FIXME: this may return EAGAIN if the fd is full */
132   if (wl_display_flush (source->display) < 0)
133     g_critical ("Failed to flush Wayland connection\n");
134 
135   return FALSE;
136 }
137 
138 static gboolean
wayland_event_source_check(GSource * base)139 wayland_event_source_check (GSource * base)
140 {
141   WaylandEventSource *source = (WaylandEventSource *) base;
142   gboolean retval;
143 
144   retval = source->pfd.revents;
145 
146   if (source->pfd.revents & G_IO_IN) {
147     wl_display_read_events (source->display);
148   } else {
149     wl_display_cancel_read (source->display);
150   }
151   source->reading = FALSE;
152 
153   return retval;
154 }
155 
156 static gboolean
wayland_event_source_dispatch(GSource * base,GSourceFunc callback,gpointer data)157 wayland_event_source_dispatch (GSource * base,
158     GSourceFunc callback, gpointer data)
159 {
160   WaylandEventSource *source = (WaylandEventSource *) base;
161 
162   if (source->queue)
163     wl_display_dispatch_queue_pending (source->display, source->queue);
164   else
165     wl_display_dispatch_pending (source->display);
166   source->pfd.revents = 0;
167 
168   if (callback)
169     callback (data);
170 
171   return TRUE;
172 }
173 
174 static void
wayland_event_source_finalize(GSource * base)175 wayland_event_source_finalize (GSource * base)
176 {
177   WaylandEventSource *source = (WaylandEventSource *) base;
178 
179   if (source->reading) {
180     wl_display_cancel_read (source->display);
181   }
182   source->reading = FALSE;
183 }
184 
185 static GSourceFuncs wayland_event_source_funcs = {
186   wayland_event_source_prepare,
187   wayland_event_source_check,
188   wayland_event_source_dispatch,
189   wayland_event_source_finalize
190 };
191 
192 GSource *
wayland_event_source_new(struct wl_display * display,struct wl_event_queue * queue)193 wayland_event_source_new (struct wl_display *display,
194     struct wl_event_queue *queue)
195 {
196   WaylandEventSource *source;
197 
198   source = (WaylandEventSource *)
199       g_source_new (&wayland_event_source_funcs, sizeof (WaylandEventSource));
200   source->display = display;
201   source->queue = queue;
202   source->pfd.fd = wl_display_get_fd (display);
203   source->pfd.events = G_IO_IN | G_IO_ERR;
204   g_source_add_poll (&source->source, &source->pfd);
205 
206   return &source->source;
207 }
208