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.
8 * @fileoverview Renders an array of slices into the provided div,
9 * using a child canvas element. Uses a FastRectRenderer to draw only
13 tvcm.requireStylesheet('tracing.tracks.track');
15 tvcm.require('tvcm.ui');
16 tvcm.require('tvcm.ui.container_that_decorates_its_children');
18 tvcm.exportTo('tracing.tracks', function() {
20 * The base class for all tracks.
23 var Track = tvcm.ui.define('track',
24 tvcm.ui.ContainerThatDecoratesItsChildren);
26 __proto__: tvcm.ui.ContainerThatDecoratesItsChildren.prototype,
28 decorate: function(viewport) {
29 tvcm.ui.ContainerThatDecoratesItsChildren.prototype.decorate.call(this);
30 if (viewport === undefined)
31 throw new Error('viewport is required when creating a Track.');
33 this.viewport_ = viewport;
34 this.classList.add('track');
38 return this.viewport_;
42 // This is a little weird here, but we have to be able to walk up the
43 // parent tree to get the context.
46 if (!this.parentNode.context)
47 throw new Error('Parent container does not support context() method.');
48 return this.parentNode.context();
51 decorateChild_: function(childTrack) {
54 undecorateChild_: function(childTrack) {
55 if (childTrack.detach)
59 updateContents_: function() {
62 drawTrack: function(type) {
63 var ctx = this.context();
65 var pixelRatio = window.devicePixelRatio || 1;
66 var bounds = this.getBoundingClientRect();
67 var canvasBounds = ctx.canvas.getBoundingClientRect();
70 ctx.translate(0, pixelRatio * (bounds.top - canvasBounds.top));
72 var dt = this.viewport.currentDisplayTransform;
73 var viewLWorld = dt.xViewToWorld(0);
74 var viewRWorld = dt.xViewToWorld(bounds.width * pixelRatio);
76 this.draw(type, viewLWorld, viewRWorld);
80 draw: function(type, viewLWorld, viewRWorld) {
83 addEventsToTrackMap: function(eventToTrackMap) {
86 addIntersectingItemsInRangeToSelection: function(
87 loVX, hiVX, loVY, hiVY, selection) {
89 var pixelRatio = window.devicePixelRatio || 1;
90 var dt = this.viewport.currentDisplayTransform;
91 var viewPixWidthWorld = dt.xViewVectorToWorld(1);
92 var loWX = dt.xViewToWorld(loVX * pixelRatio);
93 var hiWX = dt.xViewToWorld(hiVX * pixelRatio);
95 var clientRect = this.getBoundingClientRect();
96 var a = Math.max(loVY, clientRect.top);
97 var b = Math.min(hiVY, clientRect.bottom);
101 this.addIntersectingItemsInRangeToSelectionInWorldSpace(
102 loWX, hiWX, viewPixWidthWorld, selection);
105 addIntersectingItemsInRangeToSelectionInWorldSpace: function(
106 loWX, hiWX, viewPixWidthWorld, selection) {
110 * Gets implemented by supporting track types. The method adds the event
111 * closest to worldX to the selection.
113 * @param {number} worldX The position that is looked for.
114 * @param {number} worldMaxDist The maximum distance allowed from worldX to
116 * @param {number} loY Lower Y bound of the search interval in view space.
117 * @param {number} hiY Upper Y bound of the search interval in view space.
118 * @param {Selection} selection Selection to which to add hits.
120 addClosestEventToSelection: function(
121 worldX, worldMaxDist, loY, hiY, selection) {
124 addClosestInstantEventToSelection: function(instantEvents, worldX,
125 worldMaxDist, selection) {
126 var instantEvent = tvcm.findClosestElementInSortedArray(
128 function(x) { return x.start; },
135 selection.push(instantEvent);