aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/VisualBench/VisualStreamTimingModule.cpp
blob: b5726219dd13a430e5e82f560de387f738bd71f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "VisualStreamTimingModule.h"

#include "SkCanvas.h"

DEFINE_bool(reset, true, "Reset the GL context between samples.");

VisualStreamTimingModule::VisualStreamTimingModule(VisualBench* owner)
    : fInitState(kReset_InitState)
    , fOwner(owner) {
    fBenchmarkStream.reset(new VisualBenchmarkStream(owner->getSurfaceProps()));
}

inline void VisualStreamTimingModule::handleInitState(SkCanvas* canvas) {
    switch (fInitState) {
        case kNewBenchmark_InitState:
            fBenchmarkStream->current()->delayedSetup();
            // fallthrough
        case kReset_InitState:
            // This will flicker unfortunately, but as we are reseting the GLTestContext each bench,
            // we unfortunately don't have a choice
            fOwner->clear(canvas, SK_ColorWHITE, 3);
            fBenchmarkStream->current()->preTimingHooks(canvas);
            break;
        case kNone_InitState:
            break;
    }
    fInitState = kNone_InitState;
}

inline void VisualStreamTimingModule::handleTimingEvent(SkCanvas* canvas,
                                                        TimingStateMachine::ParentEvents event) {
    switch (event) {
        case TimingStateMachine::kTiming_ParentEvents:
            break;
        case TimingStateMachine::kTimingFinished_ParentEvents:
            if (this->timingFinished(fBenchmarkStream->current(), fTSM.loops(),
                                     fTSM.lastMeasurement())) {
                fBenchmarkStream->current()->postTimingHooks(canvas);
                fOwner->reset();
                fTSM.nextBenchmark();
                if (!fBenchmarkStream->next()) {
                    SkDebugf("Exiting VisualBench successfully\n");
                    fOwner->closeWindow();
                } else {
                    fInitState = kNewBenchmark_InitState;
                }
                break;
            }
            // fallthrough
        case TimingStateMachine::kReset_ParentEvents:
            if (FLAGS_reset) {
                fBenchmarkStream->current()->postTimingHooks(canvas);
                fOwner->reset();
                fInitState = kReset_InitState;
            }
            break;
    }
}

void VisualStreamTimingModule::draw(SkCanvas* canvas) {
    if (!fBenchmarkStream->current()) {
        // this should never happen but just to be safe
        // TODO research why this does happen on mac
        return;
    }

    this->handleInitState(canvas);
    this->renderFrame(canvas, fBenchmarkStream->current(), fTSM.loops());
    fOwner->present();
    TimingStateMachine::ParentEvents event = fTSM.nextFrame(FLAGS_reset);
    this->handleTimingEvent(canvas, event);
}