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.TimelineFrameModel} 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)
140 for (var i = 0; i < frames.length; ++i) {
142 durations.push(frames[i].duration);
144 var medianFrameLength = durations.qselect(Math.floor(durations.length / 2));
146 // Optimize appearance for 30fps, but leave some space so it's evident when a frame overflows.
147 // However, if at least half frames won't fit at this scale, fall back to using autoscale.
148 const targetFPS = 20;
149 var result = 1000.0 / targetFPS;
150 if (result >= medianFrameLength)
153 var maxFrameLength = Math.max.apply(Math, durations);
154 return Math.min(medianFrameLength * 2, maxFrameLength);
158 * @param {!Array.<!WebInspector.TimelineFrame>} frames
159 * @param {number} scale
160 * @param {number} windowHeight
162 _renderBars: function(frames, scale, windowHeight)
164 const maxPadding = 5 * window.devicePixelRatio;
165 this._actualOuterBarWidth = Math.min((this._canvas.width - 2 * this._outerPadding) / frames.length, this._maxInnerBarWidth + maxPadding);
166 this._actualPadding = Math.min(Math.floor(this._actualOuterBarWidth / 3), maxPadding);
168 var barWidth = this._actualOuterBarWidth - this._actualPadding;
169 for (var i = 0; i < frames.length; ++i) {
171 this._renderBar(this._barNumberToScreenPosition(i), barWidth, windowHeight, frames[i], scale);
178 _barNumberToScreenPosition: function(n)
180 return this._outerPadding + this._actualOuterBarWidth * n;
184 * @param {number} scale
185 * @param {number} height
187 _drawFPSMarks: function(scale, height)
189 const fpsMarks = [30, 60];
191 this._context.save();
192 this._context.beginPath();
193 this._context.font = (10 * window.devicePixelRatio) + "px " + window.getComputedStyle(this.element, null).getPropertyValue("font-family");
194 this._context.textAlign = "right";
195 this._context.textBaseline = "alphabetic";
197 const labelPadding = 4 * window.devicePixelRatio;
198 const baselineHeight = 3 * window.devicePixelRatio;
199 var lineHeight = 12 * window.devicePixelRatio;
200 var labelTopMargin = 0;
201 var labelOffsetY = 0; // Labels are going to be under their grid lines.
203 for (var i = 0; i < fpsMarks.length; ++i) {
204 var fps = fpsMarks[i];
205 // Draw lines one pixel above they need to be, so 60pfs line does not cross most of the frames tops.
206 var y = height - Math.floor(1000.0 / fps * scale) - 0.5;
207 var label = WebInspector.UIString("%d\u2009fps", fps);
208 var labelWidth = this._context.measureText(label).width + 2 * labelPadding;
209 var labelX = this._canvas.width;
211 if (!i && labelTopMargin < y - lineHeight)
212 labelOffsetY = -lineHeight; // Labels are going to be over their grid lines.
213 var labelY = y + labelOffsetY;
214 if (labelY < labelTopMargin || labelY + lineHeight > height)
215 break; // No space for the label, so no line as well.
217 this._context.moveTo(0, y);
218 this._context.lineTo(this._canvas.width, y);
220 this._context.fillStyle = "rgba(255, 255, 255, 0.5)";
221 this._context.fillRect(labelX - labelWidth, labelY, labelWidth, lineHeight);
222 this._context.fillStyle = "black";
223 this._context.fillText(label, labelX - labelPadding, labelY + lineHeight - baselineHeight);
224 labelTopMargin = labelY + lineHeight;
226 this._context.strokeStyle = "rgba(60, 60, 60, 0.4)";
227 this._context.stroke();
228 this._context.restore();
232 * @param {number} left
233 * @param {number} width
234 * @param {number} windowHeight
235 * @param {!WebInspector.TimelineFrame} frame
236 * @param {number} scale
238 _renderBar: function(left, width, windowHeight, frame, scale)
240 var categories = Object.keys(WebInspector.TimelineUIUtils.categories());
241 var x = Math.floor(left) + 0.5;
242 width = Math.floor(width);
244 var totalCPUTime = frame.cpuTime;
245 var normalizedScale = scale;
246 if (totalCPUTime > frame.duration)
247 normalizedScale *= frame.duration / totalCPUTime;
249 for (var i = 0, bottomOffset = windowHeight; i < categories.length; ++i) {
250 var category = categories[i];
251 var duration = frame.timeByCategory[category];
254 var height = Math.round(duration * normalizedScale);
255 var y = Math.floor(bottomOffset - height) + 0.5;
257 this._context.save();
258 this._context.translate(x, 0);
259 this._context.scale(width / this._maxInnerBarWidth, 1);
260 this._context.fillStyle = this._fillStyles[category];
261 this._context.fillRect(0, y, this._maxInnerBarWidth, Math.floor(height));
262 this._context.strokeStyle = WebInspector.TimelineUIUtils.categories()[category].borderColor;
263 this._context.beginPath();
264 this._context.moveTo(0, y);
265 this._context.lineTo(this._maxInnerBarWidth, y);
266 this._context.stroke();
267 this._context.restore();
269 bottomOffset -= height;
271 // Draw a contour for the total frame time.
272 var y0 = Math.floor(windowHeight - frame.duration * scale) + 0.5;
273 var y1 = windowHeight + 0.5;
275 this._context.strokeStyle = "rgba(90, 90, 90, 0.3)";
276 this._context.beginPath();
277 this._context.moveTo(x, y1);
278 this._context.lineTo(x, y0);
279 this._context.lineTo(x + width, y0);
280 this._context.lineTo(x + width, y1);
281 this._context.stroke();
285 * @param {number} windowLeft
286 * @param {number} windowRight
287 * @return {!{startTime: number, endTime: number}}
289 windowTimes: function(windowLeft, windowRight)
291 if (!this._barTimes.length)
292 return WebInspector.TimelineOverviewBase.prototype.windowTimes.call(this, windowLeft, windowRight);
293 var windowSpan = this._canvas.width;
294 var leftOffset = windowLeft * windowSpan;
295 var rightOffset = windowRight * windowSpan;
296 var firstBar = Math.floor(Math.max(leftOffset - this._outerPadding + this._actualPadding, 0) / this._actualOuterBarWidth);
297 var lastBar = Math.min(Math.floor(Math.max(rightOffset - this._outerPadding, 0)/ this._actualOuterBarWidth), this._barTimes.length - 1);
298 if (firstBar >= this._barTimes.length)
299 return {startTime: Infinity, endTime: Infinity};
301 const snapTolerancePixels = 3;
303 startTime: leftOffset > snapTolerancePixels ? this._barTimes[firstBar].startTime : this._model.minimumRecordTime(),
304 endTime: (rightOffset + snapTolerancePixels > windowSpan) || (lastBar >= this._barTimes.length) ? this._model.maximumRecordTime() : this._barTimes[lastBar].endTime
309 * @param {number} startTime
310 * @param {number} endTime
311 * @return {!{left: number, right: number}}
313 windowBoundaries: function(startTime, endTime)
315 if (this._barTimes.length === 0)
316 return {left: 0, right: 1};
318 * @param {number} time
319 * @param {!{startTime:number, endTime:number}} barTime
322 function barStartComparator(time, barTime)
324 return time - barTime.startTime;
327 * @param {number} time
328 * @param {!{startTime:number, endTime:number}} barTime
331 function barEndComparator(time, barTime)
333 // We need a frame where time is in [barTime.startTime, barTime.endTime), so exclude exact matches against endTime.
334 if (time === barTime.endTime)
336 return time - barTime.endTime;
339 left: this._windowBoundaryFromTime(startTime, barEndComparator),
340 right: this._windowBoundaryFromTime(endTime, barStartComparator)
345 * @param {number} time
346 * @param {function(number, !{startTime:number, endTime:number}):number} comparator
348 _windowBoundaryFromTime: function(time, comparator)
350 if (time === Infinity)
352 var index = this._firstBarAfter(time, comparator);
355 return (this._barNumberToScreenPosition(index) - this._actualPadding / 2) / this._canvas.width;
359 * @param {number} time
360 * @param {function(number, {startTime:number, endTime:number}):number} comparator
362 _firstBarAfter: function(time, comparator)
364 return insertionIndexForObjectInListSortedByFunction(time, this._barTimes, comparator);
367 __proto__: WebInspector.TimelineOverviewBase.prototype