gl/utils: also take care of the local GL context in query functions
authorMatthew Waters <matthew@centricular.com>
Thu, 12 Jan 2017 10:35:25 +0000 (21:35 +1100)
committerMatthew Waters <matthew@centricular.com>
Fri, 13 Jan 2017 00:20:51 +0000 (11:20 +1100)
Simplifies a deduplicates a lot of code in elements retrieving/setting
the local OpenGL context.

13 files changed:
docs/libs/gst-plugins-bad-libs-sections.txt
ext/gl/gstglbasemixer.c
ext/gl/gstglimagesink.c
ext/gl/gstglstereosplit.c
ext/gl/gstgltestsrc.c
ext/gl/gstglutils.h
ext/gtk/gstgtkglsink.c
ext/qt/gstqtsink.cc
ext/qt/gstqtsrc.cc
gst-libs/gst/gl/gstglbasefilter.c
gst-libs/gst/gl/gstglutils.c
gst-libs/gst/gl/gstglutils.h
gst-libs/gst/gl/gstglutils_private.h [new file with mode: 0644]

index d19b436..f9d3913 100644 (file)
@@ -1621,7 +1621,6 @@ gst_gl_context_check_framebuffer_status
 gst_gl_ensure_element_data
 gst_gl_get_plane_data_size
 gst_gl_get_plane_start
-gst_gl_run_query
 gst_gl_value_get_texture_target_mask
 gst_gl_value_set_texture_target
 gst_gl_value_set_texture_target_from_mask
index 2611f3d..6e6e949 100644 (file)
@@ -145,39 +145,12 @@ gst_gl_base_mixer_sink_event (GstAggregator * agg, GstAggregatorPad * bpad,
 static gboolean
 _find_local_gl_context (GstGLBaseMixer * mix)
 {
-  GstQuery *query;
-  GstContext *context;
-  const GstStructure *s;
-
-  if (mix->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (mix), GST_PAD_SRC,
+          &mix->context))
     return TRUE;
-
-  query = gst_query_new_context ("gst.gl.local_context");
-  if (!mix->context && gst_gl_run_query (GST_ELEMENT (mix), query, GST_PAD_SRC)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &mix->context,
-          NULL);
-    }
-  }
-  if (!mix->context
-      && gst_gl_run_query (GST_ELEMENT (mix), query, GST_PAD_SINK)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &mix->context,
-          NULL);
-    }
-  }
-
-  GST_DEBUG_OBJECT (mix, "found local context %p", mix->context);
-
-  gst_query_unref (query);
-
-  if (mix->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (mix), GST_PAD_SINK,
+          &mix->context))
     return TRUE;
-
   return FALSE;
 }
 
@@ -296,39 +269,9 @@ gst_gl_base_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
     }
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-
-      ret = gst_gl_handle_context_query ((GstElement *) mix, query,
-          &mix->display, &mix->priv->other_context);
-      if (mix->display)
-        gst_gl_display_filter_gl_api (mix->display,
-            mix_class->supported_gl_api);
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, mix->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        ret = mix->context != NULL;
-      }
-      GST_LOG_OBJECT (mix, "context query of type %s %i", context_type, ret);
-
-      if (ret)
-        return ret;
-
+      if (gst_gl_handle_context_query ((GstElement *) mix, query,
+              mix->display, mix->context, mix->priv->other_context))
+        return TRUE;
       break;
     }
     default:
@@ -508,46 +451,14 @@ gst_gl_base_mixer_src_activate_mode (GstAggregator * aggregator,
 static gboolean
 gst_gl_base_mixer_src_query (GstAggregator * agg, GstQuery * query)
 {
-  gboolean res = FALSE;
   GstGLBaseMixer *mix = GST_GL_BASE_MIXER (agg);
-  GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_GET_CLASS (mix);
 
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-
-      res = gst_gl_handle_context_query ((GstElement *) mix, query,
-          &mix->display, &mix->priv->other_context);
-      if (mix->display)
-        gst_gl_display_filter_gl_api (mix->display,
-            mix_class->supported_gl_api);
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, mix->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        res = mix->context != NULL;
-      }
-      GST_LOG_OBJECT (mix, "context query of type %s %i", context_type, res);
-
-      if (res)
-        return res;
-
+      if (gst_gl_handle_context_query ((GstElement *) mix, query,
+              mix->display, mix->context, mix->priv->other_context))
+        return TRUE;
       break;
     }
     default:
