ProfileWindowManager.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. {
  23. Name = "Profiler.ProfileThread"
  24. };
  25. _profileThread.Start();
  26. }
  27. }
  28. public void ToggleVisible()
  29. {
  30. if (Profile.ProfilingEnabled())
  31. {
  32. _window.ToggleVisible();
  33. }
  34. }
  35. public void Close()
  36. {
  37. if (_window != null)
  38. {
  39. _profilerRunning = false;
  40. _window.Close();
  41. _window.Dispose();
  42. }
  43. _window = null;
  44. }
  45. public void UpdateKeyInput(KeyboardState keyboard)
  46. {
  47. if (Profile.Controls.TogglePressed(keyboard))
  48. {
  49. ToggleVisible();
  50. }
  51. Profile.Controls.SetPrevKeyboardState(keyboard);
  52. }
  53. private void ProfileLoop()
  54. {
  55. using (_window = new ProfileWindow())
  56. {
  57. // Create thread for render loop
  58. _renderThread = new Thread(RenderLoop)
  59. {
  60. Name = "Profiler.RenderThread"
  61. };
  62. _renderThread.Start();
  63. while (_profilerRunning)
  64. {
  65. double time = (double)PerformanceCounter.ElapsedTicks / PerformanceCounter.TicksPerSecond;
  66. _window.Update(new FrameEventArgs(time - _prevTime));
  67. _prevTime = time;
  68. // Sleep to be less taxing, update usually does very little
  69. Thread.Sleep(1);
  70. }
  71. }
  72. }
  73. private void RenderLoop()
  74. {
  75. _window.Context.MakeCurrent(_window.WindowInfo);
  76. while (_profilerRunning)
  77. {
  78. _window.Draw();
  79. Thread.Sleep(1);
  80. }
  81. }
  82. }
  83. }