blob: b59988ca2642f5f3330d05ba849cdde8c4c49e0d [file] [log] [blame]
mtklein9ac68ee2014-06-20 11:29:20 -07001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "Timer.h"
8
kkinnunen9e61bb72014-10-09 05:24:15 -07009Timer::Timer(SkGLContext* gl)
mtklein9ac68ee2014-06-20 11:29:20 -070010 : fCpu(-1.0)
11 , fWall(-1.0)
12 , fTruncatedCpu(-1.0)
13 , fTruncatedWall(-1.0)
14 , fGpu(-1.0)
15#if SK_SUPPORT_GPU
16 , fGpuTimer(gl)
17#endif
18 {}
19
20void Timer::start() {
21 fSysTimer.startWall();
22 fTruncatedSysTimer.startWall();
23#if SK_SUPPORT_GPU
24 fGpuTimer.start();
25#endif
26 fSysTimer.startCpu();
27 fTruncatedSysTimer.startCpu();
28}
29
30void Timer::end() {
31 fCpu = fSysTimer.endCpu();
32#if SK_SUPPORT_GPU
33 //It is important to stop the cpu clocks first,
34 //as the following will cpu wait for the gpu to finish.
35 fGpu = fGpuTimer.end();
36#endif
37 fWall = fSysTimer.endWall();
38}
39
40void Timer::truncatedEnd() {
41 fTruncatedCpu = fTruncatedSysTimer.endCpu();
42 fTruncatedWall = fTruncatedSysTimer.endWall();
43}
44
45WallTimer::WallTimer() : fWall(-1.0) {}
46
47void WallTimer::start() {
48 fSysTimer.startWall();
49}
50
51void WallTimer::end() {
52 fWall = fSysTimer.endWall();
53}
mtklein748ca3b2015-01-15 10:56:12 -080054
55SkString HumanizeMs(double ms) {
mtkleind1f7f992015-03-12 10:29:32 -070056 if (ms > 60e+3) return SkStringPrintf("%.3gm", ms/60e+3);
57 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e+3);
58 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e+6);
mtklein748ca3b2015-01-15 10:56:12 -080059#ifdef SK_BUILD_FOR_WIN
mtkleind1f7f992015-03-12 10:29:32 -070060 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e+3);
mtklein748ca3b2015-01-15 10:56:12 -080061#else
mtkleind1f7f992015-03-12 10:29:32 -070062 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e+3);
mtklein748ca3b2015-01-15 10:56:12 -080063#endif
64 return SkStringPrintf("%.3gms", ms);
65}