blob: 3617f9de58b243253499f65df6bf340153a6d8da [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
robertphillips@google.com6177e692013-02-28 20:16:25 +000023BenchTimer::BenchTimer(SkGLContextHelper* gl)
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000024 : fCpu(-1.0)
25 , fWall(-1.0)
robertphillips@google.com91ee3a12012-08-28 12:18:40 +000026 , fTruncatedCpu(-1.0)
27 , fTruncatedWall(-1.0)
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000028 , fGpu(-1.0)
29{
bsalomon@google.com373a6632011-10-19 20:43:20 +000030 fSysTimer = new BenchSysTimer();
robertphillips@google.com91ee3a12012-08-28 12:18:40 +000031 fTruncatedSysTimer = new BenchSysTimer();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000032#if SK_SUPPORT_GPU
bsalomon@google.com373a6632011-10-19 20:43:20 +000033 if (gl) {
34 fGpuTimer = new BenchGpuTimer(gl);
35 } else {
36 fGpuTimer = NULL;
37 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000038#endif
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000039}
40
41BenchTimer::~BenchTimer() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000042 delete fSysTimer;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +000043 delete fTruncatedSysTimer;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000044#if SK_SUPPORT_GPU
bsalomon@google.com373a6632011-10-19 20:43:20 +000045 delete fGpuTimer;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000046#endif
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000047}
48
reed@google.comef77ec22013-05-29 15:39:54 +000049void BenchTimer::start(double durationScale) {
50 fDurationScale = durationScale;
51
bsalomon@google.com373a6632011-10-19 20:43:20 +000052 fSysTimer->startWall();
robertphillips@google.com91ee3a12012-08-28 12:18:40 +000053 fTruncatedSysTimer->startWall();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000054#if SK_SUPPORT_GPU
bsalomon@google.com373a6632011-10-19 20:43:20 +000055 if (fGpuTimer) {
56 fGpuTimer->startGpu();
57 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000058#endif
bsalomon@google.com373a6632011-10-19 20:43:20 +000059 fSysTimer->startCpu();
robertphillips@google.com91ee3a12012-08-28 12:18:40 +000060 fTruncatedSysTimer->startCpu();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000061}
62
63void BenchTimer::end() {
reed@google.comef77ec22013-05-29 15:39:54 +000064 fCpu = fSysTimer->endCpu() * fDurationScale;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000065#if SK_SUPPORT_GPU
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000066 //It is important to stop the cpu clocks first,
67 //as the following will cpu wait for the gpu to finish.
bsalomon@google.com373a6632011-10-19 20:43:20 +000068 if (fGpuTimer) {
reed@google.comef77ec22013-05-29 15:39:54 +000069 fGpu = fGpuTimer->endGpu() * fDurationScale;
bsalomon@google.com373a6632011-10-19 20:43:20 +000070 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000071#endif
reed@google.comef77ec22013-05-29 15:39:54 +000072 fWall = fSysTimer->endWall() * fDurationScale;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000073}
robertphillips@google.com91ee3a12012-08-28 12:18:40 +000074
75void BenchTimer::truncatedEnd() {
reed@google.comef77ec22013-05-29 15:39:54 +000076 fTruncatedCpu = fTruncatedSysTimer->endCpu() * fDurationScale;
77 fTruncatedWall = fTruncatedSysTimer->endWall() * fDurationScale;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +000078}