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