| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 | |
| 6 /** | |
| 7 * @fileoverview State and UI for trace data collection. | |
| 8 */ | |
| 9 cr.define('gpu', function() { | |
| 10 | |
| 11 function TracingController() { | |
| 12 this.overlay_ = document.createElement('div'); | |
| 13 this.overlay_.className = 'gpu-tracing-overlay'; | |
| 14 | |
| 15 cr.ui.decorate(this.overlay_, gpu.Overlay); | |
| 16 | |
| 17 this.statusDiv_ = document.createElement('div'); | |
| 18 this.overlay_.appendChild(this.statusDiv_); | |
| 19 | |
| 20 this.bufferPercentDiv_ = document.createElement('div'); | |
| 21 this.overlay_.appendChild(this.bufferPercentDiv_); | |
| 22 | |
| 23 this.stopButton_ = document.createElement('button'); | |
| 24 this.stopButton_.onclick = this.endTracing.bind(this); | |
| 25 this.stopButton_.textContent = 'Stop tracing'; | |
| 26 this.overlay_.appendChild(this.stopButton_); | |
| 27 | |
| 28 this.traceEvents_ = []; | |
| 29 | |
| 30 if (browserBridge.debugMode) { | |
| 31 var tracingControllerTests = document.createElement('script'); | |
| 32 tracingControllerTests.src = | |
| 33 './gpu_internals/tracing_controller_tests.js'; | |
| 34 document.body.appendChild(tracingControllerTests); | |
| 35 } | |
| 36 | |
| 37 this.onKeydownBoundToThis_ = this.onKeydown_.bind(this); | |
| 38 this.onKeypressBoundToThis_ = this.onKeypress_.bind(this); | |
| 39 } | |
| 40 | |
| 41 TracingController.prototype = { | |
| 42 __proto__: cr.EventTarget.prototype, | |
| 43 | |
| 44 tracingEnabled_: false, | |
| 45 tracingEnding_: false, | |
| 46 | |
| 47 onRequestBufferPercentFullComplete: function(percent_full) { | |
| 48 if (!this.overlay_.visible) | |
| 49 return; | |
| 50 | |
| 51 window.setTimeout(this.beginRequestBufferPercentFull_.bind(this), 250); | |
| 52 | |
| 53 this.bufferPercentDiv_.textContent = 'Buffer usage: ' + | |
| 54 Math.round(100 * percent_full) + '%'; | |
| 55 }, | |
| 56 | |
| 57 /** | |
| 58 * Begin requesting the buffer fullness | |
| 59 */ | |
| 60 beginRequestBufferPercentFull_: function() { | |
| 61 chrome.send('beginRequestBufferPercentFull'); | |
| 62 }, | |
| 63 | |
| 64 /** | |
| 65 * Called by info_view to empty the trace buffer | |
| 66 */ | |
| 67 beginTracing: function() { | |
| 68 if (this.tracingEnabled_) | |
| 69 throw Error('Tracing already begun.'); | |
| 70 | |
| 71 this.stopButton_.hidden = false; | |
| 72 this.statusDiv_.textContent = 'Tracing active.'; | |
| 73 this.overlay_.visible = true; | |
| 74 | |
| 75 this.tracingEnabled_ = true; | |
| 76 console.log('Beginning to trace...'); | |
| 77 this.statusDiv_.textContent = 'Tracing active.'; | |
| 78 | |
| 79 this.traceEvents_ = []; | |
| 80 if (!browserBridge.debugMode) { | |
| 81 chrome.send('beginTracing'); | |
| 82 this.beginRequestBufferPercentFull_(); | |
| 83 } else { | |
| 84 gpu.tracingControllerTestHarness.beginTracing(); | |
| 85 } | |
| 86 | |
| 87 this.tracingEnabled_ = true; | |
| 88 | |
| 89 var e = new cr.Event('traceBegun'); | |
| 90 e.events = this.traceEvents_; | |
| 91 this.dispatchEvent(e); | |
| 92 | |
| 93 e = new cr.Event('traceEventsChanged'); | |
| 94 e.numEvents = this.traceEvents_.length; | |
| 95 this.dispatchEvent(e); | |
| 96 | |
| 97 window.addEventListener('keypress', this.onKeypressBoundToThis_); | |
| 98 window.addEventListener('keydown', this.onKeydownBoundToThis_); | |
| 99 }, | |
| 100 | |
| 101 onKeydown_: function(e) { | |
| 102 if (e.keyCode == 27) { | |
| 103 this.endTracing(); | |
| 104 } | |
| 105 }, | |
| 106 | |
| 107 onKeypress_: function(e) { | |
| 108 if (e.keyIdentifier == 'Enter') { | |
| 109 this.endTracing(); | |
| 110 } | |
| 111 }, | |
| 112 /** | |
| 113 * Checks whether tracing is enabled | |
| 114 */ | |
| 115 get isTracingEnabled() { | |
| 116 return this.tracingEnabled_; | |
| 117 }, | |
| 118 | |
| 119 /** | |
| 120 * Gets the currently traced events. If tracing is active, then | |
| 121 * this can change on the fly. | |
| 122 */ | |
| 123 get traceEvents() { | |
| 124 return this.traceEvents_; | |
| 125 }, | |
| 126 | |
| 127 /** | |
| 128 * Callbed by gpu c++ code when new GPU trace data arrives. | |
| 129 */ | |
| 130 onTraceDataCollected: function(events) { | |
| 131 this.statusDiv_.textContent = 'Processing trace...'; | |
| 132 this.traceEvents_.push.apply(this.traceEvents_, events); | |
| 133 }, | |
| 134 | |
| 135 /** | |
| 136 * Called by info_view to finish tracing and update all views. | |
| 137 */ | |
| 138 endTracing: function() { | |
| 139 if (!this.tracingEnabled_) throw new Error('Tracing not begun.'); | |
| 140 if (this.tracingEnding_) return; | |
| 141 this.tracingEnding_ = true; | |
| 142 | |
| 143 this.statusDiv_.textContent = 'Ending trace...'; | |
| 144 console.log('Finishing trace'); | |
| 145 this.statusDiv_.textContent = 'Downloading trace data...'; | |
| 146 this.stopButton_.hidden = true; | |
| 147 // delay sending endTracingAsync until we get a chance to | |
| 148 // update the screen... | |
| 149 window.setTimeout(function() { | |
| 150 if (!browserBridge.debugMode) { | |
| 151 chrome.send('endTracingAsync'); | |
| 152 } else { | |
| 153 gpu.tracingControllerTestHarness.endTracing(); | |
| 154 } | |
| 155 }, 100); | |
| 156 }, | |
| 157 | |
| 158 /** | |
| 159 * Called by the browser when all processes complete tracing. | |
| 160 */ | |
| 161 onEndTracingComplete: function() { | |
| 162 window.removeEventListener('keydown', this.onKeydownBoundToThis_); | |
| 163 window.removeEventListener('keypress', this.onKeypressBoundToThis_); | |
| 164 this.overlay_.visible = false; | |
| 165 this.tracingEnabled_ = false; | |
| 166 this.tracingEnding_ = false; | |
| 167 console.log('onEndTracingComplete p1 with ' + | |
| 168 this.traceEvents_.length + ' events.'); | |
| 169 var e = new cr.Event('traceEnded'); | |
| 170 e.events = this.traceEvents_; | |
| 171 this.dispatchEvent(e); | |
| 172 }, | |
| 173 | |
| 174 /** | |
| 175 * Tells browser to put up a load dialog and load the trace file | |
| 176 */ | |
| 177 beginLoadTraceFile: function() { | |
| 178 chrome.send('loadTraceFile'); | |
| 179 }, | |
| 180 | |
| 181 /** | |
| 182 * Called by the browser when a trace file is loaded. | |
| 183 */ | |
| 184 onLoadTraceFileComplete: function(data) { | |
| 185 if (data.traceEvents) { | |
| 186 this.traceEvents_ = data.traceEvents; | |
| 187 } else { // path for loading traces saved without metadata | |
| 188 if (!data.length) | |
| 189 console.log('Expected an array when loading the trace file'); | |
| 190 else | |
| 191 this.traceEvents_ = data; | |
| 192 } | |
| 193 var e = new cr.Event('loadTraceFileComplete'); | |
| 194 e.events = this.traceEvents_; | |
| 195 this.dispatchEvent(e); | |
| 196 }, | |
| 197 | |
| 198 /** | |
| 199 * Called by the browser when loading a trace file was canceled. | |
| 200 */ | |
| 201 onLoadTraceFileCanceled: function() { | |
| 202 cr.dispatchSimpleEvent(this, 'loadTraceFileCanceled'); | |
| 203 }, | |
| 204 | |
| 205 /** | |
| 206 * Tells browser to put up a save dialog and save the trace file | |
| 207 */ | |
| 208 beginSaveTraceFile: function(traceEvents) { | |
| 209 var data = { | |
| 210 traceEvents: traceEvents, | |
| 211 clientInfo: browserBridge.clientInfo, | |
| 212 gpuInfo: browserBridge.gpuInfo | |
| 213 }; | |
| 214 chrome.send('saveTraceFile', [JSON.stringify(data)]); | |
| 215 }, | |
| 216 | |
| 217 /** | |
| 218 * Called by the browser when a trace file is saveed. | |
| 219 */ | |
| 220 onSaveTraceFileComplete: function() { | |
| 221 cr.dispatchSimpleEvent(this, 'saveTraceFileComplete'); | |
| 222 }, | |
| 223 | |
| 224 /** | |
| 225 * Called by the browser when saving a trace file was canceled. | |
| 226 */ | |
| 227 onSaveTraceFileCanceled: function() { | |
| 228 cr.dispatchSimpleEvent(this, 'saveTraceFileCanceled'); | |
| 229 }, | |
| 230 | |
| 231 selfTest: function() { | |
| 232 this.beginTracing(); | |
| 233 window.setTimeout(this.endTracing.bind(This), 500); | |
| 234 } | |
| 235 }; | |
| 236 return { | |
| 237 TracingController: TracingController | |
| 238 }; | |
| 239 }); | |
| 240 | |
| OLD | NEW |