OLD | NEW |
(Empty) | |
| 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 #include "TimingStateMachine.h" |
| 9 |
| 10 #include "SkCanvas.h" |
| 11 #include "SkCommandLineFlags.h" |
| 12 |
| 13 DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU is al
lowed to lag."); |
| 14 DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample."); |
| 15 DEFINE_double(loopMs, 5, "Each benchmark will be tuned until it takes loopsMs mi
llseconds."); |
| 16 |
| 17 TimingStateMachine::TimingStateMachine() |
| 18 : fCurrentFrame(0) |
| 19 , fLoops(1) |
| 20 , fLastMeasurement(0.) |
| 21 , fState(kPreWarmLoopsPerCanvasPreDraw_State) { |
| 22 } |
| 23 |
| 24 TimingStateMachine::ParentEvents TimingStateMachine::nextFrame(SkCanvas* canvas, |
| 25 Benchmark* benchm
ark) { |
| 26 switch (fState) { |
| 27 case kPreWarmLoopsPerCanvasPreDraw_State: |
| 28 return this->perCanvasPreDraw(canvas, benchmark, kPreWarmLoops_State
); |
| 29 case kPreWarmLoops_State: |
| 30 return this->preWarm(kTuneLoops_State); |
| 31 case kTuneLoops_State: |
| 32 return this->tuneLoops(); |
| 33 case kPreWarmTimingPerCanvasPreDraw_State: |
| 34 return this->perCanvasPreDraw(canvas, benchmark, kPreWarmTiming_Stat
e); |
| 35 case kPreWarmTiming_State: |
| 36 return this->preWarm(kTiming_State); |
| 37 case kTiming_State: |
| 38 return this->timing(canvas, benchmark); |
| 39 } |
| 40 SkFAIL("Incomplete switch\n"); |
| 41 return kTiming_ParentEvents; |
| 42 } |
| 43 |
| 44 inline void TimingStateMachine::nextState(State nextState) { |
| 45 fState = nextState; |
| 46 } |
| 47 |
| 48 TimingStateMachine::ParentEvents TimingStateMachine::perCanvasPreDraw(SkCanvas*
canvas, |
| 49 Benchmark*
benchmark, |
| 50 State next
State) { |
| 51 benchmark->perCanvasPreDraw(canvas); |
| 52 benchmark->preDraw(canvas); |
| 53 fCurrentFrame = 0; |
| 54 this->nextState(nextState); |
| 55 return kTiming_ParentEvents; |
| 56 } |
| 57 |
| 58 TimingStateMachine::ParentEvents TimingStateMachine::preWarm(State nextState) { |
| 59 if (fCurrentFrame >= FLAGS_gpuFrameLag) { |
| 60 // we currently time across all frames to make sure we capture all GPU w
ork |
| 61 this->nextState(nextState); |
| 62 fCurrentFrame = 0; |
| 63 fTimer.start(); |
| 64 } else { |
| 65 fCurrentFrame++; |
| 66 } |
| 67 return kTiming_ParentEvents; |
| 68 } |
| 69 |
| 70 inline double TimingStateMachine::elapsed() { |
| 71 fTimer.end(); |
| 72 return fTimer.fWall; |
| 73 } |
| 74 |
| 75 void TimingStateMachine::resetTimingState() { |
| 76 fCurrentFrame = 0; |
| 77 fTimer = WallTimer(); |
| 78 } |
| 79 |
| 80 inline TimingStateMachine::ParentEvents TimingStateMachine::tuneLoops() { |
| 81 if (1 << 30 == fLoops) { |
| 82 // We're about to wrap. Something's wrong with the bench. |
| 83 SkDebugf("InnerLoops wrapped\n"); |
| 84 fLoops = 1; |
| 85 return kTiming_ParentEvents; |
| 86 } else { |
| 87 double elapsedMs = this->elapsed(); |
| 88 if (elapsedMs > FLAGS_loopMs) { |
| 89 this->nextState(kPreWarmTimingPerCanvasPreDraw_State); |
| 90 } else { |
| 91 fLoops *= 2; |
| 92 this->nextState(kPreWarmLoops_State); |
| 93 } |
| 94 this->resetTimingState(); |
| 95 return kReset_ParentEvents; |
| 96 } |
| 97 } |
| 98 |
| 99 void TimingStateMachine::recordMeasurement() { |
| 100 fLastMeasurement = this->elapsed() / (FLAGS_frames * fLoops); |
| 101 } |
| 102 |
| 103 void TimingStateMachine::nextBenchmark(SkCanvas* canvas, Benchmark* benchmark) { |
| 104 benchmark->postDraw(canvas); |
| 105 benchmark->perCanvasPostDraw(canvas); |
| 106 fLoops = 1; |
| 107 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State); |
| 108 } |
| 109 |
| 110 inline TimingStateMachine::ParentEvents TimingStateMachine::timing(SkCanvas* can
vas, |
| 111 Benchmark* be
nchmark) { |
| 112 if (fCurrentFrame >= FLAGS_frames) { |
| 113 this->recordMeasurement(); |
| 114 this->resetTimingState(); |
| 115 this->nextState(kPreWarmTimingPerCanvasPreDraw_State); |
| 116 return kTimingFinished_ParentEvents; |
| 117 } else { |
| 118 fCurrentFrame++; |
| 119 return kTiming_ParentEvents; |
| 120 } |
| 121 } |
| 122 |
OLD | NEW |