blob: 349fc1529de6ac32754950f20c1ab5bd6037ecb2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
bungeman@google.combe9ad4e2011-06-07 19:16:02 +00008#include "BenchGpuTimer_gl.h"
robertphillips@google.com6177e692013-02-28 20:16:25 +00009#include "gl/SkGLContextHelper.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000010#include "gl/GrGLUtil.h"
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000011
robertphillips@google.com6177e692013-02-28 20:16:25 +000012BenchGpuTimer::BenchGpuTimer(const SkGLContextHelper* glctx) {
bsalomon@google.com373a6632011-10-19 20:43:20 +000013 fContext = glctx;
14 glctx->ref();
15 glctx->makeCurrent();
16 fStarted = false;
17 fSupported = GrGLGetVersion(glctx->gl()) > GR_GL_VER(3,3) ||
bsalomon@google.com1744f972013-02-26 21:46:32 +000018 glctx->hasExtension("GL_ARB_timer_query") ||
19 glctx->hasExtension("GL_EXT_timer_query");
rmistry@google.comfbfcd562012-08-23 18:09:54 +000020
bsalomon@google.com373a6632011-10-19 20:43:20 +000021 if (fSupported) {
22 SK_GL(*glctx, GenQueries(1, &fQuery));
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000023 }
24}
25
26BenchGpuTimer::~BenchGpuTimer() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000027 if (fSupported) {
28 fContext->makeCurrent();
29 SK_GL(*fContext, DeleteQueries(1, &fQuery));
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000030 }
bsalomon@google.com373a6632011-10-19 20:43:20 +000031 fContext->unref();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000032}
33
34void BenchGpuTimer::startGpu() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000035 if (fSupported) {
36 fContext->makeCurrent();
37 fStarted = true;
38 SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery));
39 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000040}
41
42/**
43 * It is important to stop the cpu clocks first,
44 * as this will cpu wait for the gpu to finish.
45 */
46double BenchGpuTimer::endGpu() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000047 if (fSupported) {
48 fStarted = false;
49 fContext->makeCurrent();
50 SK_GL(*fContext, EndQuery(GR_GL_TIME_ELAPSED));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
bsalomon@google.com373a6632011-10-19 20:43:20 +000052 GrGLint available = 0;
53 while (!available) {
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000054 SK_GL_NOERRCHECK(*fContext, GetQueryObjectiv(fQuery,
bsalomon@google.com373a6632011-10-19 20:43:20 +000055 GR_GL_QUERY_RESULT_AVAILABLE,
56 &available));
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000057 // If GetQueryObjectiv is erroring out we need some alternative
58 // means of breaking out of this loop
59 GrGLenum error;
60 SK_GL_RET_NOERRCHECK(*fContext, error, GetError());
61 if (GR_GL_NO_ERROR != error) {
62 break;
63 }
bsalomon@google.com373a6632011-10-19 20:43:20 +000064 }
65 GrGLuint64 totalGPUTimeElapsed = 0;
66 SK_GL(*fContext, GetQueryObjectui64v(fQuery,
67 GR_GL_QUERY_RESULT,
68 &totalGPUTimeElapsed));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069
bsalomon@google.com373a6632011-10-19 20:43:20 +000070 return totalGPUTimeElapsed / 1000000.0;
71 } else {
72 return 0;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000073 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000074}