2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-06-20 18:29:20 +00:00
|
|
|
#include "GpuTimer.h"
|
2013-02-28 20:16:25 +00:00
|
|
|
#include "gl/SkGLContextHelper.h"
|
2012-05-07 17:09:37 +00:00
|
|
|
#include "gl/GrGLUtil.h"
|
2011-10-19 20:43:20 +00:00
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
GpuTimer::GpuTimer(const SkGLContextHelper* glctx) : fContext(glctx) {
|
|
|
|
if (fContext) {
|
|
|
|
fContext->ref();
|
|
|
|
fContext->makeCurrent();
|
|
|
|
fStarted = false;
|
|
|
|
fSupported = GrGLGetVersion(fContext->gl()) > GR_GL_VER(3,3) ||
|
|
|
|
fContext->hasExtension("GL_ARB_timer_query") ||
|
|
|
|
fContext->hasExtension("GL_EXT_timer_query");
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
if (fSupported) {
|
|
|
|
SK_GL(*fContext, GenQueries(1, &fQuery));
|
|
|
|
}
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
GpuTimer::~GpuTimer() {
|
|
|
|
if (fContext) {
|
|
|
|
if (fSupported) {
|
|
|
|
fContext->makeCurrent();
|
|
|
|
SK_GL(*fContext, DeleteQueries(1, &fQuery));
|
|
|
|
}
|
|
|
|
fContext->unref();
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
void GpuTimer::start() {
|
|
|
|
if (fContext && fSupported) {
|
2011-10-19 20:43:20 +00:00
|
|
|
fContext->makeCurrent();
|
|
|
|
fStarted = true;
|
|
|
|
SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery));
|
|
|
|
}
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It is important to stop the cpu clocks first,
|
|
|
|
* as this will cpu wait for the gpu to finish.
|
|
|
|
*/
|
2014-06-20 18:29:20 +00:00
|
|
|
double GpuTimer::end() {
|
|
|
|
if (fContext && fSupported) {
|
2011-10-19 20:43:20 +00:00
|
|
|
fStarted = false;
|
|
|
|
fContext->makeCurrent();
|
|
|
|
SK_GL(*fContext, EndQuery(GR_GL_TIME_ELAPSED));
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-19 20:43:20 +00:00
|
|
|
GrGLint available = 0;
|
|
|
|
while (!available) {
|
2013-02-07 19:45:46 +00:00
|
|
|
SK_GL_NOERRCHECK(*fContext, GetQueryObjectiv(fQuery,
|
2014-06-20 18:29:20 +00:00
|
|
|
GR_GL_QUERY_RESULT_AVAILABLE,
|
|
|
|
&available));
|
2013-02-07 19:45:46 +00:00
|
|
|
// If GetQueryObjectiv is erroring out we need some alternative
|
|
|
|
// means of breaking out of this loop
|
|
|
|
GrGLenum error;
|
|
|
|
SK_GL_RET_NOERRCHECK(*fContext, error, GetError());
|
|
|
|
if (GR_GL_NO_ERROR != error) {
|
|
|
|
break;
|
|
|
|
}
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
|
|
|
GrGLuint64 totalGPUTimeElapsed = 0;
|
|
|
|
SK_GL(*fContext, GetQueryObjectui64v(fQuery,
|
|
|
|
GR_GL_QUERY_RESULT,
|
|
|
|
&totalGPUTimeElapsed));
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-19 20:43:20 +00:00
|
|
|
return totalGPUTimeElapsed / 1000000.0;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
}
|