blob: b5726219dd13a430e5e82f560de387f738bd71f3 [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
cdalton266e2022015-11-13 08:28:49 -080012DEFINE_bool(reset, true, "Reset the GL context between samples.");
13
14VisualStreamTimingModule::VisualStreamTimingModule(VisualBench* owner)
joshualittc603c142015-10-15 07:18:29 -070015 : fInitState(kReset_InitState)
joshualittd0f0bce2015-10-14 07:49:28 -070016 , fOwner(owner) {
cdaltonc70483f2015-10-26 13:14:36 -070017 fBenchmarkStream.reset(new VisualBenchmarkStream(owner->getSurfaceProps()));
joshualittd0f0bce2015-10-14 07:49:28 -070018}
19
joshualittc603c142015-10-15 07:18:29 -070020inline void VisualStreamTimingModule::handleInitState(SkCanvas* canvas) {
21 switch (fInitState) {
22 case kNewBenchmark_InitState:
joshualittc603c142015-10-15 07:18:29 -070023 fBenchmarkStream->current()->delayedSetup();
24 // fallthrough
25 case kReset_InitState:
bsalomon273c0f52016-03-31 10:59:06 -070026 // This will flicker unfortunately, but as we are reseting the GLTestContext each bench,
joshualittfb02cda2015-10-21 08:04:24 -070027 // we unfortunately don't have a choice
cdaltonf8441772015-11-13 08:56:31 -080028 fOwner->clear(canvas, SK_ColorWHITE, 3);
joshualittc603c142015-10-15 07:18:29 -070029 fBenchmarkStream->current()->preTimingHooks(canvas);
30 break;
31 case kNone_InitState:
32 break;
joshualitta3b8c672015-10-14 14:45:07 -070033 }
joshualittc603c142015-10-15 07:18:29 -070034 fInitState = kNone_InitState;
joshualitta3b8c672015-10-14 14:45:07 -070035}
36
joshualittc603c142015-10-15 07:18:29 -070037inline void VisualStreamTimingModule::handleTimingEvent(SkCanvas* canvas,
38 TimingStateMachine::ParentEvents event) {
joshualittd0f0bce2015-10-14 07:49:28 -070039 switch (event) {
joshualittd0f0bce2015-10-14 07:49:28 -070040 case TimingStateMachine::kTiming_ParentEvents:
41 break;
42 case TimingStateMachine::kTimingFinished_ParentEvents:
joshualittc603c142015-10-15 07:18:29 -070043 if (this->timingFinished(fBenchmarkStream->current(), fTSM.loops(),
44 fTSM.lastMeasurement())) {
cdalton266e2022015-11-13 08:28:49 -080045 fBenchmarkStream->current()->postTimingHooks(canvas);
46 fOwner->reset();
joshualittc603c142015-10-15 07:18:29 -070047 fTSM.nextBenchmark();
48 if (!fBenchmarkStream->next()) {
49 SkDebugf("Exiting VisualBench successfully\n");
50 fOwner->closeWindow();
51 } else {
52 fInitState = kNewBenchmark_InitState;
53 }
cdalton266e2022015-11-13 08:28:49 -080054 break;
55 }
56 // fallthrough
57 case TimingStateMachine::kReset_ParentEvents:
58 if (FLAGS_reset) {
59 fBenchmarkStream->current()->postTimingHooks(canvas);
60 fOwner->reset();
joshualittc603c142015-10-15 07:18:29 -070061 fInitState = kReset_InitState;
joshualittd0f0bce2015-10-14 07:49:28 -070062 }
63 break;
64 }
65}
joshualittc603c142015-10-15 07:18:29 -070066
67void 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();
cdalton266e2022015-11-13 08:28:49 -080077 TimingStateMachine::ParentEvents event = fTSM.nextFrame(FLAGS_reset);
joshualittc603c142015-10-15 07:18:29 -070078 this->handleTimingEvent(canvas, event);
79}