IApplicationDisplayService.cs 17 KB

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