IApplicationDisplayService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using ChocolArm64.Memory;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using static Ryujinx.HLE.HOS.Services.Android.Parcel;
  7. namespace Ryujinx.HLE.HOS.Services.Vi
  8. {
  9. class IApplicationDisplayService : IpcService
  10. {
  11. private Dictionary<int, ServiceProcessRequest> m_Commands;
  12. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  13. private IdDictionary Displays;
  14. public IApplicationDisplayService()
  15. {
  16. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  17. {
  18. { 100, GetRelayService },
  19. { 101, GetSystemDisplayService },
  20. { 102, GetManagerDisplayService },
  21. { 103, GetIndirectDisplayTransactionService },
  22. { 1000, ListDisplays },
  23. { 1010, OpenDisplay },
  24. { 1020, CloseDisplay },
  25. { 1102, GetDisplayResolution },
  26. { 2020, OpenLayer },
  27. { 2021, CloseLayer },
  28. { 2030, CreateStrayLayer },
  29. { 2031, DestroyStrayLayer },
  30. { 2101, SetLayerScalingMode },
  31. { 5202, GetDisplayVSyncEvent }
  32. };
  33. Displays = new IdDictionary();
  34. }
  35. public long GetRelayService(ServiceCtx Context)
  36. {
  37. MakeObject(Context, new IHOSBinderDriver(Context.Device.Gpu.Renderer));
  38. return 0;
  39. }
  40. public long GetSystemDisplayService(ServiceCtx Context)
  41. {
  42. MakeObject(Context, new ISystemDisplayService());
  43. return 0;
  44. }
  45. public long GetManagerDisplayService(ServiceCtx Context)
  46. {
  47. MakeObject(Context, new IManagerDisplayService());
  48. return 0;
  49. }
  50. public long GetIndirectDisplayTransactionService(ServiceCtx Context)
  51. {
  52. MakeObject(Context, new IHOSBinderDriver(Context.Device.Gpu.Renderer));
  53. return 0;
  54. }
  55. public long ListDisplays(ServiceCtx Context)
  56. {
  57. long RecBuffPtr = Context.Request.ReceiveBuff[0].Position;
  58. AMemoryHelper.FillWithZeros(Context.Memory, RecBuffPtr, 0x60);
  59. //Add only the default display to buffer
  60. Context.Memory.WriteBytes(RecBuffPtr, Encoding.ASCII.GetBytes("Default"));
  61. Context.Memory.WriteInt64(RecBuffPtr + 0x40, 0x1L);
  62. Context.Memory.WriteInt64(RecBuffPtr + 0x48, 0x1L);
  63. Context.Memory.WriteInt64(RecBuffPtr + 0x50, 1920L);
  64. Context.Memory.WriteInt64(RecBuffPtr + 0x58, 1080L);
  65. Context.ResponseData.Write(1L);
  66. return 0;
  67. }
  68. public long OpenDisplay(ServiceCtx Context)
  69. {
  70. string Name = GetDisplayName(Context);
  71. long DisplayId = Displays.Add(new Display(Name));
  72. Context.ResponseData.Write(DisplayId);
  73. return 0;
  74. }
  75. public long CloseDisplay(ServiceCtx Context)
  76. {
  77. int DisplayId = Context.RequestData.ReadInt32();
  78. Displays.Delete(DisplayId);
  79. return 0;
  80. }
  81. public long GetDisplayResolution(ServiceCtx Context)
  82. {
  83. long DisplayId = Context.RequestData.ReadInt32();
  84. Context.ResponseData.Write(1280);
  85. Context.ResponseData.Write(720);
  86. return 0;
  87. }
  88. public long OpenLayer(ServiceCtx Context)
  89. {
  90. long LayerId = Context.RequestData.ReadInt64();
  91. long UserId = Context.RequestData.ReadInt64();
  92. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  93. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  94. Context.Memory.WriteBytes(ParcelPtr, Parcel);
  95. Context.ResponseData.Write((long)Parcel.Length);
  96. return 0;
  97. }
  98. public long CloseLayer(ServiceCtx Context)
  99. {
  100. long LayerId = Context.RequestData.ReadInt64();
  101. return 0;
  102. }
  103. public long CreateStrayLayer(ServiceCtx Context)
  104. {
  105. long LayerFlags = Context.RequestData.ReadInt64();
  106. long DisplayId = Context.RequestData.ReadInt64();
  107. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  108. Display Disp = Displays.GetData<Display>((int)DisplayId);
  109. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  110. Context.Memory.WriteBytes(ParcelPtr, Parcel);
  111. Context.ResponseData.Write(0L);
  112. Context.ResponseData.Write((long)Parcel.Length);
  113. return 0;
  114. }
  115. public long DestroyStrayLayer(ServiceCtx Context)
  116. {
  117. return 0;
  118. }
  119. public long SetLayerScalingMode(ServiceCtx Context)
  120. {
  121. int ScalingMode = Context.RequestData.ReadInt32();
  122. long Unknown = Context.RequestData.ReadInt64();
  123. return 0;
  124. }
  125. public long GetDisplayVSyncEvent(ServiceCtx Context)
  126. {
  127. string Name = GetDisplayName(Context);
  128. int Handle = Context.Process.HandleTable.OpenHandle(Context.Device.System.VsyncEvent);
  129. Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
  130. return 0;
  131. }
  132. private byte[] MakeIGraphicsBufferProducer(long BasePtr)
  133. {
  134. long Id = 0x20;
  135. long CookiePtr = 0L;
  136. using (MemoryStream MS = new MemoryStream())
  137. {
  138. BinaryWriter Writer = new BinaryWriter(MS);
  139. //flat_binder_object (size is 0x28)
  140. Writer.Write(2); //Type (BINDER_TYPE_WEAK_BINDER)
  141. Writer.Write(0); //Flags
  142. Writer.Write((int)(Id >> 0));
  143. Writer.Write((int)(Id >> 32));
  144. Writer.Write((int)(CookiePtr >> 0));
  145. Writer.Write((int)(CookiePtr >> 32));
  146. Writer.Write((byte)'d');
  147. Writer.Write((byte)'i');
  148. Writer.Write((byte)'s');
  149. Writer.Write((byte)'p');
  150. Writer.Write((byte)'d');
  151. Writer.Write((byte)'r');
  152. Writer.Write((byte)'v');
  153. Writer.Write((byte)'\0');
  154. Writer.Write(0L); //Pad
  155. return MakeParcel(MS.ToArray(), new byte[] { 0, 0, 0, 0 });
  156. }
  157. }
  158. private string GetDisplayName(ServiceCtx Context)
  159. {
  160. string Name = string.Empty;
  161. for (int Index = 0; Index < 8 &&
  162. Context.RequestData.BaseStream.Position <
  163. Context.RequestData.BaseStream.Length; Index++)
  164. {
  165. byte Chr = Context.RequestData.ReadByte();
  166. if (Chr >= 0x20 && Chr < 0x7f)
  167. {
  168. Name += (char)Chr;
  169. }
  170. }
  171. return Name;
  172. }
  173. }
  174. }