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_
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"
21 class GPUTimingClient
;
31 // Id used to keep trace namespaces separate
32 enum GpuTracerSource
{
33 kTraceGroupInvalid
= -1,
35 kTraceGroupMarker
= 0,
42 // Marker structure for a Trace.
44 TraceMarker(const std::string
& category
, const std::string
& name
);
47 std::string category_
;
49 scoped_refptr
<GPUTrace
> trace_
;
52 // Traces GPU Commands.
53 class GPU_EXPORT GPUTracer
54 : public base::SupportsWeakPtr
<GPUTracer
> {
56 explicit GPUTracer(gles2::GLES2Decoder
* decoder
);
59 void Destroy(bool have_context
);
61 // Scheduled processing in decoder begins.
64 // Scheduled processing in decoder ends.
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;
83 virtual scoped_refptr
<Outputter
> CreateOutputter(const std::string
& name
);
84 virtual void PostTask();
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_
;
102 bool process_posted_
;
105 DISALLOW_COPY_AND_ASSIGN(GPUTracer
);
108 class Outputter
: public base::RefCounted
<Outputter
> {
110 virtual void TraceDevice(GpuTracerSource source
,
111 const std::string
& category
,
112 const std::string
& name
,
116 virtual void TraceServiceBegin(GpuTracerSource source
,
117 const std::string
& category
,
118 const std::string
& name
) = 0;
120 virtual void TraceServiceEnd(GpuTracerSource source
,
121 const std::string
& category
,
122 const std::string
& name
) = 0;
125 virtual ~Outputter() {}
126 friend class base::RefCounted
<Outputter
>;
129 class TraceOutputter
: public Outputter
{
131 static scoped_refptr
<TraceOutputter
> Create(const std::string
& name
);
132 void TraceDevice(GpuTracerSource source
,
133 const std::string
& category
,
134 const std::string
& name
,
136 int64 end_time
) override
;
138 void TraceServiceBegin(GpuTracerSource source
,
139 const std::string
& category
,
140 const std::string
& name
) override
;
142 void TraceServiceEnd(GpuTracerSource source
,
143 const std::string
& category
,
144 const std::string
& name
) override
;
147 friend class base::RefCounted
<Outputter
>;
148 explicit TraceOutputter(const std::string
& name
);
149 ~TraceOutputter() override
;
151 base::Thread named_thread_
;
154 DISALLOW_COPY_AND_ASSIGN(TraceOutputter
);
157 class GPU_EXPORT GPUTrace
158 : public base::RefCounted
<GPUTrace
> {
160 GPUTrace(scoped_refptr
<Outputter
> outputter
,
161 gfx::GPUTimingClient
* gpu_timing_client
,
162 const GpuTracerSource source
,
163 const std::string
& category
,
164 const std::string
& name
,
165 const bool tracing_service
,
166 const bool tracing_device
);
168 void Destroy(bool have_context
);
173 bool IsServiceTraceEnabled() const { return service_enabled_
; }
174 bool IsDeviceTraceEnabled() const { return device_enabled_
; }
182 friend class base::RefCounted
<GPUTrace
>;
184 const GpuTracerSource source_
= kTraceGroupInvalid
;
185 const std::string category_
;
186 const std::string name_
;
187 scoped_refptr
<Outputter
> outputter_
;
188 scoped_ptr
<gfx::GPUTimer
> gpu_timer_
;
189 const bool service_enabled_
= false;
190 const bool device_enabled_
= false;
192 DISALLOW_COPY_AND_ASSIGN(GPUTrace
);
195 class ScopedGPUTrace
{
197 ScopedGPUTrace(GPUTracer
* gpu_tracer
,
198 GpuTracerSource source
,
199 const std::string
& category
,
200 const std::string
& name
)
201 : gpu_tracer_(gpu_tracer
), source_(source
) {
202 gpu_tracer_
->Begin(category
, name
, source_
);
205 ~ScopedGPUTrace() { gpu_tracer_
->End(source_
); }
208 GPUTracer
* gpu_tracer_
;
209 GpuTracerSource source_
;
215 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_