IApplicationDisplayService.cs 6.4 KB

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