Switch.cs 5.2 KB

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