joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 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. |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 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 | |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 38 | ParentEvents nextFrame(bool preWarmBetweenSamples); |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 41 | * The caller should call this when they are ready to move to the next benchmark. |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 42 | */ |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 43 | void nextBenchmark(); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 44 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 45 | /* |
| 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 | |
| 53 | private: |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 54 | enum State { |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 55 | kPreWarm_State, |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 56 | kTiming_State, |
| 57 | }; |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 58 | enum InnerState { |
| 59 | kTuning_InnerState, |
| 60 | kTiming_InnerState, |
| 61 | }; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 62 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 63 | int fCurrentFrame; |
| 64 | int fLoops; |
| 65 | double fLastMeasurement; |
mtklein | f27f08b | 2015-11-03 06:54:24 -0800 | [diff] [blame] | 66 | double fStartTime; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 67 | State fState; |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 68 | InnerState fInnerState; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | #endif |