index 15ab284..79ef08e 100644 (file)
@@ -1080,39 +1080,10 @@ gst_glimage_sink_query (GstBaseSink * bsink, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-
-      res = gst_gl_handle_context_query ((GstElement *) glimage_sink, query,
-          &glimage_sink->display, &glimage_sink->other_context);
-      if (glimage_sink->display)
-        gst_gl_display_filter_gl_api (glimage_sink->display, SUPPORTED_GL_APIS);
-
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT,
-            glimage_sink->context, NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        res = glimage_sink->context != NULL;
-      }
-      GST_LOG_OBJECT (glimage_sink, "context query of type %s %i", context_type,
-          res);
-
-      if (res)
-        return res;
+      if (gst_gl_handle_context_query ((GstElement *) glimage_sink, query,
+              glimage_sink->display, glimage_sink->context,
+              glimage_sink->other_context))
+        return TRUE;
       break;
     }
     case GST_QUERY_DRAIN:
index 38836ad..5650ab4 100644 (file)
@@ -247,8 +247,8 @@ strip_mview_fields (GstCaps * incaps, GstVideoMultiviewFlags keep_flags)
     GstVideoMultiviewFlags flags, mask;
 
     gst_structure_remove_field (st, "multiview-mode");
-    if (gst_structure_get_flagset (st, "multiview-flags", (guint*) &flags,
-        (guint*) &mask)) {
+    if (gst_structure_get_flagset (st, "multiview-flags", (guint *) & flags,
+            (guint *) & mask)) {
       flags &= keep_flags;
       mask = keep_flags;
       gst_structure_set (st, "multiview-flags",
@@ -414,40 +414,12 @@ fail:
 static gboolean
 _find_local_gl_context (GstGLStereoSplit * split)
 {
-  GstQuery *query;
-  GstContext *context;
-  const GstStructure *s;
-
-  if (split->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (split), GST_PAD_SRC,
+          &split->context))
     return TRUE;
-
-  query = gst_query_new_context ("gst.gl.local_context");
-  if (!split->context
-      && gst_gl_run_query (GST_ELEMENT (split), query, GST_PAD_SRC)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &split->context,
-          NULL);
-    }
-  }
-  if (!split->context
-      && gst_gl_run_query (GST_ELEMENT (split), query, GST_PAD_SINK)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &split->context,
-          NULL);
-    }
-  }
-
-  GST_DEBUG_OBJECT (split, "found local context %p", split->context);
-
-  gst_query_unref (query);
-
-  if (split->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (split), GST_PAD_SINK,
+          &split->context))
     return TRUE;
-
   return FALSE;
 }
 
@@ -626,38 +598,9 @@ stereosplit_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-      gboolean ret;
-
-      ret = gst_gl_handle_context_query ((GstElement *) split, query,
-          &split->display, &split->other_context);
-      if (split->display)
-        gst_gl_display_filter_gl_api (split->display, SUPPORTED_GL_APIS);
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, split->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        ret = split->context != NULL;
-      }
-      GST_LOG_OBJECT (split, "context query of type %s %i", context_type, ret);
-
-      if (ret)
-        return ret;
+      if (gst_gl_handle_context_query ((GstElement *) split, query,
+              split->display, split->context, split->other_context))
+        return TRUE;
 
       return gst_pad_query_default (pad, parent, query);
     }
@@ -684,38 +627,9 @@ stereosplit_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-      gboolean ret;
-
-      ret = gst_gl_handle_context_query ((GstElement *) split, query,
-          &split->display, &split->other_context);
-      if (split->display)
-        gst_gl_display_filter_gl_api (split->display, SUPPORTED_GL_APIS);
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, split->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        ret = split->context != NULL;
-      }
-      GST_LOG_OBJECT (split, "context query of type %s %i", context_type, ret);
-
-      if (ret)
-        return ret;
+      if (gst_gl_handle_context_query ((GstElement *) split, query,
+              split->display, split->context, split->other_context))
+        return TRUE;
 
       return gst_pad_query_default (pad, parent, query);
     }
