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