< Summary

Class:DCL.GLTFast.Wrappers.GltFastDeferAgent
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTFast/Wrappers/GLTFastDeferAgent.cs
Covered lines:0
Uncovered lines:29
Coverable lines:29
Total lines:79
Line coverage:0% (0 of 29)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GltFastDeferAgent()0%2100%
Start()0%2100%
OnRenderStateChanged(...)0%2100%
UpdateTimeBudget()0%20400%
Awake()0%2100%
Update()0%2100%
ResetLastTime()0%2100%
ShouldDefer()0%2100%
ShouldDefer(...)0%2100%
FitsInCurrentFrame(...)0%2100%
BreakPoint()0%20400%
BreakPoint()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTFast/Wrappers/GLTFastDeferAgent.cs

#LineLine coverage
 1using System.Threading.Tasks;
 2using GLTFast;
 3using UnityEngine;
 4
 5namespace DCL.GLTFast.Wrappers
 6{
 7    // Note (Kinerius) This is a straight up copy of TimeBudgetPerFrameDeferAgent but it reacts to the renderer being in
 8    [DefaultExecutionOrder(-10)]
 9    public class GltFastDeferAgent : MonoBehaviour, IDeferAgent
 10    {
 11        [SerializeField]
 12        [Range(.01f, 5f)]
 13        [Tooltip("Per-frame time budget as fraction of the targeted frame time. Keep it well below 0.5, so there's enoug
 014        private float frameBudget = .5f;
 15
 16        private float lastTime;
 017        private float timeBudget = .5f / 30;
 18        private bool isRendererActive;
 19
 20        private void Start()
 21        {
 022            CommonScriptableObjects.rendererState.OnChange += OnRenderStateChanged;
 023            OnRenderStateChanged(CommonScriptableObjects.rendererState.Get(), false);
 024        }
 25
 26        private void OnRenderStateChanged(bool current, bool previous)
 27        {
 028            isRendererActive = current;
 029            UpdateTimeBudget();
 030        }
 31
 32        private void UpdateTimeBudget()
 33        {
 034            float targetFrameRate = Application.targetFrameRate;
 35
 036            if (targetFrameRate < 0)
 037                targetFrameRate = 30;
 38
 039            timeBudget = isRendererActive ? frameBudget / targetFrameRate : float.MaxValue;
 040            ResetLastTime();
 041        }
 42
 43        private void Awake()
 44        {
 045            UpdateTimeBudget();
 046        }
 47
 48        private void Update()
 49        {
 050            ResetLastTime();
 051        }
 52
 53        private void ResetLastTime()
 54        {
 055            lastTime = Time.realtimeSinceStartup;
 056        }
 57
 58        public bool ShouldDefer() =>
 059            !FitsInCurrentFrame(0);
 60
 61        public bool ShouldDefer(float duration) =>
 062            !FitsInCurrentFrame(duration);
 63
 64        private bool FitsInCurrentFrame(float duration) =>
 065            duration <= timeBudget - (Time.realtimeSinceStartup - lastTime);
 66
 67        public async Task BreakPoint()
 68        {
 069            if (ShouldDefer())
 070                await Task.Yield();
 071        }
 72
 73        public async Task BreakPoint(float duration)
 74        {
 075            if (ShouldDefer(duration))
 076                await Task.Yield();
 077        }
 78    }
 79}