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 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 26 | VisualInteractiveModule::VisualInteractiveModule(VisualBench* owner) |
| 27 | : fCurrentMeasurement(0) |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 28 | , fBenchmark(nullptr) |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 29 | , fAdvance(false) |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame^] | 30 | , fHasBeenReset(false) |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 31 | , fOwner(SkRef(owner)) { |
| 32 | fBenchmarkStream.reset(new VisualBenchmarkStream); |
| 33 | |
| 34 | memset(fMeasurements, 0, sizeof(fMeasurements)); |
| 35 | } |
| 36 | |
| 37 | inline void VisualInteractiveModule::renderFrame(SkCanvas* canvas) { |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 38 | fBenchmark->draw(fTSM.loops(), canvas); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 39 | this->drawStats(canvas); |
| 40 | canvas->flush(); |
| 41 | fOwner->present(); |
| 42 | } |
| 43 | |
| 44 | void VisualInteractiveModule::drawStats(SkCanvas* canvas) { |
| 45 | static const float kPixelPerMS = 2.0f; |
| 46 | static const int kDisplayWidth = 130; |
| 47 | static const int kDisplayHeight = 100; |
| 48 | static const int kDisplayPadding = 10; |
| 49 | static const int kGraphPadding = 3; |
| 50 | static const float kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps |
| 51 | |
| 52 | SkISize canvasSize = canvas->getDeviceSize(); |
| 53 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding), |
| 54 | SkIntToScalar(kDisplayPadding), |
| 55 | SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight)); |
| 56 | SkPaint paint; |
| 57 | canvas->clipRect(rect); |
| 58 | paint.setColor(SK_ColorBLACK); |
| 59 | canvas->drawRect(rect, paint); |
| 60 | // draw the 16ms line |
| 61 | paint.setColor(SK_ColorLTGRAY); |
| 62 | canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS, |
| 63 | rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint); |
| 64 | paint.setColor(SK_ColorRED); |
| 65 | paint.setStyle(SkPaint::kStroke_Style); |
| 66 | canvas->drawRect(rect, paint); |
| 67 | |
| 68 | int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding; |
| 69 | const int xStep = 2; |
| 70 | const int startY = SkScalarTruncToInt(rect.fBottom); |
| 71 | int i = fCurrentMeasurement; |
| 72 | do { |
| 73 | int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // round to nearest value |
| 74 | canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY), |
| 75 | SkIntToScalar(x), SkIntToScalar(endY), paint); |
| 76 | i++; |
| 77 | i &= (kMeasurementCount - 1); // fast mod |
| 78 | x += xStep; |
| 79 | } while (i != fCurrentMeasurement); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | bool VisualInteractiveModule::advanceRecordIfNecessary(SkCanvas* canvas) { |
| 84 | if (fBenchmark) { |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | fBenchmark.reset(fBenchmarkStream->next()); |
| 89 | if (!fBenchmark) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // clear both buffers |
| 94 | fOwner->clear(canvas, SK_ColorWHITE, 2); |
| 95 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 96 | fBenchmark->delayedSetup(); |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame^] | 97 | fBenchmark->preTimingHooks(canvas); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 98 | return true; |
| 99 | } |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 100 | #include "GrGpu.h" |
| 101 | #include "GrResourceCache.h" |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 102 | void VisualInteractiveModule::draw(SkCanvas* canvas) { |
| 103 | if (!this->advanceRecordIfNecessary(canvas)) { |
| 104 | SkDebugf("Exiting VisualBench successfully\n"); |
| 105 | fOwner->closeWindow(); |
| 106 | return; |
| 107 | } |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame^] | 108 | |
| 109 | if (fHasBeenReset) { |
| 110 | fHasBeenReset = false; |
| 111 | fBenchmark->preTimingHooks(canvas); |
| 112 | } |
| 113 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 114 | this->renderFrame(canvas); |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame^] | 115 | TimingStateMachine::ParentEvents event = fTSM.nextFrame(false); |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 116 | switch (event) { |
| 117 | case TimingStateMachine::kReset_ParentEvents: |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame^] | 118 | fBenchmark->postTimingHooks(canvas); |
| 119 | fHasBeenReset = true; |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 120 | fOwner->reset(); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 121 | break; |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 122 | case TimingStateMachine::kTiming_ParentEvents: |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 123 | break; |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 124 | case TimingStateMachine::kTimingFinished_ParentEvents: |
| 125 | // Record measurements |
| 126 | fMeasurements[fCurrentMeasurement++] = fTSM.lastMeasurement(); |
| 127 | fCurrentMeasurement &= (kMeasurementCount-1); // fast mod |
| 128 | SkASSERT(fCurrentMeasurement < kMeasurementCount); |
| 129 | this->drawStats(canvas); |
| 130 | if (fAdvance) { |
| 131 | fAdvance = false; |
| 132 | fTSM.nextBenchmark(canvas, fBenchmark); |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame^] | 133 | fBenchmark->postTimingHooks(canvas); |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 134 | fBenchmark.reset(nullptr); |
| 135 | fOwner->reset(); |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame^] | 136 | fHasBeenReset = true; |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 137 | } |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 138 | break; |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | bool VisualInteractiveModule::onHandleChar(SkUnichar c) { |
| 143 | if (' ' == c) { |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 144 | fAdvance = true; |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |