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_
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"
22 class GPUTimingClient
;
32 // Id used to keep trace namespaces separate
33 enum GpuTracerSource
{
34 kTraceGroupInvalid
= -1,
38 kTraceDisjoint
, // Used internally.
43 // Marker structure for a Trace.
45 TraceMarker(const std::string
& category
, const std::string
& name
);
48 std::string category_
;
50 scoped_refptr
<GPUTrace
> trace_
;
53 // Traces GPU Commands.
54 class GPU_EXPORT GPUTracer
55 : public base::SupportsWeakPtr
<GPUTracer
> {
57 explicit GPUTracer(gles2::GLES2Decoder
* decoder
);
60 void Destroy(bool have_context
);
62 // Scheduled processing in decoder begins.
65 // Scheduled processing in decoder ends.
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;
84 virtual scoped_refptr
<Outputter
> CreateOutputter(const std::string
& name
);
85 virtual void PostTask();
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;
109 DISALLOW_COPY_AND_ASSIGN(GPUTracer
);
112 class Outputter
: public base::RefCounted
<Outputter
> {
114 virtual void TraceDevice(GpuTracerSource source
,
115 const std::string
& category
,
116 const std::string
& name
,
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;
129 virtual ~Outputter() {}
130 friend class base::RefCounted
<Outputter
>;
133 class TraceOutputter
: public Outputter
{
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
,
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
;
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
];
162 DISALLOW_COPY_AND_ASSIGN(TraceOutputter
);
165 class GPU_EXPORT GPUTrace
166 : public base::RefCounted
<GPUTrace
> {
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
);
181 bool IsServiceTraceEnabled() const { return service_enabled_
; }
182 bool IsDeviceTraceEnabled() const { return device_enabled_
; }
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
{
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_
); }
216 GPUTracer
* gpu_tracer_
;
217 GpuTracerSource source_
;
223 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_