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