index 11ecc1d..51c709d 100644 (file)
@@ -326,39 +326,9 @@ gst_gl_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-
-      res = gst_gl_handle_context_query ((GstElement *) src, query,
-          &src->display, &src->other_context);
-      if (src->display)
-        gst_gl_display_filter_gl_api (src->display, SUPPORTED_GL_APIS);
-
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, src->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        res = src->context != NULL;
-      }
-      GST_LOG_OBJECT (src, "context query of type %s %i", context_type, res);
-
-      if (res)
-        return res;
-
+      if (gst_gl_handle_context_query ((GstElement *) src, query,
+              src->display, src->context, src->other_context))
+        return TRUE;
       break;
     }
     case GST_QUERY_CONVERT:
@@ -584,39 +554,9 @@ gst_gl_test_src_stop (GstBaseSrc * basesrc)
 static gboolean
 _find_local_gl_context (GstGLTestSrc * src)
 {
-  GstQuery *query;
-  GstContext *context;
-  const GstStructure *s;
-
-  if (src->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (src), GST_PAD_SRC,
+          &src->context))
     return TRUE;
-
-  query = gst_query_new_context ("gst.gl.local_context");
-  if (!src->context && gst_gl_run_query (GST_ELEMENT (src), query, GST_PAD_SRC)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &src->context,
-          NULL);
-    }
-  }
-  if (!src->context
-      && gst_gl_run_query (GST_ELEMENT (src), query, GST_PAD_SINK)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &src->context,
-          NULL);
-    }
-  }
-
-  GST_DEBUG_OBJECT (src, "found local context %p", src->context);
-
-  gst_query_unref (query);
-
-  if (src->context)
-    return TRUE;
-
   return FALSE;
 }
 
index ee8d363..ee7d4c6 100644 (file)
@@ -25,7 +25,6 @@
 
 G_BEGIN_DECLS
 
-GST_EXPORT
 gboolean gst_gl_context_gen_shader (GstGLContext * context,
     const gchar * shader_vertex_source,
     const gchar * shader_fragment_source, GstGLShader ** shader);
index 6843451..2bdd331 100644 (file)
@@ -98,37 +98,9 @@ gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-
-      res = gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
-          &gtk_sink->display, &gtk_sink->gtk_context);
-
-      if (gtk_sink->display)
-        gst_gl_display_filter_gl_api (gtk_sink->display, GST_GL_API_OPENGL3);
-
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, gtk_sink->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        res = gtk_sink->context != NULL;
-      }
-      GST_LOG_OBJECT (gtk_sink, "context query of type %s %i", context_type,
-          res);
+      if (gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
+              gtk_sink->display, gtk_sink->context, gtk_sink->gtk_context))
+        return TRUE;
       break;
     }
     default:
index 70e6858..2253156 100644 (file)
@@ -239,41 +239,9 @@ gst_qt_sink_query (GstBaseSink * bsink, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-      gboolean ret;
-
-      ret = gst_gl_handle_context_query ((GstElement *) qt_sink, query,
-          &qt_sink->display, &qt_sink->qt_context);
-
-      if (qt_sink->display)
-        gst_gl_display_filter_gl_api (qt_sink->display, gst_gl_context_get_gl_api (qt_sink->qt_context));
-
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, qt_sink->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        ret = qt_sink->context != NULL;
-      }
-      GST_LOG_OBJECT (qt_sink, "context query of type %s %i", context_type,
-          ret);
-
-      if (ret)
-        return ret;
+      if (gst_gl_handle_context_query ((GstElement *) qt_sink, query,
+          qt_sink->display, qt_sink->context, qt_sink->qt_context))
+        return TRUE;
 
       /* fallthrough */
     }
index 849018e..518a5f3 100644 (file)
@@ -283,10 +283,6 @@ gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-      gboolean ret;
-
       if (!qt_window_is_scenegraph_initialized (qt_src->window))
         return FALSE;
 
@@ -295,37 +291,9 @@ gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query)
         qt_src->qt_context = qt_window_get_qt_context (qt_src->window);
       }
 
