2014-12-18 06:00:30 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "gtkgstglwidget.h"
|
|
|
|
#include <gst/video/video.h>
|
|
|
|
|
|
|
|
#if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
#include <gst/gl/x11/gstgldisplay_x11.h>
|
|
|
|
#include <gst/gl/x11/gstglcontext_glx.h>
|
|
|
|
#endif
|
|
|
|
|
2015-06-15 04:33:08 +00:00
|
|
|
#if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
|
|
|
|
#include <gdk/gdkwayland.h>
|
|
|
|
#include <gst/gl/wayland/gstgldisplay_wayland.h>
|
|
|
|
#endif
|
|
|
|
|
2014-12-18 06:00:30 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gtkgstglwidget
|
|
|
|
* @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
|
|
|
|
* @see_also: #GtkGLArea, #GstBuffer
|
|
|
|
*
|
|
|
|
* #GtkGstGLWidget is an #GtkWidget that renders GStreamer video buffers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT gtk_gst_gl_widget_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkGstGLWidget, gtk_gst_gl_widget, GTK_TYPE_GL_AREA,
|
|
|
|
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gtkgstglwidget", 0,
|
2015-06-15 08:28:37 +00:00
|
|
|
"Gtk Gst GL Widget"););
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
#define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
|
|
|
|
GTK_TYPE_GST_GL_WIDGET, GtkGstGLWidgetPrivate))
|
|
|
|
|
|
|
|
struct _GtkGstGLWidgetPrivate
|
|
|
|
{
|
2015-06-12 05:17:30 +00:00
|
|
|
/* properties */
|
2014-12-18 06:00:30 +00:00
|
|
|
gboolean new_buffer;
|
|
|
|
|
|
|
|
gboolean initted;
|
|
|
|
GstGLDisplay *display;
|
|
|
|
GdkGLContext *gdk_context;
|
|
|
|
GstGLContext *other_context;
|
|
|
|
GstGLContext *context;
|
|
|
|
GstGLUpload *upload;
|
|
|
|
GstGLShader *shader;
|
|
|
|
GLuint vao;
|
|
|
|
GLuint vertex_buffer;
|
|
|
|
GLint attr_position;
|
|
|
|
GLint attr_texture;
|
|
|
|
GLuint current_tex;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GLfloat vertices[] = {
|
|
|
|
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
|
|
|
|
1.0f, -1.0f, 0.0f, 1.0f, 1.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gst_gl_widget_bind_buffer (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = gst_widget->priv;
|
|
|
|
const GstGLFuncs *gl = priv->context->gl_vtable;
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
/* Load the vertex position */
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->VertexAttribPointer (priv->attr_position, 3, GL_FLOAT, GL_FALSE,
|
|
|
|
5 * sizeof (GLfloat), (void *) 0);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
/* Load the texture coordinate */
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->VertexAttribPointer (priv->attr_texture, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->EnableVertexAttribArray (priv->attr_position);
|
|
|
|
gl->EnableVertexAttribArray (priv->attr_texture);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gst_gl_widget_unbind_buffer (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = gst_widget->priv;
|
|
|
|
const GstGLFuncs *gl = priv->context->gl_vtable;
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->DisableVertexAttribArray (priv->attr_position);
|
|
|
|
gl->DisableVertexAttribArray (priv->attr_texture);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gst_gl_widget_init_redisplay (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = gst_widget->priv;
|
|
|
|
const GstGLFuncs *gl = priv->context->gl_vtable;
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->shader = gst_gl_shader_new (priv->context);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_shader_compile_with_default_vf_and_check (priv->shader,
|
|
|
|
&priv->attr_position, &priv->attr_texture);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
if (gl->GenVertexArrays) {
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->GenVertexArrays (1, &priv->vao);
|
|
|
|
gl->BindVertexArray (priv->vao);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->GenBuffers (1, &priv->vertex_buffer);
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
|
2014-12-18 06:00:30 +00:00
|
|
|
gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
if (gl->GenVertexArrays) {
|
|
|
|
gtk_gst_gl_widget_bind_buffer (gst_widget);
|
|
|
|
gl->BindVertexArray (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->initted = TRUE;
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = gst_widget->priv;
|
|
|
|
const GstGLFuncs *gl = priv->context->gl_vtable;
|
2015-06-15 19:32:43 +00:00
|
|
|
const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (gst_widget->base.force_aspect_ratio) {
|
2015-06-12 05:17:30 +00:00
|
|
|
GstVideoRectangle src, dst, result;
|
2015-06-15 19:29:46 +00:00
|
|
|
gint widget_width, widget_height, widget_scale;
|
2015-06-12 05:17:30 +00:00
|
|
|
|
|
|
|
gl->ClearColor (0.0, 0.0, 0.0, 0.0);
|
|
|
|
gl->Clear (GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2015-06-15 19:29:46 +00:00
|
|
|
widget_scale = gtk_widget_get_scale_factor ((GtkWidget *) gst_widget);
|
|
|
|
widget_width = gtk_widget_get_allocated_width ((GtkWidget *) gst_widget);
|
|
|
|
widget_height = gtk_widget_get_allocated_height ((GtkWidget *) gst_widget);
|
2015-06-12 05:17:30 +00:00
|
|
|
|
|
|
|
src.x = 0;
|
|
|
|
src.y = 0;
|
2015-07-16 18:30:42 +00:00
|
|
|
src.w = gst_widget->base.display_width;
|
|
|
|
src.h = gst_widget->base.display_height;
|
2015-06-12 05:17:30 +00:00
|
|
|
|
2015-06-15 19:29:46 +00:00
|
|
|
dst.x = 0;
|
|
|
|
dst.y = 0;
|
|
|
|
dst.w = widget_width * widget_scale;
|
|
|
|
dst.h = widget_height * widget_scale;
|
2015-06-12 05:17:30 +00:00
|
|
|
|
|
|
|
gst_video_sink_center_rect (src, dst, &result, TRUE);
|
|
|
|
|
|
|
|
gl->Viewport (result.x, result.y, result.w, result.h);
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_shader_use (priv->shader);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
if (gl->BindVertexArray)
|
2015-07-16 18:30:42 +00:00
|
|
|
gl->BindVertexArray (priv->vao);
|
2014-12-18 06:00:30 +00:00
|
|
|
else
|
|
|
|
gtk_gst_gl_widget_bind_buffer (gst_widget);
|
|
|
|
|
|
|
|
gl->ActiveTexture (GL_TEXTURE0);
|
|
|
|
gl->BindTexture (GL_TEXTURE_2D, tex);
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (priv->shader, "tex", 0);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
|
|
|
|
|
|
|
|
if (gl->BindVertexArray)
|
|
|
|
gl->BindVertexArray (0);
|
|
|
|
else
|
|
|
|
gtk_gst_gl_widget_unbind_buffer (gst_widget);
|
|
|
|
|
|
|
|
gl->BindTexture (GL_TEXTURE_2D, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (widget)->priv;
|
|
|
|
GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
GTK_GST_BASE_WIDGET_LOCK (widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (!priv->initted && priv->context)
|
|
|
|
gtk_gst_gl_widget_init_redisplay (GTK_GST_GL_WIDGET (widget));
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->initted && base_widget->negotiated && base_widget->buffer) {
|
2014-12-18 06:00:30 +00:00
|
|
|
GST_DEBUG ("rendering buffer %p with gdk context %p",
|
2015-07-16 18:30:42 +00:00
|
|
|
base_widget->buffer, context);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_context_activate (priv->other_context, TRUE);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (base_widget->new_buffer || priv->current_tex == 0) {
|
2014-12-18 06:00:30 +00:00
|
|
|
GstVideoFrame gl_frame;
|
|
|
|
GstGLSyncMeta *sync_meta;
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (!gst_video_frame_map (&gl_frame, &base_widget->v_info,
|
|
|
|
base_widget->buffer, GST_MAP_READ | GST_MAP_GL)) {
|
2014-12-18 06:00:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
sync_meta = gst_buffer_get_gl_sync_meta (base_widget->buffer);
|
2014-12-18 06:00:30 +00:00
|
|
|
if (sync_meta) {
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_sync_meta_set_sync_point (sync_meta, priv->context);
|
|
|
|
gst_gl_sync_meta_wait (sync_meta, priv->other_context);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->current_tex = *(guint *) gl_frame.data[0];
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
gst_video_frame_unmap (&gl_frame);
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
_redraw_texture (GTK_GST_GL_WIDGET (widget), priv->current_tex);
|
|
|
|
base_widget->new_buffer = FALSE;
|
2014-12-18 06:00:30 +00:00
|
|
|
} else {
|
|
|
|
error:
|
|
|
|
/* FIXME: nothing to display */
|
|
|
|
glClearColor (0.0, 0.0, 0.0, 0.0);
|
|
|
|
glClear (GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->other_context)
|
|
|
|
gst_gl_context_activate (priv->other_context, FALSE);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
GTK_GST_BASE_WIDGET_UNLOCK (widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*ThreadFunc) (gpointer data);
|
|
|
|
|
|
|
|
struct invoke_context
|
|
|
|
{
|
|
|
|
ThreadFunc func;
|
|
|
|
gpointer data;
|
|
|
|
GMutex lock;
|
|
|
|
GCond cond;
|
|
|
|
gboolean fired;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_invoke_func (struct invoke_context *info)
|
|
|
|
{
|
|
|
|
g_mutex_lock (&info->lock);
|
|
|
|
info->func (info->data);
|
|
|
|
info->fired = TRUE;
|
|
|
|
g_cond_signal (&info->cond);
|
|
|
|
g_mutex_unlock (&info->lock);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_invoke_on_main (ThreadFunc func, gpointer data)
|
|
|
|
{
|
|
|
|
GMainContext *main_context = g_main_context_default ();
|
|
|
|
struct invoke_context info;
|
|
|
|
|
|
|
|
g_mutex_init (&info.lock);
|
|
|
|
g_cond_init (&info.cond);
|
|
|
|
info.fired = FALSE;
|
|
|
|
info.func = func;
|
|
|
|
info.data = data;
|
|
|
|
|
|
|
|
g_main_context_invoke (main_context, (GSourceFunc) _invoke_func, &info);
|
|
|
|
|
|
|
|
g_mutex_lock (&info.lock);
|
|
|
|
while (!info.fired)
|
|
|
|
g_cond_wait (&info.cond, &info.lock);
|
|
|
|
g_mutex_unlock (&info.lock);
|
|
|
|
|
|
|
|
g_mutex_clear (&info.lock);
|
|
|
|
g_cond_clear (&info.cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_reset_gl (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = gst_widget->priv;
|
|
|
|
const GstGLFuncs *gl = priv->other_context->gl_vtable;
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (!priv->gdk_context)
|
|
|
|
priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
|
|
|
|
|
|
|
|
if (priv->gdk_context == NULL)
|
2014-12-18 06:00:30 +00:00
|
|
|
return;
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gdk_gl_context_make_current (priv->gdk_context);
|
|
|
|
gst_gl_context_activate (priv->other_context, TRUE);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->vao) {
|
|
|
|
gl->DeleteVertexArrays (1, &priv->vao);
|
|
|
|
priv->vao = 0;
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->vertex_buffer) {
|
|
|
|
gl->DeleteBuffers (1, &priv->vertex_buffer);
|
|
|
|
priv->vertex_buffer = 0;
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->upload) {
|
|
|
|
gst_object_unref (priv->upload);
|
|
|
|
priv->upload = NULL;
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->shader) {
|
|
|
|
gst_object_unref (priv->shader);
|
|
|
|
priv->shader = NULL;
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_context_activate (priv->other_context, FALSE);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_object_unref (priv->other_context);
|
|
|
|
priv->other_context = NULL;
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
gdk_gl_context_clear_current ();
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
g_object_unref (priv->gdk_context);
|
|
|
|
priv->gdk_context = NULL;
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-07-16 18:30:42 +00:00
|
|
|
_reset (GtkGstBaseWidget * base_widget)
|
2014-12-18 06:00:30 +00:00
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (base_widget)->priv;
|
|
|
|
|
|
|
|
priv->initted = FALSE;
|
|
|
|
priv->vao = 0;
|
|
|
|
priv->vertex_buffer = 0;
|
|
|
|
priv->attr_position = 0;
|
|
|
|
priv->attr_texture = 0;
|
|
|
|
priv->current_tex = 0;
|
|
|
|
|
|
|
|
gtk_gl_area_set_has_alpha (GTK_GL_AREA (base_widget),
|
|
|
|
!base_widget->ignore_alpha);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gst_gl_widget_finalize (GObject * object)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (object)->priv;
|
|
|
|
GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (object);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
_reset (base_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->other_context)
|
|
|
|
_invoke_on_main ((ThreadFunc) _reset_gl, base_widget);
|
2015-07-15 18:32:42 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->context)
|
|
|
|
gst_object_unref (priv->context);
|
2015-07-15 18:32:42 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->display)
|
|
|
|
gst_object_unref (priv->display);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gtk_gst_base_widget_finalize (object);
|
2014-12-18 06:00:30 +00:00
|
|
|
G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
|
|
|
|
{
|
2015-06-12 05:17:30 +00:00
|
|
|
GObjectClass *gobject_klass = (GObjectClass *) klass;
|
2014-12-18 06:00:30 +00:00
|
|
|
GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstBaseWidget *base_widget_klass = (GtkGstBaseWidget *) klass;
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (GtkGstGLWidgetPrivate));
|
2015-07-16 18:30:42 +00:00
|
|
|
gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-06-12 05:17:30 +00:00
|
|
|
gobject_klass->finalize = gtk_gst_gl_widget_finalize;
|
2014-12-18 06:00:30 +00:00
|
|
|
gl_widget_klass->render = gtk_gst_gl_widget_render;
|
2015-07-16 18:30:42 +00:00
|
|
|
base_widget_klass->reset = _reset;
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-07-16 18:30:42 +00:00
|
|
|
gtk_gst_gl_widget_init (GtkGstGLWidget * gst_widget)
|
2014-12-18 06:00:30 +00:00
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
GdkDisplay *display;
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv;
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gtk_gst_base_widget_init (base_widget);
|
2015-06-12 05:17:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_widget->priv = priv = GTK_GST_GL_WIDGET_GET_PRIVATE (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
display = gdk_display_get_default ();
|
|
|
|
|
|
|
|
#if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
|
|
|
|
if (GDK_IS_X11_DISPLAY (display))
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->display = (GstGLDisplay *)
|
2014-12-18 06:00:30 +00:00
|
|
|
gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
|
|
|
|
(display));
|
|
|
|
#endif
|
2015-06-15 04:33:08 +00:00
|
|
|
#if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
|
|
|
|
if (GDK_IS_WAYLAND_DISPLAY (display)) {
|
|
|
|
struct wl_display *wayland_display =
|
|
|
|
gdk_wayland_display_get_wl_display (display);
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->display = (GstGLDisplay *)
|
2015-06-15 04:33:08 +00:00
|
|
|
gst_gl_display_wayland_new_with_display (wayland_display);
|
|
|
|
}
|
|
|
|
#endif
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-06-15 04:35:35 +00:00
|
|
|
(void) display;
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (!priv->display)
|
|
|
|
priv->display = gst_gl_display_new ();
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gtk_gl_area_set_has_alpha (GTK_GL_AREA (gst_widget),
|
|
|
|
!base_widget->ignore_alpha);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
_get_gl_context (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = gst_widget->priv;
|
2014-12-18 06:00:30 +00:00
|
|
|
GstGLPlatform platform;
|
|
|
|
GstGLAPI gl_api;
|
|
|
|
guintptr gl_handle;
|
|
|
|
|
|
|
|
gtk_widget_realize (GTK_WIDGET (gst_widget));
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->gdk_context)
|
|
|
|
g_object_unref (priv->gdk_context);
|
|
|
|
priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
|
|
|
|
if (priv->gdk_context == NULL) {
|
2014-12-18 06:00:30 +00:00
|
|
|
g_assert_not_reached ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
g_object_ref (priv->gdk_context);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gdk_gl_context_make_current (priv->gdk_context);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
|
|
|
#if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
|
2015-07-16 18:30:42 +00:00
|
|
|
if (GST_IS_GL_DISPLAY_X11 (priv->display)) {
|
2014-12-18 06:00:30 +00:00
|
|
|
platform = GST_GL_PLATFORM_GLX;
|
|
|
|
gl_api = gst_gl_context_get_current_gl_api (NULL, NULL);
|
|
|
|
gl_handle = gst_gl_context_get_current_gl_context (platform);
|
|
|
|
if (gl_handle)
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->other_context =
|
|
|
|
gst_gl_context_new_wrapped (priv->display, gl_handle,
|
2014-12-18 06:00:30 +00:00
|
|
|
platform, gl_api);
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-15 04:33:08 +00:00
|
|
|
#if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
|
2015-07-16 18:30:42 +00:00
|
|
|
if (GST_IS_GL_DISPLAY_WAYLAND (priv->display)) {
|
2015-06-15 04:33:08 +00:00
|
|
|
platform = GST_GL_PLATFORM_EGL;
|
|
|
|
gl_api = gst_gl_context_get_current_gl_api (NULL, NULL);
|
|
|
|
gl_handle = gst_gl_context_get_current_gl_context (platform);
|
|
|
|
if (gl_handle)
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->other_context =
|
|
|
|
gst_gl_context_new_wrapped (priv->display, gl_handle,
|
2015-06-15 04:33:08 +00:00
|
|
|
platform, gl_api);
|
|
|
|
}
|
|
|
|
#endif
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-06-15 04:35:35 +00:00
|
|
|
(void) platform;
|
|
|
|
(void) gl_api;
|
|
|
|
(void) gl_handle;
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->other_context) {
|
2014-12-18 06:00:30 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_context_activate (priv->other_context, TRUE);
|
|
|
|
if (!gst_gl_context_fill_info (priv->other_context, &error)) {
|
2014-12-18 06:00:30 +00:00
|
|
|
GST_ERROR ("failed to retreive gdk context info: %s", error->message);
|
2015-07-16 18:30:42 +00:00
|
|
|
g_object_unref (priv->other_context);
|
|
|
|
priv->other_context = NULL;
|
2014-12-18 06:00:30 +00:00
|
|
|
} else {
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_context_activate (priv->other_context, FALSE);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkWidget *
|
|
|
|
gtk_gst_gl_widget_new (void)
|
2014-12-18 06:00:30 +00:00
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2015-07-16 18:30:42 +00:00
|
|
|
gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * gst_widget)
|
2014-12-18 06:00:30 +00:00
|
|
|
{
|
2015-07-16 18:30:42 +00:00
|
|
|
GtkGstGLWidgetPrivate *priv = gst_widget->priv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (gst_widget), FALSE);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
GTK_GST_BASE_WIDGET_LOCK (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (priv->display && priv->gdk_context && priv->other_context) {
|
|
|
|
GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (!priv->other_context) {
|
|
|
|
GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
|
|
|
|
_invoke_on_main ((ThreadFunc) _get_gl_context, gst_widget);
|
|
|
|
GTK_GST_BASE_WIDGET_LOCK (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (!GST_GL_IS_CONTEXT (priv->other_context)) {
|
|
|
|
GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
priv->context = gst_gl_context_new (priv->display);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
if (!priv->context) {
|
|
|
|
GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
gst_gl_context_create (priv->context, priv->other_context, NULL);
|
2014-12-18 06:00:30 +00:00
|
|
|
|
2015-07-16 18:30:42 +00:00
|
|
|
GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
|
2014-12-18 06:00:30 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstGLContext *
|
|
|
|
gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
|
|
|
if (!gst_widget->priv->other_context)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gst_object_ref (gst_widget->priv->other_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
GstGLContext *
|
|
|
|
gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
|
|
|
if (!gst_widget->priv->context)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gst_object_ref (gst_widget->priv->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
GstGLDisplay *
|
|
|
|
gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
|
|
|
|
{
|
|
|
|
if (!gst_widget->priv->display)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gst_object_ref (gst_widget->priv->display);
|
|
|
|
}
|