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.
|
|
|
|
*/
|
2011-06-07 19:16:02 +00:00
|
|
|
#include "BenchGpuTimer_gl.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
|
|
|
|
2013-02-28 20:16:25 +00:00
|
|
|
BenchGpuTimer::BenchGpuTimer(const SkGLContextHelper* glctx) {
|
2011-10-19 20:43:20 +00:00
|
|
|
fContext = glctx;
|
|
|
|
glctx->ref();
|
|
|
|
glctx->makeCurrent();
|
|
|
|
fStarted = false;
|
|
|
|
fSupported = GrGLGetVersion(glctx->gl()) > GR_GL_VER(3,3) ||
|
2013-02-26 21:46:32 +00:00
|
|
|
glctx->hasExtension("GL_ARB_timer_query") ||
|
|
|
|
glctx->hasExtension("GL_EXT_timer_query");
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-19 20:43:20 +00:00
|
|
|
if (fSupported) {
|
|
|
|
SK_GL(*glctx, GenQueries(1, &fQuery));
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BenchGpuTimer::~BenchGpuTimer() {
|
2011-10-19 20:43:20 +00:00
|
|
|
if (fSupported) {
|
|
|
|
fContext->makeCurrent();
|
|
|
|
SK_GL(*fContext, DeleteQueries(1, &fQuery));
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
2011-10-19 20:43:20 +00:00
|
|
|
fContext->unref();
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BenchGpuTimer::startGpu() {
|
2011-10-19 20:43:20 +00:00
|
|
|
if (fSupported) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
double BenchGpuTimer::endGpu() {
|
2011-10-19 20:43:20 +00:00
|
|
|
if (fSupported) {
|
|
|
|
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,
|
2011-10-19 20:43: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
|
|
|
}
|
|
|
|
}
|