GLRenderer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. _device.DisposeGpu();
  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 as we aren't going to use it in GTK Thread.
  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. }
  183. public void Initialize()
  184. {
  185. if (!(_device.Gpu.Renderer is Renderer))
  186. {
  187. throw new NotSupportedException($"GPU renderer must be an OpenGL renderer when using GLRenderer!");
  188. }
  189. _renderer = (Renderer)_device.Gpu.Renderer;
  190. }
  191. public void Render()
  192. {
  193. // First take exclusivity on the OpenGL context.
  194. GraphicsContext.MakeCurrent(WindowInfo);
  195. _renderer.Initialize();
  196. // Make sure the first frame is not transparent.
  197. GL.ClearColor(OpenTK.Color.Black);
  198. GL.Clear(ClearBufferMask.ColorBufferBit);
  199. SwapBuffers();
  200. while (IsActive)
  201. {
  202. if (IsStopped)
  203. {
  204. return;
  205. }
  206. _ticks += _chrono.ElapsedTicks;
  207. _chrono.Restart();
  208. if (_device.WaitFifo())
  209. {
  210. _device.ProcessFrame();
  211. }
  212. if (_ticks >= _ticksPerFrame)
  213. {
  214. _device.PresentFrame(SwapBuffers);
  215. _device.Statistics.RecordSystemFrameTime();
  216. double hostFps = _device.Statistics.GetSystemFrameRate();
  217. double gameFps = _device.Statistics.GetGameFrameRate();
  218. string titleNameSection = string.IsNullOrWhiteSpace(_device.System.TitleName) ? string.Empty
  219. : " | " + _device.System.TitleName;
  220. string titleIdSection = string.IsNullOrWhiteSpace(_device.System.TitleIdText) ? string.Empty
  221. : " | " + _device.System.TitleIdText.ToUpper();
  222. _newTitle = $"Ryujinx {Program.Version}{titleNameSection}{titleIdSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " +
  223. $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}";
  224. _titleEvent = true;
  225. _device.System.SignalVsync();
  226. _device.VsyncEvent.Set();
  227. _ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame);
  228. }
  229. }
  230. }
  231. public void SwapBuffers()
  232. {
  233. OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers();
  234. }
  235. public void MainLoop()
  236. {
  237. while (IsActive)
  238. {
  239. if (_titleEvent)
  240. {
  241. _titleEvent = false;
  242. Gtk.Application.Invoke(delegate
  243. {
  244. this.ParentWindow.Title = _newTitle;
  245. });
  246. }
  247. if (IsFocused)
  248. {
  249. UpdateFrame();
  250. }
  251. // Polling becomes expensive if it's not slept
  252. Thread.Sleep(1);
  253. }
  254. }
  255. private bool UpdateFrame()
  256. {
  257. if (!IsActive)
  258. {
  259. return true;
  260. }
  261. if (IsStopped)
  262. {
  263. return false;
  264. }
  265. HotkeyButtons currentHotkeyButtons = 0;
  266. ControllerButtons currentButton = 0;
  267. JoystickPosition leftJoystick;
  268. JoystickPosition rightJoystick;
  269. HLE.Input.Keyboard? hidKeyboard = null;
  270. KeyboardState keyboard = OpenTK.Input.Keyboard.GetState();
  271. Gtk.Application.Invoke(delegate
  272. {
  273. HandleScreenState(keyboard);
  274. });
  275. int leftJoystickDx = 0;
  276. int leftJoystickDy = 0;
  277. int rightJoystickDx = 0;
  278. int rightJoystickDy = 0;
  279. // Normal Input
  280. currentHotkeyButtons = KeyboardControls.GetHotkeyButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  281. currentButton = KeyboardControls.GetButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  282. if (ConfigurationState.Instance.Hid.EnableKeyboard)
  283. {
  284. hidKeyboard = KeyboardControls.GetKeysDown(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  285. }
  286. (leftJoystickDx, leftJoystickDy) = KeyboardControls.GetLeftStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  287. (rightJoystickDx, rightJoystickDy) = KeyboardControls.GetRightStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  288. if (!hidKeyboard.HasValue)
  289. {
  290. hidKeyboard = new HLE.Input.Keyboard
  291. {
  292. Modifier = 0,
  293. Keys = new int[0x8]
  294. };
  295. }
  296. currentButton |= _primaryController.GetButtons();
  297. // Keyboard has priority stick-wise
  298. if (leftJoystickDx == 0 && leftJoystickDy == 0)
  299. {
  300. (leftJoystickDx, leftJoystickDy) = _primaryController.GetLeftStick();
  301. }
  302. if (rightJoystickDx == 0 && rightJoystickDy == 0)
  303. {
  304. (rightJoystickDx, rightJoystickDy) = _primaryController.GetRightStick();
  305. }
  306. leftJoystick = new JoystickPosition
  307. {
  308. Dx = leftJoystickDx,
  309. Dy = leftJoystickDy
  310. };
  311. rightJoystick = new JoystickPosition
  312. {
  313. Dx = rightJoystickDx,
  314. Dy = rightJoystickDy
  315. };
  316. currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick);
  317. bool hasTouch = false;
  318. // Get screen touch position from left mouse click
  319. // OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  320. if (IsFocused && _mousePressed)
  321. {
  322. int screenWidth = AllocatedWidth;
  323. int screenHeight = AllocatedHeight;
  324. if (AllocatedWidth > (AllocatedHeight * TouchScreenWidth) / TouchScreenHeight)
  325. {
  326. screenWidth = (AllocatedHeight * TouchScreenWidth) / TouchScreenHeight;
  327. }
  328. else
  329. {
  330. screenHeight = (AllocatedWidth * TouchScreenHeight) / TouchScreenWidth;
  331. }
  332. int startX = (AllocatedWidth - screenWidth) >> 1;
  333. int startY = (AllocatedHeight - screenHeight) >> 1;
  334. int endX = startX + screenWidth;
  335. int endY = startY + screenHeight;
  336. if (_mouseX >= startX &&
  337. _mouseY >= startY &&
  338. _mouseX < endX &&
  339. _mouseY < endY)
  340. {
  341. int screenMouseX = (int)_mouseX - startX;
  342. int screenMouseY = (int)_mouseY - startY;
  343. int mX = (screenMouseX * TouchScreenWidth) / screenWidth;
  344. int mY = (screenMouseY * TouchScreenHeight) / screenHeight;
  345. TouchPoint currentPoint = new TouchPoint
  346. {
  347. X = mX,
  348. Y = mY,
  349. // Placeholder values till more data is acquired
  350. DiameterX = 10,
  351. DiameterY = 10,
  352. Angle = 90
  353. };
  354. hasTouch = true;
  355. _device.Hid.SetTouchPoints(currentPoint);
  356. }
  357. }
  358. if (!hasTouch)
  359. {
  360. _device.Hid.SetTouchPoints();
  361. }
  362. if (ConfigurationState.Instance.Hid.EnableKeyboard && hidKeyboard.HasValue)
  363. {
  364. _device.Hid.WriteKeyboard(hidKeyboard.Value);
  365. }
  366. BaseController controller = _device.Hid.PrimaryController;
  367. controller.SendInput(currentButton, leftJoystick, rightJoystick);
  368. // Toggle vsync
  369. if (currentHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync) &&
  370. !_prevHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync))
  371. {
  372. _device.EnableDeviceVsync = !_device.EnableDeviceVsync;
  373. }
  374. _prevHotkeyButtons = currentHotkeyButtons;
  375. return true;
  376. }
  377. }
  378. }