-      ret = gst_gl_handle_context_query ((GstElement *) qt_src, query,
-          &qt_src->display, &qt_src->qt_context);
-
-      if (qt_src->display)
-        gst_gl_display_filter_gl_api (qt_src->display,
-            gst_gl_context_get_gl_api (qt_src->qt_context));
-
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.app_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.app_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT,
-            qt_src->qt_context, NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        ret = qt_src->qt_context != NULL;
-      }
-      GST_LOG_OBJECT (qt_src, "context query of type %s %i", context_type, ret);
-
-      if (ret)
-        return ret;
+      if (gst_gl_handle_context_query ((GstElement *) qt_src, query,
+          qt_src->display, qt_src->context, qt_src->qt_context))
+        return TRUE;
 
       /* fallthrough */
     }
@@ -340,31 +308,9 @@ gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query)
 static gboolean
 _find_local_gl_context (GstQtSrc * qt_src)
 {
-  GstQuery *query;
-  GstContext *context;
-  const GstStructure *s;
-
-  if (qt_src->context)
-    return TRUE;
-
-  query = gst_query_new_context ("gst.gl.local_context");
-  if (!qt_src->context
-      && gst_gl_run_query (GST_ELEMENT (qt_src), query, GST_PAD_SRC)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &qt_src->context,
-          NULL);
-    }
-  }
-
-  GST_DEBUG_OBJECT (qt_src, "found local context %p", qt_src->context);
-
-  gst_query_unref (query);
-
-  if (qt_src->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (qt_src), GST_PAD_SRC,
+      &qt_src->context))
     return TRUE;
-
   return FALSE;
 }
 
index e950d79..de921cf 100644 (file)
@@ -25,6 +25,7 @@
 #include <gst/video/gstvideometa.h>
 
 #include <gst/gl/gl.h>
