IApplicationDisplayService.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. { 2020, OpenLayer },
  24. { 2021, CloseLayer },
  25. { 2030, CreateStrayLayer },
  26. { 2031, DestroyStrayLayer },
  27. { 2101, SetLayerScalingMode },
  28. { 5202, GetDisplayVSyncEvent }
  29. };
  30. Displays = new IdDictionary();
  31. }
  32. public long GetRelayService(ServiceCtx Context)
  33. {
  34. MakeObject(Context, new IHOSBinderDriver(Context.Ns.Gpu.Renderer));
  35. return 0;
  36. }
  37. public long GetSystemDisplayService(ServiceCtx Context)
  38. {
  39. MakeObject(Context, new ISystemDisplayService());
  40. return 0;
  41. }
  42. public long GetManagerDisplayService(ServiceCtx Context)
  43. {
  44. MakeObject(Context, new IManagerDisplayService());
  45. return 0;
  46. }
  47. public long GetIndirectDisplayTransactionService(ServiceCtx Context)
  48. {
  49. MakeObject(Context, new IHOSBinderDriver(Context.Ns.Gpu.Renderer));
  50. return 0;
  51. }
  52. public long OpenDisplay(ServiceCtx Context)
  53. {
  54. string Name = GetDisplayName(Context);
  55. long DisplayId = Displays.Add(new Display(Name));
  56. Context.ResponseData.Write(DisplayId);
  57. return 0;
  58. }
  59. public long CloseDisplay(ServiceCtx Context)
  60. {
  61. int DisplayId = Context.RequestData.ReadInt32();
  62. Displays.Delete(DisplayId);
  63. return 0;
  64. }
  65. public long OpenLayer(ServiceCtx Context)
  66. {
  67. long LayerId = Context.RequestData.ReadInt64();
  68. long UserId = Context.RequestData.ReadInt64();
  69. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  70. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  71. AMemoryHelper.WriteBytes(Context.Memory, ParcelPtr, Parcel);
  72. Context.ResponseData.Write((long)Parcel.Length);
  73. return 0;
  74. }
  75. public long CloseLayer(ServiceCtx Context)
  76. {
  77. long LayerId = Context.RequestData.ReadInt64();
  78. return 0;
  79. }
  80. public long CreateStrayLayer(ServiceCtx Context)
  81. {
  82. long LayerFlags = Context.RequestData.ReadInt64();
  83. long DisplayId = Context.RequestData.ReadInt64();
  84. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  85. Display Disp = Displays.GetData<Display>((int)DisplayId);
  86. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  87. AMemoryHelper.WriteBytes(Context.Memory, ParcelPtr, Parcel);
  88. Context.ResponseData.Write(0L);
  89. Context.ResponseData.Write((long)Parcel.Length);
  90. return 0;
  91. }
  92. public long DestroyStrayLayer(ServiceCtx Context)
  93. {
  94. return 0;
  95. }
  96. public long SetLayerScalingMode(ServiceCtx Context)
  97. {
  98. int ScalingMode = Context.RequestData.ReadInt32();
  99. long Unknown = Context.RequestData.ReadInt64();
  100. return 0;
  101. }
  102. public long GetDisplayVSyncEvent(ServiceCtx Context)
  103. {
  104. string Name = GetDisplayName(Context);
  105. int Handle = Context.Process.HandleTable.OpenHandle(Context.Ns.Os.VsyncEvent);
  106. Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
  107. return 0;
  108. }
  109. private byte[] MakeIGraphicsBufferProducer(long BasePtr)
  110. {
  111. long Id = 0x20;
  112. long CookiePtr = 0L;
  113. using (MemoryStream MS = new MemoryStream())
  114. {
  115. BinaryWriter Writer = new BinaryWriter(MS);
  116. //flat_binder_object (size is 0x28)
  117. Writer.Write(2); //Type (BINDER_TYPE_WEAK_BINDER)
  118. Writer.Write(0); //Flags
  119. Writer.Write((int)(Id >> 0));
  120. Writer.Write((int)(Id >> 32));
  121. Writer.Write((int)(CookiePtr >> 0));
  122. Writer.Write((int)(CookiePtr >> 32));
  123. Writer.Write((byte)'d');
  124. Writer.Write((byte)'i');
  125. Writer.Write((byte)'s');
  126. Writer.Write((byte)'p');
  127. Writer.Write((byte)'d');
  128. Writer.Write((byte)'r');
  129. Writer.Write((byte)'v');
  130. Writer.Write((byte)'\0');
  131. Writer.Write(0L); //Pad
  132. return MakeParcel(MS.ToArray(), new byte[] { 0, 0, 0, 0 });
  133. }
  134. }
  135. private string GetDisplayName(ServiceCtx Context)
  136. {
  137. string Name = string.Empty;
  138. for (int Index = 0; Index < 8 &&
  139. Context.RequestData.BaseStream.Position <
  140. Context.RequestData.BaseStream.Length; Index++)
  141. {
  142. byte Chr = Context.RequestData.ReadByte();
  143. if (Chr >= 0x20 && Chr < 0x7f)
  144. {
  145. Name += (char)Chr;
  146. }
  147. }
  148. return Name;
  149. }
  150. }
  151. }