16ebd4239410dabf594e0d40ac610e92bf942b651joshualitt/*
26ebd4239410dabf594e0d40ac610e92bf942b651joshualitt * Copyright 2015 Google Inc.
36ebd4239410dabf594e0d40ac610e92bf942b651joshualitt *
46ebd4239410dabf594e0d40ac610e92bf942b651joshualitt * Use of this source code is governed by a BSD-style license that can be
56ebd4239410dabf594e0d40ac610e92bf942b651joshualitt * found in the LICENSE file.
66ebd4239410dabf594e0d40ac610e92bf942b651joshualitt */
76ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
86ebd4239410dabf594e0d40ac610e92bf942b651joshualitt#include "VisualDebugModule.h"
96ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
106ebd4239410dabf594e0d40ac610e92bf942b651joshualitt#include "SkCanvas.h"
116ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
126ebd4239410dabf594e0d40ac610e92bf942b651joshualittVisualDebugModule::VisualDebugModule(VisualBench* owner)
136ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    : fState(kInit_State)
146ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    , fIndex(0)
156ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    , fOwner(owner) {
166ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    // VisualDebugModule only really makes sense for SKPs
176ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    fBenchmarkStream.reset(new VisualBenchmarkStream(owner->getSurfaceProps(), true));
186ebd4239410dabf594e0d40ac610e92bf942b651joshualitt}
196ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
206ebd4239410dabf594e0d40ac610e92bf942b651joshualittbool VisualDebugModule::advanceIfNecessary(SkCanvas* canvas) {
216ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    Benchmark* benchmark = fBenchmarkStream->current();
226ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    switch (fState) {
236ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        case kInit_State: {
246ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            // setup new benchmark
256ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            benchmark->delayedSetup();
266ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            fOwner->clear(canvas, SK_ColorWHITE, 3);
276ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            benchmark->preTimingHooks(canvas);
286ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
296ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            // reset debug canvas
306ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            SkIPoint size = benchmark->getSize();
316ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            fDebugCanvas.reset(new SkDebugCanvas(size.fX, size.fY));
326ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
336ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            // pour benchmark into canvas
346ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            benchmark->draw(1, fDebugCanvas);
356ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            fIndex = fDebugCanvas->getSize() - 1;
366ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            fState = kPlay_State;
376ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            break;
386ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        }
396ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        case kPlay_State: break;
406ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        case kNext_State:
416ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            // cleanup after the last SKP
426ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            benchmark->postTimingHooks(canvas);
436ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            fOwner->reset();
446ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            if (!fBenchmarkStream->next()) {
456ebd4239410dabf594e0d40ac610e92bf942b651joshualitt                SkDebugf("Exiting VisualBench successfully\n");
466ebd4239410dabf594e0d40ac610e92bf942b651joshualitt                fOwner->closeWindow();
476ebd4239410dabf594e0d40ac610e92bf942b651joshualitt                return false;
486ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            }
496ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            fState = kInit_State;
506ebd4239410dabf594e0d40ac610e92bf942b651joshualitt            break;
516ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    }
526ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    return true;
536ebd4239410dabf594e0d40ac610e92bf942b651joshualitt}
546ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
556ebd4239410dabf594e0d40ac610e92bf942b651joshualittvoid VisualDebugModule::draw(SkCanvas* canvas) {
566ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    if (!fBenchmarkStream->current() || !this->advanceIfNecessary(canvas)) {
576ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        return;
586ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    }
596ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
606ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    fDebugCanvas->drawTo(canvas, fIndex);
616ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    canvas->flush();
626ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    fOwner->present();
636ebd4239410dabf594e0d40ac610e92bf942b651joshualitt}
646ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
656ebd4239410dabf594e0d40ac610e92bf942b651joshualittbool VisualDebugModule::onHandleChar(SkUnichar c) {
666ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    switch (c) {
676ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        case ' ': fState = kNext_State; break;
686ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        case 'a': fIndex = (fIndex + 1) % (fDebugCanvas->getSize() - 1); break;
696ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        case 's': fIndex = fIndex <= 0 ? fDebugCanvas->getSize() - 1 : fIndex - 1; break;
706ebd4239410dabf594e0d40ac610e92bf942b651joshualitt        default: break;
716ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    }
726ebd4239410dabf594e0d40ac610e92bf942b651joshualitt
736ebd4239410dabf594e0d40ac610e92bf942b651joshualitt    return true;
746ebd4239410dabf594e0d40ac610e92bf942b651joshualitt}
75