From 79b3e83983e37a77b9cedb76a7e90a1218317ea7 Mon Sep 17 00:00:00 2001 From: homuler Date: Sat, 24 Oct 2020 14:17:19 +0900 Subject: [PATCH] feat(sdk): GlContext and GlSyncToken API --- Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs | 31 +++++++++++++++ .../SDK/Scripts/Gpu/GlContext.cs.meta | 11 ++++++ .../MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs | 39 +++++++++++++++++++ .../SDK/Scripts/Gpu/GlSyncToken.cs.meta | 11 ++++++ .../SDK/Scripts/UnsafeNativeMethods.cs | 3 ++ C/mediapipe_api/gpu/BUILD | 1 + C/mediapipe_api/gpu/gl_context.cc | 4 ++ C/mediapipe_api/gpu/gl_context.h | 1 + C/mediapipe_api/gpu/gl_texture_buffer.cc | 3 +- C/mediapipe_api/gpu/gl_texture_buffer.h | 3 +- 10 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs create mode 100644 Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs.meta create mode 100644 Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs create mode 100644 Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs.meta diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs new file mode 100644 index 000000000..f1e1e2a71 --- /dev/null +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs @@ -0,0 +1,31 @@ +using System; + +using MpGlContext = System.IntPtr; + +namespace Mediapipe { + public class GlContext : ResourceHandle { + private bool _disposed = false; + + public GlContext(MpGlContext ptr, bool isOwner = true) : base(ptr, isOwner) {} + + protected override void Dispose(bool disposing) { + if (_disposed) return; + + if (OwnsResource()) { + UnsafeNativeMethods.MpGlContextDestroy(ptr); + } + + ptr = IntPtr.Zero; + + _disposed = true; + } + + public IntPtr Get() { + return UnsafeNativeMethods.MpGlContextGet(ptr); + } + + public static GlContext GetCurrent() { + return new GlContext(UnsafeNativeMethods.MpGlContextGetCurrent()); + } + } +} diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs.meta b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs.meta new file mode 100644 index 000000000..02b042ed7 --- /dev/null +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9326248fed7e3b84f91321864c9e94a2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs new file mode 100644 index 000000000..e5b92f342 --- /dev/null +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs @@ -0,0 +1,39 @@ +using System; + +using MpGlSyncToken = System.IntPtr; + +namespace Mediapipe { + public class GlSyncToken : ResourceHandle { + private bool _disposed = false; + + public GlSyncToken(MpGlSyncToken ptr, bool isOwner = true) : base(ptr, isOwner) {} + + protected override void Dispose(bool disposing) { + if (_disposed) return; + + if (OwnsResource()) { + UnsafeNativeMethods.MpGlSyncTokenDestroy(ptr); + } + + ptr = IntPtr.Zero; + + _disposed = true; + } + + public void Wait() { + UnsafeNativeMethods.MpGlSyncTokenWait(ptr); + } + + public void WaitOnGpu() { + UnsafeNativeMethods.MpGlSyncTokenWaitOnGpu(ptr); + } + + public bool IsReady() { + return UnsafeNativeMethods.MpGlSyncTokenIsReady(ptr); + } + + public GlContext GetContext() { + return new GlContext(UnsafeNativeMethods.MpGlSyncTokenGetContext(ptr)); + } + } +} diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs.meta b/Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs.meta new file mode 100644 index 000000000..0e9888685 --- /dev/null +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eabbc070c810072d8829a54176c17947 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MediaPipe/SDK/Scripts/UnsafeNativeMethods.cs b/Assets/MediaPipe/SDK/Scripts/UnsafeNativeMethods.cs index 8ac588915..5c46b208a 100644 --- a/Assets/MediaPipe/SDK/Scripts/UnsafeNativeMethods.cs +++ b/Assets/MediaPipe/SDK/Scripts/UnsafeNativeMethods.cs @@ -136,6 +136,9 @@ internal static class UnsafeNativeMethods { /// GlContext API + [DllImport (MediaPipeLibrary)] + public static extern unsafe void MpGlContextDestroy(MpGlContext glContext); + [DllImport (MediaPipeLibrary)] public static extern unsafe MpGlContext MpGlContextGetCurrent(); diff --git a/C/mediapipe_api/gpu/BUILD b/C/mediapipe_api/gpu/BUILD index 91d0529ae..5be849135 100644 --- a/C/mediapipe_api/gpu/BUILD +++ b/C/mediapipe_api/gpu/BUILD @@ -41,6 +41,7 @@ cc_library( srcs = ["gl_texture_buffer.cc"], hdrs = ["gl_texture_buffer.h"], deps = [ + ":gl_context", "//mediapipe_api:common", "@com_google_mediapipe//mediapipe/gpu:gl_texture_buffer", ], diff --git a/C/mediapipe_api/gpu/gl_context.cc b/C/mediapipe_api/gpu/gl_context.cc index 857bf029a..63b5eb0f8 100644 --- a/C/mediapipe_api/gpu/gl_context.cc +++ b/C/mediapipe_api/gpu/gl_context.cc @@ -1,5 +1,9 @@ #include "mediapipe_api/gpu/gl_context.h" +void MpGlContextDestroy(MpGlContext* gl_context) { + delete gl_context; +} + MpGlContext* MpGlContextGetCurrent() { return new MpGlContext { mediapipe::GlContext::GetCurrent() }; } diff --git a/C/mediapipe_api/gpu/gl_context.h b/C/mediapipe_api/gpu/gl_context.h index 7eb308e17..81b1950b2 100644 --- a/C/mediapipe_api/gpu/gl_context.h +++ b/C/mediapipe_api/gpu/gl_context.h @@ -16,6 +16,7 @@ typedef struct MpGlSyncToken { std::shared_ptr impl; } MpGlSyncToken; +MP_CAPI_EXPORT extern void MpGlContextDestroy(MpGlContext* gl_context); MP_CAPI_EXPORT extern MpGlContext* MpGlContextGetCurrent(); MP_CAPI_EXPORT extern mediapipe::GlContext* MpGlContextGet(MpGlContext* gl_context); diff --git a/C/mediapipe_api/gpu/gl_texture_buffer.cc b/C/mediapipe_api/gpu/gl_texture_buffer.cc index 5d27c3f3f..125a74ec0 100644 --- a/C/mediapipe_api/gpu/gl_texture_buffer.cc +++ b/C/mediapipe_api/gpu/gl_texture_buffer.cc @@ -3,7 +3,8 @@ MpGlTextureBuffer* MpGlTextureBufferCreate(GLenum target, GLuint name, int width, int height, uint32_t format_code, MpDeletionCallback* deletion_callback, mediapipe::GlContext* producer_context /* =nullptr */) { auto callback = [&deletion_callback](mediapipe::GlSyncToken token) -> void { - deletion_callback(token.get()); + auto mpToken = new MpGlSyncToken { token }; + deletion_callback(mpToken); }; auto buffer = std::make_shared( GL_TEXTURE_2D, diff --git a/C/mediapipe_api/gpu/gl_texture_buffer.h b/C/mediapipe_api/gpu/gl_texture_buffer.h index 40bebc1bd..a1d1ea0d0 100644 --- a/C/mediapipe_api/gpu/gl_texture_buffer.h +++ b/C/mediapipe_api/gpu/gl_texture_buffer.h @@ -5,6 +5,7 @@ #include #include "mediapipe/gpu/gl_texture_buffer.h" #include "mediapipe_api/common.h" +#include "mediapipe_api/gpu/gl_context.h" extern "C" { @@ -12,7 +13,7 @@ typedef struct MpGlTextureBuffer { std::shared_ptr impl; } MpGlTextureBuffer; -typedef void MpDeletionCallback(mediapipe::GlSyncPoint* gl_sync_point); +typedef void MpDeletionCallback(MpGlSyncToken* gl_sync_token); MP_CAPI_EXPORT extern MpGlTextureBuffer* MpGlTextureBufferCreate(GLenum target, GLuint name, int width, int height, uint32_t format_code, MpDeletionCallback* deletion_callback, mediapipe::GlContext* producer_context = nullptr);