blob: 881e78a0a76bb7db0f93ba364d22a3bb94ab2029 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
joshualitt02b05012015-02-11 06:56:30 -08007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
joshualitt02b05012015-02-11 06:56:30 -08009#include "Resources.h"
reed@android.com3a859a02009-01-28 00:56:29 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
joshualitt02b05012015-02-11 06:56:30 -080013#include "SkStream.h"
reed@android.com3a859a02009-01-28 00:56:29 +000014#include "SkString.h"
15#include "SkTemplates.h"
joshualitt02b05012015-02-11 06:56:30 -080016#include "SkTypeface.h"
reed@android.com3a859a02009-01-28 00:56:29 +000017
Mike Reed358fcad2018-11-23 15:27:51 -050018#ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
19
reed@google.com095186a2011-10-18 12:21:50 +000020enum FontQuality {
21 kBW,
22 kAA,
joshualitt02b05012015-02-11 06:56:30 -080023 kLCD,
reed@google.com095186a2011-10-18 12:21:50 +000024};
25
26static const char* fontQualityName(const SkPaint& paint) {
27 if (!paint.isAntiAlias()) {
28 return "BW";
29 }
30 if (paint.isLCDRenderText()) {
31 return "LCD";
32 }
33 return "AA";
34}
35
reed@android.com3a859a02009-01-28 00:56:29 +000036/* Some considerations for performance:
37 short -vs- long strings (measuring overhead)
38 tiny -vs- large pointsize (measure blit -vs- overhead)
39 1 -vs- many point sizes (measure cache lookup)
40 normal -vs- subpixel -vs- lineartext (minor)
41 force purge after each draw to measure scaler
42 textencoding?
43 text -vs- postext - pathtext
44 */
tfarinaf168b862014-06-19 12:32:29 -070045class TextBench : public Benchmark {
reed@android.com3a859a02009-01-28 00:56:29 +000046 SkPaint fPaint;
reed@android.com3a859a02009-01-28 00:56:29 +000047 SkString fText;
48 SkString fName;
reed@google.com095186a2011-10-18 12:21:50 +000049 FontQuality fFQ;
reed@google.com055af882011-11-23 15:52:02 +000050 bool fDoPos;
joshualitt02b05012015-02-11 06:56:30 -080051 bool fDoColorEmoji;
bungeman13b9c952016-05-12 10:09:30 -070052 sk_sp<SkTypeface> fColorEmojiTypeface;
reed@google.com055af882011-11-23 15:52:02 +000053 SkPoint* fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000054public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000055 TextBench(const char text[], int ps,
kkinnunenb4a797f2015-05-21 06:15:28 -070056 SkColor color, FontQuality fq, bool doColorEmoji = false, bool doPos = false)
57 : fText(text)
58 , fFQ(fq)
59 , fDoPos(doPos)
60 , fDoColorEmoji(doColorEmoji)
halcanary96fcdcc2015-08-27 07:41:13 -070061 , fPos(nullptr) {
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);
kkinnunenb4a797f2015-05-21 06:15:28 -070066 }
reed@google.com055af882011-11-23 15:52:02 +000067
Brian Salomond3b65972017-03-22 12:05:03 -040068 ~TextBench() override {
kkinnunenb4a797f2015-05-21 06:15:28 -070069 delete[] fPos;
70 }
71
72protected:
joshualitt8a6697a2015-09-30 12:11:07 -070073 void onDelayedSetup() override {
kkinnunenb4a797f2015-05-21 06:15:28 -070074 if (fDoColorEmoji) {
joshualitt02b05012015-02-11 06:56:30 -080075 SkASSERT(kBW == fFQ);
Hal Canary53e5e7d2017-12-08 14:25:14 -050076 fColorEmojiTypeface = MakeResourceAsTypeface("fonts/Funkster.ttf");
joshualitt02b05012015-02-11 06:56:30 -080077 }
78
kkinnunenb4a797f2015-05-21 06:15:28 -070079 if (fDoPos) {
80 size_t len = fText.size();
reed@google.com055af882011-11-23 15:52:02 +000081 SkScalar* adv = new SkScalar[len];
kkinnunenb4a797f2015-05-21 06:15:28 -070082 fPaint.getTextWidths(fText.c_str(), len, adv);
reed@google.com055af882011-11-23 15:52:02 +000083 fPos = new SkPoint[len];
84 SkScalar x = 0;
85 for (size_t i = 0; i < len; ++i) {
86 fPos[i].set(x, SkIntToScalar(50));
87 x += adv[i];
88 }
89 delete[] adv;
90 }
91 }
92
reed@android.com3a859a02009-01-28 00:56:29 +000093
mtkleinf0599002015-07-13 06:18:39 -070094 const char* onGetName() override {
reed@android.com3a859a02009-01-28 00:56:29 +000095 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000096 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000097 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +000098 }
reed@google.com095186a2011-10-18 12:21:50 +000099 fName.appendf("_%s", fontQualityName(fPaint));
mtklein9c84b112015-08-26 13:41:31 -0700100 if (SK_ColorBLACK == fPaint.getColor()) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000101 fName.append("_BK");
mtklein9c84b112015-08-26 13:41:31 -0700102 } else if (SK_ColorWHITE == fPaint.getColor()) {
103 fName.append("_WT");
104 } else {
105 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.comb9d84f32011-01-17 19:45:43 +0000106 }
joshualitt02b05012015-02-11 06:56:30 -0800107
kkinnunenb4a797f2015-05-21 06:15:28 -0700108 if (fDoColorEmoji) {
joshualitt02b05012015-02-11 06:56:30 -0800109 fName.append("_ColorEmoji");
110 }
111
reed@android.com3a859a02009-01-28 00:56:29 +0000112 return fName.c_str();
113 }
114
mtkleina1ebeb22015-10-01 09:43:39 -0700115 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.com3a859a02009-01-28 00:56:29 +0000116 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000117 SkRandom rand;
reed@android.com3a859a02009-01-28 00:56:29 +0000118
119 SkPaint paint(fPaint);
120 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +0000121 // explicitly need these
122 paint.setColor(fPaint.getColor());
123 paint.setAntiAlias(kBW != fFQ);
124 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000125
joshualitt02b05012015-02-11 06:56:30 -0800126 if (fDoColorEmoji && fColorEmojiTypeface) {
127 paint.setTypeface(fColorEmojiTypeface);
128 }
129
reed@android.com3a859a02009-01-28 00:56:29 +0000130 const SkScalar x0 = SkIntToScalar(-10);
131 const SkScalar y0 = SkIntToScalar(-10);
132
reed@google.com055af882011-11-23 15:52:02 +0000133 if (fDoPos) {
134 // realistically, the matrix is often at least translated, so we
135 // do that since it exercises different code in drawPosText.
136 canvas->translate(SK_Scalar1, SK_Scalar1);
137 }
138
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000139 for (int i = 0; i < loops; i++) {
reed@google.com055af882011-11-23 15:52:02 +0000140 if (fDoPos) {
141 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
142 } else {
143 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
144 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
Cary Clark2a475ea2017-04-28 15:35:12 -0400145 canvas->drawString(fText, x, y, paint);
reed@google.com055af882011-11-23 15:52:02 +0000146 }
reed@android.com3a859a02009-01-28 00:56:29 +0000147 }
148 }
149
150private:
tfarinaf168b862014-06-19 12:32:29 -0700151 typedef Benchmark INHERITED;
reed@android.com3a859a02009-01-28 00:56:29 +0000152};
153
154///////////////////////////////////////////////////////////////////////////////
155
156#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000157
mtklein9c84b112015-08-26 13:41:31 -0700158DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kBW); )
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
mtklein9c84b112015-08-26 13:41:31 -0700163DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kAA); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000164DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); )
165DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); )
166DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000167
mtklein9c84b112015-08-26 13:41:31 -0700168DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kLCD); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000169DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); )
170DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); )
171DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000172
mtklein9c84b112015-08-26 13:41:31 -0700173DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kBW, true); )
joshualitt02b05012015-02-11 06:56:30 -0800174DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); )
175DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); )
176DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); )
177
178DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); )
179DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); )
Mike Reed358fcad2018-11-23 15:27:51 -0500180
181#endif