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" |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame^] | 9 | #include "gl/SkGLContextHelper.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 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame^] | 12 | BenchGpuTimer::BenchGpuTimer(const SkGLContextHelper* glctx) { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 13 | fContext = glctx; |
| 14 | glctx->ref(); |
| 15 | glctx->makeCurrent(); |
| 16 | fStarted = false; |
| 17 | fSupported = GrGLGetVersion(glctx->gl()) > GR_GL_VER(3,3) || |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 18 | glctx->hasExtension("GL_ARB_timer_query") || |
| 19 | glctx->hasExtension("GL_EXT_timer_query"); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +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)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 51 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 52 | GrGLint available = 0; |
| 53 | while (!available) { |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 54 | SK_GL_NOERRCHECK(*fContext, GetQueryObjectiv(fQuery, |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 55 | GR_GL_QUERY_RESULT_AVAILABLE, |
| 56 | &available)); |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 57 | // 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.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 64 | } |
| 65 | GrGLuint64 totalGPUTimeElapsed = 0; |
| 66 | SK_GL(*fContext, GetQueryObjectui64v(fQuery, |
| 67 | GR_GL_QUERY_RESULT, |
| 68 | &totalGPUTimeElapsed)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 70 | return totalGPUTimeElapsed / 1000000.0; |
| 71 | } else { |
| 72 | return 0; |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 73 | } |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 74 | } |