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" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 12 | #include "SkShader.h" |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 13 | #include "SkSurface.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 14 | #include "SkTemplates.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "Test.h" |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 16 | |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 17 | // https://code.google.com/p/chromium/issues/detail?id=448299 |
| 18 | // Giant (inverse) matrix causes overflow when converting/computing using 32.32 |
| 19 | // Before the fix, we would assert (and then crash). |
| 20 | static void test_big_grad(skiatest::Reporter* reporter) { |
| 21 | const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
| 22 | const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }}; |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 23 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 24 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 25 | SkShader::kClamp_TileMode)); |
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 |
| 47 | SkShader::TileMode fTileMode; |
| 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))); |
| 65 | REPORTER_ASSERT(reporter, fTileMode == info->fTileMode); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 70 | static void none_gradproc(skiatest::Reporter* reporter, const GradRec&) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 71 | sk_sp<SkShader> s(SkShader::MakeEmptyShader()); |
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 | |
| 75 | static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
| 76 | SkAutoTUnref<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 | |
| 86 | static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 87 | sk_sp<SkShader> s(SkGradientShader::MakeLinear(rec.fPoint, rec.fColors, rec.fPos, |
| 88 | rec.fColorCount, rec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 90 | SkShader::GradientInfo info; |
| 91 | rec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType); |
| 92 | REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint))); |
| 93 | } |
| 94 | |
| 95 | static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 96 | sk_sp<SkShader> s(SkGradientShader::MakeRadial(rec.fPoint[0], rec.fRadius[0], rec.fColors, |
| 97 | rec.fPos, rec.fColorCount, rec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 99 | SkShader::GradientInfo info; |
| 100 | rec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType); |
| 101 | REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]); |
| 102 | REPORTER_ASSERT(reporter, info.fRadius[0] == rec.fRadius[0]); |
| 103 | } |
| 104 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 105 | static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 106 | sk_sp<SkShader> s(SkGradientShader::MakeSweep(rec.fPoint[0].fX, rec.fPoint[0].fY, rec.fColors, |
| 107 | rec.fPos, rec.fColorCount)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 108 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 109 | SkShader::GradientInfo info; |
| 110 | rec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType); |
| 111 | REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]); |
| 112 | } |
| 113 | |
| 114 | static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 115 | sk_sp<SkShader> s(SkGradientShader::MakeTwoPointConical(rec.fPoint[0], |
| 116 | rec.fRadius[0], |
| 117 | rec.fPoint[1], |
| 118 | rec.fRadius[1], |
| 119 | rec.fColors, |
| 120 | rec.fPos, |
| 121 | rec.fColorCount, |
| 122 | rec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 123 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 124 | SkShader::GradientInfo info; |
| 125 | rec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType); |
| 126 | REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint))); |
| 127 | REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar))); |
| 128 | } |
| 129 | |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 130 | // Ensure that repeated color gradients behave like drawing a single color |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 131 | static void TestConstantGradient(skiatest::Reporter*) { |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 132 | const SkPoint pts[] = { |
| 133 | { 0, 0 }, |
| 134 | { SkIntToScalar(10), 0 } |
| 135 | }; |
| 136 | SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE }; |
| 137 | const SkScalar pos[] = { 0, SK_Scalar1 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 138 | SkPaint paint; |
| 139 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkShader::kClamp_TileMode)); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 140 | SkBitmap outBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 141 | outBitmap.allocN32Pixels(10, 1); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 142 | SkCanvas canvas(outBitmap); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 143 | canvas.drawPaint(paint); |
| 144 | SkAutoLockPixels alp(outBitmap); |
| 145 | for (int i = 0; i < 10; i++) { |
| 146 | // The following is commented out because it currently fails |
| 147 | // Related bug: https://code.google.com/p/skia/issues/detail?id=1098 |
| 148 | |
| 149 | // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0)); |
| 150 | } |
| 151 | } |
| 152 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 153 | typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&); |
| 154 | |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 155 | static void TestGradientShaders(skiatest::Reporter* reporter) { |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 156 | static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 157 | static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
| 158 | static const SkPoint gPts[] = { |
| 159 | { 0, 0 }, |
| 160 | { SkIntToScalar(10), SkIntToScalar(20) } |
| 161 | }; |
| 162 | static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) }; |
| 163 | |
| 164 | GradRec rec; |
| 165 | rec.fColorCount = SK_ARRAY_COUNT(gColors); |
| 166 | rec.fColors = gColors; |
| 167 | rec.fPos = gPos; |
| 168 | rec.fPoint = gPts; |
| 169 | rec.fRadius = gRad; |
| 170 | rec.fTileMode = SkShader::kClamp_TileMode; |
| 171 | |
| 172 | static const GradProc gProcs[] = { |
| 173 | none_gradproc, |
| 174 | color_gradproc, |
| 175 | linear_gradproc, |
| 176 | radial_gradproc, |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 177 | sweep_gradproc, |
| 178 | conical_gradproc, |
| 179 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 180 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 181 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) { |
| 182 | gProcs[i](reporter, rec); |
| 183 | } |
| 184 | } |
| 185 | |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 186 | static void test_nearly_vertical(skiatest::Reporter* reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 187 | auto surface(SkSurface::MakeRasterN32Premul(200, 200)); |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 188 | |
| 189 | const SkPoint pts[] = {{ 100, 50 }, { 100.0001f, 50000 }}; |
| 190 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 191 | const SkScalar pos[] = { 0, 1 }; |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 192 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 193 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkShader::kClamp_TileMode)); |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 194 | |
| 195 | surface->getCanvas()->drawPaint(paint); |
| 196 | } |
| 197 | |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 198 | // A linear gradient interval can, due to numerical imprecision (likely in the divide) |
| 199 | // finish an interval with the final fx not landing outside of [p0...p1]. |
| 200 | // The old code had an assert which this test triggered. |
| 201 | // We now explicitly clamp the resulting fx value. |
| 202 | static void test_linear_fuzz(skiatest::Reporter* reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 203 | auto surface(SkSurface::MakeRasterN32Premul(1300, 630)); |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 204 | |
| 205 | const SkPoint pts[] = {{ 179.5f, -179.5f }, { 1074.5f, 715.5f }}; |
| 206 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE }; |
| 207 | const SkScalar pos[] = {0, 0.200000003f, 0.800000012f, 1 }; |
| 208 | |
reed | 9283d20 | 2016-03-13 13:01:57 -0700 | [diff] [blame] | 209 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 210 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode)); |
| 211 | |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 212 | SkRect r = {0, 83, 1254, 620}; |
| 213 | surface->getCanvas()->drawRect(r, paint); |
| 214 | } |
| 215 | |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 216 | // https://bugs.chromium.org/p/skia/issues/detail?id=5023 |
| 217 | // We should still shade pixels for which the radius is exactly 0. |
| 218 | static void test_two_point_conical_zero_radius(skiatest::Reporter* reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 219 | auto surface(SkSurface::MakeRasterN32Premul(5, 5)); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 220 | surface->getCanvas()->clear(SK_ColorRED); |
| 221 | |
| 222 | const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 223 | SkPaint p; |
| 224 | p.setShader(SkGradientShader::MakeTwoPointConical( |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 225 | SkPoint::Make(2.5f, 2.5f), 0, |
| 226 | SkPoint::Make(3.0f, 3.0f), 10, |
| 227 | colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode)); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 228 | surface->getCanvas()->drawPaint(p); |
| 229 | |
| 230 | // r == 0 for the center pixel. |
| 231 | // verify that we draw it (no red bleed) |
| 232 | SkPMColor centerPMColor; |
| 233 | surface->readPixels(SkImageInfo::MakeN32Premul(1, 1), ¢erPMColor, sizeof(SkPMColor), 2, 2); |
| 234 | REPORTER_ASSERT(reporter, SkGetPackedR32(centerPMColor) == 0); |
| 235 | } |
| 236 | |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 237 | // http://crbug.com/599458 |
| 238 | static void test_clamping_overflow(skiatest::Reporter*) { |
| 239 | SkPaint p; |
| 240 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN }; |
| 241 | const SkPoint pts1[] = { SkPoint::Make(1001, 1000001), SkPoint::Make(1000.99f, 1000000) }; |
| 242 | |
| 243 | p.setShader(SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkShader::kClamp_TileMode)); |
| 244 | |
| 245 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 246 | surface->getCanvas()->scale(100, 100); |
| 247 | surface->getCanvas()->drawPaint(p); |
| 248 | |
| 249 | const SkPoint pts2[] = { SkPoint::Make(10000.99f, 1000000), SkPoint::Make(10001, 1000001) }; |
| 250 | p.setShader(SkGradientShader::MakeLinear(pts2, colors, nullptr, 2, SkShader::kClamp_TileMode)); |
| 251 | surface->getCanvas()->drawPaint(p); |
| 252 | |
| 253 | // Passes if we don't trigger asserts. |
| 254 | } |
| 255 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 256 | DEF_TEST(Gradient, reporter) { |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 257 | TestGradientShaders(reporter); |
| 258 | TestConstantGradient(reporter); |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 259 | test_big_grad(reporter); |
fmalita | 8d38102 | 2015-11-19 10:35:34 -0800 | [diff] [blame] | 260 | test_nearly_vertical(reporter); |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 261 | test_linear_fuzz(reporter); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 262 | test_two_point_conical_zero_radius(reporter); |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 263 | test_clamping_overflow(reporter); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 264 | } |