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>
36 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
37 #include <gdk/gdkwayland.h>
38 #include <gst/gl/wayland/gstgldisplay_wayland.h>
42 * SECTION:gtkgstglwidget
43 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
44 * @see_also: #GtkGLArea, #GstBuffer
46 * #GtkGstGLWidget is an #GtkWidget that renders GStreamer video buffers.
49 #define GST_CAT_DEFAULT gtk_gst_gl_widget_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 G_DEFINE_TYPE_WITH_CODE (GtkGstGLWidget, gtk_gst_gl_widget, GTK_TYPE_GL_AREA,
53 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gtkgstglwidget", 0,
54 "Gtk Gst GL Widget"););
56 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
57 GTK_TYPE_GST_GL_WIDGET, GtkGstGLWidgetPrivate))
59 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
64 PROP_FORCE_ASPECT_RATIO,
67 struct _GtkGstGLWidgetPrivate
72 gboolean force_aspect_ratio;
82 GstGLDisplay *display;
83 GdkGLContext *gdk_context;
84 GstGLContext *other_context;
85 GstGLContext *context;
96 gtk_gst_gl_widget_get_preferred_width (GtkWidget * widget, gint * min,
99 GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
100 gint video_width = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
102 if (!gst_widget->priv->negotiated)
108 *natural = video_width;
112 gtk_gst_gl_widget_get_preferred_height (GtkWidget * widget, gint * min,
115 GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
116 gint video_height = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
118 if (!gst_widget->priv->negotiated)
124 *natural = video_height;
127 static const GLfloat vertices[] = {
128 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
129 -1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
130 -1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
131 1.0f, -1.0f, 0.0f, 1.0f, 1.0f
135 gtk_gst_gl_widget_bind_buffer (GtkGstGLWidget * gst_widget)
137 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
139 gl->BindBuffer (GL_ARRAY_BUFFER, gst_widget->priv->vertex_buffer);
141 /* Load the vertex position */
142 gl->VertexAttribPointer (gst_widget->priv->attr_position, 3, GL_FLOAT,
143 GL_FALSE, 5 * sizeof (GLfloat), (void *) 0);
145 /* Load the texture coordinate */
146 gl->VertexAttribPointer (gst_widget->priv->attr_texture, 2, GL_FLOAT,
147 GL_FALSE, 5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
149 gl->EnableVertexAttribArray (gst_widget->priv->attr_position);
150 gl->EnableVertexAttribArray (gst_widget->priv->attr_texture);
154 gtk_gst_gl_widget_unbind_buffer (GtkGstGLWidget * gst_widget)
156 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
158 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
160 gl->DisableVertexAttribArray (gst_widget->priv->attr_position);
161 gl->DisableVertexAttribArray (gst_widget->priv->attr_texture);
165 gtk_gst_gl_widget_init_redisplay (GtkGstGLWidget * gst_widget)
167 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
169 gst_widget->priv->shader = gst_gl_shader_new (gst_widget->priv->context);
171 gst_gl_shader_compile_with_default_vf_and_check
172 (gst_widget->priv->shader, &gst_widget->priv->attr_position,
173 &gst_widget->priv->attr_texture);
175 if (gl->GenVertexArrays) {
176 gl->GenVertexArrays (1, &gst_widget->priv->vao);
177 gl->BindVertexArray (gst_widget->priv->vao);
180 gl->GenBuffers (1, &gst_widget->priv->vertex_buffer);
181 gl->BindBuffer (GL_ARRAY_BUFFER, gst_widget->priv->vertex_buffer);
182 gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
185 if (gl->GenVertexArrays) {
186 gtk_gst_gl_widget_bind_buffer (gst_widget);
187 gl->BindVertexArray (0);
190 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
192 gst_widget->priv->initted = TRUE;
196 _redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
198 const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
199 GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
201 if (gst_widget->priv->force_aspect_ratio) {
202 GstVideoRectangle src, dst, result;
203 gint gtk_viewport[4];
205 gl->ClearColor (0.0, 0.0, 0.0, 0.0);
206 gl->Clear (GL_COLOR_BUFFER_BIT);
208 gl->GetIntegerv (GL_VIEWPORT, gtk_viewport);
212 src.w = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
213 src.h = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
215 dst.x = gtk_viewport[0];
216 dst.y = gtk_viewport[1];
217 dst.w = gtk_viewport[2];
218 dst.h = gtk_viewport[3];
220 gst_video_sink_center_rect (src, dst, &result, TRUE);
222 gl->Viewport (result.x, result.y, result.w, result.h);
225 gst_gl_shader_use (gst_widget->priv->shader);
227 if (gl->BindVertexArray)
228 gl->BindVertexArray (gst_widget->priv->vao);
230 gtk_gst_gl_widget_bind_buffer (gst_widget);
232 gl->ActiveTexture (GL_TEXTURE0);
233 gl->BindTexture (GL_TEXTURE_2D, tex);
234 gst_gl_shader_set_uniform_1i (gst_widget->priv->shader, "tex", 0);
236 gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
238 if (gl->BindVertexArray)
239 gl->BindVertexArray (0);
241 gtk_gst_gl_widget_unbind_buffer (gst_widget);
243 gl->BindTexture (GL_TEXTURE_2D, 0);
247 gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
249 GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
251 g_mutex_lock (&gst_widget->priv->lock);
253 if (!gst_widget->priv->initted && gst_widget->priv->context)
254 gtk_gst_gl_widget_init_redisplay (gst_widget);
256 if (gst_widget->priv->initted && gst_widget->priv->negotiated
257 && gst_widget->priv->buffer) {
258 GST_DEBUG ("rendering buffer %p with gdk context %p",
259 gst_widget->priv->buffer, context);
261 gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
263 if (gst_widget->priv->new_buffer || gst_widget->priv->current_tex == 0) {
264 GstVideoFrame gl_frame;
265 GstGLSyncMeta *sync_meta;
267 if (!gst_video_frame_map (&gl_frame, &gst_widget->priv->v_info,
268 gst_widget->priv->buffer, GST_MAP_READ | GST_MAP_GL)) {
272 sync_meta = gst_buffer_get_gl_sync_meta (gst_widget->priv->buffer);
274 gst_gl_sync_meta_set_sync_point (sync_meta, gst_widget->priv->context);
275 gst_gl_sync_meta_wait (sync_meta, gst_widget->priv->other_context);
278 gst_widget->priv->current_tex = *(guint *) gl_frame.data[0];
280 gst_video_frame_unmap (&gl_frame);
283 _redraw_texture (gst_widget, gst_widget->priv->current_tex);
284 gst_widget->priv->new_buffer = FALSE;
287 /* FIXME: nothing to display */
288 glClearColor (0.0, 0.0, 0.0, 0.0);
289 glClear (GL_COLOR_BUFFER_BIT);
292 if (gst_widget->priv->other_context)
293 gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
295 g_mutex_unlock (&gst_widget->priv->lock);
299 typedef void (*ThreadFunc) (gpointer data);
301 struct invoke_context
311 _invoke_func (struct invoke_context *info)
313 g_mutex_lock (&info->lock);
314 info->func (info->data);
316 g_cond_signal (&info->cond);
317 g_mutex_unlock (&info->lock);
319 return G_SOURCE_REMOVE;
323 _invoke_on_main (ThreadFunc func, gpointer data)
325 GMainContext *main_context = g_main_context_default ();
326 struct invoke_context info;
328 g_mutex_init (&info.lock);
329 g_cond_init (&info.cond);
334 g_main_context_invoke (main_context, (GSourceFunc) _invoke_func, &info);
336 g_mutex_lock (&info.lock);
338 g_cond_wait (&info.cond, &info.lock);
339 g_mutex_unlock (&info.lock);
341 g_mutex_clear (&info.lock);
342 g_cond_clear (&info.cond);
346 _reset_gl (GtkGstGLWidget * gst_widget)
348 const GstGLFuncs *gl = gst_widget->priv->other_context->gl_vtable;
350 if (!gst_widget->priv->gdk_context)
351 gst_widget->priv->gdk_context =
352 gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
353 if (gst_widget->priv->gdk_context == NULL)
356 gdk_gl_context_make_current (gst_widget->priv->gdk_context);
357 gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
359 if (gst_widget->priv->vao) {
360 gl->DeleteVertexArrays (1, &gst_widget->priv->vao);
361 gst_widget->priv->vao = 0;
364 if (gst_widget->priv->vertex_buffer) {
365 gl->DeleteBuffers (1, &gst_widget->priv->vertex_buffer);
366 gst_widget->priv->vertex_buffer = 0;
369 if (gst_widget->priv->upload) {
370 gst_object_unref (gst_widget->priv->upload);
371 gst_widget->priv->upload = NULL;
374 if (gst_widget->priv->shader) {
375 gst_object_unref (gst_widget->priv->shader);
376 gst_widget->priv->shader = NULL;
379 gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
381 gst_object_unref (gst_widget->priv->other_context);
382 gst_widget->priv->other_context = NULL;
384 gdk_gl_context_clear_current ();
386 g_object_unref (gst_widget->priv->gdk_context);
387 gst_widget->priv->gdk_context = NULL;
391 _reset (GtkGstGLWidget * gst_widget)
393 gst_buffer_replace (&gst_widget->priv->buffer, NULL);
395 gst_caps_replace (&gst_widget->priv->caps, NULL);
396 gst_caps_replace (&gst_widget->priv->gl_caps, NULL);
398 gst_widget->priv->negotiated = FALSE;
399 gst_widget->priv->initted = FALSE;
400 gst_widget->priv->vao = 0;
401 gst_widget->priv->vertex_buffer = 0;
402 gst_widget->priv->attr_position = 0;
403 gst_widget->priv->attr_texture = 0;
404 gst_widget->priv->current_tex = 0;
405 gst_widget->priv->new_buffer = TRUE;
409 gtk_gst_gl_widget_finalize (GObject * object)
411 GtkGstGLWidget *widget = GTK_GST_GL_WIDGET_CAST (object);
413 g_mutex_clear (&widget->priv->lock);
417 if (widget->priv->other_context) {
418 _invoke_on_main ((ThreadFunc) _reset_gl, widget);
421 if (widget->priv->context) {
422 gst_object_unref (widget->priv->context);
423 widget->priv->context = NULL;
426 if (widget->priv->display) {
427 gst_object_unref (widget->priv->display);
428 widget->priv->display = NULL;
431 G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
435 gtk_gst_gl_widget_set_property (GObject * object, guint prop_id,
436 const GValue * value, GParamSpec * pspec)
438 GtkGstGLWidget *gtk_widget = GTK_GST_GL_WIDGET (object);
441 case PROP_FORCE_ASPECT_RATIO:
442 gtk_widget->priv->force_aspect_ratio = g_value_get_boolean (value);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451 gtk_gst_gl_widget_get_property (GObject * object, guint prop_id, GValue * value,
454 GtkGstGLWidget *gtk_widget = GTK_GST_GL_WIDGET (object);
457 case PROP_FORCE_ASPECT_RATIO:
458 g_value_set_boolean (value, gtk_widget->priv->force_aspect_ratio);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
467 gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
469 GObjectClass *gobject_klass = (GObjectClass *) klass;
470 GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
471 GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
473 g_type_class_add_private (klass, sizeof (GtkGstGLWidgetPrivate));
475 gobject_klass->set_property = gtk_gst_gl_widget_set_property;
476 gobject_klass->get_property = gtk_gst_gl_widget_get_property;
477 gobject_klass->finalize = gtk_gst_gl_widget_finalize;
479 g_object_class_install_property (gobject_klass, PROP_FORCE_ASPECT_RATIO,
480 g_param_spec_boolean ("force-aspect-ratio",
481 "Force aspect ratio",
482 "When enabled, scaling will respect original aspect ratio",
483 DEFAULT_FORCE_ASPECT_RATIO,
484 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 gl_widget_klass->render = gtk_gst_gl_widget_render;
488 widget_klass->get_preferred_width = gtk_gst_gl_widget_get_preferred_width;
489 widget_klass->get_preferred_height = gtk_gst_gl_widget_get_preferred_height;
493 gtk_gst_gl_widget_init (GtkGstGLWidget * widget)
497 widget->priv = GTK_GST_GL_WIDGET_GET_PRIVATE (widget);
499 widget->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
501 g_mutex_init (&widget->priv->lock);
503 display = gdk_display_get_default ();
505 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
506 if (GDK_IS_X11_DISPLAY (display))
507 widget->priv->display = (GstGLDisplay *)
508 gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
511 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
512 if (GDK_IS_WAYLAND_DISPLAY (display)) {
513 struct wl_display *wayland_display =
514 gdk_wayland_display_get_wl_display (display);
515 widget->priv->display = (GstGLDisplay *)
516 gst_gl_display_wayland_new_with_display (wayland_display);
520 if (!widget->priv->display)
521 widget->priv->display = gst_gl_display_new ();
523 gtk_gl_area_set_has_alpha ((GtkGLArea *) widget, TRUE);
527 gtk_gst_gl_widget_new (void)
529 return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
533 _queue_draw (GtkGstGLWidget * widget)
535 gtk_widget_queue_draw (GTK_WIDGET (widget));
537 return G_SOURCE_REMOVE;
541 gtk_gst_gl_widget_set_buffer (GtkGstGLWidget * widget, GstBuffer * buffer)
543 GMainContext *main_context = g_main_context_default ();
545 g_return_if_fail (GTK_IS_GST_GL_WIDGET (widget));
546 g_return_if_fail (widget->priv->negotiated);
548 g_mutex_lock (&widget->priv->lock);
550 gst_buffer_replace (&widget->priv->buffer, buffer);
551 widget->priv->new_buffer = TRUE;
553 g_mutex_unlock (&widget->priv->lock);
555 g_main_context_invoke (main_context, (GSourceFunc) _queue_draw, widget);
559 _get_gl_context (GtkGstGLWidget * gst_widget)
561 GstGLPlatform platform;
565 gtk_widget_realize (GTK_WIDGET (gst_widget));
567 if (gst_widget->priv->gdk_context)
568 g_object_unref (gst_widget->priv->gdk_context);
569 gst_widget->priv->gdk_context =
570 gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
571 if (gst_widget->priv->gdk_context == NULL) {
572 g_assert_not_reached ();
576 g_object_ref (gst_widget->priv->gdk_context);
578 gdk_gl_context_make_current (gst_widget->priv->gdk_context);
580 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
581 if (GST_IS_GL_DISPLAY_X11 (gst_widget->priv->display)) {
582 platform = GST_GL_PLATFORM_GLX;
583 gl_api = gst_gl_context_get_current_gl_api (NULL, NULL);
584 gl_handle = gst_gl_context_get_current_gl_context (platform);
586 gst_widget->priv->other_context =
587 gst_gl_context_new_wrapped (gst_widget->priv->display, gl_handle,
591 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
592 if (GST_IS_GL_DISPLAY_WAYLAND (gst_widget->priv->display)) {
593 platform = GST_GL_PLATFORM_EGL;
594 gl_api = gst_gl_context_get_current_gl_api (NULL, NULL);
595 gl_handle = gst_gl_context_get_current_gl_context (platform);
597 gst_widget->priv->other_context =
598 gst_gl_context_new_wrapped (gst_widget->priv->display, gl_handle,
603 if (gst_widget->priv->other_context) {
604 GError *error = NULL;
606 gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
607 if (!gst_gl_context_fill_info (gst_widget->priv->other_context, &error)) {
608 GST_ERROR ("failed to retreive gdk context info: %s", error->message);
609 g_object_unref (gst_widget->priv->other_context);
610 gst_widget->priv->other_context = NULL;
612 gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
618 _queue_resize (GtkGstGLWidget * widget)
620 gtk_widget_queue_resize (GTK_WIDGET (widget));
622 return G_SOURCE_REMOVE;
626 gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * widget)
628 g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (widget), FALSE);
630 g_mutex_lock (&widget->priv->lock);
632 if (widget->priv->display && widget->priv->gdk_context
633 && widget->priv->other_context) {
634 g_mutex_unlock (&widget->priv->lock);
638 if (!widget->priv->other_context) {
639 _invoke_on_main ((ThreadFunc) _get_gl_context, widget);
642 if (!GST_GL_IS_CONTEXT (widget->priv->other_context)) {
643 g_mutex_unlock (&widget->priv->lock);
647 widget->priv->context = gst_gl_context_new (widget->priv->display);
649 if (!widget->priv->context) {
650 g_mutex_unlock (&widget->priv->lock);
654 gst_gl_context_create (widget->priv->context, widget->priv->other_context,
657 g_mutex_unlock (&widget->priv->lock);
662 gtk_gst_gl_widget_set_caps (GtkGstGLWidget * widget, GstCaps * caps)
664 GMainContext *main_context = g_main_context_default ();
667 g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (widget), FALSE);
668 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
669 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
671 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
674 if (!gst_video_info_from_caps (&v_info, caps))
677 g_mutex_lock (&widget->priv->lock);
681 gst_caps_replace (&widget->priv->caps, caps);
683 widget->priv->gl_caps = gst_video_info_to_caps (&v_info);
684 gst_caps_set_features (widget->priv->gl_caps, 0,
685 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
687 widget->priv->v_info = v_info;
688 widget->priv->negotiated = TRUE;
690 g_mutex_unlock (&widget->priv->lock);
692 g_main_context_invoke (main_context, (GSourceFunc) _queue_resize, widget);
698 gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
700 if (!gst_widget->priv->other_context)
703 return gst_object_ref (gst_widget->priv->other_context);
707 gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
709 if (!gst_widget->priv->context)
712 return gst_object_ref (gst_widget->priv->context);
716 gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
718 if (!gst_widget->priv->display)
721 return gst_object_ref (gst_widget->priv->display);