blob: 69ea24337fb752894d32da698946285da862179c [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
39 ParentEvents nextFrame(SkCanvas* canvas, Benchmark* benchmark);
40
41 /*
42 * 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
47
48 /*
49 * When TimingStateMachine returns kTimingFinished_ParentEvents, then the owner can call
50 * lastMeasurement() to get the time
51 */
52 double lastMeasurement() const { return fLastMeasurement; }
53
54 int loops() const { return fLoops; }
55
56private:
57 /*
58 * The heart of the timing state machine is an event driven timing loop.
59 * kPreWarmLoopsPerCanvasPreDraw_State: Before we begin timing, Benchmarks have a hook to
60 * access the canvas. Then we prewarm before the autotune
61 * loops step.
62 * kPreWarmLoops_State: We prewarm the gpu before auto tuning to enter a steady
63 * work state
64 * kTuneLoops_State: Then we tune the loops of the benchmark to ensure we
65 * are doing a measurable amount of work
66 * kPreWarmTimingPerCanvasPreDraw_State: Because reset the context after tuning loops to ensure
67 * coherent state, we need to give the benchmark
68 * another hook
69 * kPreWarmTiming_State: We prewarm the gpu again to enter a steady state
70 * kTiming_State: Finally we time the benchmark. When finished timing
71 * if we have enough samples then we'll start the next
72 * benchmark in the kPreWarmLoopsPerCanvasPreDraw_State.
73 * otherwise, we enter the
74 * kPreWarmTimingPerCanvasPreDraw_State for another sample
75 * In either case we reset the context.
76 */
77 enum State {
78 kPreWarmLoopsPerCanvasPreDraw_State,
79 kPreWarmLoops_State,
80 kTuneLoops_State,
81 kPreWarmTimingPerCanvasPreDraw_State,
82 kPreWarmTiming_State,
83 kTiming_State,
84 };
85
86 inline void nextState(State);
87 ParentEvents perCanvasPreDraw(SkCanvas*, Benchmark*, State);
88 ParentEvents preWarm(State nextState);
89 inline ParentEvents tuneLoops();
90 inline ParentEvents timing(SkCanvas*, Benchmark*);
91 inline double elapsed();
92 void resetTimingState();
93 void postDraw(SkCanvas*, Benchmark*);
94 void recordMeasurement();
95
96 int fCurrentFrame;
97 int fLoops;
98 double fLastMeasurement;
99 WallTimer fTimer;
100 State fState;
101};
102
103#endif