Waylandsink : Add rendering current wl_buffer for the changed geometory value
[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 #ifdef GST_WLSINK_ENHANCEMENT
46 #include <mm_types.h>
47 #include "tizen-wlvideoformat.h"
48 #else
49 #include "wlvideoformat.h"
50 #endif
51 #include "waylandpool.h"
52
53 #include <gst/wayland/wayland.h>
54 #include <gst/video/videooverlay.h>
55
56 #ifdef GST_WLSINK_ENHANCEMENT
57 #define GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD (gst_waylandsink_display_geometry_method_get_type())
58 #define GST_TYPE_WAYLANDSINK_ROTATE_ANGLE (gst_waylandsink_rotate_angle_get_type())
59 #define GST_TYPE_WAYLANDSINK_FLIP (gst_waylandsink_flip_get_type())
60
61 static GType
62 gst_waylandsink_rotate_angle_get_type (void)
63 {
64   static GType waylandsink_rotate_angle_type = 0;
65   static const GEnumValue rotate_angle_type[] = {
66     {0, "No rotate", "DEGREE_0"},
67     {1, "Rotate 90 degree", "DEGREE_90"},
68     {2, "Rotate 180 degree", "DEGREE_180"},
69     {3, "Rotate 270 degree", "DEGREE_270"},
70     {4, NULL, NULL},
71   };
72
73   if (!waylandsink_rotate_angle_type) {
74     waylandsink_rotate_angle_type =
75         g_enum_register_static ("GstWaylandSinkRotateAngleType",
76         rotate_angle_type);
77   }
78
79   return waylandsink_rotate_angle_type;
80 }
81
82
83 static GType
84 gst_waylandsink_display_geometry_method_get_type (void)
85 {
86   static GType waylandsink_display_geometry_method_type = 0;
87   static const GEnumValue display_geometry_method_type[] = {
88     {0, "Letter box", "LETTER_BOX"},
89     {1, "Origin size", "ORIGIN_SIZE"},
90     {2, "Full-screen", "FULL_SCREEN"},
91     {3, "Cropped full-screen", "CROPPED_FULL_SCREEN"},
92     {4, "Origin size(if screen size is larger than video size(width/height)) or Letter box(if video size(width/height) is larger than screen size)", "ORIGIN_SIZE_OR_LETTER_BOX"},
93     {5, NULL, NULL},
94   };
95
96   if (!waylandsink_display_geometry_method_type) {
97     waylandsink_display_geometry_method_type =
98         g_enum_register_static ("GstWaylandSinkDisplayGeometryMethodType",
99         display_geometry_method_type);
100   }
101   return waylandsink_display_geometry_method_type;
102 }
103
104 static GType
105 gst_waylandsink_flip_get_type (void)
106 {
107   static GType waylandsink_flip_type = 0;
108   static const GEnumValue flip_type[] = {
109     {FLIP_NONE, "Flip NONE", "FLIP_NONE"},
110     {FLIP_HORIZONTAL, "Flip HORIZONTAL", "FLIP_HORIZONTAL"},
111     {FLIP_VERTICAL, "Flip VERTICAL", "FLIP_VERTICAL"},
112     {FLIP_BOTH, "Flip BOTH", "FLIP_BOTH"},
113     {FLIP_NUM, NULL, NULL},
114   };
115
116   if (!waylandsink_flip_type) {
117     waylandsink_flip_type =
118         g_enum_register_static ("GstWaylandSinkFlipType", flip_type);
119   }
120
121   return waylandsink_flip_type;
122 }
123
124 #endif
125
126
127 /* signals */
128 enum
129 {
130   SIGNAL_0,
131   LAST_SIGNAL
132 };
133
134 /* Properties */
135 enum
136 {
137   PROP_0,
138   PROP_DISPLAY,
139 #ifdef GST_WLSINK_ENHANCEMENT
140   PROP_ROTATE_ANGLE,
141   PROP_DISPLAY_GEOMETRY_METHOD,
142   PROP_ORIENTATION,
143   PROP_FLIP
144 #endif
145 };
146
147 GST_DEBUG_CATEGORY (gstwayland_debug);
148 #define GST_CAT_DEFAULT gstwayland_debug
149
150 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
151     GST_PAD_SINK,
152     GST_PAD_ALWAYS,
153     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
154         ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
155             "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
156 #ifdef GST_WLSINK_ENHANCEMENT
157             "SN12, ST12, "
158 #endif
159             "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
160     );
161
162 static void gst_wayland_sink_get_property (GObject * object,
163     guint prop_id, GValue * value, GParamSpec * pspec);
164 static void gst_wayland_sink_set_property (GObject * object,
165     guint prop_id, const GValue * value, GParamSpec * pspec);
166 static void gst_wayland_sink_finalize (GObject * object);
167
168 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
169     GstStateChange transition);
170 static void gst_wayland_sink_set_context (GstElement * element,
171     GstContext * context);
172
173 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
174     GstCaps * filter);
175 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
176 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
177     GstBuffer * buffer);
178 static gboolean
179 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
180 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
181     GstBuffer * buffer);
182
183 /* VideoOverlay interface */
184 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
185     iface);
186 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
187     guintptr handle);
188 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
189     gint x, gint y, gint w, gint h);
190 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
191
192 /* WaylandVideo interface */
193 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
194     iface);
195 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
196 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
197 #ifdef GST_WLSINK_ENHANCEMENT
198 static void gst_wayland_sink_update_window_geometry (GstWaylandSink * sink);
199 static void render_last_buffer (GstWaylandSink * sink);
200 static void gst_wayland_sink_render_last_buffer (GstWaylandSink * sink);
201
202 #endif
203
204 #define gst_wayland_sink_parent_class parent_class
205 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
206     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
207         gst_wayland_sink_videooverlay_init)
208     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
209         gst_wayland_sink_waylandvideo_init));
210
211 static void
212 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
213 {
214   FUNCTION_ENTER ();
215   GObjectClass *gobject_class;
216   GstElementClass *gstelement_class;
217   GstBaseSinkClass *gstbasesink_class;
218
219   gobject_class = (GObjectClass *) klass;
220   gstelement_class = (GstElementClass *) klass;
221   gstbasesink_class = (GstBaseSinkClass *) klass;
222
223   gobject_class->set_property = gst_wayland_sink_set_property;
224   gobject_class->get_property = gst_wayland_sink_get_property;
225   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
226
227   gst_element_class_add_pad_template (gstelement_class,
228       gst_static_pad_template_get (&sink_template));
229
230   gst_element_class_set_static_metadata (gstelement_class,
231       "wayland video sink", "Sink/Video",
232       "Output to wayland surface",
233       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
234       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
235
236   gstelement_class->change_state =
237       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
238   gstelement_class->set_context =
239       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
240
241   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
242   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
243   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
244   gstbasesink_class->propose_allocation =
245       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
246   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
247
248   g_object_class_install_property (gobject_class, PROP_DISPLAY,
249       g_param_spec_string ("display", "Wayland Display name", "Wayland "
250           "display name to connect to, if not supplied via the GstContext",
251           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 #ifdef GST_WLSINK_ENHANCEMENT
253   g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
254       g_param_spec_enum ("rotate", "Rotate angle",
255           "Rotate angle of display output",
256           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
257           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258
259   g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
260       g_param_spec_enum ("display-geometry-method", "Display geometry method",
261           "Geometrical method for display",
262           GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
263           DEF_DISPLAY_GEOMETRY_METHOD,
264           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265
266   g_object_class_install_property (gobject_class, PROP_ORIENTATION,
267       g_param_spec_enum ("orientation",
268           "Orientation information used for ROI/ZOOM",
269           "Orientation information for display",
270           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
271           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
272
273   g_object_class_install_property (gobject_class, PROP_FLIP,
274       g_param_spec_enum ("flip", "Display flip",
275           "Flip for display",
276           GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
277           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
278
279 #endif
280 }
281
282 static void
283 gst_wayland_sink_init (GstWaylandSink * sink)
284 {
285   FUNCTION_ENTER ();
286
287   sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
288   sink->flip = DEF_DISPLAY_FLIP;
289   sink->rotate_angle = DEGREE_0;
290   sink->orientation = DEGREE_0;
291
292   g_mutex_init (&sink->display_lock);
293   g_mutex_init (&sink->render_lock);
294 }
295
296 static void
297 gst_wayland_sink_get_property (GObject * object,
298     guint prop_id, GValue * value, GParamSpec * pspec)
299 {
300   FUNCTION_ENTER ();
301
302   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
303   switch (prop_id) {
304     case PROP_DISPLAY:
305       GST_OBJECT_LOCK (sink);
306       g_value_set_string (value, sink->display_name);
307       GST_OBJECT_UNLOCK (sink);
308       break;
309 #ifdef GST_WLSINK_ENHANCEMENT
310     case PROP_ROTATE_ANGLE:
311       g_value_set_enum (value, sink->rotate_angle);
312       break;
313     case PROP_DISPLAY_GEOMETRY_METHOD:
314       g_value_set_enum (value, sink->display_geometry_method);
315       break;
316     case PROP_ORIENTATION:
317       g_value_set_enum (value, sink->orientation);
318       break;
319     case PROP_FLIP:
320       g_value_set_enum (value, sink->flip);
321       break;
322 #endif
323     default:
324       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325       break;
326   }
327 }
328
329 static void
330 gst_wayland_sink_set_property (GObject * object,
331     guint prop_id, const GValue * value, GParamSpec * pspec)
332 {
333   FUNCTION_ENTER ();
334
335   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
336   switch (prop_id) {
337     case PROP_DISPLAY:
338       GST_OBJECT_LOCK (sink);
339       sink->display_name = g_value_dup_string (value);
340       GST_OBJECT_UNLOCK (sink);
341       break;
342 #ifdef GST_WLSINK_ENHANCEMENT
343     case PROP_ROTATE_ANGLE:
344       sink->rotate_angle = g_value_get_enum (value);
345       GST_INFO_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
346       if (sink->window) {
347         gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
348       }
349       sink->video_info_changed = TRUE;
350       if (GST_STATE (sink) == GST_STATE_PAUSED) {
351         /*need to render last buffer */
352         gst_wayland_sink_render_last_buffer (sink);
353       }
354       break;
355     case PROP_DISPLAY_GEOMETRY_METHOD:
356       sink->display_geometry_method = g_value_get_enum (value);
357       GST_INFO_OBJECT (sink, "Display geometry method is set (%d)",
358           sink->display_geometry_method);
359       if (sink->window) {
360         gst_wl_window_set_disp_geo_method (sink->window,
361             sink->display_geometry_method);
362       }
363       sink->video_info_changed = TRUE;
364       if (GST_STATE (sink) == GST_STATE_PAUSED) {
365         /*need to render last buffer */
366         gst_wayland_sink_render_last_buffer (sink);
367       }
368       break;
369     case PROP_ORIENTATION:
370       sink->orientation = g_value_get_enum (value);
371       GST_INFO_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
372       if (sink->window) {
373         gst_wl_window_set_orientation (sink->window, sink->orientation);
374       }
375       sink->video_info_changed = TRUE;
376       if (GST_STATE (sink) == GST_STATE_PAUSED) {
377         /*need to render last buffer */
378         gst_wayland_sink_render_last_buffer (sink);
379       }
380       break;
381     case PROP_FLIP:
382       sink->flip = g_value_get_enum (value);
383       GST_INFO_OBJECT (sink, "flip is set (%d)", sink->flip);
384       if (sink->flip) {
385         gst_wl_window_set_flip (sink->window, sink->flip);
386       }
387       sink->video_info_changed = TRUE;
388       if (GST_STATE (sink) == GST_STATE_PAUSED) {
389         /*need to render last buffer */
390         gst_wayland_sink_render_last_buffer (sink);
391       }
392       break;
393 #endif
394     default:
395       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
396       break;
397   }
398 }
399
400 static void
401 gst_wayland_sink_finalize (GObject * object)
402 {
403   FUNCTION_ENTER ();
404
405   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
406   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
407
408   if (sink->last_buffer)
409     gst_buffer_unref (sink->last_buffer);
410   if (sink->display) {
411     /* see comment about this call in gst_wayland_sink_change_state() */
412 #ifdef GST_WLSINK_ENHANCEMENT
413     if (sink->pool && !sink->display->is_native_format)
414 #else
415     if (sink->pool)
416 #endif
417       gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
418           (sink->pool));
419
420     g_object_unref (sink->display);
421     sink->display = NULL;
422   }
423   if (sink->window) {
424     g_object_unref (sink->window);
425     sink->window = NULL;
426   }
427   if (sink->pool) {
428     gst_object_unref (sink->pool);
429     sink->pool = NULL;
430   }
431
432   if (sink->display_name) {
433     g_free (sink->display_name);
434     sink->display_name = NULL;
435   }
436
437   g_mutex_clear (&sink->display_lock);
438   g_mutex_clear (&sink->render_lock);
439
440   G_OBJECT_CLASS (parent_class)->finalize (object);
441 }
442
443 /* must be called with the display_lock */
444 static void
445 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
446     GstContext * context)
447 {
448   FUNCTION_ENTER ();
449
450   struct wl_display *display;
451   GError *error = NULL;
452
453   display = gst_wayland_display_handle_context_get_handle (context);
454   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
455
456   if (error) {
457     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
458         ("Could not set display handle"),
459         ("Failed to use the external wayland display: '%s'", error->message));
460     g_error_free (error);
461   }
462 }
463
464 static gboolean
465 gst_wayland_sink_find_display (GstWaylandSink * sink)
466 {
467   FUNCTION_ENTER ();
468
469   GstQuery *query;
470   GstMessage *msg;
471   GstContext *context = NULL;
472   GError *error = NULL;
473   gboolean ret = TRUE;
474
475   g_mutex_lock (&sink->display_lock);
476
477   if (!sink->display) {
478     /* first query upstream for the needed display handle */
479     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
480     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
481       gst_query_parse_context (query, &context);
482       gst_wayland_sink_set_display_from_context (sink, context);
483     }
484     gst_query_unref (query);
485
486     if (G_LIKELY (!sink->display)) {
487       /* now ask the application to set the display handle */
488       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
489           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
490
491       g_mutex_unlock (&sink->display_lock);
492       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
493       /* at this point we expect gst_wayland_sink_set_context
494        * to get called and fill sink->display */
495       g_mutex_lock (&sink->display_lock);
496
497       if (!sink->display) {
498         /* if the application didn't set a display, let's create it ourselves */
499         GST_OBJECT_LOCK (sink);
500         sink->display = gst_wl_display_new (sink->display_name, &error);
501         GST_OBJECT_UNLOCK (sink);
502
503         if (error) {
504           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
505               ("Could not initialise Wayland output"),
506               ("Failed to create GstWlDisplay: '%s'", error->message));
507           g_error_free (error);
508           ret = FALSE;
509         } else {
510           /* inform the world about the new display */
511           context =
512               gst_wayland_display_handle_context_new (sink->display->display);
513           msg = gst_message_new_have_context (GST_OBJECT_CAST (sink), context);
514           gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
515         }
516       }
517     }
518   }
519
520   g_mutex_unlock (&sink->display_lock);
521
522   return ret;
523 }
524
525 static GstStateChangeReturn
526 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
527 {
528   FUNCTION_ENTER ();
529
530   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
531   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
532
533   switch (transition) {
534     case GST_STATE_CHANGE_NULL_TO_READY:
535       if (!gst_wayland_sink_find_display (sink))
536         return GST_STATE_CHANGE_FAILURE;
537       break;
538     default:
539       break;
540   }
541
542   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
543   if (ret == GST_STATE_CHANGE_FAILURE)
544     return ret;
545
546   switch (transition) {
547     case GST_STATE_CHANGE_PAUSED_TO_READY:
548       gst_buffer_replace (&sink->last_buffer, NULL);
549       if (sink->window) {
550         if (gst_wl_window_is_toplevel (sink->window)) {
551           g_clear_object (&sink->window);
552         } else {
553           /* remove buffer from surface, show nothing */
554           wl_surface_attach (sink->window->surface, NULL, 0, 0);
555           wl_surface_damage (sink->window->surface, 0, 0,
556               sink->window->surface_width, sink->window->surface_height);
557           wl_surface_commit (sink->window->surface);
558           wl_display_flush (sink->display->display);
559         }
560       }
561       break;
562     case GST_STATE_CHANGE_READY_TO_NULL:
563       g_mutex_lock (&sink->display_lock);
564       /* If we had a toplevel window, we most likely have our own connection
565        * to the display too, and it is a good idea to disconnect and allow
566        * potentially the application to embed us with GstVideoOverlay
567        * (which requires to re-use the same display connection as the parent
568        * surface). If we didn't have a toplevel window, then the display
569        * connection that we have is definitely shared with the application
570        * and it's better to keep it around (together with the window handle)
571        * to avoid requesting them again from the application if/when we are
572        * restarted (GstVideoOverlay behaves like that in other sinks)
573        */
574       if (sink->display && !sink->window) {     /* -> the window was toplevel */
575         /* Force all buffers to return to the pool, regardless of
576          * whether the compositor has released them or not. We are
577          * going to kill the display, so we need to return all buffers
578          * to be destroyed before this happens.
579          * Note that this is done here instead of the pool destructor
580          * because the buffers hold a reference to the pool. Also,
581          * the buffers can only be unref'ed from the display's event loop
582          * and the pool holds a reference to the display. If we drop
583          * our references here, when the compositor releases the buffers,
584          * they will be unref'ed from the event loop thread, which will
585          * unref the pool and therefore the display, which will try to
586          * stop the thread from within itself and cause a deadlock.
587          */
588         if (sink->pool) {
589           gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
590               (sink->pool));
591         }
592         g_clear_object (&sink->display);
593         g_clear_object (&sink->pool);
594       }
595       g_mutex_unlock (&sink->display_lock);
596       break;
597     default:
598       break;
599   }
600
601   return ret;
602 }
603
604 static void
605 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
606 {
607   FUNCTION_ENTER ();
608
609   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
610   if (gst_context_has_context_type (context,
611           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
612     g_mutex_lock (&sink->display_lock);
613     if (G_LIKELY (!sink->display)) {
614       gst_wayland_sink_set_display_from_context (sink, context);
615     } else {
616       GST_WARNING_OBJECT (element, "changing display handle is not supported");
617       g_mutex_unlock (&sink->display_lock);
618       return;
619     }
620     g_mutex_unlock (&sink->display_lock);
621   }
622
623   GST_INFO ("element %p context %p", element, context);
624   if (GST_ELEMENT_CLASS (parent_class)->set_context)
625     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
626 }
627
628 static GstCaps *
629 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
630 {
631   FUNCTION_ENTER ();
632
633   GstWaylandSink *sink;
634   GstCaps *caps;
635   sink = GST_WAYLAND_SINK (bsink);
636
637   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
638
639   g_mutex_lock (&sink->display_lock);
640
641   if (sink->display) {
642     GValue list = G_VALUE_INIT;
643     GValue value = G_VALUE_INIT;
644     GArray *formats;
645     gint i;
646 #ifdef GST_WLSINK_ENHANCEMENT
647     enum tizen_buffer_pool_format fmt;
648 #else
649     enum wl_shm_format fmt;
650 #endif
651
652     g_value_init (&list, GST_TYPE_LIST);
653     g_value_init (&value, G_TYPE_STRING);
654
655     formats = sink->display->formats;
656     for (i = 0; i < formats->len; i++) {
657       fmt = g_array_index (formats, uint32_t, i);
658       g_value_set_string (&value, gst_wayland_format_to_string (fmt));
659       gst_value_list_append_value (&list, &value);
660     }
661
662     caps = gst_caps_make_writable (caps);
663     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
664
665     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
666   }
667
668   g_mutex_unlock (&sink->display_lock);
669
670   if (filter) {
671     GstCaps *intersection;
672
673     intersection =
674         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
675     gst_caps_unref (caps);
676     caps = intersection;
677   }
678
679   return caps;
680 }
681
682 static gboolean
683 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
684 {
685   FUNCTION_ENTER ();
686
687   GstWaylandSink *sink;
688   GstBufferPool *newpool;
689   GstVideoInfo info;
690 #ifdef GST_WLSINK_ENHANCEMENT
691   enum tizen_buffer_pool_format format;
692 #else
693   enum wl_shm_format format;
694 #endif
695   GArray *formats;
696   gint i;
697   GstStructure *structure;
698   static GstAllocationParams params = { 0, 0, 0, 15, };
699   sink = GST_WAYLAND_SINK (bsink);
700
701   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
702
703   /* extract info from caps */
704   if (!gst_video_info_from_caps (&info, caps))
705     goto invalid_format;
706 #ifdef GST_WLSINK_ENHANCEMENT
707   sink->caps = gst_caps_copy (caps);
708 #endif
709
710   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
711   if ((gint) format == -1)
712     goto invalid_format;
713
714   /* verify we support the requested format */
715   formats = sink->display->formats;
716   for (i = 0; i < formats->len; i++) {
717     if (g_array_index (formats, uint32_t, i) == format)
718       break;
719   }
720
721   if (i >= formats->len)
722     goto unsupported_format;
723
724 #ifdef GST_WLSINK_ENHANCEMENT
725   if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
726       GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
727     sink->display->is_native_format = TRUE;
728   } else {
729     sink->display->is_native_format = FALSE;
730
731     /* create a new pool for the new configuration */
732     newpool = gst_wayland_buffer_pool_new (sink->display);
733     if (!newpool)
734       goto pool_failed;
735
736     structure = gst_buffer_pool_get_config (newpool);
737     gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
738     gst_buffer_pool_config_set_allocator (structure, NULL, &params);
739     if (!gst_buffer_pool_set_config (newpool, structure))
740       goto config_failed;
741
742     gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
743     gst_object_unref (newpool);
744
745   }
746   /* store the video info */
747   sink->video_info = info;
748   sink->video_info_changed = TRUE;
749 #else
750   /* create a new pool for the new configuration */
751   newpool = gst_wayland_buffer_pool_new (sink->display);
752   if (!newpool)
753     goto pool_failed;
754
755   structure = gst_buffer_pool_get_config (newpool);
756   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
757   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
758   if (!gst_buffer_pool_set_config (newpool, structure))
759     goto config_failed;
760
761   /* store the video info */
762   sink->video_info = info;
763   sink->video_info_changed = TRUE;
764
765   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
766   gst_object_unref (newpool);
767 #endif
768   return TRUE;
769
770 invalid_format:
771   {
772     GST_DEBUG_OBJECT (sink,
773         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
774     return FALSE;
775   }
776 unsupported_format:
777   {
778     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
779         gst_wayland_format_to_string (format));
780     return FALSE;
781   }
782 pool_failed:
783   {
784     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
785     return FALSE;
786   }
787 config_failed:
788   {
789     GST_DEBUG_OBJECT (bsink, "failed setting config");
790     gst_object_unref (newpool);
791     return FALSE;
792   }
793 }
794
795 static gboolean
796 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
797 {
798   FUNCTION_ENTER ();
799
800   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
801   GstBufferPool *pool = NULL;
802   GstStructure *config;
803   GstCaps *caps;
804   guint size;
805   gboolean need_pool;
806
807   if (sink->display->is_native_format == TRUE)
808     return TRUE;
809
810   gst_query_parse_allocation (query, &caps, &need_pool);
811
812   if (caps == NULL)
813     goto no_caps;
814
815   if (sink->pool)
816     pool = gst_object_ref (sink->pool);
817
818   if (pool != NULL) {
819     GstCaps *pcaps;
820
821     /* we had a pool, check caps */
822     config = gst_buffer_pool_get_config (pool);
823     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
824
825     if (!gst_caps_is_equal (caps, pcaps)) {
826       /* different caps, we can't use this pool */
827       gst_object_unref (pool);
828       pool = NULL;
829     }
830     gst_structure_free (config);
831   }
832
833   if (pool == NULL && need_pool) {
834     GstVideoInfo info;
835
836     if (!gst_video_info_from_caps (&info, caps))
837       goto invalid_caps;
838
839     GST_DEBUG_OBJECT (sink, "create new pool");
840     pool = gst_wayland_buffer_pool_new (sink->display);
841
842     /* the normal size of a frame */
843     size = info.size;
844
845     config = gst_buffer_pool_get_config (pool);
846     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
847     if (!gst_buffer_pool_set_config (pool, config))
848       goto config_failed;
849   }
850   if (pool) {
851     gst_query_add_allocation_pool (query, pool, size, 2, 0);
852     gst_object_unref (pool);
853   }
854
855   return TRUE;
856
857   /* ERRORS */
858 no_caps:
859   {
860     GST_DEBUG_OBJECT (bsink, "no caps specified");
861     return FALSE;
862   }
863 invalid_caps:
864   {
865     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
866     return FALSE;
867   }
868 config_failed:
869   {
870     GST_DEBUG_OBJECT (bsink, "failed setting config");
871     gst_object_unref (pool);
872     return FALSE;
873   }
874 }
875
876 static GstFlowReturn
877 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
878 {
879   FUNCTION_ENTER ();
880
881   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
882   return gst_wayland_sink_render (bsink, buffer);
883 }
884
885 static void
886 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
887 {
888   FUNCTION_ENTER ();
889
890   GstWaylandSink *sink = data;
891
892   GST_LOG ("frame_redraw_cb");
893
894   g_atomic_int_set (&sink->redraw_pending, FALSE);
895   wl_callback_destroy (callback);
896 }
897
898 static const struct wl_callback_listener frame_callback_listener = {
899   frame_redraw_callback
900 };
901
902 /* must be called with the render lock */
903 static void
904 render_last_buffer (GstWaylandSink * sink)
905 {
906   FUNCTION_ENTER ();
907
908   GstWlMeta *meta;
909   struct wl_surface *surface;
910   struct wl_callback *callback;
911
912   meta = gst_buffer_get_wl_meta (sink->last_buffer);
913   surface = gst_wl_window_get_wl_surface (sink->window);
914
915   g_atomic_int_set (&sink->redraw_pending, TRUE);
916   callback = wl_surface_frame (surface);
917   wl_callback_add_listener (callback, &frame_callback_listener, sink);
918
919   /* Here we essentially add a reference to the buffer. This represents
920    * the fact that the compositor is using the buffer and it should
921    * not return back to the pool and be reused until the compositor
922    * releases it. The release is handled internally in the pool */
923   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
924
925   GST_DEBUG ("wl_surface_attach wl_buffer %p", meta->wbuffer);
926
927   wl_surface_attach (surface, meta->wbuffer, 0, 0);
928   wl_surface_damage (surface, 0, 0, sink->window->surface_width,
929       sink->window->surface_height);
930
931   wl_surface_commit (surface);
932   wl_display_flush (sink->display->display);
933 }
934
935 static GstFlowReturn
936 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
937 {
938   FUNCTION_ENTER ();
939
940   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
941   GstBuffer *to_render;
942   GstWlMeta *meta;
943   GstFlowReturn ret = GST_FLOW_OK;
944
945 #ifdef GST_WLSINK_ENHANCEMENT
946   GstBufferPool *newpool;
947   GstStructure *structure;
948   static GstAllocationParams params = { 0, 0, 0, 15, };
949 #endif
950
951   g_mutex_lock (&sink->render_lock);
952
953   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
954
955   if (G_UNLIKELY (!sink->window)) {
956     /* ask for window handle. Unlock render_lock while doing that because
957      * set_window_handle & friends will lock it in this context */
958     g_mutex_unlock (&sink->render_lock);
959     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
960     g_mutex_lock (&sink->render_lock);
961
962     if (sink->window) {
963       /* inform the window about our caps */
964       gst_wl_window_set_video_info (sink->window, &sink->video_info);
965     } else {
966       /* if we were not provided a window, create one ourselves */
967       sink->window =
968           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
969     }
970 #ifdef GST_WLSINK_ENHANCEMENT
971     gst_wayland_sink_update_window_geometry (sink);
972     sink->video_info_changed = TRUE;
973 #else
974     sink->video_info_changed = FALSE;
975 #endif
976   }
977
978   /* drop buffers until we get a frame callback */
979   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
980     goto done;
981
982   if (G_UNLIKELY (sink->video_info_changed)) {
983     gst_wl_window_set_video_info (sink->window, &sink->video_info);
984     sink->video_info_changed = FALSE;
985   }
986   GST_INFO ("window->render_rectangle(%d,%d %d x %d)",
987       sink->window->render_rectangle.x,
988       sink->window->render_rectangle.y,
989       sink->window->render_rectangle.w, sink->window->render_rectangle.h);
990   GST_INFO ("window->surface_width(%d),window->surface_height(%d)",
991       sink->window->surface_width, sink->window->surface_height);
992
993   /* now that we have for sure set the video info on the window, it must have
994    * a valid size, otherwise this means that the application has called
995    * set_window_handle() without calling set_render_rectangle(), which is
996    * absolutely necessary for us.
997    */
998   if (G_UNLIKELY (sink->window->surface_width == 0 ||
999           sink->window->surface_height == 0))
1000     goto no_window_size;
1001
1002   meta = gst_buffer_get_wl_meta (buffer);
1003
1004   if (meta && meta->pool->display == sink->display) {
1005     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
1006     to_render = buffer;
1007   } else {
1008     GstMapInfo src;
1009     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
1010
1011 #ifdef GST_WLSINK_ENHANCEMENT
1012     if (sink->display->is_native_format == TRUE) {
1013       /*in case of SN12 or ST12 video  format */
1014       GstMemory *mem;
1015       GstMapInfo mem_info = GST_MAP_INFO_INIT;
1016       MMVideoBuffer *mm_video_buf = NULL;
1017
1018       mem = gst_buffer_peek_memory (buffer, 1);
1019       gst_memory_map (mem, &mem_info, GST_MAP_READ);
1020       mm_video_buf = (MMVideoBuffer *) mem_info.data;
1021       gst_memory_unmap (mem, &mem_info);
1022
1023       if (mm_video_buf == NULL) {
1024         GST_WARNING_OBJECT (sink, "mm_video_buf is NULL. Skip rendering");
1025         return ret;
1026       }
1027       /* assign mm_video_buf info */
1028       if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
1029         GST_DEBUG_OBJECT (sink, "TBM bo %p %p %p", mm_video_buf->handle.bo[0],
1030             mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
1031
1032         sink->display->native_video_size = 0;
1033
1034         for (int i = 0; i < NV_BUF_PLANE_NUM; i++) {
1035           if (mm_video_buf->handle.bo[i] != NULL) {
1036             sink->display->bo[i] = mm_video_buf->handle.bo[i];
1037           } else {
1038             sink->display->bo[i] = 0;
1039           }
1040           sink->display->plane_size[i] = mm_video_buf->size[i];
1041           sink->display->stride_width[i] = mm_video_buf->stride_width[i];
1042           sink->display->stride_height[i] = mm_video_buf->stride_height[i];
1043           sink->display->native_video_size += sink->display->plane_size[i];
1044         }
1045       } else {
1046         GST_ERROR_OBJECT (sink, "Buffer type is not TBM");
1047         return ret;
1048       }
1049
1050       if (!sink->pool) {
1051
1052         /* create a new pool for the new configuration */
1053         newpool = gst_wayland_buffer_pool_new (sink->display);
1054         if (!newpool) {
1055           GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1056           return FALSE;
1057         }
1058         structure = gst_buffer_pool_get_config (newpool);
1059         gst_buffer_pool_config_set_params (structure, sink->caps,
1060             sink->video_info.size, 2, 0);
1061         gst_buffer_pool_config_set_allocator (structure, NULL, &params);
1062         if (!gst_buffer_pool_set_config (newpool, structure)) {
1063           GST_DEBUG_OBJECT (bsink, "failed setting config");
1064           gst_object_unref (newpool);
1065           return FALSE;
1066         }
1067
1068         gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1069         gst_object_unref (newpool);
1070
1071       }
1072
1073       if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1074         goto activate_failed;
1075
1076       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1077       if (ret != GST_FLOW_OK)
1078         goto no_buffer;
1079
1080
1081       /*add displaying buffer */
1082       GstWlMeta *meta;
1083       meta = gst_buffer_get_wl_meta (to_render);
1084       gst_wayland_buffer_pool_add_displaying_buffer (sink->pool, meta, buffer);
1085
1086     } else {
1087       /*in case of normal video format and pool is not our pool */
1088
1089       if (!sink->pool)
1090         goto no_pool;
1091
1092       if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1093         goto activate_failed;
1094
1095       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1096       if (ret != GST_FLOW_OK)
1097         goto no_buffer;
1098
1099       gst_buffer_map (buffer, &src, GST_MAP_READ);
1100       gst_buffer_fill (to_render, 0, src.data, src.size);
1101       gst_buffer_unmap (buffer, &src);
1102     }
1103 #else
1104     if (!sink->pool)
1105       goto no_pool;
1106
1107     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1108       goto activate_failed;
1109
1110     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1111     if (ret != GST_FLOW_OK)
1112       goto no_buffer;
1113
1114     gst_buffer_map (buffer, &src, GST_MAP_READ);
1115     gst_buffer_fill (to_render, 0, src.data, src.size);
1116     gst_buffer_unmap (buffer, &src);
1117 #endif
1118   }
1119
1120   gst_buffer_replace (&sink->last_buffer, to_render);
1121   render_last_buffer (sink);
1122
1123   if (buffer != to_render) {
1124     GST_LOG_OBJECT (sink, "Decrease ref count of buffer");
1125     gst_buffer_unref (to_render);
1126   }
1127   goto done;
1128
1129 no_window_size:
1130   {
1131     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1132         ("Window has no size set"),
1133         ("Make sure you set the size after calling set_window_handle"));
1134     ret = GST_FLOW_ERROR;
1135     goto done;
1136   }
1137 no_buffer:
1138   {
1139     GST_WARNING_OBJECT (sink, "could not create image");
1140     goto done;
1141   }
1142 no_pool:
1143   {
1144     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1145         ("Internal error: can't allocate images"),
1146         ("We don't have a bufferpool negotiated"));
1147     ret = GST_FLOW_ERROR;
1148     goto done;
1149   }
1150 activate_failed:
1151   {
1152     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1153     ret = GST_FLOW_ERROR;
1154     goto done;
1155   }
1156 done:
1157   {
1158     g_mutex_unlock (&sink->render_lock);
1159     return ret;
1160   }
1161 }
1162
1163 static void
1164 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1165 {
1166   FUNCTION_ENTER ();
1167
1168   iface->set_window_handle = gst_wayland_sink_set_window_handle;
1169   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1170   iface->expose = gst_wayland_sink_expose;
1171 }
1172
1173 static void
1174 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1175 {
1176   FUNCTION_ENTER ();
1177
1178   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1179   struct wl_surface *surface = (struct wl_surface *) handle;
1180
1181   g_return_if_fail (sink != NULL);
1182
1183   if (sink->window != NULL) {
1184     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1185     return;
1186   }
1187
1188   g_mutex_lock (&sink->render_lock);
1189
1190   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1191       (void *) handle);
1192
1193   g_clear_object (&sink->window);
1194
1195   if (handle) {
1196     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1197       /* we cannot use our own display with an external window handle */
1198       if (G_UNLIKELY (sink->display->own_display)) {
1199         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
1200             ("Application did not provide a wayland display handle"),
1201             ("waylandsink cannot use an externally-supplied surface without "
1202                 "an externally-supplied display handle. Consider providing a "
1203                 "display handle from your application with GstContext"));
1204       } else {
1205         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1206         GST_DEBUG ("sink->window %p", sink->window);
1207       }
1208     } else {
1209       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1210           "ignoring window handle");
1211     }
1212   }
1213 #ifdef GST_WLSINK_ENHANCEMENT
1214   gst_wayland_sink_update_window_geometry (sink);
1215 #endif
1216
1217   g_mutex_unlock (&sink->render_lock);
1218 }
1219
1220 #ifdef GST_WLSINK_ENHANCEMENT
1221 static void
1222 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1223 {
1224   FUNCTION_ENTER ();
1225   g_return_if_fail (sink != NULL);
1226   g_return_if_fail (sink->window != NULL);
1227
1228   gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1229   gst_wl_window_set_disp_geo_method (sink->window,
1230       sink->display_geometry_method);
1231   gst_wl_window_set_orientation (sink->window, sink->orientation);
1232   gst_wl_window_set_flip (sink->window, sink->flip);
1233 }
1234
1235 static void
1236 gst_wayland_sink_render_last_buffer (GstWaylandSink * sink)
1237 {
1238   FUNCTION_ENTER ();
1239   g_return_if_fail (sink != NULL);
1240
1241   g_mutex_lock (&sink->render_lock);
1242   gst_wl_window_set_video_info (sink->window, &sink->video_info);
1243   sink->video_info_changed = FALSE;
1244   if (sink->last_buffer)
1245     render_last_buffer (sink);
1246   g_mutex_unlock (&sink->render_lock);
1247 }
1248 #endif
1249 static void
1250 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1251     gint x, gint y, gint w, gint h)
1252 {
1253   FUNCTION_ENTER ();
1254
1255   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1256
1257   g_return_if_fail (sink != NULL);
1258
1259   g_mutex_lock (&sink->render_lock);
1260   if (!sink->window) {
1261     g_mutex_unlock (&sink->render_lock);
1262     GST_WARNING_OBJECT (sink,
1263         "set_render_rectangle called without window, ignoring");
1264     return;
1265   }
1266
1267   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1268       x, y, w, h);
1269   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1270
1271   g_mutex_unlock (&sink->render_lock);
1272 }
1273
1274 static void
1275 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1276 {
1277   FUNCTION_ENTER ();
1278
1279   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1280
1281   g_return_if_fail (sink != NULL);
1282
1283   GST_DEBUG_OBJECT (sink, "expose");
1284
1285   g_mutex_lock (&sink->render_lock);
1286   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1287     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1288     render_last_buffer (sink);
1289   }
1290   g_mutex_unlock (&sink->render_lock);
1291 }
1292
1293 static void
1294 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1295 {
1296   FUNCTION_ENTER ();
1297
1298   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1299   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1300 }
1301
1302 static void
1303 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1304 {
1305   FUNCTION_ENTER ();
1306
1307   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1308   g_return_if_fail (sink != NULL);
1309
1310   g_mutex_lock (&sink->render_lock);
1311   if (!sink->window || !sink->window->subsurface) {
1312     g_mutex_unlock (&sink->render_lock);
1313     GST_INFO_OBJECT (sink,
1314         "begin_geometry_change called without window, ignoring");
1315     return;
1316   }
1317
1318   wl_subsurface_set_sync (sink->window->subsurface);
1319   g_mutex_unlock (&sink->render_lock);
1320 }
1321
1322 static void
1323 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1324 {
1325   FUNCTION_ENTER ();
1326
1327   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1328   g_return_if_fail (sink != NULL);
1329
1330   g_mutex_lock (&sink->render_lock);
1331   if (!sink->window || !sink->window->subsurface) {
1332     g_mutex_unlock (&sink->render_lock);
1333     GST_INFO_OBJECT (sink,
1334         "end_geometry_change called without window, ignoring");
1335     return;
1336   }
1337
1338   wl_subsurface_set_desync (sink->window->subsurface);
1339   g_mutex_unlock (&sink->render_lock);
1340 }
1341
1342 static gboolean
1343 plugin_init (GstPlugin * plugin)
1344 {
1345   FUNCTION_ENTER ();
1346
1347   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1348       " wayland video sink");
1349
1350   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1351       GST_TYPE_WAYLAND_SINK);
1352 }
1353
1354 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1355     GST_VERSION_MINOR,
1356     waylandsink,
1357     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1358     GST_PACKAGE_ORIGIN)