diff --git a/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs b/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs index 33421fbea..96500d4df 100644 --- a/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs +++ b/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs @@ -60,7 +60,7 @@ public virtual Status StartRun(Texture texture) { 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(); diff --git a/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs b/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs index f9e38a2a9..6b7d6eedc 100644 --- a/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs +++ b/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs @@ -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(); @@ -42,7 +42,7 @@ static void GetCurrentContext(int eventId) { async void Start() { webCamScreen = GameObject.Find("WebCamScreen"); -#if UNITY_ANDROID +#if UNITY_ANDROID && !UNITY_EDITOR_OSX && !UNITY_EDITOR_WIN if (useGPU) { PluginCallback callback = GetCurrentContext; diff --git a/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs b/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs index 53731a61f..f206b0575 100644 --- a/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs +++ b/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs @@ -116,7 +116,7 @@ public void DrawScreen(ImageFrame imageFrame) { 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 diff --git a/Assets/MediaPipe/SDK/Scripts/Framework/Packet/EglSurfaceHolderPacket.cs b/Assets/MediaPipe/SDK/Scripts/Framework/Packet/EglSurfaceHolderPacket.cs index 9da57bb57..f5f0b7683 100644 --- a/Assets/MediaPipe/SDK/Scripts/Framework/Packet/EglSurfaceHolderPacket.cs +++ b/Assets/MediaPipe/SDK/Scripts/Framework/Packet/EglSurfaceHolderPacket.cs @@ -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 { public EglSurfaceHolderPacket() : base() {} @@ -32,4 +33,4 @@ public override Status ValidateAsType() { } } } -#endif \ No newline at end of file +#endif diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/EglSurfaceHolder.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/EglSurfaceHolder.cs index ed913ea0c..d93e0b66b 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/EglSurfaceHolder.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/EglSurfaceHolder.cs @@ -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; diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs index 7017f1ff7..ac30e70aa 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GlContext.cs @@ -31,7 +31,7 @@ public IntPtr sharedPtr { 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); } } @@ -43,7 +43,10 @@ public IntPtr eglConfig { 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); } } diff --git a/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs b/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs index 1361816ac..d9c319140 100644 --- a/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs +++ b/Assets/MediaPipe/SDK/Scripts/Gpu/GpuBuffer.cs @@ -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 @@ -16,7 +16,7 @@ protected override void DeleteMpPtr() { 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); } diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs index d58cfe69c..3da4ce15a 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GlContext_Safe.cs @@ -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); @@ -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 diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs index 65d91ef74..7beaccb79 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Safe.cs @@ -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 diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Unsafe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Unsafe.cs index 298392082..6a8f5620f 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Unsafe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Gpu/GpuBuffer_Unsafe.cs @@ -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 diff --git a/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs b/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs index 426223ed4..e3042e0f7 100644 --- a/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs +++ b/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs @@ -143,7 +143,7 @@ public void GetGlContext_ShouldReturnCurrentContext() { 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); diff --git a/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlContextTest.cs b/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlContextTest.cs index f4bdab91e..f5e1bd0b3 100644 --- a/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlContextTest.cs +++ b/Assets/MediaPipe/SDK/Tests/EditMode/Gpu/GlContextTest.cs @@ -40,7 +40,7 @@ public void IsCurrent_ShouldReturnFalse_When_CalledOutOfGlContext() { 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); diff --git a/Documentation/MediaPipeUnityPlugin.md b/Documentation/MediaPipeUnityPlugin.md new file mode 100644 index 000000000..cb50ed920 --- /dev/null +++ b/Documentation/MediaPipeUnityPlugin.md @@ -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 +```