blob: 9334c334765eeb53bd5a299ce6f3036690d4f47b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com3a859a02009-01-28 00:56:29 +00008#include "SkBenchmark.h"
9#include "SkCanvas.h"
reed@android.comd6638e62009-04-08 05:03:52 +000010#include "SkFontHost.h"
reed@android.com3a859a02009-01-28 00:56:29 +000011#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkString.h"
14#include "SkTemplates.h"
15
reed@google.com095186a2011-10-18 12:21:50 +000016enum FontQuality {
17 kBW,
18 kAA,
19 kLCD
20};
21
22static const char* fontQualityName(const SkPaint& paint) {
23 if (!paint.isAntiAlias()) {
24 return "BW";
25 }
26 if (paint.isLCDRenderText()) {
27 return "LCD";
28 }
29 return "AA";
30}
31
reed@android.com3a859a02009-01-28 00:56:29 +000032/* Some considerations for performance:
33 short -vs- long strings (measuring overhead)
34 tiny -vs- large pointsize (measure blit -vs- overhead)
35 1 -vs- many point sizes (measure cache lookup)
36 normal -vs- subpixel -vs- lineartext (minor)
37 force purge after each draw to measure scaler
38 textencoding?
39 text -vs- postext - pathtext
40 */
41class TextBench : public SkBenchmark {
42 SkPaint fPaint;
reed@android.com3a859a02009-01-28 00:56:29 +000043 SkString fText;
44 SkString fName;
reed@google.com095186a2011-10-18 12:21:50 +000045 FontQuality fFQ;
reed@google.com055af882011-11-23 15:52:02 +000046 bool fDoPos;
47 SkPoint* fPos;
tomhudson@google.comca529d32011-10-28 15:34:49 +000048 enum { N = SkBENCHLOOP(800) };
reed@android.com3a859a02009-01-28 00:56:29 +000049public:
reed@google.com095186a2011-10-18 12:21:50 +000050 TextBench(void* param, const char text[], int ps,
reed@google.com055af882011-11-23 15:52:02 +000051 SkColor color, FontQuality fq, bool doPos = false) : INHERITED(param) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +000052 fPos = NULL;
reed@google.com095186a2011-10-18 12:21:50 +000053 fFQ = fq;
reed@google.com055af882011-11-23 15:52:02 +000054 fDoPos = doPos;
reed@android.com3a859a02009-01-28 00:56:29 +000055 fText.set(text);
56
reed@google.com095186a2011-10-18 12:21:50 +000057 fPaint.setAntiAlias(kBW != fq);
58 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000059 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000060 fPaint.setColor(color);
reed@google.com055af882011-11-23 15:52:02 +000061
62 if (doPos) {
63 size_t len = strlen(text);
64 SkScalar* adv = new SkScalar[len];
65 fPaint.getTextWidths(text, len, adv);
66 fPos = new SkPoint[len];
67 SkScalar x = 0;
68 for (size_t i = 0; i < len; ++i) {
69 fPos[i].set(x, SkIntToScalar(50));
70 x += adv[i];
71 }
72 delete[] adv;
73 }
74 }
75
76 virtual ~TextBench() {
77 delete[] fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000078 }
79
80protected:
81 virtual const char* onGetName() {
82 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000083 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000084 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +000085 }
reed@google.com095186a2011-10-18 12:21:50 +000086 fName.appendf("_%s", fontQualityName(fPaint));
reed@google.comb9d84f32011-01-17 19:45:43 +000087 if (SK_ColorBLACK != fPaint.getColor()) {
88 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.com095186a2011-10-18 12:21:50 +000089 } else {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000090 fName.append("_BK");
reed@google.comb9d84f32011-01-17 19:45:43 +000091 }
reed@android.com3a859a02009-01-28 00:56:29 +000092 return fName.c_str();
93 }
94
95 virtual void onDraw(SkCanvas* canvas) {
96 const SkIPoint dim = this->getSize();
97 SkRandom rand;
98
99 SkPaint paint(fPaint);
100 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +0000101 // explicitly need these
102 paint.setColor(fPaint.getColor());
103 paint.setAntiAlias(kBW != fFQ);
104 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000105
106 const SkScalar x0 = SkIntToScalar(-10);
107 const SkScalar y0 = SkIntToScalar(-10);
108
reed@google.com055af882011-11-23 15:52:02 +0000109 if (fDoPos) {
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);
113 }
114
reed@google.comb9d84f32011-01-17 19:45:43 +0000115 for (int i = 0; i < N; i++) {
reed@google.com055af882011-11-23 15:52:02 +0000116 if (fDoPos) {
117 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
118 } else {
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);
122 }
reed@android.com3a859a02009-01-28 00:56:29 +0000123 }
124 }
125
126private:
127 typedef SkBenchmark INHERITED;
128};
129
130///////////////////////////////////////////////////////////////////////////////
131
132#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000133
reed@google.com095186a2011-10-18 12:21:50 +0000134static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kBW); }
135static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kBW); }
136static SkBenchmark* Fact03(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kBW); }
reed@android.comeca48362010-12-20 18:34:17 +0000137
reed@google.com095186a2011-10-18 12:21:50 +0000138static SkBenchmark* Fact11(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA); }
139static SkBenchmark* Fact12(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kAA); }
140static SkBenchmark* Fact13(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kAA); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000141
reed@google.com095186a2011-10-18 12:21:50 +0000142static SkBenchmark* Fact21(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kLCD); }
143static SkBenchmark* Fact22(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kLCD); }
144static SkBenchmark* Fact23(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kLCD); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000145
reed@google.com055af882011-11-23 15:52:02 +0000146static SkBenchmark* Fact111(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA, true); }
147
reed@google.comb9d84f32011-01-17 19:45:43 +0000148static BenchRegistry gReg01(Fact01);
149static BenchRegistry gReg02(Fact02);
reed@google.com095186a2011-10-18 12:21:50 +0000150static BenchRegistry gReg03(Fact03);
151
152static BenchRegistry gReg11(Fact11);
153static BenchRegistry gReg12(Fact12);
154static BenchRegistry gReg13(Fact13);
155
156static BenchRegistry gReg21(Fact21);
157static BenchRegistry gReg22(Fact22);
158static BenchRegistry gReg23(Fact23);
reed@android.comeca48362010-12-20 18:34:17 +0000159
reed@google.com055af882011-11-23 15:52:02 +0000160static BenchRegistry gReg111(Fact111);