blob: ad7c1d0e80c00204835d4f587517a08ec618a185 [file] [log] [blame]
joshualittd0f0bce2015-10-14 07:49:28 -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#include "VisualStreamTimingModule.h"
9
10#include "SkCanvas.h"
11
12VisualStreamTimingModule::VisualStreamTimingModule(VisualBench* owner, bool preWarmBeforeSample)
joshualittc603c142015-10-15 07:18:29 -070013 : fInitState(kReset_InitState)
joshualittd0f0bce2015-10-14 07:49:28 -070014 , fPreWarmBeforeSample(preWarmBeforeSample)
15 , fOwner(owner) {
cdaltonc70483f2015-10-26 13:14:36 -070016 fBenchmarkStream.reset(new VisualBenchmarkStream(owner->getSurfaceProps()));
joshualittd0f0bce2015-10-14 07:49:28 -070017}
18
joshualittc603c142015-10-15 07:18:29 -070019inline void VisualStreamTimingModule::handleInitState(SkCanvas* canvas) {
20 switch (fInitState) {
21 case kNewBenchmark_InitState:
joshualittc603c142015-10-15 07:18:29 -070022 fBenchmarkStream->current()->delayedSetup();
23 // fallthrough
24 case kReset_InitState:
joshualittfb02cda2015-10-21 08:04:24 -070025 // This will flicker unfortunately, but as we are reseting the GLContext each bench,
26 // we unfortunately don't have a choice
27 fOwner->clear(canvas, SK_ColorWHITE, 2);
joshualittc603c142015-10-15 07:18:29 -070028 fBenchmarkStream->current()->preTimingHooks(canvas);
29 break;
30 case kNone_InitState:
31 break;
joshualitta3b8c672015-10-14 14:45:07 -070032 }
joshualittc603c142015-10-15 07:18:29 -070033 fInitState = kNone_InitState;
joshualitta3b8c672015-10-14 14:45:07 -070034}
35
joshualittc603c142015-10-15 07:18:29 -070036inline void VisualStreamTimingModule::handleTimingEvent(SkCanvas* canvas,
37 TimingStateMachine::ParentEvents event) {
joshualittd0f0bce2015-10-14 07:49:28 -070038 switch (event) {
39 case TimingStateMachine::kReset_ParentEvents:
joshualittc603c142015-10-15 07:18:29 -070040 fBenchmarkStream->current()->postTimingHooks(canvas);
joshualittd0f0bce2015-10-14 07:49:28 -070041 fOwner->reset();
joshualittc603c142015-10-15 07:18:29 -070042 fInitState = kReset_InitState;
joshualittd0f0bce2015-10-14 07:49:28 -070043 break;
44 case TimingStateMachine::kTiming_ParentEvents:
45 break;
46 case TimingStateMachine::kTimingFinished_ParentEvents:
joshualittc603c142015-10-15 07:18:29 -070047 fBenchmarkStream->current()->postTimingHooks(canvas);
joshualittd0f0bce2015-10-14 07:49:28 -070048 fOwner->reset();
joshualittc603c142015-10-15 07:18:29 -070049 if (this->timingFinished(fBenchmarkStream->current(), fTSM.loops(),
50 fTSM.lastMeasurement())) {
51 fTSM.nextBenchmark();
52 if (!fBenchmarkStream->next()) {
53 SkDebugf("Exiting VisualBench successfully\n");
54 fOwner->closeWindow();
55 } else {
56 fInitState = kNewBenchmark_InitState;
57 }
joshualittd0f0bce2015-10-14 07:49:28 -070058 } else {
joshualittc603c142015-10-15 07:18:29 -070059 fInitState = kReset_InitState;
joshualittd0f0bce2015-10-14 07:49:28 -070060 }
61 break;
62 }
63}
joshualittc603c142015-10-15 07:18:29 -070064
65void VisualStreamTimingModule::draw(SkCanvas* canvas) {
66 if (!fBenchmarkStream->current()) {
67 // this should never happen but just to be safe
68 // TODO research why this does happen on mac
69 return;
70 }
71
72 this->handleInitState(canvas);
73 this->renderFrame(canvas, fBenchmarkStream->current(), fTSM.loops());
74 fOwner->present();
75 TimingStateMachine::ParentEvents event = fTSM.nextFrame(fPreWarmBeforeSample);
76 this->handleTimingEvent(canvas, event);
77}