Skip to content

Commit

Permalink
feat: add GlContext APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Nov 22, 2020
1 parent f49b5bb commit 755a643
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/Gl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mediapipe {
public class Gl {
public static UInt32 GL_TEXTURE_2D = 0x0DE1;

public static void Flush() {
UnsafeNativeMethods.glFlush();
}
Expand Down
34 changes: 31 additions & 3 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -25,10 +31,32 @@ public class GlContext : MpResourceHandle {
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 {
Expand Down
3 changes: 1 addition & 2 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Mediapipe {
public class GlTextureBuffer : MpResourceHandle {
private static UInt32 GL_TEXTURE_2D = 0x0DE1;
private SharedPtrHandle sharedPtrHandle;

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
Expand All @@ -27,7 +26,7 @@ public class GlTextureBuffer : MpResourceHandle {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions Assets/MediaPipe/SDK/Tests/Gpu/GlContextText.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/MediaPipe/SDK/Tests/Gpu/GlTextureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GlTextureTest {
public void target_ShouldReturnTarget() {
var glTexture = new GlTexture();

Assert.AreNotEqual(glTexture.target, 0);
Assert.AreEqual(glTexture.target, Gl.GL_TEXTURE_2D);
}
#endregion
}
Expand Down
36 changes: 35 additions & 1 deletion C/mediapipe_api/gpu/gl_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions C/mediapipe_api/gpu/gl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 755a643

Please sign in to comment.