1 /*
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 #ifndef TimingStateMachine_DEFINED
9 #define TimingStateMachine_DEFINED
10 
11 #include "Benchmark.h"
12 #include "SkTArray.h"
13 #include "Timer.h"
14 
15 class 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  */
27 class TimingStateMachine {
28 public:
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 
38     ParentEvents nextFrame(bool preWarmBetweenSamples);
39 
40     /*
41      * The caller should call this when they are ready to move to the next benchmark.
42      */
43     void nextBenchmark();
44 
45     /*
46      * When TimingStateMachine returns kTimingFinished_ParentEvents, then the owner can call
47      * lastMeasurement() to get the time
48      */
lastMeasurement()49     double lastMeasurement() const { return fLastMeasurement; }
50 
loops()51     int loops() const { return fLoops; }
52 
53 private:
54     enum State {
55         kPreWarm_State,
56         kTiming_State,
57     };
58     enum InnerState {
59         kTuning_InnerState,
60         kTiming_InnerState,
61     };
62 
63     int fCurrentFrame;
64     int fLoops;
65     double fLastMeasurement;
66     double fStartTime;
67     State fState;
68     InnerState fInnerState;
69 };
70 
71 #endif
72