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