waylandsink: replace the custom buffer pool with an allocator
[platform/upstream/gstreamer.git] / ext / wayland / gstwaylandsink.c
1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2011 Intel Corporation
4  * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2014 Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * SECTION:element-waylandsink
26  *
27  *  The waylandsink is creating its own window and render the decoded video frames to that.
28  *  Setup the Wayland environment as described in
29  *  <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
30  *  The current implementaion is based on weston compositor.
31  *
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch -v videotestsrc ! waylandsink
36  * ]| test the video rendering in wayland
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "gstwaylandsink.h"
45 #include "wlvideoformat.h"
46 #include "wlbuffer.h"
47 #include "wlshmallocator.h"
48
49 #include <gst/wayland/wayland.h>
50 #include <gst/video/videooverlay.h>
51
52 /* signals */
53 enum
54 {
55   SIGNAL_0,
56   LAST_SIGNAL
57 };
58
59 /* Properties */
60 enum
61 {
62   PROP_0,
63   PROP_DISPLAY
64 };
65
66 GST_DEBUG_CATEGORY (gstwayland_debug);
67 #define GST_CAT_DEFAULT gstwayland_debug
68
69 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
73         ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
74             "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
75             "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
76     );
77
78 static void gst_wayland_sink_get_property (GObject * object,
79     guint prop_id, GValue * value, GParamSpec * pspec);
80 static void gst_wayland_sink_set_property (GObject * object,
81     guint prop_id, const GValue * value, GParamSpec * pspec);
82 static void gst_wayland_sink_finalize (GObject * object);
83
84 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
85     GstStateChange transition);
86 static void gst_wayland_sink_set_context (GstElement * element,
87     GstContext * context);
88
89 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
90     GstCaps * filter);
91 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
92 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
93     GstBuffer * buffer);
94 static gboolean
95 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
96 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
97     GstBuffer * buffer);
98
99 /* VideoOverlay interface */
100 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
101     iface);
102 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
103     guintptr handle);
104 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
105     gint x, gint y, gint w, gint h);
106 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
107
108 /* WaylandVideo interface */
109 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
110     iface);
111 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
112 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
113
114 #define gst_wayland_sink_parent_class parent_class
115 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
116     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
117         gst_wayland_sink_videooverlay_init)
118     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
119         gst_wayland_sink_waylandvideo_init));
120
121 static void
122 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
123 {
124   GObjectClass *gobject_class;
125   GstElementClass *gstelement_class;
126   GstBaseSinkClass *gstbasesink_class;
127
128   gobject_class = (GObjectClass *) klass;
129   gstelement_class = (GstElementClass *) klass;
130   gstbasesink_class = (GstBaseSinkClass *) klass;
131
132   gobject_class->set_property = gst_wayland_sink_set_property;
133   gobject_class->get_property = gst_wayland_sink_get_property;
134   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
135
136   gst_element_class_add_pad_template (gstelement_class,
137       gst_static_pad_template_get (&sink_template));
138
139   gst_element_class_set_static_metadata (gstelement_class,
140       "wayland video sink", "Sink/Video",
141       "Output to wayland surface",
142       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
143       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
144
145   gstelement_class->change_state =
146       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
147   gstelement_class->set_context =
148       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
149
150   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
151   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
152   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
153   gstbasesink_class->propose_allocation =
154       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
155   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
156
157   g_object_class_install_property (gobject_class, PROP_DISPLAY,
158       g_param_spec_string ("display", "Wayland Display name", "Wayland "
159           "display name to connect to, if not supplied via the GstContext",
160           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 }
162
163 static void
164 gst_wayland_sink_init (GstWaylandSink * sink)
165 {
166   g_mutex_init (&sink->display_lock);
167   g_mutex_init (&sink->render_lock);
168 }
169
170 static void
171 gst_wayland_sink_get_property (GObject * object,
172     guint prop_id, GValue * value, GParamSpec * pspec)
173 {
174   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
175
176   switch (prop_id) {
177     case PROP_DISPLAY:
178       GST_OBJECT_LOCK (sink);
179       g_value_set_string (value, sink->display_name);
180       GST_OBJECT_UNLOCK (sink);
181       break;
182     default:
183       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185   }
186 }
187
188 static void
189 gst_wayland_sink_set_property (GObject * object,
190     guint prop_id, const GValue * value, GParamSpec * pspec)
191 {
192   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
193
194   switch (prop_id) {
195     case PROP_DISPLAY:
196       GST_OBJECT_LOCK (sink);
197       sink->display_name = g_value_dup_string (value);
198       GST_OBJECT_UNLOCK (sink);
199       break;
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202       break;
203   }
204 }
205
206 static void
207 gst_wayland_sink_finalize (GObject * object)
208 {
209   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
210
211   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
212
213   if (sink->last_buffer)
214     gst_buffer_unref (sink->last_buffer);
215   if (sink->display) {
216     /* the display must be stopped before droping our reference to it
217      * - see the comment on wlbuffer.c for details */
218     gst_wl_display_stop (sink->display);
219     g_object_unref (sink->display);
220   }
221   if (sink->window)
222     g_object_unref (sink->window);
223   if (sink->pool)
224     gst_object_unref (sink->pool);
225
226   if (sink->display_name)
227     g_free (sink->display_name);
228
229   g_mutex_clear (&sink->display_lock);
230   g_mutex_clear (&sink->render_lock);
231
232   G_OBJECT_CLASS (parent_class)->finalize (object);
233 }
234
235 /* must be called with the display_lock */
236 static void
237 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
238     GstContext * context)
239 {
240   struct wl_display *display;
241   GError *error = NULL;
242
243   display = gst_wayland_display_handle_context_get_handle (context);
244   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
245
246   if (error) {
247     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
248         ("Could not set display handle"),
249         ("Failed to use the external wayland display: '%s'", error->message));
250     g_error_free (error);
251   }
252 }
253
254 static gboolean
255 gst_wayland_sink_find_display (GstWaylandSink * sink)
256 {
257   GstQuery *query;
258   GstMessage *msg;
259   GstContext *context = NULL;
260   GError *error = NULL;
261   gboolean ret = TRUE;
262
263   g_mutex_lock (&sink->display_lock);
264
265   if (!sink->display) {
266     /* first query upstream for the needed display handle */
267     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
268     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
269       gst_query_parse_context (query, &context);
270       gst_wayland_sink_set_display_from_context (sink, context);
271     }
272     gst_query_unref (query);
273
274     if (G_LIKELY (!sink->display)) {
275       /* now ask the application to set the display handle */
276       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
277           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
278
279       g_mutex_unlock (&sink->display_lock);
280       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
281       /* at this point we expect gst_wayland_sink_set_context
282        * to get called and fill sink->display */
283       g_mutex_lock (&sink->display_lock);
284
285       if (!sink->display) {
286         /* if the application didn't set a display, let's create it ourselves */
287         GST_OBJECT_LOCK (sink);
288         sink->display = gst_wl_display_new (sink->display_name, &error);
289         GST_OBJECT_UNLOCK (sink);
290
291         if (error) {
292           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
293               ("Could not initialise Wayland output"),
294               ("Failed to create GstWlDisplay: '%s'", error->message));
295           g_error_free (error);
296           ret = FALSE;
297         } else {
298           /* inform the world about the new display */
299           context =
300               gst_wayland_display_handle_context_new (sink->display->display);
301           msg = gst_message_new_have_context (GST_OBJECT_CAST (sink), context);
302           gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
303         }
304       }
305     }
306   }
307
308   g_mutex_unlock (&sink->display_lock);
309
310   return ret;
311 }
312
313 static GstStateChangeReturn
314 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
315 {
316   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
317   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
318
319   switch (transition) {
320     case GST_STATE_CHANGE_NULL_TO_READY:
321       if (!gst_wayland_sink_find_display (sink))
322         return GST_STATE_CHANGE_FAILURE;
323       break;
324     default:
325       break;
326   }
327
328   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
329   if (ret == GST_STATE_CHANGE_FAILURE)
330     return ret;
331
332   switch (transition) {
333     case GST_STATE_CHANGE_PAUSED_TO_READY:
334       gst_buffer_replace (&sink->last_buffer, NULL);
335       if (sink->window) {
336         if (gst_wl_window_is_toplevel (sink->window)) {
337           g_clear_object (&sink->window);
338         } else {
339           /* remove buffer from surface, show nothing */
340           wl_surface_attach (sink->window->surface, NULL, 0, 0);
341           wl_surface_damage (sink->window->surface, 0, 0,
342               sink->window->surface_width, sink->window->surface_height);
343           wl_surface_commit (sink->window->surface);
344           wl_display_flush (sink->display->display);
345         }
346       }
347       break;
348     case GST_STATE_CHANGE_READY_TO_NULL:
349       g_mutex_lock (&sink->display_lock);
350       /* If we had a toplevel window, we most likely have our own connection
351        * to the display too, and it is a good idea to disconnect and allow
352        * potentially the application to embed us with GstVideoOverlay
353        * (which requires to re-use the same display connection as the parent
354        * surface). If we didn't have a toplevel window, then the display
355        * connection that we have is definitely shared with the application
356        * and it's better to keep it around (together with the window handle)
357        * to avoid requesting them again from the application if/when we are
358        * restarted (GstVideoOverlay behaves like that in other sinks)
359        */
360       if (sink->display && !sink->window) {     /* -> the window was toplevel */
361         /* the display must be stopped before droping our reference to it
362          * - see the comment on wlbuffer.c for details */
363         gst_wl_display_stop (sink->display);
364         g_clear_object (&sink->display);
365       }
366       g_mutex_unlock (&sink->display_lock);
367       g_clear_object (&sink->pool);
368       break;
369     default:
370       break;
371   }
372
373   return ret;
374 }
375
376 static void
377 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
378 {
379   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
380
381   if (gst_context_has_context_type (context,
382           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
383     g_mutex_lock (&sink->display_lock);
384     if (G_LIKELY (!sink->display))
385       gst_wayland_sink_set_display_from_context (sink, context);
386     else
387       GST_WARNING_OBJECT (element, "changing display handle is not supported");
388     g_mutex_unlock (&sink->display_lock);
389   }
390
391   if (GST_ELEMENT_CLASS (parent_class)->set_context)
392     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
393 }
394
395 static GstCaps *
396 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
397 {
398   GstWaylandSink *sink;
399   GstCaps *caps;
400
401   sink = GST_WAYLAND_SINK (bsink);
402
403   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
404
405   g_mutex_lock (&sink->display_lock);
406
407   if (sink->display) {
408     GValue list = G_VALUE_INIT;
409     GValue value = G_VALUE_INIT;
410     GArray *formats;
411     gint i;
412     enum wl_shm_format fmt;
413
414     g_value_init (&list, GST_TYPE_LIST);
415     g_value_init (&value, G_TYPE_STRING);
416
417     formats = sink->display->formats;
418     for (i = 0; i < formats->len; i++) {
419       fmt = g_array_index (formats, uint32_t, i);
420       g_value_set_string (&value, gst_wayland_format_to_string (fmt));
421       gst_value_list_append_value (&list, &value);
422     }
423
424     caps = gst_caps_make_writable (caps);
425     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
426
427     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
428   }
429
430   g_mutex_unlock (&sink->display_lock);
431
432   if (filter) {
433     GstCaps *intersection;
434
435     intersection =
436         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
437     gst_caps_unref (caps);
438     caps = intersection;
439   }
440
441   return caps;
442 }
443
444 static gboolean
445 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
446 {
447   GstWaylandSink *sink;
448   GstBufferPool *newpool;
449   GstVideoInfo info;
450   enum wl_shm_format format;
451   GArray *formats;
452   gint i;
453   GstStructure *structure;
454
455   sink = GST_WAYLAND_SINK (bsink);
456
457   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
458
459   /* extract info from caps */
460   if (!gst_video_info_from_caps (&info, caps))
461     goto invalid_format;
462
463   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
464   if ((gint) format == -1)
465     goto invalid_format;
466
467   /* verify we support the requested format */
468   formats = sink->display->formats;
469   for (i = 0; i < formats->len; i++) {
470     if (g_array_index (formats, uint32_t, i) == format)
471       break;
472   }
473
474   if (i >= formats->len)
475     goto unsupported_format;
476
477   /* create a new pool for the new configuration */
478   newpool = gst_video_buffer_pool_new ();
479   if (!newpool)
480     goto pool_failed;
481
482   structure = gst_buffer_pool_get_config (newpool);
483   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
484   gst_buffer_pool_config_set_allocator (structure, gst_wl_shm_allocator_get (),
485       NULL);
486   if (!gst_buffer_pool_set_config (newpool, structure))
487     goto config_failed;
488
489   /* store the video info */
490   sink->video_info = info;
491   sink->video_info_changed = TRUE;
492
493   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
494   gst_object_unref (newpool);
495
496   return TRUE;
497
498 invalid_format:
499   {
500     GST_DEBUG_OBJECT (sink,
501         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
502     return FALSE;
503   }
504 unsupported_format:
505   {
506     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
507         gst_wayland_format_to_string (format));
508     return FALSE;
509   }
510 pool_failed:
511   {
512     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
513     return FALSE;
514   }
515 config_failed:
516   {
517     GST_DEBUG_OBJECT (bsink, "failed setting config");
518     gst_object_unref (newpool);
519     return FALSE;
520   }
521 }
522
523 static gboolean
524 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
525 {
526   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
527   GstStructure *config;
528   guint size, min_bufs, max_bufs;
529
530   config = gst_buffer_pool_get_config (sink->pool);
531   gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
532
533   /* we do have a pool for sure (created in set_caps),
534    * so let's propose it anyway, but also propose the allocator on its own */
535   gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs);
536   gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL);
537
538   return TRUE;
539 }
540
541 static GstFlowReturn
542 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
543 {
544   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
545   return gst_wayland_sink_render (bsink, buffer);
546 }
547
548 static void
549 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
550 {
551   GstWaylandSink *sink = data;
552
553   GST_LOG ("frame_redraw_cb");
554
555   g_atomic_int_set (&sink->redraw_pending, FALSE);
556   wl_callback_destroy (callback);
557 }
558
559 static const struct wl_callback_listener frame_callback_listener = {
560   frame_redraw_callback
561 };
562
563 /* must be called with the render lock */
564 static void
565 render_last_buffer (GstWaylandSink * sink)
566 {
567   GstWlBuffer *wlbuffer;
568   struct wl_surface *surface;
569   struct wl_callback *callback;
570
571   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
572   surface = gst_wl_window_get_wl_surface (sink->window);
573
574   g_atomic_int_set (&sink->redraw_pending, TRUE);
575   callback = wl_surface_frame (surface);
576   wl_callback_add_listener (callback, &frame_callback_listener, sink);
577
578   gst_wl_buffer_attach (wlbuffer, sink->window);
579   wl_surface_damage (surface, 0, 0, sink->window->surface_width,
580       sink->window->surface_height);
581
582   wl_surface_commit (surface);
583   wl_display_flush (sink->display->display);
584 }
585
586 static GstFlowReturn
587 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
588 {
589   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
590   GstBuffer *to_render;
591   GstWlBuffer *wlbuffer;
592   GstFlowReturn ret = GST_FLOW_OK;
593
594   g_mutex_lock (&sink->render_lock);
595
596   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
597
598   if (G_UNLIKELY (!sink->window)) {
599     /* ask for window handle. Unlock render_lock while doing that because
600      * set_window_handle & friends will lock it in this context */
601     g_mutex_unlock (&sink->render_lock);
602     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
603     g_mutex_lock (&sink->render_lock);
604
605     if (sink->window) {
606       /* inform the window about our caps */
607       gst_wl_window_set_video_info (sink->window, &sink->video_info);
608     } else {
609       /* if we were not provided a window, create one ourselves */
610       sink->window =
611           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
612     }
613     sink->video_info_changed = FALSE;
614   }
615
616   /* drop buffers until we get a frame callback */
617   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
618     goto done;
619
620   if (G_UNLIKELY (sink->video_info_changed)) {
621     gst_wl_window_set_video_info (sink->window, &sink->video_info);
622     sink->video_info_changed = FALSE;
623   }
624
625   /* now that we have for sure set the video info on the window, it must have
626    * a valid size, otherwise this means that the application has called
627    * set_window_handle() without calling set_render_rectangle(), which is
628    * absolutely necessary for us.
629    */
630   if (G_UNLIKELY (sink->window->surface_width == 0 ||
631           sink->window->surface_height == 0))
632     goto no_window_size;
633
634   wlbuffer = gst_buffer_get_wl_buffer (buffer);
635
636   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
637     GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, "
638         "writing directly", buffer);
639     to_render = buffer;
640   } else {
641     GstMemory *mem;
642     struct wl_buffer *wbuf = NULL;
643
644     GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our "
645         "display, creating it", buffer);
646
647     mem = gst_buffer_peek_memory (buffer, 0);
648
649     if (gst_is_wl_shm_memory (mem)) {
650       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
651           &sink->video_info);
652     }
653
654     if (wbuf) {
655       gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
656       to_render = buffer;
657     } else {
658       GstMapInfo src;
659       /* we don't know how to create a wl_buffer directly from the provided
660        * memory, so we have to copy the data to a memory that we know how
661        * to handle... */
662
663       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, "
664           "copying to wl_shm memory", buffer);
665
666       /* sink->pool always exists (created in set_caps), but it may not
667        * be active if upstream is not using it */
668       if (!gst_buffer_pool_is_active (sink->pool) &&
669           !gst_buffer_pool_set_active (sink->pool, TRUE))
670         goto activate_failed;
671
672       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
673       if (ret != GST_FLOW_OK)
674         goto no_buffer;
675
676       /* the first time we acquire a buffer,
677        * we need to attach a wl_buffer on it */
678       wlbuffer = gst_buffer_get_wl_buffer (buffer);
679       if (G_UNLIKELY (!wlbuffer)) {
680         mem = gst_buffer_peek_memory (to_render, 0);
681         wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
682             &sink->video_info);
683         if (G_UNLIKELY (!wbuf))
684           goto no_wl_buffer;
685
686         gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
687       }
688
689       gst_buffer_map (buffer, &src, GST_MAP_READ);
690       gst_buffer_fill (to_render, 0, src.data, src.size);
691       gst_buffer_unmap (buffer, &src);
692     }
693   }
694
695   gst_buffer_replace (&sink->last_buffer, to_render);
696   render_last_buffer (sink);
697
698   if (buffer != to_render)
699     gst_buffer_unref (to_render);
700   goto done;
701
702 no_window_size:
703   {
704     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
705         ("Window has no size set"),
706         ("Make sure you set the size after calling set_window_handle"));
707     ret = GST_FLOW_ERROR;
708     goto done;
709   }
710 no_buffer:
711   {
712     GST_WARNING_OBJECT (sink, "could not create buffer");
713     goto done;
714   }
715 no_wl_buffer:
716   {
717     GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
718     ret = GST_FLOW_ERROR;
719     goto done;
720   }
721 activate_failed:
722   {
723     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
724     ret = GST_FLOW_ERROR;
725     goto done;
726   }
727 done:
728   {
729     g_mutex_unlock (&sink->render_lock);
730     return ret;
731   }
732 }
733
734 static void
735 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
736 {
737   iface->set_window_handle = gst_wayland_sink_set_window_handle;
738   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
739   iface->expose = gst_wayland_sink_expose;
740 }
741
742 static void
743 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
744 {
745   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
746   struct wl_surface *surface = (struct wl_surface *) handle;
747
748   g_return_if_fail (sink != NULL);
749
750   g_mutex_lock (&sink->render_lock);
751
752   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
753       (void *) handle);
754
755   g_clear_object (&sink->window);
756
757   if (handle) {
758     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
759       /* we cannot use our own display with an external window handle */
760       if (G_UNLIKELY (sink->display->own_display)) {
761         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
762             ("Application did not provide a wayland display handle"),
763             ("waylandsink cannot use an externally-supplied surface without "
764                 "an externally-supplied display handle. Consider providing a "
765                 "display handle from your application with GstContext"));
766       } else {
767         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
768       }
769     } else {
770       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
771           "ignoring window handle");
772     }
773   }
774
775   g_mutex_unlock (&sink->render_lock);
776 }
777
778 static void
779 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
780     gint x, gint y, gint w, gint h)
781 {
782   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
783
784   g_return_if_fail (sink != NULL);
785
786   g_mutex_lock (&sink->render_lock);
787   if (!sink->window) {
788     g_mutex_unlock (&sink->render_lock);
789     GST_WARNING_OBJECT (sink,
790         "set_render_rectangle called without window, ignoring");
791     return;
792   }
793
794   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
795       x, y, w, h);
796   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
797
798   g_mutex_unlock (&sink->render_lock);
799 }
800
801 static void
802 gst_wayland_sink_expose (GstVideoOverlay * overlay)
803 {
804   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
805
806   g_return_if_fail (sink != NULL);
807
808   GST_DEBUG_OBJECT (sink, "expose");
809
810   g_mutex_lock (&sink->render_lock);
811   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
812     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
813     render_last_buffer (sink);
814   }
815   g_mutex_unlock (&sink->render_lock);
816 }
817
818 static void
819 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
820 {
821   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
822   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
823 }
824
825 static void
826 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
827 {
828   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
829   g_return_if_fail (sink != NULL);
830
831   g_mutex_lock (&sink->render_lock);
832   if (!sink->window || !sink->window->subsurface) {
833     g_mutex_unlock (&sink->render_lock);
834     GST_INFO_OBJECT (sink,
835         "begin_geometry_change called without window, ignoring");
836     return;
837   }
838
839   wl_subsurface_set_sync (sink->window->subsurface);
840   g_mutex_unlock (&sink->render_lock);
841 }
842
843 static void
844 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
845 {
846   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
847   g_return_if_fail (sink != NULL);
848
849   g_mutex_lock (&sink->render_lock);
850   if (!sink->window || !sink->window->subsurface) {
851     g_mutex_unlock (&sink->render_lock);
852     GST_INFO_OBJECT (sink,
853         "end_geometry_change called without window, ignoring");
854     return;
855   }
856
857   wl_subsurface_set_desync (sink->window->subsurface);
858   g_mutex_unlock (&sink->render_lock);
859 }
860
861 static gboolean
862 plugin_init (GstPlugin * plugin)
863 {
864   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
865       " wayland video sink");
866
867   gst_wl_shm_allocator_register ();
868
869   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
870       GST_TYPE_WAYLAND_SINK);
871 }
872
873 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
874     GST_VERSION_MINOR,
875     waylandsink,
876     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
877     GST_PACKAGE_ORIGIN)