2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @extends {WebInspector.TimelineOverviewBase}
34 * @param {!WebInspector.TimelineModel} model
35 * @param {!WebInspector.TimelineFrameModelBase} frameModel
37 WebInspector.TimelineFrameOverview = function(model, frameModel)
39 WebInspector.TimelineOverviewBase.call(this, model);
40 this.element.id = "timeline-overview-frames";
41 this._frameModel = frameModel;
44 this._outerPadding = 4 * window.devicePixelRatio;
45 this._maxInnerBarWidth = 10 * window.devicePixelRatio;
46 this._topPadding = 6 * window.devicePixelRatio;
48 // The below two are really computed by update() -- but let's have something so that windowTimes() is happy.
49 this._actualPadding = 5 * window.devicePixelRatio;
50 this._actualOuterBarWidth = this._maxInnerBarWidth + this._actualPadding;
52 this._fillStyles = {};
53 var categories = WebInspector.TimelineUIUtils.categories();
54 for (var category in categories)
55 this._fillStyles[category] = WebInspector.TimelineUIUtils.createFillStyleForCategory(this._context, this._maxInnerBarWidth, 0, categories[category]);
57 this._frameTopShadeGradient = this._context.createLinearGradient(0, 0, 0, this._topPadding);
58 this._frameTopShadeGradient.addColorStop(0, "rgba(255, 255, 255, 0.9)");
59 this._frameTopShadeGradient.addColorStop(1, "rgba(255, 255, 255, 0.2)");
62 WebInspector.TimelineFrameOverview.prototype = {
64 * @param {!WebInspector.OverviewGrid} grid
66 setOverviewGrid: function(grid)
68 this._overviewGrid = grid;
69 this._overviewGrid.element.classList.add("timeline-overview-frames-mode");
74 this._overviewGrid.element.classList.remove("timeline-overview-frames-mode");
79 this._recordsPerBar = 1;
80 /** @type {!Array.<!{startTime:number, endTime:number}>} */
89 const minBarWidth = 4 * window.devicePixelRatio;
90 var frames = this._frameModel.frames();
91 var framesPerBar = Math.max(1, frames.length * minBarWidth / this._canvas.width);
92 var visibleFrames = this._aggregateFrames(frames, framesPerBar);
95 var scale = (this._canvas.height - this._topPadding) / this._computeTargetFrameLength(visibleFrames);
96 this._renderBars(visibleFrames, scale, this._canvas.height);
97 this._context.fillStyle = this._frameTopShadeGradient;
98 this._context.fillRect(0, 0, this._canvas.width, this._topPadding);
99 this._drawFPSMarks(scale, this._canvas.height);
100 this._context.restore();
104 * @param {!Array.<!WebInspector.TimelineFrame>} frames
105 * @param {number} framesPerBar
106 * @return {!Array.<!WebInspector.TimelineFrame>}
108 _aggregateFrames: function(frames, framesPerBar)
110 var visibleFrames = [];
111 for (var barNumber = 0, currentFrame = 0; currentFrame < frames.length; ++barNumber) {
112 var barStartTime = frames[currentFrame].startTime;
113 var longestFrame = null;
114 var longestDuration = 0;
116 for (var lastFrame = Math.min(Math.floor((barNumber + 1) * framesPerBar), frames.length);
117 currentFrame < lastFrame; ++currentFrame) {
118 var duration = frames[currentFrame].duration;
119 if (!longestFrame || longestDuration < duration) {
120 longestFrame = frames[currentFrame];
121 longestDuration = duration;
124 var barEndTime = frames[currentFrame - 1].endTime;
126 visibleFrames.push(longestFrame);
127 this._barTimes.push({ startTime: barStartTime, endTime: barEndTime });
130 return visibleFrames;
134 * @param {!Array.<!WebInspector.TimelineFrame>} frames
137 _computeTargetFrameLength: function(frames)
139 const targetFPS = 20;
140 var result = 1000.0 / targetFPS;
145 for (var i = 0; i < frames.length; ++i) {
147 durations.push(frames[i].duration);
149 var medianFrameLength = durations.qselect(Math.floor(durations.length / 2));
151 // Optimize appearance for 30fps, but leave some space so it's evident when a frame overflows.
152 // However, if at least half frames won't fit at this scale, fall back to using autoscale.
153 if (result >= medianFrameLength)
156 var maxFrameLength = Math.max.apply(Math, durations);
157 return Math.min(medianFrameLength * 2, maxFrameLength);
161 * @param {!Array.<!WebInspector.TimelineFrame>} frames
162 * @param {number} scale
163 * @param {number} windowHeight
165 _renderBars: function(frames, scale, windowHeight)
167 const maxPadding = 5 * window.devicePixelRatio;
168 this._actualOuterBarWidth = Math.min((this._canvas.width - 2 * this._outerPadding) / frames.length, this._maxInnerBarWidth + maxPadding);
169 this._actualPadding = Math.min(Math.floor(this._actualOuterBarWidth / 3), maxPadding);
171 var barWidth = this._actualOuterBarWidth - this._actualPadding;
172 for (var i = 0; i < frames.length; ++i) {
174 this._renderBar(this._barNumberToScreenPosition(i), barWidth, windowHeight, frames[i], scale);
181 _barNumberToScreenPosition: function(n)
183 return this._outerPadding + this._actualOuterBarWidth * n;
187 * @param {number} scale
188 * @param {number} height
190 _drawFPSMarks: function(scale, height)
192 const fpsMarks = [30, 60];
194 this._context.save();
195 this._context.beginPath();
196 this._context.font = (10 * window.devicePixelRatio) + "px " + window.getComputedStyle(this.element, null).getPropertyValue("font-family");
197 this._context.textAlign = "right";
198 this._context.textBaseline = "alphabetic";
200 const labelPadding = 4 * window.devicePixelRatio;
201 const baselineHeight = 3 * window.devicePixelRatio;
202 var lineHeight = 12 * window.devicePixelRatio;
203 var labelTopMargin = 0;
204 var labelOffsetY = 0; // Labels are going to be under their grid lines.
206 for (var i = 0; i < fpsMarks.length; ++i) {
207 var fps = fpsMarks[i];
208 // Draw lines one pixel above they need to be, so 60pfs line does not cross most of the frames tops.
209 var y = height - Math.floor(1000.0 / fps * scale) - 0.5;
210 var label = WebInspector.UIString("%d\u2009fps", fps);
211 var labelWidth = this._context.measureText(label).width + 2 * labelPadding;
212 var labelX = this._canvas.width;
214 if (!i && labelTopMargin < y - lineHeight)
215 labelOffsetY = -lineHeight; // Labels are going to be over their grid lines.
216 var labelY = y + labelOffsetY;
217 if (labelY < labelTopMargin || labelY + lineHeight > height)
218 break; // No space for the label, so no line as well.
220 this._context.moveTo(0, y);
221 this._context.lineTo(this._canvas.width, y);
223 this._context.fillStyle = "rgba(255, 255, 255, 0.5)";
224 this._context.fillRect(labelX - labelWidth, labelY, labelWidth, lineHeight);
225 this._context.fillStyle = "black";
226 this._context.fillText(label, labelX - labelPadding, labelY + lineHeight - baselineHeight);
227 labelTopMargin = labelY + lineHeight;
229 this._context.strokeStyle = "rgba(60, 60, 60, 0.4)";
230 this._context.stroke();
231 this._context.restore();
235 * @param {number} left
236 * @param {number} width
237 * @param {number} windowHeight
238 * @param {!WebInspector.TimelineFrame} frame
239 * @param {number} scale
241 _renderBar: function(left, width, windowHeight, frame, scale)
243 var categories = Object.keys(WebInspector.TimelineUIUtils.categories());
244 var x = Math.floor(left) + 0.5;
245 width = Math.floor(width);
247 var totalCPUTime = frame.cpuTime;
248 var normalizedScale = scale;
249 if (totalCPUTime > frame.duration)
250 normalizedScale *= frame.duration / totalCPUTime;
252 for (var i = 0, bottomOffset = windowHeight; i < categories.length; ++i) {
253 var category = categories[i];
254 var duration = frame.timeByCategory[category];
257 var height = Math.round(duration * normalizedScale);
258 var y = Math.floor(bottomOffset - height) + 0.5;
260 this._context.save();
261 this._context.translate(x, 0);
262 this._context.scale(width / this._maxInnerBarWidth, 1);
263 this._context.fillStyle = this._fillStyles[category];
264 this._context.fillRect(0, y, this._maxInnerBarWidth, Math.floor(height));
265 this._context.strokeStyle = WebInspector.TimelineUIUtils.categories()[category].borderColor;
266 this._context.beginPath();
267 this._context.moveTo(0, y);
268 this._context.lineTo(this._maxInnerBarWidth, y);
269 this._context.stroke();
270 this._context.restore();
272 bottomOffset -= height;
274 // Draw a contour for the total frame time.
275 var y0 = Math.floor(windowHeight - frame.duration * scale) + 0.5;
276 var y1 = windowHeight + 0.5;
278 this._context.strokeStyle = "rgba(90, 90, 90, 0.2)";
279 this._context.beginPath();
280 this._context.moveTo(x, y1);
281 this._context.lineTo(x, y0);
282 this._context.lineTo(x + width, y0);
283 this._context.lineTo(x + width, y1);
284 this._context.stroke();
288 * @param {number} windowLeft
289 * @param {number} windowRight
290 * @return {!{startTime: number, endTime: number}}
292 windowTimes: function(windowLeft, windowRight)
294 if (!this._barTimes.length)
295 return WebInspector.TimelineOverviewBase.prototype.windowTimes.call(this, windowLeft, windowRight);
296 var windowSpan = this._canvas.width;
297 var leftOffset = windowLeft * windowSpan;
298 var rightOffset = windowRight * windowSpan;
299 var firstBar = Math.floor(Math.max(leftOffset - this._outerPadding + this._actualPadding, 0) / this._actualOuterBarWidth);
300 var lastBar = Math.min(Math.floor(Math.max(rightOffset - this._outerPadding, 0)/ this._actualOuterBarWidth), this._barTimes.length - 1);
301 if (firstBar >= this._barTimes.length)
302 return {startTime: Infinity, endTime: Infinity};
304 const snapTolerancePixels = 3;
306 startTime: leftOffset > snapTolerancePixels ? this._barTimes[firstBar].startTime : this._model.minimumRecordTime(),
307 endTime: (rightOffset + snapTolerancePixels > windowSpan) || (lastBar >= this._barTimes.length) ? this._model.maximumRecordTime() : this._barTimes[lastBar].endTime
312 * @param {number} startTime
313 * @param {number} endTime
314 * @return {!{left: number, right: number}}
316 windowBoundaries: function(startTime, endTime)
318 if (this._barTimes.length === 0)
319 return {left: 0, right: 1};
321 * @param {number} time
322 * @param {!{startTime:number, endTime:number}} barTime
325 function barStartComparator(time, barTime)
327 return time - barTime.startTime;
330 * @param {number} time
331 * @param {!{startTime:number, endTime:number}} barTime
334 function barEndComparator(time, barTime)
336 // We need a frame where time is in [barTime.startTime, barTime.endTime), so exclude exact matches against endTime.
337 if (time === barTime.endTime)
339 return time - barTime.endTime;
342 left: this._windowBoundaryFromTime(startTime, barEndComparator),
343 right: this._windowBoundaryFromTime(endTime, barStartComparator)
348 * @param {number} time
349 * @param {function(number, !{startTime:number, endTime:number}):number} comparator
351 _windowBoundaryFromTime: function(time, comparator)
353 if (time === Infinity)
355 var index = this._firstBarAfter(time, comparator);
358 return (this._barNumberToScreenPosition(index) - this._actualPadding / 2) / this._canvas.width;
362 * @param {number} time
363 * @param {function(number, {startTime:number, endTime:number}):number} comparator
365 _firstBarAfter: function(time, comparator)
367 return insertionIndexForObjectInListSortedByFunction(time, this._barTimes, comparator);
370 __proto__: WebInspector.TimelineOverviewBase.prototype