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); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 24 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 25 | if (A == 0) { |
| 26 | return valid_divide(-C, B, roots); |
| 27 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 28 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 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]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 90 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 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 | |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 116 | typedef void (*TwoPointConicalProc)(TwoPtRadial* rec, SkPMColor* dstC, |
| 117 | const SkPMColor* cache, int toggle, int count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 118 | |
| 119 | static void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 120 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 121 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 122 | for (; count > 0; --count) { |
| 123 | SkFixed t = rec->nextT(); |
| 124 | if (TwoPtRadial::DontDrawT(t)) { |
| 125 | *dstC++ = 0; |
| 126 | } else { |
| 127 | SkFixed index = SkClampMax(t, 0xFFFF); |
| 128 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 129 | *dstC++ = cache[toggle + |
| 130 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 131 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 132 | #ifndef SK_IGNORE_GRADIENT_DITHER_FIX |
| 133 | toggle = next_dither_toggle(toggle); |
| 134 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 139 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 140 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 141 | for (; count > 0; --count) { |
| 142 | SkFixed t = rec->nextT(); |
| 143 | if (TwoPtRadial::DontDrawT(t)) { |
| 144 | *dstC++ = 0; |
| 145 | } else { |
| 146 | SkFixed index = repeat_tileproc(t); |
| 147 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 148 | *dstC++ = cache[toggle + |
| 149 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 150 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 151 | #ifndef SK_IGNORE_GRADIENT_DITHER_FIX |
| 152 | toggle = next_dither_toggle(toggle); |
| 153 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 158 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 159 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 160 | for (; count > 0; --count) { |
| 161 | SkFixed t = rec->nextT(); |
| 162 | if (TwoPtRadial::DontDrawT(t)) { |
| 163 | *dstC++ = 0; |
| 164 | } else { |
| 165 | SkFixed index = mirror_tileproc(t); |
| 166 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 167 | *dstC++ = cache[toggle + |
| 168 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 169 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 170 | #ifndef SK_IGNORE_GRADIENT_DITHER_FIX |
| 171 | toggle = next_dither_toggle(toggle); |
| 172 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | void SkTwoPointConicalGradient::init() { |
| 177 | fRec.init(fCenter1, fRadius1, fCenter2, fRadius2); |
| 178 | fPtsToUnit.reset(); |
| 179 | } |
| 180 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 181 | ///////////////////////////////////////////////////////////////////// |
| 182 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 183 | SkTwoPointConicalGradient::SkTwoPointConicalGradient( |
| 184 | const SkPoint& start, SkScalar startRadius, |
| 185 | const SkPoint& end, SkScalar endRadius, |
| 186 | const SkColor colors[], const SkScalar pos[], |
| 187 | int colorCount, SkShader::TileMode mode, |
| 188 | SkUnitMapper* mapper) |
| 189 | : SkGradientShaderBase(colors, pos, colorCount, mode, mapper), |
| 190 | fCenter1(start), |
| 191 | fCenter2(end), |
| 192 | fRadius1(startRadius), |
| 193 | fRadius2(endRadius) { |
| 194 | // this is degenerate, and should be caught by our caller |
| 195 | SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2); |
| 196 | this->init(); |
| 197 | } |
| 198 | |
| 199 | void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam, |
| 200 | int count) { |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 201 | #ifndef SK_IGNORE_GRADIENT_DITHER_FIX |
| 202 | int toggle = init_dither_toggle(x, y); |
| 203 | #else |
| 204 | int toggle = 0; |
| 205 | #endif |
| 206 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 207 | SkASSERT(count > 0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 208 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 209 | SkPMColor* SK_RESTRICT dstC = dstCParam; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 210 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 211 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 212 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 213 | const SkPMColor* SK_RESTRICT cache = this->getCache32(); |
| 214 | |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 215 | TwoPointConicalProc shadeProc = twopoint_repeat; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 216 | if (SkShader::kClamp_TileMode == fTileMode) { |
| 217 | shadeProc = twopoint_clamp; |
| 218 | } else if (SkShader::kMirror_TileMode == fTileMode) { |
| 219 | shadeProc = twopoint_mirror; |
| 220 | } else { |
| 221 | SkASSERT(SkShader::kRepeat_TileMode == fTileMode); |
| 222 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 223 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 224 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 225 | SkPoint srcPt; |
| 226 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 227 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 228 | SkScalar dx, fx = srcPt.fX; |
| 229 | SkScalar dy, fy = srcPt.fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 230 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 231 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 232 | SkFixed fixedX, fixedY; |
| 233 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); |
| 234 | dx = SkFixedToScalar(fixedX); |
| 235 | dy = SkFixedToScalar(fixedY); |
| 236 | } else { |
| 237 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 238 | dx = fDstToIndex.getScaleX(); |
| 239 | dy = fDstToIndex.getSkewY(); |
| 240 | } |
| 241 | |
| 242 | fRec.setup(fx, fy, dx, dy); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 243 | (*shadeProc)(&fRec, dstC, cache, toggle, count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 244 | } else { // perspective case |
| 245 | SkScalar dstX = SkIntToScalar(x); |
| 246 | SkScalar dstY = SkIntToScalar(y); |
| 247 | for (; count > 0; --count) { |
| 248 | SkPoint srcPt; |
| 249 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 250 | dstX += SK_Scalar1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 251 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 252 | fRec.setup(srcPt.fX, srcPt.fY, 0, 0); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 253 | (*shadeProc)(&fRec, dstC, cache, toggle, 1); |
| 254 | #ifndef SK_IGNORE_GRADIENT_DITHER_FIX |
| 255 | toggle = next_dither_toggle(toggle); |
| 256 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | bool SkTwoPointConicalGradient::setContext(const SkBitmap& device, |
| 262 | const SkPaint& paint, |
| 263 | const SkMatrix& matrix) { |
| 264 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 265 | return false; |
| 266 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 267 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 268 | // we don't have a span16 proc |
| 269 | fFlags &= ~kHasSpan16_Flag; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 270 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 271 | // in general, we might discard based on computed-radius, so clear |
| 272 | // this flag (todo: sometimes we can detect that we never discard...) |
| 273 | fFlags &= ~kOpaqueAlpha_Flag; |
| 274 | |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | SkShader::BitmapType SkTwoPointConicalGradient::asABitmap( |
| 279 | SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const { |
| 280 | SkPoint diff = fCenter2 - fCenter1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 281 | SkScalar diffLen = 0; |
| 282 | |
| 283 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 284 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 285 | } |
| 286 | if (matrix) { |
| 287 | diffLen = diff.length(); |
| 288 | } |
| 289 | if (matrix) { |
| 290 | if (diffLen) { |
| 291 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
| 292 | // rotate to align circle centers with the x-axis |
| 293 | matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY), |
| 294 | SkScalarMul(invDiffLen, diff.fX)); |
| 295 | } else { |
| 296 | matrix->reset(); |
| 297 | } |
| 298 | matrix->preTranslate(-fCenter1.fX, -fCenter1.fY); |
| 299 | } |
| 300 | if (xy) { |
| 301 | xy[0] = fTileMode; |
| 302 | xy[1] = kClamp_TileMode; |
| 303 | } |
| 304 | return kTwoPointConical_BitmapType; |
| 305 | } |
| 306 | |
| 307 | SkShader::GradientType SkTwoPointConicalGradient::asAGradient( |
| 308 | GradientInfo* info) const { |
| 309 | if (info) { |
| 310 | commonAsAGradient(info); |
| 311 | info->fPoint[0] = fCenter1; |
| 312 | info->fPoint[1] = fCenter2; |
| 313 | info->fRadius[0] = fRadius1; |
| 314 | info->fRadius[1] = fRadius2; |
| 315 | } |
| 316 | return kConical_GradientType; |
| 317 | } |
| 318 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 319 | SkTwoPointConicalGradient::SkTwoPointConicalGradient( |
| 320 | SkFlattenableReadBuffer& buffer) |
| 321 | : INHERITED(buffer), |
| 322 | fCenter1(buffer.readPoint()), |
| 323 | fCenter2(buffer.readPoint()), |
| 324 | fRadius1(buffer.readScalar()), |
| 325 | fRadius2(buffer.readScalar()) { |
| 326 | this->init(); |
| 327 | }; |
| 328 | |
| 329 | void SkTwoPointConicalGradient::flatten( |
| 330 | SkFlattenableWriteBuffer& buffer) const { |
| 331 | this->INHERITED::flatten(buffer); |
| 332 | buffer.writePoint(fCenter1); |
| 333 | buffer.writePoint(fCenter2); |
| 334 | buffer.writeScalar(fRadius1); |
| 335 | buffer.writeScalar(fRadius2); |
| 336 | } |
| 337 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 338 | ///////////////////////////////////////////////////////////////////// |
| 339 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 340 | #if SK_SUPPORT_GPU |
| 341 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 342 | #include "GrTBackendEffectFactory.h" |
| 343 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 344 | // For brevity |
| 345 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 346 | static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle; |
| 347 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 348 | class GrGLConical2Gradient : public GrGLGradientEffect { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 349 | public: |
| 350 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 351 | GrGLConical2Gradient(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 352 | const GrEffectRef&); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 353 | virtual ~GrGLConical2Gradient() { } |
| 354 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 355 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 356 | const GrEffectStage&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 357 | EffectKey, |
| 358 | const char* vertexCoords, |
| 359 | const char* outputColor, |
| 360 | const char* inputColor, |
| 361 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 362 | virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 363 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 364 | static EffectKey GenKey(const GrEffectStage&, const GrGLCaps& caps); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 365 | |
| 366 | protected: |
| 367 | |
| 368 | UniformHandle fVSParamUni; |
| 369 | UniformHandle fFSParamUni; |
| 370 | |
| 371 | const char* fVSVaryingName; |
| 372 | const char* fFSVaryingName; |
| 373 | |
| 374 | bool fIsDegenerate; |
| 375 | |
| 376 | // @{ |
| 377 | /// Values last uploaded as uniforms |
| 378 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 379 | SkScalar fCachedCenter; |
| 380 | SkScalar fCachedRadius; |
| 381 | SkScalar fCachedDiffRadius; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 382 | |
| 383 | // @} |
| 384 | |
| 385 | private: |
| 386 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 387 | typedef GrGLGradientEffect INHERITED; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 388 | |
| 389 | }; |
| 390 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 391 | ///////////////////////////////////////////////////////////////////// |
| 392 | |
| 393 | class GrConical2Gradient : public GrGradientEffect { |
| 394 | public: |
| 395 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 396 | static GrEffectRef* Create(GrContext* ctx, |
| 397 | const SkTwoPointConicalGradient& shader, |
| 398 | const SkMatrix& matrix, |
| 399 | SkShader::TileMode tm) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 400 | AutoEffectUnref effect(SkNEW_ARGS(GrConical2Gradient, (ctx, shader, matrix, tm))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 401 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 402 | } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 403 | |
| 404 | virtual ~GrConical2Gradient() { } |
| 405 | |
| 406 | static const char* Name() { return "Two-Point Conical Gradient"; } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 407 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 408 | return GrTBackendEffectFactory<GrConical2Gradient>::getInstance(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 409 | } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 410 | |
| 411 | // The radial gradient parameters can collapse to a linear (instead of quadratic) equation. |
| 412 | bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); } |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 413 | SkScalar center() const { return fCenterX1; } |
| 414 | SkScalar diffRadius() const { return fDiffRadius; } |
| 415 | SkScalar radius() const { return fRadius0; } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 416 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 417 | typedef GrGLConical2Gradient GLEffect; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 418 | |
| 419 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 420 | virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 421 | const GrConical2Gradient& s = CastEffect<GrConical2Gradient>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 422 | return (INHERITED::onIsEqual(sBase) && |
| 423 | this->fCenterX1 == s.fCenterX1 && |
| 424 | this->fRadius0 == s.fRadius0 && |
| 425 | this->fDiffRadius == s.fDiffRadius); |
| 426 | } |
| 427 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 428 | GrConical2Gradient(GrContext* ctx, |
| 429 | const SkTwoPointConicalGradient& shader, |
| 430 | const SkMatrix& matrix, |
| 431 | SkShader::TileMode tm) |
| 432 | : INHERITED(ctx, shader, matrix, tm) |
| 433 | , fCenterX1(shader.getCenterX1()) |
| 434 | , fRadius0(shader.getStartRadius()) |
| 435 | , fDiffRadius(shader.getDiffRadius()) { } |
| 436 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 437 | GR_DECLARE_EFFECT_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 438 | |
| 439 | // @{ |
| 440 | // Cache of values - these can change arbitrarily, EXCEPT |
| 441 | // we shouldn't change between degenerate and non-degenerate?! |
| 442 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 443 | SkScalar fCenterX1; |
| 444 | SkScalar fRadius0; |
| 445 | SkScalar fDiffRadius; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 446 | |
| 447 | // @} |
| 448 | |
| 449 | typedef GrGradientEffect INHERITED; |
| 450 | }; |
| 451 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 452 | GR_DEFINE_EFFECT_TEST(GrConical2Gradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 453 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 454 | GrEffectRef* GrConical2Gradient::TestCreate(SkRandom* random, |
| 455 | GrContext* context, |
| 456 | GrTexture**) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 457 | SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()}; |
| 458 | SkScalar radius1 = random->nextUScalar1(); |
| 459 | SkPoint center2; |
| 460 | SkScalar radius2; |
| 461 | do { |
bsalomon@google.com | fb883bf | 2012-12-11 15:32:04 +0000 | [diff] [blame] | 462 | center2.set(random->nextUScalar1(), random->nextUScalar1()); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 463 | radius2 = random->nextUScalar1 (); |
| 464 | // If the circles are identical the factory will give us an empty shader. |
| 465 | } while (radius1 == radius2 && center1 == center2); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 466 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 467 | SkColor colors[kMaxRandomGradientColors]; |
| 468 | SkScalar stopsArray[kMaxRandomGradientColors]; |
| 469 | SkScalar* stops = stopsArray; |
| 470 | SkShader::TileMode tm; |
| 471 | int colorCount = RandomGradientParams(random, colors, &stops, &tm); |
| 472 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1, |
| 473 | center2, radius2, |
| 474 | colors, stops, colorCount, |
| 475 | tm)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 476 | SkPaint paint; |
| 477 | return shader->asNewEffect(context, paint); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 481 | ///////////////////////////////////////////////////////////////////// |
| 482 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 483 | GrGLConical2Gradient::GrGLConical2Gradient(const GrBackendEffectFactory& factory, |
| 484 | const GrEffectRef& baseData) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 485 | : INHERITED(factory) |
| 486 | , fVSParamUni(kInvalidUniformHandle) |
| 487 | , fFSParamUni(kInvalidUniformHandle) |
| 488 | , fVSVaryingName(NULL) |
| 489 | , fFSVaryingName(NULL) |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 490 | , fCachedCenter(SK_ScalarMax) |
| 491 | , fCachedRadius(-SK_ScalarMax) |
| 492 | , fCachedDiffRadius(-SK_ScalarMax) { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 493 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 494 | const GrConical2Gradient& data = CastEffect<GrConical2Gradient>(baseData); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 495 | fIsDegenerate = data.isDegenerate(); |
| 496 | } |
| 497 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 498 | void GrGLConical2Gradient::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 499 | const GrEffectStage& stage, |
| 500 | EffectKey key, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 501 | const char* vertexCoords, |
| 502 | const char* outputColor, |
| 503 | const char* inputColor, |
| 504 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 505 | const char* fsCoords; |
| 506 | const char* vsCoordsVarying; |
| 507 | GrSLType coordsVaryingType; |
| 508 | this->setupMatrix(builder, key, vertexCoords, &fsCoords, &vsCoordsVarying, &coordsVaryingType); |
| 509 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 510 | this->emitYCoordUniform(builder); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 511 | // 2 copies of uniform array, 1 for each of vertex & fragment shader, |
| 512 | // to work around Xoom bug. Doesn't seem to cause performance decrease |
| 513 | // in test apps, but need to keep an eye on it. |
| 514 | fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType, |
| 515 | kFloat_GrSLType, "Conical2VSParams", 6); |
| 516 | fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType, |
| 517 | kFloat_GrSLType, "Conical2FSParams", 6); |
| 518 | |
| 519 | // For radial gradients without perspective we can pass the linear |
| 520 | // part of the quadratic as a varying. |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 521 | if (kVec2f_GrSLType == coordsVaryingType) { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 522 | builder->addVarying(kFloat_GrSLType, "Conical2BCoeff", |
| 523 | &fVSVaryingName, &fFSVaryingName); |
| 524 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 525 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 526 | // VS |
| 527 | { |
| 528 | SkString* code = &builder->fVSCode; |
| 529 | SkString p2; // distance between centers |
| 530 | SkString p3; // start radius |
| 531 | SkString p5; // difference in radii (r1 - r0) |
| 532 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2); |
| 533 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3); |
| 534 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 535 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 536 | // For radial gradients without perspective we can pass the linear |
| 537 | // part of the quadratic as a varying. |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 538 | if (kVec2f_GrSLType == coordsVaryingType) { |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 539 | // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5]) |
| 540 | code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n", |
| 541 | fVSVaryingName, p2.c_str(), |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 542 | vsCoordsVarying, p3.c_str(), p5.c_str()); |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 543 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 544 | } |
| 545 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 546 | // FS |
| 547 | { |
| 548 | SkString* code = &builder->fFSCode; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 549 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 550 | SkString cName("c"); |
| 551 | SkString ac4Name("ac4"); |
| 552 | SkString dName("d"); |
| 553 | SkString qName("q"); |
| 554 | SkString r0Name("r0"); |
| 555 | SkString r1Name("r1"); |
| 556 | SkString tName("t"); |
| 557 | SkString p0; // 4a |
| 558 | SkString p1; // 1/a |
| 559 | SkString p2; // distance between centers |
| 560 | SkString p3; // start radius |
| 561 | SkString p4; // start radius squared |
| 562 | SkString p5; // difference in radii (r1 - r0) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 563 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 564 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0); |
| 565 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1); |
| 566 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2); |
| 567 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3); |
| 568 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4); |
| 569 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 570 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 571 | // If we we're able to interpolate the linear component, |
| 572 | // bVar is the varying; otherwise compute it |
| 573 | SkString bVar; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 574 | if (kVec2f_GrSLType == coordsVaryingType) { |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 575 | bVar = fFSVaryingName; |
| 576 | } else { |
| 577 | bVar = "b"; |
| 578 | code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n", |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 579 | bVar.c_str(), p2.c_str(), fsCoords, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 580 | p3.c_str(), p5.c_str()); |
| 581 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 582 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 583 | // output will default to transparent black (we simply won't write anything |
| 584 | // else to it if invalid, instead of discarding or returning prematurely) |
| 585 | code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 586 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 587 | // c = (x^2)+(y^2) - params[4] |
| 588 | code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(), |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 589 | fsCoords, fsCoords, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 590 | p4.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 591 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 592 | // Non-degenerate case (quadratic) |
| 593 | if (!fIsDegenerate) { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 594 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 595 | // ac4 = params[0] * c |
| 596 | code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(), |
| 597 | cName.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 598 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 599 | // d = b^2 - ac4 |
| 600 | code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(), |
| 601 | bVar.c_str(), bVar.c_str(), ac4Name.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 602 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 603 | // only proceed if discriminant is >= 0 |
| 604 | code->appendf("\tif (%s >= 0.0) {\n", dName.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 605 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 606 | // intermediate value we'll use to compute the roots |
| 607 | // q = -0.5 * (b +/- sqrt(d)) |
| 608 | code->appendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)" |
| 609 | " * sqrt(%s));\n", qName.c_str(), bVar.c_str(), |
| 610 | bVar.c_str(), dName.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 611 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 612 | // compute both roots |
| 613 | // r0 = q * params[1] |
| 614 | code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(), |
| 615 | qName.c_str(), p1.c_str()); |
| 616 | // r1 = c / q |
| 617 | code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(), |
| 618 | cName.c_str(), qName.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 619 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 620 | // Note: If there are two roots that both generate radius(t) > 0, the |
| 621 | // Canvas spec says to choose the larger t. |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 622 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 623 | // so we'll look at the larger one first: |
| 624 | code->appendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(), |
| 625 | r0Name.c_str(), r1Name.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 626 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 627 | // if r(t) > 0, then we're done; t will be our x coordinate |
| 628 | code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(), |
| 629 | p5.c_str(), p3.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 630 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 631 | code->appendf("\t\t"); |
| 632 | this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 633 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 634 | // otherwise, if r(t) for the larger root was <= 0, try the other root |
| 635 | code->appendf("\t\t} else {\n"); |
| 636 | code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(), |
| 637 | r0Name.c_str(), r1Name.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 638 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 639 | // if r(t) > 0 for the smaller root, then t will be our x coordinate |
| 640 | code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n", |
| 641 | tName.c_str(), p5.c_str(), p3.c_str()); |
| 642 | |
| 643 | code->appendf("\t\t\t"); |
| 644 | this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]); |
| 645 | |
| 646 | // end if (r(t) > 0) for smaller root |
| 647 | code->appendf("\t\t\t}\n"); |
| 648 | // end if (r(t) > 0), else, for larger root |
| 649 | code->appendf("\t\t}\n"); |
| 650 | // end if (discriminant >= 0) |
| 651 | code->appendf("\t}\n"); |
| 652 | } else { |
| 653 | |
| 654 | // linear case: t = -c/b |
| 655 | code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), |
| 656 | cName.c_str(), bVar.c_str()); |
| 657 | |
| 658 | // if r(t) > 0, then t will be the x coordinate |
| 659 | code->appendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(), |
| 660 | p5.c_str(), p3.c_str()); |
| 661 | code->appendf("\t"); |
| 662 | this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]); |
| 663 | code->appendf("\t}\n"); |
| 664 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 668 | void GrGLConical2Gradient::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
| 669 | INHERITED::setData(uman, stage); |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 670 | const GrConical2Gradient& data = GetEffectFromStage<GrConical2Gradient>(stage); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 671 | GrAssert(data.isDegenerate() == fIsDegenerate); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 672 | SkScalar centerX1 = data.center(); |
| 673 | SkScalar radius0 = data.radius(); |
| 674 | SkScalar diffRadius = data.diffRadius(); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 675 | |
| 676 | if (fCachedCenter != centerX1 || |
| 677 | fCachedRadius != radius0 || |
| 678 | fCachedDiffRadius != diffRadius) { |
| 679 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 680 | SkScalar a = SkScalarMul(centerX1, centerX1) - diffRadius * diffRadius; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 681 | |
| 682 | // When we're in the degenerate (linear) case, the second |
| 683 | // value will be INF but the program doesn't read it. (We |
| 684 | // use the same 6 uniforms even though we don't need them |
| 685 | // all in the linear case just to keep the code complexity |
| 686 | // down). |
| 687 | float values[6] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 688 | SkScalarToFloat(a * 4), |
| 689 | 1.f / (SkScalarToFloat(a)), |
| 690 | SkScalarToFloat(centerX1), |
| 691 | SkScalarToFloat(radius0), |
| 692 | SkScalarToFloat(SkScalarMul(radius0, radius0)), |
| 693 | SkScalarToFloat(diffRadius) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 694 | }; |
| 695 | |
| 696 | uman.set1fv(fVSParamUni, 0, 6, values); |
| 697 | uman.set1fv(fFSParamUni, 0, 6, values); |
| 698 | fCachedCenter = centerX1; |
| 699 | fCachedRadius = radius0; |
| 700 | fCachedDiffRadius = diffRadius; |
| 701 | } |
| 702 | } |
| 703 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 704 | GrGLEffect::EffectKey GrGLConical2Gradient::GenKey(const GrEffectStage& s, const GrGLCaps&) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 705 | enum { |
| 706 | kIsDegenerate = 1 << kMatrixKeyBitCnt, |
| 707 | }; |
| 708 | |
| 709 | EffectKey key = GenMatrixKey(s); |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 710 | if (GetEffectFromStage<GrConical2Gradient>(s).isDegenerate()) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 711 | key |= kIsDegenerate; |
| 712 | } |
| 713 | return key; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 714 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 715 | |
| 716 | ///////////////////////////////////////////////////////////////////// |
| 717 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 718 | GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext* context, const SkPaint&) const { |
bsalomon@google.com | 00835cc | 2013-01-14 17:07:22 +0000 | [diff] [blame] | 719 | SkASSERT(NULL != context); |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 720 | SkASSERT(fPtsToUnit.isIdentity()); |
| 721 | // invert the localM, translate to center1, rotate so center2 is on x axis. |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 722 | SkMatrix matrix; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 723 | if (!this->getLocalMatrix().invert(&matrix)) { |
humper@google.com | 84831ac | 2013-01-14 22:09:54 +0000 | [diff] [blame] | 724 | return NULL; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 725 | } |
| 726 | matrix.postTranslate(-fCenter1.fX, -fCenter1.fY); |
| 727 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 728 | SkPoint diff = fCenter2 - fCenter1; |
| 729 | SkScalar diffLen = diff.length(); |
| 730 | if (0 != diffLen) { |
| 731 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 732 | SkMatrix rot; |
| 733 | rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY), |
| 734 | SkScalarMul(invDiffLen, diff.fX)); |
| 735 | matrix.postConcat(rot); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 736 | } |
| 737 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 738 | return GrConical2Gradient::Create(context, *this, matrix, fTileMode); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 739 | } |
| 740 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 741 | #else |
| 742 | |
bsalomon@google.com | 5d2cd20 | 2013-01-16 15:31:06 +0000 | [diff] [blame] | 743 | GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext*, const SkPaint&) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 744 | SkDEBUGFAIL("Should not call in GPU-less build"); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 745 | return NULL; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 746 | } |
| 747 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 748 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 749 | |
| 750 | #ifdef SK_DEVELOPER |
| 751 | void SkTwoPointConicalGradient::toString(SkString* str) const { |
| 752 | str->append("SkTwoPointConicalGradient: ("); |
| 753 | |
| 754 | str->append("center1: ("); |
| 755 | str->appendScalar(fCenter1.fX); |
| 756 | str->append(", "); |
| 757 | str->appendScalar(fCenter1.fY); |
| 758 | str->append(") radius1: "); |
| 759 | str->appendScalar(fRadius1); |
| 760 | str->append(" "); |
| 761 | |
| 762 | str->append("center2: ("); |
| 763 | str->appendScalar(fCenter2.fX); |
| 764 | str->append(", "); |
| 765 | str->appendScalar(fCenter2.fY); |
| 766 | str->append(") radius2: "); |
| 767 | str->appendScalar(fRadius2); |
| 768 | str->append(" "); |
| 769 | |
| 770 | this->INHERITED::toString(str); |
| 771 | |
| 772 | str->append(")"); |
| 773 | } |
| 774 | #endif |