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_
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"
25 // Id used to keep trace namespaces separate
26 enum GpuTracerSource
{
27 kTraceGroupInvalid
= -1,
29 kTraceGroupMarker
= 0,
37 kTracerTypeInvalid
= -1,
40 kTracerTypeDisjointTimer
43 // Central accesser to CPU Time
44 class GPU_EXPORT CPUTime
45 : public base::RefCounted
<CPUTime
> {
49 virtual int64
GetCurrentTime();
53 friend class base::RefCounted
<CPUTime
>;
55 DISALLOW_COPY_AND_ASSIGN(CPUTime
);
58 // Marker structure for a Trace.
60 TraceMarker(const std::string
& category
, const std::string
& name
);
63 std::string category_
;
65 scoped_refptr
<GPUTrace
> trace_
;
68 // Traces GPU Commands.
69 class GPU_EXPORT GPUTracer
70 : public base::SupportsWeakPtr
<GPUTracer
> {
72 explicit GPUTracer(gles2::GLES2Decoder
* decoder
);
75 // Scheduled processing in decoder begins.
78 // Scheduled processing in decoder ends.
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;
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();
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_
;
121 GpuTracerType tracer_type_
;
122 bool gpu_timing_synced_
;
124 bool process_posted_
;
126 DISALLOW_COPY_AND_ASSIGN(GPUTracer
);
129 class Outputter
: public base::RefCounted
<Outputter
> {
131 virtual void TraceDevice(const std::string
& category
,
132 const std::string
& name
,
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;
143 virtual ~Outputter() {}
144 friend class base::RefCounted
<Outputter
>;
147 class TraceOutputter
: public Outputter
{
149 static scoped_refptr
<TraceOutputter
> Create(const std::string
& name
);
150 void TraceDevice(const std::string
& category
,
151 const std::string
& name
,
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
;
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
> {
175 GPUTrace(scoped_refptr
<Outputter
> outputter
,
176 scoped_refptr
<CPUTime
> cpu_time
,
177 const std::string
& category
,
178 const std::string
& name
,
180 GpuTracerType tracer_type
);
182 bool IsEnabled() { return tracer_type_
!= kTracerTypeInvalid
; }
184 void Start(bool trace_service
);
185 void End(bool tracing_service
);
194 friend class base::RefCounted
<GPUTrace
>;
196 std::string category_
;
198 scoped_refptr
<Outputter
> outputter_
;
199 scoped_refptr
<CPUTime
> cpu_time_
;
204 GpuTracerType tracer_type_
;
209 DISALLOW_COPY_AND_ASSIGN(GPUTrace
);
212 class ScopedGPUTrace
{
214 ScopedGPUTrace(GPUTracer
* gpu_tracer
, GpuTracerSource source
,
215 const std::string
& category
, const std::string
& name
)
216 : gpu_tracer_(gpu_tracer
),
218 gpu_tracer_
->Begin(category
, name
, source_
);
222 gpu_tracer_
->End(source_
);
226 GPUTracer
* gpu_tracer_
;
227 GpuTracerSource source_
;
233 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_