IApplicationDisplayService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Ryujinx.HLE.OsHle.Ipc;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using static Ryujinx.HLE.OsHle.Services.Android.Parcel;
  5. namespace Ryujinx.HLE.OsHle.Services.Vi
  6. {
  7. class IApplicationDisplayService : IpcService
  8. {
  9. private Dictionary<int, ServiceProcessRequest> m_Commands;
  10. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  11. private IdDictionary Displays;
  12. public IApplicationDisplayService()
  13. {
  14. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  15. {
  16. { 100, GetRelayService },
  17. { 101, GetSystemDisplayService },
  18. { 102, GetManagerDisplayService },
  19. { 103, GetIndirectDisplayTransactionService },
  20. { 1010, OpenDisplay },
  21. { 1020, CloseDisplay },
  22. { 1102, GetDisplayResolution },
  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 GetDisplayResolution(ServiceCtx Context)
  66. {
  67. long DisplayId = Context.RequestData.ReadInt32();
  68. Context.ResponseData.Write(1280);
  69. Context.ResponseData.Write(720);
  70. return 0;
  71. }
  72. public long OpenLayer(ServiceCtx Context)
  73. {
  74. long LayerId = Context.RequestData.ReadInt64();
  75. long UserId = Context.RequestData.ReadInt64();
  76. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  77. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  78. Context.Memory.WriteBytes(ParcelPtr, Parcel);
  79. Context.ResponseData.Write((long)Parcel.Length);
  80. return 0;
  81. }
  82. public long CloseLayer(ServiceCtx Context)
  83. {
  84. long LayerId = Context.RequestData.ReadInt64();
  85. return 0;
  86. }
  87. public long CreateStrayLayer(ServiceCtx Context)
  88. {
  89. long LayerFlags = Context.RequestData.ReadInt64();
  90. long DisplayId = Context.RequestData.ReadInt64();
  91. long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
  92. Display Disp = Displays.GetData<Display>((int)DisplayId);
  93. byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
  94. Context.Memory.WriteBytes(ParcelPtr, Parcel);
  95. Context.ResponseData.Write(0L);
  96. Context.ResponseData.Write((long)Parcel.Length);
  97. return 0;
  98. }
  99. public long DestroyStrayLayer(ServiceCtx Context)
  100. {
  101. return 0;
  102. }
  103. public long SetLayerScalingMode(ServiceCtx Context)
  104. {
  105. int ScalingMode = Context.RequestData.ReadInt32();
  106. long Unknown = Context.RequestData.ReadInt64();
  107. return 0;
  108. }
  109. public long GetDisplayVSyncEvent(ServiceCtx Context)
  110. {
  111. string Name = GetDisplayName(Context);
  112. int Handle = Context.Process.HandleTable.OpenHandle(Context.Ns.Os.VsyncEvent);
  113. Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
  114. return 0;
  115. }
  116. private byte[] MakeIGraphicsBufferProducer(long BasePtr)
  117. {
  118. long Id = 0x20;
  119. long CookiePtr = 0L;
  120. using (MemoryStream MS = new MemoryStream())
  121. {
  122. BinaryWriter Writer = new BinaryWriter(MS);
  123. //flat_binder_object (size is 0x28)
  124. Writer.Write(2); //Type (BINDER_TYPE_WEAK_BINDER)
  125. Writer.Write(0); //Flags
  126. Writer.Write((int)(Id >> 0));
  127. Writer.Write((int)(Id >> 32));
  128. Writer.Write((int)(CookiePtr >> 0));
  129. Writer.Write((int)(CookiePtr >> 32));
  130. Writer.Write((byte)'d');
  131. Writer.Write((byte)'i');
  132. Writer.Write((byte)'s');
  133. Writer.Write((byte)'p');
  134. Writer.Write((byte)'d');
  135. Writer.Write((byte)'r');
  136. Writer.Write((byte)'v');
  137. Writer.Write((byte)'\0');
  138. Writer.Write(0L); //Pad
  139. return MakeParcel(MS.ToArray(), new byte[] { 0, 0, 0, 0 });
  140. }
  141. }
  142. private string GetDisplayName(ServiceCtx Context)
  143. {
  144. string Name = string.Empty;
  145. for (int Index = 0; Index < 8 &&
  146. Context.RequestData.BaseStream.Position <
  147. Context.RequestData.BaseStream.Length; Index++)
  148. {
  149. byte Chr = Context.RequestData.ReadByte();
  150. if (Chr >= 0x20 && Chr < 0x7f)
  151. {
  152. Name += (char)Chr;
  153. }
  154. }
  155. return Name;
  156. }
  157. }
  158. }