• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkSfntUtils.h"
15 #include "SkString.h"
16 #include "SkTemplates.h"
17 
18 #define STR     "Hamburgefons"
19 
20 enum FontQuality {
21     kBW,
22     kAA,
23     kLCD
24 };
25 
fontQualityName(const SkPaint & paint)26 static const char* fontQualityName(const SkPaint& paint) {
27     if (!paint.isAntiAlias()) {
28         return "BW";
29     }
30     if (paint.isLCDRenderText()) {
31         return "LCD";
32     }
33     return "AA";
34 }
35 
36 class ShaderMaskBench : public SkBenchmark {
37     SkPaint     fPaint;
38     SkString    fText;
39     SkString    fName;
40     FontQuality fFQ;
41     enum { N = SkBENCHLOOP(500) };
42 public:
ShaderMaskBench(void * param,bool isOpaque,FontQuality fq)43     ShaderMaskBench(void* param, bool isOpaque, FontQuality fq) : INHERITED(param) {
44         fFQ = fq;
45         fText.set(STR);
46 
47         fPaint.setAntiAlias(kBW != fq);
48         fPaint.setLCDRenderText(kLCD == fq);
49         fPaint.setAlpha(isOpaque ? 0xFF : 0x80);
50         fPaint.setShader(new SkColorShader)->unref();
51     }
52 
53 protected:
onGetName()54     virtual const char* onGetName() {
55         fName.printf("shadermask", SkScalarToFloat(fPaint.getTextSize()));
56         fName.appendf("_%s", fontQualityName(fPaint));
57         fName.appendf("_%02X", fPaint.getAlpha());
58         return fName.c_str();
59     }
60 
onDraw(SkCanvas * canvas)61     virtual void onDraw(SkCanvas* canvas) {
62         const SkIPoint dim = this->getSize();
63         SkRandom rand;
64 
65         SkPaint paint(fPaint);
66         this->setupPaint(&paint);
67         // explicitly need these
68         paint.setAlpha(fPaint.getAlpha());
69         paint.setAntiAlias(kBW != fFQ);
70         paint.setLCDRenderText(kLCD == fFQ);
71 
72         const SkScalar x0 = SkIntToScalar(-10);
73         const SkScalar y0 = SkIntToScalar(-10);
74 
75         paint.setTextSize(SkIntToScalar(12));
76         for (int i = 0; i < N; i++) {
77             SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
78             SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
79             canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
80         }
81 
82         paint.setTextSize(SkIntToScalar(48));
83         for (int i = 0; i < N/4; i++) {
84             SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
85             SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
86             canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
87         }
88     }
89 
90 private:
91     typedef SkBenchmark INHERITED;
92 };
93 
94 ///////////////////////////////////////////////////////////////////////////////
95 
Fact00(void * p)96 static SkBenchmark* Fact00(void* p) { return new ShaderMaskBench(p, true,  kBW); }
Fact01(void * p)97 static SkBenchmark* Fact01(void* p) { return new ShaderMaskBench(p, false, kBW); }
Fact10(void * p)98 static SkBenchmark* Fact10(void* p) { return new ShaderMaskBench(p, true,  kAA); }
Fact11(void * p)99 static SkBenchmark* Fact11(void* p) { return new ShaderMaskBench(p, false, kAA); }
Fact20(void * p)100 static SkBenchmark* Fact20(void* p) { return new ShaderMaskBench(p, true,  kLCD); }
Fact21(void * p)101 static SkBenchmark* Fact21(void* p) { return new ShaderMaskBench(p, false, kLCD); }
102 
103 static BenchRegistry gReg00(Fact00);
104 static BenchRegistry gReg01(Fact01);
105 static BenchRegistry gReg10(Fact10);
106 static BenchRegistry gReg11(Fact11);
107 static BenchRegistry gReg20(Fact20);
108 static BenchRegistry gReg21(Fact21);
109 
110