NvFlinger.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.Gal;
  3. using Ryujinx.Graphics.Memory;
  4. using Ryujinx.HLE.HOS.Kernel;
  5. using Ryujinx.HLE.HOS.Services.Nv.NvGpuAS;
  6. using Ryujinx.HLE.HOS.Services.Nv.NvMap;
  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 AutoResetEvent 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 AutoResetEvent(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. Logger.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. WaitBufferFree.Set();
  172. return MakeReplyParcel(Context, 0);
  173. }
  174. private long GbpQuery(ServiceCtx Context, BinaryReader ParcelReader)
  175. {
  176. return MakeReplyParcel(Context, 0, 0);
  177. }
  178. private long GbpConnect(ServiceCtx Context, BinaryReader ParcelReader)
  179. {
  180. return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
  181. }
  182. private long GbpDisconnect(ServiceCtx Context, BinaryReader ParcelReader)
  183. {
  184. return MakeReplyParcel(Context, 0);
  185. }
  186. private long GbpPreallocBuffer(ServiceCtx Context, BinaryReader ParcelReader)
  187. {
  188. int Slot = ParcelReader.ReadInt32();
  189. int BufferCount = ParcelReader.ReadInt32();
  190. if (BufferCount > 0)
  191. {
  192. long BufferSize = ParcelReader.ReadInt64();
  193. BufferQueue[Slot].State = BufferState.Free;
  194. BufferQueue[Slot].Data = new GbpBuffer(ParcelReader);
  195. }
  196. return MakeReplyParcel(Context, 0);
  197. }
  198. private long MakeReplyParcel(ServiceCtx Context, params int[] Ints)
  199. {
  200. using (MemoryStream MS = new MemoryStream())
  201. {
  202. BinaryWriter Writer = new BinaryWriter(MS);
  203. foreach (int Int in Ints)
  204. {
  205. Writer.Write(Int);
  206. }
  207. return MakeReplyParcel(Context, MS.ToArray());
  208. }
  209. }
  210. private long MakeReplyParcel(ServiceCtx Context, byte[] Data)
  211. {
  212. long ReplyPos = Context.Request.ReceiveBuff[0].Position;
  213. long ReplySize = Context.Request.ReceiveBuff[0].Size;
  214. byte[] Reply = MakeParcel(Data, new byte[0]);
  215. Context.Memory.WriteBytes(ReplyPos, Reply);
  216. return 0;
  217. }
  218. private void SendFrameBuffer(ServiceCtx Context, int Slot)
  219. {
  220. int FbWidth = BufferQueue[Slot].Data.Width;
  221. int FbHeight = BufferQueue[Slot].Data.Height;
  222. int NvMapHandle = BitConverter.ToInt32(BufferQueue[Slot].Data.RawData, 0x4c);
  223. int BufferOffset = BitConverter.ToInt32(BufferQueue[Slot].Data.RawData, 0x50);
  224. NvMapHandle Map = NvMapIoctl.GetNvMap(Context, NvMapHandle);;
  225. long FbAddr = Map.Address + BufferOffset;
  226. BufferQueue[Slot].State = BufferState.Acquired;
  227. Rect Crop = BufferQueue[Slot].Crop;
  228. bool FlipX = BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipX);
  229. bool FlipY = BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipY);
  230. //Note: Rotation is being ignored.
  231. int Top = Crop.Top;
  232. int Left = Crop.Left;
  233. int Right = Crop.Right;
  234. int Bottom = Crop.Bottom;
  235. NvGpuVmm Vmm = NvGpuASIoctl.GetASCtx(Context).Vmm;
  236. Renderer.QueueAction(() =>
  237. {
  238. if (!Renderer.Texture.TryGetImage(FbAddr, out GalImage Image))
  239. {
  240. Image = new GalImage(
  241. FbWidth,
  242. FbHeight, 1, 16,
  243. GalMemoryLayout.BlockLinear,
  244. GalImageFormat.RGBA8 | GalImageFormat.Unorm);
  245. }
  246. Context.Device.Gpu.ResourceManager.ClearPbCache();
  247. Context.Device.Gpu.ResourceManager.SendTexture(Vmm, FbAddr, Image);
  248. Renderer.RenderTarget.SetTransform(FlipX, FlipY, Top, Left, Right, Bottom);
  249. Renderer.RenderTarget.Present(FbAddr);
  250. ReleaseBuffer(Slot);
  251. });
  252. }
  253. private void ReleaseBuffer(int Slot)
  254. {
  255. BufferQueue[Slot].State = BufferState.Free;
  256. BinderEvent.ReadableEvent.Signal();
  257. WaitBufferFree.Set();
  258. }
  259. private int GetFreeSlotBlocking(int Width, int Height)
  260. {
  261. int Slot;
  262. do
  263. {
  264. if ((Slot = GetFreeSlot(Width, Height)) != -1)
  265. {
  266. break;
  267. }
  268. if (Disposed)
  269. {
  270. break;
  271. }
  272. WaitBufferFree.WaitOne();
  273. }
  274. while (!Disposed);
  275. return Slot;
  276. }
  277. private int GetFreeSlot(int Width, int Height)
  278. {
  279. lock (BufferQueue)
  280. {
  281. for (int Slot = 0; Slot < BufferQueue.Length; Slot++)
  282. {
  283. if (BufferQueue[Slot].State != BufferState.Free)
  284. {
  285. continue;
  286. }
  287. GbpBuffer Data = BufferQueue[Slot].Data;
  288. if (Data.Width == Width &&
  289. Data.Height == Height)
  290. {
  291. BufferQueue[Slot].State = BufferState.Dequeued;
  292. return Slot;
  293. }
  294. }
  295. }
  296. return -1;
  297. }
  298. public void Dispose()
  299. {
  300. Dispose(true);
  301. }
  302. protected virtual void Dispose(bool Disposing)
  303. {
  304. if (Disposing && !Disposed)
  305. {
  306. Disposed = true;
  307. WaitBufferFree.Set();
  308. WaitBufferFree.Dispose();
  309. }
  310. }
  311. }
  312. }