aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/VisualBench/TimingStateMachine.h
blob: 9ada22391026ef77d5eb4082559aa2dcb89997fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef TimingStateMachine_DEFINED
#define TimingStateMachine_DEFINED

#include "Benchmark.h"
#include "SkTArray.h"
#include "Timer.h"

class SkCanvas;

/*
 * Manages a timer via a state machine.  Can be used by modules to time benchmarks
 *
 * Clients call nextFrame, and must handle any requests from the timing state machine, specifically
 * to reset.  When kTimingFinished_ParentEvents is returned, then lastMeasurement() will return the
 * timing and loops() will return the number of loops used to time.
 *
 * A client may continue timing the same benchmark indefinitely.  To advance to the next
 * benchmark, the client should call nextBenchmark.
 */
class TimingStateMachine {
public:
    TimingStateMachine();

    enum ParentEvents {
        kReset_ParentEvents,
        kTiming_ParentEvents,
        kTimingFinished_ParentEvents,// This implies parent can read lastMeasurement() and must
                                     // reset
    };

    ParentEvents nextFrame(bool preWarmBetweenSamples);

    /*
     * The caller should call this when they are ready to move to the next benchmark.
     */
    void nextBenchmark();

    /*
     * When TimingStateMachine returns kTimingFinished_ParentEvents, then the owner can call
     * lastMeasurement() to get the time
     */
    double lastMeasurement() const { return fLastMeasurement; }

    int loops() const { return fLoops; }

private:
    enum State {
        kPreWarm_State,
        kTiming_State,
    };
    enum InnerState {
        kTuning_InnerState,
        kTiming_InnerState,
    };

    int fCurrentFrame;
    int fLoops;
    double fLastMeasurement;
    double fStartTime;
    State fState;
    InnerState fInnerState;
};

#endif