aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Controls/RenderTimer.cs
blob: 577115ea6bbcde2172ca113bb5df699aafdd8196 (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
using Avalonia.Rendering;
using System;
using System.Threading;
using System.Timers;

namespace Ryujinx.Ava.Ui.Controls
{
    internal class RenderTimer : IRenderTimer, IDisposable
    {
        public event Action<TimeSpan> Tick
        {
            add
            {
                _tick += value;

                if (_subscriberCount++ == 0)
                {
                    Start();
                }
            }

            remove
            {
                if (--_subscriberCount == 0)
                {
                    Stop();
                }

                _tick -= value;
            }
        }

        private Thread _tickThread;
        private readonly System.Timers.Timer _timer;

        private Action<TimeSpan> _tick;
        private int _subscriberCount;

        private bool _isRunning;

        private AutoResetEvent _resetEvent;

        public RenderTimer()
        {
            _timer = new System.Timers.Timer(15);
            _resetEvent = new AutoResetEvent(true);
            _timer.Elapsed += Timer_Elapsed;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            TickNow();
        }

        public void Start()
        {
            _timer.Start();
            if (_tickThread == null)
            {
                _tickThread = new Thread(RunTick);
                _tickThread.Name = "RenderTimerTickThread";
                _tickThread.IsBackground = true;
                _isRunning = true;
                _tickThread.Start();
            }
        }

        public void RunTick()
        {
            while (_isRunning)
            {
                _resetEvent.WaitOne();
                _tick?.Invoke(TimeSpan.FromMilliseconds(Environment.TickCount));
            }
        }

        public void TickNow()
        {
            lock (_timer)
            {
                _resetEvent.Set();
            }
        }

        public void Stop()
        {
            _timer.Stop();
        }

        public void Dispose()
        {
            _timer.Elapsed -= Timer_Elapsed;
            _timer.Stop();
            _isRunning = false;
            _resetEvent.Set();
            _tickThread.Join();
            _resetEvent.Dispose();
        }
    }
}