Switch.cs 7.7 KB

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