blob: 22150599ab82de62938d7c1a8e999cc310e1bfa8 [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 /*
joshualitta3b8c672015-10-14 14:45:07 -070041 * The caller should call this when they are ready to move to the next benchmark. The caller
42 * must call this with the *last* benchmark so post draw hooks can be invoked
joshualitt98d2e2f2015-10-05 07:23:30 -070043 */
joshualitta3b8c672015-10-14 14:45:07 -070044 void nextBenchmark(SkCanvas*, Benchmark*);
joshualitt98d2e2f2015-10-05 07:23:30 -070045
joshualitt98d2e2f2015-10-05 07:23:30 -070046 /*
47 * When TimingStateMachine returns kTimingFinished_ParentEvents, then the owner can call
48 * lastMeasurement() to get the time
49 */
50 double lastMeasurement() const { return fLastMeasurement; }
51
52 int loops() const { return fLoops; }
53
54private:
joshualitt98d2e2f2015-10-05 07:23:30 -070055 enum State {
joshualittcb54e8e2015-10-05 13:58:26 -070056 kPreWarm_State,
joshualitt98d2e2f2015-10-05 07:23:30 -070057 kTiming_State,
58 };
joshualittcb54e8e2015-10-05 13:58:26 -070059 enum InnerState {
60 kTuning_InnerState,
61 kTiming_InnerState,
62 };
joshualitt98d2e2f2015-10-05 07:23:30 -070063
joshualitt98d2e2f2015-10-05 07:23:30 -070064 inline double elapsed();
65 void resetTimingState();
joshualitt98d2e2f2015-10-05 07:23:30 -070066 void recordMeasurement();
67
68 int fCurrentFrame;
69 int fLoops;
70 double fLastMeasurement;
71 WallTimer fTimer;
72 State fState;
joshualittcb54e8e2015-10-05 13:58:26 -070073 InnerState fInnerState;
joshualitt98d2e2f2015-10-05 07:23:30 -070074};
75
76#endif