Skip to content

Commit

Permalink
feat(sdk): add GlTextureBuffer APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Dec 28, 2020
1 parent 7a340f9 commit 1643620
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Assets/MediaPipe/SDK/Scripts/Core/SharedPtrHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

/// <returns>The owning pointer</returns>
public abstract IntPtr Get();
Expand Down
6 changes: 3 additions & 3 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class GlContext : MpResourceHandle {
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();
}

Expand Down Expand Up @@ -60,7 +60,7 @@ public class GlContext : MpResourceHandle {
}

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

Expand Down Expand Up @@ -57,8 +57,60 @@ public class GlTextureBuffer : MpResourceHandle {
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);
Expand Down
6 changes: 5 additions & 1 deletion Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ public class GpuBuffer : MpResourceHandle {

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;
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -14,5 +38,6 @@ internal static partial class UnsafeNativeMethods {
UInt32 target, UInt32 name, int width, int height, GpuBufferFormat format,
[MarshalAs(UnmanagedType.FunctionPtr)]GlTextureBuffer.DeletionCallback deletionCallback,
IntPtr producerContext, out IntPtr sharedGlTextureBuffer);
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
73 changes: 73 additions & 0 deletions C/mediapipe_api/gpu/gl_texture_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
15 changes: 15 additions & 0 deletions C/mediapipe_api/gpu/gl_texture_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ MP_CAPI(MpReturnCode) mp_SharedGlTextureBuffer__ui_ui_i_i_ui_PF_PSgc(GLenum targ
std::shared_ptr<mediapipe::GlContext>* 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_
4 changes: 4 additions & 0 deletions C/mediapipe_api/gpu/gpu_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions C/mediapipe_api/gpu/gpu_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef mediapipe::StatusOr<mediapipe::GpuBuffer> 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);
Expand Down

0 comments on commit 1643620

Please sign in to comment.