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. |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "VisualInteractiveModule.h" |
| 9 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkCommandLineFlags.h" |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 12 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 13 | VisualInteractiveModule::VisualInteractiveModule(VisualBench* owner) |
cdalton | 266e202 | 2015-11-13 08:28:49 -0800 | [diff] [blame] | 14 | : INHERITED(owner) |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 15 | , fCurrentMeasurement(0) |
| 16 | , fAdvance(false) { |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 17 | memset(fMeasurements, 0, sizeof(fMeasurements)); |
| 18 | } |
| 19 | |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 20 | void VisualInteractiveModule::renderFrame(SkCanvas* canvas, Benchmark* benchmark, int loops) { |
| 21 | benchmark->draw(loops, canvas); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 22 | this->drawStats(canvas); |
| 23 | canvas->flush(); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void VisualInteractiveModule::drawStats(SkCanvas* canvas) { |
| 27 | static const float kPixelPerMS = 2.0f; |
| 28 | static const int kDisplayWidth = 130; |
| 29 | static const int kDisplayHeight = 100; |
| 30 | static const int kDisplayPadding = 10; |
| 31 | static const int kGraphPadding = 3; |
| 32 | static const float kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps |
| 33 | |
| 34 | SkISize canvasSize = canvas->getDeviceSize(); |
| 35 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding), |
| 36 | SkIntToScalar(kDisplayPadding), |
| 37 | SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight)); |
| 38 | SkPaint paint; |
| 39 | canvas->clipRect(rect); |
| 40 | paint.setColor(SK_ColorBLACK); |
| 41 | canvas->drawRect(rect, paint); |
| 42 | // draw the 16ms line |
| 43 | paint.setColor(SK_ColorLTGRAY); |
| 44 | canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS, |
| 45 | rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint); |
| 46 | paint.setColor(SK_ColorRED); |
| 47 | paint.setStyle(SkPaint::kStroke_Style); |
| 48 | canvas->drawRect(rect, paint); |
| 49 | |
| 50 | int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding; |
| 51 | const int xStep = 2; |
| 52 | const int startY = SkScalarTruncToInt(rect.fBottom); |
| 53 | int i = fCurrentMeasurement; |
| 54 | do { |
| 55 | int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // round to nearest value |
| 56 | canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY), |
| 57 | SkIntToScalar(x), SkIntToScalar(endY), paint); |
| 58 | i++; |
| 59 | i &= (kMeasurementCount - 1); // fast mod |
| 60 | x += xStep; |
| 61 | } while (i != fCurrentMeasurement); |
| 62 | |
| 63 | } |
| 64 | |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 65 | bool VisualInteractiveModule::timingFinished(Benchmark* benchmark, int loops, double measurement) { |
| 66 | // Record measurements |
| 67 | fMeasurements[fCurrentMeasurement++] = measurement; |
| 68 | fCurrentMeasurement &= (kMeasurementCount-1); // fast mod |
| 69 | SkASSERT(fCurrentMeasurement < kMeasurementCount); |
| 70 | if (fAdvance) { |
| 71 | fAdvance = false; |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 72 | return true; |
| 73 | } |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 74 | return false; |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool VisualInteractiveModule::onHandleChar(SkUnichar c) { |
| 78 | if (' ' == c) { |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 79 | fAdvance = true; |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return true; |
| 83 | } |