< Summary

Class:GPUSkinningThrottlerService
Assembly:RenderUtils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/Utils/GPUSkinning/GPUSkinningThrottlerService.cs
Covered lines:31
Uncovered lines:4
Coverable lines:35
Total lines:84
Line coverage:88.5% (31 of 35)
Covered branches:0
Total branches:0
Covered methods:10
Total methods:10
Method coverage:100% (10 of 10)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GPUSkinningThrottlerService()0%110100%
Create(...)0%220100%
Initialize()0%110100%
Register(...)0%2.52050%
Unregister(...)0%2.152066.67%
ModifyThrottling(...)0%220100%
ForceStop()0%110100%
Dispose()0%110100%
Cancel()0%220100%
ThrottleUpdateAsync()0%9.049092.31%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/Utils/GPUSkinning/GPUSkinningThrottlerService.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using GPUSkinning;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine;
 6
 7public class GPUSkinningThrottlerService : IGPUSkinningThrottlerService
 8{
 4309    private readonly Dictionary<IGPUSkinning, int> gpuSkinnings = new ();
 10
 11    private CancellationTokenSource cts;
 12
 13    public static GPUSkinningThrottlerService Create(bool initializeOnSpawn)
 14    {
 43015        GPUSkinningThrottlerService service = new GPUSkinningThrottlerService();
 16
 43017        if (initializeOnSpawn)
 43018            service.Initialize();
 19
 43020        return service;
 21    }
 22
 23    public void Initialize()
 24    {
 85525        cts = new CancellationTokenSource();
 85526        ThrottleUpdateAsync(cts.Token).Forget();
 85527    }
 28
 29    public void Register(IGPUSkinning gpuSkinning, int framesBetweenUpdates = 1)
 30    {
 531        if (!gpuSkinnings.ContainsKey(gpuSkinning))
 532            gpuSkinnings.Add(gpuSkinning, framesBetweenUpdates);
 33        else
 034            ModifyThrottling(gpuSkinning, framesBetweenUpdates);
 035    }
 36
 37    public void Unregister(IGPUSkinning gpuSkinning)
 38    {
 52139        if (gpuSkinnings.ContainsKey(gpuSkinning))
 040            gpuSkinnings.Remove(gpuSkinning);
 52141    }
 42
 43    public void ModifyThrottling(IGPUSkinning gpuSkinning, int framesBetweenUpdates)
 44    {
 545        if (gpuSkinnings.ContainsKey(gpuSkinning))
 546            gpuSkinnings[gpuSkinning] = framesBetweenUpdates;
 547    }
 48
 49    public void ForceStop()
 50    {
 151        Cancel();
 152    }
 53
 54    public void Dispose()
 55    {
 43156        Cancel();
 43157        gpuSkinnings.Clear();
 43158    }
 59
 60    private void Cancel()
 61    {
 43262        if (cts != null)
 63        {
 43064            cts.Cancel();
 43065            cts.Dispose();
 43066            cts = null;
 67        }
 43268    }
 69
 70    private async UniTaskVoid ThrottleUpdateAsync(CancellationToken ct)
 71    {
 256572        await UniTask.DelayFrame(1, PlayerLoopTiming.PostLateUpdate, ct);
 73
 74        // Cancel gracefully
 657066875        while (!ct.IsCancellationRequested)
 76        {
 1314140877            foreach (KeyValuePair<IGPUSkinning, int> entry in gpuSkinnings)
 3678                if (Time.frameCount % entry.Value == 0)
 2379                    entry.Key.Update();
 80
 1971157981            await UniTask.DelayFrame(1, PlayerLoopTiming.PostLateUpdate, ct);
 82        }
 083    }
 84}