gl: store the list of contexts within gldisplay

Removes the reliance on the allocation query to propogate GL contexts.

Allows thread safely getting a context for the a specific thread.
This commit is contained in:
Matthew Waters 2015-03-03 17:26:47 +11:00
parent 2f2470488b
commit ef0bd30c87
6 changed files with 237 additions and 257 deletions

View File

@ -479,8 +479,6 @@ gst_gl_base_mixer_decide_allocation (GstGLBaseMixer * mix, GstQuery * query)
{ {
GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_GET_CLASS (mix); GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_GET_CLASS (mix);
GError *error = NULL; GError *error = NULL;
guint idx;
GstGLContext *other_context = NULL;
gboolean ret = TRUE; gboolean ret = TRUE;
if (!gst_gl_ensure_element_data (mix, &mix->display, if (!gst_gl_ensure_element_data (mix, &mix->display,
@ -491,59 +489,21 @@ gst_gl_base_mixer_decide_allocation (GstGLBaseMixer * mix, GstQuery * query)
_find_local_gl_context (mix); _find_local_gl_context (mix);
if (gst_query_find_allocation_meta (query, if (!mix->context) {
GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) { do {
GstGLContext *context; if (mix->context)
const GstStructure *upload_meta_params; gst_object_unref (mix->context);
gpointer handle; /* just get a GL context. we don't care */
gchar *type; mix->context =
gchar *apis; gst_gl_display_get_gl_context_for_thread (mix->display, NULL);
gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
if (upload_meta_params) {
if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
GstGLContext *old = mix->context;
mix->context = context;
if (old)
gst_object_unref (old);
} else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
&type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
&& handle) {
GstGLPlatform platform;
GstGLAPI gl_apis;
GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
handle, type, apis);
platform = gst_gl_platform_from_string (type);
gl_apis = gst_gl_api_from_string (apis);
if (gl_apis && platform)
other_context =
gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
platform, gl_apis);
}
}
}
if (mix->priv->other_context) {
if (!other_context) {
other_context = mix->priv->other_context;
} else {
GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
("%s", "Cannot share with more than one GL context"),
("%s", "Cannot share with more than one GL context"));
}
}
if (!mix->context) { if (!mix->context) {
mix->context = gst_gl_context_new (mix->display); mix->context = gst_gl_context_new (mix->display);
if (!gst_gl_context_create (mix->context, other_context, &error)) if (!gst_gl_context_create (mix->context, mix->priv->other_context,
&error))
goto context_error; goto context_error;
} }
} while (!gst_gl_display_add_context (mix->display, mix->context));
}
if (mix_class->decide_allocation) if (mix_class->decide_allocation)
ret = mix_class->decide_allocation (mix, query); ret = mix_class->decide_allocation (mix, query);

View File

@ -603,46 +603,6 @@ gst_glimage_sink_mouse_event_cb (GstGLWindow * window, char *event_name,
event_name, button, posx, posy); event_name, button, posx, posy);
} }
static gboolean
_find_local_gl_context (GstGLImageSink * gl_sink)
{
GstQuery *query;
GstContext *context;
const GstStructure *s;
if (gl_sink->context)
return TRUE;
query = gst_query_new_context ("gst.gl.local_context");
if (!gl_sink->context
&& gst_gl_run_query (GST_ELEMENT (gl_sink), query, GST_PAD_SRC)) {
gst_query_parse_context (query, &context);
if (context) {
s = gst_context_get_structure (context);
gst_structure_get (s, "context", GST_GL_TYPE_CONTEXT, &gl_sink->context,
NULL);
}
}
if (!gl_sink->context
&& gst_gl_run_query (GST_ELEMENT (gl_sink), query, GST_PAD_SINK)) {
gst_query_parse_context (query, &context);
if (context) {
s = gst_context_get_structure (context);
gst_structure_get (s, "context", GST_GL_TYPE_CONTEXT, &gl_sink->context,
NULL);
}
}
GST_ERROR_OBJECT (gl_sink, "found local context %p", gl_sink->context);
gst_query_unref (query);
if (gl_sink->context)
return TRUE;
return FALSE;
}
static gboolean static gboolean
_ensure_gl_setup (GstGLImageSink * gl_sink) _ensure_gl_setup (GstGLImageSink * gl_sink)
{ {
@ -657,8 +617,13 @@ _ensure_gl_setup (GstGLImageSink * gl_sink)
gst_gl_display_filter_gl_api (gl_sink->display, SUPPORTED_GL_APIS); gst_gl_display_filter_gl_api (gl_sink->display, SUPPORTED_GL_APIS);
if (!gl_sink->context) { if (!gl_sink->context) {
do {
GstGLContext *other_context;
GstGLWindow *window; GstGLWindow *window;
if (gl_sink->context)
gst_object_unref (gl_sink->context);
GST_DEBUG_OBJECT (gl_sink, GST_DEBUG_OBJECT (gl_sink,
"No current context, creating one for %" GST_PTR_FORMAT, "No current context, creating one for %" GST_PTR_FORMAT,
gl_sink->display); gl_sink->display);
@ -684,12 +649,19 @@ _ensure_gl_setup (GstGLImageSink * gl_sink)
gst_gl_window_set_window_handle (window, gl_sink->window_id); gst_gl_window_set_window_handle (window, gl_sink->window_id);
} }
if (gl_sink->other_context) {
other_context = gst_object_ref (gl_sink->other_context);
} else {
other_context =
gst_gl_display_get_gl_context_for_thread (gl_sink->display, NULL);
}
GST_DEBUG_OBJECT (gl_sink, GST_DEBUG_OBJECT (gl_sink,
"creating context %" GST_PTR_FORMAT " from other context %" "creating context %" GST_PTR_FORMAT " from other context %"
GST_PTR_FORMAT, gl_sink->context, gl_sink->other_context); GST_PTR_FORMAT, gl_sink->context, other_context);
if (!gst_gl_context_create (gl_sink->context, gl_sink->other_context, if (!gst_gl_context_create (gl_sink->context, other_context, &error)) {
&error)) { gst_object_unref (other_context);
gst_object_unref (window); gst_object_unref (window);
goto context_error; goto context_error;
} }
@ -708,15 +680,17 @@ _ensure_gl_setup (GstGLImageSink * gl_sink)
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref); gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gl_sink->key_sig_id = g_signal_connect (window, "key-event", G_CALLBACK gl_sink->key_sig_id = g_signal_connect (window, "key-event", G_CALLBACK
(gst_glimage_sink_key_event_cb), gl_sink); (gst_glimage_sink_key_event_cb), gl_sink);
gl_sink->mouse_sig_id = g_signal_connect (window, "mouse-event", G_CALLBACK gl_sink->mouse_sig_id =
(gst_glimage_sink_mouse_event_cb), gl_sink); g_signal_connect (window, "mouse-event",
G_CALLBACK (gst_glimage_sink_mouse_event_cb), gl_sink);
if (other_context)
gst_object_unref (other_context);
gst_object_unref (window); gst_object_unref (window);
} while (!gst_gl_display_add_context (gl_sink->display, gl_sink->context));
} else } else
GST_DEBUG_OBJECT (gl_sink, "Already have a context"); GST_DEBUG_OBJECT (gl_sink, "Already have a context");
_find_local_gl_context (gl_sink);
return TRUE; return TRUE;
context_creation_error: context_creation_error:
@ -845,6 +819,7 @@ gst_glimage_sink_change_state (GstElement * element, GstStateChange transition)
switch (transition) { switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY: case GST_STATE_CHANGE_NULL_TO_READY:
_ensure_gl_setup (glimage_sink);
break; break;
case GST_STATE_CHANGE_READY_TO_PAUSED: case GST_STATE_CHANGE_READY_TO_PAUSED:
g_atomic_int_set (&glimage_sink->to_quit, 0); g_atomic_int_set (&glimage_sink->to_quit, 0);

View File

@ -803,10 +803,7 @@ gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
guint min, max, size; guint min, max, size;
gboolean update_pool; gboolean update_pool;
GError *error = NULL; GError *error = NULL;
guint idx;
guint out_width, out_height; guint out_width, out_height;
GstGLContext *other_context = NULL;
gboolean same_downstream_gl_context = FALSE;
if (!gst_gl_ensure_element_data (src, &src->display, &src->other_context)) if (!gst_gl_ensure_element_data (src, &src->display, &src->other_context))
return FALSE; return FALSE;
@ -815,60 +812,20 @@ gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
_find_local_gl_context (src); _find_local_gl_context (src);
if (gst_query_find_allocation_meta (query, if (!src->context) {
GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) { do {
GstGLContext *context; if (src->context)
const GstStructure *upload_meta_params; gst_object_unref (src->context);
gpointer handle; /* just get a GL context. we don't care */
gchar *type; src->context =
gchar *apis; gst_gl_display_get_gl_context_for_thread (src->display, NULL);
gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
if (upload_meta_params) {
if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
GstGLContext *old = src->context;
src->context = context;
if (old)
gst_object_unref (old);
same_downstream_gl_context = TRUE;
} else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
&type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
&& handle) {
GstGLPlatform platform = GST_GL_PLATFORM_NONE;
GstGLAPI gl_apis;
GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
handle, type, apis);
platform = gst_gl_platform_from_string (type);
gl_apis = gst_gl_api_from_string (apis);
if (gl_apis && platform)
other_context =
gst_gl_context_new_wrapped (src->display, (guintptr) handle,
platform, gl_apis);
}
}
}
if (src->other_context) {
if (!other_context) {
other_context = src->other_context;
} else {
GST_ELEMENT_WARNING (src, LIBRARY, SETTINGS,
("%s", "Cannot share with more than one GL context"),
("%s", "Cannot share with more than one GL context"));
}
}
if (!src->context) { if (!src->context) {
src->context = gst_gl_context_new (src->display); src->context = gst_gl_context_new (src->display);
if (!gst_gl_context_create (src->context, other_context, &error)) if (!gst_gl_context_create (src->context, src->other_context, &error))
goto context_error; goto context_error;
} }
} while (!gst_gl_display_add_context (src->display, src->context));
}
out_width = GST_VIDEO_INFO_WIDTH (&src->out_info); out_width = GST_VIDEO_INFO_WIDTH (&src->out_info);
out_height = GST_VIDEO_INFO_HEIGHT (&src->out_info); out_height = GST_VIDEO_INFO_HEIGHT (&src->out_info);
@ -893,11 +850,7 @@ gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
update_pool = FALSE; update_pool = FALSE;
} }
if (!pool || (!same_downstream_gl_context if (!pool || !GST_IS_GL_BUFFER_POOL (pool)) {
&& gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE,
NULL)
&& !gst_buffer_pool_has_option (pool,
GST_BUFFER_POOL_OPTION_GL_SYNC_META))) {
/* can't use this pool */ /* can't use this pool */
if (pool) if (pool)
gst_object_unref (pool); gst_object_unref (pool);

View File

@ -49,8 +49,7 @@ enum
#define gst_gl_base_filter_parent_class parent_class #define gst_gl_base_filter_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstGLBaseFilter, gst_gl_base_filter, G_DEFINE_TYPE_WITH_CODE (GstGLBaseFilter, gst_gl_base_filter,
GST_TYPE_BASE_TRANSFORM, GST_DEBUG_CATEGORY_INIT (gst_gl_base_filter_debug, 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_set_property (GObject * object, guint prop_id, static void gst_gl_base_filter_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec); const GValue * value, GParamSpec * pspec);
@ -356,65 +355,24 @@ gst_gl_base_filter_decide_allocation (GstBaseTransform * trans,
{ {
GstGLBaseFilter *filter = GST_GL_BASE_FILTER (trans); GstGLBaseFilter *filter = GST_GL_BASE_FILTER (trans);
GError *error = NULL; GError *error = NULL;
GstGLContext *other_context = NULL;
guint idx;
if (!_ensure_gl_setup (filter)) _find_local_gl_context (filter);
return FALSE;
if (!filter->context && gst_query_find_allocation_meta (query,
GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
GstGLContext *context;
const GstStructure *upload_meta_params;
gpointer handle;
gchar *type;
gchar *apis;
gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
if (upload_meta_params) {
if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
GstGLContext *old = filter->context;
filter->context = context;
if (old)
gst_object_unref (old);
} else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
&type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
&& handle) {
GstGLPlatform platform = GST_GL_PLATFORM_NONE;
GstGLAPI gl_apis;
GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
handle, type, apis);
platform = gst_gl_platform_from_string (type);
gl_apis = gst_gl_api_from_string (apis);
if (gl_apis && platform)
other_context =
gst_gl_context_new_wrapped (filter->display, (guintptr) handle,
platform, gl_apis);
}
}
}
if (filter->priv->other_context) {
if (!other_context) {
other_context = filter->priv->other_context;
} else {
GST_ELEMENT_WARNING (filter, LIBRARY, SETTINGS,
("%s", "Cannot share with more than one GL context"),
("%s", "Cannot share with more than one GL context"));
}
}
if (!filter->context) {
do {
if (filter->context)
gst_object_unref (filter->context);
/* just get a GL context. we don't care */
filter->context =
gst_gl_display_get_gl_context_for_thread (filter->display, NULL);
if (!filter->context) { if (!filter->context) {
filter->context = gst_gl_context_new (filter->display); filter->context = gst_gl_context_new (filter->display);
if (!gst_gl_context_create (filter->context, other_context, &error)) if (!gst_gl_context_create (filter->context,
filter->priv->other_context, &error))
goto context_error; goto context_error;
} }
} while (!gst_gl_display_add_context (filter->display, filter->context));
}
gst_gl_context_thread_add (filter->context, gst_gl_base_filter_gl_start, gst_gl_context_thread_add (filter->context, gst_gl_base_filter_gl_start,
filter); filter);

View File

@ -84,6 +84,8 @@ static guintptr gst_gl_display_default_get_handle (GstGLDisplay * display);
struct _GstGLDisplayPrivate struct _GstGLDisplayPrivate
{ {
GstGLAPI gl_api; GstGLAPI gl_api;
GList *contexts;
}; };
static void static void
@ -116,8 +118,18 @@ gst_gl_display_init (GstGLDisplay * display)
static void static void
gst_gl_display_finalize (GObject * object) gst_gl_display_finalize (GObject * object)
{ {
GstGLDisplay *display = GST_GL_DISPLAY (object);
GList *l;
GST_TRACE_OBJECT (object, "finalizing"); GST_TRACE_OBJECT (object, "finalizing");
for (l = display->priv->contexts; l; l = l->next) {
g_weak_ref_clear ((GWeakRef *) l->data);
g_free (l->data);
}
g_list_free (display->priv->contexts);
G_OBJECT_CLASS (gst_gl_display_parent_class)->finalize (object); G_OBJECT_CLASS (gst_gl_display_parent_class)->finalize (object);
} }
@ -309,3 +321,120 @@ gst_context_get_gl_display (GstContext * context, GstGLDisplay ** display)
return ret; return ret;
} }
static GstGLContext *
_get_gl_context_for_thread_unlocked (GstGLDisplay * display, GThread * thread)
{
GstGLContext *context = NULL;
GList *prev = NULL, *l = display->priv->contexts;
while (l) {
GWeakRef *ref = l->data;
GThread *context_thread;
context = g_weak_ref_get (ref);
if (!context) {
/* remove dead contexts */
g_weak_ref_clear (l->data);
display->priv->contexts = g_list_delete_link (display->priv->contexts, l);
l = prev ? prev->next : display->priv->contexts;
continue;
}
context_thread = gst_gl_context_get_thread (context);
if (thread != NULL && thread == context_thread) {
g_thread_unref (context_thread);
gst_object_unref (context);
prev = l;
l = l->next;
continue;
}
if (context_thread)
g_thread_unref (context_thread);
return context;
}
return NULL;
}
/**
* gst_gl_display_get_gl_context_for_thread:
* @display: a #GstGLDisplay
* @thread: a #GThread
*
* Returns: (transfer full): the #GstGLContext current on @thread or %NULL
*
* Since: 1.6
*/
GstGLContext *
gst_gl_display_get_gl_context_for_thread (GstGLDisplay * display,
GThread * thread)
{
GstGLContext *context;
g_return_val_if_fail (GST_IS_GL_DISPLAY (display), NULL);
GST_OBJECT_LOCK (display);
context = _get_gl_context_for_thread_unlocked (display, thread);
GST_DEBUG_OBJECT (display, "returning context %" GST_PTR_FORMAT " for thread "
"%p", context, thread);
GST_OBJECT_UNLOCK (display);
return context;
}
/**
* gst_gl_display_add_context:
* @display: a #GstGLDisplay
* @context: (transfer none): a #GstGLContext
*
* Returns: whether @context was successfully added. %FALSE may be returned
* if there already exists another context for @context's active thread.
*
* Since: 1.6
*/
gboolean
gst_gl_display_add_context (GstGLDisplay * display, GstGLContext * context)
{
GstGLDisplay *context_display;
gboolean ret = TRUE;
GThread *thread;
GWeakRef *ref;
g_return_val_if_fail (GST_IS_GL_DISPLAY (display), FALSE);
g_return_val_if_fail (GST_GL_IS_CONTEXT (context), FALSE);
context_display = gst_gl_context_get_display (context);
g_assert (context_display == display);
gst_object_unref (context_display);
GST_OBJECT_LOCK (display);
thread = gst_gl_context_get_thread (context);
if (thread) {
GstGLContext *collision =
_get_gl_context_for_thread_unlocked (display, thread);
g_thread_unref (thread);
if (collision) {
if (collision != context) {
gst_object_unref (collision);
ret = FALSE;
goto out;
}
gst_object_unref (collision);
}
}
ref = g_new0 (GWeakRef, 1);
g_weak_ref_init (ref, context);
display->priv->contexts = g_list_prepend (display->priv->contexts, ref);
out:
GST_DEBUG_OBJECT (display, "%ssuccessfully inserted context %" GST_PTR_FORMAT,
ret ? "" : "un", context);
GST_OBJECT_UNLOCK (display);
return ret;
}

View File

@ -91,6 +91,11 @@ GstGLAPI gst_gl_display_get_gl_api (GstGLDisplay * display);
void gst_context_set_gl_display (GstContext * context, GstGLDisplay * display); void gst_context_set_gl_display (GstContext * context, GstGLDisplay * display);
gboolean gst_context_get_gl_display (GstContext * context, GstGLDisplay ** display); gboolean gst_context_get_gl_display (GstContext * context, GstGLDisplay ** display);
GstGLContext * gst_gl_display_get_gl_context_for_thread (GstGLDisplay * display,
GThread * thread);
gboolean gst_gl_display_add_context (GstGLDisplay * display,
GstGLContext * context);
G_END_DECLS G_END_DECLS
#endif /* __GST_GL_DISPLAY_H__ */ #endif /* __GST_GL_DISPLAY_H__ */