浏览代码

performanceoverlay layer

/main
xingwei.zhu 6 年前
当前提交
9e9a2bcf
共有 13 个文件被更改,包括 423 次插入13 次删除
  1. 4
      Runtime/engine/UIWidgetsPanel.cs
  2. 3
      Runtime/rendering/binding.cs
  3. 33
      Runtime/rendering/layer.cs
  4. 17
      Runtime/ui/compositing.cs
  5. 27
      Samples/UIWidgetSample/MaterialSample.cs
  6. 95
      Runtime/flow/performance_overlay_layer.cs
  7. 11
      Runtime/flow/performance_overlay_layer.cs.meta
  8. 81
      Runtime/rendering/performance_overlay.cs
  9. 11
      Runtime/rendering/performance_overlay.cs.meta
  10. 98
      Runtime/service/performance_utils.cs
  11. 11
      Runtime/service/performance_utils.cs.meta
  12. 34
      Runtime/widgets/performance_overlay.cs
  13. 11
      Runtime/widgets/performance_overlay.cs.meta

4
Runtime/engine/UIWidgetsPanel.cs


using System.Collections.Generic;
using System.Collections.Generic;
using Unity.UIWidgets.service;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEngine;

}
void Update() {
PerformanceUtils.instance.updateDeltaTime(Time.unscaledDeltaTime);
this._displayMetrics.Update();
if (EventSystem.current != null && EventSystem.current.currentSelectedGameObject != this.gameObject) {
this.unfocusIfNeeded();

3
Runtime/rendering/binding.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.gestures;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.service;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.rendering {

}
protected virtual void drawFrame() {
PerformanceUtils.instance.startProfile();
PerformanceUtils.instance.endProfile();
}
public override void hitTest(HitTestResult result, Offset position) {

33
Runtime/rendering/layer.cs


defaultValue: Diagnostics.kNullDefaultValue));
}
}
public class PerformanceOverlayLayer : Layer {
public PerformanceOverlayLayer(
Rect overlayRect = null,
int? optionsMask = null
) {
D.assert(overlayRect != null);
D.assert(optionsMask != null);
this._overlayRect = overlayRect;
this.optionsMask = optionsMask ?? 0;
}
public Rect overlayRect {
get { return this._overlayRect; }
set {
if (value != this._overlayRect) {
this._overlayRect = value;
this.markNeedsAddToScene();
}
}
}
Rect _overlayRect;
public readonly int optionsMask;
internal override flow.Layer addToScene(SceneBuilder builder, Offset layerOffset = null) {
layerOffset = layerOffset ?? Offset.zero;
builder.addPerformanceOverlay(this.optionsMask, this.overlayRect.shift(layerOffset));
return null;
}
}
}

17
Runtime/ui/compositing.cs


using System;
using System;
using Unity.UIWidgets.flow;
using Unity.UIWidgets.foundation;

layer.picture = picture;
layer.isComplex = isComplexHint;
layer.willChange = willChangeHint;
this._currentLayer.add(layer);
}
public void addPerformanceOverlay(int enabledOptions, Rect bounds) {
if (this._currentLayer == null) {
return;
}
var layer = new PerformanceOverlayLayer(enabledOptions);
layer.paintBounds = Rect.fromLTRB(
bounds.left,
bounds.top,
bounds.right,
bounds.bottom
);
this._currentLayer.add(layer);
}
}

27
Samples/UIWidgetSample/MaterialSample.cs


