waylandsink: create and maintain the subsurface inside the sink
[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 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69     GST_PAD_SINK,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
72         ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
73             "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
74             "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
75     );
76
77 static void gst_wayland_sink_get_property (GObject * object,
78     guint prop_id, GValue * value, GParamSpec * pspec);
79 static void gst_wayland_sink_set_property (GObject * object,
80     guint prop_id, const GValue * value, GParamSpec * pspec);
81 static void gst_wayland_sink_finalize (GObject * object);
82
83 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
84     GstStateChange transition);
85 static void gst_wayland_sink_set_context (GstElement * element,
86     GstContext * context);
87
88 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
89     GstCaps * filter);
90 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
91 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
92     GstBuffer * buffer);
93 static gboolean
94 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
95 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
96     GstBuffer * buffer);
97
98 /* VideoOverlay interface */
99 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
100     iface);
101 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
102     guintptr handle);
103 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
104     gint x, gint y, gint w, gint h);
105 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
106
107 /* WaylandVideo interface */
108 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
109     iface);
110 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
111 static void gst_wayland_sink_resume_rendering (GstWaylandVideo * video);
112
113 #define gst_wayland_sink_parent_class parent_class
114 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
115     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
116         gst_wayland_sink_videooverlay_init)
117     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
118         gst_wayland_sink_waylandvideo_init));
119
120 static void
121 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
122 {
123   GObjectClass *gobject_class;
124   GstElementClass *gstelement_class;
125   GstBaseSinkClass *gstbasesink_class;
126
127   gobject_class = (GObjectClass *) klass;
128   gstelement_class = (GstElementClass *) klass;
129   gstbasesink_class = (GstBaseSinkClass *) klass;
130
131   gobject_class->set_property = gst_wayland_sink_set_property;
132   gobject_class->get_property = gst_wayland_sink_get_property;
133   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
134
135   gst_element_class_add_pad_template (gstelement_class,
136       gst_static_pad_template_get (&sink_template));
137
138   gst_element_class_set_static_metadata (gstelement_class,
139       "wayland video sink", "Sink/Video",
140       "Output to wayland surface",
141       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
142       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
143
144   gstelement_class->change_state =
145       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
146   gstelement_class->set_context =
147       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
148
149   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
150   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
151   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
152   gstbasesink_class->propose_allocation =
153       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
154   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
155
156   g_object_class_install_property (gobject_class, PROP_DISPLAY,
157       g_param_spec_string ("display", "Wayland Display name", "Wayland "
158           "display name to connect to, if not supplied via the GstContext",
159           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 }
161
162 static void
163 gst_wayland_sink_init (GstWaylandSink * sink)
164 {
165   g_cond_init (&sink->render_cond);
166 }
167
168 static void
169 gst_wayland_sink_get_property (GObject * object,
170     guint prop_id, GValue * value, GParamSpec * pspec)
171 {
172   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
173
174   switch (prop_id) {
175     case PROP_DISPLAY:
176       g_value_set_string (value, sink->display_name);
177       break;
178     default:
179       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180       break;
181   }
182 }
183
184 static void
185 gst_wayland_sink_set_property (GObject * object,
186     guint prop_id, const GValue * value, GParamSpec * pspec)
187 {
188   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
189
190   switch (prop_id) {
191     case PROP_DISPLAY:
192       sink->display_name = g_value_dup_string (value);
193       break;
194     default:
195       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196       break;
197   }
198 }
199
200 static void
201 gst_wayland_sink_finalize (GObject * object)
202 {
203   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
204
205   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
206
207   if (sink->last_buffer)
208     gst_buffer_unref (sink->last_buffer);
209   if (sink->display) {
210     /* see comment about this call in gst_wayland_sink_change_state() */
211     if (sink->pool) {
212       gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
213           (sink->pool));
214     }
215     g_object_unref (sink->display);
216   }
217   if (sink->window)
218     g_object_unref (sink->window);
219   if (sink->pool)
220     gst_object_unref (sink->pool);
221
222   if (sink->display_name)
223     g_free (sink->display_name);
224
225   g_cond_clear (&sink->render_cond);
226
227   G_OBJECT_CLASS (parent_class)->finalize (object);
228 }
229
230 static gboolean
231 gst_wayland_sink_find_display (GstWaylandSink * sink)
232 {
233   GstQuery *query;
234   GstMessage *msg;
235   GstContext *context = NULL;
236   GError *error = NULL;
237   gboolean ret = TRUE;
238
239   if (!sink->display) {
240     /* first query upstream for the needed display handle */
241     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
242     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
243       gst_query_parse_context (query, &context);
244       gst_wayland_sink_set_context (GST_ELEMENT (sink), context);
245     }
246     gst_query_unref (query);
247
248     if (G_LIKELY (!sink->display)) {
249       /* now ask the application to set the display handle */
250       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
251           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
252       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
253       /* at this point we expect gst_wayland_sink_set_context
254        * to get called and fill sink->display */
255
256       if (!sink->display) {
257         /* if the application didn't set a display, let's create it ourselves */
258         sink->display = gst_wl_display_new (sink->display_name, &error);
259         if (error) {
260           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
261               ("Could not initialise Wayland output"),
262               ("Failed to create GstWlDisplay: '%s'", error->message));
263           g_error_free (error);
264           ret = FALSE;
265         } else {
266           /* inform the world about the new display */
267           context =
268               gst_wayland_display_handle_context_new (sink->display->display);
269           msg = gst_message_new_have_context (GST_OBJECT_CAST (sink), context);
270           gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
271         }
272       }
273     }
274   }
275
276   return ret;
277 }
278
279 static GstStateChangeReturn
280 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
281 {
282   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
283   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
284
285   switch (transition) {
286     case GST_STATE_CHANGE_NULL_TO_READY:
287       if (!gst_wayland_sink_find_display (sink))
288         return GST_STATE_CHANGE_FAILURE;
289       break;
290     default:
291       break;
292   }
293
294   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
295   if (ret == GST_STATE_CHANGE_FAILURE)
296     return ret;
297
298   switch (transition) {
299     case GST_STATE_CHANGE_PAUSED_TO_READY:
300       if (sink->window && gst_wl_window_is_toplevel (sink->window)) {
301         gst_buffer_replace (&sink->last_buffer, NULL);
302         g_clear_object (&sink->window);
303       }
304       break;
305     case GST_STATE_CHANGE_READY_TO_NULL:
306       /* We don't need to keep the display around, unless we are embedded
307        * in another window as a subsurface, in which case we should continue
308        * to respond to expose() and therefore both the window and the display
309        * are kept alive */
310       if (sink->display && !sink->window) {     /* -> the window was toplevel */
311         /* Force all buffers to return to the pool, regardless of
312          * whether the compositor has released them or not. We are
313          * going to kill the display, so we need to return all buffers
314          * to be destroyed before this happens.
315          * Note that this is done here instead of the pool destructor
316          * because the buffers hold a reference to the pool. Also,
317          * the buffers can only be unref'ed from the display's event loop
318          * and the pool holds a reference to the display. If we drop
319          * our references here, when the compositor releases the buffers,
320          * they will be unref'ed from the event loop thread, which will
321          * unref the pool and therefore the display, which will try to
322          * stop the thread from within itself and cause a deadlock.
323          */
324         if (sink->pool) {
325           gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
326               (sink->pool));
327         }
328         g_clear_object (&sink->display);
329         g_clear_object (&sink->pool);
330       }
331       break;
332     default:
333       break;
334   }
335
336   return ret;
337 }
338
339 static void
340 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
341 {
342   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
343
344   if (gst_context_has_context_type (context,
345           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
346     struct wl_display *display;
347     GError *error = NULL;
348
349     g_clear_object (&sink->display);
350     display = gst_wayland_display_handle_context_get_handle (context);
351     sink->display = gst_wl_display_new_existing (display, FALSE, &error);
352
353     if (error) {
354       GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
355           ("Could not set display handle"),
356           ("Failed to use the external wayland display: '%s'", error->message));
357       g_error_free (error);
358     }
359   }
360
361   if (GST_ELEMENT_CLASS (parent_class)->set_context)
362     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
363 }
364
365 static GstCaps *
366 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
367 {
368   GstWaylandSink *sink;
369   GstCaps *caps;
370
371   sink = GST_WAYLAND_SINK (bsink);
372
373   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
374
375   if (sink->display) {
376     GValue list = G_VALUE_INIT;
377     GValue value = G_VALUE_INIT;
378     GArray *formats;
379     gint i;
380     enum wl_shm_format fmt;
381
382     g_value_init (&list, GST_TYPE_LIST);
383     g_value_init (&value, G_TYPE_STRING);
384
385     formats = sink->display->formats;
386     for (i = 0; i < formats->len; i++) {
387       fmt = g_array_index (formats, uint32_t, i);
388       g_value_set_string (&value, gst_wayland_format_to_string (fmt));
389       gst_value_list_append_value (&list, &value);
390     }
391
392     caps = gst_caps_make_writable (caps);
393     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
394
395     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
396   }
397
398   if (filter) {
399     GstCaps *intersection;
400
401     intersection =
402         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
403     gst_caps_unref (caps);
404     caps = intersection;
405   }
406
407   return caps;
408 }
409
410 static gboolean
411 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
412 {
413   GstWaylandSink *sink;
414   GstBufferPool *newpool;
415   GstVideoInfo info;
416   enum wl_shm_format format;
417   GArray *formats;
418   gint i;
419   GstStructure *structure;
420   static GstAllocationParams params = { 0, 0, 0, 15, };
421
422   sink = GST_WAYLAND_SINK (bsink);
423   GST_OBJECT_LOCK (sink);
424
425   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
426
427   /* extract info from caps */
428   if (!gst_video_info_from_caps (&info, caps))
429     goto invalid_format;
430
431   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
432   if (format == -1)
433     goto invalid_format;
434
435   /* store the video size */
436   sink->video_width = info.width;
437   sink->video_height = info.height;
438
439   /* verify we support the requested format */
440   formats = sink->display->formats;
441   for (i = 0; i < formats->len; i++) {
442     if (g_array_index (formats, uint32_t, i) == format)
443       break;
444   }
445
446   if (i >= formats->len)
447     goto unsupported_format;
448
449   /* create a new pool for the new configuration */
450   newpool = gst_wayland_buffer_pool_new (sink->display);
451   if (!newpool)
452     goto pool_failed;
453
454   structure = gst_buffer_pool_get_config (newpool);
455   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
456   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
457   if (!gst_buffer_pool_set_config (newpool, structure))
458     goto config_failed;
459
460   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
461   gst_object_unref (newpool);
462
463   GST_OBJECT_UNLOCK (sink);
464   return TRUE;
465
466 invalid_format:
467   {
468     GST_DEBUG_OBJECT (sink,
469         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
470     goto failure;
471   }
472 unsupported_format:
473   {
474     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
475         gst_wayland_format_to_string (format));
476     goto failure;
477   }
478 pool_failed:
479   {
480     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
481     goto failure;
482   }
483 config_failed:
484   {
485     GST_DEBUG_OBJECT (bsink, "failed setting config");
486     goto failure;
487   }
488 failure:
489   {
490     GST_OBJECT_UNLOCK (sink);
491     return FALSE;
492   }
493 }
494
495 static gboolean
496 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
497 {
498   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
499   GstBufferPool *pool = NULL;
500   GstStructure *config;
501   GstCaps *caps;
502   guint size;
503   gboolean need_pool;
504
505   gst_query_parse_allocation (query, &caps, &need_pool);
506
507   if (caps == NULL)
508     goto no_caps;
509
510   if (sink->pool)
511     pool = gst_object_ref (sink->pool);
512
513   if (pool != NULL) {
514     GstCaps *pcaps;
515
516     /* we had a pool, check caps */
517     config = gst_buffer_pool_get_config (pool);
518     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
519
520     if (!gst_caps_is_equal (caps, pcaps)) {
521       /* different caps, we can't use this pool */
522       gst_object_unref (pool);
523       pool = NULL;
524     }
525     gst_structure_free (config);
526   }
527
528   if (pool == NULL && need_pool) {
529     GstVideoInfo info;
530
531     if (!gst_video_info_from_caps (&info, caps))
532       goto invalid_caps;
533
534     GST_DEBUG_OBJECT (sink, "create new pool");
535     pool = gst_wayland_buffer_pool_new (sink->display);
536
537     /* the normal size of a frame */
538     size = info.size;
539
540     config = gst_buffer_pool_get_config (pool);
541     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
542     if (!gst_buffer_pool_set_config (pool, config))
543       goto config_failed;
544   }
545   if (pool) {
546     gst_query_add_allocation_pool (query, pool, size, 2, 0);
547     gst_object_unref (pool);
548   }
549
550   return TRUE;
551
552   /* ERRORS */
553 no_caps:
554   {
555     GST_DEBUG_OBJECT (bsink, "no caps specified");
556     return FALSE;
557   }
558 invalid_caps:
559   {
560     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
561     return FALSE;
562   }
563 config_failed:
564   {
565     GST_DEBUG_OBJECT (bsink, "failed setting config");
566     gst_object_unref (pool);
567     return FALSE;
568   }
569 }
570
571 static GstFlowReturn
572 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
573 {
574   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
575   return gst_wayland_sink_render (bsink, buffer);
576 }
577
578 static void
579 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
580 {
581   GstWaylandSink *sink = data;
582
583   GST_LOG ("frame_redraw_cb");
584
585   g_atomic_int_set (&sink->redraw_pending, FALSE);
586   wl_callback_destroy (callback);
587 }
588
589 static const struct wl_callback_listener frame_callback_listener = {
590   frame_redraw_callback
591 };
592
593 /* must be called with the object lock */
594 static void
595 render_last_buffer (GstWaylandSink * sink)
596 {
597   GstWlMeta *meta;
598   struct wl_surface *surface;
599   struct wl_callback *callback;
600   GstVideoRectangle src, dst, res;
601
602   meta = gst_buffer_get_wl_meta (sink->last_buffer);
603   surface = gst_wl_window_get_wl_surface (sink->window);
604
605   g_atomic_int_set (&sink->redraw_pending, TRUE);
606   callback = wl_surface_frame (surface);
607   wl_callback_add_listener (callback, &frame_callback_listener, sink);
608
609   /* Here we essentially add a reference to the buffer. This represents
610    * the fact that the compositor is using the buffer and it should
611    * not return back to the pool and be reused until the compositor
612    * releases it. The release is handled internally in the pool */
613   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
614
615   src.w = sink->video_width;
616   src.h = sink->video_height;
617   dst.w = sink->window->width;
618   dst.h = sink->window->height;
619   gst_video_sink_center_rect (src, dst, &res, TRUE);
620
621   if (sink->window->subsurface)
622     wl_subsurface_set_position (sink->window->subsurface,
623         sink->window->x + res.x, sink->window->y + res.y);
624   wl_viewport_set_destination (sink->window->viewport, res.w, res.h);
625
626   wl_surface_attach (surface, meta->wbuffer, 0, 0);
627   wl_surface_damage (surface, 0, 0, dst.w, dst.h);
628
629   wl_surface_commit (surface);
630   wl_display_flush (sink->display->display);
631 }
632
633 static GstFlowReturn
634 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
635 {
636   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
637   GstBuffer *to_render;
638   GstWlMeta *meta;
639   GstFlowReturn ret = GST_FLOW_OK;
640
641   /* ask for window handle. do that before locking the sink, because
642    * set_window_handle & friends will lock it in this context */
643   if (!sink->window)
644     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
645
646   GST_OBJECT_LOCK (sink);
647
648   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
649
650   /* if we were not provided a window, create one ourselves */
651   if (!sink->window)
652     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
653         sink->video_height);
654   else if (sink->window->width == 0 || sink->window->height == 0)
655     goto no_window_size;
656
657   /* surface is resizing - drop buffers until finished */
658   if (sink->drawing_frozen)
659     goto done;
660
661   /* drop buffers until we get a frame callback */
662   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
663     goto done;
664
665   meta = gst_buffer_get_wl_meta (buffer);
666
667   if (meta && meta->pool->display == sink->display) {
668     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
669     to_render = buffer;
670   } else {
671     GstMapInfo src;
672     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
673
674     if (!sink->pool)
675       goto no_pool;
676
677     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
678       goto activate_failed;
679
680     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
681     if (ret != GST_FLOW_OK)
682       goto no_buffer;
683
684     gst_buffer_map (buffer, &src, GST_MAP_READ);
685     gst_buffer_fill (to_render, 0, src.data, src.size);
686     gst_buffer_unmap (buffer, &src);
687   }
688
689   gst_buffer_replace (&sink->last_buffer, to_render);
690   render_last_buffer (sink);
691
692   /* notify _resume_rendering() in case it's waiting */
693   sink->rendered = TRUE;
694   g_cond_broadcast (&sink->render_cond);
695
696   if (buffer != to_render)
697     gst_buffer_unref (to_render);
698   goto done;
699
700 no_window_size:
701   {
702     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
703         ("Window has no size set"),
704         ("Make sure you set the size after calling set_window_handle"));
705     ret = GST_FLOW_ERROR;
706     goto done;
707   }
708 no_buffer:
709   {
710     GST_WARNING_OBJECT (sink, "could not create image");
711     goto done;
712   }
713 no_pool:
714   {
715     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
716         ("Internal error: can't allocate images"),
717         ("We don't have a bufferpool negotiated"));
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     GST_OBJECT_UNLOCK (sink);
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   GST_OBJECT_LOCK (sink);
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   GST_OBJECT_UNLOCK (sink);
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   GST_OBJECT_LOCK (sink);
787   if (!sink->window) {
788     GST_OBJECT_UNLOCK (sink);
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_size (sink->window, x, y, w, h);
797   GST_OBJECT_UNLOCK (sink);
798 }
799
800 static void
801 gst_wayland_sink_expose (GstVideoOverlay * overlay)
802 {
803   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
804
805   g_return_if_fail (sink != NULL);
806
807   GST_DEBUG_OBJECT (sink, "expose");
808
809   GST_OBJECT_LOCK (sink);
810   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
811     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
812     render_last_buffer (sink);
813   }
814   GST_OBJECT_UNLOCK (sink);
815 }
816
817 static void
818 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
819 {
820   iface->pause_rendering = gst_wayland_sink_pause_rendering;
821   iface->resume_rendering = gst_wayland_sink_resume_rendering;
822 }
823
824 static void
825 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
826 {
827   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
828   g_return_if_fail (sink != NULL);
829
830   GST_OBJECT_LOCK (sink);
831   sink->drawing_frozen = TRUE;
832   GST_OBJECT_UNLOCK (sink);
833 }
834
835 static void
836 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
837 {
838   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
839   g_return_if_fail (sink != NULL);
840
841   GST_DEBUG_OBJECT (sink, "resuming rendering");
842
843   GST_OBJECT_LOCK (sink);
844   sink->drawing_frozen = FALSE;
845
846   if (GST_STATE (sink) == GST_STATE_PLAYING) {
847     sink->rendered = FALSE;
848     while (sink->rendered == FALSE)
849       g_cond_wait (&sink->render_cond, GST_OBJECT_GET_LOCK (sink));
850     GST_DEBUG_OBJECT (sink, "synchronized with render()");
851   } else if (sink->window && sink->last_buffer &&
852       g_atomic_int_get (&sink->redraw_pending) == FALSE) {
853     render_last_buffer (sink);
854     GST_DEBUG_OBJECT (sink, "last buffer redrawn");
855   }
856
857   GST_OBJECT_UNLOCK (sink);
858 }
859
860 static gboolean
861 plugin_init (GstPlugin * plugin)
862 {
863   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
864       " wayland video sink");
865
866   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
867       GST_TYPE_WAYLAND_SINK);
868 }
869
870 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
871     GST_VERSION_MINOR,
872     waylandsink,
873     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
874     GST_PACKAGE_ORIGIN)