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