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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFilterQuality.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkFontTypes.h" |
| 15 | #include "include/core/SkMatrix.h" |
| 16 | #include "include/core/SkPaint.h" |
| 17 | #include "include/core/SkPoint.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkShader.h" |
| 20 | #include "include/core/SkSize.h" |
| 21 | #include "include/core/SkString.h" |
| 22 | #include "include/core/SkTileMode.h" |
| 23 | #include "include/core/SkTypeface.h" |
| 24 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/effects/SkGradientShader.h" |
| 26 | #include "tools/ToolUtils.h" |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 27 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 28 | #include <string.h> |
| 29 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 30 | namespace skiagm { |
| 31 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 32 | static void makebm(SkBitmap* bm, int w, int h) { |
| 33 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 34 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 35 | |
| 36 | SkCanvas canvas(*bm); |
Brian Osman | 7f36405 | 2020-02-06 11:25:43 -0500 | [diff] [blame] | 37 | SkScalar s = SkIntToScalar(std::min(w, h)); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 38 | const SkPoint kPts0[] = { { 0, 0 }, { s, s } }; |
| 39 | const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } }; |
| 40 | const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 41 | const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 }; |
| 42 | const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 44 | |
| 45 | SkPaint paint; |
| 46 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 47 | paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 48 | SK_ARRAY_COUNT(kColors0), SkTileMode::kClamp)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 49 | canvas.drawPaint(paint); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 50 | paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 51 | SK_ARRAY_COUNT(kColors1), SkTileMode::kClamp)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 52 | canvas.drawPaint(paint); |
| 53 | } |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | struct LabeledMatrix { |
| 58 | SkMatrix fMatrix; |
| 59 | const char* fLabel; |
| 60 | }; |
| 61 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 62 | constexpr int kPointSize = 300; |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 63 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 64 | class ShaderText3GM : public GM { |
| 65 | public: |
| 66 | ShaderText3GM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 67 | this->setBGColor(0xFFDDDDDD); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | protected: |
| 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | SkString onShortName() override { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 73 | return SkString("shadertext3"); |
| 74 | } |
| 75 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 76 | SkISize onISize() override { return SkISize::Make(820, 930); } |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 79 | makebm(&fBmp, kPointSize / 4, kPointSize / 4); |
| 80 | } |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 81 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 82 | void onDraw(SkCanvas* canvas) override { |
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 | SkPaint bmpPaint; |
| 85 | bmpPaint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 86 | bmpPaint.setFilterQuality(kLow_SkFilterQuality); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 87 | bmpPaint.setAlphaf(0.5f); |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 88 | canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 89 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 90 | SkFont font(ToolUtils::create_portable_typeface(), SkIntToScalar(kPointSize)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 91 | SkPaint outlinePaint; |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 92 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 93 | outlinePaint.setStrokeWidth(0.f); |
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->translate(15.f, 15.f); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 96 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 97 | // draw glyphs scaled up |
| 98 | canvas->scale(2.f, 2.f); |
| 99 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 100 | constexpr SkTileMode kTileModes[] = { |
| 101 | SkTileMode::kRepeat, |
| 102 | SkTileMode::kMirror, |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 103 | }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 104 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 105 | // position the baseline of the first run |
| 106 | canvas->translate(0.f, 0.75f * kPointSize); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 108 | canvas->save(); |
| 109 | int i = 0; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 110 | for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 111 | for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 112 | SkMatrix localM; |
| 113 | localM.setTranslate(5.f, 5.f); |
| 114 | localM.postRotate(20); |
| 115 | localM.postScale(1.15f, .85f); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 116 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 117 | SkPaint fillPaint; |
| 118 | fillPaint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 119 | fillPaint.setFilterQuality(kLow_SkFilterQuality); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 120 | fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1], &localM)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 121 | |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 122 | constexpr char kText[] = "B"; |
| 123 | canvas->drawString(kText, 0, 0, font, fillPaint); |
| 124 | canvas->drawString(kText, 0, 0, font, outlinePaint); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 125 | SkScalar w = font.measureText(kText, strlen(kText), SkTextEncoding::kUTF8); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 126 | canvas->translate(w + 10.f, 0.f); |
| 127 | ++i; |
| 128 | if (!(i % 2)) { |
| 129 | canvas->restore(); |
| 130 | canvas->translate(0, 0.75f * kPointSize); |
| 131 | canvas->save(); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | canvas->restore(); |
| 136 | } |
| 137 | |
| 138 | private: |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 139 | SkBitmap fBmp; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame^] | 140 | using INHERITED = GM; |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /////////////////////////////////////////////////////////////////////////////// |
| 144 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 145 | DEF_GM( return new ShaderText3GM; ) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 146 | } // namespace skiagm |