+#include <gst/gl/gstglutils_private.h>
 
 /**
  * SECTION:gstglbasefilter
@@ -62,8 +63,7 @@ enum
 #define gst_gl_base_filter_parent_class parent_class
 G_DEFINE_TYPE_WITH_CODE (GstGLBaseFilter, gst_gl_base_filter,
     GST_TYPE_BASE_TRANSFORM, GST_DEBUG_CATEGORY_INIT (gst_gl_base_filter_debug,
-        "glbasefilter", 0, "glbasefilter element");
-    );
+        "glbasefilter", 0, "glbasefilter element"););
 
 static void gst_gl_base_filter_finalize (GObject * object);
 static void gst_gl_base_filter_set_property (GObject * object, guint prop_id,
@@ -192,40 +192,12 @@ gst_gl_base_filter_set_context (GstElement * element, GstContext * context)
 static gboolean
 _find_local_gl_context (GstGLBaseFilter * filter)
 {
-  GstQuery *query;
-  GstContext *context;
-  const GstStructure *s;
-
-  if (filter->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (filter), GST_PAD_SRC,
+          &filter->context))
     return TRUE;
-
-  query = gst_query_new_context ("gst.gl.local_context");
-  if (!filter->context
-      && gst_gl_run_query (GST_ELEMENT (filter), query, GST_PAD_SRC)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &filter->context,
-          NULL);
-    }
-  }
-  if (!filter->context
-      && gst_gl_run_query (GST_ELEMENT (filter), query, GST_PAD_SINK)) {
-    gst_query_parse_context (query, &context);
-    if (context) {
-      s = gst_context_get_structure (context);
-      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &filter->context,
-          NULL);
-    }
-  }
-
-  GST_DEBUG_OBJECT (filter, "found local context %p", filter->context);
-
-  gst_query_unref (query);
-
-  if (filter->context)
+  if (gst_gl_query_local_gl_context (GST_ELEMENT (filter), GST_PAD_SINK,
+          &filter->context))
     return TRUE;
-
   return FALSE;
 }
 
@@ -234,7 +206,6 @@ gst_gl_base_filter_query (GstBaseTransform * trans, GstPadDirection direction,
     GstQuery * query)
 {
   GstGLBaseFilter *filter = GST_GL_BASE_FILTER (trans);
-  GstGLBaseFilterClass *filter_class = GST_GL_BASE_FILTER_GET_CLASS (filter);
 
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_ALLOCATION:
@@ -249,40 +220,9 @@ gst_gl_base_filter_query (GstBaseTransform * trans, GstPadDirection direction,
     }
     case GST_QUERY_CONTEXT:
     {
-      const gchar *context_type;
-      GstContext *context, *old_context;
-      gboolean ret;
-
-      ret = gst_gl_handle_context_query ((GstElement *) filter, query,
-          &filter->display, &filter->priv->other_context);
-      if (filter->display)
-        gst_gl_display_filter_gl_api (filter->display,
-            filter_class->supported_gl_api);
-
-      gst_query_parse_context_type (query, &context_type);
-
-      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
-        GstStructure *s;
-
-        gst_query_parse_context (query, &old_context);
-
-        if (old_context)
-          context = gst_context_copy (old_context);
-        else
-          context = gst_context_new ("gst.gl.local_context", FALSE);
-
-        s = gst_context_writable_structure (context);
-        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, filter->context,
-            NULL);
-        gst_query_set_context (query, context);
-        gst_context_unref (context);
-
-        ret = filter->context != NULL;
-      }
-      GST_LOG_OBJECT (filter, "context query of type %s %i", context_type, ret);
-
-      if (ret)
-        return ret;
+      if (gst_gl_handle_context_query ((GstElement *) filter, query,
+              filter->display, filter->context, filter->priv->other_context))
+        return TRUE;
       break;
     }
     default:
index 0b7d55d..b41ed8f 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "gl.h"
 #include "gstglutils.h"
+#include "gstglutils_private.h"
 
 #if GST_GL_HAVE_WINDOW_X11
 #include <gst/gl/x11/gstgldisplay_x11.h>
 #define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
 #define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
 
+#ifndef GST_DISABLE_GST_DEBUG
+GST_DEBUG_CATEGORY_STATIC (gst_gl_utils_debug);
+static GstDebugCategory *
+_init_gl_utils_debug_category (void)
+{
+  static volatile gsize _init = 0;
+
+  if (g_once_init_enter (&_init)) {
+    GST_DEBUG_CATEGORY_INIT (gst_gl_utils_debug, "glutils", 0,
+        "OpenGL Utilities");
+    g_once_init_leave (&_init, 1);
+  }
+
+  return gst_gl_utils_debug;
+}
+
+#define GST_CAT_DEFAULT _init_gl_utils_debug_category()
+#endif
 
 static gboolean
 gst_gl_display_found (GstElement * element, GstGLDisplay * display)
@@ -349,21 +368,24 @@ gst_gl_handle_set_context (GstElement * element, GstContext * context,
 
 gboolean
 gst_gl_handle_context_query (GstElement * element, GstQuery * query,
-    GstGLDisplay ** display, GstGLContext ** other_context)
+    GstGLDisplay * display, GstGLContext * gl_context,
+    GstGLContext * other_context)
 {
-  gboolean res = FALSE;
   const gchar *context_type;
   GstContext *context, *old_context;
 
-  g_return_val_if_fail (element != NULL, FALSE);
-  g_return_val_if_fail (query != NULL, FALSE);
-  g_return_val_if_fail (display != NULL, FALSE);
-  g_return_val_if_fail (other_context != NULL, FALSE);
+  g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
+  g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
+  g_return_val_if_fail (display == NULL || GST_IS_GL_DISPLAY (display), FALSE);
+  g_return_val_if_fail (gl_context == NULL
+      || GST_IS_GL_CONTEXT (gl_context), FALSE);
+  g_return_val_if_fail (other_context == NULL
+      || GST_IS_GL_CONTEXT (other_context), FALSE);
 
+  GST_LOG_OBJECT (element, "handle context query %" GST_PTR_FORMAT, query);
   gst_query_parse_context_type (query, &context_type);
 
-  if (g_strcmp0 (context_type, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
-
+  if (display && g_strcmp0 (context_type, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
     gst_query_parse_context (query, &old_context);
 
     if (old_context)
@@ -371,16 +393,17 @@ gst_gl_handle_context_query (GstElement * element, GstQuery * query,
     else
       context = gst_context_new (GST_GL_DISPLAY_CONTEXT_TYPE, TRUE);
 
-    gst_context_set_gl_display (context, *display);
+    gst_context_set_gl_display (context, display);
     gst_query_set_context (query, context);
     gst_context_unref (context);
+    GST_DEBUG_OBJECT (element, "successfully set %" GST_PTR_FORMAT
+        " on %" GST_PTR_FORMAT, display, query);
 
-    res = *display != NULL;
+    return TRUE;
   }
 #if GST_GL_HAVE_WINDOW_X11
-  else if (g_strcmp0 (context_type, "gst.x11.display.handle") == 0) {
+  else if (display && g_strcmp0 (context_type, "gst.x11.display.handle") == 0) {
     GstStructure *s;
-    Display *x11_display = NULL;
 
     gst_query_parse_context (query, &old_context);
 
@@ -389,24 +412,29 @@ gst_gl_handle_context_query (GstElement * element, GstQuery * query,
     else
       context = gst_context_new ("gst.x11.display.handle", TRUE);
 
-    if (*display
-        && ((*display)->type & GST_GL_DISPLAY_TYPE_X11) ==
-        GST_GL_DISPLAY_TYPE_X11)
-      x11_display = (Display *) gst_gl_display_get_handle (*display);
+    if (gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_X11) {
+      Display *x11_display = (Display *) gst_gl_display_get_handle (display);
 
-    s = gst_context_writable_structure (context);
-    gst_structure_set (s, "display", G_TYPE_POINTER, x11_display, NULL);
+      if (x11_display) {
+        s = gst_context_writable_structure (context);
+        gst_structure_set (s, "display", G_TYPE_POINTER, x11_display, NULL);
 
-    gst_query_set_context (query, context);
-    gst_context_unref (context);
+        gst_query_set_context (query, context);
+        gst_context_unref (context);
 
-    res = x11_display != NULL;
+        GST_DEBUG_OBJECT (element, "successfully set x11 display %p (from %"
+            GST_PTR_FORMAT ") on %" GST_PTR_FORMAT, x11_display, display,
+            query);
+
+        return TRUE;
+      }
+    }
   }
 #endif
 #if GST_GL_HAVE_WINDOW_WAYLAND
-  else if (g_strcmp0 (context_type, "GstWaylandDisplayHandleContextType") == 0) {
+  else if (display
+      && g_strcmp0 (context_type, "GstWaylandDisplayHandleContextType") == 0) {
     GstStructure *s;
-    struct wl_display *wayland_display = NULL;
 
     gst_query_parse_context (query, &old_context);
 
@@ -415,22 +443,27 @@ gst_gl_handle_context_query (GstElement * element, GstQuery * query,
     else
       context = gst_context_new ("GstWaylandDisplayHandleContextType", TRUE);
 
-    if (*display
-        && ((*display)->type & GST_GL_DISPLAY_TYPE_WAYLAND) ==
-        GST_GL_DISPLAY_TYPE_WAYLAND)
-      wayland_display =
-          (struct wl_display *) gst_gl_display_get_handle (*display);
+    if (gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND) {
+      struct wl_display *wayland_display =
+          (struct wl_display *) gst_gl_display_get_handle (display);
 
-    s = gst_context_writable_structure (context);
-    gst_structure_set (s, "display", G_TYPE_POINTER, wayland_display, NULL);
+      if (wayland_display) {
+        s = gst_context_writable_structure (context);
+        gst_structure_set (s, "display", G_TYPE_POINTER, wayland_display, NULL);
 
-    gst_query_set_context (query, context);
-    gst_context_unref (context);
+        gst_query_set_context (query, context);
+        gst_context_unref (context);
+
+        GST_DEBUG_OBJECT (element, "successfully set wayland display %p (from %"
+            GST_PTR_FORMAT ") on %" GST_PTR_FORMAT, wayland_display, display,
+            query);
 
-    res = wayland_display != NULL;
+        return TRUE;
+      }
+    }
   }
 #endif
-  else if (g_strcmp0 (context_type, "gst.gl.app_context") == 0) {
+  else if (other_context && g_strcmp0 (context_type, "gst.gl.app_context") == 0) {
     GstStructure *s;
 
     gst_query_parse_context (query, &old_context);
@@ -441,14 +474,65 @@ gst_gl_handle_context_query (GstElement * element, GstQuery * query,
       context = gst_context_new ("gst.gl.app_context", TRUE);
 
     s = gst_context_writable_structure (context);
-    gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, *other_context, NULL);
+    gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, other_context, NULL);
+    gst_query_set_context (query, context);
+    gst_context_unref (context);
+
+    GST_DEBUG_OBJECT (element, "successfully set application GL context %"
+        GST_PTR_FORMAT " on %" GST_PTR_FORMAT, other_context, query);
+
+    return TRUE;
+  } else if (gl_context
+      && g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
+    GstStructure *s;
+
+    gst_query_parse_context (query, &old_context);
+
+    if (old_context)
+      context = gst_context_copy (old_context);
+    else
+      context = gst_context_new ("gst.gl.local_context", TRUE);
+
+    s = gst_context_writable_structure (context);
+    gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, gl_context, NULL);
     gst_query_set_context (query, context);
     gst_context_unref (context);
 
-    res = *other_context != NULL;
+    GST_DEBUG_OBJECT (element, "successfully set GL context %"
+        GST_PTR_FORMAT " on %" GST_PTR_FORMAT, gl_context, query);
+
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+gboolean
+gst_gl_query_local_gl_context (GstElement * element, GstPadDirection direction,
+    GstGLContext ** context_ptr)
+{
+  GstQuery *query;
+  GstContext *context;
+  const GstStructure *s;
+
+  g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
+  g_return_val_if_fail (context_ptr != NULL, FALSE);
+
+  if (*context_ptr)
+    return TRUE;
+
+  query = gst_query_new_context ("gst.gl.local_context");
+  if (gst_gl_run_query (GST_ELEMENT (element), query, direction)) {
+    gst_query_parse_context (query, &context);
+    if (context) {
+      s = gst_context_get_structure (context);
+      gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, context_ptr, NULL);
+    }
   }
 
-  return res;
+  gst_query_unref (query);
+
+  return *context_ptr != NULL;
 }
 
 gsize
@@ -471,8 +555,6 @@ gst_gl_get_plane_data_size (GstVideoInfo * info, GstVideoAlignment * align,
   return plane_size;
 }
 
-/* find the difference between the start of the plane and where the video
- * data starts in the plane */
 gsize
 gst_gl_get_plane_start (GstVideoInfo * info, GstVideoAlignment * valign,
     guint plane)
