OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
13 /** | |
14 * Draws text with random parameters. The text draws each get their own clip rec t. It is also | |
15 * used as a bench to measure how well the GPU backend batches text draws. | |
16 */ | |
17 | |
18 class VariedTextGM : public skiagm::GM { | |
19 public: | |
20 VariedTextGM(bool effectiveClip, bool lcd) | |
21 : fEffectiveClip(effectiveClip) | |
22 , fLCD(lcd) { | |
robertphillips
2014/06/09 14:42:42
memset?
bsalomon
2014/06/09 16:55:25
Done.
| |
23 for (int i = 0; i < SK_ARRAY_COUNT(fTypefacesToUnref); ++i) { | |
24 fTypefacesToUnref[i] = NULL; | |
25 } | |
26 } | |
27 | |
28 ~VariedTextGM() { | |
29 for (int i = 0; i < SK_ARRAY_COUNT(fTypefacesToUnref); ++i) { | |
30 SkSafeUnref(fTypefacesToUnref[i]); | |
31 } | |
32 } | |
33 | |
34 protected: | |
35 virtual SkString onShortName() SK_OVERRIDE { | |
36 SkString name("varied_text"); | |
37 if (fEffectiveClip) { | |
38 name.append("_clipped"); | |
39 } else { | |
40 name.append("_ignorable_clip"); | |
41 } | |
42 if (fLCD) { | |
43 name.append("_lcd"); | |
44 } else { | |
45 name.append("_no_lcd"); | |
46 } | |
47 return name; | |
48 } | |
49 | |
50 virtual SkISize onISize() SK_OVERRIDE { | |
51 return SkISize::Make(640, 480); | |
52 } | |
53 | |
54 virtual void onOnceBeforeDraw() SK_OVERRIDE { | |
55 fPaint.setAntiAlias(true); | |
56 fPaint.setLCDRenderText(fLCD); | |
57 | |
58 SkISize size = this->getISize(); | |
59 SkScalar w = SkIntToScalar(size.fWidth); | |
60 SkScalar h = SkIntToScalar(size.fHeight); | |
61 | |
62 SK_COMPILE_ASSERT(4 == SK_ARRAY_COUNT(fTypefacesToUnref), typeface_cnt); | |
63 fTypefacesToUnref[0] = SkTypeface::CreateFromName("sans-serif", SkTypefa ce::kNormal); | |
64 fTypefacesToUnref[1] = SkTypeface::CreateFromName("sans-serif", SkTypefa ce::kBold); | |
65 fTypefacesToUnref[2] = SkTypeface::CreateFromName("serif", SkTypeface::k Normal); | |
66 fTypefacesToUnref[3] = SkTypeface::CreateFromName("serif", SkTypeface::k Bold); | |
67 | |
68 SkRandom random; | |
69 for (int i = 0; i < kCnt; ++i) { | |
70 int length = random.nextRangeU(kMinLength, kMaxLength); | |
71 char text[kMaxLength]; | |
72 for (int j = 0; j < length; ++j) { | |
robertphillips
2014/06/09 14:42:42
'!', 'z' ? Don't really know which is better.
bsalomon
2014/06/09 16:55:25
Done.
| |
73 text[j] = (char)random.nextRangeU(33, 122); | |
74 } | |
75 fStrings[i].set(text, length); | |
76 | |
77 fColors[i] = random.nextU(); | |
78 fColors[i] |= 0xFF000000; | |
79 | |
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 wit hout 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.f Right); | |
103 fPositions[i].fY = random.nextRangeScalar(safeRect.fTop, safeRect.fB ottom); | |
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 | |
115 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
116 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 | |
140 virtual uint32_t onGetFlags() const SK_OVERRIDE{ | |
jvanverth1
2014/06/09 14:43:28
Nit: space between SK_OVERRIDE and {
bsalomon
2014/06/09 16:55:25
Done.
| |
141 // The aa hairline stroked rects used to visualize the clip draw slightl y differently in | |
142 // quilt mode in dm. | |
143 return kAsBench_Flag | kSkipTiled_Flag; | |
144 } | |
145 | |
146 private: | |
147 static const int kCnt = 30; | |
148 static const int kMinLength = 15; | |
149 static const int kMaxLength = 40; | |
150 | |
robertphillips
2014/06/09 14:42:42
Line these guys up ?
bsalomon
2014/06/09 16:55:25
Done.
| |
151 bool fEffectiveClip; | |
152 bool fLCD; | |
153 SkTypeface* fTypefacesToUnref[4]; | |
154 SkPaint fPaint; | |
155 | |
156 // precomputed for each text draw | |
157 SkString fStrings[kCnt]; | |
158 SkColor fColors[kCnt]; | |
159 SkScalar fPtSizes[kCnt]; | |
160 SkTypeface* fTypefaces[kCnt]; | |
161 SkPoint fPositions[kCnt]; | |
162 SkRect fClipRects[kCnt]; | |
163 | |
164 typedef skiagm::GM INHERITED; | |
165 }; | |
166 | |
167 DEF_GM( return SkNEW(VariedTextGM(false, false)); ) | |
168 DEF_GM( return SkNEW(VariedTextGM(true, false)); ) | |
169 DEF_GM( return SkNEW(VariedTextGM(false, true)); ) | |
170 DEF_GM( return SkNEW(VariedTextGM(true, true)); ) | |
OLD | NEW |