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