epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | 0fdaa22 | 2011-01-25 18:35:26 +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 | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkPath.h" |
Mike Reed | a62d036 | 2018-08-28 16:26:06 -0400 | [diff] [blame] | 13 | #include "SkTextOnPath.h" |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 17 | static void makebm(SkBitmap* bm, int w, int h) { |
| 18 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 19 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 20 | |
| 21 | SkCanvas canvas(*bm); |
epoger@google.com | b28b5e4 | 2011-05-24 14:51:57 +0000 | [diff] [blame] | 22 | SkScalar s = SkIntToScalar(SkMin32(w, h)); |
reed@google.com | 3869007 | 2011-01-26 01:44:18 +0000 | [diff] [blame] | 23 | SkPoint pts[] = { { 0, 0 }, { s, s } }; |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 24 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 25 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 26 | SkPaint paint; |
| 27 | |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 28 | paint.setDither(true); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 29 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors), |
| 30 | SkShader::kClamp_TileMode)); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 31 | canvas.drawPaint(paint); |
| 32 | } |
| 33 | |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
| 36 | struct GradData { |
| 37 | int fCount; |
| 38 | const SkColor* fColors; |
| 39 | const SkScalar* fPos; |
| 40 | }; |
| 41 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 42 | constexpr SkColor gColors[] = { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 43 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 44 | }; |
| 45 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 46 | constexpr GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | { 2, gColors, nullptr }, |
| 48 | { 5, gColors, nullptr }, |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 51 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
| 52 | return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 55 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 56 | SkPoint center; |
| 57 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 58 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 59 | return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fPos, data.fCount, |
| 60 | tm); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 61 | } |
| 62 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 63 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 64 | SkPoint center; |
| 65 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 66 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 67 | return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 70 | static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 71 | SkPoint center0, center1; |
| 72 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 73 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 74 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 75 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 76 | return SkGradientShader::MakeTwoPointConical( |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 77 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 78 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 79 | data.fColors, data.fPos, data.fCount, tm); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 80 | } |
| 81 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 82 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm); |
skia.committer@gmail.com | 7693dbf | 2014-05-23 03:03:34 +0000 | [diff] [blame] | 83 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 84 | constexpr GradMaker gGradMakers[] = { |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 85 | MakeLinear, MakeRadial, MakeSweep, Make2Conical |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | /////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | class ShaderTextGM : public GM { |
| 91 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | ShaderTextGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 93 | this->setBGColor(0xFFDDDDDD); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 94 | } |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 95 | |
| 96 | protected: |
| 97 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 98 | SkString onShortName() override { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 99 | return SkString("shadertext"); |
| 100 | } |
| 101 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 102 | SkISize onISize() override { return SkISize::Make(1450, 500); } |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 103 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 104 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 105 | const char text[] = "Shaded Text"; |
| 106 | const int textLen = SK_ARRAY_COUNT(text) - 1; |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 107 | const int pointSize = 36; |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 108 | |
reed | da8cf5a | 2014-09-04 13:18:36 -0700 | [diff] [blame] | 109 | const int w = pointSize * textLen; |
| 110 | const int h = pointSize; |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 111 | |
| 112 | SkPoint pts[2] = { |
| 113 | { 0, 0 }, |
| 114 | { SkIntToScalar(w), SkIntToScalar(h) } |
| 115 | }; |
| 116 | SkScalar textBase = SkIntToScalar(h/2); |
| 117 | |
| 118 | SkShader::TileMode tileModes[] = { |
| 119 | SkShader::kClamp_TileMode, |
| 120 | SkShader::kRepeat_TileMode, |
| 121 | SkShader::kMirror_TileMode |
| 122 | }; |
| 123 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 124 | constexpr int gradCount = SK_ARRAY_COUNT(gGradData) * |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 125 | SK_ARRAY_COUNT(gGradMakers); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 126 | constexpr int bmpCount = SK_ARRAY_COUNT(tileModes) * |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 127 | SK_ARRAY_COUNT(tileModes); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 128 | sk_sp<SkShader> shaders[gradCount + bmpCount]; |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 129 | |
| 130 | int shdIdx = 0; |
| 131 | for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) { |
| 132 | for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) { |
| 133 | shaders[shdIdx++] = gGradMakers[m](pts, |
| 134 | gGradData[d], |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 135 | SkShader::kClamp_TileMode); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 138 | |
reed | da8cf5a | 2014-09-04 13:18:36 -0700 | [diff] [blame] | 139 | SkBitmap bm; |
| 140 | makebm(&bm, w/16, h/4); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 141 | for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) { |
| 142 | for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 143 | shaders[shdIdx++] = SkShader::MakeBitmapShader(bm, tileModes[tx], tileModes[ty]); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | SkPaint paint; |
| 148 | paint.setDither(true); |
| 149 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 150 | sk_tool_utils::set_portable_typeface(&paint); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 151 | paint.setTextSize(SkIntToScalar(pointSize)); |
| 152 | |
| 153 | canvas->save(); |
| 154 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
| 155 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 156 | SkPath path; |
bsalomon@google.com | 137c428 | 2011-06-14 17:13:22 +0000 | [diff] [blame] | 157 | path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15), |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | SkIntToScalar(300), SkIntToScalar(90)), |
| 159 | SkIntToScalar(225), SkIntToScalar(90), |
bsalomon@google.com | 137c428 | 2011-06-14 17:13:22 +0000 | [diff] [blame] | 160 | false); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 161 | path.close(); |
| 162 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 163 | constexpr int testsPerCol = 8; |
| 164 | constexpr int rowHeight = 60; |
| 165 | constexpr int colWidth = 300; |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 166 | canvas->save(); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 167 | for (int s = 0; s < static_cast<int>(SK_ARRAY_COUNT(shaders)); s++) { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 168 | canvas->save(); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 169 | int i = 2*s; |
| 170 | canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), |
| 171 | SkIntToScalar((i % testsPerCol) * rowHeight)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 172 | paint.setShader(shaders[s]); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 173 | canvas->drawText(text, textLen, 0, textBase, paint); |
| 174 | canvas->restore(); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 175 | canvas->save(); |
| 176 | ++i; |
| 177 | canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), |
| 178 | SkIntToScalar((i % testsPerCol) * rowHeight)); |
Mike Reed | a62d036 | 2018-08-28 16:26:06 -0400 | [diff] [blame] | 179 | SkDrawTextOnPath(text, textLen, paint, path, nullptr, canvas); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 180 | canvas->restore(); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 181 | } |
| 182 | canvas->restore(); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 183 | |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | private: |
| 187 | typedef GM INHERITED; |
| 188 | }; |
| 189 | |
| 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | static GM* MyFactory(void*) { return new ShaderTextGM; } |
| 193 | static GMRegistry reg(MyFactory); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 194 | } |