Merging gst-plugins-ugly
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / wayland / wldisplay.c
1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2014 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "wldisplay.h"
26 #include "wlbuffer.h"
27 #include "wlvideoformat.h"
28
29 #include <errno.h>
30
31 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
32 #define GST_CAT_DEFAULT gstwayland_debug
33
34 G_DEFINE_TYPE (GstWlDisplay, gst_wl_display, G_TYPE_OBJECT);
35
36 static void gst_wl_display_finalize (GObject * gobject);
37
38 static void
39 gst_wl_display_class_init (GstWlDisplayClass * klass)
40 {
41   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
42   gobject_class->finalize = gst_wl_display_finalize;
43 }
44
45 static void
46 gst_wl_display_init (GstWlDisplay * self)
47 {
48   self->shm_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
49   self->dmabuf_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
50   self->wl_fd_poll = gst_poll_new (TRUE);
51   self->buffers = g_hash_table_new (g_direct_hash, g_direct_equal);
52   g_mutex_init (&self->buffers_mutex);
53 }
54
55 static void
56 gst_wl_ref_wl_buffer (gpointer key, gpointer value, gpointer user_data)
57 {
58   g_object_ref (value);
59 }
60
61 static void
62 gst_wl_display_finalize (GObject * gobject)
63 {
64   GstWlDisplay *self = GST_WL_DISPLAY (gobject);
65
66   gst_poll_set_flushing (self->wl_fd_poll, TRUE);
67   if (self->thread)
68     g_thread_join (self->thread);
69
70   /* to avoid buffers being unregistered from another thread
71    * at the same time, take their ownership */
72   g_mutex_lock (&self->buffers_mutex);
73   self->shutting_down = TRUE;
74   g_hash_table_foreach (self->buffers, gst_wl_ref_wl_buffer, NULL);
75   g_mutex_unlock (&self->buffers_mutex);
76
77   g_hash_table_foreach (self->buffers,
78       (GHFunc) gst_wl_buffer_force_release_and_unref, NULL);
79   g_hash_table_remove_all (self->buffers);
80
81   g_array_unref (self->shm_formats);
82   g_array_unref (self->dmabuf_formats);
83   gst_poll_free (self->wl_fd_poll);
84   g_hash_table_unref (self->buffers);
85   g_mutex_clear (&self->buffers_mutex);
86
87   if (self->viewporter)
88     wp_viewporter_destroy (self->viewporter);
89
90   if (self->shm)
91     wl_shm_destroy (self->shm);
92
93   if (self->dmabuf)
94     zwp_linux_dmabuf_v1_destroy (self->dmabuf);
95
96   if (self->wl_shell)
97     wl_shell_destroy (self->wl_shell);
98
99   if (self->xdg_wm_base)
100     xdg_wm_base_destroy (self->xdg_wm_base);
101
102   if (self->fullscreen_shell)
103     zwp_fullscreen_shell_v1_release (self->fullscreen_shell);
104
105   if (self->compositor)
106     wl_compositor_destroy (self->compositor);
107
108   if (self->subcompositor)
109     wl_subcompositor_destroy (self->subcompositor);
110
111   if (self->registry)
112     wl_registry_destroy (self->registry);
113
114   if (self->display_wrapper)
115     wl_proxy_wrapper_destroy (self->display_wrapper);
116
117   if (self->queue)
118     wl_event_queue_destroy (self->queue);
119
120   if (self->own_display) {
121     wl_display_flush (self->display);
122     wl_display_disconnect (self->display);
123   }
124
125   G_OBJECT_CLASS (gst_wl_display_parent_class)->finalize (gobject);
126 }
127
128 static void
129 shm_format (void *data, struct wl_shm *wl_shm, uint32_t format)
130 {
131   GstWlDisplay *self = data;
132
133   g_array_append_val (self->shm_formats, format);
134 }
135
136 static const struct wl_shm_listener shm_listener = {
137   shm_format
138 };
139
140 static void
141 dmabuf_format (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
142     uint32_t format)
143 {
144   GstWlDisplay *self = data;
145
146   if (gst_wl_dmabuf_format_to_video_format (format) != GST_VIDEO_FORMAT_UNKNOWN)
147     g_array_append_val (self->dmabuf_formats, format);
148 }
149
150 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
151   dmabuf_format,
152 };
153
154 gboolean
155 gst_wl_display_check_format_for_shm (GstWlDisplay * display,
156     GstVideoFormat format)
157 {
158   enum wl_shm_format shm_fmt;
159   GArray *formats;
160   guint i;
161
162   shm_fmt = gst_video_format_to_wl_shm_format (format);
163   if (shm_fmt == (enum wl_shm_format) -1)
164     return FALSE;
165
166   formats = display->shm_formats;
167   for (i = 0; i < formats->len; i++) {
168     if (g_array_index (formats, uint32_t, i) == shm_fmt)
169       return TRUE;
170   }
171
172   return FALSE;
173 }
174
175 gboolean
176 gst_wl_display_check_format_for_dmabuf (GstWlDisplay * display,
177     GstVideoFormat format)
178 {
179   GArray *formats;
180   guint i, dmabuf_fmt;
181
182   if (!display->dmabuf)
183     return FALSE;
184
185   dmabuf_fmt = gst_video_format_to_wl_dmabuf_format (format);
186   if (dmabuf_fmt == (guint) - 1)
187     return FALSE;
188
189   formats = display->dmabuf_formats;
190   for (i = 0; i < formats->len; i++) {
191     if (g_array_index (formats, uint32_t, i) == dmabuf_fmt)
192       return TRUE;
193   }
194
195   return FALSE;
196 }
197
198 static void
199 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
200     uint32_t serial)
201 {
202   xdg_wm_base_pong (xdg_wm_base, serial);
203 }
204
205 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
206   handle_xdg_wm_base_ping
207 };
208
209 static void
210 registry_handle_global (void *data, struct wl_registry *registry,
211     uint32_t id, const char *interface, uint32_t version)
212 {
213   GstWlDisplay *self = data;
214
215   if (g_strcmp0 (interface, "wl_compositor") == 0) {
216     self->compositor = wl_registry_bind (registry, id, &wl_compositor_interface,
217         MIN (version, 3));
218   } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
219     self->subcompositor =
220         wl_registry_bind (registry, id, &wl_subcompositor_interface, 1);
221   } else if (g_strcmp0 (interface, "wl_shell") == 0) {
222     self->wl_shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
223   } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
224     self->xdg_wm_base =
225         wl_registry_bind (registry, id, &xdg_wm_base_interface, 1);
226     xdg_wm_base_add_listener (self->xdg_wm_base, &xdg_wm_base_listener, self);
227   } else if (g_strcmp0 (interface, "zwp_fullscreen_shell_v1") == 0) {
228     self->fullscreen_shell = wl_registry_bind (registry, id,
229         &zwp_fullscreen_shell_v1_interface, 1);
230   } else if (g_strcmp0 (interface, "wl_shm") == 0) {
231     self->shm = wl_registry_bind (registry, id, &wl_shm_interface, 1);
232     wl_shm_add_listener (self->shm, &shm_listener, self);
233   } else if (g_strcmp0 (interface, "wp_viewporter") == 0) {
234     self->viewporter =
235         wl_registry_bind (registry, id, &wp_viewporter_interface, 1);
236   } else if (g_strcmp0 (interface, "zwp_linux_dmabuf_v1") == 0) {
237     self->dmabuf =
238         wl_registry_bind (registry, id, &zwp_linux_dmabuf_v1_interface, 1);
239     zwp_linux_dmabuf_v1_add_listener (self->dmabuf, &dmabuf_listener, self);
240   }
241 }
242
243 static void
244 registry_handle_global_remove (void *data, struct wl_registry *registry,
245     uint32_t name)
246 {
247   /* temporarily do nothing */
248 }
249
250 static const struct wl_registry_listener registry_listener = {
251   registry_handle_global,
252   registry_handle_global_remove
253 };
254
255 static gpointer
256 gst_wl_display_thread_run (gpointer data)
257 {
258   GstWlDisplay *self = data;
259   GstPollFD pollfd = GST_POLL_FD_INIT;
260
261   pollfd.fd = wl_display_get_fd (self->display);
262   gst_poll_add_fd (self->wl_fd_poll, &pollfd);
263   gst_poll_fd_ctl_read (self->wl_fd_poll, &pollfd, TRUE);
264
265   /* main loop */
266   while (1) {
267     while (wl_display_prepare_read_queue (self->display, self->queue) != 0)
268       wl_display_dispatch_queue_pending (self->display, self->queue);
269     wl_display_flush (self->display);
270
271     if (gst_poll_wait (self->wl_fd_poll, GST_CLOCK_TIME_NONE) < 0) {
272       gboolean normal = (errno == EBUSY);
273       wl_display_cancel_read (self->display);
274       if (normal)
275         break;
276       else
277         goto error;
278     }
279     if (wl_display_read_events (self->display) == -1)
280       goto error;
281     wl_display_dispatch_queue_pending (self->display, self->queue);
282   }
283
284   return NULL;
285
286 error:
287   GST_ERROR ("Error communicating with the wayland server");
288   return NULL;
289 }
290
291 GstWlDisplay *
292 gst_wl_display_new (const gchar * name, GError ** error)
293 {
294   struct wl_display *display;
295
296   display = wl_display_connect (name);
297
298   if (!display) {
299     *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
300         "Failed to connect to the wayland display '%s'",
301         name ? name : "(default)");
302     return NULL;
303   } else {
304     return gst_wl_display_new_existing (display, TRUE, error);
305   }
306 }
307
308 GstWlDisplay *
309 gst_wl_display_new_existing (struct wl_display * display,
310     gboolean take_ownership, GError ** error)
311 {
312   GstWlDisplay *self;
313   GError *err = NULL;
314   gint i;
315
316   g_return_val_if_fail (display != NULL, NULL);
317
318   self = g_object_new (GST_TYPE_WL_DISPLAY, NULL);
319   self->display = display;
320   self->display_wrapper = wl_proxy_create_wrapper (display);
321   self->own_display = take_ownership;
322
323   self->queue = wl_display_create_queue (self->display);
324   wl_proxy_set_queue ((struct wl_proxy *) self->display_wrapper, self->queue);
325   self->registry = wl_display_get_registry (self->display_wrapper);
326   wl_registry_add_listener (self->registry, &registry_listener, self);
327
328   /* we need exactly 2 roundtrips to discover global objects and their state */
329   for (i = 0; i < 2; i++) {
330     if (wl_display_roundtrip_queue (self->display, self->queue) < 0) {
331       *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
332           "Error communicating with the wayland display");
333       g_object_unref (self);
334       return NULL;
335     }
336   }
337
338   /* verify we got all the required interfaces */
339 #define VERIFY_INTERFACE_EXISTS(var, interface) \
340   if (!self->var) { \
341     g_set_error (error, g_quark_from_static_string ("GstWlDisplay"), 0, \
342         "Could not bind to " interface ". Either it is not implemented in " \
343         "the compositor, or the implemented version doesn't match"); \
344     g_object_unref (self); \
345     return NULL; \
346   }
347
348   VERIFY_INTERFACE_EXISTS (compositor, "wl_compositor");
349   VERIFY_INTERFACE_EXISTS (subcompositor, "wl_subcompositor");
350   VERIFY_INTERFACE_EXISTS (shm, "wl_shm");
351
352 #undef VERIFY_INTERFACE_EXISTS
353
354   /* We make the viewporter optional even though it may cause bad display.
355    * This is so one can test wayland display on older compositor or on
356    * compositor that don't implement this extension. */
357   if (!self->viewporter) {
358     g_warning ("Wayland compositor is missing the ability to scale, video "
359         "display may not work properly.");
360   }
361
362   if (!self->dmabuf) {
363     g_warning ("Could not bind to zwp_linux_dmabuf_v1");
364   }
365
366   if (!self->wl_shell && !self->xdg_wm_base && !self->fullscreen_shell) {
367     /* If wl_surface and wl_display are passed via GstContext
368      * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
369      * In this case is correct to continue.
370      */
371     g_warning ("Could not bind to either wl_shell, xdg_wm_base or "
372         "zwp_fullscreen_shell, video display may not work properly.");
373   }
374
375   self->thread = g_thread_try_new ("GstWlDisplay", gst_wl_display_thread_run,
376       self, &err);
377   if (err) {
378     g_propagate_prefixed_error (error, err,
379         "Failed to start thread for the display's events");
380     g_object_unref (self);
381     return NULL;
382   }
383
384   return self;
385 }
386
387 void
388 gst_wl_display_register_buffer (GstWlDisplay * self, gpointer gstmem,
389     gpointer wlbuffer)
390 {
391   g_assert (!self->shutting_down);
392
393   GST_TRACE_OBJECT (self, "registering GstWlBuffer %p to GstMem %p",
394       wlbuffer, gstmem);
395
396   g_mutex_lock (&self->buffers_mutex);
397   g_hash_table_replace (self->buffers, gstmem, wlbuffer);
398   g_mutex_unlock (&self->buffers_mutex);
399 }
400
401 gpointer
402 gst_wl_display_lookup_buffer (GstWlDisplay * self, gpointer gstmem)
403 {
404   gpointer wlbuffer;
405   g_mutex_lock (&self->buffers_mutex);
406   wlbuffer = g_hash_table_lookup (self->buffers, gstmem);
407   g_mutex_unlock (&self->buffers_mutex);
408   return wlbuffer;
409 }
410
411 void
412 gst_wl_display_unregister_buffer (GstWlDisplay * self, gpointer gstmem)
413 {
414   GST_TRACE_OBJECT (self, "unregistering GstWlBuffer owned by %p", gstmem);
415
416   g_mutex_lock (&self->buffers_mutex);
417   if (G_LIKELY (!self->shutting_down))
418     g_hash_table_remove (self->buffers, gstmem);
419   g_mutex_unlock (&self->buffers_mutex);
420 }