gl: propogate other-context using GstContext

This commit is contained in:
Matthew Waters 2014-08-14 23:29:00 +10:00 committed by Tim-Philipp Müller
parent 3d7a6c8db6
commit 62e3c40d97
6 changed files with 125 additions and 121 deletions

View file

@ -173,8 +173,7 @@ enum
ARG_DISPLAY,
PROP_FORCE_ASPECT_RATIO,
PROP_PIXEL_ASPECT_RATIO,
PROP_CONTEXT,
PROP_OTHER_CONTEXT
PROP_CONTEXT
};
enum
@ -277,12 +276,6 @@ gst_glimage_sink_class_init (GstGLImageSinkClass * klass)
"The pixel aspect ratio of the device", 0, 1, G_MAXINT, 1, 1, 1,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
g_param_spec_object ("other-context",
"External OpenGL context",
"Give an external OpenGL context with which to share textures",
GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_CONTEXT,
g_param_spec_object ("context",
"OpenGL context",
@ -393,13 +386,6 @@ gst_glimage_sink_set_property (GObject * object, guint prop_id,
glimage_sink->par_d = gst_value_get_fraction_denominator (value);
break;
}
case PROP_OTHER_CONTEXT:
{
if (glimage_sink->other_context)
gst_object_unref (glimage_sink->other_context);
glimage_sink->other_context = g_value_dup_object (value);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -451,9 +437,6 @@ gst_glimage_sink_get_property (GObject * object, guint prop_id,
case PROP_CONTEXT:
g_value_set_object (value, glimage_sink->context);
break;
case PROP_OTHER_CONTEXT:
g_value_set_object (value, glimage_sink->other_context);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -485,7 +468,7 @@ _ensure_gl_setup (GstGLImageSink * gl_sink)
{
GError *error = NULL;
if (!gst_gl_ensure_display (gl_sink, &gl_sink->display))
if (!gst_gl_ensure_element_data (gl_sink, &gl_sink->display, &gl_sink->other_context))
return FALSE;
if (!gl_sink->context) {
@ -558,7 +541,7 @@ gst_glimage_sink_query (GstBaseSink * bsink, GstQuery * query)
case GST_QUERY_CONTEXT:
{
return gst_gl_handle_context_query ((GstElement *) glimage_sink, query,
&glimage_sink->display);
&glimage_sink->display, &glimage_sink->other_context);
}
default:
res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
@ -601,7 +584,7 @@ gst_glimage_sink_set_context (GstElement * element, GstContext * context)
{
GstGLImageSink *gl_sink = GST_GLIMAGE_SINK (element);
gst_gl_handle_set_context (element, context, &gl_sink->display);
gst_gl_handle_set_context (element, context, &gl_sink->display, &gl_sink->other_context);
}
static GstStateChangeReturn
@ -622,7 +605,7 @@ gst_glimage_sink_change_state (GstElement * element, GstStateChange transition)
case GST_STATE_CHANGE_READY_TO_PAUSED:
g_atomic_int_set (&glimage_sink->to_quit, 0);
if (!glimage_sink->display) {
if (!gst_gl_ensure_display (glimage_sink, &glimage_sink->display))
if (!gst_gl_ensure_element_data (glimage_sink, &glimage_sink->display, &glimage_sink->other_context))
return GST_STATE_CHANGE_FAILURE;
}
break;

View file

@ -183,7 +183,7 @@ gst_gl_mixer_propose_allocation (GstGLMixer * mix,
gst_structure_free (config);
}
if (!gst_gl_ensure_display (mix, &mix->display))
if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
return FALSE;
if (!mix->context) {
@ -308,7 +308,7 @@ gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
case GST_QUERY_CONTEXT:
{
ret = gst_gl_handle_context_query ((GstElement *) mix, query,
&mix->display);
&mix->display, &mix->other_context);
break;
}
default:
@ -333,8 +333,7 @@ enum
enum
{
PROP_0,
PROP_OTHER_CONTEXT
PROP_0
};
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
@ -410,12 +409,6 @@ gst_gl_mixer_class_init (GstGLMixerClass * klass)
gobject_class->get_property = gst_gl_mixer_get_property;
gobject_class->set_property = gst_gl_mixer_set_property;
g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
g_param_spec_object ("other-context",
"External OpenGL context",
"Give an external OpenGL context with which to share textures",
GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&src_factory));
gst_element_class_add_pad_template (element_class,
@ -487,7 +480,7 @@ gst_gl_mixer_set_context (GstElement * element, GstContext * context)
{
GstGLMixer *mix = GST_GL_MIXER (element);
gst_gl_handle_set_context (element, context, &mix->display);
gst_gl_handle_set_context (element, context, &mix->display, &mix->context);
}
static gboolean
@ -496,7 +489,7 @@ gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
gboolean result = TRUE;
if (active) {
if (!gst_gl_ensure_display (mix, &mix->display))
if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
result = FALSE;
}
@ -665,7 +658,7 @@ gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
case GST_QUERY_CONTEXT:
{
res = gst_gl_handle_context_query ((GstElement *) mix, query,
&mix->display);
&mix->display, &mix->other_context);
break;
}
case GST_QUERY_CAPS:
@ -712,7 +705,7 @@ gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
GstGLContext *other_context = NULL;
GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
if (!gst_gl_ensure_display (mix, &mix->display))
if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
return FALSE;
if (gst_query_find_allocation_meta (query,
@ -1121,12 +1114,7 @@ static void
gst_gl_mixer_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
{
GstGLMixer *mix = GST_GL_MIXER (object);
switch (prop_id) {
case PROP_OTHER_CONTEXT:
g_value_set_object (value, mix->other_context);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -1137,16 +1125,7 @@ static void
gst_gl_mixer_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec)
{
GstGLMixer *mix = GST_GL_MIXER (object);
switch (prop_id) {
case PROP_OTHER_CONTEXT:
{
if (mix->other_context)
gst_object_unref (mix->other_context);
mix->other_context = g_value_dup_object (value);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;

View file

@ -59,7 +59,6 @@ GST_DEBUG_CATEGORY_STATIC (gl_test_src_debug);
enum
{
PROP_0,
PROP_OTHER_CONTEXT,
PROP_PATTERN,
PROP_TIMESTAMP_OFFSET,
PROP_IS_LIVE
@ -165,11 +164,6 @@ gst_gl_test_src_class_init (GstGLTestSrcClass * klass)
gobject_class->get_property = gst_gl_test_src_get_property;
gobject_class->dispose = gst_gl_test_src_dispose;
g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
g_param_spec_object ("other-context",
"External OpenGL context",
"Give an external OpenGL context with which to share textures",
GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_PATTERN,
g_param_spec_enum ("pattern", "Pattern",
"Type of test pattern to generate", GST_TYPE_GL_TEST_SRC_PATTERN,
@ -415,11 +409,6 @@ gst_gl_test_src_set_property (GObject * object, guint prop_id,
GstGLTestSrc *src = GST_GL_TEST_SRC (object);
switch (prop_id) {
case PROP_OTHER_CONTEXT:
if (src->other_context)
gst_object_unref (src->other_context);
src->other_context = g_value_dup_object (value);
break;
case PROP_PATTERN:
gst_gl_test_src_set_pattern (src, g_value_get_enum (value));
break;
@ -441,9 +430,6 @@ gst_gl_test_src_get_property (GObject * object, guint prop_id,
GstGLTestSrc *src = GST_GL_TEST_SRC (object);
switch (prop_id) {
case PROP_OTHER_CONTEXT:
g_value_set_object (value, src->other_context);
break;
case PROP_PATTERN:
g_value_set_enum (value, src->pattern_type);
break;
@ -488,7 +474,7 @@ gst_gl_test_src_set_context (GstElement * element, GstContext * context)
{
GstGLTestSrc *src = GST_GL_TEST_SRC (element);
gst_gl_handle_set_context (element, context, &src->display);
gst_gl_handle_set_context (element, context, &src->display, &src->other_context);
}
static gboolean
@ -503,7 +489,7 @@ gst_gl_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
case GST_QUERY_CONTEXT:
{
res = gst_gl_handle_context_query ((GstElement *) src, query,
&src->display);
&src->display, &src->other_context);
break;
}
case GST_QUERY_CONVERT:
@ -712,7 +698,7 @@ gst_gl_test_src_start (GstBaseSrc * basesrc)
{
GstGLTestSrc *src = GST_GL_TEST_SRC (basesrc);
if (!gst_gl_ensure_display (src, &src->display))
if (!gst_gl_ensure_element_data (src, &src->display, &src->other_context))
return FALSE;
src->running_time = 0;
@ -771,7 +757,7 @@ gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
guint out_width, out_height;
GstGLContext *other_context = NULL;
if (!gst_gl_ensure_display (src, &src->display))
if (!gst_gl_ensure_element_data (src, &src->display, &src->other_context))
return FALSE;
if (gst_query_find_allocation_meta (query,

View file

@ -71,8 +71,7 @@ static GstStaticPadTemplate gst_gl_filter_sink_pad_template =
/* Properties */
enum
{
PROP_0,
PROP_OTHER_CONTEXT
PROP_0
};
#define DEBUG_INIT \
@ -140,12 +139,6 @@ gst_gl_filter_class_init (GstGLFilterClass * klass)
element_class->set_context = gst_gl_filter_set_context;
g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
g_param_spec_object ("other-context",
"External OpenGL context",
"Give an external OpenGL context with which to share textures",
GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_gl_filter_src_pad_template));
gst_element_class_add_pad_template (element_class,
@ -172,16 +165,7 @@ static void
gst_gl_filter_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstGLFilter *filter = GST_GL_FILTER (object);
switch (prop_id) {
case PROP_OTHER_CONTEXT:
{
if (filter->other_context)
gst_object_unref (filter->other_context);
filter->other_context = g_value_dup_object (value);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -192,12 +176,7 @@ static void
gst_gl_filter_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstGLFilter *filter = GST_GL_FILTER (object);
switch (prop_id) {
case PROP_OTHER_CONTEXT:
g_value_set_object (value, filter->other_context);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -209,7 +188,8 @@ gst_gl_filter_set_context (GstElement * element, GstContext * context)
{
GstGLFilter *filter = GST_GL_FILTER (element);
gst_gl_handle_set_context (element, context, &filter->display);
gst_gl_handle_set_context (element, context, &filter->display,
&filter->other_context);
}
static gboolean
@ -231,7 +211,7 @@ gst_gl_filter_query (GstBaseTransform * trans, GstPadDirection direction,
case GST_QUERY_CONTEXT:
{
return gst_gl_handle_context_query ((GstElement *) filter, query,
&filter->display);
&filter->display, &filter->other_context);
}
default:
break;
@ -314,7 +294,7 @@ gst_gl_filter_start (GstBaseTransform * bt)
GstGLFilter *filter = GST_GL_FILTER (bt);
GstGLFilterClass *filter_class = GST_GL_FILTER_GET_CLASS (filter);
if (!gst_gl_ensure_display (filter, &filter->display))
if (!gst_gl_ensure_element_data (filter, &filter->display, &filter->other_context))
return FALSE;
if (filter_class->onStart)
@ -894,7 +874,7 @@ gst_gl_filter_propose_allocation (GstBaseTransform * trans,
gst_structure_free (config);
}
if (!gst_gl_ensure_display (filter, &filter->display))
if (!gst_gl_ensure_element_data (filter, &filter->display, &filter->other_context))
return FALSE;
if (!filter->context) {
@ -1015,7 +995,7 @@ gst_gl_filter_decide_allocation (GstBaseTransform * trans, GstQuery * query)
guint in_width, in_height, out_width, out_height;
GstGLContext *other_context = NULL;
if (!gst_gl_ensure_display (filter, &filter->display))
if (!gst_gl_ensure_element_data (filter, &filter->display, &filter->other_context))
return FALSE;
if (gst_query_find_allocation_meta (query,
@ -1262,7 +1242,7 @@ gst_gl_filter_transform (GstBaseTransform * bt, GstBuffer * inbuf,
filter = GST_GL_FILTER (bt);
filter_class = GST_GL_FILTER_GET_CLASS (bt);
if (!gst_gl_ensure_display (filter, &filter->display))
if (!gst_gl_ensure_element_data (filter, &filter->display, &filter->other_context))
return GST_FLOW_NOT_NEGOTIATED;
if (!filter->upload) {

View file

@ -592,8 +592,8 @@ run_context_query (GstElement * element, GstQuery * query,
}
static GstQuery *
_gst_gl_display_context_query (GstElement * element,
GstGLDisplay ** display_ptr, const gchar * display_type)
_gst_context_query (GstElement * element,
gpointer ptr, const gchar * display_type)
{
GstQuery *query;
GstContext *ctxt;
@ -637,8 +637,7 @@ _gst_gl_display_context_query (GstElement * element,
}
static void
gst_gl_display_context_prepare (GstElement * element,
GstGLDisplay ** display_ptr)
gst_gl_display_context_query (GstElement * element, GstGLDisplay ** display_ptr)
{
GstContext *ctxt;
GstQuery *query;
@ -649,8 +648,7 @@ gst_gl_display_context_prepare (GstElement * element,
#endif
query =
_gst_gl_display_context_query (element, display_ptr,
GST_GL_DISPLAY_CONTEXT_TYPE);
_gst_context_query (element, display_ptr, GST_GL_DISPLAY_CONTEXT_TYPE);
gst_query_parse_context (query, &ctxt);
if (ctxt && gst_context_has_context_type (ctxt, GST_GL_DISPLAY_CONTEXT_TYPE)) {
gst_context_get_gl_display (ctxt, display_ptr);
@ -659,9 +657,7 @@ gst_gl_display_context_prepare (GstElement * element,
}
#if GST_GL_HAVE_WINDOW_X11
gst_query_unref (query);
query =
_gst_gl_display_context_query (element, display_ptr,
"gst.x11.display.handle");
query = _gst_context_query (element, display_ptr, "gst.x11.display.handle");
gst_query_parse_context (query, &ctxt);
if (ctxt && gst_context_has_context_type (ctxt, "gst.x11.display.handle")) {
const GstStructure *s;
@ -681,6 +677,27 @@ out:
gst_query_unref (query);
}
static void
gst_gl_context_query (GstElement * element, GstGLContext ** context_ptr)
{
GstContext *ctxt;
GstQuery *query;
#ifndef GST_DISABLE_GST_DEBUG
if (!GST_CAT_CONTEXT)
GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
#endif
query = _gst_context_query (element, context_ptr, "gst.gl.app_context");
gst_query_parse_context (query, &ctxt);
if (ctxt && gst_context_has_context_type (ctxt, "gst.gl.app_context")) {
const GstStructure *s = gst_context_get_structure (ctxt);
gst_structure_get (s, "context", GST_GL_TYPE_CONTEXT, context_ptr, NULL);
}
gst_query_unref (query);
}
/* 4) Create a context by itself and post a GST_MESSAGE_HAVE_CONTEXT
* message.
*/
@ -705,25 +722,27 @@ gst_gl_display_context_propagate (GstElement * element, GstGLDisplay * display)
}
gboolean
gst_gl_ensure_display (gpointer element, GstGLDisplay ** display_ptr)
gst_gl_ensure_element_data (gpointer element, GstGLDisplay ** display_ptr,
GstGLContext ** context_ptr)
{
GstGLDisplay *display;
g_return_val_if_fail (element != NULL, FALSE);
g_return_val_if_fail (display_ptr != NULL, FALSE);
g_return_val_if_fail (context_ptr != NULL, FALSE);
/* 1) Check if the element already has a context of the specific
* type.
*/
display = *display_ptr;
if (gst_gl_display_found (element, display))
return TRUE;
goto get_gl_context;
gst_gl_display_context_prepare (element, display_ptr);
gst_gl_display_context_query (element, display_ptr);
/* Neighbour found and it updated the display */
if (gst_gl_display_found (element, *display_ptr))
return TRUE;
goto get_gl_context;
/* If no neighboor, or application not interested, use system default */
display = gst_gl_display_new ();
@ -732,17 +751,26 @@ gst_gl_ensure_display (gpointer element, GstGLDisplay ** display_ptr)
gst_gl_display_context_propagate (element, display);
return display != NULL;
get_gl_context:
if (*context_ptr)
goto done;
gst_gl_context_query (element, context_ptr);
done:
return *display_ptr != NULL;
}
gboolean
gst_gl_handle_set_context (GstElement * element, GstContext * context,
GstGLDisplay ** display)
GstGLDisplay ** display, GstGLContext ** other_context)
{
GstGLDisplay *replacement = NULL;
GstGLDisplay *display_replacement = NULL;
GstGLContext *context_replacement = NULL;
const gchar *context_type;
g_return_val_if_fail (display, FALSE);
g_return_val_if_fail (display != NULL, FALSE);
g_return_val_if_fail (other_context != NULL, FALSE);
if (!context)
return FALSE;
@ -750,7 +778,7 @@ gst_gl_handle_set_context (GstElement * element, GstContext * context,
context_type = gst_context_get_context_type (context);
if (g_strcmp0 (context_type, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
if (!gst_context_get_gl_display (context, &replacement)) {
if (!gst_context_get_gl_display (context, &display_replacement)) {
GST_WARNING_OBJECT (element, "Failed to get display from context");
return FALSE;
}
@ -762,14 +790,43 @@ gst_gl_handle_set_context (GstElement * element, GstContext * context,
s = gst_context_get_structure (context);
if (gst_structure_get (s, "display", G_TYPE_POINTER, &display, NULL))
replacement =
display_replacement =
(GstGLDisplay *) gst_gl_display_x11_new_with_display (display);
}
#endif
else if (g_strcmp0 (context_type, "gst.gl.app_context") == 0) {
const GstStructure *s = gst_context_get_structure (context);
GstGLDisplay *context_display;
GstGLDisplay *element_display;
if (replacement) {
if (gst_structure_get (s, "context", GST_GL_TYPE_CONTEXT,
&context_replacement, NULL)) {
context_display = gst_gl_context_get_display (context_replacement);
element_display = display_replacement ? display_replacement : *display;
if (element_display
&& (gst_gl_display_get_handle_type (element_display) &
gst_gl_display_get_handle_type (context_display)) == 0) {
GST_ELEMENT_WARNING (element, LIBRARY, SETTINGS, ("%s",
"Cannot set a GL context with a different display type"), ("%s",
"Cannot set a GL context with a different display type"));
gst_object_unref (context_replacement);
context_replacement = NULL;
}
gst_object_unref (context_display);
}
}
if (display_replacement) {
GstGLDisplay *old = *display;
*display = replacement;
*display = display_replacement;
if (old)
gst_object_unref (old);
}
if (context_replacement) {
GstGLContext *old = *other_context;
*other_context = context_replacement;
if (old)
gst_object_unref (old);
@ -780,7 +837,7 @@ gst_gl_handle_set_context (GstElement * element, GstContext * context,
gboolean
gst_gl_handle_context_query (GstElement * element, GstQuery * query,
GstGLDisplay ** display)
GstGLDisplay ** display, GstGLContext ** other_context)
{
gboolean res = FALSE;
const gchar *context_type;
@ -789,6 +846,7 @@ gst_gl_handle_context_query (GstElement * element, GstQuery * query,
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);
gst_query_parse_context_type (query, &context_type);
@ -833,6 +891,23 @@ gst_gl_handle_context_query (GstElement * element, GstQuery * query,
res = x11_display != NULL;
}
#endif
else 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", TRUE);
s = gst_context_writable_structure (context);
gst_structure_set (s, "context", GST_GL_TYPE_CONTEXT, *other_context, NULL);
gst_query_set_context (query, context);
gst_context_unref (context);
res = *other_context != NULL;
}
return res;
}

View file

@ -98,11 +98,12 @@ gboolean gst_gl_context_check_framebuffer_status (GstGLContext * context);
void gst_gl_context_set_error (GstGLContext * context, const char * format, ...);
gchar *gst_gl_context_get_error (void);
gboolean gst_gl_ensure_display (gpointer element, GstGLDisplay **display_ptr);
gboolean gst_gl_ensure_element_data (gpointer element,
GstGLDisplay **display_ptr, GstGLContext ** other_context_ptr);
gboolean gst_gl_handle_set_context (GstElement * element, GstContext * context,
GstGLDisplay ** display);
GstGLDisplay ** display, GstGLContext ** other_context);
gboolean gst_gl_handle_context_query (GstElement * element, GstQuery * query,
GstGLDisplay ** display);
GstGLDisplay ** display, GstGLContext ** other_context);
G_END_DECLS