From b4656949570367f9130e08b72021042443e57fd9 Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Sun, 15 Jun 2025 19:06:28 +0900 Subject: [PATCH] hip: Add GstHipStream object Adding hip stream abstraction layer Part-of: --- subprojects/gst-plugins-bad/sys/hip/gsthip.h | 1 + .../gst-plugins-bad/sys/hip/gsthip_fwd.h | 2 + .../gst-plugins-bad/sys/hip/gsthipdevice.cpp | 20 +++ .../gst-plugins-bad/sys/hip/gsthipdevice.h | 2 + .../gst-plugins-bad/sys/hip/gsthiploader.cpp | 32 ++++ .../gst-plugins-bad/sys/hip/gsthiploader.h | 6 + .../gst-plugins-bad/sys/hip/gsthipstream.cpp | 140 ++++++++++++++++++ .../gst-plugins-bad/sys/hip/gsthipstream.h | 47 ++++++ .../gst-plugins-bad/sys/hip/meson.build | 1 + 9 files changed, 251 insertions(+) create mode 100644 subprojects/gst-plugins-bad/sys/hip/gsthipstream.cpp create mode 100644 subprojects/gst-plugins-bad/sys/hip/gsthipstream.h diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthip.h b/subprojects/gst-plugins-bad/sys/hip/gsthip.h index 72d5f13b0e..48c143f12a 100644 --- a/subprojects/gst-plugins-bad/sys/hip/gsthip.h +++ b/subprojects/gst-plugins-bad/sys/hip/gsthip.h @@ -30,4 +30,5 @@ #include "gsthiputils.h" #include "gsthiploader.h" #include "gsthip-interop.h" +#include "gsthipstream.h" diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthip_fwd.h b/subprojects/gst-plugins-bad/sys/hip/gsthip_fwd.h index 7a88a8f62e..b105b82640 100644 --- a/subprojects/gst-plugins-bad/sys/hip/gsthip_fwd.h +++ b/subprojects/gst-plugins-bad/sys/hip/gsthip_fwd.h @@ -44,5 +44,7 @@ typedef struct _GstHipBufferPoolPrivate GstHipBufferPoolPrivate; typedef struct _GstHipGraphicsResource GstHipGraphicsResource; +typedef struct _GstHipStream GstHipStream; + G_END_DECLS diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.cpp b/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.cpp index b3487abd11..6f22cf0ba0 100644 --- a/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.cpp +++ b/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.cpp @@ -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; +} diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.h b/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.h index 43dc22395e..1004cbe32e 100644 --- a/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.h +++ b/subprojects/gst-plugins-bad/sys/hip/gsthipdevice.h @@ -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 diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthiploader.cpp b/subprojects/gst-plugins-bad/sys/hip/gsthiploader.cpp index 8bb3178eb7..26d17281be 100644 --- a/subprojects/gst-plugins-bad/sys/hip/gsthiploader.cpp +++ b/subprojects/gst-plugins-bad/sys/hip/gsthiploader.cpp @@ -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) { diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthiploader.h b/subprojects/gst-plugins-bad/sys/hip/gsthiploader.h index 3e6539c3f2..91cc1cec2e 100644 --- a/subprojects/gst-plugins-bad/sys/hip/gsthiploader.h +++ b/subprojects/gst-plugins-bad/sys/hip/gsthiploader.h @@ -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); diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthipstream.cpp b/subprojects/gst-plugins-bad/sys/hip/gsthipstream.cpp new file mode 100644 index 0000000000..7041ef8287 --- /dev/null +++ b/subprojects/gst-plugins-bad/sys/hip/gsthipstream.cpp @@ -0,0 +1,140 @@ +/* GStreamer + * Copyright (C) 2025 Seungha Yang + * + * 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 + +#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); +} diff --git a/subprojects/gst-plugins-bad/sys/hip/gsthipstream.h b/subprojects/gst-plugins-bad/sys/hip/gsthipstream.h new file mode 100644 index 0000000000..092b86cb4c --- /dev/null +++ b/subprojects/gst-plugins-bad/sys/hip/gsthipstream.h @@ -0,0 +1,47 @@ +/* GStreamer + * Copyright (C) 2025 Seungha Yang + * + * 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 +#include "gsthip_fwd.h" +#include "gsthip-enums.h" +#include + +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 + diff --git a/subprojects/gst-plugins-bad/sys/hip/meson.build b/subprojects/gst-plugins-bad/sys/hip/meson.build index 77b8cfecc0..bc3425491c 100644 --- a/subprojects/gst-plugins-bad/sys/hip/meson.build +++ b/subprojects/gst-plugins-bad/sys/hip/meson.build @@ -12,6 +12,7 @@ hip_sources = [ 'gsthiputils.cpp', 'gsthip-interop.cpp', 'gsthipcompositor.cpp', + 'gsthipstream.cpp', 'plugin.cpp', ]