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 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 9 | #include "SkTwoPointRadialGradient.h" |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 10 | |
| 11 | /* Two-point radial gradients are specified by two circles, each with a center |
| 12 | point and radius. The gradient can be considered to be a series of |
| 13 | concentric circles, with the color interpolated from the start circle |
| 14 | (at t=0) to the end circle (at t=1). |
| 15 | |
| 16 | For each point (x, y) in the span, we want to find the |
| 17 | interpolated circle that intersects that point. The center |
| 18 | of the desired circle (Cx, Cy) falls at some distance t |
| 19 | along the line segment between the start point (Sx, Sy) and |
| 20 | end point (Ex, Ey): |
| 21 | |
| 22 | Cx = (1 - t) * Sx + t * Ex (0 <= t <= 1) |
| 23 | Cy = (1 - t) * Sy + t * Ey |
| 24 | |
| 25 | The radius of the desired circle (r) is also a linear interpolation t |
| 26 | between the start and end radii (Sr and Er): |
| 27 | |
| 28 | r = (1 - t) * Sr + t * Er |
| 29 | |
| 30 | But |
| 31 | |
| 32 | (x - Cx)^2 + (y - Cy)^2 = r^2 |
| 33 | |
| 34 | so |
| 35 | |
| 36 | (x - ((1 - t) * Sx + t * Ex))^2 |
| 37 | + (y - ((1 - t) * Sy + t * Ey))^2 |
| 38 | = ((1 - t) * Sr + t * Er)^2 |
| 39 | |
| 40 | Solving for t yields |
| 41 | |
| 42 | [(Sx - Ex)^2 + (Sy - Ey)^2 - (Er - Sr)^2)] * t^2 |
| 43 | + [2 * (Sx - Ex)(x - Sx) + 2 * (Sy - Ey)(y - Sy) - 2 * (Er - Sr) * Sr] * t |
| 44 | + [(x - Sx)^2 + (y - Sy)^2 - Sr^2] = 0 |
| 45 | |
| 46 | To simplify, let Dx = Sx - Ex, Dy = Sy - Ey, Dr = Er - Sr, dx = x - Sx, dy = y - Sy |
| 47 | |
| 48 | [Dx^2 + Dy^2 - Dr^2)] * t^2 |
| 49 | + 2 * [Dx * dx + Dy * dy - Dr * Sr] * t |
| 50 | + [dx^2 + dy^2 - Sr^2] = 0 |
| 51 | |
| 52 | A quadratic in t. The two roots of the quadratic reflect the two |
| 53 | possible circles on which the point may fall. Solving for t yields |
| 54 | the gradient value to use. |
| 55 | |
| 56 | If a<0, the start circle is entirely contained in the |
| 57 | end circle, and one of the roots will be <0 or >1 (off the line |
| 58 | segment). If a>0, the start circle falls at least partially |
| 59 | outside the end circle (or vice versa), and the gradient |
| 60 | defines a "tube" where a point may be on one circle (on the |
| 61 | inside of the tube) or the other (outside of the tube). We choose |
| 62 | one arbitrarily. |
| 63 | |
| 64 | In order to keep the math to within the limits of fixed point, |
| 65 | we divide the entire quadratic by Dr^2, and replace |
| 66 | (x - Sx)/Dr with x' and (y - Sy)/Dr with y', giving |
| 67 | |
| 68 | [Dx^2 / Dr^2 + Dy^2 / Dr^2 - 1)] * t^2 |
| 69 | + 2 * [x' * Dx / Dr + y' * Dy / Dr - Sr / Dr] * t |
| 70 | + [x'^2 + y'^2 - Sr^2/Dr^2] = 0 |
| 71 | |
| 72 | (x' and y' are computed by appending the subtract and scale to the |
| 73 | fDstToIndex matrix in the constructor). |
| 74 | |
| 75 | Since the 'A' component of the quadratic is independent of x' and y', it |
| 76 | is precomputed in the constructor. Since the 'B' component is linear in |
| 77 | x' and y', if x and y are linear in the span, 'B' can be computed |
| 78 | incrementally with a simple delta (db below). If it is not (e.g., |
| 79 | a perspective projection), it must be computed in the loop. |
| 80 | |
| 81 | */ |
| 82 | |
| 83 | namespace { |
| 84 | |
| 85 | inline SkFixed two_point_radial(SkScalar b, SkScalar fx, SkScalar fy, |
| 86 | SkScalar sr2d2, SkScalar foura, |
| 87 | SkScalar oneOverTwoA, bool posRoot) { |
| 88 | SkScalar c = SkScalarSquare(fx) + SkScalarSquare(fy) - sr2d2; |
| 89 | if (0 == foura) { |
| 90 | return SkScalarToFixed(SkScalarDiv(-c, b)); |
| 91 | } |
| 92 | |
| 93 | SkScalar discrim = SkScalarSquare(b) - SkScalarMul(foura, c); |
| 94 | if (discrim < 0) { |
| 95 | discrim = -discrim; |
| 96 | } |
| 97 | SkScalar rootDiscrim = SkScalarSqrt(discrim); |
| 98 | SkScalar result; |
| 99 | if (posRoot) { |
| 100 | result = SkScalarMul(-b + rootDiscrim, oneOverTwoA); |
| 101 | } else { |
| 102 | result = SkScalarMul(-b - rootDiscrim, oneOverTwoA); |
| 103 | } |
| 104 | return SkScalarToFixed(result); |
| 105 | } |
| 106 | |
| 107 | typedef void (* TwoPointRadialShadeProc)(SkScalar fx, SkScalar dx, |
| 108 | SkScalar fy, SkScalar dy, |
| 109 | SkScalar b, SkScalar db, |
| 110 | SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot, |
| 111 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 112 | int count); |
| 113 | |
| 114 | void shadeSpan_twopoint_clamp(SkScalar fx, SkScalar dx, |
| 115 | SkScalar fy, SkScalar dy, |
| 116 | SkScalar b, SkScalar db, |
| 117 | SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot, |
| 118 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 119 | int count) { |
| 120 | for (; count > 0; --count) { |
| 121 | SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, |
| 122 | fOneOverTwoA, posRoot); |
| 123 | SkFixed index = SkClampMax(t, 0xFFFF); |
| 124 | SkASSERT(index <= 0xFFFF); |
| 125 | *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift]; |
| 126 | fx += dx; |
| 127 | fy += dy; |
| 128 | b += db; |
| 129 | } |
| 130 | } |
| 131 | void shadeSpan_twopoint_mirror(SkScalar fx, SkScalar dx, |
| 132 | SkScalar fy, SkScalar dy, |
| 133 | SkScalar b, SkScalar db, |
| 134 | SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot, |
| 135 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 136 | int count) { |
| 137 | for (; count > 0; --count) { |
| 138 | SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, |
| 139 | fOneOverTwoA, posRoot); |
| 140 | SkFixed index = mirror_tileproc(t); |
| 141 | SkASSERT(index <= 0xFFFF); |
| 142 | *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift]; |
| 143 | fx += dx; |
| 144 | fy += dy; |
| 145 | b += db; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void shadeSpan_twopoint_repeat(SkScalar fx, SkScalar dx, |
| 150 | SkScalar fy, SkScalar dy, |
| 151 | SkScalar b, SkScalar db, |
| 152 | SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot, |
| 153 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 154 | int count) { |
| 155 | for (; count > 0; --count) { |
| 156 | SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, |
| 157 | fOneOverTwoA, posRoot); |
| 158 | SkFixed index = repeat_tileproc(t); |
| 159 | SkASSERT(index <= 0xFFFF); |
| 160 | *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift]; |
| 161 | fx += dx; |
| 162 | fy += dy; |
| 163 | b += db; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 168 | ///////////////////////////////////////////////////////////////////// |
| 169 | |
reed | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 170 | SkTwoPointRadialGradient::SkTwoPointRadialGradient(const SkPoint& start, SkScalar startRadius, |
| 171 | const SkPoint& end, SkScalar endRadius, |
| 172 | const Descriptor& desc) |
| 173 | : SkGradientShaderBase(desc) |
| 174 | , fCenter1(start) |
| 175 | , fCenter2(end) |
| 176 | , fRadius1(startRadius) |
| 177 | , fRadius2(endRadius) |
| 178 | { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 179 | init(); |
| 180 | } |
| 181 | |
| 182 | SkShader::BitmapType SkTwoPointRadialGradient::asABitmap( |
| 183 | SkBitmap* bitmap, |
| 184 | SkMatrix* matrix, |
| 185 | SkShader::TileMode* xy) const { |
| 186 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 187 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 188 | } |
| 189 | SkScalar diffL = 0; // just to avoid gcc warning |
| 190 | if (matrix) { |
| 191 | diffL = SkScalarSqrt(SkScalarSquare(fDiff.fX) + |
| 192 | SkScalarSquare(fDiff.fY)); |
| 193 | } |
| 194 | if (matrix) { |
| 195 | if (diffL) { |
| 196 | SkScalar invDiffL = SkScalarInvert(diffL); |
| 197 | matrix->setSinCos(-SkScalarMul(invDiffL, fDiff.fY), |
| 198 | SkScalarMul(invDiffL, fDiff.fX)); |
| 199 | } else { |
| 200 | matrix->reset(); |
| 201 | } |
| 202 | matrix->preConcat(fPtsToUnit); |
| 203 | } |
| 204 | if (xy) { |
| 205 | xy[0] = fTileMode; |
| 206 | xy[1] = kClamp_TileMode; |
| 207 | } |
| 208 | return kTwoPointRadial_BitmapType; |
| 209 | } |
| 210 | |
| 211 | SkShader::GradientType SkTwoPointRadialGradient::asAGradient( |
| 212 | SkShader::GradientInfo* info) const { |
| 213 | if (info) { |
| 214 | commonAsAGradient(info); |
| 215 | info->fPoint[0] = fCenter1; |
| 216 | info->fPoint[1] = fCenter2; |
| 217 | info->fRadius[0] = fRadius1; |
| 218 | info->fRadius[1] = fRadius2; |
| 219 | } |
| 220 | return kRadial2_GradientType; |
| 221 | } |
| 222 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 223 | size_t SkTwoPointRadialGradient::contextSize() const { |
| 224 | return sizeof(TwoPointRadialGradientContext); |
| 225 | } |
| 226 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 227 | SkShader::Context* SkTwoPointRadialGradient::onCreateContext(const ContextRec& rec, |
| 228 | void* storage) const { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 229 | // For now, we might have divided by zero, so detect that. |
| 230 | if (0 == fDiffRadius) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 231 | return NULL; |
| 232 | } |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 233 | return SkNEW_PLACEMENT_ARGS(storage, TwoPointRadialGradientContext, (*this, rec)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | SkTwoPointRadialGradient::TwoPointRadialGradientContext::TwoPointRadialGradientContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 237 | const SkTwoPointRadialGradient& shader, const ContextRec& rec) |
| 238 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 239 | { |
| 240 | // we don't have a span16 proc |
| 241 | fFlags &= ~kHasSpan16_Flag; |
| 242 | } |
| 243 | |
| 244 | void SkTwoPointRadialGradient::TwoPointRadialGradientContext::shadeSpan( |
| 245 | int x, int y, SkPMColor* dstCParam, int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 246 | SkASSERT(count > 0); |
| 247 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 248 | const SkTwoPointRadialGradient& twoPointRadialGradient = |
| 249 | static_cast<const SkTwoPointRadialGradient&>(fShader); |
| 250 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 251 | SkPMColor* SK_RESTRICT dstC = dstCParam; |
| 252 | |
| 253 | // Zero difference between radii: fill with transparent black. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 254 | if (twoPointRadialGradient.fDiffRadius == 0) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 255 | sk_bzero(dstC, count * sizeof(*dstC)); |
| 256 | return; |
| 257 | } |
| 258 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 259 | TileProc proc = twoPointRadialGradient.fTileProc; |
| 260 | const SkPMColor* SK_RESTRICT cache = fCache->getCache32(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 261 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 262 | SkScalar foura = twoPointRadialGradient.fA * 4; |
| 263 | bool posRoot = twoPointRadialGradient.fDiffRadius < 0; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 264 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 265 | SkPoint srcPt; |
| 266 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 267 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 268 | SkScalar dx, fx = srcPt.fX; |
| 269 | SkScalar dy, fy = srcPt.fY; |
| 270 | |
| 271 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 272 | SkFixed fixedX, fixedY; |
| 273 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); |
| 274 | dx = SkFixedToScalar(fixedX); |
| 275 | dy = SkFixedToScalar(fixedY); |
| 276 | } else { |
| 277 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 278 | dx = fDstToIndex.getScaleX(); |
| 279 | dy = fDstToIndex.getSkewY(); |
| 280 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 281 | SkScalar b = (SkScalarMul(twoPointRadialGradient.fDiff.fX, fx) + |
| 282 | SkScalarMul(twoPointRadialGradient.fDiff.fY, fy) - |
| 283 | twoPointRadialGradient.fStartRadius) * 2; |
| 284 | SkScalar db = (SkScalarMul(twoPointRadialGradient.fDiff.fX, dx) + |
| 285 | SkScalarMul(twoPointRadialGradient.fDiff.fY, dy)) * 2; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 286 | |
| 287 | TwoPointRadialShadeProc shadeProc = shadeSpan_twopoint_repeat; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 288 | if (SkShader::kClamp_TileMode == twoPointRadialGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 289 | shadeProc = shadeSpan_twopoint_clamp; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 290 | } else if (SkShader::kMirror_TileMode == twoPointRadialGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 291 | shadeProc = shadeSpan_twopoint_mirror; |
| 292 | } else { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 293 | SkASSERT(SkShader::kRepeat_TileMode == twoPointRadialGradient.fTileMode); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 294 | } |
| 295 | (*shadeProc)(fx, dx, fy, dy, b, db, |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 296 | twoPointRadialGradient.fSr2D2, foura, |
| 297 | twoPointRadialGradient.fOneOverTwoA, posRoot, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 298 | dstC, cache, count); |
| 299 | } else { // perspective case |
| 300 | SkScalar dstX = SkIntToScalar(x); |
| 301 | SkScalar dstY = SkIntToScalar(y); |
| 302 | for (; count > 0; --count) { |
| 303 | SkPoint srcPt; |
| 304 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 305 | SkScalar fx = srcPt.fX; |
| 306 | SkScalar fy = srcPt.fY; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 307 | SkScalar b = (SkScalarMul(twoPointRadialGradient.fDiff.fX, fx) + |
| 308 | SkScalarMul(twoPointRadialGradient.fDiff.fY, fy) - |
| 309 | twoPointRadialGradient.fStartRadius) * 2; |
| 310 | SkFixed t = two_point_radial(b, fx, fy, twoPointRadialGradient.fSr2D2, foura, |
| 311 | twoPointRadialGradient.fOneOverTwoA, posRoot); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 312 | SkFixed index = proc(t); |
| 313 | SkASSERT(index <= 0xFFFF); |
| 314 | *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift]; |
| 315 | dstX += SK_Scalar1; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 320 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 321 | void SkTwoPointRadialGradient::toString(SkString* str) const { |
| 322 | str->append("SkTwoPointRadialGradient: ("); |
| 323 | |
| 324 | str->append("center1: ("); |
| 325 | str->appendScalar(fCenter1.fX); |
| 326 | str->append(", "); |
| 327 | str->appendScalar(fCenter1.fY); |
| 328 | str->append(") radius1: "); |
| 329 | str->appendScalar(fRadius1); |
| 330 | str->append(" "); |
| 331 | |
| 332 | str->append("center2: ("); |
| 333 | str->appendScalar(fCenter2.fX); |
| 334 | str->append(", "); |
| 335 | str->appendScalar(fCenter2.fY); |
| 336 | str->append(") radius2: "); |
| 337 | str->appendScalar(fRadius2); |
| 338 | str->append(" "); |
| 339 | |
| 340 | this->INHERITED::toString(str); |
| 341 | |
| 342 | str->append(")"); |
| 343 | } |
| 344 | #endif |
| 345 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 346 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
| 347 | SkTwoPointRadialGradient::SkTwoPointRadialGradient(SkReadBuffer& buffer) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 348 | : INHERITED(buffer), |
| 349 | fCenter1(buffer.readPoint()), |
| 350 | fCenter2(buffer.readPoint()), |
| 351 | fRadius1(buffer.readScalar()), |
| 352 | fRadius2(buffer.readScalar()) { |
| 353 | init(); |
| 354 | }; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 355 | #endif |
| 356 | |
| 357 | SkFlattenable* SkTwoPointRadialGradient::CreateProc(SkReadBuffer& buffer) { |
| 358 | DescriptorScope desc; |
| 359 | if (!desc.unflatten(buffer)) { |
| 360 | return NULL; |
| 361 | } |
| 362 | const SkPoint c1 = buffer.readPoint(); |
| 363 | const SkPoint c2 = buffer.readPoint(); |
| 364 | const SkScalar r1 = buffer.readScalar(); |
| 365 | const SkScalar r2 = buffer.readScalar(); |
| 366 | return SkGradientShader::CreateTwoPointRadial(c1, r1, c2, r2, desc.fColors, desc.fPos, |
| 367 | desc.fCount, desc.fTileMode, desc.fGradFlags, |
| 368 | desc.fLocalMatrix); |
| 369 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 370 | |
| 371 | void SkTwoPointRadialGradient::flatten( |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 372 | SkWriteBuffer& buffer) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 373 | this->INHERITED::flatten(buffer); |
| 374 | buffer.writePoint(fCenter1); |
| 375 | buffer.writePoint(fCenter2); |
| 376 | buffer.writeScalar(fRadius1); |
| 377 | buffer.writeScalar(fRadius2); |
| 378 | } |
| 379 | |
| 380 | void SkTwoPointRadialGradient::init() { |
| 381 | fDiff = fCenter1 - fCenter2; |
| 382 | fDiffRadius = fRadius2 - fRadius1; |
| 383 | // hack to avoid zero-divide for now |
| 384 | SkScalar inv = fDiffRadius ? SkScalarInvert(fDiffRadius) : 0; |
| 385 | fDiff.fX = SkScalarMul(fDiff.fX, inv); |
| 386 | fDiff.fY = SkScalarMul(fDiff.fY, inv); |
| 387 | fStartRadius = SkScalarMul(fRadius1, inv); |
| 388 | fSr2D2 = SkScalarSquare(fStartRadius); |
| 389 | fA = SkScalarSquare(fDiff.fX) + SkScalarSquare(fDiff.fY) - SK_Scalar1; |
| 390 | fOneOverTwoA = fA ? SkScalarInvert(fA * 2) : 0; |
| 391 | |
| 392 | fPtsToUnit.setTranslate(-fCenter1.fX, -fCenter1.fY); |
| 393 | fPtsToUnit.postScale(inv, inv); |
| 394 | } |
| 395 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 396 | ///////////////////////////////////////////////////////////////////// |
| 397 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 398 | #if SK_SUPPORT_GPU |
| 399 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 400 | #include "GrTBackendEffectFactory.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 401 | #include "gl/builders/GrGLProgramBuilder.h" |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 402 | #include "SkGr.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 403 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 404 | // For brevity |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 405 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 406 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 407 | class GrGLRadial2Gradient : public GrGLGradientEffect { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 408 | |
| 409 | public: |
| 410 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 411 | GrGLRadial2Gradient(const GrBackendEffectFactory& factory, const GrEffect&); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 412 | virtual ~GrGLRadial2Gradient() { } |
| 413 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 414 | virtual void emitCode(GrGLProgramBuilder*, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 415 | const GrEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 416 | const GrEffectKey&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 417 | const char* outputColor, |
| 418 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 419 | const TransformedCoordsArray&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 420 | const TextureSamplerArray&) SK_OVERRIDE; |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 421 | virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 422 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 423 | static void GenKey(const GrEffect&, const GrGLCaps& caps, GrEffectKeyBuilder* b); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 424 | |
| 425 | protected: |
| 426 | |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 427 | UniformHandle fParamUni; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 428 | |
| 429 | const char* fVSVaryingName; |
| 430 | const char* fFSVaryingName; |
| 431 | |
| 432 | bool fIsDegenerate; |
| 433 | |
| 434 | // @{ |
| 435 | /// Values last uploaded as uniforms |
| 436 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 437 | SkScalar fCachedCenter; |
| 438 | SkScalar fCachedRadius; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 439 | bool fCachedPosRoot; |
| 440 | |
| 441 | // @} |
| 442 | |
| 443 | private: |
| 444 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 445 | typedef GrGLGradientEffect INHERITED; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 446 | |
| 447 | }; |
| 448 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 449 | ///////////////////////////////////////////////////////////////////// |
| 450 | |
| 451 | class GrRadial2Gradient : public GrGradientEffect { |
| 452 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 453 | static GrEffect* Create(GrContext* ctx, |
| 454 | const SkTwoPointRadialGradient& shader, |
| 455 | const SkMatrix& matrix, |
| 456 | SkShader::TileMode tm) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 457 | return SkNEW_ARGS(GrRadial2Gradient, (ctx, shader, matrix, tm)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 458 | } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 459 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 460 | virtual ~GrRadial2Gradient() { } |
| 461 | |
| 462 | static const char* Name() { return "Two-Point Radial Gradient"; } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 463 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 464 | return GrTBackendEffectFactory<GrRadial2Gradient>::getInstance(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 465 | } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 466 | |
| 467 | // The radial gradient parameters can collapse to a linear (instead of quadratic) equation. |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 468 | bool isDegenerate() const { return SK_Scalar1 == fCenterX1; } |
| 469 | SkScalar center() const { return fCenterX1; } |
| 470 | SkScalar radius() const { return fRadius0; } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 471 | bool isPosRoot() const { return SkToBool(fPosRoot); } |
| 472 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 473 | typedef GrGLRadial2Gradient GLEffect; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 474 | |
| 475 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 476 | virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 477 | const GrRadial2Gradient& s = sBase.cast<GrRadial2Gradient>(); |
bsalomon@google.com | 70db51f | 2013-01-17 18:39:59 +0000 | [diff] [blame] | 478 | return (INHERITED::onIsEqual(sBase) && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 479 | this->fCenterX1 == s.fCenterX1 && |
| 480 | this->fRadius0 == s.fRadius0 && |
| 481 | this->fPosRoot == s.fPosRoot); |
| 482 | } |
| 483 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 484 | GrRadial2Gradient(GrContext* ctx, |
| 485 | const SkTwoPointRadialGradient& shader, |
| 486 | const SkMatrix& matrix, |
| 487 | SkShader::TileMode tm) |
| 488 | : INHERITED(ctx, shader, matrix, tm) |
| 489 | , fCenterX1(shader.getCenterX1()) |
| 490 | , fRadius0(shader.getStartRadius()) |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 491 | , fPosRoot(shader.getDiffRadius() < 0) { |
| 492 | // We pass the linear part of the quadratic as a varying. |
| 493 | // float b = 2.0 * (fCenterX1 * x - fRadius0 * z) |
| 494 | fBTransform = this->getCoordTransform(); |
| 495 | SkMatrix& bMatrix = *fBTransform.accessMatrix(); |
| 496 | bMatrix[SkMatrix::kMScaleX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMScaleX]) - |
| 497 | SkScalarMul(fRadius0, bMatrix[SkMatrix::kMPersp0])); |
| 498 | bMatrix[SkMatrix::kMSkewX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMSkewX]) - |
| 499 | SkScalarMul(fRadius0, bMatrix[SkMatrix::kMPersp1])); |
| 500 | bMatrix[SkMatrix::kMTransX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMTransX]) - |
| 501 | SkScalarMul(fRadius0, bMatrix[SkMatrix::kMPersp2])); |
| 502 | this->addCoordTransform(&fBTransform); |
| 503 | } |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 504 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 505 | GR_DECLARE_EFFECT_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 506 | |
| 507 | // @{ |
| 508 | // Cache of values - these can change arbitrarily, EXCEPT |
| 509 | // we shouldn't change between degenerate and non-degenerate?! |
| 510 | |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 511 | GrCoordTransform fBTransform; |
| 512 | SkScalar fCenterX1; |
| 513 | SkScalar fRadius0; |
| 514 | SkBool8 fPosRoot; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 515 | |
| 516 | // @} |
| 517 | |
| 518 | typedef GrGradientEffect INHERITED; |
| 519 | }; |
| 520 | |
| 521 | ///////////////////////////////////////////////////////////////////// |
| 522 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 523 | GR_DEFINE_EFFECT_TEST(GrRadial2Gradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 524 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 525 | GrEffect* GrRadial2Gradient::TestCreate(SkRandom* random, |
| 526 | GrContext* context, |
| 527 | const GrDrawTargetCaps&, |
| 528 | GrTexture**) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 529 | SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()}; |
| 530 | SkScalar radius1 = random->nextUScalar1(); |
| 531 | SkPoint center2; |
| 532 | SkScalar radius2; |
| 533 | do { |
bsalomon@google.com | fb883bf | 2012-12-11 15:32:04 +0000 | [diff] [blame] | 534 | center2.set(random->nextUScalar1(), random->nextUScalar1()); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 535 | radius2 = random->nextUScalar1 (); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 536 | // There is a bug in two point radial gradients with identical radii |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 537 | } while (radius1 == radius2); |
| 538 | |
| 539 | SkColor colors[kMaxRandomGradientColors]; |
| 540 | SkScalar stopsArray[kMaxRandomGradientColors]; |
| 541 | SkScalar* stops = stopsArray; |
| 542 | SkShader::TileMode tm; |
| 543 | int colorCount = RandomGradientParams(random, colors, &stops, &tm); |
| 544 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointRadial(center1, radius1, |
| 545 | center2, radius2, |
| 546 | colors, stops, colorCount, |
| 547 | tm)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 548 | SkPaint paint; |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 549 | GrEffect* effect; |
| 550 | GrColor paintColor; |
| 551 | SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effect)); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 552 | return effect; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | ///////////////////////////////////////////////////////////////////// |
| 556 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 557 | GrGLRadial2Gradient::GrGLRadial2Gradient(const GrBackendEffectFactory& factory, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 558 | const GrEffect& effect) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 559 | : INHERITED(factory) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 560 | , fVSVaryingName(NULL) |
| 561 | , fFSVaryingName(NULL) |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 562 | , fCachedCenter(SK_ScalarMax) |
| 563 | , fCachedRadius(-SK_ScalarMax) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 564 | , fCachedPosRoot(0) { |
| 565 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 566 | const GrRadial2Gradient& data = effect.cast<GrRadial2Gradient>(); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 567 | fIsDegenerate = data.isDegenerate(); |
| 568 | } |
| 569 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 570 | void GrGLRadial2Gradient::emitCode(GrGLProgramBuilder* builder, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 571 | const GrEffect& effect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 572 | const GrEffectKey& key, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 573 | const char* outputColor, |
| 574 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 575 | const TransformedCoordsArray& coords, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 576 | const TextureSamplerArray& samplers) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 577 | uint32_t baseKey = key.get32(0); |
| 578 | this->emitUniforms(builder, baseKey); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 579 | fParamUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 580 | kFloat_GrSLType, "Radial2FSParams", 6); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 581 | |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 582 | SkString cName("c"); |
| 583 | SkString ac4Name("ac4"); |
| 584 | SkString rootName("root"); |
| 585 | SkString t; |
| 586 | SkString p0; |
| 587 | SkString p1; |
| 588 | SkString p2; |
| 589 | SkString p3; |
| 590 | SkString p4; |
| 591 | SkString p5; |
| 592 | builder->getUniformVariable(fParamUni).appendArrayAccess(0, &p0); |
| 593 | builder->getUniformVariable(fParamUni).appendArrayAccess(1, &p1); |
| 594 | builder->getUniformVariable(fParamUni).appendArrayAccess(2, &p2); |
| 595 | builder->getUniformVariable(fParamUni).appendArrayAccess(3, &p3); |
| 596 | builder->getUniformVariable(fParamUni).appendArrayAccess(4, &p4); |
| 597 | builder->getUniformVariable(fParamUni).appendArrayAccess(5, &p5); |
| 598 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 599 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 600 | // We interpolate the linear component in coords[1]. |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame^] | 601 | SkASSERT(coords[0].getType() == coords[1].getType()); |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 602 | const char* coords2D; |
| 603 | SkString bVar; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame^] | 604 | if (kVec3f_GrSLType == coords[0].getType()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 605 | fsBuilder->codeAppendf("\tvec3 interpolants = vec3(%s.xy, %s.x) / %s.z;\n", |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 606 | coords[0].c_str(), coords[1].c_str(), coords[0].c_str()); |
| 607 | coords2D = "interpolants.xy"; |
| 608 | bVar = "interpolants.z"; |
| 609 | } else { |
| 610 | coords2D = coords[0].c_str(); |
| 611 | bVar.printf("%s.x", coords[1].c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 612 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 613 | |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 614 | // c = (x^2)+(y^2) - params[4] |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 615 | fsBuilder->codeAppendf("\tfloat %s = dot(%s, %s) - %s;\n", |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 616 | cName.c_str(), coords2D, coords2D, p4.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 617 | |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 618 | // If we aren't degenerate, emit some extra code, and accept a slightly |
| 619 | // more complex coord. |
| 620 | if (!fIsDegenerate) { |
| 621 | |
| 622 | // ac4 = 4.0 * params[0] * c |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 623 | fsBuilder->codeAppendf("\tfloat %s = %s * 4.0 * %s;\n", |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 624 | ac4Name.c_str(), p0.c_str(), |
| 625 | cName.c_str()); |
| 626 | |
| 627 | // root = sqrt(b^2-4ac) |
| 628 | // (abs to avoid exception due to fp precision) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 629 | fsBuilder->codeAppendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n", |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 630 | rootName.c_str(), bVar.c_str(), bVar.c_str(), |
| 631 | ac4Name.c_str()); |
| 632 | |
| 633 | // t is: (-b + params[5] * sqrt(b^2-4ac)) * params[1] |
| 634 | t.printf("(-%s + %s * %s) * %s", bVar.c_str(), p5.c_str(), |
| 635 | rootName.c_str(), p1.c_str()); |
| 636 | } else { |
| 637 | // t is: -c/b |
| 638 | t.printf("-%s / %s", cName.c_str(), bVar.c_str()); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 639 | } |
| 640 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 641 | this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, samplers); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 642 | } |
| 643 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 644 | void GrGLRadial2Gradient::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 645 | const GrEffect& effect) { |
| 646 | INHERITED::setData(pdman, effect); |
| 647 | const GrRadial2Gradient& data = effect.cast<GrRadial2Gradient>(); |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 648 | SkASSERT(data.isDegenerate() == fIsDegenerate); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 649 | SkScalar centerX1 = data.center(); |
| 650 | SkScalar radius0 = data.radius(); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 651 | if (fCachedCenter != centerX1 || |
| 652 | fCachedRadius != radius0 || |
| 653 | fCachedPosRoot != data.isPosRoot()) { |
| 654 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 655 | SkScalar a = SkScalarMul(centerX1, centerX1) - SK_Scalar1; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 656 | |
| 657 | // When we're in the degenerate (linear) case, the second |
| 658 | // value will be INF but the program doesn't read it. (We |
| 659 | // use the same 6 uniforms even though we don't need them |
| 660 | // all in the linear case just to keep the code complexity |
| 661 | // down). |
| 662 | float values[6] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 663 | SkScalarToFloat(a), |
| 664 | 1 / (2.f * SkScalarToFloat(a)), |
| 665 | SkScalarToFloat(centerX1), |
| 666 | SkScalarToFloat(radius0), |
| 667 | SkScalarToFloat(SkScalarMul(radius0, radius0)), |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 668 | data.isPosRoot() ? 1.f : -1.f |
| 669 | }; |
| 670 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 671 | pdman.set1fv(fParamUni, 6, values); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 672 | fCachedCenter = centerX1; |
| 673 | fCachedRadius = radius0; |
| 674 | fCachedPosRoot = data.isPosRoot(); |
| 675 | } |
| 676 | } |
| 677 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 678 | void GrGLRadial2Gradient::GenKey(const GrEffect& effect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 679 | const GrGLCaps&, GrEffectKeyBuilder* b) { |
| 680 | uint32_t* key = b->add32n(2); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 681 | key[0] = GenBaseGradientKey(effect); |
| 682 | key[1] = effect.cast<GrRadial2Gradient>().isDegenerate(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 683 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 684 | |
| 685 | ///////////////////////////////////////////////////////////////////// |
| 686 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 687 | bool SkTwoPointRadialGradient::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 688 | const SkMatrix* localMatrix, GrColor* paintColor, |
| 689 | GrEffect** effect) const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 690 | SkASSERT(context); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 691 | |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 692 | // invert the localM, translate to center1 (fPtsToUni), rotate so center2 is on x axis. |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 693 | SkMatrix matrix; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 694 | if (!this->getLocalMatrix().invert(&matrix)) { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 695 | return false; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 696 | } |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 697 | if (localMatrix) { |
| 698 | SkMatrix inv; |
| 699 | if (!localMatrix->invert(&inv)) { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 700 | return false; |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 701 | } |
| 702 | matrix.postConcat(inv); |
| 703 | } |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 704 | matrix.postConcat(fPtsToUnit); |
| 705 | |
| 706 | SkScalar diffLen = fDiff.length(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 707 | if (0 != diffLen) { |
| 708 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 709 | SkMatrix rot; |
| 710 | rot.setSinCos(-SkScalarMul(invDiffLen, fDiff.fY), |
| 711 | SkScalarMul(invDiffLen, fDiff.fX)); |
| 712 | matrix.postConcat(rot); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 713 | } |
| 714 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 715 | *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
| 716 | *effect = GrRadial2Gradient::Create(context, *this, matrix, fTileMode); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 717 | |
| 718 | return true; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 719 | } |
| 720 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 721 | #else |
| 722 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 723 | bool SkTwoPointRadialGradient::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 724 | const SkMatrix* localMatrix, GrColor* paintColor, |
| 725 | GrEffect** effect) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 726 | SkDEBUGFAIL("Should not call in GPU-less build"); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 727 | return false; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | #endif |