Merge branch 'dmabuf' into 'main'

Draft: gldownload: import DMABufs from downstream pool

See merge request gstreamer/gstreamer!6792
This commit is contained in:
Jakub Adam 2024-05-03 20:24:18 +00:00
commit e1bbd3c578
6 changed files with 378 additions and 0 deletions

View file

@ -25,6 +25,7 @@
#include <gst/gl/gl.h>
#include <gst/gl/gstglfuncs.h>
#if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_DMABUF
#include <gst/gl/gstgldmabufbufferpool.h>
#include <gst/gl/egl/gsteglimage.h>
#include <gst/allocators/gstdmabuf.h>
#endif
@ -899,6 +900,8 @@ gst_gl_download_element_stop (GstBaseTransform * bt)
dl->dmabuf_allocator = NULL;
}
gst_clear_object (&dl->dmabuf_import_buffer_pool);
return TRUE;
}
@ -1292,6 +1295,21 @@ gst_gl_download_element_prepare_output_buffer (GstBaseTransform * bt,
}
#endif
#if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_DMABUF
{
GstBuffer *wrapped_dmabuf =
gst_mini_object_get_qdata (GST_MINI_OBJECT (inbuf),
g_quark_from_static_string (GST_GL_DMABUF_BUFFER));
if (wrapped_dmabuf) {
gst_buffer_copy_into (wrapped_dmabuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS,
0, -1);
*outbuf = gst_buffer_ref (wrapped_dmabuf);
return GST_FLOW_OK;
}
}
if (dl->mode == GST_GL_DOWNLOAD_MODE_DMABUF_EXPORTS) {
GstBuffer *buffer = _try_export_dmabuf (dl, inbuf);
@ -1374,6 +1392,42 @@ gst_gl_download_element_decide_allocation (GstBaseTransform * trans,
download->add_videometa = FALSE;
}
gst_clear_object (&download->dmabuf_import_buffer_pool);
if (gst_query_get_n_allocation_pools (query) > 0) {
GstBufferPool *pool;
gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
if (pool) {
/* Check if the pool can provide dmabufs */
/* TODO: some better method to check this? */
GstBuffer *buffer = NULL;
if (!gst_buffer_pool_set_active (pool, TRUE)) {
goto pool_failed;
}
if (gst_buffer_pool_acquire_buffer (pool, &buffer, NULL) != GST_FLOW_OK) {
goto pool_failed;
}
if (gst_is_dmabuf_memory (gst_buffer_peek_memory (buffer, 0))) {
download->dmabuf_import_buffer_pool = gst_object_ref (pool);
}
goto pool_finished;
pool_failed:
gst_query_remove_nth_allocation_pool (query, 0);
pool_finished:
gst_buffer_pool_set_active (pool, FALSE);
gst_clear_buffer (&buffer);
gst_clear_object (&pool);
}
}
return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
query);
}
@ -1406,6 +1460,7 @@ static gboolean
gst_gl_download_element_propose_allocation (GstBaseTransform * bt,
GstQuery * decide_query, GstQuery * query)
{
GstGLDownloadElement *download = GST_GL_DOWNLOAD_ELEMENT (bt);
GstBufferPool *pool = NULL;
GstCaps *caps;
GstGLContext *context;
@ -1446,6 +1501,14 @@ gst_gl_download_element_propose_allocation (GstBaseTransform * bt,
}
}
#endif
#if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_DMABUF
if (!pool && download->dmabuf_import_buffer_pool) {
pool = gst_gl_dmabuf_buffer_pool_new (download->parent.context,
download->dmabuf_import_buffer_pool);
}
#endif
if (!pool) {
pool = gst_gl_buffer_pool_new (context);
}

View file

@ -54,6 +54,7 @@ struct _GstGLDownloadElement
GstGlDownloadMode mode;
gboolean try_dmabuf_exports;
GstAllocator * dmabuf_allocator;
GstBufferPool * dmabuf_import_buffer_pool;
gboolean add_videometa;
};

View file

@ -78,6 +78,9 @@ typedef struct _GstGLBufferPool GstGLBufferPool;
typedef struct _GstGLBufferPoolClass GstGLBufferPoolClass;
typedef struct _GstGLBufferPoolPrivate GstGLBufferPoolPrivate;
typedef struct _GstGLDMABufBufferPool GstGLDMABufBufferPool;
typedef struct _GstGLDMABufBufferPoolPrivate GstGLDMABufBufferPoolPrivate;
typedef struct _GstGLColorConvert GstGLColorConvert;
typedef struct _GstGLColorConvertClass GstGLColorConvertClass;
typedef struct _GstGLColorConvertPrivate GstGLColorConvertPrivate;

View file

