NvFlinger.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.OsHle.Handles;
  3. using System;
  4. using System.IO;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Threading;
  8. using static Ryujinx.Core.OsHle.Objects.Android.Parcel;
  9. namespace Ryujinx.Core.OsHle.Objects.Android
  10. {
  11. class NvFlinger : IDisposable
  12. {
  13. private delegate long ServiceProcessParcel(ServiceCtx Context, BinaryReader ParcelReader);
  14. private Dictionary<(string, int), ServiceProcessParcel> Commands;
  15. private const int BufferQueueCount = 0x40;
  16. private const int BufferQueueMask = BufferQueueCount - 1;
  17. [Flags]
  18. private enum HalTransform
  19. {
  20. FlipX = 1 << 0,
  21. FlipY = 1 << 1,
  22. Rotate90 = 1 << 2
  23. }
  24. private enum BufferState
  25. {
  26. Free,
  27. Dequeued,
  28. Queued,
  29. Acquired
  30. }
  31. private struct BufferEntry
  32. {
  33. public BufferState State;
  34. public HalTransform Transform;
  35. public GbpBuffer Data;
  36. }
  37. private BufferEntry[] BufferQueue;
  38. private ManualResetEvent WaitBufferFree;
  39. private bool KeepRunning;
  40. public NvFlinger()
  41. {
  42. Commands = new Dictionary<(string, int), ServiceProcessParcel>()
  43. {
  44. { ("android.gui.IGraphicBufferProducer", 0x1), GbpRequestBuffer },
  45. { ("android.gui.IGraphicBufferProducer", 0x3), GbpDequeueBuffer },
  46. { ("android.gui.IGraphicBufferProducer", 0x7), GbpQueueBuffer },
  47. { ("android.gui.IGraphicBufferProducer", 0x8), GbpCancelBuffer },
  48. { ("android.gui.IGraphicBufferProducer", 0x9), GbpQuery },
  49. { ("android.gui.IGraphicBufferProducer", 0xa), GbpConnect },
  50. { ("android.gui.IGraphicBufferProducer", 0xe), GbpPreallocBuffer }
  51. };
  52. BufferQueue = new BufferEntry[0x40];
  53. WaitBufferFree = new ManualResetEvent(false);
  54. KeepRunning = true;
  55. }
  56. public long ProcessParcelRequest(ServiceCtx Context, byte[] ParcelData, int Code)
  57. {
  58. using (MemoryStream MS = new MemoryStream(ParcelData))
  59. {
  60. BinaryReader Reader = new BinaryReader(MS);
  61. MS.Seek(4, SeekOrigin.Current);
  62. int StrSize = Reader.ReadInt32();
  63. string InterfaceName = Encoding.Unicode.GetString(Reader.ReadBytes(StrSize * 2));
  64. long Remainder = MS.Position & 0xf;
  65. if (Remainder != 0)
  66. {
  67. MS.Seek(0x10 - Remainder, SeekOrigin.Current);
  68. }
  69. MS.Seek(0x50, SeekOrigin.Begin);
  70. if (Commands.TryGetValue((InterfaceName, Code), out ServiceProcessParcel ProcReq))
  71. {
  72. Logging.Debug($"{InterfaceName} {ProcReq.Method.Name}");
  73. return ProcReq(Context, Reader);
  74. }
  75. else
  76. {
  77. throw new NotImplementedException($"{InterfaceName} {Code}");
  78. }
  79. }
  80. }
  81. private long GbpRequestBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  82. {
  83. int Slot = ParcelReader.ReadInt32();
  84. using (MemoryStream MS = new MemoryStream())
  85. {
  86. BinaryWriter Writer = new BinaryWriter(MS);
  87. BufferEntry Entry = BufferQueue[Slot];
  88. int BufferCount = 1; //?
  89. long BufferSize = Entry.Data.Size;
  90. Writer.Write(BufferCount);
  91. Writer.Write(BufferSize);
  92. Entry.Data.Write(Writer);
  93. Writer.Write(0);
  94. return MakeReplyParcel(Context, MS.ToArray());
  95. }
  96. }
  97. private long GbpDequeueBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  98. {
  99. //TODO: Errors.
  100. int Format = ParcelReader.ReadInt32();
  101. int Width = ParcelReader.ReadInt32();
  102. int Height = ParcelReader.ReadInt32();
  103. int GetTimestamps = ParcelReader.ReadInt32();
  104. int Usage = ParcelReader.ReadInt32();
  105. int Slot = GetFreeSlotBlocking(Width, Height);
  106. return MakeReplyParcel(Context, Slot, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  107. }
  108. private long GbpQueueBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  109. {
  110. //TODO: Errors.
  111. int Slot = ParcelReader.ReadInt32();
  112. int Unknown4 = ParcelReader.ReadInt32();
  113. int Unknown8 = ParcelReader.ReadInt32();
  114. int Unknownc = ParcelReader.ReadInt32();
  115. int Timestamp = ParcelReader.ReadInt32();
  116. int IsAutoTimestamp = ParcelReader.ReadInt32();
  117. int CropTop = ParcelReader.ReadInt32();
  118. int CropLeft = ParcelReader.ReadInt32();
  119. int CropRight = ParcelReader.ReadInt32();
  120. int CropBottom = ParcelReader.ReadInt32();
  121. int ScalingMode = ParcelReader.ReadInt32();
  122. int Transform = ParcelReader.ReadInt32();
  123. int StickyTransform = ParcelReader.ReadInt32();
  124. int Unknown34 = ParcelReader.ReadInt32();
  125. int Unknown38 = ParcelReader.ReadInt32();
  126. int IsFenceValid = ParcelReader.ReadInt32();
  127. int Fence0Id = ParcelReader.ReadInt32();
  128. int Fence0Value = ParcelReader.ReadInt32();
  129. int Fence1Id = ParcelReader.ReadInt32();
  130. int Fence1Value = ParcelReader.ReadInt32();
  131. BufferQueue[Slot].Transform = (HalTransform)Transform;
  132. BufferQueue[Slot].State = BufferState.Queued;
  133. SendFrameBuffer(Context, Slot);
  134. return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
  135. }
  136. private long GbpCancelBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  137. {
  138. //TODO: Errors.
  139. int Slot = ParcelReader.ReadInt32();
  140. BufferQueue[Slot].State = BufferState.Free;
  141. return MakeReplyParcel(Context, 0);
  142. }
  143. private long GbpQuery(ServiceCtx Context, BinaryReader ParcelReader)
  144. {
  145. return MakeReplyParcel(Context, 0, 0);
  146. }
  147. private long GbpConnect(ServiceCtx Context, BinaryReader ParcelReader)
  148. {
  149. return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
  150. }
  151. private long GbpPreallocBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  152. {
  153. int Slot = ParcelReader.ReadInt32();
  154. int BufferCount = ParcelReader.ReadInt32();
  155. long BufferSize = ParcelReader.ReadInt64();
  156. BufferQueue[Slot].State = BufferState.Free;
  157. BufferQueue[Slot].Data = new GbpBuffer(ParcelReader);
  158. return MakeReplyParcel(Context, 0);
  159. }
  160. private long MakeReplyParcel(ServiceCtx Context, params int[] Ints)
  161. {
  162. using (MemoryStream MS = new MemoryStream())
  163. {
  164. BinaryWriter Writer = new BinaryWriter(MS);
  165. foreach (int Int in Ints)
  166. {
  167. Writer.Write(Int);
  168. }
  169. return MakeReplyParcel(Context, MS.ToArray());
  170. }
  171. }
  172. private long MakeReplyParcel(ServiceCtx Context, byte[] Data)
  173. {
  174. long ReplyPos = Context.Request.ReceiveBuff[0].Position;
  175. long ReplySize = Context.Request.ReceiveBuff[0].Size;
  176. byte[] Reply = MakeParcel(Data, new byte[0]);
  177. AMemoryHelper.WriteBytes(Context.Memory, ReplyPos, Reply);
  178. return 0;
  179. }
  180. private unsafe void SendFrameBuffer(ServiceCtx Context, int Slot)
  181. {
  182. int FbWidth = BufferQueue[Slot].Data.Width;
  183. int FbHeight = BufferQueue[Slot].Data.Height;
  184. int FbSize = FbWidth * FbHeight * 4;
  185. HNvMap NvMap = GetNvMap(Context, Slot);
  186. if (FbSize < 0 || NvMap.Address < 0 || NvMap.Address + FbSize > AMemoryMgr.AddrSize)
  187. {
  188. Logging.Error($"Frame buffer address {NvMap.Address:x16} is invalid!");
  189. BufferQueue[Slot].State = BufferState.Free;
  190. WaitBufferFree.Set();
  191. return;
  192. }
  193. BufferQueue[Slot].State = BufferState.Acquired;
  194. float ScaleX = 1;
  195. float ScaleY = 1;
  196. float Rotate = 0;
  197. if (BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipX))
  198. {
  199. ScaleX = -1;
  200. }
  201. if (BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipY))
  202. {
  203. ScaleY = -1;
  204. }
  205. if (BufferQueue[Slot].Transform.HasFlag(HalTransform.Rotate90))
  206. {
  207. Rotate = MathF.PI * 0.5f;
  208. }
  209. byte* Fb = (byte*)Context.Ns.Ram + NvMap.Address;
  210. Context.Ns.Gpu.Renderer.QueueAction(delegate()
  211. {
  212. Context.Ns.Gpu.Renderer.SetFrameBuffer(
  213. Fb,
  214. FbWidth,
  215. FbHeight,
  216. ScaleX,
  217. ScaleY,
  218. Rotate);
  219. BufferQueue[Slot].State = BufferState.Free;
  220. lock (WaitBufferFree)
  221. {
  222. WaitBufferFree.Set();
  223. }
  224. });
  225. }
  226. private HNvMap GetNvMap(ServiceCtx Context, int Slot)
  227. {
  228. int NvMapHandle = BitConverter.ToInt32(BufferQueue[Slot].Data.RawData, 0x4c);
  229. if (!BitConverter.IsLittleEndian)
  230. {
  231. byte[] RawValue = BitConverter.GetBytes(NvMapHandle);
  232. Array.Reverse(RawValue);
  233. NvMapHandle = BitConverter.ToInt32(RawValue, 0);
  234. }
  235. return Context.Ns.Os.Handles.GetData<HNvMap>(NvMapHandle);
  236. }
  237. private int GetFreeSlotBlocking(int Width, int Height)
  238. {
  239. int Slot;
  240. do
  241. {
  242. lock (WaitBufferFree)
  243. {
  244. if ((Slot = GetFreeSlot(Width, Height)) != -1)
  245. {
  246. break;
  247. }
  248. Logging.Debug("Waiting for a free BufferQueue slot...");
  249. if (!KeepRunning)
  250. {
  251. break;
  252. }
  253. WaitBufferFree.Reset();
  254. }
  255. WaitBufferFree.WaitOne();
  256. }
  257. while (KeepRunning);
  258. Logging.Debug($"Found free BufferQueue slot {Slot}!");
  259. return Slot;
  260. }
  261. private int GetFreeSlot(int Width, int Height)
  262. {
  263. lock (BufferQueue)
  264. {
  265. for (int Slot = 0; Slot < BufferQueue.Length; Slot++)
  266. {
  267. if (BufferQueue[Slot].State != BufferState.Free)
  268. {
  269. continue;
  270. }
  271. GbpBuffer Data = BufferQueue[Slot].Data;
  272. if (Data.Width == Width &&
  273. Data.Height == Height)
  274. {
  275. BufferQueue[Slot].State = BufferState.Dequeued;
  276. return Slot;
  277. }
  278. }
  279. }
  280. return -1;
  281. }
  282. public void Dispose()
  283. {
  284. Dispose(true);
  285. }
  286. protected virtual void Dispose(bool disposing)
  287. {
  288. if (disposing)
  289. {
  290. lock (WaitBufferFree)
  291. {
  292. KeepRunning = false;
  293. WaitBufferFree.Set();
  294. }
  295. WaitBufferFree.Dispose();
  296. }
  297. }
  298. }
  299. }