bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 7 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 12 | |
| 13 | namespace skiagm { |
| 14 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 15 | static void makebm(SkBitmap* bm, int w, int h) { |
| 16 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 17 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 18 | |
| 19 | SkCanvas canvas(*bm); |
| 20 | SkScalar s = SkIntToScalar(SkMin32(w, h)); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 21 | const SkPoint kPts0[] = { { 0, 0 }, { s, s } }; |
| 22 | const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } }; |
| 23 | const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 24 | const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 }; |
| 25 | const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 27 | |
| 28 | SkPaint paint; |
| 29 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 30 | paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, |
| 31 | SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 32 | canvas.drawPaint(paint); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 33 | paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, |
| 34 | SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 35 | canvas.drawPaint(paint); |
| 36 | } |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
| 40 | struct LabeledMatrix { |
| 41 | SkMatrix fMatrix; |
| 42 | const char* fLabel; |
| 43 | }; |
| 44 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 45 | constexpr char kText[] = "B"; |
| 46 | constexpr int kTextLen = SK_ARRAY_COUNT(kText) - 1; |
| 47 | constexpr int kPointSize = 300; |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 48 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 49 | class ShaderText3GM : public GM { |
| 50 | public: |
| 51 | ShaderText3GM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 52 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | protected: |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | SkString onShortName() override { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 58 | return SkString("shadertext3"); |
| 59 | } |
| 60 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 61 | SkISize onISize() override{ return SkISize::Make(820, 930); } |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 64 | makebm(&fBmp, kPointSize / 4, kPointSize / 4); |
| 65 | } |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 66 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 67 | void onDraw(SkCanvas* canvas) override { |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 68 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 69 | SkPaint bmpPaint; |
| 70 | bmpPaint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 71 | bmpPaint.setFilterQuality(kLow_SkFilterQuality); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 72 | bmpPaint.setAlpha(0x80); |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 73 | canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 74 | |
| 75 | SkPaint outlinePaint; |
| 76 | outlinePaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 77 | sk_tool_utils::set_portable_typeface(&outlinePaint); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 78 | outlinePaint.setTextSize(SkIntToScalar(kPointSize)); |
| 79 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 80 | outlinePaint.setStrokeWidth(0.f); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 81 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 82 | canvas->translate(15.f, 15.f); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 84 | // draw glyphs scaled up |
| 85 | canvas->scale(2.f, 2.f); |
| 86 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 87 | constexpr SkShader::TileMode kTileModes[] = { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 88 | SkShader::kRepeat_TileMode, |
| 89 | SkShader::kMirror_TileMode, |
| 90 | }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 92 | // position the baseline of the first run |
| 93 | canvas->translate(0.f, 0.75f * kPointSize); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 94 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 95 | canvas->save(); |
| 96 | int i = 0; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 97 | for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 98 | for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 99 | SkMatrix localM; |
| 100 | localM.setTranslate(5.f, 5.f); |
| 101 | localM.postRotate(20); |
| 102 | localM.postScale(1.15f, .85f); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 103 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 104 | SkPaint fillPaint; |
| 105 | fillPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 106 | sk_tool_utils::set_portable_typeface(&fillPaint); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 107 | fillPaint.setTextSize(SkIntToScalar(kPointSize)); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 108 | fillPaint.setFilterQuality(kLow_SkFilterQuality); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 109 | fillPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTileModes[tm0], |
| 110 | kTileModes[tm1], &localM)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 111 | |
| 112 | canvas->drawText(kText, kTextLen, 0, 0, fillPaint); |
| 113 | canvas->drawText(kText, kTextLen, 0, 0, outlinePaint); |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 114 | SkScalar w = fillPaint.measureText(kText, kTextLen); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 115 | canvas->translate(w + 10.f, 0.f); |
| 116 | ++i; |
| 117 | if (!(i % 2)) { |
| 118 | canvas->restore(); |
| 119 | canvas->translate(0, 0.75f * kPointSize); |
| 120 | canvas->save(); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | canvas->restore(); |
| 125 | } |
| 126 | |
| 127 | private: |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 128 | SkBitmap fBmp; |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 129 | typedef GM INHERITED; |
| 130 | }; |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 134 | static GM* MyFactory(void*) { return new ShaderText3GM; } |
| 135 | static GMRegistry reg(MyFactory); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 136 | } |