Home | History | Annotate | Download | only in bench
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "SkBenchmark.h"
      9 #include "SkCanvas.h"
     10 #include "SkColorShader.h"
     11 #include "SkFontHost.h"
     12 #include "SkPaint.h"
     13 #include "SkRandom.h"
     14 #include "SkString.h"
     15 #include "SkTemplates.h"
     16 
     17 #define STR     "Hamburgefons"
     18 
     19 enum FontQuality {
     20     kBW,
     21     kAA,
     22     kLCD
     23 };
     24 
     25 static const char* fontQualityName(const SkPaint& paint) {
     26     if (!paint.isAntiAlias()) {
     27         return "BW";
     28     }
     29     if (paint.isLCDRenderText()) {
     30         return "LCD";
     31     }
     32     return "AA";
     33 }
     34 
     35 class ShaderMaskBench : public SkBenchmark {
     36     SkPaint     fPaint;
     37     SkString    fText;
     38     SkString    fName;
     39     FontQuality fFQ;
     40 public:
     41     ShaderMaskBench(bool isOpaque, FontQuality fq)  {
     42         fFQ = fq;
     43         fText.set(STR);
     44 
     45         fPaint.setAntiAlias(kBW != fq);
     46         fPaint.setLCDRenderText(kLCD == fq);
     47         fPaint.setAlpha(isOpaque ? 0xFF : 0x80);
     48         fPaint.setShader(new SkColorShader)->unref();
     49     }
     50 
     51 protected:
     52     virtual const char* onGetName() {
     53         fName.printf("shadermask");
     54         fName.appendf("_%s", fontQualityName(fPaint));
     55         fName.appendf("_%02X", fPaint.getAlpha());
     56         return fName.c_str();
     57     }
     58 
     59     virtual void onDraw(const int loops, SkCanvas* canvas) {
     60         const SkIPoint dim = this->getSize();
     61         SkRandom rand;
     62 
     63         SkPaint paint(fPaint);
     64         this->setupPaint(&paint);
     65         // explicitly need these
     66         paint.setAlpha(fPaint.getAlpha());
     67         paint.setAntiAlias(kBW != fFQ);
     68         paint.setLCDRenderText(kLCD == fFQ);
     69 
     70         const SkScalar x0 = SkIntToScalar(-10);
     71         const SkScalar y0 = SkIntToScalar(-10);
     72 
     73         paint.setTextSize(SkIntToScalar(12));
     74         for (int i = 0; i < loops; i++) {
     75             SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
     76             SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
     77             canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
     78         }
     79 
     80         paint.setTextSize(SkIntToScalar(48));
     81         for (int i = 0; i < loops / 4 ; i++) {
     82             SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
     83             SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
     84             canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
     85         }
     86     }
     87 
     88 private:
     89     typedef SkBenchmark INHERITED;
     90 };
     91 
     92 ///////////////////////////////////////////////////////////////////////////////
     93 
     94 DEF_BENCH( return new ShaderMaskBench(true,  kBW); )
     95 DEF_BENCH( return new ShaderMaskBench(false, kBW); )
     96 DEF_BENCH( return new ShaderMaskBench(true,  kAA); )
     97 DEF_BENCH( return new ShaderMaskBench(false, kAA); )
     98 DEF_BENCH( return new ShaderMaskBench(true,  kLCD); )
     99 DEF_BENCH( return new ShaderMaskBench(false, kLCD); )
    100