hip: Add GstHipStream object

Adding hip stream abstraction layer

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/9281>
This commit is contained in:
Seungha Yang 2025-06-15 19:06:28 +09:00 committed by GStreamer Marge Bot
parent 9575de7367
commit b465694957
9 changed files with 251 additions and 0 deletions

View File

@ -30,4 +30,5 @@
#include "gsthiputils.h"
#include "gsthiploader.h"
#include "gsthip-interop.h"
#include "gsthipstream.h"

View File

@ -44,5 +44,7 @@ typedef struct _GstHipBufferPoolPrivate GstHipBufferPoolPrivate;
typedef struct _GstHipGraphicsResource GstHipGraphicsResource;
typedef struct _GstHipStream GstHipStream;
G_END_DECLS

View File

@ -51,9 +51,14 @@ enum
struct _GstHipDevicePrivate
{
~_GstHipDevicePrivate ()
{
gst_clear_hip_stream (&stream);
}
guint device_id;
GstHipVendor vendor;
gboolean texture_support;
GstHipStream *stream = nullptr;
};
#define gst_hip_device_parent_class parent_class
@ -171,11 +176,18 @@ gst_hip_device_new (GstHipVendor vendor, guint device_id)
}
}
auto stream = gst_hip_stream_new (vendor, device_id);
if (!stream) {
GST_ERROR ("Couldn't create stream");
return nullptr;
}
auto self = (GstHipDevice *) g_object_new (GST_TYPE_HIP_DEVICE, nullptr);
gst_object_ref_sink (self);
self->priv->device_id = device_id;
self->priv->vendor = vendor;
self->priv->texture_support = texture_support;
self->priv->stream = stream;
return self;
}
@ -241,3 +253,11 @@ gst_hip_device_get_device_id (GstHipDevice * device)
return device->priv->device_id;
}
GstHipStream *
gst_hip_device_get_stream (GstHipDevice * device)
{
g_return_val_if_fail (GST_IS_HIP_DEVICE (device), nullptr);
return device->priv->stream;
}

View File

@ -66,5 +66,7 @@ GstHipVendor gst_hip_device_get_vendor (GstHipDevice * device);
guint gst_hip_device_get_device_id (GstHipDevice * device);
GstHipStream * gst_hip_device_get_stream (GstHipDevice * device);
G_END_DECLS

View File

