jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 26 | static const int kGpuFrameLag = 5; |
| 27 | static const int kFrames = 5; |
| 28 | static const double kLoopMs = 5; |
| 29 | |
| 30 | VisualInteractiveModule::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 | |
| 42 | inline void VisualInteractiveModule::renderFrame(SkCanvas* canvas) { |
| 43 | fBenchmark->draw(fLoops, canvas); |
| 44 | this->drawStats(canvas); |
| 45 | canvas->flush(); |
| 46 | fOwner->present(); |
| 47 | } |
| 48 | |
| 49 | void 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 | |
| 88 | bool 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 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 101 | fBenchmark->delayedSetup(); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | void 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); |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 128 | fBenchmark->preDraw(canvas); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 129 | 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 | |
| 146 | inline void VisualInteractiveModule::nextState(State nextState) { |
| 147 | fState = nextState; |
| 148 | } |
| 149 | |
| 150 | void VisualInteractiveModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) { |
| 151 | fBenchmark->perCanvasPreDraw(canvas); |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 152 | fBenchmark->preDraw(canvas); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 153 | fCurrentFrame = 0; |
| 154 | this->nextState(nextState); |
| 155 | } |
| 156 | |
| 157 | void 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 | |
| 168 | inline double VisualInteractiveModule::elapsed() { |
| 169 | fTimer.end(); |
| 170 | return fTimer.fWall; |
| 171 | } |
| 172 | |
| 173 | void VisualInteractiveModule::resetTimingState() { |
| 174 | fCurrentFrame = 0; |
| 175 | fTimer = WallTimer(); |
| 176 | fOwner->reset(); |
| 177 | } |
| 178 | |
| 179 | void VisualInteractiveModule::scaleLoops(double elapsedMs) { |
| 180 | // Scale back the number of loops |
| 181 | fLoops = (int)ceil(fLoops * kLoopMs / elapsedMs); |
| 182 | } |
| 183 | |
| 184 | inline 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 | |
| 203 | void 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 | |
| 210 | void VisualInteractiveModule::postDraw(SkCanvas* canvas) { |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 211 | fBenchmark->postDraw(canvas); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 212 | fBenchmark->perCanvasPostDraw(canvas); |
| 213 | fBenchmark.reset(nullptr); |
| 214 | fLoops = 1; |
| 215 | } |
| 216 | |
| 217 | inline 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 | |
| 227 | bool VisualInteractiveModule::onHandleChar(SkUnichar c) { |
| 228 | if (' ' == c) { |
| 229 | this->nextState(kAdvance_State); |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |