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[] = { |
| 34 | { 2, gColors, NULL }, |
| 35 | { 2, gColors, gPos0 }, |
| 36 | { 2, gColors, gPos1 }, |
| 37 | { 5, gColors, NULL }, |
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: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 103 | GradientsGM() { |
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() { |
| 110 | return SkString("gradients"); |
| 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); |
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 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 137 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, scale); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 138 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 139 | paint.setShader(shader); |
| 140 | canvas->drawRect(r, paint); |
| 141 | shader->unref(); |
| 142 | canvas->translate(0, SkIntToScalar(120)); |
| 143 | } |
| 144 | canvas->restore(); |
| 145 | canvas->translate(SkIntToScalar(120), 0); |
| 146 | } |
| 147 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 148 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 149 | private: |
| 150 | typedef GM INHERITED; |
| 151 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 152 | DEF_GM( return new GradientsGM; ) |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 153 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 154 | // Based on the original gradient slide, but with perspective applied to the |
| 155 | // gradient shaders' local matrices |
| 156 | class GradientsLocalPerspectiveGM : public GM { |
| 157 | public: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 158 | GradientsLocalPerspectiveGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 159 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 160 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 161 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 162 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 163 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 164 | SkString onShortName() { |
| 165 | return SkString("gradients_local_perspective"); |
| 166 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 167 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 168 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 169 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 170 | virtual void onDraw(SkCanvas* canvas) { |
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 | SkPoint pts[2] = { |
| 173 | { 0, 0 }, |
| 174 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 175 | }; |
| 176 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 177 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 178 | SkPaint paint; |
| 179 | paint.setAntiAlias(true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 180 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 181 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 182 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 183 | canvas->save(); |
| 184 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 185 | // apply an increasing y perspective as we move to the right |
| 186 | SkMatrix perspective; |
| 187 | perspective.setIdentity(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 188 | perspective.setPerspY(SkIntToScalar(i+1) / 500); |
| 189 | perspective.setSkewX(SkIntToScalar(i+1) / 10); |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 190 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 191 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, perspective); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 192 | |
| 193 | paint.setShader(shader); |
| 194 | canvas->drawRect(r, paint); |
| 195 | shader->unref(); |
| 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: |
| 204 | typedef GM INHERITED; |
| 205 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 206 | DEF_GM( return new GradientsLocalPerspectiveGM; ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 207 | |
| 208 | // Based on the original gradient slide, but with perspective applied to |
| 209 | // the view matrix |
| 210 | class GradientsViewPerspectiveGM : public GradientsGM { |
| 211 | protected: |
| 212 | SkString onShortName() { |
| 213 | return SkString("gradients_view_perspective"); |
| 214 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 215 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 216 | virtual SkISize onISize() { return SkISize::Make(840, 500); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 217 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 218 | virtual void onDraw(SkCanvas* canvas) { |
| 219 | SkMatrix perspective; |
| 220 | perspective.setIdentity(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 221 | perspective.setPerspY(0.001f); |
| 222 | perspective.setSkewX(SkIntToScalar(8) / 25); |
scroggo@google.com | 837d31a | 2012-08-15 18:42:45 +0000 | [diff] [blame] | 223 | canvas->concat(perspective); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 224 | INHERITED::onDraw(canvas); |
| 225 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 226 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 227 | private: |
| 228 | typedef GradientsGM INHERITED; |
| 229 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 230 | DEF_GM( return new GradientsViewPerspectiveGM; ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 231 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 232 | /* |
| 233 | Inspired by this <canvas> javascript, where we need to detect that we are not |
| 234 | solving a quadratic equation, but must instead solve a linear (since our X^2 |
| 235 | coefficient is 0) |
| 236 | |
| 237 | ctx.fillStyle = '#f00'; |
| 238 | ctx.fillRect(0, 0, 100, 50); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 239 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 240 | var g = ctx.createRadialGradient(-80, 25, 70, 0, 25, 150); |
| 241 | g.addColorStop(0, '#f00'); |
| 242 | g.addColorStop(0.01, '#0f0'); |
| 243 | g.addColorStop(0.99, '#0f0'); |
| 244 | g.addColorStop(1, '#f00'); |
| 245 | ctx.fillStyle = g; |
| 246 | ctx.fillRect(0, 0, 100, 50); |
| 247 | */ |
| 248 | class GradientsDegenrate2PointGM : public GM { |
| 249 | public: |
| 250 | GradientsDegenrate2PointGM() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 251 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 252 | protected: |
| 253 | SkString onShortName() { |
| 254 | return SkString("gradients_degenerate_2pt"); |
| 255 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 256 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 257 | virtual SkISize onISize() { return SkISize::Make(320, 320); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 258 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 259 | void drawBG(SkCanvas* canvas) { |
| 260 | canvas->drawColor(SK_ColorBLUE); |
| 261 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 262 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 263 | virtual void onDraw(SkCanvas* canvas) { |
| 264 | this->drawBG(canvas); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 265 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 266 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorRED }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 267 | SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 }; |
epoger@google.com | 59f3abf | 2011-07-21 15:50:33 +0000 | [diff] [blame] | 268 | SkPoint c0; |
| 269 | c0.iset(-80, 25); |
| 270 | SkScalar r0 = SkIntToScalar(70); |
| 271 | SkPoint c1; |
| 272 | c1.iset(0, 25); |
| 273 | SkScalar r1 = SkIntToScalar(150); |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 274 | SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors, |
| 275 | pos, SK_ARRAY_COUNT(pos), |
| 276 | SkShader::kClamp_TileMode); |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 277 | SkPaint paint; |
| 278 | paint.setShader(s)->unref(); |
| 279 | canvas->drawPaint(paint); |
| 280 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 281 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 282 | private: |
| 283 | typedef GM INHERITED; |
| 284 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 285 | DEF_GM( return new GradientsDegenrate2PointGM; ) |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 286 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 287 | /// Tests correctness of *optimized* codepaths in gradients. |
| 288 | |
| 289 | class ClampedGradientsGM : public GM { |
| 290 | public: |
| 291 | ClampedGradientsGM() {} |
| 292 | |
| 293 | protected: |
| 294 | SkString onShortName() { return SkString("clamped_gradients"); } |
| 295 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 296 | virtual SkISize onISize() { return SkISize::Make(640, 510); } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 297 | |
| 298 | void drawBG(SkCanvas* canvas) { |
| 299 | canvas->drawColor(0xFFDDDDDD); |
| 300 | } |
| 301 | |
| 302 | virtual void onDraw(SkCanvas* canvas) { |
| 303 | this->drawBG(canvas); |
| 304 | |
| 305 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) }; |
| 306 | SkPaint paint; |
| 307 | paint.setAntiAlias(true); |
| 308 | |
| 309 | SkPoint center; |
| 310 | center.iset(0, 300); |
| 311 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 312 | SkShader* shader = SkGradientShader::CreateRadial( |
| 313 | SkPoint(center), |
bungeman@google.com | 01744a4 | 2011-10-06 19:37:39 +0000 | [diff] [blame] | 314 | SkIntToScalar(200), gColors, NULL, 5, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 315 | SkShader::kClamp_TileMode); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 316 | paint.setShader(shader); |
| 317 | canvas->drawRect(r, paint); |
| 318 | shader->unref(); |
| 319 | } |
| 320 | |
| 321 | private: |
| 322 | typedef GM INHERITED; |
| 323 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 324 | DEF_GM( return new ClampedGradientsGM; ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 325 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 326 | /// Checks quality of large radial gradients, which may display |
| 327 | /// some banding. |
| 328 | |
| 329 | class RadialGradientGM : public GM { |
| 330 | public: |
| 331 | RadialGradientGM() {} |
| 332 | |
| 333 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 334 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 335 | SkString onShortName() override { return SkString("radial_gradient"); } |
| 336 | SkISize onISize() override { return SkISize::Make(1280, 1280); } |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 337 | void drawBG(SkCanvas* canvas) { |
| 338 | canvas->drawColor(0xFF000000); |
| 339 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 340 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 341 | const SkISize dim = this->getISize(); |
| 342 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 343 | this->drawBG(canvas); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 344 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 345 | SkPaint paint; |
| 346 | paint.setDither(true); |
| 347 | SkPoint center; |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 348 | center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2); |
| 349 | SkScalar radius = SkIntToScalar(dim.width())/2; |
| 350 | const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 351 | const SkScalar pos[] = { 0.0f, |
| 352 | 0.35f, |
| 353 | 1.0f }; |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 354 | SkShader* shader = |
| 355 | SkGradientShader::CreateRadial(center, radius, colors, |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 356 | pos, SK_ARRAY_COUNT(pos), |
| 357 | SkShader::kClamp_TileMode); |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 358 | paint.setShader(shader)->unref(); |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 359 | SkRect r = { |
| 360 | 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height()) |
| 361 | }; |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 362 | canvas->drawRect(r, paint); |
| 363 | } |
| 364 | private: |
| 365 | typedef GM INHERITED; |
| 366 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 367 | DEF_GM( return new RadialGradientGM; ) |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 368 | |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 369 | class RadialGradient2GM : public GM { |
| 370 | public: |
| 371 | RadialGradient2GM() {} |
| 372 | |
| 373 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 374 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 375 | SkString onShortName() override { return SkString("radial_gradient2"); } |
| 376 | SkISize onISize() override { return SkISize::Make(800, 400); } |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 377 | void drawBG(SkCanvas* canvas) { |
| 378 | canvas->drawColor(0xFF000000); |
| 379 | } |
| 380 | |
| 381 | // Reproduces the example given in bug 7671058. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 382 | void onDraw(SkCanvas* canvas) override { |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 383 | SkPaint paint1, paint2, paint3; |
| 384 | paint1.setStyle(SkPaint::kFill_Style); |
| 385 | paint2.setStyle(SkPaint::kFill_Style); |
| 386 | paint3.setStyle(SkPaint::kFill_Style); |
| 387 | |
| 388 | const SkColor sweep_colors[] = |
| 389 | { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000 }; |
| 390 | const SkColor colors1[] = { 0xFFFFFFFF, 0x00000000 }; |
| 391 | const SkColor colors2[] = { 0xFF000000, 0x00000000 }; |
| 392 | |
| 393 | const SkScalar cx = 200, cy = 200, radius = 150; |
| 394 | SkPoint center; |
| 395 | center.set(cx, cy); |
| 396 | |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 397 | // We can either interpolate endpoints and premultiply each point (default, more precision), |
| 398 | // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap). |
| 399 | const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPremul_Flag }; |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 400 | |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 401 | for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) { |
| 402 | SkAutoTUnref<SkShader> sweep( |
| 403 | SkGradientShader::CreateSweep(cx, cy, sweep_colors, |
| 404 | NULL, SK_ARRAY_COUNT(sweep_colors), |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 405 | flags[i], NULL)); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 406 | SkAutoTUnref<SkShader> radial1( |
| 407 | SkGradientShader::CreateRadial(center, radius, colors1, |
| 408 | NULL, SK_ARRAY_COUNT(colors1), |
| 409 | SkShader::kClamp_TileMode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 410 | flags[i], NULL)); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 411 | SkAutoTUnref<SkShader> radial2( |
| 412 | SkGradientShader::CreateRadial(center, radius, colors2, |
| 413 | NULL, SK_ARRAY_COUNT(colors2), |
| 414 | SkShader::kClamp_TileMode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 415 | flags[i], NULL)); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 416 | paint1.setShader(sweep); |
| 417 | paint2.setShader(radial1); |
| 418 | paint3.setShader(radial2); |
| 419 | |
| 420 | canvas->drawCircle(cx, cy, radius, paint1); |
| 421 | canvas->drawCircle(cx, cy, radius, paint3); |
| 422 | canvas->drawCircle(cx, cy, radius, paint2); |
| 423 | |
| 424 | canvas->translate(400, 0); |
| 425 | } |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private: |
| 429 | typedef GM INHERITED; |
| 430 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 431 | DEF_GM( return new RadialGradient2GM; ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 432 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 433 | // Shallow radial (shows banding on raster) |
| 434 | class RadialGradient3GM : public GM { |
| 435 | SkAutoTUnref<SkShader> fShader; |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 436 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 437 | protected: |
| 438 | SkString onShortName() override { return SkString("radial_gradient3"); } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 439 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 440 | SkISize onISize() override { return SkISize::Make(500, 500); } |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 441 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 442 | bool runAsBench() const override { return true; } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 443 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 444 | void onOnceBeforeDraw() override { |
| 445 | const SkPoint center = { 0, 0 }; |
| 446 | const SkScalar kRadius = 3000; |
| 447 | const SkColor gColors[] = { 0xFFFFFFFF, 0xFF000000 }; |
| 448 | fShader.reset(SkGradientShader::CreateRadial(center, kRadius, gColors, NULL, 2, |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 449 | SkShader::kClamp_TileMode)); |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 450 | } |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 451 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 452 | void onDraw(SkCanvas* canvas) override { |
| 453 | SkPaint paint; |
| 454 | paint.setShader(fShader); |
| 455 | canvas->drawRect(SkRect::MakeWH(500, 500), paint); |
| 456 | } |
| 457 | |
| 458 | private: |
| 459 | typedef GM INHERITED; |
| 460 | }; |
| 461 | DEF_GM( return new RadialGradient3GM; ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 462 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 463 | } |