blob: 83fe7201fd57148409515186e7f354f499ec1177 [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:
mtklein36352bf2015-03-25 18:17:31 -070034 SkString onShortName() 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
mtklein36352bf2015-03-25 18:17:31 -070049 SkISize onISize() override {
bsalomon7c5c9da2014-06-09 15:11:30 -070050 return SkISize::Make(640, 480);
51 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onOnceBeforeDraw() 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
bungeman99fe8222015-08-20 07:57:51 -070061 static_assert(4 == SK_ARRAY_COUNT(fTypefacesToUnref), "typeface_cnt");
caryclark1818acb2015-07-24 12:09:25 -070062 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;
caryclarkae3714f2015-07-21 09:15:53 -070078 fColors[i] = sk_tool_utils::color_to_565(fColors[i]);
bsalomon7c5c9da2014-06-09 15:11:30 -070079
80 static const SkScalar kMinPtSize = 8.f;
81 static const SkScalar kMaxPtSize = 32.f;
82
83 fPtSizes[i] = random.nextRangeScalar(kMinPtSize, kMaxPtSize);
84
85 fTypefaces[i] = fTypefacesToUnref[
86 random.nextULessThan(SK_ARRAY_COUNT(fTypefacesToUnref))];
87
88 SkRect r;
89 fPaint.setColor(fColors[i]);
90 fPaint.setTypeface(fTypefaces[i]);
91 fPaint.setTextSize(fPtSizes[i]);
92
93 fPaint.measureText(fStrings[i].c_str(), fStrings[i].size(), &r);
94 // safeRect is set of x,y positions where we can draw the string without hitting
95 // the GM's border.
96 SkRect safeRect = SkRect::MakeLTRB(-r.fLeft, -r.fTop, w - r.fRight, h - r.fBottom);
97 if (safeRect.isEmpty()) {
98 // If we don't fit then just don't worry about how we get cliped to the device
99 // border.
100 safeRect = SkRect::MakeWH(w, h);
101 }
102 fPositions[i].fX = random.nextRangeScalar(safeRect.fLeft, safeRect.fRight);
103 fPositions[i].fY = random.nextRangeScalar(safeRect.fTop, safeRect.fBottom);
104
105 fClipRects[i] = r;
106 fClipRects[i].offset(fPositions[i].fX, fPositions[i].fY);
107 fClipRects[i].outset(2.f, 2.f);
108
109 if (fEffectiveClip) {
110 fClipRects[i].fRight -= 0.25f * fClipRects[i].width();
111 }
112 }
113 }
114
mtklein36352bf2015-03-25 18:17:31 -0700115 void onDraw(SkCanvas* canvas) override {
bsalomon7c5c9da2014-06-09 15:11:30 -0700116 for (int i = 0; i < kCnt; ++i) {
117 fPaint.setColor(fColors[i]);
118 fPaint.setTextSize(fPtSizes[i]);
119 fPaint.setTypeface(fTypefaces[i]);
120
121 canvas->save();
122 canvas->clipRect(fClipRects[i]);
123 canvas->translate(fPositions[i].fX, fPositions[i].fY);
124 canvas->drawText(fStrings[i].c_str(), fStrings[i].size(), 0, 0, fPaint);
125 canvas->restore();
126 }
127
128 // Visualize the clips, but not in bench mode.
129 if (kBench_Mode != this->getMode()) {
130 SkPaint wirePaint;
131 wirePaint.setAntiAlias(true);
132 wirePaint.setStrokeWidth(0);
133 wirePaint.setStyle(SkPaint::kStroke_Style);
134 for (int i = 0; i < kCnt; ++i) {
135 canvas->drawRect(fClipRects[i], wirePaint);
136 }
137 }
138 }
139
mtklein36352bf2015-03-25 18:17:31 -0700140 bool runAsBench() const override { return true; }
bsalomon7c5c9da2014-06-09 15:11:30 -0700141
142private:
143 static const int kCnt = 30;
144 static const int kMinLength = 15;
145 static const int kMaxLength = 40;
146
147 bool fEffectiveClip;
148 bool fLCD;
149 SkTypeface* fTypefacesToUnref[4];
150 SkPaint fPaint;
151
152 // precomputed for each text draw
153 SkString fStrings[kCnt];
154 SkColor fColors[kCnt];
155 SkScalar fPtSizes[kCnt];
156 SkTypeface* fTypefaces[kCnt];
157 SkPoint fPositions[kCnt];
158 SkRect fClipRects[kCnt];
159
160 typedef skiagm::GM INHERITED;
161};
162
halcanary385fe4d2015-08-26 13:07:48 -0700163DEF_GM(return new VariedTextGM(false, false);)
164DEF_GM(return new VariedTextGM(true, false);)
165DEF_GM(return new VariedTextGM(false, true);)
166DEF_GM(return new VariedTextGM(true, true);)