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