Skip to content

Commit

Permalink
refactor: marshal delegate directly
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Nov 29, 2020
1 parent c66d255 commit 891acdc
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 44 deletions.
5 changes: 2 additions & 3 deletions Assets/MediaPipe/SDK/Scripts/External/Protobuf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Mediapipe {
class Protobuf {
static Protobuf() {
UnsafeNativeMethods.google_protobuf__SetLogHandler__PF(Marshal.GetFunctionPointerForDelegate(protobufLogHandler)).Assert();
UnsafeNativeMethods.google_protobuf__SetLogHandler__PF(protobufLogHandler).Assert();
}

/// <exception cref="MediaPipeException">Thrown when an error occured in unmanaged code</exception>
Expand Down Expand Up @@ -47,8 +47,7 @@ public static List<T> DeserializeProtoVector<T>(IntPtr ptr, pb::MessageParser<T>
return protos;
}

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void ProtobufLogHandler(int level, string filename, int line, string message);
public delegate void ProtobufLogHandler(int level, string filename, int line, string message);
private static readonly ProtobufLogHandler protobufLogHandler = LogProtobufMessage;

private static void LogProtobufMessage(int level, string filename, int line, string message) {
Expand Down
6 changes: 2 additions & 4 deletions Assets/MediaPipe/SDK/Scripts/Framework/Formats/ImageFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ public class ImageFrame : MpResourceHandle {
public static readonly uint kDefaultAlignmentBoundary = 16;
public static readonly uint kGlDefaultAlignmentBoundary = 4;

public delegate void Deleter(IntPtr ptr);
private GCHandle deleterHandle;

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void Deleter(IntPtr ptr);

public ImageFrame() : base() {
UnsafeNativeMethods.mp_ImageFrame__(out var ptr).Assert();
this.ptr = ptr;
Expand All @@ -36,7 +34,7 @@ public ImageFrame(ImageFormat.Format format, int width, int height, int widthSte
UnsafeNativeMethods.mp_ImageFrame__ui_i_i_i_Pui8_PF(
format, width, height, widthStep,
(IntPtr)NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr(pixelData),
Marshal.GetFunctionPointerForDelegate(deleter),
deleter,
out var ptr
).Assert();
this.ptr = ptr;
Expand Down
22 changes: 13 additions & 9 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlCalculatorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
namespace Mediapipe {

public class GlCalculatorHelper : MpResourceHandle {
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate IntPtr MpGlStatusFunction();
public delegate IntPtr NativeGlStatusFunction();
public delegate Status GlStatusFunction();

public GlCalculatorHelper() : base() {
Expand All @@ -25,19 +24,24 @@ public void InitializeForTest(GpuResources gpuResources) {
}

public Status RunInGlContext(GlStatusFunction glStatusFunc) {
MpGlStatusFunction mpGlStatusFunc = () => {
Status status = null;

NativeGlStatusFunction nativeGlStatusFunc = () => {
try {
return glStatusFunc().mpPtr;
status = glStatusFunc();
} catch (Exception e) {
return Status.FailedPrecondition(e.ToString()).mpPtr;
status = Status.FailedPrecondition(e.ToString());
}
return status.mpPtr;
};
GCHandle mpGlStatusFuncHandle = GCHandle.Alloc(mpGlStatusFunc);
UnsafeNativeMethods.mp_GlCalculatorHelper__RunInGlContext__PF(
mpPtr, Marshal.GetFunctionPointerForDelegate(mpGlStatusFunc), out var statusPtr).Assert();
mpGlStatusFuncHandle.Free();

GCHandle nativeGlStatusFuncHandle = GCHandle.Alloc(nativeGlStatusFunc, GCHandleType.Pinned);
UnsafeNativeMethods.mp_GlCalculatorHelper__RunInGlContext__PF(mpPtr, nativeGlStatusFunc, out var statusPtr).Assert();
GC.KeepAlive(this);
nativeGlStatusFuncHandle.Free();

if (status != null) { status.Dispose(); }

return new Status(statusPtr);
}

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 @@ -5,7 +5,6 @@ namespace Mediapipe {
public class GlTextureBuffer : MpResourceHandle {
private SharedPtrHandle sharedPtrHandle;

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

Expand All @@ -20,7 +19,7 @@ public GlTextureBuffer(UInt32 target, UInt32 name, int width, int height,
deletionCallbackHandle = GCHandle.Alloc(callback, GCHandleType.Pinned);
var sharedContextPtr = glContext == null ? IntPtr.Zero : glContext.sharedPtr;
UnsafeNativeMethods.mp_SharedGlTextureBuffer__ui_ui_i_i_ui_PF_PSgc(
target, name, width, height, format, Marshal.GetFunctionPointerForDelegate(callback), sharedContextPtr, out var ptr).Assert();
target, name, width, height, format, callback, sharedContextPtr, out var ptr).Assert();

this.ptr = ptr;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Runtime.InteropServices;

namespace Mediapipe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
namespace Mediapipe {
internal static partial class UnsafeNativeMethods {
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode google_protobuf__SetLogHandler__PF([MarshalAs(UnmanagedType.FunctionPtr)]IntPtr logHandler);
public static extern MpReturnCode google_protobuf__SetLogHandler__PF(
[MarshalAs(UnmanagedType.FunctionPtr)]Protobuf.ProtobufLogHandler logHandler);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp_api_SerializedProto__delete(IntPtr serializedProto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static extern MpReturnCode mp_ImageFrame__ui_i_i_ui(
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_ImageFrame__ui_i_i_i_Pui8_PF(
ImageFormat.Format format, int width, int height, int widthStep, IntPtr pixelData,
[MarshalAs(UnmanagedType.FunctionPtr)]IntPtr deleter, out IntPtr imageFrame);
[MarshalAs(UnmanagedType.FunctionPtr)]ImageFrame.Deleter deleter, out IntPtr imageFrame);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp_ImageFrame__delete(IntPtr imageFrame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static partial class UnsafeNativeMethods {

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_GlCalculatorHelper__RunInGlContext__PF(
IntPtr glCalculatorHelper, [MarshalAs(UnmanagedType.FunctionPtr)]IntPtr glFunc, out IntPtr status);
IntPtr glCalculatorHelper, [MarshalAs(UnmanagedType.FunctionPtr)]GlCalculatorHelper.NativeGlStatusFunction glFunc, out IntPtr status);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_GlCalculatorHelper__CreateSourceTexture__Rif(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal static partial class UnsafeNativeMethods {
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_SharedGlTextureBuffer__ui_ui_i_i_ui_PF_PSgc(
UInt32 target, UInt32 name, int width, int height, GpuBufferFormat format,
[MarshalAs(UnmanagedType.FunctionPtr)]IntPtr deletionCallback, IntPtr producerContext, out IntPtr sharedGlTextureBuffer);
[MarshalAs(UnmanagedType.FunctionPtr)]GlTextureBuffer.DeletionCallback deletionCallback,
IntPtr producerContext, out IntPtr sharedGlTextureBuffer);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;

namespace Mediapipe {
internal static partial class SafeNativeMethods {
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp_api__PrepareResourceManager(
[MarshalAs(UnmanagedType.FunctionPtr)]IntPtr resolver,[MarshalAs(UnmanagedType.FunctionPtr)]IntPtr handler);
[MarshalAs(UnmanagedType.FunctionPtr)]ResourceManager.CacheFilePathResolver resolver,
[MarshalAs(UnmanagedType.FunctionPtr)]ResourceManager.ReadFileHandler handler);
}
}
19 changes: 4 additions & 15 deletions Assets/MediaPipe/SDK/Scripts/Util/ResourceManager.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
using System;
using System.Runtime.InteropServices;

namespace Mediapipe {
public abstract class ResourceManager {
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate string CacheFilePathResolver(string path);
private readonly CacheFilePathResolver cacheFilePathResolver;
public delegate string CacheFilePathResolver(string path);
public readonly CacheFilePathResolver cacheFilePathResolver;

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate bool ReadFileHandler(string path, IntPtr dst);
private readonly ReadFileHandler readFileHandler;
public delegate bool ReadFileHandler(string path, IntPtr dst);
public readonly ReadFileHandler readFileHandler;

protected ResourceManager() {
cacheFilePathResolver = CacheFileFromAsset;
readFileHandler = ReadFile;
}

public IntPtr GetCacheFilePathResolverPtr() {
return Marshal.GetFunctionPointerForDelegate(cacheFilePathResolver);
}

public IntPtr GetReadFileHandlerPtr() {
return Marshal.GetFunctionPointerForDelegate(readFileHandler);
}

protected abstract string CacheFileFromAsset(string assetPath);

protected abstract bool ReadFile(string path, IntPtr dst);
Expand Down
4 changes: 1 addition & 3 deletions Assets/MediaPipe/SDK/Scripts/Util/ResourceUtil.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;

namespace Mediapipe {
public class ResourceUtil {
public static void InitializeResourceManager(ResourceManager resourceManager) {
SafeNativeMethods.mp_api__PrepareResourceManager(resourceManager.GetCacheFilePathResolverPtr(), resourceManager.GetReadFileHandlerPtr());
SafeNativeMethods.mp_api__PrepareResourceManager(resourceManager.cacheFilePathResolver, resourceManager.readFileHandler);
}
}
}

0 comments on commit 891acdc

Please sign in to comment.