Esempio n. 1
0
        public void GetGlContext_ShouldReturnCurrentContext()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper()) {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                var glContext = glCalculatorHelper.GetGlContext();
#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);
#elif UNITY_IOS
                Assert.AreNotEqual(glContext.eaglContext, IntPtr.Zero);
#endif
            }
        }
        private GlContext GetGlContext()
        {
            GlContext glContext = null;

            var glCalculatorHelper = new GlCalculatorHelper();

            glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

            glCalculatorHelper.RunInGlContext(() => {
                glContext = GlContext.GetCurrent();
                return(Status.Ok());
            }).AssertOk();

            return(glContext);
        }
        public void GetCurrent_ShouldReturnCurrentContext_When_CalledInGlContext()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper())
            {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                glCalculatorHelper.RunInGlContext(() =>
                {
                    using (var glContext = GlContext.GetCurrent())
                    {
                        Assert.NotNull(glContext);
                        Assert.True(glContext.IsCurrent());
                    }
                }).AssertOk();
            }
        }
Esempio n. 4
0
        /// <summary>
        ///   Dispose GPU resources.
        /// </summary>
        /// <remarks>
        ///   This has to be called before the application exits.
        ///   Otherwise, UnityEditor can freeze.
        /// </remarks>
        public static void Shutdown()
        {
            if (GpuResources != null)
            {
                GpuResources.Dispose();
                GpuResources = null;
            }

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

            IsInitialized = false;
        }
        void ExpectSetSurfaceOk(IntPtr surface)
        {
            var eglSurfaceHolder   = new EglSurfaceHolder();
            var glCalculatorHelper = new GlCalculatorHelper();

            glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie());

            var status = glCalculatorHelper.RunInGlContext(() => {
                var glContext = GlContext.GetCurrent();
                eglSurfaceHolder.SetSurface(surface, glContext);

                return(Status.Ok());
            });

            Assert.True(status.ok);
        }
Esempio n. 6
0
        public void CreateDestinationTexture_ShouldReturnGlTexture_When_GpuBufferFormatIsValid()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

            glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie());

            var status = glCalculatorHelper.RunInGlContext(() => {
                var glTexture = glCalculatorHelper.CreateDestinationTexture(32, 24, GpuBufferFormat.kBGRA32);

                Assert.AreEqual(glTexture.width, 32);
                Assert.AreEqual(glTexture.height, 24);
                return(Status.Ok());
            });

            Assert.True(status.ok);
        }
        public void CreateSourceTexture_ShouldFail_When_ImageFrameFormatIsInvalid()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

            glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie());

            var imageFrame = new ImageFrame(ImageFormat.Format.SBGRA, 32, 24);
            var status     = glCalculatorHelper.RunInGlContext(() => {
                using (var texture = glCalculatorHelper.CreateSourceTexture(imageFrame)) {
                    texture.Release();
                }
                return(Status.Ok());
            });

            Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition);
        }
        public void CreateDestinationTexture_ShouldReturnGlTexture_When_GpuBufferFormatIsValid()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper())
            {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                var status = glCalculatorHelper.RunInGlContext(() =>
                {
                    var glTexture = glCalculatorHelper.CreateDestinationTexture(32, 24, GpuBufferFormat.kBGRA32);

                    Assert.AreEqual(32, glTexture.width);
                    Assert.AreEqual(24, glTexture.height);
                });

                Assert.True(status.Ok());
            }
        }
Esempio n. 9
0
    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);
    }
Esempio n. 10
0
        public void CreateSourceTexture_ShouldReturnGlTexture_When_CalledWithImageFrame()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

            glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie());

            var imageFrame = new ImageFrame(ImageFormat.Format.SRGBA, 32, 24);
            var status     = glCalculatorHelper.RunInGlContext(() => {
                var texture = glCalculatorHelper.CreateSourceTexture(imageFrame);

                Assert.AreEqual(texture.width, 32);
                Assert.AreEqual(texture.height, 24);

                texture.Dispose();
                return(Status.Ok());
            });

            Assert.True(status.ok);
        }
        public void CreateSourceTexture_ShouldFail_When_ImageFrameFormatIsInvalid()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper())
            {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                using (var imageFrame = new ImageFrame(ImageFormat.Types.Format.Sbgra, 32, 24))
                {
                    var status = glCalculatorHelper.RunInGlContext(() =>
                    {
                        using (var texture = glCalculatorHelper.CreateSourceTexture(imageFrame))
                        {
                            texture.Release();
                        }
                    });
                    Assert.AreEqual(Status.StatusCode.FailedPrecondition, status.Code());

                    status.Dispose();
                }
            }
        }
Esempio n. 12
0
    void SetupGpuResources()
    {
        if (gpuResources != null)
        {
            Debug.Log("Gpu resources are already initialized");
            return;
        }

        // TODO: have to wait for currentContext to be initialized.
        if (currentContext == IntPtr.Zero)
        {
            Debug.LogWarning("No EGL Context Found");
        }
        else
        {
            Debug.Log($"EGL Context Found ({currentContext})");
        }

        gpuResources = GpuResources.Create(currentContext).ConsumeValueOrDie();
        gpuHelper    = new GlCalculatorHelper();
        gpuHelper.InitializeForTest(gpuResources);
    }
        public void CreateSourceTexture_ShouldReturnGlTexture_When_CalledWithImageFrame()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper())
            {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                using (var imageFrame = new ImageFrame(ImageFormat.Types.Format.Srgba, 32, 24))
                {
                    var status = glCalculatorHelper.RunInGlContext(() =>
                    {
                        var texture = glCalculatorHelper.CreateSourceTexture(imageFrame);

                        Assert.AreEqual(32, texture.width);
                        Assert.AreEqual(24, texture.height);

                        texture.Dispose();
                    });
                    Assert.True(status.Ok());

                    status.Dispose();
                }
            }
        }
Esempio n. 14
0
 public void Create_ShouldReturnStatusOrGpuResources()
 {
     using (var statusOrGpuResources = GpuResources.Create()) {
         Assert.True(statusOrGpuResources.ok);
     }
 }
Esempio n. 15
0
        /// <summary>
        ///   Initialize GPU resources.
        ///   If it finishes successfully, <see cref="IsInitialized" /> will be set to <c>true</c>.
        /// </summary>
        /// <remarks>
        ///   If <see cref="IsInitialized" /> is <c>true</c>, it will do nothing.
        ///   Before the application exits, don't forget to call <see cref="Shutdown" />.
        /// </remarks>
        public static IEnumerator Initialize()
        {
            lock (_SetupLock)
            {
                if (IsInitialized)
                {
                    Logger.LogInfo(_TAG, "Already initialized");
                    yield break;
                }

#if UNITY_ANDROID
                _IsContextInitialized = SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES3;
                if (!_IsContextInitialized)
                {
                    PluginCallback callback = GetCurrentContext;

                    var fp = Marshal.GetFunctionPointerForDelegate(callback);
                    GL.IssuePluginEvent(fp, 1);
                }
#else
                _IsContextInitialized = true;
#endif

                var count = 100;
                yield return(new WaitUntil(() =>
                {
                    return --count < 0 || _IsContextInitialized;
                }));

                if (!_IsContextInitialized)
                {
                    Logger.LogError(_TAG, "Failed to get GlContext");
                    yield break;
                }

#if UNITY_ANDROID
                if (_CurrentContext == IntPtr.Zero)
                {
                    Logger.LogWarning(_TAG, "EGL context is not found, so MediaPipe won't share their EGL contexts with Unity");
                }
                else
                {
                    Logger.LogVerbose(_TAG, $"EGL context is found: {_CurrentContext}");
                }
#endif

                try
                {
                    Logger.LogInfo(_TAG, "Initializing GpuResources...");
                    var statusOrGpuResources = GpuResources.Create(_CurrentContext);

                    statusOrGpuResources.status.AssertOk();
                    GpuResources = statusOrGpuResources.Value();

                    Logger.LogInfo(_TAG, "Initializing GlCalculatorHelper...");
                    GlCalculatorHelper = new GlCalculatorHelper();
                    GlCalculatorHelper.InitializeForTest(GpuResources);

                    IsInitialized = true;
                }
                catch (EntryPointNotFoundException e)
                {
                    Logger.LogException(e);
                    Logger.LogError(_TAG, "Failed to create GpuResources. Did you build libraries with GPU enabled?");
                }
                catch (Exception e)
                {
                    Logger.LogException(e);
                }
            }
        }
    IEnumerator RunGraph()
    {
        yield return(WaitForAssets());

        if (IsAssetLoadFailed)
        {
            Debug.LogError("Failed to load assets. Stopping...");
            yield break;
        }

        yield return(WaitForGraph());

        if (graphPrefab == null)
        {
            Debug.LogWarning("No graph is set. Stopping...");
            yield break;
        }

        var webCamScreenController = webCamScreen.GetComponent <WebCamScreenController>();

        yield return(WaitForCamera(webCamScreenController));

        if (!webCamScreenController.isPlaying)
        {
            Debug.LogWarning("WebCamDevice is not working. Stopping...");
            yield break;
        }

        graphContainer = Instantiate(graphPrefab);
        var graph = graphContainer.GetComponent <IDemoGraph <TextureFrame> >();

        if (useGPU)
        {
            // TODO: have to wait for currentContext to be initialized.
            if (currentContext == IntPtr.Zero)
            {
                Debug.LogWarning("No EGL Context Found");
            }
            else
            {
                Debug.Log($"EGL Context Found ({currentContext})");
            }

            gpuResources = GpuResources.Create(currentContext).ConsumeValueOrDie();
            gpuHelper    = new GlCalculatorHelper();
            gpuHelper.InitializeForTest(gpuResources);

            graph.Initialize(gpuResources, gpuHelper);
        }
        else
        {
            graph.Initialize();
        }

        graph.StartRun(webCamScreenController.GetScreen()).AssertOk();

        while (true)
        {
            yield return(new WaitForEndOfFrame());

            var nextFrameRequest = webCamScreenController.RequestNextFrame();
            yield return(nextFrameRequest);

            var nextFrame = nextFrameRequest.textureFrame;

            graph.PushInput(nextFrame).AssertOk();
            graph.RenderOutput(webCamScreenController, nextFrame);
        }
    }
        public void status_ShouldReturnOk_When_StatusIsOk()
        {
            var statusOrGpuResources = GpuResources.Create();

            Assert.AreEqual(statusOrGpuResources.status.code, Status.StatusCode.Ok);
        }
        public void isDisposed_ShouldReturnFalse_When_NotDisposedYet()
        {
            var statusOrGpuResources = GpuResources.Create();

            Assert.False(statusOrGpuResources.isDisposed);
        }
Esempio n. 19
0
 public void Initialize(GpuResources gpuResources, GlCalculatorHelper gpuHelper)
 {
     this.Initialize();
 }
Esempio n. 20
0
 public void isDisposed_ShouldReturnFalse_When_NotDisposedYet()
 {
     using (var gpuResources = GpuResources.Create().Value()) {
         Assert.False(gpuResources.isDisposed);
     }
 }