3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "gl/SkGLContextHelper.h"
10 #include "gl/GrGLUtil.h"
12 GpuTimer::GpuTimer(const SkGLContextHelper* glctx) : fContext(glctx) {
15 fContext->makeCurrent();
17 fSupported = GrGLGetVersion(fContext->gl()) > GR_GL_VER(3,3) ||
18 fContext->hasExtension("GL_ARB_timer_query") ||
19 fContext->hasExtension("GL_EXT_timer_query");
22 SK_GL(*fContext, GenQueries(1, &fQuery));
27 GpuTimer::~GpuTimer() {
30 fContext->makeCurrent();
31 SK_GL(*fContext, DeleteQueries(1, &fQuery));
37 void GpuTimer::start() {
38 if (fContext && fSupported) {
39 fContext->makeCurrent();
41 SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery));
46 * It is important to stop the cpu clocks first,
47 * as this will cpu wait for the gpu to finish.
49 double GpuTimer::end() {
50 if (fContext && fSupported) {
52 fContext->makeCurrent();
53 SK_GL(*fContext, EndQuery(GR_GL_TIME_ELAPSED));
55 GrGLint available = 0;
57 SK_GL_NOERRCHECK(*fContext, GetQueryObjectiv(fQuery,
58 GR_GL_QUERY_RESULT_AVAILABLE,
60 // If GetQueryObjectiv is erroring out we need some alternative
61 // means of breaking out of this loop
63 SK_GL_RET_NOERRCHECK(*fContext, error, GetError());
64 if (GR_GL_NO_ERROR != error) {
68 GrGLuint64 totalGPUTimeElapsed = 0;
69 SK_GL(*fContext, GetQueryObjectui64v(fQuery,
71 &totalGPUTimeElapsed));
73 return totalGPUTimeElapsed / 1000000.0;