Example #1
0
    // Update is called once per frame
    private void Update()
    {
        if (!_areStreamsInitialized)
        {
            InitializeStreams();
        }

        totalFrameTime.Stop();
        totalFrameTime.Start();

        if (_areStreamsInitialized)
        {
            CheckForNewFrames();
        }

        astraUpdateTime.Start();
        astraUpdateTime.Stop();

        if (TimeText != null)
        {
            BackgroundUpdaterTimings backgroundTimings = AstraUnityContext.Instance.BackgroundTimings;
            float totalFrameMs  = totalFrameTime.AverageMilliseconds;
            float astraUpdateMs = backgroundTimings.updateAvgMillis;
            float lockWaitMs    = backgroundTimings.lockWaitAvgMillis;
            float updateUntilMs = backgroundTimings.updateUntilAvgMillis;
            float updateFrameMs = updateFramesTime.AverageMilliseconds;
            TimeText.text = "Tot: " + totalFrameMs.ToString("0.0") + " ms\n" +
                            "AU: " + astraUpdateMs.ToString("0.0") + " ms\n" +
                            "LockWait: " + lockWaitMs.ToString("0.0") + " ms\n" +
                            "UpdateUntil: " + updateUntilMs.ToString("0.0") + " ms\n" +
                            "UpdateFr: " + updateFrameMs.ToString("0.0") + " ms\n";
        }
    }
Example #2
0
    private void CheckForNewFrames()
    {
        if (AstraUnityContext.Instance.WaitForUpdate(5) && AstraUnityContext.Instance.IsUpdateAsyncComplete)
        {
            // Inside this block until UpdateAsync() call below, we can use the Astra API safely
            updateFramesTime.Start();

            CheckDepthReader();
            CheckColorReader();
            CheckBodyReader();
            CheckMaskedColorReader();
            CheckColorizedBodyReader();
            CheckPointReader();
            _frameCount++;

            updateFramesTime.Stop();
        }

        if (!AstraUnityContext.Instance.IsUpdateRequested)
        {
            UpdateStreamStartStop();
            // After calling UpdateAsync() the Astra API will be called from a background thread
            AstraUnityContext.Instance.UpdateAsync(UpdateUntilDelegate);
        }
    }
Example #3
0
    // Update is called once per frame
    private void Update()
    {
        if (!_areStreamsInitialized)
        {
#if ASTRA_UNITY_ANDROID_NATIVE
            return;
#else
            InitializeStreams();
#endif
        }

        totalFrameTime.Stop();
        totalFrameTime.Start();

        if (_areStreamsInitialized)
        {
            CheckForNewFrames();
        }

        astraUpdateTime.Start();
        astraUpdateTime.Stop();

        if (ToggleDebugText != null)
        {
            bool newDebugTextEnabled = ToggleDebugText.isOn;

            if (debugTextEnabled && !newDebugTextEnabled)
            {
                // Clear TimeText once if ToggleDebugText was just turned off
                TimeText.text = "";
            }

            debugTextEnabled = newDebugTextEnabled;
        }

        if (TimeText != null && debugTextEnabled)
        {
            BackgroundUpdaterTimings backgroundTimings = AstraUnityContext.Instance.BackgroundTimings;
            float totalFrameMs  = totalFrameTime.AverageMilliseconds;
            float astraUpdateMs = backgroundTimings.updateAvgMillis;
            float lockWaitMs    = backgroundTimings.lockWaitAvgMillis;
            float updateUntilMs = backgroundTimings.updateUntilAvgMillis;
            float updateFrameMs = updateFramesTime.AverageMilliseconds;
            TimeText.text = "Tot: " + totalFrameMs.ToString("0.0") + " ms\n" +
                            "AU: " + astraUpdateMs.ToString("0.0") + " ms\n" +
                            "LockWait: " + lockWaitMs.ToString("0.0") + " ms\n" +
                            "UpdateUntil: " + updateUntilMs.ToString("0.0") + " ms\n" +
                            "UpdateFr: " + updateFrameMs.ToString("0.0") + " ms\n";
        }
    }
    public void UpdateAsync(Func <bool> updateUntilCondition)
    {
        _lockWaitTime.Start();
        // _updateUntilMutex guarantees only one UpdateAsync can be in progress at a time
        _updateUntilMutex.WaitOne();
        _lockWaitTime.Stop();

        _updateUntilCondition          = updateUntilCondition;
        _updateUntilConditionSatisfied = false;
        _updateRequested = true;

        UpdateTimings();

        _updateUntilMutex.ReleaseMutex();
        _updateRequestedEvent.Set();
    }
    private void ThreadFunc()
    {
        while (_isStarted)
        {
            // Wait for notification that an update was requested
            if (_updateRequestedEvent.WaitOne(100) && _isStarted)
            {
                _updateUntilMutex.WaitOne();

                _updateUntilTime.Start();

                // Inner update loop will repeat until _updateUntilCondition is true
                // (or if _updateUntilCondition is null, it will update just once)
                while (_isStarted)
                {
                    _updateTime.Start();
                    Context.Update();
                    _updateTime.Stop();

                    // If the caller did not specify an _updateUntilCondition,
                    // or that condition is now true
                    if (_updateUntilCondition == null || _updateUntilCondition())
                    {
                        // break the loop
                        break;
                    }
                }

                _updateUntilTime.Stop();

                UpdateTimings();

                // Allow the main thread to request another update
                _updateUntilConditionSatisfied = true;
                _updateRequested = false;

                _updateUntilMutex.ReleaseMutex();

                // Notify the main thread that the _updateUntilCondition has been satisfied.
                // This will unblock the main thread if it is waiting.
                _updateUntilConditionEvent.Set();
            }
        }
    }
