IHOSBinderDriver.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using ChocolArm64.Memory;
  2. using Ryujinx.OsHle.Handles;
  3. using Ryujinx.OsHle.Ipc;
  4. using Ryujinx.OsHle.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Text;
  9. using static Ryujinx.OsHle.Objects.Android.Parcel;
  10. namespace Ryujinx.OsHle.Objects.Vi
  11. {
  12. class IHOSBinderDriver : IIpcInterface
  13. {
  14. private Dictionary<int, ServiceProcessRequest> m_Commands;
  15. public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  16. private delegate long ServiceProcessRequest2(ServiceCtx Context, byte[] ParcelData);
  17. private Dictionary<(string, int), ServiceProcessRequest2> InterfaceMthd =
  18. new Dictionary<(string, int), ServiceProcessRequest2>()
  19. {
  20. { ("android.gui.IGraphicBufferProducer", 0x1), GraphicBufferProducerRequestBuffer },
  21. { ("android.gui.IGraphicBufferProducer", 0x3), GraphicBufferProducerDequeueBuffer },
  22. { ("android.gui.IGraphicBufferProducer", 0x7), GraphicBufferProducerQueueBuffer },
  23. //{ ("android.gui.IGraphicBufferProducer", 0x8), GraphicBufferProducerCancelBuffer },
  24. { ("android.gui.IGraphicBufferProducer", 0x9), GraphicBufferProducerQuery },
  25. { ("android.gui.IGraphicBufferProducer", 0xa), GraphicBufferProducerConnect },
  26. { ("android.gui.IGraphicBufferProducer", 0xe), GraphicBufferPreallocateBuffer },
  27. };
  28. private class BufferObj
  29. {
  30. }
  31. public IdPoolWithObj BufferSlots { get; private set; }
  32. public byte[] Gbfr;
  33. public IHOSBinderDriver()
  34. {
  35. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  36. {
  37. { 0, TransactParcel },
  38. { 1, AdjustRefcount },
  39. { 2, GetNativeHandle }
  40. };
  41. BufferSlots = new IdPoolWithObj();
  42. }
  43. public long TransactParcel(ServiceCtx Context)
  44. {
  45. int Id = Context.RequestData.ReadInt32();
  46. int Code = Context.RequestData.ReadInt32();
  47. long DataPos = Context.Request.SendBuff[0].Position;
  48. long DataSize = Context.Request.SendBuff[0].Size;
  49. byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, DataPos, (int)DataSize);
  50. Data = GetParcelData(Data);
  51. using (MemoryStream MS = new MemoryStream(Data))
  52. {
  53. BinaryReader Reader = new BinaryReader(MS);
  54. MS.Seek(4, SeekOrigin.Current);
  55. int StrSize = Reader.ReadInt32();
  56. string InterfaceName = Encoding.Unicode.GetString(Data, 8, StrSize * 2);
  57. if (InterfaceMthd.TryGetValue((InterfaceName, Code), out ServiceProcessRequest2 ProcReq))
  58. {
  59. return ProcReq(Context, Data);
  60. }
  61. else
  62. {
  63. throw new NotImplementedException($"{InterfaceName} {Code}");
  64. }
  65. }
  66. }
  67. private static long GraphicBufferProducerRequestBuffer(ServiceCtx Context, byte[] ParcelData)
  68. {
  69. IHOSBinderDriver BinderDriver = Context.GetObject<IHOSBinderDriver>();
  70. int GbfrSize = BinderDriver.Gbfr?.Length ?? 0;
  71. byte[] Data = new byte[GbfrSize + 4];
  72. if (BinderDriver.Gbfr != null)
  73. {
  74. Buffer.BlockCopy(BinderDriver.Gbfr, 0, Data, 0, GbfrSize);
  75. }
  76. return MakeReplyParcel(Context, Data);
  77. }
  78. private static long GraphicBufferProducerDequeueBuffer(ServiceCtx Context, byte[] ParcelData)
  79. {
  80. IHOSBinderDriver BinderDriver = Context.GetObject<IHOSBinderDriver>();
  81. //Note: It seems that the maximum number of slots is 64, because if we return
  82. //a Slot number > 63, it seems to cause a buffer overrun and it reads garbage.
  83. //Note 2: The size of each object associated with the slot is 0x30.
  84. int Slot = BinderDriver.BufferSlots.GenerateId(new BufferObj());
  85. return MakeReplyParcel(Context, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  86. }
  87. private static long GraphicBufferProducerQueueBuffer(ServiceCtx Context, byte[] ParcelData)
  88. {
  89. return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
  90. }
  91. private static long GraphicBufferProducerCancelBuffer(ServiceCtx Context, byte[] ParcelData)
  92. {
  93. IHOSBinderDriver BinderDriver = Context.GetObject<IHOSBinderDriver>();
  94. using (MemoryStream MS = new MemoryStream(ParcelData))
  95. {
  96. BinaryReader Reader = new BinaryReader(MS);
  97. MS.Seek(0x50, SeekOrigin.Begin);
  98. int Slot = Reader.ReadInt32();
  99. BinderDriver.BufferSlots.Delete(Slot);
  100. return MakeReplyParcel(Context, 0);
  101. }
  102. }
  103. private static long GraphicBufferProducerQuery(ServiceCtx Context, byte[] ParcelData)
  104. {
  105. return MakeReplyParcel(Context, 0, 0);
  106. }
  107. private static long GraphicBufferProducerConnect(ServiceCtx Context, byte[] ParcelData)
  108. {
  109. return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
  110. }
  111. private static long GraphicBufferPreallocateBuffer(ServiceCtx Context, byte[] ParcelData)
  112. {
  113. IHOSBinderDriver BinderDriver = Context.GetObject<IHOSBinderDriver>();
  114. int GbfrSize = ParcelData.Length - 0x54;
  115. BinderDriver.Gbfr = new byte[GbfrSize];
  116. Buffer.BlockCopy(ParcelData, 0x54, BinderDriver.Gbfr, 0, GbfrSize);
  117. using (MemoryStream MS = new MemoryStream(ParcelData))
  118. {
  119. BinaryReader Reader = new BinaryReader(MS);
  120. MS.Seek(0xd4, SeekOrigin.Begin);
  121. int Handle = Reader.ReadInt32();
  122. HNvMap NvMap = Context.Ns.Os.Handles.GetData<HNvMap>(Handle);
  123. Context.Ns.Gpu.Renderer.FrameBufferPtr = NvMap.Address;
  124. }
  125. return MakeReplyParcel(Context, 0);
  126. }
  127. private static long MakeReplyParcel(ServiceCtx Context, params int[] Ints)
  128. {
  129. using (MemoryStream MS = new MemoryStream())
  130. {
  131. BinaryWriter Writer = new BinaryWriter(MS);
  132. foreach (int Int in Ints)
  133. {
  134. Writer.Write(Int);
  135. }
  136. return MakeReplyParcel(Context, MS.ToArray());
  137. }
  138. }
  139. private static long MakeReplyParcel(ServiceCtx Context, byte[] Data)
  140. {
  141. long ReplyPos = Context.Request.ReceiveBuff[0].Position;
  142. long ReplySize = Context.Request.ReceiveBuff[0].Position;
  143. byte[] Reply = MakeParcel(Data, new byte[0]);
  144. AMemoryHelper.WriteBytes(Context.Memory, ReplyPos, Reply);
  145. return 0;
  146. }
  147. public long AdjustRefcount(ServiceCtx Context)
  148. {
  149. int Id = Context.RequestData.ReadInt32();
  150. int AddVal = Context.RequestData.ReadInt32();
  151. int Type = Context.RequestData.ReadInt32();
  152. return 0;
  153. }
  154. public long GetNativeHandle(ServiceCtx Context)
  155. {
  156. int Id = Context.RequestData.ReadInt32();
  157. uint Unk = Context.RequestData.ReadUInt32();
  158. Context.Response.HandleDesc = IpcHandleDesc.MakeMove(0xbadcafe);
  159. return 0;
  160. }
  161. }
  162. }