aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/PerformanceStatistics.cs
blob: 2dabd9a08d0c60f1f1f72ac901565027636df32c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Diagnostics;
using System.Timers;

namespace Ryujinx.HLE
{
    public class PerformanceStatistics
    {
        private const double FrameRateWeight = 0.5;

        private const int FrameTypeSystem = 0;
        private const int FrameTypeGame   = 1;

        private double[] AverageFrameRate;
        private double[] AccumulatedFrameTime;
        private double[] PreviousFrameTime;

        private long[] FramesRendered;

        private object[] FrameLock;

        private double TicksToSeconds;

        private Stopwatch ExecutionTime;

        private Timer ResetTimer;

        public PerformanceStatistics()
        {
            AverageFrameRate     = new double[2];
            AccumulatedFrameTime = new double[2];
            PreviousFrameTime    = new double[2];

            FramesRendered = new long[2];

            FrameLock = new object[] { new object(), new object() };

            ExecutionTime = new Stopwatch();

            ExecutionTime.Start();

            ResetTimer = new Timer(1000);

            ResetTimer.Elapsed += ResetTimerElapsed;

            ResetTimer.AutoReset = true;

            ResetTimer.Start();

            TicksToSeconds = 1.0 / Stopwatch.Frequency;
        }

        private void ResetTimerElapsed(object sender, ElapsedEventArgs e)
        {
            CalculateAverageFrameRate(FrameTypeSystem);
            CalculateAverageFrameRate(FrameTypeGame);
        }

        private void CalculateAverageFrameRate(int FrameType)
        {
            double FrameRate = 0;

            if (AccumulatedFrameTime[FrameType] > 0)
            {
                FrameRate = FramesRendered[FrameType] / AccumulatedFrameTime[FrameType];
            }

            lock (FrameLock[FrameType])
            {
                AverageFrameRate[FrameType] = LinearInterpolate(AverageFrameRate[FrameType], FrameRate);

                FramesRendered[FrameType] = 0;

                AccumulatedFrameTime[FrameType] = 0;
            }
        }

        private double LinearInterpolate(double Old, double New)
        {
            return Old * (1.0 - FrameRateWeight) + New * FrameRateWeight;
        }

        public void RecordSystemFrameTime()
        {
            RecordFrameTime(FrameTypeSystem);
        }

        public void RecordGameFrameTime()
        {
            RecordFrameTime(FrameTypeGame);
        }

        private void RecordFrameTime(int FrameType)
        {
            double CurrentFrameTime = ExecutionTime.ElapsedTicks * TicksToSeconds;

            double ElapsedFrameTime = CurrentFrameTime - PreviousFrameTime[FrameType];

            PreviousFrameTime[FrameType] = CurrentFrameTime;

            lock (FrameLock[FrameType])
            {
                AccumulatedFrameTime[FrameType] += ElapsedFrameTime;

                FramesRendered[FrameType]++;
            }
        }

        public double GetSystemFrameRate()
        {
            return AverageFrameRate[FrameTypeSystem];
        }

        public double GetGameFrameRate()
        {
            return AverageFrameRate[FrameTypeGame];
        }
    }
}