blob: bbdbc8a6d27ec142bce1941ea457a7a6ca404783 [file] [log] [blame]
bsalomon7c5c9da2014-06-09 15:11:30 -07001/*
2 * Copyright 2014 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 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkTypeface.h"
12#include "SkRandom.h"
13
14/**
15 * Draws text with random parameters. The text draws each get their own clip rect. It is also
16 * used as a bench to measure how well the GPU backend batches text draws.
17 */
18
19class VariedTextGM : public skiagm::GM {
20public:
21 VariedTextGM(bool effectiveClip, bool lcd)
22 : fEffectiveClip(effectiveClip)
23 , fLCD(lcd) {
24 memset(fTypefacesToUnref, 0, sizeof(fTypefacesToUnref));
25 }
26
27 ~VariedTextGM() {
28 for (size_t i = 0; i < SK_ARRAY_COUNT(fTypefacesToUnref); ++i) {
29 SkSafeUnref(fTypefacesToUnref[i]);
30 }
31 }
32
33protected:
mtklein72c9faa2015-01-09 10:06:39 -080034 SkString onShortName() SK_OVERRIDE {
bsalomon7c5c9da2014-06-09 15:11:30 -070035 SkString name("varied_text");
36 if (fEffectiveClip) {
37 name.append("_clipped");
38 } else {
39 name.append("_ignorable_clip");
40 }
41 if (fLCD) {
42 name.append("_lcd");
43 } else {
44 name.append("_no_lcd");
45 }
46 return name;
47 }
48
mtklein72c9faa2015-01-09 10:06:39 -080049 SkISize onISize() SK_OVERRIDE {
bsalomon7c5c9da2014-06-09 15:11:30 -070050 return SkISize::Make(640, 480);
51 }
52
mtklein72c9faa2015-01-09 10:06:39 -080053 void onOnceBeforeDraw() SK_OVERRIDE {
bsalomon7c5c9da2014-06-09 15:11:30 -070054 fPaint.setAntiAlias(true);
55 fPaint.setLCDRenderText(fLCD);
56
57 SkISize size = this->getISize();
58 SkScalar w = SkIntToScalar(size.fWidth);
59 SkScalar h = SkIntToScalar(size.fHeight);
60
61 SK_COMPILE_ASSERT(4 == SK_ARRAY_COUNT(fTypefacesToUnref), typeface_cnt);
Cary Clark992c7b02014-07-31 08:58:44 -040062 fTypefacesToUnref[0] = sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kNormal);
63 fTypefacesToUnref[1] = sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kBold);
64 fTypefacesToUnref[2] = sk_tool_utils::create_portable_typeface("serif", SkTypeface::kNormal);
65 fTypefacesToUnref[3] = sk_tool_utils::create_portable_typeface("serif", SkTypeface::kBold);
bsalomon7c5c9da2014-06-09 15:11:30 -070066
67 SkRandom random;
68 for (int i = 0; i < kCnt; ++i) {
69 int length = random.nextRangeU(kMinLength, kMaxLength);
70 char text[kMaxLength];
71 for (int j = 0; j < length; ++j) {
72 text[j] = (char)random.nextRangeU('!', 'z');
73 }
74 fStrings[i].set(text, length);
75
76 fColors[i] = random.nextU();
77 fColors[i] |= 0xFF000000;
78
79 static const SkScalar kMinPtSize = 8.f;
80 static const SkScalar kMaxPtSize = 32.f;
81
82 fPtSizes[i] = random.nextRangeScalar(kMinPtSize, kMaxPtSize);
83
84 fTypefaces[i] = fTypefacesToUnref[
85 random.nextULessThan(SK_ARRAY_COUNT(fTypefacesToUnref))];
86
87 SkRect r;
88 fPaint.setColor(fColors[i]);
89 fPaint.setTypeface(fTypefaces[i]);
90 fPaint.setTextSize(fPtSizes[i]);
91
92 fPaint.measureText(fStrings[i].c_str(), fStrings[i].size(), &r);
93 // safeRect is set of x,y positions where we can draw the string without hitting
94 // the GM's border.
95 SkRect safeRect = SkRect::MakeLTRB(-r.fLeft, -r.fTop, w - r.fRight, h - r.fBottom);
96 if (safeRect.isEmpty()) {
97 // If we don't fit then just don't worry about how we get cliped to the device
98 // border.
99 safeRect = SkRect::MakeWH(w, h);
100 }
101 fPositions[i].fX = random.nextRangeScalar(safeRect.fLeft, safeRect.fRight);
102 fPositions[i].fY = random.nextRangeScalar(safeRect.fTop, safeRect.fBottom);
103
104 fClipRects[i] = r;
105 fClipRects[i].offset(fPositions[i].fX, fPositions[i].fY);
106 fClipRects[i].outset(2.f, 2.f);
107
108 if (fEffectiveClip) {
109 fClipRects[i].fRight -= 0.25f * fClipRects[i].width();
110 }
111 }
112 }
113
mtklein72c9faa2015-01-09 10:06:39 -0800114 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
bsalomon7c5c9da2014-06-09 15:11:30 -0700115 for (int i = 0; i < kCnt; ++i) {
116 fPaint.setColor(fColors[i]);
117 fPaint.setTextSize(fPtSizes[i]);
118 fPaint.setTypeface(fTypefaces[i]);
119
120 canvas->save();
121 canvas->clipRect(fClipRects[i]);
122 canvas->translate(fPositions[i].fX, fPositions[i].fY);
123 canvas->drawText(fStrings[i].c_str(), fStrings[i].size(), 0, 0, fPaint);
124 canvas->restore();
125 }
126
127 // Visualize the clips, but not in bench mode.
128 if (kBench_Mode != this->getMode()) {
129 SkPaint wirePaint;
130 wirePaint.setAntiAlias(true);
131 wirePaint.setStrokeWidth(0);
132 wirePaint.setStyle(SkPaint::kStroke_Style);
133 for (int i = 0; i < kCnt; ++i) {
134 canvas->drawRect(fClipRects[i], wirePaint);
135 }
136 }
137 }
138
mtkleincf5d9c92015-01-23 10:31:45 -0800139 bool runAsBench() const SK_OVERRIDE { return true; }
bsalomon7c5c9da2014-06-09 15:11:30 -0700140
141private:
142 static const int kCnt = 30;
143 static const int kMinLength = 15;
144 static const int kMaxLength = 40;
145
146 bool fEffectiveClip;
147 bool fLCD;
148 SkTypeface* fTypefacesToUnref[4];
149 SkPaint fPaint;
150
151 // precomputed for each text draw
152 SkString fStrings[kCnt];
153 SkColor fColors[kCnt];
154 SkScalar fPtSizes[kCnt];
155 SkTypeface* fTypefaces[kCnt];
156 SkPoint fPositions[kCnt];
157 SkRect fClipRects[kCnt];
158
159 typedef skiagm::GM INHERITED;
160};
161
162DEF_GM( return SkNEW(VariedTextGM(false, false)); )
163DEF_GM( return SkNEW(VariedTextGM(true, false)); )
164DEF_GM( return SkNEW(VariedTextGM(false, true)); )
165DEF_GM( return SkNEW(VariedTextGM(true, true)); )