Switch.cs 5.6 KB

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