aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Profiler/UI/ProfileWindowManager.cs
blob: c6a65a317c8c5126a88db490888c9f71e9e8c343 (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
using System.Threading;
using OpenTK;
using OpenTK.Input;
using Ryujinx.Common;

namespace Ryujinx.Profiler.UI
{
    public class ProfileWindowManager
    {
        private ProfileWindow _window;
        private Thread _profileThread;
        private Thread _renderThread;
        private bool _profilerRunning;

        // Timing
        private double _prevTime;

        public ProfileWindowManager()
        {
            if (Profile.ProfilingEnabled())
            {
                _profilerRunning = true;
                _prevTime        = 0;
                _profileThread   = new Thread(ProfileLoop);
                _profileThread.Start();
            }
        }

        public void ToggleVisible()
        {
            if (Profile.ProfilingEnabled())
            {
                _window.ToggleVisible();
            }
        }

        public void Close()
        {
            if (_window != null)
            {
                _profilerRunning = false;
                _window.Close();
                _window.Dispose();
            }

            _window = null;
        }

        public void UpdateKeyInput(KeyboardState keyboard)
        {
            if (Profile.Controls.TogglePressed(keyboard))
            {
                ToggleVisible();
            }
            Profile.Controls.SetPrevKeyboardState(keyboard);
        }

        private void ProfileLoop()
        {
            using (_window = new ProfileWindow())
            {
                // Create thread for render loop
                _renderThread = new Thread(RenderLoop);
                _renderThread.Start();

                while (_profilerRunning)
                {
                    double time = (double)PerformanceCounter.ElapsedTicks / PerformanceCounter.TicksPerSecond;
                    _window.Update(new FrameEventArgs(time - _prevTime));
                    _prevTime = time;

                    // Sleep to be less taxing, update usually does very little
                    Thread.Sleep(1);
                }
            }
        }

        private void RenderLoop()
        {
            _window.Context.MakeCurrent(_window.WindowInfo);

            while (_profilerRunning)
            {
                _window.Draw();
                Thread.Sleep(1);
            }
        }
    }
}