Switch.cs 7.2 KB

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