Skip to content

Commit

Permalink
feat(sdk): implement GlTextureBuffer API
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Oct 18, 2020
1 parent 9f8435e commit 31d3deb
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 73 deletions.
52 changes: 52 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Runtime.InteropServices;

using MpGlContext = System.IntPtr;
using MpGlTextureBuffer = System.IntPtr;
using GlSyncTokenPtr = System.IntPtr;

namespace Mediapipe {
public class GlTextureBuffer : ResourceHandle {
private static UInt32 GL_TEXTURE_2D = 0x0DE1;

private bool _disposed = false;

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void DeletionCallback(GlSyncTokenPtr ptr);
private GCHandle deletionCallbackHandle;

private GlTextureBuffer(MpGlTextureBuffer ptr) : base(ptr) {}

public GlTextureBuffer(UInt32 target, UInt32 name, int width, int height,
GpuBufferFormat format, DeletionCallback callback, MpGlContext glContext)
{
deletionCallbackHandle = GCHandle.Alloc(callback, GCHandleType.Pinned);
ptr = UnsafeNativeMethods.MpGlTextureBufferCreate(
target, name, width, height, (UInt32)format, Marshal.GetFunctionPointerForDelegate(callback), glContext);

base.TakeOwnership(ptr);
}

public GlTextureBuffer(UInt32 name, int width, int height, GpuBufferFormat format, DeletionCallback callback, MpGlContext glContext) :
this(GL_TEXTURE_2D, name, width, height, format, callback, glContext) {}

public GlTextureBuffer(UInt32 name, int width, int height, GpuBufferFormat format, DeletionCallback callback) :
this(name, width, height, format, callback, IntPtr.Zero) {}

protected override void Dispose(bool disposing) {
if (_disposed) return;

if (OwnsResource()) {
UnsafeNativeMethods.MpGlTextureBufferDestroy(ptr);
}

if (deletionCallbackHandle != null && deletionCallbackHandle.IsAllocated) {
deletionCallbackHandle.Free();
}

ptr = IntPtr.Zero;

_disposed = true;
}
}
}
11 changes: 11 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlTextureBuffer.cs.meta

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

