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