Skip to content

Commit

Permalink
feat(sdk): GlContext and GlSyncToken API
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Oct 24, 2020
1 parent 3cfdcae commit 79b3e83
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 2 deletions.
31 changes: 31 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
11 changes: 11 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs.meta

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

39 changes: 39 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
11 changes: 11 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlSyncToken.cs.meta

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

3 changes: 3 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions C/mediapipe_api/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
4 changes: 4 additions & 0 deletions C/mediapipe_api/gpu/gl_context.cc
Original file line number Diff line number Diff line change
@@ -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() };
}
Expand Down
1 change: 1 addition & 0 deletions C/mediapipe_api/gpu/gl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct MpGlSyncToken {
std::shared_ptr<mediapipe::GlSyncPoint> 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);

Expand Down
3 changes: 2 additions & 1 deletion C/mediapipe_api/gpu/gl_texture_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<mediapipe::GlTextureBuffer>(
GL_TEXTURE_2D,
Expand Down
3 changes: 2 additions & 1 deletion C/mediapipe_api/gpu/gl_texture_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include <utility>
#include "mediapipe/gpu/gl_texture_buffer.h"
#include "mediapipe_api/common.h"
#include "mediapipe_api/gpu/gl_context.h"

extern "C" {

typedef struct MpGlTextureBuffer {
std::shared_ptr<mediapipe::GlTextureBuffer> 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);
Expand Down

0 comments on commit 79b3e83

Please sign in to comment.