reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "Test.h" |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 9 | #include "SkDevice.h" |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 10 | #include "SkTemplates.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "SkColorShader.h" |
| 13 | #include "SkEmptyShader.h" |
| 14 | #include "SkGradientShader.h" |
| 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; |
| 145 | outBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 1); |
| 146 | outBitmap.allocPixels(); |
| 147 | SkPaint paint; |
| 148 | paint.setShader(s.get()); |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame^] | 149 | SkDevice device(outBitmap); |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 150 | SkCanvas canvas(&device); |
| 151 | canvas.drawPaint(paint); |
| 152 | SkAutoLockPixels alp(outBitmap); |
| 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 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 161 | typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&); |
| 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; |
| 178 | rec.fTileMode = SkShader::kClamp_TileMode; |
| 179 | |
| 180 | static const GradProc gProcs[] = { |
| 181 | none_gradproc, |
| 182 | color_gradproc, |
| 183 | linear_gradproc, |
| 184 | radial_gradproc, |
| 185 | radial2_gradproc, |
| 186 | sweep_gradproc, |
| 187 | conical_gradproc, |
| 188 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 189 | |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 190 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) { |
| 191 | gProcs[i](reporter, rec); |
| 192 | } |
| 193 | } |
| 194 | |
junov@chromium.org | e94b5e4 | 2013-01-30 15:52:06 +0000 | [diff] [blame] | 195 | static void TestGradients(skiatest::Reporter* reporter) { |
| 196 | TestGradientShaders(reporter); |
| 197 | TestConstantGradient(reporter); |
| 198 | } |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 199 | #include "TestClassDef.h" |
| 200 | DEFINE_TESTCLASS("Gradients", TestGradientsClass, TestGradients) |