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" |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 10 | #include "gl/GrGLUtil.h" |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 11 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 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"); |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 20 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 21 | if (fSupported) { |
| 22 | SK_GL(*glctx, GenQueries(1, &fQuery)); |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | |
| 26 | BenchGpuTimer::~BenchGpuTimer() { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 27 | if (fSupported) { |
| 28 | fContext->makeCurrent(); |
| 29 | SK_GL(*fContext, DeleteQueries(1, &fQuery)); |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 30 | } |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 31 | fContext->unref(); |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void BenchGpuTimer::startGpu() { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 35 | if (fSupported) { |
| 36 | fContext->makeCurrent(); |
| 37 | fStarted = true; |
| 38 | SK_GL(*fContext, BeginQuery(GR_GL_TIME_ELAPSED, fQuery)); |
| 39 | } |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 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() { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 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; |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 66 | } |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 67 | } |