Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / service / gpu_tracer.h
blob596e60bfa76131e967bcc9338665eb71bcd453d3
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 virtual bool IsTracing();
77 // Retrieve the name of the current open trace.
78 // Returns empty string if no current open trace.
79 const std::string& CurrentCategory(GpuTracerSource source) const;
80 const std::string& CurrentName(GpuTracerSource source) const;
82 protected:
83 // Trace Processing.
84 virtual scoped_refptr<Outputter> CreateOutputter(const std::string& name);
85 virtual void PostTask();
87 void Process();
88 void ProcessTraces();
89 bool CheckDisjointStatus();
90 void ClearOngoingTraces(bool have_context);
92 void IssueProcessTask();
94 scoped_refptr<gfx::GPUTimingClient> gpu_timing_client_;
95 scoped_refptr<Outputter> outputter_;
96 std::vector<TraceMarker> markers_[NUM_TRACER_SOURCES];
97 std::deque<scoped_refptr<GPUTrace> > finished_traces_;
99 const unsigned char* gpu_trace_srv_category;
100 const unsigned char* gpu_trace_dev_category;
101 gles2::GLES2Decoder* decoder_;
102 int64 disjoint_time_ = 0;
104 bool gpu_executing_ = false;
105 bool process_posted_ = false;
106 bool began_device_traces_ = false;
108 private:
109 DISALLOW_COPY_AND_ASSIGN(GPUTracer);
112 class Outputter : public base::RefCounted<Outputter> {
113 public:
114 virtual void TraceDevice(GpuTracerSource source,
115 const std::string& category,
116 const std::string& name,
117 int64 start_time,
118 int64 end_time) = 0;
120 virtual void TraceServiceBegin(GpuTracerSource source,
121 const std::string& category,
122 const std::string& name) = 0;
124 virtual void TraceServiceEnd(GpuTracerSource source,
125 const std::string& category,
126 const std::string& name) = 0;
128 protected:
129 virtual ~Outputter() {}
130 friend class base::RefCounted<Outputter>;
133 class TraceOutputter : public Outputter {
134 public:
135 static scoped_refptr<TraceOutputter> Create(const std::string& name);
136 void TraceDevice(GpuTracerSource source,
137 const std::string& category,
138 const std::string& name,
139 int64 start_time,
140 int64 end_time) override;
142 void TraceServiceBegin(GpuTracerSource source,
143 const std::string& category,
144 const std::string& name) override;
146 void TraceServiceEnd(GpuTracerSource source,
147 const std::string& category,
148 const std::string& name) override;
150 protected:
151 friend class base::RefCounted<Outputter>;
152 explicit TraceOutputter(const std::string& name);
153 ~TraceOutputter() override;
155 base::Thread named_thread_;
156 uint64 local_trace_device_id_ = 0;
157 uint64 local_trace_service_id_ = 0;
159 std::stack<uint64> trace_service_id_stack_[NUM_TRACER_SOURCES];
161 private:
162 DISALLOW_COPY_AND_ASSIGN(TraceOutputter);
165 class GPU_EXPORT GPUTrace
166 : public base::RefCounted<GPUTrace> {
167 public:
168 GPUTrace(scoped_refptr<Outputter> outputter,
169 gfx::GPUTimingClient* gpu_timing_client,
170 const GpuTracerSource source,
171 const std::string& category,
172 const std::string& name,
173 const bool tracing_service,
174 const bool tracing_device);
176 void Destroy(bool have_context);
178 void Start();
179 void End();
180 bool IsAvailable();
181 bool IsServiceTraceEnabled() const { return service_enabled_; }
182 bool IsDeviceTraceEnabled() const { return device_enabled_; }
183 void Process();
185 private:
186 ~GPUTrace();
188 void Output();
190 friend class base::RefCounted<GPUTrace>;
192 const GpuTracerSource source_ = kTraceGroupInvalid;
193 const std::string category_;
194 const std::string name_;
195 scoped_refptr<Outputter> outputter_;
196 scoped_ptr<gfx::GPUTimer> gpu_timer_;
197 const bool service_enabled_ = false;
198 const bool device_enabled_ = false;
200 DISALLOW_COPY_AND_ASSIGN(GPUTrace);
203 class ScopedGPUTrace {
204 public:
205 ScopedGPUTrace(GPUTracer* gpu_tracer,
206 GpuTracerSource source,
207 const std::string& category,
208 const std::string& name)
209 : gpu_tracer_(gpu_tracer), source_(source) {
210 gpu_tracer_->Begin(category, name, source_);
213 ~ScopedGPUTrace() { gpu_tracer_->End(source_); }
215 private:
216 GPUTracer* gpu_tracer_;
217 GpuTracerSource source_;
220 } // namespace gles2
221 } // namespace gpu
223 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_