OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "gpu/command_buffer/service/gpu_tracer.h" | |
6 | |
7 #include <deque> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/debug/trace_event.h" | |
11 #include "base/threading/thread.h" | |
12 #include "base/time.h" | |
13 #include "ui/gl/gl_bindings.h" | |
14 | |
15 namespace gpu { | |
16 namespace gles2 { | |
17 namespace { | |
18 | |
19 static const unsigned int kGPUTraceMaxQueueSize = 1024; | |
20 | |
21 class Outputter | |
22 : public base::Thread, | |
23 public base::RefCountedThreadSafe<Outputter> { | |
24 public: | |
25 static scoped_refptr<Outputter> Create(const std::string& name) { | |
26 if (!g_outputter_thread) { | |
27 g_outputter_thread = new Outputter(name); | |
28 g_outputter_thread->Start(); | |
29 } | |
30 return g_outputter_thread; | |
31 } | |
32 | |
33 private: | |
34 explicit Outputter(const std::string& name) : base::Thread(name.c_str()) {} | |
35 | |
36 virtual ~Outputter() { | |
37 g_outputter_thread->Stop(); | |
38 g_outputter_thread = NULL; | |
39 } | |
40 | |
41 static Outputter* g_outputter_thread; | |
apatrick
2012/12/03 22:05:41
You could just make this a regular global in an an
dsinclair
2012/12/04 15:59:27
Done.
| |
42 | |
43 friend class base::RefCountedThreadSafe<Outputter>; | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(Outputter); | |
46 }; | |
47 | |
48 Outputter* Outputter::g_outputter_thread = NULL; | |
49 | |
50 class Trace : public base::RefCountedThreadSafe<Trace> { | |
51 public: | |
52 explicit Trace(const std::string& name) : name_(name) {} | |
53 | |
54 virtual void Start() = 0; | |
55 virtual void End() = 0; | |
56 | |
57 // True if the the results of this query are available. | |
58 virtual bool IsAvailable() = 0; | |
59 | |
60 virtual void Process() = 0; | |
61 | |
62 virtual std::string name() { | |
63 return name_; | |
64 } | |
65 | |
66 protected: | |
67 virtual ~Trace() {} | |
68 | |
69 private: | |
70 friend class base::RefCountedThreadSafe<Trace>; | |
71 | |
72 std::string name_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(Trace); | |
75 }; | |
76 | |
77 class GLARBTimerTrace : public Trace { | |
78 public: | |
79 GLARBTimerTrace(scoped_refptr<Outputter> output, const std::string& name, | |
80 int64 offset); | |
81 | |
82 // Implementation of Tracer | |
83 virtual void Start() OVERRIDE; | |
84 virtual void End() OVERRIDE; | |
85 virtual bool IsAvailable() OVERRIDE; | |
86 virtual void Process() OVERRIDE; | |
87 | |
88 private: | |
89 ~GLARBTimerTrace() OVERRIDE; | |
90 | |
91 void Output(); | |
92 | |
93 scoped_refptr<Outputter> outputter_; | |
94 | |
95 int64 offset_; | |
96 int64 start_time_; | |
97 int64 end_time_; | |
98 | |
99 GLuint queries_[2]; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(GLARBTimerTrace); | |
102 }; | |
103 | |
104 class NoopTrace : public Trace { | |
105 public: | |
106 explicit NoopTrace(const std::string& name) : Trace(name) {} | |
107 | |
108 // Implementation of Tracer | |
109 virtual void Start() OVERRIDE {} | |
110 virtual void End() OVERRIDE {} | |
111 virtual bool IsAvailable() OVERRIDE { return true; } | |
112 virtual void Process() OVERRIDE {} | |
113 | |
114 private: | |
115 ~NoopTrace() {} | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(NoopTrace); | |
118 }; | |
119 | |
120 class GPUTracerImpl : public GPUTracer { | |
121 public: | |
122 GPUTracerImpl() | |
123 : gpu_category_enabled_(TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu")) { | |
124 } | |
125 virtual ~GPUTracerImpl() {} | |
126 | |
127 // Implementation of gpu::gles2::GPUTracer | |
128 virtual bool Begin(const std::string& name) OVERRIDE; | |
129 virtual bool End() OVERRIDE; | |
130 virtual void Process() OVERRIDE; | |
131 virtual std::string CurrentName() const OVERRIDE; | |
132 | |
133 protected: | |
134 // Create a new trace. | |
135 virtual scoped_refptr<Trace> CreateTrace(const std::string& name); | |
136 | |
137 const unsigned char* gpu_category_enabled_; | |
138 | |
139 private: | |
140 scoped_refptr<Trace> current_trace_; | |
141 std::deque<scoped_refptr<Trace> > traces_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(GPUTracerImpl); | |
144 }; | |
145 | |
146 class GPUTracerARBTimerQuery : public GPUTracerImpl { | |
147 public: | |
148 GPUTracerARBTimerQuery(); | |
149 virtual ~GPUTracerARBTimerQuery(); | |
150 | |
151 // Implementation of gpu::gles2::GPUTracer | |
152 virtual void Process() OVERRIDE; | |
153 | |
154 private: | |
155 // Implementation of GPUTracerImpl. | |
156 virtual scoped_refptr<Trace> CreateTrace(const std::string& name) OVERRIDE; | |
157 | |
158 void CalculateTimerOffset(); | |
159 | |
160 scoped_refptr<Outputter> outputter_; | |
161 | |
162 int64 timer_offset_; | |
163 int64 last_offset_check_; | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(GPUTracerARBTimerQuery); | |
166 }; | |
167 | |
168 GLARBTimerTrace::GLARBTimerTrace(scoped_refptr<Outputter> output, | |
169 const std::string& name, int64 offset) | |
170 : Trace(name), | |
171 outputter_(output), | |
172 offset_(offset), | |
173 start_time_(0), | |
174 end_time_(0) { | |
175 glGenQueries(2, queries_); | |
176 } | |
177 | |
178 GLARBTimerTrace::~GLARBTimerTrace() { | |
179 } | |
180 | |
181 void GLARBTimerTrace::Start() { | |
182 glQueryCounter(queries_[0], GL_TIMESTAMP); | |
183 } | |
184 | |
185 void GLARBTimerTrace::End() { | |
186 glQueryCounter(queries_[1], GL_TIMESTAMP); | |
187 } | |
188 | |
189 bool GLARBTimerTrace::IsAvailable() { | |
190 GLint done = 0; | |
191 glGetQueryObjectiv(queries_[1], GL_QUERY_RESULT_AVAILABLE, &done); | |
192 return !!done; | |
193 } | |
194 | |
195 void GLARBTimerTrace::Process() { | |
196 DCHECK(IsAvailable()); | |
197 | |
198 GLint64 timestamp; | |
199 | |
200 glGetQueryObjecti64v(queries_[0], GL_QUERY_RESULT, ×tamp); | |
201 start_time_ = (timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_; | |
202 | |
203 glGetQueryObjecti64v(queries_[1], GL_QUERY_RESULT, ×tamp); | |
204 end_time_ = (timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_; | |
205 | |
206 glDeleteQueries(2, queries_); | |
207 | |
208 outputter_->message_loop()->PostTask(FROM_HERE, | |
209 base::Bind(&GLARBTimerTrace::Output, this)); | |
210 } | |
211 | |
212 void GLARBTimerTrace::Output() { | |
213 // TODO(dsinclair): Output TRACE_EVENT | |
214 } | |
215 | |
216 bool GPUTracerImpl::Begin(const std::string& name) { | |
217 // Make sure we are not nesting trace commands. | |
218 if (current_trace_.get()) | |
219 return false; | |
220 | |
221 current_trace_ = CreateTrace(name); | |
222 current_trace_->Start(); | |
223 return true; | |
224 } | |
225 | |
226 bool GPUTracerImpl::End() { | |
227 if (!current_trace_.get()) | |
228 return false; | |
229 | |
230 current_trace_->End(); | |
231 traces_.push_back(current_trace_); | |
232 current_trace_ = NULL; | |
233 | |
234 if (traces_.size() > kGPUTraceMaxQueueSize) | |
235 Process(); | |
236 | |
237 return true; | |
238 } | |
239 | |
240 void GPUTracerImpl::Process() { | |
241 while (!traces_.empty() && traces_.front()->IsAvailable()) { | |
242 traces_.front()->Process(); | |
243 traces_.pop_front(); | |
244 } | |
245 } | |
246 | |
247 std::string GPUTracerImpl::CurrentName() const { | |
248 if (!current_trace_) | |
249 return NULL; | |
250 return current_trace_->name(); | |
251 } | |
252 | |
253 scoped_refptr<Trace> GPUTracerImpl::CreateTrace(const std::string& name) { | |
254 return new NoopTrace(name); | |
255 } | |
256 | |
257 GPUTracerARBTimerQuery::GPUTracerARBTimerQuery() : GPUTracerImpl() { | |
258 CalculateTimerOffset(); | |
259 outputter_ = Outputter::Create("GL_ARB_timer_query"); | |
260 } | |
261 | |
262 GPUTracerARBTimerQuery::~GPUTracerARBTimerQuery() { | |
263 } | |
264 | |
265 scoped_refptr<Trace> GPUTracerARBTimerQuery::CreateTrace( | |
266 const std::string& name) { | |
267 if (*gpu_category_enabled_) | |
268 return new GLARBTimerTrace(outputter_, name, timer_offset_); | |
269 return GPUTracerImpl::CreateTrace(name); | |
270 } | |
271 | |
272 void GPUTracerARBTimerQuery::Process() { | |
273 GPUTracerImpl::Process(); | |
274 | |
275 if (*gpu_category_enabled_ && | |
276 (last_offset_check_ + base::Time::kMicrosecondsPerSecond) < | |
277 base::TimeTicks::NowFromSystemTraceTime().ToInternalValue()) | |
278 CalculateTimerOffset(); | |
279 } | |
280 | |
281 void GPUTracerARBTimerQuery::CalculateTimerOffset() { | |
282 TRACE_EVENT0("gpu", "CalculateTimerOffset"); | |
283 // TODO(dsinclair): Change to glGetInteger64v. | |
284 GLuint64 gl_now = 0; | |
285 GLuint query; | |
286 glGenQueries(1, &query); | |
287 | |
288 glQueryCounter(query, GL_TIMESTAMP); | |
289 glGetQueryObjectui64v(query, GL_QUERY_RESULT, &gl_now); | |
290 base::TimeTicks system_now = base::TimeTicks::NowFromSystemTraceTime(); | |
291 | |
292 gl_now /= base::Time::kNanosecondsPerMicrosecond; | |
293 timer_offset_ = system_now.ToInternalValue() - gl_now; | |
294 glDeleteQueries(1, &query); | |
295 | |
296 last_offset_check_ = system_now.ToInternalValue(); | |
297 } | |
298 | |
299 } // namespace | |
300 | |
301 scoped_ptr<GPUTracer> GPUTracer::Create() { | |
302 if (gfx::g_driver_gl.ext.b_GL_ARB_timer_query) | |
303 return scoped_ptr<GPUTracer>(new GPUTracerARBTimerQuery()); | |
304 return scoped_ptr<GPUTracer>(new GPUTracerImpl()); | |
305 } | |
306 | |
307 } // namespace gles2 | |
308 } // namespace gpu | |
OLD | NEW |