IApplicationDisplayService.cs 17 KB

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