@ -70,6 +70,8 @@ struct GstHipFuncTableAmd
hipError_t (*hipFree) (void *ptr);
hipError_t (*hipHostMalloc) (void **ptr, size_t size, unsigned int flags);
hipError_t (*hipHostFree) (void *ptr);
hipError_t (*hipStreamCreate) (hipStream_t* stream);
hipError_t (*hipStreamDestroy) (hipStream_t stream);
hipError_t (*hipStreamSynchronize) (hipStream_t stream);
hipError_t (*hipModuleLoadData) (hipModule_t * module, const void *image);
hipError_t (*hipModuleUnload) (hipModule_t module);
@ -158,6 +160,8 @@ struct GstHipFuncTableCudaRt
cudaError_t (CUDAAPI *cudaFree) (void *ptr);
cudaError_t (CUDAAPI *cudaMallocHost) (void **ptr, size_t size, unsigned int flags);
cudaError_t (CUDAAPI *cudaFreeHost) (void *ptr);
cudaError_t (CUDAAPI *cudaStreamCreate) (cudaStream_t *pStream);
cudaError_t (CUDAAPI *cudaStreamDestroy) (cudaStream_t stream);
cudaError_t (CUDAAPI *cudaStreamSynchronize) (cudaStream_t stream);
cudaError_t (CUDAAPI *cudaGraphicsMapResources) (int count,
cudaGraphicsResource_t *resources, cudaStream_t stream);
@ -242,6 +246,8 @@ load_amd_func_table (void)
LOAD_SYMBOL (hipFree);
LOAD_SYMBOL (hipHostMalloc);
LOAD_SYMBOL (hipHostFree);
LOAD_SYMBOL (hipStreamCreate);
LOAD_SYMBOL (hipStreamDestroy);
LOAD_SYMBOL (hipStreamSynchronize);
LOAD_SYMBOL (hipModuleLoadData);
LOAD_SYMBOL (hipModuleUnload);
@ -361,6 +367,8 @@ load_cudart_func_table (guint major_ver, guint minor_ver)
LOAD_SYMBOL (cudaFree);
LOAD_SYMBOL (cudaMallocHost);
LOAD_SYMBOL (cudaFreeHost);
LOAD_SYMBOL (cudaStreamCreate);
LOAD_SYMBOL (cudaStreamDestroy);
LOAD_SYMBOL (cudaStreamSynchronize);
LOAD_SYMBOL (cudaGraphicsMapResources);
LOAD_SYMBOL (cudaGraphicsResourceGetMappedPointer);
@ -932,6 +940,30 @@ HipHostFree (GstHipVendor vendor, void *ptr)
return hipCUDAErrorTohipError (cuda_ret);
}
hipError_t
HipStreamCreate (GstHipVendor vendor, hipStream_t * stream)
{
CHECK_VENDOR (vendor);
if (vendor == GST_HIP_VENDOR_AMD)
return amd_ftable.hipStreamCreate (stream);
auto cuda_ret = cudart_ftable.cudaStreamCreate ((cudaStream_t *) stream);
return hipCUDAErrorTohipError (cuda_ret);
}
hipError_t
HipStreamDestroy (GstHipVendor vendor, hipStream_t stream)
{
CHECK_VENDOR (vendor);
if (vendor == GST_HIP_VENDOR_AMD)
return amd_ftable.hipStreamDestroy (stream);
auto cuda_ret = cudart_ftable.cudaStreamDestroy (stream);
return hipCUDAErrorTohipError (cuda_ret);
}
hipError_t
HipStreamSynchronize (GstHipVendor vendor, hipStream_t stream)
{

View File

@ -72,6 +72,12 @@ hipError_t HipHostMalloc (GstHipVendor vendor,
hipError_t HipHostFree (GstHipVendor vendor,
void* ptr);
hipError_t HipStreamCreate (GstHipVendor vendor,
hipStream_t* stream);
hipError_t HipStreamDestroy (GstHipVendor vendor,
hipStream_t stream);
hipError_t HipStreamSynchronize (GstHipVendor vendor,
hipStream_t stream);

View File

@ -0,0 +1,140 @@
/* GStreamer
* Copyright (C) 2025 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 "gsthip-config.h"
#include "gsthip.h"
#include <mutex>
#ifndef GST_DISABLE_GST_DEBUG
#define GST_CAT_DEFAULT ensure_debug_category()
static GstDebugCategory *
ensure_debug_category (void)
{
static GstDebugCategory *cat = nullptr;
static std::once_flag once;
std::call_once (once,[&] {
cat = _gst_debug_category_new ("hipstream", 0, "hipstream");
});
return cat;
}
#endif
/* *INDENT-OFF* */
struct _GstHipStream : public GstMiniObject
{
~_GstHipStream ()
{
if (handle) {
auto hip_ret = HipSetDevice (vendor, device_id);
if (gst_hip_result (hip_ret, vendor))
HipStreamDestroy (vendor, handle);
}
}
hipStream_t handle = nullptr;
GstHipVendor vendor;
guint device_id;
};
/* *INDENT-ON* */
static void
gst_hip_stream_free (GstHipStream * stream)
{
delete stream;
}
GST_DEFINE_MINI_OBJECT_TYPE (GstHipStream, gst_hip_stream);
GstHipStream *
gst_hip_stream_new (GstHipVendor vendor, guint device_id)
{
g_return_val_if_fail (vendor != GST_HIP_VENDOR_UNKNOWN, nullptr);
auto hip_ret = HipSetDevice (vendor, device_id);
if (!gst_hip_result (hip_ret, vendor)) {
GST_ERROR ("Couldn't set device");
return nullptr;
}
hipStream_t handle;
hip_ret = HipStreamCreate (vendor, &handle);
if (!gst_hip_result (hip_ret, vendor)) {
GST_ERROR ("Couldn't create stream");
return nullptr;
}
auto stream = new GstHipStream ();
stream->handle = handle;
stream->vendor = vendor;
stream->device_id = device_id;
gst_mini_object_init (stream, 0, gst_hip_stream_get_type (),
nullptr, nullptr, (GstMiniObjectFreeFunction) gst_hip_stream_free);
return stream;
}
GstHipVendor
gst_hip_stream_get_vendor (GstHipStream * stream)
{
g_return_val_if_fail (stream, GST_HIP_VENDOR_UNKNOWN);
return stream->vendor;
}
guint
gst_hip_stream_get_device_id (GstHipStream * stream)
{
g_return_val_if_fail (stream, G_MAXUINT);
return stream->vendor;
}
hipStream_t
gst_hip_stream_get_handle (GstHipStream * stream)
{
if (!stream)
return nullptr;
return stream->handle;
}
GstHipStream *
gst_hip_stream_ref (GstHipStream * stream)
{
return (GstHipStream *) gst_mini_object_ref (stream);
}
void
gst_hip_stream_unref (GstHipStream * stream)
{
return gst_mini_object_unref (stream);
}
void
gst_clear_hip_stream (GstHipStream ** stream)
{
gst_clear_mini_object (stream);
}

View File

@ -0,0 +1,47 @@
/* GStreamer
* Copyright (C) 2025 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 "gsthip_fwd.h"
#include "gsthip-enums.h"
#include <hip/hip_runtime.h>
G_BEGIN_DECLS
GType gst_hip_stream_get_type (void);
GstHipStream * gst_hip_stream_new (GstHipVendor vendor,
guint device_id);
GstHipVendor gst_hip_stream_get_vendor (GstHipStream * stream);
guint gst_hip_stream_get_device_id (GstHipStream * stream);
hipStream_t gst_hip_stream_get_handle (GstHipStream * stream);
GstHipStream * gst_hip_stream_ref (GstHipStream * stream);
void gst_hip_stream_unref (GstHipStream * stream);
void gst_clear_hip_stream (GstHipStream ** stream);
G_END_DECLS

View File

@ -12,6 +12,7 @@ hip_sources = [
'gsthiputils.cpp',
'gsthip-interop.cpp',
'gsthipcompositor.cpp',
'gsthipstream.cpp',
'plugin.cpp',
]