Switch.cs 6.5 KB

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