/
Canvas.js
583 lines (518 loc) · 19.3 KB
/
Canvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { GREY_30 } from 'photon-colors';
import * as React from 'react';
import memoize from 'memoize-immutable';
import { TIMELINE_MARGIN_RIGHT } from '../../app-logic/constants';
import {
withChartViewport,
type WithChartViewport,
} from '../shared/chart/Viewport';
import { ChartCanvas } from '../shared/chart/Canvas';
import { FastFillStyle } from '../../utils';
import TextMeasurement from '../../utils/text-measurement';
import { formatMilliseconds } from '../../utils/format-numbers';
import { updatePreviewSelection } from '../../actions/profile-view';
import { mapCategoryColorNameToStackChartStyles } from '../../utils/colors';
import { TooltipCallNode } from '../tooltip/CallNode';
import { TooltipMarker } from '../tooltip/Marker';
import type {
Thread,
CategoryList,
PageList,
ThreadsKey,
UserTimingMarkerPayload,
WeightType,
CallNodeInfo,
IndexIntoCallNodeTable,
CombinedTimingRows,
Milliseconds,
CssPixels,
DevicePixels,
UnitIntervalOfProfileRange,
MarkerIndex,
Marker,
} from 'firefox-profiler/types';
import type {
StackTimingDepth,
IndexIntoStackTiming,
} from '../../profile-logic/stack-timing';
import type { Viewport } from '../shared/chart/Viewport';
import type { WrapFunctionInDispatch } from '../../utils/connect';
type OwnProps = {|
+thread: Thread,
+pages: PageList | null,
+threadsKey: ThreadsKey,
+interval: Milliseconds,
+weightType: WeightType,
+rangeStart: Milliseconds,
+rangeEnd: Milliseconds,
+combinedTimingRows: CombinedTimingRows,
+stackFrameHeight: CssPixels,
+updatePreviewSelection: WrapFunctionInDispatch<
typeof updatePreviewSelection
>,
+getMarker: (MarkerIndex) => Marker,
+categories: CategoryList,
+callNodeInfo: CallNodeInfo,
+selectedCallNodeIndex: IndexIntoCallNodeTable | null,
+onSelectionChange: (IndexIntoCallNodeTable | null) => void,
+onRightClick: (IndexIntoCallNodeTable | null) => void,
+shouldDisplayTooltips: () => boolean,
+scrollToSelectionGeneration: number,
+marginLeft: CssPixels,
|};
type Props = $ReadOnly<{|
...OwnProps,
+viewport: Viewport,
|}>;
type HoveredStackTiming = {|
+depth: StackTimingDepth,
+stackTimingIndex: IndexIntoStackTiming,
|};
import './Canvas.css';
const ROW_CSS_PIXELS_HEIGHT = 16;
const TEXT_CSS_PIXELS_OFFSET_START = 3;
const TEXT_CSS_PIXELS_OFFSET_TOP = 11;
const FONT_SIZE = 10;
const BORDER_OPACITY = 0.4;
class StackChartCanvasImpl extends React.PureComponent<Props> {
componentDidUpdate(prevProps) {
// We want to scroll the selection into view when this component
// is mounted, but using componentDidMount won't work here as the
// viewport will not have completed setting its size by
// then. Instead, look for when the viewport's isSizeSet prop
// changes to true.
if (!this.props.viewport.isSizeSet) {
return;
}
const viewportDidMount = !prevProps.viewport.isSizeSet;
if (
viewportDidMount ||
this.props.scrollToSelectionGeneration >
prevProps.scrollToSelectionGeneration
) {
this._scrollSelectionIntoView();
}
}
_scrollSelectionIntoView = () => {
const {
selectedCallNodeIndex,
callNodeInfo: { callNodeTable },
} = this.props;
if (selectedCallNodeIndex === null) {
return;
}
const depth = callNodeTable.depth[selectedCallNodeIndex];
const y = depth * ROW_CSS_PIXELS_HEIGHT;
if (y < this.props.viewport.viewportTop) {
this.props.viewport.moveViewport(0, this.props.viewport.viewportTop - y);
} else if (y + ROW_CSS_PIXELS_HEIGHT > this.props.viewport.viewportBottom) {
this.props.viewport.moveViewport(
0,
this.props.viewport.viewportBottom - (y + ROW_CSS_PIXELS_HEIGHT)
);
}
};
/**
* Draw the canvas.
*
* Note that most of the units are not absolute values, but unit intervals ranged from
* 0 - 1. This was done to make the calculations easier for computing various zoomed
* and translated views independent of any particular scale. See
* src/components/shared/chart/Viewport.js for a diagram detailing the various
* components of this set-up.
*/
_drawCanvas = (
ctx: CanvasRenderingContext2D,
hoveredItem: HoveredStackTiming | null
) => {
const {
thread,
rangeStart,
rangeEnd,
combinedTimingRows,
stackFrameHeight,
selectedCallNodeIndex,
categories,
callNodeInfo: { callNodeTable },
getMarker,
marginLeft,
viewport: {
containerWidth,
containerHeight,
viewportLeft,
viewportRight,
viewportTop,
viewportBottom,
},
} = this.props;
const fastFillStyle = new FastFillStyle(ctx);
const { devicePixelRatio } = window;
// Set the font size before creating a text measurer.
ctx.font = `${FONT_SIZE * devicePixelRatio}px sans-serif`;
const textMeasurement = new TextMeasurement(ctx);
const devicePixelsWidth = containerWidth * devicePixelRatio;
const devicePixelsHeight = containerHeight * devicePixelRatio;
fastFillStyle.set('#ffffff');
ctx.fillRect(0, 0, devicePixelsWidth, devicePixelsHeight);
const rangeLength: Milliseconds = rangeEnd - rangeStart;
const viewportLength: UnitIntervalOfProfileRange =
viewportRight - viewportLeft;
const viewportDevicePixelsTop = viewportTop * devicePixelRatio;
// Convert CssPixels to Stack Depth
const startDepth = Math.floor(viewportTop / stackFrameHeight);
const endDepth = Math.ceil(viewportBottom / stackFrameHeight);
const innerContainerWidth =
containerWidth - marginLeft - TIMELINE_MARGIN_RIGHT;
const innerDevicePixelsWidth = innerContainerWidth * devicePixelRatio;
const pixelAtViewportPosition = (
viewportPosition: UnitIntervalOfProfileRange
): DevicePixels =>
devicePixelRatio *
// The right hand side of this formula is all in CSS pixels.
(marginLeft +
((viewportPosition - viewportLeft) * innerContainerWidth) /
viewportLength);
// Apply the device pixel ratio to various CssPixel constants.
const rowDevicePixelsHeight = ROW_CSS_PIXELS_HEIGHT * devicePixelRatio;
const oneCssPixelInDevicePixels = 1 * devicePixelRatio;
const textDevicePixelsOffsetStart =
TEXT_CSS_PIXELS_OFFSET_START * devicePixelRatio;
const textDevicePixelsOffsetTop =
TEXT_CSS_PIXELS_OFFSET_TOP * devicePixelRatio;
let categoryForUserTiming = categories.findIndex(
(category) => category.name === 'JavaScript'
);
if (categoryForUserTiming === -1) {
// Default to the first item in the categories list.
categoryForUserTiming = 0;
}
// Only draw the stack frames that are vertically within view.
for (let depth = startDepth; depth < endDepth; depth++) {
// Get the timing information for a row of stack frames.
const stackTiming = combinedTimingRows[depth];
if (!stackTiming) {
continue;
}
/*
* TODO - Do an O(log n) binary search to find the only samples in range rather than
* linear O(n) search for loops. Profile the results to see if this helps at all.
*
* const startSampleIndex = binarySearch(stackTiming.start, rangeStart + rangeLength * viewportLeft);
* const endSampleIndex = binarySearch(stackTiming.end, rangeStart + rangeLength * viewportRight);
*/
const pixelsInViewport = viewportLength * innerDevicePixelsWidth;
const timePerPixel = rangeLength / pixelsInViewport;
// Decide which samples to actually draw
const timeAtStart: Milliseconds =
rangeStart + rangeLength * viewportLeft - timePerPixel * marginLeft;
const timeAtEnd: Milliseconds = rangeStart + rangeLength * viewportRight;
let lastDrawnPixelX = 0;
for (let i = 0; i < stackTiming.length; i++) {
// Only draw samples that are in bounds.
if (
stackTiming.end[i] > timeAtStart &&
stackTiming.start[i] < timeAtEnd
) {
// Draw a box, but increase the size by a small portion in order to draw
// a single pixel at the end with a slight opacity.
//
// Legend:
// |======| A stack frame's timing.
// |O| A single fully opaque pixel.
// |.| A slightly transparent pixel.
// | | A fully transparent pixel.
//
// Drawing strategy:
//
// Frame timing |=====||========| |=====| |=| |=|=|=|=|
// Device Pixels |O|O|.|O|O|O|O|.| | |O|O|O|.| | |O|.| | |O|.|O|.|
// CSS Pixels | | | | | | | | | | | | |
// First compute the left and right sides of the box.
const viewportAtStartTime: UnitIntervalOfProfileRange =
(stackTiming.start[i] - rangeStart) / rangeLength;
const viewportAtEndTime: UnitIntervalOfProfileRange =
(stackTiming.end[i] - rangeStart) / rangeLength;
const floatX = pixelAtViewportPosition(viewportAtStartTime);
const floatW: DevicePixels =
((viewportAtEndTime - viewportAtStartTime) *
innerDevicePixelsWidth) /
viewportLength -
1;
// Determine if there is enough pixel space to draw this box, and snap the
// box to the pixels.
let snappedFloatX = floatX;
let snappedFloatW = floatW;
let skipDraw = true;
if (floatX >= lastDrawnPixelX) {
// The x value is past the last lastDrawnPixelX, so it can be drawn.
skipDraw = false;
} else if (floatX + floatW > lastDrawnPixelX) {
// The left side of the box is before the lastDrawnPixelX value, but the
// right hand side is within a range to be drawn. Truncate the box a little
// bit in order to draw it to the screen in the free space.
snappedFloatW = floatW - (lastDrawnPixelX - floatX);
snappedFloatX = lastDrawnPixelX;
skipDraw = false;
}
if (skipDraw) {
// This box didn't satisfy the constraints in the above if checks, so skip it.
continue;
}
// Convert or compute all of the integer values for drawing the box.
// Note, this should all be Math.round instead of floor and ceil, but some
// off by one errors appear to be creating gaps where there shouldn't be any.
const intX = Math.floor(snappedFloatX);
const intY = Math.round(
depth * rowDevicePixelsHeight - viewportDevicePixelsTop
);
const intW = Math.ceil(Math.max(1, snappedFloatW));
const intH = Math.round(
rowDevicePixelsHeight - oneCssPixelInDevicePixels
);
// Look up information about this stack frame.
let text, category, isSelected;
if (stackTiming.callNode) {
const callNodeIndex = stackTiming.callNode[i];
const funcIndex = callNodeTable.func[callNodeIndex];
const funcNameIndex = thread.funcTable.name[funcIndex];
text = thread.stringTable.getString(funcNameIndex);
const categoryIndex = callNodeTable.category[callNodeIndex];
category = categories[categoryIndex];
isSelected = selectedCallNodeIndex === callNodeIndex;
} else {
const markerIndex = stackTiming.index[i];
const markerPayload = ((getMarker(markerIndex)
.data: any): UserTimingMarkerPayload);
text = markerPayload.name;
category = categories[categoryForUserTiming];
isSelected = selectedCallNodeIndex === markerIndex;
}
const isHovered =
hoveredItem &&
depth === hoveredItem.depth &&
i === hoveredItem.stackTimingIndex;
const colorStyles = this._mapCategoryColorNameToStyles(
category.color
);
// Draw the box.
fastFillStyle.set(
isHovered || isSelected
? colorStyles.selectedFillStyle
: colorStyles.unselectedFillStyle
);
ctx.fillRect(
intX,
intY,
// Add on a bit of BORDER_OPACITY to the end of the width, to draw a partial
// pixel. This will effectively draw a transparent version of the fill color
// without having to change the fill color. At the time of this writing it
// was the same performance cost as only providing integer values here.
intW + BORDER_OPACITY,
intH
);
lastDrawnPixelX =
intX +
intW +
// The border on the right is 1 device pixel wide.
1;
// Draw the text label if it fits. Use the original float values here so that
// the text doesn't snap around when moving. Only the boxes should snap.
const textX: DevicePixels =
// Constrain the x coordinate to the leftmost area.
Math.max(floatX, 0) + textDevicePixelsOffsetStart;
const textW: DevicePixels = Math.max(0, floatW - (textX - floatX));
if (textW > textMeasurement.minWidth) {
const fittedText = textMeasurement.getFittedText(text, textW);
if (fittedText) {
fastFillStyle.set(
isHovered || isSelected
? colorStyles.selectedTextColor
: '#000000'
);
ctx.fillText(fittedText, textX, intY + textDevicePixelsOffsetTop);
}
}
}
}
}
// Draw the borders on the left and right.
fastFillStyle.set(GREY_30);
ctx.fillRect(
pixelAtViewportPosition(0),
0,
oneCssPixelInDevicePixels,
devicePixelsHeight
);
ctx.fillRect(
pixelAtViewportPosition(1),
0,
oneCssPixelInDevicePixels,
devicePixelsHeight
);
};
// Provide a memoized function that maps the category color names to specific color
// choices that are used across this project's charts.
_mapCategoryColorNameToStyles = memoize(
mapCategoryColorNameToStackChartStyles,
{
// Memoize every color that is seen.
limit: Infinity,
}
);
_getHoveredStackInfo = ({
depth,
stackTimingIndex,
}: HoveredStackTiming): React.Node | null => {
const {
thread,
weightType,
threadsKey,
combinedTimingRows,
categories,
callNodeInfo,
getMarker,
shouldDisplayTooltips,
interval,
pages,
} = this.props;
if (!shouldDisplayTooltips()) {
return null;
}
const timing = combinedTimingRows[depth];
if (timing.index) {
const markerIndex = timing.index[stackTimingIndex];
return (
<TooltipMarker
markerIndex={markerIndex}
marker={getMarker(markerIndex)}
threadsKey={threadsKey}
restrictHeightWidth={true}
/>
);
}
const callNodeIndex = timing.callNode[stackTimingIndex];
const duration =
timing.end[stackTimingIndex] - timing.start[stackTimingIndex];
return (
<TooltipCallNode
thread={thread}
weightType={weightType}
pages={pages}
interval={interval}
callNodeIndex={callNodeIndex}
callNodeInfo={callNodeInfo}
categories={categories}
// The stack chart doesn't support other call tree summary types.
callTreeSummaryStrategy="timing"
durationText={formatMilliseconds(duration)}
/>
);
};
_onDoubleClickStack = (hoveredItem: HoveredStackTiming | null) => {
if (hoveredItem === null) {
return;
}
const { depth, stackTimingIndex } = hoveredItem;
const { combinedTimingRows, updatePreviewSelection } = this.props;
updatePreviewSelection({
hasSelection: true,
isModifying: false,
selectionStart: combinedTimingRows[depth].start[stackTimingIndex],
selectionEnd: combinedTimingRows[depth].end[stackTimingIndex],
});
};
_getCallNodeIndexOrMarkerIndexFromHoveredItem(
hoveredItem: HoveredStackTiming | null
): {| index: number, type: 'marker' | 'call-node' |} | null {
if (hoveredItem === null) {
return null;
}
const { depth, stackTimingIndex } = hoveredItem;
const { combinedTimingRows } = this.props;
if (combinedTimingRows[depth].callNode) {
const callNodeIndex =
combinedTimingRows[depth].callNode[stackTimingIndex];
return { index: callNodeIndex, type: 'call-node' };
}
const index = combinedTimingRows[depth].index[stackTimingIndex];
return { index, type: 'marker' };
}
_onSelectItem = (hoveredItem: HoveredStackTiming | null) => {
// Change our selection to the hovered item, or deselect (with
// null) if there's nothing hovered.
const result =
this._getCallNodeIndexOrMarkerIndexFromHoveredItem(hoveredItem);
if (!result) {
this.props.onSelectionChange(null);
}
// TODO implement selecting user timing markers #2355
if (result && result.type === 'call-node') {
this.props.onSelectionChange(result.index);
}
};
_onRightClick = (hoveredItem: HoveredStackTiming | null) => {
const result =
this._getCallNodeIndexOrMarkerIndexFromHoveredItem(hoveredItem);
// TODO implement right clicking user timing markers #2354
if (result && result.type === 'call-node') {
this.props.onRightClick(result.index);
}
};
_hitTest = (x: CssPixels, y: CssPixels): HoveredStackTiming | null => {
const {
rangeStart,
rangeEnd,
combinedTimingRows,
marginLeft,
viewport: { viewportLeft, viewportRight, viewportTop, containerWidth },
} = this.props;
const innerDevicePixelsWidth =
containerWidth - marginLeft - TIMELINE_MARGIN_RIGHT;
const rangeLength: Milliseconds = rangeEnd - rangeStart;
const viewportLength: UnitIntervalOfProfileRange =
viewportRight - viewportLeft;
const unitIntervalTime: UnitIntervalOfProfileRange =
viewportLeft +
viewportLength * ((x - marginLeft) / innerDevicePixelsWidth);
const time: Milliseconds = rangeStart + unitIntervalTime * rangeLength;
const depth = Math.floor((y + viewportTop) / ROW_CSS_PIXELS_HEIGHT);
const stackTiming = combinedTimingRows[depth];
if (!stackTiming) {
return null;
}
for (let i = 0; i < stackTiming.length; i++) {
const start = stackTiming.start[i];
const end = stackTiming.end[i];
if (start < time && end > time) {
return { depth, stackTimingIndex: i };
}
}
return null;
};
render() {
const { containerWidth, containerHeight, isDragging } = this.props.viewport;
return (
<ChartCanvas
scaleCtxToCssPixels={false}
className="stackChartCanvas"
containerWidth={containerWidth}
containerHeight={containerHeight}
isDragging={isDragging}
onDoubleClickItem={this._onDoubleClickStack}
getHoveredItemInfo={this._getHoveredStackInfo}
drawCanvas={this._drawCanvas}
hitTest={this._hitTest}
onSelectItem={this._onSelectItem}
onRightClick={this._onRightClick}
/>
);
}
}
export const StackChartCanvas = (withChartViewport: WithChartViewport<
OwnProps,
Props
>)(StackChartCanvasImpl);