IApplicationDisplayService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  100. Parcel parcel = new Parcel(0x28, 0x4);
  101. parcel.WriteObject(producer, "dispdrv\0");
  102. ReadOnlySpan<byte> parcelData = parcel.Finish();
  103. context.Memory.Write((ulong)parcelPtr, parcelData);
  104. context.ResponseData.Write((long)parcelData.Length);
  105. return ResultCode.Success;
  106. }
  107. [Command(2021)]
  108. // CloseLayer(u64)
  109. public ResultCode CloseLayer(ServiceCtx context)
  110. {
  111. long layerId = context.RequestData.ReadInt64();
  112. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  113. return ResultCode.Success;
  114. }
  115. [Command(2030)]
  116. // CreateStrayLayer(u32, u64) -> (u64, u64, buffer<bytes, 6>)
  117. public ResultCode CreateStrayLayer(ServiceCtx context)
  118. {
  119. long layerFlags = context.RequestData.ReadInt64();
  120. long displayId = context.RequestData.ReadInt64();
  121. long parcelPtr = context.Request.ReceiveBuff[0].Position;
  122. // TODO: support multi display.
  123. Display disp = _displays.GetData<Display>((int)displayId);
  124. IBinder producer = context.Device.System.SurfaceFlinger.CreateLayer(0, out long layerId);
  125. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  126. Parcel parcel = new Parcel(0x28, 0x4);
  127. parcel.WriteObject(producer, "dispdrv\0");
  128. ReadOnlySpan<byte> parcelData = parcel.Finish();
  129. context.Memory.Write((ulong)parcelPtr, parcelData);
  130. context.ResponseData.Write(layerId);
  131. context.ResponseData.Write((long)parcelData.Length);
  132. return ResultCode.Success;
  133. }
  134. [Command(2031)]
  135. // DestroyStrayLayer(u64)
  136. public ResultCode DestroyStrayLayer(ServiceCtx context)
  137. {
  138. long layerId = context.RequestData.ReadInt64();
  139. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  140. return ResultCode.Success;
  141. }
  142. [Command(2101)]
  143. // SetLayerScalingMode(u32, u64)
  144. public ResultCode SetLayerScalingMode(ServiceCtx context)
  145. {
  146. int scalingMode = context.RequestData.ReadInt32();
  147. long layerId = context.RequestData.ReadInt64();
  148. return ResultCode.Success;
  149. }
  150. [Command(2102)] // 5.0.0+
  151. // ConvertScalingMode(unknown) -> unknown
  152. public ResultCode ConvertScalingMode(ServiceCtx context)
  153. {
  154. SourceScalingMode scalingMode = (SourceScalingMode)context.RequestData.ReadInt32();
  155. DestinationScalingMode? convertedScalingMode = ConvertScalingMode(scalingMode);
  156. if (!convertedScalingMode.HasValue)
  157. {
  158. // Scaling mode out of the range of valid values.
  159. return ResultCode.InvalidArguments;
  160. }
  161. if (scalingMode != SourceScalingMode.ScaleToWindow &&
  162. scalingMode != SourceScalingMode.PreserveAspectRatio)
  163. {
  164. // Invalid scaling mode specified.
  165. return ResultCode.InvalidScalingMode;
  166. }
  167. context.ResponseData.Write((ulong)convertedScalingMode);
  168. return ResultCode.Success;
  169. }
  170. private DestinationScalingMode? ConvertScalingMode(SourceScalingMode source)
  171. {
  172. switch (source)
  173. {
  174. case SourceScalingMode.None: return DestinationScalingMode.None;
  175. case SourceScalingMode.Freeze: return DestinationScalingMode.Freeze;
  176. case SourceScalingMode.ScaleAndCrop: return DestinationScalingMode.ScaleAndCrop;
  177. case SourceScalingMode.ScaleToWindow: return DestinationScalingMode.ScaleToWindow;
  178. case SourceScalingMode.PreserveAspectRatio: return DestinationScalingMode.PreserveAspectRatio;
  179. }
  180. return null;
  181. }
  182. [Command(2450)]
  183. // GetIndirectLayerImageMap(s64 width, s64 height, u64 handle, nn::applet::AppletResourceUserId, pid) -> (s64, s64, buffer<bytes, 0x46>)
  184. public ResultCode GetIndirectLayerImageMap(ServiceCtx context)
  185. {
  186. // The size of the layer buffer should be an aligned multiple of width * height
  187. // because it was created using GetIndirectLayerImageRequiredMemoryInfo as a guide.
  188. long layerBuffPosition = context.Request.ReceiveBuff[0].Position;
  189. long layerBuffSize = context.Request.ReceiveBuff[0].Size;
  190. // Fill the layer with zeros.
  191. context.Memory.Fill((ulong)layerBuffPosition, (ulong)layerBuffSize, 0x00);
  192. Logger.Stub?.PrintStub(LogClass.ServiceVi);
  193. return ResultCode.Success;
  194. }
  195. [Command(2460)]
  196. // GetIndirectLayerImageRequiredMemoryInfo(u64 width, u64 height) -> (u64 size, u64 alignment)
  197. public ResultCode GetIndirectLayerImageRequiredMemoryInfo(ServiceCtx context)
  198. {
  199. /*
  200. // Doesn't occur in our case.
  201. if (sizePtr == null || address_alignmentPtr == null)
  202. {
  203. return ResultCode.InvalidArguments;
  204. }
  205. */
  206. int width = (int)context.RequestData.ReadUInt64();
  207. int height = (int)context.RequestData.ReadUInt64();
  208. if (height < 0 || width < 0)
  209. {
  210. return ResultCode.InvalidLayerSize;
  211. }
  212. else
  213. {
  214. /*
  215. // Doesn't occur in our case.
  216. if (!service_initialized)
  217. {
  218. return ResultCode.InvalidArguments;
  219. }
  220. */
  221. const ulong defaultAlignment = 0x1000;
  222. const ulong defaultSize = 0x20000;
  223. // NOTE: The official service setup a A8B8G8R8 texture with a linear layout and then query its size.
  224. // As we don't need this texture on the emulator, we can just simplify this logic and directly
  225. // do a linear layout size calculation. (stride * height * bytePerPixel)
  226. int pitch = BitUtils.AlignUp(BitUtils.DivRoundUp(width * 32, 8), 64);
  227. int memorySize = pitch * BitUtils.AlignUp(height, 64);
  228. ulong requiredMemorySize = (ulong)BitUtils.AlignUp(memorySize, (int)defaultAlignment);
  229. ulong size = (requiredMemorySize + defaultSize - 1) / defaultSize * defaultSize;
  230. context.ResponseData.Write(size);
  231. context.ResponseData.Write(defaultAlignment);
  232. }
  233. return ResultCode.Success;
  234. }
  235. [Command(5202)]
  236. // GetDisplayVsyncEvent(u64) -> handle<copy>
  237. public ResultCode GetDisplayVSyncEvent(ServiceCtx context)
  238. {
  239. string name = GetDisplayName(context);
  240. if (_vsyncEventHandle == 0)
  241. {
  242. if (context.Process.HandleTable.GenerateHandle(context.Device.System.VsyncEvent.ReadableEvent, out _vsyncEventHandle) != KernelResult.Success)
  243. {
  244. throw new InvalidOperationException("Out of handles!");
  245. }
  246. }
  247. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_vsyncEventHandle);
  248. return ResultCode.Success;
  249. }
  250. private string GetDisplayName(ServiceCtx context)
  251. {
  252. string name = string.Empty;
  253. for (int index = 0; index < 8 &&
  254. context.RequestData.BaseStream.Position <
  255. context.RequestData.BaseStream.Length; index++)
  256. {
  257. byte chr = context.RequestData.ReadByte();
  258. if (chr >= 0x20 && chr < 0x7f)
  259. {
  260. name += (char)chr;
  261. }
  262. }
  263. return name;
  264. }
  265. }
  266. }