IApplicationDisplayService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. IBinder producer = context.Device.System.SurfaceFlinger.OpenLayer(context.Request.HandleDesc.PId, layerId);
  193. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  194. Parcel parcel = new Parcel(0x28, 0x4);
  195. parcel.WriteObject(producer, "dispdrv\0");
  196. ReadOnlySpan<byte> parcelData = parcel.Finish();
  197. context.Memory.Write(parcelPtr, parcelData);
  198. context.ResponseData.Write((long)parcelData.Length);
  199. return ResultCode.Success;
  200. }
  201. [CommandHipc(2021)]
  202. // CloseLayer(u64)
  203. public ResultCode CloseLayer(ServiceCtx context)
  204. {
  205. long layerId = context.RequestData.ReadInt64();
  206. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  207. return ResultCode.Success;
  208. }
  209. [CommandHipc(2030)]
  210. // CreateStrayLayer(u32, u64) -> (u64, u64, buffer<bytes, 6>)
  211. public ResultCode CreateStrayLayer(ServiceCtx context)
  212. {
  213. long layerFlags = context.RequestData.ReadInt64();
  214. long displayId = context.RequestData.ReadInt64();
  215. ulong parcelPtr = context.Request.ReceiveBuff[0].Position;
  216. // TODO: support multi display.
  217. IBinder producer = context.Device.System.SurfaceFlinger.CreateLayer(0, out long layerId);
  218. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  219. Parcel parcel = new Parcel(0x28, 0x4);
  220. parcel.WriteObject(producer, "dispdrv\0");
  221. ReadOnlySpan<byte> parcelData = parcel.Finish();
  222. context.Memory.Write(parcelPtr, parcelData);
  223. context.ResponseData.Write(layerId);
  224. context.ResponseData.Write((long)parcelData.Length);
  225. return ResultCode.Success;
  226. }
  227. [CommandHipc(2031)]
  228. // DestroyStrayLayer(u64)
  229. public ResultCode DestroyStrayLayer(ServiceCtx context)
  230. {
  231. long layerId = context.RequestData.ReadInt64();
  232. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  233. return ResultCode.Success;
  234. }
  235. [CommandHipc(2101)]
  236. // SetLayerScalingMode(u32, u64)
  237. public ResultCode SetLayerScalingMode(ServiceCtx context)
  238. {
  239. /*
  240. uint sourceScalingMode = context.RequestData.ReadUInt32();
  241. ulong layerId = context.RequestData.ReadUInt64();
  242. */
  243. // NOTE: Original service converts SourceScalingMode to DestinationScalingMode but does nothing with the converted value.
  244. return ResultCode.Success;
  245. }
  246. [CommandHipc(2102)] // 5.0.0+
  247. // ConvertScalingMode(u32 source_scaling_mode) -> u64 destination_scaling_mode
  248. public ResultCode ConvertScalingMode(ServiceCtx context)
  249. {
  250. SourceScalingMode scalingMode = (SourceScalingMode)context.RequestData.ReadInt32();
  251. DestinationScalingMode? convertedScalingMode = scalingMode switch
  252. {
  253. SourceScalingMode.None => DestinationScalingMode.None,
  254. SourceScalingMode.Freeze => DestinationScalingMode.Freeze,
  255. SourceScalingMode.ScaleAndCrop => DestinationScalingMode.ScaleAndCrop,
  256. SourceScalingMode.ScaleToWindow => DestinationScalingMode.ScaleToWindow,
  257. SourceScalingMode.PreserveAspectRatio => DestinationScalingMode.PreserveAspectRatio,
  258. _ => null,
  259. };
  260. if (!convertedScalingMode.HasValue)
  261. {
  262. // Scaling mode out of the range of valid values.
  263. return ResultCode.InvalidArguments;
  264. }
  265. if (scalingMode != SourceScalingMode.ScaleToWindow && scalingMode != SourceScalingMode.PreserveAspectRatio)
  266. {
  267. // Invalid scaling mode specified.
  268. return ResultCode.InvalidScalingMode;
  269. }
  270. context.ResponseData.Write((ulong)convertedScalingMode);
  271. return ResultCode.Success;
  272. }
  273. private ulong GetA8B8G8R8LayerSize(int width, int height, out int pitch, out int alignment)
  274. {
  275. const int defaultAlignment = 0x1000;
  276. const ulong defaultSize = 0x20000;
  277. alignment = defaultAlignment;
  278. pitch = BitUtils.AlignUp(BitUtils.DivRoundUp(width * 32, 8), 64);
  279. int memorySize = pitch * BitUtils.AlignUp(height, 64);
  280. ulong requiredMemorySize = (ulong)BitUtils.AlignUp(memorySize, alignment);
  281. return (requiredMemorySize + defaultSize - 1) / defaultSize * defaultSize;
  282. }
  283. [CommandHipc(2450)]
  284. // GetIndirectLayerImageMap(s64 width, s64 height, u64 handle, nn::applet::AppletResourceUserId, pid) -> (s64, s64, buffer<bytes, 0x46>)
  285. public ResultCode GetIndirectLayerImageMap(ServiceCtx context)
  286. {
  287. // The size of the layer buffer should be an aligned multiple of width * height
  288. // because it was created using GetIndirectLayerImageRequiredMemoryInfo as a guide.
  289. long layerWidth = context.RequestData.ReadInt64();
  290. long layerHeight = context.RequestData.ReadInt64();
  291. long layerHandle = context.RequestData.ReadInt64();
  292. ulong layerBuffPosition = context.Request.ReceiveBuff[0].Position;
  293. ulong layerBuffSize = context.Request.ReceiveBuff[0].Size;
  294. // Get the pitch of the layer that is necessary to render correctly.
  295. ulong size = GetA8B8G8R8LayerSize((int)layerWidth, (int)layerHeight, out int pitch, out _);
  296. Debug.Assert(layerBuffSize == size);
  297. RenderingSurfaceInfo surfaceInfo = new RenderingSurfaceInfo(ColorFormat.A8B8G8R8, (uint)layerWidth, (uint)layerHeight, (uint)pitch, (uint)layerBuffSize);
  298. // Get the applet associated with the handle.
  299. object appletObject = context.Device.System.AppletState.IndirectLayerHandles.GetData((int)layerHandle);
  300. if (appletObject == null)
  301. {
  302. Logger.Error?.Print(LogClass.ServiceVi, $"Indirect layer handle {layerHandle} does not match any applet");
  303. return ResultCode.Success;
  304. }
  305. Debug.Assert(appletObject is IApplet);
  306. IApplet applet = appletObject as IApplet;
  307. if (!applet.DrawTo(surfaceInfo, context.Memory, layerBuffPosition))
  308. {
  309. Logger.Warning?.Print(LogClass.ServiceVi, $"Applet did not draw on indirect layer handle {layerHandle}");
  310. return ResultCode.Success;
  311. }
  312. context.ResponseData.Write(layerWidth);
  313. context.ResponseData.Write(layerHeight);
  314. return ResultCode.Success;
  315. }
  316. [CommandHipc(2460)]
  317. // GetIndirectLayerImageRequiredMemoryInfo(u64 width, u64 height) -> (u64 size, u64 alignment)
  318. public ResultCode GetIndirectLayerImageRequiredMemoryInfo(ServiceCtx context)
  319. {
  320. /*
  321. // Doesn't occur in our case.
  322. if (sizePtr == null || address_alignmentPtr == null)
  323. {
  324. return ResultCode.InvalidArguments;
  325. }
  326. */
  327. int width = (int)context.RequestData.ReadUInt64();
  328. int height = (int)context.RequestData.ReadUInt64();
  329. if (height < 0 || width < 0)
  330. {
  331. return ResultCode.InvalidLayerSize;
  332. }
  333. else
  334. {
  335. /*
  336. // Doesn't occur in our case.
  337. if (!service_initialized)
  338. {
  339. return ResultCode.InvalidArguments;
  340. }
  341. */
  342. // NOTE: The official service setup a A8B8G8R8 texture with a linear layout and then query its size.
  343. // As we don't need this texture on the emulator, we can just simplify this logic and directly
  344. // do a linear layout size calculation. (stride * height * bytePerPixel)
  345. ulong size = GetA8B8G8R8LayerSize(width, height, out int pitch, out int alignment);
  346. context.ResponseData.Write(size);
  347. context.ResponseData.Write(alignment);
  348. }
  349. return ResultCode.Success;
  350. }
  351. [CommandHipc(5202)]
  352. // GetDisplayVsyncEvent(u64) -> handle<copy>
  353. public ResultCode GetDisplayVSyncEvent(ServiceCtx context)
  354. {
  355. ulong displayId = context.RequestData.ReadUInt64();
  356. if (!_openDisplays.TryGetValue(displayId, out DisplayState displayState))
  357. {
  358. return ResultCode.InvalidValue;
  359. }
  360. if (displayState.RetrievedEventsCount > 0)
  361. {
  362. return ResultCode.PermissionDenied;
  363. }
  364. if (_vsyncEventHandle == 0)
  365. {
  366. if (context.Process.HandleTable.GenerateHandle(context.Device.System.VsyncEvent.ReadableEvent, out _vsyncEventHandle) != KernelResult.Success)
  367. {
  368. throw new InvalidOperationException("Out of handles!");
  369. }
  370. }
  371. displayState.RetrievedEventsCount++;
  372. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_vsyncEventHandle);
  373. return ResultCode.Success;
  374. }
  375. }
  376. }