-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): implement GlTextureBuffer API
- Loading branch information
Showing
10 changed files
with
241 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.