blob: 7aac2c0ab0e89c2edf3b91c660a8d7ca0137258c [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;
reed@android.com3a859a02009-01-28 00:56:29 +000048public:
reed@google.com095186a2011-10-18 12:21:50 +000049 TextBench(void* param, const char text[], int ps,
reed@google.com055af882011-11-23 15:52:02 +000050 SkColor color, FontQuality fq, bool doPos = false) : INHERITED(param) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +000051 fPos = NULL;
reed@google.com095186a2011-10-18 12:21:50 +000052 fFQ = fq;
reed@google.com055af882011-11-23 15:52:02 +000053 fDoPos = doPos;
reed@android.com3a859a02009-01-28 00:56:29 +000054 fText.set(text);
55
reed@google.com095186a2011-10-18 12:21:50 +000056 fPaint.setAntiAlias(kBW != fq);
57 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000058 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000059 fPaint.setColor(color);
reed@google.com055af882011-11-23 15:52:02 +000060
61 if (doPos) {
62 size_t len = strlen(text);
63 SkScalar* adv = new SkScalar[len];
64 fPaint.getTextWidths(text, len, adv);
65 fPos = new SkPoint[len];
66 SkScalar x = 0;
67 for (size_t i = 0; i < len; ++i) {
68 fPos[i].set(x, SkIntToScalar(50));
69 x += adv[i];
70 }
71 delete[] adv;
72 }
73 }
74
75 virtual ~TextBench() {
76 delete[] fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000077 }
78
79protected:
80 virtual const char* onGetName() {
81 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000082 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000083 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +000084 }
reed@google.com095186a2011-10-18 12:21:50 +000085 fName.appendf("_%s", fontQualityName(fPaint));
reed@google.comb9d84f32011-01-17 19:45:43 +000086 if (SK_ColorBLACK != fPaint.getColor()) {
87 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.com095186a2011-10-18 12:21:50 +000088 } else {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000089 fName.append("_BK");
reed@google.comb9d84f32011-01-17 19:45:43 +000090 }
reed@android.com3a859a02009-01-28 00:56:29 +000091 return fName.c_str();
92 }
93
94 virtual void onDraw(SkCanvas* canvas) {
95 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000096 SkRandom rand;
reed@android.com3a859a02009-01-28 00:56:29 +000097
98 SkPaint paint(fPaint);
99 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +0000100 // explicitly need these
101 paint.setColor(fPaint.getColor());
102 paint.setAntiAlias(kBW != fFQ);
103 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000104
105 const SkScalar x0 = SkIntToScalar(-10);
106 const SkScalar y0 = SkIntToScalar(-10);
107
reed@google.com055af882011-11-23 15:52:02 +0000108 if (fDoPos) {
109 // realistically, the matrix is often at least translated, so we
110 // do that since it exercises different code in drawPosText.
111 canvas->translate(SK_Scalar1, SK_Scalar1);
112 }
113
mtklein@google.comc2897432013-09-10 19:23:38 +0000114 for (int i = 0; i < this->getLoops(); i++) {
reed@google.com055af882011-11-23 15:52:02 +0000115 if (fDoPos) {
116 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
117 } else {
118 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
119 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
120 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
121 }
reed@android.com3a859a02009-01-28 00:56:29 +0000122 }
123 }
124
125private:
126 typedef SkBenchmark INHERITED;
127};
128
129///////////////////////////////////////////////////////////////////////////////
130
131#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000132
reed@google.com095186a2011-10-18 12:21:50 +0000133static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kBW); }
134static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kBW); }
135static SkBenchmark* Fact03(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kBW); }
reed@android.comeca48362010-12-20 18:34:17 +0000136
reed@google.com095186a2011-10-18 12:21:50 +0000137static SkBenchmark* Fact11(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA); }
138static SkBenchmark* Fact12(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kAA); }
139static SkBenchmark* Fact13(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kAA); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000140
reed@google.com095186a2011-10-18 12:21:50 +0000141static SkBenchmark* Fact21(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kLCD); }
142static SkBenchmark* Fact22(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kLCD); }
143static SkBenchmark* Fact23(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kLCD); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000144
reed@google.com055af882011-11-23 15:52:02 +0000145static SkBenchmark* Fact111(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA, true); }
146
reed@google.comb9d84f32011-01-17 19:45:43 +0000147static BenchRegistry gReg01(Fact01);
148static BenchRegistry gReg02(Fact02);
reed@google.com095186a2011-10-18 12:21:50 +0000149static BenchRegistry gReg03(Fact03);
150
151static BenchRegistry gReg11(Fact11);
152static BenchRegistry gReg12(Fact12);
153static BenchRegistry gReg13(Fact13);
154
155static BenchRegistry gReg21(Fact21);
156static BenchRegistry gReg22(Fact22);
157static BenchRegistry gReg23(Fact23);
reed@android.comeca48362010-12-20 18:34:17 +0000158
reed@google.com055af882011-11-23 15:52:02 +0000159static BenchRegistry gReg111(Fact111);