OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkPath.h" | 10 #include "SkPath.h" |
11 #include "SkTypeface.h" | 11 #include "SkTypeface.h" |
12 #include "SkRandom.h" | 12 #include "SkRandom.h" |
13 | 13 |
14 /** | 14 /** |
15 * Draws text with random parameters. The text draws each get their own clip rec
t. It is also | 15 * Draws text with random parameters. The text draws each get their own clip rec
t. It is also |
16 * used as a bench to measure how well the GPU backend batches text draws. | 16 * used as a bench to measure how well the GPU backend batches text draws. |
17 */ | 17 */ |
18 | 18 |
19 class VariedTextGM : public skiagm::GM { | 19 class VariedTextGM : public skiagm::GM { |
20 public: | 20 public: |
21 VariedTextGM(bool effectiveClip, bool lcd) | 21 VariedTextGM(bool effectiveClip, bool lcd) |
22 : fEffectiveClip(effectiveClip) | 22 : fEffectiveClip(effectiveClip) |
23 , fLCD(lcd) { | 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 } | 24 } |
32 | 25 |
33 protected: | 26 protected: |
34 SkString onShortName() override { | 27 SkString onShortName() override { |
35 SkString name("varied_text"); | 28 SkString name("varied_text"); |
36 if (fEffectiveClip) { | 29 if (fEffectiveClip) { |
37 name.append("_clipped"); | 30 name.append("_clipped"); |
38 } else { | 31 } else { |
39 name.append("_ignorable_clip"); | 32 name.append("_ignorable_clip"); |
40 } | 33 } |
(...skipping 10 matching lines...) Expand all Loading... |
51 } | 44 } |
52 | 45 |
53 void onOnceBeforeDraw() override { | 46 void onOnceBeforeDraw() override { |
54 fPaint.setAntiAlias(true); | 47 fPaint.setAntiAlias(true); |
55 fPaint.setLCDRenderText(fLCD); | 48 fPaint.setLCDRenderText(fLCD); |
56 | 49 |
57 SkISize size = this->getISize(); | 50 SkISize size = this->getISize(); |
58 SkScalar w = SkIntToScalar(size.fWidth); | 51 SkScalar w = SkIntToScalar(size.fWidth); |
59 SkScalar h = SkIntToScalar(size.fHeight); | 52 SkScalar h = SkIntToScalar(size.fHeight); |
60 | 53 |
61 static_assert(4 == SK_ARRAY_COUNT(fTypefacesToUnref), "typeface_cnt"); | 54 static_assert(4 == SK_ARRAY_COUNT(fTypefaces), "typeface_cnt"); |
62 fTypefacesToUnref[0] = sk_tool_utils::create_portable_typeface("sans-ser
if", SkTypeface::kNormal); | 55 fTypefaces[0] = sk_tool_utils::create_portable_typeface("sans-serif", Sk
Typeface::kNormal); |
63 fTypefacesToUnref[1] = sk_tool_utils::create_portable_typeface("sans-ser
if", SkTypeface::kBold); | 56 fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif", Sk
Typeface::kBold); |
64 fTypefacesToUnref[2] = sk_tool_utils::create_portable_typeface("serif",
SkTypeface::kNormal); | 57 fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkTypef
ace::kNormal); |
65 fTypefacesToUnref[3] = sk_tool_utils::create_portable_typeface("serif",
SkTypeface::kBold); | 58 fTypefaces[3] = sk_tool_utils::create_portable_typeface("serif", SkTypef
ace::kBold); |
66 | 59 |
67 SkRandom random; | 60 SkRandom random; |
68 for (int i = 0; i < kCnt; ++i) { | 61 for (int i = 0; i < kCnt; ++i) { |
69 int length = random.nextRangeU(kMinLength, kMaxLength); | 62 int length = random.nextRangeU(kMinLength, kMaxLength); |
70 char text[kMaxLength]; | 63 char text[kMaxLength]; |
71 for (int j = 0; j < length; ++j) { | 64 for (int j = 0; j < length; ++j) { |
72 text[j] = (char)random.nextRangeU('!', 'z'); | 65 text[j] = (char)random.nextRangeU('!', 'z'); |
73 } | 66 } |
74 fStrings[i].set(text, length); | 67 fStrings[i].set(text, length); |
75 | 68 |
76 fColors[i] = random.nextU(); | 69 fColors[i] = random.nextU(); |
77 fColors[i] |= 0xFF000000; | 70 fColors[i] |= 0xFF000000; |
78 fColors[i] = sk_tool_utils::color_to_565(fColors[i]); | 71 fColors[i] = sk_tool_utils::color_to_565(fColors[i]); |
79 | 72 |
80 static const SkScalar kMinPtSize = 8.f; | 73 static const SkScalar kMinPtSize = 8.f; |
81 static const SkScalar kMaxPtSize = 32.f; | 74 static const SkScalar kMaxPtSize = 32.f; |
82 | 75 |
83 fPtSizes[i] = random.nextRangeScalar(kMinPtSize, kMaxPtSize); | 76 fPtSizes[i] = random.nextRangeScalar(kMinPtSize, kMaxPtSize); |
84 | 77 |
85 fTypefaces[i] = fTypefacesToUnref[ | 78 fTypefaceIndices[i] = random.nextULessThan(SK_ARRAY_COUNT(fTypefaces
)); |
86 random.nextULessThan(SK_ARRAY_COUNT(fTypefacesToUnref))]; | |
87 | 79 |
88 SkRect r; | 80 SkRect r; |
89 fPaint.setColor(fColors[i]); | 81 fPaint.setColor(fColors[i]); |
90 fPaint.setTypeface(fTypefaces[i]); | 82 fPaint.setTypeface(fTypefaces[fTypefaceIndices[i]]); |
91 fPaint.setTextSize(fPtSizes[i]); | 83 fPaint.setTextSize(fPtSizes[i]); |
92 | 84 |
93 fPaint.measureText(fStrings[i].c_str(), fStrings[i].size(), &r); | 85 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 | 86 // safeRect is set of x,y positions where we can draw the string wit
hout hitting |
95 // the GM's border. | 87 // the GM's border. |
96 SkRect safeRect = SkRect::MakeLTRB(-r.fLeft, -r.fTop, w - r.fRight,
h - r.fBottom); | 88 SkRect safeRect = SkRect::MakeLTRB(-r.fLeft, -r.fTop, w - r.fRight,
h - r.fBottom); |
97 if (safeRect.isEmpty()) { | 89 if (safeRect.isEmpty()) { |
98 // If we don't fit then just don't worry about how we get cliped
to the device | 90 // If we don't fit then just don't worry about how we get cliped
to the device |
99 // border. | 91 // border. |
100 safeRect = SkRect::MakeWH(w, h); | 92 safeRect = SkRect::MakeWH(w, h); |
101 } | 93 } |
102 fPositions[i].fX = random.nextRangeScalar(safeRect.fLeft, safeRect.f
Right); | 94 fPositions[i].fX = random.nextRangeScalar(safeRect.fLeft, safeRect.f
Right); |
103 fPositions[i].fY = random.nextRangeScalar(safeRect.fTop, safeRect.fB
ottom); | 95 fPositions[i].fY = random.nextRangeScalar(safeRect.fTop, safeRect.fB
ottom); |
104 | 96 |
105 fClipRects[i] = r; | 97 fClipRects[i] = r; |
106 fClipRects[i].offset(fPositions[i].fX, fPositions[i].fY); | 98 fClipRects[i].offset(fPositions[i].fX, fPositions[i].fY); |
107 fClipRects[i].outset(2.f, 2.f); | 99 fClipRects[i].outset(2.f, 2.f); |
108 | 100 |
109 if (fEffectiveClip) { | 101 if (fEffectiveClip) { |
110 fClipRects[i].fRight -= 0.25f * fClipRects[i].width(); | 102 fClipRects[i].fRight -= 0.25f * fClipRects[i].width(); |
111 } | 103 } |
112 } | 104 } |
113 } | 105 } |
114 | 106 |
115 void onDraw(SkCanvas* canvas) override { | 107 void onDraw(SkCanvas* canvas) override { |
116 for (int i = 0; i < kCnt; ++i) { | 108 for (int i = 0; i < kCnt; ++i) { |
117 fPaint.setColor(fColors[i]); | 109 fPaint.setColor(fColors[i]); |
118 fPaint.setTextSize(fPtSizes[i]); | 110 fPaint.setTextSize(fPtSizes[i]); |
119 fPaint.setTypeface(fTypefaces[i]); | 111 fPaint.setTypeface(fTypefaces[fTypefaceIndices[i]]); |
120 | 112 |
121 canvas->save(); | 113 canvas->save(); |
122 canvas->clipRect(fClipRects[i]); | 114 canvas->clipRect(fClipRects[i]); |
123 canvas->translate(fPositions[i].fX, fPositions[i].fY); | 115 canvas->translate(fPositions[i].fX, fPositions[i].fY); |
124 canvas->drawText(fStrings[i].c_str(), fStrings[i].size(), 0, 0,
fPaint); | 116 canvas->drawText(fStrings[i].c_str(), fStrings[i].size(), 0, 0,
fPaint); |
125 canvas->restore(); | 117 canvas->restore(); |
126 } | 118 } |
127 | 119 |
128 // Visualize the clips, but not in bench mode. | 120 // Visualize the clips, but not in bench mode. |
129 if (kBench_Mode != this->getMode()) { | 121 if (kBench_Mode != this->getMode()) { |
130 SkPaint wirePaint; | 122 SkPaint wirePaint; |
131 wirePaint.setAntiAlias(true); | 123 wirePaint.setAntiAlias(true); |
132 wirePaint.setStrokeWidth(0); | 124 wirePaint.setStrokeWidth(0); |
133 wirePaint.setStyle(SkPaint::kStroke_Style); | 125 wirePaint.setStyle(SkPaint::kStroke_Style); |
134 for (int i = 0; i < kCnt; ++i) { | 126 for (int i = 0; i < kCnt; ++i) { |
135 canvas->drawRect(fClipRects[i], wirePaint); | 127 canvas->drawRect(fClipRects[i], wirePaint); |
136 } | 128 } |
137 } | 129 } |
138 } | 130 } |
139 | 131 |
140 bool runAsBench() const override { return true; } | 132 bool runAsBench() const override { return true; } |
141 | 133 |
142 private: | 134 private: |
143 static const int kCnt = 30; | 135 static const int kCnt = 30; |
144 static const int kMinLength = 15; | 136 static const int kMinLength = 15; |
145 static const int kMaxLength = 40; | 137 static const int kMaxLength = 40; |
146 | 138 |
147 bool fEffectiveClip; | 139 bool fEffectiveClip; |
148 bool fLCD; | 140 bool fLCD; |
149 SkTypeface* fTypefacesToUnref[4]; | 141 sk_sp<SkTypeface> fTypefaces[4]; |
150 SkPaint fPaint; | 142 SkPaint fPaint; |
151 | 143 |
152 // precomputed for each text draw | 144 // precomputed for each text draw |
153 SkString fStrings[kCnt]; | 145 SkString fStrings[kCnt]; |
154 SkColor fColors[kCnt]; | 146 SkColor fColors[kCnt]; |
155 SkScalar fPtSizes[kCnt]; | 147 SkScalar fPtSizes[kCnt]; |
156 SkTypeface* fTypefaces[kCnt]; | 148 int fTypefaceIndices[kCnt]; |
157 SkPoint fPositions[kCnt]; | 149 SkPoint fPositions[kCnt]; |
158 SkRect fClipRects[kCnt]; | 150 SkRect fClipRects[kCnt]; |
159 | 151 |
160 typedef skiagm::GM INHERITED; | 152 typedef skiagm::GM INHERITED; |
161 }; | 153 }; |
162 | 154 |
163 DEF_GM(return new VariedTextGM(false, false);) | 155 DEF_GM(return new VariedTextGM(false, false);) |
164 DEF_GM(return new VariedTextGM(true, false);) | 156 DEF_GM(return new VariedTextGM(true, false);) |
165 DEF_GM(return new VariedTextGM(false, true);) | 157 DEF_GM(return new VariedTextGM(false, true);) |
166 DEF_GM(return new VariedTextGM(true, true);) | 158 DEF_GM(return new VariedTextGM(true, true);) |
OLD | NEW |