xingwei.zhu
6 年前
当前提交
db412ae7
共有 12 个文件被更改,包括 162 次插入 和 171 次删除
-
8Runtime/editor/editor_window.cs
-
2Runtime/editor/rasterizer.cs
-
29Runtime/flow/compositor_context.cs
-
2Runtime/flow/layer.cs
-
4Runtime/flow/layer_tree.cs
-
55Runtime/flow/performance_overlay_layer.cs
-
3Runtime/rendering/binding.cs
-
17Runtime/ui/window.cs
-
2Runtime/flow/instrumentation.cs.meta
-
113Runtime/flow/instrumentation.cs
-
98Runtime/service/performance_utils.cs
-
0/Runtime/flow/instrumentation.cs.meta
|
|||
using Unity.UIWidgets.material; |
|||
using Unity.UIWidgets.ui; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
using Canvas = Unity.UIWidgets.ui.Canvas; |
|||
using Rect = Unity.UIWidgets.ui.Rect; |
|||
|
|||
namespace Unity.UIWidgets.flow { |
|||
static class InstrumentationUtils { |
|||
public const int kMaxSamples = 120; |
|||
|
|||
public static float now() { |
|||
#if UNITY_EDITOR
|
|||
return (float) EditorApplication.timeSinceStartup; |
|||
#else
|
|||
return Time.realtimeSinceStartup; |
|||
#endif
|
|||
} |
|||
} |
|||
|
|||
public class Stopwatch { |
|||
float _start; |
|||
float[] _laps; |
|||
int _currentSample; |
|||
bool _cacheDirty; |
|||
|
|||
public Stopwatch() { |
|||
this._start = InstrumentationUtils.now(); |
|||
this._currentSample = 0; |
|||
float delta = 0f; |
|||
|
|||
this._laps = new float[InstrumentationUtils.kMaxSamples]; |
|||
for (int i = 0; i < this._laps.Length; i++) { |
|||
this._laps[i] = delta; |
|||
} |
|||
|
|||
this._cacheDirty = true; |
|||
} |
|||
|
|||
public void start() { |
|||
this._start = InstrumentationUtils.now(); |
|||
this._currentSample = (this._currentSample + 1) % InstrumentationUtils.kMaxSamples; |
|||
} |
|||
|
|||
public void stop() { |
|||
this._laps[this._currentSample] = InstrumentationUtils.now() - this._start; |
|||
} |
|||
|
|||
public void setLapTime(float delta) { |
|||
this._currentSample = (this._currentSample + 1) % InstrumentationUtils.kMaxSamples; |
|||
this._laps[this._currentSample] = delta; |
|||
} |
|||
|
|||
public float lastLap() { |
|||
return this._laps[(this._currentSample - 1) % InstrumentationUtils.kMaxSamples]; |
|||
} |
|||
|
|||
public float maxDelta() { |
|||
float maxDelta = 0f; |
|||
for (int i = 0; i < this._laps.Length; i++) { |
|||
if (maxDelta < this._laps[i]) { |
|||
maxDelta = this._laps[i]; |
|||
} |
|||
} |
|||
|
|||
return maxDelta; |
|||
} |
|||
|
|||
public void visualize(Canvas canvas, Rect rect) { |
|||
Paint paint = new Paint {color = Colors.blue}; |
|||
Paint paint2 = new Paint {color = Colors.red}; |
|||
Paint paint3 = new Paint {color = Colors.green}; |
|||
Paint paint4 = new Paint {color = Colors.white70}; |
|||
|
|||
float[] costFrames = this._laps; |
|||
int curFrame = (this._currentSample - 1) % InstrumentationUtils.kMaxSamples; |
|||
|
|||
float barWidth = Mathf.Max(1, rect.width / costFrames.Length); |
|||
float perHeight = rect.height / 32.0f; |
|||
|
|||
canvas.drawRect(rect, paint4); |
|||
canvas.drawRect(Rect.fromLTWH(rect.left, rect.top + perHeight * 16.0f, rect.width, 1), paint3); |
|||
|
|||
float cur_x = rect.left; |
|||
Path barPath = new Path(); |
|||
|
|||
for (var i = 0; i < costFrames.Length; i++) { |
|||
if (costFrames[i] != 0) { |
|||
float curHeight = Mathf.Min(perHeight * costFrames[i] * 1000, rect.height); |
|||
Rect barRect = Rect.fromLTWH(cur_x, rect.top + rect.height - curHeight, barWidth, curHeight); |
|||
barPath.addRect(barRect); |
|||
} |
|||
|
|||
cur_x += barWidth; |
|||
} |
|||
|
|||
canvas.drawPath(barPath, paint); |
|||
if (curFrame >= 0 && curFrame < costFrames.Length && costFrames[curFrame] != 0) { |
|||
float curHeight = Mathf.Min(perHeight * costFrames[curFrame] * 1000, rect.height); |
|||
Rect barRect = Rect.fromLTWH(rect.left + barWidth * curFrame, rect.top + rect.height - curHeight, |
|||
barWidth, curHeight); |
|||
canvas.drawRect(barRect, paint2); |
|||
|
|||
var pb = new ParagraphBuilder(new ParagraphStyle { }); |
|||
pb.addText("Current Frame Cost: " + costFrames[curFrame] * 1000 + "ms" + " ; Max(in last 120 frames): " + this.maxDelta() * 1000 + "ms"); |
|||
var paragraph = pb.build(); |
|||
paragraph.layout(new ParagraphConstraints(width: 800)); |
|||
|
|||
canvas.drawParagraph(paragraph, new Offset(rect.left, rect.top + rect.height - 12)); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
using System.Diagnostics; |
|||
using Unity.UIWidgets.foundation; |
|||
|
|||
namespace Unity.UIWidgets.service { |
|||
public class PerformanceUtils { |
|||
public static PerformanceUtils instance { |
|||
get { |
|||
if (_instance != null) { |
|||
return _instance; |
|||
} |
|||
|
|||
_instance = new PerformanceUtils(); |
|||
_instance._setup(); |
|||
|
|||
return _instance; |
|||
} |
|||
} |
|||
|
|||
static PerformanceUtils _instance; |
|||
|
|||
const int FrameBufferSize = 200; |
|||
|
|||
float[] _frames; |
|||
int _curFrameId; |
|||
Stopwatch _stopwatch; |
|||
|
|||
float deltaTime = 0.0f; |
|||
|
|||
bool _enabled; |
|||
|
|||
|
|||
void _setup() { |
|||
this._frames = new float[FrameBufferSize]; |
|||
this._curFrameId = -1; |
|||
this._enabled = false; |
|||
} |
|||
|
|||
void _ensureStopWatch() { |
|||
if (this._stopwatch == null) { |
|||
this._stopwatch = new Stopwatch(); |
|||
} |
|||
} |
|||
|
|||
public void updateDeltaTime(float unscaledDeltaTime) { |
|||
this.deltaTime += (unscaledDeltaTime - this.deltaTime) * 0.1f; |
|||
} |
|||
|
|||
public float getFPS() { |
|||
return 1.0f / this.deltaTime; |
|||
} |
|||
|
|||
public void startProfile() { |
|||
if (!this._enabled) { |
|||
return; |
|||
} |
|||
|
|||
this._ensureStopWatch(); |
|||
if (this._stopwatch.IsRunning) { |
|||
D.assert(false, "Try to start the stopwatch when it is already running"); |
|||
return; |
|||
} |
|||
|
|||
this._stopwatch.Start(); |
|||
} |
|||
|
|||
public void endProfile() { |
|||
if (!this._enabled || this._stopwatch == null) { |
|||
return; |
|||
} |
|||
|
|||
if (!this._stopwatch.IsRunning) { |
|||
D.assert(false, "Try to record the stopwatch when it is already stopped"); |
|||
} |
|||
|
|||
this._stopwatch.Stop(); |
|||
float frameCost = this._stopwatch.ElapsedMilliseconds; |
|||
this._stopwatch.Reset(); |
|||
if (frameCost == 0) { |
|||
return; |
|||
} |
|||
|
|||
this._curFrameId = (this._curFrameId + 1) % FrameBufferSize; |
|||
this._frames[this._curFrameId] = frameCost; |
|||
} |
|||
|
|||
public float[] getFrames() { |
|||
if (!this._enabled) { |
|||
this._enabled = true; |
|||
} |
|||
|
|||
return this._frames; |
|||
} |
|||
|
|||
public int getCurFrame() { |
|||
return this._curFrameId; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue