1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Renders an array of slices into the provided div,
* using a child canvas element. Uses a FastRectRenderer to draw only
* the visible slices.
*/
cr.define('gpu', function() {
const palletteBase = [
{r: 138, g: 113, b: 152},
{r: 175, g: 112, b: 133},
{r: 127, g: 135, b: 225},
{r: 93, g: 81, b: 137},
{r: 116, g: 143, b: 119},
{r: 178, g: 214, b: 122},
{r: 87, g: 109, b: 147},
{r: 119, g: 155, b: 95},
{r: 114, g: 180, b: 160},
{r: 132, g: 85, b: 103},
{r: 157, g: 210, b: 150},
{r: 148, g: 94, b: 86},
{r: 164, g: 108, b: 138},
{r: 139, g: 191, b: 150},
{r: 110, g: 99, b: 145},
{r: 80, g: 129, b: 109},
{r: 125, g: 140, b: 149},
{r: 93, g: 124, b: 132},
{r: 140, g: 85, b: 140},
{r: 104, g: 163, b: 162},
{r: 132, g: 141, b: 178},
{r: 131, g: 105, b: 147},
{r: 135, g: 183, b: 98},
{r: 152, g: 134, b: 177},
{r: 141, g: 188, b: 141},
{r: 133, g: 160, b: 210},
{r: 126, g: 186, b: 148},
{r: 112, g: 198, b: 205},
{r: 180, g: 122, b: 195},
{r: 203, g: 144, b: 152}];
function brighten(c) {
return {r: Math.min(255, c.r + Math.floor(c.r * 0.45)),
g: Math.min(255, c.g + Math.floor(c.g * 0.45)),
b: Math.min(255, c.b + Math.floor(c.b * 0.45))};
}
function colorToString(c) {
return 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')';
}
const selectedIdBoost = palletteBase.length;
const pallette = palletteBase.concat(palletteBase.map(brighten)).
map(colorToString);
var textWidthMap = { };
function quickMeasureText(ctx, text) {
var w = textWidthMap[text];
if (!w) {
w = ctx.measureText(text).width;
textWidthMap[text] = w;
}
return w;
}
function addTrack(thisTrack, slices) {
var track = new TimelineSliceTrack();
track.heading = '';
track.slices = slices;
track.viewport = thisTrack.viewport_;
thisTrack.tracks_.push(track);
thisTrack.appendChild(track);
}
/**
* Generic base class for timeline tracks
*/
TimelineThreadTrack = cr.ui.define('div');
TimelineThreadTrack.prototype = {
__proto__: HTMLDivElement.prototype,
decorate: function() {
this.className = 'timeline-thread-track';
},
set thread(thread) {
this.thread_ = thread;
this.updateChildTracks_();
},
set viewport(v) {
this.viewport_ = v;
for (var i = 0; i < this.tracks_.length; i++)
this.tracks_[i].viewport = v;
this.invalidate();
},
invalidate: function() {
if (this.parentNode)
this.parentNode.invalidate();
},
onResize: function() {
for (var i = 0; i < this.tracks_.length; i++)
this.tracks_[i].onResize();
},
get firstCanvas() {
if (this.tracks_.length)
return this.tracks_[0].firstCanvas;
return undefined;
},
redraw: function() {
for (var i = 0; i < this.tracks_.length; i++)
this.tracks_[i].redraw();
},
updateChildTracks_: function() {
this.textContent = '';
this.tracks_ = [];
if (this.thread_) {
for (var srI = 0; srI < this.thread_.nonNestedSubRows.length; ++srI) {
addTrack(this, this.thread_.nonNestedSubRows[srI]);
}
for (var srI = 0; srI < this.thread_.subRows.length; ++srI) {
addTrack(this, this.thread_.subRows[srI]);
}
if (this.tracks_.length > 0) {
this.tracks_[0].heading = this.thread_.parent.pid + ': ' +
this.thread_.tid + ': ';
}
}
},
/**
* Picks a slice, if any, at a given location.
* @param {number} wX X location to search at, in worldspace.
* @param {number} wY Y location to search at, in offset space.
* offset space.
* @param {function():*} onHitCallback Callback to call with the slice,
* if one is found.
* @return {boolean} true if a slice was found, otherwise false.
*/
pick: function(wX, wY, onHitCallback) {
for (var i = 0; i < this.tracks_.length; i++) {
var trackClientRect = this.tracks_[i].getBoundingClientRect();
if (wY >= trackClientRect.top && wY < trackClientRect.bottom)
return this.tracks_[i].pick(wX, onHitCallback);
}
return false;
},
/**
* Finds slices intersecting the given interval.
* @param {number} loWX Lower X bound of the interval to search, in
* worldspace.
* @param {number} hiWX Upper X bound of the interval to search, in
* worldspace.
* @param {number} loY Lower Y bound of the interval to search, in
* offset space.
* @param {number} hiY Upper Y bound of the interval to search, in
* offset space.
* @param {function():*} onHitCallback Function to call for each slice
* intersecting the interval.
*/
pickRange: function(loWX, hiWX, loY, hiY, onHitCallback) {
for (var i = 0; i < this.tracks_.length; i++) {
var trackClientRect = this.tracks_[i].getBoundingClientRect();
var a = Math.max(loY, trackClientRect.top);
var b = Math.min(hiY, trackClientRect.bottom);
if (a <= b)
this.tracks_[i].pickRange(loWX, hiWX, loY, hiY, onHitCallback);
}
}
};
/**
* Creates a new timeline track div element
* @constructor
* @extends {HTMLDivElement}
*/
TimelineSliceTrack = cr.ui.define('div');
TimelineSliceTrack.prototype = {
__proto__: HTMLDivElement.prototype,
decorate: function() {
this.className = 'timeline-slice-track';
this.slices_ = null;
this.titleDiv_ = document.createElement('div');
this.titleDiv_.className = 'timeline-slice-track-title';
this.appendChild(this.titleDiv_);
this.canvasContainer_ = document.createElement('div');
this.canvasContainer_.className = 'timeline-slice-track-canvas-container';
this.appendChild(this.canvasContainer_);
this.canvas_ = document.createElement('canvas');
this.canvas_.className = 'timeline-slice-track-canvas';
this.canvasContainer_.appendChild(this.canvas_);
this.ctx_ = this.canvas_.getContext('2d');
},
set heading(text) {
this.titleDiv_.textContent = text;
},
set slices(slices) {
this.slices_ = slices;
this.invalidate();
},
set viewport(v) {
this.viewport_ = v;
this.invalidate();
},
invalidate: function() {
if (this.parentNode)
this.parentNode.invalidate();
},
get firstCanvas() {
return this.canvas_;
},
onResize: function() {
this.canvas_.width = this.canvasContainer_.clientWidth;
this.canvas_.height = this.canvasContainer_.clientHeight;
this.invalidate();
},
redraw: function() {
if (!this.viewport_)
return;
var ctx = this.ctx_;
var canvasW = this.canvas_.width;
var canvasH = this.canvas_.height;
ctx.clearRect(0, 0, canvasW, canvasH);
// culling...
var vp = this.viewport_;
var pixWidth = vp.xViewVectorToWorld(1);
var viewLWorld = vp.xViewToWorld(0);
var viewRWorld = vp.xViewToWorld(canvasW);
// Draw grid without a transform because the scale
// affects line width.
if (vp.gridEnabled) {
var x = vp.gridTimebase;
ctx.beginPath();
while (x < viewRWorld) {
if (x >= viewLWorld) {
// Do conversion to viewspace here rather than on
// x to avoid precision issues.
var vx = vp.xWorldToView(x);
ctx.moveTo(vx, 0);
ctx.lineTo(vx, canvasH);
}
x += vp.gridStep;
}
ctx.strokeStyle = 'rgba(255,0,0,0.25)';
ctx.stroke();
}
// begin rendering in world space
ctx.save();
vp.applyTransformToCanavs(ctx);
// tracks
var tr = new gpu.FastRectRenderer(ctx, viewLWorld, 2 * pixWidth,
2 * pixWidth, viewRWorld, pallette);
tr.setYandH(0, canvasH);
var slices = this.slices_;
for (var i = 0; i < slices.length; ++i) {
var slice = slices[i];
var x = slice.start;
// Less than 0.001 causes short events to disappear when zoomed in.
var w = Math.max(slice.duration, 0.001);
var colorId;
colorId = slice.selected ?
slice.colorId + selectedIdBoost :
slice.colorId;
if (w < pixWidth)
w = pixWidth;
tr.fillRect(x, w, colorId);
}
tr.flush();
ctx.restore();
// labels
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.font = '10px sans-serif';
ctx.strokeStyle = 'rgb(0,0,0)';
ctx.fillStyle = 'rgb(0,0,0)';
var quickDiscardThresshold = pixWidth * 20; // dont render until 20px wide
for (var i = 0; i < slices.length; ++i) {
var slice = slices[i];
if (slice.duration > quickDiscardThresshold) {
var title = slice.title;
if (slice.didNotFinish) {
title += " (Did Not Finish)";
}
var labelWidth = quickMeasureText(ctx, title) + 2;
var labelWidthWorld = pixWidth * labelWidth;
if (labelWidthWorld < slice.duration) {
var cX = vp.xWorldToView(slice.start + 0.5 * slice.duration);
ctx.fillText(title, cX, 2.5);
}
}
}
},
/**
* Picks a slice, if any, at a given location.
* @param {number} wX X location to search at, in worldspace.
* @param {number} wY Y location to search at, in offset space.
* offset space.
* @param {function():*} onHitCallback Callback to call with the slice,
* if one is found.
* @return {boolean} true if a slice was found, otherwise false.
*/
pick: function(wX, wY, onHitCallback) {
var clientRect = this.getBoundingClientRect();
if (wY < clientRect.top || wY >= clientRect.bottom)
return false;
var x = gpu.findLowIndexInSortedIntervals(this.slices_,
function(x) { return x.start; },
function(x) { return x.duration; },
wX);
if (x >= 0 && x < this.slices_.length) {
onHitCallback('slice', this, this.slices_[x]);
return true;
}
return false;
},
/**
* Finds slices intersecting the given interval.
* @param {number} loWX Lower X bound of the interval to search, in
* worldspace.
* @param {number} hiWX Upper X bound of the interval to search, in
* worldspace.
* @param {number} loY Lower Y bound of the interval to search, in
* offset space.
* @param {number} hiY Upper Y bound of the interval to search, in
* offset space.
* @param {function():*} onHitCallback Function to call for each slice
* intersecting the interval.
*/
pickRange: function(loWX, hiWX, loY, hiY, onHitCallback) {
var clientRect = this.getBoundingClientRect();
var a = Math.max(loY, clientRect.top);
var b = Math.min(hiY, clientRect.bottom);
if (a > b)
return;
var that = this;
function onPickHit(slice) {
onHitCallback('slice', that, slice);
}
gpu.iterateOverIntersectingIntervals(this.slices_,
function(x) { return x.start; },
function(x) { return x.duration; },
loWX, hiWX,
onPickHit);
}
};
return {
TimelineSliceTrack: TimelineSliceTrack,
TimelineThreadTrack: TimelineThreadTrack
};
});
|