IApplicationDisplayService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.OsHle.Handles;
  3. using Ryujinx.Core.OsHle.Ipc;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using static Ryujinx.Core.OsHle.Objects.Android.Parcel;
  7. using static Ryujinx.Core.OsHle.Objects.ObjHelper;
  8. namespace Ryujinx.Core.OsHle.Objects.Vi
  9. {
  10. class IApplicationDisplayService : IIpcInterface
  11. {
  12. private Dictionary<int, ServiceProcessRequest> m_Commands;
  13. public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  14. public IApplicationDisplayService()
  15. {
  16. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  17. {
  18. { 100, GetRelayService },
  19. { 101, GetSystemDisplayService },
  20. { 102, GetManagerDisplayService },
  21. { 103, GetIndirectDisplayTransactionService },
  22. { 1010, OpenDisplay },
  23. { 2020, OpenLayer },
  24. { 2030, CreateStrayLayer },
  25. { 2101, SetLayerScalingMode },
  26. { 5202, GetDisplayVSyncEvent }
  27. };
  28. }
  29. public long GetRelayService(ServiceCtx Context)
  30. {
  31. MakeObject(Context, new IHOSBinderDriver());
  32. return 0;
  33. }
  34. public long GetSystemDisplayService(ServiceCtx Context)
  35. {
  36. MakeObject(Context, new ISystemDisplayService());
  37. return 0;
  38. }
  39. public long GetManagerDisplayService(ServiceCtx Context)
  40. {
  41. MakeObject(Context, new IManagerDisplayService());
  42. return 0;
  43. }
  44. public long GetIndirectDisplayTransactionService(ServiceCtx Context)
  45. {
  46. MakeObject(Context, new IHOSBinderDriver());
  47. return 0;
  48. }
  49. public long OpenDisplay(ServiceCtx Context)
  50. {
  51. string Name = GetDisplayName(Context);
  52. long DisplayId = Context.Ns.Os.Displays.GenerateId(new Display(Name));
  53. Context.ResponseData.Write(DisplayId);
  54. return 0;
  55. }
  56. public long OpenLayer(ServiceCtx Context)
  57. {
  58. long LayerId = Context.RequestData.ReadInt64();
  59. long UserId = Context.RequestData.ReadInt64();
  60. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  61. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  62. AMemoryHelper.WriteBytes(Context.Memory, ParcelPtr, Parcel);
  63. Context.ResponseData.Write((long)Parcel.Length);
  64. return 0;
  65. }
  66. public long CreateStrayLayer(ServiceCtx Context)
  67. {
  68. long LayerFlags = Context.RequestData.ReadInt64();
  69. long DisplayId = Context.RequestData.ReadInt64();
  70. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  71. Display Disp = Context.Ns.Os.Displays.GetData<Display>((int)DisplayId);
  72. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  73. AMemoryHelper.WriteBytes(Context.Memory, ParcelPtr, Parcel);
  74. Context.ResponseData.Write(0L);
  75. Context.ResponseData.Write((long)Parcel.Length);
  76. return 0;
  77. }
  78. public long SetLayerScalingMode(ServiceCtx Context)
  79. {
  80. int ScalingMode = Context.RequestData.ReadInt32();
  81. long Unknown = Context.RequestData.ReadInt64();
  82. return 0;
  83. }
  84. public long GetDisplayVSyncEvent(ServiceCtx Context)
  85. {
  86. string Name = GetDisplayName(Context);
  87. int Handle = Context.Ns.Os.Handles.GenerateId(new HEvent());
  88. Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
  89. return 0;
  90. }
  91. private byte[] MakeIGraphicsBufferProducer(long BasePtr)
  92. {
  93. long Id = 0x20;
  94. long CookiePtr = 0L;
  95. using (MemoryStream MS = new MemoryStream())
  96. {
  97. BinaryWriter Writer = new BinaryWriter(MS);
  98. //flat_binder_object (size is 0x28)
  99. Writer.Write(2); //Type (BINDER_TYPE_WEAK_BINDER)
  100. Writer.Write(0); //Flags
  101. Writer.Write((int)(Id >> 0));
  102. Writer.Write((int)(Id >> 32));
  103. Writer.Write((int)(CookiePtr >> 0));
  104. Writer.Write((int)(CookiePtr >> 32));
  105. Writer.Write((byte)'d');
  106. Writer.Write((byte)'i');
  107. Writer.Write((byte)'s');
  108. Writer.Write((byte)'p');
  109. Writer.Write((byte)'d');
  110. Writer.Write((byte)'r');
  111. Writer.Write((byte)'v');
  112. Writer.Write((byte)'\0');
  113. Writer.Write(0L); //Pad
  114. return MakeParcel(MS.ToArray(), new byte[] { 0, 0, 0, 0 });
  115. }
  116. }
  117. private string GetDisplayName(ServiceCtx Context)
  118. {
  119. string Name = string.Empty;
  120. for (int Index = 0; Index < 8 &&
  121. Context.RequestData.BaseStream.Position <
  122. Context.RequestData.BaseStream.Length; Index++)
  123. {
  124. byte Chr = Context.RequestData.ReadByte();
  125. if (Chr >= 0x20 && Chr < 0x7f)
  126. {
  127. Name += (char)Chr;
  128. }
  129. }
  130. return Name;
  131. }
  132. }
  133. }