Ejemplo n.º 1
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;
        }
    }
 public void Ctor_ShouldInstantiateGlCalculatorHelper()
 {
     using (var glCalculatorHelper = new GlCalculatorHelper())
     {
         Assert.AreNotEqual(IntPtr.Zero, glCalculatorHelper.mpPtr);
     }
 }
 public void IsDisposed_ShouldReturnFalse_When_NotDisposedYet()
 {
     using (var glCalculatorHelper = new GlCalculatorHelper())
     {
         Assert.False(glCalculatorHelper.isDisposed);
     }
 }
Ejemplo n.º 4
0
    public void Initialize(GpuResources gpuResources, GlCalculatorHelper gpuHelper)
    {
        this.Initialize();

        graph.SetGpuResources(gpuResources).AssertOk();
        this.gpuHelper = gpuHelper;
    }
Ejemplo n.º 5
0
        public void InitializeForTest_ShouldInitialize()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

            Assert.False(glCalculatorHelper.Initialized());
            glCalculatorHelper.InitializeForTest(GpuResources.Create().ConsumeValueOrDie());
            Assert.True(glCalculatorHelper.Initialized());
        }
Ejemplo n.º 6
0
        private GlContext GetGlContext()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper()) {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                return(glCalculatorHelper.GetGlContext());
            }
        }
        public void IsDisposed_ShouldReturnTrue_When_AlreadyDisposed()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

            glCalculatorHelper.Dispose();

            Assert.True(glCalculatorHelper.isDisposed);
        }
Ejemplo n.º 8
0
 public void InitializeForTest_ShouldInitialize()
 {
     using (var glCalculatorHelper = new GlCalculatorHelper()) {
         Assert.False(glCalculatorHelper.Initialized());
         glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
         Assert.True(glCalculatorHelper.Initialized());
     }
 }
Ejemplo n.º 9
0
        public void framebuffer_ShouldReturnGLName()
        {
            var glCalculatorHelper = new GlCalculatorHelper();

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

            // default frame buffer
            Assert.AreEqual(glCalculatorHelper.framebuffer, 0);
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
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 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);
        }
        public void Framebuffer_ShouldReturnGLName()
        {
            using (var glCalculatorHelper = new GlCalculatorHelper())
            {
                glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

                // default frame buffer
                Assert.AreEqual(0, glCalculatorHelper.framebuffer);
            }
        }
Ejemplo n.º 15
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");
                }
            }
        }
    protected virtual void OnDestroy()
    {
        Stop();
        graph     = null;
        gpuHelper = null;

        if (stopwatch != null && stopwatch.IsRunning)
        {
            stopwatch.Stop();
        }
    }
Ejemplo n.º 17
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();
        }
Ejemplo n.º 18
0
 protected virtual void OnDestroy()
 {
     Stop();
     if (graph != null)
     {
         graph.Dispose();
         graph = null;
     }
     if (gpuHelper != null)
     {
         gpuHelper.Dispose();
         gpuHelper = null;
     }
 }
Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo 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();
            }
        }
Ejemplo n.º 23
0
        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);
        }
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
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;
        }
        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());
            }
        }
Ejemplo n.º 27
0
    /// <summary>
    ///   This method must be called (only) once before calling StartRun.
    ///   `graph` and `gpuHelper` (if useGPU is true) are initialized here.
    ///    If the config is invalid, it throws an error.
    /// </summary>
    public void Initialize()
    {
        if (config == null)
        {
            throw new InvalidOperationException("config is missing");
        }

        graph = new CalculatorGraph(config.text);

        if (shouldUseGPU())
        {
            var gpuResources = new StatusOrGpuResources().ConsumeValue();
            graph.SetGpuResources(gpuResources).AssertOk();

            gpuHelper = new GlCalculatorHelper();
            gpuHelper.InitializeForTest(graph.GetGpuResources());
        }
    }
Ejemplo n.º 28
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);
    }
Ejemplo n.º 29
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();
                }
            }
        }