Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 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 |
| 13 | * distribution. |
| 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. |
| 17 | * |
| 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. |
| 29 | */ |
| 30 | |
Jan Scheffler | 0aecd3a | 2020-07-30 07:37:13 | [diff] [blame] | 31 | // @ts-nocheck |
| 32 | // TODO(crbug.com/1011811): Enable TypeScript compiler checks |
| 33 | |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 34 | import * as Common from '../common/common.js'; |
| 35 | import * as Host from '../host/host.js'; |
Jack Franklin | 1be909c | 2020-03-04 08:57:41 | [diff] [blame] | 36 | import * as Platform from '../platform/platform.js'; |
Paul Lewis | ead4575 | 2020-06-23 09:51:36 | [diff] [blame] | 37 | import * as SDK from '../sdk/sdk.js'; // eslint-disable-line no-unused-vars |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame^] | 38 | import * as ThemeSupport from '../theme_support/theme_support.js'; |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 39 | import * as UI from '../ui/ui.js'; |
| 40 | |
Paul Lewis | 2cfa94e | 2020-01-13 10:30:15 | [diff] [blame] | 41 | import {ChartViewport, ChartViewportDelegate} from './ChartViewport.js'; // eslint-disable-line no-unused-vars |
| 42 | import {Calculator, TimelineGrid} from './TimelineGrid.js'; // eslint-disable-line no-unused-vars |
| 43 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | /** |
| 45 | * @interface |
| 46 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 47 | export class FlameChartDelegate { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 48 | /** |
| 49 | * @param {number} startTime |
| 50 | * @param {number} endTime |
| 51 | * @param {boolean} animate |
| 52 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 53 | windowChanged(startTime, endTime, animate) { |
| 54 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * @param {number} startTime |
| 58 | * @param {number} endTime |
| 59 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 60 | updateRangeSelection(startTime, endTime) { |
| 61 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | |
| 63 | /** |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 64 | * @param {!FlameChart} flameChart |
Tim van der Lippe | 8ef250c | 2020-02-20 16:29:25 | [diff] [blame] | 65 | * @param {?Group} group |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 66 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 67 | updateSelectedGroup(flameChart, group) { |
| 68 | } |
| 69 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * @unrestricted |
Paul Lewis | 2cfa94e | 2020-01-13 10:30:15 | [diff] [blame] | 73 | * @implements {Calculator} |
| 74 | * @implements {ChartViewportDelegate} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | */ |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 76 | export class FlameChart extends UI.Widget.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | /** |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 78 | * @param {!FlameChartDataProvider} dataProvider |
| 79 | * @param {!FlameChartDelegate} flameChartDelegate |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 80 | * @param {!Common.Settings.Setting=} groupExpansionSetting |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | */ |
| 82 | constructor(dataProvider, flameChartDelegate, groupExpansionSetting) { |
| 83 | super(true); |
| 84 | this.registerRequiredCSS('perf_ui/flameChart.css'); |
| 85 | this.contentElement.classList.add('flame-chart-main-pane'); |
| 86 | this._groupExpansionSetting = groupExpansionSetting; |
| 87 | this._groupExpansionState = groupExpansionSetting && groupExpansionSetting.get() || {}; |
| 88 | this._flameChartDelegate = flameChartDelegate; |
| 89 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 90 | this._useWebGL = Root.Runtime.experiments.isEnabled('timelineWebGL'); |
Paul Lewis | 2cfa94e | 2020-01-13 10:30:15 | [diff] [blame] | 91 | this._chartViewport = new ChartViewport(this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 92 | this._chartViewport.show(this.contentElement); |
| 93 | |
| 94 | this._dataProvider = dataProvider; |
| 95 | |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 96 | this._candyStripeCanvas = /** @type {!HTMLCanvasElement} */ (document.createElement('canvas')); |
| 97 | this._createCandyStripePattern(); |
| 98 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 99 | this._viewportElement = this._chartViewport.viewportElement; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 100 | if (this._useWebGL) { |
| 101 | this._canvasGL = /** @type {!HTMLCanvasElement} */ (this._viewportElement.createChild('canvas', 'fill')); |
| 102 | this._initWebGL(); |
| 103 | } |
| 104 | this._canvas = /** @type {!HTMLCanvasElement} */ (this._viewportElement.createChild('canvas', 'fill')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 105 | |
Joel Einbinder | 83fc76e | 2018-06-11 23:19:47 | [diff] [blame] | 106 | this._canvas.tabIndex = 0; |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 107 | UI.ARIAUtils.setAccessibleName(this._canvas, ls`Flame Chart`); |
| 108 | UI.ARIAUtils.markAsTree(this._canvas); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 109 | this.setDefaultFocusedElement(this._canvas); |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 110 | this._canvas.classList.add('flame-chart-canvas'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 111 | this._canvas.addEventListener('mousemove', this._onMouseMove.bind(this), false); |
| 112 | this._canvas.addEventListener('mouseout', this._onMouseOut.bind(this), false); |
| 113 | this._canvas.addEventListener('click', this._onClick.bind(this), false); |
| 114 | this._canvas.addEventListener('keydown', this._onKeyDown.bind(this), false); |
| 115 | |
| 116 | this._entryInfo = this._viewportElement.createChild('div', 'flame-chart-entry-info'); |
| 117 | this._markerHighlighElement = this._viewportElement.createChild('div', 'flame-chart-marker-highlight-element'); |
| 118 | this._highlightElement = this._viewportElement.createChild('div', 'flame-chart-highlight-element'); |
| 119 | this._selectedElement = this._viewportElement.createChild('div', 'flame-chart-selected-element'); |
Michael Liao | 712bbc2 | 2019-10-15 19:21:51 | [diff] [blame] | 120 | this._canvas.addEventListener('focus', () => { |
| 121 | this._selectedElement.classList.remove('flame-chart-unfocused-selected-element'); |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 122 | this.dispatchEventToListeners(Events.CanvasFocused); |
Michael Liao | 712bbc2 | 2019-10-15 19:21:51 | [diff] [blame] | 123 | }, false); |
| 124 | this._canvas.addEventListener('blur', () => { |
| 125 | this._selectedElement.classList.add('flame-chart-unfocused-selected-element'); |
| 126 | }, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 127 | |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 128 | UI.UIUtils.installDragHandle( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 129 | this._viewportElement, this._startDragging.bind(this), this._dragging.bind(this), this._endDragging.bind(this), |
| 130 | null); |
| 131 | |
| 132 | this._rulerEnabled = true; |
| 133 | this._rangeSelectionStart = 0; |
| 134 | this._rangeSelectionEnd = 0; |
| 135 | this._barHeight = 17; |
| 136 | this._textBaseline = 5; |
| 137 | this._textPadding = 5; |
| 138 | this._markerRadius = 6; |
| 139 | this._chartViewport.setWindowTimes( |
| 140 | dataProvider.minimumBoundary(), dataProvider.minimumBoundary() + dataProvider.totalTime()); |
| 141 | |
| 142 | /** @const */ |
| 143 | this._headerLeftPadding = 6; |
| 144 | /** @const */ |
| 145 | this._arrowSide = 8; |
| 146 | /** @const */ |
| 147 | this._expansionArrowIndent = this._headerLeftPadding + this._arrowSide / 2; |
| 148 | /** @const */ |
| 149 | this._headerLabelXPadding = 3; |
| 150 | /** @const */ |
| 151 | this._headerLabelYPadding = 2; |
| 152 | |
| 153 | this._highlightedMarkerIndex = -1; |
| 154 | this._highlightedEntryIndex = -1; |
| 155 | this._selectedEntryIndex = -1; |
| 156 | this._rawTimelineDataLength = 0; |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 157 | /** @type {!Map<string, !Map<string,number>>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 158 | this._textWidth = new Map(); |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 159 | /** @type {!Map<number, !{x: number, width: number}>} */ |
| 160 | this._markerPositions = new Map(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 161 | |
| 162 | this._lastMouseOffsetX = 0; |
| 163 | this._selectedGroup = -1; |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 164 | |
| 165 | // Keyboard focused group is used to navigate groups irrespective of whether they are selectable or not |
| 166 | this._keyboardFocusedGroup = -1; |
| 167 | |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame^] | 168 | this._selectedGroupBackroundColor = ThemeSupport.ThemeSupport.instance().patchColorText( |
| 169 | Colors.SelectedGroupBackground, ThemeSupport.ThemeSupport.ColorUsage.Background); |
| 170 | this._selectedGroupBorderColor = ThemeSupport.ThemeSupport.instance().patchColorText( |
| 171 | Colors.SelectedGroupBorder, ThemeSupport.ThemeSupport.ColorUsage.Background); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @override |
| 176 | */ |
| 177 | willHide() { |
| 178 | this.hideHighlight(); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @param {number} value |
| 183 | */ |
| 184 | setBarHeight(value) { |
| 185 | this._barHeight = value; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @param {number} value |
| 190 | */ |
| 191 | setTextBaseline(value) { |
| 192 | this._textBaseline = value; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @param {number} value |
| 197 | */ |
| 198 | setTextPadding(value) { |
| 199 | this._textPadding = value; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param {boolean} enable |
| 204 | */ |
| 205 | enableRuler(enable) { |
| 206 | this._rulerEnabled = enable; |
| 207 | } |
| 208 | |
| 209 | alwaysShowVerticalScroll() { |
| 210 | this._chartViewport.alwaysShowVerticalScroll(); |
| 211 | } |
| 212 | |
| 213 | disableRangeSelection() { |
| 214 | this._chartViewport.disableRangeSelection(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @param {number} entryIndex |
| 219 | */ |
| 220 | highlightEntry(entryIndex) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 221 | if (this._highlightedEntryIndex === entryIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 222 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 223 | } |
| 224 | if (!this._dataProvider.entryColor(entryIndex)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 225 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 226 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 227 | this._highlightedEntryIndex = entryIndex; |
| 228 | this._updateElementPosition(this._highlightElement, this._highlightedEntryIndex); |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 229 | this.dispatchEventToListeners(Events.EntryHighlighted, entryIndex); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | hideHighlight() { |
| 233 | this._entryInfo.removeChildren(); |
| 234 | this._highlightedEntryIndex = -1; |
| 235 | this._updateElementPosition(this._highlightElement, this._highlightedEntryIndex); |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 236 | this.dispatchEventToListeners(Events.EntryHighlighted, -1); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 237 | } |
| 238 | |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 239 | _createCandyStripePattern() { |
| 240 | // Set the candy stripe pattern to 17px so it repeats well. |
| 241 | const size = 17; |
| 242 | this._candyStripeCanvas.width = size; |
| 243 | this._candyStripeCanvas.height = size; |
| 244 | |
| 245 | const ctx = this._candyStripeCanvas.getContext('2d'); |
| 246 | |
| 247 | // Rotate the stripe by 45deg to the right. |
| 248 | ctx.translate(size * 0.5, size * 0.5); |
| 249 | ctx.rotate(Math.PI * 0.25); |
| 250 | ctx.translate(-size * 0.5, -size * 0.5); |
| 251 | |
| 252 | ctx.fillStyle = 'rgba(255, 0, 0, 0.4)'; |
| 253 | for (let x = -size; x < size * 2; x += 3) { |
| 254 | ctx.fillRect(x, -size, 1, size * 3); |
| 255 | } |
| 256 | } |
| 257 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 258 | _resetCanvas() { |
| 259 | const ratio = window.devicePixelRatio; |
| 260 | const width = Math.round(this._offsetWidth * ratio); |
| 261 | const height = Math.round(this._offsetHeight * ratio); |
| 262 | this._canvas.width = width; |
| 263 | this._canvas.height = height; |
| 264 | this._canvas.style.width = `${width / ratio}px`; |
| 265 | this._canvas.style.height = `${height / ratio}px`; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 266 | if (this._useWebGL) { |
| 267 | this._canvasGL.width = width; |
| 268 | this._canvasGL.height = height; |
| 269 | this._canvasGL.style.width = `${width / ratio}px`; |
| 270 | this._canvasGL.style.height = `${height / ratio}px`; |
| 271 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @override |
| 276 | * @param {number} startTime |
| 277 | * @param {number} endTime |
| 278 | * @param {boolean} animate |
| 279 | */ |
Alexei Filippov | 2578eb0 | 2018-04-11 08:15:05 | [diff] [blame] | 280 | windowChanged(startTime, endTime, animate) { |
| 281 | this._flameChartDelegate.windowChanged(startTime, endTime, animate); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * @override |
| 286 | * @param {number} startTime |
| 287 | * @param {number} endTime |
| 288 | */ |
| 289 | updateRangeSelection(startTime, endTime) { |
| 290 | this._flameChartDelegate.updateRangeSelection(startTime, endTime); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @override |
| 295 | * @param {number} width |
| 296 | * @param {number} height |
| 297 | */ |
| 298 | setSize(width, height) { |
| 299 | this._offsetWidth = width; |
| 300 | this._offsetHeight = height; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @param {!MouseEvent} event |
| 305 | */ |
| 306 | _startDragging(event) { |
| 307 | this.hideHighlight(); |
| 308 | this._maxDragOffset = 0; |
| 309 | this._dragStartX = event.pageX; |
| 310 | this._dragStartY = event.pageY; |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @param {!MouseEvent} event |
| 316 | */ |
| 317 | _dragging(event) { |
| 318 | const dx = event.pageX - this._dragStartX; |
| 319 | const dy = event.pageY - this._dragStartY; |
| 320 | this._maxDragOffset = Math.max(this._maxDragOffset, Math.sqrt(dx * dx + dy * dy)); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * @param {!MouseEvent} event |
| 325 | */ |
| 326 | _endDragging(event) { |
| 327 | this._updateHighlight(); |
| 328 | } |
| 329 | |
| 330 | /** |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 331 | * @return {?TimelineData} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 332 | */ |
| 333 | _timelineData() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 334 | if (!this._dataProvider) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 336 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 337 | const timelineData = this._dataProvider.timelineData(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 338 | if (timelineData !== this._rawTimelineData || timelineData.entryStartTimes.length !== this._rawTimelineDataLength) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 339 | this._processTimelineData(timelineData); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 340 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 341 | return this._rawTimelineData; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * @param {number} entryIndex |
| 346 | */ |
| 347 | _revealEntry(entryIndex) { |
| 348 | const timelineData = this._timelineData(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 349 | if (!timelineData) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 350 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 351 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 352 | const timeLeft = this._chartViewport.windowLeftTime(); |
| 353 | const timeRight = this._chartViewport.windowRightTime(); |
| 354 | const entryStartTime = timelineData.entryStartTimes[entryIndex]; |
| 355 | const entryTotalTime = timelineData.entryTotalTimes[entryIndex]; |
| 356 | const entryEndTime = entryStartTime + entryTotalTime; |
| 357 | let minEntryTimeWindow = Math.min(entryTotalTime, timeRight - timeLeft); |
| 358 | |
| 359 | const level = timelineData.entryLevels[entryIndex]; |
| 360 | this._chartViewport.setScrollOffset(this._levelToOffset(level), this._levelHeight(level)); |
| 361 | |
| 362 | const minVisibleWidthPx = 30; |
| 363 | const futurePixelToTime = (timeRight - timeLeft) / this._offsetWidth; |
| 364 | minEntryTimeWindow = Math.max(minEntryTimeWindow, futurePixelToTime * minVisibleWidthPx); |
| 365 | if (timeLeft > entryEndTime) { |
| 366 | const delta = timeLeft - entryEndTime + minEntryTimeWindow; |
Alexei Filippov | 2578eb0 | 2018-04-11 08:15:05 | [diff] [blame] | 367 | this.windowChanged(timeLeft - delta, timeRight - delta, /* animate */ true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 368 | } else if (timeRight < entryStartTime) { |
| 369 | const delta = entryStartTime - timeRight + minEntryTimeWindow; |
Alexei Filippov | 2578eb0 | 2018-04-11 08:15:05 | [diff] [blame] | 370 | this.windowChanged(timeLeft + delta, timeRight + delta, /* animate */ true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * @param {number} startTime |
| 376 | * @param {number} endTime |
| 377 | * @param {boolean=} animate |
| 378 | */ |
| 379 | setWindowTimes(startTime, endTime, animate) { |
| 380 | this._chartViewport.setWindowTimes(startTime, endTime, animate); |
| 381 | this._updateHighlight(); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * @param {!Event} event |
| 386 | */ |
| 387 | _onMouseMove(event) { |
| 388 | this._lastMouseOffsetX = event.offsetX; |
| 389 | this._lastMouseOffsetY = event.offsetY; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 390 | if (!this._enabled()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 391 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 392 | } |
| 393 | if (this._chartViewport.isDragging()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 394 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 395 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 396 | if (this._coordinatesToGroupIndex(event.offsetX, event.offsetY, true /* headerOnly */) >= 0) { |
| 397 | this.hideHighlight(); |
| 398 | this._viewportElement.style.cursor = 'pointer'; |
| 399 | return; |
| 400 | } |
| 401 | this._updateHighlight(); |
| 402 | } |
| 403 | |
| 404 | _updateHighlight() { |
Alexei Filippov | 8ee6638 | 2018-11-30 01:53:56 | [diff] [blame] | 405 | const entryIndex = this._coordinatesToEntryIndex(this._lastMouseOffsetX, this._lastMouseOffsetY); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 406 | if (entryIndex === -1) { |
| 407 | this.hideHighlight(); |
| 408 | const group = |
| 409 | this._coordinatesToGroupIndex(this._lastMouseOffsetX, this._lastMouseOffsetY, false /* headerOnly */); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 410 | if (group >= 0 && this._rawTimelineData.groups[group].selectable) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 411 | this._viewportElement.style.cursor = 'pointer'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 412 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 413 | this._viewportElement.style.cursor = 'default'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 414 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 415 | return; |
| 416 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 417 | if (this._chartViewport.isDragging()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 418 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 419 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 420 | this._updatePopover(entryIndex); |
| 421 | this._viewportElement.style.cursor = this._dataProvider.canJumpToEntry(entryIndex) ? 'pointer' : 'default'; |
| 422 | this.highlightEntry(entryIndex); |
| 423 | } |
| 424 | |
| 425 | _onMouseOut() { |
| 426 | this._lastMouseOffsetX = -1; |
| 427 | this._lastMouseOffsetY = -1; |
| 428 | this.hideHighlight(); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * @param {number} entryIndex |
| 433 | */ |
| 434 | _updatePopover(entryIndex) { |
| 435 | if (entryIndex === this._highlightedEntryIndex) { |
| 436 | this._updatePopoverOffset(); |
| 437 | return; |
| 438 | } |
| 439 | this._entryInfo.removeChildren(); |
| 440 | const popoverElement = this._dataProvider.prepareHighlightedEntryInfo(entryIndex); |
| 441 | if (popoverElement) { |
| 442 | this._entryInfo.appendChild(popoverElement); |
| 443 | this._updatePopoverOffset(); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | _updatePopoverOffset() { |
| 448 | const mouseX = this._lastMouseOffsetX; |
| 449 | const mouseY = this._lastMouseOffsetY; |
| 450 | const parentWidth = this._entryInfo.parentElement.clientWidth; |
| 451 | const parentHeight = this._entryInfo.parentElement.clientHeight; |
| 452 | const infoWidth = this._entryInfo.clientWidth; |
| 453 | const infoHeight = this._entryInfo.clientHeight; |
| 454 | const /** @const */ offsetX = 10; |
| 455 | const /** @const */ offsetY = 6; |
| 456 | let x; |
| 457 | let y; |
| 458 | for (let quadrant = 0; quadrant < 4; ++quadrant) { |
| 459 | const dx = quadrant & 2 ? -offsetX - infoWidth : offsetX; |
| 460 | const dy = quadrant & 1 ? -offsetY - infoHeight : offsetY; |
Jack Franklin | 1be909c | 2020-03-04 08:57:41 | [diff] [blame] | 461 | x = Platform.NumberUtilities.clamp(mouseX + dx, 0, parentWidth - infoWidth); |
| 462 | y = Platform.NumberUtilities.clamp(mouseY + dy, 0, parentHeight - infoHeight); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 463 | if (x >= mouseX || mouseX >= x + infoWidth || y >= mouseY || mouseY >= y + infoHeight) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 464 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 465 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 466 | } |
| 467 | this._entryInfo.style.left = x + 'px'; |
| 468 | this._entryInfo.style.top = y + 'px'; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * @param {!Event} event |
| 473 | */ |
| 474 | _onClick(event) { |
| 475 | this.focus(); |
| 476 | // onClick comes after dragStart and dragEnd events. |
| 477 | // So if there was drag (mouse move) in the middle of that events |
| 478 | // we skip the click. Otherwise we jump to the sources. |
| 479 | const /** @const */ clickThreshold = 5; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 480 | if (this._maxDragOffset > clickThreshold) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 481 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 482 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 483 | |
| 484 | this._selectGroup(this._coordinatesToGroupIndex(event.offsetX, event.offsetY, false /* headerOnly */)); |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 485 | this._toggleGroupExpand(this._coordinatesToGroupIndex(event.offsetX, event.offsetY, true /* headerOnly */)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 486 | const timelineData = this._timelineData(); |
| 487 | if (event.shiftKey && this._highlightedEntryIndex !== -1 && timelineData) { |
| 488 | const start = timelineData.entryStartTimes[this._highlightedEntryIndex]; |
| 489 | const end = start + timelineData.entryTotalTimes[this._highlightedEntryIndex]; |
| 490 | this._chartViewport.setRangeSelection(start, end); |
| 491 | } else { |
| 492 | this._chartViewport.onClick(event); |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 493 | this.dispatchEventToListeners(Events.EntryInvoked, this._highlightedEntryIndex); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * @param {number} groupIndex |
| 499 | */ |
| 500 | _selectGroup(groupIndex) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 501 | if (groupIndex < 0 || this._selectedGroup === groupIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 502 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 503 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 504 | const groups = this._rawTimelineData.groups; |
| 505 | this._keyboardFocusedGroup = groupIndex; |
Anubha Mathur | 430a80f | 2019-12-12 00:26:11 | [diff] [blame] | 506 | this._scrollGroupIntoView(groupIndex); |
Michael Liao | 0bad0c3 | 2020-01-02 18:51:04 | [diff] [blame] | 507 | const groupName = groups[groupIndex].name; |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 508 | if (!groups[groupIndex].selectable) { |
| 509 | this._deselectAllGroups(); |
Michael Liao | 0bad0c3 | 2020-01-02 18:51:04 | [diff] [blame] | 510 | UI.ARIAUtils.alert(ls`${groupName} hovered`, this._canvas); |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 511 | } else { |
| 512 | this._selectedGroup = groupIndex; |
| 513 | this._flameChartDelegate.updateSelectedGroup(this, groups[groupIndex]); |
| 514 | this._resetCanvas(); |
| 515 | this._draw(); |
Michael Liao (WPT) | b54514b | 2019-08-16 23:11:53 | [diff] [blame] | 516 | UI.ARIAUtils.alert(ls`${groupName} selected`, this._canvas); |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 517 | } |
| 518 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 519 | |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 520 | _deselectAllGroups() { |
| 521 | this._selectedGroup = -1; |
| 522 | this._flameChartDelegate.updateSelectedGroup(this, null); |
| 523 | this._resetCanvas(); |
| 524 | this._draw(); |
| 525 | } |
| 526 | |
| 527 | _deselectAllEntries() { |
| 528 | this._selectedEntryIndex = -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 529 | this._resetCanvas(); |
| 530 | this._draw(); |
| 531 | } |
| 532 | |
| 533 | /** |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 534 | * @param {number} index |
| 535 | */ |
| 536 | _isGroupFocused(index) { |
| 537 | return index === this._selectedGroup || index === this._keyboardFocusedGroup; |
| 538 | } |
| 539 | |
| 540 | /** |
Anubha Mathur | 430a80f | 2019-12-12 00:26:11 | [diff] [blame] | 541 | * @param {number} index |
| 542 | */ |
| 543 | _scrollGroupIntoView(index) { |
| 544 | if (index < 0) { |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | const groups = this._rawTimelineData.groups; |
| 549 | const groupOffsets = this._groupOffsets; |
| 550 | const groupTop = groupOffsets[index]; |
| 551 | |
| 552 | let nextOffset = groupOffsets[index + 1]; |
| 553 | if (index === groups.length - 1) { |
| 554 | nextOffset += groups[index].style.padding; |
| 555 | } |
| 556 | |
| 557 | // For the top group, scroll all the way to the top of the chart |
| 558 | // to accommodate the bar with time markers |
| 559 | const scrollTop = index === 0 ? 0 : groupTop; |
| 560 | |
| 561 | const scrollHeight = Math.min(nextOffset - scrollTop, this._chartViewport.chartHeight()); |
| 562 | this._chartViewport.setScrollOffset(scrollTop, scrollHeight); |
| 563 | } |
| 564 | |
| 565 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 566 | * @param {number} groupIndex |
| 567 | */ |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 568 | _toggleGroupExpand(groupIndex) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 569 | if (groupIndex < 0 || !this._isGroupCollapsible(groupIndex)) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 570 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 571 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 572 | |
| 573 | this._expandGroup(groupIndex, !this._rawTimelineData.groups[groupIndex].expanded /* setExpanded */); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * @param {number} groupIndex |
| 578 | * @param {boolean=} setExpanded |
Michael Liao (WPT) | b54514b | 2019-08-16 23:11:53 | [diff] [blame] | 579 | * @param {boolean=} propagatedExpand |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 580 | */ |
Michael Liao (WPT) | b54514b | 2019-08-16 23:11:53 | [diff] [blame] | 581 | _expandGroup(groupIndex, setExpanded = true, propagatedExpand = false) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 582 | if (groupIndex < 0 || !this._isGroupCollapsible(groupIndex)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 583 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 584 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 585 | |
| 586 | const groups = this._rawTimelineData.groups; |
| 587 | const group = groups[groupIndex]; |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 588 | group.expanded = setExpanded; |
| 589 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 590 | this._groupExpansionState[group.name] = group.expanded; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 591 | if (this._groupExpansionSetting) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 592 | this._groupExpansionSetting.set(this._groupExpansionState); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 593 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 594 | this._updateLevelPositions(); |
| 595 | |
| 596 | this._updateHighlight(); |
| 597 | if (!group.expanded) { |
| 598 | const timelineData = this._timelineData(); |
| 599 | const level = timelineData.entryLevels[this._selectedEntryIndex]; |
| 600 | if (this._selectedEntryIndex >= 0 && level >= group.startLevel && |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 601 | (groupIndex >= groups.length - 1 || groups[groupIndex + 1].startLevel > level)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 602 | this._selectedEntryIndex = -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 603 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | this._updateHeight(); |
| 607 | this._resetCanvas(); |
| 608 | this._draw(); |
Michael Liao (WPT) | b54514b | 2019-08-16 23:11:53 | [diff] [blame] | 609 | |
Anubha Mathur | 430a80f | 2019-12-12 00:26:11 | [diff] [blame] | 610 | this._scrollGroupIntoView(groupIndex); |
Michael Liao (WPT) | b54514b | 2019-08-16 23:11:53 | [diff] [blame] | 611 | // We only want to read expanded/collapsed state on user inputted expand/collapse |
| 612 | if (!propagatedExpand) { |
| 613 | const groupName = groups[groupIndex].name; |
| 614 | const content = group.expanded ? ls`${groupName} expanded` : ls`${groupName} collapsed`; |
| 615 | UI.ARIAUtils.alert(content, this._canvas); |
| 616 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** |
| 620 | * @param {!Event} e |
| 621 | */ |
| 622 | _onKeyDown(e) { |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 623 | if (!UI.KeyboardShortcut.KeyboardShortcut.hasNoModifiers(e) || !this._timelineData()) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 624 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 625 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 626 | |
| 627 | const eventHandled = this._handleSelectionNavigation(e); |
| 628 | |
| 629 | // Handle keyboard navigation in groups |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 630 | if (!eventHandled && this._rawTimelineData && this._rawTimelineData.groups) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 631 | this._handleKeyboardGroupNavigation(e); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 632 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
Ted Meyer | b0928d0 | 2020-06-23 05:12:45 | [diff] [blame] | 636 | * @param {string} eventName |
| 637 | * @param {function(!Event):undefined} onEvent |
| 638 | */ |
| 639 | bindCanvasEvent(eventName, onEvent) { |
| 640 | this._canvas.addEventListener(eventName, onEvent); |
| 641 | } |
| 642 | |
| 643 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 644 | * @param {!Event} e |
| 645 | */ |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 646 | _handleKeyboardGroupNavigation(e) { |
| 647 | let handled = false; |
| 648 | let entrySelected = false; |
| 649 | |
| 650 | if (e.code === 'ArrowUp') { |
| 651 | handled = this._selectPreviousGroup(); |
| 652 | } else if (e.code === 'ArrowDown') { |
| 653 | handled = this._selectNextGroup(); |
| 654 | } else if (e.code === 'ArrowLeft') { |
| 655 | if (this._keyboardFocusedGroup >= 0) { |
| 656 | this._expandGroup(this._keyboardFocusedGroup, false /* setExpanded */); |
| 657 | handled = true; |
| 658 | } |
| 659 | } else if (e.code === 'ArrowRight') { |
| 660 | if (this._keyboardFocusedGroup >= 0) { |
| 661 | this._expandGroup(this._keyboardFocusedGroup, true /* setExpanded */); |
| 662 | this._selectFirstChild(); |
| 663 | handled = true; |
| 664 | } |
| 665 | } else if (isEnterKey(e)) { |
| 666 | entrySelected = this._selectFirstEntryInCurrentGroup(); |
| 667 | handled = entrySelected; |
| 668 | } |
| 669 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 670 | if (handled && !entrySelected) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 671 | this._deselectAllEntries(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 672 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 673 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 674 | if (handled) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 675 | e.consume(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 676 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
| 680 | * @return {boolean} |
| 681 | */ |
| 682 | _selectFirstEntryInCurrentGroup() { |
| 683 | const allGroups = this._rawTimelineData.groups; |
| 684 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 685 | if (this._keyboardFocusedGroup < 0) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 686 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 687 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 688 | |
| 689 | const group = allGroups[this._keyboardFocusedGroup]; |
| 690 | const startLevelInGroup = group.startLevel; |
| 691 | |
| 692 | // Return if no levels in this group |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 693 | if (startLevelInGroup < 0) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 694 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 695 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 696 | |
| 697 | // Make sure this is the innermost nested group with this startLevel |
| 698 | // This is because a parent group also contains levels of all its child groups |
| 699 | // So check if the next group has the same level, if it does, user should |
| 700 | // go to that child group to select this entry |
| 701 | if (this._keyboardFocusedGroup < allGroups.length - 1 && |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 702 | allGroups[this._keyboardFocusedGroup + 1].startLevel === startLevelInGroup) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 703 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 704 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 705 | |
| 706 | |
| 707 | // Get first (default) entry in startLevel of selected group |
| 708 | const firstEntryIndex = this._timelineLevels[startLevelInGroup][0]; |
| 709 | |
| 710 | this._expandGroup(this._keyboardFocusedGroup, true /* setExpanded */); |
| 711 | this.setSelectedEntry(firstEntryIndex); |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * @return {boolean} |
| 717 | */ |
| 718 | _selectPreviousGroup() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 719 | if (this._keyboardFocusedGroup <= 0) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 720 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 721 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 722 | |
| 723 | const groupIndexToSelect = this._getGroupIndexToSelect(-1 /* offset */); |
| 724 | this._selectGroup(groupIndexToSelect); |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * @return {boolean} |
| 730 | */ |
| 731 | _selectNextGroup() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 732 | if (this._keyboardFocusedGroup >= this._rawTimelineData.groups.length - 1) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 733 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 734 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 735 | |
| 736 | const groupIndexToSelect = this._getGroupIndexToSelect(1 /* offset */); |
| 737 | this._selectGroup(groupIndexToSelect); |
| 738 | return true; |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * @param {number} offset |
| 743 | * @return {number} |
| 744 | */ |
| 745 | _getGroupIndexToSelect(offset) { |
| 746 | const allGroups = this._rawTimelineData.groups; |
| 747 | let groupIndexToSelect = this._keyboardFocusedGroup; |
| 748 | let groupName, groupWithSubNestingLevel; |
| 749 | |
| 750 | do { |
| 751 | groupIndexToSelect += offset; |
| 752 | groupName = this._rawTimelineData.groups[groupIndexToSelect].name; |
| 753 | groupWithSubNestingLevel = this._keyboardFocusedGroup !== -1 && |
| 754 | allGroups[groupIndexToSelect].style.nestingLevel > allGroups[this._keyboardFocusedGroup].style.nestingLevel; |
| 755 | } while (groupIndexToSelect > 0 && groupIndexToSelect < allGroups.length - 1 && |
| 756 | (!groupName || groupWithSubNestingLevel)); |
| 757 | |
| 758 | return groupIndexToSelect; |
| 759 | } |
| 760 | |
| 761 | _selectFirstChild() { |
| 762 | const allGroups = this._rawTimelineData.groups; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 763 | if (this._keyboardFocusedGroup < 0 || this._keyboardFocusedGroup >= allGroups.length - 1) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 764 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 765 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 766 | |
| 767 | const groupIndexToSelect = this._keyboardFocusedGroup + 1; |
| 768 | if (allGroups[groupIndexToSelect].style.nestingLevel > allGroups[this._keyboardFocusedGroup].style.nestingLevel) { |
| 769 | this._selectGroup(groupIndexToSelect); |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * @param {!Event} e |
| 775 | * @return {boolean} |
| 776 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 777 | _handleSelectionNavigation(e) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 778 | if (this._selectedEntryIndex === -1) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 779 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 780 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 781 | const timelineData = this._timelineData(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 782 | if (!timelineData) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 783 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 784 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 785 | |
| 786 | /** |
| 787 | * @param {number} time |
| 788 | * @param {number} entryIndex |
| 789 | * @return {number} |
| 790 | */ |
| 791 | function timeComparator(time, entryIndex) { |
| 792 | return time - timelineData.entryStartTimes[entryIndex]; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * @param {number} entry1 |
| 797 | * @param {number} entry2 |
| 798 | * @return {boolean} |
| 799 | */ |
| 800 | function entriesIntersect(entry1, entry2) { |
| 801 | const start1 = timelineData.entryStartTimes[entry1]; |
| 802 | const start2 = timelineData.entryStartTimes[entry2]; |
| 803 | const end1 = start1 + timelineData.entryTotalTimes[entry1]; |
| 804 | const end2 = start2 + timelineData.entryTotalTimes[entry2]; |
| 805 | return start1 < end2 && start2 < end1; |
| 806 | } |
| 807 | |
| 808 | const keys = UI.KeyboardShortcut.Keys; |
| 809 | if (e.keyCode === keys.Left.code || e.keyCode === keys.Right.code) { |
| 810 | const level = timelineData.entryLevels[this._selectedEntryIndex]; |
| 811 | const levelIndexes = this._timelineLevels[level]; |
| 812 | let indexOnLevel = levelIndexes.lowerBound(this._selectedEntryIndex); |
| 813 | indexOnLevel += e.keyCode === keys.Left.code ? -1 : 1; |
| 814 | e.consume(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 815 | if (indexOnLevel >= 0 && indexOnLevel < levelIndexes.length) { |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 816 | this.dispatchEventToListeners(Events.EntrySelected, levelIndexes[indexOnLevel]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 817 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 818 | return true; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 819 | } |
| 820 | if (e.keyCode === keys.Up.code || e.keyCode === keys.Down.code) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 821 | let level = timelineData.entryLevels[this._selectedEntryIndex]; |
| 822 | level += e.keyCode === keys.Up.code ? -1 : 1; |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 823 | if (level < 0 || level >= this._timelineLevels.length) { |
| 824 | this._deselectAllEntries(); |
| 825 | e.consume(true); |
| 826 | return true; |
| 827 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 828 | const entryTime = timelineData.entryStartTimes[this._selectedEntryIndex] + |
| 829 | timelineData.entryTotalTimes[this._selectedEntryIndex] / 2; |
| 830 | const levelIndexes = this._timelineLevels[level]; |
| 831 | let indexOnLevel = levelIndexes.upperBound(entryTime, timeComparator) - 1; |
| 832 | if (!entriesIntersect(this._selectedEntryIndex, levelIndexes[indexOnLevel])) { |
| 833 | ++indexOnLevel; |
| 834 | if (indexOnLevel >= levelIndexes.length || |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 835 | !entriesIntersect(this._selectedEntryIndex, levelIndexes[indexOnLevel])) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 836 | if (e.code === 'ArrowDown') { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 837 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 838 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 839 | |
| 840 | // Stay in the current group and give focus to the parent group instead of entries |
| 841 | this._deselectAllEntries(); |
| 842 | e.consume(true); |
| 843 | return true; |
| 844 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 845 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 846 | e.consume(true); |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 847 | this.dispatchEventToListeners(Events.EntrySelected, levelIndexes[indexOnLevel]); |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 848 | return true; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 849 | } |
Michael Liao | 712bbc2 | 2019-10-15 19:21:51 | [diff] [blame] | 850 | if (isEnterKey(e)) { |
| 851 | e.consume(true); |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 852 | this.dispatchEventToListeners(Events.EntryInvoked, this._selectedEntryIndex); |
Michael Liao | 712bbc2 | 2019-10-15 19:21:51 | [diff] [blame] | 853 | return true; |
| 854 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 855 | return false; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /** |
| 859 | * @param {number} x |
| 860 | * @param {number} y |
| 861 | * @return {number} |
| 862 | */ |
| 863 | _coordinatesToEntryIndex(x, y) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 864 | if (x < 0 || y < 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 865 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 866 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 867 | const timelineData = this._timelineData(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 868 | if (!timelineData) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 869 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 870 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 871 | y += this._chartViewport.scrollOffset(); |
| 872 | const cursorLevel = this._visibleLevelOffsets.upperBound(y) - 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 873 | if (cursorLevel < 0 || !this._visibleLevels[cursorLevel]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 874 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 875 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 876 | const offsetFromLevel = y - this._visibleLevelOffsets[cursorLevel]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 877 | if (offsetFromLevel > this._levelHeight(cursorLevel)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 878 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 879 | } |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 880 | |
| 881 | // Check markers first. |
| 882 | for (const [index, pos] of this._markerPositions) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 883 | if (timelineData.entryLevels[index] !== cursorLevel) { |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 884 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 885 | } |
| 886 | if (pos.x <= x && x < pos.x + pos.width) { |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 887 | return /** @type {number} */ (index); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 888 | } |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | // Check regular entries. |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 892 | const entryStartTimes = timelineData.entryStartTimes; |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 893 | const entriesOnLevel = this._timelineLevels[cursorLevel]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 894 | if (!entriesOnLevel || !entriesOnLevel.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 895 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 896 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 897 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 898 | const cursorTime = this._chartViewport.pixelToTime(x); |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 899 | const indexOnLevel = Math.max( |
| 900 | entriesOnLevel.upperBound(cursorTime, (time, entryIndex) => time - entryStartTimes[entryIndex]) - 1, 0); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 901 | |
| 902 | /** |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 903 | * @this {FlameChart} |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 904 | * @param {number|undefined} entryIndex |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 905 | * @return {boolean} |
| 906 | */ |
| 907 | function checkEntryHit(entryIndex) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 908 | if (entryIndex === undefined) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 909 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 910 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 911 | const startTime = entryStartTimes[entryIndex]; |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 912 | const duration = timelineData.entryTotalTimes[entryIndex]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 913 | const startX = this._chartViewport.timeToPosition(startTime); |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 914 | const endX = this._chartViewport.timeToPosition(startTime + duration); |
| 915 | const barThresholdPx = 3; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 916 | return startX - barThresholdPx < x && x < endX + barThresholdPx; |
| 917 | } |
| 918 | |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 919 | let entryIndex = entriesOnLevel[indexOnLevel]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 920 | if (checkEntryHit.call(this, entryIndex)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 921 | return entryIndex; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 922 | } |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 923 | entryIndex = entriesOnLevel[indexOnLevel + 1]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 924 | if (checkEntryHit.call(this, entryIndex)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 925 | return entryIndex; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 926 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 927 | return -1; |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * @param {number} x |
| 932 | * @param {number} y |
| 933 | * @param {boolean} headerOnly |
| 934 | * @return {number} |
| 935 | */ |
| 936 | _coordinatesToGroupIndex(x, y, headerOnly) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 937 | if (x < 0 || y < 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 938 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 939 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 940 | y += this._chartViewport.scrollOffset(); |
| 941 | const groups = this._rawTimelineData.groups || []; |
| 942 | const group = this._groupOffsets.upperBound(y) - 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 943 | if (group < 0 || group >= groups.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 944 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 945 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 946 | const height = headerOnly ? groups[group].style.height : this._groupOffsets[group + 1] - this._groupOffsets[group]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 947 | if (y - this._groupOffsets[group] >= height) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 948 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 949 | } |
| 950 | if (!headerOnly) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 951 | return group; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 952 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 953 | |
| 954 | const context = /** @type {!CanvasRenderingContext2D} */ (this._canvas.getContext('2d')); |
| 955 | context.save(); |
| 956 | context.font = groups[group].style.font; |
| 957 | const right = this._headerLeftPadding + this._labelWidthForGroup(context, groups[group]); |
| 958 | context.restore(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 959 | if (x > right) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 960 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 961 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 962 | |
| 963 | return group; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * @param {number} x |
| 968 | * @return {number} |
| 969 | */ |
| 970 | _markerIndexAtPosition(x) { |
| 971 | const markers = this._timelineData().markers; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 972 | if (!markers) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 973 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 974 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 975 | const /** @const */ accurracyOffsetPx = 4; |
| 976 | const time = this._chartViewport.pixelToTime(x); |
| 977 | const leftTime = this._chartViewport.pixelToTime(x - accurracyOffsetPx); |
| 978 | const rightTime = this._chartViewport.pixelToTime(x + accurracyOffsetPx); |
| 979 | const left = this._markerIndexBeforeTime(leftTime); |
| 980 | let markerIndex = -1; |
| 981 | let distance = Infinity; |
| 982 | for (let i = left; i < markers.length && markers[i].startTime() < rightTime; i++) { |
| 983 | const nextDistance = Math.abs(markers[i].startTime() - time); |
| 984 | if (nextDistance < distance) { |
| 985 | markerIndex = i; |
| 986 | distance = nextDistance; |
| 987 | } |
| 988 | } |
| 989 | return markerIndex; |
| 990 | } |
| 991 | |
| 992 | /** |
| 993 | * @param {number} time |
| 994 | * @return {number} |
| 995 | */ |
| 996 | _markerIndexBeforeTime(time) { |
| 997 | return this._timelineData().markers.lowerBound( |
| 998 | time, (markerTimestamp, marker) => markerTimestamp - marker.startTime()); |
| 999 | } |
| 1000 | |
| 1001 | _draw() { |
| 1002 | const timelineData = this._timelineData(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1003 | if (!timelineData) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1004 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1005 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1006 | |
| 1007 | const width = this._offsetWidth; |
| 1008 | const height = this._offsetHeight; |
| 1009 | const context = /** @type {!CanvasRenderingContext2D} */ (this._canvas.getContext('2d')); |
| 1010 | context.save(); |
| 1011 | const ratio = window.devicePixelRatio; |
| 1012 | const top = this._chartViewport.scrollOffset(); |
| 1013 | context.scale(ratio, ratio); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1014 | context.fillStyle = 'rgba(0, 0, 0, 0)'; |
| 1015 | context.fillRect(0, 0, width, height); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1016 | context.translate(0, -top); |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1017 | const defaultFont = '11px ' + Host.Platform.fontFamily(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1018 | context.font = defaultFont; |
| 1019 | |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 1020 | const candyStripePattern = context.createPattern(this._candyStripeCanvas, 'repeat'); |
| 1021 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1022 | const entryTotalTimes = timelineData.entryTotalTimes; |
| 1023 | const entryStartTimes = timelineData.entryStartTimes; |
| 1024 | const entryLevels = timelineData.entryLevels; |
| 1025 | const timeToPixel = this._chartViewport.timeToPixel(); |
| 1026 | |
| 1027 | const titleIndices = []; |
| 1028 | const markerIndices = []; |
| 1029 | const textPadding = this._textPadding; |
Mathias Bynens | 23ee1aa | 2020-03-02 12:06:38 | [diff] [blame] | 1030 | const minTextWidth = 2 * textPadding + UI.UIUtils.measureTextWidth(context, '…'); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1031 | const minTextWidthDuration = this._chartViewport.pixelToTimeOffset(minTextWidth); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1032 | const minVisibleBarLevel = Math.max(this._visibleLevelOffsets.upperBound(top) - 1, 0); |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 1033 | this._markerPositions.clear(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1034 | |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 1035 | let mainThreadTopLevel = -1; |
| 1036 | |
| 1037 | // Find the main thread so that we can mark tasks longer than 50ms. |
| 1038 | if ('groups' in timelineData && Array.isArray(timelineData.groups)) { |
| 1039 | const mainThread = timelineData.groups.find(v => { |
| 1040 | if (!v._track) { |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | return v._track.name === 'CrRendererMain'; |
| 1045 | }); |
| 1046 | |
| 1047 | if (mainThread) { |
| 1048 | mainThreadTopLevel = mainThread.startLevel; |
| 1049 | } |
| 1050 | } |
| 1051 | |
Paul Lewis | 06985d4 | 2020-04-30 14:51:16 | [diff] [blame] | 1052 | /** @type {!Map<string, {indexes: !Array<number>}>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1053 | const colorBuckets = new Map(); |
| 1054 | for (let level = minVisibleBarLevel; level < this._dataProvider.maxStackDepth(); ++level) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1055 | if (this._levelToOffset(level) > top + height) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1056 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1057 | } |
| 1058 | if (!this._visibleLevels[level]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1059 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1060 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1061 | |
| 1062 | // Entries are ordered by start time within a level, so find the last visible entry. |
| 1063 | const levelIndexes = this._timelineLevels[level]; |
| 1064 | const rightIndexOnLevel = |
| 1065 | levelIndexes.lowerBound( |
| 1066 | this._chartViewport.windowRightTime(), (time, entryIndex) => time - entryStartTimes[entryIndex]) - |
| 1067 | 1; |
| 1068 | let lastDrawOffset = Infinity; |
| 1069 | for (let entryIndexOnLevel = rightIndexOnLevel; entryIndexOnLevel >= 0; --entryIndexOnLevel) { |
| 1070 | const entryIndex = levelIndexes[entryIndexOnLevel]; |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1071 | const duration = entryTotalTimes[entryIndex]; |
| 1072 | if (isNaN(duration)) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1073 | markerIndices.push(entryIndex); |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1074 | continue; |
| 1075 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1076 | if (duration >= minTextWidthDuration || this._forceDecorationCache[entryIndex]) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1077 | titleIndices.push(entryIndex); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1078 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1079 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1080 | const entryStartTime = entryStartTimes[entryIndex]; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1081 | const entryOffsetRight = entryStartTime + duration; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1082 | if (entryOffsetRight <= this._chartViewport.windowLeftTime()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1083 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1084 | } |
| 1085 | if (this._useWebGL) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1086 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1087 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1088 | |
| 1089 | const barX = this._timeToPositionClipped(entryStartTime); |
| 1090 | // Check if the entry entirely fits into an already drawn pixel, we can just skip drawing it. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1091 | if (barX >= lastDrawOffset) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1092 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1093 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1094 | lastDrawOffset = barX; |
| 1095 | |
Alexei Filippov | e519762 | 2018-08-18 03:04:33 | [diff] [blame] | 1096 | const color = this._entryColorsCache[entryIndex]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1097 | let bucket = colorBuckets.get(color); |
| 1098 | if (!bucket) { |
Paul Lewis | 06985d4 | 2020-04-30 14:51:16 | [diff] [blame] | 1099 | bucket = {indexes: []}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1100 | colorBuckets.set(color, bucket); |
| 1101 | } |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 1102 | bucket.indexes.push(entryIndex); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1106 | if (this._useWebGL) { |
| 1107 | this._drawGL(); |
| 1108 | } else { |
| 1109 | context.save(); |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1110 | this._forEachGroupInViewport((offset, index, group, isFirst, groupHeight) => { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 1111 | if (this._isGroupFocused(index)) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1112 | context.fillStyle = this._selectedGroupBackroundColor; |
| 1113 | context.fillRect(0, offset, width, groupHeight - group.style.padding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1114 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1115 | }); |
| 1116 | context.restore(); |
| 1117 | |
Paul Lewis | 06985d4 | 2020-04-30 14:51:16 | [diff] [blame] | 1118 | for (const [color, {indexes}] of colorBuckets) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1119 | context.beginPath(); |
| 1120 | for (let i = 0; i < indexes.length; ++i) { |
| 1121 | const entryIndex = indexes[i]; |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1122 | const duration = entryTotalTimes[entryIndex]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1123 | if (isNaN(duration)) { |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1124 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1125 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1126 | const entryStartTime = entryStartTimes[entryIndex]; |
| 1127 | const barX = this._timeToPositionClipped(entryStartTime); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1128 | const barLevel = entryLevels[entryIndex]; |
| 1129 | const barHeight = this._levelHeight(barLevel); |
| 1130 | const barY = this._levelToOffset(barLevel); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1131 | const barRight = this._timeToPositionClipped(entryStartTime + duration); |
| 1132 | const barWidth = Math.max(barRight - barX, 1); |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1133 | context.rect(barX, barY, barWidth - 0.4, barHeight - 1); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1134 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1135 | context.fillStyle = color; |
| 1136 | context.fill(); |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 1137 | |
| 1138 | // Draw long task regions. |
| 1139 | context.beginPath(); |
| 1140 | for (let i = 0; i < indexes.length; ++i) { |
| 1141 | const entryIndex = indexes[i]; |
| 1142 | const duration = entryTotalTimes[entryIndex]; |
Paul Lewis | 06985d4 | 2020-04-30 14:51:16 | [diff] [blame] | 1143 | const showLongDurations = entryLevels[entryIndex] === mainThreadTopLevel; |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 1144 | |
| 1145 | if (!showLongDurations) { |
| 1146 | continue; |
| 1147 | } |
| 1148 | |
Paul Lewis | 06985d4 | 2020-04-30 14:51:16 | [diff] [blame] | 1149 | if (isNaN(duration) || duration < 50) { |
Paul Lewis | 49b1682 | 2020-02-21 14:22:03 | [diff] [blame] | 1150 | continue; |
| 1151 | } |
| 1152 | |
| 1153 | const entryStartTime = entryStartTimes[entryIndex]; |
| 1154 | const barX = this._timeToPositionClipped(entryStartTime + 50); |
| 1155 | const barLevel = entryLevels[entryIndex]; |
| 1156 | const barHeight = this._levelHeight(barLevel); |
| 1157 | const barY = this._levelToOffset(barLevel); |
| 1158 | const barRight = this._timeToPositionClipped(entryStartTime + duration); |
| 1159 | const barWidth = Math.max(barRight - barX, 1); |
| 1160 | context.rect(barX, barY, barWidth - 0.4, barHeight - 1); |
| 1161 | } |
| 1162 | |
| 1163 | context.fillStyle = candyStripePattern; |
| 1164 | context.fill(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1165 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1166 | } |
| 1167 | |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1168 | context.textBaseline = 'alphabetic'; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1169 | context.beginPath(); |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 1170 | let lastMarkerLevel = -1; |
| 1171 | let lastMarkerX = -Infinity; |
| 1172 | // Markers are sorted top to bottom, right to left. |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1173 | for (let m = markerIndices.length - 1; m >= 0; --m) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1174 | const entryIndex = markerIndices[m]; |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1175 | const title = this._dataProvider.entryTitle(entryIndex); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1176 | if (!title) { |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1177 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1178 | } |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 1179 | const entryStartTime = entryStartTimes[entryIndex]; |
| 1180 | const level = entryLevels[entryIndex]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1181 | if (lastMarkerLevel !== level) { |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 1182 | lastMarkerX = -Infinity; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1183 | } |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 1184 | const x = Math.max(this._chartViewport.timeToPosition(entryStartTime), lastMarkerX); |
| 1185 | const y = this._levelToOffset(level); |
| 1186 | const h = this._levelHeight(level); |
| 1187 | const padding = 4; |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1188 | const width = Math.ceil(UI.UIUtils.measureTextWidth(context, title)) + 2 * padding; |
Alexei Filippov | 214df61 | 2018-11-13 19:14:00 | [diff] [blame] | 1189 | lastMarkerX = x + width + 1; |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 1190 | lastMarkerLevel = level; |
| 1191 | this._markerPositions.set(entryIndex, {x, width}); |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1192 | context.fillStyle = this._dataProvider.entryColor(entryIndex); |
| 1193 | context.fillRect(x, y, width, h - 1); |
| 1194 | context.fillStyle = 'white'; |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 1195 | context.fillText(title, x + padding, y + h - this._textBaseline); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1196 | } |
| 1197 | context.strokeStyle = 'rgba(0, 0, 0, 0.2)'; |
| 1198 | context.stroke(); |
| 1199 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1200 | for (let i = 0; i < titleIndices.length; ++i) { |
| 1201 | const entryIndex = titleIndices[i]; |
| 1202 | const entryStartTime = entryStartTimes[entryIndex]; |
| 1203 | const barX = this._timeToPositionClipped(entryStartTime); |
| 1204 | const barRight = Math.min(this._timeToPositionClipped(entryStartTime + entryTotalTimes[entryIndex]), width) + 1; |
| 1205 | const barWidth = barRight - barX; |
| 1206 | const barLevel = entryLevels[entryIndex]; |
| 1207 | const barY = this._levelToOffset(barLevel); |
| 1208 | let text = this._dataProvider.entryTitle(entryIndex); |
| 1209 | if (text && text.length) { |
| 1210 | context.font = this._dataProvider.entryFont(entryIndex) || defaultFont; |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1211 | text = UI.UIUtils.trimTextMiddle(context, text, barWidth - 2 * textPadding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1212 | } |
| 1213 | const unclippedBarX = this._chartViewport.timeToPosition(entryStartTime); |
| 1214 | const barHeight = this._levelHeight(barLevel); |
| 1215 | if (this._dataProvider.decorateEntry( |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1216 | entryIndex, context, text, barX, barY, barWidth, barHeight, unclippedBarX, timeToPixel)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1217 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1218 | } |
| 1219 | if (!text || !text.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1220 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1221 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1222 | context.fillStyle = this._dataProvider.textColor(entryIndex); |
| 1223 | context.fillText(text, barX + textPadding, barY + barHeight - this._textBaseline); |
| 1224 | } |
| 1225 | |
| 1226 | context.restore(); |
| 1227 | |
| 1228 | this._drawGroupHeaders(width, height); |
| 1229 | this._drawFlowEvents(context, width, height); |
| 1230 | this._drawMarkers(); |
Paul Lewis | 2cfa94e | 2020-01-13 10:30:15 | [diff] [blame] | 1231 | const dividersData = TimelineGrid.calculateGridOffsets(this); |
Paul Lewis | ead4575 | 2020-06-23 09:51:36 | [diff] [blame] | 1232 | const navStartTimes = Array.from(this._dataProvider.navStartTimes().values()); |
| 1233 | |
| 1234 | let navStartTimeIndex = 0; |
| 1235 | const drawAdjustedTime = time => { |
| 1236 | if (navStartTimes.length === 0) { |
| 1237 | return this.formatValue(time, dividersData.precision); |
| 1238 | } |
| 1239 | |
| 1240 | // Track when the time crosses the boundary to the next nav start record, |
| 1241 | // and when it does, move the nav start array index accordingly. |
| 1242 | const hasNextNavStartTime = navStartTimes.length > navStartTimeIndex + 1; |
| 1243 | if (hasNextNavStartTime && time > navStartTimes[navStartTimeIndex + 1].startTime) { |
| 1244 | navStartTimeIndex++; |
| 1245 | } |
| 1246 | |
| 1247 | // Adjust the time by the nearest nav start marker's value. |
| 1248 | const nearestMarker = navStartTimes[navStartTimeIndex]; |
| 1249 | if (nearestMarker) { |
| 1250 | time -= nearestMarker.startTime - this.zeroTime(); |
| 1251 | } |
| 1252 | |
| 1253 | return this.formatValue(time, dividersData.precision); |
| 1254 | }; |
| 1255 | |
Paul Lewis | 2cfa94e | 2020-01-13 10:30:15 | [diff] [blame] | 1256 | TimelineGrid.drawCanvasGrid(context, dividersData); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1257 | if (this._rulerEnabled) { |
Paul Lewis | ead4575 | 2020-06-23 09:51:36 | [diff] [blame] | 1258 | TimelineGrid.drawCanvasHeaders(context, dividersData, drawAdjustedTime, 3, HeaderHeight); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | this._updateElementPosition(this._highlightElement, this._highlightedEntryIndex); |
| 1262 | this._updateElementPosition(this._selectedElement, this._selectedEntryIndex); |
| 1263 | this._updateMarkerHighlight(); |
| 1264 | } |
| 1265 | |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1266 | _initWebGL() { |
| 1267 | const gl = /** @type {?WebGLRenderingContext} */ (this._canvasGL.getContext('webgl')); |
| 1268 | if (!gl) { |
| 1269 | console.error('Failed to obtain WebGL context.'); |
| 1270 | this._useWebGL = false; // Fallback to use canvas. |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | const vertexShaderSource = ` |
| 1275 | attribute vec2 aVertexPosition; |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1276 | attribute float aVertexColor; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1277 | |
| 1278 | uniform vec2 uScalingFactor; |
| 1279 | uniform vec2 uShiftVector; |
| 1280 | |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1281 | varying mediump vec2 vPalettePosition; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1282 | |
| 1283 | void main() { |
| 1284 | vec2 shiftedPosition = aVertexPosition - uShiftVector; |
| 1285 | gl_Position = vec4(shiftedPosition * uScalingFactor + vec2(-1.0, 1.0), 0.0, 1.0); |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1286 | vPalettePosition = vec2(aVertexColor, 0.5); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1287 | }`; |
| 1288 | |
| 1289 | const fragmentShaderSource = ` |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1290 | varying mediump vec2 vPalettePosition; |
| 1291 | uniform sampler2D uSampler; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1292 | |
| 1293 | void main() { |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1294 | gl_FragColor = texture2D(uSampler, vPalettePosition); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1295 | }`; |
| 1296 | |
| 1297 | /** |
| 1298 | * @param {!WebGLRenderingContext} gl |
| 1299 | * @param {number} type |
| 1300 | * @param {string} source |
| 1301 | * @return {?WebGLShader} |
| 1302 | */ |
| 1303 | function loadShader(gl, type, source) { |
| 1304 | const shader = gl.createShader(type); |
| 1305 | gl.shaderSource(shader, source); |
| 1306 | gl.compileShader(shader); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1307 | if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1308 | return shader; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1309 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1310 | console.error('Shader compile error: ' + gl.getShaderInfoLog(shader)); |
| 1311 | gl.deleteShader(shader); |
| 1312 | return null; |
| 1313 | } |
| 1314 | |
| 1315 | const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vertexShaderSource); |
| 1316 | const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource); |
| 1317 | |
| 1318 | const shaderProgram = gl.createProgram(); |
| 1319 | gl.attachShader(shaderProgram, vertexShader); |
| 1320 | gl.attachShader(shaderProgram, fragmentShader); |
| 1321 | gl.linkProgram(shaderProgram); |
| 1322 | |
| 1323 | if (gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { |
| 1324 | this._shaderProgram = shaderProgram; |
| 1325 | gl.useProgram(shaderProgram); |
| 1326 | } else { |
| 1327 | console.error('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram)); |
| 1328 | this._shaderProgram = null; |
| 1329 | } |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1330 | |
| 1331 | this._vertexBuffer = gl.createBuffer(); |
| 1332 | this._colorBuffer = gl.createBuffer(); |
| 1333 | |
| 1334 | this._uScalingFactor = gl.getUniformLocation(shaderProgram, 'uScalingFactor'); |
| 1335 | this._uShiftVector = gl.getUniformLocation(shaderProgram, 'uShiftVector'); |
| 1336 | const uSampler = gl.getUniformLocation(shaderProgram, 'uSampler'); |
| 1337 | gl.uniform1i(uSampler, 0); |
| 1338 | this._aVertexPosition = gl.getAttribLocation(this._shaderProgram, 'aVertexPosition'); |
| 1339 | this._aVertexColor = gl.getAttribLocation(this._shaderProgram, 'aVertexColor'); |
| 1340 | gl.enableVertexAttribArray(this._aVertexPosition); |
| 1341 | gl.enableVertexAttribArray(this._aVertexColor); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | _setupGLGeometry() { |
| 1345 | const gl = /** @type {?WebGLRenderingContext} */ (this._canvasGL.getContext('webgl')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1346 | if (!gl) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1347 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1348 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1349 | |
| 1350 | const timelineData = this._timelineData(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1351 | if (!timelineData) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1352 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1353 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1354 | |
| 1355 | const entryTotalTimes = timelineData.entryTotalTimes; |
| 1356 | const entryStartTimes = timelineData.entryStartTimes; |
| 1357 | const entryLevels = timelineData.entryLevels; |
| 1358 | |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1359 | const verticesPerBar = 6; |
| 1360 | const vertexArray = new Float32Array(entryTotalTimes.length * verticesPerBar * 2); |
| 1361 | let colorArray = new Uint8Array(entryTotalTimes.length * verticesPerBar); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1362 | let vertex = 0; |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1363 | /** @type {!Map<string, number>} */ |
Alexei Filippov | c34372c | 2018-08-16 20:37:39 | [diff] [blame] | 1364 | const parsedColorCache = new Map(); |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1365 | /** @type {!Array<number>} */ |
| 1366 | const colors = []; |
| 1367 | |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1368 | const collapsedOverviewLevels = new Array(this._visibleLevels.length); |
| 1369 | const groups = this._rawTimelineData.groups || []; |
| 1370 | this._forEachGroup((offset, index, group) => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1371 | if (group.style.useFirstLineForOverview || !this._isGroupCollapsible(index) || group.expanded) { |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1372 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1373 | } |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1374 | let nextGroup = index + 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1375 | while (nextGroup < groups.length && groups[nextGroup].style.nestingLevel > group.style.nestingLevel) { |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1376 | ++nextGroup; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1377 | } |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1378 | const endLevel = nextGroup < groups.length ? groups[nextGroup].startLevel : this._dataProvider.maxStackDepth(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1379 | for (let i = group.startLevel; i < endLevel; ++i) { |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1380 | collapsedOverviewLevels[i] = offset; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1381 | } |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1382 | }); |
| 1383 | |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1384 | for (let i = 0; i < entryTotalTimes.length; ++i) { |
| 1385 | const level = entryLevels[i]; |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1386 | const collapsedGroupOffset = collapsedOverviewLevels[level]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1387 | if (!this._visibleLevels[level] && !collapsedGroupOffset) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1388 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1389 | } |
Alexei Filippov | e519762 | 2018-08-18 03:04:33 | [diff] [blame] | 1390 | const color = this._entryColorsCache[i]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1391 | if (!color) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1392 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1393 | } |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1394 | let colorIndex = parsedColorCache.get(color); |
| 1395 | if (colorIndex === undefined) { |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1396 | const rgba = Common.Color.Color.parse(color).canonicalRGBA(); |
Alexei Filippov | c34372c | 2018-08-16 20:37:39 | [diff] [blame] | 1397 | rgba[3] = Math.round(rgba[3] * 255); |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1398 | colorIndex = colors.length / 4; |
| 1399 | colors.push(...rgba); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1400 | if (colorIndex === 256) { |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1401 | colorArray = new Uint16Array(colorArray); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1402 | } |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1403 | parsedColorCache.set(color, colorIndex); |
Alexei Filippov | c34372c | 2018-08-16 20:37:39 | [diff] [blame] | 1404 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1405 | for (let j = 0; j < verticesPerBar; ++j) { |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1406 | colorArray[vertex + j] = colorIndex; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1407 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1408 | |
| 1409 | const vpos = vertex * 2; |
| 1410 | const x0 = entryStartTimes[i] - this._minimumBoundary; |
| 1411 | const x1 = x0 + entryTotalTimes[i]; |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1412 | const y0 = collapsedGroupOffset || this._levelToOffset(level); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1413 | const y1 = y0 + this._levelHeight(level) - 1; |
| 1414 | vertexArray[vpos + 0] = x0; |
| 1415 | vertexArray[vpos + 1] = y0; |
| 1416 | vertexArray[vpos + 2] = x1; |
| 1417 | vertexArray[vpos + 3] = y0; |
| 1418 | vertexArray[vpos + 4] = x0; |
| 1419 | vertexArray[vpos + 5] = y1; |
| 1420 | vertexArray[vpos + 6] = x0; |
| 1421 | vertexArray[vpos + 7] = y1; |
| 1422 | vertexArray[vpos + 8] = x1; |
| 1423 | vertexArray[vpos + 9] = y0; |
| 1424 | vertexArray[vpos + 10] = x1; |
| 1425 | vertexArray[vpos + 11] = y1; |
| 1426 | |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1427 | vertex += verticesPerBar; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1428 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1429 | this._vertexCount = vertex; |
| 1430 | |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1431 | const paletteTexture = gl.createTexture(); |
| 1432 | gl.bindTexture(gl.TEXTURE_2D, paletteTexture); |
| 1433 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 1434 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 1435 | gl.activeTexture(gl.TEXTURE0); |
| 1436 | |
| 1437 | const numColors = colors.length / 4; |
| 1438 | const useShortForColors = numColors >= 256; |
| 1439 | const width = !useShortForColors ? 256 : Math.min(1 << 16, gl.getParameter(gl.MAX_TEXTURE_SIZE)); |
| 1440 | console.assert(numColors <= width, 'Too many colors'); |
| 1441 | const height = 1; |
| 1442 | const colorIndexType = useShortForColors ? gl.UNSIGNED_SHORT : gl.UNSIGNED_BYTE; |
| 1443 | if (useShortForColors) { |
| 1444 | const factor = (1 << 16) / width; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1445 | for (let i = 0; i < vertex; ++i) { |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1446 | colorArray[i] *= factor; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1447 | } |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | const pixels = new Uint8Array(width * 4); |
| 1451 | pixels.set(colors); |
| 1452 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels); |
| 1453 | |
| 1454 | gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1455 | gl.bufferData(gl.ARRAY_BUFFER, vertexArray, gl.STATIC_DRAW); |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1456 | gl.vertexAttribPointer(this._aVertexPosition, /* vertexComponents */ 2, gl.FLOAT, false, 0, 0); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1457 | |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1458 | gl.bindBuffer(gl.ARRAY_BUFFER, this._colorBuffer); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1459 | gl.bufferData(gl.ARRAY_BUFFER, colorArray, gl.STATIC_DRAW); |
Alexei Filippov | af3ffed | 2018-08-18 01:56:09 | [diff] [blame] | 1460 | gl.vertexAttribPointer(this._aVertexColor, /* colorComponents */ 1, colorIndexType, true, 0, 0); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | _drawGL() { |
| 1464 | const gl = /** @type {?WebGLRenderingContext} */ (this._canvasGL.getContext('webgl')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1465 | if (!gl) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1466 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1467 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1468 | const timelineData = this._timelineData(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1469 | if (!timelineData) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1470 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1471 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1472 | |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1473 | if (!this._prevTimelineData || timelineData.entryTotalTimes !== this._prevTimelineData.entryTotalTimes) { |
| 1474 | this._prevTimelineData = timelineData; |
| 1475 | this._setupGLGeometry(); |
| 1476 | } |
| 1477 | |
Alexei Filippov | c34372c | 2018-08-16 20:37:39 | [diff] [blame] | 1478 | gl.viewport(0, 0, this._canvasGL.width, this._canvasGL.height); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1479 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1480 | if (!this._vertexCount) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1481 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1482 | } |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1483 | |
Alexei Filippov | c34372c | 2018-08-16 20:37:39 | [diff] [blame] | 1484 | const viewportScale = [2.0 / this.boundarySpan(), -2.0 * window.devicePixelRatio / this._canvasGL.height]; |
| 1485 | const viewportShift = [this.minimumBoundary() - this.zeroTime(), this._chartViewport.scrollOffset()]; |
| 1486 | gl.uniform2fv(this._uScalingFactor, viewportScale); |
| 1487 | gl.uniform2fv(this._uShiftVector, viewportShift); |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1488 | |
| 1489 | gl.drawArrays(gl.TRIANGLES, 0, this._vertexCount); |
| 1490 | } |
| 1491 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1492 | /** |
| 1493 | * @param {number} width |
| 1494 | * @param {number} height |
| 1495 | */ |
| 1496 | _drawGroupHeaders(width, height) { |
| 1497 | const context = /** @type {!CanvasRenderingContext2D} */ (this._canvas.getContext('2d')); |
| 1498 | const top = this._chartViewport.scrollOffset(); |
| 1499 | const ratio = window.devicePixelRatio; |
| 1500 | const groups = this._rawTimelineData.groups || []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1501 | if (!groups.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1502 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1503 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1504 | |
| 1505 | const groupOffsets = this._groupOffsets; |
| 1506 | const lastGroupOffset = Array.prototype.peekLast.call(groupOffsets); |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame^] | 1507 | const colorUsage = ThemeSupport.ThemeSupport.ColorUsage; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1508 | |
| 1509 | context.save(); |
| 1510 | context.scale(ratio, ratio); |
| 1511 | context.translate(0, -top); |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1512 | const defaultFont = '11px ' + Host.Platform.fontFamily(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1513 | context.font = defaultFont; |
| 1514 | |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame^] | 1515 | context.fillStyle = ThemeSupport.ThemeSupport.instance().patchColorText('#fff', colorUsage.Background); |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1516 | this._forEachGroupInViewport((offset, index, group) => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1517 | const paddingHeight = group.style.padding; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1518 | if (paddingHeight < 5) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1519 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1520 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1521 | context.fillRect(0, offset - paddingHeight + 2, width, paddingHeight - 4); |
| 1522 | }); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1523 | if (groups.length && lastGroupOffset < top + height) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1524 | context.fillRect(0, lastGroupOffset + 2, width, top + height - lastGroupOffset); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1525 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1526 | |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame^] | 1527 | context.strokeStyle = ThemeSupport.ThemeSupport.instance().patchColorText('#eee', colorUsage.Background); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1528 | context.beginPath(); |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1529 | this._forEachGroupInViewport((offset, index, group, isFirst) => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1530 | if (isFirst || group.style.padding < 4) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1531 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1532 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1533 | hLine(offset - 2.5); |
| 1534 | }); |
| 1535 | hLine(lastGroupOffset + 1.5); |
| 1536 | context.stroke(); |
| 1537 | |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1538 | this._forEachGroupInViewport((offset, index, group) => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1539 | if (group.style.useFirstLineForOverview) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1540 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1541 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1542 | if (!this._isGroupCollapsible(index) || group.expanded) { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 1543 | if (!group.style.shareHeaderLine && this._isGroupFocused(index)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1544 | context.fillStyle = group.style.backgroundColor; |
| 1545 | context.fillRect(0, offset, width, group.style.height); |
| 1546 | } |
| 1547 | return; |
| 1548 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1549 | if (this._useWebGL) { |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1550 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1551 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1552 | let nextGroup = index + 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1553 | while (nextGroup < groups.length && groups[nextGroup].style.nestingLevel > group.style.nestingLevel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1554 | nextGroup++; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1555 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1556 | const endLevel = nextGroup < groups.length ? groups[nextGroup].startLevel : this._dataProvider.maxStackDepth(); |
Alexei Filippov | 23428d4 | 2018-04-28 01:09:07 | [diff] [blame] | 1557 | this._drawCollapsedOverviewForGroup(group, offset, endLevel); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1558 | }); |
| 1559 | |
| 1560 | context.save(); |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1561 | this._forEachGroupInViewport((offset, index, group) => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1562 | context.font = group.style.font; |
| 1563 | if (this._isGroupCollapsible(index) && !group.expanded || group.style.shareHeaderLine) { |
| 1564 | const width = this._labelWidthForGroup(context, group) + 2; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1565 | if (this._isGroupFocused(index)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1566 | context.fillStyle = this._selectedGroupBackroundColor; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1567 | } else { |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1568 | context.fillStyle = Common.Color.Color.parse(group.style.backgroundColor).setAlpha(0.8).asString(null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1569 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1570 | |
| 1571 | context.fillRect( |
| 1572 | this._headerLeftPadding - this._headerLabelXPadding, offset + this._headerLabelYPadding, width, |
| 1573 | group.style.height - 2 * this._headerLabelYPadding); |
| 1574 | } |
| 1575 | context.fillStyle = group.style.color; |
| 1576 | context.fillText( |
| 1577 | group.name, Math.floor(this._expansionArrowIndent * (group.style.nestingLevel + 1) + this._arrowSide), |
| 1578 | offset + group.style.height - this._textBaseline); |
| 1579 | }); |
| 1580 | context.restore(); |
| 1581 | |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame^] | 1582 | context.fillStyle = ThemeSupport.ThemeSupport.instance().patchColorText('#6e6e6e', colorUsage.Foreground); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1583 | context.beginPath(); |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1584 | this._forEachGroupInViewport((offset, index, group) => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1585 | if (this._isGroupCollapsible(index)) { |
| 1586 | drawExpansionArrow.call( |
| 1587 | this, this._expansionArrowIndent * (group.style.nestingLevel + 1), |
| 1588 | offset + group.style.height - this._textBaseline - this._arrowSide / 2, !!group.expanded); |
| 1589 | } |
| 1590 | }); |
| 1591 | context.fill(); |
| 1592 | |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame^] | 1593 | context.strokeStyle = ThemeSupport.ThemeSupport.instance().patchColorText('#ddd', colorUsage.Background); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1594 | context.beginPath(); |
| 1595 | context.stroke(); |
| 1596 | |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1597 | this._forEachGroupInViewport((offset, index, group, isFirst, groupHeight) => { |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 1598 | if (this._isGroupFocused(index)) { |
Alexei Filippov | 23428d4 | 2018-04-28 01:09:07 | [diff] [blame] | 1599 | const lineWidth = 2; |
| 1600 | const bracketLength = 10; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1601 | context.fillStyle = this._selectedGroupBorderColor; |
Alexei Filippov | 23428d4 | 2018-04-28 01:09:07 | [diff] [blame] | 1602 | context.fillRect(0, offset - lineWidth, lineWidth, groupHeight - group.style.padding + 2 * lineWidth); |
| 1603 | context.fillRect(0, offset - lineWidth, bracketLength, lineWidth); |
| 1604 | context.fillRect(0, offset + groupHeight - group.style.padding, bracketLength, lineWidth); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1605 | } |
| 1606 | }); |
| 1607 | |
| 1608 | context.restore(); |
| 1609 | |
| 1610 | /** |
| 1611 | * @param {number} y |
| 1612 | */ |
| 1613 | function hLine(y) { |
| 1614 | context.moveTo(0, y); |
| 1615 | context.lineTo(width, y); |
| 1616 | } |
| 1617 | |
| 1618 | /** |
| 1619 | * @param {number} x |
| 1620 | * @param {number} y |
| 1621 | * @param {boolean} expanded |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 1622 | * @this {FlameChart} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1623 | */ |
| 1624 | function drawExpansionArrow(x, y, expanded) { |
| 1625 | const arrowHeight = this._arrowSide * Math.sqrt(3) / 2; |
| 1626 | const arrowCenterOffset = Math.round(arrowHeight / 2); |
| 1627 | context.save(); |
| 1628 | context.translate(x, y); |
| 1629 | context.rotate(expanded ? Math.PI / 2 : 0); |
| 1630 | context.moveTo(-arrowCenterOffset, -this._arrowSide / 2); |
| 1631 | context.lineTo(-arrowCenterOffset, this._arrowSide / 2); |
| 1632 | context.lineTo(arrowHeight - arrowCenterOffset, 0); |
| 1633 | context.restore(); |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | /** |
Tim van der Lippe | 8ef250c | 2020-02-20 16:29:25 | [diff] [blame] | 1638 | * @param {function(number, number, !Group, boolean, number)} callback |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1639 | */ |
| 1640 | _forEachGroup(callback) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1641 | const groups = this._rawTimelineData.groups || []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1642 | if (!groups.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1643 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1644 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1645 | const groupOffsets = this._groupOffsets; |
| 1646 | /** @type !Array<{nestingLevel: number, visible: boolean}> */ |
| 1647 | const groupStack = [{nestingLevel: -1, visible: true}]; |
| 1648 | for (let i = 0; i < groups.length; ++i) { |
| 1649 | const groupTop = groupOffsets[i]; |
| 1650 | const group = groups[i]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1651 | let firstGroup = true; |
| 1652 | while (groupStack.peekLast().nestingLevel >= group.style.nestingLevel) { |
| 1653 | groupStack.pop(); |
| 1654 | firstGroup = false; |
| 1655 | } |
| 1656 | const parentGroupVisible = groupStack.peekLast().visible; |
| 1657 | const thisGroupVisible = parentGroupVisible && (!this._isGroupCollapsible(i) || group.expanded); |
| 1658 | groupStack.push({nestingLevel: group.style.nestingLevel, visible: thisGroupVisible}); |
Alexei Filippov | 23428d4 | 2018-04-28 01:09:07 | [diff] [blame] | 1659 | const nextOffset = i === groups.length - 1 ? groupOffsets[i + 1] + group.style.padding : groupOffsets[i + 1]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1660 | if (!parentGroupVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1661 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1662 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1663 | callback(groupTop, i, group, firstGroup, nextOffset - groupTop); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | /** |
Tim van der Lippe | 8ef250c | 2020-02-20 16:29:25 | [diff] [blame] | 1668 | * @param {function(number, number, !Group, boolean, number)} callback |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1669 | */ |
| 1670 | _forEachGroupInViewport(callback) { |
| 1671 | const top = this._chartViewport.scrollOffset(); |
| 1672 | this._forEachGroup((groupTop, index, group, firstGroup, height) => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1673 | if (groupTop - group.style.padding > top + this._offsetHeight) { |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1674 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1675 | } |
| 1676 | if (groupTop + height < top) { |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1677 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1678 | } |
Alexei Filippov | 5f6b11d | 2018-08-18 03:30:28 | [diff] [blame] | 1679 | callback(groupTop, index, group, firstGroup, height); |
| 1680 | }); |
| 1681 | } |
| 1682 | |
| 1683 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1684 | * @param {!CanvasRenderingContext2D} context |
Tim van der Lippe | 8ef250c | 2020-02-20 16:29:25 | [diff] [blame] | 1685 | * @param {!Group} group |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1686 | * @return {number} |
| 1687 | */ |
| 1688 | _labelWidthForGroup(context, group) { |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1689 | return UI.UIUtils.measureTextWidth(context, group.name) + |
| 1690 | this._expansionArrowIndent * (group.style.nestingLevel + 1) + 2 * this._headerLabelXPadding; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /** |
Tim van der Lippe | 8ef250c | 2020-02-20 16:29:25 | [diff] [blame] | 1694 | * @param {!Group} group |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1695 | * @param {number} y |
| 1696 | * @param {number} endLevel |
| 1697 | */ |
| 1698 | _drawCollapsedOverviewForGroup(group, y, endLevel) { |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1699 | const range = new Common.SegmentedRange.SegmentedRange(mergeCallback); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1700 | const timeWindowLeft = this._chartViewport.windowLeftTime(); |
| 1701 | const timeWindowRight = this._chartViewport.windowRightTime(); |
| 1702 | const context = /** @type {!CanvasRenderingContext2D} */ (this._canvas.getContext('2d')); |
| 1703 | const barHeight = group.style.height; |
| 1704 | const entryStartTimes = this._rawTimelineData.entryStartTimes; |
| 1705 | const entryTotalTimes = this._rawTimelineData.entryTotalTimes; |
| 1706 | const timeToPixel = this._chartViewport.timeToPixel(); |
| 1707 | |
| 1708 | for (let level = group.startLevel; level < endLevel; ++level) { |
| 1709 | const levelIndexes = this._timelineLevels[level]; |
| 1710 | const rightIndexOnLevel = |
| 1711 | levelIndexes.lowerBound(timeWindowRight, (time, entryIndex) => time - entryStartTimes[entryIndex]) - 1; |
| 1712 | let lastDrawOffset = Infinity; |
| 1713 | |
| 1714 | for (let entryIndexOnLevel = rightIndexOnLevel; entryIndexOnLevel >= 0; --entryIndexOnLevel) { |
| 1715 | const entryIndex = levelIndexes[entryIndexOnLevel]; |
| 1716 | const entryStartTime = entryStartTimes[entryIndex]; |
| 1717 | const barX = this._timeToPositionClipped(entryStartTime); |
| 1718 | const entryEndTime = entryStartTime + entryTotalTimes[entryIndex]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1719 | if (isNaN(entryEndTime) || barX >= lastDrawOffset) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1720 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1721 | } |
| 1722 | if (entryEndTime <= timeWindowLeft) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1723 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1724 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1725 | lastDrawOffset = barX; |
Alexei Filippov | e519762 | 2018-08-18 03:04:33 | [diff] [blame] | 1726 | const color = this._entryColorsCache[entryIndex]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1727 | const endBarX = this._timeToPositionClipped(entryEndTime); |
| 1728 | if (group.style.useDecoratorsForOverview && this._dataProvider.forceDecoration(entryIndex)) { |
| 1729 | const unclippedBarX = this._chartViewport.timeToPosition(entryStartTime); |
| 1730 | const barWidth = endBarX - barX; |
| 1731 | context.beginPath(); |
| 1732 | context.fillStyle = color; |
| 1733 | context.fillRect(barX, y, barWidth, barHeight - 1); |
| 1734 | this._dataProvider.decorateEntry( |
| 1735 | entryIndex, context, '', barX, y, barWidth, barHeight, unclippedBarX, timeToPixel); |
| 1736 | continue; |
| 1737 | } |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1738 | range.append(new Common.SegmentedRange.Segment(barX, endBarX, color)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | const segments = range.segments().slice().sort((a, b) => a.data.localeCompare(b.data)); |
| 1743 | let lastColor; |
| 1744 | context.beginPath(); |
| 1745 | for (let i = 0; i < segments.length; ++i) { |
| 1746 | const segment = segments[i]; |
| 1747 | if (lastColor !== segments[i].data) { |
| 1748 | context.fill(); |
| 1749 | context.beginPath(); |
| 1750 | lastColor = segments[i].data; |
| 1751 | context.fillStyle = lastColor; |
| 1752 | } |
Alexei Filippov | 23428d4 | 2018-04-28 01:09:07 | [diff] [blame] | 1753 | context.rect(segment.begin, y, segment.end - segment.begin, barHeight); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1754 | } |
| 1755 | context.fill(); |
| 1756 | |
| 1757 | /** |
Tim van der Lippe | bb76917 | 2020-02-12 15:32:44 | [diff] [blame] | 1758 | * @param {!Common.SegmentedRange.Segment} a |
| 1759 | * @param {!Common.SegmentedRange.Segment} b |
| 1760 | * @return {?Common.SegmentedRange.Segment} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1761 | */ |
| 1762 | function mergeCallback(a, b) { |
| 1763 | return a.data === b.data && a.end + 0.4 > b.end ? a : null; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | /** |
| 1768 | * @param {!CanvasRenderingContext2D} context |
| 1769 | * @param {number} height |
| 1770 | * @param {number} width |
| 1771 | */ |
| 1772 | _drawFlowEvents(context, width, height) { |
| 1773 | context.save(); |
| 1774 | const ratio = window.devicePixelRatio; |
| 1775 | const top = this._chartViewport.scrollOffset(); |
| 1776 | const arrowWidth = 6; |
| 1777 | context.scale(ratio, ratio); |
| 1778 | context.translate(0, -top); |
| 1779 | |
| 1780 | context.fillStyle = '#7f5050'; |
| 1781 | context.strokeStyle = '#7f5050'; |
| 1782 | const td = this._timelineData(); |
| 1783 | const endIndex = td.flowStartTimes.lowerBound(this._chartViewport.windowRightTime()); |
| 1784 | |
| 1785 | context.lineWidth = 0.5; |
| 1786 | for (let i = 0; i < endIndex; ++i) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1787 | if (!td.flowEndTimes[i] || td.flowEndTimes[i] < this._chartViewport.windowLeftTime()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1788 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1789 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1790 | const startX = this._chartViewport.timeToPosition(td.flowStartTimes[i]); |
| 1791 | const endX = this._chartViewport.timeToPosition(td.flowEndTimes[i]); |
| 1792 | const startLevel = td.flowStartLevels[i]; |
| 1793 | const endLevel = td.flowEndLevels[i]; |
| 1794 | const startY = this._levelToOffset(startLevel) + this._levelHeight(startLevel) / 2; |
| 1795 | const endY = this._levelToOffset(endLevel) + this._levelHeight(endLevel) / 2; |
| 1796 | |
| 1797 | |
| 1798 | const segment = Math.min((endX - startX) / 4, 40); |
| 1799 | const distanceTime = td.flowEndTimes[i] - td.flowStartTimes[i]; |
| 1800 | const distanceY = (endY - startY) / 10; |
| 1801 | const spread = 30; |
| 1802 | const lineY = distanceTime < 1 ? startY : spread + Math.max(0, startY + distanceY * (i % spread)); |
| 1803 | |
| 1804 | const p = []; |
| 1805 | p.push({x: startX, y: startY}); |
| 1806 | p.push({x: startX + arrowWidth, y: startY}); |
| 1807 | p.push({x: startX + segment + 2 * arrowWidth, y: startY}); |
| 1808 | p.push({x: startX + segment, y: lineY}); |
| 1809 | p.push({x: startX + segment * 2, y: lineY}); |
| 1810 | p.push({x: endX - segment * 2, y: lineY}); |
| 1811 | p.push({x: endX - segment, y: lineY}); |
| 1812 | p.push({x: endX - segment - 2 * arrowWidth, y: endY}); |
| 1813 | p.push({x: endX - arrowWidth, y: endY}); |
| 1814 | |
| 1815 | context.beginPath(); |
| 1816 | context.moveTo(p[0].x, p[0].y); |
| 1817 | context.lineTo(p[1].x, p[1].y); |
| 1818 | context.bezierCurveTo(p[2].x, p[2].y, p[3].x, p[3].y, p[4].x, p[4].y); |
| 1819 | context.lineTo(p[5].x, p[5].y); |
| 1820 | context.bezierCurveTo(p[6].x, p[6].y, p[7].x, p[7].y, p[8].x, p[8].y); |
| 1821 | context.stroke(); |
| 1822 | |
| 1823 | context.beginPath(); |
| 1824 | context.arc(startX, startY, 2, -Math.PI / 2, Math.PI / 2, false); |
| 1825 | context.fill(); |
| 1826 | |
| 1827 | context.beginPath(); |
| 1828 | context.moveTo(endX, endY); |
| 1829 | context.lineTo(endX - arrowWidth, endY - 3); |
| 1830 | context.lineTo(endX - arrowWidth, endY + 3); |
| 1831 | context.fill(); |
| 1832 | } |
| 1833 | context.restore(); |
| 1834 | } |
| 1835 | |
| 1836 | _drawMarkers() { |
| 1837 | const markers = this._timelineData().markers; |
| 1838 | const left = this._markerIndexBeforeTime(this.minimumBoundary()); |
| 1839 | const rightBoundary = this.maximumBoundary(); |
| 1840 | const timeToPixel = this._chartViewport.timeToPixel(); |
| 1841 | |
| 1842 | const context = /** @type {!CanvasRenderingContext2D} */ (this._canvas.getContext('2d')); |
| 1843 | context.save(); |
| 1844 | const ratio = window.devicePixelRatio; |
| 1845 | context.scale(ratio, ratio); |
| 1846 | context.translate(0, 3); |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 1847 | const height = HeaderHeight - 1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1848 | for (let i = left; i < markers.length; i++) { |
| 1849 | const timestamp = markers[i].startTime(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1850 | if (timestamp > rightBoundary) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1851 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1852 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1853 | markers[i].draw(context, this._chartViewport.timeToPosition(timestamp), height, timeToPixel); |
| 1854 | } |
| 1855 | context.restore(); |
| 1856 | } |
| 1857 | |
| 1858 | _updateMarkerHighlight() { |
| 1859 | const element = this._markerHighlighElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1860 | if (element.parentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1861 | element.remove(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1862 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1863 | const markerIndex = this._highlightedMarkerIndex; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1864 | if (markerIndex === -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1865 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1866 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1867 | const marker = this._timelineData().markers[markerIndex]; |
| 1868 | const barX = this._timeToPositionClipped(marker.startTime()); |
| 1869 | element.title = marker.title(); |
| 1870 | const style = element.style; |
| 1871 | style.left = barX + 'px'; |
| 1872 | style.backgroundColor = marker.color(); |
| 1873 | this._viewportElement.appendChild(element); |
| 1874 | } |
| 1875 | |
| 1876 | /** |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 1877 | * @param {?TimelineData} timelineData |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1878 | */ |
| 1879 | _processTimelineData(timelineData) { |
| 1880 | if (!timelineData) { |
| 1881 | this._timelineLevels = null; |
| 1882 | this._visibleLevelOffsets = null; |
| 1883 | this._visibleLevels = null; |
| 1884 | this._groupOffsets = null; |
| 1885 | this._rawTimelineData = null; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1886 | this._forceDecorationCache = null; |
Alexei Filippov | e519762 | 2018-08-18 03:04:33 | [diff] [blame] | 1887 | this._entryColorsCache = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1888 | this._rawTimelineDataLength = 0; |
| 1889 | this._selectedGroup = -1; |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 1890 | this._keyboardFocusedGroup = -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1891 | this._flameChartDelegate.updateSelectedGroup(this, null); |
| 1892 | return; |
| 1893 | } |
| 1894 | |
| 1895 | this._rawTimelineData = timelineData; |
| 1896 | this._rawTimelineDataLength = timelineData.entryStartTimes.length; |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1897 | this._forceDecorationCache = new Int8Array(this._rawTimelineDataLength); |
Alexei Filippov | e519762 | 2018-08-18 03:04:33 | [diff] [blame] | 1898 | this._entryColorsCache = new Array(this._rawTimelineDataLength); |
| 1899 | for (let i = 0; i < this._rawTimelineDataLength; ++i) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 1900 | this._forceDecorationCache[i] = this._dataProvider.forceDecoration(i) ? 1 : 0; |
Alexei Filippov | e519762 | 2018-08-18 03:04:33 | [diff] [blame] | 1901 | this._entryColorsCache[i] = this._dataProvider.entryColor(i); |
| 1902 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1903 | |
| 1904 | const entryCounters = new Uint32Array(this._dataProvider.maxStackDepth() + 1); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1905 | for (let i = 0; i < timelineData.entryLevels.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1906 | ++entryCounters[timelineData.entryLevels[i]]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1907 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1908 | const levelIndexes = new Array(entryCounters.length); |
| 1909 | for (let i = 0; i < levelIndexes.length; ++i) { |
| 1910 | levelIndexes[i] = new Uint32Array(entryCounters[i]); |
| 1911 | entryCounters[i] = 0; |
| 1912 | } |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 1913 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1914 | for (let i = 0; i < timelineData.entryLevels.length; ++i) { |
| 1915 | const level = timelineData.entryLevels[i]; |
| 1916 | levelIndexes[level][entryCounters[level]++] = i; |
| 1917 | } |
| 1918 | this._timelineLevels = levelIndexes; |
| 1919 | const groups = this._rawTimelineData.groups || []; |
| 1920 | for (let i = 0; i < groups.length; ++i) { |
| 1921 | const expanded = this._groupExpansionState[groups[i].name]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1922 | if (expanded !== undefined) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1923 | groups[i].expanded = expanded; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1924 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1925 | } |
| 1926 | this._updateLevelPositions(); |
| 1927 | this._updateHeight(); |
| 1928 | |
| 1929 | this._selectedGroup = timelineData.selectedGroup ? groups.indexOf(timelineData.selectedGroup) : -1; |
Anubha Mathur | 72dd582 | 2019-06-13 23:05:19 | [diff] [blame] | 1930 | this._keyboardFocusedGroup = this._selectedGroup; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1931 | this._flameChartDelegate.updateSelectedGroup(this, timelineData.selectedGroup); |
| 1932 | } |
| 1933 | |
| 1934 | _updateLevelPositions() { |
| 1935 | const levelCount = this._dataProvider.maxStackDepth(); |
| 1936 | const groups = this._rawTimelineData.groups || []; |
| 1937 | this._visibleLevelOffsets = new Uint32Array(levelCount + 1); |
| 1938 | this._visibleLevelHeights = new Uint32Array(levelCount); |
| 1939 | this._visibleLevels = new Uint16Array(levelCount); |
| 1940 | this._groupOffsets = new Uint32Array(groups.length + 1); |
| 1941 | |
| 1942 | let groupIndex = -1; |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 1943 | let currentOffset = this._rulerEnabled ? HeaderHeight + 2 : 2; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1944 | let visible = true; |
| 1945 | /** @type !Array<{nestingLevel: number, visible: boolean}> */ |
| 1946 | const groupStack = [{nestingLevel: -1, visible: true}]; |
| 1947 | const lastGroupLevel = Math.max(levelCount, groups.length ? groups.peekLast().startLevel + 1 : 0); |
| 1948 | let level; |
| 1949 | for (level = 0; level < lastGroupLevel; ++level) { |
| 1950 | let parentGroupIsVisible = true; |
| 1951 | let style; |
| 1952 | while (groupIndex < groups.length - 1 && level === groups[groupIndex + 1].startLevel) { |
| 1953 | ++groupIndex; |
| 1954 | style = groups[groupIndex].style; |
| 1955 | let nextLevel = true; |
| 1956 | while (groupStack.peekLast().nestingLevel >= style.nestingLevel) { |
| 1957 | groupStack.pop(); |
| 1958 | nextLevel = false; |
| 1959 | } |
| 1960 | const thisGroupIsVisible = |
| 1961 | groupIndex >= 0 && this._isGroupCollapsible(groupIndex) ? groups[groupIndex].expanded : true; |
| 1962 | parentGroupIsVisible = groupStack.peekLast().visible; |
| 1963 | visible = thisGroupIsVisible && parentGroupIsVisible; |
| 1964 | groupStack.push({nestingLevel: style.nestingLevel, visible: visible}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1965 | if (parentGroupIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1966 | currentOffset += nextLevel ? 0 : style.padding; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1967 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1968 | this._groupOffsets[groupIndex] = currentOffset; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1969 | if (parentGroupIsVisible && !style.shareHeaderLine) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1970 | currentOffset += style.height; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1971 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1972 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1973 | if (level >= levelCount) { |
Yang Guo | 8fb3ac5 | 2019-07-31 20:17:50 | [diff] [blame] | 1974 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1975 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1976 | const isFirstOnLevel = groupIndex >= 0 && level === groups[groupIndex].startLevel; |
| 1977 | const thisLevelIsVisible = |
| 1978 | parentGroupIsVisible && (visible || isFirstOnLevel && groups[groupIndex].style.useFirstLineForOverview); |
Yang Guo | 8fb3ac5 | 2019-07-31 20:17:50 | [diff] [blame] | 1979 | let height; |
| 1980 | if (groupIndex >= 0) { |
| 1981 | const group = groups[groupIndex]; |
| 1982 | const styleB = group.style; |
| 1983 | height = isFirstOnLevel && !styleB.shareHeaderLine || (styleB.collapsible && !group.expanded) ? |
| 1984 | styleB.height : |
| 1985 | (styleB.itemsHeight || this._barHeight); |
| 1986 | } else { |
| 1987 | height = this._barHeight; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1988 | } |
Yang Guo | 8fb3ac5 | 2019-07-31 20:17:50 | [diff] [blame] | 1989 | this._visibleLevels[level] = thisLevelIsVisible; |
| 1990 | this._visibleLevelOffsets[level] = currentOffset; |
| 1991 | this._visibleLevelHeights[level] = height; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1992 | if (thisLevelIsVisible || (parentGroupIsVisible && style && style.shareHeaderLine && isFirstOnLevel)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1993 | currentOffset += this._visibleLevelHeights[level]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1994 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1995 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1996 | if (groupIndex >= 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1997 | this._groupOffsets[groupIndex + 1] = currentOffset; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1998 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1999 | this._visibleLevelOffsets[level] = currentOffset; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2000 | if (this._useWebGL) { |
Alexei Filippov | 57ccafb | 2018-08-14 20:59:05 | [diff] [blame] | 2001 | this._setupGLGeometry(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2002 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /** |
| 2006 | * @param {number} index |
| 2007 | */ |
| 2008 | _isGroupCollapsible(index) { |
| 2009 | const groups = this._rawTimelineData.groups || []; |
| 2010 | const style = groups[index].style; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2011 | if (!style.shareHeaderLine || !style.collapsible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2012 | return !!style.collapsible; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2013 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2014 | const isLastGroup = index + 1 >= groups.length; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2015 | if (!isLastGroup && groups[index + 1].style.nestingLevel > style.nestingLevel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2016 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2017 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2018 | const nextGroupLevel = isLastGroup ? this._dataProvider.maxStackDepth() : groups[index + 1].startLevel; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2019 | if (nextGroupLevel !== groups[index].startLevel + 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2020 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2021 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2022 | // For groups that only have one line and share header line, pretend these are not collapsible |
| 2023 | // unless the itemsHeight does not match the headerHeight |
| 2024 | return style.height !== style.itemsHeight; |
| 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | * @param {number} entryIndex |
| 2029 | */ |
| 2030 | setSelectedEntry(entryIndex) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2031 | if (this._selectedEntryIndex === entryIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2032 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2033 | } |
| 2034 | if (entryIndex !== -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2035 | this._chartViewport.hideRangeSelection(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2036 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2037 | this._selectedEntryIndex = entryIndex; |
| 2038 | this._revealEntry(entryIndex); |
| 2039 | this._updateElementPosition(this._selectedElement, this._selectedEntryIndex); |
| 2040 | } |
| 2041 | |
| 2042 | /** |
| 2043 | * @param {!Element} element |
| 2044 | * @param {number} entryIndex |
| 2045 | */ |
| 2046 | _updateElementPosition(element, entryIndex) { |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 2047 | const elementMinWidthPx = 2; |
| 2048 | element.classList.add('hidden'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2049 | if (entryIndex === -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2050 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2051 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2052 | const timelineData = this._timelineData(); |
| 2053 | const startTime = timelineData.entryStartTimes[entryIndex]; |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 2054 | const duration = timelineData.entryTotalTimes[entryIndex]; |
Alexei Filippov | 6510650 | 2018-11-29 05:16:19 | [diff] [blame] | 2055 | let barX = 0; |
| 2056 | let barWidth = 0; |
| 2057 | let visible = true; |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 2058 | if (Number.isNaN(duration)) { |
| 2059 | const position = this._markerPositions.get(entryIndex); |
Alexei Filippov | 6510650 | 2018-11-29 05:16:19 | [diff] [blame] | 2060 | if (position) { |
| 2061 | barX = position.x; |
| 2062 | barWidth = position.width; |
| 2063 | } else { |
| 2064 | visible = false; |
| 2065 | } |
Alexei Filippov | 6c622e9 | 2018-11-10 02:13:59 | [diff] [blame] | 2066 | } else { |
| 2067 | barX = this._chartViewport.timeToPosition(startTime); |
| 2068 | barWidth = duration * this._chartViewport.timeToPixel(); |
| 2069 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2070 | if (barX + barWidth <= 0 || barX >= this._offsetWidth) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2071 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2072 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2073 | const barCenter = barX + barWidth / 2; |
| 2074 | barWidth = Math.max(barWidth, elementMinWidthPx); |
| 2075 | barX = barCenter - barWidth / 2; |
| 2076 | const entryLevel = timelineData.entryLevels[entryIndex]; |
| 2077 | const barY = this._levelToOffset(entryLevel) - this._chartViewport.scrollOffset(); |
| 2078 | const barHeight = this._levelHeight(entryLevel); |
| 2079 | const style = element.style; |
| 2080 | style.left = barX + 'px'; |
| 2081 | style.top = barY + 'px'; |
| 2082 | style.width = barWidth + 'px'; |
| 2083 | style.height = barHeight - 1 + 'px'; |
Alexei Filippov | 6510650 | 2018-11-29 05:16:19 | [diff] [blame] | 2084 | element.classList.toggle('hidden', !visible); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2085 | this._viewportElement.appendChild(element); |
| 2086 | } |
| 2087 | |
| 2088 | /** |
| 2089 | * @param {number} time |
| 2090 | * @return {number} |
| 2091 | */ |
| 2092 | _timeToPositionClipped(time) { |
Jack Franklin | 1be909c | 2020-03-04 08:57:41 | [diff] [blame] | 2093 | return Platform.NumberUtilities.clamp(this._chartViewport.timeToPosition(time), 0, this._offsetWidth); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | /** |
| 2097 | * @param {number} level |
| 2098 | * @return {number} |
| 2099 | */ |
| 2100 | _levelToOffset(level) { |
| 2101 | return this._visibleLevelOffsets[level]; |
| 2102 | } |
| 2103 | |
| 2104 | /** |
| 2105 | * @param {number} level |
| 2106 | * @return {number} |
| 2107 | */ |
| 2108 | _levelHeight(level) { |
| 2109 | return this._visibleLevelHeights[level]; |
| 2110 | } |
| 2111 | |
| 2112 | _updateBoundaries() { |
| 2113 | this._totalTime = this._dataProvider.totalTime(); |
| 2114 | this._minimumBoundary = this._dataProvider.minimumBoundary(); |
| 2115 | this._chartViewport.setBoundaries(this._minimumBoundary, this._totalTime); |
| 2116 | } |
| 2117 | |
| 2118 | _updateHeight() { |
Alexei Filippov | 23428d4 | 2018-04-28 01:09:07 | [diff] [blame] | 2119 | const height = this._levelToOffset(this._dataProvider.maxStackDepth()) + 2; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2120 | this._chartViewport.setContentHeight(height); |
| 2121 | } |
| 2122 | |
| 2123 | /** |
| 2124 | * @override |
| 2125 | */ |
| 2126 | onResize() { |
| 2127 | this.scheduleUpdate(); |
| 2128 | } |
| 2129 | |
| 2130 | /** |
| 2131 | * @override |
| 2132 | */ |
| 2133 | update() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2134 | if (!this._timelineData()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2135 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2136 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2137 | this._resetCanvas(); |
| 2138 | this._updateHeight(); |
| 2139 | this._updateBoundaries(); |
| 2140 | this._draw(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2141 | if (!this._chartViewport.isDragging()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2142 | this._updateHighlight(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 2143 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | reset() { |
| 2147 | this._chartViewport.reset(); |
| 2148 | this._rawTimelineData = null; |
| 2149 | this._rawTimelineDataLength = 0; |
| 2150 | this._highlightedMarkerIndex = -1; |
| 2151 | this._highlightedEntryIndex = -1; |
| 2152 | this._selectedEntryIndex = -1; |
| 2153 | /** @type {!Map<string,!Map<string,number>>} */ |
| 2154 | this._textWidth = new Map(); |
| 2155 | this._chartViewport.scheduleUpdate(); |
| 2156 | } |
| 2157 | |
| 2158 | scheduleUpdate() { |
| 2159 | this._chartViewport.scheduleUpdate(); |
| 2160 | } |
| 2161 | |
| 2162 | _enabled() { |
| 2163 | return this._rawTimelineDataLength !== 0; |
| 2164 | } |
| 2165 | |
| 2166 | /** |
| 2167 | * @override |
| 2168 | * @param {number} time |
| 2169 | * @return {number} |
| 2170 | */ |
| 2171 | computePosition(time) { |
| 2172 | return this._chartViewport.timeToPosition(time); |
| 2173 | } |
| 2174 | |
| 2175 | /** |
| 2176 | * @override |
| 2177 | * @param {number} value |
| 2178 | * @param {number=} precision |
| 2179 | * @return {string} |
| 2180 | */ |
| 2181 | formatValue(value, precision) { |
| 2182 | return this._dataProvider.formatValue(value - this.zeroTime(), precision); |
| 2183 | } |
| 2184 | |
| 2185 | /** |
| 2186 | * @override |
| 2187 | * @return {number} |
| 2188 | */ |
| 2189 | maximumBoundary() { |
| 2190 | return this._chartViewport.windowRightTime(); |
| 2191 | } |
| 2192 | |
| 2193 | /** |
| 2194 | * @override |
| 2195 | * @return {number} |
| 2196 | */ |
| 2197 | minimumBoundary() { |
| 2198 | return this._chartViewport.windowLeftTime(); |
| 2199 | } |
| 2200 | |
| 2201 | /** |
| 2202 | * @override |
| 2203 | * @return {number} |
| 2204 | */ |
| 2205 | zeroTime() { |
| 2206 | return this._dataProvider.minimumBoundary(); |
| 2207 | } |
| 2208 | |
| 2209 | /** |
| 2210 | * @override |
| 2211 | * @return {number} |
| 2212 | */ |
| 2213 | boundarySpan() { |
Alexei Filippov | 2578eb0 | 2018-04-11 08:15:05 | [diff] [blame] | 2214 | return this.maximumBoundary() - this.minimumBoundary(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2215 | } |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2216 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2217 | |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2218 | export const HeaderHeight = 15; |
| 2219 | export const MinimalTimeWindowMs = 0.5; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2220 | |
| 2221 | /** |
| 2222 | * @unrestricted |
| 2223 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2224 | export class TimelineData { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2225 | /** |
| 2226 | * @param {!Array<number>|!Uint16Array} entryLevels |
| 2227 | * @param {!Array<number>|!Float32Array} entryTotalTimes |
| 2228 | * @param {!Array<number>|!Float64Array} entryStartTimes |
Tim van der Lippe | 8ef250c | 2020-02-20 16:29:25 | [diff] [blame] | 2229 | * @param {?Array<!Group>} groups |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2230 | */ |
| 2231 | constructor(entryLevels, entryTotalTimes, entryStartTimes, groups) { |
| 2232 | this.entryLevels = entryLevels; |
| 2233 | this.entryTotalTimes = entryTotalTimes; |
| 2234 | this.entryStartTimes = entryStartTimes; |
| 2235 | this.groups = groups; |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2236 | /** @type {!Array.<!FlameChartMarker>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2237 | this.markers = []; |
| 2238 | this.flowStartTimes = []; |
| 2239 | this.flowStartLevels = []; |
| 2240 | this.flowEndTimes = []; |
| 2241 | this.flowEndLevels = []; |
Tim van der Lippe | 8ef250c | 2020-02-20 16:29:25 | [diff] [blame] | 2242 | /** @type {?Group} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2243 | this.selectedGroup = null; |
| 2244 | } |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2245 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2246 | |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2247 | /** |
| 2248 | * @interface |
| 2249 | */ |
| 2250 | export class FlameChartDataProvider { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2251 | /** |
| 2252 | * @return {number} |
| 2253 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2254 | minimumBoundary() { |
| 2255 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2256 | |
| 2257 | /** |
| 2258 | * @return {number} |
| 2259 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2260 | totalTime() { |
| 2261 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2262 | |
| 2263 | /** |
| 2264 | * @param {number} value |
| 2265 | * @param {number=} precision |
| 2266 | * @return {string} |
| 2267 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2268 | formatValue(value, precision) { |
| 2269 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2270 | |
| 2271 | /** |
| 2272 | * @return {number} |
| 2273 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2274 | maxStackDepth() { |
| 2275 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2276 | |
| 2277 | /** |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2278 | * @return {?TimelineData} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2279 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2280 | timelineData() { |
| 2281 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2282 | |
| 2283 | /** |
| 2284 | * @param {number} entryIndex |
| 2285 | * @return {?Element} |
| 2286 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2287 | prepareHighlightedEntryInfo(entryIndex) { |
| 2288 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2289 | |
| 2290 | /** |
| 2291 | * @param {number} entryIndex |
| 2292 | * @return {boolean} |
| 2293 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2294 | canJumpToEntry(entryIndex) { |
| 2295 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2296 | |
| 2297 | /** |
| 2298 | * @param {number} entryIndex |
| 2299 | * @return {?string} |
| 2300 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2301 | entryTitle(entryIndex) { |
| 2302 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2303 | |
| 2304 | /** |
| 2305 | * @param {number} entryIndex |
| 2306 | * @return {?string} |
| 2307 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2308 | entryFont(entryIndex) { |
| 2309 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2310 | |
| 2311 | /** |
| 2312 | * @param {number} entryIndex |
| 2313 | * @return {string} |
| 2314 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2315 | entryColor(entryIndex) { |
| 2316 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2317 | |
| 2318 | /** |
| 2319 | * @param {number} entryIndex |
| 2320 | * @param {!CanvasRenderingContext2D} context |
| 2321 | * @param {?string} text |
| 2322 | * @param {number} barX |
| 2323 | * @param {number} barY |
| 2324 | * @param {number} barWidth |
| 2325 | * @param {number} barHeight |
| 2326 | * @param {number} unclippedBarX |
| 2327 | * @param {number} timeToPixelRatio |
| 2328 | * @return {boolean} |
| 2329 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2330 | decorateEntry(entryIndex, context, text, barX, barY, barWidth, barHeight, unclippedBarX, timeToPixelRatio) { |
| 2331 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2332 | |
| 2333 | /** |
| 2334 | * @param {number} entryIndex |
| 2335 | * @return {boolean} |
| 2336 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2337 | forceDecoration(entryIndex) { |
| 2338 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2339 | |
| 2340 | /** |
| 2341 | * @param {number} entryIndex |
| 2342 | * @return {string} |
| 2343 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2344 | textColor(entryIndex) { |
| 2345 | } |
Paul Lewis | ead4575 | 2020-06-23 09:51:36 | [diff] [blame] | 2346 | |
| 2347 | /** |
| 2348 | * @return {!Map<string, !SDK.TracingModel.Event>} |
| 2349 | */ |
| 2350 | navStartTimes() { |
| 2351 | } |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2352 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2353 | |
| 2354 | /** |
| 2355 | * @interface |
| 2356 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2357 | export class FlameChartMarker { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2358 | /** |
| 2359 | * @return {number} |
| 2360 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2361 | startTime() { |
| 2362 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2363 | |
| 2364 | /** |
| 2365 | * @return {string} |
| 2366 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2367 | color() { |
| 2368 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2369 | |
| 2370 | /** |
Alexei Filippov | 72d792d | 2018-11-06 07:15:04 | [diff] [blame] | 2371 | * @return {?string} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2372 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2373 | title() { |
| 2374 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2375 | |
| 2376 | /** |
| 2377 | * @param {!CanvasRenderingContext2D} context |
| 2378 | * @param {number} x |
| 2379 | * @param {number} height |
| 2380 | * @param {number} pixelsPerMillisecond |
| 2381 | */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2382 | draw(context, x, height, pixelsPerMillisecond) { |
| 2383 | } |
| 2384 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2385 | |
| 2386 | /** @enum {symbol} */ |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2387 | export const Events = { |
Michael Liao | 712bbc2 | 2019-10-15 19:21:51 | [diff] [blame] | 2388 | CanvasFocused: Symbol('CanvasFocused'), |
| 2389 | EntryInvoked: Symbol('EntryInvoked'), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2390 | EntrySelected: Symbol('EntrySelected'), |
| 2391 | EntryHighlighted: Symbol('EntryHighlighted') |
| 2392 | }; |
| 2393 | |
Tim van der Lippe | fd2b2ce | 2020-01-03 15:05:18 | [diff] [blame] | 2394 | export const Colors = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2395 | SelectedGroupBackground: 'hsl(215, 85%, 98%)', |
| 2396 | SelectedGroupBorder: 'hsl(216, 68%, 54%)', |
| 2397 | }; |
Tim van der Lippe | cec9b76 | 2020-02-13 15:31:22 | [diff] [blame] | 2398 | |
| 2399 | /** |
| 2400 | * @typedef {!{ |
| 2401 | * name: string, |
| 2402 | * startLevel: number, |
| 2403 | * expanded: (boolean|undefined), |
| 2404 | * selectable: (boolean|undefined), |
| 2405 | * style: !GroupStyle |
| 2406 | * }} |
| 2407 | */ |
| 2408 | export let Group; |
| 2409 | |
| 2410 | /** |
| 2411 | * @typedef {!{ |
| 2412 | * height: number, |
| 2413 | * padding: number, |
| 2414 | * collapsible: boolean, |
| 2415 | * font: string, |
| 2416 | * color: string, |
| 2417 | * backgroundColor: string, |
| 2418 | * nestingLevel: number, |
| 2419 | * itemsHeight: (number|undefined), |
| 2420 | * shareHeaderLine: (boolean|undefined), |
| 2421 | * useFirstLineForOverview: (boolean|undefined), |
| 2422 | * useDecoratorsForOverview: (boolean|undefined) |
| 2423 | * }} |
| 2424 | */ |
| 2425 | export let GroupStyle; |