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
31/**
32 * @constructor
33 * @extends {WebInspector.TimelineOverviewBase}
34 * @param {!WebInspector.TimelineModel} model
35 */
36WebInspector.TimelineEventOverview = function(model)
37{
38    WebInspector.TimelineOverviewBase.call(this, model);
39
40    this.element.id = "timeline-overview-events";
41
42    this._fillStyles = {};
43    var categories = WebInspector.TimelinePresentationModel.categories();
44    for (var category in categories) {
45        this._fillStyles[category] = WebInspector.TimelinePresentationModel.createFillStyleForCategory(this._context, 0, WebInspector.TimelineEventOverview._stripGradientHeight, categories[category]);
46        categories[category].addEventListener(WebInspector.TimelineCategory.Events.VisibilityChanged, this._onCategoryVisibilityChanged, this);
47    }
48
49    this._disabledCategoryFillStyle = WebInspector.TimelinePresentationModel.createFillStyle(this._context, 0, WebInspector.TimelineEventOverview._stripGradientHeight,
50        "rgb(218, 218, 218)", "rgb(170, 170, 170)", "rgb(143, 143, 143)");
51
52    this._disabledCategoryBorderStyle = "rgb(143, 143, 143)";
53}
54
55/** @const */
56WebInspector.TimelineEventOverview._numberOfStrips = 3;
57
58/** @const */
59WebInspector.TimelineEventOverview._stripGradientHeight = 120;
60
61WebInspector.TimelineEventOverview.prototype = {
62    update: function()
63    {
64        this.resetCanvas();
65
66        var stripHeight = Math.round(this._canvas.height  / WebInspector.TimelineEventOverview._numberOfStrips);
67        var timeOffset = this._model.minimumRecordTime();
68        var timeSpan = this._model.maximumRecordTime() - timeOffset;
69        var scale = this._canvas.width / timeSpan;
70
71        var lastBarByGroup = [];
72
73        this._context.fillStyle = "rgba(0, 0, 0, 0.05)";
74        for (var i = 1; i < WebInspector.TimelineEventOverview._numberOfStrips; i += 2)
75            this._context.fillRect(0.5, i * stripHeight + 0.5, this._canvas.width, stripHeight);
76
77        /**
78         * @this {WebInspector.TimelineEventOverview}
79         */
80        function appendRecord(record)
81        {
82            if (record.type === WebInspector.TimelineModel.RecordType.BeginFrame)
83                return;
84            var recordStart = Math.floor((WebInspector.TimelineModel.startTimeInSeconds(record) - timeOffset) * scale);
85            var recordEnd = Math.ceil((WebInspector.TimelineModel.endTimeInSeconds(record) - timeOffset) * scale);
86            var category = WebInspector.TimelinePresentationModel.categoryForRecord(record);
87            if (category.overviewStripGroupIndex < 0)
88                return;
89            var bar = lastBarByGroup[category.overviewStripGroupIndex];
90            // This bar may be merged with previous -- so just adjust the previous bar.
91            const barsMergeThreshold = 2;
92            if (bar && bar.category === category && bar.end + barsMergeThreshold >= recordStart) {
93                if (recordEnd > bar.end)
94                    bar.end = recordEnd;
95                return;
96            }
97            if (bar)
98                this._renderBar(bar.start, bar.end, stripHeight, bar.category);
99            lastBarByGroup[category.overviewStripGroupIndex] = { start: recordStart, end: recordEnd, category: category };
100        }
101        WebInspector.TimelinePresentationModel.forAllRecords(this._model.records, appendRecord.bind(this));
102        for (var i = 0; i < lastBarByGroup.length; ++i) {
103            if (lastBarByGroup[i])
104                this._renderBar(lastBarByGroup[i].start, lastBarByGroup[i].end, stripHeight, lastBarByGroup[i].category);
105        }
106    },
107
108    _onCategoryVisibilityChanged: function()
109    {
110        this.update();
111    },
112
113    /**
114     * @param {number} begin
115     * @param {number} end
116     * @param {number} height
117     * @param {!WebInspector.TimelineCategory} category
118     */
119    _renderBar: function(begin, end, height, category)
120    {
121        const stripPadding = 4 * window.devicePixelRatio;
122        const innerStripHeight = height - 2 * stripPadding;
123
124        var x = begin + 0.5;
125        var y = category.overviewStripGroupIndex * height + stripPadding + 0.5;
126        var width = Math.max(end - begin, 1);
127
128        this._context.save();
129        this._context.translate(x, y);
130        this._context.scale(1, innerStripHeight / WebInspector.TimelineEventOverview._stripGradientHeight);
131        this._context.fillStyle = category.hidden ? this._disabledCategoryFillStyle : this._fillStyles[category.name];
132        this._context.fillRect(0, 0, width, WebInspector.TimelineEventOverview._stripGradientHeight);
133        this._context.strokeStyle = category.hidden ? this._disabledCategoryBorderStyle : category.borderColor;
134        this._context.strokeRect(0, 0, width, WebInspector.TimelineEventOverview._stripGradientHeight);
135        this._context.restore();
136    },
137
138    __proto__: WebInspector.TimelineOverviewBase.prototype
139}
140