• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 
19 class VariedTextGM : public skiagm::GM {
20 public:
VariedTextGM(bool effectiveClip,bool lcd)21     VariedTextGM(bool effectiveClip, bool lcd)
22         : fEffectiveClip(effectiveClip)
23         , fLCD(lcd) {
24         memset(fTypefacesToUnref, 0, sizeof(fTypefacesToUnref));
25     }
26 
~VariedTextGM()27     ~VariedTextGM() {
28         for (size_t i = 0; i < SK_ARRAY_COUNT(fTypefacesToUnref); ++i) {
29             SkSafeUnref(fTypefacesToUnref[i]);
30         }
31     }
32 
33 protected:
onShortName()34     SkString onShortName() override {
35         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 
onISize()49     SkISize onISize() override {
50         return SkISize::Make(640, 480);
51     }
52 
onOnceBeforeDraw()53     void onOnceBeforeDraw() override {
54         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         static_assert(4 == SK_ARRAY_COUNT(fTypefacesToUnref), "typeface_cnt");
62         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);
66 
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             fColors[i] = sk_tool_utils::color_to_565(fColors[i]);
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 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 
onDraw(SkCanvas * canvas)115     void onDraw(SkCanvas* canvas) 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 
runAsBench() const140     bool runAsBench() const override { return true; }
141 
142 private:
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 
163 DEF_GM(return new VariedTextGM(false, false);)
164 DEF_GM(return new VariedTextGM(true, false);)
165 DEF_GM(return new VariedTextGM(false, true);)
166 DEF_GM(return new VariedTextGM(true, true);)
167