IApplicationDisplayService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Common.Memory;
  4. using Ryujinx.Cpu;
  5. using Ryujinx.HLE.HOS.Ipc;
  6. using Ryujinx.HLE.HOS.Kernel.Common;
  7. using Ryujinx.HLE.HOS.Services.SurfaceFlinger;
  8. using Ryujinx.HLE.HOS.Services.Vi.RootService.ApplicationDisplayService;
  9. using Ryujinx.HLE.HOS.Services.Vi.RootService.ApplicationDisplayService.Types;
  10. using Ryujinx.HLE.HOS.Services.Vi.Types;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Runtime.CompilerServices;
  14. using System.Text;
  15. namespace Ryujinx.HLE.HOS.Services.Vi.RootService
  16. {
  17. class IApplicationDisplayService : IpcService
  18. {
  19. private readonly ViServiceType _serviceType;
  20. private readonly List<DisplayInfo> _displayInfo;
  21. private readonly Dictionary<ulong, DisplayInfo> _openDisplayInfo;
  22. private int _vsyncEventHandle;
  23. public IApplicationDisplayService(ViServiceType serviceType)
  24. {
  25. _serviceType = serviceType;
  26. _displayInfo = new List<DisplayInfo>();
  27. _openDisplayInfo = new Dictionary<ulong, DisplayInfo>();
  28. void AddDisplayInfo(string name, bool layerLimitEnabled, ulong layerLimitMax, ulong width, ulong height)
  29. {
  30. DisplayInfo displayInfo = new DisplayInfo()
  31. {
  32. Name = new Array40<byte>(),
  33. LayerLimitEnabled = layerLimitEnabled,
  34. Padding = new Array7<byte>(),
  35. LayerLimitMax = layerLimitMax,
  36. Width = width,
  37. Height = height
  38. };
  39. Encoding.ASCII.GetBytes(name).AsSpan().CopyTo(displayInfo.Name.ToSpan());
  40. _displayInfo.Add(displayInfo);
  41. }
  42. AddDisplayInfo("Default", true, 1, 1920, 1080);
  43. AddDisplayInfo("External", true, 1, 1920, 1080);
  44. AddDisplayInfo("Edid", true, 1, 0, 0);
  45. AddDisplayInfo("Internal", true, 1, 1920, 1080);
  46. AddDisplayInfo("Null", false, 0, 1920, 1080);
  47. }
  48. [CommandHipc(100)]
  49. // GetRelayService() -> object<nns::hosbinder::IHOSBinderDriver>
  50. public ResultCode GetRelayService(ServiceCtx context)
  51. {
  52. // FIXME: Should be _serviceType != ViServiceType.Application but guests crashes if we do this check.
  53. if (_serviceType > ViServiceType.System)
  54. {
  55. return ResultCode.InvalidRange;
  56. }
  57. MakeObject(context, new HOSBinderDriverServer());
  58. return ResultCode.Success;
  59. }
  60. [CommandHipc(101)]
  61. // GetSystemDisplayService() -> object<nn::visrv::sf::ISystemDisplayService>
  62. public ResultCode GetSystemDisplayService(ServiceCtx context)
  63. {
  64. // FIXME: Should be _serviceType == ViServiceType.System but guests crashes if we do this check.
  65. if (_serviceType > ViServiceType.System)
  66. {
  67. return ResultCode.InvalidRange;
  68. }
  69. MakeObject(context, new ISystemDisplayService(this));
  70. return ResultCode.Success;
  71. }
  72. [CommandHipc(102)]
  73. // GetManagerDisplayService() -> object<nn::visrv::sf::IManagerDisplayService>
  74. public ResultCode GetManagerDisplayService(ServiceCtx context)
  75. {
  76. if (_serviceType > ViServiceType.System)
  77. {
  78. return ResultCode.InvalidRange;
  79. }
  80. MakeObject(context, new IManagerDisplayService(this));
  81. return ResultCode.Success;
  82. }
  83. [CommandHipc(103)] // 2.0.0+
  84. // GetIndirectDisplayTransactionService() -> object<nns::hosbinder::IHOSBinderDriver>
  85. public ResultCode GetIndirectDisplayTransactionService(ServiceCtx context)
  86. {
  87. if (_serviceType > ViServiceType.System)
  88. {
  89. return ResultCode.InvalidRange;
  90. }
  91. MakeObject(context, new HOSBinderDriverServer());
  92. return ResultCode.Success;
  93. }
  94. [CommandHipc(1000)]
  95. // ListDisplays() -> (u64 count, buffer<nn::vi::DisplayInfo, 6>)
  96. public ResultCode ListDisplays(ServiceCtx context)
  97. {
  98. ulong displayInfoBuffer = context.Request.ReceiveBuff[0].Position;
  99. // TODO: Determine when more than one display is needed.
  100. ulong displayCount = 1;
  101. for (int i = 0; i < (int)displayCount; i++)
  102. {
  103. context.Memory.Fill(displayInfoBuffer + (ulong)(i * Unsafe.SizeOf<DisplayInfo>()), (ulong)(Unsafe.SizeOf<DisplayInfo>()), 0x00);
  104. context.Memory.Write(displayInfoBuffer, _displayInfo[i]);
  105. }
  106. context.ResponseData.Write(displayCount);
  107. return ResultCode.Success;
  108. }
  109. [CommandHipc(1010)]
  110. // OpenDisplay(nn::vi::DisplayName) -> u64 display_id
  111. public ResultCode OpenDisplay(ServiceCtx context)
  112. {
  113. string name = "";
  114. for (int index = 0; index < 8 && context.RequestData.BaseStream.Position < context.RequestData.BaseStream.Length; index++)
  115. {
  116. byte chr = context.RequestData.ReadByte();
  117. if (chr >= 0x20 && chr < 0x7f)
  118. {
  119. name += (char)chr;
  120. }
  121. }
  122. return OpenDisplayImpl(context, name);
  123. }
  124. [CommandHipc(1011)]
  125. // OpenDefaultDisplay() -> u64 display_id
  126. public ResultCode OpenDefaultDisplay(ServiceCtx context)
  127. {
  128. return OpenDisplayImpl(context, "Default");
  129. }
  130. private ResultCode OpenDisplayImpl(ServiceCtx context, string name)
  131. {
  132. if (name == "")
  133. {
  134. return ResultCode.InvalidValue;
  135. }
  136. int displayId = _displayInfo.FindIndex(display => Encoding.ASCII.GetString(display.Name.ToSpan()).Trim('\0') == name);
  137. if (displayId == -1)
  138. {
  139. return ResultCode.InvalidValue;
  140. }
  141. if (!_openDisplayInfo.TryAdd((ulong)displayId, _displayInfo[displayId]))
  142. {
  143. return ResultCode.AlreadyOpened;
  144. }
  145. context.ResponseData.Write((ulong)displayId);
  146. return ResultCode.Success;
  147. }
  148. [CommandHipc(1020)]
  149. // CloseDisplay(u64 display_id)
  150. public ResultCode CloseDisplay(ServiceCtx context)
  151. {
  152. ulong displayId = context.RequestData.ReadUInt64();
  153. if (!_openDisplayInfo.Remove(displayId))
  154. {
  155. return ResultCode.InvalidValue;
  156. }
  157. return ResultCode.Success;
  158. }
  159. [CommandHipc(1101)]
  160. // SetDisplayEnabled(u32 enabled_bool, u64 display_id)
  161. public ResultCode SetDisplayEnabled(ServiceCtx context)
  162. {
  163. // NOTE: Stubbed in original service.
  164. return ResultCode.Success;
  165. }
  166. [CommandHipc(1102)]
  167. // GetDisplayResolution(u64 display_id) -> (u64 width, u64 height)
  168. public ResultCode GetDisplayResolution(ServiceCtx context)
  169. {
  170. // NOTE: Not used in original service.
  171. // ulong displayId = context.RequestData.ReadUInt64();
  172. // NOTE: Returns ResultCode.InvalidArguments if width and height pointer are null, doesn't occur in our case.
  173. // NOTE: Values are hardcoded in original service.
  174. context.ResponseData.Write(1280UL); // Width
  175. context.ResponseData.Write(720UL); // Height
  176. return ResultCode.Success;
  177. }
  178. [CommandHipc(2020)]
  179. // OpenLayer(nn::vi::DisplayName, u64, nn::applet::AppletResourceUserId, pid) -> (u64, buffer<bytes, 6>)
  180. public ResultCode OpenLayer(ServiceCtx context)
  181. {
  182. // TODO: support multi display.
  183. byte[] displayName = context.RequestData.ReadBytes(0x40);
  184. long layerId = context.RequestData.ReadInt64();
  185. long userId = context.RequestData.ReadInt64();
  186. ulong parcelPtr = context.Request.ReceiveBuff[0].Position;
  187. IBinder producer = context.Device.System.SurfaceFlinger.OpenLayer(context.Request.HandleDesc.PId, layerId);
  188. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  189. Parcel parcel = new Parcel(0x28, 0x4);
  190. parcel.WriteObject(producer, "dispdrv\0");
  191. ReadOnlySpan<byte> parcelData = parcel.Finish();
  192. context.Memory.Write(parcelPtr, parcelData);
  193. context.ResponseData.Write((long)parcelData.Length);
  194. return ResultCode.Success;
  195. }
  196. [CommandHipc(2021)]
  197. // CloseLayer(u64)
  198. public ResultCode CloseLayer(ServiceCtx context)
  199. {
  200. long layerId = context.RequestData.ReadInt64();
  201. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  202. return ResultCode.Success;
  203. }
  204. [CommandHipc(2030)]
  205. // CreateStrayLayer(u32, u64) -> (u64, u64, buffer<bytes, 6>)
  206. public ResultCode CreateStrayLayer(ServiceCtx context)
  207. {
  208. long layerFlags = context.RequestData.ReadInt64();
  209. long displayId = context.RequestData.ReadInt64();
  210. ulong parcelPtr = context.Request.ReceiveBuff[0].Position;
  211. // TODO: support multi display.
  212. IBinder producer = context.Device.System.SurfaceFlinger.CreateLayer(0, out long layerId);
  213. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  214. Parcel parcel = new Parcel(0x28, 0x4);
  215. parcel.WriteObject(producer, "dispdrv\0");
  216. ReadOnlySpan<byte> parcelData = parcel.Finish();
  217. context.Memory.Write(parcelPtr, parcelData);
  218. context.ResponseData.Write(layerId);
  219. context.ResponseData.Write((long)parcelData.Length);
  220. return ResultCode.Success;
  221. }
  222. [CommandHipc(2031)]
  223. // DestroyStrayLayer(u64)
  224. public ResultCode DestroyStrayLayer(ServiceCtx context)
  225. {
  226. long layerId = context.RequestData.ReadInt64();
  227. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  228. return ResultCode.Success;
  229. }
  230. [CommandHipc(2101)]
  231. // SetLayerScalingMode(u32, u64)
  232. public ResultCode SetLayerScalingMode(ServiceCtx context)
  233. {
  234. /*
  235. uint sourceScalingMode = context.RequestData.ReadUInt32();
  236. ulong layerId = context.RequestData.ReadUInt64();
  237. */
  238. // NOTE: Original service converts SourceScalingMode to DestinationScalingMode but does nothing with the converted value.
  239. return ResultCode.Success;
  240. }
  241. [CommandHipc(2102)] // 5.0.0+
  242. // ConvertScalingMode(u32 source_scaling_mode) -> u64 destination_scaling_mode
  243. public ResultCode ConvertScalingMode(ServiceCtx context)
  244. {
  245. SourceScalingMode scalingMode = (SourceScalingMode)context.RequestData.ReadInt32();
  246. DestinationScalingMode? convertedScalingMode = scalingMode switch
  247. {
  248. SourceScalingMode.None => DestinationScalingMode.None,
  249. SourceScalingMode.Freeze => DestinationScalingMode.Freeze,
  250. SourceScalingMode.ScaleAndCrop => DestinationScalingMode.ScaleAndCrop,
  251. SourceScalingMode.ScaleToWindow => DestinationScalingMode.ScaleToWindow,
  252. SourceScalingMode.PreserveAspectRatio => DestinationScalingMode.PreserveAspectRatio,
  253. _ => null,
  254. };
  255. if (!convertedScalingMode.HasValue)
  256. {
  257. // Scaling mode out of the range of valid values.
  258. return ResultCode.InvalidArguments;
  259. }
  260. if (scalingMode != SourceScalingMode.ScaleToWindow && scalingMode != SourceScalingMode.PreserveAspectRatio)
  261. {
  262. // Invalid scaling mode specified.
  263. return ResultCode.InvalidScalingMode;
  264. }
  265. context.ResponseData.Write((ulong)convertedScalingMode);
  266. return ResultCode.Success;
  267. }
  268. [CommandHipc(2450)]
  269. // GetIndirectLayerImageMap(s64 width, s64 height, u64 handle, nn::applet::AppletResourceUserId, pid) -> (s64, s64, buffer<bytes, 0x46>)
  270. public ResultCode GetIndirectLayerImageMap(ServiceCtx context)
  271. {
  272. // The size of the layer buffer should be an aligned multiple of width * height
  273. // because it was created using GetIndirectLayerImageRequiredMemoryInfo as a guide.
  274. ulong layerBuffPosition = context.Request.ReceiveBuff[0].Position;
  275. ulong layerBuffSize = context.Request.ReceiveBuff[0].Size;
  276. // Fill the layer with zeros.
  277. context.Memory.Fill(layerBuffPosition, layerBuffSize, 0x00);
  278. Logger.Stub?.PrintStub(LogClass.ServiceVi);
  279. return ResultCode.Success;
  280. }
  281. [CommandHipc(2460)]
  282. // GetIndirectLayerImageRequiredMemoryInfo(u64 width, u64 height) -> (u64 size, u64 alignment)
  283. public ResultCode GetIndirectLayerImageRequiredMemoryInfo(ServiceCtx context)
  284. {
  285. /*
  286. // Doesn't occur in our case.
  287. if (sizePtr == null || address_alignmentPtr == null)
  288. {
  289. return ResultCode.InvalidArguments;
  290. }
  291. */
  292. int width = (int)context.RequestData.ReadUInt64();
  293. int height = (int)context.RequestData.ReadUInt64();
  294. if (height < 0 || width < 0)
  295. {
  296. return ResultCode.InvalidLayerSize;
  297. }
  298. else
  299. {
  300. /*
  301. // Doesn't occur in our case.
  302. if (!service_initialized)
  303. {
  304. return ResultCode.InvalidArguments;
  305. }
  306. */
  307. const ulong defaultAlignment = 0x1000;
  308. const ulong defaultSize = 0x20000;
  309. // NOTE: The official service setup a A8B8G8R8 texture with a linear layout and then query its size.
  310. // As we don't need this texture on the emulator, we can just simplify this logic and directly
  311. // do a linear layout size calculation. (stride * height * bytePerPixel)
  312. int pitch = BitUtils.AlignUp(BitUtils.DivRoundUp(width * 32, 8), 64);
  313. int memorySize = pitch * BitUtils.AlignUp(height, 64);
  314. ulong requiredMemorySize = (ulong)BitUtils.AlignUp(memorySize, (int)defaultAlignment);
  315. ulong size = (requiredMemorySize + defaultSize - 1) / defaultSize * defaultSize;
  316. context.ResponseData.Write(size);
  317. context.ResponseData.Write(defaultAlignment);
  318. }
  319. return ResultCode.Success;
  320. }
  321. [CommandHipc(5202)]
  322. // GetDisplayVsyncEvent(u64) -> handle<copy>
  323. public ResultCode GetDisplayVSyncEvent(ServiceCtx context)
  324. {
  325. ulong displayId = context.RequestData.ReadUInt64();
  326. if (!_openDisplayInfo.ContainsKey(displayId))
  327. {
  328. return ResultCode.InvalidValue;
  329. }
  330. if (_vsyncEventHandle == 0)
  331. {
  332. if (context.Process.HandleTable.GenerateHandle(context.Device.System.VsyncEvent.ReadableEvent, out _vsyncEventHandle) != KernelResult.Success)
  333. {
  334. throw new InvalidOperationException("Out of handles!");
  335. }
  336. }
  337. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_vsyncEventHandle);
  338. return ResultCode.Success;
  339. }
  340. }
  341. }