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