NvFlinger.cs 14 KB

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