@playcanvas/inspector 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -4
- package/package.json +1 -1
- package/src/entity-inspector-script.js +10 -0
- package/src/frame-graph-view.js +40 -9
- package/src/inspector.js +555 -24
- package/src/node-model.js +3 -2
- package/src/perf-capture.js +2034 -0
- package/src/perf-chart.js +3989 -0
- package/src/perf-copy.js +367 -0
- package/src/perf-view.js +1224 -0
- package/src/property-view.js +3 -0
- package/src/styles.js +179 -1
- package/types/entity-inspector-script.d.ts +8 -0
- package/types/frame-graph-view.d.ts +7 -0
- package/types/inspector.d.ts +199 -5
- package/types/perf-capture.d.ts +871 -0
- package/types/perf-chart.d.ts +1407 -0
- package/types/perf-copy.d.ts +61 -0
- package/types/perf-view.d.ts +231 -0
|
@@ -0,0 +1,3989 @@
|
|
|
1
|
+
import { formatBytes } from './model.js';
|
|
2
|
+
import { SCRIPT_CALLS, SCRIPT_ORDER, SCRIPT_STRIDE } from './perf-capture.js';
|
|
3
|
+
import {
|
|
4
|
+
ASSET_COLOR, ASSET_COLORS, ASSET_KINDS, CPU_PARTS, EVENT_KINDS, FAILED_COLOR, HITCH_FACTOR, MILESTONE_COLOR, OTHER_SCRIPTS_COLOR,
|
|
5
|
+
RENDER_PHASES, REQUEST_COLOR, SCRIPT_ROWS, callCount, captureNorms, effectName, eventLines, eventMask, eventUnsupported, formatCount,
|
|
6
|
+
formatDuration, formatMs, formatValue, loadColor, loadText, previousSlot, scriptColorAt, scriptLegend
|
|
7
|
+
} from './perf-view.js';
|
|
8
|
+
|
|
9
|
+
/** @import { PerfCapture, PerfLoad } from './perf-capture.js' */
|
|
10
|
+
/** @import { PerfPart, PerfScriptLegend } from './perf-view.js' */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {object} PerfChartDef
|
|
14
|
+
* @ignore
|
|
15
|
+
* @property {string} key - A key unique among the charts.
|
|
16
|
+
* @property {string} title - The title drawn beside it.
|
|
17
|
+
* @property {string} tip - What it shows, while the pointer is over its title.
|
|
18
|
+
* @property {string} total - The channel holding the sum of its parts, which sets its scale and
|
|
19
|
+
* picks the frame a column of many frames shows.
|
|
20
|
+
* @property {PerfPart[]} parts - What it stacks, bottom up.
|
|
21
|
+
* @property {PerfPart[]} [lines] - Parts of the stack drawn as lines across it, as they are
|
|
22
|
+
* counted within the parts already.
|
|
23
|
+
* @property {PerfPart[]} [plain] - What it stacks with an engine that does not break its total
|
|
24
|
+
* down: the release engine.
|
|
25
|
+
* @property {(value: number) => string} format - Formats its figures.
|
|
26
|
+
* @property {boolean} [timing] - Whether it is a duration, scaled to show the display interval
|
|
27
|
+
* as a watermark.
|
|
28
|
+
* @property {number} [unit] - The unit its scale is rounded in, such as a megabyte for memory.
|
|
29
|
+
* @property {string} [empty] - What it says while it has nothing to draw.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {object} PerfLine
|
|
34
|
+
* @ignore
|
|
35
|
+
* @property {string} key - The channel it draws.
|
|
36
|
+
* @property {string} name - The display name.
|
|
37
|
+
* @property {string} color - The color it is drawn in.
|
|
38
|
+
* @property {string} [unsupported] - Why it cannot show, where it cannot.
|
|
39
|
+
* @property {Float64Array} [values] - The ring it draws, where it is not a channel of the capture.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {object} PerfLineChart
|
|
44
|
+
* @ignore
|
|
45
|
+
* @property {PerfLine[]} lines - Its lines.
|
|
46
|
+
* @property {(value: number) => string} format - Formats their values.
|
|
47
|
+
* @property {Int32Array} pick - The frame each column showed.
|
|
48
|
+
* @property {number} min - The value at its bottom.
|
|
49
|
+
* @property {number} max - The value at its top.
|
|
50
|
+
* @property {PerfLevels|null} levels - How a height in it reads as a value, null while it has none.
|
|
51
|
+
* @property {Float64Array[]} [values] - The ring each line was drawn from, where it is not a
|
|
52
|
+
* channel of the capture.
|
|
53
|
+
* @property {boolean} [stacked] - Whether its lines are stacked, as the times of the scripts are.
|
|
54
|
+
* @property {PerfScriptLegend} [legend] - The script types it gives a band of their own.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @typedef {object} PerfScriptLine
|
|
59
|
+
* @ignore
|
|
60
|
+
* @property {string} key - The key it is hidden under.
|
|
61
|
+
* @property {string} name - The display name.
|
|
62
|
+
* @property {string} color - The color it is drawn in.
|
|
63
|
+
* @property {number} id - The script type, by its index into the names of the capture, or -1 for
|
|
64
|
+
* the other types together.
|
|
65
|
+
* @property {boolean} pinned - Whether it has its band because it is pinned.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @typedef {object} PerfLevels
|
|
70
|
+
* @ignore
|
|
71
|
+
* @property {number} top - The top of the area its values are drawn in.
|
|
72
|
+
* @property {number} bottom - Its bottom.
|
|
73
|
+
* @property {number} min - The value at the bottom.
|
|
74
|
+
* @property {number} max - The value at the top.
|
|
75
|
+
* @property {(value: number) => string} format - Formats a value.
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @typedef {object} PerfShape
|
|
80
|
+
* @ignore
|
|
81
|
+
* @property {string} total - The channel a chart is scaled to.
|
|
82
|
+
* @property {PerfPart[]} parts - What it stacks.
|
|
83
|
+
* @property {PerfPart[]} lines - Its lines across the stack.
|
|
84
|
+
* @property {Float64Array[]} partData - The ring of each part.
|
|
85
|
+
* @property {Float64Array[]} lineData - The ring of each line.
|
|
86
|
+
* @property {boolean} reduced - Whether the inspector's time is left out.
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @typedef {object} PerfRow
|
|
91
|
+
* @ignore
|
|
92
|
+
* @property {number} top - Its top edge in CSS pixels.
|
|
93
|
+
* @property {number} height - Its height.
|
|
94
|
+
* @property {number} [chart] - The index of the chart it holds, into {@link CHARTS}.
|
|
95
|
+
* @property {PerfLevels} [levels] - How a height in it reads as a value, for a chart.
|
|
96
|
+
* @property {string} [lines] - The section of the line chart it is a line of the list of:
|
|
97
|
+
* 'scripts', 'counters', 'memory' or 'resources'.
|
|
98
|
+
* @property {number} [index] - Which line of that list it is, -1 for a row that is not one.
|
|
99
|
+
* @property {'more'|'fewer'} [control] - The row of the Scripts list it is that shows more script
|
|
100
|
+
* types, or fewer.
|
|
101
|
+
* @property {string} [loading] - The row of the Loading section it is.
|
|
102
|
+
* @property {number} [kind] - The index of the kind of event it marks, into {@link EVENT_KINDS}.
|
|
103
|
+
* @property {string} [section] - The section it is the title of: 'memory', 'resources', 'events'
|
|
104
|
+
* or the key of a collapsed chart.
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @typedef {object} PerfHoverLine
|
|
109
|
+
* @ignore
|
|
110
|
+
* @property {string} text - The line.
|
|
111
|
+
* @property {string} [color] - The color of what it names, shown as a chip in front of it.
|
|
112
|
+
* @property {boolean} [indent] - Whether it is a part of the line above.
|
|
113
|
+
* @property {boolean} [wrap] - Whether it is prose, wrapped to the width of the box.
|
|
114
|
+
* @property {boolean} [strong] - Whether it is what the pointer is over, among the lines.
|
|
115
|
+
* @property {boolean} [nested] - Whether it is a part of the indented line above, as a script type
|
|
116
|
+
* among the other scripts.
|
|
117
|
+
* @property {boolean} [muted] - Whether it says about the lines rather than being one of them.
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @typedef {object} PerfTimeBar
|
|
122
|
+
* @ignore
|
|
123
|
+
* @property {string} lane - The key of the row of the frame timeline it goes on.
|
|
124
|
+
* @property {number} start - When it began, in milliseconds from the start of the frame, NaN
|
|
125
|
+
* where the engine did not time it.
|
|
126
|
+
* @property {number} duration - How long it took in milliseconds.
|
|
127
|
+
* @property {string} color - The color it is drawn in.
|
|
128
|
+
* @property {string} name - What it is.
|
|
129
|
+
* @property {boolean} positioned - Whether its start is known, rather than placed after the bar
|
|
130
|
+
* before, as a GPU pass is.
|
|
131
|
+
* @property {boolean} [instant] - Whether it has no duration, as an asset finishing loading.
|
|
132
|
+
* @property {number} [calls] - How many calls it took, as a script type does; the mean per frame
|
|
133
|
+
* for an average frame.
|
|
134
|
+
* @property {string} [label] - What its hover box calls it, where its name alone is not enough.
|
|
135
|
+
* @property {number} [script] - The script type it is, by its index into the names of the capture.
|
|
136
|
+
* @property {PerfPassLink} [pass] - The pass of the frame graph it is, which a click shows on the
|
|
137
|
+
* Frame graph tab.
|
|
138
|
+
* @property {number} [asset] - The id of the asset it is the loading of, which a click shows on the
|
|
139
|
+
* Assets tab.
|
|
140
|
+
* @property {boolean} [phase] - Whether it is a phase of the render, placed after the one before
|
|
141
|
+
* it rather than where it ran.
|
|
142
|
+
* @property {number} [labelEnd] - Where its label stops, in milliseconds into the frame: where the
|
|
143
|
+
* next bar of its lane begins.
|
|
144
|
+
* @property {number} [run] - How many passes of its name follow on in a row from it, it among
|
|
145
|
+
* them, which share one label; 0 for one inside such a run.
|
|
146
|
+
* @property {number} [runDuration] - Their time together.
|
|
147
|
+
* @property {PerfScriptType[]} [children] - What it is made of, which its hover box lists, as the
|
|
148
|
+
* script types of the scripts.
|
|
149
|
+
*/
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @typedef {object} PerfPassLink
|
|
153
|
+
* @ignore
|
|
154
|
+
* @property {string} name - The profiler name of the pass.
|
|
155
|
+
* @property {number} occurrence - Which of the passes of the frame graph with that name it is.
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @typedef {object} PerfScriptType
|
|
160
|
+
* @ignore
|
|
161
|
+
* @property {number} id - The script type, by its index into the names of the capture.
|
|
162
|
+
* @property {string} name - Its name.
|
|
163
|
+
* @property {number} duration - Its own time in an update or a post-update, in milliseconds.
|
|
164
|
+
* @property {number} [calls] - How many calls that took.
|
|
165
|
+
* @property {number} [order] - Where its first call came among the types', 1 for the first.
|
|
166
|
+
* @property {string} color - The color it is drawn in.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @typedef {object} PerfBar
|
|
171
|
+
* @ignore
|
|
172
|
+
* @property {number} x0 - Its left edge in CSS pixels.
|
|
173
|
+
* @property {number} x1 - Its right edge.
|
|
174
|
+
* @property {string} lane - The key of the row of the frame timeline it is on.
|
|
175
|
+
* @property {string} name - What it is.
|
|
176
|
+
* @property {string} color - The color it is drawn in.
|
|
177
|
+
* @property {number} duration - How long it took in milliseconds, zero for an instant.
|
|
178
|
+
* @property {number} start - When it began, in milliseconds into the frame, NaN where unknown.
|
|
179
|
+
* @property {number} [calls] - How many calls it took.
|
|
180
|
+
* @property {string} [label] - What its hover box calls it.
|
|
181
|
+
* @property {number} [script] - The script type it is, which a click shows on the Scripts tab.
|
|
182
|
+
* @property {PerfPassLink} [pass] - The pass of the frame graph it is, which a click shows on the
|
|
183
|
+
* Frame graph tab.
|
|
184
|
+
* @property {number} [asset] - The id of the asset it is the loading of.
|
|
185
|
+
* @property {PerfScriptType[]} [children] - What it is made of.
|
|
186
|
+
*/
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The charts, top to bottom, above the rows of events.
|
|
190
|
+
*
|
|
191
|
+
* @type {PerfChartDef[]}
|
|
192
|
+
*/
|
|
193
|
+
const CHARTS = [
|
|
194
|
+
{
|
|
195
|
+
key: 'frame',
|
|
196
|
+
title: 'Frame',
|
|
197
|
+
tip: 'From the start of each frame to the start of the next, including any wait for the display',
|
|
198
|
+
total: 'interval',
|
|
199
|
+
format: formatMs,
|
|
200
|
+
timing: true,
|
|
201
|
+
parts: [{ key: 'interval', name: 'Frame', color: '#ffd60a', channel: 'interval' }]
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
key: 'cpu',
|
|
205
|
+
title: 'CPU',
|
|
206
|
+
tip: 'Main-thread time of the update and the render, by part. What the frame takes beyond it is spent outside the engine',
|
|
207
|
+
total: 'cpu',
|
|
208
|
+
format: formatMs,
|
|
209
|
+
timing: true,
|
|
210
|
+
parts: CPU_PARTS
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
key: 'gpu',
|
|
214
|
+
title: 'GPU',
|
|
215
|
+
tip: 'The time the GPU took for each frame',
|
|
216
|
+
total: 'gpu',
|
|
217
|
+
format: formatMs,
|
|
218
|
+
timing: true,
|
|
219
|
+
empty: 'Turn GPU timings on to record the GPU time of each frame',
|
|
220
|
+
parts: [{ key: 'gpu', name: 'GPU', color: '#bf5af2', channel: 'gpu' }]
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
key: 'draws',
|
|
224
|
+
title: 'Draw calls',
|
|
225
|
+
tip: 'The draw calls each frame submitted, by kind with the profiler and debug engines. Skinned draws are counted\nwithin the others, so they are a line across them',
|
|
226
|
+
total: 'draws',
|
|
227
|
+
format: formatCount,
|
|
228
|
+
parts: [
|
|
229
|
+
{ key: 'forward', name: 'Forward', color: '#64d2ff', channel: 'drawsForward' },
|
|
230
|
+
{ key: 'shadow', name: 'Shadow', color: '#5e5ce6', channel: 'drawsShadow' },
|
|
231
|
+
{ key: 'misc', name: 'Other', color: '#ff9f0a', channel: 'drawsMisc' }
|
|
232
|
+
],
|
|
233
|
+
lines: [{ key: 'skinned', name: 'Skinned', color: '#30d158', channel: 'drawsSkinned' }],
|
|
234
|
+
plain: [{ key: 'draws', name: 'Draw calls', color: '#64d2ff', channel: 'draws' }]
|
|
235
|
+
}
|
|
236
|
+
];
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The memory lines of the Memory chart, sharing its scale. The JavaScript heap is reported by
|
|
240
|
+
* Chromium browsers only.
|
|
241
|
+
*
|
|
242
|
+
* @type {{ key: string, name: string, color: string }[]}
|
|
243
|
+
*/
|
|
244
|
+
const MEMORY = [
|
|
245
|
+
{ key: 'heap', name: 'JS heap', color: '#ffd60a' },
|
|
246
|
+
{ key: 'vram', name: 'Video memory', color: '#e5e5ea' },
|
|
247
|
+
{ key: 'vramTex', name: 'Textures', color: '#ff9f0a' },
|
|
248
|
+
{ key: 'vramVb', name: 'Vertex buffers', color: '#30d158' },
|
|
249
|
+
{ key: 'vramIb', name: 'Index buffers', color: '#00c7be' },
|
|
250
|
+
{ key: 'vramUb', name: 'Uniform buffers', color: '#0a84ff' },
|
|
251
|
+
{ key: 'vramSb', name: 'Storage buffers', color: '#bf5af2' }
|
|
252
|
+
];
|
|
253
|
+
|
|
254
|
+
// the smallest range the Memory chart spans, so the noise of a level that barely changes stays
|
|
255
|
+
// flat: a share of the largest value, or a fixed amount, whichever is larger
|
|
256
|
+
const MEMORY_MIN_SHARE = 0.01;
|
|
257
|
+
const MEMORY_MIN_BYTES = 256 * 1024;
|
|
258
|
+
|
|
259
|
+
// the least range of the Resources chart, so a count changing by one does not fill it
|
|
260
|
+
const RESOURCE_MIN_COUNT = 4;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The resources on the device, a line each in the Resources section. Pipelines only exist on
|
|
264
|
+
* WebGPU.
|
|
265
|
+
*
|
|
266
|
+
* @type {{ key: string, name: string, color: string }[]}
|
|
267
|
+
*/
|
|
268
|
+
const RESOURCES = [
|
|
269
|
+
{ key: 'textures', name: 'Textures', color: '#ff9f0a' },
|
|
270
|
+
{ key: 'targets', name: 'Render targets', color: '#ff375f' },
|
|
271
|
+
{ key: 'shaders', name: 'Shaders', color: '#bf5af2' },
|
|
272
|
+
{ key: 'buffers', name: 'Buffers', color: '#30d158' },
|
|
273
|
+
{ key: 'pipelines', name: 'Pipelines', color: '#64d2ff' }
|
|
274
|
+
];
|
|
275
|
+
|
|
276
|
+
// the phases of the render on the frame timeline, with their colors: shades around the green of
|
|
277
|
+
// the render
|
|
278
|
+
const RENDER_TIMELINE = RENDER_PHASES.map(([key, name], i) => [key, name, ['#66d4cf', '#34c759', '#8be0a4', '#00c7be', '#64d2ff', '#a2e85b', '#30b0c7', '#c4f0a0'][i]]);
|
|
279
|
+
|
|
280
|
+
// the colors GPU passes cycle through in the frame timeline
|
|
281
|
+
const PASS_COLORS = ['#bf5af2', '#0a84ff', '#30d158', '#ff9f0a', '#ff375f', '#64d2ff', '#ffd60a', '#5e5ce6'];
|
|
282
|
+
|
|
283
|
+
// the color of the update in the frame timeline, under its parts
|
|
284
|
+
const UPDATE_COLOR = '#2f6fdb';
|
|
285
|
+
|
|
286
|
+
// the shortest time the charts spread over, in milliseconds, so a short capture does not draw as a
|
|
287
|
+
// few wide bars
|
|
288
|
+
const MIN_SPAN = 5000;
|
|
289
|
+
|
|
290
|
+
// the shortest part of a frame, in milliseconds, the timeline zooms in to
|
|
291
|
+
const MIN_TIME_SPAN = 0.05;
|
|
292
|
+
|
|
293
|
+
// the shortest time the charts zoom in to, in milliseconds
|
|
294
|
+
const MIN_ZOOM = 250;
|
|
295
|
+
|
|
296
|
+
// how far above the 95th percentile of its columns a chart scales to: beyond that a spike is cut
|
|
297
|
+
// off, marked along the top, rather than flattening everything else
|
|
298
|
+
const SPIKE_HEADROOM = 3;
|
|
299
|
+
|
|
300
|
+
// how far above the display interval a timing chart scales at least, so its watermark has room
|
|
301
|
+
const BUDGET_HEADROOM = 1.15;
|
|
302
|
+
|
|
303
|
+
// how often the charts redraw while recording, in milliseconds
|
|
304
|
+
const REDRAW_INTERVAL = 250;
|
|
305
|
+
|
|
306
|
+
// the most bars of a row of the frame timeline its hover box lists
|
|
307
|
+
const TIMELINE_HOVER_BARS = 12;
|
|
308
|
+
|
|
309
|
+
// the longest line of a hover box that keeps to one line, in characters
|
|
310
|
+
const HOVER_LINE_CHARS = 52;
|
|
311
|
+
|
|
312
|
+
// the most script types and GPU passes a hover box lists: the script types of the timeline and
|
|
313
|
+
// those among the other scripts of the Scripts chart, and the GPU passes and effects of the timeline
|
|
314
|
+
const HOVER_SCRIPT_TYPES = 20;
|
|
315
|
+
|
|
316
|
+
// the most frames of a column the details of its events list
|
|
317
|
+
const HOVER_FRAMES = 3;
|
|
318
|
+
|
|
319
|
+
// the width of the names to the left of the charts and the timeline
|
|
320
|
+
const GUTTER = 108;
|
|
321
|
+
// the least and the most the user can make it, and the least of the plot it leaves
|
|
322
|
+
const MIN_GUTTER = 70;
|
|
323
|
+
const MAX_GUTTER = 320;
|
|
324
|
+
const MIN_PLOT = 160;
|
|
325
|
+
// how far either side of the edge of the plot a press takes the divider beside the names
|
|
326
|
+
const DIVIDER_REACH = 4;
|
|
327
|
+
|
|
328
|
+
const CHART_GAP = 3;
|
|
329
|
+
// the least height of a chart: below it, the charts area scrolls rather than squeeze them further
|
|
330
|
+
const MIN_CHART_HEIGHT = 30;
|
|
331
|
+
|
|
332
|
+
// the dash patterns of the display interval line, made once
|
|
333
|
+
const DASH = [3, 3];
|
|
334
|
+
const NO_DASH = [];
|
|
335
|
+
|
|
336
|
+
// no parts, shared
|
|
337
|
+
const NO_PARTS = /** @type {PerfPart[]} */ ([]);
|
|
338
|
+
|
|
339
|
+
const SECTION_TITLE = 14;
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The rows of the Loading section, top down, with their heights and what each shows.
|
|
343
|
+
*
|
|
344
|
+
* @type {{ key: string, name: string, height: number, tip: string }[]}
|
|
345
|
+
*/
|
|
346
|
+
const LOADING_ROWS = [
|
|
347
|
+
{ key: 'milestones', name: 'Milestones', height: 12, tip: 'Moments of the page and the app: the page arriving and loading, its first paints, the preload, the app starting and its first frame' },
|
|
348
|
+
{ key: 'assets', name: 'Assets', height: 24, tip: 'Assets loading, from asking for them to their resource being ready: the request of the file solid, what came after, as parsing and uploading, faint. The color is the kind of asset' },
|
|
349
|
+
{ key: 'requests', name: 'Requests', height: 14, tip: 'How many requests of the page were in flight' }
|
|
350
|
+
];
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* @returns {PerfHoverLine[]} The key to the colors of assets loading: each kind with its asset
|
|
354
|
+
* types, the other types, and the assets that failed.
|
|
355
|
+
*/
|
|
356
|
+
function assetKey() {
|
|
357
|
+
return [
|
|
358
|
+
...ASSET_KINDS.map(kind => ({ text: `${kind.name}: ${kind.types.join(', ')}`, color: kind.color, indent: true })),
|
|
359
|
+
{ text: 'Other types', color: ASSET_COLOR, indent: true },
|
|
360
|
+
{ text: 'Failed', color: FAILED_COLOR, indent: true }
|
|
361
|
+
];
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// the rows the assets are packed into, and the height of each
|
|
365
|
+
const ASSET_LANES = 3;
|
|
366
|
+
const ASSET_LANE = 7;
|
|
367
|
+
|
|
368
|
+
const SCRIPT_ROW = 13;
|
|
369
|
+
// the fewest rows the Scripts chart is tall, for an app of few script types
|
|
370
|
+
const SCRIPT_CHART_ROWS = 3;
|
|
371
|
+
const MEMORY_ROW = 13;
|
|
372
|
+
// the least range of a counter's line, a share of its largest value or else a least amount, so a
|
|
373
|
+
// counter that barely changes is flat
|
|
374
|
+
const COUNTER_MIN_SHARE = 0.05;
|
|
375
|
+
const COUNTER_MIN_RANGE = 1e-3;
|
|
376
|
+
const COUNTERS_TIP = 'Counters the app keeps in app.stats.user, as each frame ended: set a counter by name, and reset it on frameupdate to count per frame. Each line has a scale of its own, as their units are the app\'s';
|
|
377
|
+
const RESOURCE_ROW = 13;
|
|
378
|
+
const EVENT_ROW = 10;
|
|
379
|
+
const TIMELINE_HEIGHT = 88;
|
|
380
|
+
// the space between the groups of lanes of the timeline, the CPU, the GPU, the events and what
|
|
381
|
+
// loaded, with a line across it, and the height of the timeline collapsed to its title
|
|
382
|
+
const TIMELINE_GROUP_GAP = 6;
|
|
383
|
+
const TIMELINE_TITLE = 16;
|
|
384
|
+
// the lanes of the timeline the assets and the requests of an entry are packed into, at most, and
|
|
385
|
+
// how much taller the timeline is for each lane of loading
|
|
386
|
+
const TIMELINE_ASSET_LANES = 4;
|
|
387
|
+
const TIMELINE_REQUEST_LANES = 3;
|
|
388
|
+
const LOADING_LINE = 11;
|
|
389
|
+
|
|
390
|
+
// how much taller the timeline is with its line of script types, and with its line of the passes
|
|
391
|
+
// of the GPU effects
|
|
392
|
+
const SCRIPT_TYPES_LINE = 13;
|
|
393
|
+
const GPU_PASSES_LINE = 11;
|
|
394
|
+
const FONT = '10px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif';
|
|
395
|
+
const BOLD = `600 ${FONT}`;
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* @param {number} value - A positive value.
|
|
399
|
+
* @returns {number} The smallest round number at or above it: one of a few steps between
|
|
400
|
+
* consecutive powers of ten, close enough together that a chart keeps most of its height.
|
|
401
|
+
*/
|
|
402
|
+
function niceCeil(value) {
|
|
403
|
+
if (!(value > 0)) return 1;
|
|
404
|
+
const power = 10 ** Math.floor(Math.log10(value));
|
|
405
|
+
for (const step of [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]) {
|
|
406
|
+
if (step * power >= value) return step * power;
|
|
407
|
+
}
|
|
408
|
+
return 10 * power;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* A horizontal scrollbar: a track with a thumb sized to the part of something in view. The thumb
|
|
413
|
+
* is dragged to pan, the track beside it clicked to page and the bar scrolled over to pan. It only
|
|
414
|
+
* says how the user moves it: its owner moves the view, and sets the thumb.
|
|
415
|
+
*
|
|
416
|
+
* @ignore
|
|
417
|
+
*/
|
|
418
|
+
class PerfScrollbar {
|
|
419
|
+
/**
|
|
420
|
+
* The bar, to place.
|
|
421
|
+
*
|
|
422
|
+
* @type {HTMLElement}
|
|
423
|
+
*/
|
|
424
|
+
el;
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* @type {HTMLElement}
|
|
428
|
+
* @private
|
|
429
|
+
*/
|
|
430
|
+
_thumb;
|
|
431
|
+
|
|
432
|
+
/** @private */
|
|
433
|
+
_active = false;
|
|
434
|
+
|
|
435
|
+
/** @private */
|
|
436
|
+
_start = 0;
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* The thumb being dragged: where the pointer went down and where the thumb started then.
|
|
440
|
+
*
|
|
441
|
+
* @type {{ x: number, start: number }|null}
|
|
442
|
+
* @private
|
|
443
|
+
*/
|
|
444
|
+
_drag = null;
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* @param {Document} doc - The document to make it in.
|
|
448
|
+
* @param {object} handlers - What the user does with it.
|
|
449
|
+
* @param {(start: number) => void} handlers.onDrag - The thumb was dragged to start at this
|
|
450
|
+
* fraction of the bar.
|
|
451
|
+
* @param {(direction: number) => void} handlers.onPage - The track was clicked: -1 left of the
|
|
452
|
+
* thumb, 1 right of it.
|
|
453
|
+
* @param {(delta: number) => void} handlers.onWheel - The bar was scrolled over, by this many
|
|
454
|
+
* pixels, sideways or down.
|
|
455
|
+
*/
|
|
456
|
+
constructor(doc, { onDrag, onPage, onWheel }) {
|
|
457
|
+
this.el = doc.createElement('div');
|
|
458
|
+
this.el.className = 'pci-perf-hscroll pci-inactive';
|
|
459
|
+
this._thumb = doc.createElement('div');
|
|
460
|
+
this._thumb.className = 'pci-perf-hscroll-thumb';
|
|
461
|
+
this.el.appendChild(this._thumb);
|
|
462
|
+
|
|
463
|
+
this.el.addEventListener('pointerdown', (e) => {
|
|
464
|
+
if (e.button !== 0 || e.ctrlKey || !this._active) return;
|
|
465
|
+
e.preventDefault();
|
|
466
|
+
if (e.target === this._thumb) {
|
|
467
|
+
this.el.setPointerCapture?.(e.pointerId);
|
|
468
|
+
this._drag = { x: e.clientX, start: this._start };
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
onPage(e.clientX < this._thumb.getBoundingClientRect().left ? -1 : 1);
|
|
472
|
+
});
|
|
473
|
+
this.el.addEventListener('pointermove', (e) => {
|
|
474
|
+
if (this._drag) onDrag(this._drag.start + (e.clientX - this._drag.x) / this.el.getBoundingClientRect().width);
|
|
475
|
+
});
|
|
476
|
+
const up = () => {
|
|
477
|
+
this._drag = null;
|
|
478
|
+
};
|
|
479
|
+
this.el.addEventListener('pointerup', up);
|
|
480
|
+
this.el.addEventListener('pointercancel', up);
|
|
481
|
+
this.el.addEventListener('contextmenu', up);
|
|
482
|
+
this.el.addEventListener('wheel', (e) => {
|
|
483
|
+
if (!this._active) return;
|
|
484
|
+
e.preventDefault();
|
|
485
|
+
onWheel(Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY);
|
|
486
|
+
}, { passive: false });
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Sizes the thumb. It fills the bar, dimmed, while everything is in view.
|
|
491
|
+
*
|
|
492
|
+
* @param {number} start - Where the view starts, as a fraction of the whole.
|
|
493
|
+
* @param {number} size - How much of the whole is in view.
|
|
494
|
+
*/
|
|
495
|
+
set(start, size) {
|
|
496
|
+
this._active = size < 1;
|
|
497
|
+
this._start = this._active ? start : 0;
|
|
498
|
+
this._thumb.style.left = `${(100 * this._start).toFixed(3)}%`;
|
|
499
|
+
this._thumb.style.width = `${(100 * (this._active ? size : 1)).toFixed(3)}%`;
|
|
500
|
+
this.el.classList.toggle('pci-inactive', !this._active);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* The charts of a capture, a column per frame or per group of frames, with the frame they show
|
|
506
|
+
* picked by a cursor that every chart shares, the events of the frames in a row per kind below
|
|
507
|
+
* them, and below that the timeline of the frame shown. A column of many frames shows the one
|
|
508
|
+
* with the largest figure in that chart, so a spike never hides between pixels, and clicking the
|
|
509
|
+
* column picks that frame. Hovering a column says what it shows.
|
|
510
|
+
*
|
|
511
|
+
* Click a frame to show it, drag to select frames together, scroll to zoom, scroll with Shift to
|
|
512
|
+
* pan and double-click to see the whole capture again.
|
|
513
|
+
*
|
|
514
|
+
* @ignore
|
|
515
|
+
*/
|
|
516
|
+
class PerfCharts {
|
|
517
|
+
/**
|
|
518
|
+
* Called when the frame shown or the selected frames change.
|
|
519
|
+
*
|
|
520
|
+
* @type {() => void}
|
|
521
|
+
*/
|
|
522
|
+
onChange = () => {};
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* The frame picked, or -1 to follow the newest.
|
|
526
|
+
*
|
|
527
|
+
* @type {number}
|
|
528
|
+
*/
|
|
529
|
+
picked = -1;
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* The frames selected together, inclusive, or null.
|
|
533
|
+
*
|
|
534
|
+
* @type {{ from: number, to: number }|null}
|
|
535
|
+
*/
|
|
536
|
+
range = null;
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* The charts and the sections under them that are collapsed to their titles: the keys of
|
|
540
|
+
* {@link CHARTS}, 'resources' and 'events'.
|
|
541
|
+
*
|
|
542
|
+
* @type {Set<string>}
|
|
543
|
+
*/
|
|
544
|
+
collapsed = new Set();
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* The lines of the Memory and Resources charts, and the rows of events, that are hidden: each
|
|
548
|
+
* by its section and key, such as 'memory:vramTex' or 'events:resource'.
|
|
549
|
+
*
|
|
550
|
+
* @type {Set<string>}
|
|
551
|
+
*/
|
|
552
|
+
hidden = new Set();
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Called when a section is collapsed or expanded, or a line hidden or shown: what is kept
|
|
556
|
+
* between sessions.
|
|
557
|
+
*
|
|
558
|
+
* @type {() => void}
|
|
559
|
+
*/
|
|
560
|
+
onCollapse = () => {};
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* How many of the longest script types the Scripts chart gives a band of their own, besides
|
|
564
|
+
* those pinned: a multiple of {@link SCRIPT_ROWS}, more with each Show more.
|
|
565
|
+
*
|
|
566
|
+
* @type {number}
|
|
567
|
+
*/
|
|
568
|
+
scriptRows = SCRIPT_ROWS;
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* The names of the script types pinned, which the Scripts chart gives a band of their own
|
|
572
|
+
* whatever their time.
|
|
573
|
+
*
|
|
574
|
+
* @type {Set<string>}
|
|
575
|
+
*/
|
|
576
|
+
pinned = new Set();
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Called with a script type clicked on the timeline, by its index into the names of the
|
|
580
|
+
* capture, to show it on the Scripts tab.
|
|
581
|
+
*
|
|
582
|
+
* @type {(id: number) => void}
|
|
583
|
+
*/
|
|
584
|
+
onSelectScript = () => {};
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Called with the id of an asset clicked in the Loading section, to show it on the Assets tab.
|
|
588
|
+
*
|
|
589
|
+
* @type {(id: number) => void}
|
|
590
|
+
*/
|
|
591
|
+
onSelectAsset = () => {};
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Called with a GPU pass or effect clicked on the timeline, to show it on the Frame graph tab.
|
|
595
|
+
*
|
|
596
|
+
* @type {(pass: PerfPassLink) => void}
|
|
597
|
+
*/
|
|
598
|
+
onSelectPass = () => {};
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Whether the CPU chart includes the inspector's own time. It runs inside the app's update and
|
|
602
|
+
* can dwarf a light app, so leaving it out scales the chart to the app's own time.
|
|
603
|
+
*
|
|
604
|
+
* @type {boolean}
|
|
605
|
+
*/
|
|
606
|
+
showInspector = true;
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* @type {PerfCapture}
|
|
610
|
+
* @private
|
|
611
|
+
*/
|
|
612
|
+
_capture;
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* The frames in view while zoomed in, null to view the whole capture. A view reaching the
|
|
616
|
+
* newest frame follows it as frames are recorded.
|
|
617
|
+
*
|
|
618
|
+
* @type {{ from: number, span: number, follow: boolean }|null}
|
|
619
|
+
* @private
|
|
620
|
+
*/
|
|
621
|
+
_view = null;
|
|
622
|
+
|
|
623
|
+
/** @private */
|
|
624
|
+
_hover = -1;
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* The level under the pointer in a chart: a line across it there, labelled with its value, to
|
|
628
|
+
* hold samples against. Null while the pointer is not over a chart.
|
|
629
|
+
*
|
|
630
|
+
* @type {{ y: number, value: number, format: (value: number) => string }|null}
|
|
631
|
+
* @private
|
|
632
|
+
*/
|
|
633
|
+
_level = null;
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* The bars of the frame timeline last drawn, and the one under the pointer.
|
|
637
|
+
*
|
|
638
|
+
* @type {PerfBar[]}
|
|
639
|
+
* @private
|
|
640
|
+
*/
|
|
641
|
+
_bars = [];
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* The rows of the frame timeline last drawn.
|
|
645
|
+
*
|
|
646
|
+
* @type {{ key: string, name: string, title: string, y: number, h: number }[]}
|
|
647
|
+
* @private
|
|
648
|
+
*/
|
|
649
|
+
_lanes = [];
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* The frame each column of each chart showed when last drawn, to pick it on a click.
|
|
653
|
+
*
|
|
654
|
+
* @type {Int32Array[]}
|
|
655
|
+
* @private
|
|
656
|
+
*/
|
|
657
|
+
_columns = [];
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Arrays kept between redraws, by what they are for.
|
|
662
|
+
*
|
|
663
|
+
* @type {Map<string, Int32Array|Float64Array>}
|
|
664
|
+
* @private
|
|
665
|
+
*/
|
|
666
|
+
_scratch = new Map();
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* The ways each chart can be drawn, made once, see {@link _shape}.
|
|
670
|
+
*
|
|
671
|
+
* @type {Map<string, PerfShape>}
|
|
672
|
+
* @private
|
|
673
|
+
*/
|
|
674
|
+
_shapes = new Map();
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* What the Loading section last drew, by row, for the pointer: each item across the canvas, in
|
|
678
|
+
* CSS pixels, with its lane among the assets.
|
|
679
|
+
*
|
|
680
|
+
* @type {Record<string, { x0: number, x1: number, lane: number, load: any }[]>}
|
|
681
|
+
* @private
|
|
682
|
+
*/
|
|
683
|
+
_loadingDrawn = {};
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* The script types the Scripts chart last drew, telling when a new selection changes them.
|
|
687
|
+
*
|
|
688
|
+
* @private
|
|
689
|
+
*/
|
|
690
|
+
_legendSignature = '';
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* The line charts, and the stacked chart of the scripts, as last drawn, by section, null while
|
|
694
|
+
* collapsed or empty.
|
|
695
|
+
*
|
|
696
|
+
* @type {{ scripts: PerfLineChart|null, memory: PerfLineChart|null, resources: PerfLineChart|null }}
|
|
697
|
+
* @private
|
|
698
|
+
*/
|
|
699
|
+
_lineCharts = { scripts: null, counters: null, memory: null, resources: null };
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* The first and one past the last frame of each column when last drawn.
|
|
703
|
+
*
|
|
704
|
+
* @type {{ lo: Int32Array, hi: Int32Array }}
|
|
705
|
+
* @private
|
|
706
|
+
*/
|
|
707
|
+
_spans = { lo: new Int32Array(0), hi: new Int32Array(0) };
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* The charts and rows of events when last drawn.
|
|
711
|
+
*
|
|
712
|
+
* @type {PerfRow[]}
|
|
713
|
+
* @private
|
|
714
|
+
*/
|
|
715
|
+
_rows = [];
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* What needs drawing on the next {@link update}: the charts, the frame timeline, and the
|
|
719
|
+
* overlay marking the frame shown, the frame hovered and the frames selected together. The
|
|
720
|
+
* overlay is a canvas of its own, so moving the pointer redraws only it.
|
|
721
|
+
*
|
|
722
|
+
* @private
|
|
723
|
+
*/
|
|
724
|
+
_drawn = { version: -1, time: 0, charts: true, timeline: true, overlay: true };
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Where the charts last drew their frames, which the overlay and the pointer go by, so they
|
|
728
|
+
* match what is on screen between redraws while frames are being recorded.
|
|
729
|
+
*
|
|
730
|
+
* @private
|
|
731
|
+
*/
|
|
732
|
+
_layout = { from: 0, span: MIN_SPAN, plot: 1, bottom: 0 };
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* The height the charts and their sections were last laid out in, which is taller than their
|
|
736
|
+
* box when they do not fit it, so the box scrolls.
|
|
737
|
+
*
|
|
738
|
+
* @private
|
|
739
|
+
*/
|
|
740
|
+
_contentHeight = 0;
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* The press on the charts being held: where it went down, the frame there, whether it drags
|
|
744
|
+
* out a range, and the pointer it took. A press with Shift while zoomed in pans instead, from
|
|
745
|
+
* where the view began, once it has moved, and one on the divider beside the names drags it.
|
|
746
|
+
*
|
|
747
|
+
* @type {{ x: number, seq: number, ranging: boolean, pointerId: number, pan?: boolean, from?: number, moved?: boolean, resize?: boolean, clientX?: number, gutter?: number }|null}
|
|
748
|
+
* @private
|
|
749
|
+
*/
|
|
750
|
+
_press = null;
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Where the pointer is across the charts right of the names, NaN while it is not over them.
|
|
754
|
+
*
|
|
755
|
+
* @type {number}
|
|
756
|
+
* @private
|
|
757
|
+
*/
|
|
758
|
+
_pointerX = NaN;
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Whether the divider beside the names is under the pointer or being dragged.
|
|
762
|
+
*
|
|
763
|
+
* @type {boolean}
|
|
764
|
+
* @private
|
|
765
|
+
*/
|
|
766
|
+
_dividerHot = false;
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* The width of the names left of the charts and the timeline.
|
|
770
|
+
*
|
|
771
|
+
* @type {number}
|
|
772
|
+
* @private
|
|
773
|
+
*/
|
|
774
|
+
_gutter = GUTTER;
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* The press on the frame timeline being held, which drags the divider beside the names or,
|
|
778
|
+
* with Shift while zoomed in, pans.
|
|
779
|
+
*
|
|
780
|
+
* @type {{ x: number, pointerId: number, resize?: boolean, clientX?: number, gutter?: number, pan?: boolean, from?: number, moved?: boolean }|null}
|
|
781
|
+
* @private
|
|
782
|
+
*/
|
|
783
|
+
_timePress = null;
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Where the pointer is across the timeline right of the names, NaN while it is not over it.
|
|
787
|
+
*
|
|
788
|
+
* @type {number}
|
|
789
|
+
* @private
|
|
790
|
+
*/
|
|
791
|
+
_timePointerX = NaN;
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* The cursor of the timeline for what is under the pointer: a hand over a bar a click shows.
|
|
795
|
+
*
|
|
796
|
+
* @type {string}
|
|
797
|
+
* @private
|
|
798
|
+
*/
|
|
799
|
+
_timeCursor = '';
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Whether a press on the timeline dragged, so the click it ends in does nothing.
|
|
803
|
+
*
|
|
804
|
+
* @type {boolean}
|
|
805
|
+
* @private
|
|
806
|
+
*/
|
|
807
|
+
_timeDragged = false;
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Where the title of the timeline ends across it, which a click collapses or expands it up to.
|
|
811
|
+
*
|
|
812
|
+
* @type {number}
|
|
813
|
+
* @private
|
|
814
|
+
*/
|
|
815
|
+
_timelineTitleEnd = 0;
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* The window whose Shift key the cursor follows while the pointer is over the charts.
|
|
819
|
+
*
|
|
820
|
+
* @type {Window|null}
|
|
821
|
+
* @private
|
|
822
|
+
*/
|
|
823
|
+
_keyView = null;
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* What each hover box shows, to rebuild its lines only when that changes.
|
|
827
|
+
*
|
|
828
|
+
* @type {WeakMap<HTMLElement, string>}
|
|
829
|
+
* @private
|
|
830
|
+
*/
|
|
831
|
+
_hoverSignatures = new WeakMap();
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Redraws the charts and the timeline as soon as their boxes change size, before the browser
|
|
835
|
+
* paints them stretched.
|
|
836
|
+
*
|
|
837
|
+
* @type {ResizeObserver|null}
|
|
838
|
+
* @private
|
|
839
|
+
*/
|
|
840
|
+
_resizeObserver = null;
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* The part of the frame the timeline shows while zoomed in, in milliseconds from its start, or
|
|
844
|
+
* null for all of it.
|
|
845
|
+
*
|
|
846
|
+
* @type {{ from: number, span: number }|null}
|
|
847
|
+
* @private
|
|
848
|
+
*/
|
|
849
|
+
_timeView = null;
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Where the timeline last drew: the part of the frame in view, the length of the frame and the
|
|
853
|
+
* width it was drawn across.
|
|
854
|
+
*
|
|
855
|
+
* @private
|
|
856
|
+
*/
|
|
857
|
+
_timeLayout = { from: 0, span: 1, length: 1, plot: 1 };
|
|
858
|
+
|
|
859
|
+
/**
|
|
860
|
+
* The height of the timeline, taller while it has a line of script types.
|
|
861
|
+
*
|
|
862
|
+
* @private
|
|
863
|
+
*/
|
|
864
|
+
_timelineHeight = TIMELINE_HEIGHT;
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* The size of the charts and of the timeline, as the resize observer last reported them. A
|
|
868
|
+
* draw goes by these rather than asking the page, which would lay out the whole panel there
|
|
869
|
+
* and then after the property view changed it, inside the app's frame.
|
|
870
|
+
*
|
|
871
|
+
* @type {Map<Element, { width: number, height: number }>}
|
|
872
|
+
* @private
|
|
873
|
+
*/
|
|
874
|
+
_sizes = new Map();
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* The window the resize observer belongs to. The panel can move into a window of its own,
|
|
878
|
+
* whose observer it then needs.
|
|
879
|
+
*
|
|
880
|
+
* @type {Window|null}
|
|
881
|
+
* @private
|
|
882
|
+
*/
|
|
883
|
+
_resizeView = null;
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* @param {HTMLElement} container - The element to fill.
|
|
887
|
+
* @param {PerfCapture} capture - The capture to chart.
|
|
888
|
+
*/
|
|
889
|
+
constructor(container, capture) {
|
|
890
|
+
this._capture = capture;
|
|
891
|
+
const doc = container.ownerDocument;
|
|
892
|
+
|
|
893
|
+
this._chartsEl = doc.createElement('div');
|
|
894
|
+
this._chartsEl.className = 'pci-perf-charts';
|
|
895
|
+
this._canvas = doc.createElement('canvas');
|
|
896
|
+
this._overlay = doc.createElement('canvas');
|
|
897
|
+
this._overlay.className = 'pci-perf-overlay';
|
|
898
|
+
this._chartsEl.append(this._canvas, this._overlay);
|
|
899
|
+
// the hover box of the charts lives in the panel around them, so the scrolling of the
|
|
900
|
+
// charts does not cut it off
|
|
901
|
+
this._panelEl = container;
|
|
902
|
+
this._panelEl.classList.add('pci-perf');
|
|
903
|
+
this._hoverEl = doc.createElement('div');
|
|
904
|
+
this._hoverEl.className = 'pci-perf-hover';
|
|
905
|
+
|
|
906
|
+
this._timelineEl = doc.createElement('div');
|
|
907
|
+
this._timelineEl.className = 'pci-perf-timeline';
|
|
908
|
+
this._timelineEl.style.height = `${TIMELINE_HEIGHT}px`;
|
|
909
|
+
this._timeline = doc.createElement('canvas');
|
|
910
|
+
this._timelineHoverEl = doc.createElement('div');
|
|
911
|
+
this._timelineHoverEl.className = 'pci-perf-hover';
|
|
912
|
+
this._timelineEl.append(this._timeline, this._timelineHoverEl);
|
|
913
|
+
|
|
914
|
+
// a scrollbar under the plot of the charts, for the frames in view while zoomed in, and one
|
|
915
|
+
// under the timeline, for the part of the frame in view
|
|
916
|
+
this._scrollbar = new PerfScrollbar(doc, {
|
|
917
|
+
onDrag: start => this._scrollTo(this._capture.startPos + start * Math.max(1, this._capture.endPos - this._capture.startPos)),
|
|
918
|
+
onPage: direction => this._scrollTo(this._span().from + direction * this._span().span),
|
|
919
|
+
onWheel: delta => this._scrollTo(this._span().from + (delta / this._layout.plot) * this._span().span)
|
|
920
|
+
});
|
|
921
|
+
this._timeScrollbar = new PerfScrollbar(doc, {
|
|
922
|
+
onDrag: start => this._scrollTimeTo(start * this._timeLayout.length),
|
|
923
|
+
onPage: direction => this._scrollTimeTo(this._timeLayout.from + direction * this._timeLayout.span),
|
|
924
|
+
onWheel: delta => this._scrollTimeTo(this._timeLayout.from + (delta / this._timeLayout.plot) * this._timeLayout.span)
|
|
925
|
+
});
|
|
926
|
+
for (const bar of [this._scrollbar, this._timeScrollbar]) bar.el.style.marginLeft = `${8 + this._gutter}px`;
|
|
927
|
+
|
|
928
|
+
container.append(this._chartsEl, this._scrollbar.el, this._timelineEl, this._timeScrollbar.el, this._hoverEl);
|
|
929
|
+
|
|
930
|
+
const canvas = this._canvas;
|
|
931
|
+
canvas.addEventListener('pointerdown', this._onPointerDown);
|
|
932
|
+
canvas.addEventListener('pointermove', this._onPointerMove);
|
|
933
|
+
canvas.addEventListener('pointerup', this._onPointerUp);
|
|
934
|
+
canvas.addEventListener('pointercancel', this._onPointerUp);
|
|
935
|
+
canvas.addEventListener('pointerleave', this._onPointerLeave);
|
|
936
|
+
canvas.addEventListener('wheel', this._onWheel, { passive: false });
|
|
937
|
+
canvas.addEventListener('dblclick', this._onDoubleClick);
|
|
938
|
+
canvas.addEventListener('contextmenu', this._onContextMenu);
|
|
939
|
+
this._timeline.addEventListener('pointerdown', this._onTimelineDown);
|
|
940
|
+
this._timeline.addEventListener('pointerup', this._onTimelineUp);
|
|
941
|
+
this._timeline.addEventListener('pointercancel', this._onTimelineUp);
|
|
942
|
+
this._timeline.addEventListener('pointermove', this._onTimelineMove);
|
|
943
|
+
this._timeline.addEventListener('click', this._onTimelineClick);
|
|
944
|
+
this._timeline.addEventListener('pointerleave', this._onTimelineLeave);
|
|
945
|
+
this._timeline.addEventListener('wheel', this._onTimelineWheel, { passive: false });
|
|
946
|
+
this._timeline.addEventListener('dblclick', this._onTimelineDoubleClick);
|
|
947
|
+
this._observeSize();
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/**
|
|
951
|
+
* Stops watching the size of the charts.
|
|
952
|
+
*/
|
|
953
|
+
destroy() {
|
|
954
|
+
this._resizeObserver?.disconnect();
|
|
955
|
+
this._resizeObserver = null;
|
|
956
|
+
this._resizeView = null;
|
|
957
|
+
this._watchKeys(false);
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Watches the size of the charts and the timeline with an observer of the window they are in,
|
|
962
|
+
* making a new one when they have moved to another window.
|
|
963
|
+
*
|
|
964
|
+
* @private
|
|
965
|
+
*/
|
|
966
|
+
_observeSize() {
|
|
967
|
+
const view = this._chartsEl.ownerDocument.defaultView;
|
|
968
|
+
if (view === this._resizeView) return;
|
|
969
|
+
this._resizeObserver?.disconnect();
|
|
970
|
+
this._resizeView = view;
|
|
971
|
+
const Observer = /** @type {any} */ (view)?.ResizeObserver;
|
|
972
|
+
this._resizeObserver = Observer ? new Observer(this._onResize) : null;
|
|
973
|
+
this._resizeObserver?.observe(this._chartsEl);
|
|
974
|
+
this._resizeObserver?.observe(this._timelineEl);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* The charts or the timeline changed size, such as while the splitter above the property view
|
|
979
|
+
* is dragged: draws them at their new size now, rather than leaving the browser to stretch the
|
|
980
|
+
* last drawing until the next redraw.
|
|
981
|
+
*
|
|
982
|
+
* @private
|
|
983
|
+
*/
|
|
984
|
+
_onResize = (entries) => {
|
|
985
|
+
for (const entry of entries) this._sizes.set(entry.target, { width: entry.contentRect.width, height: entry.contentRect.height });
|
|
986
|
+
this.draw();
|
|
987
|
+
const drawn = this._drawn;
|
|
988
|
+
drawn.charts = false;
|
|
989
|
+
drawn.timeline = false;
|
|
990
|
+
drawn.overlay = false;
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* The width of the names left of the charts and the timeline, in CSS pixels, which dragging
|
|
995
|
+
* the divider beside them changes. It keeps the plot at least a little wide.
|
|
996
|
+
*
|
|
997
|
+
* @type {number}
|
|
998
|
+
*/
|
|
999
|
+
set gutter(value) {
|
|
1000
|
+
const width = this._layout.plot + this._gutter;
|
|
1001
|
+
const most = width > MIN_PLOT + MIN_GUTTER ? Math.min(MAX_GUTTER, width - MIN_PLOT) : MAX_GUTTER;
|
|
1002
|
+
const next = Math.round(Math.max(MIN_GUTTER, Math.min(most, value)));
|
|
1003
|
+
if (!Number.isFinite(next) || next === this._gutter) return;
|
|
1004
|
+
this._gutter = next;
|
|
1005
|
+
for (const bar of [this._scrollbar, this._timeScrollbar]) bar.el.style.marginLeft = `${8 + next}px`;
|
|
1006
|
+
this.invalidate();
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
get gutter() {
|
|
1010
|
+
return this._gutter;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* The element holding the charts, which scrolls when they do not fit it.
|
|
1015
|
+
*
|
|
1016
|
+
* @type {HTMLElement}
|
|
1017
|
+
*/
|
|
1018
|
+
get chartsElement() {
|
|
1019
|
+
return this._chartsEl;
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
/**
|
|
1023
|
+
* The element holding the frame timeline, for a tooltip.
|
|
1024
|
+
*
|
|
1025
|
+
* @type {HTMLElement}
|
|
1026
|
+
*/
|
|
1027
|
+
get timelineElement() {
|
|
1028
|
+
return this._timelineEl;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/**
|
|
1032
|
+
* The frame shown below the charts: the one picked, or the newest when none is.
|
|
1033
|
+
*
|
|
1034
|
+
* @type {number}
|
|
1035
|
+
*/
|
|
1036
|
+
get shown() {
|
|
1037
|
+
const capture = this._capture;
|
|
1038
|
+
// a frame picked long ago has made way for newer ones: show the oldest kept instead
|
|
1039
|
+
if (this.picked >= 0) return Math.max(capture.first, Math.min(capture.end - 1, this.picked));
|
|
1040
|
+
return capture.end - 1;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* Whether the frame shown follows the newest one.
|
|
1045
|
+
*
|
|
1046
|
+
* @type {boolean}
|
|
1047
|
+
*/
|
|
1048
|
+
get following() {
|
|
1049
|
+
return this.picked < 0;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
/**
|
|
1053
|
+
* Shows a frame, which stops following the newest one.
|
|
1054
|
+
*
|
|
1055
|
+
* @param {number} seq - The frame, clamped to the frames kept.
|
|
1056
|
+
*/
|
|
1057
|
+
pick(seq) {
|
|
1058
|
+
const capture = this._capture;
|
|
1059
|
+
if (capture.count === 0) return;
|
|
1060
|
+
this.picked = Math.max(capture.first, Math.min(capture.end - 1, seq));
|
|
1061
|
+
this._reveal(this.picked);
|
|
1062
|
+
this.invalidate();
|
|
1063
|
+
this.onChange();
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
/**
|
|
1067
|
+
* Follows the newest frame again.
|
|
1068
|
+
*/
|
|
1069
|
+
follow() {
|
|
1070
|
+
this.picked = -1;
|
|
1071
|
+
if (this._view) this._view.follow = true;
|
|
1072
|
+
this.invalidate();
|
|
1073
|
+
this.onChange();
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
/**
|
|
1077
|
+
* Moves the frame shown.
|
|
1078
|
+
*
|
|
1079
|
+
* @param {number} delta - How many frames to move by, back when negative.
|
|
1080
|
+
*/
|
|
1081
|
+
step(delta) {
|
|
1082
|
+
this.pick(this.shown + delta);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* Forgets the frames selected together, the zoom and the frame picked, as after a clear.
|
|
1087
|
+
*/
|
|
1088
|
+
reset() {
|
|
1089
|
+
this.picked = -1;
|
|
1090
|
+
this.range = null;
|
|
1091
|
+
this._view = null;
|
|
1092
|
+
this._hover = -1;
|
|
1093
|
+
this.invalidate();
|
|
1094
|
+
this.onChange();
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
/**
|
|
1098
|
+
* Unselects the frames selected together.
|
|
1099
|
+
*/
|
|
1100
|
+
clearRange() {
|
|
1101
|
+
if (!this.range) return;
|
|
1102
|
+
this.range = null;
|
|
1103
|
+
this._drawn.overlay = true;
|
|
1104
|
+
this._drawn.timeline = true;
|
|
1105
|
+
this._checkScriptLegend();
|
|
1106
|
+
this.onChange();
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/**
|
|
1110
|
+
* Pins a script type to the Scripts chart, giving it a band of its own whatever its time, or
|
|
1111
|
+
* unpins it.
|
|
1112
|
+
*
|
|
1113
|
+
* @param {string} name - The name of the script type.
|
|
1114
|
+
*/
|
|
1115
|
+
togglePin(name) {
|
|
1116
|
+
if (this.pinned.has(name)) this.pinned.delete(name);
|
|
1117
|
+
else this.pinned.add(name);
|
|
1118
|
+
this.invalidate();
|
|
1119
|
+
this.onCollapse();
|
|
1120
|
+
this.onChange();
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
* Asks for everything to be redrawn on the next {@link update}.
|
|
1125
|
+
*/
|
|
1126
|
+
invalidate() {
|
|
1127
|
+
const drawn = this._drawn;
|
|
1128
|
+
drawn.charts = true;
|
|
1129
|
+
drawn.timeline = true;
|
|
1130
|
+
drawn.overlay = true;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
/**
|
|
1134
|
+
* Redraws what changed: the charts and the frame timeline at most every
|
|
1135
|
+
* {@link REDRAW_INTERVAL} while frames are being recorded, and right away for a change of
|
|
1136
|
+
* view; the overlay alone as the pointer moves.
|
|
1137
|
+
*
|
|
1138
|
+
* @param {number} now - The current `performance.now()` time.
|
|
1139
|
+
* @returns {boolean} Whether it drew the charts or the timeline, the costly part, so other work
|
|
1140
|
+
* of the panel can wait for another frame.
|
|
1141
|
+
*/
|
|
1142
|
+
update(now) {
|
|
1143
|
+
this._observeSize();
|
|
1144
|
+
const drawn = this._drawn;
|
|
1145
|
+
const recorded = drawn.version !== this._capture.version && now - drawn.time >= REDRAW_INTERVAL;
|
|
1146
|
+
let costly = false;
|
|
1147
|
+
if (recorded || drawn.charts) {
|
|
1148
|
+
drawn.version = this._capture.version;
|
|
1149
|
+
drawn.time = now;
|
|
1150
|
+
drawn.charts = false;
|
|
1151
|
+
this._drawCharts();
|
|
1152
|
+
costly = true;
|
|
1153
|
+
// the frames moved under the marks, and the newest frame shown may be a new one
|
|
1154
|
+
drawn.overlay = true;
|
|
1155
|
+
drawn.timeline ||= recorded;
|
|
1156
|
+
}
|
|
1157
|
+
// the timeline a frame after the charts, while frames are being recorded, so no one frame
|
|
1158
|
+
// draws both; a change of view draws it right away
|
|
1159
|
+
if (drawn.timeline && !(costly && recorded)) {
|
|
1160
|
+
drawn.timeline = false;
|
|
1161
|
+
this._drawTimeline();
|
|
1162
|
+
costly = true;
|
|
1163
|
+
}
|
|
1164
|
+
if (drawn.overlay) {
|
|
1165
|
+
drawn.overlay = false;
|
|
1166
|
+
this._drawOverlay();
|
|
1167
|
+
}
|
|
1168
|
+
return costly;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* Draws the charts, the frame timeline and the overlay now.
|
|
1173
|
+
*/
|
|
1174
|
+
draw() {
|
|
1175
|
+
this._drawCharts();
|
|
1176
|
+
this._drawTimeline();
|
|
1177
|
+
this._drawOverlay();
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* @param {HTMLCanvasElement} canvas - A canvas.
|
|
1182
|
+
* @param {HTMLElement} box - The element it fills.
|
|
1183
|
+
* @param {number} [height] - Its height, when it is taller than the element. Defaults to the
|
|
1184
|
+
* height of the element.
|
|
1185
|
+
* @returns {{ ctx: CanvasRenderingContext2D, width: number, height: number }|null} Its context,
|
|
1186
|
+
* scaled to CSS pixels, and its size in them, or null where it cannot be drawn.
|
|
1187
|
+
* @private
|
|
1188
|
+
*/
|
|
1189
|
+
_prepare(canvas, box, height) {
|
|
1190
|
+
const size = this._sizeOf(box);
|
|
1191
|
+
const width = size.width;
|
|
1192
|
+
height ??= size.height;
|
|
1193
|
+
if (width <= this._gutter || height <= 0) return null;
|
|
1194
|
+
const ctx = canvas.getContext('2d');
|
|
1195
|
+
if (!ctx) return null;
|
|
1196
|
+
const ratio = canvas.ownerDocument.defaultView?.devicePixelRatio ?? 1;
|
|
1197
|
+
const w = Math.round(width * ratio);
|
|
1198
|
+
const h = Math.round(height * ratio);
|
|
1199
|
+
if (canvas.width !== w || canvas.height !== h) {
|
|
1200
|
+
canvas.width = w;
|
|
1201
|
+
canvas.height = h;
|
|
1202
|
+
}
|
|
1203
|
+
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
1204
|
+
ctx.clearRect(0, 0, width, height);
|
|
1205
|
+
ctx.font = FONT;
|
|
1206
|
+
ctx.textBaseline = 'top';
|
|
1207
|
+
return { ctx, width, height };
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
/**
|
|
1211
|
+
* @param {HTMLElement} box - The charts or the timeline.
|
|
1212
|
+
* @returns {{ width: number, height: number }} Its size, as the resize observer reported it, or
|
|
1213
|
+
* from the page where there is no observer.
|
|
1214
|
+
* @private
|
|
1215
|
+
*/
|
|
1216
|
+
_sizeOf(box) {
|
|
1217
|
+
return this._sizes.get(box) ?? { width: box.clientWidth, height: box.clientHeight };
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/**
|
|
1221
|
+
* @returns {{ from: number, span: number }} The time in view on the time axis, in
|
|
1222
|
+
* milliseconds: where it starts and how long it is.
|
|
1223
|
+
* @private
|
|
1224
|
+
*/
|
|
1225
|
+
_span() {
|
|
1226
|
+
const capture = this._capture;
|
|
1227
|
+
const view = this._view;
|
|
1228
|
+
const start = capture.startPos;
|
|
1229
|
+
const end = capture.endPos;
|
|
1230
|
+
if (view) {
|
|
1231
|
+
if (view.follow) view.from = end - view.span;
|
|
1232
|
+
view.from = Math.max(start, Math.min(end - view.span, view.from));
|
|
1233
|
+
return view;
|
|
1234
|
+
}
|
|
1235
|
+
return { from: start, span: Math.max(MIN_SPAN, end - start) };
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
/**
|
|
1239
|
+
* @param {number} x - A position across the charts right of the names, in CSS pixels.
|
|
1240
|
+
* @returns {number} The entry drawn there, the one before it in a gap, which may not be kept.
|
|
1241
|
+
* @private
|
|
1242
|
+
*/
|
|
1243
|
+
_seqAt(x) {
|
|
1244
|
+
const { from, span, plot } = this._layout;
|
|
1245
|
+
return this._capture.seqAtPos(from + (x / plot) * span);
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
/**
|
|
1249
|
+
* @param {number} seq - A kept entry.
|
|
1250
|
+
* @returns {number} Where it was drawn across the canvas, in CSS pixels.
|
|
1251
|
+
* @private
|
|
1252
|
+
*/
|
|
1253
|
+
_xOf(seq) {
|
|
1254
|
+
const { from, span, plot } = this._layout;
|
|
1255
|
+
return this._gutter + ((this._capture.get('pos', seq) - from) / span) * plot;
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* @param {number} seq - A kept entry.
|
|
1260
|
+
* @returns {number} How wide it was drawn, in CSS pixels.
|
|
1261
|
+
* @private
|
|
1262
|
+
*/
|
|
1263
|
+
_wOf(seq) {
|
|
1264
|
+
const { span, plot } = this._layout;
|
|
1265
|
+
return (this._capture.get('width', seq) / span) * plot;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
/**
|
|
1269
|
+
* Scrolls a zoomed-in view to an entry, if it is out of view.
|
|
1270
|
+
*
|
|
1271
|
+
* @param {number} seq - The entry.
|
|
1272
|
+
* @private
|
|
1273
|
+
*/
|
|
1274
|
+
_reveal(seq) {
|
|
1275
|
+
const view = this._view;
|
|
1276
|
+
if (!view) return;
|
|
1277
|
+
const { from, span } = this._span();
|
|
1278
|
+
const pos = this._capture.get('pos', seq);
|
|
1279
|
+
if (pos < from || pos >= from + span) {
|
|
1280
|
+
view.from = pos - span / 2;
|
|
1281
|
+
view.follow = false;
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* @param {string} key - What the array is for.
|
|
1287
|
+
* @param {number} length - Its length.
|
|
1288
|
+
* @returns {Int32Array} An array kept for that, reused while its length stays the same, so a
|
|
1289
|
+
* redraw makes no garbage. Its content is left from the last use.
|
|
1290
|
+
* @private
|
|
1291
|
+
*/
|
|
1292
|
+
_ints(key, length) {
|
|
1293
|
+
let array = this._scratch.get(key);
|
|
1294
|
+
if (!(array instanceof Int32Array) || array.length !== length) {
|
|
1295
|
+
array = new Int32Array(length);
|
|
1296
|
+
this._scratch.set(key, array);
|
|
1297
|
+
}
|
|
1298
|
+
return array;
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* @param {string} key - What the array is for.
|
|
1303
|
+
* @param {number} length - Its length.
|
|
1304
|
+
* @returns {Float64Array} An array kept for that, as {@link _ints} keeps one.
|
|
1305
|
+
* @private
|
|
1306
|
+
*/
|
|
1307
|
+
_floats(key, length) {
|
|
1308
|
+
let array = this._scratch.get(key);
|
|
1309
|
+
if (!(array instanceof Float64Array) || array.length !== length) {
|
|
1310
|
+
array = new Float64Array(length);
|
|
1311
|
+
this._scratch.set(key, array);
|
|
1312
|
+
}
|
|
1313
|
+
return array;
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
/**
|
|
1317
|
+
* @param {PerfChartDef} chart - A chart.
|
|
1318
|
+
* @returns {PerfShape} The
|
|
1319
|
+
* channel it is scaled to, the parts it stacks and the lines across them: without the
|
|
1320
|
+
* inspector's time in the CPU chart while that is left out, and the total alone where the
|
|
1321
|
+
* engine does not break it down. Reduced when the inspector's time is left out.
|
|
1322
|
+
* @private
|
|
1323
|
+
*/
|
|
1324
|
+
_shape(chart) {
|
|
1325
|
+
const reduced = chart.key === 'cpu' && !this.showInspector;
|
|
1326
|
+
const plain = !!chart.plain && !this._capture.detailed;
|
|
1327
|
+
// made once for each way a chart can be drawn, as it is asked for on every redraw
|
|
1328
|
+
const key = `${chart.key}:${reduced ? 1 : 0}${plain ? 1 : 0}`;
|
|
1329
|
+
let shape = this._shapes.get(key);
|
|
1330
|
+
if (!shape) {
|
|
1331
|
+
const parts = reduced ? chart.parts.filter(part => part.key !== 'inspector') : plain ? chart.plain : chart.parts;
|
|
1332
|
+
const lines = reduced || plain ? NO_PARTS : chart.lines ?? NO_PARTS;
|
|
1333
|
+
// the rings the parts are read from, which stay the same for the life of the capture
|
|
1334
|
+
const d = this._capture.data;
|
|
1335
|
+
shape = {
|
|
1336
|
+
total: reduced ? 'cpuApp' : chart.total,
|
|
1337
|
+
parts,
|
|
1338
|
+
lines,
|
|
1339
|
+
partData: parts.map(part => d[part.channel]),
|
|
1340
|
+
lineData: lines.map(line => d[line.channel]),
|
|
1341
|
+
reduced
|
|
1342
|
+
};
|
|
1343
|
+
this._shapes.set(key, shape);
|
|
1344
|
+
}
|
|
1345
|
+
return shape;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/** @private */
|
|
1349
|
+
_drawCharts() {
|
|
1350
|
+
const scripts = this._scriptList();
|
|
1351
|
+
const scriptsOpen = !this.collapsed.has('scripts');
|
|
1352
|
+
const scriptsHeight = SECTION_TITLE + (scriptsOpen ? Math.max(SCRIPT_CHART_ROWS, scripts.lines.length + scripts.controls.length) * SCRIPT_ROW : 0);
|
|
1353
|
+
const resources = this._resourceList();
|
|
1354
|
+
const resourcesOpen = !this.collapsed.has('resources');
|
|
1355
|
+
const eventsOpen = !this.collapsed.has('events');
|
|
1356
|
+
const resourcesHeight = SECTION_TITLE + (resourcesOpen ? resources.length * RESOURCE_ROW : 0);
|
|
1357
|
+
const memoryOpen = !this.collapsed.has('memory');
|
|
1358
|
+
const memoryHeight = SECTION_TITLE + (memoryOpen ? MEMORY.length * MEMORY_ROW : 0);
|
|
1359
|
+
const eventKinds = this._eventKinds();
|
|
1360
|
+
const eventsHeight = SECTION_TITLE + (eventsOpen ? eventKinds.length * EVENT_ROW : 0);
|
|
1361
|
+
const counters = this._counterList();
|
|
1362
|
+
const countersOpen = !this.collapsed.has('counters');
|
|
1363
|
+
const countersHeight = SECTION_TITLE + (countersOpen ? Math.max(1, counters.length) * RESOURCE_ROW : 0);
|
|
1364
|
+
// a collapsed chart keeps only its title, the open ones share the rest
|
|
1365
|
+
let openCharts = 0;
|
|
1366
|
+
for (let i = 0; i < CHARTS.length; i++) {
|
|
1367
|
+
if (!this.collapsed.has(CHARTS[i].key)) openCharts++;
|
|
1368
|
+
}
|
|
1369
|
+
const loadingOpen = !this.collapsed.has('loading');
|
|
1370
|
+
const loadingHeight = SECTION_TITLE + (loadingOpen ? LOADING_ROWS.reduce((sum, row) => sum + row.height + 1, 0) : 0);
|
|
1371
|
+
const sections = scriptsHeight + countersHeight + loadingHeight + memoryHeight + resourcesHeight + eventsHeight + (CHARTS.length - openCharts) * SECTION_TITLE + CHART_GAP * (CHARTS.length + 5);
|
|
1372
|
+
|
|
1373
|
+
// taller than the box when the charts would get too short in it: the box scrolls then
|
|
1374
|
+
const contentHeight = Math.max(this._sizeOf(this._chartsEl).height, sections + openCharts * MIN_CHART_HEIGHT);
|
|
1375
|
+
if (contentHeight !== this._contentHeight) {
|
|
1376
|
+
this._contentHeight = contentHeight;
|
|
1377
|
+
this._canvas.style.height = `${contentHeight}px`;
|
|
1378
|
+
this._overlay.style.height = `${contentHeight}px`;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
const prepared = this._prepare(this._canvas, this._chartsEl, contentHeight);
|
|
1382
|
+
if (!prepared) return;
|
|
1383
|
+
const { ctx, width, height } = prepared;
|
|
1384
|
+
const capture = this._capture;
|
|
1385
|
+
const d = capture.data;
|
|
1386
|
+
const { budget, median } = captureNorms(capture);
|
|
1387
|
+
const { from, span } = this._span();
|
|
1388
|
+
// whole columns, so a frame lands in the same column whether drawn or picked
|
|
1389
|
+
const plot = Math.max(1, Math.floor(width - this._gutter));
|
|
1390
|
+
const columns = plot;
|
|
1391
|
+
const perColumn = span / columns;
|
|
1392
|
+
const first = capture.first;
|
|
1393
|
+
const end = capture.end;
|
|
1394
|
+
const slots = capture.slots;
|
|
1395
|
+
const layout = this._layout;
|
|
1396
|
+
layout.from = from;
|
|
1397
|
+
layout.span = span;
|
|
1398
|
+
layout.plot = plot;
|
|
1399
|
+
|
|
1400
|
+
// whole pixels throughout: the canvas boxes every fractional coordinate passed to it into an
|
|
1401
|
+
// object, which at thousands of bars a redraw is a steady stream of garbage
|
|
1402
|
+
const chartHeight = openCharts ? Math.floor((height - sections) / openCharts) : 0;
|
|
1403
|
+
this._rows = [];
|
|
1404
|
+
|
|
1405
|
+
// the entries each column covers, shared by every chart: those starting in its time, and
|
|
1406
|
+
// the one going on as it begins, so an entry spans every column of its time
|
|
1407
|
+
const lo = this._ints('lo', columns);
|
|
1408
|
+
const hi = this._ints('hi', columns);
|
|
1409
|
+
const pos = d.pos;
|
|
1410
|
+
const widths = d.width;
|
|
1411
|
+
for (let c = 0; c < columns; c++) {
|
|
1412
|
+
const t0 = from + c * perColumn;
|
|
1413
|
+
const t1 = t0 + perColumn;
|
|
1414
|
+
let a = capture.count ? capture.seqAtPos(t0) : first;
|
|
1415
|
+
if (capture.count && pos[a % slots] + widths[a % slots] <= t0 && pos[a % slots] < t0) a++;
|
|
1416
|
+
let b = capture.count ? capture.seqAtPos(t1) : first;
|
|
1417
|
+
if (capture.count && pos[b % slots] < t1) b++;
|
|
1418
|
+
lo[c] = Math.max(first, a);
|
|
1419
|
+
hi[c] = Math.min(end, Math.max(lo[c], b));
|
|
1420
|
+
}
|
|
1421
|
+
this._spans.lo = lo;
|
|
1422
|
+
this._spans.hi = hi;
|
|
1423
|
+
|
|
1424
|
+
const shown = capture.count ? this.shown : -1;
|
|
1425
|
+
|
|
1426
|
+
// where each bar goes across: a column each, showing the frame a chart picks for it, or
|
|
1427
|
+
// zoomed in until a frame spans several columns, a frame each, with a gap to the next once
|
|
1428
|
+
// there is room for one, so the frames read apart. Kept in arrays, so the many bars of a
|
|
1429
|
+
// redraw are drawn without making garbage
|
|
1430
|
+
// a frame of the median length wide enough on its own: each at its time, as wide as it was
|
|
1431
|
+
const frameWidth = (Number.isFinite(median) ? median : 16) * plot / span;
|
|
1432
|
+
const wide = frameWidth > 1;
|
|
1433
|
+
const visibleFrom = capture.count ? Math.max(first, lo[0]) : first;
|
|
1434
|
+
const visibleTo = capture.count ? Math.min(end, Math.max(visibleFrom, hi[columns - 1])) : first;
|
|
1435
|
+
const bars = wide ? Math.max(0, visibleTo - visibleFrom) : columns;
|
|
1436
|
+
const barX = this._ints('barX', bars);
|
|
1437
|
+
const barW = this._ints('barW', bars);
|
|
1438
|
+
for (let i = 0; i < bars; i++) {
|
|
1439
|
+
if (wide) {
|
|
1440
|
+
// the entry cut by the left edge is drawn from the edge, and a gap to the next once
|
|
1441
|
+
// there is room for one, so the frames read apart
|
|
1442
|
+
const seq = visibleFrom + i;
|
|
1443
|
+
const x = this._xOf(seq);
|
|
1444
|
+
const w = this._wOf(seq);
|
|
1445
|
+
const left = Math.max(this._gutter, Math.round(x));
|
|
1446
|
+
barX[i] = left;
|
|
1447
|
+
barW[i] = Math.max(1, Math.round(x + w) - left - (w >= 4 ? 1 : 0));
|
|
1448
|
+
} else {
|
|
1449
|
+
barX[i] = this._gutter + i;
|
|
1450
|
+
barW[i] = 1;
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
let chartsBottom = 0;
|
|
1455
|
+
for (let index = 0; index < CHARTS.length; index++) {
|
|
1456
|
+
const chart = CHARTS[index];
|
|
1457
|
+
const top = chartsBottom;
|
|
1458
|
+
const shape = this._shape(chart);
|
|
1459
|
+
const totals = d[shape.total];
|
|
1460
|
+
const pick = this._ints(`pick:${index}`, columns).fill(-1);
|
|
1461
|
+
this._columns[index] = pick;
|
|
1462
|
+
if (this.collapsed.has(chart.key)) {
|
|
1463
|
+
// its title alone, with the figure of the frame shown
|
|
1464
|
+
this._sectionTitle(ctx, chart.key, chart.title, top, false);
|
|
1465
|
+
if (capture.has(shown)) {
|
|
1466
|
+
ctx.fillStyle = shape.parts.length === 1 ? shape.parts[0].color : '#b9c4c7';
|
|
1467
|
+
ctx.fillText(chart.format(totals[capture.slot(shown)]), this._gutter + 4, top + 2);
|
|
1468
|
+
}
|
|
1469
|
+
chartsBottom += SECTION_TITLE + CHART_GAP;
|
|
1470
|
+
continue;
|
|
1471
|
+
}
|
|
1472
|
+
chartsBottom += chartHeight + CHART_GAP;
|
|
1473
|
+
this._rows.push({ top, height: chartHeight, chart: index });
|
|
1474
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.03)';
|
|
1475
|
+
ctx.fillRect(this._gutter, top, plot, chartHeight);
|
|
1476
|
+
|
|
1477
|
+
// the frame each column shows: its largest in this chart
|
|
1478
|
+
const peaks = this._floats('peaks', columns);
|
|
1479
|
+
const peakCount = PerfCharts._pickLargest(totals, slots, lo, hi, pick, peaks);
|
|
1480
|
+
const sorted = peaks.subarray(0, peakCount).sort();
|
|
1481
|
+
const max = peakCount ? sorted[peakCount - 1] : 0;
|
|
1482
|
+
const typical = peakCount ? sorted[Math.floor(0.95 * (peakCount - 1))] : 0;
|
|
1483
|
+
const unit = chart.unit ?? 1;
|
|
1484
|
+
// a duration is scaled to show the display interval too, its watermark, so the time a
|
|
1485
|
+
// frame has to spare reads off every timing chart alike
|
|
1486
|
+
const watermark = chart.timing && Number.isFinite(budget) ? budget * BUDGET_HEADROOM : 0;
|
|
1487
|
+
const scale = niceCeil(Math.max(Math.min(max, SPIKE_HEADROOM * typical), watermark) / unit) * unit;
|
|
1488
|
+
const bottom = top + chartHeight;
|
|
1489
|
+
const toY = v => Math.max(top, (bottom - (v / scale) * chartHeight + 0.5) | 0);
|
|
1490
|
+
this._rows[this._rows.length - 1].levels = { top, bottom, min: 0, max: scale, format: chart.format };
|
|
1491
|
+
|
|
1492
|
+
// the parts stacked, with the spikes cut off by the scale marked along the top, and in
|
|
1493
|
+
// the frame chart the hitches
|
|
1494
|
+
const hitchAbove = chart.key === 'frame' && Number.isFinite(median) ? HITCH_FACTOR * median : Infinity;
|
|
1495
|
+
PerfCharts._drawStack(ctx, shape, totals, slots, wide ? null : pick, visibleFrom, bars, barX, barW, top, chartHeight, scale, hitchAbove,
|
|
1496
|
+
this._ints('stackBase', bars), this._floats('stackSum', bars));
|
|
1497
|
+
|
|
1498
|
+
if (chart.timing && budget <= scale) {
|
|
1499
|
+
ctx.strokeStyle = 'rgba(255, 255, 255, 0.45)';
|
|
1500
|
+
ctx.setLineDash(DASH);
|
|
1501
|
+
ctx.beginPath();
|
|
1502
|
+
const y = Math.round(toY(budget)) + 0.5;
|
|
1503
|
+
ctx.moveTo(this._gutter, y);
|
|
1504
|
+
ctx.lineTo(width, y);
|
|
1505
|
+
ctx.stroke();
|
|
1506
|
+
ctx.setLineDash(NO_DASH);
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
if (peakCount === 0 && chart.empty && capture.count) {
|
|
1510
|
+
this._message(ctx, chart.empty, '#7f8f93', plot, top + chartHeight / 2 - 5);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
// the name and the figure of the frame shown beside the chart, its scale in its corner
|
|
1514
|
+
ctx.font = BOLD;
|
|
1515
|
+
ctx.fillStyle = '#dbe2e4';
|
|
1516
|
+
this._gutterText(ctx, `▾ ${chart.title}`, 4, top + 2);
|
|
1517
|
+
ctx.font = FONT;
|
|
1518
|
+
if (capture.has(shown)) {
|
|
1519
|
+
ctx.fillStyle = shape.parts.length === 1 ? shape.parts[0].color : '#b9c4c7';
|
|
1520
|
+
this._gutterText(ctx, chart.format(totals[capture.slot(shown)]), 14, top + 14);
|
|
1521
|
+
}
|
|
1522
|
+
PerfCharts._label(ctx, chart.format(scale), width - 3, top + 2, 'right');
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
// the time of each script type, stacked beside the list of them
|
|
1526
|
+
const scriptsTop = chartsBottom;
|
|
1527
|
+
this._sectionTitle(ctx, 'scripts', 'Scripts', scriptsTop, scriptsOpen);
|
|
1528
|
+
this._lineCharts.scripts = scriptsOpen ?
|
|
1529
|
+
this._drawScriptChart(ctx, scripts, scriptsTop + SECTION_TITLE, width, plot, lo, hi, wide, visibleFrom, visibleTo, bars, barX, barW) : null;
|
|
1530
|
+
|
|
1531
|
+
// the app's own counters, a line each on a scale of its own, as their units are the app's
|
|
1532
|
+
const countersTop = scriptsTop + scriptsHeight + CHART_GAP;
|
|
1533
|
+
this._sectionTitle(ctx, 'counters', 'Counters', countersTop, countersOpen);
|
|
1534
|
+
this._lineCharts.counters = countersOpen && counters.length ?
|
|
1535
|
+
this._drawLineChart(ctx, 'counters', counters, formatValue, max => Math.max(Math.abs(max) * COUNTER_MIN_SHARE, COUNTER_MIN_RANGE),
|
|
1536
|
+
countersTop + SECTION_TITLE, RESOURCE_ROW, width, plot, lo, hi, wide, visibleFrom, visibleTo, bars, barX, barW, true) : null;
|
|
1537
|
+
if (countersOpen && !counters.length) {
|
|
1538
|
+
const text = capture.supports.counters ? 'Counters the app sets in app.stats.user show here' : 'This engine has no app.stats.user for counters of the app';
|
|
1539
|
+
this._message(ctx, text, '#7f8f93', plot, countersTop + SECTION_TITLE + RESOURCE_ROW / 2 - 5);
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
// what loaded, on the time axis
|
|
1543
|
+
const loadingTop = countersTop + countersHeight + CHART_GAP;
|
|
1544
|
+
this._sectionTitle(ctx, 'loading', 'Loading', loadingTop, loadingOpen);
|
|
1545
|
+
this._loadingDrawn = {};
|
|
1546
|
+
if (loadingOpen) this._drawLoading(ctx, loadingTop + SECTION_TITLE, plot, from, span);
|
|
1547
|
+
|
|
1548
|
+
// memory and the resources: a chart each, of lines sharing a scale from the smallest to
|
|
1549
|
+
// the largest value of the lines shown, beside a list of them that hides or shows each, so
|
|
1550
|
+
// hiding the large ones lets the small ones fill the chart
|
|
1551
|
+
const memoryTop = loadingTop + loadingHeight + CHART_GAP;
|
|
1552
|
+
this._sectionTitle(ctx, 'memory', 'Memory', memoryTop, memoryOpen);
|
|
1553
|
+
this._lineCharts.memory = memoryOpen ?
|
|
1554
|
+
this._drawLineChart(ctx, 'memory', this._memoryList(), formatBytes, max => Math.max(MEMORY_MIN_SHARE * max, MEMORY_MIN_BYTES),
|
|
1555
|
+
memoryTop + SECTION_TITLE, MEMORY_ROW, width, plot, lo, hi, wide, visibleFrom, visibleTo, bars, barX, barW) : null;
|
|
1556
|
+
|
|
1557
|
+
const resourcesTop = memoryTop + memoryHeight + CHART_GAP;
|
|
1558
|
+
this._sectionTitle(ctx, 'resources', 'Resources', resourcesTop, resourcesOpen);
|
|
1559
|
+
this._lineCharts.resources = resourcesOpen ?
|
|
1560
|
+
this._drawLineChart(ctx, 'resources', resources, formatCount, max => Math.max(MEMORY_MIN_SHARE * max, RESOURCE_MIN_COUNT),
|
|
1561
|
+
resourcesTop + SECTION_TITLE, RESOURCE_ROW, width, plot, lo, hi, wide, visibleFrom, visibleTo, bars, barX, barW) : null;
|
|
1562
|
+
|
|
1563
|
+
// the events: a row per kind, each bar marked when its frame, or one of its column's, had
|
|
1564
|
+
// one
|
|
1565
|
+
const eventsTop = resourcesTop + resourcesHeight + CHART_GAP;
|
|
1566
|
+
this._sectionTitle(ctx, 'events', 'Events', eventsTop, eventsOpen);
|
|
1567
|
+
if (eventsOpen) {
|
|
1568
|
+
const masks = this._ints('masks', bars).fill(0);
|
|
1569
|
+
for (let i = 0; i < bars; i++) {
|
|
1570
|
+
if (wide) {
|
|
1571
|
+
masks[i] = eventMask(capture, visibleFrom + i);
|
|
1572
|
+
} else {
|
|
1573
|
+
for (let seq = lo[i]; seq < hi[i]; seq++) masks[i] |= eventMask(capture, seq);
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
for (let r = 0; r < eventKinds.length; r++) {
|
|
1577
|
+
const k = eventKinds[r];
|
|
1578
|
+
const kind = EVENT_KINDS[k];
|
|
1579
|
+
const top = eventsTop + SECTION_TITLE + r * EVENT_ROW;
|
|
1580
|
+
this._rows.push({ top, height: EVENT_ROW, kind: k });
|
|
1581
|
+
ctx.fillStyle = r % 2 ? 'rgba(255, 255, 255, 0.02)' : 'rgba(255, 255, 255, 0.04)';
|
|
1582
|
+
ctx.fillRect(this._gutter, top, plot, EVENT_ROW);
|
|
1583
|
+
const unsupported = !!eventUnsupported(capture, kind.key);
|
|
1584
|
+
const on = !unsupported && !this.hidden.has(`events:${kind.key}`);
|
|
1585
|
+
PerfCharts._checkbox(ctx, 10.5, top + 1.5, unsupported ? '#56656a' : kind.color, on);
|
|
1586
|
+
ctx.fillStyle = on ? '#b9c4c7' : '#56656a';
|
|
1587
|
+
this._gutterText(ctx, kind.short ?? kind.name, 23, top);
|
|
1588
|
+
if (!on) continue;
|
|
1589
|
+
ctx.fillStyle = kind.color;
|
|
1590
|
+
PerfCharts._drawMarks(ctx, masks, 1 << k, bars, barX, barW, top + 1, EVENT_ROW - 2);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
layout.bottom = eventsTop + eventsHeight;
|
|
1595
|
+
this._drawStartupAndGaps(ctx, chartsBottom, plot);
|
|
1596
|
+
this._updateScrollbar();
|
|
1597
|
+
|
|
1598
|
+
// nothing recorded yet: said in the first chart open, or beside the first title, where
|
|
1599
|
+
// nothing else is drawn while the capture is empty
|
|
1600
|
+
if (capture.count === 0) {
|
|
1601
|
+
const first = this._rows.find(row => row.chart !== undefined);
|
|
1602
|
+
const y = first ? first.top + first.height / 2 - 5 : 2;
|
|
1603
|
+
this._message(ctx, capture.recording ? 'Recording…' : 'Press Record to capture frames', '#93a2a6', plot, y);
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
/**
|
|
1608
|
+
* What loaded over a time of the axis: the milestones, the assets and the requests of the kept
|
|
1609
|
+
* entries, and the assets loading now, each with where it starts and ends on the axis.
|
|
1610
|
+
*
|
|
1611
|
+
* @param {number} from - Where the time starts on the axis.
|
|
1612
|
+
* @param {number} to - Where it ends.
|
|
1613
|
+
* @returns {Record<string, { p0: number, p1: number, load: PerfLoad }[]>} The items by row.
|
|
1614
|
+
* @private
|
|
1615
|
+
*/
|
|
1616
|
+
_loadingItems(from, to) {
|
|
1617
|
+
const capture = this._capture;
|
|
1618
|
+
/** @type {Record<string, { p0: number, p1: number, load: PerfLoad }[]>} */
|
|
1619
|
+
const items = { milestones: [], assets: [], requests: [] };
|
|
1620
|
+
if (!capture.count) return items;
|
|
1621
|
+
const rows = { milestone: items.milestones, asset: items.assets, request: items.requests };
|
|
1622
|
+
const add = (list, load, start, end) => {
|
|
1623
|
+
const p1 = capture.posAt(end);
|
|
1624
|
+
const p0 = Number.isFinite(start) ? capture.posAt(start) : p1;
|
|
1625
|
+
if (p1 >= from && p0 <= to) list.push({ p0, p1, load });
|
|
1626
|
+
};
|
|
1627
|
+
for (let seq = capture.first; seq < capture.end; seq++) {
|
|
1628
|
+
for (const load of capture.loads(seq)) add(rows[load.kind], load, load.start, load.end);
|
|
1629
|
+
}
|
|
1630
|
+
const now = performance.now();
|
|
1631
|
+
for (const load of capture.pendingLoads()) add(items.assets, load, load.start, now);
|
|
1632
|
+
return items;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
/**
|
|
1636
|
+
* Draws the Loading section: a row each of milestones, assets packed into lanes and the
|
|
1637
|
+
* requests in flight, on the time axis of the charts.
|
|
1638
|
+
*
|
|
1639
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
1640
|
+
* @param {number} top - The top of the section's rows.
|
|
1641
|
+
* @param {number} plot - The width of the plot.
|
|
1642
|
+
* @param {number} from - Where the time in view starts on the axis.
|
|
1643
|
+
* @param {number} span - How long it is.
|
|
1644
|
+
* @private
|
|
1645
|
+
*/
|
|
1646
|
+
_drawLoading(ctx, top, plot, from, span) {
|
|
1647
|
+
const capture = this._capture;
|
|
1648
|
+
const items = this._loadingItems(from, from + span);
|
|
1649
|
+
const toX = p => this._gutter + ((p - from) / span) * plot;
|
|
1650
|
+
const drawn = this._loadingDrawn;
|
|
1651
|
+
let y = top;
|
|
1652
|
+
ctx.save();
|
|
1653
|
+
for (const row of LOADING_ROWS) {
|
|
1654
|
+
this._rows.push({ top: y, height: row.height, loading: row.key });
|
|
1655
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.03)';
|
|
1656
|
+
ctx.fillRect(this._gutter, y, plot, row.height);
|
|
1657
|
+
ctx.fillStyle = '#b9c4c7';
|
|
1658
|
+
this._gutterText(ctx, row.name, 10, y + (row.height - 10) / 2);
|
|
1659
|
+
ctx.save();
|
|
1660
|
+
ctx.beginPath();
|
|
1661
|
+
ctx.rect(this._gutter, y, plot, row.height);
|
|
1662
|
+
ctx.clip();
|
|
1663
|
+
const list = (drawn[row.key] = []);
|
|
1664
|
+
const pixels = items[row.key].map(({ p0, p1, load }) => ({ x0: toX(p0), x1: Math.max(toX(p0) + 1, toX(p1)), lane: 0, load }));
|
|
1665
|
+
if (row.key === 'milestones') {
|
|
1666
|
+
pixels.sort((a, b) => a.x0 - b.x0);
|
|
1667
|
+
for (let i = 0; i < pixels.length; i++) {
|
|
1668
|
+
const item = pixels[i];
|
|
1669
|
+
ctx.fillStyle = MILESTONE_COLOR;
|
|
1670
|
+
ctx.fillRect(Math.round(item.x0), y, 1, row.height);
|
|
1671
|
+
// named where there is room before the next
|
|
1672
|
+
const room = (pixels[i + 1]?.x0 ?? this._gutter + plot) - item.x0 - 6;
|
|
1673
|
+
const text = PerfCharts._fit(ctx, item.load.name, '', room);
|
|
1674
|
+
if (text) ctx.fillText(text, item.x0 + 3, y + 1);
|
|
1675
|
+
list.push(item);
|
|
1676
|
+
}
|
|
1677
|
+
} else if (row.key === 'assets') {
|
|
1678
|
+
// packed into lanes, each asset in the first lane free at its start
|
|
1679
|
+
pixels.sort((a, b) => a.x0 - b.x0);
|
|
1680
|
+
const ends = new Array(ASSET_LANES).fill(-Infinity);
|
|
1681
|
+
for (const item of pixels) {
|
|
1682
|
+
let lane = ends.findIndex(end => end < item.x0);
|
|
1683
|
+
if (lane < 0) lane = ends.indexOf(Math.min(...ends));
|
|
1684
|
+
ends[lane] = item.x1;
|
|
1685
|
+
item.lane = lane;
|
|
1686
|
+
const load = item.load;
|
|
1687
|
+
const ly = y + 1 + lane * (ASSET_LANE + 1);
|
|
1688
|
+
const color = load.failed ? FAILED_COLOR : ASSET_COLORS[load.type] ?? ASSET_COLOR;
|
|
1689
|
+
ctx.fillStyle = color;
|
|
1690
|
+
ctx.globalAlpha = 0.45;
|
|
1691
|
+
ctx.fillRect(Math.round(item.x0), ly, Math.max(1, Math.round(item.x1 - item.x0)), ASSET_LANE);
|
|
1692
|
+
ctx.globalAlpha = 1;
|
|
1693
|
+
// the request of its file solid
|
|
1694
|
+
if (Number.isFinite(load.netEnd) && !load.failed) {
|
|
1695
|
+
const n0 = toX(capture.posAt(load.netStart));
|
|
1696
|
+
const n1 = toX(capture.posAt(load.netEnd));
|
|
1697
|
+
ctx.fillRect(Math.round(n0), ly, Math.max(1, Math.round(n1 - n0)), ASSET_LANE);
|
|
1698
|
+
}
|
|
1699
|
+
list.push(item);
|
|
1700
|
+
}
|
|
1701
|
+
} else {
|
|
1702
|
+
// how many requests were in flight across each column
|
|
1703
|
+
const counts = new Int32Array(plot);
|
|
1704
|
+
for (const item of pixels) {
|
|
1705
|
+
for (let c = Math.max(0, Math.floor(item.x0 - this._gutter)); c <= Math.min(plot - 1, Math.floor(item.x1 - this._gutter)); c++) counts[c]++;
|
|
1706
|
+
list.push(item);
|
|
1707
|
+
}
|
|
1708
|
+
let most = 0;
|
|
1709
|
+
for (let c = 0; c < plot; c++) most = Math.max(most, counts[c]);
|
|
1710
|
+
ctx.fillStyle = REQUEST_COLOR;
|
|
1711
|
+
for (let c = 0; c < plot; c++) {
|
|
1712
|
+
if (!counts[c]) continue;
|
|
1713
|
+
const h = Math.max(1, Math.round((counts[c] / most) * (row.height - 2)));
|
|
1714
|
+
ctx.fillRect(this._gutter + c, y + row.height - 1 - h, 1, h);
|
|
1715
|
+
}
|
|
1716
|
+
if (most) PerfCharts._label(ctx, `${most}`, this._gutter + plot - 3, y + 1, 'right');
|
|
1717
|
+
}
|
|
1718
|
+
ctx.restore();
|
|
1719
|
+
y += row.height + 1;
|
|
1720
|
+
}
|
|
1721
|
+
ctx.restore();
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
/**
|
|
1725
|
+
* @param {string} key - A row of the Loading section.
|
|
1726
|
+
* @param {number} x - Where the pointer is across the plot, negative over the names.
|
|
1727
|
+
* @returns {PerfHoverLine[]} What the row has under the pointer, the longest first, or what the
|
|
1728
|
+
* row shows over its name.
|
|
1729
|
+
* @private
|
|
1730
|
+
*/
|
|
1731
|
+
_loadingLines(key, x) {
|
|
1732
|
+
const row = LOADING_ROWS.find(r => r.key === key);
|
|
1733
|
+
if (x < 0) return key === 'assets' ? [{ text: row.tip, wrap: true }, ...assetKey()] : [{ text: row.tip, wrap: true }];
|
|
1734
|
+
const px = x + this._gutter;
|
|
1735
|
+
const drawn = this._loadingDrawn[key] ?? [];
|
|
1736
|
+
const slack = key === 'milestones' ? 3 : 1;
|
|
1737
|
+
const under = drawn.filter(item => px >= item.x0 - slack && px <= item.x1 + slack);
|
|
1738
|
+
if (!under.length) return [];
|
|
1739
|
+
const duration = item => (Number.isFinite(item.load.start) ? item.load.end - item.load.start : 0);
|
|
1740
|
+
under.sort((a, b) => (key === 'milestones' ? a.x0 - b.x0 : duration(b) - duration(a)));
|
|
1741
|
+
/** @type {PerfHoverLine[]} */
|
|
1742
|
+
const lines = [];
|
|
1743
|
+
if (key === 'requests') lines.push({ text: `${under.length} ${under.length === 1 ? 'request' : 'requests'} in flight` });
|
|
1744
|
+
if (key === 'assets') lines.push({ text: `${under.length} ${under.length === 1 ? 'asset' : 'assets'} loading` });
|
|
1745
|
+
for (const item of under.slice(0, HOVER_SCRIPT_TYPES)) lines.push({ text: loadText(item.load), color: loadColor(item.load), indent: key === 'requests' || key === 'assets' });
|
|
1746
|
+
if (under.length > HOVER_SCRIPT_TYPES) lines.push({ text: `… and ${under.length - HOVER_SCRIPT_TYPES} more`, indent: true });
|
|
1747
|
+
if (key === 'assets') lines.push({ text: 'Click an asset to show it on the Assets tab' });
|
|
1748
|
+
return lines;
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
/**
|
|
1752
|
+
* Marks the start of the app over the charts, which have no figures for it, and the long
|
|
1753
|
+
* times between entries folded away, with how long each was.
|
|
1754
|
+
*
|
|
1755
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
1756
|
+
* @param {number} chartsBottom - The bottom of the charts.
|
|
1757
|
+
* @param {number} plot - The width of the plot.
|
|
1758
|
+
* @private
|
|
1759
|
+
*/
|
|
1760
|
+
_drawStartupAndGaps(ctx, chartsBottom, plot) {
|
|
1761
|
+
const capture = this._capture;
|
|
1762
|
+
if (!capture.count) return;
|
|
1763
|
+
ctx.save();
|
|
1764
|
+
ctx.beginPath();
|
|
1765
|
+
ctx.rect(this._gutter, 0, plot, chartsBottom);
|
|
1766
|
+
ctx.clip();
|
|
1767
|
+
const first = capture.first;
|
|
1768
|
+
if (capture.isStartup(first)) {
|
|
1769
|
+
const x = Math.round(this._xOf(first));
|
|
1770
|
+
const w = Math.max(1, Math.round(this._wOf(first)));
|
|
1771
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.05)';
|
|
1772
|
+
ctx.fillRect(x, 0, w, chartsBottom);
|
|
1773
|
+
// hatched, as it has no figures of a frame
|
|
1774
|
+
ctx.strokeStyle = 'rgba(255, 255, 255, 0.07)';
|
|
1775
|
+
ctx.beginPath();
|
|
1776
|
+
for (let i = -chartsBottom; i < w; i += 8) {
|
|
1777
|
+
ctx.moveTo(x + i, chartsBottom);
|
|
1778
|
+
ctx.lineTo(x + i + chartsBottom, 0);
|
|
1779
|
+
}
|
|
1780
|
+
ctx.stroke();
|
|
1781
|
+
// labelled at the left of what is in view of it
|
|
1782
|
+
const left = Math.max(x, this._gutter);
|
|
1783
|
+
const label = PerfCharts._fit(ctx, 'Startup', formatDuration(capture.get('width', first)), Math.min(x + w, this._gutter + plot) - left - 8);
|
|
1784
|
+
if (label) PerfCharts._label(ctx, label, left + 4, 16, 'left');
|
|
1785
|
+
}
|
|
1786
|
+
// the gaps, a break line in each with how long it was
|
|
1787
|
+
for (let seq = first + 1; seq < capture.end; seq++) {
|
|
1788
|
+
const gap = capture.get('gap', seq);
|
|
1789
|
+
if (!(gap > 0)) continue;
|
|
1790
|
+
const x1 = Math.round(this._xOf(seq));
|
|
1791
|
+
const x0 = Math.round(this._xOf(seq - 1) + this._wOf(seq - 1));
|
|
1792
|
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.35)';
|
|
1793
|
+
ctx.fillRect(x0, 0, Math.max(1, x1 - x0), chartsBottom);
|
|
1794
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.35)';
|
|
1795
|
+
ctx.fillRect(Math.round((x0 + x1) / 2), 0, 1, chartsBottom);
|
|
1796
|
+
const label = `+${formatDuration(gap)}`;
|
|
1797
|
+
const left = Math.max(x0, this._gutter);
|
|
1798
|
+
if (ctx.measureText(label).width + 6 < Math.min(x1, this._gutter + plot) - left) PerfCharts._label(ctx, label, left + 3, 16, 'left');
|
|
1799
|
+
}
|
|
1800
|
+
ctx.restore();
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
/**
|
|
1804
|
+
* Sizes the thumb of the scrollbar to the time in view, out of all that is kept. It fills the
|
|
1805
|
+
* bar, dimmed, while the whole capture is in view.
|
|
1806
|
+
*
|
|
1807
|
+
* @private
|
|
1808
|
+
*/
|
|
1809
|
+
_updateScrollbar() {
|
|
1810
|
+
const capture = this._capture;
|
|
1811
|
+
const { from, span } = this._span();
|
|
1812
|
+
const total = Math.max(1, capture.endPos - capture.startPos);
|
|
1813
|
+
const zoomed = !!this._view && span < total;
|
|
1814
|
+
this._scrollbar.set(zoomed ? (from - capture.startPos) / total : 0, zoomed ? span / total : 1);
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
/**
|
|
1818
|
+
* Moves a zoomed-in view so it starts at a time, following the newest frames when it reaches
|
|
1819
|
+
* them.
|
|
1820
|
+
*
|
|
1821
|
+
* @param {number} from - Where on the time axis the view starts.
|
|
1822
|
+
* @private
|
|
1823
|
+
*/
|
|
1824
|
+
_scrollTo(from) {
|
|
1825
|
+
const view = this._view;
|
|
1826
|
+
if (!view) return;
|
|
1827
|
+
const capture = this._capture;
|
|
1828
|
+
view.from = Math.max(capture.startPos, Math.min(capture.endPos - view.span, from));
|
|
1829
|
+
view.follow = view.from + view.span >= capture.endPos;
|
|
1830
|
+
this._updateScrollbar();
|
|
1831
|
+
this.invalidate();
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
// the loops over every column or frame of a redraw, apart in small functions the engine
|
|
1835
|
+
// compiles to tight code soon, rather than in the large one drawing everything, where its
|
|
1836
|
+
// numbers would be boxed into garbage until that is optimized
|
|
1837
|
+
|
|
1838
|
+
/**
|
|
1839
|
+
* Picks the frame each column shows: the one with the largest value.
|
|
1840
|
+
*
|
|
1841
|
+
* @param {Float64Array} values - The ring of the channel.
|
|
1842
|
+
* @param {number} slots - The length of the ring.
|
|
1843
|
+
* @param {Int32Array} lo - The first frame of each column.
|
|
1844
|
+
* @param {Int32Array} hi - One past the last frame of each column.
|
|
1845
|
+
* @param {Int32Array} pick - Receives the frame of each column, left as it was for a column
|
|
1846
|
+
* without a value.
|
|
1847
|
+
* @param {Float64Array} peaks - Receives the values picked, packed.
|
|
1848
|
+
* @returns {number} How many columns had a value.
|
|
1849
|
+
* @private
|
|
1850
|
+
*/
|
|
1851
|
+
static _pickLargest(values, slots, lo, hi, pick, peaks) {
|
|
1852
|
+
let count = 0;
|
|
1853
|
+
for (let c = 0; c < lo.length; c++) {
|
|
1854
|
+
let best = -1;
|
|
1855
|
+
let bestValue = -Infinity;
|
|
1856
|
+
for (let seq = lo[c]; seq < hi[c]; seq++) {
|
|
1857
|
+
const v = values[seq % slots];
|
|
1858
|
+
if (v > bestValue) {
|
|
1859
|
+
bestValue = v;
|
|
1860
|
+
best = seq;
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
if (best >= 0 && bestValue > -Infinity && bestValue < Infinity) {
|
|
1864
|
+
pick[c] = best;
|
|
1865
|
+
peaks[count++] = bestValue;
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
return count;
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
/**
|
|
1872
|
+
* Draws the parts of a chart stacked, a bar each, each part at least a pixel high so none of
|
|
1873
|
+
* them goes missing, with the lines across the stack, the spikes cut off by the scale marked
|
|
1874
|
+
* along the top and the hitches above them.
|
|
1875
|
+
*
|
|
1876
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
1877
|
+
* @param {PerfShape} shape - What the chart draws.
|
|
1878
|
+
* @param {Float64Array} totals - The ring of its total.
|
|
1879
|
+
* @param {number} slots - The length of the rings.
|
|
1880
|
+
* @param {Int32Array|null} pick - The frame of each column, or null when each bar is a frame
|
|
1881
|
+
* of its own, from the first in view.
|
|
1882
|
+
* @param {number} visibleFrom - The first frame in view.
|
|
1883
|
+
* @param {number} bars - How many bars there are.
|
|
1884
|
+
* @param {Int32Array} barX - The left edge of each bar.
|
|
1885
|
+
* @param {Int32Array} barW - The width of each bar.
|
|
1886
|
+
* @param {number} top - The top of the chart, a whole pixel.
|
|
1887
|
+
* @param {number} height - Its height, a whole number of pixels.
|
|
1888
|
+
* @param {number} scale - The value at its top.
|
|
1889
|
+
* @param {number} hitchAbove - The total a hitch takes over, Infinity for none.
|
|
1890
|
+
* @param {Int32Array} base - Room for where the stack of each bar has got to.
|
|
1891
|
+
* @param {Float64Array} sums - Room for the sum of each bar so far.
|
|
1892
|
+
* @private
|
|
1893
|
+
*/
|
|
1894
|
+
static _drawStack(ctx, shape, totals, slots, pick, visibleFrom, bars, barX, barW, top, height, scale, hitchAbove, base, sums) {
|
|
1895
|
+
const { parts, lines, partData, lineData } = shape;
|
|
1896
|
+
const bottom = top + height;
|
|
1897
|
+
// a part at a time across every bar, so its color is set once: setting it per bar parses
|
|
1898
|
+
// the color thousands of times a redraw. Each bar keeps where its stack has got to
|
|
1899
|
+
base.fill(bottom);
|
|
1900
|
+
sums.fill(0);
|
|
1901
|
+
for (let n = 0; n < parts.length; n++) {
|
|
1902
|
+
const data = partData[n];
|
|
1903
|
+
ctx.fillStyle = parts[n].color;
|
|
1904
|
+
for (let i = 0; i < bars; i++) {
|
|
1905
|
+
const seq = pick ? pick[i] : visibleFrom + i;
|
|
1906
|
+
const w = barW[i];
|
|
1907
|
+
const y0 = base[i];
|
|
1908
|
+
if (seq < 0 || w <= 0 || y0 <= top) continue;
|
|
1909
|
+
const v = data[seq % slots];
|
|
1910
|
+
if (!(v > 0)) continue;
|
|
1911
|
+
const sum = sums[i] + v;
|
|
1912
|
+
sums[i] = sum;
|
|
1913
|
+
const y1 = Math.min(y0 - 1, Math.max(top, (bottom - (sum / scale) * height + 0.5) | 0));
|
|
1914
|
+
ctx.fillRect(barX[i], y1, w, y0 - y1);
|
|
1915
|
+
base[i] = y1;
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
for (let n = 0; n < lines.length; n++) {
|
|
1919
|
+
const data = lineData[n];
|
|
1920
|
+
ctx.fillStyle = lines[n].color;
|
|
1921
|
+
for (let i = 0; i < bars; i++) {
|
|
1922
|
+
const seq = pick ? pick[i] : visibleFrom + i;
|
|
1923
|
+
if (seq < 0 || barW[i] <= 0) continue;
|
|
1924
|
+
const v = data[seq % slots];
|
|
1925
|
+
if (v > 0 && v <= scale) ctx.fillRect(barX[i], ((bottom - (v / scale) * height + 0.5) | 0) - 1, barW[i], 2);
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
// the spikes cut off by the scale, and above them the hitches
|
|
1929
|
+
for (const [above, color, h] of [[scale, '#ffffff', 2], [hitchAbove, '#ff3b30', 3]]) {
|
|
1930
|
+
if (above === Infinity) continue;
|
|
1931
|
+
ctx.fillStyle = color;
|
|
1932
|
+
for (let i = 0; i < bars; i++) {
|
|
1933
|
+
const seq = pick ? pick[i] : visibleFrom + i;
|
|
1934
|
+
if (seq >= 0 && barW[i] > 0 && totals[seq % slots] > above) ctx.fillRect(barX[i], top, barW[i], h);
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
/**
|
|
1940
|
+
* Draws a line through the values of the bars, scaled from the smallest to the largest. A
|
|
1941
|
+
* frame without a value breaks it.
|
|
1942
|
+
*
|
|
1943
|
+
* @param {CanvasRenderingContext2D} ctx - The context, with its stroke set.
|
|
1944
|
+
* @param {Float64Array} values - The ring of the channel.
|
|
1945
|
+
* @param {number} slots - The length of the ring.
|
|
1946
|
+
* @param {Int32Array|null} pick - The frame of each column, or null when each bar is a frame
|
|
1947
|
+
* of its own, from the first in view.
|
|
1948
|
+
* @param {number} visibleFrom - The first frame in view.
|
|
1949
|
+
* @param {number} bars - How many bars there are.
|
|
1950
|
+
* @param {Int32Array} barX - The left edge of each bar.
|
|
1951
|
+
* @param {Int32Array} barW - The width of each bar.
|
|
1952
|
+
* @param {number} bottom - Where the smallest value goes down the canvas.
|
|
1953
|
+
* @param {number} reach - How far up the largest goes from there.
|
|
1954
|
+
* @param {number} min - The smallest value.
|
|
1955
|
+
* @param {number} max - The largest.
|
|
1956
|
+
* @private
|
|
1957
|
+
*/
|
|
1958
|
+
static _drawLine(ctx, values, slots, pick, visibleFrom, bars, barX, barW, bottom, reach, min, max) {
|
|
1959
|
+
const spread = max - min;
|
|
1960
|
+
ctx.beginPath();
|
|
1961
|
+
let drawing = false;
|
|
1962
|
+
for (let i = 0; i < bars; i++) {
|
|
1963
|
+
const seq = pick ? pick[i] : visibleFrom + i;
|
|
1964
|
+
const v = seq >= 0 ? values[seq % slots] : NaN;
|
|
1965
|
+
if (Number.isNaN(v) || barW[i] <= 0) {
|
|
1966
|
+
drawing = false;
|
|
1967
|
+
continue;
|
|
1968
|
+
}
|
|
1969
|
+
const x = barX[i] + (barW[i] >> 1);
|
|
1970
|
+
const y = (bottom - (spread > 0 ? (v - min) / spread : 0.5) * reach + 0.5) | 0;
|
|
1971
|
+
if (drawing) {
|
|
1972
|
+
ctx.lineTo(x, y);
|
|
1973
|
+
} else {
|
|
1974
|
+
ctx.moveTo(x, y);
|
|
1975
|
+
drawing = true;
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
ctx.stroke();
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
/**
|
|
1982
|
+
* Marks the bars whose mask has a bit.
|
|
1983
|
+
*
|
|
1984
|
+
* @param {CanvasRenderingContext2D} ctx - The context, with its fill set.
|
|
1985
|
+
* @param {Int32Array} masks - The kinds of event of each bar.
|
|
1986
|
+
* @param {number} bit - The kind to mark.
|
|
1987
|
+
* @param {number} bars - How many bars there are.
|
|
1988
|
+
* @param {Int32Array} barX - The left edge of each bar.
|
|
1989
|
+
* @param {Int32Array} barW - The width of each bar.
|
|
1990
|
+
* @param {number} top - The top of the marks.
|
|
1991
|
+
* @param {number} height - Their height.
|
|
1992
|
+
* @private
|
|
1993
|
+
*/
|
|
1994
|
+
static _drawMarks(ctx, masks, bit, bars, barX, barW, top, height) {
|
|
1995
|
+
for (let i = 0; i < bars; i++) {
|
|
1996
|
+
if ((masks[i] & bit) && barW[i] > 0) ctx.fillRect(barX[i], top, barW[i], height);
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
|
|
2000
|
+
/**
|
|
2001
|
+
* @returns {PerfScriptLegend} The script types the Scripts chart gives a band of their own,
|
|
2002
|
+
* ranked over the frames selected together, or the whole capture.
|
|
2003
|
+
* @private
|
|
2004
|
+
*/
|
|
2005
|
+
_scriptLegend() {
|
|
2006
|
+
const capture = this._capture;
|
|
2007
|
+
const range = this.range;
|
|
2008
|
+
const from = range ? Math.max(capture.first, range.from) : capture.first;
|
|
2009
|
+
const to = range ? Math.min(capture.end - 1, range.to) : capture.end - 1;
|
|
2010
|
+
return scriptLegend(capture, from, to, this.scriptRows, this.pinned);
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
/**
|
|
2014
|
+
* The Scripts chart ranks its types over the frames selected together, so a new selection can
|
|
2015
|
+
* change its bands: redraws it when it does.
|
|
2016
|
+
*
|
|
2017
|
+
* @private
|
|
2018
|
+
*/
|
|
2019
|
+
_checkScriptLegend() {
|
|
2020
|
+
if (!this.collapsed.has('scripts') && this._scriptLegend().signature !== this._legendSignature) this._drawn.charts = true;
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
/**
|
|
2024
|
+
* @returns {{ legend: PerfScriptLegend, lines: PerfScriptLine[], controls: ('more'|'fewer')[] }}
|
|
2025
|
+
* What the Scripts chart stacks: the script types with a band of their own and the others
|
|
2026
|
+
* together; and the rows of its list that show more types or fewer.
|
|
2027
|
+
* @private
|
|
2028
|
+
*/
|
|
2029
|
+
_scriptList() {
|
|
2030
|
+
const legend = this._scriptLegend();
|
|
2031
|
+
/** @type {PerfScriptLine[]} */
|
|
2032
|
+
const lines = legend.types.map(type => ({ key: type.name, name: type.name, color: type.color, id: type.id, pinned: type.pinned }));
|
|
2033
|
+
if (legend.others) lines.push({ key: '#other', name: 'Other scripts', color: OTHER_SCRIPTS_COLOR, id: -1, pinned: false });
|
|
2034
|
+
/** @type {('more'|'fewer')[]} */
|
|
2035
|
+
const controls = [];
|
|
2036
|
+
if (legend.others) controls.push('more');
|
|
2037
|
+
if (this.scriptRows > SCRIPT_ROWS) controls.push('fewer');
|
|
2038
|
+
return { legend, lines, controls };
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
/**
|
|
2042
|
+
* @returns {string} Why the Scripts chart has nothing to draw.
|
|
2043
|
+
* @private
|
|
2044
|
+
*/
|
|
2045
|
+
_scriptsEmpty() {
|
|
2046
|
+
const capture = this._capture;
|
|
2047
|
+
if (!capture.supports.scripts) return 'This app has no script system';
|
|
2048
|
+
if (!capture.timeScripts) return 'Turn Script timing on to time each script type while recording';
|
|
2049
|
+
return capture.count ? 'No script was timed in the frames kept' : 'Each script type is timed while recording';
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
/**
|
|
2053
|
+
* Draws the time of each script type stacked, beside the list of them, each with a box ticked
|
|
2054
|
+
* while it shows, so hiding the large ones lets the small ones fill the chart.
|
|
2055
|
+
*
|
|
2056
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
2057
|
+
* @param {{ legend: PerfScriptLegend, lines: PerfScriptLine[], controls: ('more'|'fewer')[] }} list - What
|
|
2058
|
+
* it stacks, and the rows of its list that show more types or fewer.
|
|
2059
|
+
* @param {number} top - The top of the chart.
|
|
2060
|
+
* @param {number} width - The width of the canvas.
|
|
2061
|
+
* @param {number} plot - The width of the plot.
|
|
2062
|
+
* @param {Int32Array} lo - The first frame of each column.
|
|
2063
|
+
* @param {Int32Array} hi - One past the last frame of each column.
|
|
2064
|
+
* @param {boolean} wide - Whether each bar is a frame of its own.
|
|
2065
|
+
* @param {number} visibleFrom - The first frame in view.
|
|
2066
|
+
* @param {number} visibleTo - One past the last frame in view.
|
|
2067
|
+
* @param {number} bars - How many bars there are.
|
|
2068
|
+
* @param {Int32Array} barX - The left edge of each bar.
|
|
2069
|
+
* @param {Int32Array} barW - The width of each bar.
|
|
2070
|
+
* @returns {PerfLineChart|null} What it drew, for the pointer, or null for nothing.
|
|
2071
|
+
* @private
|
|
2072
|
+
*/
|
|
2073
|
+
_drawScriptChart(ctx, { legend, lines: list, controls }, top, width, plot, lo, hi, wide, visibleFrom, visibleTo, bars, barX, barW) {
|
|
2074
|
+
const capture = this._capture;
|
|
2075
|
+
const d = capture.data;
|
|
2076
|
+
const slots = capture.slots;
|
|
2077
|
+
const rows = Math.max(SCRIPT_CHART_ROWS, list.length + controls.length);
|
|
2078
|
+
const height = rows * SCRIPT_ROW;
|
|
2079
|
+
this._legendSignature = legend.signature;
|
|
2080
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.03)';
|
|
2081
|
+
ctx.fillRect(this._gutter, top, plot, height);
|
|
2082
|
+
if (!list.length) {
|
|
2083
|
+
this._message(ctx, this._scriptsEmpty(), '#7f8f93', plot, top + height / 2 - 5);
|
|
2084
|
+
return null;
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
// the time of each type in each frame in view, in a ring of its own, the others' being what
|
|
2088
|
+
// the listed ones leave of all the scripts' time, and the sum of those shown
|
|
2089
|
+
const hidden = list.map(line => this.hidden.has(`scripts:${line.key}`));
|
|
2090
|
+
const rings = list.map((line, i) => this._floats(`scripts:${i}`, slots));
|
|
2091
|
+
const sums = this._floats('scripts:sum', slots);
|
|
2092
|
+
for (let seq = visibleFrom; seq < visibleTo; seq++) {
|
|
2093
|
+
const slot = seq % slots;
|
|
2094
|
+
const times = capture.scriptFigures(seq);
|
|
2095
|
+
let listed = 0;
|
|
2096
|
+
let shown = 0;
|
|
2097
|
+
for (let i = 0; i < list.length; i++) {
|
|
2098
|
+
const id = list[i].id;
|
|
2099
|
+
let v = NaN;
|
|
2100
|
+
if (times && id >= 0) {
|
|
2101
|
+
const base = id * SCRIPT_STRIDE;
|
|
2102
|
+
v = base < times.length ? times[base] + times[base + 1] + times[base + 2] : 0;
|
|
2103
|
+
listed += v;
|
|
2104
|
+
} else if (times) {
|
|
2105
|
+
v = Math.max(0, d.scriptsTotal[slot] - listed);
|
|
2106
|
+
}
|
|
2107
|
+
rings[i][slot] = v;
|
|
2108
|
+
if (!hidden[i] && v > 0) shown += v;
|
|
2109
|
+
}
|
|
2110
|
+
sums[slot] = times ? shown : NaN;
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
// the list: a box each, ticked while the type shows, a pin before the types pinned, then the
|
|
2114
|
+
// rows showing more types or fewer, and the others last; the rows below it still read the
|
|
2115
|
+
// chart
|
|
2116
|
+
const parts = [];
|
|
2117
|
+
const partData = [];
|
|
2118
|
+
const other = list.length && list[list.length - 1].id === -1 ? list.length - 1 : -1;
|
|
2119
|
+
/** @type {{ index: number, control?: 'more'|'fewer' }[]} */
|
|
2120
|
+
const order = [];
|
|
2121
|
+
for (let i = 0; i < list.length; i++) {
|
|
2122
|
+
if (i !== other) order.push({ index: i });
|
|
2123
|
+
}
|
|
2124
|
+
for (const control of controls) order.push({ index: -1, control });
|
|
2125
|
+
if (other >= 0) order.push({ index: other });
|
|
2126
|
+
for (let r = 0; r < rows; r++) {
|
|
2127
|
+
const y = top + r * SCRIPT_ROW;
|
|
2128
|
+
const entry = order[r];
|
|
2129
|
+
if (entry?.control) {
|
|
2130
|
+
this._rows.push({ top: y, height: SCRIPT_ROW, lines: 'scripts', index: -1, control: entry.control });
|
|
2131
|
+
ctx.fillStyle = '#93a2a6';
|
|
2132
|
+
this._gutterText(ctx, entry.control === 'more' ? `▸ ${Math.min(SCRIPT_ROWS, legend.others)} more` : '▴ Fewer', 10, y + 1);
|
|
2133
|
+
continue;
|
|
2134
|
+
}
|
|
2135
|
+
const i = entry ? entry.index : -1;
|
|
2136
|
+
this._rows.push({ top: y, height: SCRIPT_ROW, lines: 'scripts', index: i });
|
|
2137
|
+
if (i < 0) continue;
|
|
2138
|
+
const line = list[i];
|
|
2139
|
+
PerfCharts._checkbox(ctx, 10.5, y + 2.5, line.color, !hidden[i]);
|
|
2140
|
+
ctx.fillStyle = hidden[i] ? '#56656a' : '#b9c4c7';
|
|
2141
|
+
this._gutterText(ctx, `${line.pinned ? '📌' : ''}${line.name}`, 23, y + 1);
|
|
2142
|
+
}
|
|
2143
|
+
// stacked from the first of the list up, so the longest is the steady base and the others
|
|
2144
|
+
// ride on top
|
|
2145
|
+
for (let i = 0; i < list.length; i++) {
|
|
2146
|
+
if (hidden[i]) continue;
|
|
2147
|
+
const line = list[i];
|
|
2148
|
+
parts.push({ key: line.key, name: line.name, color: line.color, channel: '' });
|
|
2149
|
+
partData.push(rings[i]);
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
// scaled to the largest frame of the columns, short of the spikes, as the CPU chart is
|
|
2153
|
+
const pick = this._ints('scripts:pick', lo.length).fill(-1);
|
|
2154
|
+
const peaks = this._floats('peaks', lo.length);
|
|
2155
|
+
const peakCount = PerfCharts._pickLargest(sums, slots, lo, hi, pick, peaks);
|
|
2156
|
+
const sorted = peaks.subarray(0, peakCount).sort();
|
|
2157
|
+
const max = peakCount ? sorted[peakCount - 1] : 0;
|
|
2158
|
+
const typical = peakCount ? sorted[Math.floor(0.95 * (peakCount - 1))] : 0;
|
|
2159
|
+
const scale = niceCeil(Math.min(max, SPIKE_HEADROOM * typical));
|
|
2160
|
+
/** @type {PerfShape} */
|
|
2161
|
+
const shape = { total: 'scriptsTotal', parts, lines: NO_PARTS, partData, lineData: [], reduced: false };
|
|
2162
|
+
PerfCharts._drawStack(ctx, shape, sums, slots, wide ? null : pick, visibleFrom, bars, barX, barW, top, height, scale, Infinity,
|
|
2163
|
+
this._ints('stackBase', bars), this._floats('stackSum', bars));
|
|
2164
|
+
PerfCharts._label(ctx, formatMs(scale), width - 3, top + 1, 'right');
|
|
2165
|
+
const levels = { top, bottom: top + height, min: 0, max: scale, format: formatMs };
|
|
2166
|
+
return { lines: list, format: formatMs, pick, min: 0, max: scale, levels, values: rings, stacked: true, legend };
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
/**
|
|
2170
|
+
* @returns {PerfLine[]} The lines of the Memory chart, the JavaScript heap where the browser
|
|
2171
|
+
* reports it.
|
|
2172
|
+
* @private
|
|
2173
|
+
*/
|
|
2174
|
+
_memoryList() {
|
|
2175
|
+
const heap = this._capture.supports.heap;
|
|
2176
|
+
return MEMORY.map(line => (line.key === 'heap' && !heap ? { ...line, unsupported: 'Only Chromium browsers report the size of the JavaScript heap' } : line));
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
/**
|
|
2180
|
+
* Draws a chart of lines sharing a scale, beside the list of them, each with a box ticked
|
|
2181
|
+
* while its line shows. The scale runs from the smallest to the largest value of the lines
|
|
2182
|
+
* shown in view, spread to a least range so a level that barely changes is flat.
|
|
2183
|
+
*
|
|
2184
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
2185
|
+
* @param {string} section - The section the chart is in, which the hidden lines are kept
|
|
2186
|
+
* under.
|
|
2187
|
+
* @param {PerfLine[]} lines - Its lines.
|
|
2188
|
+
* @param {(value: number) => string} format - Formats their values.
|
|
2189
|
+
* @param {(max: number) => number} least - The least range for a largest value.
|
|
2190
|
+
* @param {number} top - The top of the chart.
|
|
2191
|
+
* @param {number} rowHeight - The height of a line of the list; the chart is as tall as the
|
|
2192
|
+
* list.
|
|
2193
|
+
* @param {number} width - The width of the canvas.
|
|
2194
|
+
* @param {number} plot - The width of the plot.
|
|
2195
|
+
* @param {Int32Array} lo - The first frame of each column.
|
|
2196
|
+
* @param {Int32Array} hi - One past the last frame of each column.
|
|
2197
|
+
* @param {boolean} wide - Whether each bar is a frame of its own.
|
|
2198
|
+
* @param {number} visibleFrom - The first frame in view.
|
|
2199
|
+
* @param {number} visibleTo - One past the last frame in view.
|
|
2200
|
+
* @param {number} bars - How many bars there are.
|
|
2201
|
+
* @param {Int32Array} barX - The left edge of each bar.
|
|
2202
|
+
* @param {Int32Array} barW - The width of each bar.
|
|
2203
|
+
* @param {boolean} [own] - Whether each line has a scale of its own, from its smallest to its
|
|
2204
|
+
* largest value in view, as lines of different units do. The chart then reads no levels.
|
|
2205
|
+
* @returns {PerfLineChart} What it drew, for the pointer.
|
|
2206
|
+
* @private
|
|
2207
|
+
*/
|
|
2208
|
+
_drawLineChart(ctx, section, lines, format, least, top, rowHeight, width, plot, lo, hi, wide, visibleFrom, visibleTo, bars, barX, barW, own = false) {
|
|
2209
|
+
const capture = this._capture;
|
|
2210
|
+
const d = capture.data;
|
|
2211
|
+
const slots = capture.slots;
|
|
2212
|
+
const height = lines.length * rowHeight;
|
|
2213
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.03)';
|
|
2214
|
+
ctx.fillRect(this._gutter, top, plot, height);
|
|
2215
|
+
|
|
2216
|
+
// the list, a box each, ticked while the line shows
|
|
2217
|
+
const range = this._floats('lineRange', 2);
|
|
2218
|
+
let min = Infinity;
|
|
2219
|
+
let max = -Infinity;
|
|
2220
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2221
|
+
const line = lines[i];
|
|
2222
|
+
const y = top + i * rowHeight;
|
|
2223
|
+
this._rows.push({ top: y, height: rowHeight, lines: section, index: i });
|
|
2224
|
+
const on = !line.unsupported && !this.hidden.has(`${section}:${line.key}`);
|
|
2225
|
+
PerfCharts._checkbox(ctx, 10.5, y + 2.5, line.unsupported ? '#56656a' : line.color, on);
|
|
2226
|
+
ctx.fillStyle = on ? '#b9c4c7' : '#56656a';
|
|
2227
|
+
this._gutterText(ctx, line.name, 23, y + 1);
|
|
2228
|
+
if (!on) continue;
|
|
2229
|
+
PerfCharts._rangeOf(line.values ?? d[line.key], slots, visibleFrom, visibleTo, range);
|
|
2230
|
+
if (range[0] < min) min = range[0];
|
|
2231
|
+
if (range[1] > max) max = range[1];
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
// the last frame of each column, as these are levels rather than costs
|
|
2235
|
+
const pick = this._ints(`${section}:pick`, lo.length);
|
|
2236
|
+
for (let c = 0; c < lo.length; c++) pick[c] = hi[c] > lo[c] ? hi[c] - 1 : -1;
|
|
2237
|
+
/** @type {PerfLineChart} */
|
|
2238
|
+
const drawn = { lines, format, pick, min: 0, max: 0, levels: null };
|
|
2239
|
+
if (!(max >= min)) return drawn;
|
|
2240
|
+
|
|
2241
|
+
// each line from its own smallest to its own largest, spread to the least range
|
|
2242
|
+
if (own) {
|
|
2243
|
+
ctx.lineWidth = 1.5;
|
|
2244
|
+
for (const line of lines) {
|
|
2245
|
+
if (line.unsupported || this.hidden.has(`${section}:${line.key}`)) continue;
|
|
2246
|
+
const values = line.values ?? d[line.key];
|
|
2247
|
+
PerfCharts._rangeOf(values, slots, visibleFrom, visibleTo, range);
|
|
2248
|
+
let lineMin = range[0];
|
|
2249
|
+
let lineMax = range[1];
|
|
2250
|
+
if (!(lineMax >= lineMin)) continue;
|
|
2251
|
+
const spread = least(lineMax);
|
|
2252
|
+
if (lineMax - lineMin < spread) {
|
|
2253
|
+
lineMin = (lineMin + lineMax) / 2 - spread / 2;
|
|
2254
|
+
lineMax = lineMin + spread;
|
|
2255
|
+
}
|
|
2256
|
+
ctx.strokeStyle = line.color;
|
|
2257
|
+
PerfCharts._drawLine(ctx, values, slots, wide ? null : pick, visibleFrom, bars, barX, barW, top + height - 2, height - 4, lineMin, lineMax);
|
|
2258
|
+
}
|
|
2259
|
+
ctx.lineWidth = 1;
|
|
2260
|
+
return drawn;
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
const span = least(max);
|
|
2264
|
+
if (max - min < span) {
|
|
2265
|
+
const middle = (min + max) / 2;
|
|
2266
|
+
min = Math.max(0, middle - span / 2);
|
|
2267
|
+
max = min + span;
|
|
2268
|
+
}
|
|
2269
|
+
drawn.min = min;
|
|
2270
|
+
drawn.max = max;
|
|
2271
|
+
// the lines are drawn between two pixels in from the top and the bottom
|
|
2272
|
+
drawn.levels = { top: top + 2, bottom: top + height - 2, min, max, format };
|
|
2273
|
+
|
|
2274
|
+
ctx.lineWidth = 1.5;
|
|
2275
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2276
|
+
const line = lines[i];
|
|
2277
|
+
if (line.unsupported || this.hidden.has(`${section}:${line.key}`)) continue;
|
|
2278
|
+
ctx.strokeStyle = line.color;
|
|
2279
|
+
PerfCharts._drawLine(ctx, line.values ?? d[line.key], slots, wide ? null : pick, visibleFrom, bars, barX, barW, top + height - 2, height - 4, min, max);
|
|
2280
|
+
}
|
|
2281
|
+
ctx.lineWidth = 1;
|
|
2282
|
+
PerfCharts._label(ctx, format(max), width - 3, top + 1, 'right');
|
|
2283
|
+
PerfCharts._label(ctx, format(min), width - 3, top + height - 12, 'right');
|
|
2284
|
+
return drawn;
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
/**
|
|
2288
|
+
* Draws the box beside a line or a row of events, ticked while it shows.
|
|
2289
|
+
*
|
|
2290
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
2291
|
+
* @param {number} x - Its left edge, on a half pixel so its outline is sharp.
|
|
2292
|
+
* @param {number} y - Its top edge, likewise.
|
|
2293
|
+
* @param {string} color - The color of what it shows.
|
|
2294
|
+
* @param {boolean} on - Whether it is ticked.
|
|
2295
|
+
* @private
|
|
2296
|
+
*/
|
|
2297
|
+
static _checkbox(ctx, x, y, color, on) {
|
|
2298
|
+
ctx.strokeStyle = color;
|
|
2299
|
+
ctx.strokeRect(x, y, 7, 7);
|
|
2300
|
+
if (on) {
|
|
2301
|
+
ctx.fillStyle = color;
|
|
2302
|
+
ctx.fillRect(x + 1.5, y + 1.5, 4, 4);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
|
|
2306
|
+
/**
|
|
2307
|
+
* Finds the smallest and largest value of a span of frames.
|
|
2308
|
+
*
|
|
2309
|
+
* @param {Float64Array} values - The ring of the channel.
|
|
2310
|
+
* @param {number} slots - The length of the ring.
|
|
2311
|
+
* @param {number} from - The first frame.
|
|
2312
|
+
* @param {number} to - One past the last frame.
|
|
2313
|
+
* @param {Float64Array} range - Receives the smallest and the largest value, Infinity and
|
|
2314
|
+
* -Infinity when there are none.
|
|
2315
|
+
* @private
|
|
2316
|
+
*/
|
|
2317
|
+
static _rangeOf(values, slots, from, to, range) {
|
|
2318
|
+
let min = Infinity;
|
|
2319
|
+
let max = -Infinity;
|
|
2320
|
+
for (let seq = from; seq < to; seq++) {
|
|
2321
|
+
const v = values[seq % slots];
|
|
2322
|
+
if (Number.isNaN(v)) continue;
|
|
2323
|
+
if (v < min) min = v;
|
|
2324
|
+
if (v > max) max = v;
|
|
2325
|
+
}
|
|
2326
|
+
range[0] = min;
|
|
2327
|
+
range[1] = max;
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
/**
|
|
2331
|
+
* @returns {{ key: string, name: string, color: string }[]} The resources the device has.
|
|
2332
|
+
* @private
|
|
2333
|
+
*/
|
|
2334
|
+
_resourceList() {
|
|
2335
|
+
return this._capture.supports.pipelines ? RESOURCES : RESOURCES.filter(resource => resource.key !== 'pipelines');
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
/**
|
|
2339
|
+
* @returns {number[]} The kinds of event the Events section has a row for, by their index into
|
|
2340
|
+
* {@link EVENT_KINDS}: the bakes of lightmaps only for an app with a lightmapper.
|
|
2341
|
+
* @private
|
|
2342
|
+
*/
|
|
2343
|
+
_eventKinds() {
|
|
2344
|
+
const lightmapper = this._capture.supports.lightmapper;
|
|
2345
|
+
const kinds = [];
|
|
2346
|
+
for (let k = 0; k < EVENT_KINDS.length; k++) {
|
|
2347
|
+
if (EVENT_KINDS[k].key !== 'bake' || lightmapper) kinds.push(k);
|
|
2348
|
+
}
|
|
2349
|
+
return kinds;
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
/**
|
|
2353
|
+
* @returns {PerfLine[]} The lines of the Counters chart, one for each counter of
|
|
2354
|
+
* `app.stats.user` the app has set, in the order it first set them.
|
|
2355
|
+
* @private
|
|
2356
|
+
*/
|
|
2357
|
+
_counterList() {
|
|
2358
|
+
const capture = this._capture;
|
|
2359
|
+
return capture.counterNames.map((name, id) => ({ key: name, name, color: scriptColorAt(id), values: capture.counterRings[id] }));
|
|
2360
|
+
}
|
|
2361
|
+
|
|
2362
|
+
/**
|
|
2363
|
+
* Draws the title of a section under the charts, which collapses or expands it when clicked.
|
|
2364
|
+
*
|
|
2365
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
2366
|
+
* @param {string} key - The section.
|
|
2367
|
+
* @param {string} title - Its title.
|
|
2368
|
+
* @param {number} top - Where it goes down the canvas.
|
|
2369
|
+
* @param {boolean} open - Whether the section is expanded.
|
|
2370
|
+
* @private
|
|
2371
|
+
*/
|
|
2372
|
+
_sectionTitle(ctx, key, title, top, open) {
|
|
2373
|
+
this._rows.push({ top, height: SECTION_TITLE, section: key });
|
|
2374
|
+
ctx.font = BOLD;
|
|
2375
|
+
ctx.fillStyle = '#dbe2e4';
|
|
2376
|
+
this._gutterText(ctx, `${open ? '▾' : '▸'} ${title}`, 4, top + 2);
|
|
2377
|
+
ctx.font = FONT;
|
|
2378
|
+
}
|
|
2379
|
+
|
|
2380
|
+
/**
|
|
2381
|
+
* Draws the marks over the charts: the frames selected together, the frame hovered and the
|
|
2382
|
+
* frame shown, where the charts last drew them.
|
|
2383
|
+
*
|
|
2384
|
+
* @private
|
|
2385
|
+
*/
|
|
2386
|
+
_drawOverlay() {
|
|
2387
|
+
const prepared = this._prepare(this._overlay, this._chartsEl, this._contentHeight || undefined);
|
|
2388
|
+
if (!prepared) return;
|
|
2389
|
+
const { ctx } = prepared;
|
|
2390
|
+
const capture = this._capture;
|
|
2391
|
+
const { bottom, plot } = this._layout;
|
|
2392
|
+
// the marks of entries cut to the plot, as one zoomed into begins left of it
|
|
2393
|
+
ctx.save();
|
|
2394
|
+
ctx.beginPath();
|
|
2395
|
+
ctx.rect(this._gutter, 0, plot, bottom);
|
|
2396
|
+
ctx.clip();
|
|
2397
|
+
if (this.range && capture.has(this.range.from) && capture.has(this.range.to)) {
|
|
2398
|
+
const x0 = this._xOf(this.range.from);
|
|
2399
|
+
const x1 = this._xOf(this.range.to) + this._wOf(this.range.to);
|
|
2400
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.1)';
|
|
2401
|
+
ctx.fillRect(x0, 0, Math.max(1, x1 - x0), bottom);
|
|
2402
|
+
}
|
|
2403
|
+
// an entry wide enough is covered whole and edged, a narrower one marked by a line through
|
|
2404
|
+
// its middle
|
|
2405
|
+
const mark = (seq, fill, edge) => {
|
|
2406
|
+
const x = this._xOf(seq);
|
|
2407
|
+
const w = this._wOf(seq);
|
|
2408
|
+
if (x + w < this._gutter) return;
|
|
2409
|
+
if (w >= 3) {
|
|
2410
|
+
ctx.fillStyle = fill;
|
|
2411
|
+
ctx.fillRect(x, 0, w, bottom);
|
|
2412
|
+
ctx.fillStyle = edge;
|
|
2413
|
+
ctx.fillRect(x, 0, 1, bottom);
|
|
2414
|
+
ctx.fillRect(x + w - 1, 0, 1, bottom);
|
|
2415
|
+
} else {
|
|
2416
|
+
ctx.fillStyle = edge;
|
|
2417
|
+
ctx.fillRect(Math.round(x + w / 2) - 1, 0, 2, bottom);
|
|
2418
|
+
}
|
|
2419
|
+
};
|
|
2420
|
+
const shown = capture.count ? this.shown : -1;
|
|
2421
|
+
if (this._hover >= 0 && capture.has(this._hover) && this._hover !== shown) {
|
|
2422
|
+
mark(this._hover, 'rgba(255, 255, 255, 0.1)', 'rgba(255, 255, 255, 0.4)');
|
|
2423
|
+
}
|
|
2424
|
+
if (shown >= 0) mark(shown, 'rgba(255, 255, 255, 0.22)', '#ffffff');
|
|
2425
|
+
ctx.restore();
|
|
2426
|
+
this._drawDivider(ctx, bottom);
|
|
2427
|
+
|
|
2428
|
+
// the level under the pointer, across the chart it is over, with its value at the left
|
|
2429
|
+
const level = this._level;
|
|
2430
|
+
if (level) {
|
|
2431
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';
|
|
2432
|
+
ctx.fillRect(this._gutter, level.y, plot, 1);
|
|
2433
|
+
PerfCharts._label(ctx, level.format(level.value), this._gutter + 4, level.y - 13 < 0 ? level.y + 3 : level.y - 13, 'left');
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
/**
|
|
2438
|
+
* Draws the divider between the names and the plot, brighter while under the pointer.
|
|
2439
|
+
*
|
|
2440
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
2441
|
+
* @param {number} height - How far down it goes.
|
|
2442
|
+
* @private
|
|
2443
|
+
*/
|
|
2444
|
+
_drawDivider(ctx, height) {
|
|
2445
|
+
ctx.fillStyle = this._dividerHot ? 'rgba(255, 255, 255, 0.45)' : 'rgba(255, 255, 255, 0.07)';
|
|
2446
|
+
ctx.fillRect(this._gutter - (this._dividerHot ? 3 : 2), 0, this._dividerHot ? 2 : 1, height);
|
|
2447
|
+
}
|
|
2448
|
+
|
|
2449
|
+
/**
|
|
2450
|
+
* Draws a label over a dark backing, to read over the bars.
|
|
2451
|
+
*
|
|
2452
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
2453
|
+
* @param {string} text - The text.
|
|
2454
|
+
* @param {number} x - Where it is anchored across.
|
|
2455
|
+
* @param {number} y - Its top.
|
|
2456
|
+
* @param {'left'|'right'} align - Which side the anchor is on.
|
|
2457
|
+
* @private
|
|
2458
|
+
*/
|
|
2459
|
+
static _label(ctx, text, x, y, align) {
|
|
2460
|
+
const w = ctx.measureText(text).width;
|
|
2461
|
+
const left = align === 'left' ? x : x - w;
|
|
2462
|
+
ctx.fillStyle = 'rgba(19, 25, 27, 0.75)';
|
|
2463
|
+
ctx.fillRect(left - 2, y - 1, w + 4, 12);
|
|
2464
|
+
ctx.fillStyle = '#dbe2e4';
|
|
2465
|
+
ctx.fillText(text, left, y);
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
/**
|
|
2469
|
+
* Draws a line of text centered across the plot, cut short with an ellipsis where the plot is
|
|
2470
|
+
* too narrow for it.
|
|
2471
|
+
*
|
|
2472
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
2473
|
+
* @param {string} text - The text.
|
|
2474
|
+
* @param {string} color - Its color.
|
|
2475
|
+
* @param {number} plot - The width of the plot.
|
|
2476
|
+
* @param {number} y - Its top.
|
|
2477
|
+
* @private
|
|
2478
|
+
*/
|
|
2479
|
+
_message(ctx, text, color, plot, y) {
|
|
2480
|
+
ctx.fillStyle = color;
|
|
2481
|
+
ctx.textAlign = 'center';
|
|
2482
|
+
ctx.fillText(PerfCharts._fit(ctx, text, '', plot - 8), this._gutter + plot / 2, y);
|
|
2483
|
+
ctx.textAlign = 'left';
|
|
2484
|
+
}
|
|
2485
|
+
|
|
2486
|
+
/**
|
|
2487
|
+
* As much of a label as fits: the name and its time, the name alone, or the start of the name
|
|
2488
|
+
* cut short with an ellipsis.
|
|
2489
|
+
*
|
|
2490
|
+
* @param {CanvasRenderingContext2D} ctx - The context, with the font set.
|
|
2491
|
+
* @param {string} name - The name.
|
|
2492
|
+
* @param {string} time - Its time, empty for none.
|
|
2493
|
+
* @param {number} room - The width there is.
|
|
2494
|
+
* @returns {string} What fits, empty when not even a letter does.
|
|
2495
|
+
* @private
|
|
2496
|
+
*/
|
|
2497
|
+
static _fit(ctx, name, time, room) {
|
|
2498
|
+
if (time) {
|
|
2499
|
+
const text = `${name} ${time}`;
|
|
2500
|
+
if (ctx.measureText(text).width <= room) return text;
|
|
2501
|
+
}
|
|
2502
|
+
if (ctx.measureText(name).width <= room) return name;
|
|
2503
|
+
// the most letters that fit with the ellipsis after them
|
|
2504
|
+
let lo = 0;
|
|
2505
|
+
let hi = name.length - 1;
|
|
2506
|
+
while (lo < hi) {
|
|
2507
|
+
const mid = (lo + hi + 1) >> 1;
|
|
2508
|
+
if (ctx.measureText(`${name.slice(0, mid)}…`).width <= room) lo = mid;
|
|
2509
|
+
else hi = mid - 1;
|
|
2510
|
+
}
|
|
2511
|
+
return lo ? `${name.slice(0, lo).trimEnd()}…` : '';
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
/**
|
|
2515
|
+
* Draws a name in the gutter left of the plot, cut to what fits before the plot, as the width
|
|
2516
|
+
* of the font differs between systems.
|
|
2517
|
+
*
|
|
2518
|
+
* @param {CanvasRenderingContext2D} ctx - The context, with the font set.
|
|
2519
|
+
* @param {string} text - The name.
|
|
2520
|
+
* @param {number} x - Where it starts.
|
|
2521
|
+
* @param {number} y - Its top.
|
|
2522
|
+
* @private
|
|
2523
|
+
*/
|
|
2524
|
+
_gutterText(ctx, text, x, y) {
|
|
2525
|
+
const fitted = PerfCharts._fit(ctx, text, '', this._gutter - x - 4);
|
|
2526
|
+
if (fitted) ctx.fillText(fitted, x, y);
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
/**
|
|
2530
|
+
* What the timeline shows: the frame shown, or with frames selected together their average
|
|
2531
|
+
* frame, each phase at its average start for its average time per frame, and each GPU pass for
|
|
2532
|
+
* its average time.
|
|
2533
|
+
*
|
|
2534
|
+
* @returns {{ title: string, interval: number, frames: number, bars: PerfTimeBar[] }|null} The
|
|
2535
|
+
* title, the frame interval, how many frames it covers and the bars, in milliseconds from the
|
|
2536
|
+
* start of the frame. Null when there is nothing to show.
|
|
2537
|
+
* @private
|
|
2538
|
+
*/
|
|
2539
|
+
_timelineModel() {
|
|
2540
|
+
const capture = this._capture;
|
|
2541
|
+
if (!capture.count) return null;
|
|
2542
|
+
const d = capture.data;
|
|
2543
|
+
const range = this.range;
|
|
2544
|
+
const from = range ? Math.max(capture.first, range.from) : this.shown;
|
|
2545
|
+
const to = range ? Math.min(capture.end - 1, range.to) : this.shown;
|
|
2546
|
+
if (to < from) return null;
|
|
2547
|
+
const frames = to - from + 1;
|
|
2548
|
+
|
|
2549
|
+
// the mean of a figure over the frames, and of the start of a phase over the frames that
|
|
2550
|
+
// timed it
|
|
2551
|
+
const mean = (key) => {
|
|
2552
|
+
let sum = 0;
|
|
2553
|
+
for (let seq = from; seq <= to; seq++) {
|
|
2554
|
+
const v = d[key][capture.slot(seq)];
|
|
2555
|
+
if (Number.isFinite(v)) sum += v;
|
|
2556
|
+
}
|
|
2557
|
+
return sum / frames;
|
|
2558
|
+
};
|
|
2559
|
+
const start = (key) => {
|
|
2560
|
+
let sum = 0;
|
|
2561
|
+
let n = 0;
|
|
2562
|
+
for (let seq = from; seq <= to; seq++) {
|
|
2563
|
+
const slot = capture.slot(seq);
|
|
2564
|
+
const v = d[key][slot] - d.tick[slot];
|
|
2565
|
+
if (Number.isFinite(v)) {
|
|
2566
|
+
sum += v;
|
|
2567
|
+
n++;
|
|
2568
|
+
}
|
|
2569
|
+
}
|
|
2570
|
+
return n ? sum / n : NaN;
|
|
2571
|
+
};
|
|
2572
|
+
|
|
2573
|
+
/** @type {PerfTimeBar[]} */
|
|
2574
|
+
let bars = [];
|
|
2575
|
+
const phase = (lane, startKey, key, color, name) => {
|
|
2576
|
+
bars.push({ lane, start: start(startKey), duration: mean(key), color, name, positioned: true });
|
|
2577
|
+
};
|
|
2578
|
+
// the mean span between two timestamps of a frame, over the frames that have both
|
|
2579
|
+
const between = (fromKey, toKey) => {
|
|
2580
|
+
let sum = 0;
|
|
2581
|
+
let n = 0;
|
|
2582
|
+
for (let seq = from; seq <= to; seq++) {
|
|
2583
|
+
const slot = capture.slot(seq);
|
|
2584
|
+
const v = d[toKey][slot] - d[fromKey][slot];
|
|
2585
|
+
if (Number.isFinite(v)) {
|
|
2586
|
+
sum += v;
|
|
2587
|
+
n++;
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
return n ? sum / n : NaN;
|
|
2591
|
+
};
|
|
2592
|
+
const color = key => CPU_PARTS.find(p => p.key === key).color;
|
|
2593
|
+
|
|
2594
|
+
// the frame in the order scripts see it: the update, the post-update, the update event,
|
|
2595
|
+
// where the inspector does its work, and the render. The update runs until the post-update
|
|
2596
|
+
// starts: the update of the systems, which steps the physics, and the animation
|
|
2597
|
+
bars.push({ lane: 'cpu', start: start('updateStart'), duration: between('updateStart', 'postStart'), color: UPDATE_COLOR, name: 'Update', positioned: true });
|
|
2598
|
+
phase('cpu', 'postStart', 'post', color('post'), 'Post-update');
|
|
2599
|
+
phase('cpu', 'inspectorStart', 'inspector', color('inspector'), 'Inspector');
|
|
2600
|
+
phase('cpu', 'renderStart', 'render', color('render'), 'Render');
|
|
2601
|
+
// the line below breaks the update and the render down, as a flame chart does, leaving
|
|
2602
|
+
// what is not timed apart as a gap: the physics step and the animation where they ran,
|
|
2603
|
+
// the rest of the update being the scripts and the other components; and the phases of
|
|
2604
|
+
// the render the profiler and debug engines time, back to back from its start, as they do
|
|
2605
|
+
// not say when each ran
|
|
2606
|
+
phase('detail', 'physicsStart', 'physics', color('physics'), 'Physics');
|
|
2607
|
+
phase('detail', 'animStart', 'anim', color('anim'), 'Animation');
|
|
2608
|
+
// the scripts while they were timed: their own time in the update and the post-update,
|
|
2609
|
+
// each from the start of its first call, the mean over the frames that timed them
|
|
2610
|
+
if (Number.isFinite(start('scriptsUpdateStart')) || Number.isFinite(start('scriptsPostStart'))) {
|
|
2611
|
+
const timedMean = (key) => {
|
|
2612
|
+
let sum = 0;
|
|
2613
|
+
let n = 0;
|
|
2614
|
+
for (let seq = from; seq <= to; seq++) {
|
|
2615
|
+
const v = d[key][capture.slot(seq)];
|
|
2616
|
+
if (Number.isFinite(v)) {
|
|
2617
|
+
sum += v;
|
|
2618
|
+
n++;
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
return n ? sum / n : NaN;
|
|
2622
|
+
};
|
|
2623
|
+
const scriptsColor = color('scripts');
|
|
2624
|
+
const updateTypes = this._scriptTypes(from, to, 0);
|
|
2625
|
+
const postTypes = this._scriptTypes(from, to, 1);
|
|
2626
|
+
bars.push({ lane: 'detail', start: start('scriptsUpdateStart'), duration: timedMean('scriptsUpdate'), color: scriptsColor, name: 'Scripts', positioned: true, children: updateTypes });
|
|
2627
|
+
bars.push({ lane: 'detail', start: start('scriptsPostStart'), duration: timedMean('scriptsPost'), color: scriptsColor, name: 'Post-update scripts', positioned: true, children: postTypes });
|
|
2628
|
+
// the line below them breaks each down by script type. The calls of the types
|
|
2629
|
+
// interleave, entity by entity, so each is its time in all, back to back from the start
|
|
2630
|
+
// of the scripts in the order the types first ran; what is left at the end is the
|
|
2631
|
+
// initializing of scripts created along the way
|
|
2632
|
+
for (const [types, startKey, method] of [[updateTypes, 'scriptsUpdateStart', ''], [postTypes, 'scriptsPostStart', ', post-update']]) {
|
|
2633
|
+
let at = start(startKey);
|
|
2634
|
+
for (const type of [...types].sort((a, b) => a.order - b.order)) {
|
|
2635
|
+
bars.push({ lane: 'types', start: at, duration: type.duration, color: type.color, name: type.name, positioned: false, calls: type.calls, label: `${type.name}${method}`, script: type.id });
|
|
2636
|
+
at += type.duration;
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
if (capture.detailed) {
|
|
2641
|
+
let at = start('renderStart');
|
|
2642
|
+
for (const [key, name, phaseColor] of RENDER_TIMELINE) {
|
|
2643
|
+
const time = mean(key);
|
|
2644
|
+
bars.push({ lane: 'detail', start: at, duration: time, color: phaseColor, name, positioned: false, phase: true });
|
|
2645
|
+
at += time;
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
|
|
2649
|
+
this._gpuBars(from, to, bars);
|
|
2650
|
+
|
|
2651
|
+
// the events of a single frame, and what loaded during it; an average frame has none of
|
|
2652
|
+
// its own
|
|
2653
|
+
let bakedInRender = false;
|
|
2654
|
+
if (!range) {
|
|
2655
|
+
const tick = d.tick[capture.slot(from)];
|
|
2656
|
+
for (const event of capture.events(from)) {
|
|
2657
|
+
const kind = EVENT_KINDS.find(k => k.key === event.kind) ?? EVENT_KINDS.find(k => k.key === 'marker');
|
|
2658
|
+
const at = Math.max(0, event.start - tick);
|
|
2659
|
+
const duration = event.duration - (at - (event.start - tick));
|
|
2660
|
+
// a bake is work of the CPU: on its line where it ran, as in the app's update
|
|
2661
|
+
// event, or on the line below inside the part of the frame it ran in
|
|
2662
|
+
if (event.kind === 'bake') {
|
|
2663
|
+
const inside = bars.find(bar => bar.lane === 'cpu' && bar.start < at + duration && bar.start + bar.duration > at);
|
|
2664
|
+
bars.push({ lane: inside ? 'detail' : 'cpu', start: at, duration, color: kind.color, name: 'Lightmap bake', positioned: true });
|
|
2665
|
+
// a bake in the render, as the engine's first, puts its own rendering into the
|
|
2666
|
+
// times of the render's phases, which would count it twice beside it
|
|
2667
|
+
if (inside?.name === 'Render') bakedInRender = true;
|
|
2668
|
+
continue;
|
|
2669
|
+
}
|
|
2670
|
+
bars.push({ lane: 'events', start: at, duration, color: kind.color, name: event.name, positioned: true, instant: event.duration <= 0 });
|
|
2671
|
+
}
|
|
2672
|
+
this._loadBars(from, bars);
|
|
2673
|
+
}
|
|
2674
|
+
if (bakedInRender) bars = bars.filter(bar => !bar.phase);
|
|
2675
|
+
|
|
2676
|
+
const title = range ? `${capture.rangeLabel(from, to)}, average` : capture.label(from);
|
|
2677
|
+
// the start of the app has no frame interval: it is as long as it lasted
|
|
2678
|
+
const interval = !range && capture.isStartup(from) ? d.width[capture.slot(from)] : mean('interval');
|
|
2679
|
+
return { title, interval, frames, bars };
|
|
2680
|
+
}
|
|
2681
|
+
|
|
2682
|
+
/**
|
|
2683
|
+
* Adds what loaded during an entry to the bars of its timeline: the moments in it, and the
|
|
2684
|
+
* assets and the requests going on in it, each packed into the first lane free at its start,
|
|
2685
|
+
* cut to the entry.
|
|
2686
|
+
*
|
|
2687
|
+
* @param {number} seq - The entry.
|
|
2688
|
+
* @param {PerfTimeBar[]} bars - Receives the bars.
|
|
2689
|
+
* @private
|
|
2690
|
+
*/
|
|
2691
|
+
_loadBars(seq, bars) {
|
|
2692
|
+
const capture = this._capture;
|
|
2693
|
+
const d = capture.data;
|
|
2694
|
+
const slot = capture.slot(seq);
|
|
2695
|
+
const tick = d.tick[slot];
|
|
2696
|
+
const end = tick + d.width[slot];
|
|
2697
|
+
const now = performance.now();
|
|
2698
|
+
const kinds = { milestone: [], asset: [], request: [] };
|
|
2699
|
+
const take = (load, loadEnd) => {
|
|
2700
|
+
const start = Number.isFinite(load.start) ? load.start : loadEnd;
|
|
2701
|
+
if (loadEnd >= tick && start <= end) kinds[load.kind].push({ load, start, end: loadEnd });
|
|
2702
|
+
};
|
|
2703
|
+
for (let s = capture.first; s < capture.end; s++) {
|
|
2704
|
+
for (const load of capture.loads(s)) take(load, load.end);
|
|
2705
|
+
}
|
|
2706
|
+
for (const load of capture.pendingLoads()) take(load, now);
|
|
2707
|
+
|
|
2708
|
+
for (const { load, start } of kinds.milestone) {
|
|
2709
|
+
bars.push({ lane: 'milestones', start: start - tick, duration: 0, color: loadColor(load), name: load.name, label: loadText(load), positioned: true, instant: true });
|
|
2710
|
+
}
|
|
2711
|
+
for (const [kind, prefix, count] of [['asset', 'asset', TIMELINE_ASSET_LANES], ['request', 'request', TIMELINE_REQUEST_LANES]]) {
|
|
2712
|
+
const list = kinds[kind].sort((a, b) => a.start - b.start);
|
|
2713
|
+
const ends = new Array(count).fill(-Infinity);
|
|
2714
|
+
// the bar before in each lane, whose label stops where the next bar begins, as a lane
|
|
2715
|
+
// full up takes bars over the ones still in it
|
|
2716
|
+
const last = new Array(count).fill(null);
|
|
2717
|
+
for (const { load, start, end: loadEnd } of list) {
|
|
2718
|
+
let lane = ends.findIndex(e => e <= start);
|
|
2719
|
+
if (lane < 0) lane = ends.indexOf(Math.min(...ends));
|
|
2720
|
+
ends[lane] = loadEnd;
|
|
2721
|
+
const from = Math.max(0, start - tick);
|
|
2722
|
+
const to = Math.min(end, loadEnd) - tick;
|
|
2723
|
+
if (last[lane]) last[lane].labelEnd = from;
|
|
2724
|
+
bars.push(last[lane] = {
|
|
2725
|
+
lane: `${prefix}${lane}`,
|
|
2726
|
+
start: from,
|
|
2727
|
+
duration: Math.max(0, to - from),
|
|
2728
|
+
color: loadColor(load),
|
|
2729
|
+
name: load.name,
|
|
2730
|
+
label: loadText(load),
|
|
2731
|
+
positioned: true,
|
|
2732
|
+
instant: !(to > from),
|
|
2733
|
+
...(load.id !== undefined ? { asset: load.id } : {})
|
|
2734
|
+
});
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
}
|
|
2738
|
+
|
|
2739
|
+
/**
|
|
2740
|
+
* Adds the GPU passes of the frames to the bars of the timeline. The GPU does not say when each
|
|
2741
|
+
* pass ran, only for how long, so they are back to back, each apart, its mean over the frames
|
|
2742
|
+
* that timed the GPU, in the order the passes first ran. The passes an effect runs go on a line
|
|
2743
|
+
* of their own under a bar of the effect, as in a flame chart; a pass of its own, or every pass
|
|
2744
|
+
* where the frame graph did not match, goes on the GPU line.
|
|
2745
|
+
*
|
|
2746
|
+
* @param {number} from - The first frame.
|
|
2747
|
+
* @param {number} to - The last frame, inclusive.
|
|
2748
|
+
* @param {PerfTimeBar[]} bars - Receives the bars.
|
|
2749
|
+
* @private
|
|
2750
|
+
*/
|
|
2751
|
+
_gpuBars(from, to, bars) {
|
|
2752
|
+
const capture = this._capture;
|
|
2753
|
+
// each pass by its name and which of the passes with that name it is in its frame
|
|
2754
|
+
/** @type {Map<string, { name: string, time: number, owner: string|null, occurrence: number, ownerOccurrence: number }[]>} */
|
|
2755
|
+
const byName = new Map();
|
|
2756
|
+
const order = [];
|
|
2757
|
+
let timed = 0;
|
|
2758
|
+
for (let seq = from; seq <= to; seq++) {
|
|
2759
|
+
const list = capture.passesInOrder(seq);
|
|
2760
|
+
if (!list.length) continue;
|
|
2761
|
+
timed++;
|
|
2762
|
+
const seen = new Map();
|
|
2763
|
+
for (const pass of list) {
|
|
2764
|
+
const k = seen.get(pass.name) ?? 0;
|
|
2765
|
+
seen.set(pass.name, k + 1);
|
|
2766
|
+
let same = byName.get(pass.name);
|
|
2767
|
+
if (!same) byName.set(pass.name, same = []);
|
|
2768
|
+
let entry = same[k];
|
|
2769
|
+
if (!entry) {
|
|
2770
|
+
entry = { name: pass.name, time: 0, owner: null, occurrence: -1, ownerOccurrence: -1 };
|
|
2771
|
+
same[k] = entry;
|
|
2772
|
+
order.push(entry);
|
|
2773
|
+
}
|
|
2774
|
+
entry.time += pass.time;
|
|
2775
|
+
// as the newest frame has it
|
|
2776
|
+
entry.owner = pass.owner;
|
|
2777
|
+
entry.occurrence = pass.occurrence;
|
|
2778
|
+
entry.ownerOccurrence = pass.ownerOccurrence;
|
|
2779
|
+
}
|
|
2780
|
+
}
|
|
2781
|
+
if (!timed) return;
|
|
2782
|
+
|
|
2783
|
+
// a color for each name, of pass or effect, in the order they come
|
|
2784
|
+
const colors = new Map();
|
|
2785
|
+
const colorOf = (name) => {
|
|
2786
|
+
let color = colors.get(name);
|
|
2787
|
+
if (!color) colors.set(name, color = PASS_COLORS[colors.size % PASS_COLORS.length]);
|
|
2788
|
+
return color;
|
|
2789
|
+
};
|
|
2790
|
+
const link = (name, occurrence) => (occurrence >= 0 ? { name, occurrence } : undefined);
|
|
2791
|
+
const label = entry => (byName.get(entry.name).length > 1 ? `${entry.name} ${byName.get(entry.name).indexOf(entry) + 1}/${byName.get(entry.name).length}` : entry.name);
|
|
2792
|
+
let t = 0;
|
|
2793
|
+
for (let i = 0; i < order.length;) {
|
|
2794
|
+
const entry = order[i];
|
|
2795
|
+
if (!entry.owner) {
|
|
2796
|
+
const time = entry.time / timed;
|
|
2797
|
+
bars.push({ lane: 'gpu', start: t, duration: time, color: colorOf(entry.name), name: entry.name, label: label(entry), positioned: false, pass: link(entry.name, entry.occurrence) });
|
|
2798
|
+
t += time;
|
|
2799
|
+
i++;
|
|
2800
|
+
continue;
|
|
2801
|
+
}
|
|
2802
|
+
// the passes the effect runs in a row, under a bar of it
|
|
2803
|
+
const { owner, ownerOccurrence } = entry;
|
|
2804
|
+
const start = t;
|
|
2805
|
+
const children = [];
|
|
2806
|
+
for (; i < order.length && order[i].owner === owner && order[i].ownerOccurrence === ownerOccurrence; i++) {
|
|
2807
|
+
const pass = order[i];
|
|
2808
|
+
const time = pass.time / timed;
|
|
2809
|
+
const color = colorOf(pass.name);
|
|
2810
|
+
bars.push({ lane: 'gpuPasses', start: t, duration: time, color, name: pass.name, label: label(pass), positioned: false, pass: link(pass.name, pass.occurrence) });
|
|
2811
|
+
children.push({ name: label(pass), duration: time, color });
|
|
2812
|
+
t += time;
|
|
2813
|
+
}
|
|
2814
|
+
bars.push({ lane: 'gpu', start, duration: t - start, color: colorOf(owner), name: effectName(owner), positioned: false, pass: link(owner, ownerOccurrence), children });
|
|
2815
|
+
}
|
|
2816
|
+
|
|
2817
|
+
// passes of one name in a row share a label: the first of them says how many there are
|
|
2818
|
+
for (let i = 0; i < bars.length; i++) {
|
|
2819
|
+
const bar = bars[i];
|
|
2820
|
+
if (bar.lane !== 'gpu' && bar.lane !== 'gpuPasses') continue;
|
|
2821
|
+
if (bar.run === 0) continue;
|
|
2822
|
+
let n = 1;
|
|
2823
|
+
let duration = bar.duration;
|
|
2824
|
+
for (let j = i + 1; j < bars.length && bars[j].lane === bar.lane && bars[j].name === bar.name && !bars[j].children && !bar.children; j++) {
|
|
2825
|
+
bars[j].run = 0;
|
|
2826
|
+
duration += bars[j].duration;
|
|
2827
|
+
n++;
|
|
2828
|
+
}
|
|
2829
|
+
if (n > 1) {
|
|
2830
|
+
bar.run = n;
|
|
2831
|
+
bar.runDuration = duration;
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
/**
|
|
2837
|
+
* @param {number} from - The first frame.
|
|
2838
|
+
* @param {number} to - The last frame, inclusive.
|
|
2839
|
+
* @param {number} column - Which method of the script types: 0 their update, 1 their
|
|
2840
|
+
* post-update.
|
|
2841
|
+
* @returns {PerfScriptType[]} Each script type with that method, with its time and its calls,
|
|
2842
|
+
* the mean over the frames that timed scripts, and where its first call came, the mean over
|
|
2843
|
+
* the frames it ran in.
|
|
2844
|
+
* @private
|
|
2845
|
+
*/
|
|
2846
|
+
_scriptTypes(from, to, column) {
|
|
2847
|
+
const capture = this._capture;
|
|
2848
|
+
const names = capture.scriptNames;
|
|
2849
|
+
// summed by type in arrays, as an app can have hundreds of them over many frames
|
|
2850
|
+
const times = new Float64Array(names.length);
|
|
2851
|
+
const calls = new Float64Array(names.length);
|
|
2852
|
+
const orders = new Float64Array(names.length);
|
|
2853
|
+
const ran = new Float64Array(names.length);
|
|
2854
|
+
let timed = 0;
|
|
2855
|
+
for (let seq = from; seq <= to; seq++) {
|
|
2856
|
+
const figures = capture.scriptFigures(seq);
|
|
2857
|
+
if (!figures) continue;
|
|
2858
|
+
timed++;
|
|
2859
|
+
for (let base = 0, id = 0; base < figures.length; base += SCRIPT_STRIDE, id++) {
|
|
2860
|
+
const n = figures[base + SCRIPT_CALLS + column];
|
|
2861
|
+
if (!n) continue;
|
|
2862
|
+
times[id] += figures[base + column];
|
|
2863
|
+
calls[id] += n;
|
|
2864
|
+
orders[id] += figures[base + SCRIPT_ORDER + column];
|
|
2865
|
+
ran[id]++;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
const colors = this._scriptLegend().colors;
|
|
2869
|
+
/** @type {PerfScriptType[]} */
|
|
2870
|
+
const types = [];
|
|
2871
|
+
for (let id = 0; id < names.length; id++) {
|
|
2872
|
+
if (ran[id]) types.push({ id, name: names[id], duration: times[id] / timed, calls: calls[id] / timed, order: orders[id] / ran[id], color: colors.get(id) ?? OTHER_SCRIPTS_COLOR });
|
|
2873
|
+
}
|
|
2874
|
+
return types;
|
|
2875
|
+
}
|
|
2876
|
+
|
|
2877
|
+
/**
|
|
2878
|
+
* What the timeline shows, to copy: its title, how many frames it covers, the frame interval
|
|
2879
|
+
* and its bars by row. A GPU pass has no start, as the GPU does not say when it ran.
|
|
2880
|
+
*
|
|
2881
|
+
* @returns {object|null} The timeline, or null when there is nothing to show.
|
|
2882
|
+
*/
|
|
2883
|
+
timelineJson() {
|
|
2884
|
+
const model = this._timelineModel();
|
|
2885
|
+
if (!model) return null;
|
|
2886
|
+
const rows = { cpu: 'CPU', detail: 'Update and render', types: 'Script types', gpu: 'GPU', gpuPasses: 'Passes of the effects', events: 'Events', milestones: 'Milestones' };
|
|
2887
|
+
const bars = model.bars.filter(bar => bar.duration > 0 || bar.instant).map(bar => ({
|
|
2888
|
+
row: rows[bar.lane] ?? (bar.lane.startsWith('asset') ? 'Assets' : bar.lane.startsWith('request') ? 'Requests' : bar.lane),
|
|
2889
|
+
name: bar.name,
|
|
2890
|
+
start: bar.positioned && Number.isFinite(bar.start) ? bar.start : null,
|
|
2891
|
+
duration: bar.instant ? 0 : bar.duration,
|
|
2892
|
+
...(bar.calls !== undefined ? { calls: bar.calls } : {}),
|
|
2893
|
+
...(bar.children ? { parts: bar.children.map(child => ({ name: child.name, duration: child.duration, ...(child.calls !== undefined ? { calls: child.calls } : {}) })) } : {})
|
|
2894
|
+
}));
|
|
2895
|
+
return { title: model.title, frames: model.frames, interval: model.interval, bars };
|
|
2896
|
+
}
|
|
2897
|
+
|
|
2898
|
+
/**
|
|
2899
|
+
* What a right click copies, by where it is: the name of a chart, the title of a section or a
|
|
2900
|
+
* line of a list copies that series; the plot copies the frame under the pointer, or the
|
|
2901
|
+
* frames selected together when it is among them; the timeline copies what it shows.
|
|
2902
|
+
*
|
|
2903
|
+
* @param {Node} target - What was right-clicked.
|
|
2904
|
+
* @param {MouseEvent} e - The click, for where it was.
|
|
2905
|
+
* @returns {{ kind: 'series'|'frame'|'range'|'timeline', key?: string, seq?: number }|null} What
|
|
2906
|
+
* to copy, or null for nothing.
|
|
2907
|
+
*/
|
|
2908
|
+
copyAt(target, e) {
|
|
2909
|
+
const capture = this._capture;
|
|
2910
|
+
if (!capture.count) return null;
|
|
2911
|
+
if (this._timelineEl.contains(target)) return { kind: 'timeline' };
|
|
2912
|
+
if (target !== this._canvas && target !== this._overlay) return null;
|
|
2913
|
+
const { x, row } = this._locate(e);
|
|
2914
|
+
if (!row) return null;
|
|
2915
|
+
const series = row.section ?? (row.chart !== undefined ? CHARTS[row.chart].key : row.lines ?? (row.kind !== undefined ? 'events' : null));
|
|
2916
|
+
if (row.section || x < 0) return series ? { kind: 'series', key: series } : null;
|
|
2917
|
+
const seq = this._pickAt(e);
|
|
2918
|
+
if (this.range && seq >= this.range.from && seq <= this.range.to) return { kind: 'range' };
|
|
2919
|
+
return capture.has(seq) ? { kind: 'frame', seq } : null;
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
/**
|
|
2923
|
+
* The frames a copy covers: those selected together, or every frame kept.
|
|
2924
|
+
*
|
|
2925
|
+
* @type {{ from: number, to: number }}
|
|
2926
|
+
*/
|
|
2927
|
+
get copyRange() {
|
|
2928
|
+
const capture = this._capture;
|
|
2929
|
+
const range = this.range;
|
|
2930
|
+
return range ? { from: Math.max(capture.first, range.from), to: Math.min(capture.end - 1, range.to) } : { from: capture.first, to: capture.end - 1 };
|
|
2931
|
+
}
|
|
2932
|
+
|
|
2933
|
+
/**
|
|
2934
|
+
* Moves a zoomed-in timeline so it starts at a time of the frame.
|
|
2935
|
+
*
|
|
2936
|
+
* @param {number} from - The first millisecond of the frame to view.
|
|
2937
|
+
* @private
|
|
2938
|
+
*/
|
|
2939
|
+
_scrollTimeTo(from) {
|
|
2940
|
+
const view = this._timeView;
|
|
2941
|
+
if (!view) return;
|
|
2942
|
+
view.from = Math.max(0, Math.min(this._timeLayout.length - view.span, from));
|
|
2943
|
+
this._drawn.timeline = true;
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
/**
|
|
2947
|
+
* Draws the frame shown, or the average frame of the frames selected together, along its own
|
|
2948
|
+
* time: the update and the render with their parts where the engine timed them, the GPU passes
|
|
2949
|
+
* back to back, and what happened meanwhile, each on a row named beside it. Zoomed in, only
|
|
2950
|
+
* part of the frame is drawn, across the whole width.
|
|
2951
|
+
*
|
|
2952
|
+
* @private
|
|
2953
|
+
*/
|
|
2954
|
+
_drawTimeline() {
|
|
2955
|
+
const model = this._timelineModel();
|
|
2956
|
+
// a line more for the script types, while the frames shown timed them
|
|
2957
|
+
const typed = !!model?.bars.some(bar => bar.lane === 'types');
|
|
2958
|
+
// and for the passes of the GPU effects, where the frame graph says which effect runs them
|
|
2959
|
+
const owned = !!model?.bars.some(bar => bar.lane === 'gpuPasses');
|
|
2960
|
+
// and a lane each for the milestones, the assets and the requests of the entry
|
|
2961
|
+
const loadingLanes = [];
|
|
2962
|
+
if (model?.bars.some(bar => bar.lane === 'milestones')) loadingLanes.push({ key: 'milestones', name: 'Milestones', title: 'Milestones' });
|
|
2963
|
+
for (const [prefix, count, name] of [['asset', TIMELINE_ASSET_LANES, 'Assets'], ['request', TIMELINE_REQUEST_LANES, 'Requests']]) {
|
|
2964
|
+
for (let i = 0; i < count; i++) {
|
|
2965
|
+
if (model?.bars.some(bar => bar.lane === `${prefix}${i}`)) loadingLanes.push({ key: `${prefix}${i}`, name: i ? '' : name, title: name });
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
// collapsed from its title, only the title is left
|
|
2969
|
+
const collapsed = this.collapsed.has('timeline');
|
|
2970
|
+
const groups = 2 + (loadingLanes.length ? 1 : 0);
|
|
2971
|
+
const timelineHeight = collapsed ? TIMELINE_TITLE :
|
|
2972
|
+
TIMELINE_HEIGHT + groups * TIMELINE_GROUP_GAP + (typed ? SCRIPT_TYPES_LINE : 0) + (owned ? GPU_PASSES_LINE : 0) + loadingLanes.length * LOADING_LINE;
|
|
2973
|
+
if (timelineHeight !== this._timelineHeight) {
|
|
2974
|
+
this._timelineHeight = timelineHeight;
|
|
2975
|
+
this._timelineEl.style.height = `${timelineHeight}px`;
|
|
2976
|
+
}
|
|
2977
|
+
const prepared = this._prepare(this._timeline, this._timelineEl, timelineHeight);
|
|
2978
|
+
this._bars = [];
|
|
2979
|
+
this._lanes = [];
|
|
2980
|
+
this._timeScrollbar.el.style.display = collapsed ? 'none' : '';
|
|
2981
|
+
if (!prepared) return;
|
|
2982
|
+
const { ctx, width, height } = prepared;
|
|
2983
|
+
if (!model) {
|
|
2984
|
+
this._timeScrollbar.set(0, 1);
|
|
2985
|
+
this._timelineTitleEnd = 0;
|
|
2986
|
+
return;
|
|
2987
|
+
}
|
|
2988
|
+
if (collapsed) {
|
|
2989
|
+
this._drawTimelineTitle(ctx, model, false);
|
|
2990
|
+
return;
|
|
2991
|
+
}
|
|
2992
|
+
|
|
2993
|
+
const { budget } = captureNorms(this._capture);
|
|
2994
|
+
const { interval, bars: frameBars } = model;
|
|
2995
|
+
let gpuTotal = 0;
|
|
2996
|
+
for (const bar of frameBars) {
|
|
2997
|
+
if (bar.lane === 'gpu') gpuTotal += bar.duration;
|
|
2998
|
+
}
|
|
2999
|
+
const length = Math.max(interval, Number.isFinite(budget) ? budget : 0, gpuTotal);
|
|
3000
|
+
const plot = width - this._gutter;
|
|
3001
|
+
|
|
3002
|
+
// the part of the frame in view
|
|
3003
|
+
const view = this._timeView;
|
|
3004
|
+
if (view) {
|
|
3005
|
+
view.span = Math.min(view.span, length);
|
|
3006
|
+
view.from = Math.max(0, Math.min(length - view.span, view.from));
|
|
3007
|
+
}
|
|
3008
|
+
const from = view ? view.from : 0;
|
|
3009
|
+
const span = view ? view.span : length;
|
|
3010
|
+
const layout = this._timeLayout;
|
|
3011
|
+
layout.from = from;
|
|
3012
|
+
layout.span = span;
|
|
3013
|
+
layout.length = length;
|
|
3014
|
+
layout.plot = plot;
|
|
3015
|
+
this._timeScrollbar.set(from / length, span / length);
|
|
3016
|
+
const toX = t => this._gutter + ((t - from) / span) * plot;
|
|
3017
|
+
|
|
3018
|
+
// the CPU, and below it, unnamed as in a flame chart, what its update and its render are
|
|
3019
|
+
// made of, and the script types; then the GPU, and below it the passes of its effects
|
|
3020
|
+
const lanes = [
|
|
3021
|
+
{ key: 'cpu', name: 'CPU', title: 'CPU', y: 16, h: 12 },
|
|
3022
|
+
{ key: 'detail', name: '', title: 'Update and render', y: 29, h: 10 }
|
|
3023
|
+
];
|
|
3024
|
+
let y = 39;
|
|
3025
|
+
// a line across the gap between groups of lanes
|
|
3026
|
+
const separators = [];
|
|
3027
|
+
const group = () => {
|
|
3028
|
+
separators.push(y + 2 + TIMELINE_GROUP_GAP / 2);
|
|
3029
|
+
y += TIMELINE_GROUP_GAP;
|
|
3030
|
+
};
|
|
3031
|
+
if (typed) {
|
|
3032
|
+
lanes.push({ key: 'types', name: '', title: 'Script types', y: y + 3, h: 10 });
|
|
3033
|
+
y += SCRIPT_TYPES_LINE;
|
|
3034
|
+
}
|
|
3035
|
+
group();
|
|
3036
|
+
lanes.push({ key: 'gpu', name: 'GPU', title: 'GPU passes', y: y + 4, h: 10 });
|
|
3037
|
+
y += 14;
|
|
3038
|
+
if (owned) {
|
|
3039
|
+
lanes.push({ key: 'gpuPasses', name: '', title: 'Passes of the effects', y: y + 1, h: 10 });
|
|
3040
|
+
y += GPU_PASSES_LINE;
|
|
3041
|
+
}
|
|
3042
|
+
group();
|
|
3043
|
+
lanes.push({ key: 'events', name: 'Events', title: 'Events', y: y + 4, h: 8 });
|
|
3044
|
+
y += 12;
|
|
3045
|
+
if (loadingLanes.length) group();
|
|
3046
|
+
for (const loading of loadingLanes) {
|
|
3047
|
+
lanes.push({ ...loading, y: y + 3, h: 8 });
|
|
3048
|
+
y += LOADING_LINE;
|
|
3049
|
+
}
|
|
3050
|
+
const lane = Object.fromEntries(lanes.map(l => [l.key, l]));
|
|
3051
|
+
this._lanes = lanes;
|
|
3052
|
+
for (const { name, y, h } of lanes) {
|
|
3053
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.03)';
|
|
3054
|
+
ctx.fillRect(this._gutter, y, plot, h);
|
|
3055
|
+
ctx.fillStyle = '#b9c4c7';
|
|
3056
|
+
this._gutterText(ctx, name, 6, y + (h - 10) / 2);
|
|
3057
|
+
}
|
|
3058
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.1)';
|
|
3059
|
+
for (const separator of separators) ctx.fillRect(4, Math.round(separator), width - 4, 1);
|
|
3060
|
+
|
|
3061
|
+
// the bars, cut to the plot while zoomed in; an instant, or a span too short to see, is
|
|
3062
|
+
// drawn a pixel wide
|
|
3063
|
+
ctx.save();
|
|
3064
|
+
ctx.beginPath();
|
|
3065
|
+
ctx.rect(this._gutter, 0, plot, height);
|
|
3066
|
+
ctx.clip();
|
|
3067
|
+
// the labels once every bar is drawn, as the label of a run spans the bars after its first
|
|
3068
|
+
const labels = [];
|
|
3069
|
+
for (const bar of frameBars) {
|
|
3070
|
+
if (!(bar.duration > 0 || bar.instant) || !Number.isFinite(bar.start)) continue;
|
|
3071
|
+
const row = lane[bar.lane];
|
|
3072
|
+
const x0 = toX(bar.start);
|
|
3073
|
+
const x1 = Math.max(x0 + 1, toX(bar.start + Math.max(0, bar.duration)));
|
|
3074
|
+
if (x1 < this._gutter || x0 > width) continue;
|
|
3075
|
+
ctx.fillStyle = bar.color;
|
|
3076
|
+
// the passes of a run are told apart by a gap, where there is room for one
|
|
3077
|
+
const inRun = bar.run !== undefined;
|
|
3078
|
+
ctx.fillRect(x0, row.y, x1 - x0 - (inRun && x1 - x0 >= 3 ? 1 : 0), row.h);
|
|
3079
|
+
if (bar.run !== 0) {
|
|
3080
|
+
// a run is labelled once, across it, with how many passes it has
|
|
3081
|
+
const runEnd = bar.run > 1 ? Math.max(x1, toX(bar.start + bar.runDuration)) : x1;
|
|
3082
|
+
const left = Math.max(this._gutter, x0);
|
|
3083
|
+
const name = bar.run > 1 ? `${bar.name} ×${bar.run}` : bar.name;
|
|
3084
|
+
const labelEnd = bar.labelEnd !== undefined ? toX(bar.labelEnd) : Infinity;
|
|
3085
|
+
const text = PerfCharts._fit(ctx, name, bar.instant ? '' : formatMs(bar.run > 1 ? bar.runDuration : bar.duration), Math.min(width, runEnd, labelEnd) - left - 6);
|
|
3086
|
+
if (text) labels.push(text, left + 3, row.y + (row.h - 10) / 2);
|
|
3087
|
+
}
|
|
3088
|
+
this._bars.push({ x0, x1, lane: bar.lane, name: bar.name, color: bar.color, duration: bar.instant ? 0 : bar.duration, start: bar.positioned ? bar.start : NaN, calls: bar.calls, label: bar.label, script: bar.script, pass: bar.pass, asset: bar.asset, children: bar.children });
|
|
3089
|
+
}
|
|
3090
|
+
ctx.fillStyle = '#0b0f10';
|
|
3091
|
+
for (let i = 0; i < labels.length; i += 3) ctx.fillText(labels[i], labels[i + 1], labels[i + 2]);
|
|
3092
|
+
|
|
3093
|
+
// the end of the frame, and the display interval
|
|
3094
|
+
const lineTop = lane.cpu.y - 2;
|
|
3095
|
+
const lastLane = lanes[lanes.length - 1];
|
|
3096
|
+
const lineHeight = lastLane.y + lastLane.h + 2 - lineTop;
|
|
3097
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';
|
|
3098
|
+
ctx.fillRect(Math.min(width - 1, toX(interval)), lineTop, 1, lineHeight);
|
|
3099
|
+
if (Number.isFinite(budget)) {
|
|
3100
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.25)';
|
|
3101
|
+
ctx.fillRect(toX(budget), lineTop, 1, lineHeight);
|
|
3102
|
+
}
|
|
3103
|
+
ctx.restore();
|
|
3104
|
+
|
|
3105
|
+
// a scale of milliseconds along the bottom, over the part in view
|
|
3106
|
+
const step = niceCeil(span / 8);
|
|
3107
|
+
ctx.fillStyle = '#7f8f93';
|
|
3108
|
+
let labelled = false;
|
|
3109
|
+
for (let i = Math.ceil(from / step); i * step <= from + span; i++) {
|
|
3110
|
+
// rounded, as adding up fractional steps drifts off the decimals
|
|
3111
|
+
const ms = +(i * step).toFixed(4);
|
|
3112
|
+
const x = toX(ms);
|
|
3113
|
+
ctx.fillRect(x, height - 16, 1, 3);
|
|
3114
|
+
if (x < width - 30) {
|
|
3115
|
+
ctx.fillText(`${ms}${labelled ? '' : ' ms'}`, x + 1, height - 12);
|
|
3116
|
+
labelled = true;
|
|
3117
|
+
}
|
|
3118
|
+
}
|
|
3119
|
+
|
|
3120
|
+
this._drawTimelineTitle(ctx, model, true);
|
|
3121
|
+
this._drawDivider(ctx, height);
|
|
3122
|
+
}
|
|
3123
|
+
|
|
3124
|
+
/**
|
|
3125
|
+
* Draws the title of the timeline, which a click collapses or expands, with the length of the
|
|
3126
|
+
* frame.
|
|
3127
|
+
*
|
|
3128
|
+
* @param {CanvasRenderingContext2D} ctx - The context.
|
|
3129
|
+
* @param {{ title: string, interval: number }} model - What the timeline shows.
|
|
3130
|
+
* @param {boolean} open - Whether the timeline is expanded.
|
|
3131
|
+
* @private
|
|
3132
|
+
*/
|
|
3133
|
+
_drawTimelineTitle(ctx, model, open) {
|
|
3134
|
+
const title = `${open ? '▾' : '▸'} ${model.title}`;
|
|
3135
|
+
ctx.font = BOLD;
|
|
3136
|
+
ctx.fillStyle = '#dbe2e4';
|
|
3137
|
+
ctx.fillText(title, 4, 2);
|
|
3138
|
+
const titleWidth = ctx.measureText(title).width;
|
|
3139
|
+
ctx.font = FONT;
|
|
3140
|
+
this._timelineTitleEnd = 4 + titleWidth;
|
|
3141
|
+
PerfCharts._label(ctx, formatMs(model.interval), Math.max(this._gutter, 12 + titleWidth), 2, 'left');
|
|
3142
|
+
}
|
|
3143
|
+
|
|
3144
|
+
/**
|
|
3145
|
+
* Zooms the timeline around the pointer, or pans it with Shift or a sideways scroll.
|
|
3146
|
+
*
|
|
3147
|
+
* @param {WheelEvent} e - The event.
|
|
3148
|
+
* @private
|
|
3149
|
+
*/
|
|
3150
|
+
_onTimelineWheel = (e) => {
|
|
3151
|
+
const layout = this._timeLayout;
|
|
3152
|
+
const x = e.clientX - this._timeline.getBoundingClientRect().left - this._gutter;
|
|
3153
|
+
if (x < 0) return;
|
|
3154
|
+
e.preventDefault();
|
|
3155
|
+
const { from, span, length, plot } = layout;
|
|
3156
|
+
const pan = e.shiftKey ? e.deltaY : e.deltaX;
|
|
3157
|
+
if (Math.abs(pan) > Math.abs(e.shiftKey ? 0 : e.deltaY)) {
|
|
3158
|
+
if (this._timeView) this._scrollTimeTo(from + (pan / plot) * span);
|
|
3159
|
+
} else {
|
|
3160
|
+
const anchor = from + (x / plot) * span;
|
|
3161
|
+
const next = Math.max(MIN_TIME_SPAN, span * Math.exp(e.deltaY * 0.002));
|
|
3162
|
+
this._timeView = next >= length ? null : { from: anchor - (x / plot) * next, span: next };
|
|
3163
|
+
}
|
|
3164
|
+
this._renderHover(this._timelineHoverEl, this._timelineEl, [], null, true);
|
|
3165
|
+
this._drawn.timeline = true;
|
|
3166
|
+
};
|
|
3167
|
+
|
|
3168
|
+
/** @private */
|
|
3169
|
+
_onTimelineDoubleClick = () => {
|
|
3170
|
+
this._timeView = null;
|
|
3171
|
+
this._drawn.timeline = true;
|
|
3172
|
+
};
|
|
3173
|
+
|
|
3174
|
+
/**
|
|
3175
|
+
* @param {PointerEvent|MouseEvent} e - A pointer event on the charts.
|
|
3176
|
+
* @returns {{ x: number, y: number, row: PerfRow|null, column: number }} Where it is: across
|
|
3177
|
+
* the charts right of the names, down the canvas, the chart or row of events, and the column.
|
|
3178
|
+
* @private
|
|
3179
|
+
*/
|
|
3180
|
+
_locate(e) {
|
|
3181
|
+
const rect = this._canvas.getBoundingClientRect();
|
|
3182
|
+
const x = e.clientX - rect.left - this._gutter;
|
|
3183
|
+
const y = e.clientY - rect.top;
|
|
3184
|
+
const row = this._rows.find(r => y >= r.top && y < r.top + r.height) ?? null;
|
|
3185
|
+
return { x, y, row, column: Math.floor(x) };
|
|
3186
|
+
}
|
|
3187
|
+
|
|
3188
|
+
/**
|
|
3189
|
+
* @param {PointerEvent|MouseEvent} e - A pointer event on the charts.
|
|
3190
|
+
* @returns {number} The frame under it: the one its column shows in the chart it is over, or
|
|
3191
|
+
* the first of its frames with an event of the kind of the row it is over.
|
|
3192
|
+
* @private
|
|
3193
|
+
*/
|
|
3194
|
+
_pickAt(e) {
|
|
3195
|
+
const { x, row, column } = this._locate(e);
|
|
3196
|
+
if (row?.chart !== undefined) {
|
|
3197
|
+
const picked = this._columns[row.chart]?.[column] ?? -1;
|
|
3198
|
+
if (picked >= 0) return picked;
|
|
3199
|
+
} else if (row?.lines) {
|
|
3200
|
+
const picked = this._lineCharts[row.lines]?.pick[column] ?? -1;
|
|
3201
|
+
if (picked >= 0) return picked;
|
|
3202
|
+
} else if (row?.kind !== undefined) {
|
|
3203
|
+
const seq = this._eventFrames(column, row.kind)[0];
|
|
3204
|
+
if (seq !== undefined) return seq;
|
|
3205
|
+
}
|
|
3206
|
+
return this._seqAt(x);
|
|
3207
|
+
}
|
|
3208
|
+
|
|
3209
|
+
/**
|
|
3210
|
+
* @param {number} column - A column.
|
|
3211
|
+
* @param {number} kind - The index of a kind of event.
|
|
3212
|
+
* @returns {number[]} The frames of the column with an event of that kind.
|
|
3213
|
+
* @private
|
|
3214
|
+
*/
|
|
3215
|
+
_eventFrames(column, kind) {
|
|
3216
|
+
const { lo, hi } = this._spans;
|
|
3217
|
+
const frames = [];
|
|
3218
|
+
if (column < 0 || column >= lo.length) return frames;
|
|
3219
|
+
const bit = 1 << kind;
|
|
3220
|
+
for (let seq = lo[column]; seq < hi[column]; seq++) {
|
|
3221
|
+
if (eventMask(this._capture, seq) & bit) frames.push(seq);
|
|
3222
|
+
}
|
|
3223
|
+
return frames;
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3226
|
+
/**
|
|
3227
|
+
* @param {PointerEvent} e - A pointer event on the charts.
|
|
3228
|
+
* @returns {PerfHoverLine[]} What is under it: the figures of the frame a chart shows there,
|
|
3229
|
+
* each part with the color it is drawn in, the events of a row, or what a name beside the
|
|
3230
|
+
* charts stands for. Empty for nothing.
|
|
3231
|
+
* @private
|
|
3232
|
+
*/
|
|
3233
|
+
_hoverLines(e) {
|
|
3234
|
+
const capture = this._capture;
|
|
3235
|
+
const { x, row, column } = this._locate(e);
|
|
3236
|
+
if (!row) return [];
|
|
3237
|
+
if (row.section) {
|
|
3238
|
+
const toggle = { text: this.collapsed.has(row.section) ? 'Click to expand' : 'Click to collapse' };
|
|
3239
|
+
return row.section === 'counters' ? [{ text: COUNTERS_TIP, wrap: true }, toggle] : [toggle];
|
|
3240
|
+
}
|
|
3241
|
+
const chart = row.chart !== undefined ? CHARTS[row.chart] : null;
|
|
3242
|
+
const kind = row.kind !== undefined ? EVENT_KINDS[row.kind] : null;
|
|
3243
|
+
if (row.lines) return this._lineChartLines(row.lines, row.index, x, column, row.control);
|
|
3244
|
+
if (row.loading) return this._loadingLines(row.loading, x);
|
|
3245
|
+
if (x < 0) {
|
|
3246
|
+
const tip = chart?.tip ?? kind?.tip;
|
|
3247
|
+
const lines = tip ? [{ text: tip, wrap: true }] : [];
|
|
3248
|
+
if (chart) lines.push({ text: 'Click the name to collapse the chart' });
|
|
3249
|
+
if (kind && !eventUnsupported(capture, kind.key)) {
|
|
3250
|
+
lines.push({ text: `Click to ${this.hidden.has(`events:${kind.key}`) ? 'show' : 'hide'} its marks` });
|
|
3251
|
+
}
|
|
3252
|
+
return lines;
|
|
3253
|
+
}
|
|
3254
|
+
|
|
3255
|
+
if (kind) {
|
|
3256
|
+
const unsupported = eventUnsupported(capture, kind.key);
|
|
3257
|
+
if (unsupported) return [{ text: unsupported, wrap: true }];
|
|
3258
|
+
if (this.hidden.has(`events:${kind.key}`)) return [];
|
|
3259
|
+
const frames = this._eventFrames(column, row.kind);
|
|
3260
|
+
/** @type {PerfHoverLine[]} */
|
|
3261
|
+
const lines = [];
|
|
3262
|
+
for (const seq of frames.slice(0, HOVER_FRAMES)) {
|
|
3263
|
+
lines.push({ text: capture.label(seq) });
|
|
3264
|
+
for (const text of eventLines(capture, seq, kind.key)) lines.push({ text, color: kind.color, indent: true });
|
|
3265
|
+
}
|
|
3266
|
+
if (frames.length > HOVER_FRAMES) lines.push({ text: `… and ${frames.length - HOVER_FRAMES} more frames` });
|
|
3267
|
+
return lines;
|
|
3268
|
+
}
|
|
3269
|
+
|
|
3270
|
+
const seq = this._columns[row.chart]?.[column] ?? -1;
|
|
3271
|
+
if (!capture.has(seq)) return [];
|
|
3272
|
+
const { lo, hi } = this._spans;
|
|
3273
|
+
const many = hi[column] - lo[column];
|
|
3274
|
+
const d = capture.data;
|
|
3275
|
+
const s = capture.slot(seq);
|
|
3276
|
+
const shape = this._shape(chart);
|
|
3277
|
+
const total = d[shape.total][s];
|
|
3278
|
+
const fps = chart.key === 'frame' && total > 0 ? ` · ${(1000 / total).toFixed(1)} fps` : '';
|
|
3279
|
+
const without = shape.reduced ? `, without the inspector's ${chart.format(d.inspector[s])}` : '';
|
|
3280
|
+
/** @type {PerfHoverLine[]} */
|
|
3281
|
+
const lines = [
|
|
3282
|
+
{ text: `${capture.label(seq)}${many > 1 ? `, the largest of ${many} frames here` : ''}` },
|
|
3283
|
+
// a chart of one part has its color on the total, the others on each part
|
|
3284
|
+
{ text: `${chart.title} ${chart.format(total)}${fps}${without}`, color: shape.parts.length === 1 ? shape.parts[0].color : undefined }
|
|
3285
|
+
];
|
|
3286
|
+
if (shape.parts.length > 1) {
|
|
3287
|
+
// largest first, the chips telling them apart in the stack
|
|
3288
|
+
const parts = shape.parts.map(part => ({ part, v: d[part.channel][s] })).filter(({ v }) => v > 0);
|
|
3289
|
+
parts.sort((a, b) => b.v - a.v);
|
|
3290
|
+
for (const { part, v } of parts) lines.push({ text: `${part.name} ${chart.format(v)}`, color: part.color, indent: true });
|
|
3291
|
+
}
|
|
3292
|
+
for (const line of shape.lines) {
|
|
3293
|
+
const v = d[line.channel][s];
|
|
3294
|
+
if (v > 0) lines.push({ text: `${line.name} ${chart.format(v)}, among those above`, color: line.color, indent: true });
|
|
3295
|
+
}
|
|
3296
|
+
return lines;
|
|
3297
|
+
}
|
|
3298
|
+
|
|
3299
|
+
/**
|
|
3300
|
+
* Shows one line of a line chart alone, hiding the others, or shows them all again when it
|
|
3301
|
+
* already is.
|
|
3302
|
+
*
|
|
3303
|
+
* @param {string} section - The section of the chart.
|
|
3304
|
+
* @param {number} index - The line.
|
|
3305
|
+
* @private
|
|
3306
|
+
*/
|
|
3307
|
+
_solo(section, index) {
|
|
3308
|
+
const keys = this._lineCharts[section].lines.filter(line => !line.unsupported).map(line => `${section}:${line.key}`);
|
|
3309
|
+
const key = this._lineToggle(section, index);
|
|
3310
|
+
const alone = !this.hidden.has(key) && keys.every(other => other === key || this.hidden.has(other));
|
|
3311
|
+
for (const other of keys) {
|
|
3312
|
+
if (alone || other === key) this.hidden.delete(other);
|
|
3313
|
+
else this.hidden.add(other);
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3316
|
+
|
|
3317
|
+
/**
|
|
3318
|
+
* @param {string} section - The section of a line chart.
|
|
3319
|
+
* @param {number} index - One of its lines.
|
|
3320
|
+
* @returns {string|null} The key the line is hidden under, or null for one that cannot show.
|
|
3321
|
+
* @private
|
|
3322
|
+
*/
|
|
3323
|
+
_lineToggle(section, index) {
|
|
3324
|
+
const line = this._lineCharts[section]?.lines[index];
|
|
3325
|
+
return line && !line.unsupported ? `${section}:${line.key}` : null;
|
|
3326
|
+
}
|
|
3327
|
+
|
|
3328
|
+
/**
|
|
3329
|
+
* @param {string} section - The section of the line chart under the pointer.
|
|
3330
|
+
* @param {number} index - The line the pointer is beside.
|
|
3331
|
+
* @param {number} x - Where the pointer is across the plot, negative over the list.
|
|
3332
|
+
* @param {number} column - The column under it.
|
|
3333
|
+
* @param {'more'|'fewer'} [control] - The row of the Scripts list showing more types or fewer
|
|
3334
|
+
* the pointer is beside.
|
|
3335
|
+
* @returns {PerfHoverLine[]} Over the list, what clicking does; over the chart, every line of
|
|
3336
|
+
* the frame there, largest first, with its change since the frame before, and for the other
|
|
3337
|
+
* scripts the types among them.
|
|
3338
|
+
* @private
|
|
3339
|
+
*/
|
|
3340
|
+
_lineChartLines(section, index, x, column, control) {
|
|
3341
|
+
const capture = this._capture;
|
|
3342
|
+
const drawn = this._lineCharts[section];
|
|
3343
|
+
if (!drawn) return [];
|
|
3344
|
+
const line = drawn.lines[index];
|
|
3345
|
+
if (x < 0) {
|
|
3346
|
+
if (control === 'more') {
|
|
3347
|
+
const others = drawn.legend?.others ?? 0;
|
|
3348
|
+
return [{ text: `Give the next ${Math.min(SCRIPT_ROWS, others)} of the ${formatCount(others)} other script types a band of their own`, wrap: true }];
|
|
3349
|
+
}
|
|
3350
|
+
if (control === 'fewer') return [{ text: `Show only the ${SCRIPT_ROWS} longest script types, and those pinned`, wrap: true }];
|
|
3351
|
+
if (!line) return [];
|
|
3352
|
+
if (line.unsupported) return [{ text: line.unsupported, wrap: true }];
|
|
3353
|
+
// the name of a script is its own, as written
|
|
3354
|
+
const name = drawn.stacked && line.key !== '#other' ? line.name : line.name.toLowerCase();
|
|
3355
|
+
const hint = [`Click to ${this.hidden.has(`${section}:${line.key}`) ? 'show' : 'hide'} ${name} in the chart, Alt+click to show it alone`];
|
|
3356
|
+
if (drawn.stacked && line.id >= 0) hint.push(line.pinned ? 'Pinned, so it keeps a band of its own. Shift+click to unpin it' : 'Shift+click to pin it, so it keeps a band of its own');
|
|
3357
|
+
return hint.map(text => ({ text, wrap: true }));
|
|
3358
|
+
}
|
|
3359
|
+
const seq = drawn.pick[column] ?? -1;
|
|
3360
|
+
if (!capture.has(seq)) return [];
|
|
3361
|
+
const d = capture.data;
|
|
3362
|
+
const s = capture.slot(seq);
|
|
3363
|
+
if (drawn.stacked) {
|
|
3364
|
+
/** @type {PerfHoverLine[]} */
|
|
3365
|
+
const lines = [{ text: capture.label(seq) }];
|
|
3366
|
+
if (!(d.scriptsTotal[s] >= 0)) return [...lines, { text: 'Scripts were not timed in this frame', indent: true }];
|
|
3367
|
+
lines.push({ text: `Scripts ${formatMs(d.scriptsTotal[s])} · ${callCount(d.scriptCalls[s])}` });
|
|
3368
|
+
// the calls of each type, the other scripts' being what the listed ones leave
|
|
3369
|
+
const figures = capture.scriptFigures(seq);
|
|
3370
|
+
let listed = 0;
|
|
3371
|
+
const callsOf = (l) => {
|
|
3372
|
+
if (l.id < 0) return d.scriptCalls[s] - listed;
|
|
3373
|
+
const base = l.id * SCRIPT_STRIDE;
|
|
3374
|
+
const n = figures && base < figures.length ? figures[base + SCRIPT_CALLS] + figures[base + SCRIPT_CALLS + 1] + figures[base + SCRIPT_CALLS + 2] : 0;
|
|
3375
|
+
listed += n;
|
|
3376
|
+
return n;
|
|
3377
|
+
};
|
|
3378
|
+
const entries = drawn.lines.map((l, i) => ({ l, v: drawn.values[i][s], n: callsOf(l) })).filter(({ v }) => v > 0);
|
|
3379
|
+
// longest first, the other scripts last with what they are made of
|
|
3380
|
+
entries.sort((a, b) => Number(a.l.id === -1) - Number(b.l.id === -1) || b.v - a.v);
|
|
3381
|
+
for (const { l, v, n } of entries) {
|
|
3382
|
+
const hidden = this.hidden.has(`${section}:${l.key}`) ? ', hidden' : '';
|
|
3383
|
+
lines.push({ text: `${l.name} ${formatMs(v)} · ${callCount(n)}${hidden}`, color: l.color, indent: true });
|
|
3384
|
+
if (l.id === -1 && drawn.legend) lines.push(...this._otherScriptLines(seq, drawn.legend));
|
|
3385
|
+
}
|
|
3386
|
+
return lines;
|
|
3387
|
+
}
|
|
3388
|
+
const p = previousSlot(capture, seq);
|
|
3389
|
+
const ringOf = l => l.values ?? d[l.key];
|
|
3390
|
+
const entries = drawn.lines.map(l => ({ l, v: ringOf(l)[s] })).filter(({ v }) => Number.isFinite(v));
|
|
3391
|
+
entries.sort((a, b) => b.v - a.v);
|
|
3392
|
+
/** @type {PerfHoverLine[]} */
|
|
3393
|
+
const lines = [{ text: capture.label(seq) }];
|
|
3394
|
+
for (const { l, v } of entries) {
|
|
3395
|
+
const delta = p >= 0 ? v - ringOf(l)[p] : 0;
|
|
3396
|
+
const change = Number.isFinite(delta) && delta !== 0 ? ` (${delta > 0 ? '+' : '−'}${drawn.format(Math.abs(delta))})` : '';
|
|
3397
|
+
const hidden = this.hidden.has(`${section}:${l.key}`) ? ', hidden' : '';
|
|
3398
|
+
lines.push({ text: `${l.name} ${drawn.format(v)}${change}${hidden}`, color: l.color, indent: true });
|
|
3399
|
+
}
|
|
3400
|
+
return lines;
|
|
3401
|
+
}
|
|
3402
|
+
|
|
3403
|
+
/**
|
|
3404
|
+
* @param {number} seq - A kept frame.
|
|
3405
|
+
* @param {PerfScriptLegend} legend - The script types with a band of their own.
|
|
3406
|
+
* @returns {PerfHoverLine[]} What the other scripts are made of in the frame: the longest of
|
|
3407
|
+
* their types, how many more there were, and the calls of those that took too little to time.
|
|
3408
|
+
* @private
|
|
3409
|
+
*/
|
|
3410
|
+
_otherScriptLines(seq, legend) {
|
|
3411
|
+
const capture = this._capture;
|
|
3412
|
+
const times = capture.scriptFigures(seq);
|
|
3413
|
+
if (!times) return [];
|
|
3414
|
+
const others = [];
|
|
3415
|
+
// the calls of the types that read as no time, as the clock steps over them
|
|
3416
|
+
let untimed = 0;
|
|
3417
|
+
for (let base = 0, id = 0; base < times.length; base += SCRIPT_STRIDE, id++) {
|
|
3418
|
+
if (legend.colors.has(id)) continue;
|
|
3419
|
+
const v = times[base] + times[base + 1] + times[base + 2];
|
|
3420
|
+
const n = times[base + SCRIPT_CALLS] + times[base + SCRIPT_CALLS + 1] + times[base + SCRIPT_CALLS + 2];
|
|
3421
|
+
if (v > 0) others.push({ id, v, n });
|
|
3422
|
+
else untimed += n;
|
|
3423
|
+
}
|
|
3424
|
+
others.sort((a, b) => b.v - a.v);
|
|
3425
|
+
/** @type {PerfHoverLine[]} */
|
|
3426
|
+
const lines = [{ text: 'made up of', indent: true, nested: true, muted: true }];
|
|
3427
|
+
for (const { id, v, n } of others.slice(0, HOVER_SCRIPT_TYPES)) lines.push({ text: `${capture.scriptNames[id]} ${formatMs(v)} · ${callCount(n)}`, indent: true, nested: true });
|
|
3428
|
+
if (others.length > HOVER_SCRIPT_TYPES) lines.push({ text: `and ${others.length - HOVER_SCRIPT_TYPES} more`, indent: true, nested: true, muted: true });
|
|
3429
|
+
if (untimed > 0) lines.push({ text: `and ${callCount(untimed)} too short to time`, indent: true, nested: true, muted: true });
|
|
3430
|
+
return lines;
|
|
3431
|
+
}
|
|
3432
|
+
|
|
3433
|
+
/**
|
|
3434
|
+
* Shows what is under the pointer in a box beside it, or hides the box. The lines are rebuilt
|
|
3435
|
+
* only when they change, as the pointer moves within a column.
|
|
3436
|
+
*
|
|
3437
|
+
* @param {PointerEvent|null} e - A pointer event on the charts, or null to hide the box.
|
|
3438
|
+
* @private
|
|
3439
|
+
*/
|
|
3440
|
+
_showHover(e) {
|
|
3441
|
+
this._renderHover(this._hoverEl, this._panelEl, e ? this._hoverLines(e) : [], e, false);
|
|
3442
|
+
}
|
|
3443
|
+
|
|
3444
|
+
/**
|
|
3445
|
+
* Fills a hover box with lines and places it beside the pointer, or hides it when there are
|
|
3446
|
+
* none. The lines are rebuilt only when they change.
|
|
3447
|
+
*
|
|
3448
|
+
* @param {HTMLElement} box - The hover box.
|
|
3449
|
+
* @param {HTMLElement} container - The element it is placed in.
|
|
3450
|
+
* @param {PerfHoverLine[]} lines - What it says.
|
|
3451
|
+
* @param {PointerEvent|null} e - The pointer event it follows.
|
|
3452
|
+
* @param {boolean} above - Whether it goes above the pointer rather than below. It goes above
|
|
3453
|
+
* anyway when there is no room below.
|
|
3454
|
+
* @private
|
|
3455
|
+
*/
|
|
3456
|
+
_renderHover(box, container, lines, e, above) {
|
|
3457
|
+
if (!lines.length || !e) {
|
|
3458
|
+
box.style.display = 'none';
|
|
3459
|
+
return;
|
|
3460
|
+
}
|
|
3461
|
+
const signature = lines.map(line => `${line.color ?? ''}|${line.indent ? 1 : 0}|${line.nested ? 1 : 0}|${line.muted ? 1 : 0}|${line.strong ? 1 : 0}|${line.text}`).join('\n');
|
|
3462
|
+
if (signature !== this._hoverSignatures.get(box)) {
|
|
3463
|
+
this._hoverSignatures.set(box, signature);
|
|
3464
|
+
const doc = box.ownerDocument;
|
|
3465
|
+
box.textContent = '';
|
|
3466
|
+
for (const line of lines) {
|
|
3467
|
+
const lineEl = doc.createElement('div');
|
|
3468
|
+
// a line too long for the box wraps, rather than running out of it
|
|
3469
|
+
const wrap = line.wrap || line.text.length > HOVER_LINE_CHARS;
|
|
3470
|
+
lineEl.className = `pci-perf-hover-line${line.indent ? ' pci-indent' : ''}${line.nested ? ' pci-nested' : ''}${line.muted ? ' pci-muted' : ''}${wrap ? ' pci-wrap' : ''}${line.strong ? ' pci-strong' : ''}`;
|
|
3471
|
+
if (line.color) {
|
|
3472
|
+
const swatch = doc.createElement('span');
|
|
3473
|
+
swatch.className = 'pci-perf-swatch';
|
|
3474
|
+
swatch.style.background = line.color;
|
|
3475
|
+
lineEl.appendChild(swatch);
|
|
3476
|
+
}
|
|
3477
|
+
lineEl.appendChild(doc.createTextNode(line.text));
|
|
3478
|
+
box.appendChild(lineEl);
|
|
3479
|
+
}
|
|
3480
|
+
}
|
|
3481
|
+
box.style.display = 'block';
|
|
3482
|
+
const rect = container.getBoundingClientRect();
|
|
3483
|
+
const x = e.clientX - rect.left;
|
|
3484
|
+
const y = e.clientY - rect.top;
|
|
3485
|
+
const w = box.offsetWidth;
|
|
3486
|
+
const h = box.offsetHeight;
|
|
3487
|
+
const room = (container.ownerDocument.defaultView?.innerHeight ?? Infinity) - e.clientY - 4;
|
|
3488
|
+
box.style.left = `${x + 14 + w > rect.width ? Math.max(0, x - 14 - w) : x + 14}px`;
|
|
3489
|
+
box.style.top = `${above || h + 14 > room ? y - 10 - h : y + 14}px`;
|
|
3490
|
+
}
|
|
3491
|
+
|
|
3492
|
+
/**
|
|
3493
|
+
* @param {PointerEvent} e - A pointer event on the frame timeline.
|
|
3494
|
+
* @returns {PerfHoverLine[]} Everything on the row under it, largest first, with the bar under
|
|
3495
|
+
* it marked: the small bars of a row are hard to point at one by one.
|
|
3496
|
+
* @private
|
|
3497
|
+
*/
|
|
3498
|
+
_timelineLines(e) {
|
|
3499
|
+
const hit = this._timelineHit(e);
|
|
3500
|
+
if (!hit) return [];
|
|
3501
|
+
const { lane, bars, hovered } = hit;
|
|
3502
|
+
// the names of the lanes of the assets give the key to their colors
|
|
3503
|
+
if (hit.x < this._gutter && /^asset\d+$/.test(lane.key)) {
|
|
3504
|
+
return [{ text: 'Assets loading in the frame, cut to it, packed into lanes. The color is the kind of asset', wrap: true }, ...assetKey()];
|
|
3505
|
+
}
|
|
3506
|
+
const average = this.range ? ', average per frame' : '';
|
|
3507
|
+
// the scripts list their types rather than the row
|
|
3508
|
+
if (hovered?.children?.length) {
|
|
3509
|
+
const at = Number.isFinite(hovered.start) ? ` at +${formatMs(hovered.start)}` : '';
|
|
3510
|
+
/** @type {PerfHoverLine[]} */
|
|
3511
|
+
const lines = [{ text: `${hovered.name} ${formatMs(hovered.duration)}${at}${average}` }];
|
|
3512
|
+
const children = [...hovered.children].sort((a, b) => b.duration - a.duration);
|
|
3513
|
+
for (const child of children.slice(0, HOVER_SCRIPT_TYPES)) {
|
|
3514
|
+
const calls = child.calls !== undefined ? ` · ${callCount(child.calls)}` : '';
|
|
3515
|
+
lines.push({ text: `${child.name} ${formatMs(child.duration)}${calls}`, color: child.color, indent: true });
|
|
3516
|
+
}
|
|
3517
|
+
if (hovered.pass) lines.push({ text: `Click to show ${hovered.name} on the Frame graph tab` });
|
|
3518
|
+
if (children.length > HOVER_SCRIPT_TYPES) lines.push({ text: `… and ${children.length - HOVER_SCRIPT_TYPES} more`, indent: true });
|
|
3519
|
+
return lines;
|
|
3520
|
+
}
|
|
3521
|
+
// a total only where the bars add up to one: the breakdown is of two
|
|
3522
|
+
const timed = lane.key === 'cpu' || lane.key === 'gpu';
|
|
3523
|
+
const total = bars.reduce((sum, b) => sum + b.duration, 0);
|
|
3524
|
+
/** @type {PerfHoverLine[]} */
|
|
3525
|
+
const lines = [{ text: `${lane.title}${timed ? ` ${formatMs(total)}` : ''}${average}` }];
|
|
3526
|
+
// what loaded says what it was itself, and the moments list in their order
|
|
3527
|
+
const loading = lane.key === 'milestones' || /^(?:asset|request)\d+$/.test(lane.key);
|
|
3528
|
+
const sorted = lane.key === 'events' || lane.key === 'milestones' ? bars : [...bars].sort((a, b) => b.duration - a.duration);
|
|
3529
|
+
const long = lane.key === 'types' || lane.key === 'gpu' || lane.key === 'gpuPasses' || loading;
|
|
3530
|
+
const shown = sorted.slice(0, long ? HOVER_SCRIPT_TYPES : TIMELINE_HOVER_BARS);
|
|
3531
|
+
// the hovered bar is listed even when the list is cut short
|
|
3532
|
+
if (hovered && !shown.includes(hovered)) shown[shown.length - 1] = hovered;
|
|
3533
|
+
for (const b of shown) {
|
|
3534
|
+
const time = b.duration > 0 ? ` ${formatMs(b.duration)}` : '';
|
|
3535
|
+
const at = Number.isFinite(b.start) ? ` at +${formatMs(b.start)}` : '';
|
|
3536
|
+
const calls = b.calls !== undefined ? ` · ${callCount(b.calls)}` : '';
|
|
3537
|
+
lines.push({ text: loading ? b.label : `${b.label ?? b.name}${time}${at}${calls}`, color: b.color, indent: true, strong: b === hovered });
|
|
3538
|
+
}
|
|
3539
|
+
if (sorted.length > shown.length) lines.push({ text: `… and ${sorted.length - shown.length} more`, indent: true });
|
|
3540
|
+
if (hovered?.script !== undefined) lines.push({ text: `Click to show ${hovered.name} on the Scripts tab` });
|
|
3541
|
+
if (hovered?.asset !== undefined) lines.push({ text: `Click to show ${hovered.name} on the Assets tab` });
|
|
3542
|
+
if (hovered?.pass) lines.push({ text: `Click to show ${hovered.label ?? hovered.name} on the Frame graph tab` });
|
|
3543
|
+
if (lane.key === 'detail') {
|
|
3544
|
+
const scripted = bars.some(b => b.children);
|
|
3545
|
+
lines.push({
|
|
3546
|
+
text: scripted ? 'The rest of the update is the other components, and the cost of timing the scripts' :
|
|
3547
|
+
'The rest of the update is the scripts and the other components. Turn on Script timing to time the scripts',
|
|
3548
|
+
wrap: true
|
|
3549
|
+
});
|
|
3550
|
+
if (!this._capture.detailed) lines.push({ text: 'The phases of the render are timed by the profiler and debug engines', wrap: true });
|
|
3551
|
+
}
|
|
3552
|
+
return lines;
|
|
3553
|
+
}
|
|
3554
|
+
|
|
3555
|
+
/**
|
|
3556
|
+
* @param {PointerEvent} e - The event.
|
|
3557
|
+
* @private
|
|
3558
|
+
*/
|
|
3559
|
+
_onPointerDown = (e) => {
|
|
3560
|
+
// a Ctrl+click is the context click of macOS, whose release can go missing once the menu
|
|
3561
|
+
// it opens shows: a press started on it would keep the pointer, so the menu got none
|
|
3562
|
+
if (e.button !== 0 || e.ctrlKey) return;
|
|
3563
|
+
const { x, row } = this._locate(e);
|
|
3564
|
+
// the divider beside the names drags to make them wider or narrower
|
|
3565
|
+
if (PerfCharts._onDivider(x)) {
|
|
3566
|
+
this._canvas.setPointerCapture?.(e.pointerId);
|
|
3567
|
+
this._press = { x, seq: -1, ranging: false, pointerId: e.pointerId, resize: true, clientX: e.clientX, gutter: this._gutter };
|
|
3568
|
+
this._showHover(null);
|
|
3569
|
+
this._updateCursor(e.shiftKey);
|
|
3570
|
+
return;
|
|
3571
|
+
}
|
|
3572
|
+
// a section's title, or the name beside an open chart, collapses or expands it
|
|
3573
|
+
const section = row?.section ?? (x < 0 && row?.chart !== undefined ? CHARTS[row.chart].key : null);
|
|
3574
|
+
if (section) {
|
|
3575
|
+
if (this.collapsed.has(section)) this.collapsed.delete(section);
|
|
3576
|
+
else this.collapsed.add(section);
|
|
3577
|
+
// the rows move, so what the box says no longer matches what is under the pointer
|
|
3578
|
+
this._showHover(null);
|
|
3579
|
+
this.invalidate();
|
|
3580
|
+
this.onCollapse();
|
|
3581
|
+
return;
|
|
3582
|
+
}
|
|
3583
|
+
// the rows of the Scripts list that show more script types, or fewer
|
|
3584
|
+
if (x < 0 && row?.control) {
|
|
3585
|
+
this.scriptRows = row.control === 'more' ? this.scriptRows + SCRIPT_ROWS : SCRIPT_ROWS;
|
|
3586
|
+
this._showHover(null);
|
|
3587
|
+
this.invalidate();
|
|
3588
|
+
this.onCollapse();
|
|
3589
|
+
return;
|
|
3590
|
+
}
|
|
3591
|
+
// a line of a line chart, or a row of events, from its box: Alt shows it alone, and Shift
|
|
3592
|
+
// pins a script type
|
|
3593
|
+
const toggle = x >= 0 ? null : row?.lines ? this._lineToggle(row.lines, row.index) :
|
|
3594
|
+
row?.kind !== undefined && !eventUnsupported(this._capture, EVENT_KINDS[row.kind].key) ? `events:${EVENT_KINDS[row.kind].key}` : null;
|
|
3595
|
+
if (toggle) {
|
|
3596
|
+
const line = row.lines ? this._lineCharts[row.lines].lines[row.index] : null;
|
|
3597
|
+
if (e.shiftKey && row.lines === 'scripts' && line.id >= 0) {
|
|
3598
|
+
this._hoverSignatures.delete(this._hoverEl);
|
|
3599
|
+
this.togglePin(line.name);
|
|
3600
|
+
this._showHover(null);
|
|
3601
|
+
return;
|
|
3602
|
+
}
|
|
3603
|
+
if (e.altKey && row.lines) this._solo(row.lines, row.index);
|
|
3604
|
+
else if (this.hidden.has(toggle)) this.hidden.delete(toggle);
|
|
3605
|
+
else this.hidden.add(toggle);
|
|
3606
|
+
this._hoverSignatures.delete(this._hoverEl);
|
|
3607
|
+
this._showHover(e);
|
|
3608
|
+
this.invalidate();
|
|
3609
|
+
this.onCollapse();
|
|
3610
|
+
return;
|
|
3611
|
+
}
|
|
3612
|
+
if (x < 0) return;
|
|
3613
|
+
this._canvas.setPointerCapture?.(e.pointerId);
|
|
3614
|
+
this._press = { x, seq: this._seqAt(x), ranging: false, pointerId: e.pointerId };
|
|
3615
|
+
// with Shift, zoomed in, the drag pans the view
|
|
3616
|
+
if (e.shiftKey && this._view) Object.assign(this._press, { pan: true, from: this._layout.from, moved: false });
|
|
3617
|
+
this._showHover(null);
|
|
3618
|
+
this._updateCursor(e.shiftKey);
|
|
3619
|
+
};
|
|
3620
|
+
|
|
3621
|
+
/**
|
|
3622
|
+
* @param {PointerEvent} e - The event.
|
|
3623
|
+
* @private
|
|
3624
|
+
*/
|
|
3625
|
+
_onPointerMove = (e) => {
|
|
3626
|
+
const { x } = this._locate(e);
|
|
3627
|
+
const press = this._press;
|
|
3628
|
+
this._pointerX = x;
|
|
3629
|
+
this._watchKeys(true);
|
|
3630
|
+
this._updateCursor(e.shiftKey);
|
|
3631
|
+
this._setDividerHot(press?.resize || (!press && PerfCharts._onDivider(x)));
|
|
3632
|
+
if (press?.resize) {
|
|
3633
|
+
this.gutter = press.gutter + e.clientX - press.clientX;
|
|
3634
|
+
return;
|
|
3635
|
+
}
|
|
3636
|
+
if (!press && PerfCharts._onDivider(x)) {
|
|
3637
|
+
this._showHover(null);
|
|
3638
|
+
return;
|
|
3639
|
+
}
|
|
3640
|
+
if (press?.pan) {
|
|
3641
|
+
press.moved ||= Math.abs(x - press.x) > 4;
|
|
3642
|
+
if (!press.moved) return;
|
|
3643
|
+
const { span, plot } = this._layout;
|
|
3644
|
+
const from = press.from - ((x - press.x) / plot) * span;
|
|
3645
|
+
this._view = { from, span, follow: from + span >= this._capture.endPos };
|
|
3646
|
+
this.invalidate();
|
|
3647
|
+
return;
|
|
3648
|
+
}
|
|
3649
|
+
if (press && (press.ranging || Math.abs(x - press.x) > 4)) {
|
|
3650
|
+
press.ranging = true;
|
|
3651
|
+
const capture = this._capture;
|
|
3652
|
+
const a = Math.max(capture.first, Math.min(press.seq, this._seqAt(x)));
|
|
3653
|
+
const b = Math.min(capture.end - 1, Math.max(press.seq, this._seqAt(x)));
|
|
3654
|
+
this.range = b >= a ? { from: a, to: b } : null;
|
|
3655
|
+
this._drawn.overlay = true;
|
|
3656
|
+
this._drawn.timeline = true;
|
|
3657
|
+
this._checkScriptLegend();
|
|
3658
|
+
this.onChange();
|
|
3659
|
+
} else if (!press) {
|
|
3660
|
+
this._showHover(e);
|
|
3661
|
+
}
|
|
3662
|
+
// only the marks move with the pointer: the charts keep to their own pace
|
|
3663
|
+
const hover = x >= 0 ? this._pickAt(e) : -1;
|
|
3664
|
+
if (hover !== this._hover) {
|
|
3665
|
+
this._hover = hover;
|
|
3666
|
+
this._drawn.overlay = true;
|
|
3667
|
+
}
|
|
3668
|
+
const level = x >= 0 ? this._levelAt(e) : null;
|
|
3669
|
+
if (level?.y !== this._level?.y || level?.value !== this._level?.value) {
|
|
3670
|
+
this._level = level;
|
|
3671
|
+
this._drawn.overlay = true;
|
|
3672
|
+
}
|
|
3673
|
+
};
|
|
3674
|
+
|
|
3675
|
+
/**
|
|
3676
|
+
* @param {PointerEvent} e - A pointer event on the charts.
|
|
3677
|
+
* @returns {{ y: number, value: number, format: (value: number) => string }|null} The level
|
|
3678
|
+
* under it in the chart it is over, a whole pixel down the canvas, or null outside one.
|
|
3679
|
+
* @private
|
|
3680
|
+
*/
|
|
3681
|
+
_levelAt(e) {
|
|
3682
|
+
const { y, row } = this._locate(e);
|
|
3683
|
+
const levels = row?.levels ?? (row?.lines ? this._lineCharts[row.lines]?.levels : null);
|
|
3684
|
+
if (!levels) return null;
|
|
3685
|
+
const { top, bottom, min, max, format } = levels;
|
|
3686
|
+
const at = Math.max(top, Math.min(bottom, Math.round(y)));
|
|
3687
|
+
return { y: at, value: min + ((bottom - at) / (bottom - top)) * (max - min), format };
|
|
3688
|
+
}
|
|
3689
|
+
|
|
3690
|
+
/**
|
|
3691
|
+
* @param {PointerEvent} e - The event.
|
|
3692
|
+
* @private
|
|
3693
|
+
*/
|
|
3694
|
+
_onPointerUp = (e) => {
|
|
3695
|
+
const press = this._press;
|
|
3696
|
+
this._press = null;
|
|
3697
|
+
this._updateCursor(e.shiftKey);
|
|
3698
|
+
if (press?.resize) {
|
|
3699
|
+
this.onCollapse();
|
|
3700
|
+
return;
|
|
3701
|
+
}
|
|
3702
|
+
if (!press || e.type === 'pointercancel') return;
|
|
3703
|
+
// a drag that panned picks nothing; a Shift+click without moving still picks the frame
|
|
3704
|
+
if (press.pan && press.moved) return;
|
|
3705
|
+
if (!press.ranging) {
|
|
3706
|
+
// an asset of the Loading section shows on the Assets tab rather than picking a frame
|
|
3707
|
+
const { x, y, row } = this._locate(e);
|
|
3708
|
+
if (row?.loading === 'assets') {
|
|
3709
|
+
const lane = Math.floor((y - row.top - 1) / (ASSET_LANE + 1));
|
|
3710
|
+
const px = x + this._gutter;
|
|
3711
|
+
const hit = this._loadingDrawn.assets?.find(item => item.lane === lane && px >= item.x0 - 1 && px <= item.x1 + 1);
|
|
3712
|
+
if (hit && hit.load.id !== undefined) {
|
|
3713
|
+
this.onSelectAsset(hit.load.id);
|
|
3714
|
+
return;
|
|
3715
|
+
}
|
|
3716
|
+
}
|
|
3717
|
+
this.range = null;
|
|
3718
|
+
this.pick(this._pickAt(e));
|
|
3719
|
+
}
|
|
3720
|
+
};
|
|
3721
|
+
|
|
3722
|
+
/**
|
|
3723
|
+
* A menu opens over the charts: ends any press, letting go of the pointer, so the menu gets it,
|
|
3724
|
+
* and hides the hover box, which the menu takes the place of.
|
|
3725
|
+
*
|
|
3726
|
+
* @private
|
|
3727
|
+
*/
|
|
3728
|
+
_onContextMenu = () => {
|
|
3729
|
+
const press = this._press;
|
|
3730
|
+
this._press = null;
|
|
3731
|
+
if (press && this._canvas.hasPointerCapture?.(press.pointerId)) this._canvas.releasePointerCapture(press.pointerId);
|
|
3732
|
+
this._showHover(null);
|
|
3733
|
+
};
|
|
3734
|
+
|
|
3735
|
+
/** @private */
|
|
3736
|
+
_onPointerLeave = () => {
|
|
3737
|
+
this._hover = -1;
|
|
3738
|
+
this._level = null;
|
|
3739
|
+
this._pointerX = NaN;
|
|
3740
|
+
this._watchKeys(false);
|
|
3741
|
+
this._updateCursor(false);
|
|
3742
|
+
this._setDividerHot(false);
|
|
3743
|
+
this._showHover(null);
|
|
3744
|
+
this._drawn.overlay = true;
|
|
3745
|
+
};
|
|
3746
|
+
|
|
3747
|
+
/**
|
|
3748
|
+
* Shows that a drag pans: a hand over the plot while Shift is held and the charts are zoomed
|
|
3749
|
+
* in, closed while it pans.
|
|
3750
|
+
*
|
|
3751
|
+
* @param {boolean} shift - Whether Shift is held.
|
|
3752
|
+
* @private
|
|
3753
|
+
*/
|
|
3754
|
+
_updateCursor(shift) {
|
|
3755
|
+
const press = this._press;
|
|
3756
|
+
const cursor = press?.resize || (!press && PerfCharts._onDivider(this._pointerX)) ? 'col-resize' :
|
|
3757
|
+
press?.pan ? 'grabbing' : shift && this._view && this._pointerX >= 0 ? 'grab' : '';
|
|
3758
|
+
if (this._canvas.style.cursor !== cursor) this._canvas.style.cursor = cursor;
|
|
3759
|
+
const time = this._timePress;
|
|
3760
|
+
const timeCursor = time?.resize || (!time && PerfCharts._onDivider(this._timePointerX)) ? 'col-resize' :
|
|
3761
|
+
time?.pan ? 'grabbing' : shift && this._timeView && this._timePointerX >= 0 ? 'grab' : this._timeCursor;
|
|
3762
|
+
if (this._timeline.style.cursor !== timeCursor) this._timeline.style.cursor = timeCursor;
|
|
3763
|
+
}
|
|
3764
|
+
|
|
3765
|
+
/**
|
|
3766
|
+
* @param {number} x - A position across the charts or the timeline right of the names.
|
|
3767
|
+
* @returns {boolean} Whether it is on the divider beside the names.
|
|
3768
|
+
* @private
|
|
3769
|
+
*/
|
|
3770
|
+
static _onDivider(x) {
|
|
3771
|
+
return x >= -DIVIDER_REACH && x <= DIVIDER_REACH / 2;
|
|
3772
|
+
}
|
|
3773
|
+
|
|
3774
|
+
/**
|
|
3775
|
+
* Marks the divider beside the names as under the pointer or being dragged, or not.
|
|
3776
|
+
*
|
|
3777
|
+
* @param {boolean} hot - Whether it is.
|
|
3778
|
+
* @private
|
|
3779
|
+
*/
|
|
3780
|
+
_setDividerHot(hot) {
|
|
3781
|
+
if (hot === this._dividerHot) return;
|
|
3782
|
+
this._dividerHot = hot;
|
|
3783
|
+
this._drawn.overlay = true;
|
|
3784
|
+
this._drawn.timeline = true;
|
|
3785
|
+
}
|
|
3786
|
+
|
|
3787
|
+
/**
|
|
3788
|
+
* Follows the Shift key of the window the charts are in while the pointer is over them, so the
|
|
3789
|
+
* cursor changes as Shift is pressed or let go without the pointer moving.
|
|
3790
|
+
*
|
|
3791
|
+
* @param {boolean} on - Whether to follow it.
|
|
3792
|
+
* @private
|
|
3793
|
+
*/
|
|
3794
|
+
_watchKeys(on) {
|
|
3795
|
+
const view = on ? this._canvas.ownerDocument.defaultView : null;
|
|
3796
|
+
if (view === this._keyView) return;
|
|
3797
|
+
this._keyView?.removeEventListener('keydown', this._onKey);
|
|
3798
|
+
this._keyView?.removeEventListener('keyup', this._onKey);
|
|
3799
|
+
this._keyView = view;
|
|
3800
|
+
view?.addEventListener('keydown', this._onKey);
|
|
3801
|
+
view?.addEventListener('keyup', this._onKey);
|
|
3802
|
+
}
|
|
3803
|
+
|
|
3804
|
+
/**
|
|
3805
|
+
* @param {KeyboardEvent} e - The event.
|
|
3806
|
+
* @private
|
|
3807
|
+
*/
|
|
3808
|
+
_onKey = (e) => {
|
|
3809
|
+
if (e.key === 'Shift') this._updateCursor(e.type === 'keydown');
|
|
3810
|
+
};
|
|
3811
|
+
|
|
3812
|
+
/**
|
|
3813
|
+
* Zooms around the pointer, or pans with Shift or a sideways scroll.
|
|
3814
|
+
*
|
|
3815
|
+
* @param {WheelEvent} e - The event.
|
|
3816
|
+
* @private
|
|
3817
|
+
*/
|
|
3818
|
+
_onWheel = (e) => {
|
|
3819
|
+
// over the names the wheel scrolls the charts, when they do not fit their box
|
|
3820
|
+
if (this._locate(e).x < 0) {
|
|
3821
|
+
this._showHover(null);
|
|
3822
|
+
return;
|
|
3823
|
+
}
|
|
3824
|
+
e.preventDefault();
|
|
3825
|
+
const capture = this._capture;
|
|
3826
|
+
if (capture.count === 0) return;
|
|
3827
|
+
// the frames as drawn, so the zoom keeps the frame under the pointer in place
|
|
3828
|
+
const { from, span, plot } = this._layout;
|
|
3829
|
+
const pan = e.shiftKey ? e.deltaY : e.deltaX;
|
|
3830
|
+
if (Math.abs(pan) > Math.abs(e.shiftKey ? 0 : e.deltaY)) {
|
|
3831
|
+
const next = from + (pan / plot) * span;
|
|
3832
|
+
this._view = { from: next, span, follow: next + span >= capture.endPos };
|
|
3833
|
+
} else {
|
|
3834
|
+
const x = Math.max(0, this._locate(e).x);
|
|
3835
|
+
const anchor = from + (x / plot) * span;
|
|
3836
|
+
const next = Math.max(MIN_ZOOM, span * Math.exp(e.deltaY * 0.002));
|
|
3837
|
+
// zoomed back out to the whole capture: a short one is spread over the shortest time
|
|
3838
|
+
// the charts show, so it can be zoomed into before it fills them
|
|
3839
|
+
if (next >= Math.max(MIN_SPAN, capture.endPos - capture.startPos)) {
|
|
3840
|
+
this._view = null;
|
|
3841
|
+
} else {
|
|
3842
|
+
const start = anchor - (x / plot) * next;
|
|
3843
|
+
this._view = { from: start, span: next, follow: start + next >= capture.endPos };
|
|
3844
|
+
}
|
|
3845
|
+
}
|
|
3846
|
+
this._showHover(null);
|
|
3847
|
+
this.invalidate();
|
|
3848
|
+
};
|
|
3849
|
+
|
|
3850
|
+
/** @private */
|
|
3851
|
+
_onDoubleClick = () => {
|
|
3852
|
+
this._view = null;
|
|
3853
|
+
this.clearRange();
|
|
3854
|
+
this.invalidate();
|
|
3855
|
+
};
|
|
3856
|
+
|
|
3857
|
+
/**
|
|
3858
|
+
* @param {PointerEvent} e - The event.
|
|
3859
|
+
* @private
|
|
3860
|
+
*/
|
|
3861
|
+
_onTimelineMove = (e) => {
|
|
3862
|
+
const x = e.clientX - this._timeline.getBoundingClientRect().left - this._gutter;
|
|
3863
|
+
const press = this._timePress;
|
|
3864
|
+
this._timePointerX = x;
|
|
3865
|
+
this._watchKeys(true);
|
|
3866
|
+
this._setDividerHot(press?.resize || (!press && PerfCharts._onDivider(x)));
|
|
3867
|
+
if (press?.resize) {
|
|
3868
|
+
this.gutter = press.gutter + e.clientX - press.clientX;
|
|
3869
|
+
} else if (press?.pan) {
|
|
3870
|
+
press.moved ||= Math.abs(x - press.x) > 4;
|
|
3871
|
+
const { span, plot } = this._timeLayout;
|
|
3872
|
+
if (press.moved) this._scrollTimeTo(press.from - ((x - press.x) / plot) * span);
|
|
3873
|
+
}
|
|
3874
|
+
if (press || PerfCharts._onDivider(x)) {
|
|
3875
|
+
this._renderHover(this._timelineHoverEl, this._timelineEl, [], null, true);
|
|
3876
|
+
} else {
|
|
3877
|
+
this._renderHover(this._timelineHoverEl, this._timelineEl, this._timelineLines(e), e, true);
|
|
3878
|
+
const hovered = this._timelineHit(e)?.hovered;
|
|
3879
|
+
this._timeCursor = this._onTimelineTitle(e) || hovered?.script !== undefined || hovered?.pass || hovered?.asset !== undefined ? 'pointer' : '';
|
|
3880
|
+
}
|
|
3881
|
+
this._updateCursor(e.shiftKey);
|
|
3882
|
+
};
|
|
3883
|
+
|
|
3884
|
+
/**
|
|
3885
|
+
* @param {MouseEvent} e - A pointer event on the timeline.
|
|
3886
|
+
* @returns {boolean} Whether it is on the title of the timeline.
|
|
3887
|
+
* @private
|
|
3888
|
+
*/
|
|
3889
|
+
_onTimelineTitle(e) {
|
|
3890
|
+
const rect = this._timeline.getBoundingClientRect();
|
|
3891
|
+
const x = e.clientX - rect.left;
|
|
3892
|
+
const y = e.clientY - rect.top;
|
|
3893
|
+
return y >= 0 && y < TIMELINE_TITLE - 2 && x >= 0 && x < this._timelineTitleEnd + 4;
|
|
3894
|
+
}
|
|
3895
|
+
|
|
3896
|
+
/**
|
|
3897
|
+
* A press on the divider beside the names drags it, and one with Shift while zoomed in pans.
|
|
3898
|
+
*
|
|
3899
|
+
* @param {PointerEvent} e - The event.
|
|
3900
|
+
* @private
|
|
3901
|
+
*/
|
|
3902
|
+
_onTimelineDown = (e) => {
|
|
3903
|
+
this._timeDragged = false;
|
|
3904
|
+
if (e.button !== 0 || e.ctrlKey) return;
|
|
3905
|
+
const x = e.clientX - this._timeline.getBoundingClientRect().left - this._gutter;
|
|
3906
|
+
if (PerfCharts._onDivider(x)) {
|
|
3907
|
+
this._timePress = { x, pointerId: e.pointerId, resize: true, clientX: e.clientX, gutter: this._gutter };
|
|
3908
|
+
} else if (e.shiftKey && this._timeView && x >= 0) {
|
|
3909
|
+
this._timePress = { x, pointerId: e.pointerId, pan: true, from: this._timeLayout.from, moved: false };
|
|
3910
|
+
} else {
|
|
3911
|
+
return;
|
|
3912
|
+
}
|
|
3913
|
+
this._timeline.setPointerCapture?.(e.pointerId);
|
|
3914
|
+
this._renderHover(this._timelineHoverEl, this._timelineEl, [], null, true);
|
|
3915
|
+
this._updateCursor(e.shiftKey);
|
|
3916
|
+
};
|
|
3917
|
+
|
|
3918
|
+
/**
|
|
3919
|
+
* @param {PointerEvent} e - The event.
|
|
3920
|
+
* @private
|
|
3921
|
+
*/
|
|
3922
|
+
_onTimelineUp = (e) => {
|
|
3923
|
+
const press = this._timePress;
|
|
3924
|
+
this._timePress = null;
|
|
3925
|
+
this._updateCursor(e.shiftKey);
|
|
3926
|
+
if (!press) return;
|
|
3927
|
+
this._timeDragged = press.resize || press.moved;
|
|
3928
|
+
if (press.resize) this.onCollapse();
|
|
3929
|
+
};
|
|
3930
|
+
|
|
3931
|
+
/**
|
|
3932
|
+
* A script type clicked on the timeline shows on the Scripts tab.
|
|
3933
|
+
*
|
|
3934
|
+
* @param {MouseEvent} e - The event.
|
|
3935
|
+
* @private
|
|
3936
|
+
*/
|
|
3937
|
+
_onTimelineClick = (e) => {
|
|
3938
|
+
// the end of a drag clicks nothing
|
|
3939
|
+
if (this._timeDragged) {
|
|
3940
|
+
this._timeDragged = false;
|
|
3941
|
+
return;
|
|
3942
|
+
}
|
|
3943
|
+
// the title collapses or expands the timeline
|
|
3944
|
+
if (this._onTimelineTitle(e)) {
|
|
3945
|
+
if (this.collapsed.has('timeline')) this.collapsed.delete('timeline');
|
|
3946
|
+
else this.collapsed.add('timeline');
|
|
3947
|
+
this._renderHover(this._timelineHoverEl, this._timelineEl, [], null, true);
|
|
3948
|
+
this._drawn.timeline = true;
|
|
3949
|
+
this.onCollapse();
|
|
3950
|
+
return;
|
|
3951
|
+
}
|
|
3952
|
+
const hovered = this._timelineHit(e)?.hovered;
|
|
3953
|
+
if (hovered?.script !== undefined) this.onSelectScript(hovered.script);
|
|
3954
|
+
else if (hovered?.pass) this.onSelectPass(hovered.pass);
|
|
3955
|
+
else if (hovered?.asset !== undefined) this.onSelectAsset(hovered.asset);
|
|
3956
|
+
};
|
|
3957
|
+
|
|
3958
|
+
/**
|
|
3959
|
+
* @param {MouseEvent} e - A pointer event on the frame timeline.
|
|
3960
|
+
* @returns {{ lane: { key: string, title: string }, bars: PerfBar[], x: number, hovered: PerfBar|null }|null}
|
|
3961
|
+
* The row under it, its bars, where across the timeline it is and the bar under it, or null
|
|
3962
|
+
* where there is no row with bars.
|
|
3963
|
+
* @private
|
|
3964
|
+
*/
|
|
3965
|
+
_timelineHit(e) {
|
|
3966
|
+
const rect = this._timeline.getBoundingClientRect();
|
|
3967
|
+
const x = e.clientX - rect.left;
|
|
3968
|
+
const y = e.clientY - rect.top;
|
|
3969
|
+
const lane = this._lanes.find(l => y >= l.y - 1 && y < l.y + l.h + 1);
|
|
3970
|
+
if (!lane) return null;
|
|
3971
|
+
const own = this._bars.filter(b => b.lane === lane.key);
|
|
3972
|
+
if (!own.length) return null;
|
|
3973
|
+
// the lanes of the assets, and of the requests, list together
|
|
3974
|
+
const group = /^(asset|request)\d+$/.exec(lane.key)?.[1];
|
|
3975
|
+
const bars = group ? this._bars.filter(b => b.lane.startsWith(group) && /\d$/.test(b.lane)) : own;
|
|
3976
|
+
return { lane, bars, x, hovered: own.find(b => x >= b.x0 - (b.x1 - b.x0 < 3 ? 2 : 0) && x < b.x1 + (b.x1 - b.x0 < 3 ? 2 : 0)) ?? null };
|
|
3977
|
+
}
|
|
3978
|
+
|
|
3979
|
+
/** @private */
|
|
3980
|
+
_onTimelineLeave = () => {
|
|
3981
|
+
this._timePointerX = NaN;
|
|
3982
|
+
this._watchKeys(false);
|
|
3983
|
+
this._setDividerHot(false);
|
|
3984
|
+
this._updateCursor(false);
|
|
3985
|
+
this._renderHover(this._timelineHoverEl, this._timelineEl, [], null, true);
|
|
3986
|
+
};
|
|
3987
|
+
}
|
|
3988
|
+
|
|
3989
|
+
export { CHARTS, PerfCharts };
|