blob: af922a92bf10352cc422da9abbea92e15904847e [file] [log] [blame]
jvanverthf5d1b2d2015-09-15 07:40:56 -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
9#include "VisualInteractiveModule.h"
10
11#include "ProcStats.h"
12#include "SkApplication.h"
13#include "SkCanvas.h"
14#include "SkCommandLineFlags.h"
15#include "SkForceLinking.h"
16#include "SkGraphics.h"
17#include "SkGr.h"
18#include "SkImageDecoder.h"
19#include "SkOSFile.h"
20#include "SkStream.h"
21#include "Stats.h"
22#include "gl/GrGLInterface.h"
23
24__SK_FORCE_IMAGE_DECODER_LINKING;
25
26static const int kGpuFrameLag = 5;
27static const int kFrames = 5;
28static const double kLoopMs = 5;
29
30VisualInteractiveModule::VisualInteractiveModule(VisualBench* owner)
31 : fCurrentMeasurement(0)
32 , fCurrentFrame(0)
33 , fLoops(1)
34 , fState(kPreWarmLoops_State)
35 , fBenchmark(nullptr)
36 , fOwner(SkRef(owner)) {
37 fBenchmarkStream.reset(new VisualBenchmarkStream);
38
39 memset(fMeasurements, 0, sizeof(fMeasurements));
40}
41
42inline void VisualInteractiveModule::renderFrame(SkCanvas* canvas) {
43 fBenchmark->draw(fLoops, canvas);
44 this->drawStats(canvas);
45 canvas->flush();
46 fOwner->present();
47}
48
49void VisualInteractiveModule::drawStats(SkCanvas* canvas) {
50 static const float kPixelPerMS = 2.0f;
51 static const int kDisplayWidth = 130;
52 static const int kDisplayHeight = 100;
53 static const int kDisplayPadding = 10;
54 static const int kGraphPadding = 3;
55 static const float kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
56
57 SkISize canvasSize = canvas->getDeviceSize();
58 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
59 SkIntToScalar(kDisplayPadding),
60 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
61 SkPaint paint;
62 canvas->clipRect(rect);
63 paint.setColor(SK_ColorBLACK);
64 canvas->drawRect(rect, paint);
65 // draw the 16ms line
66 paint.setColor(SK_ColorLTGRAY);
67 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
68 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
69 paint.setColor(SK_ColorRED);
70 paint.setStyle(SkPaint::kStroke_Style);
71 canvas->drawRect(rect, paint);
72
73 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
74 const int xStep = 2;
75 const int startY = SkScalarTruncToInt(rect.fBottom);
76 int i = fCurrentMeasurement;
77 do {
78 int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // round to nearest value
79 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
80 SkIntToScalar(x), SkIntToScalar(endY), paint);
81 i++;
82 i &= (kMeasurementCount - 1); // fast mod
83 x += xStep;
84 } while (i != fCurrentMeasurement);
85
86}
87
88bool VisualInteractiveModule::advanceRecordIfNecessary(SkCanvas* canvas) {
89 if (fBenchmark) {
90 return true;
91 }
92
93 fBenchmark.reset(fBenchmarkStream->next());
94 if (!fBenchmark) {
95 return false;
96 }
97
98 // clear both buffers
99 fOwner->clear(canvas, SK_ColorWHITE, 2);
100
joshualitt8a6697a2015-09-30 12:11:07 -0700101 fBenchmark->delayedSetup();
jvanverthf5d1b2d2015-09-15 07:40:56 -0700102
103 return true;
104}
105
106void VisualInteractiveModule::draw(SkCanvas* canvas) {
107 if (!this->advanceRecordIfNecessary(canvas)) {
108 SkDebugf("Exiting VisualBench successfully\n");
109 fOwner->closeWindow();
110 return;
111 }
112 this->renderFrame(canvas);
113 switch (fState) {
114 case kPreWarmLoopsPerCanvasPreDraw_State: {
115 this->perCanvasPreDraw(canvas, kPreWarmLoops_State);
116 break;
117 }
118 case kPreWarmLoops_State: {
119 this->preWarm(kTuneLoops_State);
120 break;
121 }
122 case kTuneLoops_State: {
123 this->tuneLoops(canvas);
124 break;
125 }
126 case kPreTiming_State: {
127 fBenchmark->perCanvasPreDraw(canvas);
joshualitt8a6697a2015-09-30 12:11:07 -0700128 fBenchmark->preDraw(canvas);
jvanverthf5d1b2d2015-09-15 07:40:56 -0700129 fCurrentFrame = 0;
130 fTimer.start();
131 fState = kTiming_State;
132 // fall to next state
133 }
134 case kTiming_State: {
135 this->timing(canvas);
136 break;
137 }
138 case kAdvance_State: {
139 this->postDraw(canvas);
140 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
141 break;
142 }
143 }
144}
145
146inline void VisualInteractiveModule::nextState(State nextState) {
147 fState = nextState;
148}
149
150void VisualInteractiveModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) {
151 fBenchmark->perCanvasPreDraw(canvas);
joshualitt8a6697a2015-09-30 12:11:07 -0700152 fBenchmark->preDraw(canvas);
jvanverthf5d1b2d2015-09-15 07:40:56 -0700153 fCurrentFrame = 0;
154 this->nextState(nextState);
155}
156
157void VisualInteractiveModule::preWarm(State nextState) {
158 if (fCurrentFrame >= kGpuFrameLag) {
159 // we currently time across all frames to make sure we capture all GPU work
160 this->nextState(nextState);
161 fCurrentFrame = 0;
162 fTimer.start();
163 } else {
164 fCurrentFrame++;
165 }
166}
167
168inline double VisualInteractiveModule::elapsed() {
169 fTimer.end();
170 return fTimer.fWall;
171}
172
173void VisualInteractiveModule::resetTimingState() {
174 fCurrentFrame = 0;
175 fTimer = WallTimer();
176 fOwner->reset();
177}
178
179void VisualInteractiveModule::scaleLoops(double elapsedMs) {
180 // Scale back the number of loops
181 fLoops = (int)ceil(fLoops * kLoopMs / elapsedMs);
182}
183
184inline void VisualInteractiveModule::tuneLoops(SkCanvas* canvas) {
185 if (1 << 30 == fLoops) {
186 // We're about to wrap. Something's wrong with the bench.
187 SkDebugf("InnerLoops wrapped\n");
188 fLoops = 0;
189 } else {
190 double elapsedMs = this->elapsed();
191 if (elapsedMs > kLoopMs) {
192 this->scaleLoops(elapsedMs);
193 fBenchmark->perCanvasPostDraw(canvas);
194 this->nextState(kPreTiming_State);
195 } else {
196 fLoops *= 2;
197 this->nextState(kPreWarmLoops_State);
198 }
199 this->resetTimingState();
200 }
201}
202
203void VisualInteractiveModule::recordMeasurement() {
204 double measurement = this->elapsed() / (kFrames * fLoops);
205 fMeasurements[fCurrentMeasurement++] = measurement;
206 fCurrentMeasurement &= (kMeasurementCount-1); // fast mod
207 SkASSERT(fCurrentMeasurement < kMeasurementCount);
208}
209
210void VisualInteractiveModule::postDraw(SkCanvas* canvas) {
joshualitt8a6697a2015-09-30 12:11:07 -0700211 fBenchmark->postDraw(canvas);
jvanverthf5d1b2d2015-09-15 07:40:56 -0700212 fBenchmark->perCanvasPostDraw(canvas);
213 fBenchmark.reset(nullptr);
214 fLoops = 1;
215}
216
217inline void VisualInteractiveModule::timing(SkCanvas* canvas) {
218 if (fCurrentFrame >= kFrames) {
219 this->recordMeasurement();
220 fTimer.start();
221 fCurrentFrame = 0;
222 } else {
223 fCurrentFrame++;
224 }
225}
226
227bool VisualInteractiveModule::onHandleChar(SkUnichar c) {
228 if (' ' == c) {
229 this->nextState(kAdvance_State);
230 }
231
232 return true;
233}