Make GL bindings conditional
[chromium-blink-merge.git] / gpu / command_buffer / service / gpu_tracer.h
blob594a98ebcbdc3532bf2eae34206c9cc1546f005e
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.
5 // This file contains the GPUTrace class.
6 #ifndef GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_
7 #define GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread.h"
15 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
16 #include "gpu/gpu_export.h"
17 #include "ui/gl/gl_bindings.h"
19 namespace gpu {
20 namespace gles2 {
22 class Outputter;
23 class GPUTrace;
25 // Id used to keep trace namespaces separate
26 enum GpuTracerSource {
27 kTraceGroupInvalid = -1,
29 kTraceGroupMarker = 0,
30 kTraceCHROMIUM = 1,
31 kTraceDecoder = 2,
33 NUM_TRACER_SOURCES
36 enum GpuTracerType {
37 kTracerTypeInvalid = -1,
39 kTracerTypeARBTimer,
40 kTracerTypeDisjointTimer
43 // Central accesser to CPU Time
44 class GPU_EXPORT CPUTime
45 : public base::RefCounted<CPUTime> {
46 public:
47 CPUTime();
49 virtual int64 GetCurrentTime();
51 protected:
52 virtual ~CPUTime();
53 friend class base::RefCounted<CPUTime>;
55 DISALLOW_COPY_AND_ASSIGN(CPUTime);
58 // Marker structure for a Trace.
59 struct TraceMarker {
60 TraceMarker(const std::string& category, const std::string& name);
61 ~TraceMarker();
63 std::string category_;
64 std::string name_;
65 scoped_refptr<GPUTrace> trace_;
68 // Traces GPU Commands.
69 class GPU_EXPORT GPUTracer
70 : public base::SupportsWeakPtr<GPUTracer> {
71 public:
72 explicit GPUTracer(gles2::GLES2Decoder* decoder);
73 virtual ~GPUTracer();
75 // Scheduled processing in decoder begins.
76 bool BeginDecoding();
78 // Scheduled processing in decoder ends.
79 bool EndDecoding();
81 // Begin a trace marker.
82 bool Begin(const std::string& category, const std::string& name,
83 GpuTracerSource source);
85 // End the last started trace marker.
86 bool End(GpuTracerSource source);
88 virtual bool IsTracing();
90 // Retrieve the name of the current open trace.
91 // Returns empty string if no current open trace.
92 const std::string& CurrentCategory(GpuTracerSource source) const;
93 const std::string& CurrentName(GpuTracerSource source) const;
95 protected:
96 // Trace Processing.
97 scoped_refptr<GPUTrace> CreateTrace(const std::string& category,
98 const std::string& name);
99 virtual scoped_refptr<Outputter> CreateOutputter(const std::string& name);
100 virtual scoped_refptr<CPUTime> CreateCPUTime();
101 virtual GpuTracerType DetermineTracerType();
102 virtual void PostTask();
104 void Process();
105 void ProcessTraces();
107 void CalculateTimerOffset();
108 void IssueProcessTask();
110 scoped_refptr<Outputter> outputter_;
111 scoped_refptr<CPUTime> cpu_time_;
112 std::vector<TraceMarker> markers_[NUM_TRACER_SOURCES];
113 std::deque<scoped_refptr<GPUTrace> > traces_;
115 const unsigned char* gpu_trace_srv_category;
116 const unsigned char* gpu_trace_dev_category;
117 gles2::GLES2Decoder* decoder_;
119 int64 timer_offset_;
121 GpuTracerType tracer_type_;
122 bool gpu_timing_synced_;
123 bool gpu_executing_;
124 bool process_posted_;
126 DISALLOW_COPY_AND_ASSIGN(GPUTracer);
129 class Outputter : public base::RefCounted<Outputter> {
130 public:
131 virtual void TraceDevice(const std::string& category,
132 const std::string& name,
133 int64 start_time,
134 int64 end_time) = 0;
136 virtual void TraceServiceBegin(const std::string& category,
137 const std::string& name) = 0;
139 virtual void TraceServiceEnd(const std::string& category,
140 const std::string& name) = 0;
142 protected:
143 virtual ~Outputter() {}
144 friend class base::RefCounted<Outputter>;
147 class TraceOutputter : public Outputter {
148 public:
149 static scoped_refptr<TraceOutputter> Create(const std::string& name);
150 void TraceDevice(const std::string& category,
151 const std::string& name,
152 int64 start_time,
153 int64 end_time) override;
155 void TraceServiceBegin(const std::string& category,
156 const std::string& name) override;
158 void TraceServiceEnd(const std::string& category,
159 const std::string& name) override;
161 protected:
162 friend class base::RefCounted<Outputter>;
163 explicit TraceOutputter(const std::string& name);
164 ~TraceOutputter() override;
166 base::Thread named_thread_;
167 uint64 local_trace_id_;
169 DISALLOW_COPY_AND_ASSIGN(TraceOutputter);
172 class GPU_EXPORT GPUTrace
173 : public base::RefCounted<GPUTrace> {
174 public:
175 GPUTrace(scoped_refptr<Outputter> outputter,
176 scoped_refptr<CPUTime> cpu_time,
177 const std::string& category,
178 const std::string& name,
179 int64 offset,
180 GpuTracerType tracer_type);
182 bool IsEnabled() { return tracer_type_ != kTracerTypeInvalid; }
184 void Start(bool trace_service);
185 void End(bool tracing_service);
186 bool IsAvailable();
187 void Process();
189 private:
190 ~GPUTrace();
192 void Output();
194 friend class base::RefCounted<GPUTrace>;
196 std::string category_;
197 std::string name_;
198 scoped_refptr<Outputter> outputter_;
199 scoped_refptr<CPUTime> cpu_time_;
201 int64 offset_;
202 int64 start_time_;
203 int64 end_time_;
204 GpuTracerType tracer_type_;
205 bool end_requested_;
207 GLuint queries_[2];
209 DISALLOW_COPY_AND_ASSIGN(GPUTrace);
212 class ScopedGPUTrace {
213 public:
214 ScopedGPUTrace(GPUTracer* gpu_tracer, GpuTracerSource source,
215 const std::string& category, const std::string& name)
216 : gpu_tracer_(gpu_tracer),
217 source_(source) {
218 gpu_tracer_->Begin(category, name, source_);
221 ~ScopedGPUTrace() {
222 gpu_tracer_->End(source_);
225 private:
226 GPUTracer* gpu_tracer_;
227 GpuTracerSource source_;
230 } // namespace gles2
231 } // namespace gpu
233 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_