Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gpuprofiler.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFXPROFILE_GPUPROFILER_H_
19 #define ION_GFXPROFILE_GPUPROFILER_H_
20 
23 
24 #include <deque>
25 #include <stack>
26 #include <vector>
27 
28 #include "base/integral_types.h"
32 #include "ion/profile/profiling.h"
34 
35 namespace ion {
36 namespace profile {
37 class CallTraceTest;
38 } // namespace profile
39 namespace gfxprofile {
40 
55 class GpuProfiler {
56  public:
58  static GpuProfiler* Get();
59 
60  GpuProfiler();
61  ~GpuProfiler();
62 
64  const;
65 
68  void SetGraphicsManager(const gfx::GraphicsManagerPtr& gfx_mgr);
69 
74  void SetEnableGpuTracing(bool enabled) {
75  enable_gpu_tracing_ = enabled;
76  }
77 
81  if (enable_gpu_tracing_) {
82  return graphics_manager_.Get();
83  } else {
84  return NULL;
85  }
86  }
87 
90  void PollGlTimerQueries();
91 
93  void EnterGlScope(uint32 id);
94 
96  void LeaveGlScope();
97 
98  private:
101  struct GpuTimerQuery {
102  enum QueryType {
103  kQueryBeginFrame,
104  kQueryBeginScope,
105  kQueryEndScope,
106  };
107 
109  GpuTimerQuery(uint64 timestamp_ns,
110  int scope_id,
111  GLuint query_id,
112  QueryType type)
113  : cpu_timestamp_ns(timestamp_ns),
114  scope_event_id(scope_id),
115  gl_query_id(query_id),
116  query_type(type) {}
117 
118  uint64 cpu_timestamp_ns;
119  int scope_event_id;
120  GLuint gl_query_id;
121  QueryType query_type;
122  };
123 
126  explicit GpuProfiler(ion::profile::CallTraceManager* manager);
127 
129  void SyncGlTimebase();
130 
132  GLuint TryAllocateGlQueryId();
133 
136 
138  base::Setting<bool> enable_gpu_tracing_;
139 
141  int64 gl_timer_offset_ns_;
142 
144  gfx::GraphicsManagerPtr graphics_manager_;
145 
149  std::deque<GpuTimerQuery> pending_gpu_queries_;
150 
154  std::stack<GLuint, std::vector<GLuint> > gl_timer_query_id_pool_;
155 
157 };
158 
162  public:
163  ScopedGlTracer(GpuProfiler* profiler, int id) : profiler_(profiler) {
164  profiler_->EnterGlScope(id);
165  }
166 
168  profiler_->LeaveGlScope();
169  }
170 
171  private:
172  GpuProfiler* profiler_;
173 };
174 
175 } // namespace gfxprofile
176 } // namespace ion
177 
178 #if ION_PRODUCTION
179 #define ION_PROFILE_GPU(group_name)
180 #else
181 #define ION_PROFILE_GPU(group_name) \
184  /* Force compilation to fail if group_name is not a literal string */ \
185  (void) group_name " must be a literal string."; \
186  ION_PROFILE_FUNCTION(group_name); \
187  ION_DECLARE_SAFE_STATIC_POINTER_WITH_CONSTRUCTOR( \
188  int, ION_PROFILING_PASTE3(gpu_scope_event_id_), \
189  new int(::ion::profile::GetCallTraceManager()-> \
190  GetScopeEnterEvent("GPU_" group_name))); \
191  ::ion::gfxprofile::ScopedGlTracer ION_PROFILING_PASTE3(gpu_scope_tracer_)( \
192  ::ion::gfxprofile::GpuProfiler::Get(), \
193  *ION_PROFILING_PASTE3(gpu_scope_event_id_))
194 #endif // ION_PRODUCTION
195 
196 #endif // ION_GFXPROFILE_GPUPROFILER_H_
GraphicsManager manages the graphics library for an application.
Traces the GPU start and end times of the GL commands submitted in the same scope.
Definition: gpuprofiler.h:161
bool IsGpuProfilingSupported(const ion::gfx::GraphicsManagerPtr &gfx_mgr) const
Definition: gpuprofiler.cc:45
friend class ion::profile::CallTraceTest
Definition: gpuprofiler.h:156
std::string type
Definition: printer.cc:353
void EnterGlScope(uint32 id)
Records the beginning of a scoped GL trace event.
Definition: gpuprofiler.cc:88
static GpuProfiler * Get()
Gets the GpuProfiler singleton instance.
Definition: gpuprofiler.cc:26
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
void SetGraphicsManager(const gfx::GraphicsManagerPtr &gfx_mgr)
Sets the GraphicsManager that is required for performing GPU tracing via OpenGL.
Definition: gpuprofiler.cc:60
ScopedGlTracer(GpuProfiler *profiler, int id)
Definition: gpuprofiler.h:163
Singleton class that augments CallTraceManager with GPU tracing support.
Definition: gpuprofiler.h:55
base::ReferentPtr< GraphicsManager >::Type GraphicsManagerPtr
Convenience typedef for shared pointer to a GraphicsManager.
Manages call trace recording for visualization in Web Tracing Framework (WTF) format.
void SetEnableGpuTracing(bool enabled)
Enables runtime GPU tracing.
Definition: gpuprofiler.h:74
gfx::GraphicsManager * GetGraphicsManagerOrNull() const
Gets the GraphicsManager if GPU tracing is enabled.
Definition: gpuprofiler.h:80
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
void LeaveGlScope()
Records the end of a scoped GL trace event.
Definition: gpuprofiler.cc:103
void PollGlTimerQueries()
Polls (non-blocking) for completed GL timer query data and adds events into the trace buffer...
Definition: gpuprofiler.cc:156