index 942371e..e1207aa 100644 (file)
@@ -36,11 +36,11 @@ gboolean gst_gl_handle_set_context (GstElement * element, GstContext * context,
     GstGLDisplay ** display, GstGLContext ** other_context);
 GST_EXPORT
 gboolean gst_gl_handle_context_query (GstElement * element, GstQuery * query, 
-    GstGLDisplay ** display, GstGLContext ** other_context);
-
+    GstGLDisplay * display, GstGLContext * context, GstGLContext * other_context);
 GST_EXPORT
-gboolean gst_gl_run_query (GstElement * element,
-    GstQuery * query, GstPadDirection direction);
+gboolean gst_gl_query_local_gl_context (GstElement * element, GstPadDirection direction,
+    GstGLContext ** context_ptr);
+
 GST_EXPORT
 gsize gst_gl_get_plane_data_size (GstVideoInfo * info, GstVideoAlignment * align,
     guint plane);
diff --git a/gst-libs/gst/gl/gstglutils_private.h b/gst-libs/gst/gl/gstglutils_private.h
new file mode 100644 (file)
index 0000000..9321d8d
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * GStreamer
+ * Copyright (C) 2017 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.
+ */
+
+#ifndef __GST_GL_UTILS_PRIVATE_H__
+#define __GST_GL_UTILS_PRIVATE_H__
+
+#include <gst/gl/gstgl_fwd.h>
+
+G_BEGIN_DECLS
+
+gboolean gst_gl_run_query (GstElement * element, GstQuery * query, GstPadDirection direction);
+
+G_END_DECLS
+
+#endif /* __GST_GL_UTILS_H__ */