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 | */ |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 7 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | |
| 11 | namespace skiagm { |
| 12 | |
| 13 | struct GradData { |
| 14 | int fCount; |
| 15 | const SkColor* fColors; |
| 16 | const SkScalar* fPos; |
| 17 | }; |
| 18 | |
| 19 | static const SkColor gColors[] = { |
| 20 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 21 | }; |
| 22 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 23 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 24 | static const SkScalar gPos2[] = { |
| 25 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 26 | }; |
| 27 | |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 28 | static const SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 29 | static const SkColor gColorClamp[] = { |
| 30 | SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE |
| 31 | }; |
| 32 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 33 | static const GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | { 2, gColors, nullptr }, |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 35 | { 2, gColors, gPos0 }, |
| 36 | { 2, gColors, gPos1 }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | { 5, gColors, nullptr }, |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 38 | { 5, gColors, gPos2 }, |
| 39 | { 4, gColorClamp, gPosClamp } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 43 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 44 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 45 | data.fCount, tm, 0, &localMatrix); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 46 | } |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 47 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 48 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 49 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 50 | SkPoint center; |
| 51 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 52 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 53 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 54 | data.fPos, data.fCount, tm, 0, &localMatrix); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 58 | SkShader::TileMode, const SkMatrix& localMatrix) { |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 59 | SkPoint center; |
| 60 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 61 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 62 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 63 | data.fPos, data.fCount, 0, &localMatrix); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 64 | } |
| 65 | |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 66 | static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 67 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 68 | SkPoint center0, center1; |
| 69 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 70 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 71 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 72 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 73 | return SkGradientShader::CreateTwoPointConical( |
| 74 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 75 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 76 | data.fColors, data.fPos, data.fCount, tm, |
| 77 | 0, &localMatrix); |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 78 | } |
| 79 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 80 | static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 81 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 82 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 83 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 84 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 85 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 86 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 87 | return SkGradientShader::CreateTwoPointConical(center1, radius1, |
| 88 | center0, radius0, |
| 89 | data.fColors, data.fPos, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 90 | data.fCount, tm, 0, &localMatrix); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 91 | } |
| 92 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 93 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 94 | SkShader::TileMode tm, const SkMatrix& localMatrix); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 95 | static const GradMaker gGradMakers[] = { |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 96 | MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | class GradientsGM : public GM { |
| 102 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 103 | GradientsGM(bool dither) : fDither(dither) { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 104 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 105 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 107 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 108 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 109 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 110 | return SkString(fDither ? "gradients" : "gradients_nodither"); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 111 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 113 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 114 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 115 | virtual void onDraw(SkCanvas* canvas) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 116 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 117 | SkPoint pts[2] = { |
| 118 | { 0, 0 }, |
| 119 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 120 | }; |
| 121 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 122 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 123 | SkPaint paint; |
| 124 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 125 | paint.setDither(fDither); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 126 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 127 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 128 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 129 | canvas->save(); |
| 130 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 131 | SkMatrix scale = SkMatrix::I(); |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 132 | |
| 133 | if (i == 5) { // if the clamp case |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 134 | scale.setScale(0.5f, 0.5f); |
| 135 | scale.postTranslate(25.f, 25.f); |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 136 | } |
skia.committer@gmail.com | d55e357 | 2013-08-08 07:01:20 +0000 | [diff] [blame] | 137 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 138 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, scale); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 139 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 140 | paint.setShader(shader); |
| 141 | canvas->drawRect(r, paint); |
| 142 | shader->unref(); |
| 143 | canvas->translate(0, SkIntToScalar(120)); |
| 144 | } |
| 145 | canvas->restore(); |
| 146 | canvas->translate(SkIntToScalar(120), 0); |
| 147 | } |
| 148 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 149 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 150 | protected: |
| 151 | bool fDither; |
| 152 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 153 | private: |
| 154 | typedef GM INHERITED; |
| 155 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 156 | DEF_GM( return new GradientsGM(true); ) |
| 157 | DEF_GM( return new GradientsGM(false); ) |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 158 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 159 | // Based on the original gradient slide, but with perspective applied to the |
| 160 | // gradient shaders' local matrices |
| 161 | class GradientsLocalPerspectiveGM : public GM { |
| 162 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 163 | GradientsLocalPerspectiveGM(bool dither) : fDither(dither) { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 164 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 165 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 166 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 167 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 168 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 169 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 170 | return SkString(fDither ? "gradients_local_perspective" : |
| 171 | "gradients_local_perspective_nodither"); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 172 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 173 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 174 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 175 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 176 | virtual void onDraw(SkCanvas* canvas) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 177 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 178 | SkPoint pts[2] = { |
| 179 | { 0, 0 }, |
| 180 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 181 | }; |
| 182 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 183 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 184 | SkPaint paint; |
| 185 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 186 | paint.setDither(fDither); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 187 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 188 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 189 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 190 | canvas->save(); |
| 191 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 192 | // apply an increasing y perspective as we move to the right |
| 193 | SkMatrix perspective; |
| 194 | perspective.setIdentity(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 195 | perspective.setPerspY(SkIntToScalar(i+1) / 500); |
| 196 | perspective.setSkewX(SkIntToScalar(i+1) / 10); |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 197 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 198 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, perspective); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 199 | |
| 200 | paint.setShader(shader); |
| 201 | canvas->drawRect(r, paint); |
| 202 | shader->unref(); |
| 203 | canvas->translate(0, SkIntToScalar(120)); |
| 204 | } |
| 205 | canvas->restore(); |
| 206 | canvas->translate(SkIntToScalar(120), 0); |
| 207 | } |
| 208 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 209 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 210 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 211 | bool fDither; |
| 212 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 213 | typedef GM INHERITED; |
| 214 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 215 | DEF_GM( return new GradientsLocalPerspectiveGM(true); ) |
| 216 | DEF_GM( return new GradientsLocalPerspectiveGM(false); ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 217 | |
| 218 | // Based on the original gradient slide, but with perspective applied to |
| 219 | // the view matrix |
| 220 | class GradientsViewPerspectiveGM : public GradientsGM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 221 | public: |
| 222 | GradientsViewPerspectiveGM(bool dither) : INHERITED(dither) { } |
| 223 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 224 | protected: |
| 225 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 226 | return SkString(fDither ? "gradients_view_perspective" : |
| 227 | "gradients_view_perspective_nodither"); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 228 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 229 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 230 | virtual SkISize onISize() { return SkISize::Make(840, 500); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 231 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 232 | virtual void onDraw(SkCanvas* canvas) { |
| 233 | SkMatrix perspective; |
| 234 | perspective.setIdentity(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 235 | perspective.setPerspY(0.001f); |
| 236 | perspective.setSkewX(SkIntToScalar(8) / 25); |
scroggo@google.com | 837d31a | 2012-08-15 18:42:45 +0000 | [diff] [blame] | 237 | canvas->concat(perspective); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 238 | INHERITED::onDraw(canvas); |
| 239 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 240 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 241 | private: |
| 242 | typedef GradientsGM INHERITED; |
| 243 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 244 | DEF_GM( return new GradientsViewPerspectiveGM(true); ) |
| 245 | DEF_GM( return new GradientsViewPerspectiveGM(false); ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 246 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 247 | /* |
| 248 | Inspired by this <canvas> javascript, where we need to detect that we are not |
| 249 | solving a quadratic equation, but must instead solve a linear (since our X^2 |
| 250 | coefficient is 0) |
| 251 | |
| 252 | ctx.fillStyle = '#f00'; |
| 253 | ctx.fillRect(0, 0, 100, 50); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 254 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 255 | var g = ctx.createRadialGradient(-80, 25, 70, 0, 25, 150); |
| 256 | g.addColorStop(0, '#f00'); |
| 257 | g.addColorStop(0.01, '#0f0'); |
| 258 | g.addColorStop(0.99, '#0f0'); |
| 259 | g.addColorStop(1, '#f00'); |
| 260 | ctx.fillStyle = g; |
| 261 | ctx.fillRect(0, 0, 100, 50); |
| 262 | */ |
| 263 | class GradientsDegenrate2PointGM : public GM { |
| 264 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 265 | GradientsDegenrate2PointGM(bool dither) : fDither(dither) {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 266 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 267 | protected: |
| 268 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 269 | return SkString(fDither ? "gradients_degenerate_2pt" : "gradients_degenerate_2pt_nodither"); |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 270 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 271 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 272 | virtual SkISize onISize() { return SkISize::Make(320, 320); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 273 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 274 | void drawBG(SkCanvas* canvas) { |
| 275 | canvas->drawColor(SK_ColorBLUE); |
| 276 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 277 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 278 | virtual void onDraw(SkCanvas* canvas) { |
| 279 | this->drawBG(canvas); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 280 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 281 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorRED }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 282 | SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 }; |
epoger@google.com | 59f3abf | 2011-07-21 15:50:33 +0000 | [diff] [blame] | 283 | SkPoint c0; |
| 284 | c0.iset(-80, 25); |
| 285 | SkScalar r0 = SkIntToScalar(70); |
| 286 | SkPoint c1; |
| 287 | c1.iset(0, 25); |
| 288 | SkScalar r1 = SkIntToScalar(150); |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 289 | SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors, |
| 290 | pos, SK_ARRAY_COUNT(pos), |
| 291 | SkShader::kClamp_TileMode); |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 292 | SkPaint paint; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 293 | paint.setDither(fDither); |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 294 | paint.setShader(s)->unref(); |
| 295 | canvas->drawPaint(paint); |
| 296 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 297 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 298 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 299 | bool fDither; |
| 300 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 301 | typedef GM INHERITED; |
| 302 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 303 | DEF_GM( return new GradientsDegenrate2PointGM(true); ) |
| 304 | DEF_GM( return new GradientsDegenrate2PointGM(false); ) |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 305 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 306 | /// Tests correctness of *optimized* codepaths in gradients. |
| 307 | |
| 308 | class ClampedGradientsGM : public GM { |
| 309 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 310 | ClampedGradientsGM(bool dither) : fDither(dither) {} |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 311 | |
| 312 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 313 | SkString onShortName() { |
| 314 | return SkString(fDither ? "clamped_gradients" : "clamped_gradients_nodither"); |
| 315 | } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 316 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 317 | virtual SkISize onISize() { return SkISize::Make(640, 510); } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 318 | |
| 319 | void drawBG(SkCanvas* canvas) { |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 320 | canvas->drawColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | virtual void onDraw(SkCanvas* canvas) { |
| 324 | this->drawBG(canvas); |
| 325 | |
| 326 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) }; |
| 327 | SkPaint paint; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 328 | paint.setDither(fDither); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 329 | paint.setAntiAlias(true); |
| 330 | |
| 331 | SkPoint center; |
| 332 | center.iset(0, 300); |
| 333 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 334 | SkShader* shader = SkGradientShader::CreateRadial( |
| 335 | SkPoint(center), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 336 | SkIntToScalar(200), gColors, nullptr, 5, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 337 | SkShader::kClamp_TileMode); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 338 | paint.setShader(shader); |
| 339 | canvas->drawRect(r, paint); |
| 340 | shader->unref(); |
| 341 | } |
| 342 | |
| 343 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 344 | bool fDither; |
| 345 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 346 | typedef GM INHERITED; |
| 347 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 348 | DEF_GM( return new ClampedGradientsGM(true); ) |
| 349 | DEF_GM( return new ClampedGradientsGM(false); ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 350 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 351 | /// Checks quality of large radial gradients, which may display |
| 352 | /// some banding. |
| 353 | |
| 354 | class RadialGradientGM : public GM { |
| 355 | public: |
| 356 | RadialGradientGM() {} |
| 357 | |
| 358 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 359 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 360 | SkString onShortName() override { return SkString("radial_gradient"); } |
| 361 | SkISize onISize() override { return SkISize::Make(1280, 1280); } |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 362 | void drawBG(SkCanvas* canvas) { |
| 363 | canvas->drawColor(0xFF000000); |
| 364 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 365 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 366 | const SkISize dim = this->getISize(); |
| 367 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 368 | this->drawBG(canvas); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 369 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 370 | SkPaint paint; |
| 371 | paint.setDither(true); |
| 372 | SkPoint center; |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 373 | center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2); |
| 374 | SkScalar radius = SkIntToScalar(dim.width())/2; |
| 375 | const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 376 | const SkScalar pos[] = { 0.0f, |
| 377 | 0.35f, |
| 378 | 1.0f }; |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 379 | SkShader* shader = |
| 380 | SkGradientShader::CreateRadial(center, radius, colors, |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 381 | pos, SK_ARRAY_COUNT(pos), |
| 382 | SkShader::kClamp_TileMode); |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 383 | paint.setShader(shader)->unref(); |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 384 | SkRect r = { |
| 385 | 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height()) |
| 386 | }; |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 387 | canvas->drawRect(r, paint); |
| 388 | } |
| 389 | private: |
| 390 | typedef GM INHERITED; |
| 391 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 392 | DEF_GM( return new RadialGradientGM; ) |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 393 | |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 394 | class RadialGradient2GM : public GM { |
| 395 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 396 | RadialGradient2GM(bool dither) : fDither(dither) {} |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 397 | |
| 398 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 399 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 400 | SkString onShortName() override { |
| 401 | return SkString(fDither ? "radial_gradient2" : "radial_gradient2_nodither"); |
| 402 | } |
| 403 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 404 | SkISize onISize() override { return SkISize::Make(800, 400); } |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 405 | void drawBG(SkCanvas* canvas) { |
| 406 | canvas->drawColor(0xFF000000); |
| 407 | } |
| 408 | |
| 409 | // Reproduces the example given in bug 7671058. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 410 | void onDraw(SkCanvas* canvas) override { |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 411 | SkPaint paint1, paint2, paint3; |
| 412 | paint1.setStyle(SkPaint::kFill_Style); |
| 413 | paint2.setStyle(SkPaint::kFill_Style); |
| 414 | paint3.setStyle(SkPaint::kFill_Style); |
| 415 | |
| 416 | const SkColor sweep_colors[] = |
| 417 | { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000 }; |
| 418 | const SkColor colors1[] = { 0xFFFFFFFF, 0x00000000 }; |
| 419 | const SkColor colors2[] = { 0xFF000000, 0x00000000 }; |
| 420 | |
| 421 | const SkScalar cx = 200, cy = 200, radius = 150; |
| 422 | SkPoint center; |
| 423 | center.set(cx, cy); |
| 424 | |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 425 | // We can either interpolate endpoints and premultiply each point (default, more precision), |
| 426 | // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap). |
| 427 | const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPremul_Flag }; |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 428 | |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 429 | for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) { |
| 430 | SkAutoTUnref<SkShader> sweep( |
| 431 | SkGradientShader::CreateSweep(cx, cy, sweep_colors, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 432 | nullptr, SK_ARRAY_COUNT(sweep_colors), |
| 433 | flags[i], nullptr)); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 434 | SkAutoTUnref<SkShader> radial1( |
| 435 | SkGradientShader::CreateRadial(center, radius, colors1, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 436 | nullptr, SK_ARRAY_COUNT(colors1), |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 437 | SkShader::kClamp_TileMode, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 438 | flags[i], nullptr)); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 439 | SkAutoTUnref<SkShader> radial2( |
| 440 | SkGradientShader::CreateRadial(center, radius, colors2, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 441 | nullptr, SK_ARRAY_COUNT(colors2), |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 442 | SkShader::kClamp_TileMode, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 443 | flags[i], nullptr)); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 444 | paint1.setShader(sweep); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 445 | paint1.setDither(fDither); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 446 | paint2.setShader(radial1); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 447 | paint2.setDither(fDither); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 448 | paint3.setShader(radial2); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 449 | paint3.setDither(fDither); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 450 | |
| 451 | canvas->drawCircle(cx, cy, radius, paint1); |
| 452 | canvas->drawCircle(cx, cy, radius, paint3); |
| 453 | canvas->drawCircle(cx, cy, radius, paint2); |
| 454 | |
| 455 | canvas->translate(400, 0); |
| 456 | } |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 460 | bool fDither; |
| 461 | |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 462 | typedef GM INHERITED; |
| 463 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 464 | DEF_GM( return new RadialGradient2GM(true); ) |
| 465 | DEF_GM( return new RadialGradient2GM(false); ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 466 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 467 | // Shallow radial (shows banding on raster) |
| 468 | class RadialGradient3GM : public GM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 469 | public: |
| 470 | RadialGradient3GM(bool dither) : fDither(dither) { } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 471 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 472 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 473 | SkString onShortName() override { |
| 474 | return SkString(fDither ? "radial_gradient3" : "radial_gradient3_nodither"); |
| 475 | } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 476 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 477 | SkISize onISize() override { return SkISize::Make(500, 500); } |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 478 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 479 | bool runAsBench() const override { return true; } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 480 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 481 | void onOnceBeforeDraw() override { |
| 482 | const SkPoint center = { 0, 0 }; |
| 483 | const SkScalar kRadius = 3000; |
| 484 | const SkColor gColors[] = { 0xFFFFFFFF, 0xFF000000 }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 485 | fShader.reset(SkGradientShader::CreateRadial(center, kRadius, gColors, nullptr, 2, |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 486 | SkShader::kClamp_TileMode)); |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 487 | } |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 488 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 489 | void onDraw(SkCanvas* canvas) override { |
| 490 | SkPaint paint; |
| 491 | paint.setShader(fShader); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 492 | paint.setDither(fDither); |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 493 | canvas->drawRect(SkRect::MakeWH(500, 500), paint); |
| 494 | } |
| 495 | |
| 496 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 497 | SkAutoTUnref<SkShader> fShader; |
| 498 | bool fDither; |
| 499 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 500 | typedef GM INHERITED; |
| 501 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 502 | DEF_GM( return new RadialGradient3GM(true); ) |
| 503 | DEF_GM( return new RadialGradient3GM(false); ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 504 | |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 505 | class RadialGradient4GM : public GM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 506 | public: |
| 507 | RadialGradient4GM(bool dither) : fDither(dither) { } |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 508 | |
| 509 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 510 | SkString onShortName() override { |
| 511 | return SkString(fDither ? "radial_gradient4" : "radial_gradient4_nodither"); |
| 512 | } |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 513 | |
| 514 | SkISize onISize() override { return SkISize::Make(500, 500); } |
| 515 | |
| 516 | void onOnceBeforeDraw() override { |
| 517 | const SkPoint center = { 250, 250 }; |
| 518 | const SkScalar kRadius = 250; |
| 519 | const SkColor colors[] = { SK_ColorRED, SK_ColorRED, SK_ColorWHITE, SK_ColorWHITE, |
| 520 | SK_ColorRED }; |
| 521 | const SkScalar pos[] = { 0, .4f, .4f, .8f, .8f, 1 }; |
| 522 | fShader.reset(SkGradientShader::CreateRadial(center, kRadius, colors, pos, |
| 523 | SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode)); |
| 524 | } |
| 525 | |
| 526 | void onDraw(SkCanvas* canvas) override { |
| 527 | SkPaint paint; |
| 528 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 529 | paint.setDither(fDither); |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 530 | paint.setShader(fShader); |
| 531 | canvas->drawRect(SkRect::MakeWH(500, 500), paint); |
| 532 | } |
| 533 | |
| 534 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 535 | SkAutoTUnref<SkShader> fShader; |
| 536 | bool fDither; |
| 537 | |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 538 | typedef GM INHERITED; |
| 539 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 540 | DEF_GM( return new RadialGradient4GM(true); ) |
| 541 | DEF_GM( return new RadialGradient4GM(false); ) |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 542 | |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 543 | class LinearGradientGM : public GM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 544 | public: |
| 545 | LinearGradientGM(bool dither) : fDither(dither) { } |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 546 | |
| 547 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 548 | SkString onShortName() override { |
| 549 | return SkString(fDither ? "linear_gradient" : "linear_gradient_nodither"); |
| 550 | } |
| 551 | |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 552 | const SkScalar kWidthBump = 30.f; |
| 553 | const SkScalar kHeight = 5.f; |
| 554 | const SkScalar kMinWidth = 540.f; |
| 555 | |
| 556 | SkISize onISize() override { return SkISize::Make(500, 500); } |
| 557 | |
| 558 | void onOnceBeforeDraw() override { |
| 559 | SkPoint pts[2] = { {0, 0}, {0, 0} }; |
| 560 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorWHITE, 0xFF008200, 0xFF008200, |
| 561 | SK_ColorWHITE, SK_ColorWHITE }; |
| 562 | const SkScalar unitPos[] = { 0, 50, 70, 500, 540 }; |
| 563 | SkScalar pos[6]; |
| 564 | pos[5] = 1; |
| 565 | for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) { |
| 566 | pts[1].fX = 500.f + index * kWidthBump; |
| 567 | for (int inner = 0; inner < (int) SK_ARRAY_COUNT(unitPos); ++inner) { |
| 568 | pos[inner] = unitPos[inner] / (kMinWidth + index * kWidthBump); |
| 569 | } |
| 570 | fShader[index].reset(SkGradientShader::CreateLinear(pts, colors, pos, |
| 571 | SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode)); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | void onDraw(SkCanvas* canvas) override { |
| 576 | SkPaint paint; |
| 577 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 578 | paint.setDither(fDither); |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 579 | for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) { |
| 580 | paint.setShader(fShader[index]); |
| 581 | canvas->drawRect(SkRect::MakeLTRB(0, index * kHeight, kMinWidth + index * kWidthBump, |
| 582 | (index + 1) * kHeight), paint); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 587 | SkAutoTUnref<SkShader> fShader[100]; |
| 588 | bool fDither; |
| 589 | |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 590 | typedef GM INHERITED; |
| 591 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 592 | DEF_GM( return new LinearGradientGM(true); ) |
| 593 | DEF_GM( return new LinearGradientGM(false); ) |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 594 | |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 595 | class LinearGradientTinyGM : public GM { |
| 596 | protected: |
| 597 | SkString onShortName() override { |
| 598 | return SkString("linear_gradient_tiny"); |
| 599 | } |
| 600 | |
| 601 | SkISize onISize() override { |
| 602 | return SkISize::Make(600, 500); |
| 603 | } |
| 604 | |
| 605 | void onDraw(SkCanvas* canvas) override { |
| 606 | const SkScalar kRectSize = 100; |
| 607 | const unsigned kStopCount = 3; |
| 608 | const SkColor colors[kStopCount] = { SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN }; |
| 609 | const struct { |
| 610 | SkPoint pts[2]; |
| 611 | SkScalar pos[kStopCount]; |
| 612 | } configs[] = { |
reed | de3aac8 | 2015-11-22 13:00:04 -0800 | [diff] [blame] | 613 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999f, 1 }}, |
| 614 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000001f, 1 }}, |
| 615 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999999f, 1 }}, |
| 616 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000000001f, 1 }}, |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 617 | |
reed | de3aac8 | 2015-11-22 13:00:04 -0800 | [diff] [blame] | 618 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999f, 1 }}, |
| 619 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000001f, 1 }}, |
| 620 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999999f, 1 }}, |
| 621 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000000001f, 1 }}, |
| 622 | |
| 623 | { { SkPoint::Make(0, 0), SkPoint::Make(0.00001f, 0) }, { 0, 0.5f, 1 }}, |
| 624 | { { SkPoint::Make(9.99999f, 0), SkPoint::Make(10, 0) }, { 0, 0.5f, 1 }}, |
| 625 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 0.00001f) }, { 0, 0.5f, 1 }}, |
| 626 | { { SkPoint::Make(0, 9.99999f), SkPoint::Make(0, 10) }, { 0, 0.5f, 1 }}, |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 627 | }; |
| 628 | |
| 629 | SkPaint paint; |
| 630 | for (unsigned i = 0; i < SK_ARRAY_COUNT(configs); ++i) { |
| 631 | SkAutoCanvasRestore acr(canvas, true); |
| 632 | SkAutoTUnref<SkShader> gradient( |
| 633 | SkGradientShader::CreateLinear(configs[i].pts, colors, configs[i].pos, kStopCount, |
| 634 | SkShader::kClamp_TileMode)); |
| 635 | canvas->translate(kRectSize * ((i % 4) * 1.5f + 0.25f), |
| 636 | kRectSize * ((i / 4) * 1.5f + 0.25f)); |
| 637 | |
| 638 | paint.setShader(gradient); |
| 639 | canvas->drawRect(SkRect::MakeWH(kRectSize, kRectSize), paint); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | private: |
| 644 | typedef GM INHERITED; |
| 645 | }; |
| 646 | DEF_GM( return new LinearGradientTinyGM(); ) |
| 647 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 648 | } |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 649 | |
| 650 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 651 | |
| 652 | struct GradRun { |
| 653 | SkColor fColors[4]; |
| 654 | SkScalar fPos[4]; |
| 655 | int fCount; |
| 656 | }; |
| 657 | |
| 658 | #define SIZE 121 |
| 659 | |
| 660 | static SkShader* make_linear(const GradRun& run, SkShader::TileMode mode) { |
| 661 | const SkPoint pts[] { { 30, 30 }, { SIZE - 30, SIZE - 30 } }; |
| 662 | return SkGradientShader::CreateLinear(pts, run.fColors, run.fPos, run.fCount, mode); |
| 663 | } |
| 664 | |
| 665 | static SkShader* make_radial(const GradRun& run, SkShader::TileMode mode) { |
| 666 | const SkScalar half = SIZE * 0.5f; |
| 667 | return SkGradientShader::CreateRadial({half,half}, half - 10, |
| 668 | run.fColors, run.fPos, run.fCount, mode); |
| 669 | } |
| 670 | |
| 671 | static SkShader* make_conical(const GradRun& run, SkShader::TileMode mode) { |
| 672 | const SkScalar half = SIZE * 0.5f; |
| 673 | const SkPoint center { half, half }; |
| 674 | return SkGradientShader::CreateTwoPointConical(center, 20, center, half - 10, |
| 675 | run.fColors, run.fPos, run.fCount, mode); |
| 676 | } |
| 677 | |
| 678 | static SkShader* make_sweep(const GradRun& run, SkShader::TileMode) { |
| 679 | const SkScalar half = SIZE * 0.5f; |
| 680 | return SkGradientShader::CreateSweep(half, half, run.fColors, run.fPos, run.fCount); |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Exercise duplicate color-stops, at the ends, and in the middle |
| 685 | * |
| 686 | * At the time of this writing, only Linear correctly deals with duplicates at the ends, |
| 687 | * and then only correctly on CPU backend. |
| 688 | */ |
| 689 | DEF_SIMPLE_GM(gradients_dup_color_stops, canvas, 704, 564) { |
| 690 | const SkColor preColor = 0xFFFF0000; // clamp color before start |
| 691 | const SkColor postColor = 0xFF0000FF; // clamp color after end |
| 692 | const SkColor color0 = 0xFF000000; |
| 693 | const SkColor color1 = 0xFF00FF00; |
| 694 | const SkColor badColor = 0xFF3388BB; // should never be seen, fills out fixed-size array |
| 695 | |
| 696 | const GradRun runs[] = { |
| 697 | { { color0, color1, badColor, badColor }, |
| 698 | { 0, 1, -1, -1 }, |
| 699 | 2, |
| 700 | }, |
| 701 | { { preColor, color0, color1, badColor }, |
| 702 | { 0, 0, 1, -1 }, |
| 703 | 3, |
| 704 | }, |
| 705 | { { color0, color1, postColor, badColor }, |
| 706 | { 0, 1, 1, -1 }, |
| 707 | 3, |
| 708 | }, |
| 709 | { { preColor, color0, color1, postColor }, |
| 710 | { 0, 0, 1, 1 }, |
| 711 | 4, |
| 712 | }, |
| 713 | { { color0, color0, color1, color1 }, |
| 714 | { 0, 0.5f, 0.5f, 1 }, |
| 715 | 4, |
| 716 | }, |
| 717 | }; |
| 718 | SkShader* (*factories[])(const GradRun&, SkShader::TileMode) { |
| 719 | make_linear, make_radial, make_conical, make_sweep |
| 720 | }; |
| 721 | |
| 722 | const SkRect rect = SkRect::MakeWH(SIZE, SIZE); |
| 723 | const SkScalar dx = SIZE + 20; |
| 724 | const SkScalar dy = SIZE + 20; |
| 725 | const SkShader::TileMode mode = SkShader::kClamp_TileMode; |
| 726 | |
| 727 | SkPaint paint; |
| 728 | canvas->translate(10, 10 - dy); |
| 729 | for (auto factory : factories) { |
| 730 | canvas->translate(0, dy); |
| 731 | SkAutoCanvasRestore acr(canvas, true); |
| 732 | for (const auto& run : runs) { |
| 733 | paint.setShader(factory(run, mode))->unref(); |
| 734 | canvas->drawRect(rect, paint); |
| 735 | canvas->translate(dx, 0); |
| 736 | } |
| 737 | } |
| 738 | } |