WindowBase.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. using Humanizer;
  2. using LibHac.Tools.Fs;
  3. using Ryujinx.Ava;
  4. using Ryujinx.Common.Configuration;
  5. using Ryujinx.Common.Configuration.Hid;
  6. using Ryujinx.Common.Logging;
  7. using Ryujinx.Graphics.GAL;
  8. using Ryujinx.Graphics.GAL.Multithreading;
  9. using Ryujinx.Graphics.Gpu;
  10. using Ryujinx.Graphics.OpenGL;
  11. using Ryujinx.HLE.HOS.Applets;
  12. using Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy.Types;
  13. using Ryujinx.HLE.UI;
  14. using Ryujinx.Input;
  15. using Ryujinx.Input.HLE;
  16. using Ryujinx.Input.SDL2;
  17. using Ryujinx.SDL2.Common;
  18. using System;
  19. using System.Collections.Concurrent;
  20. using System.Collections.Generic;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Threading;
  25. using static SDL2.SDL;
  26. using AntiAliasing = Ryujinx.Common.Configuration.AntiAliasing;
  27. using ScalingFilter = Ryujinx.Common.Configuration.ScalingFilter;
  28. using Switch = Ryujinx.HLE.Switch;
  29. namespace Ryujinx.Headless
  30. {
  31. abstract partial class WindowBase : IHostUIHandler, IDisposable
  32. {
  33. protected const int DefaultWidth = 1280;
  34. protected const int DefaultHeight = 720;
  35. private const int TargetFps = 60;
  36. private SDL_WindowFlags DefaultFlags = SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI | SDL_WindowFlags.SDL_WINDOW_RESIZABLE | SDL_WindowFlags.SDL_WINDOW_INPUT_FOCUS | SDL_WindowFlags.SDL_WINDOW_SHOWN;
  37. private SDL_WindowFlags FullscreenFlag = 0;
  38. private static readonly ConcurrentQueue<Action> _mainThreadActions = new();
  39. [LibraryImport("SDL2")]
  40. // TODO: Remove this as soon as SDL2-CS was updated to expose this method publicly
  41. private static partial nint SDL_LoadBMP_RW(nint src, int freesrc);
  42. public static void QueueMainThreadAction(Action action)
  43. {
  44. _mainThreadActions.Enqueue(action);
  45. }
  46. public NpadManager NpadManager { get; }
  47. public TouchScreenManager TouchScreenManager { get; }
  48. public Switch Device { get; private set; }
  49. public IRenderer Renderer { get; private set; }
  50. public event EventHandler<StatusUpdatedEventArgs> StatusUpdatedEvent;
  51. protected nint WindowHandle { get; set; }
  52. public IHostUITheme HostUITheme { get; }
  53. public int Width { get; private set; }
  54. public int Height { get; private set; }
  55. public int DisplayId { get; set; }
  56. public bool IsFullscreen { get; set; }
  57. public bool IsExclusiveFullscreen { get; set; }
  58. public int ExclusiveFullscreenWidth { get; set; }
  59. public int ExclusiveFullscreenHeight { get; set; }
  60. public AntiAliasing AntiAliasing { get; set; }
  61. public ScalingFilter ScalingFilter { get; set; }
  62. public int ScalingFilterLevel { get; set; }
  63. protected SDL2MouseDriver MouseDriver;
  64. private readonly InputManager _inputManager;
  65. private readonly IKeyboard _keyboardInterface;
  66. private readonly GraphicsDebugLevel _glLogLevel;
  67. private readonly Stopwatch _chrono;
  68. private readonly long _ticksPerFrame;
  69. private readonly CancellationTokenSource _gpuCancellationTokenSource;
  70. private readonly ManualResetEvent _exitEvent;
  71. private readonly ManualResetEvent _gpuDoneEvent;
  72. private long _ticks;
  73. private bool _isActive;
  74. private bool _isStopped;
  75. private uint _windowId;
  76. private string _gpuDriverName;
  77. private readonly AspectRatio _aspectRatio;
  78. private readonly bool _enableMouse;
  79. private readonly bool _ignoreControllerApplet;
  80. public WindowBase(
  81. InputManager inputManager,
  82. GraphicsDebugLevel glLogLevel,
  83. AspectRatio aspectRatio,
  84. bool enableMouse,
  85. HideCursorMode hideCursorMode,
  86. bool ignoreControllerApplet)
  87. {
  88. MouseDriver = new SDL2MouseDriver(hideCursorMode);
  89. _inputManager = inputManager;
  90. _inputManager.SetMouseDriver(MouseDriver);
  91. NpadManager = _inputManager.CreateNpadManager();
  92. TouchScreenManager = _inputManager.CreateTouchScreenManager();
  93. _keyboardInterface = (IKeyboard)_inputManager.KeyboardDriver.GetGamepad("0");
  94. _glLogLevel = glLogLevel;
  95. _chrono = new Stopwatch();
  96. _ticksPerFrame = Stopwatch.Frequency / TargetFps;
  97. _gpuCancellationTokenSource = new CancellationTokenSource();
  98. _exitEvent = new ManualResetEvent(false);
  99. _gpuDoneEvent = new ManualResetEvent(false);
  100. _aspectRatio = aspectRatio;
  101. _enableMouse = enableMouse;
  102. _ignoreControllerApplet = ignoreControllerApplet;
  103. HostUITheme = new HeadlessHostUiTheme();
  104. SDL2Driver.Instance.Initialize();
  105. }
  106. public void Initialize(Switch device, List<InputConfig> inputConfigs, bool enableKeyboard, bool enableMouse)
  107. {
  108. Device = device;
  109. IRenderer renderer = Device.Gpu.Renderer;
  110. if (renderer is ThreadedRenderer tr)
  111. {
  112. renderer = tr.BaseRenderer;
  113. }
  114. Renderer = renderer;
  115. NpadManager.Initialize(device, inputConfigs, enableKeyboard, enableMouse);
  116. TouchScreenManager.Initialize(device);
  117. }
  118. private void SetWindowIcon()
  119. {
  120. Stream iconStream = typeof(Program).Assembly.GetManifestResourceStream("HeadlessLogo");
  121. byte[] iconBytes = new byte[iconStream!.Length];
  122. if (iconStream.Read(iconBytes, 0, iconBytes.Length) != iconBytes.Length)
  123. {
  124. Logger.Error?.Print(LogClass.Application, "Failed to read icon to byte array.");
  125. iconStream.Close();
  126. return;
  127. }
  128. iconStream.Close();
  129. unsafe
  130. {
  131. fixed (byte* iconPtr = iconBytes)
  132. {
  133. nint rwOpsStruct = SDL_RWFromConstMem((nint)iconPtr, iconBytes.Length);
  134. nint iconHandle = SDL_LoadBMP_RW(rwOpsStruct, 1);
  135. SDL_SetWindowIcon(WindowHandle, iconHandle);
  136. SDL_FreeSurface(iconHandle);
  137. }
  138. }
  139. }
  140. private void InitializeWindow()
  141. {
  142. var activeProcess = Device.Processes.ActiveApplication;
  143. var nacp = activeProcess.ApplicationControlProperties;
  144. int desiredLanguage = (int)Device.System.State.DesiredTitleLanguage;
  145. string titleNameSection = string.IsNullOrWhiteSpace(nacp.Title[desiredLanguage].NameString.ToString()) ? string.Empty : $" - {nacp.Title[desiredLanguage].NameString.ToString()}";
  146. string titleVersionSection = string.IsNullOrWhiteSpace(nacp.DisplayVersionString.ToString()) ? string.Empty : $" v{nacp.DisplayVersionString.ToString()}";
  147. string titleIdSection = string.IsNullOrWhiteSpace(activeProcess.ProgramIdText) ? string.Empty : $" ({activeProcess.ProgramIdText.ToUpper()})";
  148. string titleArchSection = activeProcess.Is64Bit ? " (64-bit)" : " (32-bit)";
  149. Width = DefaultWidth;
  150. Height = DefaultHeight;
  151. if (IsExclusiveFullscreen)
  152. {
  153. Width = ExclusiveFullscreenWidth;
  154. Height = ExclusiveFullscreenHeight;
  155. DefaultFlags = SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI;
  156. FullscreenFlag = SDL_WindowFlags.SDL_WINDOW_FULLSCREEN;
  157. }
  158. else if (IsFullscreen)
  159. {
  160. DefaultFlags = SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI;
  161. FullscreenFlag = SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP;
  162. }
  163. WindowHandle = SDL_CreateWindow($"Ryujinx {Program.Version}{titleNameSection}{titleVersionSection}{titleIdSection}{titleArchSection}", SDL_WINDOWPOS_CENTERED_DISPLAY(DisplayId), SDL_WINDOWPOS_CENTERED_DISPLAY(DisplayId), Width, Height, DefaultFlags | FullscreenFlag | WindowFlags);
  164. if (WindowHandle == nint.Zero)
  165. {
  166. string errorMessage = $"SDL_CreateWindow failed with error \"{SDL_GetError()}\"";
  167. Logger.Error?.Print(LogClass.Application, errorMessage);
  168. throw new Exception(errorMessage);
  169. }
  170. SetWindowIcon();
  171. _windowId = SDL_GetWindowID(WindowHandle);
  172. SDL2Driver.Instance.RegisterWindow(_windowId, HandleWindowEvent);
  173. }
  174. private void HandleWindowEvent(SDL_Event evnt)
  175. {
  176. if (evnt.type == SDL_EventType.SDL_WINDOWEVENT)
  177. {
  178. switch (evnt.window.windowEvent)
  179. {
  180. case SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED:
  181. // Unlike on Windows, this event fires on macOS when triggering fullscreen mode.
  182. // And promptly crashes the process because `Renderer?.window.SetSize` is undefined.
  183. // As we don't need this to fire in either case we can test for fullscreen.
  184. if (!IsFullscreen && !IsExclusiveFullscreen)
  185. {
  186. Width = evnt.window.data1;
  187. Height = evnt.window.data2;
  188. Renderer?.Window.SetSize(Width, Height);
  189. MouseDriver.SetClientSize(Width, Height);
  190. }
  191. break;
  192. case SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE:
  193. Exit();
  194. break;
  195. }
  196. }
  197. else
  198. {
  199. MouseDriver.Update(evnt);
  200. }
  201. }
  202. protected abstract void InitializeWindowRenderer();
  203. protected abstract void InitializeRenderer();
  204. protected abstract void FinalizeWindowRenderer();
  205. protected abstract void SwapBuffers();
  206. public abstract SDL_WindowFlags WindowFlags { get; }
  207. private string GetGpuDriverName()
  208. {
  209. return Renderer.GetHardwareInfo().GpuDriver;
  210. }
  211. private void SetAntiAliasing()
  212. {
  213. Renderer?.Window.SetAntiAliasing(AntiAliasing);
  214. }
  215. private void SetScalingFilter()
  216. {
  217. Renderer?.Window.SetScalingFilter(ScalingFilter);
  218. Renderer?.Window.SetScalingFilterLevel(ScalingFilterLevel);
  219. }
  220. public void Render()
  221. {
  222. InitializeWindowRenderer();
  223. Device.Gpu.Renderer.Initialize(_glLogLevel);
  224. InitializeRenderer();
  225. SetAntiAliasing();
  226. SetScalingFilter();
  227. _gpuDriverName = GetGpuDriverName();
  228. Device.Gpu.Renderer.RunLoop(() =>
  229. {
  230. Device.Gpu.SetGpuThread();
  231. Device.Gpu.InitializeShaderCache(_gpuCancellationTokenSource.Token);
  232. while (_isActive)
  233. {
  234. if (_isStopped)
  235. {
  236. return;
  237. }
  238. _ticks += _chrono.ElapsedTicks;
  239. _chrono.Restart();
  240. if (Device.WaitFifo())
  241. {
  242. Device.Statistics.RecordFifoStart();
  243. Device.ProcessFrame();
  244. Device.Statistics.RecordFifoEnd();
  245. }
  246. while (Device.ConsumeFrameAvailable())
  247. {
  248. Device.PresentFrame(SwapBuffers);
  249. }
  250. if (_ticks >= _ticksPerFrame)
  251. {
  252. string dockedMode = Device.System.State.DockedMode ? "Docked" : "Handheld";
  253. float scale = GraphicsConfig.ResScale;
  254. if (scale != 1)
  255. {
  256. dockedMode += $" ({scale}x)";
  257. }
  258. StatusUpdatedEvent?.Invoke(this, new StatusUpdatedEventArgs(
  259. Device.VSyncMode.ToString(),
  260. dockedMode,
  261. Device.Configuration.AspectRatio.ToText(),
  262. $"{Device.Statistics.GetGameFrameRate():00.00} FPS ({Device.Statistics.GetGameFrameTime():00.00} ms)",
  263. $"FIFO: {Device.Statistics.GetFifoPercent():0.00} %",
  264. $"GPU: {_gpuDriverName}"));
  265. _ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame);
  266. }
  267. }
  268. // Make sure all commands in the run loop are fully executed before leaving the loop.
  269. if (Device.Gpu.Renderer is ThreadedRenderer threaded)
  270. {
  271. threaded.FlushThreadedCommands();
  272. }
  273. _gpuDoneEvent.Set();
  274. });
  275. FinalizeWindowRenderer();
  276. }
  277. public void Exit()
  278. {
  279. TouchScreenManager?.Dispose();
  280. NpadManager?.Dispose();
  281. if (_isStopped)
  282. {
  283. return;
  284. }
  285. _gpuCancellationTokenSource.Cancel();
  286. _isStopped = true;
  287. _isActive = false;
  288. _exitEvent.WaitOne();
  289. _exitEvent.Dispose();
  290. }
  291. public static void ProcessMainThreadQueue()
  292. {
  293. while (_mainThreadActions.TryDequeue(out Action action))
  294. {
  295. action();
  296. }
  297. }
  298. public void MainLoop()
  299. {
  300. while (_isActive)
  301. {
  302. UpdateFrame();
  303. SDL_PumpEvents();
  304. ProcessMainThreadQueue();
  305. // Polling becomes expensive if it's not slept
  306. Thread.Sleep(1);
  307. }
  308. _exitEvent.Set();
  309. }
  310. private void NvidiaStutterWorkaround()
  311. {
  312. while (_isActive)
  313. {
  314. // When NVIDIA Threaded Optimization is on, the driver will snapshot all threads in the system whenever the application creates any new ones.
  315. // The ThreadPool has something called a "GateThread" which terminates itself after some inactivity.
  316. // However, it immediately starts up again, since the rules regarding when to terminate and when to start differ.
  317. // This creates a new thread every second or so.
  318. // The main problem with this is that the thread snapshot can take 70ms, is on the OpenGL thread and will delay rendering any graphics.
  319. // This is a little over budget on a frame time of 16ms, so creates a large stutter.
  320. // The solution is to keep the ThreadPool active so that it never has a reason to terminate the GateThread.
  321. // TODO: This should be removed when the issue with the GateThread is resolved.
  322. ThreadPool.QueueUserWorkItem(state => { });
  323. Thread.Sleep(300);
  324. }
  325. }
  326. private bool UpdateFrame()
  327. {
  328. if (!_isActive)
  329. {
  330. return true;
  331. }
  332. if (_isStopped)
  333. {
  334. return false;
  335. }
  336. NpadManager.Update();
  337. // Touchscreen
  338. bool hasTouch = false;
  339. // Get screen touch position
  340. if (!_enableMouse)
  341. {
  342. hasTouch = TouchScreenManager.Update(true, (_inputManager.MouseDriver as SDL2MouseDriver).IsButtonPressed(MouseButton.Button1), _aspectRatio.ToFloat());
  343. }
  344. if (!hasTouch)
  345. {
  346. TouchScreenManager.Update(false);
  347. }
  348. Device.Hid.DebugPad.Update();
  349. // TODO: Replace this with MouseDriver.CheckIdle() when mouse motion events are received on every supported platform.
  350. MouseDriver.UpdatePosition();
  351. return true;
  352. }
  353. public void Execute()
  354. {
  355. _chrono.Restart();
  356. _isActive = true;
  357. InitializeWindow();
  358. Thread renderLoopThread = new(Render)
  359. {
  360. Name = "GUI.RenderLoop",
  361. };
  362. renderLoopThread.Start();
  363. Thread nvidiaStutterWorkaround = null;
  364. if (Renderer is OpenGLRenderer)
  365. {
  366. nvidiaStutterWorkaround = new Thread(NvidiaStutterWorkaround)
  367. {
  368. Name = "GUI.NvidiaStutterWorkaround",
  369. };
  370. nvidiaStutterWorkaround.Start();
  371. }
  372. MainLoop();
  373. // NOTE: The render loop is allowed to stay alive until the renderer itself is disposed, as it may handle resource dispose.
  374. // We only need to wait for all commands submitted during the main gpu loop to be processed.
  375. _gpuDoneEvent.WaitOne();
  376. _gpuDoneEvent.Dispose();
  377. nvidiaStutterWorkaround?.Join();
  378. Exit();
  379. }
  380. public bool DisplayInputDialog(SoftwareKeyboardUIArgs args, out string userText)
  381. {
  382. // SDL2 doesn't support input dialogs
  383. userText = "Ryujinx";
  384. return true;
  385. }
  386. public bool DisplayMessageDialog(string title, string message)
  387. {
  388. SDL_ShowSimpleMessageBox(SDL_MessageBoxFlags.SDL_MESSAGEBOX_INFORMATION, title, message, WindowHandle);
  389. return true;
  390. }
  391. public bool DisplayCabinetDialog(out string userText)
  392. {
  393. // SDL2 doesn't support input dialogs
  394. userText = "Ryujinx";
  395. return true;
  396. }
  397. public void DisplayCabinetMessageDialog()
  398. {
  399. SDL_ShowSimpleMessageBox(SDL_MessageBoxFlags.SDL_MESSAGEBOX_INFORMATION, "Cabinet Dialog", "Please scan your Amiibo now.", WindowHandle);
  400. }
  401. public bool DisplayMessageDialog(ControllerAppletUIArgs args)
  402. {
  403. if (_ignoreControllerApplet) return false;
  404. string playerCount = args.PlayerCountMin == args.PlayerCountMax ? $"exactly {args.PlayerCountMin}" : $"{args.PlayerCountMin}-{args.PlayerCountMax}";
  405. string message = $"Application requests {playerCount} {"player".ToQuantity(args.PlayerCountMin + args.PlayerCountMax, ShowQuantityAs.None)} with:\n\n"
  406. + $"TYPES: {args.SupportedStyles}\n\n"
  407. + $"PLAYERS: {string.Join(", ", args.SupportedPlayers)}\n\n"
  408. + (args.IsDocked ? "Docked mode set. Handheld is also invalid.\n\n" : string.Empty)
  409. + "Please reconfigure Input now and then press OK.";
  410. return DisplayMessageDialog("Controller Applet", message);
  411. }
  412. public IDynamicTextInputHandler CreateDynamicTextInputHandler()
  413. {
  414. return new HeadlessDynamicTextInputHandler();
  415. }
  416. public void ExecuteProgram(Switch device, ProgramSpecifyKind kind, ulong value)
  417. {
  418. device.Configuration.UserChannelPersistence.ExecuteProgram(kind, value);
  419. Exit();
  420. }
  421. public bool DisplayErrorAppletDialog(string title, string message, string[] buttonsText)
  422. {
  423. SDL_MessageBoxData data = new()
  424. {
  425. title = title,
  426. message = message,
  427. buttons = new SDL_MessageBoxButtonData[buttonsText.Length],
  428. numbuttons = buttonsText.Length,
  429. window = WindowHandle,
  430. };
  431. for (int i = 0; i < buttonsText.Length; i++)
  432. {
  433. data.buttons[i] = new SDL_MessageBoxButtonData
  434. {
  435. buttonid = i,
  436. text = buttonsText[i],
  437. };
  438. }
  439. SDL_ShowMessageBox(ref data, out int _);
  440. return true;
  441. }
  442. public void Dispose()
  443. {
  444. Dispose(true);
  445. }
  446. protected virtual void Dispose(bool disposing)
  447. {
  448. if (disposing)
  449. {
  450. _isActive = false;
  451. TouchScreenManager?.Dispose();
  452. NpadManager.Dispose();
  453. SDL2Driver.Instance.UnregisterWindow(_windowId);
  454. SDL_DestroyWindow(WindowHandle);
  455. SDL2Driver.Instance.Dispose();
  456. }
  457. }
  458. }
  459. }