cuda: Add GstCudaStream object

Wrap CUstream handle with GstCudaStream to make it ref-counted
object. This GstCudaStream object will be used later for
CUDA stream sharing

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3629>
This commit is contained in:
Seungha Yang 2022-12-19 20:57:30 +09:00 committed by GStreamer Marge Bot
parent 661b5f60c6
commit a7c54ebc06
4 changed files with 280 additions and 0 deletions

View file

@ -32,5 +32,6 @@
#include <gst/cuda/gstcudaloader.h>
#include <gst/cuda/gstcudamemory.h>
#include <gst/cuda/gstcudanvrtc.h>
#include <gst/cuda/gstcudastream.h>
#include <gst/cuda/gstcudautils.h>

View file

@ -0,0 +1,208 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* 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 "cuda-gst.h"
#include "gstcudastream.h"
#include "gstcudautils.h"
#ifndef GST_DISABLE_GST_DEBUG
#define GST_CAT_DEFAULT ensure_debug_category()
static GstDebugCategory *
ensure_debug_category (void)
{
static gsize cat_once = 0;
if (g_once_init_enter (&cat_once)) {
gsize temp = (gsize) _gst_debug_category_new ("cudastream", 0,
"cudastream");
g_once_init_leave (&cat_once, temp);
}
return (GstDebugCategory *) cat_once;
}
#else
#define ensure_debug_category() /* NOOP */
#endif /* GST_DISABLE_GST_DEBUG */
static gint
gst_cuda_stream_compare_func (const GstCudaStream * a, const GstCudaStream * b)
{
if (a == b)
return GST_VALUE_EQUAL;
return GST_VALUE_UNORDERED;
}
static void
gst_cuda_stream_init_once (GType type)
{
static GstValueTable table = {
0, (GstValueCompareFunc) gst_cuda_stream_compare_func,
NULL, NULL
};
table.type = type;
gst_value_register (&table);
}
G_DEFINE_BOXED_TYPE_WITH_CODE (GstCudaStream, gst_cuda_stream,
(GBoxedCopyFunc) gst_mini_object_ref,
(GBoxedFreeFunc) gst_mini_object_unref,
gst_cuda_stream_init_once (g_define_type_id));
struct _GstCudaStreamPrivate
{
CUstream handle;
};
static void
_gst_cuda_stream_free (GstCudaStream * stream)
{
GstCudaStreamPrivate *priv = stream->priv;
if (stream->context) {
if (priv->handle) {
gst_cuda_context_push (stream->context);
CuStreamDestroy (priv->handle);
gst_cuda_context_pop (NULL);
}
gst_object_unref (stream->context);
}
g_free (priv);
g_free (stream);
}
/**
* gst_cuda_stream_new:
* @context: a #GstCudaContext
*
* Creates a new #GstCudaStream
*
* Returns: (transfer full) (nullable): a new #GstCudaStream or %NULL on
* failure
*
* Since: 1.24
*/
GstCudaStream *
gst_cuda_stream_new (GstCudaContext * context)
{
GstCudaStream *self;
CUresult cuda_ret;
CUstream stream;
g_return_val_if_fail (GST_IS_CUDA_CONTEXT (context), NULL);
if (!gst_cuda_context_push (context)) {
GST_ERROR_OBJECT (context, "Couldn't push context");
return NULL;
}
cuda_ret = CuStreamCreate (&stream, CU_STREAM_DEFAULT);
gst_cuda_context_pop (NULL);
if (!gst_cuda_result (cuda_ret)) {
GST_ERROR_OBJECT (context, "Couldn't create stream");
return NULL;
}
self = g_new0 (GstCudaStream, 1);
self->context = gst_object_ref (context);
self->priv = g_new0 (GstCudaStreamPrivate, 1);
self->priv->handle = stream;
gst_mini_object_init (GST_MINI_OBJECT_CAST (self), 0,
GST_TYPE_CUDA_STREAM, NULL, NULL,
(GstMiniObjectFreeFunction) _gst_cuda_stream_free);
return self;
}
/**
* gst_cuda_stream_get_handle:
* @stream: (allow-none): a #GstCudaStream
*
* Get CUDA stream handle
*
* Returns: a `CUstream` handle of @stream or %NULL if @stream is %NULL
*
* Since: 1.24
*/
CUstream
gst_cuda_stream_get_handle (GstCudaStream * stream)
{
g_return_val_if_fail (!stream || GST_IS_CUDA_STREAM (stream), NULL);
if (!stream)
return NULL;
return stream->priv->handle;
}
/**
* gst_cuda_stream_ref:
* @stream: a #GstCudaStream
*
* Increase the reference count of @stream.
*
* Returns: (transfer full): @stream
*
* Since: 1.24
*/
GstCudaStream *
gst_cuda_stream_ref (GstCudaStream * stream)
{
return (GstCudaStream *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (stream));
}
/**
* gst_cuda_stream_unref:
* @stream: a #GstCudaStream
*
* Decrease the reference count of @stream.
*
* Since: 1.24
*/
void
gst_cuda_stream_unref (GstCudaStream * stream)
{
gst_mini_object_unref (GST_MINI_OBJECT_CAST (stream));
}
/**
* gst_clear_cuda_stream: (skip)
* @stream: a pointer to a #GstCudaStream reference
*
* Clears a reference to a #GstCudaStream.
*
* Since: 1.24
*/
void
gst_clear_cuda_stream (GstCudaStream ** stream)
{
if (stream && *stream) {
gst_cuda_stream_unref (*stream);
*stream = NULL;
}
}

View file

@ -0,0 +1,69 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* 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.
*/
#pragma once
#include <gst/gst.h>
#include <gst/cuda/cuda-prelude.h>
#include <gst/cuda/gstcudacontext.h>
#include <cuda.h>
G_BEGIN_DECLS
#define GST_TYPE_CUDA_STREAM (gst_cuda_stream_get_type())
#define GST_IS_CUDA_STREAM(obj) (GST_IS_MINI_OBJECT_TYPE(obj, GST_TYPE_CUDA_STREAM))
#define GST_CUDA_STREAM(obj) ((GstCudaStream *)obj)
typedef struct _GstCudaStream GstCudaStream;
typedef struct _GstCudaStreamPrivate GstCudaStreamPrivate;
/**
* GstCudaStream:
*
* Since: 1.24
*/
struct _GstCudaStream
{
GstMiniObject parent;
GstCudaContext *context;
/*< private >*/
GstCudaStreamPrivate *priv;
};
GST_CUDA_API
GType gst_cuda_stream_get_type (void);
GST_CUDA_API
GstCudaStream * gst_cuda_stream_new (GstCudaContext * context);
GST_CUDA_API
CUstream gst_cuda_stream_get_handle (GstCudaStream * stream);
GST_CUDA_API
GstCudaStream * gst_cuda_stream_ref (GstCudaStream * stream);
GST_CUDA_API
void gst_cuda_stream_unref (GstCudaStream * stream);
GST_CUDA_API
void gst_clear_cuda_stream (GstCudaStream ** stream);
G_END_DECLS

View file

@ -4,6 +4,7 @@ cuda_sources = files([
'gstcudaloader.c',
'gstcudamemory.c',
'gstcudanvrtc.c',
'gstcudastream.c',
'gstcudautils.c',
])
@ -15,6 +16,7 @@ cuda_headers = files([
'gstcudaloader.h',
'gstcudamemory.h',
'gstcudanvrtc.h',
'gstcudastream.h',
'gstcudautils.h',
])