Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / gpu / command_buffer / service / gpu_tracer.h
blob1bd19730223467756aa952c629817418bebbc03b
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 <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread.h"
17 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
18 #include "gpu/gpu_export.h"
20 namespace gfx {
21 class GPUTimingClient;
22 class GPUTimer;
25 namespace gpu {
26 namespace gles2 {
28 class Outputter;
29 class GPUTrace;
31 // Id used to keep trace namespaces separate
32 enum GpuTracerSource {
33 kTraceGroupInvalid = -1,
35 kTraceGroupMarker = 0,
36 kTraceCHROMIUM = 1,
37 kTraceDecoder = 2,
39 NUM_TRACER_SOURCES
42 // Marker structure for a Trace.
43 struct TraceMarker {
44 TraceMarker(const std::string& category, const std::string& name);
45 ~TraceMarker();
47 std::string category_;
48 std::string name_;
49 scoped_refptr<GPUTrace> trace_;
52 // Traces GPU Commands.
53 class GPU_EXPORT GPUTracer
54 : public base::SupportsWeakPtr<GPUTracer> {
55 public:
56 explicit GPUTracer(gles2::GLES2Decoder* decoder);
57 virtual ~GPUTracer();
59 void Destroy(bool have_context);
61 // Scheduled processing in decoder begins.
62 bool BeginDecoding();
64 // Scheduled processing in decoder ends.
65 bool EndDecoding();
67 // Begin a trace marker.
68 bool Begin(const std::string& category, const std::string& name,
69 GpuTracerSource source);
71 // End the last started trace marker.
72 bool End(GpuTracerSource source);
74 virtual bool IsTracing();
76 // Retrieve the name of the current open trace.
77 // Returns empty string if no current open trace.
78 const std::string& CurrentCategory(GpuTracerSource source) const;
79 const std::string& CurrentName(GpuTracerSource source) const;
81 protected:
82 // Trace Processing.
83 virtual scoped_refptr<Outputter> CreateOutputter(const std::string& name);
84 virtual void PostTask();
86 void Process();
87 void ProcessTraces();
88 void ClearFinishedTraces(bool have_context);
90 void IssueProcessTask();
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_;
101 bool gpu_executing_;
102 bool process_posted_;
104 private:
105 DISALLOW_COPY_AND_ASSIGN(GPUTracer);
108 class Outputter : public base::RefCounted<Outputter> {
109 public:
110 virtual void TraceDevice(const std::string& category,
111 const std::string& name,
112 int64 start_time,
113 int64 end_time) = 0;
115 virtual void TraceServiceBegin(const std::string& category,
116 const std::string& name) = 0;
118 virtual void TraceServiceEnd(const std::string& category,
119 const std::string& name) = 0;
121 protected:
122 virtual ~Outputter() {}
123 friend class base::RefCounted<Outputter>;
126 class TraceOutputter : public Outputter {
127 public:
128 static scoped_refptr<TraceOutputter> Create(const std::string& name);
129 void TraceDevice(const std::string& category,
130 const std::string& name,
131 int64 start_time,
132 int64 end_time) override;
134 void TraceServiceBegin(const std::string& category,
135 const std::string& name) override;
137 void TraceServiceEnd(const std::string& category,
138 const std::string& name) override;
140 protected:
141 friend class base::RefCounted<Outputter>;
142 explicit TraceOutputter(const std::string& name);
143 ~TraceOutputter() override;
145 base::Thread named_thread_;
146 uint64 local_trace_id_;
148 private:
149 DISALLOW_COPY_AND_ASSIGN(TraceOutputter);
152 class GPU_EXPORT GPUTrace
153 : public base::RefCounted<GPUTrace> {
154 public:
155 GPUTrace(scoped_refptr<Outputter> outputter,
156 gfx::GPUTimingClient* gpu_timing_client,
157 const std::string& category,
158 const std::string& name,
159 const bool tracing_service,
160 const bool tracing_device);
162 void Destroy(bool have_context);
164 void Start();
165 void End();
166 bool IsAvailable();
167 bool IsServiceTraceEnabled() const { return service_enabled_; }
168 bool IsDeviceTraceEnabled() const { return device_enabled_; }
169 void Process();
171 private:
172 ~GPUTrace();
174 void Output();
176 friend class base::RefCounted<GPUTrace>;
178 std::string category_;
179 std::string name_;
180 scoped_refptr<Outputter> outputter_;
181 scoped_ptr<gfx::GPUTimer> gpu_timer_;
182 const bool service_enabled_ = false;
183 const bool device_enabled_ = false;
185 DISALLOW_COPY_AND_ASSIGN(GPUTrace);
188 class ScopedGPUTrace {
189 public:
190 ScopedGPUTrace(GPUTracer* gpu_tracer,
191 GpuTracerSource source,
192 const std::string& category,
193 const std::string& name)
194 : gpu_tracer_(gpu_tracer), source_(source) {
195 gpu_tracer_->Begin(category, name, source_);
198 ~ScopedGPUTrace() { gpu_tracer_->End(source_); }
200 private:
201 GPUTracer* gpu_tracer_;
202 GpuTracerSource source_;
205 } // namespace gles2
206 } // namespace gpu
208 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_