14 changes: 14 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using MpGlCalculatorHelper = System.IntPtr;
using MpGlContext = System.IntPtr;
using MpGlSyncToken = System.IntPtr;
using MpGlTextureBuffer = System.IntPtr;
using MpGpuResources = System.IntPtr;
using MpLandmarkList = System.IntPtr;
using MpLandmarkListVector = System.IntPtr;
Expand All @@ -30,6 +31,7 @@
using CacheFilePathResolverPtr = System.IntPtr;
using GlContextPtr = System.IntPtr;
using GlTexturePtr = System.IntPtr;
using GlTextureBufferDeletionCallbackPtr = System.IntPtr;
using GlTextureInfoPtr = System.IntPtr;
using GlStatusFunctionPtr = System.IntPtr;
using GpuBufferPtr = System.IntPtr;
Expand Down Expand Up @@ -175,12 +177,24 @@ internal static class UnsafeNativeMethods {
public static extern unsafe GpuBufferPtr MpGlTextureGetGpuBufferFrame(GlTexturePtr glTexture);


/// GlTextureBuffer API
[DllImport (MediaPipeLibrary)]
public static extern unsafe MpGlTextureBuffer MpGlTextureBufferCreate(UInt32 target, UInt32 name, int width, int height, UInt32 formatCode,
[MarshalAs(UnmanagedType.FunctionPtr)]GlTextureBufferDeletionCallbackPtr callbackPtr, MpGlContext producerContext);

[DllImport (MediaPipeLibrary)]
public static extern unsafe void MpGlTextureBufferDestroy(MpGlTextureBuffer glTextureBuffer);


/// GlTextureInfo API
[DllImport (MediaPipeLibrary)]
public static extern unsafe int MpGlTextureInfoDestroy(GlTextureInfoPtr glTextureInfo);


/// GpuBuffer API
[DllImport (MediaPipeLibrary)]
public static extern unsafe GpuBufferPtr MpGpuBufferCreate(MpGlTextureBuffer glTextureBuffer);

[DllImport (MediaPipeLibrary)]
public static extern unsafe void MpGpuBufferDestroy(GpuBufferPtr gpuBuffer);

Expand Down
46 changes: 35 additions & 11 deletions C/mediapipe_api/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ cc_library(
srcs = ["gl_base.cc"],
hdrs = ["gl_base.h"],
deps = [
"//mediapipe_api:common",
"@com_google_mediapipe//mediapipe/gpu:gl_base",
],
alwayslink = True,
)

cc_library(
name = "gl_calculator_helper",
srcs = ["gl_calculator_helper.cc"],
hdrs = ["gl_calculator_helper.h"],
deps = [
"//mediapipe_api:common",
"//mediapipe_api/framework/port:status",
"@com_google_mediapipe//mediapipe/gpu:gl_calculator_helper",
],
alwayslink = True,
)
Expand All @@ -18,35 +30,34 @@ cc_library(
srcs = ["gl_context.cc"],
hdrs = ["gl_context.h"],
deps = [
"@com_google_mediapipe//mediapipe/gpu:gl_context",
"//mediapipe_api:common",
"@com_google_mediapipe//mediapipe/gpu:gl_context",
],
alwayslink = True,
)

cc_library(
name = "gpu_shared_data_internal",
srcs = ["gpu_shared_data_internal.cc"],
hdrs = ["gpu_shared_data_internal.h"],
name = "gl_texture_buffer",
srcs = ["gl_texture_buffer.cc"],
hdrs = ["gl_texture_buffer.h"],
deps = [
"@com_google_mediapipe//mediapipe/gpu:gpu_shared_data_internal",
"//mediapipe_api:common",
"//mediapipe_api/framework/port:status",
"//mediapipe_api/framework/port:statusor",
"@com_google_mediapipe//mediapipe/gpu:gl_texture_buffer",
],
alwayslink = True,
)

cc_library(
name = "gl_calculator_helper",
srcs = ["gl_calculator_helper.cc"],
hdrs = ["gl_calculator_helper.h"],
name = "gpu_buffer",
srcs = ["gpu_buffer.cc"],
hdrs = ["gpu_buffer.h"],
deps = [
"@com_google_mediapipe//mediapipe/gpu:gl_calculator_helper",
":gl_texture_buffer",
"//mediapipe_api:common",
"//mediapipe_api/framework:packet",
"//mediapipe_api/framework/port:status",
"//mediapipe_api/framework/port:statusor",
"@com_google_mediapipe//mediapipe/gpu:gpu_buffer",
],
alwayslink = True,
)
Expand All @@ -56,8 +67,21 @@ cc_library(
srcs = ["gpu_buffer_format.cc"],
hdrs = ["gpu_buffer_format.h"],
deps = [
"//mediapipe_api:common",
"@com_google_mediapipe//mediapipe/gpu:gpu_buffer_format",
],
alwayslink = True,
)

cc_library(
name = "gpu_shared_data_internal",
srcs = ["gpu_shared_data_internal.cc"],
hdrs = ["gpu_shared_data_internal.h"],
deps = [
"//mediapipe_api:common",
"//mediapipe_api/framework/port:status",
"//mediapipe_api/framework/port:statusor",
"@com_google_mediapipe//mediapipe/gpu:gpu_shared_data_internal",
],
alwayslink = True,
)
46 changes: 0 additions & 46 deletions C/mediapipe_api/gpu/gl_calculator_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,52 +53,6 @@ void MpGlTextureRelease(mediapipe::GlTexture* gl_texture) {
gl_texture->Release();
}

void MpGpuBufferDestroy(mediapipe::GpuBuffer* gpu_buffer) {
delete gpu_buffer;
}

uint32_t MpGpuBufferFormat(mediapipe::GpuBuffer* gpu_buffer) {
return static_cast<uint32_t>(gpu_buffer->format());
}

int MpGpuBufferWidth(mediapipe::GpuBuffer* gpu_buffer) {
return gpu_buffer->width();
}

int MpGpuBufferHeight(mediapipe::GpuBuffer* gpu_buffer) {
return gpu_buffer->height();
}

mediapipe::GpuBuffer* MpGlTextureGetGpuBufferFrame(mediapipe::GlTexture* gl_texture) {
return gl_texture->GetFrame<mediapipe::GpuBuffer>().release();
}

MpPacket* MpMakeGpuBufferPacketAt(mediapipe::GpuBuffer* gpu_buffer, int timestamp) {
auto packet = mediapipe::Adopt(gpu_buffer).At(mediapipe::Timestamp(timestamp));

return new MpPacket { std::move(packet) };
}

mediapipe::GpuBuffer* MpPacketGetGpuBuffer(MpPacket* packet) {
auto holder = static_cast<const UnsafePacketHolder<mediapipe::GpuBuffer>*>(mediapipe::packet_internal::GetHolder(*packet->impl));

return holder->Get();
}

MpStatusOrGpuBuffer* MpPacketConsumeGpuBuffer(MpPacket* packet) {
auto status_or_gpu_buffer = packet->impl->Consume<mediapipe::GpuBuffer>();

return new MpStatusOrGpuBuffer { std::move(status_or_gpu_buffer) };
}

void MpStatusOrGpuBufferDestroy(MpStatusOrGpuBuffer* status_or_gpu_buffer) {
delete status_or_gpu_buffer;
}

MpStatus* MpStatusOrGpuBufferStatus(MpStatusOrGpuBuffer* status_or_gpu_buffer) {
return new MpStatus { *status_or_gpu_buffer->status };
}

mediapipe::GpuBuffer* MpStatusOrGpuBufferConsumeValue(MpStatusOrGpuBuffer* gpu_buffer) {
return gpu_buffer->value.release();
}
16 changes: 0 additions & 16 deletions C/mediapipe_api/gpu/gl_calculator_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#include <memory>
#include "mediapipe/gpu/gl_calculator_helper.h"
#include "mediapipe_api/common.h"
#include "mediapipe_api/framework/packet.h"
#include "mediapipe_api/framework/port/status.h"
#include "mediapipe_api/framework/port/statusor.h"

extern "C" {

Expand All @@ -16,7 +14,6 @@ typedef struct MpGlCalculatorHelper {
MpGlCalculatorHelper() : impl { std::make_unique<mediapipe::GlCalculatorHelper>() } {}
} MpGlCalculatorHelper;

typedef MpStatusOrValue<std::unique_ptr<mediapipe::GpuBuffer>> MpStatusOrGpuBuffer;
typedef MpStatus* MpGlStatusFunction();

/** GlCalculatorHelper API */
Expand All @@ -37,19 +34,6 @@ MP_CAPI_EXPORT extern int MpGlTextureHeight(mediapipe::GlTexture* gl_texture);
MP_CAPI_EXPORT extern void MpGlTextureRelease(mediapipe::GlTexture* gl_texture);
MP_CAPI_EXPORT extern mediapipe::GpuBuffer* MpGlTextureGetGpuBufferFrame(mediapipe::GlTexture* gl_texture);

/** GpuBuffer API */
MP_CAPI_EXPORT extern void MpGpuBufferDestroy(mediapipe::GpuBuffer* gpu_buffer);
MP_CAPI_EXPORT extern uint32_t MpGpuBufferFormat(mediapipe::GpuBuffer* gpu_buffer);
MP_CAPI_EXPORT extern int MpGpuBufferWidth(mediapipe::GpuBuffer* gpu_buffer);
MP_CAPI_EXPORT extern int MpGpuBufferHeight(mediapipe::GpuBuffer* gpu_buffer);

MP_CAPI_EXPORT extern MpPacket* MpMakeGpuBufferPacketAt(mediapipe::GpuBuffer* gpu_buffer, int timestamp);
MP_CAPI_EXPORT extern mediapipe::GpuBuffer* MpPacketGetGpuBuffer(MpPacket* packet);
MP_CAPI_EXPORT extern MpStatusOrGpuBuffer* MpPacketConsumeGpuBuffer(MpPacket* packet);
MP_CAPI_EXPORT extern void MpStatusOrGpuBufferDestroy(MpStatusOrGpuBuffer* status_or_gpu_buffer);
MP_CAPI_EXPORT extern MpStatus* MpStatusOrGpuBufferStatus(MpStatusOrGpuBuffer* status_or_gpu_buffer);
MP_CAPI_EXPORT extern mediapipe::GpuBuffer* MpStatusOrGpuBufferConsumeValue(MpStatusOrGpuBuffer* status_or_gpu_buffer);

} // extern "C"

#endif // C_MEDIAPIPE_API_GPU_GL_CALCULATOR_HELPER_H_
22 changes: 22 additions & 0 deletions C/mediapipe_api/gpu/gl_texture_buffer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "mediapipe_api/gpu/gl_texture_buffer.h"

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 buffer = std::make_shared<mediapipe::GlTextureBuffer>(
GL_TEXTURE_2D,
name,
width,
height,
static_cast<mediapipe::GpuBufferFormat>(format_code),
callback,
std::shared_ptr<mediapipe::GlContext>(producer_context));

return new MpGlTextureBuffer { std::move(buffer) };
}

