rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkTwoPointConicalGradient.h" |
| 9 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 10 | #include "SkTwoPointConicalGradient_gpu.h" |
| 11 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 12 | struct TwoPtRadialContext { |
| 13 | const TwoPtRadial& fRec; |
| 14 | float fRelX, fRelY; |
| 15 | const float fIncX, fIncY; |
| 16 | float fB; |
| 17 | const float fDB; |
| 18 | |
| 19 | TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkScalar fy, |
| 20 | SkScalar dfx, SkScalar dfy); |
| 21 | SkFixed nextT(); |
| 22 | }; |
| 23 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 24 | static int valid_divide(float numer, float denom, float* ratio) { |
| 25 | SkASSERT(ratio); |
| 26 | if (0 == denom) { |
| 27 | return 0; |
| 28 | } |
| 29 | *ratio = numer / denom; |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | // Return the number of distinct real roots, and write them into roots[] in |
| 34 | // ascending order |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 35 | static int find_quad_roots(float A, float B, float C, float roots[2], bool descendingOrder = false) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 36 | SkASSERT(roots); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 37 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 38 | if (A == 0) { |
| 39 | return valid_divide(-C, B, roots); |
| 40 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 41 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 42 | float R = B*B - 4*A*C; |
| 43 | if (R < 0) { |
| 44 | return 0; |
| 45 | } |
| 46 | R = sk_float_sqrt(R); |
| 47 | |
| 48 | #if 1 |
| 49 | float Q = B; |
| 50 | if (Q < 0) { |
| 51 | Q -= R; |
| 52 | } else { |
| 53 | Q += R; |
| 54 | } |
| 55 | #else |
| 56 | // on 10.6 this was much slower than the above branch :( |
| 57 | float Q = B + copysignf(R, B); |
| 58 | #endif |
| 59 | Q *= -0.5f; |
| 60 | if (0 == Q) { |
| 61 | roots[0] = 0; |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | float r0 = Q / A; |
| 66 | float r1 = C / Q; |
| 67 | roots[0] = r0 < r1 ? r0 : r1; |
| 68 | roots[1] = r0 > r1 ? r0 : r1; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 69 | if (descendingOrder) { |
| 70 | SkTSwap(roots[0], roots[1]); |
| 71 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 72 | return 2; |
| 73 | } |
| 74 | |
| 75 | static float lerp(float x, float dx, float t) { |
| 76 | return x + t * dx; |
| 77 | } |
| 78 | |
| 79 | static float sqr(float x) { return x * x; } |
| 80 | |
| 81 | void TwoPtRadial::init(const SkPoint& center0, SkScalar rad0, |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 82 | const SkPoint& center1, SkScalar rad1, |
| 83 | bool flipped) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 84 | fCenterX = SkScalarToFloat(center0.fX); |
| 85 | fCenterY = SkScalarToFloat(center0.fY); |
| 86 | fDCenterX = SkScalarToFloat(center1.fX) - fCenterX; |
| 87 | fDCenterY = SkScalarToFloat(center1.fY) - fCenterY; |
| 88 | fRadius = SkScalarToFloat(rad0); |
| 89 | fDRadius = SkScalarToFloat(rad1) - fRadius; |
| 90 | |
| 91 | fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius); |
| 92 | fRadius2 = sqr(fRadius); |
| 93 | fRDR = fRadius * fDRadius; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 94 | |
| 95 | fFlipped = flipped; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 96 | } |
| 97 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 98 | TwoPtRadialContext::TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkScalar fy, |
| 99 | SkScalar dfx, SkScalar dfy) |
| 100 | : fRec(rec) |
| 101 | , fRelX(SkScalarToFloat(fx) - rec.fCenterX) |
| 102 | , fRelY(SkScalarToFloat(fy) - rec.fCenterY) |
| 103 | , fIncX(SkScalarToFloat(dfx)) |
| 104 | , fIncY(SkScalarToFloat(dfy)) |
| 105 | , fB(-2 * (rec.fDCenterX * fRelX + rec.fDCenterY * fRelY + rec.fRDR)) |
| 106 | , fDB(-2 * (rec.fDCenterX * fIncX + rec.fDCenterY * fIncY)) {} |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 108 | SkFixed TwoPtRadialContext::nextT() { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 109 | float roots[2]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 110 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 111 | float C = sqr(fRelX) + sqr(fRelY) - fRec.fRadius2; |
| 112 | int countRoots = find_quad_roots(fRec.fA, fB, C, roots, fRec.fFlipped); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 113 | |
| 114 | fRelX += fIncX; |
| 115 | fRelY += fIncY; |
| 116 | fB += fDB; |
| 117 | |
| 118 | if (0 == countRoots) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 119 | return TwoPtRadial::kDontDrawT; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Prefer the bigger t value if both give a radius(t) > 0 |
| 123 | // find_quad_roots returns the values sorted, so we start with the last |
| 124 | float t = roots[countRoots - 1]; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 125 | float r = lerp(fRec.fRadius, fRec.fDRadius, t); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 126 | if (r <= 0) { |
| 127 | t = roots[0]; // might be the same as roots[countRoots-1] |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 128 | r = lerp(fRec.fRadius, fRec.fDRadius, t); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 129 | if (r <= 0) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 130 | return TwoPtRadial::kDontDrawT; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return SkFloatToFixed(t); |
| 134 | } |
| 135 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 136 | typedef void (*TwoPointConicalProc)(TwoPtRadialContext* rec, SkPMColor* dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 137 | const SkPMColor* cache, int toggle, int count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 138 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 139 | static void twopoint_clamp(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 140 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 141 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 142 | for (; count > 0; --count) { |
| 143 | SkFixed t = rec->nextT(); |
| 144 | if (TwoPtRadial::DontDrawT(t)) { |
| 145 | *dstC++ = 0; |
| 146 | } else { |
| 147 | SkFixed index = SkClampMax(t, 0xFFFF); |
| 148 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 149 | *dstC++ = cache[toggle + |
| 150 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 151 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 152 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 156 | static void twopoint_repeat(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 157 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 158 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 159 | for (; count > 0; --count) { |
| 160 | SkFixed t = rec->nextT(); |
| 161 | if (TwoPtRadial::DontDrawT(t)) { |
| 162 | *dstC++ = 0; |
| 163 | } else { |
| 164 | SkFixed index = repeat_tileproc(t); |
| 165 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 166 | *dstC++ = cache[toggle + |
| 167 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 168 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 169 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 173 | static void twopoint_mirror(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 174 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 175 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 176 | for (; count > 0; --count) { |
| 177 | SkFixed t = rec->nextT(); |
| 178 | if (TwoPtRadial::DontDrawT(t)) { |
| 179 | *dstC++ = 0; |
| 180 | } else { |
| 181 | SkFixed index = mirror_tileproc(t); |
| 182 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 183 | *dstC++ = cache[toggle + |
| 184 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 185 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 186 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | void SkTwoPointConicalGradient::init() { |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 191 | fRec.init(fCenter1, fRadius1, fCenter2, fRadius2, fFlippedGrad); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 192 | fPtsToUnit.reset(); |
| 193 | } |
| 194 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 195 | ///////////////////////////////////////////////////////////////////// |
| 196 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 197 | SkTwoPointConicalGradient::SkTwoPointConicalGradient( |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 198 | const SkPoint& start, SkScalar startRadius, |
| 199 | const SkPoint& end, SkScalar endRadius, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 200 | bool flippedGrad, const Descriptor& desc, |
| 201 | const SkMatrix* localMatrix) |
| 202 | : SkGradientShaderBase(desc, localMatrix), |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 203 | fCenter1(start), |
| 204 | fCenter2(end), |
| 205 | fRadius1(startRadius), |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 206 | fRadius2(endRadius), |
| 207 | fFlippedGrad(flippedGrad) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 208 | // this is degenerate, and should be caught by our caller |
| 209 | SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2); |
| 210 | this->init(); |
| 211 | } |
| 212 | |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 213 | bool SkTwoPointConicalGradient::isOpaque() const { |
robertphillips@google.com | cb6d97c | 2013-07-09 13:50:09 +0000 | [diff] [blame] | 214 | // Because areas outside the cone are left untouched, we cannot treat the |
| 215 | // shader as opaque even if the gradient itself is opaque. |
| 216 | // TODO(junov): Compute whether the cone fills the plane crbug.com/222380 |
| 217 | return false; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 218 | } |
| 219 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 220 | size_t SkTwoPointConicalGradient::contextSize() const { |
| 221 | return sizeof(TwoPointConicalGradientContext); |
| 222 | } |
| 223 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 224 | SkShader::Context* SkTwoPointConicalGradient::onCreateContext(const ContextRec& rec, |
| 225 | void* storage) const { |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 226 | return SkNEW_PLACEMENT_ARGS(storage, TwoPointConicalGradientContext, (*this, rec)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | SkTwoPointConicalGradient::TwoPointConicalGradientContext::TwoPointConicalGradientContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 230 | const SkTwoPointConicalGradient& shader, const ContextRec& rec) |
| 231 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 232 | { |
| 233 | // we don't have a span16 proc |
| 234 | fFlags &= ~kHasSpan16_Flag; |
| 235 | |
| 236 | // in general, we might discard based on computed-radius, so clear |
| 237 | // this flag (todo: sometimes we can detect that we never discard...) |
| 238 | fFlags &= ~kOpaqueAlpha_Flag; |
| 239 | } |
| 240 | |
| 241 | void SkTwoPointConicalGradient::TwoPointConicalGradientContext::shadeSpan( |
| 242 | int x, int y, SkPMColor* dstCParam, int count) { |
| 243 | const SkTwoPointConicalGradient& twoPointConicalGradient = |
| 244 | static_cast<const SkTwoPointConicalGradient&>(fShader); |
| 245 | |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 246 | int toggle = init_dither_toggle(x, y); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 247 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 248 | SkASSERT(count > 0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 249 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 250 | SkPMColor* SK_RESTRICT dstC = dstCParam; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 251 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 252 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 253 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 254 | const SkPMColor* SK_RESTRICT cache = fCache->getCache32(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 255 | |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 256 | TwoPointConicalProc shadeProc = twopoint_repeat; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 257 | if (SkShader::kClamp_TileMode == twoPointConicalGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 258 | shadeProc = twopoint_clamp; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 259 | } else if (SkShader::kMirror_TileMode == twoPointConicalGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 260 | shadeProc = twopoint_mirror; |
| 261 | } else { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 262 | SkASSERT(SkShader::kRepeat_TileMode == twoPointConicalGradient.fTileMode); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 263 | } |
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 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 266 | SkPoint srcPt; |
| 267 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 268 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 269 | SkScalar dx, fx = srcPt.fX; |
| 270 | SkScalar dy, fy = srcPt.fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 271 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 272 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 273 | SkFixed fixedX, fixedY; |
| 274 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); |
| 275 | dx = SkFixedToScalar(fixedX); |
| 276 | dy = SkFixedToScalar(fixedY); |
| 277 | } else { |
| 278 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 279 | dx = fDstToIndex.getScaleX(); |
| 280 | dy = fDstToIndex.getSkewY(); |
| 281 | } |
| 282 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 283 | TwoPtRadialContext rec(twoPointConicalGradient.fRec, fx, fy, dx, dy); |
| 284 | (*shadeProc)(&rec, dstC, cache, toggle, count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 285 | } else { // perspective case |
mike@reedtribe.org | 139a235 | 2013-11-14 20:15:51 +0000 | [diff] [blame] | 286 | SkScalar dstX = SkIntToScalar(x) + SK_ScalarHalf; |
| 287 | SkScalar dstY = SkIntToScalar(y) + SK_ScalarHalf; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 288 | for (; count > 0; --count) { |
| 289 | SkPoint srcPt; |
| 290 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 291 | TwoPtRadialContext rec(twoPointConicalGradient.fRec, srcPt.fX, srcPt.fY, 0, 0); |
| 292 | (*shadeProc)(&rec, dstC, cache, toggle, 1); |
mike@reedtribe.org | 139a235 | 2013-11-14 20:15:51 +0000 | [diff] [blame] | 293 | |
| 294 | dstX += SK_Scalar1; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 295 | toggle = next_dither_toggle(toggle); |
mike@reedtribe.org | 139a235 | 2013-11-14 20:15:51 +0000 | [diff] [blame] | 296 | dstC += 1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 301 | SkShader::BitmapType SkTwoPointConicalGradient::asABitmap( |
| 302 | SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const { |
| 303 | SkPoint diff = fCenter2 - fCenter1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 304 | SkScalar diffLen = 0; |
| 305 | |
| 306 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 307 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 308 | } |
| 309 | if (matrix) { |
| 310 | diffLen = diff.length(); |
| 311 | } |
| 312 | if (matrix) { |
| 313 | if (diffLen) { |
| 314 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
| 315 | // rotate to align circle centers with the x-axis |
| 316 | matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY), |
| 317 | SkScalarMul(invDiffLen, diff.fX)); |
| 318 | } else { |
| 319 | matrix->reset(); |
| 320 | } |
| 321 | matrix->preTranslate(-fCenter1.fX, -fCenter1.fY); |
| 322 | } |
| 323 | if (xy) { |
| 324 | xy[0] = fTileMode; |
| 325 | xy[1] = kClamp_TileMode; |
| 326 | } |
| 327 | return kTwoPointConical_BitmapType; |
| 328 | } |
| 329 | |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 330 | // Returns the original non-sorted version of the gradient |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 331 | SkShader::GradientType SkTwoPointConicalGradient::asAGradient( |
| 332 | GradientInfo* info) const { |
| 333 | if (info) { |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 334 | commonAsAGradient(info, fFlippedGrad); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 335 | info->fPoint[0] = fCenter1; |
| 336 | info->fPoint[1] = fCenter2; |
| 337 | info->fRadius[0] = fRadius1; |
| 338 | info->fRadius[1] = fRadius2; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 339 | if (fFlippedGrad) { |
| 340 | SkTSwap(info->fPoint[0], info->fPoint[1]); |
| 341 | SkTSwap(info->fRadius[0], info->fRadius[1]); |
| 342 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 343 | } |
| 344 | return kConical_GradientType; |
| 345 | } |
| 346 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 347 | SkTwoPointConicalGradient::SkTwoPointConicalGradient( |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 348 | SkReadBuffer& buffer) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 349 | : INHERITED(buffer), |
| 350 | fCenter1(buffer.readPoint()), |
| 351 | fCenter2(buffer.readPoint()), |
| 352 | fRadius1(buffer.readScalar()), |
| 353 | fRadius2(buffer.readScalar()) { |
commit-bot@chromium.org | 7ed173b | 2014-05-20 17:31:08 +0000 | [diff] [blame^] | 354 | if (buffer.isVersionLT(SkReadBuffer::kGradientFlippedFlag_Version)) { |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 355 | // V23_COMPATIBILITY_CODE |
| 356 | // Sort gradient by radius size for old pictures |
| 357 | if (fRadius2 < fRadius1) { |
| 358 | SkTSwap(fCenter1, fCenter2); |
| 359 | SkTSwap(fRadius1, fRadius2); |
| 360 | this->flipGradientColors(); |
| 361 | fFlippedGrad = true; |
| 362 | } else { |
| 363 | fFlippedGrad = false; |
| 364 | } |
commit-bot@chromium.org | 7ed173b | 2014-05-20 17:31:08 +0000 | [diff] [blame^] | 365 | } else { |
| 366 | fFlippedGrad = buffer.readBool(); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 367 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 368 | this->init(); |
| 369 | }; |
| 370 | |
| 371 | void SkTwoPointConicalGradient::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); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 378 | buffer.writeBool(fFlippedGrad); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 379 | } |
| 380 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 381 | #if SK_SUPPORT_GPU |
| 382 | |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 383 | GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext* context, const SkPaint&, |
| 384 | const SkMatrix* localMatrix) const { |
bsalomon@google.com | 00835cc | 2013-01-14 17:07:22 +0000 | [diff] [blame] | 385 | SkASSERT(NULL != context); |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 386 | SkASSERT(fPtsToUnit.isIdentity()); |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 387 | |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 388 | return Gr2PtConicalGradientEffect::Create(context, *this, fTileMode, localMatrix); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 389 | } |
| 390 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 391 | #else |
| 392 | |
reed@google.com | eaa6749 | 2014-05-09 20:52:23 +0000 | [diff] [blame] | 393 | GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext*, const SkPaint&, |
| 394 | const SkMatrix*) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 395 | SkDEBUGFAIL("Should not call in GPU-less build"); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 396 | return NULL; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 397 | } |
| 398 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 399 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 400 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 401 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 402 | void SkTwoPointConicalGradient::toString(SkString* str) const { |
| 403 | str->append("SkTwoPointConicalGradient: ("); |
| 404 | |
| 405 | str->append("center1: ("); |
| 406 | str->appendScalar(fCenter1.fX); |
| 407 | str->append(", "); |
| 408 | str->appendScalar(fCenter1.fY); |
| 409 | str->append(") radius1: "); |
| 410 | str->appendScalar(fRadius1); |
| 411 | str->append(" "); |
| 412 | |
| 413 | str->append("center2: ("); |
| 414 | str->appendScalar(fCenter2.fX); |
| 415 | str->append(", "); |
| 416 | str->appendScalar(fCenter2.fY); |
| 417 | str->append(") radius2: "); |
| 418 | str->appendScalar(fRadius2); |
| 419 | str->append(" "); |
| 420 | |
| 421 | this->INHERITED::toString(str); |
| 422 | |
| 423 | str->append(")"); |
| 424 | } |
| 425 | #endif |