NvFlinger.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.Gal;
  3. using Ryujinx.Graphics.Memory;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  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. _renderer = renderer;
  69. _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, long replySize) = context.Request.GetBufferType0x22();
  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.RGBA8 | 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.Present(fbAddr);
  249. ReleaseBuffer(slot);
  250. });
  251. }
  252. private void ReleaseBuffer(int slot)
  253. {
  254. _bufferQueue[slot].State = BufferState.Free;
  255. _binderEvent.ReadableEvent.Signal();
  256. _waitBufferFree.Set();
  257. }
  258. private int GetFreeSlotBlocking(int width, int height)
  259. {
  260. int slot;
  261. do
  262. {
  263. if ((slot = GetFreeSlot(width, height)) != -1)
  264. {
  265. break;
  266. }
  267. if (_disposed)
  268. {
  269. break;
  270. }
  271. _waitBufferFree.WaitOne();
  272. }
  273. while (!_disposed);
  274. return slot;
  275. }
  276. private int GetFreeSlot(int width, int height)
  277. {
  278. lock (_bufferQueue)
  279. {
  280. for (int slot = 0; slot < _bufferQueue.Length; slot++)
  281. {
  282. if (_bufferQueue[slot].State != BufferState.Free)
  283. {
  284. continue;
  285. }
  286. GbpBuffer data = _bufferQueue[slot].Data;
  287. if (data.Width == width &&
  288. data.Height == height)
  289. {
  290. _bufferQueue[slot].State = BufferState.Dequeued;
  291. return slot;
  292. }
  293. }
  294. }
  295. return -1;
  296. }
  297. public void Dispose()
  298. {
  299. Dispose(true);
  300. }
  301. protected virtual void Dispose(bool disposing)
  302. {
  303. if (disposing && !_disposed)
  304. {
  305. _disposed = true;
  306. _waitBufferFree.Set();
  307. _waitBufferFree.Dispose();
  308. }
  309. }
  310. }
  311. }