ViIHOSBinderDriver.cs 7.0 KB

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