Waylandsink : Set zero to bufferpool config size because we need to create wl_buffer...
[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     uint32_t 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 #ifdef GST_WLSINK_ENHANCEMENT
661       /* TBM doesn't support SN12. So we add SN12 manually as supported format.
662        * SN12 is exactly same with NV12.
663        */
664       if (fmt == TBM_FORMAT_NV12) {
665         g_value_set_string (&value,
666             gst_video_format_to_string (GST_VIDEO_FORMAT_SN12));
667         gst_value_list_append_value (&list, &value);
668       }
669 #endif
670     }
671
672     caps = gst_caps_make_writable (caps);
673     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
674
675     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
676   }
677
678   g_mutex_unlock (&sink->display_lock);
679
680   if (filter) {
681     GstCaps *intersection;
682
683     intersection =
684         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
685     gst_caps_unref (caps);
686     caps = intersection;
687   }
688
689   return caps;
690 }
691
692 static gboolean
693 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
694 {
695   FUNCTION_ENTER ();
696
697   GstWaylandSink *sink;
698   GstBufferPool *newpool;
699   GstVideoInfo info;
700 #ifdef GST_WLSINK_ENHANCEMENT
701   uint32_t format;
702 #else
703   enum wl_shm_format format;
704 #endif
705   GArray *formats;
706   gint i;
707   GstStructure *structure;
708   static GstAllocationParams params = { 0, 0, 0, 15, };
709   sink = GST_WAYLAND_SINK (bsink);
710
711   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
712
713   /* extract info from caps */
714   if (!gst_video_info_from_caps (&info, caps))
715     goto invalid_format;
716 #ifdef GST_WLSINK_ENHANCEMENT
717   sink->caps = gst_caps_copy (caps);
718 #endif
719
720   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
721   if ((gint) format == -1)
722     goto invalid_format;
723
724   /* verify we support the requested format */
725   formats = sink->display->formats;
726   for (i = 0; i < formats->len; i++) {
727     if (g_array_index (formats, uint32_t, i) == format)
728       break;
729   }
730
731   if (i >= formats->len)
732     goto unsupported_format;
733
734 #ifdef GST_WLSINK_ENHANCEMENT
735   if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
736       GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
737     sink->display->is_native_format = TRUE;
738   } else {
739     sink->display->is_native_format = FALSE;
740
741     /* create a new pool for the new configuration */
742     newpool = gst_wayland_buffer_pool_new (sink->display);
743     if (!newpool)
744       goto pool_failed;
745
746     structure = gst_buffer_pool_get_config (newpool);
747     gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
748     gst_buffer_pool_config_set_allocator (structure, NULL, &params);
749     if (!gst_buffer_pool_set_config (newpool, structure))
750       goto config_failed;
751
752     gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
753     gst_object_unref (newpool);
754
755   }
756   /* store the video info */
757   sink->video_info = info;
758   sink->video_info_changed = TRUE;
759 #else
760   /* create a new pool for the new configuration */
761   newpool = gst_wayland_buffer_pool_new (sink->display);
762   if (!newpool)
763     goto pool_failed;
764
765   structure = gst_buffer_pool_get_config (newpool);
766   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
767   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
768   if (!gst_buffer_pool_set_config (newpool, structure))
769     goto config_failed;
770
771   /* store the video info */
772   sink->video_info = info;
773   sink->video_info_changed = TRUE;
774
775   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
776   gst_object_unref (newpool);
777 #endif
778   return TRUE;
779
780 invalid_format:
781   {
782     GST_DEBUG_OBJECT (sink,
783         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
784     return FALSE;
785   }
786 unsupported_format:
787   {
788     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
789         gst_wayland_format_to_string (format));
790     return FALSE;
791   }
792 pool_failed:
793   {
794     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
795     return FALSE;
796   }
797 config_failed:
798   {
799     GST_DEBUG_OBJECT (bsink, "failed setting config");
800     gst_object_unref (newpool);
801     return FALSE;
802   }
803 }
804
805 static gboolean
806 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
807 {
808   FUNCTION_ENTER ();
809
810   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
811   GstBufferPool *pool = NULL;
812   GstStructure *config;
813   GstCaps *caps;
814   guint size;
815   gboolean need_pool;
816
817   if (sink->display->is_native_format == TRUE)
818     return TRUE;
819
820   gst_query_parse_allocation (query, &caps, &need_pool);
821
822   if (caps == NULL)
823     goto no_caps;
824
825   if (sink->pool)
826     pool = gst_object_ref (sink->pool);
827
828   if (pool != NULL) {
829     GstCaps *pcaps;
830
831     /* we had a pool, check caps */
832     config = gst_buffer_pool_get_config (pool);
833     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
834
835     if (!gst_caps_is_equal (caps, pcaps)) {
836       /* different caps, we can't use this pool */
837       gst_object_unref (pool);
838       pool = NULL;
839     }
840     gst_structure_free (config);
841   }
842
843   if (pool == NULL && need_pool) {
844     GstVideoInfo info;
845
846     if (!gst_video_info_from_caps (&info, caps))
847       goto invalid_caps;
848
849     GST_DEBUG_OBJECT (sink, "create new pool");
850     pool = gst_wayland_buffer_pool_new (sink->display);
851
852     /* the normal size of a frame */
853     size = info.size;
854
855     config = gst_buffer_pool_get_config (pool);
856     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
857     if (!gst_buffer_pool_set_config (pool, config))
858       goto config_failed;
859   }
860   if (pool) {
861     gst_query_add_allocation_pool (query, pool, size, 2, 0);
862     gst_object_unref (pool);
863   }
864
865   return TRUE;
866
867   /* ERRORS */
868 no_caps:
869   {
870     GST_DEBUG_OBJECT (bsink, "no caps specified");
871     return FALSE;
872   }
873 invalid_caps:
874   {
875     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
876     return FALSE;
877   }
878 config_failed:
879   {
880     GST_DEBUG_OBJECT (bsink, "failed setting config");
881     gst_object_unref (pool);
882     return FALSE;
883   }
884 }
885
886 static GstFlowReturn
887 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
888 {
889   FUNCTION_ENTER ();
890
891   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
892   return gst_wayland_sink_render (bsink, buffer);
893 }
894
895 static void
896 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
897 {
898   FUNCTION_ENTER ();
899
900   GstWaylandSink *sink = data;
901
902   GST_LOG ("frame_redraw_cb");
903
904   g_atomic_int_set (&sink->redraw_pending, FALSE);
905   wl_callback_destroy (callback);
906 }
907
908 static const struct wl_callback_listener frame_callback_listener = {
909   frame_redraw_callback
910 };
911
912 /* must be called with the render lock */
913 static void
914 render_last_buffer (GstWaylandSink * sink)
915 {
916   FUNCTION_ENTER ();
917
918   GstWlMeta *meta;
919   struct wl_surface *surface;
920   struct wl_callback *callback;
921
922   meta = gst_buffer_get_wl_meta (sink->last_buffer);
923   surface = gst_wl_window_get_wl_surface (sink->window);
924
925   g_atomic_int_set (&sink->redraw_pending, TRUE);
926   callback = wl_surface_frame (surface);
927   wl_callback_add_listener (callback, &frame_callback_listener, sink);
928
929   /* Here we essentially add a reference to the buffer. This represents
930    * the fact that the compositor is using the buffer and it should
931    * not return back to the pool and be reused until the compositor
932    * releases it. The release is handled internally in the pool */
933   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
934
935   GST_DEBUG ("wl_surface_attach wl_buffer %p", meta->wbuffer);
936
937   wl_surface_attach (surface, meta->wbuffer, 0, 0);
938   wl_surface_damage (surface, 0, 0, sink->window->surface_width,
939       sink->window->surface_height);
940
941   wl_surface_commit (surface);
942   wl_display_flush (sink->display->display);
943 }
944
945 static GstFlowReturn
946 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
947 {
948   FUNCTION_ENTER ();
949
950   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
951   GstBuffer *to_render;
952   GstWlMeta *meta;
953   GstFlowReturn ret = GST_FLOW_OK;
954
955 #ifdef GST_WLSINK_ENHANCEMENT
956   GstBufferPool *newpool;
957   GstStructure *structure;
958   static GstAllocationParams params = { 0, 0, 0, 15, };
959 #endif
960
961   g_mutex_lock (&sink->render_lock);
962
963   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
964
965   if (G_UNLIKELY (!sink->window)) {
966     /* ask for window handle. Unlock render_lock while doing that because
967      * set_window_handle & friends will lock it in this context */
968     g_mutex_unlock (&sink->render_lock);
969     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
970     g_mutex_lock (&sink->render_lock);
971
972     if (sink->window) {
973       /* inform the window about our caps */
974       gst_wl_window_set_video_info (sink->window, &sink->video_info);
975     } else {
976       /* if we were not provided a window, create one ourselves */
977       sink->window =
978           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
979     }
980 #ifdef GST_WLSINK_ENHANCEMENT
981     gst_wayland_sink_update_window_geometry (sink);
982     sink->video_info_changed = TRUE;
983 #else
984     sink->video_info_changed = FALSE;
985 #endif
986   }
987
988   /* drop buffers until we get a frame callback */
989   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
990     goto done;
991
992   if (G_UNLIKELY (sink->video_info_changed)) {
993     gst_wl_window_set_video_info (sink->window, &sink->video_info);
994     sink->video_info_changed = FALSE;
995   }
996   GST_INFO ("window->render_rectangle(%d,%d %d x %d)",
997       sink->window->render_rectangle.x,
998       sink->window->render_rectangle.y,
999       sink->window->render_rectangle.w, sink->window->render_rectangle.h);
1000   GST_INFO ("window->surface_width(%d),window->surface_height(%d)",
1001       sink->window->surface_width, sink->window->surface_height);
1002
1003   /* now that we have for sure set the video info on the window, it must have
1004    * a valid size, otherwise this means that the application has called
1005    * set_window_handle() without calling set_render_rectangle(), which is
1006    * absolutely necessary for us.
1007    */
1008   if (G_UNLIKELY (sink->window->surface_width == 0 ||
1009           sink->window->surface_height == 0))
1010     goto no_window_size;
1011
1012   meta = gst_buffer_get_wl_meta (buffer);
1013
1014   if (meta && meta->pool->display == sink->display) {
1015     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
1016     to_render = buffer;
1017   } else {
1018     GstMapInfo src;
1019     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
1020
1021 #ifdef GST_WLSINK_ENHANCEMENT
1022     if (sink->display->is_native_format == TRUE) {
1023       /*in case of SN12 or ST12 video  format */
1024       GstMemory *mem;
1025       GstMapInfo mem_info = GST_MAP_INFO_INIT;
1026       MMVideoBuffer *mm_video_buf = NULL;
1027
1028       mem = gst_buffer_peek_memory (buffer, 1);
1029       gst_memory_map (mem, &mem_info, GST_MAP_READ);
1030       mm_video_buf = (MMVideoBuffer *) mem_info.data;
1031       gst_memory_unmap (mem, &mem_info);
1032
1033       if (mm_video_buf == NULL) {
1034         GST_WARNING_OBJECT (sink, "mm_video_buf is NULL. Skip rendering");
1035         return ret;
1036       }
1037       /* assign mm_video_buf info */
1038       if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
1039         GST_DEBUG_OBJECT (sink, "TBM bo %p %p %p", mm_video_buf->handle.bo[0],
1040             mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
1041
1042         sink->display->native_video_size = 0;
1043
1044         for (int i = 0; i < NV_BUF_PLANE_NUM; i++) {
1045           if (mm_video_buf->handle.bo[i] != NULL) {
1046             sink->display->bo[i] = mm_video_buf->handle.bo[i];
1047           } else {
1048             sink->display->bo[i] = 0;
1049           }
1050           sink->display->plane_size[i] = mm_video_buf->size[i];
1051           sink->display->stride_width[i] = mm_video_buf->stride_width[i];
1052           sink->display->stride_height[i] = mm_video_buf->stride_height[i];
1053           sink->display->native_video_size += sink->display->plane_size[i];
1054         }
1055       } else {
1056         GST_ERROR_OBJECT (sink, "Buffer type is not TBM");
1057         return ret;
1058       }
1059
1060       if (!sink->pool) {
1061
1062         /* create a new pool for the new configuration */
1063         newpool = gst_wayland_buffer_pool_new (sink->display);
1064         if (!newpool) {
1065           GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1066           return FALSE;
1067         }
1068         structure = gst_buffer_pool_get_config (newpool);
1069         /*When the buffer is released, Core compare size with buffer size,
1070            wl_buffer is not created if the size is same. It is a very critical problem
1071            So we set 0 to size */
1072         gst_buffer_pool_config_set_params (structure, sink->caps, 0, 2, 0);
1073         gst_buffer_pool_config_set_allocator (structure, NULL, &params);
1074         if (!gst_buffer_pool_set_config (newpool, structure)) {
1075           GST_DEBUG_OBJECT (bsink, "failed setting config");
1076           gst_object_unref (newpool);
1077           return FALSE;
1078         }
1079
1080         gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1081         gst_object_unref (newpool);
1082
1083       }
1084
1085       if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1086         goto activate_failed;
1087
1088       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1089       if (ret != GST_FLOW_OK)
1090         goto no_buffer;
1091
1092
1093       /*add displaying buffer */
1094       GstWlMeta *meta;
1095       meta = gst_buffer_get_wl_meta (to_render);
1096       gst_wayland_buffer_pool_add_displaying_buffer (sink->pool, meta, buffer);
1097
1098     } else {
1099       /*in case of normal video format and pool is not our pool */
1100
1101       if (!sink->pool)
1102         goto no_pool;
1103
1104       if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1105         goto activate_failed;
1106
1107       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1108       if (ret != GST_FLOW_OK)
1109         goto no_buffer;
1110
1111       gst_buffer_map (buffer, &src, GST_MAP_READ);
1112       gst_buffer_fill (to_render, 0, src.data, src.size);
1113       gst_buffer_unmap (buffer, &src);
1114     }
1115 #else
1116     if (!sink->pool)
1117       goto no_pool;
1118
1119     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1120       goto activate_failed;
1121
1122     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1123     if (ret != GST_FLOW_OK)
1124       goto no_buffer;
1125
1126     gst_buffer_map (buffer, &src, GST_MAP_READ);
1127     gst_buffer_fill (to_render, 0, src.data, src.size);
1128     gst_buffer_unmap (buffer, &src);
1129 #endif
1130   }
1131
1132   gst_buffer_replace (&sink->last_buffer, to_render);
1133   render_last_buffer (sink);
1134
1135   if (buffer != to_render) {
1136     GST_LOG_OBJECT (sink, "Decrease ref count of buffer");
1137     gst_buffer_unref (to_render);
1138   }
1139   goto done;
1140
1141 no_window_size:
1142   {
1143     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1144         ("Window has no size set"),
1145         ("Make sure you set the size after calling set_window_handle"));
1146     ret = GST_FLOW_ERROR;
1147     goto done;
1148   }
1149 no_buffer:
1150   {
1151     GST_WARNING_OBJECT (sink, "could not create image");
1152     goto done;
1153   }
1154 no_pool:
1155   {
1156     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1157         ("Internal error: can't allocate images"),
1158         ("We don't have a bufferpool negotiated"));
1159     ret = GST_FLOW_ERROR;
1160     goto done;
1161   }
1162 activate_failed:
1163   {
1164     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1165     ret = GST_FLOW_ERROR;
1166     goto done;
1167   }
1168 done:
1169   {
1170     g_mutex_unlock (&sink->render_lock);
1171     return ret;
1172   }
1173 }
1174
1175 static void
1176 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1177 {
1178   FUNCTION_ENTER ();
1179
1180   iface->set_window_handle = gst_wayland_sink_set_window_handle;
1181   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1182   iface->expose = gst_wayland_sink_expose;
1183 }
1184
1185 static void
1186 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1187 {
1188   FUNCTION_ENTER ();
1189
1190   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1191   struct wl_surface *surface = (struct wl_surface *) handle;
1192
1193   g_return_if_fail (sink != NULL);
1194
1195   if (sink->window != NULL) {
1196     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1197     return;
1198   }
1199
1200   g_mutex_lock (&sink->render_lock);
1201
1202   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1203       (void *) handle);
1204
1205   g_clear_object (&sink->window);
1206
1207   if (handle) {
1208     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1209       /* we cannot use our own display with an external window handle */
1210       if (G_UNLIKELY (sink->display->own_display)) {
1211         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
1212             ("Application did not provide a wayland display handle"),
1213             ("waylandsink cannot use an externally-supplied surface without "
1214                 "an externally-supplied display handle. Consider providing a "
1215                 "display handle from your application with GstContext"));
1216       } else {
1217         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1218         GST_DEBUG ("sink->window %p", sink->window);
1219       }
1220     } else {
1221       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1222           "ignoring window handle");
1223     }
1224   }
1225 #ifdef GST_WLSINK_ENHANCEMENT
1226   gst_wayland_sink_update_window_geometry (sink);
1227 #endif
1228
1229   g_mutex_unlock (&sink->render_lock);
1230 }
1231
1232 #ifdef GST_WLSINK_ENHANCEMENT
1233 static void
1234 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1235 {
1236   FUNCTION_ENTER ();
1237   g_return_if_fail (sink != NULL);
1238   g_return_if_fail (sink->window != NULL);
1239
1240   gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1241   gst_wl_window_set_disp_geo_method (sink->window,
1242       sink->display_geometry_method);
1243   gst_wl_window_set_orientation (sink->window, sink->orientation);
1244   gst_wl_window_set_flip (sink->window, sink->flip);
1245 }
1246
1247 static void
1248 gst_wayland_sink_render_last_buffer (GstWaylandSink * sink)
1249 {
1250   FUNCTION_ENTER ();
1251   g_return_if_fail (sink != NULL);
1252
1253   g_mutex_lock (&sink->render_lock);
1254   gst_wl_window_set_video_info (sink->window, &sink->video_info);
1255   sink->video_info_changed = FALSE;
1256   if (sink->last_buffer)
1257     render_last_buffer (sink);
1258   g_mutex_unlock (&sink->render_lock);
1259 }
1260 #endif
1261 static void
1262 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1263     gint x, gint y, gint w, gint h)
1264 {
1265   FUNCTION_ENTER ();
1266
1267   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1268
1269   g_return_if_fail (sink != NULL);
1270
1271   g_mutex_lock (&sink->render_lock);
1272   if (!sink->window) {
1273     g_mutex_unlock (&sink->render_lock);
1274     GST_WARNING_OBJECT (sink,
1275         "set_render_rectangle called without window, ignoring");
1276     return;
1277   }
1278
1279   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1280       x, y, w, h);
1281   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1282
1283   g_mutex_unlock (&sink->render_lock);
1284 }
1285
1286 static void
1287 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1288 {
1289   FUNCTION_ENTER ();
1290
1291   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1292
1293   g_return_if_fail (sink != NULL);
1294
1295   GST_DEBUG_OBJECT (sink, "expose");
1296
1297   g_mutex_lock (&sink->render_lock);
1298   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1299     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1300     render_last_buffer (sink);
1301   }
1302   g_mutex_unlock (&sink->render_lock);
1303 }
1304
1305 static void
1306 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1307 {
1308   FUNCTION_ENTER ();
1309
1310   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1311   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1312 }
1313
1314 static void
1315 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1316 {
1317   FUNCTION_ENTER ();
1318
1319   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1320   g_return_if_fail (sink != NULL);
1321
1322   g_mutex_lock (&sink->render_lock);
1323   if (!sink->window || !sink->window->subsurface) {
1324     g_mutex_unlock (&sink->render_lock);
1325     GST_INFO_OBJECT (sink,
1326         "begin_geometry_change called without window, ignoring");
1327     return;
1328   }
1329
1330   wl_subsurface_set_sync (sink->window->subsurface);
1331   g_mutex_unlock (&sink->render_lock);
1332 }
1333
1334 static void
1335 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1336 {
1337   FUNCTION_ENTER ();
1338
1339   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1340   g_return_if_fail (sink != NULL);
1341
1342   g_mutex_lock (&sink->render_lock);
1343   if (!sink->window || !sink->window->subsurface) {
1344     g_mutex_unlock (&sink->render_lock);
1345     GST_INFO_OBJECT (sink,
1346         "end_geometry_change called without window, ignoring");
1347     return;
1348   }
1349
1350   wl_subsurface_set_desync (sink->window->subsurface);
1351   g_mutex_unlock (&sink->render_lock);
1352 }
1353
1354 static gboolean
1355 plugin_init (GstPlugin * plugin)
1356 {
1357   FUNCTION_ENTER ();
1358
1359   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1360       " wayland video sink");
1361
1362   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1363       GST_TYPE_WAYLAND_SINK);
1364 }
1365
1366 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1367     GST_VERSION_MINOR,
1368     waylandsink,
1369     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1370     GST_PACKAGE_ORIGIN)