Switch.cs 6.1 KB

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