ProfilerServiceImpl.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Diagnostics;
  3. using SRDebugger.Services;
  4. using SRF;
  5. using SRF.Service;
  6. using UnityEngine;
  7. namespace SRDebugger.Profiler
  8. {
  9. [Service(typeof(IProfilerService))]
  10. public class ProfilerServiceImpl : SRServiceBase<IProfilerService>, IProfilerService
  11. {
  12. public float AverageFrameTime { get; private set; }
  13. public float LastFrameTime { get; private set; }
  14. public CircularBuffer<ProfilerFrame> FrameBuffer
  15. {
  16. get
  17. {
  18. return this._frameBuffer;
  19. }
  20. }
  21. protected void PushFrame(double totalTime, double updateTime, double renderTime)
  22. {
  23. this._frameBuffer.PushBack(new ProfilerFrame
  24. {
  25. OtherTime = totalTime - updateTime - renderTime,
  26. UpdateTime = updateTime,
  27. RenderTime = renderTime
  28. });
  29. }
  30. protected override void Awake()
  31. {
  32. base.Awake();
  33. this._lateUpdateListener = base.gameObject.AddComponent<ProfilerLateUpdateListener>();
  34. this._lateUpdateListener.OnLateUpdate = new Action(this.OnLateUpdate);
  35. base.CachedGameObject.hideFlags = HideFlags.NotEditable;
  36. base.CachedTransform.SetParent(Hierarchy.Get("SRDebugger"), true);
  37. }
  38. protected override void Update()
  39. {
  40. base.Update();
  41. if (this.FrameBuffer.Count > 0)
  42. {
  43. ProfilerFrame value = this.FrameBuffer.Back();
  44. value.FrameTime = (double)Time.deltaTime;
  45. this.FrameBuffer[this.FrameBuffer.Count - 1] = value;
  46. }
  47. this.LastFrameTime = Time.deltaTime;
  48. int num = Mathf.Min(20, this.FrameBuffer.Count);
  49. double num2 = 0.0;
  50. for (int i = 0; i < num; i++)
  51. {
  52. num2 += this.FrameBuffer[i].FrameTime;
  53. }
  54. this.AverageFrameTime = (float)num2 / (float)num;
  55. if (this._reportedCameras != this._cameraListeners.Count)
  56. {
  57. }
  58. if (this._stopwatch.IsRunning)
  59. {
  60. this._stopwatch.Stop();
  61. this._stopwatch.Reset();
  62. }
  63. this._updateDuration = (this._renderDuration = (this._updateToRenderDuration = 0.0));
  64. this._reportedCameras = 0;
  65. this.CameraCheck();
  66. this._stopwatch.Start();
  67. }
  68. private void OnLateUpdate()
  69. {
  70. this._updateDuration = this._stopwatch.Elapsed.TotalSeconds;
  71. }
  72. private void EndFrame()
  73. {
  74. if (this._stopwatch.IsRunning)
  75. {
  76. this.PushFrame(this._stopwatch.Elapsed.TotalSeconds, this._updateDuration, this._renderDuration);
  77. this._stopwatch.Reset();
  78. this._stopwatch.Start();
  79. }
  80. }
  81. private void CameraDurationCallback(ProfilerCameraListener listener, double duration)
  82. {
  83. this._reportedCameras++;
  84. this._renderDuration = this._stopwatch.Elapsed.TotalSeconds - this._updateDuration - this._updateToRenderDuration;
  85. if (this._reportedCameras >= this.GetExpectedCameraCount())
  86. {
  87. this.EndFrame();
  88. }
  89. }
  90. private int GetExpectedCameraCount()
  91. {
  92. int num = 0;
  93. for (int i = 0; i < this._cameraListeners.Count; i++)
  94. {
  95. if (!(this._cameraListeners[i] != null) || (this._cameraListeners[i].isActiveAndEnabled && this._cameraListeners[i].Camera.isActiveAndEnabled))
  96. {
  97. num++;
  98. }
  99. }
  100. return num;
  101. }
  102. private void CameraCheck()
  103. {
  104. for (int i = this._cameraListeners.Count - 1; i >= 0; i--)
  105. {
  106. if (this._cameraListeners[i] == null)
  107. {
  108. this._cameraListeners.RemoveAt(i);
  109. }
  110. }
  111. if (Camera.allCamerasCount == this._cameraListeners.Count)
  112. {
  113. return;
  114. }
  115. if (Camera.allCamerasCount > this._cameraCache.Length)
  116. {
  117. this._cameraCache = new Camera[Camera.allCamerasCount];
  118. }
  119. int allCameras = Camera.GetAllCameras(this._cameraCache);
  120. for (int j = 0; j < allCameras; j++)
  121. {
  122. Camera camera = this._cameraCache[j];
  123. bool flag = false;
  124. for (int k = 0; k < this._cameraListeners.Count; k++)
  125. {
  126. if (this._cameraListeners[k].Camera == camera)
  127. {
  128. flag = true;
  129. break;
  130. }
  131. }
  132. if (!flag)
  133. {
  134. ProfilerCameraListener profilerCameraListener = camera.gameObject.AddComponent<ProfilerCameraListener>();
  135. profilerCameraListener.hideFlags = (HideFlags.DontSaveInEditor | HideFlags.NotEditable | HideFlags.DontSaveInBuild | HideFlags.DontUnloadUnusedAsset);
  136. profilerCameraListener.RenderDurationCallback = new Action<ProfilerCameraListener, double>(this.CameraDurationCallback);
  137. this._cameraListeners.Add(profilerCameraListener);
  138. }
  139. }
  140. }
  141. private const int FrameBufferSize = 400;
  142. private readonly SRList<ProfilerCameraListener> _cameraListeners = new SRList<ProfilerCameraListener>();
  143. private readonly CircularBuffer<ProfilerFrame> _frameBuffer = new CircularBuffer<ProfilerFrame>(400);
  144. private Camera[] _cameraCache = new Camera[6];
  145. private ProfilerLateUpdateListener _lateUpdateListener;
  146. private double _renderDuration;
  147. private int _reportedCameras;
  148. private Stopwatch _stopwatch = new Stopwatch();
  149. private double _updateDuration;
  150. private double _updateToRenderDuration;
  151. }
  152. }