ProfileWindowManager.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Threading;
  2. using OpenTK;
  3. using OpenTK.Input;
  4. using Ryujinx.Common;
  5. namespace Ryujinx.Profiler.UI
  6. {
  7. public class ProfileWindowManager
  8. {
  9. private ProfileWindow _window;
  10. private Thread _profileThread;
  11. private Thread _renderThread;
  12. private bool _profilerRunning;
  13. // Timing
  14. private double _prevTime;
  15. public ProfileWindowManager()
  16. {
  17. if (Profile.ProfilingEnabled())
  18. {
  19. _profilerRunning = true;
  20. _prevTime = 0;
  21. _profileThread = new Thread(ProfileLoop);
  22. _profileThread.Start();
  23. }
  24. }
  25. public void ToggleVisible()
  26. {
  27. if (Profile.ProfilingEnabled())
  28. {
  29. _window.ToggleVisible();
  30. }
  31. }
  32. public void Close()
  33. {
  34. if (_window != null)
  35. {
  36. _profilerRunning = false;
  37. _window.Close();
  38. _window.Dispose();
  39. }
  40. _window = null;
  41. }
  42. public void UpdateKeyInput(KeyboardState keyboard)
  43. {
  44. if (Profile.Controls.TogglePressed(keyboard))
  45. {
  46. ToggleVisible();
  47. }
  48. Profile.Controls.SetPrevKeyboardState(keyboard);
  49. }
  50. private void ProfileLoop()
  51. {
  52. using (_window = new ProfileWindow())
  53. {
  54. // Create thread for render loop
  55. _renderThread = new Thread(RenderLoop);
  56. _renderThread.Start();
  57. while (_profilerRunning)
  58. {
  59. double time = (double)PerformanceCounter.ElapsedTicks / PerformanceCounter.TicksPerSecond;
  60. _window.Update(new FrameEventArgs(time - _prevTime));
  61. _prevTime = time;
  62. // Sleep to be less taxing, update usually does very little
  63. Thread.Sleep(1);
  64. }
  65. }
  66. }
  67. private void RenderLoop()
  68. {
  69. _window.Context.MakeCurrent(_window.WindowInfo);
  70. while (_profilerRunning)
  71. {
  72. _window.Draw();
  73. Thread.Sleep(1);
  74. }
  75. }
  76. }
  77. }