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 committed by Tim-Philipp Müller
parent 2e16961ab3
commit 813fb633b4
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);
GError *error = NULL;
guint idx;
GstGLContext *other_context = NULL;
gboolean ret = TRUE;
if (!gst_gl_ensure_element_data (mix, &mix->display,
@ -491,58 +489,20 @@ gst_gl_base_mixer_decide_allocation (GstGLBaseMixer * mix, GstQuery * query)
_find_local_gl_context (mix);
if (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 = 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) {
mix->context = gst_gl_context_new (mix->display);
if (!gst_gl_context_create (mix->context, other_context, &error))
goto context_error;
do {
if (mix->context)
gst_object_unref (mix->context);
/* just get a GL context. we don't care */
mix->context =
gst_gl_display_get_gl_context_for_thread (mix->display, NULL);
if (!mix->context) {
mix->context = gst_gl_context_new (mix->display);
if (!gst_gl_context_create (mix->context, mix->priv->other_context,
&error))
goto context_error;
}
} while (!gst_gl_display_add_context (mix->display, mix->context));
}
if (mix_class->decide_allocation)

View file

@ -603,46 +603,6 @@ gst_glimage_sink_mouse_event_cb (GstGLWindow * window, char *event_name,
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
_ensure_gl_setup (GstGLImageSink * gl_sink)
{
@ -657,66 +617,80 @@ _ensure_gl_setup (GstGLImageSink * gl_sink)
gst_gl_display_filter_gl_api (gl_sink->display, SUPPORTED_GL_APIS);
if (!gl_sink->context) {
GstGLWindow *window;
do {
GstGLContext *other_context;
GstGLWindow *window;
GST_DEBUG_OBJECT (gl_sink,
"No current context, creating one for %" GST_PTR_FORMAT,
gl_sink->display);
if (gl_sink->context)
gst_object_unref (gl_sink->context);
gl_sink->context = gst_gl_context_new (gl_sink->display);
if (!gl_sink->context)
goto context_creation_error;
GST_DEBUG_OBJECT (gl_sink,
"No current context, creating one for %" GST_PTR_FORMAT,
gl_sink->display);
window = gst_gl_context_get_window (gl_sink->context);
gl_sink->context = gst_gl_context_new (gl_sink->display);
if (!gl_sink->context)
goto context_creation_error;
GST_DEBUG_OBJECT (gl_sink, "got window %" GST_PTR_FORMAT, window);
window = gst_gl_context_get_window (gl_sink->context);
if (!gl_sink->window_id && !gl_sink->new_window_id)
gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (gl_sink));
GST_DEBUG_OBJECT (gl_sink, "got window %" GST_PTR_FORMAT, window);
GST_DEBUG_OBJECT (gl_sink,
"window_id : %" G_GUINTPTR_FORMAT " , new_window_id : %"
G_GUINTPTR_FORMAT, gl_sink->window_id, gl_sink->new_window_id);
if (!gl_sink->window_id && !gl_sink->new_window_id)
gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (gl_sink));
if (gl_sink->window_id != gl_sink->new_window_id) {
gl_sink->window_id = gl_sink->new_window_id;
GST_DEBUG_OBJECT (gl_sink, "Setting window handle on gl window");
gst_gl_window_set_window_handle (window, gl_sink->window_id);
}
GST_DEBUG_OBJECT (gl_sink,
"window_id : %" G_GUINTPTR_FORMAT " , new_window_id : %"
G_GUINTPTR_FORMAT, gl_sink->window_id, gl_sink->new_window_id);
GST_DEBUG_OBJECT (gl_sink,
"creating context %" GST_PTR_FORMAT " from other context %"
GST_PTR_FORMAT, gl_sink->context, gl_sink->other_context);
if (gl_sink->window_id != gl_sink->new_window_id) {
gl_sink->window_id = gl_sink->new_window_id;
GST_DEBUG_OBJECT (gl_sink, "Setting window handle on gl window");
gst_gl_window_set_window_handle (window, gl_sink->window_id);
}
if (!gst_gl_context_create (gl_sink->context, gl_sink->other_context,
&error)) {
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,
"creating context %" GST_PTR_FORMAT " from other context %"
GST_PTR_FORMAT, gl_sink->context, other_context);
if (!gst_gl_context_create (gl_sink->context, other_context, &error)) {
gst_object_unref (other_context);
gst_object_unref (window);
goto context_error;
}
gst_gl_window_handle_events (window, gl_sink->handle_events);
/* setup callbacks */
gst_gl_window_set_resize_callback (window,
GST_GL_WINDOW_RESIZE_CB (gst_glimage_sink_on_resize),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gst_gl_window_set_draw_callback (window,
GST_GL_WINDOW_CB (gst_glimage_sink_on_draw),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gst_gl_window_set_close_callback (window,
GST_GL_WINDOW_CB (gst_glimage_sink_on_close),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gl_sink->key_sig_id = g_signal_connect (window, "key-event", G_CALLBACK
(gst_glimage_sink_key_event_cb), gl_sink);
gl_sink->mouse_sig_id =
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);
goto context_error;
}
gst_gl_window_handle_events (window, gl_sink->handle_events);
/* setup callbacks */
gst_gl_window_set_resize_callback (window,
GST_GL_WINDOW_RESIZE_CB (gst_glimage_sink_on_resize),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gst_gl_window_set_draw_callback (window,
GST_GL_WINDOW_CB (gst_glimage_sink_on_draw),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gst_gl_window_set_close_callback (window,
GST_GL_WINDOW_CB (gst_glimage_sink_on_close),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gl_sink->key_sig_id = g_signal_connect (window, "key-event", G_CALLBACK
(gst_glimage_sink_key_event_cb), gl_sink);
gl_sink->mouse_sig_id = g_signal_connect (window, "mouse-event", G_CALLBACK
(gst_glimage_sink_mouse_event_cb), gl_sink);
gst_object_unref (window);
} while (!gst_gl_display_add_context (gl_sink->display, gl_sink->context));
} else
GST_DEBUG_OBJECT (gl_sink, "Already have a context");
_find_local_gl_context (gl_sink);
return TRUE;
context_creation_error:
@ -845,6 +819,7 @@ gst_glimage_sink_change_state (GstElement * element, GstStateChange transition)
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
_ensure_gl_setup (glimage_sink);
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
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;
gboolean update_pool;
GError *error = NULL;
guint idx;
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))
return FALSE;
@ -815,59 +812,19 @@ gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
_find_local_gl_context (src);
if (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 = 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) {
src->context = gst_gl_context_new (src->display);
if (!gst_gl_context_create (src->context, other_context, &error))
goto context_error;
do {
if (src->context)
gst_object_unref (src->context);
/* just get a GL context. we don't care */
src->context =
gst_gl_display_get_gl_context_for_thread (src->display, NULL);
if (!src->context) {
src->context = gst_gl_context_new (src->display);
if (!gst_gl_context_create (src->context, src->other_context, &error))
goto context_error;
}
} while (!gst_gl_display_add_context (src->display, src->context));
}
out_width = GST_VIDEO_INFO_WIDTH (&src->out_info);
@ -893,11 +850,7 @@ gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
update_pool = FALSE;
}
if (!pool || (!same_downstream_gl_context
&& 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))) {
if (!pool || !GST_IS_GL_BUFFER_POOL (pool)) {
/* can't use this pool */
if (pool)
gst_object_unref (pool);

View file

@ -49,8 +49,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_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
@ -356,64 +355,23 @@ gst_gl_base_filter_decide_allocation (GstBaseTransform * trans,
{
GstGLBaseFilter *filter = GST_GL_BASE_FILTER (trans);
GError *error = NULL;
GstGLContext *other_context = NULL;
guint idx;
if (!_ensure_gl_setup (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"));
}
}
_find_local_gl_context (filter);
if (!filter->context) {
filter->context = gst_gl_context_new (filter->display);
if (!gst_gl_context_create (filter->context, other_context, &error))
goto context_error;
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) {
filter->context = gst_gl_context_new (filter->display);
if (!gst_gl_context_create (filter->context,
filter->priv->other_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,

View file

@ -84,6 +84,8 @@ static guintptr gst_gl_display_default_get_handle (GstGLDisplay * display);
struct _GstGLDisplayPrivate
{
GstGLAPI gl_api;
GList *contexts;
};
static void
@ -116,8 +118,18 @@ gst_gl_display_init (GstGLDisplay * display)
static void
gst_gl_display_finalize (GObject * object)
{
GstGLDisplay *display = GST_GL_DISPLAY (object);
GList *l;
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);
}
@ -309,3 +321,120 @@ gst_context_get_gl_display (GstContext * context, GstGLDisplay ** display)
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);
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
#endif /* __GST_GL_DISPLAY_H__ */