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