waylandsink: Implement expose() and handle resizing properly in non-PLAYING states
[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 "waylandpool.h"
47
48 #include <gst/wayland/wayland.h>
49 #include <gst/video/videooverlay.h>
50
51 /* signals */
52 enum
53 {
54   SIGNAL_0,
55   LAST_SIGNAL
56 };
57
58 /* Properties */
59 enum
60 {
61   PROP_0,
62   PROP_DISPLAY
63 };
64
65 GST_DEBUG_CATEGORY (gstwayland_debug);
66 #define GST_CAT_DEFAULT gstwayland_debug
67
68 #if G_BYTE_ORDER == G_BIG_ENDIAN
69 #define CAPS "{xRGB, ARGB}"
70 #else
71 #define CAPS "{BGRx, BGRA}"
72 #endif
73
74 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
75     GST_PAD_SINK,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS))
78     );
79
80 static void gst_wayland_sink_get_property (GObject * object,
81     guint prop_id, GValue * value, GParamSpec * pspec);
82 static void gst_wayland_sink_set_property (GObject * object,
83     guint prop_id, const GValue * value, GParamSpec * pspec);
84 static void gst_wayland_sink_finalize (GObject * object);
85
86 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
87     GstStateChange transition);
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_expose (GstVideoOverlay * overlay);
105
106 /* WaylandVideo interface */
107 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
108     iface);
109 static void gst_wayland_sink_set_surface_size (GstWaylandVideo * video,
110     gint w, gint h);
111 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
112 static void gst_wayland_sink_resume_rendering (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
144   gstelement_class->change_state =
145       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
146
147   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
148   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
149   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
150   gstbasesink_class->propose_allocation =
151       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
152   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
153
154   g_object_class_install_property (gobject_class, PROP_DISPLAY,
155       g_param_spec_string ("display", "Wayland Display name", "Wayland "
156           "display name to connect to, if not supplied with GstVideoOverlay",
157           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 }
159
160 static void
161 gst_wayland_sink_init (GstWaylandSink * sink)
162 {
163   g_cond_init (&sink->render_cond);
164 }
165
166 static void
167 gst_wayland_sink_get_property (GObject * object,
168     guint prop_id, GValue * value, GParamSpec * pspec)
169 {
170   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
171
172   switch (prop_id) {
173     case PROP_DISPLAY:
174       g_value_set_string (value, sink->display_name);
175       break;
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179   }
180 }
181
182 static void
183 gst_wayland_sink_set_property (GObject * object,
184     guint prop_id, const GValue * value, GParamSpec * pspec)
185 {
186   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
187
188   switch (prop_id) {
189     case PROP_DISPLAY:
190       sink->display_name = g_value_dup_string (value);
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195   }
196 }
197
198 static void
199 gst_wayland_sink_finalize (GObject * object)
200 {
201   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
202
203   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
204
205   if (sink->last_buffer)
206     gst_buffer_unref (sink->last_buffer);
207   if (sink->display) {
208     /* see comment about this call in gst_wayland_sink_change_state() */
209     gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
210         (sink->pool));
211     g_object_unref (sink->display);
212   }
213   if (sink->window)
214     g_object_unref (sink->window);
215   if (sink->pool)
216     gst_object_unref (sink->pool);
217
218   if (sink->display_name)
219     g_free (sink->display_name);
220
221   g_cond_clear (&sink->render_cond);
222
223   G_OBJECT_CLASS (parent_class)->finalize (object);
224 }
225
226 static GstStateChangeReturn
227 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
228 {
229   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
230   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
231
232   switch (transition) {
233     case GST_STATE_CHANGE_READY_TO_PAUSED:
234       if (!sink->window)
235         gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
236       break;
237     default:
238       break;
239   }
240
241   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
242   if (ret == GST_STATE_CHANGE_FAILURE)
243     return ret;
244
245   switch (transition) {
246     case GST_STATE_CHANGE_PAUSED_TO_READY:
247       if (gst_wl_window_is_toplevel (sink->window)) {
248         /* Force all buffers to return to the pool, regardless of
249          * whether the compositor has released them or not. We are
250          * going to kill the display, so we need to return all buffers
251          * to be destroyed before this happens.
252          * Note that this is done here instead of the pool destructor
253          * because the buffers hold a reference to the pool. Also,
254          * the buffers can only be unref'ed from the display's event loop
255          * and the pool holds a reference to the display. If we drop
256          * our references here, when the compositor releases the buffers,
257          * they will be unref'ed from the event loop thread, which will
258          * unref the pool and therefore the display, which will try to
259          * stop the thread from within itself and cause a deadlock.
260          */
261         gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
262             (sink->pool));
263         gst_buffer_replace (&sink->last_buffer, NULL);
264         g_clear_object (&sink->window);
265         g_clear_object (&sink->display);
266         g_clear_object (&sink->pool);
267       }
268       break;
269     default:
270       break;
271   }
272
273   return ret;
274 }
275
276 static GstCaps *
277 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
278 {
279   GstWaylandSink *sink;
280   GstCaps *caps;
281
282   sink = GST_WAYLAND_SINK (bsink);
283
284   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
285
286   if (filter) {
287     GstCaps *intersection;
288
289     intersection =
290         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
291     gst_caps_unref (caps);
292     caps = intersection;
293   }
294   return caps;
295 }
296
297 static gboolean
298 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
299 {
300   GstWaylandSink *sink;
301   GstBufferPool *newpool;
302   GstVideoInfo info;
303   GError *error = NULL;
304   enum wl_shm_format format;
305   GArray *formats;
306   gint i;
307   GstStructure *structure;
308   static GstAllocationParams params = { 0, 0, 0, 15, };
309
310   sink = GST_WAYLAND_SINK (bsink);
311   GST_OBJECT_LOCK (sink);
312
313   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
314
315   /* extract info from caps */
316   if (!gst_video_info_from_caps (&info, caps))
317     goto invalid_format;
318
319   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
320   if (format == -1)
321     goto invalid_format;
322
323   /* store the video size */
324   sink->video_width = info.width;
325   sink->video_height = info.height;
326
327   /* create the output window if needed */
328   if (!sink->window) {
329     if (!sink->display)
330       sink->display = gst_wl_display_new (sink->display_name, &error);
331
332     if (sink->display == NULL)
333       goto display_failed;
334
335     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
336         sink->video_height);
337   }
338
339   /* verify we support the requested format */
340   formats = sink->display->formats;
341   for (i = 0; i < formats->len; i++) {
342     if (g_array_index (formats, uint32_t, i) == format)
343       break;
344   }
345
346   if (i >= formats->len)
347     goto unsupported_format;
348
349   /* create a new pool for the new configuration */
350   newpool = gst_wayland_buffer_pool_new (sink->display);
351   if (!newpool)
352     goto pool_failed;
353
354   structure = gst_buffer_pool_get_config (newpool);
355   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
356   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
357   if (!gst_buffer_pool_set_config (newpool, structure))
358     goto config_failed;
359
360   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
361   gst_object_unref (newpool);
362
363   GST_OBJECT_UNLOCK (sink);
364   return TRUE;
365
366 invalid_format:
367   {
368     GST_DEBUG_OBJECT (sink,
369         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
370     goto failure;
371   }
372 display_failed:
373   {
374     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
375         ("Could not initialise Wayland output"),
376         ("Failed to create GstWlDisplay: '%s'", error->message));
377     g_error_free (error);
378     goto failure;
379   }
380 unsupported_format:
381   {
382     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
383         gst_wayland_format_to_string (format));
384     goto failure;
385   }
386 pool_failed:
387   {
388     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
389     goto failure;
390   }
391 config_failed:
392   {
393     GST_DEBUG_OBJECT (bsink, "failed setting config");
394     goto failure;
395   }
396 failure:
397   {
398     GST_OBJECT_UNLOCK (sink);
399     return FALSE;
400   }
401 }
402
403 static gboolean
404 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
405 {
406   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
407   GstBufferPool *pool = NULL;
408   GstStructure *config;
409   GstCaps *caps;
410   guint size;
411   gboolean need_pool;
412
413   gst_query_parse_allocation (query, &caps, &need_pool);
414
415   if (caps == NULL)
416     goto no_caps;
417
418   if (sink->pool)
419     pool = gst_object_ref (sink->pool);
420
421   if (pool != NULL) {
422     GstCaps *pcaps;
423
424     /* we had a pool, check caps */
425     config = gst_buffer_pool_get_config (pool);
426     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
427
428     if (!gst_caps_is_equal (caps, pcaps)) {
429       /* different caps, we can't use this pool */
430       gst_object_unref (pool);
431       pool = NULL;
432     }
433     gst_structure_free (config);
434   }
435
436   if (pool == NULL && need_pool) {
437     GstVideoInfo info;
438
439     if (!gst_video_info_from_caps (&info, caps))
440       goto invalid_caps;
441
442     GST_DEBUG_OBJECT (sink, "create new pool");
443     pool = gst_wayland_buffer_pool_new (sink->display);
444
445     /* the normal size of a frame */
446     size = info.size;
447
448     config = gst_buffer_pool_get_config (pool);
449     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
450     if (!gst_buffer_pool_set_config (pool, config))
451       goto config_failed;
452   }
453   if (pool) {
454     gst_query_add_allocation_pool (query, pool, size, 2, 0);
455     gst_object_unref (pool);
456   }
457
458   return TRUE;
459
460   /* ERRORS */
461 no_caps:
462   {
463     GST_DEBUG_OBJECT (bsink, "no caps specified");
464     return FALSE;
465   }
466 invalid_caps:
467   {
468     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
469     return FALSE;
470   }
471 config_failed:
472   {
473     GST_DEBUG_OBJECT (bsink, "failed setting config");
474     gst_object_unref (pool);
475     return FALSE;
476   }
477 }
478
479 static GstFlowReturn
480 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
481 {
482   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
483   return gst_wayland_sink_render (bsink, buffer);
484 }
485
486 static void
487 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
488 {
489   GstWaylandSink *sink = data;
490
491   GST_LOG ("frame_redraw_cb");
492
493   g_atomic_int_set (&sink->redraw_pending, FALSE);
494   wl_callback_destroy (callback);
495 }
496
497 static const struct wl_callback_listener frame_callback_listener = {
498   frame_redraw_callback
499 };
500
501 /* must be called with the object lock */
502 static void
503 render_last_buffer (GstWaylandSink * sink)
504 {
505   GstWlMeta *meta;
506   struct wl_surface *surface;
507   struct wl_callback *callback;
508   GstVideoRectangle src, dst, res;
509
510   meta = gst_buffer_get_wl_meta (sink->last_buffer);
511   surface = gst_wl_window_get_wl_surface (sink->window);
512
513   g_atomic_int_set (&sink->redraw_pending, TRUE);
514   callback = wl_surface_frame (surface);
515   wl_callback_add_listener (callback, &frame_callback_listener, sink);
516
517   /* Here we essentially add a reference to the buffer. This represents
518    * the fact that the compositor is using the buffer and it should
519    * not return back to the pool and be reused until the compositor
520    * releases it. The release is handled internally in the pool */
521   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
522
523   src.w = sink->video_width;
524   src.h = sink->video_height;
525   dst.w = sink->window->width;
526   dst.h = sink->window->height;
527   gst_video_sink_center_rect (src, dst, &res, FALSE);
528
529   wl_viewport_set (sink->window->viewport, wl_fixed_from_int (0),
530       wl_fixed_from_int (0), wl_fixed_from_int (src.w),
531       wl_fixed_from_int (src.h), res.w, res.h);
532
533   wl_surface_attach (surface, meta->wbuffer, 0, 0);
534   wl_surface_damage (surface, 0, 0, res.w, res.h);
535
536   wl_surface_commit (surface);
537   wl_display_flush (sink->display->display);
538 }
539
540 static GstFlowReturn
541 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
542 {
543   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
544   GstBuffer *to_render;
545   GstWlMeta *meta;
546   GstFlowReturn ret = GST_FLOW_OK;
547
548   GST_OBJECT_LOCK (sink);
549
550   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
551
552   /* surface is resizing - drop buffers until finished */
553   if (sink->drawing_frozen)
554     goto done;
555
556   /* drop buffers until we get a frame callback */
557   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
558     goto done;
559
560   meta = gst_buffer_get_wl_meta (buffer);
561
562   if (meta && meta->pool->display == sink->display) {
563     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
564     to_render = buffer;
565   } else {
566     GstMapInfo src;
567     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
568
569     if (!sink->pool)
570       goto no_pool;
571
572     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
573       goto activate_failed;
574
575     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
576     if (ret != GST_FLOW_OK)
577       goto no_buffer;
578
579     gst_buffer_map (buffer, &src, GST_MAP_READ);
580     gst_buffer_fill (to_render, 0, src.data, src.size);
581     gst_buffer_unmap (buffer, &src);
582   }
583
584   gst_buffer_replace (&sink->last_buffer, to_render);
585   render_last_buffer (sink);
586
587   /* notify _resume_rendering() in case it's waiting */
588   g_cond_broadcast (&sink->render_cond);
589
590   if (buffer != to_render)
591     gst_buffer_unref (to_render);
592   goto done;
593
594 no_buffer:
595   {
596     GST_WARNING_OBJECT (sink, "could not create image");
597     goto done;
598   }
599 no_pool:
600   {
601     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
602         ("Internal error: can't allocate images"),
603         ("We don't have a bufferpool negotiated"));
604     ret = GST_FLOW_ERROR;
605     goto done;
606   }
607 activate_failed:
608   {
609     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
610     ret = GST_FLOW_ERROR;
611     goto done;
612   }
613 done:
614   {
615     GST_OBJECT_UNLOCK (sink);
616     return ret;
617   }
618 }
619
620 static void
621 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
622 {
623   iface->set_window_handle = gst_wayland_sink_set_window_handle;
624   iface->expose = gst_wayland_sink_expose;
625 }
626
627 static void
628 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
629 {
630   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
631   GstWaylandWindowHandle *whandle = (GstWaylandWindowHandle *) handle;
632   GError *error = NULL;
633   GstPad *peer;
634
635   g_return_if_fail (sink != NULL);
636
637   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
638       (void *) handle);
639
640   if (GST_STATE (sink) > GST_STATE_READY)
641     gst_wayland_sink_pause_rendering (GST_WAYLAND_VIDEO (sink));
642
643   g_clear_object (&sink->window);
644   g_clear_object (&sink->display);
645
646   if (handle) {
647     sink->display =
648         gst_wl_display_new_existing (whandle->display, FALSE, &error);
649     if (error) {
650       GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
651           ("Could not set window handle"),
652           ("Failed to use the external wayland display: '%s'", error->message));
653       g_error_free (error);
654     } else {
655       wl_proxy_set_queue ((struct wl_proxy *) whandle->surface,
656           sink->display->queue);
657       sink->window = gst_wl_window_new_from_surface (sink->display,
658           whandle->surface, whandle->width, whandle->height);
659     }
660   } else {
661     /* reconfigure to force creation of new window in set_caps */
662     sink->negotiated = FALSE;
663     peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
664     if (peer) {
665       gst_pad_send_event (peer, gst_event_new_reconfigure ());
666       gst_object_unref (peer);
667     }
668   }
669
670   if (GST_STATE (sink) > GST_STATE_READY)
671     gst_wayland_sink_resume_rendering (GST_WAYLAND_VIDEO (sink));
672 }
673
674 static void
675 gst_wayland_sink_expose (GstVideoOverlay * overlay)
676 {
677   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
678
679   g_return_if_fail (sink != NULL);
680
681   GST_DEBUG_OBJECT (sink, "expose");
682
683   GST_OBJECT_LOCK (sink);
684   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
685     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
686     render_last_buffer (sink);
687   }
688   GST_OBJECT_UNLOCK (sink);
689 }
690
691 static void
692 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
693 {
694   iface->set_surface_size = gst_wayland_sink_set_surface_size;
695   iface->pause_rendering = gst_wayland_sink_pause_rendering;
696   iface->resume_rendering = gst_wayland_sink_resume_rendering;
697 }
698
699 static void
700 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
701 {
702   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
703
704   g_return_if_fail (sink != NULL);
705   g_return_if_fail (sink->window != NULL);
706
707   GST_OBJECT_LOCK (sink);
708   if (!sink->window) {
709     GST_OBJECT_UNLOCK (sink);
710     GST_WARNING_OBJECT (sink,
711         "set_surface_size called without window, ignoring");
712     return;
713   }
714
715   gst_wl_window_set_size (sink->window, w, h);
716   GST_OBJECT_UNLOCK (sink);
717 }
718
719 static void
720 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
721 {
722   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
723   g_return_if_fail (sink != NULL);
724
725   GST_OBJECT_LOCK (sink);
726   sink->drawing_frozen = TRUE;
727   GST_OBJECT_UNLOCK (sink);
728 }
729
730 static void
731 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
732 {
733   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
734   g_return_if_fail (sink != NULL);
735
736   GST_OBJECT_LOCK (sink);
737   sink->drawing_frozen = FALSE;
738
739   if (GST_STATE (sink) == GST_STATE_PLAYING)
740     g_cond_wait (&sink->render_cond, GST_OBJECT_GET_LOCK (sink));
741   else if (sink->window && sink->last_buffer &&
742       g_atomic_int_get (&sink->redraw_pending) == FALSE)
743     render_last_buffer (sink);
744
745   GST_OBJECT_UNLOCK (sink);
746 }
747
748 static gboolean
749 plugin_init (GstPlugin * plugin)
750 {
751   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
752       " wayland video sink");
753
754   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
755       GST_TYPE_WAYLAND_SINK);
756 }
757
758 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
759     GST_VERSION_MINOR,
760     waylandsink,
761     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
762     GST_PACKAGE_ORIGIN)