IApplicationDisplayService.cs 17 KB

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