Example #6
0
    // Update is called once per frame
    private void Update()
    {
        if (!_areStreamsInitialized)
        {
            InitializeStreams();
        }

        UpdateStreamStartStop();

        totalFrameTime.Stop();
        totalFrameTime.Start();

        astraUpdateTime.Start();
        AstraUnityContext.Instance.Update();

        if (_frameReadyDirty)
        {
            astraUpdateTime.Stop();
            _frameReadyDirty = false;
        }
        else
        {
            astraUpdateTime.Pause();
        }

        if (TimeText != null)
        {
            float frameReadyMs          = frameReadyTime.AverageMilliseconds;
            float astraUpdateMs         = astraUpdateTime.AverageMilliseconds;
            float totalFrameMs          = totalFrameTime.AverageMilliseconds;
            float astraUpdateInternalMs = astraUpdateMs - frameReadyMs;
            TimeText.text = "Tot: " + totalFrameMs.ToString("0.0") + " ms\n" +
                            "AU: " + astraUpdateMs.ToString("0.0") + " ms\n" +
                            "FR: " + frameReadyMs.ToString("0.0") + " ms\n" +
                            "AUI: " + astraUpdateInternalMs.ToString("0.0") + " ms\n";
        }
    }
Example #7
0
    private void FrameReady(object sender, FrameReadyEventArgs e)
    {
        frameReadyTime.Start();

        //Debug.Log("FrameReady " + _frameCount);
        DepthFrame depthFrame = e.Frame.GetFrame <DepthFrame>();

        if (depthFrame != null)
        {
            if (_lastDepthFrameIndex != depthFrame.FrameIndex)
            {
                _lastDepthFrameIndex = depthFrame.FrameIndex;

                NewDepthFrameEvent.Invoke(depthFrame);
            }
        }

        ColorFrame colorFrame = e.Frame.GetFrame <ColorFrame>();

        if (colorFrame != null)
        {
            if (_lastColorFrameIndex != colorFrame.FrameIndex)
            {
                _lastColorFrameIndex = colorFrame.FrameIndex;

                NewColorFrameEvent.Invoke(colorFrame);
            }
        }

        BodyFrame bodyFrame = e.Frame.GetFrame <BodyFrame>();

        if (bodyFrame != null)
        {
            if (_lastBodyFrameIndex != bodyFrame.FrameIndex)
            {
                _lastBodyFrameIndex = bodyFrame.FrameIndex;

                NewBodyFrameEvent.Invoke(_bodyStream, bodyFrame);
                NewBodyMaskEvent.Invoke(bodyFrame.BodyMask);
            }
        }

        MaskedColorFrame maskedColorFrame = e.Frame.GetFrame <MaskedColorFrame>();

        if (maskedColorFrame != null)
        {
            if (_lastMaskedColorFrameIndex != maskedColorFrame.FrameIndex)
            {
                _lastMaskedColorFrameIndex = maskedColorFrame.FrameIndex;

                NewMaskedColorFrameEvent.Invoke(maskedColorFrame);
            }
        }

        ColorizedBodyFrame colorizedBodyFrame = e.Frame.GetFrame <ColorizedBodyFrame>();

        if (colorizedBodyFrame != null)
        {
            if (_lastColorizedBodyFrameIndex != colorizedBodyFrame.FrameIndex)
            {
                _lastColorizedBodyFrameIndex = colorizedBodyFrame.FrameIndex;

                NewColorizedBodyFrameEvent.Invoke(colorizedBodyFrame);
            }
        }

        _frameCount++;
        _frameReadyDirty = true;
        frameReadyTime.Stop();
    }