blob: 32909832f38d4806d443cac5b5a14d352234db9c [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"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +00009#include "gl/SkGLContext.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000010#include "gl/GrGLUtil.h"
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000011
bsalomon@google.com373a6632011-10-19 20:43:20 +000012BenchGpuTimer::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");
bungeman@google.combe9ad4e2011-06-07 19:16:02 +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));
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;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000066 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000067}