gstgldmabufbufferpool: use gsteglimagecache

Store the imported GstEGLImage in a GstEGLImageCache. Since this passes
ownership to the cache, stop unreffing the images from the GstMemory.
Free the cache when the buffer pool is stopped.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6792>
This commit is contained in:
Philipp Zabel 2024-07-29 10:41:42 +02:00 committed by Nicolas Dufresne
parent f18d6fcb40
commit c024c5e3bf

View file

@ -30,16 +30,16 @@
#include <gst/gl/gstglmemory.h> #include <gst/gl/gstglmemory.h>
#include <gst/gl/gstglsyncmeta.h> #include <gst/gl/gstglsyncmeta.h>
#include <gst/gl/egl/gsteglimage.h> #include <gst/gl/egl/gsteglimage.h>
#include <gst/gl/egl/gsteglimagecache.h>
#define GST_GL_DMABUF_EGLIMAGE "gst.gl.dmabuf.eglimage" #include <gst/gl/egl/gstglmemoryegl.h>
typedef struct _GstGLDMABufBufferPoolPrivate typedef struct _GstGLDMABufBufferPoolPrivate
{ {
GstBufferPool *dmabuf_pool; GstBufferPool *dmabuf_pool;
GstCaps *dmabuf_caps; GstCaps *dmabuf_caps;
GstGLMemoryAllocator *allocator;
GstGLVideoAllocationParams *glparams; GstGLVideoAllocationParams *glparams;
GstVideoInfoDmaDrm drm_info; GstVideoInfoDmaDrm drm_info;
GstEGLImageCache *eglimage_cache;
gboolean add_glsyncmeta; gboolean add_glsyncmeta;
} GstGLDMABufBufferPoolPrivate; } GstGLDMABufBufferPoolPrivate;
@ -78,19 +78,8 @@ gst_gl_dmabuf_buffer_pool_set_config (GstBufferPool * pool,
goto wrong_config; goto wrong_config;
} }
gst_clear_object (&self->priv->allocator); if (allocator && !GST_IS_GL_MEMORY_EGL_ALLOCATOR (allocator)) {
goto wrong_allocator;
if (allocator) {
if (!GST_IS_GL_MEMORY_ALLOCATOR (allocator)) {
gst_clear_object (&allocator);
goto wrong_allocator;
} else {
self->priv->allocator = gst_object_ref (allocator);
}
} else {
self->priv->allocator =
gst_gl_memory_allocator_get_default (GST_GL_BUFFER_POOL
(pool)->context);
} }
/* /*
@ -193,6 +182,11 @@ gst_gl_dmabuf_buffer_pool_start (GstBufferPool * pool)
return FALSE; return FALSE;
} }
if (self->priv->eglimage_cache) {
gst_egl_image_cache_unref (self->priv->eglimage_cache);
}
self->priv->eglimage_cache = gst_egl_image_cache_new ();
return GST_BUFFER_POOL_CLASS (parent_class)->start (pool); return GST_BUFFER_POOL_CLASS (parent_class)->start (pool);
} }
@ -205,33 +199,34 @@ gst_gl_dmabuf_buffer_pool_stop (GstBufferPool * pool)
return FALSE; return FALSE;
} }
if (self->priv->eglimage_cache) {
gst_egl_image_cache_unref (self->priv->eglimage_cache);
}
return GST_BUFFER_POOL_CLASS (parent_class)->stop (pool); return GST_BUFFER_POOL_CLASS (parent_class)->stop (pool);
} }
typedef struct typedef struct
{ {
GstEGLImage *eglimage[GST_VIDEO_MAX_PLANES]; GstEGLImage *eglimage[GST_VIDEO_MAX_PLANES];
gpointer gpuhandle[GST_VIDEO_MAX_PLANES]; GstGLVideoAllocationParams *glparams;
guint n_planes; GstBuffer *outbuf;
} WrapDMABufData; } BufferSetupData;
static void static void
_wrap_dmabuf_eglimage (GstGLContext * context, gpointer data) _setup_buffer_gl_thread (GstGLContext * context, BufferSetupData * d)
{ {
WrapDMABufData *d = data; GstGLMemoryAllocator *allocator =
const GstGLFuncs *gl = context->gl_vtable; GST_GL_MEMORY_ALLOCATOR (gst_allocator_find
GLuint tex_ids[GST_VIDEO_MAX_PLANES]; (GST_GL_MEMORY_EGL_ALLOCATOR_NAME));
guint i;
gl->GenTextures (d->n_planes, tex_ids); if (!gst_gl_memory_setup_buffer (allocator, d->outbuf, d->glparams, NULL,
(gpointer *) d->eglimage,
for (i = 0; i < d->n_planes; ++i) { GST_VIDEO_INFO_N_PLANES (d->glparams->v_info))) {
gl->BindTexture (GL_TEXTURE_2D, tex_ids[i]); gst_clear_buffer (&d->outbuf);
gl->EGLImageTargetTexture2D (GL_TEXTURE_2D,
gst_egl_image_get_image (d->eglimage[i]));
d->gpuhandle[i] = GUINT_TO_POINTER (tex_ids[i]);
} }
gst_clear_object (&allocator);
} }
static GstFlowReturn static GstFlowReturn
@ -274,8 +269,9 @@ gst_gl_dmabuf_buffer_pool_acquire_buffer (GstBufferPool * pool,
GstVideoMeta *vmeta; GstVideoMeta *vmeta;
GstFlowReturn ret; GstFlowReturn ret;
GstBuffer *dmabuf; GstBuffer *dmabuf;
GstBuffer *buf; GstMemory *previous_mem = NULL;
WrapDMABufData data; GstEGLImageCacheEntry *cache_entry = NULL;
BufferSetupData data;
guint i; guint i;
ret = gst_buffer_pool_acquire_buffer (self->priv->dmabuf_pool, &dmabuf, NULL); ret = gst_buffer_pool_acquire_buffer (self->priv->dmabuf_pool, &dmabuf, NULL);
@ -286,9 +282,7 @@ gst_gl_dmabuf_buffer_pool_acquire_buffer (GstBufferPool * pool,
vmeta = gst_buffer_get_video_meta (dmabuf); vmeta = gst_buffer_get_video_meta (dmabuf);
g_return_val_if_fail (vmeta, GST_FLOW_ERROR); g_return_val_if_fail (vmeta, GST_FLOW_ERROR);
data.n_planes = GST_VIDEO_INFO_N_PLANES (v_info); for (i = 0; i < GST_VIDEO_INFO_N_PLANES (v_info); ++i) {
for (i = 0; i < data.n_planes; ++i) {
guint mem_idx, length; guint mem_idx, length;
gsize skip; gsize skip;
GstMemory *dmabufmem; GstMemory *dmabufmem;
@ -306,6 +300,16 @@ gst_gl_dmabuf_buffer_pool_acquire_buffer (GstBufferPool * pool,
g_assert (gst_is_dmabuf_memory (dmabufmem)); g_assert (gst_is_dmabuf_memory (dmabufmem));
/*
* Check if an EGLImage is cached. Remember the previous memory and cache
* entry to avoid repeated lookups if all dmabufmem point to the same
* memory. Otherwise create one and cache it.
*/
data.eglimage[i] = gst_egl_image_cache_lookup (self->priv->eglimage_cache,
dmabufmem, i, &previous_mem, &cache_entry);
if (data.eglimage[i])
continue;
/* Anything that is not using GLMemory format RGBA is using indirect /* Anything that is not using GLMemory format RGBA is using indirect
* dmabuf importation with linear modifiers */ * dmabuf importation with linear modifiers */
if (GST_VIDEO_INFO_FORMAT (v_info) != GST_VIDEO_FORMAT_RGBA) { if (GST_VIDEO_INFO_FORMAT (v_info) != GST_VIDEO_FORMAT_RGBA) {
@ -317,49 +321,48 @@ gst_gl_dmabuf_buffer_pool_acquire_buffer (GstBufferPool * pool,
gst_egl_image_from_dmabuf_direct_target_with_dma_drm (glpool->context, gst_egl_image_from_dmabuf_direct_target_with_dma_drm (glpool->context,
1, &fd, &skip, &self->priv->drm_info, GL_TEXTURE_2D); 1, &fd, &skip, &self->priv->drm_info, GL_TEXTURE_2D);
} }
gst_egl_image_cache_store (self->priv->eglimage_cache, dmabufmem, i,
data.eglimage[i], &cache_entry);
} }
gst_gl_context_thread_add (glpool->context, _wrap_dmabuf_eglimage, &data); data.glparams = self->priv->glparams;
ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (pool, &buf, ret =
GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (pool, &data.outbuf,
params); params);
if (ret != GST_FLOW_OK) { if (ret != GST_FLOW_OK) {
return GST_FLOW_ERROR; ret = GST_FLOW_ERROR;
goto out;
} }
if (!gst_gl_memory_setup_buffer (self->priv->allocator, buf, gst_gl_context_thread_add (glpool->context,
self->priv->glparams, NULL, data.gpuhandle, data.n_planes)) { (GstGLContextThreadFunc) _setup_buffer_gl_thread, &data);
if (!data.outbuf) {
goto mem_create_failed; goto mem_create_failed;
} }
for (i = 0; i < data.n_planes; ++i) { gst_buffer_add_parent_buffer_meta (data.outbuf, dmabuf);
GstMemory *mem = gst_buffer_peek_memory (buf, i);
/* Unset wrapped flag, we want the texture be freed with the memory. */ *buffer = data.outbuf;
GST_GL_MEMORY_CAST (mem)->texture_wrapped = FALSE;
gst_mini_object_set_qdata (GST_MINI_OBJECT (mem), out:
g_quark_from_static_string (GST_GL_DMABUF_EGLIMAGE),
data.eglimage[i], (GDestroyNotify) gst_egl_image_unref);
}
gst_buffer_add_parent_buffer_meta (buf, dmabuf);
gst_clear_buffer (&dmabuf); gst_clear_buffer (&dmabuf);
*buffer = buf; return ret;
return GST_FLOW_OK;
/* ERROR */ /* ERROR */
no_buffer: no_buffer:
{ {
GST_WARNING_OBJECT (self, "Could not create DMABuf buffer"); GST_WARNING_OBJECT (self, "Could not create DMABuf buffer");
return GST_FLOW_ERROR; ret = GST_FLOW_ERROR;
goto out;
} }
mem_create_failed: mem_create_failed:
{ {
GST_WARNING_OBJECT (self, "Could not create GL Memory"); GST_WARNING_OBJECT (self, "Could not create GL Memory");
return GST_FLOW_ERROR; ret = GST_FLOW_ERROR;
goto out;
} }
} }
@ -381,19 +384,11 @@ gst_is_gl_dmabuf_buffer (GstBuffer * buffer)
GstBuffer * GstBuffer *
gst_gl_dmabuf_buffer_unwrap (GstBuffer * buffer) gst_gl_dmabuf_buffer_unwrap (GstBuffer * buffer)
{ {
GstGLDMABufBufferPool *pool;
GstParentBufferMeta *meta; GstParentBufferMeta *meta;
GstBuffer *wrapped_dmabuf = NULL; GstBuffer *wrapped_dmabuf = NULL;
g_return_val_if_fail (gst_is_gl_dmabuf_buffer (buffer), NULL); g_return_val_if_fail (gst_is_gl_dmabuf_buffer (buffer), NULL);
pool = GST_GL_DMABUF_BUFFER_POOL (buffer->pool);
if (gst_buffer_peek_memory (buffer, 0)->allocator !=
GST_ALLOCATOR (pool->priv->allocator)) {
return NULL;
}
meta = gst_buffer_get_parent_buffer_meta (buffer); meta = gst_buffer_get_parent_buffer_meta (buffer);
if (meta && meta->buffer) { if (meta && meta->buffer) {