waylandsink : 1. Apply flush buffer
[platform/upstream/gstreamer.git] / ext / wayland / gstwaylandsink.c
1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2011 Intel Corporation
4  * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2014 Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * SECTION:element-waylandsink
26  *
27  *  The waylandsink is creating its own window and render the decoded video frames to that.
28  *  Setup the Wayland environment as described in
29  *  <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
30  *  The current implementaion is based on weston compositor.
31  *
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch -v videotestsrc ! waylandsink
36  * ]| test the video rendering in wayland
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "gstwaylandsink.h"
45 #ifdef GST_WLSINK_ENHANCEMENT
46 #include <mm_types.h>
47 #include "tizen-wlvideoformat.h"
48 #endif
49 #include "wlvideoformat.h"
50 #include "wlbuffer.h"
51 #include "wlshmallocator.h"
52
53 #include <gst/wayland/wayland.h>
54 #include <gst/video/videooverlay.h>
55
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60
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   GObjectClass *gobject_class;
221   GstElementClass *gstelement_class;
222   GstBaseSinkClass *gstbasesink_class;
223   FUNCTION;
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 gst_wayland_sink_update_last_buffer_geometry (GstWaylandSink * sink)
328 {
329   GstWlBuffer *wlbuffer;
330   FUNCTION;
331   g_return_if_fail (sink != NULL);
332
333   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
334   wlbuffer->used_by_compositor = FALSE;
335
336   /*need to render last buffer */
337   /* reuse current GstWlBuffer */
338   render_last_buffer (sink);
339   /* ref count is incresed in gst_wl_buffer_attach() of render_last_buffer(),
340      to call gst_wl_buffer_finalize(), we need to decrease buffer ref count */
341   gst_buffer_unref (wlbuffer->gstbuffer);
342 }
343 #ifdef USE_WL_FLUSH_BUFFER
344 static int
345 gst_wayland_sink_make_flush_buffer (GstWlDisplay * display, MMVideoBuffer * mm_video_buf)
346 {
347   GstWlFlushBuffer *flush_buffer = NULL;
348   tbm_bo bo = NULL;
349   int bo_size = 0;
350   int i;
351   FUNCTION;
352
353   g_return_val_if_fail (display != NULL, FALSE);
354   g_return_val_if_fail (mm_video_buf != NULL, FALSE);
355
356   flush_buffer = (GstWlFlushBuffer *)malloc(sizeof(GstWlFlushBuffer));
357   if (!flush_buffer){
358     GST_ERROR ("GstWlFlushBuffer alloc faile");
359     return FALSE;
360   }
361   memset (flush_buffer, 0x0, sizeof(GstWlFlushBuffer));
362
363   display->flush_tbm_bufmgr = wayland_tbm_client_get_bufmgr (display->tbm_client);
364   g_return_if_fail (display->flush_tbm_bufmgr != NULL);
365
366   for (i=0; i<NV_BUF_PLANE_NUM; i++){
367     if (mm_video_buf->handle.bo[i] != NULL){
368       tbm_bo_handle src;
369       tbm_bo_handle dst;
370
371       /* get bo size */
372       bo_size = tbm_bo_size (mm_video_buf->handle.bo[i]);
373       GST_LOG ("tbm bo size: %d", bo_size);
374       /* alloc bo */
375       bo = tbm_bo_alloc (display->flush_tbm_bufmgr, bo_size, TBM_DEVICE_CPU);
376       if (!bo) {
377         GST_ERROR ("alloc tbm bo(size:%d) failed: %s", bo_size, strerror(errno));
378         return FALSE;
379       }
380       GST_INFO ("flush buffer tbm_bo =(%p)", bo);
381       flush_buffer->bo[i] = bo;
382       /* get virtual address */
383       src.ptr = dst.ptr = NULL;
384       /* bo map, we can use tbm_bo_map too. */
385       src = tbm_bo_get_handle (mm_video_buf->handle.bo[i], TBM_DEVICE_CPU);
386       dst = tbm_bo_get_handle (bo, TBM_DEVICE_CPU);
387       if (!src.ptr || !dst.ptr) {
388         GST_ERROR ("get tbm bo handle failed src(%p) dst(%p): %s", src.ptr, dst.ptr, strerror (errno));
389         tbm_bo_unref (mm_video_buf->handle.bo[i]);
390         tbm_bo_unref (bo);
391         return FALSE;
392       }
393       /* copy */
394       memcpy (dst.ptr, src.ptr, bo_size);
395       /* bo unmap */
396       tbm_bo_unmap (mm_video_buf->handle.bo[i]);
397       tbm_bo_unmap (bo);
398     }
399   }
400   display->flush_buffer = flush_buffer;
401   return TRUE;
402 }
403
404 static int
405 gst_wayland_sink_copy_mm_video_buf_info_to_flush (GstWlDisplay * display, MMVideoBuffer * mm_video_buf)
406 {
407   int ret = FALSE;
408   g_return_val_if_fail (display != NULL, FALSE);
409   g_return_val_if_fail (mm_video_buf != NULL, FALSE);
410   FUNCTION;
411
412   ret = gst_wayland_sink_make_flush_buffer(display, mm_video_buf);
413   if (ret){
414     int i;
415     for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
416       if (display->flush_buffer->bo[i] != NULL) {
417         display->bo[i] = display->flush_buffer->bo[i];
418         GST_LOG("bo %p", display->bo[i]);
419       } else {
420         display->bo[i] = 0;
421       }
422       display->plane_size[i] = mm_video_buf->size[i];
423       display->stride_width[i] = mm_video_buf->stride_width[i];
424       display->stride_height[i] = mm_video_buf->stride_height[i];
425       display->native_video_size += display->plane_size[i];
426     }
427   }
428   return ret;
429 }
430 #endif
431
432 static void
433 gst_wayland_sink_add_mm_video_buf_info (GstWlDisplay * display, MMVideoBuffer * mm_video_buf)
434 {
435   int i;
436   g_return_if_fail (display != NULL);
437   g_return_if_fail (mm_video_buf != NULL);
438   FUNCTION;
439
440   for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
441     if (mm_video_buf->handle.bo[i] != NULL) {
442       display->bo[i] = mm_video_buf->handle.bo[i];
443     } else {
444       display->bo[i] = 0;
445     }
446     display->plane_size[i] = mm_video_buf->size[i];
447     display->stride_width[i] = mm_video_buf->stride_width[i];
448     display->stride_height[i] = mm_video_buf->stride_height[i];
449     display->native_video_size += display->plane_size[i];
450   }
451 }
452
453 static int
454 gst_wayland_sink_get_mm_video_buf_info(GstWlDisplay * display, GstBuffer * buffer)
455 {
456   GstMemory *mem;
457   GstMapInfo mem_info = GST_MAP_INFO_INIT;
458   MMVideoBuffer *mm_video_buf = NULL;
459
460   g_return_val_if_fail (display != NULL, FALSE);
461   g_return_val_if_fail (buffer != NULL, FALSE);
462
463   FUNCTION;
464
465   mem = gst_buffer_peek_memory (buffer, 1);
466   gst_memory_map (mem, &mem_info, GST_MAP_READ);
467   mm_video_buf = (MMVideoBuffer *) mem_info.data;
468   gst_memory_unmap (mem, &mem_info);
469
470   if (mm_video_buf == NULL) {
471     GST_WARNING ("mm_video_buf is NULL. Skip rendering");
472     return FALSE;
473   }
474   /* assign mm_video_buf info */
475   if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
476     GST_DEBUG ("TBM bo %p %p %p", mm_video_buf->handle.bo[0],
477         mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
478     display->native_video_size = 0;
479     display->flush_request = mm_video_buf->flush_request;
480     GST_DEBUG ("flush_request value is %d",display->flush_request);
481 #ifdef USE_WL_FLUSH_BUFFER
482     if (display->flush_request) {
483       if(!gst_wayland_sink_copy_mm_video_buf_info_to_flush(display, mm_video_buf)){
484         GST_ERROR("cat not copy mm_video_buf info to flush");
485         return FALSE;
486         }
487     } else
488 #endif
489       /* normal routine */
490       gst_wayland_sink_add_mm_video_buf_info(display, mm_video_buf);
491   } else {
492     GST_ERROR ("Buffer type is not TBM");
493     return FALSE;
494   }
495   return TRUE;
496 }
497
498 #endif
499 static void
500 gst_wayland_sink_get_property (GObject * object,
501     guint prop_id, GValue * value, GParamSpec * pspec)
502 {
503   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
504   FUNCTION;
505
506   switch (prop_id) {
507     case PROP_DISPLAY:
508       GST_OBJECT_LOCK (sink);
509       g_value_set_string (value, sink->display_name);
510       GST_OBJECT_UNLOCK (sink);
511       break;
512 #ifdef GST_WLSINK_ENHANCEMENT
513     case PROP_USE_TBM:
514       g_value_set_boolean (value, sink->USE_TBM);
515       break;
516     case PROP_ROTATE_ANGLE:
517       g_value_set_enum (value, sink->rotate_angle);
518       break;
519     case PROP_DISPLAY_GEOMETRY_METHOD:
520       g_value_set_enum (value, sink->display_geometry_method);
521       break;
522     case PROP_ORIENTATION:
523       g_value_set_enum (value, sink->orientation);
524       break;
525     case PROP_FLIP:
526       g_value_set_enum (value, sink->flip);
527       break;
528 #endif
529     default:
530       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531       break;
532   }
533 }
534
535 static void
536 gst_wayland_sink_set_property (GObject * object,
537     guint prop_id, const GValue * value, GParamSpec * pspec)
538 {
539   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
540   FUNCTION;
541   g_mutex_lock (&sink->render_lock);
542
543   switch (prop_id) {
544     case PROP_DISPLAY:
545       GST_OBJECT_LOCK (sink);
546       sink->display_name = g_value_dup_string (value);
547       GST_OBJECT_UNLOCK (sink);
548       break;
549 #ifdef GST_WLSINK_ENHANCEMENT
550     case PROP_USE_TBM:
551       sink->USE_TBM = g_value_get_boolean (value);
552       GST_LOG ("1:USE TBM 0: USE SHM set(%d)", sink->USE_TBM);
553       break;
554
555     case PROP_ROTATE_ANGLE:
556       if (sink->rotate_angle == g_value_get_enum (value))
557         break;
558       sink->rotate_angle = g_value_get_enum (value);
559       GST_WARNING_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
560       sink->video_info_changed = TRUE;
561       if (sink->window) {
562         gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
563       }
564       break;
565
566     case PROP_DISPLAY_GEOMETRY_METHOD:
567       if (sink->display_geometry_method == g_value_get_enum (value))
568         break;
569       sink->display_geometry_method = g_value_get_enum (value);
570       GST_WARNING_OBJECT (sink, "Display geometry method is set (%d)",
571           sink->display_geometry_method);
572       sink->video_info_changed = TRUE;
573       if (sink->window) {
574         gst_wl_window_set_disp_geo_method (sink->window,
575             sink->display_geometry_method);
576       }
577       break;
578
579     case PROP_ORIENTATION:
580       if (sink->orientation == g_value_get_enum (value))
581         break;
582       sink->orientation = g_value_get_enum (value);
583       GST_WARNING_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
584       sink->video_info_changed = TRUE;
585       if (sink->window) {
586         gst_wl_window_set_orientation (sink->window, sink->orientation);
587       }
588       break;
589
590     case PROP_FLIP:
591       if (sink->flip == g_value_get_enum (value))
592         break;
593       sink->flip = g_value_get_enum (value);
594       GST_WARNING_OBJECT (sink, "flip is set (%d)", sink->flip);
595       sink->video_info_changed = TRUE;
596       if (sink->window) {
597         gst_wl_window_set_flip (sink->window, sink->flip);
598       }
599       break;
600 #endif
601     default:
602       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
603       break;
604   }
605   if (sink->video_info_changed && sink->window
606       && GST_STATE (sink) == GST_STATE_PAUSED) {
607     gst_wayland_sink_update_last_buffer_geometry (sink);
608   }
609   g_mutex_unlock (&sink->render_lock);
610
611 }
612
613 static void
614 gst_wayland_sink_finalize (GObject * object)
615 {
616   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
617   FUNCTION;
618   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
619
620   if (sink->last_buffer)
621     gst_buffer_unref (sink->last_buffer);
622   if (sink->display)
623     g_object_unref (sink->display);
624   if (sink->window)
625     g_object_unref (sink->window);
626   if (sink->pool)
627     gst_object_unref (sink->pool);
628
629   if (sink->display_name)
630     g_free (sink->display_name);
631
632   g_mutex_clear (&sink->display_lock);
633   g_mutex_clear (&sink->render_lock);
634
635   G_OBJECT_CLASS (parent_class)->finalize (object);
636 }
637
638 /* must be called with the display_lock */
639 static void
640 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
641     GstContext * context)
642 {
643   struct wl_display *display;
644   GError *error = NULL;
645   FUNCTION;
646
647   display = gst_wayland_display_handle_context_get_handle (context);
648   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
649
650   if (error) {
651     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
652         ("Could not set display handle"),
653         ("Failed to use the external wayland display: '%s'", error->message));
654     g_error_free (error);
655   }
656 #ifdef GST_WLSINK_ENHANCEMENT
657   sink->display->USE_TBM = sink->USE_TBM;
658 #endif
659 }
660
661 static gboolean
662 gst_wayland_sink_find_display (GstWaylandSink * sink)
663 {
664   GstQuery *query;
665   GstMessage *msg;
666   GstContext *context = NULL;
667   GError *error = NULL;
668   gboolean ret = TRUE;
669   FUNCTION;
670
671   g_mutex_lock (&sink->display_lock);
672
673   if (!sink->display) {
674     /* first query upstream for the needed display handle */
675     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
676     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
677       gst_query_parse_context (query, &context);
678       gst_wayland_sink_set_display_from_context (sink, context);
679     }
680     gst_query_unref (query);
681
682     if (G_LIKELY (!sink->display)) {
683       /* now ask the application to set the display handle */
684       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
685           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
686
687       g_mutex_unlock (&sink->display_lock);
688       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
689       /* at this point we expect gst_wayland_sink_set_context
690        * to get called and fill sink->display */
691       g_mutex_lock (&sink->display_lock);
692
693       if (!sink->display) {
694         /* if the application didn't set a display, let's create it ourselves */
695         GST_OBJECT_LOCK (sink);
696         sink->display = gst_wl_display_new (sink->display_name, &error);
697         GST_OBJECT_UNLOCK (sink);
698
699         if (error) {
700           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
701               ("Could not initialise Wayland output"),
702               ("Failed to create GstWlDisplay: '%s'", error->message));
703           g_error_free (error);
704           ret = FALSE;
705         }
706 #ifdef GST_WLSINK_ENHANCEMENT
707         sink->display->USE_TBM = sink->USE_TBM;
708 #endif
709       }
710     }
711   }
712
713   g_mutex_unlock (&sink->display_lock);
714
715   return ret;
716 }
717
718 static GstStateChangeReturn
719 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
720 {
721   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
722   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
723   FUNCTION;
724
725   switch (transition) {
726     case GST_STATE_CHANGE_NULL_TO_READY:
727       if (!gst_wayland_sink_find_display (sink))
728         return GST_STATE_CHANGE_FAILURE;
729       break;
730     default:
731       break;
732   }
733
734   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
735   if (ret == GST_STATE_CHANGE_FAILURE)
736     return ret;
737
738   switch (transition) {
739     case GST_STATE_CHANGE_PAUSED_TO_READY:
740       gst_buffer_replace (&sink->last_buffer, NULL);
741       if (sink->window) {
742         if (gst_wl_window_is_toplevel (sink->window)) {
743           g_clear_object (&sink->window);
744         } else {
745           /* remove buffer from surface, show nothing */
746 #ifdef USE_WL_FLUSH_BUFFER
747           sink->display->flush_request = 0;
748 #endif
749           gst_wl_window_render (sink->window, NULL, NULL);
750         }
751       }
752       break;
753     case GST_STATE_CHANGE_READY_TO_NULL:
754       g_mutex_lock (&sink->display_lock);
755       /* If we had a toplevel window, we most likely have our own connection
756        * to the display too, and it is a good idea to disconnect and allow
757        * potentially the application to embed us with GstVideoOverlay
758        * (which requires to re-use the same display connection as the parent
759        * surface). If we didn't have a toplevel window, then the display
760        * connection that we have is definitely shared with the application
761        * and it's better to keep it around (together with the window handle)
762        * to avoid requesting them again from the application if/when we are
763        * restarted (GstVideoOverlay behaves like that in other sinks)
764        */
765       if (sink->display && !sink->window) {     /* -> the window was toplevel */
766         g_clear_object (&sink->display);
767       }
768       g_mutex_unlock (&sink->display_lock);
769       g_clear_object (&sink->pool);
770       break;
771     default:
772       break;
773   }
774
775   return ret;
776 }
777
778 static void
779 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
780 {
781   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
782   FUNCTION;
783
784   if (gst_context_has_context_type (context,
785           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
786     g_mutex_lock (&sink->display_lock);
787     if (G_LIKELY (!sink->display))
788       gst_wayland_sink_set_display_from_context (sink, context);
789     else {
790       GST_WARNING_OBJECT (element, "changing display handle is not supported");
791 #ifdef GST_WLSINK_ENHANCEMENT
792       g_mutex_unlock (&sink->display_lock);
793       return;
794 #endif
795     }
796     g_mutex_unlock (&sink->display_lock);
797   }
798
799   if (GST_ELEMENT_CLASS (parent_class)->set_context)
800     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
801 }
802
803 static GstCaps *
804 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
805 {
806   GstWaylandSink *sink;
807   GstCaps *caps;
808   FUNCTION;
809
810   sink = GST_WAYLAND_SINK (bsink);
811
812   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
813
814   g_mutex_lock (&sink->display_lock);
815
816   if (sink->display) {
817     GValue list = G_VALUE_INIT;
818     GValue value = G_VALUE_INIT;
819     GArray *formats;
820     gint i;
821 #ifdef GST_WLSINK_ENHANCEMENT
822     uint32_t tbm_fmt;
823 #endif
824     enum wl_shm_format fmt;
825
826     g_value_init (&list, GST_TYPE_LIST);
827     g_value_init (&value, G_TYPE_STRING);
828 #ifdef GST_WLSINK_ENHANCEMENT
829     if (sink->display->USE_TBM)
830       formats = sink->display->tbm_formats;
831     else                        /* SHM */
832 #endif
833       formats = sink->display->formats;
834
835     for (i = 0; i < formats->len; i++) {
836 #ifdef GST_WLSINK_ENHANCEMENT
837       if (sink->USE_TBM) {
838         tbm_fmt = g_array_index (formats, uint32_t, i);
839         g_value_set_string (&value, gst_wl_tbm_format_to_string (tbm_fmt));
840         gst_value_list_append_value (&list, &value);
841         /* TBM doesn't support SN12. So we add SN12 manually as supported format.
842          * SN12 is exactly same with NV12.
843          */
844         if (tbm_fmt == TBM_FORMAT_NV12) {
845           g_value_set_string (&value,
846               gst_video_format_to_string (GST_VIDEO_FORMAT_SN12));
847           gst_value_list_append_value (&list, &value);
848         }
849       } else {                  /* USE SHM */
850         fmt = g_array_index (formats, uint32_t, i);
851         g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
852         gst_value_list_append_value (&list, &value);
853       }
854 #else /* open source */
855       fmt = g_array_index (formats, uint32_t, i);
856       g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
857       gst_value_list_append_value (&list, &value);
858 #endif
859     }
860
861     caps = gst_caps_make_writable (caps);
862     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
863
864     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
865   }
866
867   g_mutex_unlock (&sink->display_lock);
868
869   if (filter) {
870     GstCaps *intersection;
871
872     intersection =
873         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
874     gst_caps_unref (caps);
875     caps = intersection;
876   }
877
878   return caps;
879 }
880
881 static gboolean
882 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
883 {
884   GstWaylandSink *sink;
885   GstBufferPool *newpool;
886   GstVideoInfo info;
887 #ifdef GST_WLSINK_ENHANCEMENT
888   uint32_t tbm_format;
889 #endif
890   enum wl_shm_format format;
891
892   GArray *formats;
893   gint i;
894   GstStructure *structure;
895   GstWlShmAllocator *self = NULL;
896
897   FUNCTION;
898
899   sink = GST_WAYLAND_SINK (bsink);
900
901   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
902
903   /* extract info from caps */
904   if (!gst_video_info_from_caps (&info, caps))
905     goto invalid_format;
906 #ifdef GST_WLSINK_ENHANCEMENT
907   sink->caps = gst_caps_copy (caps);
908   if (sink->USE_TBM) {
909     tbm_format =
910         gst_video_format_to_wl_tbm_format (GST_VIDEO_INFO_FORMAT (&info));
911     if ((gint) tbm_format == -1)
912       goto invalid_format;
913   } else {
914     format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
915     if ((gint) format == -1)
916       goto invalid_format;
917   }
918 #else /* open source */
919   format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
920
921   if ((gint) format == -1)
922     goto invalid_format;
923 #endif
924
925   /* verify we support the requested format */
926 #ifdef GST_WLSINK_ENHANCEMENT
927   if (sink->display->USE_TBM) {
928     GST_INFO ("USE TBM FORMAT");
929     formats = sink->display->tbm_formats;
930     for (i = 0; i < formats->len; i++) {
931       if (g_array_index (formats, uint32_t, i) == tbm_format)
932         break;
933     }
934   } else {                      /* USE SHM */
935     GST_INFO ("USE SHM FORMAT");
936     formats = sink->display->formats;
937     for (i = 0; i < formats->len; i++) {
938       if (g_array_index (formats, uint32_t, i) == format)
939         break;
940     }
941   }
942 #else /* open source */
943
944   formats = sink->display->formats;
945   for (i = 0; i < formats->len; i++) {
946     if (g_array_index (formats, uint32_t, i) == format)
947       break;
948   }
949 #endif
950   if (i >= formats->len)
951     goto unsupported_format;
952
953 #ifdef GST_WLSINK_ENHANCEMENT
954   if (sink->USE_TBM) {
955     if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
956         GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
957       sink->display->is_native_format = TRUE;
958
959       /* store the video info */
960       sink->video_info = info;
961       sink->video_info_changed = TRUE;
962     } else {
963       sink->display->is_native_format = FALSE;
964       self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
965       self->display = sink->display;
966       /* create a new pool for the new configuration */
967       newpool = gst_video_buffer_pool_new ();
968       if (!newpool)
969         goto pool_failed;
970
971       structure = gst_buffer_pool_get_config (newpool);
972       gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
973       gst_buffer_pool_config_set_allocator (structure,
974           gst_wl_shm_allocator_get (), NULL);
975       if (!gst_buffer_pool_set_config (newpool, structure))
976         goto config_failed;
977
978       /* store the video info */
979       sink->video_info = info;
980       sink->video_info_changed = TRUE;
981
982       gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
983       gst_object_unref (newpool);
984     }
985   } else {                      /* USE SHM */
986
987     self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
988     self->display = sink->display;
989
990     /* create a new pool for the new configuration */
991     newpool = gst_video_buffer_pool_new ();
992     if (!newpool)
993       goto pool_failed;
994
995     structure = gst_buffer_pool_get_config (newpool);
996     gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
997     gst_buffer_pool_config_set_allocator (structure,
998         gst_wl_shm_allocator_get (), NULL);
999     if (!gst_buffer_pool_set_config (newpool, structure))
1000       goto config_failed;
1001
1002     /* store the video info */
1003     sink->video_info = info;
1004     sink->video_info_changed = TRUE;
1005
1006     gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1007     gst_object_unref (newpool);
1008   }
1009 #else /*open source */
1010   /* create a new pool for the new configuration */
1011   newpool = gst_video_buffer_pool_new ();
1012   if (!newpool)
1013     goto pool_failed;
1014
1015   structure = gst_buffer_pool_get_config (newpool);
1016   gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1017   gst_buffer_pool_config_set_allocator (structure,
1018       gst_wl_shm_allocator_get (), NULL);
1019   if (!gst_buffer_pool_set_config (newpool, structure))
1020     goto config_failed;
1021
1022   /* store the video info */
1023   sink->video_info = info;
1024   sink->video_info_changed = TRUE;
1025
1026   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1027   gst_object_unref (newpool);
1028 #endif
1029
1030   return TRUE;
1031
1032 invalid_format:
1033   {
1034     GST_DEBUG_OBJECT (sink,
1035         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
1036     return FALSE;
1037   }
1038 unsupported_format:
1039   {
1040 #ifdef GST_WLSINK_ENHANCEMENT
1041     if (sink->USE_TBM)
1042       GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1043           gst_wl_tbm_format_to_string (tbm_format));
1044     else                        /*USE SHM */
1045       GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1046           gst_wl_shm_format_to_string (format));
1047 #else /*open source */
1048     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1049         gst_wl_shm_format_to_string (format));
1050 #endif
1051     return FALSE;
1052   }
1053 pool_failed:
1054   {
1055     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1056     return FALSE;
1057   }
1058 config_failed:
1059   {
1060     GST_DEBUG_OBJECT (bsink, "failed setting config");
1061     gst_object_unref (newpool);
1062     return FALSE;
1063   }
1064 }
1065
1066 static gboolean
1067 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1068 {
1069   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1070   GstStructure *config;
1071   guint size, min_bufs, max_bufs;
1072 #ifdef GST_WLSINK_ENHANCEMENT
1073   gboolean need_pool;
1074   GstCaps *caps;
1075   FUNCTION;
1076
1077   if (sink->USE_TBM) {
1078     if (sink->display->is_native_format == TRUE)
1079       return TRUE;
1080
1081     gst_query_parse_allocation (query, &caps, &need_pool);
1082
1083     if (caps == NULL) {
1084       GST_DEBUG_OBJECT (bsink, "no caps specified");
1085       return FALSE;
1086     }
1087   }
1088 #endif
1089   config = gst_buffer_pool_get_config (sink->pool);
1090   gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
1091
1092   /* we do have a pool for sure (created in set_caps),
1093    * so let's propose it anyway, but also propose the allocator on its own */
1094   gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs);
1095   gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL);
1096
1097   gst_structure_free (config);
1098
1099   return TRUE;
1100 }
1101
1102 static GstFlowReturn
1103 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
1104 {
1105   FUNCTION;
1106   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
1107   return gst_wayland_sink_render (bsink, buffer);
1108 }
1109
1110 static void
1111 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
1112 {
1113   GstWaylandSink *sink = data;
1114   FUNCTION;
1115
1116   GST_LOG ("frame_redraw_cb");
1117
1118   g_atomic_int_set (&sink->redraw_pending, FALSE);
1119   wl_callback_destroy (callback);
1120 }
1121
1122 static const struct wl_callback_listener frame_callback_listener = {
1123   frame_redraw_callback
1124 };
1125
1126 #ifdef GST_WLSINK_ENHANCEMENT
1127 static void
1128 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1129 {
1130   FUNCTION;
1131   g_return_if_fail (sink != NULL);
1132   g_return_if_fail (sink->window != NULL);
1133
1134   gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1135   gst_wl_window_set_disp_geo_method (sink->window,
1136       sink->display_geometry_method);
1137   gst_wl_window_set_orientation (sink->window, sink->orientation);
1138   gst_wl_window_set_flip (sink->window, sink->flip);
1139 }
1140 #endif
1141 /* must be called with the render lock */
1142 static void
1143 render_last_buffer (GstWaylandSink * sink)
1144 {
1145   GstWlBuffer *wlbuffer;
1146   const GstVideoInfo *info = NULL;
1147   struct wl_surface *surface;
1148   struct wl_callback *callback;
1149   FUNCTION;
1150
1151   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
1152   surface = gst_wl_window_get_wl_surface (sink->window);
1153
1154   g_atomic_int_set (&sink->redraw_pending, TRUE);
1155   callback = wl_surface_frame (surface);
1156   /* frame_callback_listener is called when wayland-client finish rendering the wl_buffer */
1157   wl_callback_add_listener (callback, &frame_callback_listener, sink);
1158
1159   if (G_UNLIKELY (sink->video_info_changed)) {
1160     info = &sink->video_info;
1161     sink->video_info_changed = FALSE;
1162   }
1163 #ifdef GST_WLSINK_ENHANCEMENT
1164   if (sink->last_buffer)
1165     gst_wl_window_render (sink->window, wlbuffer, info);
1166   else {
1167     if (G_UNLIKELY (info)) {
1168       gst_wl_window_set_video_info (sink->window, &info);
1169     }
1170   }
1171 #else
1172   gst_wl_window_render (sink->window, wlbuffer, info);
1173 #endif
1174 }
1175
1176 static GstFlowReturn
1177 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
1178 {
1179   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1180   GstBuffer *to_render;
1181   GstWlBuffer *wlbuffer;
1182   GstFlowReturn ret = GST_FLOW_OK;
1183   FUNCTION;
1184
1185   g_mutex_lock (&sink->render_lock);
1186
1187   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
1188
1189   if (G_UNLIKELY (!sink->window)) {
1190     /* ask for window handle. Unlock render_lock while doing that because
1191      * set_window_handle & friends will lock it in this context */
1192     g_mutex_unlock (&sink->render_lock);
1193     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
1194     g_mutex_lock (&sink->render_lock);
1195
1196     if (!sink->window) {
1197       /* if we were not provided a window, create one ourselves */
1198       sink->window =
1199           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
1200     }
1201   }
1202 #ifdef GST_WLSINK_ENHANCEMENT
1203   gst_wayland_sink_update_window_geometry (sink);
1204   sink->video_info_changed = TRUE;
1205 #endif
1206   /* drop buffers until we get a frame callback */
1207   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
1208     goto done;
1209   /* make sure that the application has called set_render_rectangle() */
1210   if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
1211     goto no_window_size;
1212
1213 #ifdef GST_WLSINK_ENHANCEMENT
1214
1215   wlbuffer = gst_buffer_get_wl_buffer (buffer);
1216   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1217     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
1218     GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1219     to_render = buffer;
1220 #ifdef DUMP_BUFFER
1221     GstMemory *mem;
1222     GstMapInfo mem_info = GST_MAP_INFO_INIT;
1223     int size = GST_VIDEO_INFO_SIZE (&sink->video_info);
1224     mem = gst_buffer_peek_memory (to_render, 0);
1225     gst_memory_map (mem, &mem_info, GST_MAP_READ);
1226     void *data;
1227     data = mem_info.data;
1228     int ret;
1229     char file_name[128];
1230
1231     sprintf (file_name, "/home/owner/DUMP/_WLSINK_OUT_DUMP_%2.2d.dump",
1232         dump__cnt++);
1233     ret = __write_rawdata (file_name, data, size);
1234     if (ret) {
1235       GST_ERROR ("_write_rawdata() failed");
1236     }
1237     GST_INFO ("DUMP IMAGE %d, size (%d)", dump__cnt, size);
1238     gst_memory_unmap (mem, &mem_info);
1239 #endif
1240   } else {
1241     GstMemory *mem;
1242     struct wl_buffer *wbuf = NULL;
1243
1244     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
1245     mem = gst_buffer_peek_memory (buffer, 0);
1246     if (gst_is_wl_shm_memory (mem)) {
1247       FUNCTION;
1248       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1249           &sink->video_info);
1250       if (wbuf) {
1251         gst_buffer_add_wl_buffer (buffer, wbuf, sink->display); //careat GstWlBuffer and add  gstbuffer, wlbuffer, display and etc
1252         to_render = buffer;
1253       }
1254     } else {                    //buffer is not from our pool and have not wl_buffer
1255       GstMapInfo src;
1256       /* we don't know how to create a wl_buffer directly from the provided
1257        * memory, so we have to copy the data to a memory that we know how
1258        * to handle... */
1259
1260       GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1261       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1262           buffer);
1263
1264       if (sink->USE_TBM && sink->display->is_native_format) {
1265         /* in case of SN12 or ST12 */
1266         if (!gst_wayland_sink_get_mm_video_buf_info(sink->display, buffer))
1267           return GST_FLOW_ERROR;
1268
1269         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1270         if (G_UNLIKELY (!wlbuffer)) {
1271           wbuf =
1272               gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1273               &sink->video_info);
1274           if (G_UNLIKELY (!wbuf))
1275             goto no_wl_buffer;
1276           gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1277         }
1278       }
1279       else if (sink->USE_TBM && !sink->display->is_native_format) {
1280
1281         /* sink->pool always exists (created in set_caps), but it may not
1282          * be active if upstream is not using it */
1283         if (!gst_buffer_pool_is_active (sink->pool)
1284             && !gst_buffer_pool_set_active (sink->pool, TRUE))
1285           goto activate_failed;
1286
1287         ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1288         if (ret != GST_FLOW_OK)
1289           goto no_buffer;
1290
1291         //GstMemory *mem;
1292         //mem = gst_buffer_peek_memory (to_render, 0);
1293         //if (gst_is_wl_shm_memory (mem)) {
1294         GST_INFO ("to_render buffer is our buffer");
1295         //}
1296         /* the first time we acquire a buffer,
1297          * we need to attach a wl_buffer on it */
1298         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1299         if (G_UNLIKELY (!wlbuffer)) {
1300           mem = gst_buffer_peek_memory (to_render, 0);
1301           wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1302               &sink->video_info);
1303           if (G_UNLIKELY (!wbuf))
1304             goto no_wl_buffer;
1305
1306           wlbuffer = gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1307         }
1308
1309         gst_buffer_map (buffer, &src, GST_MAP_READ);
1310         gst_buffer_fill (to_render, 0, src.data, src.size);
1311         gst_buffer_unmap (buffer, &src);
1312       } else {                  /* USE SHM */
1313         /* sink->pool always exists (created in set_caps), but it may not
1314          * be active if upstream is not using it */
1315         if (!gst_buffer_pool_is_active (sink->pool) &&
1316             !gst_buffer_pool_set_active (sink->pool, TRUE))
1317           goto activate_failed;
1318         ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1319         if (ret != GST_FLOW_OK)
1320           goto no_buffer;
1321         /* the first time we acquire a buffer,
1322          * we need to attach a wl_buffer on it */
1323         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1324         if (G_UNLIKELY (!wlbuffer)) {
1325           mem = gst_buffer_peek_memory (to_render, 0);
1326           wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1327               &sink->video_info);
1328           if (G_UNLIKELY (!wbuf))
1329             goto no_wl_buffer;
1330
1331           gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1332
1333         }
1334
1335         gst_buffer_map (buffer, &src, GST_MAP_READ);
1336         gst_buffer_fill (to_render, 0, src.data, src.size);
1337         gst_buffer_unmap (buffer, &src);
1338       }
1339     }
1340   }
1341
1342   if (sink->USE_TBM && sink->display->is_native_format) {
1343     if (G_UNLIKELY (buffer == sink->last_buffer)) {
1344       GST_LOG_OBJECT (sink, "Buffer already being rendered");
1345       goto done;
1346     }
1347     gst_buffer_replace (&sink->last_buffer, buffer);
1348     render_last_buffer (sink);
1349
1350     goto done;
1351   } else {                      /* USE SHM or normal format */
1352     /* drop double rendering */
1353     if (G_UNLIKELY (buffer == sink->last_buffer)) {
1354       GST_LOG_OBJECT (sink, "Buffer already being rendered");
1355       goto done;
1356     }
1357     gst_buffer_replace (&sink->last_buffer, to_render);
1358     render_last_buffer (sink);
1359
1360     if (buffer != to_render)
1361       gst_buffer_unref (to_render);
1362
1363     goto done;
1364   }
1365
1366 #else /* open source */
1367
1368   wlbuffer = gst_buffer_get_wl_buffer (buffer);
1369
1370   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1371     GST_LOG_OBJECT (sink,
1372         "buffer %p has a wl_buffer from our display, " "writing directly",
1373         buffer);
1374     GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1375     to_render = buffer;
1376
1377   } else {
1378     GstMemory *mem;
1379     struct wl_buffer *wbuf = NULL;
1380
1381     GST_LOG_OBJECT (sink,
1382         "buffer %p does not have a wl_buffer from our " "display, creating it",
1383         buffer);
1384     mem = gst_buffer_peek_memory (buffer, 0);
1385     if (gst_is_wl_shm_memory (mem)) {
1386       FUNCTION;
1387       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1388           &sink->video_info);
1389     }
1390     if (wbuf) {
1391       gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1392       to_render = buffer;
1393
1394     } else {
1395       GstMapInfo src;
1396       /* we don't know how to create a wl_buffer directly from the provided
1397        * memory, so we have to copy the data to a memory that we know how
1398        * to handle... */
1399
1400       GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1401       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1402           buffer);
1403       /* sink->pool always exists (created in set_caps), but it may not
1404        * be active if upstream is not using it */
1405       if (!gst_buffer_pool_is_active (sink->pool) &&
1406           !gst_buffer_pool_set_active (sink->pool, TRUE))
1407         goto activate_failed;
1408
1409       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1410       if (ret != GST_FLOW_OK)
1411         goto no_buffer;
1412
1413       /* the first time we acquire a buffer,
1414        * we need to attach a wl_buffer on it */
1415       wlbuffer = gst_buffer_get_wl_buffer (buffer);
1416       if (G_UNLIKELY (!wlbuffer)) {
1417         mem = gst_buffer_peek_memory (to_render, 0);
1418         wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1419             &sink->video_info);
1420         if (G_UNLIKELY (!wbuf))
1421           goto no_wl_buffer;
1422
1423         gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1424       }
1425
1426       gst_buffer_map (buffer, &src, GST_MAP_READ);
1427       gst_buffer_fill (to_render, 0, src.data, src.size);
1428       gst_buffer_unmap (buffer, &src);
1429     }
1430   }
1431   /* drop double rendering */
1432   if (G_UNLIKELY (buffer == sink->last_buffer)) {
1433     GST_LOG_OBJECT (sink, "Buffer already being rendered");
1434     goto done;
1435   }
1436
1437   gst_buffer_replace (&sink->last_buffer, to_render);
1438   render_last_buffer (sink);
1439
1440   if (buffer != to_render)
1441     gst_buffer_unref (to_render);
1442
1443   goto done;
1444
1445 #endif /* GST_WLSINK_ENHANCEMENT */
1446
1447 no_window_size:
1448   {
1449     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1450         ("Window has no size set"),
1451         ("Make sure you set the size after calling set_window_handle"));
1452     ret = GST_FLOW_ERROR;
1453     goto done;
1454   }
1455 no_buffer:
1456   {
1457     GST_WARNING_OBJECT (sink, "could not create buffer");
1458     goto done;
1459   }
1460 no_wl_buffer:
1461   {
1462     GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
1463     ret = GST_FLOW_ERROR;
1464     goto done;
1465   }
1466 activate_failed:
1467   {
1468     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1469     ret = GST_FLOW_ERROR;
1470     goto done;
1471   }
1472 done:
1473   {
1474     g_mutex_unlock (&sink->render_lock);
1475     return ret;
1476   }
1477 }
1478
1479 static void
1480 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1481 {
1482   iface->set_window_handle = gst_wayland_sink_set_window_handle;
1483   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1484   iface->expose = gst_wayland_sink_expose;
1485 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1486   iface->set_wl_window_wl_surface_id =
1487       gst_wayland_sink_set_wl_window_wl_surface_id;
1488 #endif
1489 }
1490
1491 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1492 static void
1493 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
1494     guintptr wl_surface_id)
1495 {
1496   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1497   FUNCTION;
1498   g_return_if_fail (sink != NULL);
1499
1500   if (sink->window != NULL) {
1501     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1502     return;
1503   }
1504   g_mutex_lock (&sink->render_lock);
1505   g_clear_object (&sink->window);
1506
1507   GST_INFO ("wl_surface_id %d %p", (int) wl_surface_id, (guintptr)wl_surface_id);
1508
1509   if (wl_surface_id) {
1510     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1511       /* we cannot use our own display with an external window handle */
1512       if (G_UNLIKELY (sink->display->own_display)) {
1513         sink->display->wl_surface_id = (int) wl_surface_id;
1514         sink->window = gst_wl_window_new_in_surface (sink->display, NULL);
1515       }
1516     } else {
1517       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1518           "ignoring window handle");
1519     }
1520   }
1521   gst_wayland_sink_update_window_geometry (sink);
1522
1523   g_mutex_unlock (&sink->render_lock);
1524
1525 }
1526 #endif
1527
1528 static void
1529 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1530 {
1531   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1532   struct wl_surface *surface = (struct wl_surface *) handle;
1533   FUNCTION;
1534
1535   g_return_if_fail (sink != NULL);
1536
1537 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1538   if (sink->window != NULL) {
1539     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1540     return;
1541   }
1542 #endif
1543   g_mutex_lock (&sink->render_lock);
1544
1545   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1546       (void *) handle);
1547
1548   g_clear_object (&sink->window);
1549
1550   if (handle) {
1551     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1552       /* we cannot use our own display with an external window handle */
1553       if (G_UNLIKELY (sink->display->own_display)) {
1554                   GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
1555                           ("Application did not provide a wayland display handle"),
1556                           ("Now waylandsink use internal display handle "
1557                                   "which is created ourselves. Consider providing a "
1558                                   "display handle from your application with GstContext"));
1559                   sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1560       } else {
1561         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1562       }
1563     } else {
1564       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1565           "ignoring window handle");
1566     }
1567   }
1568   g_mutex_unlock (&sink->render_lock);
1569
1570 }
1571
1572 static void
1573 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1574     gint x, gint y, gint w, gint h)
1575 {
1576   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1577   FUNCTION;
1578
1579   g_return_if_fail (sink != NULL);
1580
1581   g_mutex_lock (&sink->render_lock);
1582   if (!sink->window) {
1583     g_mutex_unlock (&sink->render_lock);
1584     GST_WARNING_OBJECT (sink,
1585         "set_render_rectangle called without window, ignoring");
1586     return;
1587   }
1588
1589   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1590       x, y, w, h);
1591   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1592
1593   g_mutex_unlock (&sink->render_lock);
1594 }
1595
1596 static void
1597 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1598 {
1599   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1600   FUNCTION;
1601
1602   g_return_if_fail (sink != NULL);
1603
1604   GST_DEBUG_OBJECT (sink, "expose");
1605
1606   g_mutex_lock (&sink->render_lock);
1607   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1608     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1609     render_last_buffer (sink);
1610   }
1611   g_mutex_unlock (&sink->render_lock);
1612 }
1613
1614 static void
1615 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1616 {
1617   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1618   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1619 }
1620
1621 static void
1622 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1623 {
1624   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1625   FUNCTION;
1626   g_return_if_fail (sink != NULL);
1627
1628   g_mutex_lock (&sink->render_lock);
1629   if (!sink->window || !sink->window->area_subsurface) {
1630     g_mutex_unlock (&sink->render_lock);
1631     GST_INFO_OBJECT (sink,
1632         "begin_geometry_change called without window, ignoring");
1633     return;
1634   }
1635
1636   wl_subsurface_set_sync (sink->window->area_subsurface);
1637   g_mutex_unlock (&sink->render_lock);
1638 }
1639
1640 static void
1641 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1642 {
1643   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1644   FUNCTION;
1645   g_return_if_fail (sink != NULL);
1646
1647   g_mutex_lock (&sink->render_lock);
1648   if (!sink->window || !sink->window->area_subsurface) {
1649     g_mutex_unlock (&sink->render_lock);
1650     GST_INFO_OBJECT (sink,
1651         "end_geometry_change called without window, ignoring");
1652     return;
1653   }
1654
1655   wl_subsurface_set_desync (sink->window->area_subsurface);
1656   g_mutex_unlock (&sink->render_lock);
1657 }
1658
1659 static gboolean
1660 plugin_init (GstPlugin * plugin)
1661 {
1662   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1663       " wayland video sink");
1664
1665   gst_wl_shm_allocator_register ();
1666
1667   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1668       GST_TYPE_WAYLAND_SINK);
1669 }
1670
1671 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1672     GST_VERSION_MINOR,
1673     waylandsink,
1674     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1675     GST_PACKAGE_ORIGIN)