GLRenderer.cs 15 KB

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