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