@ -0,0 +1,264 @@
/*
* GStreamer
* Copyright © 2024 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstgldmabufbufferpool.h"
#include <gst/allocators/gstdmabuf.h>
#include <gst/gl/gstglcontext.h>
#include <gst/gl/gstglfuncs.h>
#include <gst/gl/gstglmemory.h>
#include <gst/gl/egl/gsteglimage.h>
/**
* SECTION:gstgldmabufbufferpool
* @title: GstGLDMABufBufferPool
* @short_description: buffer pool that wraps DMABuf file descriptors into #GstGLBaseMemory objects
* @see_also: #GstBufferPool, #GstGLBaseMemory, #GstGLMemory
*/
typedef struct _GstGLDMABufBufferPoolPrivate
{
GstBufferPool *dmabuf_pool;
GstGLMemoryAllocator *allocator;
GstGLVideoAllocationParams *glparams;
} GstGLDMABufBufferPoolPrivate;
GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_DMABUF_BUFFER_POOL);
#define GST_CAT_DEFAULT GST_CAT_GL_DMABUF_BUFFER_POOL
G_DEFINE_TYPE_WITH_CODE (GstGLDMABufBufferPool, gst_gl_dmabuf_buffer_pool,
GST_TYPE_GL_BUFFER_POOL,
G_ADD_PRIVATE (GstGLDMABufBufferPool)
GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_DMABUF_BUFFER_POOL, "gldmabufbufferpool", 0, \
"GL-DMABuf Buffer Pool"));
static gboolean
gst_gl_dmabuf_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
{
GstGLDMABufBufferPool *self = GST_GL_DMABUF_BUFFER_POOL (pool);
GstAllocator *allocator = NULL;
GstAllocationParams alloc_params;
if (!gst_buffer_pool_config_get_allocator (config, &allocator, &alloc_params)) {
goto wrong_config;
}
gst_clear_object (&self->priv->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);
}
if (!GST_BUFFER_POOL_CLASS (gst_gl_dmabuf_buffer_pool_parent_class)->set_config (pool, config)) {
return FALSE;
}
if (self->priv->glparams) {
gst_gl_allocation_params_free ((GstGLAllocationParams *) self->priv->glparams);
}
self->priv->glparams = (GstGLVideoAllocationParams *)
gst_gl_buffer_pool_get_gl_allocation_params (GST_GL_BUFFER_POOL (pool));
self->priv->glparams->parent.alloc_flags |=
GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_WRAP_GPU_HANDLE;
return TRUE;
wrong_config:
{
GST_WARNING_OBJECT (pool, "Incorrect config for this pool");
return FALSE;
}
wrong_allocator:
{
GST_WARNING_OBJECT (pool, "Incorrect allocator type for this pool");
return FALSE;
}
}
static gboolean
gst_gl_dmabuf_buffer_pool_start (GstBufferPool * pool)
{
GstGLDMABufBufferPool *self = GST_GL_DMABUF_BUFFER_POOL (pool);
if (!gst_buffer_pool_set_active (self->priv->dmabuf_pool, TRUE)) {
return FALSE;
}
return GST_BUFFER_POOL_CLASS (gst_gl_dmabuf_buffer_pool_parent_class)->start (pool);
}
static gboolean
gst_gl_dmabuf_buffer_pool_stop (GstBufferPool * pool)
{
GstGLDMABufBufferPool *self = GST_GL_DMABUF_BUFFER_POOL (pool);
if (!gst_buffer_pool_set_active (self->priv->dmabuf_pool, FALSE)) {
return FALSE;
}
return GST_BUFFER_POOL_CLASS (gst_gl_dmabuf_buffer_pool_parent_class)->stop (pool);
}
typedef struct {
GstEGLImage *eglimage;
gpointer gpuhandle;
} WrapDMABufData;
static void
_wrap_dmabuf_eglimage (GstGLContext * context, gpointer data)
{
WrapDMABufData *d = data;
const GstGLFuncs *gl = context->gl_vtable;
guint tex_id;
gl->GenTextures (1, &tex_id);
gl->BindTexture (GL_TEXTURE_2D, tex_id);
gl->EGLImageTargetTexture2D (GL_TEXTURE_2D, gst_egl_image_get_image (d->eglimage));
gl->BindTexture (GL_TEXTURE_2D, 0);
d->gpuhandle = GUINT_TO_POINTER (tex_id);
}
/* This function handles GstBuffer creation */
static GstFlowReturn
gst_gl_dmabuf_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
GstBufferPoolAcquireParams * params)
{
GstGLDMABufBufferPool *self = GST_GL_DMABUF_BUFFER_POOL (pool);
GstGLBufferPool *glpool = GST_GL_BUFFER_POOL (pool);
GstFlowReturn ret;
GstBuffer *dmabuf;
GstBuffer *buf;
GstMemory *dmabufmem;
WrapDMABufData data;
ret = gst_buffer_pool_acquire_buffer (self->priv->dmabuf_pool, &dmabuf, NULL);
if (ret != GST_FLOW_OK) {
goto no_buffer;
}
dmabufmem = gst_buffer_peek_memory (dmabuf, 0);
g_assert (gst_is_dmabuf_memory (dmabufmem));
data.eglimage = gst_egl_image_from_dmabuf (glpool->context,
gst_dmabuf_memory_get_fd (dmabufmem), self->priv->glparams->v_info, 0, 0);
gst_gl_context_thread_add (glpool->context, _wrap_dmabuf_eglimage, &data);
if (!(buf = gst_buffer_new ())) {
goto no_buffer;
}
if (!gst_gl_memory_setup_buffer (self->priv->allocator, buf,
self->priv->glparams, NULL, &data.gpuhandle, 1)) {
goto mem_create_failed;
}
/* Unset wrapped flag, we want the texture be freed with the buffer. */
GST_GL_MEMORY_CAST (gst_buffer_peek_memory (buf, 0))->texture_wrapped = FALSE;
gst_mini_object_set_qdata (GST_MINI_OBJECT (buf),
g_quark_from_static_string (GST_GL_DMABUF_BUFFER), dmabuf,
(GDestroyNotify) gst_buffer_unref);
*buffer = buf;
return GST_FLOW_OK;
/* ERROR */
no_buffer:
{
GST_WARNING_OBJECT (self, "Could not create DMABuf buffer");
return GST_FLOW_ERROR;
}
mem_create_failed:
{
GST_WARNING_OBJECT (self, "Could not create GL Memory");
return GST_FLOW_ERROR;
}
}
GstBufferPool *
gst_gl_dmabuf_buffer_pool_new (GstGLContext * context, GstBufferPool * dmabuf_pool)
{
GstGLDMABufBufferPool *pool;
pool = g_object_new (GST_TYPE_GL_DMABUF_BUFFER_POOL, NULL);
gst_object_ref_sink (pool);
GST_GL_BUFFER_POOL (pool)->context = gst_object_ref (context);
pool->priv->dmabuf_pool = gst_object_ref (dmabuf_pool);
GST_LOG_OBJECT (pool, "new GL-DMABuf buffer pool for pool %" GST_PTR_FORMAT
" and context %" GST_PTR_FORMAT, dmabuf_pool, context);
return GST_BUFFER_POOL_CAST (pool);
}
static void
gst_gl_dmabuf_buffer_pool_init (GstGLDMABufBufferPool * pool)
{
pool->priv = gst_gl_dmabuf_buffer_pool_get_instance_private (pool);
}
static void
gst_gl_dmabuf_buffer_pool_finalize (GObject * object)
{
GstGLDMABufBufferPool *self = GST_GL_DMABUF_BUFFER_POOL (object);
GST_LOG_OBJECT (self, "finalize GL-DMABuf buffer pool");
gst_clear_object (&self->priv->dmabuf_pool);
if (self->priv->glparams) {
gst_gl_allocation_params_free ((GstGLAllocationParams *) self->priv->glparams);
}
G_OBJECT_CLASS (gst_gl_dmabuf_buffer_pool_parent_class)->finalize (object);
}
static void
gst_gl_dmabuf_buffer_pool_class_init (GstGLDMABufBufferPoolClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
gobject_class->finalize = gst_gl_dmabuf_buffer_pool_finalize;
gstbufferpool_class->set_config = gst_gl_dmabuf_buffer_pool_set_config;
gstbufferpool_class->alloc_buffer = gst_gl_dmabuf_buffer_pool_alloc;
gstbufferpool_class->start = gst_gl_dmabuf_buffer_pool_start;
gstbufferpool_class->stop = gst_gl_dmabuf_buffer_pool_stop;
}

