ProfilerGraphControl.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Collections.Generic;
  3. using SRDebugger.Services;
  4. using SRF;
  5. using SRF.Service;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace SRDebugger.UI.Controls
  9. {
  10. [RequireComponent(typeof(RectTransform))]
  11. [ExecuteInEditMode]
  12. [RequireComponent(typeof(CanvasRenderer))]
  13. public class ProfilerGraphControl : Graphic
  14. {
  15. protected override void Awake()
  16. {
  17. base.Awake();
  18. this._profilerService = SRServiceManager.GetService<IProfilerService>();
  19. }
  20. protected override void Start()
  21. {
  22. base.Start();
  23. }
  24. protected void Update()
  25. {
  26. this.SetVerticesDirty();
  27. }
  28. [Obsolete]
  29. protected override void OnPopulateMesh(Mesh m)
  30. {
  31. this._meshVertices.Clear();
  32. this._meshVertexColors.Clear();
  33. this._meshTriangles.Clear();
  34. float width = base.rectTransform.rect.width;
  35. float height = base.rectTransform.rect.height;
  36. this._clipBounds = new Rect(0f, 0f, width, height);
  37. int num = this.TargetFps;
  38. if (Application.isPlaying && this.TargetFpsUseApplication && Application.targetFrameRate > 0)
  39. {
  40. num = Application.targetFrameRate;
  41. }
  42. float num2 = 1f / (float)num;
  43. int num3 = -1;
  44. float num4 = (!this.FloatingScale) ? (1f / (float)num) : this.CalculateMaxFrameTime();
  45. if (this.FloatingScale)
  46. {
  47. for (int i = 0; i < ProfilerGraphControl.ScaleSteps.Length; i++)
  48. {
  49. float num5 = ProfilerGraphControl.ScaleSteps[i];
  50. if (num4 < num5 * 1.1f)
  51. {
  52. num2 = num5;
  53. num3 = i;
  54. break;
  55. }
  56. }
  57. if (num3 < 0)
  58. {
  59. num3 = ProfilerGraphControl.ScaleSteps.Length - 1;
  60. num2 = ProfilerGraphControl.ScaleSteps[num3];
  61. }
  62. }
  63. else
  64. {
  65. for (int j = 0; j < ProfilerGraphControl.ScaleSteps.Length; j++)
  66. {
  67. float num6 = ProfilerGraphControl.ScaleSteps[j];
  68. if (num4 > num6)
  69. {
  70. num3 = j;
  71. }
  72. }
  73. }
  74. float num7 = (height - (float)(this.VerticalPadding * 2)) / num2;
  75. int num8 = this.CalculateVisibleDataPointCount();
  76. int frameBufferCurrentSize = this.GetFrameBufferCurrentSize();
  77. for (int k = 0; k < frameBufferCurrentSize; k++)
  78. {
  79. if (k >= num8)
  80. {
  81. break;
  82. }
  83. ProfilerFrame frame = this.GetFrame(frameBufferCurrentSize - k - 1);
  84. float xPosition = width - 4f * (float)k - 4f - width / 2f;
  85. this.DrawDataPoint(xPosition, num7, frame);
  86. }
  87. if (this.DrawAxes)
  88. {
  89. if (!this.FloatingScale)
  90. {
  91. this.DrawAxis(num2, num2 * num7, this.GetAxisLabel(0));
  92. }
  93. int num9 = 2;
  94. int num10 = 0;
  95. if (!this.FloatingScale)
  96. {
  97. num10++;
  98. }
  99. for (int l = num3; l >= 0; l--)
  100. {
  101. if (num10 >= num9)
  102. {
  103. break;
  104. }
  105. this.DrawAxis(ProfilerGraphControl.ScaleSteps[l], ProfilerGraphControl.ScaleSteps[l] * num7, this.GetAxisLabel(num10));
  106. num10++;
  107. }
  108. }
  109. m.Clear();
  110. m.SetVertices(this._meshVertices);
  111. m.SetColors(this._meshVertexColors);
  112. m.SetTriangles(this._meshTriangles, 0);
  113. }
  114. protected void DrawDataPoint(float xPosition, float verticalScale, ProfilerFrame frame)
  115. {
  116. float x = Mathf.Min(this._clipBounds.width / 2f, xPosition + 4f - 2f);
  117. float num = 0f;
  118. for (int i = 0; i < 3; i++)
  119. {
  120. int num2 = i;
  121. float num3 = 0f;
  122. if (i == 0)
  123. {
  124. num3 = (float)frame.UpdateTime;
  125. }
  126. else if (i == 1)
  127. {
  128. num3 = (float)frame.RenderTime;
  129. }
  130. else if (i == 2)
  131. {
  132. num3 = (float)frame.OtherTime;
  133. }
  134. num3 *= verticalScale;
  135. if (!num3.ApproxZero() && num3 - 4f >= 0f)
  136. {
  137. float num4 = num + 2f - base.rectTransform.rect.height / 2f;
  138. if (this.VerticalAlignment == ProfilerGraphControl.VerticalAlignments.Top)
  139. {
  140. num4 = base.rectTransform.rect.height / 2f - num - 2f;
  141. }
  142. float y = num4 + num3 - 2f;
  143. if (this.VerticalAlignment == ProfilerGraphControl.VerticalAlignments.Top)
  144. {
  145. y = num4 - num3 + 2f;
  146. }
  147. Color c = this.LineColours[num2];
  148. this.AddRect(new Vector3(Mathf.Max(-this._clipBounds.width / 2f, xPosition), num4), new Vector3(Mathf.Max(-this._clipBounds.width / 2f, xPosition), y), new Vector3(x, y), new Vector3(x, num4), c);
  149. num += num3;
  150. }
  151. }
  152. }
  153. protected void DrawAxis(float frameTime, float yPosition, ProfilerGraphAxisLabel label)
  154. {
  155. float num = -base.rectTransform.rect.width * 0.5f;
  156. float x = -num;
  157. float y = yPosition - base.rectTransform.rect.height * 0.5f + 0.5f;
  158. float y2 = yPosition - base.rectTransform.rect.height * 0.5f - 0.5f;
  159. Color c = new Color(1f, 1f, 1f, 0.4f);
  160. this.AddRect(new Vector3(num, y2), new Vector3(num, y), new Vector3(x, y), new Vector3(x, y2), c);
  161. if (label != null)
  162. {
  163. label.SetValue(frameTime, yPosition);
  164. }
  165. }
  166. protected void AddRect(Vector3 tl, Vector3 tr, Vector3 bl, Vector3 br, Color c)
  167. {
  168. this._meshVertices.Add(tl);
  169. this._meshVertices.Add(tr);
  170. this._meshVertices.Add(bl);
  171. this._meshVertices.Add(br);
  172. this._meshTriangles.Add(this._meshVertices.Count - 4);
  173. this._meshTriangles.Add(this._meshVertices.Count - 3);
  174. this._meshTriangles.Add(this._meshVertices.Count - 1);
  175. this._meshTriangles.Add(this._meshVertices.Count - 2);
  176. this._meshTriangles.Add(this._meshVertices.Count - 1);
  177. this._meshTriangles.Add(this._meshVertices.Count - 3);
  178. this._meshVertexColors.Add(c);
  179. this._meshVertexColors.Add(c);
  180. this._meshVertexColors.Add(c);
  181. this._meshVertexColors.Add(c);
  182. }
  183. protected ProfilerFrame GetFrame(int i)
  184. {
  185. return this._profilerService.FrameBuffer[i];
  186. }
  187. protected int CalculateVisibleDataPointCount()
  188. {
  189. return Mathf.RoundToInt(base.rectTransform.rect.width / 4f);
  190. }
  191. protected int GetFrameBufferCurrentSize()
  192. {
  193. return this._profilerService.FrameBuffer.Count;
  194. }
  195. protected int GetFrameBufferMaxSize()
  196. {
  197. return this._profilerService.FrameBuffer.Capacity;
  198. }
  199. protected float CalculateMaxFrameTime()
  200. {
  201. int frameBufferCurrentSize = this.GetFrameBufferCurrentSize();
  202. int num = Mathf.Min(this.CalculateVisibleDataPointCount(), frameBufferCurrentSize);
  203. double num2 = 0.0;
  204. for (int i = 0; i < num; i++)
  205. {
  206. int i2 = frameBufferCurrentSize - i - 1;
  207. ProfilerFrame frame = this.GetFrame(i2);
  208. if (frame.FrameTime > num2)
  209. {
  210. num2 = frame.FrameTime;
  211. }
  212. }
  213. return (float)num2;
  214. }
  215. private ProfilerGraphAxisLabel GetAxisLabel(int index)
  216. {
  217. if (this._axisLabels == null || !Application.isPlaying)
  218. {
  219. this._axisLabels = base.GetComponentsInChildren<ProfilerGraphAxisLabel>();
  220. }
  221. if (this._axisLabels.Length > index)
  222. {
  223. return this._axisLabels[index];
  224. }
  225. UnityEngine.Debug.LogWarning("[SRDebugger.Profiler] Not enough axis labels in pool");
  226. return null;
  227. }
  228. public ProfilerGraphControl.VerticalAlignments VerticalAlignment = ProfilerGraphControl.VerticalAlignments.Bottom;
  229. private static readonly float[] ScaleSteps = new float[]
  230. {
  231. 0.005f,
  232. 0.00625f,
  233. 0.008333334f,
  234. 0.01f,
  235. 0.0166666675f,
  236. 0.0333333351f,
  237. 0.05f,
  238. 0.0833333358f,
  239. 0.166666672f
  240. };
  241. public bool FloatingScale;
  242. public bool TargetFpsUseApplication;
  243. public bool DrawAxes = true;
  244. public int TargetFps = 60;
  245. public bool Clip = true;
  246. public const float DataPointMargin = 2f;
  247. public const float DataPointVerticalMargin = 2f;
  248. public const float DataPointWidth = 4f;
  249. public int VerticalPadding = 10;
  250. public const int LineCount = 3;
  251. public Color[] LineColours = new Color[0];
  252. private IProfilerService _profilerService;
  253. private ProfilerGraphAxisLabel[] _axisLabels;
  254. private Rect _clipBounds;
  255. private readonly List<Vector3> _meshVertices = new List<Vector3>();
  256. private readonly List<Color32> _meshVertexColors = new List<Color32>();
  257. private readonly List<int> _meshTriangles = new List<int>();
  258. public enum VerticalAlignments
  259. {
  260. Top,
  261. Bottom
  262. }
  263. }
  264. }