IApplicationDisplayService.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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? convertedScalingMode = ConvertScalingMode(scalingMode);
  137. if (!convertedScalingMode.HasValue)
  138. {
  139. //Scaling mode out of the range of valid values.
  140. return MakeError(ErrorModule.Vi, 1);
  141. }
  142. if (scalingMode != SrcScalingMode.ScaleToWindow &&
  143. scalingMode != SrcScalingMode.PreserveAspectRatio)
  144. {
  145. //Invalid scaling mode specified.
  146. return MakeError(ErrorModule.Vi, 6);
  147. }
  148. context.ResponseData.Write((ulong)convertedScalingMode);
  149. return 0;
  150. }
  151. private DstScalingMode? ConvertScalingMode(SrcScalingMode source)
  152. {
  153. switch (source)
  154. {
  155. case SrcScalingMode.None: return DstScalingMode.None;
  156. case SrcScalingMode.Freeze: return DstScalingMode.Freeze;
  157. case SrcScalingMode.ScaleAndCrop: return DstScalingMode.ScaleAndCrop;
  158. case SrcScalingMode.ScaleToWindow: return DstScalingMode.ScaleToWindow;
  159. case SrcScalingMode.PreserveAspectRatio: return DstScalingMode.PreserveAspectRatio;
  160. }
  161. return null;
  162. }
  163. public long GetDisplayVSyncEvent(ServiceCtx context)
  164. {
  165. string name = GetDisplayName(context);
  166. if (context.Process.HandleTable.GenerateHandle(context.Device.System.VsyncEvent.ReadableEvent, out int handle) != KernelResult.Success)
  167. {
  168. throw new InvalidOperationException("Out of handles!");
  169. }
  170. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  171. return 0;
  172. }
  173. private byte[] MakeIGraphicsBufferProducer(long basePtr)
  174. {
  175. long id = 0x20;
  176. long cookiePtr = 0L;
  177. using (MemoryStream ms = new MemoryStream())
  178. {
  179. BinaryWriter writer = new BinaryWriter(ms);
  180. //flat_binder_object (size is 0x28)
  181. writer.Write(2); //Type (BINDER_TYPE_WEAK_BINDER)
  182. writer.Write(0); //Flags
  183. writer.Write((int)(id >> 0));
  184. writer.Write((int)(id >> 32));
  185. writer.Write((int)(cookiePtr >> 0));
  186. writer.Write((int)(cookiePtr >> 32));
  187. writer.Write((byte)'d');
  188. writer.Write((byte)'i');
  189. writer.Write((byte)'s');
  190. writer.Write((byte)'p');
  191. writer.Write((byte)'d');
  192. writer.Write((byte)'r');
  193. writer.Write((byte)'v');
  194. writer.Write((byte)'\0');
  195. writer.Write(0L); //Pad
  196. return MakeParcel(ms.ToArray(), new byte[] { 0, 0, 0, 0 });
  197. }
  198. }
  199. private string GetDisplayName(ServiceCtx context)
  200. {
  201. string name = string.Empty;
  202. for (int index = 0; index < 8 &&
  203. context.RequestData.BaseStream.Position <
  204. context.RequestData.BaseStream.Length; index++)
  205. {
  206. byte chr = context.RequestData.ReadByte();
  207. if (chr >= 0x20 && chr < 0x7f)
  208. {
  209. name += (char)chr;
  210. }
  211. }
  212. return name;
  213. }
  214. }
  215. }