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
|
// Copyright (c) 2013 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.
'use strict';
base.require('base.sorted_array_utils');
base.require('tracing.color_scheme');
base.require('tracing.elided_cache');
/**
* @fileoverview Provides various helper methods for drawing to a provided
* canvas.
*/
base.exportTo('tracing', function() {
var elidedTitleCache = new tracing.ElidedTitleCache();
var palette = tracing.getColorPalette();
var EventPresenter = tracing.EventPresenter;
/**
* Should we elide text on trace labels?
* Without eliding, text that is too wide isn't drawn at all.
* Disable if you feel this causes a performance problem.
* This is a default value that can be overridden in tracks for testing.
* @const
*/
var SHOULD_ELIDE_TEXT = true;
/**
* Draw the define line into |ctx|.
*
* @param {Context} ctx The context to draw into.
* @param {float} x1 The start x position of the line.
* @param {float} y1 The start y position of the line.
* @param {float} x2 The end x position of the line.
* @param {float} y2 The end y position of the line.
*/
function drawLine(ctx, x1, y1, x2, y2) {
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
}
/**
* Draw the defined triangle into |ctx|.
*
* @param {Context} ctx The context to draw into.
* @param {float} x1 The first corner x.
* @param {float} y1 The first corner y.
* @param {float} x2 The second corner x.
* @param {float} y2 The second corner y.
* @param {float} x3 The third corner x.
* @param {float} y3 The third corner y.
*/
function drawTriangle(ctx, x1, y1, x2, y2, x3, y3) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.closePath();
}
/**
* Draw an arrow into |ctx|.
*
* @param {Context} ctx The context to draw into.
* @param {float} x1 The shaft x.
* @param {float} y1 The shaft y.
* @param {float} x2 The head x.
* @param {float} y2 The head y.
* @param {float} arrowLength The length of the head.
* @param {float} arrowWidth The width of the head.
*/
function drawArrow(ctx, x1, y1, x2, y2, arrowLength, arrowWidth) {
var dx = x2 - x1;
var dy = y2 - y1;
var len = Math.sqrt(dx * dx + dy * dy);
var perc = (len - arrowLength) / len;
var bx = x1 + perc * dx;
var by = y1 + perc * dy;
var ux = dx / len;
var uy = dy / len;
var ax = uy * arrowWidth;
var ay = -ux * arrowWidth;
ctx.beginPath();
drawLine(ctx, x1, y1, x2, y2);
ctx.stroke();
drawTriangle(ctx,
bx + ax, by + ay,
x2, y2,
bx - ax, by - ay);
ctx.fill();
}
/**
* Draw the provided slices to the screen.
*
* Each of the elements in |slices| must provide the follow methods:
* * start
* * duration
* * colorId
* * selected
*
* @param {Context} ctx The canvas context.
* @param {TimelineDrawTransform} dt The draw transform.
* @param {float} viewLWorld The left most point of the world viewport.
* @param {float} viewLWorld The right most point of the world viewport.
* @param {float} viewHeight The height of the viewport.
* @param {Array} slices The slices to draw.
* @param {bool} async Whether the slices are drawn with async style.
*/
function drawSlices(ctx, dt, viewLWorld, viewRWorld, viewHeight, slices,
async) {
var pixelRatio = window.devicePixelRatio || 1;
var pixWidth = dt.xViewVectorToWorld(1);
var height = viewHeight * pixelRatio;
// Begin rendering in world space.
ctx.save();
dt.applyTransformToCanvas(ctx);
var tr = new tracing.FastRectRenderer(
ctx, 2 * pixWidth, 2 * pixWidth, palette);
tr.setYandH(0, height);
var lowSlice = base.findLowIndexInSortedArray(
slices,
function(slice) { return slice.start + slice.duration; },
viewLWorld);
for (var i = lowSlice; i < slices.length; ++i) {
var slice = slices[i];
var x = slice.start;
if (x > viewRWorld)
break;
var w = pixWidth;
if (slice.duration > 0) {
w = Math.max(slice.duration, 0.001);
if (w < pixWidth)
w = pixWidth;
}
var colorId = EventPresenter.getSliceColorId(slice);
var alpha = EventPresenter.getSliceAlpha(slice, async);
tr.fillRect(x, w, colorId, alpha);
}
tr.flush();
ctx.restore();
}
/**
* Draw the provided instant slices as lines to the screen.
*
* Each of the elements in |slices| must provide the follow methods:
* * start
* * duration with value of 0.
* * colorId
* * selected
*
* @param {Context} ctx The canvas context.
* @param {TimelineDrawTransform} dt The draw transform.
* @param {float} viewLWorld The left most point of the world viewport.
* @param {float} viewLWorld The right most point of the world viewport.
* @param {float} viewHeight The height of the viewport.
* @param {Array} slices The slices to draw.
* @param {Numer} lineWidthInPixels The width of the lines.
*/
function drawInstantSlicesAsLines(
ctx, dt, viewLWorld, viewRWorld, viewHeight, slices, lineWidthInPixels) {
var pixelRatio = window.devicePixelRatio || 1;
var height = viewHeight * pixelRatio;
var pixWidth = dt.xViewVectorToWorld(1);
// Begin rendering in world space.
ctx.save();
ctx.lineWidth = pixWidth * lineWidthInPixels;
dt.applyTransformToCanvas(ctx);
ctx.beginPath();
var lowSlice = base.findLowIndexInSortedArray(
slices,
function(slice) { return slice.start; },
viewLWorld);
for (var i = lowSlice; i < slices.length; ++i) {
var slice = slices[i];
var x = slice.start;
if (x > viewRWorld)
break;
ctx.strokeStyle = EventPresenter.getInstantSliceColor(slice);
ctx.moveTo(x, 0);
ctx.lineTo(x, height);
}
ctx.stroke();
ctx.restore();
}
/**
* Draws the labels for the given slices.
*
* The |slices| array must contain objects with the following API:
* * start
* * duration
* * title
* * didNotFinish (optional)
*
* @param {Context} ctx The graphics context.
* @param {TimelineDrawTransform} dt The draw transform.
* @param {float} viewLWorld The left most point of the world viewport.
* @param {float} viewLWorld The right most point of the world viewport.
* @param {Array} slices The slices to label.
* @param {bool} async Whether the slice labels are drawn with async style.
*/
function drawLabels(ctx, dt, viewLWorld, viewRWorld, slices, async) {
var pixelRatio = window.devicePixelRatio || 1;
var pixWidth = dt.xViewVectorToWorld(1);
ctx.save();
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.font = (10 * pixelRatio) + 'px sans-serif';
if (async)
ctx.font = 'italic ' + ctx.font;
var lowSlice = base.findLowIndexInSortedArray(
slices,
function(slice) { return slice.start + slice.duration; },
viewLWorld);
// Don't render text until until it is 20px wide
var quickDiscardThresshold = pixWidth * 20;
for (var i = lowSlice; i < slices.length; ++i) {
var slice = slices[i];
if (slice.start > viewRWorld)
break;
if (slice.duration <= quickDiscardThresshold)
continue;
var title = slice.title +
(slice.didNotFinish ? ' (Did Not Finish)' : '');
var drawnTitle = title;
var drawnWidth = elidedTitleCache.labelWidth(ctx, drawnTitle);
var fullLabelWidth = elidedTitleCache.labelWidthWorld(
ctx, drawnTitle, pixWidth);
if (SHOULD_ELIDE_TEXT && fullLabelWidth > slice.duration) {
var elidedValues = elidedTitleCache.get(
ctx, pixWidth,
drawnTitle, drawnWidth,
slice.duration);
drawnTitle = elidedValues.string;
drawnWidth = elidedValues.width;
}
if (drawnWidth * pixWidth < slice.duration) {
ctx.fillStyle = EventPresenter.getTextColor(slice);
var cX = dt.xWorldToView(slice.start + 0.5 * slice.duration);
ctx.fillText(drawnTitle, cX, 2.5 * pixelRatio, drawnWidth);
}
}
ctx.restore();
}
return {
drawSlices: drawSlices,
drawInstantSlicesAsLines: drawInstantSlicesAsLines,
drawLabels: drawLabels,
drawLine: drawLine,
drawTriangle: drawTriangle,
drawArrow: drawArrow,
elidedTitleCache_: elidedTitleCache
};
});
|