void MpGlTextureBufferDestroy(MpGlTextureBuffer* gl_texture_buffer) {
delete gl_texture_buffer;
}
24 changes: 24 additions & 0 deletions C/mediapipe_api/gpu/gl_texture_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef C_MEDIAPIPE_API_GPU_GL_TEXTURE_BUFFER_H_
#define C_MEDIAPIPE_API_GPU_GL_TEXTURE_BUFFER_H_

#include <memory>
#include <utility>
#include "mediapipe/gpu/gl_texture_buffer.h"
#include "mediapipe_api/common.h"

extern "C" {

typedef struct MpGlTextureBuffer {
std::shared_ptr<mediapipe::GlTextureBuffer> impl;
} MpGlTextureBuffer;

typedef void MpDeletionCallback(mediapipe::GlSyncPoint* gl_sync_point);

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

MP_CAPI_EXPORT extern void MpGlTextureBufferDestroy(MpGlTextureBuffer* gl_texture_buffer);

} // extern "C"

#endif // C_MEDIAPIPE_API_GPU_GL_TEXTURE_BUFFER_H_
52 changes: 52 additions & 0 deletions C/mediapipe_api/gpu/gpu_buffer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <utility>
#include "mediapipe_api/gpu/gpu_buffer.h"

mediapipe::GpuBuffer* MpGpuBufferCreate(MpGlTextureBuffer* gl_texture_buffer) {
return new mediapipe::GpuBuffer { gl_texture_buffer->impl };
}

