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.Services.Nv.NvDrvServices;
  16. using Ryujinx.HLE.HOS.SystemState;
  17. using Ryujinx.Memory;
  18. using System;
  19. namespace Ryujinx.HLE
  20. {
  21. public class Switch : IDisposable
  22. {
  23. public IAalOutput AudioOut { get; private set; }
  24. internal MemoryBlock Memory { get; private set; }
  25. public GpuContext Gpu { get; private set; }
  26. internal NvMemoryAllocator MemoryAllocator { get; private set; }
  27. internal Host1xDevice Host1x { get; }
  28. public VirtualFileSystem FileSystem { get; private set; }
  29. public Horizon System { get; private set; }
  30. public ApplicationLoader Application { get; }
  31. public PerformanceStatistics Statistics { get; private set; }
  32. public UserChannelPersistence UserChannelPersistence { get; }
  33. public Hid Hid { get; private set; }
  34. public IHostUiHandler UiHandler { get; set; }
  35. public bool EnableDeviceVsync { get; set; } = true;
  36. public Switch(VirtualFileSystem fileSystem, ContentManager contentManager, UserChannelPersistence userChannelPersistence, IRenderer renderer, IAalOutput audioOut)
  37. {
  38. if (renderer == null)
  39. {
  40. throw new ArgumentNullException(nameof(renderer));
  41. }
  42. if (audioOut == null)
  43. {
  44. throw new ArgumentNullException(nameof(audioOut));
  45. }
  46. if (userChannelPersistence == null)
  47. {
  48. throw new ArgumentNullException(nameof(userChannelPersistence));
  49. }
  50. UserChannelPersistence = userChannelPersistence;
  51. AudioOut = audioOut;
  52. Memory = new MemoryBlock(1UL << 32);
  53. Gpu = new GpuContext(renderer);
  54. MemoryAllocator = new NvMemoryAllocator();
  55. Host1x = new Host1xDevice(Gpu.Synchronization);
  56. var nvdec = new NvdecDevice(Gpu.MemoryManager);
  57. var vic = new VicDevice(Gpu.MemoryManager);
  58. Host1x.RegisterDevice(ClassId.Nvdec, nvdec);
  59. Host1x.RegisterDevice(ClassId.Vic, vic);
  60. nvdec.FrameDecoded += (FrameDecodedEventArgs e) =>
  61. {
  62. // FIXME:
  63. // Figure out what is causing frame ordering issues on H264.
  64. // For now this is needed as workaround.
  65. if (e.CodecId == CodecId.H264)
  66. {
  67. vic.SetSurfaceOverride(e.LumaOffset, e.ChromaOffset, 0);
  68. }
  69. else
  70. {
  71. vic.DisableSurfaceOverride();
  72. }
  73. };
  74. FileSystem = fileSystem;
  75. System = new Horizon(this, contentManager);
  76. System.InitializeServices();
  77. Statistics = new PerformanceStatistics();
  78. Hid = new Hid(this, System.HidBaseAddress);
  79. Hid.InitDevices();
  80. Application = new ApplicationLoader(this, fileSystem, contentManager);
  81. }
  82. public void Initialize()
  83. {
  84. System.State.SetLanguage((SystemLanguage)ConfigurationState.Instance.System.Language.Value);
  85. System.State.SetRegion((RegionCode)ConfigurationState.Instance.System.Region.Value);
  86. EnableDeviceVsync = ConfigurationState.Instance.Graphics.EnableVsync;
  87. System.State.DockedMode = ConfigurationState.Instance.System.EnableDockedMode;
  88. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  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. }