diff --git a/Assets/MediaPipe/Examples/Scripts/ResourceManager/TextureFrame.cs b/Assets/MediaPipe/Examples/Scripts/ResourceManager/TextureFrame.cs index c13ec141e..2ad005790 100644 --- a/Assets/MediaPipe/Examples/Scripts/ResourceManager/TextureFrame.cs +++ b/Assets/MediaPipe/Examples/Scripts/ResourceManager/TextureFrame.cs @@ -26,7 +26,7 @@ public TextureFormat format { } public TextureFrame(int width, int height) { - this.texture = new Texture2D(width, height, TextureFormat.BGRA32, false); + this.texture = new Texture2D(width, height, TextureFormat.RGBA32, false); releaseCallbackHandle = GCHandle.Alloc((GlTextureBuffer.DeletionCallback)this.OnRelease, GCHandleType.Pinned); } diff --git a/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs b/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs index b6c9ab2f0..f9ca53602 100644 --- a/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs +++ b/Assets/MediaPipe/Examples/Scripts/SceneDirector.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Collections; +using System.Runtime.InteropServices; using Mediapipe; using UnityEngine; @@ -22,6 +23,9 @@ public class SceneDirector : MonoBehaviour { bool IsAssetLoaded = false; bool IsAssetLoadFailed = false; + delegate void PluginCallback(int eventId); + GCHandle InitializeGpuHelperHandle; + void OnEnable() { var nameForGlog = Path.Combine(Application.dataPath, "MediaPipePlugin"); var logDir = Path.Combine(Application.persistentDataPath, "Logs", "MediaPipe"); @@ -33,13 +37,30 @@ void OnEnable() { Glog.Initialize(nameForGlog, logDir); } + void InitializeGpuHelper(int eventId) { + // context is EGL_NO_CONTEXT if the graphics API is not OpenGL ES + var context = Egl.getCurrentContext(); + + if (context == IntPtr.Zero) { + Debug.LogWarning("No EGL Context Found"); + } else { + Debug.Log($"EGL Context Found ({context})"); + } + + gpuResources = GpuResources.Create(context).ConsumeValueOrDie(); + gpuHelper = new GlCalculatorHelper(); + gpuHelper.InitializeForTest(gpuResources); + } + async void Start() { webCamScreen = GameObject.Find("WebCamScreen"); if (useGPU) { - gpuResources = GpuResources.Create(Egl.getCurrentContext()).ConsumeValueOrDie(); - gpuHelper = new GlCalculatorHelper(); - gpuHelper.InitializeForTest(gpuResources); + PluginCallback gpuHelperInitializer = InitializeGpuHelper; + InitializeGpuHelperHandle = GCHandle.Alloc(gpuHelperInitializer, GCHandleType.Pinned); + + var fp = Marshal.GetFunctionPointerForDelegate(gpuHelperInitializer); + GL.IssuePluginEvent(fp, 1); } #if UNITY_EDITOR @@ -60,6 +81,12 @@ async void Start() { } void OnDisable() { + StopGraph(); + + if (InitializeGpuHelperHandle.IsAllocated) { + InitializeGpuHelperHandle.Free(); + } + Glog.Shutdown(); } @@ -140,6 +167,7 @@ IEnumerator RunGraph() { var graph = graphContainer.GetComponent>(); if (useGPU) { + // TODO: have to wait for gpuHelper to be initialized. graph.Initialize(gpuResources, gpuHelper); } else { graph.Initialize(); diff --git a/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs b/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs index 08a6c7747..86cce2166 100644 --- a/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs +++ b/Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs @@ -41,6 +41,7 @@ public void ResetScreen(WebCamDevice? device) { try { webCamTexture.Play(); + Debug.Log($"WebCamTexture Graphics Format: {webCamTexture.graphicsFormat}"); } catch (Exception e) { Debug.LogWarning(e.ToString()); return; @@ -74,7 +75,7 @@ public IntPtr GetNativeTexturePtr() { public void InitScreen() { Renderer renderer = GetComponent(); - outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.BGRA32, false); + outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false); renderer.material.mainTexture = outputTexture; pixelData = new Color32[webCamTexture.width * webCamTexture.height]; diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 13f90e2b5..3246d51d4 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -393,7 +393,7 @@ PlayerSettings: m_GraphicsJobMode: 0 m_BuildTargetGraphicsAPIs: - m_BuildTarget: AndroidPlayer - m_APIs: 150000000b000000 + m_APIs: 0b000000 m_Automatic: 0 - m_BuildTarget: LinuxStandaloneSupport m_APIs: 1500000011000000 @@ -401,7 +401,7 @@ PlayerSettings: m_BuildTargetVRSettings: [] openGLRequireES31: 0 openGLRequireES31AEP: 0 - openGLRequireES32: 0 + openGLRequireES32: 1 m_TemplateCustomTags: {} mobileMTRendering: Android: 1