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 bool HasTracesToProcess();
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;
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;
106 DISALLOW_COPY_AND_ASSIGN(GPUTracer
);
109 class Outputter
: public base::RefCounted
<Outputter
> {
111 virtual void TraceDevice(GpuTracerSource source
,
112 const std::string
& category
,
113 const std::string
& name
,
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;
126 virtual ~Outputter() {}
127 friend class base::RefCounted
<Outputter
>;
130 class TraceOutputter
: public Outputter
{
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
,
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
;
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
];
159 DISALLOW_COPY_AND_ASSIGN(TraceOutputter
);
162 class GPU_EXPORT GPUTrace
163 : public base::RefCounted
<GPUTrace
> {
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
);
178 bool IsServiceTraceEnabled() const { return service_enabled_
; }
179 bool IsDeviceTraceEnabled() const { return device_enabled_
; }
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
{
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_
); }
213 GPUTracer
* gpu_tracer_
;
214 GpuTracerSource source_
;
220 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_