blob: 39484a2e044ca6a5a358add6a9e8a063b09b4482 [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 */
joshualitt02b05012015-02-11 06:56:30 -08008
tfarinaf168b862014-06-19 12:32:29 -07009#include "Benchmark.h"
joshualitt02b05012015-02-11 06:56:30 -080010#include "Resources.h"
reed@android.com3a859a02009-01-28 00:56:29 +000011#include "SkCanvas.h"
12#include "SkPaint.h"
13#include "SkRandom.h"
joshualitt02b05012015-02-11 06:56:30 -080014#include "SkStream.h"
reed@android.com3a859a02009-01-28 00:56:29 +000015#include "SkString.h"
16#include "SkTemplates.h"
joshualitt02b05012015-02-11 06:56:30 -080017#include "SkTypeface.h"
reed@android.com3a859a02009-01-28 00:56:29 +000018
reed@google.com095186a2011-10-18 12:21:50 +000019enum FontQuality {
20 kBW,
21 kAA,
joshualitt02b05012015-02-11 06:56:30 -080022 kLCD,
reed@google.com095186a2011-10-18 12:21:50 +000023};
24
25static const char* fontQualityName(const SkPaint& paint) {
26 if (!paint.isAntiAlias()) {
27 return "BW";
28 }
29 if (paint.isLCDRenderText()) {
30 return "LCD";
31 }
32 return "AA";
33}
34
reed@android.com3a859a02009-01-28 00:56:29 +000035/* Some considerations for performance:
36 short -vs- long strings (measuring overhead)
37 tiny -vs- large pointsize (measure blit -vs- overhead)
38 1 -vs- many point sizes (measure cache lookup)
39 normal -vs- subpixel -vs- lineartext (minor)
40 force purge after each draw to measure scaler
41 textencoding?
42 text -vs- postext - pathtext
43 */
tfarinaf168b862014-06-19 12:32:29 -070044class TextBench : public Benchmark {
reed@android.com3a859a02009-01-28 00:56:29 +000045 SkPaint fPaint;
reed@android.com3a859a02009-01-28 00:56:29 +000046 SkString fText;
47 SkString fName;
reed@google.com095186a2011-10-18 12:21:50 +000048 FontQuality fFQ;
reed@google.com055af882011-11-23 15:52:02 +000049 bool fDoPos;
joshualitt02b05012015-02-11 06:56:30 -080050 bool fDoColorEmoji;
51 SkAutoTUnref<SkTypeface> fColorEmojiTypeface;
reed@google.com055af882011-11-23 15:52:02 +000052 SkPoint* fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000053public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000054 TextBench(const char text[], int ps,
joshualitt02b05012015-02-11 06:56:30 -080055 SkColor color, FontQuality fq, bool doColorEmoji = false, bool doPos = false) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +000056 fPos = NULL;
reed@google.com095186a2011-10-18 12:21:50 +000057 fFQ = fq;
reed@google.com055af882011-11-23 15:52:02 +000058 fDoPos = doPos;
joshualitt02b05012015-02-11 06:56:30 -080059 fDoColorEmoji = doColorEmoji;
reed@android.com3a859a02009-01-28 00:56:29 +000060 fText.set(text);
61
reed@google.com095186a2011-10-18 12:21:50 +000062 fPaint.setAntiAlias(kBW != fq);
63 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000064 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000065 fPaint.setColor(color);
reed@google.com055af882011-11-23 15:52:02 +000066
joshualitt02b05012015-02-11 06:56:30 -080067 if (doColorEmoji) {
68 SkASSERT(kBW == fFQ);
69 SkString filename = GetResourcePath("/Funkster.ttf");
70 SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
71 if (stream->isValid()) {
72 fColorEmojiTypeface.reset(SkTypeface::CreateFromStream(stream.detach()));
73 } else {
74 SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n");
75 }
76 }
77
reed@google.com055af882011-11-23 15:52:02 +000078 if (doPos) {
79 size_t len = strlen(text);
80 SkScalar* adv = new SkScalar[len];
81 fPaint.getTextWidths(text, len, adv);
82 fPos = new SkPoint[len];
83 SkScalar x = 0;
84 for (size_t i = 0; i < len; ++i) {
85 fPos[i].set(x, SkIntToScalar(50));
86 x += adv[i];
87 }
88 delete[] adv;
89 }
90 }
91
92 virtual ~TextBench() {
93 delete[] fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000094 }
95
96protected:
97 virtual const char* onGetName() {
98 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000099 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000100 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +0000101 }
reed@google.com095186a2011-10-18 12:21:50 +0000102 fName.appendf("_%s", fontQualityName(fPaint));
reed@google.comb9d84f32011-01-17 19:45:43 +0000103 if (SK_ColorBLACK != fPaint.getColor()) {
104 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.com095186a2011-10-18 12:21:50 +0000105 } else {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000106 fName.append("_BK");
reed@google.comb9d84f32011-01-17 19:45:43 +0000107 }
joshualitt02b05012015-02-11 06:56:30 -0800108
109 if (fDoColorEmoji && fColorEmojiTypeface) {
110 fName.append("_ColorEmoji");
111 }
112
reed@android.com3a859a02009-01-28 00:56:29 +0000113 return fName.c_str();
114 }
115
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000116 virtual void onDraw(const int loops, SkCanvas* canvas) {
reed@android.com3a859a02009-01-28 00:56:29 +0000117 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000118 SkRandom rand;
reed@android.com3a859a02009-01-28 00:56:29 +0000119
120 SkPaint paint(fPaint);
121 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +0000122 // explicitly need these
123 paint.setColor(fPaint.getColor());
124 paint.setAntiAlias(kBW != fFQ);
125 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000126
joshualitt02b05012015-02-11 06:56:30 -0800127 if (fDoColorEmoji && fColorEmojiTypeface) {
128 paint.setTypeface(fColorEmojiTypeface);
129 }
130
reed@android.com3a859a02009-01-28 00:56:29 +0000131 const SkScalar x0 = SkIntToScalar(-10);
132 const SkScalar y0 = SkIntToScalar(-10);
133
reed@google.com055af882011-11-23 15:52:02 +0000134 if (fDoPos) {
135 // realistically, the matrix is often at least translated, so we
136 // do that since it exercises different code in drawPosText.
137 canvas->translate(SK_Scalar1, SK_Scalar1);
138 }
139
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000140 for (int i = 0; i < loops; i++) {
reed@google.com055af882011-11-23 15:52:02 +0000141 if (fDoPos) {
142 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
143 } else {
144 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
145 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
146 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
147 }
reed@android.com3a859a02009-01-28 00:56:29 +0000148 }
149 }
150
151private:
tfarinaf168b862014-06-19 12:32:29 -0700152 typedef Benchmark INHERITED;
reed@android.com3a859a02009-01-28 00:56:29 +0000153};
154
155///////////////////////////////////////////////////////////////////////////////
156
157#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000158
mtklein@google.com410e6e82013-09-13 19:52:27 +0000159DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); )
160DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); )
161DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); )
reed@android.comeca48362010-12-20 18:34:17 +0000162
mtklein@google.com410e6e82013-09-13 19:52:27 +0000163DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); )
164DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); )
165DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000166
mtklein@google.com410e6e82013-09-13 19:52:27 +0000167DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); )
168DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); )
169DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000170
joshualitt02b05012015-02-11 06:56:30 -0800171DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); )
172DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); )
173DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); )
174
175DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); )
176DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); )