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