Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / gpu / command_buffer / service / gpu_tracer.h
blobcb3f8902cac9720122b79dc9532870c02ec9056d
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 <deque>
10 #include <stack>
11 #include <string>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/thread.h"
18 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
19 #include "gpu/gpu_export.h"
21 namespace gfx {
22 class GPUTimingClient;
23 class GPUTimer;
26 namespace gpu {
27 namespace gles2 {
29 class Outputter;
30 class GPUTrace;
32 // Id used to keep trace namespaces separate
33 enum GpuTracerSource {
34 kTraceGroupInvalid = -1,
36 kTraceCHROMIUM,
37 kTraceDecoder,
38 kTraceDisjoint, // Used internally.
40 NUM_TRACER_SOURCES
43 // Marker structure for a Trace.
44 struct TraceMarker {
45 TraceMarker(const std::string& category, const std::string& name);
46 ~TraceMarker();
48 std::string category_;
49 std::string name_;
50 scoped_refptr<GPUTrace> trace_;
53 // Traces GPU Commands.
54 class GPU_EXPORT GPUTracer
55 : public base::SupportsWeakPtr<GPUTracer> {
56 public:
57 explicit GPUTracer(gles2::GLES2Decoder* decoder);
58 virtual ~GPUTracer();
60 void Destroy(bool have_context);
62 // Scheduled processing in decoder begins.
63 bool BeginDecoding();
65 // Scheduled processing in decoder ends.
66 bool EndDecoding();
68 // Begin a trace marker.
69 bool Begin(const std::string& category, const std::string& name,
70 GpuTracerSource source);
72 // End the last started trace marker.
73 bool End(GpuTracerSource source);
75 bool HasTracesToProcess();
76 void ProcessTraces();
78 virtual bool IsTracing();
80 // Retrieve the name of the current open trace.
81 // Returns empty string if no current open trace.
82 const std::string& CurrentCategory(GpuTracerSource source) const;
83 const std::string& CurrentName(GpuTracerSource source) const;
85 protected:
86 // Trace Processing.
87 virtual scoped_refptr<Outputter> CreateOutputter(const std::string& name);
89 bool CheckDisjointStatus();
90 void ClearOngoingTraces(bool have_context);
92 scoped_refptr<gfx::GPUTimingClient> gpu_timing_client_;
93 scoped_refptr<Outputter> outputter_;
94 std::vector<TraceMarker> markers_[NUM_TRACER_SOURCES];
95 std::deque<scoped_refptr<GPUTrace> > finished_traces_;
97 const unsigned char* gpu_trace_srv_category;
98 const unsigned char* gpu_trace_dev_category;
99 gles2::GLES2Decoder* decoder_;
100 int64 disjoint_time_ = 0;
102 bool gpu_executing_ = false;
103 bool began_device_traces_ = false;
105 private:
106 DISALLOW_COPY_AND_ASSIGN(GPUTracer);
109 class Outputter : public base::RefCounted<Outputter> {
110 public:
111 virtual void TraceDevice(GpuTracerSource source,
112 const std::string& category,
113 const std::string& name,
114 int64 start_time,
115 int64 end_time) = 0;
117 virtual void TraceServiceBegin(GpuTracerSource source,
118 const std::string& category,
119 const std::string& name) = 0;
121 virtual void TraceServiceEnd(GpuTracerSource source,
122 const std::string& category,
123 const std::string& name) = 0;
125 protected:
126 virtual ~Outputter() {}
127 friend class base::RefCounted<Outputter>;
130 class TraceOutputter : public Outputter {
131 public:
132 static scoped_refptr<TraceOutputter> Create(const std::string& name);
133 void TraceDevice(GpuTracerSource source,
134 const std::string& category,
135 const std::string& name,
136 int64 start_time,
137 int64 end_time) override;
139 void TraceServiceBegin(GpuTracerSource source,
140 const std::string& category,
141 const std::string& name) override;
143 void TraceServiceEnd(GpuTracerSource source,
144 const std::string& category,
145 const std::string& name) override;
147 protected:
148 friend class base::RefCounted<Outputter>;
149 explicit TraceOutputter(const std::string& name);
150 ~TraceOutputter() override;
152 base::Thread named_thread_;
153 uint64 local_trace_device_id_ = 0;
154 uint64 local_trace_service_id_ = 0;
156 std::stack<uint64> trace_service_id_stack_[NUM_TRACER_SOURCES];
158 private:
159 DISALLOW_COPY_AND_ASSIGN(TraceOutputter);
162 class GPU_EXPORT GPUTrace
163 : public base::RefCounted<GPUTrace> {
164 public:
165 GPUTrace(scoped_refptr<Outputter> outputter,
166 gfx::GPUTimingClient* gpu_timing_client,
167 const GpuTracerSource source,
168 const std::string& category,
169 const std::string& name,
170 const bool tracing_service,
171 const bool tracing_device);
173 void Destroy(bool have_context);
175 void Start();
176 void End();
177 bool IsAvailable();
178 bool IsServiceTraceEnabled() const { return service_enabled_; }
179 bool IsDeviceTraceEnabled() const { return device_enabled_; }
180 void Process();
182 private:
183 ~GPUTrace();
185 void Output();
187 friend class base::RefCounted<GPUTrace>;
189 const GpuTracerSource source_ = kTraceGroupInvalid;
190 const std::string category_;
191 const std::string name_;
192 scoped_refptr<Outputter> outputter_;
193 scoped_ptr<gfx::GPUTimer> gpu_timer_;
194 const bool service_enabled_ = false;
195 const bool device_enabled_ = false;
197 DISALLOW_COPY_AND_ASSIGN(GPUTrace);
200 class ScopedGPUTrace {
201 public:
202 ScopedGPUTrace(GPUTracer* gpu_tracer,
203 GpuTracerSource source,
204 const std::string& category,
205 const std::string& name)
206 : gpu_tracer_(gpu_tracer), source_(source) {
207 gpu_tracer_->Begin(category, name, source_);
210 ~ScopedGPUTrace() { gpu_tracer_->End(source_); }
212 private:
213 GPUTracer* gpu_tracer_;
214 GpuTracerSource source_;
217 } // namespace gles2
218 } // namespace gpu
220 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_