Switch.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using LibHac.FsSystem;
  2. using Ryujinx.Audio.Backends.CompatLayer;
  3. using Ryujinx.Audio.Integration;
  4. using Ryujinx.Common;
  5. using Ryujinx.Common.Logging;
  6. using Ryujinx.Configuration;
  7. using Ryujinx.Graphics.GAL;
  8. using Ryujinx.Graphics.Gpu;
  9. using Ryujinx.Graphics.Host1x;
  10. using Ryujinx.Graphics.Nvdec;
  11. using Ryujinx.Graphics.Vic;
  12. using Ryujinx.HLE.FileSystem;
  13. using Ryujinx.HLE.FileSystem.Content;
  14. using Ryujinx.HLE.HOS;
  15. using Ryujinx.HLE.HOS.Services;
  16. using Ryujinx.HLE.HOS.Services.Apm;
  17. using Ryujinx.HLE.HOS.Services.Hid;
  18. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices;
  19. using Ryujinx.HLE.HOS.SystemState;
  20. using Ryujinx.Memory;
  21. using System;
  22. namespace Ryujinx.HLE
  23. {
  24. public class Switch : IDisposable
  25. {
  26. public IHardwareDeviceDriver AudioDeviceDriver { get; private set; }
  27. internal MemoryBlock Memory { get; private set; }
  28. public GpuContext Gpu { get; private set; }
  29. internal NvMemoryAllocator MemoryAllocator { get; private set; }
  30. internal Host1xDevice Host1x { get; }
  31. public VirtualFileSystem FileSystem { get; private set; }
  32. public Horizon System { get; private set; }
  33. public ApplicationLoader Application { get; }
  34. public PerformanceStatistics Statistics { get; private set; }
  35. public UserChannelPersistence UserChannelPersistence { get; }
  36. public Hid Hid { get; private set; }
  37. public IHostUiHandler UiHandler { get; set; }
  38. public bool EnableDeviceVsync { get; set; } = true;
  39. public Switch(VirtualFileSystem fileSystem, ContentManager contentManager, UserChannelPersistence userChannelPersistence, IRenderer renderer, IHardwareDeviceDriver audioDeviceDriver)
  40. {
  41. if (renderer == null)
  42. {
  43. throw new ArgumentNullException(nameof(renderer));
  44. }
  45. if (audioDeviceDriver == null)
  46. {
  47. throw new ArgumentNullException(nameof(audioDeviceDriver));
  48. }
  49. if (userChannelPersistence == null)
  50. {
  51. throw new ArgumentNullException(nameof(userChannelPersistence));
  52. }
  53. UserChannelPersistence = userChannelPersistence;
  54. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(audioDeviceDriver);
  55. Memory = new MemoryBlock(1UL << 32);
  56. Gpu = new GpuContext(renderer);
  57. MemoryAllocator = new NvMemoryAllocator();
  58. Host1x = new Host1xDevice(Gpu.Synchronization);
  59. var nvdec = new NvdecDevice(Gpu.MemoryManager);
  60. var vic = new VicDevice(Gpu.MemoryManager);
  61. Host1x.RegisterDevice(ClassId.Nvdec, nvdec);
  62. Host1x.RegisterDevice(ClassId.Vic, vic);
  63. nvdec.FrameDecoded += (FrameDecodedEventArgs e) =>
  64. {
  65. // FIXME:
  66. // Figure out what is causing frame ordering issues on H264.
  67. // For now this is needed as workaround.
  68. if (e.CodecId == CodecId.H264)
  69. {
  70. vic.SetSurfaceOverride(e.LumaOffset, e.ChromaOffset, 0);
  71. }
  72. else
  73. {
  74. vic.DisableSurfaceOverride();
  75. }
  76. };
  77. FileSystem = fileSystem;
  78. System = new Horizon(this, contentManager);
  79. System.InitializeServices();
  80. Statistics = new PerformanceStatistics();
  81. Hid = new Hid(this, System.HidBaseAddress);
  82. Hid.InitDevices();
  83. Application = new ApplicationLoader(this, fileSystem, contentManager);
  84. }
  85. public void Initialize()
  86. {
  87. System.State.SetLanguage((SystemLanguage)ConfigurationState.Instance.System.Language.Value);
  88. System.State.SetRegion((RegionCode)ConfigurationState.Instance.System.Region.Value);
  89. EnableDeviceVsync = ConfigurationState.Instance.Graphics.EnableVsync;
  90. System.State.DockedMode = ConfigurationState.Instance.System.EnableDockedMode;
  91. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  92. System.EnablePtc = ConfigurationState.Instance.System.EnablePtc;
  93. System.FsIntegrityCheckLevel = GetIntegrityCheckLevel();
  94. System.GlobalAccessLogMode = ConfigurationState.Instance.System.FsGlobalAccessLogMode;
  95. ServiceConfiguration.IgnoreMissingServices = ConfigurationState.Instance.System.IgnoreMissingServices;
  96. ConfigurationState.Instance.System.IgnoreMissingServices.Event += (object _, ReactiveEventArgs<bool> args) =>
  97. {
  98. ServiceConfiguration.IgnoreMissingServices = args.NewValue;
  99. };
  100. // Configure controllers
  101. Hid.RefreshInputConfig(ConfigurationState.Instance.Hid.InputConfig.Value);
  102. ConfigurationState.Instance.Hid.InputConfig.Event += Hid.RefreshInputConfigEvent;
  103. Logger.Info?.Print(LogClass.Application, $"AudioBackend: {ConfigurationState.Instance.System.AudioBackend.Value}");
  104. Logger.Info?.Print(LogClass.Application, $"IsDocked: {ConfigurationState.Instance.System.EnableDockedMode.Value}");
  105. Logger.Info?.Print(LogClass.Application, $"Vsync: {ConfigurationState.Instance.Graphics.EnableVsync.Value}");
  106. }
  107. public static IntegrityCheckLevel GetIntegrityCheckLevel()
  108. {
  109. return ConfigurationState.Instance.System.EnableFsIntegrityChecks
  110. ? IntegrityCheckLevel.ErrorOnInvalid
  111. : IntegrityCheckLevel.None;
  112. }
  113. public void LoadCart(string exeFsDir, string romFsFile = null)
  114. {
  115. Application.LoadCart(exeFsDir, romFsFile);
  116. }
  117. public void LoadXci(string xciFile)
  118. {
  119. Application.LoadXci(xciFile);
  120. }
  121. public void LoadNca(string ncaFile)
  122. {
  123. Application.LoadNca(ncaFile);
  124. }
  125. public void LoadNsp(string nspFile)
  126. {
  127. Application.LoadNsp(nspFile);
  128. }
  129. public void LoadProgram(string fileName)
  130. {
  131. Application.LoadProgram(fileName);
  132. }
  133. public bool WaitFifo()
  134. {
  135. return Gpu.GPFifo.WaitForCommands();
  136. }
  137. public void ProcessFrame()
  138. {
  139. Gpu.Renderer.PreFrame();
  140. Gpu.GPFifo.DispatchCalls();
  141. }
  142. public bool ConsumeFrameAvailable()
  143. {
  144. return Gpu.Window.ConsumeFrameAvailable();
  145. }
  146. public void PresentFrame(Action swapBuffersCallback)
  147. {
  148. Gpu.Window.Present(swapBuffersCallback);
  149. }
  150. public void DisposeGpu()
  151. {
  152. Gpu.Dispose();
  153. }
  154. public void Dispose()
  155. {
  156. Dispose(true);
  157. }
  158. protected virtual void Dispose(bool disposing)
  159. {
  160. if (disposing)
  161. {
  162. ConfigurationState.Instance.Hid.InputConfig.Event -= Hid.RefreshInputConfigEvent;
  163. System.Dispose();
  164. Host1x.Dispose();
  165. AudioDeviceDriver.Dispose();
  166. FileSystem.Unload();
  167. Memory.Dispose();
  168. }
  169. }
  170. }
  171. }