class MaterialButtonWidgetState : State<MaterialButtonWidget> {
public override Widget build(BuildContext context) {
return new Material(
child: new Center(
child: new FlatButton(
shape: new RoundedRectangleBorder(borderRadius: BorderRadius.all(20.0f)),
color: new Color(0xFF00FF00),
splashColor: new Color(0xFFFF0011),
highlightColor: new Color(0x88FF0011),
child: new Text("Click Me"),
onPressed: () => { Debug.Log("pressed here"); }
)
)
return new Stack(
children: new List<Widget> {
new Material(
child: new Center(
child: new FlatButton(
shape: new RoundedRectangleBorder(borderRadius: BorderRadius.all(20.0f)),
color: new Color(0xFF00FF00),
splashColor: new Color(0xFFFF0011),
highlightColor: new Color(0x88FF0011),
child: new Text("Click Me"),
onPressed: () => { Debug.Log("pressed here"); }
)
)
),
new PerformanceOverlay()
}
);
}
}

95
Runtime/flow/performance_overlay_layer.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.service;
using Unity.UIWidgets.ui;
using UnityEngine;
using Canvas = Unity.UIWidgets.ui.Canvas;
using Rect = Unity.UIWidgets.ui.Rect;
namespace Unity.UIWidgets.flow {
public class PerformanceOverlayLayer : Layer {
public PerformanceOverlayLayer(int options) {
this._options = options;
}
readonly int _options;
public override void paint(PaintContext context) {
D.assert(this.needsPainting);
const int padding = 8;
const int fpsHeight = 20;
Canvas canvas = context.canvas;
canvas.save();
float x = this.paintBounds.left + padding;
float y = this.paintBounds.top + padding;
float width = this.paintBounds.width - padding * 2;
float height = this.paintBounds.height;
this._drawFPS(canvas, x, y);
if ((this._options & (int) PerformanceOverlayOption.drawFrameCost) == 1) {
this._drawFrameCost(canvas, x, y + fpsHeight, width, height - padding - fpsHeight);
}
canvas.restore();
}
void _drawFPS(Canvas canvas, float x, float y) {
var pb = new ParagraphBuilder(new ParagraphStyle { });
pb.addText("FPS = " + PerformanceUtils.instance.getFPS());
var paragraph = pb.build();
paragraph.layout(new ParagraphConstraints(width: 300));
canvas.drawParagraph(paragraph, new Offset(x, y));
}
void _drawFrameCost(Canvas canvas, float x, float y, float width, float height) {
Rect visualizationRect = Rect.fromLTWH(x, y, width, height);
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 = PerformanceUtils.instance.getFrames();
int curFrame = PerformanceUtils.instance.getCurFrame();
float barWidth = Mathf.Max(1, width / costFrames.Length);
float perHeight = height / 32.0f;
canvas.drawRect(visualizationRect, paint4);
canvas.drawRect(Rect.fromLTWH(x, y + perHeight * 16.0f, width, 1), paint3);
float cur_x = x;
Path barPath = new Path();
for (var i = 0; i < costFrames.Length; i++) {
if (costFrames[i] != 0) {
float curHeight = Mathf.Min(perHeight * costFrames[i], height);
Rect barRect = Rect.fromLTWH(cur_x, y + 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], height);
Rect barRect = Rect.fromLTWH(x + barWidth * curFrame, y + height - curHeight, barWidth, curHeight);
canvas.drawRect(barRect, paint2);
var pb = new ParagraphBuilder(new ParagraphStyle { });
pb.addText("Frame Cost: " + costFrames[curFrame] + "ms");
var paragraph = pb.build();
paragraph.layout(new ParagraphConstraints(width: 300));
canvas.drawParagraph(paragraph, new Offset(x, y + height - 12));
}
}
}
}

11
Runtime/flow/performance_overlay_layer.cs.meta


fileFormatVersion: 2
guid: 8abf5cb3d8e90423fb0d2476c8dea244
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

81
Runtime/rendering/performance_overlay.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.rendering {
public enum PerformanceOverlayOption {
drawFPS, //default
drawFrameCost
}
public class RenderPerformanceOverlay : RenderBox {
public RenderPerformanceOverlay(
int optionsMask = 0
) {
this._optionMask = optionsMask;
}
public int optionsMask {
get { return this._optionMask; }
set {
if (value == this._optionMask) {
return;
}
this._optionMask = value;
this.markNeedsPaint();
}
}
int _optionMask;
protected override bool sizedByParent {
get { return true; }
}
protected override bool alwaysNeedsCompositing {
get { return true; }
}
protected override float computeMinIntrinsicWidth(float height) {
return 0.0f;
}
protected override float computeMaxIntrinsicWidth(float height) {
return 0.0f;
}
float _intrinsicHeight {
get {
const float kDefaultGraphHeight = 80.0f;
float result = 20f;
if ((this.optionsMask | (1 << (int) PerformanceOverlayOption.drawFrameCost)) > 0) {
result += kDefaultGraphHeight;
}
return result;
}
}
protected override float computeMinIntrinsicHeight(float width) {
return this._intrinsicHeight;
}
protected override float computeMaxIntrinsicHeight(float width) {
return this._intrinsicHeight;
}
protected override void performResize() {
this.size = this.constraints.constrain(new Size(float.PositiveInfinity, this._intrinsicHeight));
}
public override void paint(PaintingContext context, Offset offset) {
D.assert(this.needsCompositing);
context.addLayer(new PerformanceOverlayLayer(
overlayRect: Rect.fromLTWH(offset.dx, offset.dy, this.size.width, this.size.height),
optionsMask: this.optionsMask
));
}
}
}

11
Runtime/rendering/performance_overlay.cs.meta


fileFormatVersion: 2
guid: 2e3164a1dd18d4b4fb226e57522bb7ca
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

98
Runtime/service/performance_utils.cs


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;
}
}
}

11
Runtime/service/performance_utils.cs.meta


fileFormatVersion: 2
guid: 819a2117a730b431bb069817e3ed8cd7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

34
Runtime/widgets/performance_overlay.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.rendering;
namespace Unity.UIWidgets.widgets {
public class PerformanceOverlay : LeafRenderObjectWidget {
public PerformanceOverlay(
Key key = null,
int optionsMask = 0
) : base(key: key) {
this.optionsMask = optionsMask;
}
public readonly int optionsMask;
public static PerformanceOverlay allEnabled(
Key key = null
) {
return new PerformanceOverlay(
optionsMask: (1 << (int) PerformanceOverlayOption.drawFPS) |
(1 << (int) PerformanceOverlayOption.drawFrameCost)
);
}
public override RenderObject createRenderObject(BuildContext context) {
return new RenderPerformanceOverlay(
optionsMask: this.optionsMask);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
RenderPerformanceOverlay _renderObject = (RenderPerformanceOverlay) renderObject;
_renderObject.optionsMask = this.optionsMask;
}
}
}

11
Runtime/widgets/performance_overlay.cs.meta


fileFormatVersion: 2
guid: 6f53af60c4e394d96880f3b94fe34dd3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存