joshualitt | d0f0bce | 2015-10-14 07:49:28 -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. |
| 6 | */ |
| 7 | |
| 8 | #include "VisualStreamTimingModule.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | |
| 12 | VisualStreamTimingModule::VisualStreamTimingModule(VisualBench* owner, bool preWarmBeforeSample) |
| 13 | : fReinitializeBenchmark(false) |
| 14 | , fPreWarmBeforeSample(preWarmBeforeSample) |
| 15 | , fOwner(owner) { |
| 16 | fBenchmarkStream.reset(new VisualBenchmarkStream); |
| 17 | } |
| 18 | |
| 19 | bool VisualStreamTimingModule::nextBenchmarkIfNecessary(SkCanvas* canvas) { |
| 20 | if (fBenchmark) { |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | fBenchmark.reset(fBenchmarkStream->next()); |
| 25 | if (!fBenchmark) { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | fOwner->clear(canvas, SK_ColorWHITE, 2); |
| 30 | |
| 31 | fBenchmark->delayedSetup(); |
| 32 | fBenchmark->preTimingHooks(canvas); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | void VisualStreamTimingModule::draw(SkCanvas* canvas) { |
| 37 | if (!this->nextBenchmarkIfNecessary(canvas)) { |
| 38 | SkDebugf("Exiting VisualBench successfully\n"); |
| 39 | fOwner->closeWindow(); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (fReinitializeBenchmark) { |
| 44 | fReinitializeBenchmark = false; |
| 45 | fBenchmark->preTimingHooks(canvas); |
| 46 | } |
| 47 | |
| 48 | this->renderFrame(canvas, fBenchmark, fTSM.loops()); |
| 49 | fOwner->present(); |
| 50 | TimingStateMachine::ParentEvents event = fTSM.nextFrame(fPreWarmBeforeSample); |
| 51 | switch (event) { |
| 52 | case TimingStateMachine::kReset_ParentEvents: |
| 53 | fBenchmark->postTimingHooks(canvas); |
| 54 | fOwner->reset(); |
| 55 | fReinitializeBenchmark = true; |
| 56 | break; |
| 57 | case TimingStateMachine::kTiming_ParentEvents: |
| 58 | break; |
| 59 | case TimingStateMachine::kTimingFinished_ParentEvents: |
| 60 | fBenchmark->postTimingHooks(canvas); |
| 61 | fOwner->reset(); |
| 62 | if (this->timingFinished(fBenchmark, fTSM.loops(), fTSM.lastMeasurement())) { |
| 63 | fTSM.nextBenchmark(canvas, fBenchmark); |
| 64 | fBenchmark.reset(nullptr); |
| 65 | } else { |
| 66 | fReinitializeBenchmark = true; |
| 67 | } |
| 68 | break; |
| 69 | } |
| 70 | } |