IApplicationDisplayService.cs 8.6 KB

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