1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview State and UI for trace data collection.
*/
cr.define('gpu', function() {
function TracingController() {
this.startButton_ = document.createElement('div');
this.startButton_.className = 'gpu-tracing-start-button';
this.startButton_.textContent = 'Start tracing';
this.startButton_.onclick = this.beginTracing.bind(this);
document.body.appendChild(this.startButton_);
this.overlay_ = document.createElement('div');
this.overlay_.className = 'gpu-tracing-overlay';
cr.ui.decorate(this.overlay_, gpu.Overlay);
var statusDiv = document.createElement('div');
statusDiv.textContent = 'Tracing active.';
this.overlay_.appendChild(statusDiv);
var stopButton = document.createElement('button');
stopButton.onclick = this.endTracing.bind(this);
stopButton.innerText = 'Stop tracing';
this.overlay_.appendChild(stopButton);
this.traceEvents_ = [];
if (browserBridge.debugMode) {
var tracingControllerTests = document.createElement('script');
tracingControllerTests.src =
'./gpu_internals/tracing_controller_tests.js';
document.body.appendChild(tracingControllerTests);
}
this.onKeydownBoundToThis_ = this.onKeydown_.bind(this);
this.onKeypressBoundToThis_ = this.onKeypress_.bind(this);
}
TracingController.prototype = {
__proto__: cr.EventTarget.prototype,
tracingEnabled_: false,
/**
* Called by info_view to empty the trace buffer
*/
beginTracing: function() {
if (this.tracingEnabled_)
throw Error('Tracing already begun.');
this.overlay_.visible = true;
this.tracingEnabled_ = true;
console.log('Beginning to trace...');
this.traceEvents_ = [];
if (!browserBridge.debugMode)
chrome.send('beginTracing');
this.tracingEnabled_ = true;
var e = new cr.Event('traceBegun');
e.events = this.traceEvents_;
this.dispatchEvent(e);
e = new cr.Event('traceEventsChanged');
e.numEvents = this.traceEvents_.length;
this.dispatchEvent(e);
window.addEventListener('keypress', this.onKeypressBoundToThis_);
window.addEventListener('keydown', this.onKeydownBoundToThis_);
// In debug mode, stop tracing automatically
if (browserBridge.debugMode)
window.setTimeout(this.endTracing.bind(this), 100);
},
onKeydown_: function(e) {
if (e.keyCode == 27) {
this.endTracing();
}
},
onKeypress_: function(e) {
if (e.keyIdentifier == 'Enter') {
this.endTracing();
}
},
/**
* Checks whether tracing is enabled
*/
get isTracingEnabled() {
return this.tracingEnabled_;
},
/**
* Gets the currently traced events. If tracing is active, then
* this can change on the fly.
*/
get traceEvents() {
return this.traceEvents_;
},
/**
* Callbed by gpu c++ code when new GPU trace data arrives.
*/
onTraceDataCollected: function(events) {
this.traceEvents_.push.apply(this.traceEvents_, events);
},
/**
* Called by info_view to finish tracing and update all views.
*/
endTracing: function() {
if (!this.tracingEnabled_) throw new Error('Tracing not begun.');
window.removeEventListener('keydown', this.onKeydownBoundToThis_);
window.removeEventListener('keypress', this.onKeypressBoundToThis_);
console.log('Finishing trace');
if (!browserBridge.debugMode) {
chrome.send('endTracingAsync');
} else {
var events = tracingControllerTestEvents;
this.onTraceDataCollected(events);
window.setTimeout(this.onEndTracingComplete.bind(this), 250);
}
},
/**
* Called by the browser when all processes complete tracing.
*/
onEndTracingComplete: function() {
this.overlay_.visible = false;
this.tracingEnabled_ = false;
console.log('onEndTracingComplete p1 with ' +
this.traceEvents_.length + ' events.');
var e = new cr.Event('traceEnded');
e.events = this.traceEvents_;
this.dispatchEvent(e);
},
selfTest: function() {
this.beginTracing();
window.setTimeout(this.endTracing.bind(This), 500);
}
};
return {
TracingController: TracingController
};
});
|