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