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