IApplicationDisplayService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Cpu;
  4. using Ryujinx.HLE.HOS.Ipc;
  5. using Ryujinx.HLE.HOS.Kernel.Common;
  6. using Ryujinx.HLE.HOS.Services.SurfaceFlinger;
  7. using Ryujinx.HLE.HOS.Services.Vi.RootService.ApplicationDisplayService;
  8. using System;
  9. using System.Text;
  10. namespace Ryujinx.HLE.HOS.Services.Vi.RootService
  11. {
  12. class IApplicationDisplayService : IpcService
  13. {
  14. private readonly IdDictionary _displays;
  15. private int _vsyncEventHandle;
  16. public IApplicationDisplayService()
  17. {
  18. _displays = new IdDictionary();
  19. }
  20. [Command(100)]
  21. // GetRelayService() -> object<nns::hosbinder::IHOSBinderDriver>
  22. public ResultCode GetRelayService(ServiceCtx context)
  23. {
  24. MakeObject(context, new HOSBinderDriverServer());
  25. return ResultCode.Success;
  26. }
  27. [Command(101)]
  28. // GetSystemDisplayService() -> object<nn::visrv::sf::ISystemDisplayService>
  29. public ResultCode GetSystemDisplayService(ServiceCtx context)
  30. {
  31. MakeObject(context, new ISystemDisplayService(this));
  32. return ResultCode.Success;
  33. }
  34. [Command(102)]
  35. // GetManagerDisplayService() -> object<nn::visrv::sf::IManagerDisplayService>
  36. public ResultCode GetManagerDisplayService(ServiceCtx context)
  37. {
  38. MakeObject(context, new IManagerDisplayService(this));
  39. return ResultCode.Success;
  40. }
  41. [Command(103)] // 2.0.0+
  42. // GetIndirectDisplayTransactionService() -> object<nns::hosbinder::IHOSBinderDriver>
  43. public ResultCode GetIndirectDisplayTransactionService(ServiceCtx context)
  44. {
  45. MakeObject(context, new HOSBinderDriverServer());
  46. return ResultCode.Success;
  47. }
  48. [Command(1000)]
  49. // ListDisplays() -> (u64, buffer<nn::vi::DisplayInfo, 6>)
  50. public ResultCode ListDisplays(ServiceCtx context)
  51. {
  52. long recBuffPtr = context.Request.ReceiveBuff[0].Position;
  53. MemoryHelper.FillWithZeros(context.Memory, recBuffPtr, 0x60);
  54. // Add only the default display to buffer
  55. context.Memory.Write((ulong)recBuffPtr, Encoding.ASCII.GetBytes("Default"));
  56. context.Memory.Write((ulong)recBuffPtr + 0x40, 0x1L);
  57. context.Memory.Write((ulong)recBuffPtr + 0x48, 0x1L);
  58. context.Memory.Write((ulong)recBuffPtr + 0x50, 1280L);
  59. context.Memory.Write((ulong)recBuffPtr + 0x58, 720L);
  60. context.ResponseData.Write(1L);
  61. return ResultCode.Success;
  62. }
  63. [Command(1010)]
  64. // OpenDisplay(nn::vi::DisplayName) -> u64
  65. public ResultCode OpenDisplay(ServiceCtx context)
  66. {
  67. string name = GetDisplayName(context);
  68. long displayId = _displays.Add(new Display(name));
  69. context.ResponseData.Write(displayId);
  70. return ResultCode.Success;
  71. }
  72. [Command(1020)]
  73. // CloseDisplay(u64)
  74. public ResultCode CloseDisplay(ServiceCtx context)
  75. {
  76. int displayId = context.RequestData.ReadInt32();
  77. _displays.Delete(displayId);
  78. return ResultCode.Success;
  79. }
  80. [Command(1102)]
  81. // GetDisplayResolution(u64) -> (u64, u64)
  82. public ResultCode GetDisplayResolution(ServiceCtx context)
  83. {
  84. long displayId = context.RequestData.ReadInt32();
  85. context.ResponseData.Write(1280);
  86. context.ResponseData.Write(720);
  87. return ResultCode.Success;
  88. }
  89. [Command(2020)]
  90. // OpenLayer(nn::vi::DisplayName, u64, nn::applet::AppletResourceUserId, pid) -> (u64, buffer<bytes, 6>)
  91. public ResultCode OpenLayer(ServiceCtx context)
  92. {
  93. // TODO: support multi display.
  94. byte[] displayName = context.RequestData.ReadBytes(0x40);
  95. long layerId = context.RequestData.ReadInt64();
  96. long userId = context.RequestData.ReadInt64();
  97. long parcelPtr = context.Request.ReceiveBuff[0].Position;
  98. IBinder producer = context.Device.System.SurfaceFlinger.OpenLayer(context.Request.HandleDesc.PId, layerId);
  99. Parcel parcel = new Parcel(0x28, 0x4);
  100. parcel.WriteObject(producer, "dispdrv\0");
  101. ReadOnlySpan<byte> parcelData = parcel.Finish();
  102. context.Memory.Write((ulong)parcelPtr, parcelData);
  103. context.ResponseData.Write((long)parcelData.Length);
  104. return ResultCode.Success;
  105. }
  106. [Command(2021)]
  107. // CloseLayer(u64)
  108. public ResultCode CloseLayer(ServiceCtx context)
  109. {
  110. long layerId = context.RequestData.ReadInt64();
  111. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  112. return ResultCode.Success;
  113. }
  114. [Command(2030)]
  115. // CreateStrayLayer(u32, u64) -> (u64, u64, buffer<bytes, 6>)
  116. public ResultCode CreateStrayLayer(ServiceCtx context)
  117. {
  118. long layerFlags = context.RequestData.ReadInt64();
  119. long displayId = context.RequestData.ReadInt64();
  120. long parcelPtr = context.Request.ReceiveBuff[0].Position;
  121. // TODO: support multi display.
  122. Display disp = _displays.GetData<Display>((int)displayId);
  123. IBinder producer = context.Device.System.SurfaceFlinger.CreateLayer(0, out long layerId);
  124. Parcel parcel = new Parcel(0x28, 0x4);
  125. parcel.WriteObject(producer, "dispdrv\0");
  126. ReadOnlySpan<byte> parcelData = parcel.Finish();
  127. context.Memory.Write((ulong)parcelPtr, parcelData);
  128. context.ResponseData.Write(layerId);
  129. context.ResponseData.Write((long)parcelData.Length);
  130. return ResultCode.Success;
  131. }
  132. [Command(2031)]
  133. // DestroyStrayLayer(u64)
  134. public ResultCode DestroyStrayLayer(ServiceCtx context)
  135. {
  136. long layerId = context.RequestData.ReadInt64();
  137. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  138. return ResultCode.Success;
  139. }
  140. [Command(2101)]
  141. // SetLayerScalingMode(u32, u64)
  142. public ResultCode SetLayerScalingMode(ServiceCtx context)
  143. {
  144. int scalingMode = context.RequestData.ReadInt32();
  145. long layerId = context.RequestData.ReadInt64();
  146. return ResultCode.Success;
  147. }
  148. [Command(2102)] // 5.0.0+
  149. // ConvertScalingMode(unknown) -> unknown
  150. public ResultCode ConvertScalingMode(ServiceCtx context)
  151. {
  152. SourceScalingMode scalingMode = (SourceScalingMode)context.RequestData.ReadInt32();
  153. DestinationScalingMode? convertedScalingMode = ConvertScalingMode(scalingMode);
  154. if (!convertedScalingMode.HasValue)
  155. {
  156. // Scaling mode out of the range of valid values.
  157. return ResultCode.InvalidArguments;
  158. }
  159. if (scalingMode != SourceScalingMode.ScaleToWindow &&
  160. scalingMode != SourceScalingMode.PreserveAspectRatio)
  161. {
  162. // Invalid scaling mode specified.
  163. return ResultCode.InvalidScalingMode;
  164. }
  165. context.ResponseData.Write((ulong)convertedScalingMode);
  166. return ResultCode.Success;
  167. }
  168. private DestinationScalingMode? ConvertScalingMode(SourceScalingMode source)
  169. {
  170. switch (source)
  171. {
  172. case SourceScalingMode.None: return DestinationScalingMode.None;
  173. case SourceScalingMode.Freeze: return DestinationScalingMode.Freeze;
  174. case SourceScalingMode.ScaleAndCrop: return DestinationScalingMode.ScaleAndCrop;
  175. case SourceScalingMode.ScaleToWindow: return DestinationScalingMode.ScaleToWindow;
  176. case SourceScalingMode.PreserveAspectRatio: return DestinationScalingMode.PreserveAspectRatio;
  177. }
  178. return null;
  179. }
  180. [Command(2450)]
  181. // GetIndirectLayerImageMap(s64 width, s64 height, u64 handle, nn::applet::AppletResourceUserId, pid) -> (s64, s64, buffer<bytes, 0x46>)
  182. public ResultCode GetIndirectLayerImageMap(ServiceCtx context)
  183. {
  184. // The size of the layer buffer should be an aligned multiple of width * height
  185. // because it was created using GetIndirectLayerImageRequiredMemoryInfo as a guide.
  186. long layerBuffPosition = context.Request.ReceiveBuff[0].Position;
  187. long layerBuffSize = context.Request.ReceiveBuff[0].Size;
  188. // Fill the layer with zeros.
  189. context.Memory.Fill((ulong)layerBuffPosition, (ulong)layerBuffSize, 0x00);
  190. Logger.Stub?.PrintStub(LogClass.ServiceVi);
  191. return ResultCode.Success;
  192. }
  193. [Command(2460)]
  194. // GetIndirectLayerImageRequiredMemoryInfo(u64 width, u64 height) -> (u64 size, u64 alignment)
  195. public ResultCode GetIndirectLayerImageRequiredMemoryInfo(ServiceCtx context)
  196. {
  197. /*
  198. // Doesn't occur in our case.
  199. if (sizePtr == null || address_alignmentPtr == null)
  200. {
  201. return ResultCode.InvalidArguments;
  202. }
  203. */
  204. int width = (int)context.RequestData.ReadUInt64();
  205. int height = (int)context.RequestData.ReadUInt64();
  206. if (height < 0 || width < 0)
  207. {
  208. return ResultCode.InvalidLayerSize;
  209. }
  210. else
  211. {
  212. /*
  213. // Doesn't occur in our case.
  214. if (!service_initialized)
  215. {
  216. return ResultCode.InvalidArguments;
  217. }
  218. */
  219. const ulong defaultAlignment = 0x1000;
  220. const ulong defaultSize = 0x20000;
  221. // NOTE: The official service setup a A8B8G8R8 texture with a linear layout and then query its size.
  222. // As we don't need this texture on the emulator, we can just simplify this logic and directly
  223. // do a linear layout size calculation. (stride * height * bytePerPixel)
  224. int pitch = BitUtils.AlignUp(BitUtils.DivRoundUp(width * 32, 8), 64);
  225. int memorySize = pitch * BitUtils.AlignUp(height, 64);
  226. ulong requiredMemorySize = (ulong)BitUtils.AlignUp(memorySize, (int)defaultAlignment);
  227. ulong size = (requiredMemorySize + defaultSize - 1) / defaultSize * defaultSize;
  228. context.ResponseData.Write(size);
  229. context.ResponseData.Write(defaultAlignment);
  230. }
  231. return ResultCode.Success;
  232. }
  233. [Command(5202)]
  234. // GetDisplayVsyncEvent(u64) -> handle<copy>
  235. public ResultCode GetDisplayVSyncEvent(ServiceCtx context)
  236. {
  237. string name = GetDisplayName(context);
  238. if (_vsyncEventHandle == 0)
  239. {
  240. if (context.Process.HandleTable.GenerateHandle(context.Device.System.VsyncEvent.ReadableEvent, out _vsyncEventHandle) != KernelResult.Success)
  241. {
  242. throw new InvalidOperationException("Out of handles!");
  243. }
  244. }
  245. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_vsyncEventHandle);
  246. return ResultCode.Success;
  247. }
  248. private string GetDisplayName(ServiceCtx context)
  249. {
  250. string name = string.Empty;
  251. for (int index = 0; index < 8 &&
  252. context.RequestData.BaseStream.Position <
  253. context.RequestData.BaseStream.Length; index++)
  254. {
  255. byte chr = context.RequestData.ReadByte();
  256. if (chr >= 0x20 && chr < 0x7f)
  257. {
  258. name += (char)chr;
  259. }
  260. }
  261. return name;
  262. }
  263. }
  264. }