3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SkBenchmark.h"
10 #include "SkFontHost.h"
13 #include "SkSfntUtils.h"
15 #include "SkTemplates.h"
23 static const char* fontQualityName(const SkPaint& paint) {
24 if (!paint.isAntiAlias()) {
27 if (paint.isLCDRenderText()) {
33 /* Some considerations for performance:
34 short -vs- long strings (measuring overhead)
35 tiny -vs- large pointsize (measure blit -vs- overhead)
36 1 -vs- many point sizes (measure cache lookup)
37 normal -vs- subpixel -vs- lineartext (minor)
38 force purge after each draw to measure scaler
40 text -vs- postext - pathtext
42 class TextBench : public SkBenchmark {
49 enum { N = SkBENCHLOOP(800) };
51 TextBench(void* param, const char text[], int ps,
52 SkColor color, FontQuality fq, bool doPos = false) : INHERITED(param) {
57 fPaint.setAntiAlias(kBW != fq);
58 fPaint.setLCDRenderText(kLCD == fq);
59 fPaint.setTextSize(SkIntToScalar(ps));
60 fPaint.setColor(color);
63 size_t len = strlen(text);
64 SkScalar* adv = new SkScalar[len];
65 fPaint.getTextWidths(text, len, adv);
66 fPos = new SkPoint[len];
68 for (size_t i = 0; i < len; ++i) {
69 fPos[i].set(x, SkIntToScalar(50));
76 virtual ~TextBench() {
81 virtual const char* onGetName() {
82 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
86 fName.appendf("_%s", fontQualityName(fPaint));
87 if (SK_ColorBLACK != fPaint.getColor()) {
88 fName.appendf("_%02X", fPaint.getAlpha());
95 virtual void onDraw(SkCanvas* canvas) {
96 const SkIPoint dim = this->getSize();
99 SkPaint paint(fPaint);
100 this->setupPaint(&paint);
101 // explicitly need these
102 paint.setColor(fPaint.getColor());
103 paint.setAntiAlias(kBW != fFQ);
104 paint.setLCDRenderText(kLCD == fFQ);
106 const SkScalar x0 = SkIntToScalar(-10);
107 const SkScalar y0 = SkIntToScalar(-10);
110 // realistically, the matrix is often at least translated, so we
111 // do that since it exercises different code in drawPosText.
112 canvas->translate(SK_Scalar1, SK_Scalar1);
115 for (int i = 0; i < N; i++) {
117 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
119 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
120 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
121 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
127 typedef SkBenchmark INHERITED;
130 ///////////////////////////////////////////////////////////////////////////////
132 #define STR "Hamburgefons"
134 static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kBW); }
135 static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kBW); }
136 static SkBenchmark* Fact03(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kBW); }
138 static SkBenchmark* Fact11(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA); }
139 static SkBenchmark* Fact12(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kAA); }
140 static SkBenchmark* Fact13(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kAA); }
142 static SkBenchmark* Fact21(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kLCD); }
143 static SkBenchmark* Fact22(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kLCD); }
144 static SkBenchmark* Fact23(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kLCD); }
146 static SkBenchmark* Fact111(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA, true); }
148 static BenchRegistry gReg01(Fact01);
149 static BenchRegistry gReg02(Fact02);
150 static BenchRegistry gReg03(Fact03);
152 static BenchRegistry gReg11(Fact11);
153 static BenchRegistry gReg12(Fact12);
154 static BenchRegistry gReg13(Fact13);
156 static BenchRegistry gReg21(Fact21);
157 static BenchRegistry gReg22(Fact22);
158 static BenchRegistry gReg23(Fact23);
160 static BenchRegistry gReg111(Fact111);