3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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.
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.
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.
27 #include "gtkgstglwidget.h"
28 #include <gst/video/video.h>
30 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
32 #include <gst/gl/x11/gstgldisplay_x11.h>
33 #include <gst/gl/x11/gstglcontext_glx.h>
37 * SECTION:gtkgstglwidget
38 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
39 * @see_also: #GtkGLArea, #GstBuffer
41 * #GtkGstGLWidget is an #GtkWidget that renders GStreamer video buffers.
44 #define GST_CAT_DEFAULT gtk_gst_gl_widget_debug
45 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
47 G_DEFINE_TYPE_WITH_CODE (GtkGstGLWidget, gtk_gst_gl_widget, GTK_TYPE_GL_AREA,
48 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gtkgstglwidget", 0,
49 "Gtk Gst GL Widget"););
51 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
52 GTK_TYPE_GST_GL_WIDGET, GtkGstGLWidgetPrivate))
54 struct _GtkGstGLWidgetPrivate
66 GstGLDisplay *display;
67 GdkGLContext *gdk_context;
68 GstGLContext *other_context;
69 GstGLContext *context;
80 gtk_gst_gl_widget_get_preferred_width (GtkWidget * widget, gint * min,
83 GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
84 gint video_width = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
86 if (!gst_widget->priv->negotiated)
92 *natural = video_width;
96 gtk_gst_gl_widget_get_preferred_height (GtkWidget * widget, gint * min,
99 GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
100 gint video_height = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
102 if (!gst_widget->priv->negotiated)
108 *natural = video_height;
111 static const GLfloat vertices[] = {
112 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
113 -1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
114 -1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
115 1.0f, -1.0f, 0.0f, 1.0f, 1.0f
119 gtk_gst_gl_widget_bind_buffer (GtkGstGLWidget * gst_widget)
121 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
123 gl->BindBuffer (GL_ARRAY_BUFFER, gst_widget->priv->vertex_buffer);
125 /* Load the vertex position */
126 gl->VertexAttribPointer (gst_widget->priv->attr_position, 3, GL_FLOAT,
127 GL_FALSE, 5 * sizeof (GLfloat), (void *) 0);
129 /* Load the texture coordinate */
130 gl->VertexAttribPointer (gst_widget->priv->attr_texture, 2, GL_FLOAT,
131 GL_FALSE, 5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
133 gl->EnableVertexAttribArray (gst_widget->priv->attr_position);
134 gl->EnableVertexAttribArray (gst_widget->priv->attr_texture);
138 gtk_gst_gl_widget_unbind_buffer (GtkGstGLWidget * gst_widget)
140 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
142 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
144 gl->DisableVertexAttribArray (gst_widget->priv->attr_position);
145 gl->DisableVertexAttribArray (gst_widget->priv->attr_texture);
149 gtk_gst_gl_widget_init_redisplay (GtkGstGLWidget * gst_widget)
151 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
153 gst_widget->priv->shader = gst_gl_shader_new (gst_widget->priv->context);
155 gst_gl_shader_compile_with_default_vf_and_check
156 (gst_widget->priv->shader, &gst_widget->priv->attr_position,
157 &gst_widget->priv->attr_texture);
159 if (gl->GenVertexArrays) {
160 gl->GenVertexArrays (1, &gst_widget->priv->vao);
161 gl->BindVertexArray (gst_widget->priv->vao);
164 gl->GenBuffers (1, &gst_widget->priv->vertex_buffer);
165 gl->BindBuffer (GL_ARRAY_BUFFER, gst_widget->priv->vertex_buffer);
166 gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
169 if (gl->GenVertexArrays) {
170 gtk_gst_gl_widget_bind_buffer (gst_widget);
171 gl->BindVertexArray (0);
174 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
176 gst_widget->priv->initted = TRUE;
180 _redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
182 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
183 GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
185 gst_gl_shader_use (gst_widget->priv->shader);
187 if (gl->BindVertexArray)
188 gl->BindVertexArray (gst_widget->priv->vao);
190 gtk_gst_gl_widget_bind_buffer (gst_widget);
192 gl->ActiveTexture (GL_TEXTURE0);
193 gl->BindTexture (GL_TEXTURE_2D, tex);
194 gst_gl_shader_set_uniform_1i (gst_widget->priv->shader, "tex", 0);
196 gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
198 if (gl->BindVertexArray)
199 gl->BindVertexArray (0);
201 gtk_gst_gl_widget_unbind_buffer (gst_widget);
203 gl->BindTexture (GL_TEXTURE_2D, 0);
207 gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
209 GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
211 g_mutex_lock (&gst_widget->priv->lock);
213 if (!gst_widget->priv->initted && gst_widget->priv->context)
214 gtk_gst_gl_widget_init_redisplay (gst_widget);
216 if (gst_widget->priv->initted && gst_widget->priv->negotiated
217 && gst_widget->priv->buffer) {
218 GST_DEBUG ("rendering buffer %p with gdk context %p",
219 gst_widget->priv->buffer, context);
221 gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
223 if (gst_widget->priv->new_buffer || gst_widget->priv->current_tex == 0) {
224 GstVideoFrame gl_frame;
225 GstGLSyncMeta *sync_meta;
227 if (!gst_video_frame_map (&gl_frame, &gst_widget->priv->v_info,
228 gst_widget->priv->buffer, GST_MAP_READ | GST_MAP_GL)) {
232 sync_meta = gst_buffer_get_gl_sync_meta (gst_widget->priv->buffer);
234 gst_gl_sync_meta_set_sync_point (sync_meta, gst_widget->priv->context);
235 gst_gl_sync_meta_wait (sync_meta, gst_widget->priv->other_context);
238 gst_widget->priv->current_tex = *(guint *) gl_frame.data[0];
240 gst_video_frame_unmap (&gl_frame);
243 _redraw_texture (gst_widget, gst_widget->priv->current_tex);
244 gst_widget->priv->new_buffer = FALSE;
247 /* FIXME: nothing to display */
248 glClearColor (0.0, 0.0, 0.0, 0.0);
249 glClear (GL_COLOR_BUFFER_BIT);
252 gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
254 g_mutex_unlock (&gst_widget->priv->lock);
258 typedef void (*ThreadFunc) (gpointer data);
260 struct invoke_context
270 _invoke_func (struct invoke_context *info)
272 g_mutex_lock (&info->lock);
273 info->func (info->data);
275 g_cond_signal (&info->cond);
276 g_mutex_unlock (&info->lock);
278 return G_SOURCE_REMOVE;
282 _invoke_on_main (ThreadFunc func, gpointer data)
284 GMainContext *main_context = g_main_context_default ();
285 struct invoke_context info;
287 g_mutex_init (&info.lock);
288 g_cond_init (&info.cond);
293 g_main_context_invoke (main_context, (GSourceFunc) _invoke_func, &info);
295 g_mutex_lock (&info.lock);
297 g_cond_wait (&info.cond, &info.lock);
298 g_mutex_unlock (&info.lock);
300 g_mutex_clear (&info.lock);
301 g_cond_clear (&info.cond);
305 _reset_gl (GtkGstGLWidget * gst_widget)
307 const GstGLFuncs *gl = gst_widget->priv->other_context->gl_vtable;
309 if (!gst_widget->priv->gdk_context)
310 gst_widget->priv->gdk_context =
311 gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
312 if (gst_widget->priv->gdk_context == NULL)
315 gdk_gl_context_make_current (gst_widget->priv->gdk_context);
316 gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
318 if (gst_widget->priv->vao) {
319 gl->DeleteVertexArrays (1, &gst_widget->priv->vao);
320 gst_widget->priv->vao = 0;
323 if (gst_widget->priv->vertex_buffer) {
324 gl->DeleteBuffers (1, &gst_widget->priv->vertex_buffer);
325 gst_widget->priv->vertex_buffer = 0;
328 if (gst_widget->priv->upload) {
329 gst_object_unref (gst_widget->priv->upload);
330 gst_widget->priv->upload = NULL;
333 if (gst_widget->priv->shader) {
334 gst_object_unref (gst_widget->priv->shader);
335 gst_widget->priv->shader = NULL;
338 gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
340 gst_object_unref (gst_widget->priv->other_context);
341 gst_widget->priv->other_context = NULL;
343 gdk_gl_context_clear_current ();
345 g_object_unref (gst_widget->priv->gdk_context);
346 gst_widget->priv->gdk_context = NULL;
350 _reset (GtkGstGLWidget * gst_widget)
352 gst_buffer_replace (&gst_widget->priv->buffer, NULL);
354 gst_caps_replace (&gst_widget->priv->caps, NULL);
355 gst_caps_replace (&gst_widget->priv->gl_caps, NULL);
357 gst_widget->priv->negotiated = FALSE;
358 gst_widget->priv->initted = FALSE;
359 gst_widget->priv->vao = 0;
360 gst_widget->priv->vertex_buffer = 0;
361 gst_widget->priv->attr_position = 0;
362 gst_widget->priv->attr_texture = 0;
363 gst_widget->priv->current_tex = 0;
364 gst_widget->priv->new_buffer = TRUE;
368 gtk_gst_gl_widget_finalize (GObject * object)
370 GtkGstGLWidget *widget = GTK_GST_GL_WIDGET_CAST (object);
372 g_mutex_clear (&widget->priv->lock);
376 if (widget->priv->other_context) {
377 _invoke_on_main ((ThreadFunc) _reset_gl, widget);
380 if (widget->priv->context) {
381 gst_object_unref (widget->priv->context);
382 widget->priv->context = NULL;
385 if (widget->priv->display) {
386 gst_object_unref (widget->priv->display);
387 widget->priv->display = NULL;
390 G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
394 gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
396 GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
397 GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
399 g_type_class_add_private (klass, sizeof (GtkGstGLWidgetPrivate));
401 gl_widget_klass->render = gtk_gst_gl_widget_render;
402 widget_klass->get_preferred_width = gtk_gst_gl_widget_get_preferred_width;
403 widget_klass->get_preferred_height = gtk_gst_gl_widget_get_preferred_height;
405 G_OBJECT_CLASS (klass)->finalize = gtk_gst_gl_widget_finalize;
409 gtk_gst_gl_widget_init (GtkGstGLWidget * widget)
413 widget->priv = GTK_GST_GL_WIDGET_GET_PRIVATE (widget);
415 g_mutex_init (&widget->priv->lock);
417 display = gdk_display_get_default ();
419 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
420 if (GDK_IS_X11_DISPLAY (display))
421 widget->priv->display = (GstGLDisplay *)
422 gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
426 if (!widget->priv->display)
427 widget->priv->display = gst_gl_display_new ();
429 gtk_gl_area_set_has_alpha ((GtkGLArea *) widget, TRUE);
433 gtk_gst_gl_widget_new (void)
435 return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
439 _queue_draw (GtkGstGLWidget * widget)
441 gtk_widget_queue_draw (GTK_WIDGET (widget));
443 return G_SOURCE_REMOVE;
447 gtk_gst_gl_widget_set_buffer (GtkGstGLWidget * widget, GstBuffer * buffer)
449 GMainContext *main_context = g_main_context_default ();
451 g_return_if_fail (GTK_IS_GST_GL_WIDGET (widget));
452 g_return_if_fail (widget->priv->negotiated);
454 g_mutex_lock (&widget->priv->lock);
456 gst_buffer_replace (&widget->priv->buffer, buffer);
457 widget->priv->new_buffer = TRUE;
459 g_mutex_unlock (&widget->priv->lock);
461 g_main_context_invoke (main_context, (GSourceFunc) _queue_draw, widget);
465 _get_gl_context (GtkGstGLWidget * gst_widget)
467 GstGLPlatform platform;
471 gtk_widget_realize (GTK_WIDGET (gst_widget));
473 if (gst_widget->priv->gdk_context)
474 g_object_unref (gst_widget->priv->gdk_context);
475 gst_widget->priv->gdk_context =
476 gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
477 if (gst_widget->priv->gdk_context == NULL) {
478 g_assert_not_reached ();
482 g_object_ref (gst_widget->priv->gdk_context);
484 gdk_gl_context_make_current (gst_widget->priv->gdk_context);
486 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
487 if (GST_IS_GL_DISPLAY_X11 (gst_widget->priv->display)) {
488 platform = GST_GL_PLATFORM_GLX;
489 gl_api = gst_gl_context_get_current_gl_api (NULL, NULL);
490 gl_handle = gst_gl_context_get_current_gl_context (platform);
492 gst_widget->priv->other_context =
493 gst_gl_context_new_wrapped (gst_widget->priv->display, gl_handle,
498 if (gst_widget->priv->other_context) {
499 GError *error = NULL;
501 gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
502 if (!gst_gl_context_fill_info (gst_widget->priv->other_context, &error)) {
503 GST_ERROR ("failed to retreive gdk context info: %s", error->message);
504 g_object_unref (gst_widget->priv->other_context);
505 gst_widget->priv->other_context = NULL;
507 gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
513 _queue_resize (GtkGstGLWidget * widget)
515 gtk_widget_queue_resize (GTK_WIDGET (widget));
517 return G_SOURCE_REMOVE;
521 gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * widget)
523 g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (widget), FALSE);
525 g_mutex_lock (&widget->priv->lock);
527 if (widget->priv->display && widget->priv->gdk_context
528 && widget->priv->other_context) {
529 g_mutex_unlock (&widget->priv->lock);
533 if (!widget->priv->other_context) {
534 _invoke_on_main ((ThreadFunc) _get_gl_context, widget);
537 if (!GST_GL_IS_CONTEXT (widget->priv->other_context)) {
538 g_mutex_unlock (&widget->priv->lock);
542 widget->priv->context = gst_gl_context_new (widget->priv->display);
544 if (!widget->priv->context) {
545 g_mutex_unlock (&widget->priv->lock);
549 gst_gl_context_create (widget->priv->context, widget->priv->other_context,
552 g_mutex_unlock (&widget->priv->lock);
557 gtk_gst_gl_widget_set_caps (GtkGstGLWidget * widget, GstCaps * caps)
559 GMainContext *main_context = g_main_context_default ();
562 g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (widget), FALSE);
563 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
564 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
566 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
569 if (!gst_video_info_from_caps (&v_info, caps))
572 g_mutex_lock (&widget->priv->lock);
576 gst_caps_replace (&widget->priv->caps, caps);
578 widget->priv->gl_caps = gst_video_info_to_caps (&v_info);
579 gst_caps_set_features (widget->priv->gl_caps, 0,
580 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
582 widget->priv->v_info = v_info;
583 widget->priv->negotiated = TRUE;
585 g_mutex_unlock (&widget->priv->lock);
587 g_main_context_invoke (main_context, (GSourceFunc) _queue_resize, widget);
593 gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
595 if (!gst_widget->priv->other_context)
598 return gst_object_ref (gst_widget->priv->other_context);
602 gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
604 if (!gst_widget->priv->context)
607 return gst_object_ref (gst_widget->priv->context);
611 gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
613 if (!gst_widget->priv->display)
616 return gst_object_ref (gst_widget->priv->display);