1 /*
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 "VisualDebugModule.h"
9 
10 #include "SkCanvas.h"
11 
VisualDebugModule(VisualBench * owner)12 VisualDebugModule::VisualDebugModule(VisualBench* owner)
13     : fState(kInit_State)
14     , fIndex(0)
15     , fOwner(owner) {
16     // VisualDebugModule only really makes sense for SKPs
17     fBenchmarkStream.reset(new VisualBenchmarkStream(owner->getSurfaceProps(), true));
18 }
19 
advanceIfNecessary(SkCanvas * canvas)20 bool VisualDebugModule::advanceIfNecessary(SkCanvas* canvas) {
21     Benchmark* benchmark = fBenchmarkStream->current();
22     switch (fState) {
23         case kInit_State: {
24             // setup new benchmark
25             benchmark->delayedSetup();
26             fOwner->clear(canvas, SK_ColorWHITE, 3);
27             benchmark->preTimingHooks(canvas);
28 
29             // reset debug canvas
30             SkIPoint size = benchmark->getSize();
31             fDebugCanvas.reset(new SkDebugCanvas(size.fX, size.fY));
32 
33             // pour benchmark into canvas
34             benchmark->draw(1, fDebugCanvas);
35             fIndex = fDebugCanvas->getSize() - 1;
36             fState = kPlay_State;
37             break;
38         }
39         case kPlay_State: break;
40         case kNext_State:
41             // cleanup after the last SKP
42             benchmark->postTimingHooks(canvas);
43             fOwner->reset();
44             if (!fBenchmarkStream->next()) {
45                 SkDebugf("Exiting VisualBench successfully\n");
46                 fOwner->closeWindow();
47                 return false;
48             }
49             fState = kInit_State;
50             break;
51     }
52     return true;
53 }
54 
draw(SkCanvas * canvas)55 void VisualDebugModule::draw(SkCanvas* canvas) {
56     if (!fBenchmarkStream->current() || !this->advanceIfNecessary(canvas)) {
57         return;
58     }
59 
60     fDebugCanvas->drawTo(canvas, fIndex);
61     canvas->flush();
62     fOwner->present();
63 }
64 
onHandleChar(SkUnichar c)65 bool VisualDebugModule::onHandleChar(SkUnichar c) {
66     switch (c) {
67         case ' ': fState = kNext_State; break;
68         case 'a': fIndex = (fIndex + 1) % (fDebugCanvas->getSize() - 1); break;
69         case 's': fIndex = fIndex <= 0 ? fDebugCanvas->getSize() - 1 : fIndex - 1; break;
70         default: break;
71     }
72 
73     return true;
74 }
75