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