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.
4
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_
8
9#include <string>
10
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"
18
19namespace gpu {
20namespace gles2 {
21
22class Outputter;
23class GPUTrace;
24
25// Id used to keep trace namespaces separate
26enum GpuTracerSource {
27  kTraceGroupInvalid = -1,
28
29  kTraceGroupMarker = 0,
30  kTraceCHROMIUM = 1,
31  kTraceDecoder = 2,
32
33  NUM_TRACER_SOURCES
34};
35
36enum GpuTracerType {
37  kTracerTypeInvalid = -1,
38
39  kTracerTypeARBTimer,
40  kTracerTypeDisjointTimer
41};
42
43// Marker structure for a Trace.
44struct TraceMarker {
45  TraceMarker(const std::string& name);
46  ~TraceMarker();
47
48  std::string name_;
49  scoped_refptr<GPUTrace> trace_;
50};
51
52// Traces GPU Commands.
53class GPUTracer : public base::SupportsWeakPtr<GPUTracer> {
54 public:
55  explicit GPUTracer(gles2::GLES2Decoder* decoder);
56  ~GPUTracer();
57
58  // Scheduled processing in decoder begins.
59  bool BeginDecoding();
60
61  // Scheduled processing in decoder ends.
62  bool EndDecoding();
63
64  // Begin a trace marker.
65  bool Begin(const std::string& name, GpuTracerSource source);
66
67  // End the last started trace marker.
68  bool End(GpuTracerSource source);
69
70  bool IsTracing();
71
72  // Retrieve the name of the current open trace.
73  // Returns empty string if no current open trace.
74  const std::string& CurrentName() const;
75
76 private:
77  // Trace Processing.
78  scoped_refptr<GPUTrace> CreateTrace(const std::string& name);
79  void Process();
80  void ProcessTraces();
81
82  void CalculateTimerOffset();
83  void IssueProcessTask();
84
85  scoped_refptr<Outputter> outputter_;
86  std::vector<TraceMarker> markers_[NUM_TRACER_SOURCES];
87  std::deque<scoped_refptr<GPUTrace> > traces_;
88
89  const unsigned char* gpu_trace_srv_category;
90  const unsigned char* gpu_trace_dev_category;
91  gles2::GLES2Decoder* decoder_;
92
93  int64 timer_offset_;
94  GpuTracerSource last_tracer_source_;
95
96  GpuTracerType tracer_type_;
97  bool gpu_timing_synced_;
98  bool gpu_executing_;
99  bool process_posted_;
100
101  DISALLOW_COPY_AND_ASSIGN(GPUTracer);
102};
103
104class Outputter : public base::RefCounted<Outputter> {
105 public:
106  virtual void Trace(const std::string& name,
107                     int64 start_time,
108                     int64 end_time) = 0;
109
110 protected:
111  virtual ~Outputter() {}
112  friend class base::RefCounted<Outputter>;
113};
114
115class TraceOutputter : public Outputter {
116 public:
117  static scoped_refptr<TraceOutputter> Create(const std::string& name);
118  virtual void Trace(const std::string& name,
119                     int64 start_time,
120                     int64 end_time) OVERRIDE;
121
122 protected:
123  friend class base::RefCounted<Outputter>;
124  explicit TraceOutputter(const std::string& name);
125  virtual ~TraceOutputter();
126
127  base::Thread named_thread_;
128  uint64 local_trace_id_;
129
130  DISALLOW_COPY_AND_ASSIGN(TraceOutputter);
131};
132
133class GPU_EXPORT GPUTrace
134    : public base::RefCounted<GPUTrace> {
135 public:
136  GPUTrace(scoped_refptr<Outputter> outputter,
137           const std::string& name,
138           int64 offset,
139           GpuTracerType tracer_type);
140
141  bool IsEnabled() { return tracer_type_ != kTracerTypeInvalid; }
142  const std::string& name() { return name_; }
143
144  void Start();
145  void End();
146  bool IsAvailable();
147  void Process();
148
149 private:
150  ~GPUTrace();
151
152  void Output();
153
154  friend class base::RefCounted<GPUTrace>;
155
156  std::string name_;
157  scoped_refptr<Outputter> outputter_;
158
159  int64 offset_;
160  int64 start_time_;
161  int64 end_time_;
162  GpuTracerType tracer_type_;
163  bool end_requested_;
164
165  GLuint queries_[2];
166
167  DISALLOW_COPY_AND_ASSIGN(GPUTrace);
168};
169
170}  // namespace gles2
171}  // namespace gpu
172
173#endif  // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_
174