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 | |
cdalton | 266e202 | 2015-11-13 08:28:49 -0800 | [diff] [blame] | 12 | DEFINE_bool(reset, true, "Reset the GL context between samples."); |
| 13 | |
| 14 | VisualStreamTimingModule::VisualStreamTimingModule(VisualBench* owner) |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 15 | : fInitState(kReset_InitState) |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 16 | , fOwner(owner) { |
cdalton | c70483f | 2015-10-26 13:14:36 -0700 | [diff] [blame] | 17 | fBenchmarkStream.reset(new VisualBenchmarkStream(owner->getSurfaceProps())); |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 18 | } |
| 19 | |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 20 | inline void VisualStreamTimingModule::handleInitState(SkCanvas* canvas) { |
| 21 | switch (fInitState) { |
| 22 | case kNewBenchmark_InitState: |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 23 | fBenchmarkStream->current()->delayedSetup(); |
| 24 | // fallthrough |
| 25 | case kReset_InitState: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 26 | // This will flicker unfortunately, but as we are reseting the GLTestContext each bench, |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 27 | // we unfortunately don't have a choice |
cdalton | f844177 | 2015-11-13 08:56:31 -0800 | [diff] [blame] | 28 | fOwner->clear(canvas, SK_ColorWHITE, 3); |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 29 | fBenchmarkStream->current()->preTimingHooks(canvas); |
| 30 | break; |
| 31 | case kNone_InitState: |
| 32 | break; |
joshualitt | a3b8c67 | 2015-10-14 14:45:07 -0700 | [diff] [blame] | 33 | } |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 34 | fInitState = kNone_InitState; |
joshualitt | a3b8c67 | 2015-10-14 14:45:07 -0700 | [diff] [blame] | 35 | } |
| 36 | |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 37 | inline void VisualStreamTimingModule::handleTimingEvent(SkCanvas* canvas, |
| 38 | TimingStateMachine::ParentEvents event) { |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 39 | switch (event) { |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 40 | case TimingStateMachine::kTiming_ParentEvents: |
| 41 | break; |
| 42 | case TimingStateMachine::kTimingFinished_ParentEvents: |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 43 | if (this->timingFinished(fBenchmarkStream->current(), fTSM.loops(), |
| 44 | fTSM.lastMeasurement())) { |
cdalton | 266e202 | 2015-11-13 08:28:49 -0800 | [diff] [blame] | 45 | fBenchmarkStream->current()->postTimingHooks(canvas); |
| 46 | fOwner->reset(); |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 47 | fTSM.nextBenchmark(); |
| 48 | if (!fBenchmarkStream->next()) { |
| 49 | SkDebugf("Exiting VisualBench successfully\n"); |
| 50 | fOwner->closeWindow(); |
| 51 | } else { |
| 52 | fInitState = kNewBenchmark_InitState; |
| 53 | } |
cdalton | 266e202 | 2015-11-13 08:28:49 -0800 | [diff] [blame] | 54 | break; |
| 55 | } |
| 56 | // fallthrough |
| 57 | case TimingStateMachine::kReset_ParentEvents: |
| 58 | if (FLAGS_reset) { |
| 59 | fBenchmarkStream->current()->postTimingHooks(canvas); |
| 60 | fOwner->reset(); |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 61 | fInitState = kReset_InitState; |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 62 | } |
| 63 | break; |
| 64 | } |
| 65 | } |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 66 | |
| 67 | void VisualStreamTimingModule::draw(SkCanvas* canvas) { |
| 68 | if (!fBenchmarkStream->current()) { |
| 69 | // this should never happen but just to be safe |
| 70 | // TODO research why this does happen on mac |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | this->handleInitState(canvas); |
| 75 | this->renderFrame(canvas, fBenchmarkStream->current(), fTSM.loops()); |
| 76 | fOwner->present(); |
cdalton | 266e202 | 2015-11-13 08:28:49 -0800 | [diff] [blame] | 77 | TimingStateMachine::ParentEvents event = fTSM.nextFrame(FLAGS_reset); |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 78 | this->handleTimingEvent(canvas, event); |
| 79 | } |