IHOSBinderDriver.cs 6.9 KB

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