Switch.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. System.InitializeServices();
  74. Statistics = new PerformanceStatistics();
  75. Hid = new Hid(this, System.HidBaseAddress);
  76. Hid.InitDevices();
  77. Application = new ApplicationLoader(this, fileSystem, contentManager);
  78. }
  79. public void Initialize()
  80. {
  81. System.State.SetLanguage((SystemLanguage)ConfigurationState.Instance.System.Language.Value);
  82. System.State.SetRegion((RegionCode)ConfigurationState.Instance.System.Region.Value);
  83. EnableDeviceVsync = ConfigurationState.Instance.Graphics.EnableVsync;
  84. System.State.DockedMode = ConfigurationState.Instance.System.EnableDockedMode;
  85. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  86. if (ConfigurationState.Instance.System.EnableMulticoreScheduling)
  87. {
  88. System.EnableMultiCoreScheduling();
  89. }
  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. // Configure controllers
  95. Hid.RefreshInputConfig(ConfigurationState.Instance.Hid.InputConfig.Value);
  96. ConfigurationState.Instance.Hid.InputConfig.Event += Hid.RefreshInputConfigEvent;
  97. }
  98. public static IntegrityCheckLevel GetIntegrityCheckLevel()
  99. {
  100. return ConfigurationState.Instance.System.EnableFsIntegrityChecks
  101. ? IntegrityCheckLevel.ErrorOnInvalid
  102. : IntegrityCheckLevel.None;
  103. }
  104. public void LoadCart(string exeFsDir, string romFsFile = null)
  105. {
  106. Application.LoadCart(exeFsDir, romFsFile);
  107. }
  108. public void LoadXci(string xciFile)
  109. {
  110. Application.LoadXci(xciFile);
  111. }
  112. public void LoadNca(string ncaFile)
  113. {
  114. Application.LoadNca(ncaFile);
  115. }
  116. public void LoadNsp(string nspFile)
  117. {
  118. Application.LoadNsp(nspFile);
  119. }
  120. public void LoadProgram(string fileName)
  121. {
  122. Application.LoadProgram(fileName);
  123. }
  124. public bool WaitFifo()
  125. {
  126. return Gpu.GPFifo.WaitForCommands();
  127. }
  128. public void ProcessFrame()
  129. {
  130. Gpu.Renderer.PreFrame();
  131. Gpu.GPFifo.DispatchCalls();
  132. }
  133. public void PresentFrame(Action swapBuffersCallback)
  134. {
  135. Gpu.Window.Present(swapBuffersCallback);
  136. }
  137. public void DisposeGpu()
  138. {
  139. Gpu.Dispose();
  140. }
  141. public void Dispose()
  142. {
  143. Dispose(true);
  144. }
  145. protected virtual void Dispose(bool disposing)
  146. {
  147. if (disposing)
  148. {
  149. ConfigurationState.Instance.Hid.InputConfig.Event -= Hid.RefreshInputConfigEvent;
  150. System.Dispose();
  151. Host1x.Dispose();
  152. AudioOut.Dispose();
  153. FileSystem.Unload();
  154. Memory.Dispose();
  155. }
  156. }
  157. }
  158. }