blob: e252ead4a3b35384332a3a027f955a0265b52751 [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"
reed@android.com9781ca52009-04-14 14:28:22 +000013#include "SkSfntUtils.h"
reed@android.com3a859a02009-01-28 00:56:29 +000014#include "SkString.h"
15#include "SkTemplates.h"
16
reed@google.com095186a2011-10-18 12:21:50 +000017enum FontQuality {
18 kBW,
19 kAA,
20 kLCD
21};
22
23static const char* fontQualityName(const SkPaint& paint) {
24 if (!paint.isAntiAlias()) {
25 return "BW";
26 }
27 if (paint.isLCDRenderText()) {
28 return "LCD";
29 }
30 return "AA";
31}
32
reed@android.com3a859a02009-01-28 00:56:29 +000033/* Some considerations for performance:
34 short -vs- long strings (measuring overhead)
35 tiny -vs- large pointsize (measure blit -vs- overhead)
36 1 -vs- many point sizes (measure cache lookup)
37 normal -vs- subpixel -vs- lineartext (minor)
38 force purge after each draw to measure scaler
39 textencoding?
40 text -vs- postext - pathtext
41 */
42class TextBench : public SkBenchmark {
43 SkPaint fPaint;
reed@android.com3a859a02009-01-28 00:56:29 +000044 SkString fText;
45 SkString fName;
reed@google.com095186a2011-10-18 12:21:50 +000046 FontQuality fFQ;
tomhudson@google.comca529d32011-10-28 15:34:49 +000047 enum { N = SkBENCHLOOP(800) };
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,
50 SkColor color, FontQuality fq) : INHERITED(param) {
51 fFQ = fq;
reed@android.com3a859a02009-01-28 00:56:29 +000052 fText.set(text);
53
reed@google.com095186a2011-10-18 12:21:50 +000054 fPaint.setAntiAlias(kBW != fq);
55 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000056 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000057 fPaint.setColor(color);
reed@android.com3a859a02009-01-28 00:56:29 +000058 }
59
60protected:
61 virtual const char* onGetName() {
62 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com095186a2011-10-18 12:21:50 +000063 fName.appendf("_%s", fontQualityName(fPaint));
reed@google.comb9d84f32011-01-17 19:45:43 +000064 if (SK_ColorBLACK != fPaint.getColor()) {
65 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.com095186a2011-10-18 12:21:50 +000066 } else {
67 fName.appendf("_BK");
reed@google.comb9d84f32011-01-17 19:45:43 +000068 }
reed@android.com3a859a02009-01-28 00:56:29 +000069 return fName.c_str();
70 }
71
72 virtual void onDraw(SkCanvas* canvas) {
73 const SkIPoint dim = this->getSize();
74 SkRandom rand;
75
76 SkPaint paint(fPaint);
77 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +000078 // explicitly need these
79 paint.setColor(fPaint.getColor());
80 paint.setAntiAlias(kBW != fFQ);
81 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +000082
83 const SkScalar x0 = SkIntToScalar(-10);
84 const SkScalar y0 = SkIntToScalar(-10);
85
reed@google.comb9d84f32011-01-17 19:45:43 +000086 for (int i = 0; i < N; i++) {
87 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
88 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
reed@google.com095186a2011-10-18 12:21:50 +000089 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
reed@android.com3a859a02009-01-28 00:56:29 +000090 }
91 }
92
93private:
94 typedef SkBenchmark INHERITED;
95};
96
97///////////////////////////////////////////////////////////////////////////////
98
99#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000100
reed@google.com095186a2011-10-18 12:21:50 +0000101static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kBW); }
102static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kBW); }
103static SkBenchmark* Fact03(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kBW); }
reed@android.comeca48362010-12-20 18:34:17 +0000104
reed@google.com095186a2011-10-18 12:21:50 +0000105static SkBenchmark* Fact11(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA); }
106static SkBenchmark* Fact12(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kAA); }
107static SkBenchmark* Fact13(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kAA); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000108
reed@google.com095186a2011-10-18 12:21:50 +0000109static SkBenchmark* Fact21(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kLCD); }
110static SkBenchmark* Fact22(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kLCD); }
111static SkBenchmark* Fact23(void* p) { return new TextBench(p, STR, 16, 0x88FF0000, kLCD); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000112
reed@google.comb9d84f32011-01-17 19:45:43 +0000113static BenchRegistry gReg01(Fact01);
114static BenchRegistry gReg02(Fact02);
reed@google.com095186a2011-10-18 12:21:50 +0000115static BenchRegistry gReg03(Fact03);
116
117static BenchRegistry gReg11(Fact11);
118static BenchRegistry gReg12(Fact12);
119static BenchRegistry gReg13(Fact13);
120
121static BenchRegistry gReg21(Fact21);
122static BenchRegistry gReg22(Fact22);
123static BenchRegistry gReg23(Fact23);
reed@android.comeca48362010-12-20 18:34:17 +0000124