GLRenderer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. using Gdk;
  2. using OpenTK;
  3. using OpenTK.Graphics;
  4. using OpenTK.Graphics.OpenGL;
  5. using OpenTK.Input;
  6. using OpenTK.Platform;
  7. using Ryujinx.Configuration;
  8. using Ryujinx.Graphics.OpenGL;
  9. using Ryujinx.HLE;
  10. using Ryujinx.HLE.Input;
  11. using Ryujinx.Ui;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Threading;
  16. namespace Ryujinx.Ui
  17. {
  18. public class GLRenderer : GLWidget
  19. {
  20. private const int TouchScreenWidth = 1280;
  21. private const int TouchScreenHeight = 720;
  22. private const int TargetFps = 60;
  23. public ManualResetEvent WaitEvent { get; set; }
  24. public bool IsActive { get; set; }
  25. public bool IsStopped { get; set; }
  26. public bool IsFocused { get; set; }
  27. private double _mouseX;
  28. private double _mouseY;
  29. private bool _mousePressed;
  30. private bool _titleEvent;
  31. private bool _toggleFullscreen;
  32. private string _newTitle;
  33. private readonly long _ticksPerFrame;
  34. private long _ticks = 0;
  35. private System.Diagnostics.Stopwatch _chrono;
  36. private Switch _device;
  37. private Renderer _renderer;
  38. private HotkeyButtons _prevHotkeyButtons = 0;
  39. private Input.NpadController _primaryController;
  40. public GLRenderer(Switch device)
  41. : base (new GraphicsMode(new ColorFormat()),
  42. 3, 3,
  43. GraphicsContextFlags.ForwardCompatible)
  44. {
  45. WaitEvent = new ManualResetEvent(false);
  46. _device = device;
  47. this.Initialized += GLRenderer_Initialized;
  48. this.Destroyed += GLRenderer_Destroyed;
  49. this.ShuttingDown += GLRenderer_ShuttingDown;
  50. Initialize();
  51. _chrono = new System.Diagnostics.Stopwatch();
  52. _ticksPerFrame = System.Diagnostics.Stopwatch.Frequency / TargetFps;
  53. _primaryController = new Input.NpadController(ConfigurationState.Instance.Hid.JoystickControls);
  54. AddEvents((int)(Gdk.EventMask.ButtonPressMask
  55. | Gdk.EventMask.ButtonReleaseMask
  56. | Gdk.EventMask.PointerMotionMask
  57. | Gdk.EventMask.KeyPressMask
  58. | Gdk.EventMask.KeyReleaseMask));
  59. this.Shown += Renderer_Shown;
  60. }
  61. private void GLRenderer_ShuttingDown(object sender, EventArgs args)
  62. {
  63. Exit();
  64. }
  65. private void Parent_FocusOutEvent(object o, Gtk.FocusOutEventArgs args)
  66. {
  67. IsFocused = false;
  68. }
  69. private void Parent_FocusInEvent(object o, Gtk.FocusInEventArgs args)
  70. {
  71. IsFocused = true;
  72. }
  73. private void GLRenderer_Destroyed(object sender, EventArgs e)
  74. {
  75. Dispose();
  76. }
  77. protected void Renderer_Shown(object sender, EventArgs e)
  78. {
  79. IsFocused = this.ParentWindow.State.HasFlag(Gdk.WindowState.Focused);
  80. }
  81. public void HandleScreenState(KeyboardState keyboard)
  82. {
  83. bool toggleFullscreen = keyboard.IsKeyDown(OpenTK.Input.Key.F11)
  84. || ((keyboard.IsKeyDown(OpenTK.Input.Key.AltLeft)
  85. || keyboard.IsKeyDown(OpenTK.Input.Key.AltRight))
  86. && keyboard.IsKeyDown(OpenTK.Input.Key.Enter))
  87. || keyboard.IsKeyDown(OpenTK.Input.Key.Escape);
  88. bool fullScreenToggled = ParentWindow.State.HasFlag(Gdk.WindowState.Fullscreen);
  89. if (toggleFullscreen != _toggleFullscreen)
  90. {
  91. if (toggleFullscreen)
  92. {
  93. if (fullScreenToggled)
  94. {
  95. ParentWindow.Unfullscreen();
  96. (Toplevel as MainWindow)?.ToggleExtraWidgets(true);
  97. }
  98. else
  99. {
  100. if (keyboard.IsKeyDown(OpenTK.Input.Key.Escape))
  101. {
  102. Exit();
  103. }
  104. else
  105. {
  106. ParentWindow.Fullscreen();
  107. (Toplevel as MainWindow)?.ToggleExtraWidgets(false);
  108. }
  109. }
  110. }
  111. }
  112. _toggleFullscreen = toggleFullscreen;
  113. }
  114. private void GLRenderer_Initialized(object sender, EventArgs e)
  115. {
  116. // Release the GL exclusivity that OpenTK gave us.
  117. GraphicsContext.MakeCurrent(null);
  118. WaitEvent.Set();
  119. }
  120. protected override bool OnConfigureEvent(EventConfigure evnt)
  121. {
  122. var result = base.OnConfigureEvent(evnt);
  123. _renderer.Window.SetSize(AllocatedWidth, AllocatedHeight);
  124. return result;
  125. }
  126. public void Start()
  127. {
  128. IsRenderHandler = true;
  129. _chrono.Restart();
  130. IsActive = true;
  131. Gtk.Window parent = this.Toplevel as Gtk.Window;
  132. parent.FocusInEvent += Parent_FocusInEvent;
  133. parent.FocusOutEvent += Parent_FocusOutEvent;
  134. Gtk.Application.Invoke(delegate
  135. {
  136. parent.Present();
  137. });
  138. Thread renderLoopThread = new Thread(Render)
  139. {
  140. Name = "GUI.RenderLoop"
  141. };
  142. renderLoopThread.Start();
  143. MainLoop();
  144. renderLoopThread.Join();
  145. Exit();
  146. }
  147. protected override bool OnButtonPressEvent(EventButton evnt)
  148. {
  149. _mouseX = evnt.X;
  150. _mouseY = evnt.Y;
  151. if (evnt.Button == 1)
  152. {
  153. _mousePressed = true;
  154. }
  155. return false;
  156. }
  157. protected override bool OnButtonReleaseEvent(EventButton evnt)
  158. {
  159. if (evnt.Button == 1)
  160. {
  161. _mousePressed = false;
  162. }
  163. return false;
  164. }
  165. protected override bool OnMotionNotifyEvent(EventMotion evnt)
  166. {
  167. if (evnt.Device.InputSource == InputSource.Mouse)
  168. {
  169. _mouseX = evnt.X;
  170. _mouseY = evnt.Y;
  171. }
  172. return false;
  173. }
  174. public void Exit()
  175. {
  176. if (IsStopped)
  177. {
  178. return;
  179. }
  180. IsStopped = true;
  181. IsActive = false;
  182. using (ScopedGlContext scopedGLContext = new ScopedGlContext(WindowInfo, GraphicsContext))
  183. {
  184. _device.DisposeGpu();
  185. }
  186. WaitEvent.Set();
  187. }
  188. public void Initialize()
  189. {
  190. if (!(_device.Gpu.Renderer is Renderer))
  191. {
  192. throw new NotSupportedException($"GPU renderer must be an OpenGL renderer when using GLRenderer!");
  193. }
  194. _renderer = (Renderer)_device.Gpu.Renderer;
  195. }
  196. public void Render()
  197. {
  198. using (ScopedGlContext scopedGLContext = new ScopedGlContext(WindowInfo, GraphicsContext))
  199. {
  200. _renderer.Initialize();
  201. SwapBuffers();
  202. }
  203. while (IsActive)
  204. {
  205. if (IsStopped)
  206. {
  207. return;
  208. }
  209. using (ScopedGlContext scopedGLContext = new ScopedGlContext(WindowInfo, GraphicsContext))
  210. {
  211. _ticks += _chrono.ElapsedTicks;
  212. _chrono.Restart();
  213. if (_device.WaitFifo())
  214. {
  215. _device.ProcessFrame();
  216. }
  217. if (_ticks >= _ticksPerFrame)
  218. {
  219. _device.PresentFrame(SwapBuffers);
  220. _device.Statistics.RecordSystemFrameTime();
  221. double hostFps = _device.Statistics.GetSystemFrameRate();
  222. double gameFps = _device.Statistics.GetGameFrameRate();
  223. string titleNameSection = string.IsNullOrWhiteSpace(_device.System.TitleName) ? string.Empty
  224. : " | " + _device.System.TitleName;
  225. string titleIdSection = string.IsNullOrWhiteSpace(_device.System.TitleIdText) ? string.Empty
  226. : " | " + _device.System.TitleIdText.ToUpper();
  227. _newTitle = $"Ryujinx {Program.Version}{titleNameSection}{titleIdSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " +
  228. $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}";
  229. _titleEvent = true;
  230. _device.System.SignalVsync();
  231. _device.VsyncEvent.Set();
  232. _ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame);
  233. }
  234. }
  235. }
  236. }
  237. public void SwapBuffers()
  238. {
  239. OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers();
  240. }
  241. public void MainLoop()
  242. {
  243. while (IsActive)
  244. {
  245. if (_titleEvent)
  246. {
  247. _titleEvent = false;
  248. Gtk.Application.Invoke(delegate
  249. {
  250. this.ParentWindow.Title = _newTitle;
  251. });
  252. }
  253. if (IsFocused)
  254. {
  255. UpdateFrame();
  256. }
  257. // Polling becomes expensive if it's not slept
  258. Thread.Sleep(1);
  259. }
  260. }
  261. private bool UpdateFrame()
  262. {
  263. if (!IsActive)
  264. {
  265. return true;
  266. }
  267. if (IsStopped)
  268. {
  269. return false;
  270. }
  271. HotkeyButtons currentHotkeyButtons = 0;
  272. ControllerButtons currentButton = 0;
  273. JoystickPosition leftJoystick;
  274. JoystickPosition rightJoystick;
  275. HLE.Input.Keyboard? hidKeyboard = null;
  276. KeyboardState keyboard = OpenTK.Input.Keyboard.GetState();
  277. Gtk.Application.Invoke(delegate
  278. {
  279. HandleScreenState(keyboard);
  280. });
  281. int leftJoystickDx = 0;
  282. int leftJoystickDy = 0;
  283. int rightJoystickDx = 0;
  284. int rightJoystickDy = 0;
  285. // Normal Input
  286. currentHotkeyButtons = KeyboardControls.GetHotkeyButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  287. currentButton = KeyboardControls.GetButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  288. if (ConfigurationState.Instance.Hid.EnableKeyboard)
  289. {
  290. hidKeyboard = KeyboardControls.GetKeysDown(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  291. }
  292. (leftJoystickDx, leftJoystickDy) = KeyboardControls.GetLeftStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  293. (rightJoystickDx, rightJoystickDy) = KeyboardControls.GetRightStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  294. if (!hidKeyboard.HasValue)
  295. {
  296. hidKeyboard = new HLE.Input.Keyboard
  297. {
  298. Modifier = 0,
  299. Keys = new int[0x8]
  300. };
  301. }
  302. currentButton |= _primaryController.GetButtons();
  303. // Keyboard has priority stick-wise
  304. if (leftJoystickDx == 0 && leftJoystickDy == 0)
  305. {
  306. (leftJoystickDx, leftJoystickDy) = _primaryController.GetLeftStick();
  307. }
  308. if (rightJoystickDx == 0 && rightJoystickDy == 0)
  309. {
  310. (rightJoystickDx, rightJoystickDy) = _primaryController.GetRightStick();
  311. }
  312. leftJoystick = new JoystickPosition
  313. {
  314. Dx = leftJoystickDx,
  315. Dy = leftJoystickDy
  316. };
  317. rightJoystick = new JoystickPosition
  318. {
  319. Dx = rightJoystickDx,
  320. Dy = rightJoystickDy
  321. };
  322. currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick);
  323. bool hasTouch = false;
  324. // Get screen touch position from left mouse click
  325. // OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  326. if (IsFocused && _mousePressed)
  327. {
  328. int screenWidth = AllocatedWidth;
  329. int screenHeight = AllocatedHeight;
  330. if (AllocatedWidth > (AllocatedHeight * TouchScreenWidth) / TouchScreenHeight)
  331. {
  332. screenWidth = (AllocatedHeight * TouchScreenWidth) / TouchScreenHeight;
  333. }
  334. else
  335. {
  336. screenHeight = (AllocatedWidth * TouchScreenHeight) / TouchScreenWidth;
  337. }
  338. int startX = (AllocatedWidth - screenWidth) >> 1;
  339. int startY = (AllocatedHeight - screenHeight) >> 1;
  340. int endX = startX + screenWidth;
  341. int endY = startY + screenHeight;
  342. if (_mouseX >= startX &&
  343. _mouseY >= startY &&
  344. _mouseX < endX &&
  345. _mouseY < endY)
  346. {
  347. int screenMouseX = (int)_mouseX - startX;
  348. int screenMouseY = (int)_mouseY - startY;
  349. int mX = (screenMouseX * TouchScreenWidth) / screenWidth;
  350. int mY = (screenMouseY * TouchScreenHeight) / screenHeight;
  351. TouchPoint currentPoint = new TouchPoint
  352. {
  353. X = mX,
  354. Y = mY,
  355. // Placeholder values till more data is acquired
  356. DiameterX = 10,
  357. DiameterY = 10,
  358. Angle = 90
  359. };
  360. hasTouch = true;
  361. _device.Hid.SetTouchPoints(currentPoint);
  362. }
  363. }
  364. if (!hasTouch)
  365. {
  366. _device.Hid.SetTouchPoints();
  367. }
  368. if (ConfigurationState.Instance.Hid.EnableKeyboard && hidKeyboard.HasValue)
  369. {
  370. _device.Hid.WriteKeyboard(hidKeyboard.Value);
  371. }
  372. BaseController controller = _device.Hid.PrimaryController;
  373. controller.SendInput(currentButton, leftJoystick, rightJoystick);
  374. // Toggle vsync
  375. if (currentHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync) &&
  376. !_prevHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync))
  377. {
  378. _device.EnableDeviceVsync = !_device.EnableDeviceVsync;
  379. }
  380. _prevHotkeyButtons = currentHotkeyButtons;
  381. return true;
  382. }
  383. }
  384. }