Skip to content

Commit

Permalink
Add enabled option to ministats (#2218)
Browse files Browse the repository at this point in the history
* add enabled option to ministats
Co-authored-by: Will Eastcott <will@playcanvas.com>
  • Loading branch information
slimbuck committed Jun 30, 2020
1 parent 6974f24 commit ddb11c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
10 changes: 10 additions & 0 deletions extras/mini-stats/cpu-timer.js
Expand Up @@ -4,6 +4,8 @@ function CpuTimer(app) {
this._timings = [];
this._prevTimings = [];

this.enabled = true;

app.on('frameupdate', this.begin.bind(this, 'update'));
app.on('framerender', this.mark.bind(this, 'render'));
app.on('frameend', this.mark.bind(this, 'other'));
Expand All @@ -12,6 +14,10 @@ function CpuTimer(app) {
Object.assign(CpuTimer.prototype, {
// mark the beginning of the frame
begin: function (name) {
if (!this.enabled) {
return;
}

// end previous frame timings
if (this._frameIndex < this._frameTimings.length) {
this._frameTimings.splice(this._frameIndex);
Expand All @@ -27,6 +33,10 @@ Object.assign(CpuTimer.prototype, {

// mark
mark: function (name) {
if (!this.enabled) {
return;
}

var timestamp = pc.now();

// end previous mark
Expand Down
14 changes: 14 additions & 0 deletions extras/mini-stats/gpu-timer.js
Expand Up @@ -9,6 +9,8 @@ function GpuTimer(app) {
this._timings = [];
this._prevTimings = [];

this.enabled = true;

app.on('frameupdate', this.begin.bind(this, 'update'));
app.on('framerender', this.mark.bind(this, 'render'));
app.on('frameend', this.end.bind(this));
Expand All @@ -17,6 +19,10 @@ function GpuTimer(app) {
Object.assign(GpuTimer.prototype, {
// mark the beginning of the frame
begin: function (name) {
if (!this.enabled) {
return;
}

// store previous frame's queries
if (this._frameQueries.length > 0) {
this.end();
Expand All @@ -43,6 +49,10 @@ Object.assign(GpuTimer.prototype, {

// mark
mark: function (name) {
if (!this.enabled) {
return;
}

// end previous query
if (this._frameQueries.length > 0) {
this._gl.endQuery(this._ext.TIME_ELAPSED_EXT);
Expand All @@ -57,6 +67,10 @@ Object.assign(GpuTimer.prototype, {

// end of frame
end: function () {
if (!this.enabled) {
return;
}

this._gl.endQuery(this._ext.TIME_ELAPSED_EXT);
this._frames.push(this._frameQueries);
this._frameQueries = [];
Expand Down
39 changes: 28 additions & 11 deletions extras/mini-stats/mini-stats.js
Expand Up @@ -332,14 +332,16 @@ Object.assign(Graph.prototype, {
},

render: function (render2d, x, y, w, h) {
render2d.quad(this.texture,
x + w,
y,
-w,
h,
this.cursor,
0.5 + (this.enabled ? this.yOffset : 0),
-w, 0);
if (this.enabled) {
render2d.quad(this.texture,
x + w,
y,
-w,
h,
this.cursor,
0.5 + this.yOffset,
-w, 0);
}
}
});

Expand Down Expand Up @@ -419,7 +421,7 @@ function MiniStats(app) {

div.addEventListener('click', function (event) {
event.preventDefault();
if (self.enabled) {
if (self._enabled) {
size = (size + 1) % sizes.length;
self.resize(sizes[size].width, sizes[size].height, sizes[size].graphs);
}
Expand All @@ -430,7 +432,7 @@ function MiniStats(app) {
});

app.on('postrender', function () {
if (self.enabled) {
if (self._enabled) {
self.render();
}
});
Expand All @@ -447,7 +449,7 @@ function MiniStats(app) {
this.gspacing = 2;
this.clr = [1, 1, 1, 0.5];

this.enabled = true;
this._enabled = true;

this.resize(sizes[size].width, sizes[size].height, sizes[size].graphs);
}
Expand All @@ -467,6 +469,21 @@ Object.defineProperties(MiniStats.prototype, {
var graphs = this.graphs;
return this.height * graphs.length + this.gspacing * (graphs.length - 1);
}
},

enabled: {
get: function () {
return this._enabled;
},
set: function (value) {
if (value !== this._enabled) {
this._enabled = value;
for (var i = 0; i < this.graphs.length; ++i) {
this.graphs[i].enabled = value;
this.graphs[i].timer.enabled = value;
}
}
}
}
});

Expand Down

0 comments on commit ddb11c3

Please sign in to comment.