-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1101197-optimize-framerate.patch
421 lines (399 loc) · 14.4 KB
/
1101197-optimize-framerate.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
From c455c032fe859bc91ad10116ed63386a9d5ec6d7 Mon Sep 17 00:00:00 2001
From: Jordan Santell <[email protected]>
Date: Wed, 19 Nov 2014 23:32:41 -0800
Subject: Bug 1101197 - optimize framerate rendering in new performance
tool, r=vp
diff --git a/browser/devtools/performance/controller.js b/browser/devtools/performance/controller.js
index 59cf85b..280de76 100644
--- a/browser/devtools/performance/controller.js
+++ b/browser/devtools/performance/controller.js
@@ -13,16 +13,18 @@ let require = devtools.require;
devtools.lazyRequireGetter(this, "Services");
devtools.lazyRequireGetter(this, "promise");
devtools.lazyRequireGetter(this, "EventEmitter",
"devtools/toolkit/event-emitter");
devtools.lazyRequireGetter(this, "DevToolsUtils",
"devtools/toolkit/DevToolsUtils");
devtools.lazyRequireGetter(this, "FramerateFront",
"devtools/server/actors/framerate", true);
+devtools.lazyRequireGetter(this, "sparsifyLineData",
+ "resource:///modules/devtools/graph-utils", true);
devtools.lazyRequireGetter(this, "L10N",
"devtools/profiler/global", true);
devtools.lazyImporter(this, "LineGraphWidget",
"resource:///modules/devtools/Graphs.jsm");
devtools.lazyRequireGetter(this, "CallView",
"devtools/profiler/tree-view", true);
devtools.lazyRequireGetter(this, "ThreadNode",
"devtools/profiler/tree-model", true);
diff --git a/browser/devtools/performance/views/overview.js b/browser/devtools/performance/views/overview.js
index 292a0a4..cdf5585 100644
--- a/browser/devtools/performance/views/overview.js
+++ b/browser/devtools/performance/views/overview.js
@@ -1,21 +1,24 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const OVERVIEW_UPDATE_INTERVAL = 100;
const FRAMERATE_CALC_INTERVAL = 16; // ms
const FRAMERATE_GRAPH_HEIGHT = 60; // px
+const FRAMERATE_MIN_DISTANCE_BETWEEN_POINTS = 9; // sqrt(x) px
/**
* View handler for the overview panel's time view, displaying
* framerate over time.
*/
+let count = 0, originalLength, sparseLength;
+
let OverviewView = {
/**
* Sets up the view with event binding.
*/
initialize: function () {
this._framerateEl = $("#time-framerate");
this._ticksData = [];
@@ -41,27 +44,42 @@ let OverviewView = {
},
/**
* Called at most every OVERVIEW_UPDATE_INTERVAL milliseconds
* and uses data fetched from `_onTimelineData` to render
* data into all the corresponding overview graphs.
*/
_onRecordingTick: Task.async(function *() {
- yield this.framerateGraph.setDataWhenReady(this._ticksData);
+ let { minValue, maxValue, sumValues, data } = yield sparsifyLineData({
+ data: this._ticksData,
+ width: this.framerateGraph.width,
+ height: this.framerateGraph.height,
+ dampenValuesFactor: this.framerateGraph.dampenValuesFactor,
+ dataOffsetX: this.framerateGraph.dataOffsetX,
+ minDistanceBetweenPoints: this.framerateGraph.minDistanceBetweenPoints
+ });
+
+ count++;
+ sparseLength = data.length;
+ originalLength = this._ticksData.length;
+
+ yield this.framerateGraph.setDataWhenReady(data, { minValue, maxValue, sumValues });
+
this.emit(EVENTS.OVERVIEW_RENDERED);
this._draw();
}),
/**
* Sets up the framerate graph.
*/
_initializeFramerateGraph: function () {
let graph = new LineGraphWidget(this._framerateEl, L10N.getStr("graphs.fps"));
- graph.minDistanceBetweenPoints = 1;
+
+ graph.minDistanceBetweenPoints = FRAMERATE_MIN_DISTANCE_BETWEEN_POINTS;
graph.fixedHeight = FRAMERATE_GRAPH_HEIGHT;
graph.selectionEnabled = false;
this.framerateGraph = graph;
},
/**
* Called to refresh the timer to keep firing _onRecordingTick.
*/
@@ -78,16 +96,17 @@ let OverviewView = {
*/
_start: function () {
this._timeoutId = setTimeout(this._onRecordingTick, OVERVIEW_UPDATE_INTERVAL);
this.framerateGraph.dropSelection();
},
_stop: function () {
+ console.log(count, originalLength, sparseLength);
clearTimeout(this._timeoutId);
this.framerateGraph.selectionEnabled = true;
},
/**
* Called when the TimelineFront has new data for
* framerate, markers or memory, and stores the data
* to be plotted subsequently.
diff --git a/browser/devtools/shared/moz.build b/browser/devtools/shared/moz.build
index 8a8d92a..1bfb3a9 100644
--- a/browser/devtools/shared/moz.build
+++ b/browser/devtools/shared/moz.build
@@ -17,16 +17,18 @@ EXTRA_JS_MODULES.devtools += [
'Parser.jsm',
'SplitView.jsm',
]
EXTRA_JS_MODULES.devtools += [
'widgets/AbstractTreeItem.jsm',
'widgets/BreadcrumbsWidget.jsm',
'widgets/Chart.jsm',
+ 'widgets/graph-utils-worker.js',
+ 'widgets/graph-utils.js',
'widgets/Graphs.jsm',
'widgets/SideMenuWidget.jsm',
'widgets/SimpleListWidget.jsm',
'widgets/VariablesView.jsm',
'widgets/VariablesViewController.jsm',
'widgets/ViewHelpers.jsm',
]
diff --git a/browser/devtools/shared/widgets/Graphs.jsm b/browser/devtools/shared/widgets/Graphs.jsm
index 1822910..f34f057 100644
--- a/browser/devtools/shared/widgets/Graphs.jsm
+++ b/browser/devtools/shared/widgets/Graphs.jsm
@@ -309,35 +309,39 @@ AbstractCanvasGraph.prototype = {
dataScaleX: 1,
dataScaleY: 1,
/**
* Sets the data source for this graph.
*
* @param object data
* The data source. The actual format is specified by subclasses.
+ * @param object cached
+ * Additional information that may be used to reduce rendering
+ * time for the next draw. Should be cleared out after draw.
*/
- setData: function(data) {
+ setData: function(data, cached) {
this._data = data;
+ this._cachedData = cached;
this._cachedBackgroundImage = this.buildBackgroundImage();
this._cachedGraphImage = this.buildGraphImage();
this._shouldRedraw = true;
},
/**
* Same as `setData`, but waits for this graph to finish initializing first.
*
* @param object data
* The data source. The actual format is specified by subclasses.
* @return promise
* A promise resolved once the data is set.
*/
- setDataWhenReady: Task.async(function*(data) {
+ setDataWhenReady: Task.async(function*(data, cached) {
yield this.ready();
- this.setData(data);
+ this.setData(data, cached);
}),
/**
* Adds a mask to this graph.
*
* @param any mask, options
* See `buildMaskImage` in inheriting classes for the required args.
*/
@@ -1233,21 +1237,36 @@ LineGraphWidget.prototype = Heritage.extend(AbstractCanvasGraph.prototype, {
let height = this._height;
let totalTicks = this._data.length;
let firstTick = totalTicks ? this._data[0].delta : 0;
let lastTick = totalTicks ? this._data[totalTicks - 1].delta : 0;
let maxValue = Number.MIN_SAFE_INTEGER;
let minValue = Number.MAX_SAFE_INTEGER;
let sumValues = 0;
-
- for (let { delta, value } of this._data) {
- maxValue = Math.max(value, maxValue);
- minValue = Math.min(value, minValue);
- sumValues += value;
+ // If we use cached minValue, maxValue and sumValues,
+ // then we can assume that we've already removed points
+ // that did not meet the minDistanceBetweenPoints requirement
+ let forceDrawAllPoints = false;
+
+ if (this._cachedData &&
+ "minValue" in this._cachedData &&
+ "maxValue" in this._cachedData &&
+ "sumValues" in this._cachedData) {
+ minValue = this._cachedData.minValue;
+ maxValue = this._cachedData.maxValue;
+ sumValues = this._cachedData.sumValues;
+ forceDrawAllPoints = true;
+ this._cachedData = null;
+ } else {
+ for (let { delta, value } of this._data) {
+ maxValue = Math.max(value, maxValue);
+ minValue = Math.min(value, minValue);
+ sumValues += value;
+ }
}
let dataScaleX = this.dataScaleX = width / (lastTick - this.dataOffsetX);
let dataScaleY = this.dataScaleY = height / maxValue * this.dampenValuesFactor;
/**
* Calculates the squared distance between two 2D points.
*/
@@ -1277,18 +1296,17 @@ LineGraphWidget.prototype = Heritage.extend(AbstractCanvasGraph.prototype, {
let currX = (delta - this.dataOffsetX) * dataScaleX;
let currY = height - value * dataScaleY;
if (delta == firstTick) {
ctx.moveTo(-LINE_GRAPH_STROKE_WIDTH, height);
ctx.lineTo(-LINE_GRAPH_STROKE_WIDTH, currY);
}
- let distance = distSquared(prevX, prevY, currX, currY);
- if (distance >= this.minDistanceBetweenPoints) {
+ if (forceDrawAllPoints || distSquared(prevX, prevY, currX, currY) >= this.minDistanceBetweenPoints) {
ctx.lineTo(currX, currY);
prevX = currX;
prevY = currY;
}
if (delta == lastTick) {
ctx.lineTo(width + LINE_GRAPH_STROKE_WIDTH, currY);
ctx.lineTo(width + LINE_GRAPH_STROKE_WIDTH, height);
diff --git a/browser/devtools/shared/widgets/graph-utils-worker.js b/browser/devtools/shared/widgets/graph-utils-worker.js
new file mode 100644
index 0000000..9fc42a8
--- /dev/null
+++ b/browser/devtools/shared/widgets/graph-utils-worker.js
@@ -0,0 +1,113 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// MIN_VALUE_DELTA is a heuristic for whether or not to render
+// two framerate values at the same delta, and should only
+// render if the value difference is > MIN_VALUE_DELTA
+const MIN_VALUE_DELTA = 3;
+// How many values can be thrown away in a row before one is automatically added
+const MAX_CONSECUTIVE_SKIPPED_TICKS = 10;
+
+self.onmessage = (event) => {
+ const { data } = event;
+
+ try {
+ if (data.type === "sparsifyLineData") {
+ let { data: sparseData, minValue, maxValue, sumValues } = sparsifyLineData(data);
+ self.postMessage({
+ id: data.id,
+ data: sparseData,
+ minValue: minValue,
+ maxValue: maxValue,
+ sumValues: sumValues
+ });
+ }
+ } catch (e) {
+ self.postMessage({
+ id: data.id,
+ error: e.message + "\n" + e.stack
+ });
+ }
+};
+
+function distSquared (x0, y0, x1, y1) {
+ return (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0);
+}
+
+/**
+ * Reduce an array for a LineGraph based off of minDistanceBetweenPoints
+ * for the points to use with before passing into being rendered by
+ * LineGraph.
+ */
+function sparsifyLineData ({ data, width, height, dampenValuesFactor, dataOffsetX, minDistanceBetweenPoints }) {
+ let result = [];
+ let totalTicks = data.length;
+ let maxValue = Number.MIN_SAFE_INTEGER;
+ let minValue = Number.MAX_SAFE_INTEGER;
+ let sumValues = 0;
+
+ // We need the maxValue so might as well calculate other things
+ // to cache the work so Graphs.jsm doesn't need to do this again
+ for (let { value } of data) {
+ maxValue = Math.max(value, maxValue);
+ minValue = Math.min(value, minValue);
+ sumValues += value;
+ }
+
+ let firstTick = totalTicks ? data[0].delta : 0;
+ let lastTick = totalTicks ? data[totalTicks - 1].delta : 0;
+ let dataScaleX = width / (lastTick - dataOffsetX);
+ let dataScaleY = height / maxValue * dampenValuesFactor;
+ let prevX = 0;
+ let prevY = 0;
+ let prevValue = null;
+ let prevDelta = null;
+ let skippedTicks = 0;
+
+ for (let i = 0; i < data.length; i++) {
+ let { delta, value } = data[i];
+
+ // Now filter out if line distance < 1
+ let currX = (delta - dataOffsetX) * dataScaleX;
+ let currY = height - value * dataScaleY;
+
+ let dist = distSquared(prevX, prevY, currX, currY);
+
+ prevX = currX;
+ prevY = currY;
+
+ // If distance is far enough from previous point, render the first
+ if (dist >= minDistanceBetweenPoints || ++skippedTicks > MAX_CONSECUTIVE_SKIPPED_TICKS) {
+
+ // If the previous value was not saved, and the difference
+ // between the current and last unsaved value is > MIN_VALUE_DELTA,
+ // also save the previous value.
+ if (prevValue !== null && Math.abs(value - prevValue) > MIN_VALUE_DELTA) {
+ result.push({ delta: prevDelta, value: prevValue });
+ }
+
+ result.push({ delta, value });
+ skippedTicks = 0;
+
+ // Since we're pushing the current value,
+ // nullify prevValue and prevDelta, so the next loop doesn't add this twice
+ prevValue = prevDelta = null;
+
+ continue;
+ } else {
+ // Since this tick didn't make the cut,
+ // store the values so that the next loop may add it if a large
+ // enough gap
+ prevDelta = delta;
+ prevValue = value;
+ }
+
+ if (delta === firstTick || delta === lastTick) {
+ result.push({ delta, value });
+ }
+ }
+
+ return { minValue, maxValue, sumValues, data: result };
+}
diff --git a/browser/devtools/shared/widgets/graph-utils.js b/browser/devtools/shared/widgets/graph-utils.js
new file mode 100644
index 0000000..4537896
--- /dev/null
+++ b/browser/devtools/shared/widgets/graph-utils.js
@@ -0,0 +1,40 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Promise } = require("resource://gre/modules/Promise.jsm");
+const { ChromeWorker } = require("chrome");
+let graphTaskId = 0;
+let graphUtilsWorker;
+
+/**
+ * Takes an array that would be rendered via LineGraph, as well as
+ * some of the graph's properties, to render a filtered array
+ * off the main thread.
+ */
+exports.sparsifyLineData = function ({ data, width, height, dampenValuesFactor, dataOffsetX, minDistanceBetweenPoints }) {
+ let worker = graphUtilsWorker || new ChromeWorker("resource:///modules/devtools/graph-utils-worker.js");
+ let id = graphTaskId++;
+ let { promise, resolve } = Promise.defer();
+
+ worker.addEventListener("message", function listener ({ data }) {
+ if (data.id === id) {
+ worker.removeEventListener("message", listener);
+ resolve(data);
+ }
+ });
+
+ worker.postMessage({
+ id: id,
+ data: data,
+ width: width,
+ height: height,
+ dampenValuesFactor: dampenValuesFactor,
+ dataOffsetX: dataOffsetX,
+ minDistanceBetweenPoints: minDistanceBetweenPoints,
+ type: "sparsifyLineData"
+ });
+
+ return promise;
+};
--
1.8.4.2