diff --git a/Assets/MediaPipe/SDK/Scripts/Core/SharedPtrHandle.cs b/Assets/MediaPipe/SDK/Scripts/Core/SharedPtrHandle.cs index 7598eda0e..d717d52fc 100644 --- a/Assets/MediaPipe/SDK/Scripts/Core/SharedPtrHandle.cs +++ b/Assets/MediaPipe/SDK/Scripts/Core/SharedPtrHandle.cs @@ -2,7 +2,7 @@ namespace Mediapipe { public abstract class SharedPtrHandle : MpResourceHandle { - protected SharedPtrHandle(IntPtr ptr) : base(ptr) {} + protected SharedPtrHandle(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {} /// The owning pointer public abstract IntPtr Get(); diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs index 9b5039727..575405e52 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs @@ -10,8 +10,8 @@ public static GlContext GetCurrent() { return glContextPtr == IntPtr.Zero ? null : new GlContext(glContextPtr); } - public GlContext(IntPtr ptr) : base(ptr) { - sharedPtrHandle = new SharedPtr(ptr); + public GlContext(IntPtr ptr, bool isOwner = true) : base(isOwner) { + sharedPtrHandle = new SharedPtr(ptr, isOwner); this.ptr = sharedPtrHandle.Get(); } @@ -60,7 +60,7 @@ public long glFinishCount { } private class SharedPtr : SharedPtrHandle { - public SharedPtr(IntPtr ptr) : base(ptr) {} + public SharedPtr(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {} protected override void DeleteMpPtr() { UnsafeNativeMethods.mp_SharedGlContext__delete(ptr); diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs index 62855ac94..a12fecda5 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs @@ -13,8 +13,8 @@ public class GlTextureBuffer : MpResourceHandle { public delegate void DeletionCallback(UInt64 name, IntPtr glSyncToken); private GCHandle deletionCallbackHandle; - private GlTextureBuffer(IntPtr ptr) : base() { - sharedPtrHandle = new SharedPtr(ptr); + public GlTextureBuffer(IntPtr ptr, bool isOwner = true) : base(isOwner) { + sharedPtrHandle = new SharedPtr(ptr, isOwner); this.ptr = sharedPtrHandle.Get(); } @@ -57,8 +57,60 @@ public IntPtr sharedPtr { get { return sharedPtrHandle == null ? IntPtr.Zero : sharedPtrHandle.mpPtr; } } + public uint Name() { + return SafeNativeMethods.mp_GlTextureBuffer__name(mpPtr); + } + + public uint Target() { + return SafeNativeMethods.mp_GlTextureBuffer__target(mpPtr); + } + + public int Width() { + return SafeNativeMethods.mp_GlTexture__width(mpPtr); + } + + public int Height() { + return SafeNativeMethods.mp_GlTextureBuffer__height(mpPtr); + } + + public GpuBufferFormat Format() { + return SafeNativeMethods.mp_GlTextureBuffer__format(mpPtr); + } + + public void WaitUntilComplete() { + UnsafeNativeMethods.mp_GlTextureBuffer__WaitUntilComplete(mpPtr).Assert(); + } + + public void WaitOnGpu() { + UnsafeNativeMethods.mp_GlTextureBuffer__WaitOnGpu(mpPtr).Assert(); + } + + public void Reuse() { + UnsafeNativeMethods.mp_GlTextureBuffer__Reuse(mpPtr).Assert(); + } + + public void Updated(GlSyncPoint prodToken) { + UnsafeNativeMethods.mp_GlTextureBuffer__Updated__Pgst(mpPtr, prodToken.sharedPtr).Assert(); + } + + public void DidRead(GlSyncPoint consToken) { + UnsafeNativeMethods.mp_GlTextureBuffer__DidRead__Pgst(mpPtr, consToken.sharedPtr).Assert(); + } + + public void WaitForConsumers() { + UnsafeNativeMethods.mp_GlTextureBuffer__WaitForConsumers(mpPtr).Assert(); + } + + public void WaitForConsumersOnGpu() { + UnsafeNativeMethods.mp_GlTextureBuffer__WaitForConsumersOnGpu(mpPtr).Assert(); + } + + public GlContext GetProducerContext() { + return new GlContext(SafeNativeMethods.mp_GlTextureBuffer__GetProducerContext(mpPtr), false); + } + private class SharedPtr : SharedPtrHandle { - public SharedPtr(IntPtr ptr) : base(ptr) {} + public SharedPtr(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {} protected override void DeleteMpPtr() { UnsafeNativeMethods.mp_SharedGlTextureBuffer__delete(ptr); diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs index ca40c7463..d365e218d 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs @@ -6,7 +6,7 @@ public GpuBuffer(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {} public GpuBuffer(GlTextureBuffer glTextureBuffer) : base() { UnsafeNativeMethods.mp_GpuBuffer__PSgtb(glTextureBuffer.sharedPtr, out var ptr).Assert(); - glTextureBuffer.Dispose(); // GpuBuffer will manage the resource + glTextureBuffer.Dispose(); // respect move semantics this.ptr = ptr; } @@ -14,6 +14,10 @@ protected override void DeleteMpPtr() { UnsafeNativeMethods.mp_GpuBuffer__delete(ptr); } + public GlTextureBuffer GetGlTextureBuffer() { + return new GlTextureBuffer(SafeNativeMethods.mp_GpuBuffer__GetGlTextureBufferSharedPtr(mpPtr), false); + } + public GpuBufferFormat Format() { return SafeNativeMethods.mp_GpuBuffer__format(mpPtr); } diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Safe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Safe.cs index 4db6a02db..36605118e 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Safe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Safe.cs @@ -4,7 +4,29 @@ namespace Mediapipe { internal static partial class SafeNativeMethods { + #region GlTextureBuffer + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern UInt32 mp_GlTextureBuffer__name(IntPtr glTextureBuffer); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern UInt32 mp_GlTextureBuffer__target(IntPtr glTextureBuffer); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern int mp_GlTextureBuffer__width(IntPtr glTextureBuffer); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern int mp_GlTextureBuffer__height(IntPtr glTextureBuffer); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern GpuBufferFormat mp_GlTextureBuffer__format(IntPtr glTextureBuffer); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern IntPtr mp_GlTextureBuffer__GetProducerContext(IntPtr glTextureBuffer); + #endregion + + #region SharedGlTextureBuffer [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] public static extern IntPtr mp_SharedGlTextureBuffer__get(IntPtr glTextureBuffer); + #endregion } } diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Unsafe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Unsafe.cs index 74ef7467e..d10d768b7 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Unsafe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlTextureBuffer_Unsafe.cs @@ -3,6 +3,30 @@ namespace Mediapipe { internal static partial class UnsafeNativeMethods { + #region GlTextureBuffer + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_GlTextureBuffer__WaitUntilComplete(IntPtr glTextureBuffer); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_GlTextureBuffer__WaitOnGpu(IntPtr glTextureBuffer); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_GlTextureBuffer__Reuse(IntPtr glTextureBuffer); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_GlTextureBuffer__Updated__Pgst(IntPtr glTextureBuffer, IntPtr prodToken); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_GlTextureBuffer__DidRead__Pgst(IntPtr glTextureBuffer, IntPtr consToken); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_GlTextureBuffer__WaitForConsumers(IntPtr glTextureBuffer); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_GlTextureBuffer__WaitForConsumersOnGpu(IntPtr glTextureBuffer); + #endregion + + #region SharedGlTextureBuffer [DllImport (MediaPipeLibrary, ExactSpelling = true)] public static extern void mp_SharedGlTextureBuffer__delete(IntPtr glTextureBuffer); @@ -14,5 +38,6 @@ public static extern MpReturnCode mp_SharedGlTextureBuffer__ui_ui_i_i_ui_PF_PSgc UInt32 target, UInt32 name, int width, int height, GpuBufferFormat format, [MarshalAs(UnmanagedType.FunctionPtr)]GlTextureBuffer.DeletionCallback deletionCallback, IntPtr producerContext, out IntPtr sharedGlTextureBuffer); + #endregion } } diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs index 646aeac52..27761ce92 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs @@ -4,6 +4,9 @@ namespace Mediapipe { internal static partial class SafeNativeMethods { + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern IntPtr mp_GpuBuffer__GetGlTextureBufferSharedPtr(IntPtr gpuBuffer); + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] public static extern int mp_GpuBuffer__width(IntPtr gpuBuffer); diff --git a/C/mediapipe_api/gpu/gl_texture_buffer.cc b/C/mediapipe_api/gpu/gl_texture_buffer.cc index 9792e1610..da6fc0681 100644 --- a/C/mediapipe_api/gpu/gl_texture_buffer.cc +++ b/C/mediapipe_api/gpu/gl_texture_buffer.cc @@ -38,3 +38,76 @@ MpReturnCode mp_SharedGlTextureBuffer__ui_ui_i_i_ui_PF_PSgc(GLenum target, RETURN_CODE(MpReturnCode::Success); } CATCH_EXCEPTION } + +GLuint mp_GlTextureBuffer__name(mediapipe::GlTextureBuffer* gl_texture_buffer) { + return gl_texture_buffer->name(); +} + +GLenum mp_GlTextureBuffer__target(mediapipe::GlTextureBuffer* gl_texture_buffer) { + return gl_texture_buffer->target(); +} + +int mp_GlTextureBuffer__width(mediapipe::GlTextureBuffer* gl_texture_buffer) { + return gl_texture_buffer->width(); +} + +int mp_GlTextureBuffer__height(mediapipe::GlTextureBuffer* gl_texture_buffer) { + return gl_texture_buffer->height(); +} + +mediapipe::GpuBufferFormat mp_GlTextureBuffer__format(mediapipe::GlTextureBuffer* gl_texture_buffer) { + return gl_texture_buffer->format(); +} + +MpReturnCode mp_GlTextureBuffer__WaitUntilComplete(mediapipe::GlTextureBuffer* gl_texture_buffer) { + TRY_ALL { + gl_texture_buffer->WaitUntilComplete(); + RETURN_CODE(MpReturnCode::Success); + } CATCH_ALL +} + +MpReturnCode mp_GlTextureBuffer__WaitOnGpu(mediapipe::GlTextureBuffer* gl_texture_buffer) { + TRY_ALL { + gl_texture_buffer->WaitOnGpu(); + RETURN_CODE(MpReturnCode::Success); + } CATCH_ALL +} + +MpReturnCode mp_GlTextureBuffer__Reuse(mediapipe::GlTextureBuffer* gl_texture_buffer) { + TRY_ALL { + gl_texture_buffer->Reuse(); + RETURN_CODE(MpReturnCode::Success); + } CATCH_ALL +} + +MpReturnCode mp_GlTextureBuffer__Updated__Pgst(mediapipe::GlTextureBuffer* gl_texture_buffer, mediapipe::GlSyncToken* prod_token) { + TRY_ALL { + gl_texture_buffer->Updated(*prod_token); + RETURN_CODE(MpReturnCode::Success); + } CATCH_ALL +} + +MpReturnCode mp_GlTextureBuffer__DidRead__Pgst(mediapipe::GlTextureBuffer* gl_texture_buffer, mediapipe::GlSyncToken* cons_token) { + TRY_ALL { + gl_texture_buffer->DidRead(*cons_token); + RETURN_CODE(MpReturnCode::Success); + } CATCH_ALL +} + +MpReturnCode mp_GlTextureBuffer__WaitForConsumers(mediapipe::GlTextureBuffer* gl_texture_buffer) { + TRY_ALL { + gl_texture_buffer->WaitForConsumers(); + RETURN_CODE(MpReturnCode::Success); + } CATCH_ALL +} + +MpReturnCode mp_GlTextureBuffer__WaitForConsumersOnGpu(mediapipe::GlTextureBuffer* gl_texture_buffer) { + TRY_ALL { + gl_texture_buffer->WaitForConsumersOnGpu(); + RETURN_CODE(MpReturnCode::Success); + } CATCH_ALL +} + +const SharedGlContext& mp_GlTextureBuffer__GetProducerContext(mediapipe::GlTextureBuffer* gl_texture_buffer) { + return gl_texture_buffer->GetProducerContext(); +} diff --git a/C/mediapipe_api/gpu/gl_texture_buffer.h b/C/mediapipe_api/gpu/gl_texture_buffer.h index 50602ed8e..1f8cb3585 100644 --- a/C/mediapipe_api/gpu/gl_texture_buffer.h +++ b/C/mediapipe_api/gpu/gl_texture_buffer.h @@ -25,6 +25,21 @@ MP_CAPI(MpReturnCode) mp_SharedGlTextureBuffer__ui_ui_i_i_ui_PF_PSgc(GLenum targ std::shared_ptr* producer_context, SharedGlTextureBuffer** gl_texture_buffer_out); +MP_CAPI(GLuint) mp_GlTextureBuffer__name(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(GLenum) mp_GlTextureBuffer__target(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(int) mp_GlTextureBuffer__width(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(int) mp_GlTextureBuffer__height(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(mediapipe::GpuBufferFormat) mp_GlTextureBuffer__format(mediapipe::GlTextureBuffer* gl_texture_buffer); + +MP_CAPI(MpReturnCode) mp_GlTextureBuffer__WaitUntilComplete(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(MpReturnCode) mp_GlTextureBuffer__WaitOnGpu(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(MpReturnCode) mp_GlTextureBuffer__Reuse(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(MpReturnCode) mp_GlTextureBuffer__Updated__Pgst(mediapipe::GlTextureBuffer* gl_texture_buffer, mediapipe::GlSyncToken* prod_token); +MP_CAPI(MpReturnCode) mp_GlTextureBuffer__DidRead__Pgst(mediapipe::GlTextureBuffer* gl_texture_buffer, mediapipe::GlSyncToken* cons_token); +MP_CAPI(MpReturnCode) mp_GlTextureBuffer__WaitForConsumers(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(MpReturnCode) mp_GlTextureBuffer__WaitForConsumersOnGpu(mediapipe::GlTextureBuffer* gl_texture_buffer); +MP_CAPI(const SharedGlContext&) mp_GlTextureBuffer__GetProducerContext(mediapipe::GlTextureBuffer* gl_texture_buffer); + } // extern "C" #endif // C_MEDIAPIPE_API_GPU_GL_TEXTURE_BUFFER_H_ diff --git a/C/mediapipe_api/gpu/gpu_buffer.cc b/C/mediapipe_api/gpu/gpu_buffer.cc index 4f95066c0..48c51830c 100644 --- a/C/mediapipe_api/gpu/gpu_buffer.cc +++ b/C/mediapipe_api/gpu/gpu_buffer.cc @@ -12,6 +12,10 @@ void mp_GpuBuffer__delete(mediapipe::GpuBuffer* gpu_buffer) { delete gpu_buffer; } +const SharedGlTextureBuffer& mp_GpuBuffer__GetGlTextureBufferSharedPtr(mediapipe::GpuBuffer* gpu_buffer) { + return gpu_buffer->GetGlTextureBufferSharedPtr(); +} + int mp_GpuBuffer__width(mediapipe::GpuBuffer* gpu_buffer) { return gpu_buffer->width(); } diff --git a/C/mediapipe_api/gpu/gpu_buffer.h b/C/mediapipe_api/gpu/gpu_buffer.h index 2051c7c1a..a713dede7 100644 --- a/C/mediapipe_api/gpu/gpu_buffer.h +++ b/C/mediapipe_api/gpu/gpu_buffer.h @@ -15,6 +15,7 @@ typedef mediapipe::StatusOr StatusOrGpuBuffer; MP_CAPI(MpReturnCode) mp_GpuBuffer__PSgtb(SharedGlTextureBuffer* gl_texture_buffer, mediapipe::GpuBuffer** gpu_buffer_out); MP_CAPI(void) mp_GpuBuffer__delete(mediapipe::GpuBuffer* gpu_buffer); +MP_CAPI(const SharedGlTextureBuffer&) mp_GpuBuffer__GetGlTextureBufferSharedPtr(mediapipe::GpuBuffer* gpu_buffer); MP_CAPI(int) mp_GpuBuffer__width(mediapipe::GpuBuffer* gpu_buffer); MP_CAPI(int) mp_GpuBuffer__height(mediapipe::GpuBuffer* gpu_buffer); MP_CAPI(mediapipe::GpuBufferFormat) mp_GpuBuffer__format(mediapipe::GpuBuffer* gpu_buffer);