View file

@ -0,0 +1,46 @@
/*
* GStreamer
* Copyright © 2024 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef _GST_GL_DMABUF_BUFFER_POOL_H_
#define _GST_GL_DMABUF_BUFFER_POOL_H_
#include <gst/gl/gstglbufferpool.h>
G_BEGIN_DECLS
#define GST_TYPE_GL_DMABUF_BUFFER_POOL (gst_gl_dmabuf_buffer_pool_get_type())
G_DECLARE_FINAL_TYPE (GstGLDMABufBufferPool, gst_gl_dmabuf_buffer_pool, GST, GL_DMABUF_BUFFER_POOL, GstGLBufferPool)
struct _GstGLDMABufBufferPool
{
GstGLBufferPool parent;
/*< private >*/
GstGLDMABufBufferPoolPrivate *priv;
};
GST_GL_API
GstBufferPool *gst_gl_dmabuf_buffer_pool_new (GstGLContext * context, GstBufferPool * dmabuf_pool);
#define GST_GL_DMABUF_BUFFER "gst.gl.dmabuf.buffer"
G_END_DECLS
#endif /* _GST_GL_DMABUF_BUFFER_POOL_H_ */

View file

@ -25,6 +25,7 @@ gl_sources = files([
'gstglcontextquirks.c',
'gstgldebug.c',
'gstgldisplay.c',
'gstgldmabufbufferpool.c',
'gstglfeature.c',
'gstglfilter.c',
'gstglformat.c',