NvFlinger.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.Graphics.Memory;
  3. using Ryujinx.HLE.HOS.Kernel;
  4. using Ryujinx.HLE.HOS.Services.Nv.NvGpuAS;
  5. using Ryujinx.HLE.HOS.Services.Nv.NvMap;
  6. using Ryujinx.HLE.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using System.Threading;
  12. using static Ryujinx.HLE.HOS.Services.Android.Parcel;
  13. namespace Ryujinx.HLE.HOS.Services.Android
  14. {
  15. class NvFlinger : IDisposable
  16. {
  17. private delegate long ServiceProcessParcel(ServiceCtx Context, BinaryReader ParcelReader);
  18. private Dictionary<(string, int), ServiceProcessParcel> Commands;
  19. private KEvent BinderEvent;
  20. private IGalRenderer Renderer;
  21. private const int BufferQueueCount = 0x40;
  22. private const int BufferQueueMask = BufferQueueCount - 1;
  23. [Flags]
  24. private enum HalTransform
  25. {
  26. FlipX = 1 << 0,
  27. FlipY = 1 << 1,
  28. Rotate90 = 1 << 2
  29. }
  30. private enum BufferState
  31. {
  32. Free,
  33. Dequeued,
  34. Queued,
  35. Acquired
  36. }
  37. private struct Rect
  38. {
  39. public int Top;
  40. public int Left;
  41. public int Right;
  42. public int Bottom;
  43. }
  44. private struct BufferEntry
  45. {
  46. public BufferState State;
  47. public HalTransform Transform;
  48. public Rect Crop;
  49. public GbpBuffer Data;
  50. }
  51. private BufferEntry[] BufferQueue;
  52. private ManualResetEvent WaitBufferFree;
  53. private bool Disposed;
  54. public NvFlinger(IGalRenderer Renderer, KEvent BinderEvent)
  55. {
  56. Commands = new Dictionary<(string, int), ServiceProcessParcel>()
  57. {
  58. { ("android.gui.IGraphicBufferProducer", 0x1), GbpRequestBuffer },
  59. { ("android.gui.IGraphicBufferProducer", 0x3), GbpDequeueBuffer },
  60. { ("android.gui.IGraphicBufferProducer", 0x4), GbpDetachBuffer },
  61. { ("android.gui.IGraphicBufferProducer", 0x7), GbpQueueBuffer },
  62. { ("android.gui.IGraphicBufferProducer", 0x8), GbpCancelBuffer },
  63. { ("android.gui.IGraphicBufferProducer", 0x9), GbpQuery },
  64. { ("android.gui.IGraphicBufferProducer", 0xa), GbpConnect },
  65. { ("android.gui.IGraphicBufferProducer", 0xb), GbpDisconnect },
  66. { ("android.gui.IGraphicBufferProducer", 0xe), GbpPreallocBuffer }
  67. };
  68. this.Renderer = Renderer;
  69. this.BinderEvent = BinderEvent;
  70. BufferQueue = new BufferEntry[0x40];
  71. WaitBufferFree = new ManualResetEvent(false);
  72. }
  73. public long ProcessParcelRequest(ServiceCtx Context, byte[] ParcelData, int Code)
  74. {
  75. using (MemoryStream MS = new MemoryStream(ParcelData))
  76. {
  77. BinaryReader Reader = new BinaryReader(MS);
  78. MS.Seek(4, SeekOrigin.Current);
  79. int StrSize = Reader.ReadInt32();
  80. string InterfaceName = Encoding.Unicode.GetString(Reader.ReadBytes(StrSize * 2));
  81. long Remainder = MS.Position & 0xf;
  82. if (Remainder != 0)
  83. {
  84. MS.Seek(0x10 - Remainder, SeekOrigin.Current);
  85. }
  86. MS.Seek(0x50, SeekOrigin.Begin);
  87. if (Commands.TryGetValue((InterfaceName, Code), out ServiceProcessParcel ProcReq))
  88. {
  89. Context.Device.Log.PrintDebug(LogClass.ServiceVi, $"{InterfaceName} {ProcReq.Method.Name}");
  90. return ProcReq(Context, Reader);
  91. }
  92. else
  93. {
  94. throw new NotImplementedException($"{InterfaceName} {Code}");
  95. }
  96. }
  97. }
  98. private long GbpRequestBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  99. {
  100. int Slot = ParcelReader.ReadInt32();
  101. using (MemoryStream MS = new MemoryStream())
  102. {
  103. BinaryWriter Writer = new BinaryWriter(MS);
  104. BufferEntry Entry = BufferQueue[Slot];
  105. int BufferCount = 1; //?
  106. long BufferSize = Entry.Data.Size;
  107. Writer.Write(BufferCount);
  108. Writer.Write(BufferSize);
  109. Entry.Data.Write(Writer);
  110. Writer.Write(0);
  111. return MakeReplyParcel(Context, MS.ToArray());
  112. }
  113. }
  114. private long GbpDequeueBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  115. {
  116. //TODO: Errors.
  117. int Format = ParcelReader.ReadInt32();
  118. int Width = ParcelReader.ReadInt32();
  119. int Height = ParcelReader.ReadInt32();
  120. int GetTimestamps = ParcelReader.ReadInt32();
  121. int Usage = ParcelReader.ReadInt32();
  122. int Slot = GetFreeSlotBlocking(Width, Height);
  123. return MakeReplyParcel(Context, Slot, 1, 0x24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  124. }
  125. private long GbpQueueBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  126. {
  127. Context.Device.Statistics.RecordGameFrameTime();
  128. //TODO: Errors.
  129. int Slot = ParcelReader.ReadInt32();
  130. int Unknown4 = ParcelReader.ReadInt32();
  131. int Unknown8 = ParcelReader.ReadInt32();
  132. int Unknownc = ParcelReader.ReadInt32();
  133. int Timestamp = ParcelReader.ReadInt32();
  134. int IsAutoTimestamp = ParcelReader.ReadInt32();
  135. int CropTop = ParcelReader.ReadInt32();
  136. int CropLeft = ParcelReader.ReadInt32();
  137. int CropRight = ParcelReader.ReadInt32();
  138. int CropBottom = ParcelReader.ReadInt32();
  139. int ScalingMode = ParcelReader.ReadInt32();
  140. int Transform = ParcelReader.ReadInt32();
  141. int StickyTransform = ParcelReader.ReadInt32();
  142. int Unknown34 = ParcelReader.ReadInt32();
  143. int Unknown38 = ParcelReader.ReadInt32();
  144. int IsFenceValid = ParcelReader.ReadInt32();
  145. int Fence0Id = ParcelReader.ReadInt32();
  146. int Fence0Value = ParcelReader.ReadInt32();
  147. int Fence1Id = ParcelReader.ReadInt32();
  148. int Fence1Value = ParcelReader.ReadInt32();
  149. BufferQueue[Slot].Transform = (HalTransform)Transform;
  150. BufferQueue[Slot].Crop.Top = CropTop;
  151. BufferQueue[Slot].Crop.Left = CropLeft;
  152. BufferQueue[Slot].Crop.Right = CropRight;
  153. BufferQueue[Slot].Crop.Bottom = CropBottom;
  154. BufferQueue[Slot].State = BufferState.Queued;
  155. SendFrameBuffer(Context, Slot);
  156. if (Context.Device.EnableDeviceVsync)
  157. {
  158. Context.Device.VsyncEvent.WaitOne();
  159. }
  160. return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
  161. }
  162. private long GbpDetachBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  163. {
  164. return MakeReplyParcel(Context, 0);
  165. }
  166. private long GbpCancelBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  167. {
  168. //TODO: Errors.
  169. int Slot = ParcelReader.ReadInt32();
  170. BufferQueue[Slot].State = BufferState.Free;
  171. return MakeReplyParcel(Context, 0);
  172. }
  173. private long GbpQuery(ServiceCtx Context, BinaryReader ParcelReader)
  174. {
  175. return MakeReplyParcel(Context, 0, 0);
  176. }
  177. private long GbpConnect(ServiceCtx Context, BinaryReader ParcelReader)
  178. {
  179. return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
  180. }
  181. private long GbpDisconnect(ServiceCtx Context, BinaryReader ParcelReader)
  182. {
  183. return MakeReplyParcel(Context, 0);
  184. }
  185. private long GbpPreallocBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  186. {
  187. int Slot = ParcelReader.ReadInt32();
  188. int BufferCount = ParcelReader.ReadInt32();
  189. if (BufferCount > 0)
  190. {
  191. long BufferSize = ParcelReader.ReadInt64();
  192. BufferQueue[Slot].State = BufferState.Free;
  193. BufferQueue[Slot].Data = new GbpBuffer(ParcelReader);
  194. }
  195. return MakeReplyParcel(Context, 0);
  196. }
  197. private long MakeReplyParcel(ServiceCtx Context, params int[] Ints)
  198. {
  199. using (MemoryStream MS = new MemoryStream())
  200. {
  201. BinaryWriter Writer = new BinaryWriter(MS);
  202. foreach (int Int in Ints)
  203. {
  204. Writer.Write(Int);
  205. }
  206. return MakeReplyParcel(Context, MS.ToArray());
  207. }
  208. }
  209. private long MakeReplyParcel(ServiceCtx Context, byte[] Data)
  210. {
  211. long ReplyPos = Context.Request.ReceiveBuff[0].Position;
  212. long ReplySize = Context.Request.ReceiveBuff[0].Size;
  213. byte[] Reply = MakeParcel(Data, new byte[0]);
  214. Context.Memory.WriteBytes(ReplyPos, Reply);
  215. return 0;
  216. }
  217. private void SendFrameBuffer(ServiceCtx Context, int Slot)
  218. {
  219. int FbWidth = BufferQueue[Slot].Data.Width;
  220. int FbHeight = BufferQueue[Slot].Data.Height;
  221. int NvMapHandle = BitConverter.ToInt32(BufferQueue[Slot].Data.RawData, 0x4c);
  222. int BufferOffset = BitConverter.ToInt32(BufferQueue[Slot].Data.RawData, 0x50);
  223. NvMapHandle Map = NvMapIoctl.GetNvMap(Context, NvMapHandle);;
  224. long FbAddr = Map.Address + BufferOffset;
  225. BufferQueue[Slot].State = BufferState.Acquired;
  226. Rect Crop = BufferQueue[Slot].Crop;
  227. bool FlipX = BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipX);
  228. bool FlipY = BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipY);
  229. //Note: Rotation is being ignored.
  230. int Top = Crop.Top;
  231. int Left = Crop.Left;
  232. int Right = Crop.Right;
  233. int Bottom = Crop.Bottom;
  234. NvGpuVmm Vmm = NvGpuASIoctl.GetASCtx(Context).Vmm;
  235. Renderer.QueueAction(() =>
  236. {
  237. if (!Renderer.Texture.TryGetImage(FbAddr, out GalImage Image))
  238. {
  239. Image = new GalImage(
  240. FbWidth,
  241. FbHeight, 1, 16,
  242. GalMemoryLayout.BlockLinear,
  243. GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm);
  244. }
  245. Context.Device.Gpu.ResourceManager.ClearPbCache();
  246. Context.Device.Gpu.ResourceManager.SendTexture(Vmm, FbAddr, Image);
  247. Renderer.RenderTarget.SetTransform(FlipX, FlipY, Top, Left, Right, Bottom);
  248. Renderer.RenderTarget.Set(FbAddr);
  249. ReleaseBuffer(Slot);
  250. });
  251. }
  252. private void ReleaseBuffer(int Slot)
  253. {
  254. BufferQueue[Slot].State = BufferState.Free;
  255. BinderEvent.WaitEvent.Set();
  256. lock (WaitBufferFree)
  257. {
  258. WaitBufferFree.Set();
  259. }
  260. }
  261. private int GetFreeSlotBlocking(int Width, int Height)
  262. {
  263. int Slot;
  264. do
  265. {
  266. lock (WaitBufferFree)
  267. {
  268. if ((Slot = GetFreeSlot(Width, Height)) != -1)
  269. {
  270. break;
  271. }
  272. if (Disposed)
  273. {
  274. break;
  275. }
  276. WaitBufferFree.Reset();
  277. }
  278. WaitBufferFree.WaitOne();
  279. }
  280. while (!Disposed);
  281. return Slot;
  282. }
  283. private int GetFreeSlot(int Width, int Height)
  284. {
  285. lock (BufferQueue)
  286. {
  287. for (int Slot = 0; Slot < BufferQueue.Length; Slot++)
  288. {
  289. if (BufferQueue[Slot].State != BufferState.Free)
  290. {
  291. continue;
  292. }
  293. GbpBuffer Data = BufferQueue[Slot].Data;
  294. if (Data.Width == Width &&
  295. Data.Height == Height)
  296. {
  297. BufferQueue[Slot].State = BufferState.Dequeued;
  298. return Slot;
  299. }
  300. }
  301. }
  302. return -1;
  303. }
  304. public void Dispose()
  305. {
  306. Dispose(true);
  307. }
  308. protected virtual void Dispose(bool Disposing)
  309. {
  310. if (Disposing && !Disposed)
  311. {
  312. Disposed = true;
  313. lock (WaitBufferFree)
  314. {
  315. WaitBufferFree.Set();
  316. }
  317. WaitBufferFree.Dispose();
  318. }
  319. }
  320. }
  321. }