ProfileWindowManager.cs 2.3 KB

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