rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 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 | |
| 9 | #include "SkTwoPointConicalGradient.h" |
| 10 | |
| 11 | static int valid_divide(float numer, float denom, float* ratio) { |
| 12 | SkASSERT(ratio); |
| 13 | if (0 == denom) { |
| 14 | return 0; |
| 15 | } |
| 16 | *ratio = numer / denom; |
| 17 | return 1; |
| 18 | } |
| 19 | |
| 20 | // Return the number of distinct real roots, and write them into roots[] in |
| 21 | // ascending order |
| 22 | static int find_quad_roots(float A, float B, float C, float roots[2]) { |
| 23 | SkASSERT(roots); |
| 24 | |
| 25 | if (A == 0) { |
| 26 | return valid_divide(-C, B, roots); |
| 27 | } |
| 28 | |
| 29 | float R = B*B - 4*A*C; |
| 30 | if (R < 0) { |
| 31 | return 0; |
| 32 | } |
| 33 | R = sk_float_sqrt(R); |
| 34 | |
| 35 | #if 1 |
| 36 | float Q = B; |
| 37 | if (Q < 0) { |
| 38 | Q -= R; |
| 39 | } else { |
| 40 | Q += R; |
| 41 | } |
| 42 | #else |
| 43 | // on 10.6 this was much slower than the above branch :( |
| 44 | float Q = B + copysignf(R, B); |
| 45 | #endif |
| 46 | Q *= -0.5f; |
| 47 | if (0 == Q) { |
| 48 | roots[0] = 0; |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | float r0 = Q / A; |
| 53 | float r1 = C / Q; |
| 54 | roots[0] = r0 < r1 ? r0 : r1; |
| 55 | roots[1] = r0 > r1 ? r0 : r1; |
| 56 | return 2; |
| 57 | } |
| 58 | |
| 59 | static float lerp(float x, float dx, float t) { |
| 60 | return x + t * dx; |
| 61 | } |
| 62 | |
| 63 | static float sqr(float x) { return x * x; } |
| 64 | |
| 65 | void TwoPtRadial::init(const SkPoint& center0, SkScalar rad0, |
| 66 | const SkPoint& center1, SkScalar rad1) { |
| 67 | fCenterX = SkScalarToFloat(center0.fX); |
| 68 | fCenterY = SkScalarToFloat(center0.fY); |
| 69 | fDCenterX = SkScalarToFloat(center1.fX) - fCenterX; |
| 70 | fDCenterY = SkScalarToFloat(center1.fY) - fCenterY; |
| 71 | fRadius = SkScalarToFloat(rad0); |
| 72 | fDRadius = SkScalarToFloat(rad1) - fRadius; |
| 73 | |
| 74 | fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius); |
| 75 | fRadius2 = sqr(fRadius); |
| 76 | fRDR = fRadius * fDRadius; |
| 77 | } |
| 78 | |
| 79 | void TwoPtRadial::setup(SkScalar fx, SkScalar fy, SkScalar dfx, SkScalar dfy) { |
| 80 | fRelX = SkScalarToFloat(fx) - fCenterX; |
| 81 | fRelY = SkScalarToFloat(fy) - fCenterY; |
| 82 | fIncX = SkScalarToFloat(dfx); |
| 83 | fIncY = SkScalarToFloat(dfy); |
| 84 | fB = -2 * (fDCenterX * fRelX + fDCenterY * fRelY + fRDR); |
| 85 | fDB = -2 * (fDCenterX * fIncX + fDCenterY * fIncY); |
| 86 | } |
| 87 | |
| 88 | SkFixed TwoPtRadial::nextT() { |
| 89 | float roots[2]; |
| 90 | |
| 91 | float C = sqr(fRelX) + sqr(fRelY) - fRadius2; |
| 92 | int countRoots = find_quad_roots(fA, fB, C, roots); |
| 93 | |
| 94 | fRelX += fIncX; |
| 95 | fRelY += fIncY; |
| 96 | fB += fDB; |
| 97 | |
| 98 | if (0 == countRoots) { |
| 99 | return kDontDrawT; |
| 100 | } |
| 101 | |
| 102 | // Prefer the bigger t value if both give a radius(t) > 0 |
| 103 | // find_quad_roots returns the values sorted, so we start with the last |
| 104 | float t = roots[countRoots - 1]; |
| 105 | float r = lerp(fRadius, fDRadius, t); |
| 106 | if (r <= 0) { |
| 107 | t = roots[0]; // might be the same as roots[countRoots-1] |
| 108 | r = lerp(fRadius, fDRadius, t); |
| 109 | if (r <= 0) { |
| 110 | return kDontDrawT; |
| 111 | } |
| 112 | } |
| 113 | return SkFloatToFixed(t); |
| 114 | } |
| 115 | |
| 116 | typedef void (*TwoPointRadialProc)(TwoPtRadial* rec, SkPMColor* dstC, |
| 117 | const SkPMColor* cache, int count); |
| 118 | |
| 119 | static void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, |
| 120 | const SkPMColor* SK_RESTRICT cache, int count) { |
| 121 | for (; count > 0; --count) { |
| 122 | SkFixed t = rec->nextT(); |
| 123 | if (TwoPtRadial::DontDrawT(t)) { |
| 124 | *dstC++ = 0; |
| 125 | } else { |
| 126 | SkFixed index = SkClampMax(t, 0xFFFF); |
| 127 | SkASSERT(index <= 0xFFFF); |
| 128 | *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift]; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, |
| 134 | const SkPMColor* SK_RESTRICT cache, int count) { |
| 135 | for (; count > 0; --count) { |
| 136 | SkFixed t = rec->nextT(); |
| 137 | if (TwoPtRadial::DontDrawT(t)) { |
| 138 | *dstC++ = 0; |
| 139 | } else { |
| 140 | SkFixed index = repeat_tileproc(t); |
| 141 | SkASSERT(index <= 0xFFFF); |
| 142 | *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift]; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, |
| 148 | const SkPMColor* SK_RESTRICT cache, int count) { |
| 149 | for (; count > 0; --count) { |
| 150 | SkFixed t = rec->nextT(); |
| 151 | if (TwoPtRadial::DontDrawT(t)) { |
| 152 | *dstC++ = 0; |
| 153 | } else { |
| 154 | SkFixed index = mirror_tileproc(t); |
| 155 | SkASSERT(index <= 0xFFFF); |
| 156 | *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift]; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void SkTwoPointConicalGradient::init() { |
| 162 | fRec.init(fCenter1, fRadius1, fCenter2, fRadius2); |
| 163 | fPtsToUnit.reset(); |
| 164 | } |
| 165 | |
| 166 | SkTwoPointConicalGradient::SkTwoPointConicalGradient( |
| 167 | const SkPoint& start, SkScalar startRadius, |
| 168 | const SkPoint& end, SkScalar endRadius, |
| 169 | const SkColor colors[], const SkScalar pos[], |
| 170 | int colorCount, SkShader::TileMode mode, |
| 171 | SkUnitMapper* mapper) |
| 172 | : SkGradientShaderBase(colors, pos, colorCount, mode, mapper), |
| 173 | fCenter1(start), |
| 174 | fCenter2(end), |
| 175 | fRadius1(startRadius), |
| 176 | fRadius2(endRadius) { |
| 177 | // this is degenerate, and should be caught by our caller |
| 178 | SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2); |
| 179 | this->init(); |
| 180 | } |
| 181 | |
| 182 | void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam, |
| 183 | int count) { |
| 184 | SkASSERT(count > 0); |
| 185 | |
| 186 | SkPMColor* SK_RESTRICT dstC = dstCParam; |
| 187 | |
| 188 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
| 189 | TileProc proc = fTileProc; |
| 190 | const SkPMColor* SK_RESTRICT cache = this->getCache32(); |
| 191 | |
| 192 | TwoPointRadialProc shadeProc = twopoint_repeat; |
| 193 | if (SkShader::kClamp_TileMode == fTileMode) { |
| 194 | shadeProc = twopoint_clamp; |
| 195 | } else if (SkShader::kMirror_TileMode == fTileMode) { |
| 196 | shadeProc = twopoint_mirror; |
| 197 | } else { |
| 198 | SkASSERT(SkShader::kRepeat_TileMode == fTileMode); |
| 199 | } |
| 200 | |
| 201 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 202 | SkPoint srcPt; |
| 203 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 204 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 205 | SkScalar dx, fx = srcPt.fX; |
| 206 | SkScalar dy, fy = srcPt.fY; |
| 207 | |
| 208 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 209 | SkFixed fixedX, fixedY; |
| 210 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); |
| 211 | dx = SkFixedToScalar(fixedX); |
| 212 | dy = SkFixedToScalar(fixedY); |
| 213 | } else { |
| 214 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 215 | dx = fDstToIndex.getScaleX(); |
| 216 | dy = fDstToIndex.getSkewY(); |
| 217 | } |
| 218 | |
| 219 | fRec.setup(fx, fy, dx, dy); |
| 220 | (*shadeProc)(&fRec, dstC, cache, count); |
| 221 | } else { // perspective case |
| 222 | SkScalar dstX = SkIntToScalar(x); |
| 223 | SkScalar dstY = SkIntToScalar(y); |
| 224 | for (; count > 0; --count) { |
| 225 | SkPoint srcPt; |
| 226 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 227 | dstX += SK_Scalar1; |
| 228 | |
| 229 | fRec.setup(srcPt.fX, srcPt.fY, 0, 0); |
| 230 | (*shadeProc)(&fRec, dstC, cache, 1); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | bool SkTwoPointConicalGradient::setContext(const SkBitmap& device, |
| 236 | const SkPaint& paint, |
| 237 | const SkMatrix& matrix) { |
| 238 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // we don't have a span16 proc |
| 243 | fFlags &= ~kHasSpan16_Flag; |
| 244 | |
| 245 | // in general, we might discard based on computed-radius, so clear |
| 246 | // this flag (todo: sometimes we can detect that we never discard...) |
| 247 | fFlags &= ~kOpaqueAlpha_Flag; |
| 248 | |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | SkShader::BitmapType SkTwoPointConicalGradient::asABitmap( |
| 253 | SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const { |
| 254 | SkPoint diff = fCenter2 - fCenter1; |
| 255 | SkScalar diffRadius = fRadius2 - fRadius1; |
| 256 | SkScalar startRadius = fRadius1; |
| 257 | SkScalar diffLen = 0; |
| 258 | |
| 259 | if (bitmap) { |
| 260 | this->commonAsABitmap(bitmap); |
| 261 | } |
| 262 | if (matrix) { |
| 263 | diffLen = diff.length(); |
| 264 | } |
| 265 | if (matrix) { |
| 266 | if (diffLen) { |
| 267 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
| 268 | // rotate to align circle centers with the x-axis |
| 269 | matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY), |
| 270 | SkScalarMul(invDiffLen, diff.fX)); |
| 271 | } else { |
| 272 | matrix->reset(); |
| 273 | } |
| 274 | matrix->preTranslate(-fCenter1.fX, -fCenter1.fY); |
| 275 | } |
| 276 | if (xy) { |
| 277 | xy[0] = fTileMode; |
| 278 | xy[1] = kClamp_TileMode; |
| 279 | } |
| 280 | return kTwoPointConical_BitmapType; |
| 281 | } |
| 282 | |
| 283 | SkShader::GradientType SkTwoPointConicalGradient::asAGradient( |
| 284 | GradientInfo* info) const { |
| 285 | if (info) { |
| 286 | commonAsAGradient(info); |
| 287 | info->fPoint[0] = fCenter1; |
| 288 | info->fPoint[1] = fCenter2; |
| 289 | info->fRadius[0] = fRadius1; |
| 290 | info->fRadius[1] = fRadius2; |
| 291 | } |
| 292 | return kConical_GradientType; |
| 293 | } |
| 294 | |
| 295 | GrCustomStage* SkTwoPointConicalGradient::asNewCustomStage( |
| 296 | GrContext* context, GrSamplerState* sampler) const { |
| 297 | SkASSERT(NULL != context && NULL != sampler); |
| 298 | SkPoint diff = fCenter2 - fCenter1; |
| 299 | SkScalar diffLen = diff.length(); |
| 300 | if (0 != diffLen) { |
| 301 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
| 302 | sampler->matrix()->setSinCos(-SkScalarMul(invDiffLen, diff.fY), |
| 303 | SkScalarMul(invDiffLen, diff.fX)); |
| 304 | } else { |
| 305 | sampler->matrix()->reset(); |
| 306 | } |
| 307 | sampler->matrix()->preTranslate(-fCenter1.fX, -fCenter1.fY); |
| 308 | sampler->textureParams()->setTileModeX(fTileMode); |
| 309 | sampler->textureParams()->setTileModeY(kClamp_TileMode); |
| 310 | sampler->textureParams()->setBilerp(true); |
| 311 | return SkNEW_ARGS(GrConical2Gradient, (context, *this, sampler, |
| 312 | diffLen, fRadius1, fRadius2 - fRadius1)); |
| 313 | } |
| 314 | |
| 315 | SkTwoPointConicalGradient::SkTwoPointConicalGradient( |
| 316 | SkFlattenableReadBuffer& buffer) |
| 317 | : INHERITED(buffer), |
| 318 | fCenter1(buffer.readPoint()), |
| 319 | fCenter2(buffer.readPoint()), |
| 320 | fRadius1(buffer.readScalar()), |
| 321 | fRadius2(buffer.readScalar()) { |
| 322 | this->init(); |
| 323 | }; |
| 324 | |
| 325 | void SkTwoPointConicalGradient::flatten( |
| 326 | SkFlattenableWriteBuffer& buffer) const { |
| 327 | this->INHERITED::flatten(buffer); |
| 328 | buffer.writePoint(fCenter1); |
| 329 | buffer.writePoint(fCenter2); |
| 330 | buffer.writeScalar(fRadius1); |
| 331 | buffer.writeScalar(fRadius2); |
| 332 | } |
| 333 | |