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" |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 9 | #include "SkColorShader.h" |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 11 | #include "SkShader.h" |
| 12 | #include "SkTemplates.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 14 | |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 15 | // https://code.google.com/p/chromium/issues/detail?id=448299 |
| 16 | // Giant (inverse) matrix causes overflow when converting/computing using 32.32 |
| 17 | // Before the fix, we would assert (and then crash). |
| 18 | static void test_big_grad(skiatest::Reporter* reporter) { |
| 19 | const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
| 20 | const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }}; |
| 21 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
| 22 | SkPaint paint; |
| 23 | paint.setShader(s)->unref(); |
| 24 | |
| 25 | SkBitmap bm; |
| 26 | bm.allocN32Pixels(2000, 1); |
| 27 | SkCanvas c(bm); |
| 28 | |
| 29 | const SkScalar affine[] = { |
| 30 | 1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f |
| 31 | }; |
| 32 | SkMatrix matrix; |
| 33 | matrix.setAffine(affine); |
| 34 | c.concat(matrix); |
| 35 | |
| 36 | c.drawPaint(paint); |
| 37 | } |
| 38 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 39 | struct GradRec { |
| 40 | int fColorCount; |
| 41 | const SkColor* fColors; |
| 42 | const SkScalar* fPos; |
| 43 | const SkPoint* fPoint; // 2 |
| 44 | const SkScalar* fRadius; // 2 |
| 45 | SkShader::TileMode fTileMode; |
| 46 | |
| 47 | void gradCheck(skiatest::Reporter* reporter, SkShader* shader, |
| 48 | SkShader::GradientInfo* info, |
| 49 | SkShader::GradientType gt) const { |
| 50 | SkAutoTMalloc<SkColor> colorStorage(fColorCount); |
| 51 | SkAutoTMalloc<SkScalar> posStorage(fColorCount); |
| 52 | |
| 53 | info->fColorCount = fColorCount; |
| 54 | info->fColors = colorStorage; |
| 55 | info->fColorOffsets = posStorage.get(); |
| 56 | REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt); |
| 57 | |
| 58 | REPORTER_ASSERT(reporter, info->fColorCount == fColorCount); |
| 59 | REPORTER_ASSERT(reporter, |
| 60 | !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor))); |
| 61 | REPORTER_ASSERT(reporter, |
| 62 | !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar))); |
| 63 | REPORTER_ASSERT(reporter, fTileMode == info->fTileMode); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 68 | static void none_gradproc(skiatest::Reporter* reporter, const GradRec&) { |
reed | e1085e0 | 2014-07-03 07:26:01 -0700 | [diff] [blame] | 69 | SkAutoTUnref<SkShader> s(SkShader::CreateEmptyShader()); |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(NULL)); |
| 71 | } |
| 72 | |
| 73 | static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
| 74 | SkAutoTUnref<SkShader> s(new SkColorShader(rec.fColors[0])); |
| 75 | REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(NULL)); |
| 76 | |
| 77 | SkShader::GradientInfo info; |
robertphillips@google.com | 37ebe3f | 2013-03-04 20:03:44 +0000 | [diff] [blame] | 78 | info.fColors = NULL; |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 79 | info.fColorCount = 0; |
| 80 | s->asAGradient(&info); |
| 81 | REPORTER_ASSERT(reporter, 1 == info.fColorCount); |
| 82 | } |
| 83 | |
| 84 | static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
| 85 | SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(rec.fPoint, |
| 86 | rec.fColors, |
| 87 | rec.fPos, |
| 88 | rec.fColorCount, |
| 89 | rec.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; |
| 92 | rec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType); |
| 93 | REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint))); |
| 94 | } |
| 95 | |
| 96 | static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
| 97 | SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial(rec.fPoint[0], |
| 98 | rec.fRadius[0], |
| 99 | rec.fColors, |
| 100 | rec.fPos, |
| 101 | rec.fColorCount, |
| 102 | rec.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; |
| 105 | rec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType); |
| 106 | REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]); |
| 107 | REPORTER_ASSERT(reporter, info.fRadius[0] == rec.fRadius[0]); |
| 108 | } |
| 109 | |
| 110 | static void radial2_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
| 111 | SkAutoTUnref<SkShader> s(SkGradientShader::CreateTwoPointRadial(rec.fPoint[0], |
| 112 | rec.fRadius[0], |
| 113 | rec.fPoint[1], |
| 114 | rec.fRadius[1], |
| 115 | rec.fColors, |
| 116 | rec.fPos, |
| 117 | rec.fColorCount, |
| 118 | rec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 120 | SkShader::GradientInfo info; |
| 121 | rec.gradCheck(reporter, s, &info, SkShader::kRadial2_GradientType); |
| 122 | REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint))); |
| 123 | REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar))); |
| 124 | } |
| 125 | |
| 126 | static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
| 127 | SkAutoTUnref<SkShader> s(SkGradientShader::CreateSweep(rec.fPoint[0].fX, |
| 128 | rec.fPoint[0].fY, |
| 129 | rec.fColors, |
| 130 | rec.fPos, |
| 131 | rec.fColorCount)); |
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; |
| 134 | rec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType); |
| 135 | REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]); |
| 136 | } |
| 137 | |
| 138 | static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& rec) { |
| 139 | SkAutoTUnref<SkShader> s(SkGradientShader::CreateTwoPointConical(rec.fPoint[0], |
| 140 | rec.fRadius[0], |
| 141 | rec.fPoint[1], |
| 142 | rec.fRadius[1], |
| 143 | rec.fColors, |
| 144 | rec.fPos, |
| 145 | rec.fColorCount, |
| 146 | rec.fTileMode)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 147 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 148 | SkShader::GradientInfo info; |
| 149 | rec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType); |
| 150 | REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint))); |
| 151 | REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar))); |
| 152 | } |
| 153 | |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 154 | // Ensure that repeated color gradients behave like drawing a single color |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 155 | static void TestConstantGradient(skiatest::Reporter*) { |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 156 | const SkPoint pts[] = { |
| 157 | { 0, 0 }, |
| 158 | { SkIntToScalar(10), 0 } |
| 159 | }; |
| 160 | SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE }; |
| 161 | const SkScalar pos[] = { 0, SK_Scalar1 }; |
| 162 | SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(pts, |
| 163 | colors, |
| 164 | pos, |
| 165 | 2, |
| 166 | SkShader::kClamp_TileMode)); |
| 167 | SkBitmap outBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 168 | outBitmap.allocN32Pixels(10, 1); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 169 | SkPaint paint; |
| 170 | paint.setShader(s.get()); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 171 | SkCanvas canvas(outBitmap); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 172 | canvas.drawPaint(paint); |
| 173 | SkAutoLockPixels alp(outBitmap); |
| 174 | for (int i = 0; i < 10; i++) { |
| 175 | // The following is commented out because it currently fails |
| 176 | // Related bug: https://code.google.com/p/skia/issues/detail?id=1098 |
| 177 | |
| 178 | // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0)); |
| 179 | } |
| 180 | } |
| 181 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 182 | typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&); |
| 183 | |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 184 | static void TestGradientShaders(skiatest::Reporter* reporter) { |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 185 | static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 186 | static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
| 187 | static const SkPoint gPts[] = { |
| 188 | { 0, 0 }, |
| 189 | { SkIntToScalar(10), SkIntToScalar(20) } |
| 190 | }; |
| 191 | static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) }; |
| 192 | |
| 193 | GradRec rec; |
| 194 | rec.fColorCount = SK_ARRAY_COUNT(gColors); |
| 195 | rec.fColors = gColors; |
| 196 | rec.fPos = gPos; |
| 197 | rec.fPoint = gPts; |
| 198 | rec.fRadius = gRad; |
| 199 | rec.fTileMode = SkShader::kClamp_TileMode; |
| 200 | |
| 201 | static const GradProc gProcs[] = { |
| 202 | none_gradproc, |
| 203 | color_gradproc, |
| 204 | linear_gradproc, |
| 205 | radial_gradproc, |
| 206 | radial2_gradproc, |
| 207 | sweep_gradproc, |
| 208 | conical_gradproc, |
| 209 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 210 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 211 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) { |
| 212 | gProcs[i](reporter, rec); |
| 213 | } |
| 214 | } |
| 215 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 216 | DEF_TEST(Gradient, reporter) { |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 217 | TestGradientShaders(reporter); |
| 218 | TestConstantGradient(reporter); |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 219 | test_big_grad(reporter); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 220 | } |