reed@google.com | 8322697 | 2012-06-07 20:26:47 +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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorPriv.h" |
| 10 | #include "include/core/SkShader.h" |
| 11 | #include "include/core/SkSurface.h" |
| 12 | #include "include/effects/SkGradientShader.h" |
| 13 | #include "include/private/SkTemplates.h" |
| 14 | #include "src/core/SkTLazy.h" |
| 15 | #include "src/shaders/SkColorShader.h" |
| 16 | #include "tests/Test.h" |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 17 | |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 18 | // https://code.google.com/p/chromium/issues/detail?id=448299 |
| 19 | // Giant (inverse) matrix causes overflow when converting/computing using 32.32 |
| 20 | // Before the fix, we would assert (and then crash). |
| 21 | static void test_big_grad(skiatest::Reporter* reporter) { |
| 22 | const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
| 23 | const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }}; |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 24 | SkPaint paint; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 25 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp)); |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 26 | |
| 27 | SkBitmap bm; |
| 28 | bm.allocN32Pixels(2000, 1); |
| 29 | SkCanvas c(bm); |
| 30 | |
| 31 | const SkScalar affine[] = { |
| 32 | 1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f |
| 33 | }; |
| 34 | SkMatrix matrix; |
| 35 | matrix.setAffine(affine); |
| 36 | c.concat(matrix); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 37 | |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 38 | c.drawPaint(paint); |
| 39 | } |
| 40 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 41 | struct GradRec { |
| 42 | int fColorCount; |
| 43 | const SkColor* fColors; |
| 44 | const SkScalar* fPos; |
| 45 | const SkPoint* fPoint; // 2 |
| 46 | const SkScalar* fRadius; // 2 |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 47 | SkTileMode fTileMode; |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 48 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 49 | void gradCheck(skiatest::Reporter* reporter, const sk_sp<SkShader>& shader, |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 50 | SkShader::GradientInfo* info, |
| 51 | SkShader::GradientType gt) const { |
| 52 | SkAutoTMalloc<SkColor> colorStorage(fColorCount); |
| 53 | SkAutoTMalloc<SkScalar> posStorage(fColorCount); |
| 54 | |
| 55 | info->fColorCount = fColorCount; |
| 56 | info->fColors = colorStorage; |
| 57 | info->fColorOffsets = posStorage.get(); |
| 58 | REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt); |
| 59 | |
| 60 | REPORTER_ASSERT(reporter, info->fColorCount == fColorCount); |
| 61 | REPORTER_ASSERT(reporter, |
| 62 | !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor))); |
| 63 | REPORTER_ASSERT(reporter, |
| 64 | !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar))); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, fTileMode == (SkTileMode)info->fTileMode); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 66 | } |
| 67 | }; |
| 68 | |
| 69 | |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 70 | static void none_gradproc(skiatest::Reporter* reporter, const GradRec&, const GradRec&) { |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 71 | sk_sp<SkShader> s(SkShaders::Empty()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(nullptr)); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 73 | } |
| 74 | |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 75 | static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec, const GradRec&) { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 76 | sk_sp<SkShader> s(new SkColorShader(rec.fColors[0])); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(nullptr)); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 78 | |
| 79 | SkShader::GradientInfo info; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | info.fColors = nullptr; |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 81 | info.fColorCount = 0; |
| 82 | s->asAGradient(&info); |
| 83 | REPORTER_ASSERT(reporter, 1 == info.fColorCount); |
| 84 | } |
| 85 | |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 86 | static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec, |
| 87 | const GradRec& checkRec) { |
| 88 | sk_sp<SkShader> s(SkGradientShader::MakeLinear(buildRec.fPoint, buildRec.fColors, buildRec.fPos, |
| 89 | buildRec.fColorCount, buildRec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 90 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 91 | SkShader::GradientInfo info; |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 92 | checkRec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType); |
| 93 | REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint))); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 94 | } |
| 95 | |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 96 | static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec, |
| 97 | const GradRec& checkRec) { |
| 98 | sk_sp<SkShader> s(SkGradientShader::MakeRadial(buildRec.fPoint[0], buildRec.fRadius[0], |
| 99 | buildRec.fColors, buildRec.fPos, |
| 100 | buildRec.fColorCount, buildRec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 101 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 102 | SkShader::GradientInfo info; |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 103 | checkRec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType); |
| 104 | REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]); |
| 105 | REPORTER_ASSERT(reporter, info.fRadius[0] == checkRec.fRadius[0]); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 106 | } |
| 107 | |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 108 | static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec, |
| 109 | const GradRec& checkRec) { |
| 110 | sk_sp<SkShader> s(SkGradientShader::MakeSweep(buildRec.fPoint[0].fX, buildRec.fPoint[0].fY, |
| 111 | buildRec.fColors, buildRec.fPos, |
| 112 | buildRec.fColorCount)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 113 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 114 | SkShader::GradientInfo info; |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 115 | checkRec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType); |
| 116 | REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 117 | } |
| 118 | |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 119 | static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec, |
| 120 | const GradRec& checkRec) { |
| 121 | sk_sp<SkShader> s(SkGradientShader::MakeTwoPointConical(buildRec.fPoint[0], |
| 122 | buildRec.fRadius[0], |
| 123 | buildRec.fPoint[1], |
| 124 | buildRec.fRadius[1], |
| 125 | buildRec.fColors, |
| 126 | buildRec.fPos, |
| 127 | buildRec.fColorCount, |
| 128 | buildRec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 129 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 130 | SkShader::GradientInfo info; |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 131 | checkRec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType); |
| 132 | REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint))); |
| 133 | REPORTER_ASSERT(reporter, !memcmp(info.fRadius, checkRec.fRadius, 2 * sizeof(SkScalar))); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 134 | } |
| 135 | |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 136 | // Ensure that repeated color gradients behave like drawing a single color |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 137 | static void TestConstantGradient(skiatest::Reporter*) { |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 138 | const SkPoint pts[] = { |
| 139 | { 0, 0 }, |
| 140 | { SkIntToScalar(10), 0 } |
| 141 | }; |
| 142 | SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE }; |
| 143 | const SkScalar pos[] = { 0, SK_Scalar1 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 144 | SkPaint paint; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 145 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp)); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 146 | SkBitmap outBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 147 | outBitmap.allocN32Pixels(10, 1); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 148 | SkCanvas canvas(outBitmap); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 149 | canvas.drawPaint(paint); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 150 | for (int i = 0; i < 10; i++) { |
| 151 | // The following is commented out because it currently fails |
| 152 | // Related bug: https://code.google.com/p/skia/issues/detail?id=1098 |
| 153 | |
| 154 | // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0)); |
| 155 | } |
| 156 | } |
| 157 | |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 158 | typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&, const GradRec&); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 159 | |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 160 | static void TestGradientShaders(skiatest::Reporter* reporter) { |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 161 | static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 162 | static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
| 163 | static const SkPoint gPts[] = { |
| 164 | { 0, 0 }, |
| 165 | { SkIntToScalar(10), SkIntToScalar(20) } |
| 166 | }; |
| 167 | static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) }; |
| 168 | |
| 169 | GradRec rec; |
| 170 | rec.fColorCount = SK_ARRAY_COUNT(gColors); |
| 171 | rec.fColors = gColors; |
| 172 | rec.fPos = gPos; |
| 173 | rec.fPoint = gPts; |
| 174 | rec.fRadius = gRad; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 175 | rec.fTileMode = SkTileMode::kClamp; |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 176 | |
| 177 | static const GradProc gProcs[] = { |
| 178 | none_gradproc, |
| 179 | color_gradproc, |
| 180 | linear_gradproc, |
| 181 | radial_gradproc, |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 182 | sweep_gradproc, |
| 183 | conical_gradproc, |
| 184 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 185 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 186 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) { |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 187 | gProcs[i](reporter, rec, rec); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void TestGradientOptimization(skiatest::Reporter* reporter) { |
| 192 | static const struct { |
| 193 | GradProc fProc; |
| 194 | bool fIsClampRestricted; |
| 195 | } gProcInfo[] = { |
| 196 | { linear_gradproc , false }, |
| 197 | { radial_gradproc , false }, |
| 198 | { sweep_gradproc , true }, // sweep is funky in that it always pretends to be kClamp. |
| 199 | { conical_gradproc, false }, |
| 200 | }; |
| 201 | |
| 202 | static const SkColor gC_00[] = { 0xff000000, 0xff000000 }; |
| 203 | static const SkColor gC_01[] = { 0xff000000, 0xffffffff }; |
| 204 | static const SkColor gC_11[] = { 0xffffffff, 0xffffffff }; |
| 205 | static const SkColor gC_001[] = { 0xff000000, 0xff000000, 0xffffffff }; |
| 206 | static const SkColor gC_011[] = { 0xff000000, 0xffffffff, 0xffffffff }; |
| 207 | static const SkColor gC_0011[] = { 0xff000000, 0xff000000, 0xffffffff, 0xffffffff }; |
| 208 | |
| 209 | static const SkScalar gP_01[] = { 0, 1 }; |
| 210 | static const SkScalar gP_001[] = { 0, 0, 1 }; |
| 211 | static const SkScalar gP_011[] = { 0, 1, 1 }; |
| 212 | static const SkScalar gP_0x1[] = { 0, .5f, 1 }; |
| 213 | static const SkScalar gP_0011[] = { 0, 0, 1, 1 }; |
| 214 | |
| 215 | static const SkPoint gPts[] = { {0, 0}, {1, 1} }; |
| 216 | static const SkScalar gRadii[] = { 1, 2 }; |
| 217 | |
| 218 | static const struct { |
| 219 | const SkColor* fCol; |
| 220 | const SkScalar* fPos; |
| 221 | int fCount; |
| 222 | |
| 223 | const SkColor* fExpectedCol; |
| 224 | const SkScalar* fExpectedPos; |
| 225 | int fExpectedCount; |
| 226 | bool fRequiresNonClamp; |
| 227 | } gTests[] = { |
| 228 | { gC_001, gP_001, 3, gC_01, gP_01, 2, false }, |
| 229 | { gC_001, gP_011, 3, gC_00, gP_01, 2, true }, |
| 230 | { gC_001, gP_0x1, 3, gC_001, gP_0x1, 3, false }, |
| 231 | { gC_001, nullptr, 3, gC_001, gP_0x1, 3, false }, |
| 232 | |
| 233 | { gC_011, gP_001, 3, gC_11, gP_01, 2, true }, |
| 234 | { gC_011, gP_011, 3, gC_01, gP_01, 2, false }, |
| 235 | { gC_011, gP_0x1, 3, gC_011, gP_0x1, 3, false }, |
| 236 | { gC_011, nullptr, 3, gC_011, gP_0x1, 3, false }, |
| 237 | |
| 238 | { gC_0011, gP_0011, 4, gC_0011, gP_0011, 4, false }, |
| 239 | }; |
| 240 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 241 | const SkTileMode modes[] = { |
| 242 | SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror, |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 243 | // TODO: add kDecal_TileMode when it is implemented |
| 244 | }; |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 245 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProcInfo); ++i) { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 246 | for (auto mode : modes) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 247 | if (gProcInfo[i].fIsClampRestricted && mode != SkTileMode::kClamp) { |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 248 | continue; |
| 249 | } |
| 250 | |
| 251 | for (size_t t = 0; t < SK_ARRAY_COUNT(gTests); ++t) { |
| 252 | GradRec rec; |
| 253 | rec.fColorCount = gTests[t].fCount; |
| 254 | rec.fColors = gTests[t].fCol; |
| 255 | rec.fPos = gTests[t].fPos; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 256 | rec.fTileMode = mode; |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 257 | rec.fPoint = gPts; |
| 258 | rec.fRadius = gRadii; |
| 259 | |
| 260 | GradRec expected = rec; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 261 | if (!gTests[t].fRequiresNonClamp || mode != SkTileMode::kClamp) { |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 262 | expected.fColorCount = gTests[t].fExpectedCount; |
| 263 | expected.fColors = gTests[t].fExpectedCol; |
| 264 | expected.fPos = gTests[t].fExpectedPos; |
| 265 | } |
| 266 | |
| 267 | gProcInfo[i].fProc(reporter, rec, expected); |
| 268 | } |
| 269 | } |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 273 | static void test_nearly_vertical(skiatest::Reporter* reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 274 | auto surface(SkSurface::MakeRasterN32Premul(200, 200)); |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 275 | |
| 276 | const SkPoint pts[] = {{ 100, 50 }, { 100.0001f, 50000 }}; |
| 277 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 278 | const SkScalar pos[] = { 0, 1 }; |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 279 | SkPaint paint; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 280 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp)); |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 281 | |
| 282 | surface->getCanvas()->drawPaint(paint); |
| 283 | } |
| 284 | |
James Zern | 44e91c9 | 2016-11-09 19:22:46 -0800 | [diff] [blame] | 285 | static void test_vertical(skiatest::Reporter* reporter) { |
| 286 | auto surface(SkSurface::MakeRasterN32Premul(200, 200)); |
| 287 | |
| 288 | const SkPoint pts[] = {{ 100, 50 }, { 100, 50 }}; |
| 289 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 290 | const SkScalar pos[] = { 0, 1 }; |
| 291 | SkPaint paint; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 292 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp)); |
James Zern | 44e91c9 | 2016-11-09 19:22:46 -0800 | [diff] [blame] | 293 | |
| 294 | surface->getCanvas()->drawPaint(paint); |
| 295 | } |
| 296 | |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 297 | // A linear gradient interval can, due to numerical imprecision (likely in the divide) |
| 298 | // finish an interval with the final fx not landing outside of [p0...p1]. |
| 299 | // The old code had an assert which this test triggered. |
| 300 | // We now explicitly clamp the resulting fx value. |
| 301 | static void test_linear_fuzz(skiatest::Reporter* reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 302 | auto surface(SkSurface::MakeRasterN32Premul(1300, 630)); |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 303 | |
| 304 | const SkPoint pts[] = {{ 179.5f, -179.5f }, { 1074.5f, 715.5f }}; |
| 305 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE }; |
| 306 | const SkScalar pos[] = {0, 0.200000003f, 0.800000012f, 1 }; |
| 307 | |
reed | 9283d20 | 2016-03-13 13:01:57 -0700 | [diff] [blame] | 308 | SkPaint paint; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 309 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 4, SkTileMode::kClamp)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 310 | |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 311 | SkRect r = {0, 83, 1254, 620}; |
| 312 | surface->getCanvas()->drawRect(r, paint); |
| 313 | } |
| 314 | |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 315 | // https://bugs.chromium.org/p/skia/issues/detail?id=5023 |
| 316 | // We should still shade pixels for which the radius is exactly 0. |
| 317 | static void test_two_point_conical_zero_radius(skiatest::Reporter* reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 318 | auto surface(SkSurface::MakeRasterN32Premul(5, 5)); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 319 | surface->getCanvas()->clear(SK_ColorRED); |
| 320 | |
| 321 | const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 322 | SkPaint p; |
| 323 | p.setShader(SkGradientShader::MakeTwoPointConical( |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 324 | SkPoint::Make(2.5f, 2.5f), 0, |
| 325 | SkPoint::Make(3.0f, 3.0f), 10, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 326 | colors, nullptr, SK_ARRAY_COUNT(colors), SkTileMode::kClamp)); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 327 | surface->getCanvas()->drawPaint(p); |
| 328 | |
| 329 | // r == 0 for the center pixel. |
| 330 | // verify that we draw it (no red bleed) |
| 331 | SkPMColor centerPMColor; |
| 332 | surface->readPixels(SkImageInfo::MakeN32Premul(1, 1), ¢erPMColor, sizeof(SkPMColor), 2, 2); |
| 333 | REPORTER_ASSERT(reporter, SkGetPackedR32(centerPMColor) == 0); |
| 334 | } |
| 335 | |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 336 | // http://crbug.com/599458 |
| 337 | static void test_clamping_overflow(skiatest::Reporter*) { |
| 338 | SkPaint p; |
| 339 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN }; |
| 340 | const SkPoint pts1[] = { SkPoint::Make(1001, 1000001), SkPoint::Make(1000.99f, 1000000) }; |
| 341 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 342 | p.setShader(SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkTileMode::kClamp)); |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 343 | |
| 344 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 345 | surface->getCanvas()->scale(100, 100); |
| 346 | surface->getCanvas()->drawPaint(p); |
| 347 | |
| 348 | const SkPoint pts2[] = { SkPoint::Make(10000.99f, 1000000), SkPoint::Make(10001, 1000001) }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 349 | p.setShader(SkGradientShader::MakeLinear(pts2, colors, nullptr, 2, SkTileMode::kClamp)); |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 350 | surface->getCanvas()->drawPaint(p); |
| 351 | |
| 352 | // Passes if we don't trigger asserts. |
| 353 | } |
| 354 | |
fmalita | c523104 | 2016-08-10 05:45:50 -0700 | [diff] [blame] | 355 | // http://crbug.com/636194 |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 356 | static void test_degenerate_linear(skiatest::Reporter*) { |
fmalita | c523104 | 2016-08-10 05:45:50 -0700 | [diff] [blame] | 357 | SkPaint p; |
| 358 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN }; |
| 359 | const SkPoint pts[] = { |
| 360 | SkPoint::Make(-46058024627067344430605278824628224.0f, 0), |
| 361 | SkPoint::Make(SK_ScalarMax, 0) |
| 362 | }; |
| 363 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 364 | p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp)); |
fmalita | c523104 | 2016-08-10 05:45:50 -0700 | [diff] [blame] | 365 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 366 | surface->getCanvas()->drawPaint(p); |
| 367 | |
| 368 | // Passes if we don't trigger asserts. |
| 369 | } |
| 370 | |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 371 | // "Interesting" fuzzer values. |
| 372 | static void test_linear_fuzzer(skiatest::Reporter*) { |
| 373 | static const SkColor gColors0[] = { 0x30303030, 0x30303030 }; |
Florin Malita | 3d1a6bc | 2017-02-09 15:05:15 -0500 | [diff] [blame] | 374 | static const SkColor gColors1[] = { 0x30303030, 0x30303030, 0x30303030 }; |
| 375 | |
| 376 | static const SkScalar gPos1[] = { 0, 0, 1 }; |
| 377 | |
Florin Malita | d956966 | 2017-02-09 16:41:34 -0500 | [diff] [blame] | 378 | static const SkScalar gMatrix0[9] = { |
| 379 | 6.40969056e-10f, 0 , 6.40969056e-10f, |
| 380 | 0 , 4.42539023e-39f, 6.40969056e-10f, |
| 381 | 0 , 0 , 1 |
| 382 | }; |
| 383 | static const SkScalar gMatrix1[9] = { |
| 384 | -2.75294113f , 6.40969056e-10f, 6.40969056e-10f, |
| 385 | 6.40969056e-10f, 6.40969056e-10f, -3.32810161e+24f, |
| 386 | 6.40969056e-10f, 6.40969056e-10f, 0 |
| 387 | }; |
| 388 | static const SkScalar gMatrix2[9] = { |
| 389 | 7.93481258e+17f, 6.40969056e-10f, 6.40969056e-10f, |
| 390 | 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f, |
| 391 | 6.40969056e-10f, 6.40969056e-10f, 0.688235283f |
| 392 | }; |
| 393 | static const SkScalar gMatrix3[9] = { |
| 394 | 1.89180674e+11f, 6.40969056e-10f, 6.40969056e-10f, |
| 395 | 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f, |
| 396 | 6.40969056e-10f, 11276.0469f , 8.12524808e+20f |
| 397 | }; |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 398 | |
| 399 | static const struct { |
| 400 | SkPoint fPts[2]; |
| 401 | const SkColor* fColors; |
| 402 | const SkScalar* fPos; |
| 403 | int fCount; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 404 | SkTileMode fTileMode; |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 405 | uint32_t fFlags; |
| 406 | const SkScalar* fLocalMatrix; |
| 407 | const SkScalar* fGlobalMatrix; |
| 408 | } gConfigs[] = { |
| 409 | { |
Florin Malita | 3d1a6bc | 2017-02-09 15:05:15 -0500 | [diff] [blame] | 410 | {{0, -2.752941f}, {0, 0}}, |
| 411 | gColors0, |
| 412 | nullptr, |
| 413 | SK_ARRAY_COUNT(gColors0), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 414 | SkTileMode::kClamp, |
Florin Malita | 3d1a6bc | 2017-02-09 15:05:15 -0500 | [diff] [blame] | 415 | 0, |
| 416 | gMatrix0, |
| 417 | nullptr |
| 418 | }, |
| 419 | { |
| 420 | {{4.42539023e-39f, -4.42539023e-39f}, {9.78041162e-15f, 4.42539023e-39f}}, |
| 421 | gColors1, |
| 422 | gPos1, |
| 423 | SK_ARRAY_COUNT(gColors1), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 424 | SkTileMode::kClamp, |
Florin Malita | 3d1a6bc | 2017-02-09 15:05:15 -0500 | [diff] [blame] | 425 | 0, |
| 426 | nullptr, |
| 427 | gMatrix1 |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 428 | }, |
Florin Malita | d956966 | 2017-02-09 16:41:34 -0500 | [diff] [blame] | 429 | { |
| 430 | {{4.42539023e-39f, 6.40969056e-10f}, {6.40969056e-10f, 1.49237238e-19f}}, |
| 431 | gColors1, |
| 432 | gPos1, |
| 433 | SK_ARRAY_COUNT(gColors1), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 434 | SkTileMode::kClamp, |
Florin Malita | d956966 | 2017-02-09 16:41:34 -0500 | [diff] [blame] | 435 | 0, |
| 436 | nullptr, |
| 437 | gMatrix2 |
| 438 | }, |
| 439 | { |
| 440 | {{6.40969056e-10f, 6.40969056e-10f}, {6.40969056e-10f, -0.688235283f}}, |
| 441 | gColors0, |
| 442 | nullptr, |
| 443 | SK_ARRAY_COUNT(gColors0), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 444 | SkTileMode::kClamp, |
Florin Malita | d956966 | 2017-02-09 16:41:34 -0500 | [diff] [blame] | 445 | 0, |
| 446 | gMatrix3, |
| 447 | nullptr |
| 448 | }, |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 449 | }; |
| 450 | |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 451 | sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB(); |
| 452 | SkColorSpace* colorSpaces[] = { |
| 453 | nullptr, // hits the legacy gradient impl |
| 454 | srgb.get(), // triggers 4f/raster-pipeline |
| 455 | }; |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 456 | |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 457 | SkPaint paint; |
| 458 | |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame^] | 459 | for (const SkColorSpace* colorSpace : colorSpaces) { |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 460 | |
| 461 | sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::Make(100, 100, |
| 462 | kN32_SkColorType, |
| 463 | kPremul_SkAlphaType, |
| 464 | sk_ref_sp(colorSpace))); |
| 465 | SkCanvas* canvas = surface->getCanvas(); |
| 466 | |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 467 | for (const auto& config : gConfigs) { |
| 468 | SkAutoCanvasRestore acr(canvas, false); |
| 469 | SkTLazy<SkMatrix> localMatrix; |
| 470 | if (config.fLocalMatrix) { |
| 471 | localMatrix.init(); |
| 472 | localMatrix.get()->set9(config.fLocalMatrix); |
| 473 | } |
| 474 | |
| 475 | paint.setShader(SkGradientShader::MakeLinear(config.fPts, |
| 476 | config.fColors, |
| 477 | config.fPos, |
| 478 | config.fCount, |
| 479 | config.fTileMode, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 480 | config.fFlags, |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 481 | localMatrix.getMaybeNull())); |
Florin Malita | 3d1a6bc | 2017-02-09 15:05:15 -0500 | [diff] [blame] | 482 | if (config.fGlobalMatrix) { |
| 483 | SkMatrix m; |
| 484 | m.set9(config.fGlobalMatrix); |
| 485 | canvas->save(); |
| 486 | canvas->concat(m); |
| 487 | } |
| 488 | |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 489 | canvas->drawPaint(paint); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
Florin Malita | 0301308 | 2017-04-18 13:47:15 -0400 | [diff] [blame] | 494 | static void test_sweep_fuzzer(skiatest::Reporter*) { |
| 495 | static const SkColor gColors0[] = { 0x30303030, 0x30303030, 0x30303030 }; |
| 496 | static const SkScalar gPos0[] = { -47919293023455565225163489280.0f, 0, 1 }; |
| 497 | static const SkScalar gMatrix0[9] = { |
| 498 | 1.12116716e-13f, 0 , 8.50489682e+16f, |
| 499 | 4.1917041e-41f , 3.51369881e-23f, -2.54344271e-26f, |
| 500 | 9.61111907e+17f, -3.35263808e-29f, -1.35659403e+14f |
| 501 | }; |
| 502 | static const struct { |
| 503 | SkPoint fCenter; |
| 504 | const SkColor* fColors; |
| 505 | const SkScalar* fPos; |
| 506 | int fCount; |
| 507 | const SkScalar* fGlobalMatrix; |
| 508 | } gConfigs[] = { |
| 509 | { |
| 510 | { 0, 0 }, |
| 511 | gColors0, |
| 512 | gPos0, |
| 513 | SK_ARRAY_COUNT(gColors0), |
| 514 | gMatrix0 |
| 515 | }, |
| 516 | }; |
| 517 | |
| 518 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100); |
| 519 | SkCanvas* canvas = surface->getCanvas(); |
| 520 | SkPaint paint; |
| 521 | |
| 522 | for (const auto& config : gConfigs) { |
| 523 | paint.setShader(SkGradientShader::MakeSweep(config.fCenter.x(), |
| 524 | config.fCenter.y(), |
| 525 | config.fColors, |
| 526 | config.fPos, |
| 527 | config.fCount)); |
| 528 | |
| 529 | SkAutoCanvasRestore acr(canvas, false); |
| 530 | if (config.fGlobalMatrix) { |
| 531 | SkMatrix m; |
| 532 | m.set9(config.fGlobalMatrix); |
| 533 | canvas->save(); |
| 534 | canvas->concat(m); |
| 535 | } |
| 536 | canvas->drawPaint(paint); |
| 537 | } |
| 538 | } |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 539 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 540 | DEF_TEST(Gradient, reporter) { |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 541 | TestGradientShaders(reporter); |
fmalita | 5122967 | 2016-08-22 06:22:28 -0700 | [diff] [blame] | 542 | TestGradientOptimization(reporter); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 543 | TestConstantGradient(reporter); |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 544 | test_big_grad(reporter); |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 545 | test_nearly_vertical(reporter); |
James Zern | 44e91c9 | 2016-11-09 19:22:46 -0800 | [diff] [blame] | 546 | test_vertical(reporter); |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 547 | test_linear_fuzz(reporter); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 548 | test_two_point_conical_zero_radius(reporter); |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 549 | test_clamping_overflow(reporter); |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 550 | test_degenerate_linear(reporter); |
| 551 | test_linear_fuzzer(reporter); |
Florin Malita | 0301308 | 2017-04-18 13:47:15 -0400 | [diff] [blame] | 552 | test_sweep_fuzzer(reporter); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 553 | } |