gtkglsink: Hide and clean the GtkWindow we might create
[platform/upstream/gst-plugins-good.git] / ext / gtk / gstgtkglsink.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:gstgtkglsink
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "gstgtkglsink.h"
31 #include "gtkgstglwidget.h"
32
33 GST_DEBUG_CATEGORY (gst_debug_gtk_gl_sink);
34 #define GST_CAT_DEFAULT gst_debug_gtk_gl_sink
35
36 static gboolean gst_gtk_gl_sink_start (GstBaseSink * bsink);
37 static gboolean gst_gtk_gl_sink_stop (GstBaseSink * bsink);
38 static gboolean gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query);
39 static gboolean gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink,
40     GstQuery * query);
41 static GstCaps *gst_gtk_gl_sink_get_caps (GstBaseSink * bsink,
42     GstCaps * filter);
43
44 static GstStaticPadTemplate gst_gtk_gl_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
49         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, "RGBA")));
50
51 #define gst_gtk_gl_sink_parent_class parent_class
52 G_DEFINE_TYPE_WITH_CODE (GstGtkGLSink, gst_gtk_gl_sink,
53     GST_TYPE_GTK_BASE_SINK, GST_DEBUG_CATEGORY_INIT (gst_debug_gtk_gl_sink,
54         "gtkglsink", 0, "Gtk Video Sink"));
55
56 static void
57 gst_gtk_gl_sink_class_init (GstGtkGLSinkClass * klass)
58 {
59   GstElementClass *gstelement_class;
60   GstBaseSinkClass *gstbasesink_class;
61   GstGtkBaseSinkClass *gstgtkbasesink_class;
62
63   gstelement_class = (GstElementClass *) klass;
64   gstbasesink_class = (GstBaseSinkClass *) klass;
65   gstgtkbasesink_class = (GstGtkBaseSinkClass *) klass;
66
67   gstbasesink_class->query = gst_gtk_gl_sink_query;
68   gstbasesink_class->propose_allocation = gst_gtk_gl_sink_propose_allocation;
69   gstbasesink_class->start = gst_gtk_gl_sink_start;
70   gstbasesink_class->stop = gst_gtk_gl_sink_stop;
71   gstbasesink_class->get_caps = gst_gtk_gl_sink_get_caps;
72
73   gstgtkbasesink_class->create_widget = gtk_gst_gl_widget_new;
74   gstgtkbasesink_class->window_title = "Gtk+ GL renderer";
75
76   gst_element_class_set_metadata (gstelement_class, "Gtk Video Sink",
77       "Sink/Video", "A video sink that renders to a GtkWidget",
78       "Matthew Waters <matthew@centricular.com>");
79
80   gst_element_class_add_pad_template (gstelement_class,
81       gst_static_pad_template_get (&gst_gtk_gl_sink_template));
82 }
83
84 static void
85 gst_gtk_gl_sink_init (GstGtkGLSink * gtk_sink)
86 {
87 }
88
89 static gboolean
90 gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query)
91 {
92   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
93   gboolean res = FALSE;
94
95   switch (GST_QUERY_TYPE (query)) {
96     case GST_QUERY_CONTEXT:
97     {
98       const gchar *context_type;
99       GstContext *context, *old_context;
100
101       res = gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
102           &gtk_sink->display, &gtk_sink->gtk_context);
103
104       if (gtk_sink->display)
105         gst_gl_display_filter_gl_api (gtk_sink->display, GST_GL_API_OPENGL3);
106
107       gst_query_parse_context_type (query, &context_type);
108
109       if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
110         GstStructure *s;
111
112         gst_query_parse_context (query, &old_context);
113
114         if (old_context)
115           context = gst_context_copy (old_context);
116         else
117           context = gst_context_new ("gst.gl.local_context", FALSE);
118
119         s = gst_context_writable_structure (context);
120         gst_structure_set (s, "context", GST_GL_TYPE_CONTEXT, gtk_sink->context,
121             NULL);
122         gst_query_set_context (query, context);
123         gst_context_unref (context);
124
125         res = gtk_sink->context != NULL;
126       }
127       GST_LOG_OBJECT (gtk_sink, "context query of type %s %i", context_type,
128           res);
129       break;
130     }
131     default:
132       res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
133       break;
134   }
135
136   return res;
137 }
138
139 static void
140 _size_changed_cb (GtkWidget * widget, GdkRectangle * rectangle,
141     GstGtkGLSink * gtk_sink)
142 {
143   gint scale_factor, width, height;
144   gboolean reconfigure;
145
146   scale_factor = gtk_widget_get_scale_factor (widget);
147   width = scale_factor * gtk_widget_get_allocated_width (widget);
148   height = scale_factor * gtk_widget_get_allocated_height (widget);
149
150   GST_OBJECT_LOCK (gtk_sink);
151   reconfigure =
152       (width != gtk_sink->display_width || height != gtk_sink->display_height);
153   gtk_sink->display_width = width;
154   gtk_sink->display_height = height;
155   GST_OBJECT_UNLOCK (gtk_sink);
156
157   if (reconfigure) {
158     GST_DEBUG_OBJECT (gtk_sink, "Sending reconfigure event on sinkpad.");
159     gst_pad_push_event (GST_BASE_SINK (gtk_sink)->sinkpad,
160         gst_event_new_reconfigure ());
161   }
162 }
163
164 static gboolean
165 gst_gtk_gl_sink_start (GstBaseSink * bsink)
166 {
167   GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (bsink);
168   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
169   GtkGstGLWidget *gst_widget;
170
171   if (!GST_BASE_SINK_CLASS (parent_class)->start (bsink))
172     return FALSE;
173
174   /* After this point, gtk_sink->widget will always be set */
175   gst_widget = GTK_GST_GL_WIDGET (base_sink->widget);
176
177   /* Track the allocation size */
178   g_signal_connect (gst_widget, "size-allocate", G_CALLBACK (_size_changed_cb),
179       gtk_sink);
180   _size_changed_cb (GTK_WIDGET (gst_widget), NULL, gtk_sink);
181
182   if (!gtk_gst_gl_widget_init_winsys (gst_widget))
183     return FALSE;
184
185   gtk_sink->display = gtk_gst_gl_widget_get_display (gst_widget);
186   gtk_sink->context = gtk_gst_gl_widget_get_context (gst_widget);
187   gtk_sink->gtk_context = gtk_gst_gl_widget_get_gtk_context (gst_widget);
188
189   if (!gtk_sink->display || !gtk_sink->context || !gtk_sink->gtk_context)
190     return FALSE;
191
192   return TRUE;
193 }
194
195 static gboolean
196 gst_gtk_gl_sink_stop (GstBaseSink * bsink)
197 {
198   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
199
200   if (gtk_sink->display) {
201     gst_object_unref (gtk_sink->display);
202     gtk_sink->display = NULL;
203   }
204
205   if (gtk_sink->context) {
206     gst_object_unref (gtk_sink->context);
207     gtk_sink->context = NULL;
208   }
209
210   if (gtk_sink->gtk_context) {
211     gst_object_unref (gtk_sink->gtk_context);
212     gtk_sink->gtk_context = NULL;
213   }
214
215   return GST_BASE_SINK_CLASS (parent_class)->stop (bsink);
216 }
217
218 static gboolean
219 gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
220 {
221   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
222   GstBufferPool *pool = NULL;
223   GstStructure *config;
224   GstCaps *caps;
225   guint size;
226   gboolean need_pool;
227   GstStructure *allocation_meta = NULL;
228   gint display_width, display_height;
229
230   if (!gtk_sink->display || !gtk_sink->context)
231     return FALSE;
232
233   gst_query_parse_allocation (query, &caps, &need_pool);
234
235   if (caps == NULL)
236     goto no_caps;
237
238   if (need_pool) {
239     GstVideoInfo info;
240
241     if (!gst_video_info_from_caps (&info, caps))
242       goto invalid_caps;
243
244     GST_DEBUG_OBJECT (gtk_sink, "create new pool");
245     pool = gst_gl_buffer_pool_new (gtk_sink->context);
246
247     /* the normal size of a frame */
248     size = info.size;
249
250     config = gst_buffer_pool_get_config (pool);
251     gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
252     gst_buffer_pool_config_add_option (config,
253         GST_BUFFER_POOL_OPTION_GL_SYNC_META);
254
255     if (!gst_buffer_pool_set_config (pool, config))
256       goto config_failed;
257
258     /* we need at least 2 buffer because we hold on to the last one */
259     gst_query_add_allocation_pool (query, pool, size, 2, 0);
260     gst_object_unref (pool);
261   }
262
263   GST_OBJECT_LOCK (gtk_sink);
264   display_width = gtk_sink->display_width;
265   display_height = gtk_sink->display_height;
266   GST_OBJECT_UNLOCK (gtk_sink);
267
268   if (display_width != 0 && display_height != 0) {
269     GST_DEBUG_OBJECT (gtk_sink, "sending alloc query with size %dx%d",
270         display_width, display_height);
271     allocation_meta = gst_structure_new ("GstVideoOverlayCompositionMeta",
272         "width", G_TYPE_UINT, display_width,
273         "height", G_TYPE_UINT, display_height, NULL);
274   }
275
276   gst_query_add_allocation_meta (query,
277       GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, allocation_meta);
278
279   if (allocation_meta)
280     gst_structure_free (allocation_meta);
281
282   /* we also support various metadata */
283   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
284
285   if (gtk_sink->context->gl_vtable->FenceSync)
286     gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
287
288   return TRUE;
289
290   /* ERRORS */
291 no_caps:
292   {
293     GST_DEBUG_OBJECT (bsink, "no caps specified");
294     return FALSE;
295   }
296 invalid_caps:
297   {
298     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
299     return FALSE;
300   }
301 config_failed:
302   {
303     GST_DEBUG_OBJECT (bsink, "failed setting config");
304     return FALSE;
305   }
306 }
307
308 static GstCaps *
309 gst_gtk_gl_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
310 {
311   GstCaps *tmp = NULL;
312   GstCaps *result = NULL;
313
314   tmp = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));
315
316   if (filter) {
317     GST_DEBUG_OBJECT (bsink, "intersecting with filter caps %" GST_PTR_FORMAT,
318         filter);
319
320     result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
321     gst_caps_unref (tmp);
322   } else {
323     result = tmp;
324   }
325
326   result = gst_gl_overlay_compositor_add_caps (result);
327
328   GST_DEBUG_OBJECT (bsink, "returning caps: %" GST_PTR_FORMAT, result);
329
330   return result;
331 }