blob: 8ac08a70bc1a61b8f1dcffbc9b0c5609c4977d2c [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 "BenchTimer.h"
9#if defined(SK_BUILD_FOR_WIN32)
10 #include "BenchSysTimer_windows.h"
11#elif defined(SK_BUILD_FOR_MAC)
12 #include "BenchSysTimer_mach.h"
djsollen@google.com58629292011-11-03 13:08:29 +000013#elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID)
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000014 #include "BenchSysTimer_posix.h"
15#else
16 #include "BenchSysTimer_c.h"
17#endif
18
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000019#if SK_SUPPORT_GPU
bsalomon@google.com373a6632011-10-19 20:43:20 +000020#include "BenchGpuTimer_gl.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000021#endif
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000022
bsalomon@google.com373a6632011-10-19 20:43:20 +000023BenchTimer::BenchTimer(SkGLContext* gl)
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000024 : fCpu(-1.0)
25 , fWall(-1.0)
26 , fGpu(-1.0)
27{
bsalomon@google.com373a6632011-10-19 20:43:20 +000028 fSysTimer = new BenchSysTimer();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000029#if SK_SUPPORT_GPU
bsalomon@google.com373a6632011-10-19 20:43:20 +000030 if (gl) {
31 fGpuTimer = new BenchGpuTimer(gl);
32 } else {
33 fGpuTimer = NULL;
34 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000035#endif
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000036}
37
38BenchTimer::~BenchTimer() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000039 delete fSysTimer;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000040#if SK_SUPPORT_GPU
bsalomon@google.com373a6632011-10-19 20:43:20 +000041 delete fGpuTimer;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000042#endif
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000043}
44
45void BenchTimer::start() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000046 fSysTimer->startWall();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000047#if SK_SUPPORT_GPU
bsalomon@google.com373a6632011-10-19 20:43:20 +000048 if (fGpuTimer) {
49 fGpuTimer->startGpu();
50 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000051#endif
bsalomon@google.com373a6632011-10-19 20:43:20 +000052 fSysTimer->startCpu();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000053}
54
55void BenchTimer::end() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000056 fCpu = fSysTimer->endCpu();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000057#if SK_SUPPORT_GPU
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000058 //It is important to stop the cpu clocks first,
59 //as the following will cpu wait for the gpu to finish.
bsalomon@google.com373a6632011-10-19 20:43:20 +000060 if (fGpuTimer) {
61 fGpu = fGpuTimer->endGpu();
62 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000063#endif
bsalomon@google.com373a6632011-10-19 20:43:20 +000064 fWall = fSysTimer->endWall();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000065}