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 | a3b8c67 | 2015-10-14 14:45:07 -0700 | [diff] [blame] | 41 | * 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 |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 43 | */ |
joshualitt | a3b8c67 | 2015-10-14 14:45:07 -0700 | [diff] [blame] | 44 | void nextBenchmark(SkCanvas*, Benchmark*); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 45 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 46 | /* |
| 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 | |
| 54 | private: |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 55 | enum State { |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 56 | kPreWarm_State, |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 57 | kTiming_State, |
| 58 | }; |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 59 | enum InnerState { |
| 60 | kTuning_InnerState, |
| 61 | kTiming_InnerState, |
| 62 | }; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 63 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 64 | inline double elapsed(); |
| 65 | void resetTimingState(); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 66 | void recordMeasurement(); |
| 67 | |
| 68 | int fCurrentFrame; |
| 69 | int fLoops; |
| 70 | double fLastMeasurement; |
| 71 | WallTimer fTimer; |
| 72 | State fState; |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 73 | InnerState fInnerState; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | #endif |