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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | struct GradData { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 15 | int fCount; |
| 16 | const SkColor* fColors; |
| 17 | const SkColor4f* fColors4f; |
| 18 | const SkScalar* fPos; |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 21 | constexpr SkColor gColors[] = { |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 22 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 23 | }; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 24 | constexpr SkColor4f gColors4f[] ={ |
| 25 | { 1.0f, 0.0f, 0.0f, 1.0f }, // Red |
| 26 | { 0.0f, 1.0f, 0.0f, 1.0f }, // Green |
| 27 | { 0.0f, 0.0f, 1.0f, 1.0f }, // Blue |
| 28 | { 1.0f, 1.0f, 1.0f, 1.0f }, // White |
| 29 | { 0.0f, 0.0f, 0.0f, 1.0f } // Black |
| 30 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 31 | constexpr SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 32 | constexpr SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 33 | constexpr SkScalar gPos2[] = { |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 34 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 35 | }; |
| 36 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 37 | constexpr SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 38 | constexpr SkColor gColorClamp[] = { |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 39 | SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE |
| 40 | }; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 41 | constexpr SkColor4f gColor4fClamp[] ={ |
| 42 | { 1.0f, 0.0f, 0.0f, 1.0f }, // Red |
| 43 | { 0.0f, 1.0f, 0.0f, 1.0f }, // Green |
| 44 | { 0.0f, 1.0f, 0.0f, 1.0f }, // Green |
| 45 | { 0.0f, 0.0f, 1.0f, 1.0f } // Blue |
| 46 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 47 | constexpr GradData gGradData[] = { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 48 | { 2, gColors, gColors4f, nullptr }, |
| 49 | { 2, gColors, gColors4f, gPos0 }, |
| 50 | { 2, gColors, gColors4f, gPos1 }, |
| 51 | { 5, gColors, gColors4f, nullptr }, |
| 52 | { 5, gColors, gColors4f, gPos2 }, |
| 53 | { 4, gColorClamp, gColor4fClamp, gPosClamp } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 56 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, |
| 57 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 58 | return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm, 0, |
| 59 | &localMatrix); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 60 | } |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 61 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 62 | static sk_sp<SkShader> MakeLinear4f(const SkPoint pts[2], const GradData& data, |
| 63 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 64 | auto srgb = SkColorSpace::MakeSRGBLinear(); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 65 | return SkGradientShader::MakeLinear(pts, data.fColors4f, srgb, data.fPos, data.fCount, tm, 0, |
| 66 | &localMatrix); |
| 67 | } |
| 68 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, |
| 70 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 71 | SkPoint center; |
| 72 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 73 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 74 | return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fPos, data.fCount, |
| 75 | tm, 0, &localMatrix); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 76 | } |
| 77 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 78 | static sk_sp<SkShader> MakeRadial4f(const SkPoint pts[2], const GradData& data, |
| 79 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 80 | SkPoint center; |
| 81 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 82 | SkScalarAve(pts[0].fY, pts[1].fY)); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 83 | auto srgb = SkColorSpace::MakeSRGBLinear(); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 84 | return SkGradientShader::MakeRadial(center, center.fX, data.fColors4f, srgb, data.fPos, |
| 85 | data.fCount, tm, 0, &localMatrix); |
| 86 | } |
| 87 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 88 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, |
| 89 | SkShader::TileMode, const SkMatrix& localMatrix) { |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 90 | SkPoint center; |
| 91 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 92 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 93 | return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount, |
| 94 | 0, &localMatrix); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 95 | } |
| 96 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 97 | static sk_sp<SkShader> MakeSweep4f(const SkPoint pts[2], const GradData& data, |
| 98 | SkShader::TileMode, const SkMatrix& localMatrix) { |
| 99 | SkPoint center; |
| 100 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 101 | SkScalarAve(pts[0].fY, pts[1].fY)); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 102 | auto srgb = SkColorSpace::MakeSRGBLinear(); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 103 | return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors4f, srgb, data.fPos, |
| 104 | data.fCount, 0, &localMatrix); |
| 105 | } |
| 106 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 107 | static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data, |
| 108 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 109 | SkPoint center0, center1; |
| 110 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 111 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 112 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 113 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 114 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 115 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 116 | data.fColors, data.fPos, data.fCount, tm, |
| 117 | 0, &localMatrix); |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 118 | } |
| 119 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 120 | static sk_sp<SkShader> Make2Radial4f(const SkPoint pts[2], const GradData& data, |
| 121 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 122 | SkPoint center0, center1; |
| 123 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 124 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 125 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3) / 5), |
| 126 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1) / 4)); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 127 | auto srgb = SkColorSpace::MakeSRGBLinear(); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 128 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 129 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 130 | data.fColors4f, srgb, data.fPos, data.fCount, tm, |
| 131 | 0, &localMatrix); |
| 132 | } |
| 133 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 134 | static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 135 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 136 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 137 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 138 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 139 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 140 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 141 | return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0, |
| 142 | data.fColors, data.fPos, |
| 143 | data.fCount, tm, 0, &localMatrix); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 144 | } |
| 145 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 146 | static sk_sp<SkShader> Make2Conical4f(const SkPoint pts[2], const GradData& data, |
| 147 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 148 | SkPoint center0, center1; |
| 149 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 150 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
| 151 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 152 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 153 | auto srgb = SkColorSpace::MakeSRGBLinear(); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 154 | return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0, |
| 155 | data.fColors4f, srgb, data.fPos, |
| 156 | data.fCount, tm, 0, &localMatrix); |
| 157 | } |
| 158 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 159 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 160 | SkShader::TileMode tm, const SkMatrix& localMatrix); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 161 | constexpr GradMaker gGradMakers[] = { |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 162 | MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 163 | }; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 164 | constexpr GradMaker gGradMakers4f[] ={ |
| 165 | MakeLinear4f, MakeRadial4f, MakeSweep4f, Make2Radial4f, Make2Conical4f |
| 166 | }; |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 167 | |
| 168 | /////////////////////////////////////////////////////////////////////////////// |
| 169 | |
| 170 | class GradientsGM : public GM { |
| 171 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 172 | GradientsGM(bool dither) : fDither(dither) { |
Matt Sarett | eebe87f | 2017-01-26 15:14:56 -0500 | [diff] [blame] | 173 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 174 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 175 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 176 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 177 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 178 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 179 | return SkString(fDither ? "gradients" : "gradients_nodither"); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 180 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 182 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 183 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 184 | virtual void onDraw(SkCanvas* canvas) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 185 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 186 | SkPoint pts[2] = { |
| 187 | { 0, 0 }, |
Matt Sarett | eebe87f | 2017-01-26 15:14:56 -0500 | [diff] [blame] | 188 | { SkIntToScalar(100), SkIntToScalar(100) } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 189 | }; |
| 190 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
Matt Sarett | eebe87f | 2017-01-26 15:14:56 -0500 | [diff] [blame] | 191 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 192 | SkPaint paint; |
| 193 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 194 | paint.setDither(fDither); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 195 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 196 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 197 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 198 | canvas->save(); |
| 199 | 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] | 200 | SkMatrix scale = SkMatrix::I(); |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 201 | |
| 202 | if (i == 5) { // if the clamp case |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 203 | scale.setScale(0.5f, 0.5f); |
| 204 | scale.postTranslate(25.f, 25.f); |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 205 | } |
skia.committer@gmail.com | d55e357 | 2013-08-08 07:01:20 +0000 | [diff] [blame] | 206 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 207 | paint.setShader(gGradMakers[j](pts, gGradData[i], tm, scale)); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 208 | canvas->drawRect(r, paint); |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 209 | canvas->translate(0, SkIntToScalar(120)); |
| 210 | } |
| 211 | canvas->restore(); |
| 212 | canvas->translate(SkIntToScalar(120), 0); |
| 213 | } |
| 214 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 215 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 216 | protected: |
| 217 | bool fDither; |
| 218 | |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 219 | private: |
| 220 | typedef GM INHERITED; |
| 221 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 222 | DEF_GM( return new GradientsGM(true); ) |
| 223 | DEF_GM( return new GradientsGM(false); ) |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 224 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 225 | // Like the original gradients GM, but using the SkColor4f shader factories. Should be identical. |
| 226 | class Gradients4fGM : public GM { |
| 227 | public: |
| 228 | Gradients4fGM(bool dither) : fDither(dither) { |
| 229 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
| 230 | } |
| 231 | |
| 232 | protected: |
| 233 | |
| 234 | SkString onShortName() { |
| 235 | return SkString(fDither ? "gradients4f" : "gradients4f_nodither"); |
| 236 | } |
| 237 | |
| 238 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
| 239 | |
| 240 | virtual void onDraw(SkCanvas* canvas) { |
| 241 | |
| 242 | SkPoint pts[2] ={ |
| 243 | { 0, 0 }, |
| 244 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 245 | }; |
| 246 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 247 | SkRect r ={ 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 248 | SkPaint paint; |
| 249 | paint.setAntiAlias(true); |
| 250 | paint.setDither(fDither); |
| 251 | |
| 252 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 253 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 254 | canvas->save(); |
| 255 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers4f); j++) { |
| 256 | SkMatrix scale = SkMatrix::I(); |
| 257 | |
| 258 | if (i == 5) { // if the clamp case |
| 259 | scale.setScale(0.5f, 0.5f); |
| 260 | scale.postTranslate(25.f, 25.f); |
| 261 | } |
| 262 | |
| 263 | paint.setShader(gGradMakers4f[j](pts, gGradData[i], tm, scale)); |
| 264 | canvas->drawRect(r, paint); |
| 265 | canvas->translate(0, SkIntToScalar(120)); |
| 266 | } |
| 267 | canvas->restore(); |
| 268 | canvas->translate(SkIntToScalar(120), 0); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | protected: |
| 273 | bool fDither; |
| 274 | |
| 275 | private: |
| 276 | typedef GM INHERITED; |
| 277 | }; |
| 278 | DEF_GM(return new Gradients4fGM(true); ) |
| 279 | DEF_GM(return new Gradients4fGM(false); ) |
| 280 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 281 | // Based on the original gradient slide, but with perspective applied to the |
| 282 | // gradient shaders' local matrices |
| 283 | class GradientsLocalPerspectiveGM : public GM { |
| 284 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 285 | GradientsLocalPerspectiveGM(bool dither) : fDither(dither) { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 286 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 287 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 288 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 289 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 290 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 291 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 292 | return SkString(fDither ? "gradients_local_perspective" : |
| 293 | "gradients_local_perspective_nodither"); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 294 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 295 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 296 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 297 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 298 | virtual void onDraw(SkCanvas* canvas) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 299 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 300 | SkPoint pts[2] = { |
| 301 | { 0, 0 }, |
| 302 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 303 | }; |
| 304 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 305 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 306 | SkPaint paint; |
| 307 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 308 | paint.setDither(fDither); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 309 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 310 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 311 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 312 | canvas->save(); |
| 313 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 314 | // apply an increasing y perspective as we move to the right |
| 315 | SkMatrix perspective; |
| 316 | perspective.setIdentity(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 317 | perspective.setPerspY(SkIntToScalar(i+1) / 500); |
| 318 | perspective.setSkewX(SkIntToScalar(i+1) / 10); |
commit-bot@chromium.org | 8ba1ad3 | 2013-08-07 15:22:13 +0000 | [diff] [blame] | 319 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 320 | paint.setShader(gGradMakers[j](pts, gGradData[i], tm, perspective)); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 321 | canvas->drawRect(r, paint); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 322 | canvas->translate(0, SkIntToScalar(120)); |
| 323 | } |
| 324 | canvas->restore(); |
| 325 | canvas->translate(SkIntToScalar(120), 0); |
| 326 | } |
| 327 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 328 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 329 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 330 | bool fDither; |
| 331 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 332 | typedef GM INHERITED; |
| 333 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 334 | DEF_GM( return new GradientsLocalPerspectiveGM(true); ) |
| 335 | DEF_GM( return new GradientsLocalPerspectiveGM(false); ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 336 | |
| 337 | // Based on the original gradient slide, but with perspective applied to |
| 338 | // the view matrix |
| 339 | class GradientsViewPerspectiveGM : public GradientsGM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 340 | public: |
| 341 | GradientsViewPerspectiveGM(bool dither) : INHERITED(dither) { } |
| 342 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 343 | protected: |
| 344 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 345 | return SkString(fDither ? "gradients_view_perspective" : |
| 346 | "gradients_view_perspective_nodither"); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 347 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 348 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 349 | virtual SkISize onISize() { return SkISize::Make(840, 500); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 350 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 351 | virtual void onDraw(SkCanvas* canvas) { |
| 352 | SkMatrix perspective; |
| 353 | perspective.setIdentity(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 354 | perspective.setPerspY(0.001f); |
| 355 | perspective.setSkewX(SkIntToScalar(8) / 25); |
scroggo@google.com | 837d31a | 2012-08-15 18:42:45 +0000 | [diff] [blame] | 356 | canvas->concat(perspective); |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 357 | INHERITED::onDraw(canvas); |
| 358 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 359 | |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 360 | private: |
| 361 | typedef GradientsGM INHERITED; |
| 362 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 363 | DEF_GM( return new GradientsViewPerspectiveGM(true); ) |
| 364 | DEF_GM( return new GradientsViewPerspectiveGM(false); ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 365 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 366 | /* |
| 367 | Inspired by this <canvas> javascript, where we need to detect that we are not |
| 368 | solving a quadratic equation, but must instead solve a linear (since our X^2 |
| 369 | coefficient is 0) |
| 370 | |
| 371 | ctx.fillStyle = '#f00'; |
| 372 | ctx.fillRect(0, 0, 100, 50); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 373 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 374 | var g = ctx.createRadialGradient(-80, 25, 70, 0, 25, 150); |
| 375 | g.addColorStop(0, '#f00'); |
| 376 | g.addColorStop(0.01, '#0f0'); |
| 377 | g.addColorStop(0.99, '#0f0'); |
| 378 | g.addColorStop(1, '#f00'); |
| 379 | ctx.fillStyle = g; |
| 380 | ctx.fillRect(0, 0, 100, 50); |
| 381 | */ |
| 382 | class GradientsDegenrate2PointGM : public GM { |
| 383 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 384 | GradientsDegenrate2PointGM(bool dither) : fDither(dither) {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 385 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 386 | protected: |
| 387 | SkString onShortName() { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 388 | return SkString(fDither ? "gradients_degenerate_2pt" : "gradients_degenerate_2pt_nodither"); |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 389 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 390 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 391 | virtual SkISize onISize() { return SkISize::Make(320, 320); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 392 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 393 | void drawBG(SkCanvas* canvas) { |
| 394 | canvas->drawColor(SK_ColorBLUE); |
| 395 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 396 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 397 | virtual void onDraw(SkCanvas* canvas) { |
| 398 | this->drawBG(canvas); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 399 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 400 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorRED }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 401 | SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 }; |
epoger@google.com | 59f3abf | 2011-07-21 15:50:33 +0000 | [diff] [blame] | 402 | SkPoint c0; |
| 403 | c0.iset(-80, 25); |
| 404 | SkScalar r0 = SkIntToScalar(70); |
| 405 | SkPoint c1; |
| 406 | c1.iset(0, 25); |
| 407 | SkScalar r1 = SkIntToScalar(150); |
reed | 9283d20 | 2016-03-13 13:01:57 -0700 | [diff] [blame] | 408 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 409 | paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors, |
| 410 | pos, SK_ARRAY_COUNT(pos), |
| 411 | SkShader::kClamp_TileMode)); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 412 | paint.setDither(fDither); |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 413 | canvas->drawPaint(paint); |
| 414 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 415 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 416 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 417 | bool fDither; |
| 418 | |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 419 | typedef GM INHERITED; |
| 420 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 421 | DEF_GM( return new GradientsDegenrate2PointGM(true); ) |
| 422 | DEF_GM( return new GradientsDegenrate2PointGM(false); ) |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 423 | |
caryclark | cb10071 | 2016-02-26 05:59:40 -0800 | [diff] [blame] | 424 | /* bug.skia.org/517 |
| 425 | <canvas id="canvas"></canvas> |
| 426 | <script> |
| 427 | var c = document.getElementById("canvas"); |
| 428 | var ctx = c.getContext("2d"); |
| 429 | ctx.fillStyle = '#ff0'; |
| 430 | ctx.fillRect(0, 0, 100, 50); |
| 431 | |
| 432 | var g = ctx.createRadialGradient(200, 25, 20, 200, 25, 10); |
| 433 | g.addColorStop(0, '#0f0'); |
| 434 | g.addColorStop(0.003, '#f00'); // 0.004 makes this work |
| 435 | g.addColorStop(1, '#ff0'); |
| 436 | ctx.fillStyle = g; |
| 437 | ctx.fillRect(0, 0, 100, 50); |
| 438 | </script> |
| 439 | */ |
| 440 | |
| 441 | // should draw only green |
| 442 | DEF_SIMPLE_GM(small_color_stop, canvas, 100, 150) { |
| 443 | SkColor colors[] = { SK_ColorGREEN, SK_ColorRED, SK_ColorYELLOW }; |
| 444 | SkScalar pos[] = { 0, 0.003f, SK_Scalar1 }; // 0.004f makes this work |
| 445 | SkPoint c0 = { 200, 25 }; |
| 446 | SkScalar r0 = 20; |
| 447 | SkPoint c1 = { 200, 25 }; |
| 448 | SkScalar r1 = 10; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 449 | |
caryclark | cb10071 | 2016-02-26 05:59:40 -0800 | [diff] [blame] | 450 | SkPaint paint; |
| 451 | paint.setColor(SK_ColorYELLOW); |
| 452 | canvas->drawRect(SkRect::MakeWH(100, 150), paint); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 453 | paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors, pos, |
| 454 | SK_ARRAY_COUNT(pos), |
| 455 | SkShader::kClamp_TileMode)); |
caryclark | cb10071 | 2016-02-26 05:59:40 -0800 | [diff] [blame] | 456 | canvas->drawRect(SkRect::MakeWH(100, 150), paint); |
| 457 | } |
| 458 | |
| 459 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 460 | /// Tests correctness of *optimized* codepaths in gradients. |
| 461 | |
| 462 | class ClampedGradientsGM : public GM { |
| 463 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 464 | ClampedGradientsGM(bool dither) : fDither(dither) {} |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 465 | |
| 466 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 467 | SkString onShortName() { |
| 468 | return SkString(fDither ? "clamped_gradients" : "clamped_gradients_nodither"); |
| 469 | } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 470 | |
edisonn@google.com | 1da3a80 | 2013-09-19 17:55:49 +0000 | [diff] [blame] | 471 | virtual SkISize onISize() { return SkISize::Make(640, 510); } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 472 | |
| 473 | void drawBG(SkCanvas* canvas) { |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 474 | canvas->drawColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | virtual void onDraw(SkCanvas* canvas) { |
| 478 | this->drawBG(canvas); |
| 479 | |
| 480 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) }; |
| 481 | SkPaint paint; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 482 | paint.setDither(fDither); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 483 | paint.setAntiAlias(true); |
| 484 | |
| 485 | SkPoint center; |
| 486 | center.iset(0, 300); |
| 487 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 488 | paint.setShader(SkGradientShader::MakeRadial( |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 489 | SkPoint(center), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 490 | SkIntToScalar(200), gColors, nullptr, 5, |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 491 | SkShader::kClamp_TileMode)); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 492 | canvas->drawRect(r, paint); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 496 | bool fDither; |
| 497 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 498 | typedef GM INHERITED; |
| 499 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 500 | DEF_GM( return new ClampedGradientsGM(true); ) |
| 501 | DEF_GM( return new ClampedGradientsGM(false); ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 502 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 503 | /// Checks quality of large radial gradients, which may display |
| 504 | /// some banding. |
| 505 | |
| 506 | class RadialGradientGM : public GM { |
| 507 | public: |
| 508 | RadialGradientGM() {} |
| 509 | |
| 510 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 511 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 512 | SkString onShortName() override { return SkString("radial_gradient"); } |
| 513 | SkISize onISize() override { return SkISize::Make(1280, 1280); } |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 514 | void drawBG(SkCanvas* canvas) { |
| 515 | canvas->drawColor(0xFF000000); |
| 516 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 517 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 518 | const SkISize dim = this->getISize(); |
| 519 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 520 | this->drawBG(canvas); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 521 | |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 522 | SkPaint paint; |
| 523 | paint.setDither(true); |
| 524 | SkPoint center; |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 525 | center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2); |
| 526 | SkScalar radius = SkIntToScalar(dim.width())/2; |
| 527 | const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 528 | const SkScalar pos[] = { 0.0f, |
| 529 | 0.35f, |
| 530 | 1.0f }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 531 | paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos, |
| 532 | SK_ARRAY_COUNT(pos), |
| 533 | SkShader::kClamp_TileMode)); |
reed@google.com | bb0948f | 2012-01-31 14:44:13 +0000 | [diff] [blame] | 534 | SkRect r = { |
| 535 | 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height()) |
| 536 | }; |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 537 | canvas->drawRect(r, paint); |
| 538 | } |
| 539 | private: |
| 540 | typedef GM INHERITED; |
| 541 | }; |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 542 | DEF_GM( return new RadialGradientGM; ) |
tomhudson@google.com | b18e58c | 2012-01-30 20:00:13 +0000 | [diff] [blame] | 543 | |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 544 | class RadialGradient2GM : public GM { |
| 545 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 546 | RadialGradient2GM(bool dither) : fDither(dither) {} |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 547 | |
| 548 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 549 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 550 | SkString onShortName() override { |
| 551 | return SkString(fDither ? "radial_gradient2" : "radial_gradient2_nodither"); |
| 552 | } |
| 553 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 554 | SkISize onISize() override { return SkISize::Make(800, 400); } |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 555 | void drawBG(SkCanvas* canvas) { |
| 556 | canvas->drawColor(0xFF000000); |
| 557 | } |
| 558 | |
| 559 | // Reproduces the example given in bug 7671058. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 560 | void onDraw(SkCanvas* canvas) override { |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 561 | SkPaint paint1, paint2, paint3; |
| 562 | paint1.setStyle(SkPaint::kFill_Style); |
| 563 | paint2.setStyle(SkPaint::kFill_Style); |
| 564 | paint3.setStyle(SkPaint::kFill_Style); |
| 565 | |
| 566 | const SkColor sweep_colors[] = |
| 567 | { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000 }; |
| 568 | const SkColor colors1[] = { 0xFFFFFFFF, 0x00000000 }; |
| 569 | const SkColor colors2[] = { 0xFF000000, 0x00000000 }; |
| 570 | |
| 571 | const SkScalar cx = 200, cy = 200, radius = 150; |
| 572 | SkPoint center; |
| 573 | center.set(cx, cy); |
| 574 | |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 575 | // We can either interpolate endpoints and premultiply each point (default, more precision), |
| 576 | // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap). |
| 577 | const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPremul_Flag }; |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 578 | |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 579 | for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 580 | paint1.setShader(SkGradientShader::MakeSweep(cx, cy, sweep_colors, |
| 581 | nullptr, SK_ARRAY_COUNT(sweep_colors), |
| 582 | flags[i], nullptr)); |
| 583 | paint2.setShader(SkGradientShader::MakeRadial(center, radius, colors1, |
| 584 | nullptr, SK_ARRAY_COUNT(colors1), |
| 585 | SkShader::kClamp_TileMode, |
| 586 | flags[i], nullptr)); |
| 587 | paint3.setShader(SkGradientShader::MakeRadial(center, radius, colors2, |
| 588 | nullptr, SK_ARRAY_COUNT(colors2), |
| 589 | SkShader::kClamp_TileMode, |
| 590 | flags[i], nullptr)); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 591 | paint1.setDither(fDither); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 592 | paint2.setDither(fDither); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 593 | paint3.setDither(fDither); |
mtklein@google.com | 3ef7eea | 2013-09-16 13:02:52 +0000 | [diff] [blame] | 594 | |
| 595 | canvas->drawCircle(cx, cy, radius, paint1); |
| 596 | canvas->drawCircle(cx, cy, radius, paint3); |
| 597 | canvas->drawCircle(cx, cy, radius, paint2); |
| 598 | |
| 599 | canvas->translate(400, 0); |
| 600 | } |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | private: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 604 | bool fDither; |
| 605 | |
mtklein@google.com | 361a72f | 2013-08-19 18:43:34 +0000 | [diff] [blame] | 606 | typedef GM INHERITED; |
| 607 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 608 | DEF_GM( return new RadialGradient2GM(true); ) |
| 609 | DEF_GM( return new RadialGradient2GM(false); ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 610 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 611 | // Shallow radial (shows banding on raster) |
| 612 | class RadialGradient3GM : public GM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 613 | public: |
| 614 | RadialGradient3GM(bool dither) : fDither(dither) { } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 615 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 616 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 617 | SkString onShortName() override { |
| 618 | return SkString(fDither ? "radial_gradient3" : "radial_gradient3_nodither"); |
| 619 | } |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 620 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 621 | SkISize onISize() override { return SkISize::Make(500, 500); } |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 622 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 623 | bool runAsBench() const override { return true; } |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 624 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 625 | void onOnceBeforeDraw() override { |
| 626 | const SkPoint center = { 0, 0 }; |
| 627 | const SkScalar kRadius = 3000; |
| 628 | const SkColor gColors[] = { 0xFFFFFFFF, 0xFF000000 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 629 | fShader = SkGradientShader::MakeRadial(center, kRadius, gColors, nullptr, 2, |
| 630 | SkShader::kClamp_TileMode); |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 631 | } |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 632 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 633 | void onDraw(SkCanvas* canvas) override { |
| 634 | SkPaint paint; |
| 635 | paint.setShader(fShader); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 636 | paint.setDither(fDither); |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 637 | canvas->drawRect(SkRect::MakeWH(500, 500), paint); |
| 638 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 639 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 640 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 641 | sk_sp<SkShader> fShader; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 642 | bool fDither; |
| 643 | |
reed | 3d9005c | 2015-04-23 10:30:27 -0700 | [diff] [blame] | 644 | typedef GM INHERITED; |
| 645 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 646 | DEF_GM( return new RadialGradient3GM(true); ) |
| 647 | DEF_GM( return new RadialGradient3GM(false); ) |
rileya@google.com | 5cf2c28 | 2012-07-09 14:42:16 +0000 | [diff] [blame] | 648 | |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 649 | class RadialGradient4GM : public GM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 650 | public: |
| 651 | RadialGradient4GM(bool dither) : fDither(dither) { } |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 652 | |
| 653 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 654 | SkString onShortName() override { |
| 655 | return SkString(fDither ? "radial_gradient4" : "radial_gradient4_nodither"); |
| 656 | } |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 657 | |
| 658 | SkISize onISize() override { return SkISize::Make(500, 500); } |
| 659 | |
| 660 | void onOnceBeforeDraw() override { |
| 661 | const SkPoint center = { 250, 250 }; |
| 662 | const SkScalar kRadius = 250; |
| 663 | const SkColor colors[] = { SK_ColorRED, SK_ColorRED, SK_ColorWHITE, SK_ColorWHITE, |
| 664 | SK_ColorRED }; |
| 665 | const SkScalar pos[] = { 0, .4f, .4f, .8f, .8f, 1 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 666 | fShader = SkGradientShader::MakeRadial(center, kRadius, colors, pos, |
| 667 | SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode); |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void onDraw(SkCanvas* canvas) override { |
| 671 | SkPaint paint; |
| 672 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 673 | paint.setDither(fDither); |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 674 | paint.setShader(fShader); |
| 675 | canvas->drawRect(SkRect::MakeWH(500, 500), paint); |
| 676 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 677 | |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 678 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 679 | sk_sp<SkShader> fShader; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 680 | bool fDither; |
| 681 | |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 682 | typedef GM INHERITED; |
| 683 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 684 | DEF_GM( return new RadialGradient4GM(true); ) |
| 685 | DEF_GM( return new RadialGradient4GM(false); ) |
caryclark | 1864bfa | 2015-07-30 06:41:39 -0700 | [diff] [blame] | 686 | |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 687 | class LinearGradientGM : public GM { |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 688 | public: |
| 689 | LinearGradientGM(bool dither) : fDither(dither) { } |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 690 | |
| 691 | protected: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 692 | SkString onShortName() override { |
| 693 | return SkString(fDither ? "linear_gradient" : "linear_gradient_nodither"); |
| 694 | } |
| 695 | |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 696 | const SkScalar kWidthBump = 30.f; |
| 697 | const SkScalar kHeight = 5.f; |
| 698 | const SkScalar kMinWidth = 540.f; |
| 699 | |
| 700 | SkISize onISize() override { return SkISize::Make(500, 500); } |
| 701 | |
| 702 | void onOnceBeforeDraw() override { |
| 703 | SkPoint pts[2] = { {0, 0}, {0, 0} }; |
| 704 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorWHITE, 0xFF008200, 0xFF008200, |
| 705 | SK_ColorWHITE, SK_ColorWHITE }; |
| 706 | const SkScalar unitPos[] = { 0, 50, 70, 500, 540 }; |
| 707 | SkScalar pos[6]; |
| 708 | pos[5] = 1; |
| 709 | for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) { |
| 710 | pts[1].fX = 500.f + index * kWidthBump; |
| 711 | for (int inner = 0; inner < (int) SK_ARRAY_COUNT(unitPos); ++inner) { |
| 712 | pos[inner] = unitPos[inner] / (kMinWidth + index * kWidthBump); |
| 713 | } |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 714 | fShader[index] = SkGradientShader::MakeLinear(pts, colors, pos, |
| 715 | SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode); |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
| 719 | void onDraw(SkCanvas* canvas) override { |
| 720 | SkPaint paint; |
| 721 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 722 | paint.setDither(fDither); |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 723 | for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) { |
| 724 | paint.setShader(fShader[index]); |
| 725 | canvas->drawRect(SkRect::MakeLTRB(0, index * kHeight, kMinWidth + index * kWidthBump, |
| 726 | (index + 1) * kHeight), paint); |
| 727 | } |
| 728 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 729 | |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 730 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 731 | sk_sp<SkShader> fShader[100]; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 732 | bool fDither; |
| 733 | |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 734 | typedef GM INHERITED; |
| 735 | }; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 736 | DEF_GM( return new LinearGradientGM(true); ) |
| 737 | DEF_GM( return new LinearGradientGM(false); ) |
caryclark | 159fa57 | 2015-07-30 12:35:48 -0700 | [diff] [blame] | 738 | |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 739 | class LinearGradientTinyGM : public GM { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 740 | public: |
| 741 | LinearGradientTinyGM(uint32_t flags, const char* suffix = nullptr) |
| 742 | : fName("linear_gradient_tiny") |
| 743 | , fFlags(flags) { |
| 744 | fName.append(suffix); |
| 745 | } |
| 746 | |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 747 | protected: |
| 748 | SkString onShortName() override { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 749 | return fName; |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | SkISize onISize() override { |
| 753 | return SkISize::Make(600, 500); |
| 754 | } |
| 755 | |
| 756 | void onDraw(SkCanvas* canvas) override { |
| 757 | const SkScalar kRectSize = 100; |
| 758 | const unsigned kStopCount = 3; |
| 759 | const SkColor colors[kStopCount] = { SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN }; |
| 760 | const struct { |
| 761 | SkPoint pts[2]; |
| 762 | SkScalar pos[kStopCount]; |
| 763 | } configs[] = { |
reed | de3aac8 | 2015-11-22 13:00:04 -0800 | [diff] [blame] | 764 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999f, 1 }}, |
| 765 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000001f, 1 }}, |
| 766 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999999f, 1 }}, |
| 767 | { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000000001f, 1 }}, |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 768 | |
reed | de3aac8 | 2015-11-22 13:00:04 -0800 | [diff] [blame] | 769 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999f, 1 }}, |
| 770 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000001f, 1 }}, |
| 771 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999999f, 1 }}, |
| 772 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000000001f, 1 }}, |
| 773 | |
| 774 | { { SkPoint::Make(0, 0), SkPoint::Make(0.00001f, 0) }, { 0, 0.5f, 1 }}, |
| 775 | { { SkPoint::Make(9.99999f, 0), SkPoint::Make(10, 0) }, { 0, 0.5f, 1 }}, |
| 776 | { { SkPoint::Make(0, 0), SkPoint::Make(0, 0.00001f) }, { 0, 0.5f, 1 }}, |
| 777 | { { SkPoint::Make(0, 9.99999f), SkPoint::Make(0, 10) }, { 0, 0.5f, 1 }}, |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 778 | }; |
| 779 | |
| 780 | SkPaint paint; |
| 781 | for (unsigned i = 0; i < SK_ARRAY_COUNT(configs); ++i) { |
| 782 | SkAutoCanvasRestore acr(canvas, true); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 783 | paint.setShader(SkGradientShader::MakeLinear(configs[i].pts, colors, configs[i].pos, |
| 784 | kStopCount, SkShader::kClamp_TileMode, |
| 785 | fFlags, nullptr)); |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 786 | canvas->translate(kRectSize * ((i % 4) * 1.5f + 0.25f), |
| 787 | kRectSize * ((i / 4) * 1.5f + 0.25f)); |
| 788 | |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 789 | canvas->drawRect(SkRect::MakeWH(kRectSize, kRectSize), paint); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | private: |
| 794 | typedef GM INHERITED; |
fmalita | 8b78bd6 | 2015-11-20 13:58:24 -0800 | [diff] [blame] | 795 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 796 | SkString fName; |
| 797 | uint32_t fFlags; |
| 798 | }; |
| 799 | DEF_GM( return new LinearGradientTinyGM(0); ) |
reed@android.com | 42309d4 | 2009-06-22 02:06:35 +0000 | [diff] [blame] | 800 | } |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 801 | |
| 802 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 803 | |
| 804 | struct GradRun { |
| 805 | SkColor fColors[4]; |
| 806 | SkScalar fPos[4]; |
| 807 | int fCount; |
| 808 | }; |
| 809 | |
| 810 | #define SIZE 121 |
| 811 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 812 | static sk_sp<SkShader> make_linear(const GradRun& run, SkShader::TileMode mode) { |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 813 | const SkPoint pts[] { { 30, 30 }, { SIZE - 30, SIZE - 30 } }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 814 | return SkGradientShader::MakeLinear(pts, run.fColors, run.fPos, run.fCount, mode); |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 815 | } |
| 816 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 817 | static sk_sp<SkShader> make_radial(const GradRun& run, SkShader::TileMode mode) { |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 818 | const SkScalar half = SIZE * 0.5f; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 819 | return SkGradientShader::MakeRadial({half,half}, half - 10, run.fColors, run.fPos, |
| 820 | run.fCount, mode); |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 821 | } |
| 822 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 823 | static sk_sp<SkShader> make_conical(const GradRun& run, SkShader::TileMode mode) { |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 824 | const SkScalar half = SIZE * 0.5f; |
| 825 | const SkPoint center { half, half }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 826 | return SkGradientShader::MakeTwoPointConical(center, 20, center, half - 10, |
| 827 | run.fColors, run.fPos, run.fCount, mode); |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 828 | } |
| 829 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 830 | static sk_sp<SkShader> make_sweep(const GradRun& run, SkShader::TileMode) { |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 831 | const SkScalar half = SIZE * 0.5f; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 832 | return SkGradientShader::MakeSweep(half, half, run.fColors, run.fPos, run.fCount); |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Exercise duplicate color-stops, at the ends, and in the middle |
| 837 | * |
| 838 | * At the time of this writing, only Linear correctly deals with duplicates at the ends, |
| 839 | * and then only correctly on CPU backend. |
| 840 | */ |
| 841 | DEF_SIMPLE_GM(gradients_dup_color_stops, canvas, 704, 564) { |
| 842 | const SkColor preColor = 0xFFFF0000; // clamp color before start |
| 843 | const SkColor postColor = 0xFF0000FF; // clamp color after end |
| 844 | const SkColor color0 = 0xFF000000; |
| 845 | const SkColor color1 = 0xFF00FF00; |
| 846 | const SkColor badColor = 0xFF3388BB; // should never be seen, fills out fixed-size array |
| 847 | |
| 848 | const GradRun runs[] = { |
| 849 | { { color0, color1, badColor, badColor }, |
| 850 | { 0, 1, -1, -1 }, |
| 851 | 2, |
| 852 | }, |
| 853 | { { preColor, color0, color1, badColor }, |
| 854 | { 0, 0, 1, -1 }, |
| 855 | 3, |
| 856 | }, |
| 857 | { { color0, color1, postColor, badColor }, |
| 858 | { 0, 1, 1, -1 }, |
| 859 | 3, |
| 860 | }, |
| 861 | { { preColor, color0, color1, postColor }, |
| 862 | { 0, 0, 1, 1 }, |
| 863 | 4, |
| 864 | }, |
| 865 | { { color0, color0, color1, color1 }, |
| 866 | { 0, 0.5f, 0.5f, 1 }, |
| 867 | 4, |
| 868 | }, |
| 869 | }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 870 | sk_sp<SkShader> (*factories[])(const GradRun&, SkShader::TileMode) { |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 871 | make_linear, make_radial, make_conical, make_sweep |
| 872 | }; |
| 873 | |
| 874 | const SkRect rect = SkRect::MakeWH(SIZE, SIZE); |
| 875 | const SkScalar dx = SIZE + 20; |
| 876 | const SkScalar dy = SIZE + 20; |
| 877 | const SkShader::TileMode mode = SkShader::kClamp_TileMode; |
| 878 | |
| 879 | SkPaint paint; |
| 880 | canvas->translate(10, 10 - dy); |
| 881 | for (auto factory : factories) { |
| 882 | canvas->translate(0, dy); |
| 883 | SkAutoCanvasRestore acr(canvas, true); |
| 884 | for (const auto& run : runs) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 885 | paint.setShader(factory(run, mode)); |
reed | d4eaa25 | 2016-01-22 10:35:26 -0800 | [diff] [blame] | 886 | canvas->drawRect(rect, paint); |
| 887 | canvas->translate(dx, 0); |
| 888 | } |
| 889 | } |
| 890 | } |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 891 | |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 892 | static void draw_many_stops(SkCanvas* canvas) { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 893 | const unsigned kStopCount = 200; |
| 894 | const SkPoint pts[] = { {50, 50}, {450, 465}}; |
| 895 | |
| 896 | SkColor colors[kStopCount]; |
| 897 | for (unsigned i = 0; i < kStopCount; i++) { |
| 898 | switch (i % 5) { |
| 899 | case 0: colors[i] = SK_ColorRED; break; |
| 900 | case 1: colors[i] = SK_ColorGREEN; break; |
| 901 | case 2: colors[i] = SK_ColorGREEN; break; |
| 902 | case 3: colors[i] = SK_ColorBLUE; break; |
| 903 | case 4: colors[i] = SK_ColorRED; break; |
| 904 | } |
| 905 | } |
| 906 | |
reed | 9283d20 | 2016-03-13 13:01:57 -0700 | [diff] [blame] | 907 | SkPaint p; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 908 | p.setShader(SkGradientShader::MakeLinear( |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 909 | pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode)); |
reed | 9283d20 | 2016-03-13 13:01:57 -0700 | [diff] [blame] | 910 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 911 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p); |
| 912 | } |
| 913 | |
| 914 | DEF_SIMPLE_GM(gradient_many_stops, canvas, 500, 500) { |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 915 | draw_many_stops(canvas); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 916 | } |
| 917 | |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 918 | static void draw_subpixel_gradient(SkCanvas* canvas) { |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 919 | const SkPoint pts[] = { {50, 50}, {50.1f, 50.1f}}; |
| 920 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 921 | SkPaint p; |
| 922 | p.setShader(SkGradientShader::MakeLinear( |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 923 | pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kRepeat_TileMode)); |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 924 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p); |
| 925 | } |
| 926 | |
| 927 | DEF_SIMPLE_GM(gradient_subpixel, canvas, 500, 500) { |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 928 | draw_subpixel_gradient(canvas); |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 929 | } |
Florin Malita | a0ac963 | 2017-05-03 13:07:28 -0400 | [diff] [blame] | 930 | |
| 931 | #include "SkPictureRecorder.h" |
| 932 | |
| 933 | static void draw_circle_shader(SkCanvas* canvas, SkScalar cx, SkScalar cy, SkScalar r, |
| 934 | sk_sp<SkShader> (*shaderFunc)()) { |
| 935 | SkPaint p; |
| 936 | p.setAntiAlias(true); |
| 937 | p.setShader(shaderFunc()); |
| 938 | canvas->drawCircle(cx, cy, r, p); |
| 939 | |
| 940 | p.setShader(nullptr); |
| 941 | p.setColor(SK_ColorGRAY); |
| 942 | p.setStyle(SkPaint::kStroke_Style); |
| 943 | p.setStrokeWidth(2); |
| 944 | canvas->drawCircle(cx, cy, r, p); |
| 945 | } |
| 946 | |
| 947 | DEF_SIMPLE_GM(fancy_gradients, canvas, 800, 300) { |
| 948 | draw_circle_shader(canvas, 150, 150, 100, []() -> sk_sp<SkShader> { |
| 949 | // Checkerboard using two linear gradients + picture shader. |
| 950 | SkScalar kTileSize = 80 / sqrtf(2); |
| 951 | SkColor colors1[] = { 0xff000000, 0xff000000, |
| 952 | 0xffffffff, 0xffffffff, |
| 953 | 0xff000000, 0xff000000 }; |
| 954 | SkColor colors2[] = { 0xff000000, 0xff000000, |
| 955 | 0x00000000, 0x00000000, |
| 956 | 0xff000000, 0xff000000 }; |
| 957 | SkScalar pos[] = { 0, .25f, .25f, .75f, .75f, 1 }; |
| 958 | static_assert(SK_ARRAY_COUNT(colors1) == SK_ARRAY_COUNT(pos), "color/pos size mismatch"); |
| 959 | static_assert(SK_ARRAY_COUNT(colors2) == SK_ARRAY_COUNT(pos), "color/pos size mismatch"); |
| 960 | |
| 961 | SkPictureRecorder recorder; |
| 962 | recorder.beginRecording(SkRect::MakeWH(kTileSize, kTileSize)); |
| 963 | |
| 964 | SkPaint p; |
| 965 | |
| 966 | SkPoint pts1[] = { { 0, 0 }, { kTileSize, kTileSize }}; |
| 967 | p.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos, SK_ARRAY_COUNT(colors1), |
| 968 | SkShader::kClamp_TileMode, 0, nullptr)); |
| 969 | recorder.getRecordingCanvas()->drawPaint(p); |
| 970 | |
| 971 | SkPoint pts2[] = { { 0, kTileSize }, { kTileSize, 0 }}; |
| 972 | p.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos, SK_ARRAY_COUNT(colors2), |
| 973 | SkShader::kClamp_TileMode, 0, nullptr)); |
| 974 | recorder.getRecordingCanvas()->drawPaint(p); |
| 975 | |
| 976 | SkMatrix m = SkMatrix::I(); |
| 977 | m.preRotate(45); |
| 978 | return SkShader::MakePictureShader(recorder.finishRecordingAsPicture(), |
| 979 | SkShader::kRepeat_TileMode, |
| 980 | SkShader::kRepeat_TileMode, &m, nullptr); |
| 981 | }); |
| 982 | |
| 983 | draw_circle_shader(canvas, 400, 150, 100, []() -> sk_sp<SkShader> { |
| 984 | // Checkerboard using a sweep gradient + picture shader. |
| 985 | SkScalar kTileSize = 80; |
| 986 | SkColor colors[] = { 0xff000000, 0xff000000, |
| 987 | 0xffffffff, 0xffffffff, |
| 988 | 0xff000000, 0xff000000, |
| 989 | 0xffffffff, 0xffffffff }; |
| 990 | SkScalar pos[] = { 0, .25f, .25f, .5f, .5f, .75f, .75f, 1 }; |
| 991 | static_assert(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos), "color/pos size mismatch"); |
| 992 | |
| 993 | SkPaint p; |
| 994 | p.setShader(SkGradientShader::MakeSweep(kTileSize / 2, kTileSize / 2, |
| 995 | colors, pos, SK_ARRAY_COUNT(colors), 0, nullptr)); |
| 996 | SkPictureRecorder recorder; |
| 997 | recorder.beginRecording(SkRect::MakeWH(kTileSize, kTileSize))->drawPaint(p); |
| 998 | |
| 999 | return SkShader::MakePictureShader(recorder.finishRecordingAsPicture(), |
| 1000 | SkShader::kRepeat_TileMode, |
| 1001 | SkShader::kRepeat_TileMode, nullptr, nullptr); |
| 1002 | }); |
| 1003 | |
| 1004 | draw_circle_shader(canvas, 650, 150, 100, []() -> sk_sp<SkShader> { |
| 1005 | // Dartboard using sweep + radial. |
| 1006 | const SkColor a = 0xffffffff; |
| 1007 | const SkColor b = 0xff000000; |
| 1008 | SkColor colors[] = { a, a, b, b, a, a, b, b, a, a, b, b, a, a, b, b}; |
| 1009 | SkScalar pos[] = { 0, .125f, .125f, .25f, .25f, .375f, .375f, .5f, .5f, |
| 1010 | .625f, .625f, .75f, .75f, .875f, .875f, 1}; |
| 1011 | static_assert(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos), "color/pos size mismatch"); |
| 1012 | |
| 1013 | SkPoint center = { 650, 150 }; |
| 1014 | sk_sp<SkShader> sweep1 = SkGradientShader::MakeSweep(center.x(), center.y(), colors, pos, |
| 1015 | SK_ARRAY_COUNT(colors), 0, nullptr); |
| 1016 | SkMatrix m = SkMatrix::I(); |
| 1017 | m.preRotate(22.5f, center.x(), center.y()); |
| 1018 | sk_sp<SkShader> sweep2 = SkGradientShader::MakeSweep(center.x(), center.y(), colors, pos, |
| 1019 | SK_ARRAY_COUNT(colors), 0, &m); |
| 1020 | |
| 1021 | sk_sp<SkShader> sweep(SkShader::MakeComposeShader(sweep1, sweep2, SkBlendMode::kExclusion)); |
| 1022 | |
| 1023 | SkScalar radialPos[] = { 0, .02f, .02f, .04f, .04f, .08f, .08f, .16f, .16f, .31f, .31f, |
| 1024 | .62f, .62f, 1, 1, 1 }; |
| 1025 | static_assert(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(radialPos), |
| 1026 | "color/pos size mismatch"); |
| 1027 | |
| 1028 | return SkShader::MakeComposeShader(sweep, |
| 1029 | SkGradientShader::MakeRadial(center, 100, colors, |
| 1030 | radialPos, |
| 1031 | SK_ARRAY_COUNT(radialPos), |
| 1032 | SkShader::kClamp_TileMode), |
| 1033 | SkBlendMode::kExclusion); |
| 1034 | }); |
| 1035 | } |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 1036 | |
Florin Malita | 4d27429 | 2017-08-02 10:04:48 -0400 | [diff] [blame] | 1037 | DEF_SIMPLE_GM(sweep_tiling, canvas, 690, 512) { |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 1038 | static constexpr SkScalar size = 160; |
| 1039 | static constexpr SkColor colors[] = { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN }; |
| 1040 | static constexpr SkScalar pos[] = { 0, .25f, .50f }; |
| 1041 | static_assert(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos), "size mismatch"); |
| 1042 | |
| 1043 | static constexpr SkShader::TileMode modes[] = { SkShader::kClamp_TileMode, |
| 1044 | SkShader::kRepeat_TileMode, |
| 1045 | SkShader::kMirror_TileMode }; |
| 1046 | |
| 1047 | static const struct { |
| 1048 | SkScalar start, end; |
| 1049 | } angles[] = { |
| 1050 | { -330, -270 }, |
| 1051 | { 30, 90 }, |
| 1052 | { 390, 450 }, |
Florin Malita | 50f7a1e | 2017-08-02 09:40:26 -0400 | [diff] [blame] | 1053 | { -30, 800 }, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 1054 | }; |
| 1055 | |
| 1056 | SkPaint p; |
| 1057 | const SkRect r = SkRect::MakeWH(size, size); |
| 1058 | |
| 1059 | for (auto mode : modes) { |
| 1060 | { |
| 1061 | SkAutoCanvasRestore acr(canvas, true); |
| 1062 | |
| 1063 | for (auto angle : angles) { |
| 1064 | p.setShader(SkGradientShader::MakeSweep(size / 2, size / 2, colors, pos, |
| 1065 | SK_ARRAY_COUNT(colors), mode, |
| 1066 | angle.start, angle.end, 0, nullptr)); |
| 1067 | |
| 1068 | canvas->drawRect(r, p); |
| 1069 | canvas->translate(size * 1.1f, 0); |
| 1070 | } |
| 1071 | } |
| 1072 | canvas->translate(0, size * 1.1f); |
| 1073 | } |
| 1074 | } |
Florin Malita | 36f054a | 2017-08-03 12:55:41 -0400 | [diff] [blame] | 1075 | |
| 1076 | // Exercises the special-case Ganesh gradient effects. |
Brian Osman | a8e5744 | 2017-09-11 17:21:35 -0400 | [diff] [blame] | 1077 | DEF_SIMPLE_GM(gradients_interesting, canvas, 640, 1300) { |
Florin Malita | 36f054a | 2017-08-03 12:55:41 -0400 | [diff] [blame] | 1078 | static const SkColor colors2[] = { SK_ColorRED, SK_ColorBLUE }; |
| 1079 | static const SkColor colors3[] = { SK_ColorRED, SK_ColorYELLOW, SK_ColorBLUE }; |
| 1080 | static const SkColor colors4[] = { SK_ColorRED, SK_ColorYELLOW, SK_ColorYELLOW, SK_ColorBLUE }; |
| 1081 | |
Brian Osman | a8e5744 | 2017-09-11 17:21:35 -0400 | [diff] [blame] | 1082 | static const SkScalar softRight[] = { 0, .999f, 1 }; // Based on Android launcher "clipping" |
| 1083 | static const SkScalar hardLeft[] = { 0, 0, 1 }; |
| 1084 | static const SkScalar hardRight[] = { 0, 1, 1 }; |
| 1085 | static const SkScalar hardCenter[] = { 0, .5f, .5f, 1 }; |
Florin Malita | 36f054a | 2017-08-03 12:55:41 -0400 | [diff] [blame] | 1086 | |
| 1087 | static const struct { |
| 1088 | const SkColor* colors; |
| 1089 | const SkScalar* pos; |
| 1090 | int count; |
| 1091 | } configs[] = { |
| 1092 | { colors2, nullptr, 2 }, // kTwo_ColorType |
Brian Osman | 2ab4b2b | 2017-09-12 11:20:56 -0400 | [diff] [blame] | 1093 | { colors3, nullptr, 3 }, // kThree_ColorType (simple) |
| 1094 | { colors3, softRight, 3 }, // kThree_ColorType (tricky) |
Florin Malita | 36f054a | 2017-08-03 12:55:41 -0400 | [diff] [blame] | 1095 | { colors3, hardLeft, 3 }, // kHardStopLeftEdged_ColorType |
| 1096 | { colors3, hardRight, 3 }, // kHardStopRightEdged_ColorType |
| 1097 | { colors4, hardCenter, 4 }, // kSingleHardStop_ColorType |
| 1098 | }; |
| 1099 | |
| 1100 | static const SkShader::TileMode modes[] = { |
| 1101 | SkShader::kClamp_TileMode, |
| 1102 | SkShader::kRepeat_TileMode, |
| 1103 | SkShader::kMirror_TileMode, |
| 1104 | }; |
| 1105 | |
| 1106 | static constexpr SkScalar size = 200; |
| 1107 | static const SkPoint pts[] = { { size / 3, size / 3 }, { size * 2 / 3, size * 2 / 3} }; |
| 1108 | |
| 1109 | SkPaint p; |
| 1110 | for (const auto& cfg : configs) { |
| 1111 | { |
| 1112 | SkAutoCanvasRestore acr(canvas, true); |
| 1113 | for (auto mode : modes) { |
| 1114 | p.setShader(SkGradientShader::MakeLinear(pts, cfg.colors, cfg.pos, cfg.count, |
| 1115 | mode)); |
| 1116 | canvas->drawRect(SkRect::MakeWH(size, size), p); |
| 1117 | canvas->translate(size * 1.1f, 0); |
| 1118 | } |
| 1119 | } |
| 1120 | canvas->translate(0, size * 1.1f); |
| 1121 | } |
| 1122 | } |