24 namespace gfxprofile {
33 enable_gpu_tracing_(
"enable_gpu_tracing", false),
34 gl_timer_offset_ns_(0) {
39 enable_gpu_tracing_(
"enable_gpu_tracing", false),
40 gl_timer_offset_ns_(0) {
47 if (!gfx_mgr->IsExtensionSupported(
"GL_EXT_disjoint_timer_query")) {
66 graphics_manager_ = gfx_mgr;
71 GLuint GpuProfiler::TryAllocateGlQueryId() {
78 if (gl_timer_query_id_pool_.empty()) {
80 gfx_mgr->GenQueries(1, &query_id);
82 query_id = gl_timer_query_id_pool_.top();
83 gl_timer_query_id_pool_.pop();
94 GLuint query_id = TryAllocateGlQueryId();
97 pending_gpu_queries_.push_back(GpuTimerQuery(
99 GpuTimerQuery::kQueryBeginScope));
109 GLuint query_id = TryAllocateGlQueryId();
112 pending_gpu_queries_.push_back(
113 GpuTimerQuery(manager_->
GetTimeInNs(), 0, query_id,
114 GpuTimerQuery::kQueryEndScope));
118 void GpuProfiler::SyncGlTimebase() {
133 int attempts_remaining = 3;
139 int64 cpu_timebase_ns =
static_cast<int64
>(manager_->
GetTimeInNs());
144 gl_timer_offset_ns_ = cpu_timebase_ns - gl_timestamp;
147 LOG(
WARNING) <<
"Skipping disjoint GPU timestamp";
148 }
while (--attempts_remaining > 0);
150 if (attempts_remaining == 0) {
151 LOG(
ERROR) <<
"Failed to sync GL timebase due to disjoint results";
152 gl_timer_offset_ns_ = 0;
165 GLuint begin_frame_id = TryAllocateGlQueryId();
166 if (begin_frame_id != 0) {
168 pending_gpu_queries_.push_back(
169 GpuTimerQuery(manager_->
GetTimeInNs(), 0, begin_frame_id,
170 GpuTimerQuery::kQueryBeginFrame));
173 bool has_checked_disjoint =
false;
174 bool was_disjoint =
false;
176 if (pending_gpu_queries_.empty()) {
181 GpuTimerQuery query = pending_gpu_queries_.front();
192 pending_gpu_queries_.pop_front();
193 gl_timer_query_id_pool_.push(query.gl_query_id);
195 if (!has_checked_disjoint) {
199 has_checked_disjoint =
true;
200 GLint disjoint_occurred = 0;
202 was_disjoint = !!disjoint_occurred;
204 LOG(
WARNING) <<
"Skipping disjoint GPU events";
213 gfx_mgr->GetQueryObjecti64v(query.gl_query_id,
216 uint64 adjusted_timestamp_ns = timestamp_ns + gl_timer_offset_ns_;
218 if (query.query_type == GpuTimerQuery::kQueryBeginFrame ||
219 query.query_type == GpuTimerQuery::kQueryBeginScope) {
220 if (adjusted_timestamp_ns < query.cpu_timestamp_ns) {
222 gl_timer_offset_ns_ += query.cpu_timestamp_ns - adjusted_timestamp_ns;
223 adjusted_timestamp_ns = query.cpu_timestamp_ns;
227 uint32 adjusted_timestamp_us =
228 static_cast<uint32
>(adjusted_timestamp_ns / 1000ll);
229 switch (query.query_type) {
230 case GpuTimerQuery::kQueryBeginFrame:
232 case GpuTimerQuery::kQueryBeginScope:
235 case GpuTimerQuery::kQueryEndScope:
GraphicsManager manages the graphics library for an application.
bool IsGpuProfilingSupported(const ion::gfx::GraphicsManagerPtr &gfx_mgr) const
#define ION_DECLARE_SAFE_STATIC_POINTER(type, variable)
Declare a static non-array pointer and calls a default constructor.
Class for recording frame events.
CallTraceManager * GetCallTraceManager()
Get the global, static instance of CallTraceManager.
void EnterGlScope(uint32 id)
Records the beginning of a scoped GL trace event.
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
static GpuProfiler * Get()
Gets the GpuProfiler singleton instance.
void EnterScopeAtTime(uint32 timestamp, int event_id)
Same as EnterScope, but with specified timestamp.
framebuffer alpha dfactor const GLvoid usage alpha const GLvoid data border const GLuint buffers const GLuint renderbuffers count renderbuffer GLuint framebuffers GLsizei GLint GLenum GLchar name GLsizei GLuint shaders GLint data GLsizei GLchar info_log GLint params GLint GLint precision GLint params GLint params GLint params GLfloat params GLvoid pointer height height const GLchar const GLint length mask dppass param param const GLvoid data const GLint1 value const GLfloat2 value v2 v2 v3 v3 const GLmatrix2 value const GLmatrix4 value v1 v2 v3 const GLvoid pointer GLsizei GLchar label const GLchar marker const void userParam const GLchar buf void params height const GLuint1 value const GLuint2 value const GLuint3 value const GLuint4 value void image primCount divisor length size sampler GLuint samplers GLint params const GLfloat params const GLint params flags timeout const GLvoid data const GLvoid pixels fixed_sample_locations GLfloat val height fixed_sample_locations primitive_mode const GLuint ids GLsizei GLsizei GLenum GLchar name const GLchar buffer_mode GLuint arrays GLint params GetQueryObjectiv
void SetGraphicsManager(const gfx::GraphicsManagerPtr &gfx_mgr)
Sets the GraphicsManager that is required for performing GPU tracing via OpenGL.
Singleton class that augments CallTraceManager with GPU tracing support.
TraceRecorder * GetNamedTraceRecorder(NamedTraceRecorderType name)
Gets the TraceRecorder instance specific to the current thread of the given name. ...
Manages call trace recording for visualization in Web Tracing Framework (WTF) format.
gfx::GraphicsManager * GetGraphicsManagerOrNull() const
Gets the GraphicsManager if GPU tracing is enabled.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
void LeaveScopeAtTime(uint32 timestamp)
Same as LeaveScope, but with specified timestamp.
void LeaveGlScope()
Records the end of a scoped GL trace event.
void PollGlTimerQueries()
Polls (non-blocking) for completed GL timer query data and adds events into the trace buffer...
uint64 GetTimeInNs() const
Returns the time in nanoseconds, relative to the timebase.