private bool IsStale(TextureFrame textureFrame)
 {
     lock (_formatLock)
     {
         return(textureFrame.width != _textureWidth || textureFrame.height != _textureHeight);
     }
 }
        private TextureFrame CreateNewTextureFrame()
        {
            var textureFrame = new TextureFrame(_textureWidth, _textureHeight, _format);

            textureFrame.OnRelease.AddListener(OnTextureFrameRelease);

            return(textureFrame);
        }
예제 #3
0
        static void RegisterInstance(TextureFrame textureFrame)
        {
            var name = textureFrame.GetTextureName();
            var id   = textureFrame.instanceId;

            lock (((ICollection)nameTable).SyncRoot) {
                if (AcquireName(name, id))
                {
                    instanceTable.Add(id, textureFrame);
                    nameTable.Add(name, id);
                    return;
                }
            }
            throw new ArgumentException("Another instance has the same name");
        }
예제 #4
0
        void OnTextureFrameRelease(TextureFrame textureFrame)
        {
            lock (((ICollection)textureFramesInUse).SyncRoot) {
                if (!textureFramesInUse.Remove(textureFrame.GetInstanceID()))
                {
                    // won't be run
                    Logger.LogWarning(TAG, "The released texture does not belong to the pool");
                    return;
                }

                if (frameCount > poolSize || IsStale(textureFrame))
                {
                    return;
                }
                availableTextureFrames.Enqueue(textureFrame);
            }
        }
예제 #5
0
        protected static void ReadFromImageSource(TextureFrame textureFrame, RunningMode runningMode, GraphRunner.ConfigType configType)
        {
            var sourceTexture = ImageSourceProvider.imageSource.GetCurrentTexture();

            // For some reason, when the image is coiped on GPU, latency tends to be high.
            // So even when OpenGL ES is available, use CPU to copy images.
            var textureType = sourceTexture.GetType();

            if (textureType == typeof(WebCamTexture))
            {
                textureFrame.ReadTextureFromOnCPU((WebCamTexture)sourceTexture);
            }
            else if (textureType == typeof(Texture2D))
            {
                textureFrame.ReadTextureFromOnCPU((Texture2D)sourceTexture);
            }
            else
            {
                textureFrame.ReadTextureFromOnCPU(sourceTexture);
            }
        }
        public bool TryGetTextureFrame(out TextureFrame outFrame)
        {
            TextureFrame nextFrame = null;

            lock (((ICollection)_availableTextureFrames).SyncRoot)
            {
                if (_poolSize <= frameCount)
                {
                    while (_availableTextureFrames.Count > 0)
                    {
                        var textureFrame = _availableTextureFrames.Dequeue();

                        if (!IsStale(textureFrame))
                        {
                            nextFrame = textureFrame;
                            break;
                        }
                    }
                }
                else
                {
                    nextFrame = CreateNewTextureFrame();
                }
            }

            if (nextFrame == null)
            {
                outFrame = null;
                return(false);
            }

            lock (((ICollection)_textureFramesInUse).SyncRoot)
            {
                _textureFramesInUse.Add(nextFrame.GetInstanceID(), nextFrame);
            }

            nextFrame.WaitUntilReleased();
            outFrame = nextFrame;
            return(true);
        }
예제 #7
0
        IEnumerator YieldTextureFrame(Action <TextureFrame> callback)
        {
            TextureFrame nextFrame = null;

            lock (((ICollection)availableTextureFrames).SyncRoot) {
                yield return(new WaitUntil(() => {
                    return poolSize > frameCount || availableTextureFrames.Count > 0;
                }));

                if (poolSize <= frameCount)
                {
                    while (availableTextureFrames.Count > 0)
                    {
                        var textureFrame = availableTextureFrames.Dequeue();

                        if (!IsStale(textureFrame))
                        {
                            nextFrame = textureFrame;
                            break;
                        }
                    }
                }

                if (nextFrame == null)
                {
                    nextFrame = CreateNewTextureFrame();
                }
            }

            lock (((ICollection)textureFramesInUse).SyncRoot) {
                textureFramesInUse.Add(nextFrame.GetInstanceID(), nextFrame);
            }

            nextFrame.WaitUntilReleased();
            callback(nextFrame);

            yield return(nextFrame);
        }