IApplicationDisplayService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 Array64<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.Write(displayInfoBuffer + (ulong)(i * Unsafe.SizeOf<DisplayInfo>()), _displayInfo[i]);
  104. }
  105. context.ResponseData.Write(displayCount);
  106. return ResultCode.Success;
  107. }
  108. [CommandHipc(1010)]
  109. // OpenDisplay(nn::vi::DisplayName) -> u64 display_id
  110. public ResultCode OpenDisplay(ServiceCtx context)
  111. {
  112. string name = "";
  113. for (int index = 0; index < 8 && context.RequestData.BaseStream.Position < context.RequestData.BaseStream.Length; index++)
  114. {
  115. byte chr = context.RequestData.ReadByte();
  116. if (chr >= 0x20 && chr < 0x7f)
  117. {
  118. name += (char)chr;
  119. }
  120. }
  121. return OpenDisplayImpl(context, name);
  122. }
  123. [CommandHipc(1011)]
  124. // OpenDefaultDisplay() -> u64 display_id
  125. public ResultCode OpenDefaultDisplay(ServiceCtx context)
  126. {
  127. return OpenDisplayImpl(context, "Default");
  128. }
  129. private ResultCode OpenDisplayImpl(ServiceCtx context, string name)
  130. {
  131. if (name == "")
  132. {
  133. return ResultCode.InvalidValue;
  134. }
  135. int displayId = _displayInfo.FindIndex(display => Encoding.ASCII.GetString(display.Name.ToSpan()).Trim('\0') == name);
  136. if (displayId == -1)
  137. {
  138. return ResultCode.InvalidValue;
  139. }
  140. if (!_openDisplayInfo.TryAdd((ulong)displayId, _displayInfo[displayId]))
  141. {
  142. return ResultCode.AlreadyOpened;
  143. }
  144. context.ResponseData.Write((ulong)displayId);
  145. return ResultCode.Success;
  146. }
  147. [CommandHipc(1020)]
  148. // CloseDisplay(u64 display_id)
  149. public ResultCode CloseDisplay(ServiceCtx context)
  150. {
  151. ulong displayId = context.RequestData.ReadUInt64();
  152. if (!_openDisplayInfo.Remove(displayId))
  153. {
  154. return ResultCode.InvalidValue;
  155. }
  156. return ResultCode.Success;
  157. }
  158. [CommandHipc(1101)]
  159. // SetDisplayEnabled(u32 enabled_bool, u64 display_id)
  160. public ResultCode SetDisplayEnabled(ServiceCtx context)
  161. {
  162. // NOTE: Stubbed in original service.
  163. return ResultCode.Success;
  164. }
  165. [CommandHipc(1102)]
  166. // GetDisplayResolution(u64 display_id) -> (u64 width, u64 height)
  167. public ResultCode GetDisplayResolution(ServiceCtx context)
  168. {
  169. // NOTE: Not used in original service.
  170. // ulong displayId = context.RequestData.ReadUInt64();
  171. // NOTE: Returns ResultCode.InvalidArguments if width and height pointer are null, doesn't occur in our case.
  172. // NOTE: Values are hardcoded in original service.
  173. context.ResponseData.Write(1280UL); // Width
  174. context.ResponseData.Write(720UL); // Height
  175. return ResultCode.Success;
  176. }
  177. [CommandHipc(2020)]
  178. // OpenLayer(nn::vi::DisplayName, u64, nn::applet::AppletResourceUserId, pid) -> (u64, buffer<bytes, 6>)
  179. public ResultCode OpenLayer(ServiceCtx context)
  180. {
  181. // TODO: support multi display.
  182. byte[] displayName = context.RequestData.ReadBytes(0x40);
  183. long layerId = context.RequestData.ReadInt64();
  184. long userId = context.RequestData.ReadInt64();
  185. ulong parcelPtr = context.Request.ReceiveBuff[0].Position;
  186. IBinder producer = context.Device.System.SurfaceFlinger.OpenLayer(context.Request.HandleDesc.PId, layerId);
  187. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  188. Parcel parcel = new Parcel(0x28, 0x4);
  189. parcel.WriteObject(producer, "dispdrv\0");
  190. ReadOnlySpan<byte> parcelData = parcel.Finish();
  191. context.Memory.Write(parcelPtr, parcelData);
  192. context.ResponseData.Write((long)parcelData.Length);
  193. return ResultCode.Success;
  194. }
  195. [CommandHipc(2021)]
  196. // CloseLayer(u64)
  197. public ResultCode CloseLayer(ServiceCtx context)
  198. {
  199. long layerId = context.RequestData.ReadInt64();
  200. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  201. return ResultCode.Success;
  202. }
  203. [CommandHipc(2030)]
  204. // CreateStrayLayer(u32, u64) -> (u64, u64, buffer<bytes, 6>)
  205. public ResultCode CreateStrayLayer(ServiceCtx context)
  206. {
  207. long layerFlags = context.RequestData.ReadInt64();
  208. long displayId = context.RequestData.ReadInt64();
  209. ulong parcelPtr = context.Request.ReceiveBuff[0].Position;
  210. // TODO: support multi display.
  211. IBinder producer = context.Device.System.SurfaceFlinger.CreateLayer(0, out long layerId);
  212. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  213. Parcel parcel = new Parcel(0x28, 0x4);
  214. parcel.WriteObject(producer, "dispdrv\0");
  215. ReadOnlySpan<byte> parcelData = parcel.Finish();
  216. context.Memory.Write(parcelPtr, parcelData);
  217. context.ResponseData.Write(layerId);
  218. context.ResponseData.Write((long)parcelData.Length);
  219. return ResultCode.Success;
  220. }
  221. [CommandHipc(2031)]
  222. // DestroyStrayLayer(u64)
  223. public ResultCode DestroyStrayLayer(ServiceCtx context)
  224. {
  225. long layerId = context.RequestData.ReadInt64();
  226. context.Device.System.SurfaceFlinger.CloseLayer(layerId);
  227. return ResultCode.Success;
  228. }
  229. [CommandHipc(2101)]
  230. // SetLayerScalingMode(u32, u64)
  231. public ResultCode SetLayerScalingMode(ServiceCtx context)
  232. {
  233. /*
  234. uint sourceScalingMode = context.RequestData.ReadUInt32();
  235. ulong layerId = context.RequestData.ReadUInt64();
  236. */
  237. // NOTE: Original service converts SourceScalingMode to DestinationScalingMode but does nothing with the converted value.
  238. return ResultCode.Success;
  239. }
  240. [CommandHipc(2102)] // 5.0.0+
  241. // ConvertScalingMode(u32 source_scaling_mode) -> u64 destination_scaling_mode
  242. public ResultCode ConvertScalingMode(ServiceCtx context)
  243. {
  244. SourceScalingMode scalingMode = (SourceScalingMode)context.RequestData.ReadInt32();
  245. DestinationScalingMode? convertedScalingMode = scalingMode switch
  246. {
  247. SourceScalingMode.None => DestinationScalingMode.None,
  248. SourceScalingMode.Freeze => DestinationScalingMode.Freeze,
  249. SourceScalingMode.ScaleAndCrop => DestinationScalingMode.ScaleAndCrop,
  250. SourceScalingMode.ScaleToWindow => DestinationScalingMode.ScaleToWindow,
  251. SourceScalingMode.PreserveAspectRatio => DestinationScalingMode.PreserveAspectRatio,
  252. _ => null,
  253. };
  254. if (!convertedScalingMode.HasValue)
  255. {
  256. // Scaling mode out of the range of valid values.
  257. return ResultCode.InvalidArguments;
  258. }
  259. if (scalingMode != SourceScalingMode.ScaleToWindow && scalingMode != SourceScalingMode.PreserveAspectRatio)
  260. {
  261. // Invalid scaling mode specified.
  262. return ResultCode.InvalidScalingMode;
  263. }
  264. context.ResponseData.Write((ulong)convertedScalingMode);
  265. return ResultCode.Success;
  266. }
  267. [CommandHipc(2450)]
  268. // GetIndirectLayerImageMap(s64 width, s64 height, u64 handle, nn::applet::AppletResourceUserId, pid) -> (s64, s64, buffer<bytes, 0x46>)
  269. public ResultCode GetIndirectLayerImageMap(ServiceCtx context)
  270. {
  271. // The size of the layer buffer should be an aligned multiple of width * height
  272. // because it was created using GetIndirectLayerImageRequiredMemoryInfo as a guide.
  273. ulong layerBuffPosition = context.Request.ReceiveBuff[0].Position;
  274. ulong layerBuffSize = context.Request.ReceiveBuff[0].Size;
  275. // Fill the layer with zeros.
  276. context.Memory.Fill(layerBuffPosition, layerBuffSize, 0x00);
  277. Logger.Stub?.PrintStub(LogClass.ServiceVi);
  278. return ResultCode.Success;
  279. }
  280. [CommandHipc(2460)]
  281. // GetIndirectLayerImageRequiredMemoryInfo(u64 width, u64 height) -> (u64 size, u64 alignment)
  282. public ResultCode GetIndirectLayerImageRequiredMemoryInfo(ServiceCtx context)
  283. {
  284. /*
  285. // Doesn't occur in our case.
  286. if (sizePtr == null || address_alignmentPtr == null)
  287. {
  288. return ResultCode.InvalidArguments;
  289. }
  290. */
  291. int width = (int)context.RequestData.ReadUInt64();
  292. int height = (int)context.RequestData.ReadUInt64();
  293. if (height < 0 || width < 0)
  294. {
  295. return ResultCode.InvalidLayerSize;
  296. }
  297. else
  298. {
  299. /*
  300. // Doesn't occur in our case.
  301. if (!service_initialized)
  302. {
  303. return ResultCode.InvalidArguments;
  304. }
  305. */
  306. const ulong defaultAlignment = 0x1000;
  307. const ulong defaultSize = 0x20000;
  308. // NOTE: The official service setup a A8B8G8R8 texture with a linear layout and then query its size.
  309. // As we don't need this texture on the emulator, we can just simplify this logic and directly
  310. // do a linear layout size calculation. (stride * height * bytePerPixel)
  311. int pitch = BitUtils.AlignUp(BitUtils.DivRoundUp(width * 32, 8), 64);
  312. int memorySize = pitch * BitUtils.AlignUp(height, 64);
  313. ulong requiredMemorySize = (ulong)BitUtils.AlignUp(memorySize, (int)defaultAlignment);
  314. ulong size = (requiredMemorySize + defaultSize - 1) / defaultSize * defaultSize;
  315. context.ResponseData.Write(size);
  316. context.ResponseData.Write(defaultAlignment);
  317. }
  318. return ResultCode.Success;
  319. }
  320. [CommandHipc(5202)]
  321. // GetDisplayVsyncEvent(u64) -> handle<copy>
  322. public ResultCode GetDisplayVSyncEvent(ServiceCtx context)
  323. {
  324. ulong displayId = context.RequestData.ReadUInt64();
  325. if (!_openDisplayInfo.ContainsKey(displayId))
  326. {
  327. return ResultCode.InvalidValue;
  328. }
  329. if (_vsyncEventHandle == 0)
  330. {
  331. if (context.Process.HandleTable.GenerateHandle(context.Device.System.VsyncEvent.ReadableEvent, out _vsyncEventHandle) != KernelResult.Success)
  332. {
  333. throw new InvalidOperationException("Out of handles!");
  334. }
  335. }
  336. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_vsyncEventHandle);
  337. return ResultCode.Success;
  338. }
  339. }
  340. }