commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 1 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 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 "gm.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | |
| 11 | namespace skiagm { |
| 12 | |
| 13 | struct GradData { |
| 14 | int fCount; |
| 15 | const SkColor* fColors; |
| 16 | const SkScalar* fPos; |
| 17 | }; |
| 18 | |
| 19 | static const SkColor gColors[] = { |
| 20 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 21 | }; |
| 22 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 23 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 24 | static const SkScalar gPos2[] = { |
| 25 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 26 | }; |
| 27 | |
| 28 | static const SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 29 | static const SkColor gColorClamp[] = { |
| 30 | SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE |
| 31 | }; |
| 32 | |
| 33 | static const GradData gGradData[] = { |
| 34 | { 2, gColors, gPos0 }, |
| 35 | { 2, gColors, gPos1 }, |
| 36 | { 5, gColors, gPos2 }, |
| 37 | { 4, gColorClamp, gPosClamp } |
| 38 | }; |
| 39 | |
| 40 | static SkShader* Make2ConicalOutside(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 41 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 42 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 43 | SkPoint center0, center1; |
| 44 | SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
| 45 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 46 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 47 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 48 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 49 | center1, radius1, |
| 50 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 51 | data.fCount, tm, mapper, |
| 52 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static SkShader* Make2ConicalOutsideFlip(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 56 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 57 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 58 | SkPoint center0, center1; |
| 59 | SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
| 60 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 61 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 62 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 63 | return SkGradientShader::CreateTwoPointConical(center1, radius1, |
| 64 | center0, radius0, |
| 65 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 66 | data.fCount, tm, mapper, |
| 67 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static SkShader* Make2ConicalInside(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 71 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 72 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 73 | SkPoint center0, center1; |
| 74 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 75 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 76 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 77 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 78 | return SkGradientShader::CreateTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 79 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 80 | data.fColors, data.fPos, data.fCount, tm, mapper, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 81 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static SkShader* Make2ConicalInsideFlip(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 85 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 86 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 87 | SkPoint center0, center1; |
| 88 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 89 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 90 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 91 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 92 | return SkGradientShader::CreateTwoPointConical(center0, (pts[1].fX - pts[0].fX) / 2, |
| 93 | center1, (pts[1].fX - pts[0].fX) / 7, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 94 | data.fColors, data.fPos, data.fCount, tm, mapper, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 95 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static SkShader* Make2ConicalInsideCenter(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 99 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 100 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 101 | SkPoint center0, center1; |
| 102 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 103 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 104 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 105 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 106 | return SkGradientShader::CreateTwoPointConical(center0, (pts[1].fX - pts[0].fX) / 7, |
| 107 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 108 | data.fColors, data.fPos, data.fCount, tm, mapper, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 109 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static SkShader* Make2ConicalZeroRad(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 113 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 114 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 115 | SkPoint center0, center1; |
| 116 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 117 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 118 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 119 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 120 | return SkGradientShader::CreateTwoPointConical(center1, 0.f, |
| 121 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 122 | data.fColors, data.fPos, data.fCount, tm, mapper, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 123 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static SkShader* Make2ConicalZeroRadFlip(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 127 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 128 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 129 | SkPoint center0, center1; |
| 130 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 131 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 132 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 133 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 134 | return SkGradientShader::CreateTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 2, |
| 135 | center0, 0.f, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 136 | data.fColors, data.fPos, data.fCount, tm, mapper, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 137 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static SkShader* Make2ConicalZeroRadCenter(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 141 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 142 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 143 | SkPoint center0, center1; |
| 144 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 145 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 146 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 147 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 148 | return SkGradientShader::CreateTwoPointConical(center0, 0.f, |
| 149 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 150 | data.fColors, data.fPos, data.fCount, tm, mapper, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 151 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static SkShader* Make2ConicalZeroRadOutside(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 155 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 156 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 157 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 158 | SkScalar radius0 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 159 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 160 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 161 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 162 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 163 | center1, radius1, |
| 164 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 165 | data.fCount, tm, mapper, |
| 166 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static SkShader* Make2ConicalZeroRadFlipOutside(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 170 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 171 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 172 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 173 | SkScalar radius0 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 174 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 175 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 176 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 177 | return SkGradientShader::CreateTwoPointConical(center1, radius1, |
| 178 | center0, radius0, |
| 179 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 180 | data.fCount, tm, mapper, |
| 181 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static SkShader* Make2ConicalEdgeX(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 185 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 186 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 187 | SkPoint center0, center1; |
| 188 | SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 7); |
| 189 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 190 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 191 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 192 | center0.set(center1.fX + radius1, center1.fY); |
| 193 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 194 | center1, radius1, |
| 195 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 196 | data.fCount, tm, mapper, |
| 197 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static SkShader* Make2ConicalEdgeY(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 201 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 202 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 203 | SkPoint center0, center1; |
| 204 | SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 7); |
| 205 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 206 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 207 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 208 | center0.set(center1.fX, center1.fY + radius1); |
| 209 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 210 | center1, radius1, |
| 211 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 212 | data.fCount, tm, mapper, |
| 213 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 214 | } |
| 215 | static SkShader* Make2ConicalZeroRadEdgeX(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 216 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 217 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 218 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 219 | SkScalar radius0 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 220 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 221 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 222 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 223 | center0.set(center1.fX + radius1, center1.fY); |
| 224 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 225 | center1, radius1, |
| 226 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 227 | data.fCount, tm, mapper, |
| 228 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static SkShader* Make2ConicalZeroRadEdgeY(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 232 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 233 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 234 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 235 | SkScalar radius0 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 236 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 237 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 238 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 239 | center0.set(center1.fX, center1.fY + radius1); |
| 240 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 241 | center1, radius1, |
| 242 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 243 | data.fCount, tm, mapper, |
| 244 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static SkShader* Make2ConicalTouchX(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 248 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 249 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 250 | SkPoint center0, center1; |
| 251 | SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 7); |
| 252 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 253 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 254 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 255 | center0.set(center1.fX - radius1 + radius0, center1.fY); |
| 256 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 257 | center1, radius1, |
| 258 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 259 | data.fCount, tm, mapper, |
| 260 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static SkShader* Make2ConicalTouchY(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 264 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 265 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 266 | SkPoint center0, center1; |
| 267 | SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 7); |
| 268 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 269 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 270 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 271 | center0.set(center1.fX, center1.fY + radius1 - radius0); |
| 272 | return SkGradientShader::CreateTwoPointConical(center0, radius0, |
| 273 | center1, radius1, |
| 274 | data.fColors, data.fPos, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 275 | data.fCount, tm, mapper, |
| 276 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static SkShader* Make2ConicalInsideSmallRad(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 280 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 281 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 282 | SkPoint center0, center1; |
| 283 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 284 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 285 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 286 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 287 | return SkGradientShader::CreateTwoPointConical(center0, 0.0000000000000000001f, |
| 288 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 289 | data.fColors, data.fPos, data.fCount, tm, mapper, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 290 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 294 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 295 | const SkMatrix& localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 296 | |
| 297 | static const GradMaker gGradMakersOutside[] = { |
| 298 | Make2ConicalOutside, Make2ConicalOutsideFlip, |
| 299 | Make2ConicalZeroRadOutside, Make2ConicalZeroRadFlipOutside |
| 300 | }; |
| 301 | |
| 302 | static const GradMaker gGradMakersInside[] = { |
| 303 | Make2ConicalInside, Make2ConicalInsideFlip, Make2ConicalInsideCenter, |
| 304 | Make2ConicalZeroRad, Make2ConicalZeroRadFlip, Make2ConicalZeroRadCenter, |
| 305 | }; |
| 306 | |
| 307 | static const GradMaker gGradMakersEdgeCases[] = { |
| 308 | Make2ConicalEdgeX, Make2ConicalEdgeY, |
| 309 | Make2ConicalZeroRadEdgeX, Make2ConicalZeroRadEdgeY, |
| 310 | Make2ConicalTouchX, Make2ConicalTouchY, |
| 311 | Make2ConicalInsideSmallRad |
| 312 | }; |
| 313 | |
| 314 | |
| 315 | static const struct { |
| 316 | const GradMaker* fMaker; |
| 317 | const int fCount; |
| 318 | const char* fName; |
| 319 | } gGradCases[] = { |
| 320 | { gGradMakersOutside, SK_ARRAY_COUNT(gGradMakersOutside), "outside" }, |
| 321 | { gGradMakersInside, SK_ARRAY_COUNT(gGradMakersInside), "inside" }, |
| 322 | { gGradMakersEdgeCases, SK_ARRAY_COUNT(gGradMakersEdgeCases), "edge" }, |
| 323 | }; |
| 324 | |
| 325 | enum GradCaseType { // these must match the order in gGradCases |
| 326 | kOutside_GradCaseType, |
| 327 | kInside_GradCaseType, |
| 328 | kEdge_GradCaseType, |
| 329 | }; |
| 330 | |
| 331 | /////////////////////////////////////////////////////////////////////////////// |
| 332 | |
| 333 | class ConicalGradientsGM : public GM { |
| 334 | public: |
| 335 | ConicalGradientsGM(GradCaseType gradCaseType) { |
| 336 | this->setBGColor(0xFFDDDDDD); |
| 337 | fName.printf("gradients_2pt_conical_%s", gGradCases[gradCaseType].fName); |
| 338 | fGradCaseType = gradCaseType; |
| 339 | } |
| 340 | |
| 341 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 342 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 343 | if (fGradCaseType != kInside_GradCaseType) { |
| 344 | return kSkipTiled_Flag; |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 349 | SkString onShortName() { |
| 350 | return fName; |
| 351 | } |
| 352 | |
| 353 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
| 354 | |
| 355 | virtual void onDraw(SkCanvas* canvas) { |
| 356 | |
| 357 | SkPoint pts[2] = { |
| 358 | { 0, 0 }, |
| 359 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 360 | }; |
| 361 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 362 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 363 | SkPaint paint; |
| 364 | paint.setAntiAlias(true); |
| 365 | |
| 366 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 367 | |
| 368 | const GradMaker* gradMaker = gGradCases[fGradCaseType].fMaker; |
| 369 | const int count = gGradCases[fGradCaseType].fCount; |
| 370 | |
| 371 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 372 | canvas->save(); |
| 373 | for (int j = 0; j < count; j++) { |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 374 | SkMatrix scale = SkMatrix::I(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 375 | |
| 376 | if (i == 3) { // if the clamp case |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 377 | scale.setScale(0.5f, 0.5f); |
| 378 | scale.postTranslate(25.f, 25.f); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 379 | } |
| 380 | |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 381 | SkShader* shader = gradMaker[j](pts, gGradData[i], tm, NULL, scale); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 382 | paint.setShader(shader); |
| 383 | canvas->drawRect(r, paint); |
| 384 | shader->unref(); |
| 385 | canvas->translate(0, SkIntToScalar(120)); |
| 386 | } |
| 387 | canvas->restore(); |
| 388 | canvas->translate(SkIntToScalar(120), 0); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | private: |
| 393 | typedef GM INHERITED; |
| 394 | |
| 395 | GradCaseType fGradCaseType; |
| 396 | SkString fName; |
| 397 | }; |
| 398 | /////////////////////////////////////////////////////////////////////////////// |
| 399 | |
| 400 | static GM* MyFactory1(void*) { return new ConicalGradientsGM(kInside_GradCaseType); } |
| 401 | static GMRegistry reg1(MyFactory1); |
| 402 | |
| 403 | static GM* MyFactory2(void*) { return new ConicalGradientsGM(kOutside_GradCaseType); } |
| 404 | static GMRegistry reg2(MyFactory2); |
| 405 | |
| 406 | static GM* MyFactory3(void*) { return new ConicalGradientsGM(kEdge_GradCaseType); } |
| 407 | static GMRegistry reg3(MyFactory3); |
| 408 | } |