| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 * | 6 * |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "VisualInteractiveModule.h" | 9 #include "VisualInteractiveModule.h" |
| 10 | 10 |
| 11 #include "ProcStats.h" | 11 #include "ProcStats.h" |
| 12 #include "SkApplication.h" | 12 #include "SkApplication.h" |
| 13 #include "SkCanvas.h" | 13 #include "SkCanvas.h" |
| 14 #include "SkCommandLineFlags.h" | 14 #include "SkCommandLineFlags.h" |
| 15 #include "SkForceLinking.h" | 15 #include "SkForceLinking.h" |
| 16 #include "SkGraphics.h" | 16 #include "SkGraphics.h" |
| 17 #include "SkGr.h" | 17 #include "SkGr.h" |
| 18 #include "SkImageDecoder.h" | 18 #include "SkImageDecoder.h" |
| 19 #include "SkOSFile.h" | 19 #include "SkOSFile.h" |
| 20 #include "SkStream.h" | 20 #include "SkStream.h" |
| 21 #include "Stats.h" | 21 #include "Stats.h" |
| 22 #include "gl/GrGLInterface.h" | 22 #include "gl/GrGLInterface.h" |
| 23 | 23 |
| 24 __SK_FORCE_IMAGE_DECODER_LINKING; | 24 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 25 | 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) | 26 VisualInteractiveModule::VisualInteractiveModule(VisualBench* owner) |
| 31 : fCurrentMeasurement(0) | 27 : fCurrentMeasurement(0) |
| 32 , fCurrentFrame(0) | |
| 33 , fLoops(1) | |
| 34 , fState(kPreWarmLoops_State) | |
| 35 , fBenchmark(nullptr) | 28 , fBenchmark(nullptr) |
| 29 , fAdvance(false) |
| 36 , fOwner(SkRef(owner)) { | 30 , fOwner(SkRef(owner)) { |
| 37 fBenchmarkStream.reset(new VisualBenchmarkStream); | 31 fBenchmarkStream.reset(new VisualBenchmarkStream); |
| 38 | 32 |
| 39 memset(fMeasurements, 0, sizeof(fMeasurements)); | 33 memset(fMeasurements, 0, sizeof(fMeasurements)); |
| 40 } | 34 } |
| 41 | 35 |
| 42 inline void VisualInteractiveModule::renderFrame(SkCanvas* canvas) { | 36 inline void VisualInteractiveModule::renderFrame(SkCanvas* canvas) { |
| 43 fBenchmark->draw(fLoops, canvas); | 37 fBenchmark->draw(fTSM.loops(), canvas); |
| 44 this->drawStats(canvas); | 38 this->drawStats(canvas); |
| 45 canvas->flush(); | 39 canvas->flush(); |
| 46 fOwner->present(); | 40 fOwner->present(); |
| 47 } | 41 } |
| 48 | 42 |
| 49 void VisualInteractiveModule::drawStats(SkCanvas* canvas) { | 43 void VisualInteractiveModule::drawStats(SkCanvas* canvas) { |
| 50 static const float kPixelPerMS = 2.0f; | 44 static const float kPixelPerMS = 2.0f; |
| 51 static const int kDisplayWidth = 130; | 45 static const int kDisplayWidth = 130; |
| 52 static const int kDisplayHeight = 100; | 46 static const int kDisplayHeight = 100; |
| 53 static const int kDisplayPadding = 10; | 47 static const int kDisplayPadding = 10; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 return false; | 89 return false; |
| 96 } | 90 } |
| 97 | 91 |
| 98 // clear both buffers | 92 // clear both buffers |
| 99 fOwner->clear(canvas, SK_ColorWHITE, 2); | 93 fOwner->clear(canvas, SK_ColorWHITE, 2); |
| 100 | 94 |
| 101 fBenchmark->delayedSetup(); | 95 fBenchmark->delayedSetup(); |
| 102 | 96 |
| 103 return true; | 97 return true; |
| 104 } | 98 } |
| 105 | 99 #include "GrGpu.h" |
| 100 #include "GrResourceCache.h" |
| 106 void VisualInteractiveModule::draw(SkCanvas* canvas) { | 101 void VisualInteractiveModule::draw(SkCanvas* canvas) { |
| 107 if (!this->advanceRecordIfNecessary(canvas)) { | 102 if (!this->advanceRecordIfNecessary(canvas)) { |
| 108 SkDebugf("Exiting VisualBench successfully\n"); | 103 SkDebugf("Exiting VisualBench successfully\n"); |
| 109 fOwner->closeWindow(); | 104 fOwner->closeWindow(); |
| 110 return; | 105 return; |
| 111 } | 106 } |
| 112 this->renderFrame(canvas); | 107 this->renderFrame(canvas); |
| 113 switch (fState) { | 108 TimingStateMachine::ParentEvents event = fTSM.nextFrame(canvas, fBenchmark); |
| 114 case kPreWarmLoopsPerCanvasPreDraw_State: { | 109 switch (event) { |
| 115 this->perCanvasPreDraw(canvas, kPreWarmLoops_State); | 110 case TimingStateMachine::kReset_ParentEvents: |
| 111 fOwner->reset(); |
| 116 break; | 112 break; |
| 117 } | 113 case TimingStateMachine::kTiming_ParentEvents: |
| 118 case kPreWarmLoops_State: { | |
| 119 this->preWarm(kTuneLoops_State); | |
| 120 break; | 114 break; |
| 121 } | 115 case TimingStateMachine::kTimingFinished_ParentEvents: |
| 122 case kTuneLoops_State: { | 116 // Record measurements |
| 123 this->tuneLoops(canvas); | 117 fMeasurements[fCurrentMeasurement++] = fTSM.lastMeasurement(); |
| 118 fCurrentMeasurement &= (kMeasurementCount-1); // fast mod |
| 119 SkASSERT(fCurrentMeasurement < kMeasurementCount); |
| 120 this->drawStats(canvas); |
| 121 if (fAdvance) { |
| 122 fAdvance = false; |
| 123 fTSM.nextBenchmark(canvas, fBenchmark); |
| 124 fBenchmark.reset(nullptr); |
| 125 fOwner->reset(); |
| 126 } else { |
| 127 fTSM.nextSample(); |
| 128 } |
| 124 break; | 129 break; |
| 125 } | |
| 126 case kPreTiming_State: { | |
| 127 fBenchmark->perCanvasPreDraw(canvas); | |
| 128 fBenchmark->preDraw(canvas); | |
| 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); | |
| 152 fBenchmark->preDraw(canvas); | |
| 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 w
ork | |
| 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) { | |
| 211 fBenchmark->postDraw(canvas); | |
| 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 } | 130 } |
| 225 } | 131 } |
| 226 | 132 |
| 227 bool VisualInteractiveModule::onHandleChar(SkUnichar c) { | 133 bool VisualInteractiveModule::onHandleChar(SkUnichar c) { |
| 228 if (' ' == c) { | 134 if (' ' == c) { |
| 229 this->nextState(kAdvance_State); | 135 fAdvance = true; |
| 230 } | 136 } |
| 231 | 137 |
| 232 return true; | 138 return true; |
| 233 } | 139 } |
| OLD | NEW |