diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/Gl.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/Gl.cs index 7c4dda5f2..ee31358b7 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/Gl.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/Gl.cs @@ -2,6 +2,8 @@ namespace Mediapipe { public class Gl { + public static UInt32 GL_TEXTURE_2D = 0x0DE1; + public static void Flush() { UnsafeNativeMethods.glFlush(); } diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs index 51127b45e..9b5039727 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs @@ -3,6 +3,12 @@ namespace Mediapipe { public class GlContext : MpResourceHandle { private SharedPtrHandle sharedPtrHandle; + + public static GlContext GetCurrent() { + UnsafeNativeMethods.mp_GlContext_GetCurrent(out var glContextPtr).Assert(); + + return glContextPtr == IntPtr.Zero ? null : new GlContext(glContextPtr); + } public GlContext(IntPtr ptr) : base(ptr) { sharedPtrHandle = new SharedPtr(ptr); @@ -25,10 +31,32 @@ public IntPtr sharedPtr { get { return sharedPtrHandle == null ? IntPtr.Zero : sharedPtrHandle.mpPtr; } } - public static GlContext GetCurrent() { - UnsafeNativeMethods.mp_GlContext_GetCurrent(out var glContextPtr).Assert(); + public IntPtr eglDisplay { + get { return SafeNativeMethods.mp_GlContext__egl_display(mpPtr); } + } + + public IntPtr eglConfig { + get { return SafeNativeMethods.mp_GlContext__egl_config(mpPtr); } + } + + public IntPtr eglContext { + get { return SafeNativeMethods.mp_GlContext__egl_context(mpPtr); } + } + + public bool IsCurrent() { + return SafeNativeMethods.mp_GlContext__IsCurrent(mpPtr); + } + + public int glMajorVersion { + get { return SafeNativeMethods.mp_GlContext__gl_major_version(mpPtr); } + } + + public int glMinorVersion { + get { return SafeNativeMethods.mp_GlContext__gl_minor_version(mpPtr); } + } - return new GlContext(glContextPtr); + public long glFinishCount { + get { return SafeNativeMethods.mp_GlContext__gl_finish_count(mpPtr); } } private class SharedPtr : SharedPtrHandle { diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs index 5a4ab2c19..a2196fe2b 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs @@ -3,7 +3,6 @@ namespace Mediapipe { public class GlTextureBuffer : MpResourceHandle { - private static UInt32 GL_TEXTURE_2D = 0x0DE1; private SharedPtrHandle sharedPtrHandle; [UnmanagedFunctionPointer(CallingConvention.StdCall)] @@ -27,7 +26,7 @@ public GlTextureBuffer(UInt32 target, UInt32 name, int width, int height, } public GlTextureBuffer(UInt32 name, int width, int height, GpuBufferFormat format, DeletionCallback callback, GlContext glContext = null) : - this(GL_TEXTURE_2D, name, width, height, format, callback, glContext) {} + this(Gl.GL_TEXTURE_2D, name, width, height, format, callback, glContext) {} protected override void DisposeManaged() { if (sharedPtrHandle != null) { diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs index ffb0c4aed..6e62f1a0e 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs @@ -7,6 +7,28 @@ internal static partial class SafeNativeMethods { #region GlContext [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] public static extern IntPtr mp_SharedGlContext__get(IntPtr sharedGlContext); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern IntPtr mp_GlContext__egl_display(IntPtr GlContext); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern IntPtr mp_GlContext__egl_config(IntPtr GlContext); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern IntPtr mp_GlContext__egl_context(IntPtr GlContext); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.I1)] + public static extern bool mp_GlContext__IsCurrent(IntPtr GlContext); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern int mp_GlContext__gl_major_version(IntPtr GlContext); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern int mp_GlContext__gl_minor_version(IntPtr GlContext); + + [Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern long mp_GlContext__gl_finish_count(IntPtr GlContext); #endregion #region GlSyncToken diff --git a/Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs b/Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs new file mode 100644 index 000000000..38c11f540 --- /dev/null +++ b/Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs @@ -0,0 +1,66 @@ +using Mediapipe; +using NUnit.Framework; +using System; + +namespace Tests { + public class GlContextTest { + #region .GetCurrent + [Test, GpuOnly] + public void GetCurrent_ShouldReturnNull_When_CalledOutOfGlContext() { + var glContext = GlContext.GetCurrent(); + + Assert.Null(glContext); + } + + [Test, GpuOnly] + public void GetCurrent_ShouldReturnCurrentContext_When_CalledInGlContext() { + var glCalculatorHelper = new GlCalculatorHelper(); + glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie()); + + glCalculatorHelper.RunInGlContext(() => { + var glContext = GlContext.GetCurrent(); + + Assert.NotNull(glContext); + Assert.True(glContext.IsCurrent()); + return Status.Ok(); + }).AssertOk(); + } + #endregion + + #region #IsCurrent + public void IsCurrent_ShouldReturnFalse_When_CalledOutOfGlContext() { + var glContext = GetGlContext(); + + Assert.False(glContext.IsCurrent()); + } + #endregion + + #region properties + [Test, GpuOnly] + public void ShouldReturnProperties() { + var glContext = GetGlContext(); + + Assert.AreNotEqual(glContext.eglDisplay, IntPtr.Zero); + Assert.AreNotEqual(glContext.eglConfig, IntPtr.Zero); + Assert.AreNotEqual(glContext.eglContext, IntPtr.Zero); + Assert.AreEqual(glContext.glMajorVersion, 3); + Assert.AreEqual(glContext.glMinorVersion, 2); + Assert.AreEqual(glContext.glFinishCount, 0); + } + #endregion + + private GlContext GetGlContext() { + GlContext glContext = null; + + var glCalculatorHelper = new GlCalculatorHelper(); + glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie()); + + glCalculatorHelper.RunInGlContext(() => { + glContext = GlContext.GetCurrent(); + return Status.Ok(); + }).AssertOk(); + + return glContext; + } + } +} diff --git a/Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs.meta b/Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs.meta new file mode 100644 index 000000000..0eb97b387 --- /dev/null +++ b/Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d5e3ffbb666f2d185b043064194cbf12 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MediaPipe/SDK/Tests/Gpu/GlTextureTest.cs b/Assets/MediaPipe/SDK/Tests/Gpu/GlTextureTest.cs index 81af2fa40..460cc15e8 100644 --- a/Assets/MediaPipe/SDK/Tests/Gpu/GlTextureTest.cs +++ b/Assets/MediaPipe/SDK/Tests/Gpu/GlTextureTest.cs @@ -44,7 +44,7 @@ public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed() { public void target_ShouldReturnTarget() { var glTexture = new GlTexture(); - Assert.AreNotEqual(glTexture.target, 0); + Assert.AreEqual(glTexture.target, Gl.GL_TEXTURE_2D); } #endregion } diff --git a/C/mediapipe_api/gpu/gl_context.cc b/C/mediapipe_api/gpu/gl_context.cc index 34664178d..4a79399f2 100644 --- a/C/mediapipe_api/gpu/gl_context.cc +++ b/C/mediapipe_api/gpu/gl_context.cc @@ -14,7 +14,13 @@ void mp_SharedGlContext__reset(SharedGlContext* shared_gl_context) { MpReturnCode mp_GlContext_GetCurrent(SharedGlContext** shared_gl_context_out) { TRY { - *shared_gl_context_out = new SharedGlContext { mediapipe::GlContext::GetCurrent() }; + auto gl_context = mediapipe::GlContext::GetCurrent(); + + if (gl_context.get() == nullptr) { + *shared_gl_context_out = nullptr; + } else { + *shared_gl_context_out = new SharedGlContext { gl_context }; + } RETURN_CODE(MpReturnCode::Success); } CATCH_EXCEPTION } @@ -44,6 +50,34 @@ MpReturnCode mp_GlContext_Create__ui_b(mediapipe::PlatformGlContext share_contex } CATCH_EXCEPTION } +EGLDisplay mp_GlContext__egl_display(mediapipe::GlContext* gl_context) { + return gl_context->egl_display(); +} + +EGLConfig mp_GlContext__egl_config(mediapipe::GlContext* gl_context) { + return gl_context->egl_config(); +} + +EGLContext mp_GlContext__egl_context(mediapipe::GlContext* gl_context) { + return gl_context->egl_context(); +} + +bool mp_GlContext__IsCurrent(mediapipe::GlContext* gl_context) { + return gl_context->IsCurrent(); +} + +GLint mp_GlContext__gl_major_version(mediapipe::GlContext* gl_context) { + return gl_context->gl_major_version(); +} + +GLint mp_GlContext__gl_minor_version(mediapipe::GlContext* gl_context) { + return gl_context->gl_minor_version(); +} + +int64_t mp_GlContext__gl_finish_count(mediapipe::GlContext* gl_context) { + return gl_context->gl_finish_count(); +} + // GlSyncToken API void mp_GlSyncToken__delete(mediapipe::GlSyncToken* gl_sync_token) { delete gl_sync_token; diff --git a/C/mediapipe_api/gpu/gl_context.h b/C/mediapipe_api/gpu/gl_context.h index 531b6cbaa..f8ee9411a 100644 --- a/C/mediapipe_api/gpu/gl_context.h +++ b/C/mediapipe_api/gpu/gl_context.h @@ -24,6 +24,13 @@ MP_CAPI(MpReturnCode) mp_GlContext_Create__Rgc_b(mediapipe::GlContext* share_con MP_CAPI(MpReturnCode) mp_GlContext_Create__ui_b(mediapipe::PlatformGlContext share_context, bool create_thread, StatusOrSharedGlContext** status_or_shared_gl_context_out); +MP_CAPI(EGLDisplay) mp_GlContext__egl_display(mediapipe::GlContext* gl_context); +MP_CAPI(EGLConfig) mp_GlContext__egl_config(mediapipe::GlContext* gl_context); +MP_CAPI(EGLContext) mp_GlContext__egl_context(mediapipe::GlContext* gl_context); +MP_CAPI(bool) mp_GlContext__IsCurrent(mediapipe::GlContext* gl_context); +MP_CAPI(GLint) mp_GlContext__gl_major_version(mediapipe::GlContext* gl_context); +MP_CAPI(GLint) mp_GlContext__gl_minor_version(mediapipe::GlContext* gl_context); +MP_CAPI(int64_t) mp_GlContext__gl_finish_count(mediapipe::GlContext* gl_context); // GlSyncToken API MP_CAPI(void) mp_GlSyncToken__delete(mediapipe::GlSyncToken* gl_sync_token);