blob: 9ada22391026ef77d5eb4082559aa2dcb89997fd [file] [log] [blame]
joshualitt98d2e2f2015-10-05 07:23:30 -07001/*
2 * Copyright 2015 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.
joshualitt98d2e2f2015-10-05 07:23:30 -07006 */
7
8#ifndef TimingStateMachine_DEFINED
9#define TimingStateMachine_DEFINED
10
11#include "Benchmark.h"
12#include "SkTArray.h"
13#include "Timer.h"
14
15class SkCanvas;
16
17/*
18 * Manages a timer via a state machine. Can be used by modules to time benchmarks
19 *
20 * Clients call nextFrame, and must handle any requests from the timing state machine, specifically
21 * to reset. When kTimingFinished_ParentEvents is returned, then lastMeasurement() will return the
22 * timing and loops() will return the number of loops used to time.
23 *
24 * A client may continue timing the same benchmark indefinitely. To advance to the next
25 * benchmark, the client should call nextBenchmark.
26 */
27class TimingStateMachine {
28public:
29 TimingStateMachine();
30
31 enum ParentEvents {
32 kReset_ParentEvents,
33 kTiming_ParentEvents,
34 kTimingFinished_ParentEvents,// This implies parent can read lastMeasurement() and must
35 // reset
36 };
37
joshualittcb54e8e2015-10-05 13:58:26 -070038 ParentEvents nextFrame(bool preWarmBetweenSamples);
joshualittdc5db592015-10-05 13:24:55 -070039
40 /*
joshualittc603c142015-10-15 07:18:29 -070041 * The caller should call this when they are ready to move to the next benchmark.
joshualitt98d2e2f2015-10-05 07:23:30 -070042 */
joshualittc603c142015-10-15 07:18:29 -070043 void nextBenchmark();
joshualitt98d2e2f2015-10-05 07:23:30 -070044
joshualitt98d2e2f2015-10-05 07:23:30 -070045 /*
46 * When TimingStateMachine returns kTimingFinished_ParentEvents, then the owner can call
47 * lastMeasurement() to get the time
48 */
49 double lastMeasurement() const { return fLastMeasurement; }
50
51 int loops() const { return fLoops; }
52
53private:
joshualitt98d2e2f2015-10-05 07:23:30 -070054 enum State {
joshualittcb54e8e2015-10-05 13:58:26 -070055 kPreWarm_State,
joshualitt98d2e2f2015-10-05 07:23:30 -070056 kTiming_State,
57 };
joshualittcb54e8e2015-10-05 13:58:26 -070058 enum InnerState {
59 kTuning_InnerState,
60 kTiming_InnerState,
61 };
joshualitt98d2e2f2015-10-05 07:23:30 -070062
joshualitt98d2e2f2015-10-05 07:23:30 -070063 int fCurrentFrame;
64 int fLoops;
65 double fLastMeasurement;
mtkleinf27f08b2015-11-03 06:54:24 -080066 double fStartTime;
joshualitt98d2e2f2015-10-05 07:23:30 -070067 State fState;
joshualittcb54e8e2015-10-05 13:58:26 -070068 InnerState fInnerState;
joshualitt98d2e2f2015-10-05 07:23:30 -070069};
70
71#endif