void MpGpuBufferDestroy(mediapipe::GpuBuffer* gpu_buffer) {
delete gpu_buffer;
}

uint32_t MpGpuBufferFormat(mediapipe::GpuBuffer* gpu_buffer) {
return static_cast<uint32_t>(gpu_buffer->format());
}

int MpGpuBufferWidth(mediapipe::GpuBuffer* gpu_buffer) {
return gpu_buffer->width();
}

int MpGpuBufferHeight(mediapipe::GpuBuffer* gpu_buffer) {
return gpu_buffer->height();
}

MpPacket* MpMakeGpuBufferPacketAt(mediapipe::GpuBuffer* gpu_buffer, int timestamp) {
auto packet = mediapipe::Adopt(gpu_buffer).At(mediapipe::Timestamp(timestamp));

return new MpPacket { std::move(packet) };
}

mediapipe::GpuBuffer* MpPacketGetGpuBuffer(MpPacket* packet) {
auto holder = static_cast<const UnsafePacketHolder<mediapipe::GpuBuffer>*>(mediapipe::packet_internal::GetHolder(*packet->impl));

return holder->Get();
}

MpStatusOrGpuBuffer* MpPacketConsumeGpuBuffer(MpPacket* packet) {
auto status_or_gpu_buffer = packet->impl->Consume<mediapipe::GpuBuffer>();

return new MpStatusOrGpuBuffer { std::move(status_or_gpu_buffer) };
}

void MpStatusOrGpuBufferDestroy(MpStatusOrGpuBuffer* status_or_gpu_buffer) {
delete status_or_gpu_buffer;
}

MpStatus* MpStatusOrGpuBufferStatus(MpStatusOrGpuBuffer* status_or_gpu_buffer) {
return new MpStatus { *status_or_gpu_buffer->status };
}

mediapipe::GpuBuffer* MpStatusOrGpuBufferConsumeValue(MpStatusOrGpuBuffer* gpu_buffer) {
return gpu_buffer->value.release();
}
Loading

0 comments on commit 31d3deb

Please sign in to comment.