mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-03-30 12:49:40 +00:00
libs: make GstVaapiVideoPool thread-safe.
https://bugzilla.gnome.org/show_bug.cgi?id=707108 Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
This commit is contained in:
parent
f9d0d6e272
commit
f9ee8703cf
2 changed files with 93 additions and 22 deletions
|
@ -64,6 +64,7 @@ gst_vaapi_video_pool_init(GstVaapiVideoPool *pool, GstVaapiDisplay *display,
|
||||||
pool->capacity = 0;
|
pool->capacity = 0;
|
||||||
|
|
||||||
g_queue_init(&pool->free_objects);
|
g_queue_init(&pool->free_objects);
|
||||||
|
g_mutex_init(&pool->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -73,6 +74,7 @@ gst_vaapi_video_pool_finalize(GstVaapiVideoPool *pool)
|
||||||
g_queue_foreach(&pool->free_objects, (GFunc)gst_vaapi_object_unref, NULL);
|
g_queue_foreach(&pool->free_objects, (GFunc)gst_vaapi_object_unref, NULL);
|
||||||
g_queue_clear(&pool->free_objects);
|
g_queue_clear(&pool->free_objects);
|
||||||
gst_vaapi_display_replace(&pool->display, NULL);
|
gst_vaapi_display_replace(&pool->display, NULL);
|
||||||
|
g_mutex_clear(&pool->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -163,13 +165,11 @@ gst_vaapi_video_pool_get_object_type(GstVaapiVideoPool *pool)
|
||||||
*
|
*
|
||||||
* Return value: a possibly newly allocated object, or %NULL on error
|
* Return value: a possibly newly allocated object, or %NULL on error
|
||||||
*/
|
*/
|
||||||
gpointer
|
static gpointer
|
||||||
gst_vaapi_video_pool_get_object(GstVaapiVideoPool *pool)
|
gst_vaapi_video_pool_get_object_unlocked(GstVaapiVideoPool *pool)
|
||||||
{
|
{
|
||||||
gpointer object;
|
gpointer object;
|
||||||
|
|
||||||
g_return_val_if_fail(pool != NULL, NULL);
|
|
||||||
|
|
||||||
if (pool->capacity && pool->used_count >= pool->capacity)
|
if (pool->capacity && pool->used_count >= pool->capacity)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -185,6 +185,19 @@ gst_vaapi_video_pool_get_object(GstVaapiVideoPool *pool)
|
||||||
return gst_vaapi_object_ref(object);
|
return gst_vaapi_object_ref(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gpointer
|
||||||
|
gst_vaapi_video_pool_get_object(GstVaapiVideoPool *pool)
|
||||||
|
{
|
||||||
|
gpointer object;
|
||||||
|
|
||||||
|
g_return_val_if_fail(pool != NULL, NULL);
|
||||||
|
|
||||||
|
g_mutex_lock(&pool->mutex);
|
||||||
|
object = gst_vaapi_video_pool_get_object_unlocked(pool);
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gst_vaapi_video_pool_put_object:
|
* gst_vaapi_video_pool_put_object:
|
||||||
* @pool: a #GstVaapiVideoPool
|
* @pool: a #GstVaapiVideoPool
|
||||||
|
@ -195,14 +208,12 @@ gst_vaapi_video_pool_get_object(GstVaapiVideoPool *pool)
|
||||||
* Calling this function with an arbitrary object yields undefined
|
* Calling this function with an arbitrary object yields undefined
|
||||||
* behaviour.
|
* behaviour.
|
||||||
*/
|
*/
|
||||||
void
|
static void
|
||||||
gst_vaapi_video_pool_put_object(GstVaapiVideoPool *pool, gpointer object)
|
gst_vaapi_video_pool_put_object_unlocked(GstVaapiVideoPool *pool,
|
||||||
|
gpointer object)
|
||||||
{
|
{
|
||||||
GList *elem;
|
GList *elem;
|
||||||
|
|
||||||
g_return_if_fail(pool != NULL);
|
|
||||||
g_return_if_fail(object != NULL);
|
|
||||||
|
|
||||||
elem = g_list_find(pool->used_objects, object);
|
elem = g_list_find(pool->used_objects, object);
|
||||||
if (!elem)
|
if (!elem)
|
||||||
return;
|
return;
|
||||||
|
@ -213,6 +224,17 @@ gst_vaapi_video_pool_put_object(GstVaapiVideoPool *pool, gpointer object)
|
||||||
g_queue_push_tail(&pool->free_objects, object);
|
g_queue_push_tail(&pool->free_objects, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
gst_vaapi_video_pool_put_object(GstVaapiVideoPool *pool, gpointer object)
|
||||||
|
{
|
||||||
|
g_return_if_fail(pool != NULL);
|
||||||
|
g_return_if_fail(object != NULL);
|
||||||
|
|
||||||
|
g_mutex_lock(&pool->mutex);
|
||||||
|
gst_vaapi_video_pool_put_object_unlocked(pool, object);
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gst_vaapi_video_pool_add_object:
|
* gst_vaapi_video_pool_add_object:
|
||||||
* @pool: a #GstVaapiVideoPool
|
* @pool: a #GstVaapiVideoPool
|
||||||
|
@ -224,14 +246,26 @@ gst_vaapi_video_pool_put_object(GstVaapiVideoPool *pool, gpointer object)
|
||||||
*
|
*
|
||||||
* Return value: %TRUE on success.
|
* Return value: %TRUE on success.
|
||||||
*/
|
*/
|
||||||
|
static inline gboolean
|
||||||
|
gst_vaapi_video_pool_add_object_unlocked(GstVaapiVideoPool *pool,
|
||||||
|
gpointer object)
|
||||||
|
{
|
||||||
|
g_queue_push_tail(&pool->free_objects, gst_vaapi_object_ref(object));
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
gst_vaapi_video_pool_add_object(GstVaapiVideoPool *pool, gpointer object)
|
gst_vaapi_video_pool_add_object(GstVaapiVideoPool *pool, gpointer object)
|
||||||
{
|
{
|
||||||
|
gboolean success;
|
||||||
|
|
||||||
g_return_val_if_fail(pool != NULL, FALSE);
|
g_return_val_if_fail(pool != NULL, FALSE);
|
||||||
g_return_val_if_fail(object != NULL, FALSE);
|
g_return_val_if_fail(object != NULL, FALSE);
|
||||||
|
|
||||||
g_queue_push_tail(&pool->free_objects, gst_vaapi_object_ref(object));
|
g_mutex_lock(&pool->mutex);
|
||||||
return TRUE;
|
success = gst_vaapi_video_pool_add_object_unlocked(pool, object);
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -245,21 +279,33 @@ gst_vaapi_video_pool_add_object(GstVaapiVideoPool *pool, gpointer object)
|
||||||
*
|
*
|
||||||
* Return value: %TRUE on success.
|
* Return value: %TRUE on success.
|
||||||
*/
|
*/
|
||||||
gboolean
|
static gboolean
|
||||||
gst_vaapi_video_pool_add_objects(GstVaapiVideoPool *pool, GPtrArray *objects)
|
gst_vaapi_video_pool_add_objects_unlocked(GstVaapiVideoPool *pool,
|
||||||
|
GPtrArray *objects)
|
||||||
{
|
{
|
||||||
guint i;
|
guint i;
|
||||||
|
|
||||||
g_return_val_if_fail(pool != NULL, FALSE);
|
|
||||||
|
|
||||||
for (i = 0; i < objects->len; i++) {
|
for (i = 0; i < objects->len; i++) {
|
||||||
gpointer const object = g_ptr_array_index(objects, i);
|
gpointer const object = g_ptr_array_index(objects, i);
|
||||||
if (!gst_vaapi_video_pool_add_object(pool, object))
|
if (!gst_vaapi_video_pool_add_object_unlocked(pool, object))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
gst_vaapi_video_pool_add_objects(GstVaapiVideoPool *pool, GPtrArray *objects)
|
||||||
|
{
|
||||||
|
gboolean success;
|
||||||
|
|
||||||
|
g_return_val_if_fail(pool != NULL, FALSE);
|
||||||
|
|
||||||
|
g_mutex_lock(&pool->mutex);
|
||||||
|
success = gst_vaapi_video_pool_add_objects_unlocked(pool, objects);
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gst_vaapi_video_pool_get_size:
|
* gst_vaapi_video_pool_get_size:
|
||||||
* @pool: a #GstVaapiVideoPool
|
* @pool: a #GstVaapiVideoPool
|
||||||
|
@ -271,9 +317,14 @@ gst_vaapi_video_pool_add_objects(GstVaapiVideoPool *pool, GPtrArray *objects)
|
||||||
guint
|
guint
|
||||||
gst_vaapi_video_pool_get_size(GstVaapiVideoPool *pool)
|
gst_vaapi_video_pool_get_size(GstVaapiVideoPool *pool)
|
||||||
{
|
{
|
||||||
|
guint size;
|
||||||
|
|
||||||
g_return_val_if_fail(pool != NULL, 0);
|
g_return_val_if_fail(pool != NULL, 0);
|
||||||
|
|
||||||
return g_queue_get_length(&pool->free_objects);
|
g_mutex_lock(&pool->mutex);
|
||||||
|
size = g_queue_get_length(&pool->free_objects);
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -288,13 +339,11 @@ gst_vaapi_video_pool_get_size(GstVaapiVideoPool *pool)
|
||||||
*
|
*
|
||||||
* Return value: %TRUE on success
|
* Return value: %TRUE on success
|
||||||
*/
|
*/
|
||||||
gboolean
|
static gboolean
|
||||||
gst_vaapi_video_pool_reserve(GstVaapiVideoPool *pool, guint n)
|
gst_vaapi_video_pool_reserve_unlocked(GstVaapiVideoPool *pool, guint n)
|
||||||
{
|
{
|
||||||
guint i, num_allocated;
|
guint i, num_allocated;
|
||||||
|
|
||||||
g_return_val_if_fail(pool != NULL, 0);
|
|
||||||
|
|
||||||
num_allocated = gst_vaapi_video_pool_get_size(pool) + pool->used_count;
|
num_allocated = gst_vaapi_video_pool_get_size(pool) + pool->used_count;
|
||||||
if (n < num_allocated)
|
if (n < num_allocated)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
@ -311,6 +360,19 @@ gst_vaapi_video_pool_reserve(GstVaapiVideoPool *pool, guint n)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
gst_vaapi_video_pool_reserve(GstVaapiVideoPool *pool, guint n)
|
||||||
|
{
|
||||||
|
gboolean success;
|
||||||
|
|
||||||
|
g_return_val_if_fail(pool != NULL, 0);
|
||||||
|
|
||||||
|
g_mutex_lock(&pool->mutex);
|
||||||
|
success = gst_vaapi_video_pool_reserve_unlocked(pool, n);
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gst_vaapi_video_pool_get_capacity:
|
* gst_vaapi_video_pool_get_capacity:
|
||||||
* @pool: a #GstVaapiVideoPool
|
* @pool: a #GstVaapiVideoPool
|
||||||
|
@ -323,9 +385,15 @@ gst_vaapi_video_pool_reserve(GstVaapiVideoPool *pool, guint n)
|
||||||
guint
|
guint
|
||||||
gst_vaapi_video_pool_get_capacity(GstVaapiVideoPool *pool)
|
gst_vaapi_video_pool_get_capacity(GstVaapiVideoPool *pool)
|
||||||
{
|
{
|
||||||
|
guint capacity;
|
||||||
|
|
||||||
g_return_val_if_fail(pool != NULL, 0);
|
g_return_val_if_fail(pool != NULL, 0);
|
||||||
|
|
||||||
return pool->capacity;
|
g_mutex_lock(&pool->mutex);
|
||||||
|
capacity = pool->capacity;
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
|
|
||||||
|
return capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -340,5 +408,7 @@ gst_vaapi_video_pool_set_capacity(GstVaapiVideoPool *pool, guint capacity)
|
||||||
{
|
{
|
||||||
g_return_if_fail(pool != NULL);
|
g_return_if_fail(pool != NULL);
|
||||||
|
|
||||||
|
g_mutex_lock(&pool->mutex);
|
||||||
pool->capacity = capacity;
|
pool->capacity = capacity;
|
||||||
|
g_mutex_unlock(&pool->mutex);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,7 @@ struct _GstVaapiVideoPool {
|
||||||
GList *used_objects;
|
GList *used_objects;
|
||||||
guint used_count;
|
guint used_count;
|
||||||
guint capacity;
|
guint capacity;
|
||||||
|
GMutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue