Esempio n. 1
0
 public void Create_ShouldReturnStatusOrGpuResources()
 {
     using (var statusOrGpuResources = GpuResources.Create())
     {
         Assert.True(statusOrGpuResources.Ok());
     }
 }
Esempio n. 2
0
    async void Start()
    {
        webCamScreen = GameObject.Find("WebCamScreen");

        if (useGPU)
        {
            gpuResources = GpuResources.Create().ConsumeValue();

            gpuHelper = new GlCalculatorHelper();
            gpuHelper.InitializeForTest(gpuResources);
        }

    #if UNITY_EDITOR
        var resourceManager = LocalAssetManager.Instance;
    #else
        var resourceManager = AssetBundleManager.Instance;
    #endif

        ResourceUtil.InitializeResourceManager(resourceManager);

        try {
            await resourceManager.LoadAllAssetsAsync();

            IsAssetLoaded = true;
        } catch (Exception e) {
            Debug.LogError(e);
            IsAssetLoadFailed = true;
        }
    }
Esempio n. 3
0
 public void IsDisposed_ShouldReturnFalse_When_NotDisposedYet()
 {
     using (var gpuResources = GpuResources.Create().Value())
     {
         Assert.False(gpuResources.isDisposed);
     }
 }
Esempio n. 4
0
 public void Status_ShouldReturnOk_When_StatusIsOk()
 {
     using (var statusOrGpuResources = GpuResources.Create())
     {
         Assert.AreEqual(Status.StatusCode.Ok, statusOrGpuResources.status.Code());
     }
 }
Esempio n. 5
0
        private GlContext GetGlContext()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper()) {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                return(glCalculatorHelper.GetGlContext());
            }
        }
Esempio n. 6
0
        public void IsDisposed_ShouldReturnTrue_When_AlreadyDisposed()
        {
            var gpuResources = GpuResources.Create().Value();

            gpuResources.Dispose();

            Assert.True(gpuResources.isDisposed);
        }
        public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed()
        {
            var statusOrGpuResources = GpuResources.Create();

            statusOrGpuResources.Dispose();

            Assert.True(statusOrGpuResources.isDisposed);
        }
Esempio n. 8
0
        public void InitializeForTest_ShouldInitialize()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

            Assert.False(glCalculatorHelper.Initialized());
            glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie());
            Assert.True(glCalculatorHelper.Initialized());
        }
Esempio n. 9
0
 public void InitializeForTest_ShouldInitialize()
 {
     using (var glCalculatorHelper = new GlCalculatorHelper()) {
         Assert.False(glCalculatorHelper.Initialized());
         glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
         Assert.True(glCalculatorHelper.Initialized());
     }
 }
Esempio n. 10
0
        public void framebuffer_ShouldReturnGLName()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

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

            // default frame buffer
            Assert.AreEqual(glCalculatorHelper.framebuffer, 0);
        }
Esempio n. 11
0
        public void RunInGlContext_ShouldReturnInternal_When_FunctionReturnsInternal()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper()) {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                var status = glCalculatorHelper.RunInGlContext(() => { return(Status.Build(Status.StatusCode.Internal, "error")); });
                Assert.AreEqual(status.code, Status.StatusCode.Internal);
            }
        }
Esempio n. 12
0
        public void RunInGlContext_ShouldReturnOk_When_FunctionReturnsOk()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper()) {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                var status = glCalculatorHelper.RunInGlContext(() => { return(Status.Ok()); });
                Assert.True(status.ok);
            }
        }
        public void Framebuffer_ShouldReturnGLName()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper())
            {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                // default frame buffer
                Assert.AreEqual(0, glCalculatorHelper.framebuffer);
            }
        }
        public void RunInGlContext_ShouldReturnInternal_When_FunctionThrows()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper())
            {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                var status = glCalculatorHelper.RunInGlContext((GlCalculatorHelper.GlFunction)(() => { throw new Exception("Function Throws"); }));
                Assert.AreEqual(Status.StatusCode.Internal, status.Code());
            }
        }
        public void RunInGlContext_ShouldReturnFailedPreCondition_When_FunctionThrows()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

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

            var status = glCalculatorHelper.RunInGlContext(() => { throw new InvalidProgramException(); });

            Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition);
        }
Esempio n. 16
0
        public static IEnumerator Initialize()
        {
            lock (setupLock) {
                if (isInitialized)
                {
                    Logger.LogWarning(TAG, "Already set up");
                    yield break;
                }

#if UNITY_ANDROID && !UNITY_EDITOR
                isContextInitialized = false;
                PluginCallback callback = GetCurrentContext;

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

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

                if (!isContextInitialized)
                {
                    throw new TimeoutException("Failed to get GlContext");
                }

#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...");
                    gpuResources = GpuResources.Create(currentContext).Value();

                    Logger.LogInfo(TAG, "Initializing GlCalculatorHelper...");
                    glCalculatorHelper = new GlCalculatorHelper();
                    glCalculatorHelper.InitializeForTest(gpuResources);

                    isInitialized = true;
                } catch (Exception e) {
                    Logger.LogException(e);
                    Logger.LogError(TAG, "Failed to create GpuResources. If your native library is built for CPU, change 'Preferable Inference Mode' to CPU from the Inspector Window for Bootstrap");
                }
            }
        }
        public void Value_ShouldReturnGpuResources_When_StatusIsOk()
        {
            var statusOrGpuResources = GpuResources.Create();

            Assert.True(statusOrGpuResources.ok);

            var gpuResources = statusOrGpuResources.Value();

            Assert.IsInstanceOf <GpuResources>(gpuResources);
            Assert.True(statusOrGpuResources.isDisposed);
        }
Esempio n. 18
0
        public void GetCurrent_ShouldReturnCurrentContext_When_CalledInGlContext()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

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

            glCalculatorHelper.RunInGlContext(() => {
                var glContext = GlContext.GetCurrent();

                Assert.NotNull(glContext);
                Assert.True(glContext.IsCurrent());
                return(Status.Ok());
            }).AssertOk();
        }
Esempio n. 19
0
        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(() => {
                var texture = glCalculatorHelper.CreateSourceTexture(imageFrame);
                texture.Dispose();
                return(Status.Ok());
            });

            Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition);
        }
Esempio n. 20
0
        private GlContext GetGlContext()
        {
            GlContext glContext = null;

            var glCalculatorHelper = new GlCalculatorHelper();

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

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

            return(glContext);
        }
Esempio n. 21
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
            }
        }
        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. 23
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);
        }
        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);
        }
        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. 26
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. 27
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. 29
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();
                }
            }
        }