1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.require('tvcm.sorted_array_utils');
8 tvcm.require('tracing.tracks.container_track');
9 tvcm.require('tvcm.ui');
11 tvcm.exportTo('tracing.tracks', function() {
14 * A track that displays a SliceGroup.
16 * @extends {ContainerTrack}
19 var SliceGroupTrack = tvcm.ui.define(
20 'slice-group-track', tracing.tracks.ContainerTrack);
22 SliceGroupTrack.prototype = {
24 __proto__: tracing.tracks.ContainerTrack.prototype,
26 decorate: function(viewport) {
27 tracing.tracks.ContainerTrack.prototype.decorate.call(this, viewport);
28 this.classList.add('slice-group-track');
39 this.updateContents_();
48 this.updateContents_();
57 this.updateContents_();
60 addSliceTrack_: function(slices) {
61 var track = new tracing.tracks.SliceTrack(this.viewport);
62 track.slices = slices;
63 this.appendChild(track);
68 return tvcm.asArray(this.children).map(function(sliceTrack) {
69 return sliceTrack.slices;
73 get hasVisibleContent() {
74 return this.children.length > 0;
77 updateContents_: function() {
79 this.updateHeadingAndTooltip_();
83 var slices = this.group_.slices;
84 if (this.areArrayContentsSame_(this.filteredSlices_, slices)) {
85 this.updateHeadingAndTooltip_();
89 this.filteredSlices_ = slices;
94 var subRows = this.buildSubRows_(slices);
95 for (var srI = 0; srI < subRows.length; srI++) {
96 var subRow = subRows[srI];
99 this.addSliceTrack_(subRow);
101 this.updateHeadingAndTooltip_();
104 updateHeadingAndTooltip_: function() {
105 if (!this.firstChild)
107 this.firstChild.heading = this.heading_;
108 this.firstChild.tooltip = this.tooltip_;
112 * Breaks up the list of slices into N rows, each of which is a list of
113 * slices that are non overlapping.
115 buildSubRows_: function(slices) {
116 // This function works by walking through slices by start time.
118 // The basic idea here is to insert each slice as deep into the subrow
119 // list as it can go such that every subSlice is fully contained by its
122 // Visually, if we start with this:
127 // To place this slice:
129 // We first check row 2's last item, [d]. [e] wont fit into [d] (they dont
130 // even intersect). So we go to row 1. That gives us [b], and [d] wont fit
131 // into that either. So, we go to row 0 and its last slice, [a]. That can
132 // completely contain [e], so that means we should add [e] as a subchild
133 // of [a]. That puts it on row 1, yielding:
138 // If we then get this slice:
140 // We do the same deepest-to-shallowest walk of the subrows trying to fit
141 // it. This time, it doesn't fit in any open slice. So, we simply append
150 for (var i = 0; i < slices.length; i++) {
151 if (slices[i].subSlices)
152 slices[i].subSlices.splice(0,
153 slices[i].subSlices.length);
157 ops.sort(function(ix, iy) {
160 if (x.start != y.start)
161 return x.start - y.start;
163 // Elements get inserted into the slices array in order of when the
164 // slices start. Because slices must be properly nested, we break
165 // start-time ties by assuming that the elements appearing earlier in
166 // the slices array (and thus ending earlier) start earlier.
171 this.badSlices_ = []; // TODO(simonjam): Connect this again.
173 for (var i = 0; i < ops.length; i++) {
175 var slice = slices[op];
177 // Try to fit the slice into the existing subrows.
178 var inserted = false;
179 for (var j = subRows.length - 1; j >= 0; j--) {
180 if (subRows[j].length == 0)
183 var insertedSlice = subRows[j][subRows[j].length - 1];
184 if (slice.start < insertedSlice.start) {
185 this.badSlices_.push(slice);
188 if (insertedSlice.bounds(slice)) {
189 // Insert it into subRow j + 1.
190 while (subRows.length <= j + 1)
192 subRows[j + 1].push(slice);
193 if (insertedSlice.subSlices)
194 insertedSlice.subSlices.push(slice);
202 // Append it to subRow[0] as a root.
203 subRows[0].push(slice);
209 areArrayContentsSame_: function(a, b) {
212 if (!a.length || !b.length)
214 if (a.length != b.length)
216 for (var i = 0; i < a.length; ++i) {
225 SliceGroupTrack: SliceGroupTrack