epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 8 | #include "BenchGpuTimer_gl.h" |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 9 | #include "gl/SkGLContext.h" |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 11 | BenchGpuTimer::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.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 20 | if (fSupported) { |
| 21 | SK_GL(*glctx, GenQueries(1, &fQuery)); |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
| 25 | BenchGpuTimer::~BenchGpuTimer() { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 26 | if (fSupported) { |
| 27 | fContext->makeCurrent(); |
| 28 | SK_GL(*fContext, DeleteQueries(1, &fQuery)); |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 29 | } |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 30 | fContext->unref(); |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void BenchGpuTimer::startGpu() { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 34 | if (fSupported) { |
| 35 | fContext->makeCurrent(); |
| 36 | fStarted = true; |
| 37 | SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery)); |
| 38 | } |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * It is important to stop the cpu clocks first, |
| 43 | * as this will cpu wait for the gpu to finish. |
| 44 | */ |
| 45 | double BenchGpuTimer::endGpu() { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 46 | 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.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 65 | } |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 66 | } |