IApplicationDisplayService.cs 7.4 KB

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