Home | History | Annotate | Download | only in bench
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "BenchGpuTimer_gl.h"
      9 #include "gl/SkGLContext.h"
     10 #include "gl/GrGLUtil.h"
     11 
     12 BenchGpuTimer::BenchGpuTimer(const SkGLContext* glctx) {
     13     fContext = glctx;
     14     glctx->ref();
     15     glctx->makeCurrent();
     16     fStarted = false;
     17     fSupported = GrGLGetVersion(glctx->gl()) > GR_GL_VER(3,3) ||
     18                  GrGLHasExtension(glctx->gl(), "GL_ARB_timer_query") ||
     19                  GrGLHasExtension(glctx->gl(), "GL_EXT_timer_query");
     20 
     21     if (fSupported) {
     22         SK_GL(*glctx, GenQueries(1, &fQuery));
     23     }
     24 }
     25 
     26 BenchGpuTimer::~BenchGpuTimer() {
     27     if (fSupported) {
     28         fContext->makeCurrent();
     29         SK_GL(*fContext, DeleteQueries(1, &fQuery));
     30     }
     31     fContext->unref();
     32 }
     33 
     34 void BenchGpuTimer::startGpu() {
     35     if (fSupported) {
     36         fContext->makeCurrent();
     37         fStarted = true;
     38         SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery));
     39     }
     40 }
     41 
     42 /**
     43  * It is important to stop the cpu clocks first,
     44  * as this will cpu wait for the gpu to finish.
     45  */
     46 double BenchGpuTimer::endGpu() {
     47     if (fSupported) {
     48         fStarted = false;
     49         fContext->makeCurrent();
     50         SK_GL(*fContext, EndQuery(GR_GL_TIME_ELAPSED));
     51 
     52         GrGLint available = 0;
     53         while (!available) {
     54             SK_GL(*fContext, GetQueryObjectiv(fQuery,
     55                                              GR_GL_QUERY_RESULT_AVAILABLE,
     56                                              &available));
     57         }
     58         GrGLuint64 totalGPUTimeElapsed = 0;
     59         SK_GL(*fContext, GetQueryObjectui64v(fQuery,
     60                                              GR_GL_QUERY_RESULT,
     61                                              &totalGPUTimeElapsed));
     62 
     63         return totalGPUTimeElapsed / 1000000.0;
     64     } else {
     65         return 0;
     66     }
     67 }
     68