blob: b7bd88b63980be733bb891cbc87b76d63a92a913 [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"
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000010
bsalomon@google.com373a6632011-10-19 20:43:20 +000011BenchGpuTimer::BenchGpuTimer(const SkGLContext* glctx) {
12 fContext = glctx;
13 glctx->ref();
14 glctx->makeCurrent();
15 fStarted = false;
16 fSupported = GrGLGetVersion(glctx->gl()) > GR_GL_VER(3,3) ||
17 GrGLHasExtension(glctx->gl(), "GL_ARB_timer_query") ||
18 GrGLHasExtension(glctx->gl(), "GL_EXT_timer_query");
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000019
bsalomon@google.com373a6632011-10-19 20:43:20 +000020 if (fSupported) {
21 SK_GL(*glctx, GenQueries(1, &fQuery));
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000022 }
23}
24
25BenchGpuTimer::~BenchGpuTimer() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000026 if (fSupported) {
27 fContext->makeCurrent();
28 SK_GL(*fContext, DeleteQueries(1, &fQuery));
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000029 }
bsalomon@google.com373a6632011-10-19 20:43:20 +000030 fContext->unref();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000031}
32
33void BenchGpuTimer::startGpu() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000034 if (fSupported) {
35 fContext->makeCurrent();
36 fStarted = true;
37 SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery));
38 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000039}
40
41/**
42 * It is important to stop the cpu clocks first,
43 * as this will cpu wait for the gpu to finish.
44 */
45double BenchGpuTimer::endGpu() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000046 if (fSupported) {
47 fStarted = false;
48 fContext->makeCurrent();
49 SK_GL(*fContext, EndQuery(GR_GL_TIME_ELAPSED));
50
51 GrGLint available = 0;
52 while (!available) {
53 SK_GL(*fContext, GetQueryObjectiv(fQuery,
54 GR_GL_QUERY_RESULT_AVAILABLE,
55 &available));
56 }
57 GrGLuint64 totalGPUTimeElapsed = 0;
58 SK_GL(*fContext, GetQueryObjectui64v(fQuery,
59 GR_GL_QUERY_RESULT,
60 &totalGPUTimeElapsed));
61
62 return totalGPUTimeElapsed / 1000000.0;
63 } else {
64 return 0;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000065 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000066}