blob: 7f7a5affb5dfeb29cf41523e2e687dd0a6d988bc [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.
6 *
7 */
8
9#ifndef TimingStateMachine_DEFINED
10#define TimingStateMachine_DEFINED
11
12#include "Benchmark.h"
13#include "SkTArray.h"
14#include "Timer.h"
15
16class SkCanvas;
17
18/*
19 * Manages a timer via a state machine. Can be used by modules to time benchmarks
20 *
21 * Clients call nextFrame, and must handle any requests from the timing state machine, specifically
22 * to reset. When kTimingFinished_ParentEvents is returned, then lastMeasurement() will return the
23 * timing and loops() will return the number of loops used to time.
24 *
25 * A client may continue timing the same benchmark indefinitely. To advance to the next
26 * benchmark, the client should call nextBenchmark.
27 */
28class TimingStateMachine {
29public:
30 TimingStateMachine();
31
32 enum ParentEvents {
33 kReset_ParentEvents,
34 kTiming_ParentEvents,
35 kTimingFinished_ParentEvents,// This implies parent can read lastMeasurement() and must
36 // reset
37 };
38
joshualittcb54e8e2015-10-05 13:58:26 -070039 ParentEvents nextFrame(bool preWarmBetweenSamples);
joshualittdc5db592015-10-05 13:24:55 -070040
41 /*
joshualitt98d2e2f2015-10-05 07:23:30 -070042 * The caller should call this when they are ready to move to the next benchmark. The caller
43 * must call this with the *last* benchmark so post draw hooks can be invoked
44 */
45 void nextBenchmark(SkCanvas*, Benchmark*);
46
joshualitt98d2e2f2015-10-05 07:23:30 -070047 /*
48 * When TimingStateMachine returns kTimingFinished_ParentEvents, then the owner can call
49 * lastMeasurement() to get the time
50 */
51 double lastMeasurement() const { return fLastMeasurement; }
52
53 int loops() const { return fLoops; }
54
55private:
joshualitt98d2e2f2015-10-05 07:23:30 -070056 enum State {
joshualittcb54e8e2015-10-05 13:58:26 -070057 kPreWarm_State,
joshualitt98d2e2f2015-10-05 07:23:30 -070058 kTiming_State,
59 };
joshualittcb54e8e2015-10-05 13:58:26 -070060 enum InnerState {
61 kTuning_InnerState,
62 kTiming_InnerState,
63 };
joshualitt98d2e2f2015-10-05 07:23:30 -070064
joshualitt98d2e2f2015-10-05 07:23:30 -070065 inline double elapsed();
66 void resetTimingState();
joshualitt98d2e2f2015-10-05 07:23:30 -070067 void recordMeasurement();
68
69 int fCurrentFrame;
70 int fLoops;
71 double fLastMeasurement;
72 WallTimer fTimer;
73 State fState;
joshualittcb54e8e2015-10-05 13:58:26 -070074 InnerState fInnerState;
joshualitt98d2e2f2015-10-05 07:23:30 -070075};
76
77#endif