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