Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / tracing / tracks / process_track.js
1 // Copyright (c) 2013 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.
4
5 'use strict';
6
7 tvcm.require('tracing.tracks.process_track_base');
8 tvcm.require('tracing.draw_helpers');
9
10 tvcm.exportTo('tracing.tracks', function() {
11   var ProcessTrackBase = tracing.tracks.ProcessTrackBase;
12
13   /**
14    * @constructor
15    */
16   var ProcessTrack = tvcm.ui.define('process-track', ProcessTrackBase);
17
18   ProcessTrack.prototype = {
19     __proto__: ProcessTrackBase.prototype,
20
21     decorate: function(viewport) {
22       tracing.tracks.ProcessTrackBase.prototype.decorate.call(this, viewport);
23     },
24
25     drawTrack: function(type) {
26       switch (type) {
27         case tracing.tracks.DrawType.INSTANT_EVENT:
28           if (!this.processBase.instantEvents ||
29               this.processBase.instantEvents.length === 0)
30             break;
31
32           var ctx = this.context();
33
34           var pixelRatio = window.devicePixelRatio || 1;
35           var bounds = this.getBoundingClientRect();
36           var canvasBounds = ctx.canvas.getBoundingClientRect();
37
38           ctx.save();
39           ctx.translate(0, pixelRatio * (bounds.top - canvasBounds.top));
40
41           var dt = this.viewport.currentDisplayTransform;
42           var viewLWorld = dt.xViewToWorld(0);
43           var viewRWorld = dt.xViewToWorld(
44               bounds.width * pixelRatio);
45
46           tracing.drawInstantSlicesAsLines(
47               ctx,
48               this.viewport.currentDisplayTransform,
49               viewLWorld,
50               viewRWorld,
51               bounds.height,
52               this.processBase.instantEvents,
53               1);
54
55           ctx.restore();
56
57           break;
58
59         case tracing.tracks.DrawType.BACKGROUND:
60           this.drawBackground_();
61           // Don't bother recursing further, Process is the only level that
62           // draws backgrounds.
63           return;
64       }
65
66       tracing.tracks.ContainerTrack.prototype.drawTrack.call(this, type);
67     },
68
69     drawBackground_: function() {
70       var ctx = this.context();
71       var canvasBounds = ctx.canvas.getBoundingClientRect();
72       var pixelRatio = window.devicePixelRatio || 1;
73
74       var draw = false;
75       ctx.fillStyle = '#eee';
76       for (var i = 0; i < this.children.length; ++i) {
77         if (!(this.children[i] instanceof tracing.tracks.Track) ||
78             (this.children[i] instanceof tracing.tracks.SpacingTrack))
79           continue;
80
81         draw = !draw;
82         if (!draw)
83           continue;
84
85         var bounds = this.children[i].getBoundingClientRect();
86         ctx.fillRect(0, pixelRatio * (bounds.top - canvasBounds.top),
87             ctx.canvas.width, pixelRatio * bounds.height);
88       }
89     },
90
91     // Process maps to processBase because we derive from ProcessTrackBase.
92     set process(process) {
93       this.processBase = process;
94     },
95
96     get process() {
97       return this.processBase;
98     },
99
100     addIntersectingItemsInRangeToSelectionInWorldSpace: function(
101         loWX, hiWX, viewPixWidthWorld, selection) {
102       function onPickHit(instantEvent) {
103         selection.push(instantEvent);
104       }
105       tvcm.iterateOverIntersectingIntervals(this.processBase.instantEvents,
106           function(x) { return x.start; },
107           function(x) { return x.duration; },
108           loWX, hiWX,
109           onPickHit.bind(this));
110
111       tracing.tracks.ContainerTrack.prototype.
112           addIntersectingItemsInRangeToSelectionInWorldSpace.
113           apply(this, arguments);
114     },
115
116     addClosestEventToSelection: function(worldX, worldMaxDist, loY, hiY,
117                                          selection) {
118       this.addClosestInstantEventToSelection(this.processBase.instantEvents,
119                                              worldX, worldMaxDist, selection);
120       tracing.tracks.ContainerTrack.prototype.addClosestEventToSelection.
121           apply(this, arguments);
122     }
123   };
124
125   return {
126     ProcessTrack: ProcessTrack
127   };
128 });