aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/VisualBench/VisualDebugModule.cpp
blob: e4e70ddacf86b4fdd4c6ff978e3799cfed693c50 (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
/*
 * 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 "VisualDebugModule.h"

#include "SkCanvas.h"

VisualDebugModule::VisualDebugModule(VisualBench* owner)
    : fState(kInit_State)
    , fIndex(0)
    , fOwner(owner) {
    // VisualDebugModule only really makes sense for SKPs
    fBenchmarkStream.reset(new VisualBenchmarkStream(owner->getSurfaceProps(), true));
}

bool VisualDebugModule::advanceIfNecessary(SkCanvas* canvas) {
    Benchmark* benchmark = fBenchmarkStream->current();
    switch (fState) {
        case kInit_State: {
            // setup new benchmark
            benchmark->delayedSetup();
            fOwner->clear(canvas, SK_ColorWHITE, 3);
            benchmark->preTimingHooks(canvas);

            // reset debug canvas
            SkIPoint size = benchmark->getSize();
            fDebugCanvas.reset(new SkDebugCanvas(size.fX, size.fY));

            // pour benchmark into canvas
            benchmark->draw(1, fDebugCanvas);
            fIndex = fDebugCanvas->getSize() - 1;
            fState = kPlay_State;
            break;
        }
        case kPlay_State: break;
        case kNext_State:
            // cleanup after the last SKP
            benchmark->postTimingHooks(canvas);
            fOwner->reset();
            if (!fBenchmarkStream->next()) {
                SkDebugf("Exiting VisualBench successfully\n");
                fOwner->closeWindow();
                return false;
            }
            fState = kInit_State;
            break;
    }
    return true;
}

void VisualDebugModule::draw(SkCanvas* canvas) {
    if (!fBenchmarkStream->current() || !this->advanceIfNecessary(canvas)) {
        return;
    }

    fDebugCanvas->drawTo(canvas, fIndex);
    canvas->flush();
    fOwner->present();
}

bool VisualDebugModule::onHandleChar(SkUnichar c) {
    switch (c) {
        case ' ': fState = kNext_State; break;
        case 'a': fIndex = (fIndex + 1) % (fDebugCanvas->getSize() - 1); break;
        case 's': fIndex = fIndex <= 0 ? fDebugCanvas->getSize() - 1 : fIndex - 1; break;
        default: break;
    }

    return true;
}