Switch.cs 6.7 KB

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