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" |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 9 | |
Florin Malita | a66ef2d | 2017-06-28 10:02:40 -0400 | [diff] [blame^] | 10 | #include "SkRasterPipeline.h" |
| 11 | #include "../../jumper/SkJumper.h" |
| 12 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 13 | struct TwoPtRadialContext { |
| 14 | const TwoPtRadial& fRec; |
| 15 | float fRelX, fRelY; |
| 16 | const float fIncX, fIncY; |
| 17 | float fB; |
| 18 | const float fDB; |
| 19 | |
| 20 | TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkScalar fy, |
| 21 | SkScalar dfx, SkScalar dfy); |
| 22 | SkFixed nextT(); |
| 23 | }; |
| 24 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 25 | static int valid_divide(float numer, float denom, float* ratio) { |
| 26 | SkASSERT(ratio); |
| 27 | if (0 == denom) { |
| 28 | return 0; |
| 29 | } |
| 30 | *ratio = numer / denom; |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | // Return the number of distinct real roots, and write them into roots[] in |
| 35 | // ascending order |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 36 | 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] | 37 | SkASSERT(roots); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 38 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 39 | if (A == 0) { |
| 40 | return valid_divide(-C, B, roots); |
| 41 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 42 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 43 | float R = B*B - 4*A*C; |
| 44 | if (R < 0) { |
| 45 | return 0; |
| 46 | } |
| 47 | R = sk_float_sqrt(R); |
| 48 | |
| 49 | #if 1 |
| 50 | float Q = B; |
| 51 | if (Q < 0) { |
| 52 | Q -= R; |
| 53 | } else { |
| 54 | Q += R; |
| 55 | } |
| 56 | #else |
| 57 | // on 10.6 this was much slower than the above branch :( |
| 58 | float Q = B + copysignf(R, B); |
| 59 | #endif |
| 60 | Q *= -0.5f; |
| 61 | if (0 == Q) { |
| 62 | roots[0] = 0; |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | float r0 = Q / A; |
| 67 | float r1 = C / Q; |
| 68 | roots[0] = r0 < r1 ? r0 : r1; |
| 69 | roots[1] = r0 > r1 ? r0 : r1; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 70 | if (descendingOrder) { |
| 71 | SkTSwap(roots[0], roots[1]); |
| 72 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 73 | return 2; |
| 74 | } |
| 75 | |
| 76 | static float lerp(float x, float dx, float t) { |
| 77 | return x + t * dx; |
| 78 | } |
| 79 | |
| 80 | static float sqr(float x) { return x * x; } |
| 81 | |
| 82 | void TwoPtRadial::init(const SkPoint& center0, SkScalar rad0, |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 83 | const SkPoint& center1, SkScalar rad1, |
| 84 | bool flipped) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 85 | fCenterX = SkScalarToFloat(center0.fX); |
| 86 | fCenterY = SkScalarToFloat(center0.fY); |
| 87 | fDCenterX = SkScalarToFloat(center1.fX) - fCenterX; |
| 88 | fDCenterY = SkScalarToFloat(center1.fY) - fCenterY; |
| 89 | fRadius = SkScalarToFloat(rad0); |
| 90 | fDRadius = SkScalarToFloat(rad1) - fRadius; |
| 91 | |
| 92 | fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius); |
| 93 | fRadius2 = sqr(fRadius); |
| 94 | fRDR = fRadius * fDRadius; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 95 | |
| 96 | fFlipped = flipped; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 97 | } |
| 98 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 99 | TwoPtRadialContext::TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkScalar fy, |
| 100 | SkScalar dfx, SkScalar dfy) |
| 101 | : fRec(rec) |
| 102 | , fRelX(SkScalarToFloat(fx) - rec.fCenterX) |
| 103 | , fRelY(SkScalarToFloat(fy) - rec.fCenterY) |
| 104 | , fIncX(SkScalarToFloat(dfx)) |
| 105 | , fIncY(SkScalarToFloat(dfy)) |
| 106 | , fB(-2 * (rec.fDCenterX * fRelX + rec.fDCenterY * fRelY + rec.fRDR)) |
| 107 | , fDB(-2 * (rec.fDCenterX * fIncX + rec.fDCenterY * fIncY)) {} |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 109 | SkFixed TwoPtRadialContext::nextT() { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 110 | float roots[2]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 112 | float C = sqr(fRelX) + sqr(fRelY) - fRec.fRadius2; |
| 113 | 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] | 114 | |
| 115 | fRelX += fIncX; |
| 116 | fRelY += fIncY; |
| 117 | fB += fDB; |
| 118 | |
| 119 | if (0 == countRoots) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 120 | return TwoPtRadial::kDontDrawT; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Prefer the bigger t value if both give a radius(t) > 0 |
| 124 | // find_quad_roots returns the values sorted, so we start with the last |
| 125 | float t = roots[countRoots - 1]; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 126 | float r = lerp(fRec.fRadius, fRec.fDRadius, t); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 127 | if (r < 0) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 128 | 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] | 129 | r = lerp(fRec.fRadius, fRec.fDRadius, t); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 130 | if (r < 0) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 131 | return TwoPtRadial::kDontDrawT; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
fmalita | e736506 | 2016-04-07 19:27:45 -0700 | [diff] [blame] | 134 | return SkFloatToFixed(t); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 135 | } |
| 136 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 137 | typedef void (*TwoPointConicalProc)(TwoPtRadialContext* rec, SkPMColor* dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 138 | const SkPMColor* cache, int toggle, int count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 139 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 140 | static void twopoint_clamp(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 141 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 142 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 143 | for (; count > 0; --count) { |
| 144 | SkFixed t = rec->nextT(); |
| 145 | if (TwoPtRadial::DontDrawT(t)) { |
| 146 | *dstC++ = 0; |
| 147 | } else { |
| 148 | SkFixed index = SkClampMax(t, 0xFFFF); |
| 149 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 150 | *dstC++ = cache[toggle + |
| 151 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 152 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 153 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 157 | static void twopoint_repeat(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 158 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 159 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 160 | for (; count > 0; --count) { |
| 161 | SkFixed t = rec->nextT(); |
| 162 | if (TwoPtRadial::DontDrawT(t)) { |
| 163 | *dstC++ = 0; |
| 164 | } else { |
| 165 | SkFixed index = repeat_tileproc(t); |
| 166 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 167 | *dstC++ = cache[toggle + |
| 168 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 169 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 170 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 174 | static void twopoint_mirror(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 175 | const SkPMColor* SK_RESTRICT cache, int toggle, |
| 176 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 177 | for (; count > 0; --count) { |
| 178 | SkFixed t = rec->nextT(); |
| 179 | if (TwoPtRadial::DontDrawT(t)) { |
| 180 | *dstC++ = 0; |
| 181 | } else { |
| 182 | SkFixed index = mirror_tileproc(t); |
| 183 | SkASSERT(index <= 0xFFFF); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 184 | *dstC++ = cache[toggle + |
| 185 | (index >> SkGradientShaderBase::kCache32Shift)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 186 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 187 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 191 | ///////////////////////////////////////////////////////////////////// |
| 192 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 193 | SkTwoPointConicalGradient::SkTwoPointConicalGradient( |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 194 | const SkPoint& start, SkScalar startRadius, |
| 195 | const SkPoint& end, SkScalar endRadius, |
reed | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 196 | bool flippedGrad, const Descriptor& desc) |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 197 | : SkGradientShaderBase(desc, SkMatrix::I()) |
reed | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 198 | , fCenter1(start) |
| 199 | , fCenter2(end) |
| 200 | , fRadius1(startRadius) |
| 201 | , fRadius2(endRadius) |
| 202 | , fFlippedGrad(flippedGrad) |
| 203 | { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 204 | // this is degenerate, and should be caught by our caller |
| 205 | SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2); |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 206 | fRec.init(fCenter1, fRadius1, fCenter2, fRadius2, fFlippedGrad); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 207 | } |
| 208 | |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 209 | bool SkTwoPointConicalGradient::isOpaque() const { |
robertphillips@google.com | cb6d97c | 2013-07-09 13:50:09 +0000 | [diff] [blame] | 210 | // Because areas outside the cone are left untouched, we cannot treat the |
| 211 | // shader as opaque even if the gradient itself is opaque. |
| 212 | // TODO(junov): Compute whether the cone fills the plane crbug.com/222380 |
| 213 | return false; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 216 | SkShaderBase::Context* SkTwoPointConicalGradient::onMakeContext( |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 217 | const ContextRec& rec, SkArenaAlloc* alloc) const { |
| 218 | return CheckedMakeContext<TwoPointConicalGradientContext>(alloc, *this, rec); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | SkTwoPointConicalGradient::TwoPointConicalGradientContext::TwoPointConicalGradientContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 222 | const SkTwoPointConicalGradient& shader, const ContextRec& rec) |
| 223 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 224 | { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 225 | // in general, we might discard based on computed-radius, so clear |
| 226 | // this flag (todo: sometimes we can detect that we never discard...) |
| 227 | fFlags &= ~kOpaqueAlpha_Flag; |
| 228 | } |
| 229 | |
| 230 | void SkTwoPointConicalGradient::TwoPointConicalGradientContext::shadeSpan( |
| 231 | int x, int y, SkPMColor* dstCParam, int count) { |
| 232 | const SkTwoPointConicalGradient& twoPointConicalGradient = |
| 233 | static_cast<const SkTwoPointConicalGradient&>(fShader); |
| 234 | |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 235 | int toggle = init_dither_toggle(x, y); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 236 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 237 | SkASSERT(count > 0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 238 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 239 | SkPMColor* SK_RESTRICT dstC = dstCParam; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 240 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 241 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 242 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 243 | const SkPMColor* SK_RESTRICT cache = fCache->getCache32(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 244 | |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 245 | TwoPointConicalProc shadeProc = twopoint_repeat; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 246 | if (SkShader::kClamp_TileMode == twoPointConicalGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 247 | shadeProc = twopoint_clamp; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 248 | } else if (SkShader::kMirror_TileMode == twoPointConicalGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 249 | shadeProc = twopoint_mirror; |
| 250 | } else { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 251 | SkASSERT(SkShader::kRepeat_TileMode == twoPointConicalGradient.fTileMode); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 252 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 253 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 254 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 255 | SkPoint srcPt; |
| 256 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 257 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 258 | SkScalar dx, fx = srcPt.fX; |
| 259 | SkScalar dy, fy = srcPt.fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 260 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 261 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
benjaminwagner | 8e17556 | 2016-02-16 10:09:40 -0800 | [diff] [blame] | 262 | const auto step = fDstToIndex.fixedStepInX(SkIntToScalar(y)); |
| 263 | dx = step.fX; |
| 264 | dy = step.fY; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 265 | } else { |
| 266 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 267 | dx = fDstToIndex.getScaleX(); |
| 268 | dy = fDstToIndex.getSkewY(); |
| 269 | } |
| 270 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 271 | TwoPtRadialContext rec(twoPointConicalGradient.fRec, fx, fy, dx, dy); |
| 272 | (*shadeProc)(&rec, dstC, cache, toggle, count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 273 | } else { // perspective case |
mike@reedtribe.org | 139a235 | 2013-11-14 20:15:51 +0000 | [diff] [blame] | 274 | SkScalar dstX = SkIntToScalar(x) + SK_ScalarHalf; |
| 275 | SkScalar dstY = SkIntToScalar(y) + SK_ScalarHalf; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 276 | for (; count > 0; --count) { |
| 277 | SkPoint srcPt; |
| 278 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 279 | TwoPtRadialContext rec(twoPointConicalGradient.fRec, srcPt.fX, srcPt.fY, 0, 0); |
| 280 | (*shadeProc)(&rec, dstC, cache, toggle, 1); |
mike@reedtribe.org | 139a235 | 2013-11-14 20:15:51 +0000 | [diff] [blame] | 281 | |
| 282 | dstX += SK_Scalar1; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 283 | toggle = next_dither_toggle(toggle); |
mike@reedtribe.org | 139a235 | 2013-11-14 20:15:51 +0000 | [diff] [blame] | 284 | dstC += 1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 289 | // Returns the original non-sorted version of the gradient |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 290 | SkShader::GradientType SkTwoPointConicalGradient::asAGradient( |
| 291 | GradientInfo* info) const { |
| 292 | if (info) { |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 293 | commonAsAGradient(info, fFlippedGrad); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 294 | info->fPoint[0] = fCenter1; |
| 295 | info->fPoint[1] = fCenter2; |
| 296 | info->fRadius[0] = fRadius1; |
| 297 | info->fRadius[1] = fRadius2; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 298 | if (fFlippedGrad) { |
| 299 | SkTSwap(info->fPoint[0], info->fPoint[1]); |
| 300 | SkTSwap(info->fRadius[0], info->fRadius[1]); |
| 301 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 302 | } |
| 303 | return kConical_GradientType; |
| 304 | } |
| 305 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 306 | sk_sp<SkFlattenable> SkTwoPointConicalGradient::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 307 | DescriptorScope desc; |
| 308 | if (!desc.unflatten(buffer)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 309 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 310 | } |
| 311 | SkPoint c1 = buffer.readPoint(); |
| 312 | SkPoint c2 = buffer.readPoint(); |
| 313 | SkScalar r1 = buffer.readScalar(); |
| 314 | SkScalar r2 = buffer.readScalar(); |
| 315 | |
| 316 | if (buffer.readBool()) { // flipped |
| 317 | SkTSwap(c1, c2); |
| 318 | SkTSwap(r1, r2); |
| 319 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 320 | SkColor4f* colors = desc.mutableColors(); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 321 | SkScalar* pos = desc.mutablePos(); |
| 322 | const int last = desc.fCount - 1; |
| 323 | const int half = desc.fCount >> 1; |
| 324 | for (int i = 0; i < half; ++i) { |
| 325 | SkTSwap(colors[i], colors[last - i]); |
| 326 | if (pos) { |
| 327 | SkScalar tmp = pos[i]; |
| 328 | pos[i] = SK_Scalar1 - pos[last - i]; |
| 329 | pos[last - i] = SK_Scalar1 - tmp; |
| 330 | } |
| 331 | } |
| 332 | if (pos) { |
| 333 | if (desc.fCount & 1) { |
| 334 | pos[half] = SK_Scalar1 - pos[half]; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 339 | return SkGradientShader::MakeTwoPointConical(c1, r1, c2, r2, desc.fColors, |
| 340 | std::move(desc.fColorSpace), desc.fPos, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 341 | desc.fCount, desc.fTileMode, desc.fGradFlags, |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 342 | desc.fLocalMatrix); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void SkTwoPointConicalGradient::flatten(SkWriteBuffer& buffer) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 346 | this->INHERITED::flatten(buffer); |
| 347 | buffer.writePoint(fCenter1); |
| 348 | buffer.writePoint(fCenter2); |
| 349 | buffer.writeScalar(fRadius1); |
| 350 | buffer.writeScalar(fRadius2); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 351 | buffer.writeBool(fFlippedGrad); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 352 | } |
| 353 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 354 | #if SK_SUPPORT_GPU |
| 355 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 356 | #include "SkGr.h" |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 357 | #include "SkTwoPointConicalGradient_gpu.h" |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 358 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 359 | sk_sp<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor( |
brianosman | 1638c0d | 2016-07-25 05:12:53 -0700 | [diff] [blame] | 360 | const AsFPArgs& args) const { |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 361 | SkASSERT(args.fContext); |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 362 | SkASSERT(fPtsToUnit.isIdentity()); |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 363 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(), |
| 364 | args.fDstColorSpace); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 365 | sk_sp<GrFragmentProcessor> inner(Gr2PtConicalGradientEffect::Make( |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 366 | GrGradientEffect::CreateArgs(args.fContext, this, args.fLocalMatrix, fTileMode, |
| 367 | std::move(colorSpaceXform), SkToBool(args.fDstColorSpace)))); |
Brian Salomon | 6af2701 | 2017-06-09 08:21:42 -0400 | [diff] [blame] | 368 | if (!inner) { |
| 369 | return nullptr; |
| 370 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 371 | return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 372 | } |
| 373 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 374 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 375 | |
Matt Sarett | 6cc6ae75 | 2017-04-18 18:29:12 -0400 | [diff] [blame] | 376 | sk_sp<SkShader> SkTwoPointConicalGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
| 377 | SkSTArray<8, SkColor> origColorsStorage(fColorCount); |
| 378 | SkSTArray<8, SkScalar> origPosStorage(fColorCount); |
| 379 | SkSTArray<8, SkColor> xformedColorsStorage(fColorCount); |
| 380 | SkColor* origColors = origColorsStorage.begin(); |
| 381 | SkScalar* origPos = fOrigPos ? origPosStorage.begin() : nullptr; |
| 382 | SkColor* xformedColors = xformedColorsStorage.begin(); |
| 383 | |
| 384 | // Flip if necessary |
| 385 | SkPoint center1 = fFlippedGrad ? fCenter2 : fCenter1; |
| 386 | SkPoint center2 = fFlippedGrad ? fCenter1 : fCenter2; |
| 387 | SkScalar radius1 = fFlippedGrad ? fRadius2 : fRadius1; |
| 388 | SkScalar radius2 = fFlippedGrad ? fRadius1 : fRadius2; |
| 389 | for (int i = 0; i < fColorCount; i++) { |
| 390 | origColors[i] = fFlippedGrad ? fOrigColors[fColorCount - i - 1] : fOrigColors[i]; |
| 391 | if (origPos) { |
| 392 | origPos[i] = fFlippedGrad ? 1.0f - fOrigPos[fColorCount - i - 1] : fOrigPos[i]; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | xformer->apply(xformedColors, origColors, fColorCount); |
| 397 | return SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, xformedColors, |
| 398 | origPos, fColorCount, fTileMode, fGradFlags, |
| 399 | &this->getLocalMatrix()); |
| 400 | } |
| 401 | |
| 402 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 403 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 404 | void SkTwoPointConicalGradient::toString(SkString* str) const { |
| 405 | str->append("SkTwoPointConicalGradient: ("); |
| 406 | |
| 407 | str->append("center1: ("); |
| 408 | str->appendScalar(fCenter1.fX); |
| 409 | str->append(", "); |
| 410 | str->appendScalar(fCenter1.fY); |
| 411 | str->append(") radius1: "); |
| 412 | str->appendScalar(fRadius1); |
| 413 | str->append(" "); |
| 414 | |
| 415 | str->append("center2: ("); |
| 416 | str->appendScalar(fCenter2.fX); |
| 417 | str->append(", "); |
| 418 | str->appendScalar(fCenter2.fY); |
| 419 | str->append(") radius2: "); |
| 420 | str->appendScalar(fRadius2); |
| 421 | str->append(" "); |
| 422 | |
| 423 | this->INHERITED::toString(str); |
| 424 | |
| 425 | str->append(")"); |
| 426 | } |
| 427 | #endif |
Florin Malita | 0bb0411 | 2017-06-27 14:35:50 -0400 | [diff] [blame] | 428 | |
| 429 | bool SkTwoPointConicalGradient::adjustMatrixAndAppendStages(SkArenaAlloc* alloc, |
| 430 | SkMatrix* matrix, |
| 431 | SkRasterPipeline* p) const { |
Florin Malita | a66ef2d | 2017-06-28 10:02:40 -0400 | [diff] [blame^] | 432 | const auto dCenter = (fCenter1 - fCenter2).length(); |
| 433 | const auto dRadius = fRadius2 - fRadius1; |
| 434 | SkASSERT(dRadius >= 0); |
| 435 | |
Florin Malita | 0bb0411 | 2017-06-27 14:35:50 -0400 | [diff] [blame] | 436 | // When the two circles are concentric, we can pretend we're radial (with a tiny *twist). |
Florin Malita | a66ef2d | 2017-06-28 10:02:40 -0400 | [diff] [blame^] | 437 | if (SkScalarNearlyZero(dCenter)) { |
Florin Malita | 0bb0411 | 2017-06-27 14:35:50 -0400 | [diff] [blame] | 438 | matrix->postTranslate(-fCenter1.fX, -fCenter1.fY); |
| 439 | matrix->postScale(1 / fRadius2, 1 / fRadius2); |
| 440 | p->append(SkRasterPipeline::xy_to_radius); |
| 441 | |
| 442 | // Tiny twist: radial computes a t for [0, r2], but we want a t for [r1, r2]. |
Florin Malita | a66ef2d | 2017-06-28 10:02:40 -0400 | [diff] [blame^] | 443 | auto scale = fRadius2 / dRadius; |
| 444 | auto bias = -fRadius1 / dRadius; |
Florin Malita | 0bb0411 | 2017-06-27 14:35:50 -0400 | [diff] [blame] | 445 | |
| 446 | auto mad_matrix = SkMatrix::Concat(SkMatrix::MakeTrans(bias, 0), |
| 447 | SkMatrix::MakeScale(scale, 1)); |
| 448 | auto m = alloc->makeArrayDefault<float>(6); |
| 449 | SkAssertResult(mad_matrix.asAffine(m)); |
| 450 | p->append(SkRasterPipeline::matrix_2x3, m); |
| 451 | |
| 452 | return true; |
| 453 | } |
| 454 | |
Florin Malita | a66ef2d | 2017-06-28 10:02:40 -0400 | [diff] [blame^] | 455 | if (dCenter + fRadius1 > fRadius2) { |
| 456 | // We only handle well behaved cases for now. |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | // To simplify the stage math, we transform the universe (translate/scale/rotate) |
| 461 | // such that fCenter1 -> (0, 0) and fCenter2 -> (1, 0). |
| 462 | SkMatrix map_to_unit_vector; |
| 463 | const SkPoint centers[2] = { fCenter1, fCenter2 }; |
| 464 | const SkPoint unitvec[2] = { {0, 0}, {1, 0} }; |
| 465 | SkAssertResult(map_to_unit_vector.setPolyToPoly(centers, unitvec, 2)); |
| 466 | matrix->postConcat(map_to_unit_vector); |
| 467 | |
| 468 | // Since we've squashed the centers into a unit vector, we must also scale |
| 469 | // all the coefficient variables by (1 / dCenter). |
| 470 | const auto coeffA = 1 - dRadius * dRadius / (dCenter * dCenter); |
| 471 | if (SkScalarNearlyZero(coeffA)) { |
| 472 | // We only handle well behaved quadratic cases for now. |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | auto* ctx = alloc->make<SkJumper_2PtConicalCtx>(); |
| 477 | ctx->fCoeffA = coeffA; |
| 478 | ctx->fInvCoeffA = 1 / coeffA; |
| 479 | ctx->fR0 = fRadius1 / dCenter; |
| 480 | ctx->fDR = dRadius / dCenter; |
| 481 | |
| 482 | p->append(SkRasterPipeline::xy_to_2pt_conical, ctx); |
| 483 | return true; |
Florin Malita | 0bb0411 | 2017-06-27 14:35:50 -0400 | [diff] [blame] | 484 | } |