Skip to content

Commit

Permalink
fix: correct compilation conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Feb 6, 2021
1 parent 94deaba commit b787ee3
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Assets/MediaPipe/Examples/Scripts/DemoGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {
public Status PushInput(TextureFrame textureFrame) {
var timestamp = GetCurrentTimestamp();

#if !UNITY_ANDROID
#if UNITY_EDITOR || !UNITY_ANDROID
var imageFrame = new ImageFrame(
ImageFormat.Format.SRGBA, textureFrame.width, textureFrame.height, 4 * textureFrame.width, textureFrame.GetRawNativeByteArray());
textureFrame.Release();
Expand Down
8 changes: 4 additions & 4 deletions Assets/MediaPipe/Examples/Scripts/SceneDirector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public class SceneDirector : MonoBehaviour {
bool IsAssetLoadFailed = false;

void OnEnable() {
// Uncomment for debugging
// System.Environment.SetEnvironmentVariable("GLOG_v", "2");
// for debugging
System.Environment.SetEnvironmentVariable("GLOG_v", "2");
}

#if UNITY_ANDROID
#if (UNITY_STANDALONE_LINUX || UNITY_ANDROID) && !UNITY_EDITOR_OSX && !UNITY_EDITOR_WIN
[AOT.MonoPInvokeCallback(typeof(PluginCallback))]
static void GetCurrentContext(int eventId) {
currentContext = Egl.getCurrentContext();
Expand All @@ -42,7 +42,7 @@ public class SceneDirector : MonoBehaviour {
async void Start() {
webCamScreen = GameObject.Find("WebCamScreen");

#if UNITY_ANDROID
#if UNITY_ANDROID && !UNITY_EDITOR_OSX && !UNITY_EDITOR_WIN
if (useGPU) {
PluginCallback callback = GetCurrentContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class WebCamScreenController : MonoBehaviour {
public void DrawScreen(GpuBuffer gpuBuffer) {
if (!isWebCamReady) { return; }

#if UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || UNITY_ANDROID
#if (UNITY_STANDALONE_LINUX || UNITY_ANDROID) && !UNITY_EDITOR_OSX && !UNITY_EDITOR_WIN
// TODO: create an external texture
outputTexture.UpdateExternalTexture((IntPtr)gpuBuffer.GetGlTextureBuffer().Name());
#else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if UNITY_STANDALONE_LINUX || UNITY_ANDROID
using System;

#if UNITY_STANDALONE_LINUX || UNITY_ANDROID
// defined on Linux, but usefull only with OpenGL ES
namespace Mediapipe {
public class EglSurfaceHolderPacket : Packet<EglSurfaceHolder> {
public EglSurfaceHolderPacket() : base() {}
Expand Down Expand Up @@ -32,4 +33,4 @@ public class EglSurfaceHolderPacket : Packet<EglSurfaceHolder> {
}
}
}
#endif
#endif
3 changes: 2 additions & 1 deletion Assets/MediaPipe/SDK/Scripts/Gpu/EglSurfaceHolder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;

#if UNITY_STANDALONE_LINUX || UNITY_ANDROID // defined on Linux, but usefull only with OpenGL ES
#if UNITY_STANDALONE_LINUX || UNITY_ANDROID
// defined on Linux, but usefull only with OpenGL ES
namespace Mediapipe {
public class EglSurfaceHolder : MpResourceHandle {
private UniquePtrHandle uniquePtrHandle;
Expand Down
7 changes: 5 additions & 2 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class GlContext : MpResourceHandle {
get { return sharedPtrHandle == null ? IntPtr.Zero : sharedPtrHandle.mpPtr; }
}

#if UNITY_STANDALONE_LINUX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
public IntPtr eglDisplay {
get { return SafeNativeMethods.mp_GlContext__egl_display(mpPtr); }
}
Expand All @@ -43,7 +43,10 @@ public class GlContext : MpResourceHandle {
public IntPtr eglContext {
get { return SafeNativeMethods.mp_GlContext__egl_context(mpPtr); }
}
#elif UNITY_STANDALONE_OSX
#endif

#if UNITY_STANDALONE_OSX
// NOTE: On macOS, native libs cannot be built with GPU enabled, so it cannot be used actually.
public IntPtr nsglContext {
get { return SafeNativeMethods.mp_GlContext__nsgl_context(mpPtr); }
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mediapipe {
public class GpuBuffer : MpResourceHandle {
public GpuBuffer(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {}

#if UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
public GpuBuffer(GlTextureBuffer glTextureBuffer) : base() {
UnsafeNativeMethods.mp_GpuBuffer__PSgtb(glTextureBuffer.sharedPtr, out var ptr).Assert();
glTextureBuffer.Dispose(); // respect move semantics
Expand All @@ -16,7 +16,7 @@ public class GpuBuffer : MpResourceHandle {
UnsafeNativeMethods.mp_GpuBuffer__delete(ptr);
}

#if UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
public GlTextureBuffer GetGlTextureBuffer() {
return new GlTextureBuffer(SafeNativeMethods.mp_GpuBuffer__GetGlTextureBufferSharedPtr(mpPtr), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class SafeNativeMethods {
[Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern IntPtr mp_SharedGlContext__get(IntPtr sharedGlContext);

#if UNITY_STANDALONE_LINUX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
[Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern IntPtr mp_GlContext__egl_display(IntPtr GlContext);

Expand All @@ -17,7 +17,9 @@ internal static partial class SafeNativeMethods {

[Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern IntPtr mp_GlContext__egl_context(IntPtr GlContext);
#elif UNITY_IOS
#endif

#if UNITY_IOS
[Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern IntPtr mp_GlContext__eagl_context(IntPtr GlContext);
#elif UNITY_STANDALONE_OSX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Mediapipe {
internal static partial class SafeNativeMethods {
#if UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
[Pure, DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern IntPtr mp_GpuBuffer__GetGlTextureBufferSharedPtr(IntPtr gpuBuffer);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Mediapipe {
internal static partial class UnsafeNativeMethods {
#if UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_GpuBuffer__PSgtb(IntPtr glTextureBuffer, out IntPtr gpuBuffer);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class GlCalculatorHelperTest {
glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie());

var glContext = glCalculatorHelper.GetGlContext();
#if UNITY_STANDALONE_LINUX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
Assert.AreNotEqual(glContext.eglContext, IntPtr.Zero);
#elif UNITY_STANDALONE_OSX
Assert.AreNotEqual(glContext.nsglContext, IntPtr.Zero);
Expand Down
2 changes: 1 addition & 1 deletion Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class GlContextTest {
public void ShouldReturnProperties() {
var glContext = GetGlContext();

#if UNITY_STANDALONE_LINUX || UNITY_ANDROID
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
Assert.AreNotEqual(glContext.eglDisplay, IntPtr.Zero);
Assert.AreNotEqual(glContext.eglConfig, IntPtr.Zero);
Assert.AreNotEqual(glContext.eglContext, IntPtr.Zero);
Expand Down
39 changes: 39 additions & 0 deletions Documentation/MediaPipeUnityPlugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# MediaPipeUnityPlugin

## Platform dependent compilation
```cs
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID || UNITY_IOS
// When GPU is available (SDK)
#elif (UNITY_STANDALONE_LINUX || UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR_OSX && !UNITY_EDITOR_WIN
// (Application Code)
#endif

#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
// When GpuBuffer uses GlTextureBuffer (SDK)
#elif (UNITY_STANDALONE_LINUX || UNITY_ANDROID) && !UNITY_EDITOR_OSX && !UNITY_EDITOR_WIN
// (Application Code)
#endif

#if UNITY_IOS
// When GpuBuffer uses CVPixelBuffer (SDK)
#elif UNITY_IOS && !UNITY_EDITOR
// (Application Code)
#endif

#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
// When EGL is available (SDK)
#elif (UNITY_STANDALONE_LINUX || UNITY_ANDROID) && !UNITY_EDITOR_OSX && !UNITY_EDITOR_WIN
// (Application Code)
#endif

#if UNITY_IOS
// When EAGL is available (SDK)
#if UNITY_IOS && !UNITY_EDITOR
// (Application Code)
#endif

#if UNITY_STANDALONE_OSX
// When NSGL is available (SDK)
// NOTE: On macOS, native libs cannot be built with GPU enabled, so NSGL isn't available now.
#endif
```

0 comments on commit b787ee3

Please sign in to comment.