Switch.cs 7.6 KB

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