#include "BenchGpuTimer_gl.h"
#include "gl/SkGLContextHelper.h"
#include "gl/GrGLUtil.h"
BenchGpuTimer::BenchGpuTimer(const SkGLContextHelper* glctx) {
    fContext = glctx;
    glctx->ref();
    glctx->makeCurrent();
    fStarted = false;
    fSupported = GrGLGetVersion(glctx->gl()) > GR_GL_VER(3,3) ||
                 glctx->hasExtension("GL_ARB_timer_query") ||
                 glctx->hasExtension("GL_EXT_timer_query");
    if (fSupported) {
        SK_GL(*glctx, GenQueries(1, &fQuery));
    }
}
BenchGpuTimer::~BenchGpuTimer() {
    if (fSupported) {
        fContext->makeCurrent();
        SK_GL(*fContext, DeleteQueries(1, &fQuery));
    }
    fContext->unref();
}
void BenchGpuTimer::startGpu() {
    if (fSupported) {
        fContext->makeCurrent();
        fStarted = true;
        SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery));
    }
}
double BenchGpuTimer::endGpu() {
    if (fSupported) {
        fStarted = false;
        fContext->makeCurrent();
        SK_GL(*fContext, EndQuery(GR_GL_TIME_ELAPSED));
        GrGLint available = 0;
        while (!available) {
            SK_GL_NOERRCHECK(*fContext, GetQueryObjectiv(fQuery,
                                             GR_GL_QUERY_RESULT_AVAILABLE,
                                             &available));
                                    GrGLenum error;
            SK_GL_RET_NOERRCHECK(*fContext, error, GetError());
            if (GR_GL_NO_ERROR != error) {
                break;
            }
        }
        GrGLuint64 totalGPUTimeElapsed = 0;
        SK_GL(*fContext, GetQueryObjectui64v(fQuery,
                                             GR_GL_QUERY_RESULT,
                                             &totalGPUTimeElapsed));
        return totalGPUTimeElapsed / 1000000.0;
    } else {
        return 0;
    }
}