blob: 0a6af34d8f6b8555fa5356c9cf3951c1727a0c2e [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,
kkinnunenb4a797f2015-05-21 06:15:28 -070055 SkColor color, FontQuality fq, bool doColorEmoji = false, bool doPos = false)
56 : fText(text)
57 , fFQ(fq)
58 , fDoPos(doPos)
59 , fDoColorEmoji(doColorEmoji)
60 , fPos(NULL) {
reed@google.com095186a2011-10-18 12:21:50 +000061 fPaint.setAntiAlias(kBW != fq);
62 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000063 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000064 fPaint.setColor(color);
kkinnunenb4a797f2015-05-21 06:15:28 -070065 }
reed@google.com055af882011-11-23 15:52:02 +000066
kkinnunenb4a797f2015-05-21 06:15:28 -070067 virtual ~TextBench() {
68 delete[] fPos;
69 }
70
71protected:
72 void onPreDraw() override {
73 if (fDoColorEmoji) {
joshualitt02b05012015-02-11 06:56:30 -080074 SkASSERT(kBW == fFQ);
bungeman3ffa1262015-04-30 17:12:58 -040075 fColorEmojiTypeface.reset(GetResourceAsTypeface("/fonts/Funkster.ttf"));
joshualitt02b05012015-02-11 06:56:30 -080076 }
77
kkinnunenb4a797f2015-05-21 06:15:28 -070078 if (fDoPos) {
79 size_t len = fText.size();
reed@google.com055af882011-11-23 15:52:02 +000080 SkScalar* adv = new SkScalar[len];
kkinnunenb4a797f2015-05-21 06:15:28 -070081 fPaint.getTextWidths(fText.c_str(), len, adv);
reed@google.com055af882011-11-23 15:52:02 +000082 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
reed@android.com3a859a02009-01-28 00:56:29 +000092
reed@android.com3a859a02009-01-28 00:56:29 +000093 virtual const char* onGetName() {
94 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000095 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000096 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +000097 }
reed@google.com095186a2011-10-18 12:21:50 +000098 fName.appendf("_%s", fontQualityName(fPaint));
reed@google.comb9d84f32011-01-17 19:45:43 +000099 if (SK_ColorBLACK != fPaint.getColor()) {
100 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.com095186a2011-10-18 12:21:50 +0000101 } else {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000102 fName.append("_BK");
reed@google.comb9d84f32011-01-17 19:45:43 +0000103 }
joshualitt02b05012015-02-11 06:56:30 -0800104
kkinnunenb4a797f2015-05-21 06:15:28 -0700105 if (fDoColorEmoji) {
joshualitt02b05012015-02-11 06:56:30 -0800106 fName.append("_ColorEmoji");
107 }
108
reed@android.com3a859a02009-01-28 00:56:29 +0000109 return fName.c_str();
110 }
111
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000112 virtual void onDraw(const int loops, SkCanvas* canvas) {
reed@android.com3a859a02009-01-28 00:56:29 +0000113 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000114 SkRandom rand;
reed@android.com3a859a02009-01-28 00:56:29 +0000115
116 SkPaint paint(fPaint);
117 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +0000118 // explicitly need these
119 paint.setColor(fPaint.getColor());
120 paint.setAntiAlias(kBW != fFQ);
121 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000122
joshualitt02b05012015-02-11 06:56:30 -0800123 if (fDoColorEmoji && fColorEmojiTypeface) {
124 paint.setTypeface(fColorEmojiTypeface);
125 }
126
reed@android.com3a859a02009-01-28 00:56:29 +0000127 const SkScalar x0 = SkIntToScalar(-10);
128 const SkScalar y0 = SkIntToScalar(-10);
129
reed@google.com055af882011-11-23 15:52:02 +0000130 if (fDoPos) {
131 // realistically, the matrix is often at least translated, so we
132 // do that since it exercises different code in drawPosText.
133 canvas->translate(SK_Scalar1, SK_Scalar1);
134 }
135
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000136 for (int i = 0; i < loops; i++) {
reed@google.com055af882011-11-23 15:52:02 +0000137 if (fDoPos) {
138 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
139 } else {
140 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
141 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
142 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
143 }
reed@android.com3a859a02009-01-28 00:56:29 +0000144 }
145 }
146
147private:
tfarinaf168b862014-06-19 12:32:29 -0700148 typedef Benchmark INHERITED;
reed@android.com3a859a02009-01-28 00:56:29 +0000149};
150
151///////////////////////////////////////////////////////////////////////////////
152
153#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000154
mtklein@google.com410e6e82013-09-13 19:52:27 +0000155DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); )
156DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); )
157DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); )
reed@android.comeca48362010-12-20 18:34:17 +0000158
mtklein@google.com410e6e82013-09-13 19:52:27 +0000159DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); )
160DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); )
161DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000162
mtklein@google.com410e6e82013-09-13 19:52:27 +0000163DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); )
164DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); )
165DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000166
joshualitt02b05012015-02-11 06:56:30 -0800167DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); )
168DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); )
169DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); )
170
171DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); )
172DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); )