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