IApplicationDisplayService.cs 6.1 KB

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