rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "SkRadialGradient.h" |
| 10 | #include "SkRadialGradient_Table.h" |
| 11 | |
| 12 | #define kSQRT_TABLE_BITS 11 |
| 13 | #define kSQRT_TABLE_SIZE (1 << kSQRT_TABLE_BITS) |
| 14 | |
reed@google.com | 4ec5c95 | 2013-12-19 15:00:18 +0000 | [diff] [blame] | 15 | #if 0 |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | void SkRadialGradient_BuildTable() { |
| 20 | // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table |
| 21 | |
| 22 | FILE* file = ::fopen("SkRadialGradient_Table.h", "w"); |
| 23 | SkASSERT(file); |
| 24 | ::fprintf(file, "static const uint8_t gSqrt8Table[] = {\n"); |
| 25 | |
| 26 | for (int i = 0; i < kSQRT_TABLE_SIZE; i++) { |
| 27 | if ((i & 15) == 0) { |
| 28 | ::fprintf(file, "\t"); |
| 29 | } |
| 30 | |
| 31 | uint8_t value = SkToU8(SkFixedSqrt(i * SK_Fixed1 / kSQRT_TABLE_SIZE) >> 8); |
| 32 | |
| 33 | ::fprintf(file, "0x%02X", value); |
| 34 | if (i < kSQRT_TABLE_SIZE-1) { |
| 35 | ::fprintf(file, ", "); |
| 36 | } |
| 37 | if ((i & 15) == 15) { |
| 38 | ::fprintf(file, "\n"); |
| 39 | } |
| 40 | } |
| 41 | ::fprintf(file, "};\n"); |
| 42 | ::fclose(file); |
| 43 | } |
| 44 | |
| 45 | #endif |
| 46 | |
| 47 | namespace { |
| 48 | |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 49 | // GCC doesn't like using static functions as template arguments. So force these to be non-static. |
| 50 | inline SkFixed mirror_tileproc_nonstatic(SkFixed x) { |
| 51 | return mirror_tileproc(x); |
| 52 | } |
| 53 | |
| 54 | inline SkFixed repeat_tileproc_nonstatic(SkFixed x) { |
| 55 | return repeat_tileproc(x); |
| 56 | } |
| 57 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 58 | void rad_to_unit_matrix(const SkPoint& center, SkScalar radius, |
| 59 | SkMatrix* matrix) { |
| 60 | SkScalar inv = SkScalarInvert(radius); |
| 61 | |
| 62 | matrix->setTranslate(-center.fX, -center.fY); |
| 63 | matrix->postScale(inv, inv); |
| 64 | } |
| 65 | |
| 66 | typedef void (* RadialShade16Proc)(SkScalar sfx, SkScalar sdx, |
| 67 | SkScalar sfy, SkScalar sdy, |
| 68 | uint16_t* dstC, const uint16_t* cache, |
| 69 | int toggle, int count); |
| 70 | |
| 71 | void shadeSpan16_radial_clamp(SkScalar sfx, SkScalar sdx, |
| 72 | SkScalar sfy, SkScalar sdy, |
| 73 | uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache, |
| 74 | int toggle, int count) { |
| 75 | const uint8_t* SK_RESTRICT sqrt_table = gSqrt8Table; |
| 76 | |
| 77 | /* knock these down so we can pin against +- 0x7FFF, which is an |
| 78 | immediate load, rather than 0xFFFF which is slower. This is a |
| 79 | compromise, since it reduces our precision, but that appears |
| 80 | to be visually OK. If we decide this is OK for all of our cases, |
| 81 | we could (it seems) put this scale-down into fDstToIndex, |
| 82 | to avoid having to do these extra shifts each time. |
| 83 | */ |
| 84 | SkFixed fx = SkScalarToFixed(sfx) >> 1; |
| 85 | SkFixed dx = SkScalarToFixed(sdx) >> 1; |
| 86 | SkFixed fy = SkScalarToFixed(sfy) >> 1; |
| 87 | SkFixed dy = SkScalarToFixed(sdy) >> 1; |
| 88 | // might perform this check for the other modes, |
| 89 | // but the win will be a smaller % of the total |
| 90 | if (dy == 0) { |
| 91 | fy = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1); |
| 92 | fy *= fy; |
| 93 | do { |
| 94 | unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1); |
| 95 | unsigned fi = (xx * xx + fy) >> (14 + 16 - kSQRT_TABLE_BITS); |
| 96 | fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); |
| 97 | fx += dx; |
| 98 | *dstC++ = cache[toggle + |
| 99 | (sqrt_table[fi] >> SkGradientShaderBase::kSqrt16Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 100 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 101 | } while (--count != 0); |
| 102 | } else { |
| 103 | do { |
| 104 | unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1); |
| 105 | unsigned fi = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1); |
| 106 | fi = (xx * xx + fi * fi) >> (14 + 16 - kSQRT_TABLE_BITS); |
| 107 | fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); |
| 108 | fx += dx; |
| 109 | fy += dy; |
| 110 | *dstC++ = cache[toggle + |
| 111 | (sqrt_table[fi] >> SkGradientShaderBase::kSqrt16Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 112 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 113 | } while (--count != 0); |
| 114 | } |
| 115 | } |
| 116 | |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 117 | template <SkFixed (*TileProc)(SkFixed)> |
| 118 | void shadeSpan16_radial(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar dy, |
| 119 | uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache, |
| 120 | int toggle, int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 121 | do { |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 122 | const SkFixed dist = SkFloatToFixed(sk_float_sqrt(fx*fx + fy*fy)); |
| 123 | const unsigned fi = TileProc(dist); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 124 | SkASSERT(fi <= 0xFFFF); |
| 125 | *dstC++ = cache[toggle + (fi >> SkGradientShaderBase::kCache16Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 126 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 127 | fx += dx; |
| 128 | fy += dy; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 129 | } while (--count != 0); |
| 130 | } |
| 131 | |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 132 | void shadeSpan16_radial_mirror(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar dy, |
| 133 | uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache, |
| 134 | int toggle, int count) { |
| 135 | shadeSpan16_radial<mirror_tileproc_nonstatic>(fx, dx, fy, dy, dstC, cache, toggle, count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 136 | } |
| 137 | |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 138 | void shadeSpan16_radial_repeat(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar dy, |
| 139 | uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache, |
| 140 | int toggle, int count) { |
| 141 | shadeSpan16_radial<repeat_tileproc_nonstatic>(fx, dx, fy, dy, dstC, cache, toggle, count); |
| 142 | } |
| 143 | |
| 144 | } // namespace |
| 145 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 146 | ///////////////////////////////////////////////////////////////////// |
| 147 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 148 | SkRadialGradient::SkRadialGradient(const SkPoint& center, SkScalar radius, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 149 | const Descriptor& desc, const SkMatrix* localMatrix) |
| 150 | : SkGradientShaderBase(desc, localMatrix), |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 151 | fCenter(center), |
| 152 | fRadius(radius) |
| 153 | { |
| 154 | // make sure our table is insync with our current #define for kSQRT_TABLE_SIZE |
| 155 | SkASSERT(sizeof(gSqrt8Table) == kSQRT_TABLE_SIZE); |
| 156 | |
| 157 | rad_to_unit_matrix(center, radius, &fPtsToUnit); |
| 158 | } |
| 159 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 160 | size_t SkRadialGradient::contextSize() const { |
| 161 | return sizeof(RadialGradientContext); |
| 162 | } |
| 163 | |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 164 | SkShader::Context* SkRadialGradient::createContext(const ContextRec& rec, void* storage) const { |
| 165 | if (!this->validContext(rec)) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 166 | return NULL; |
| 167 | } |
| 168 | |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 169 | return SkNEW_PLACEMENT_ARGS(storage, RadialGradientContext, (*this, rec)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | SkRadialGradient::RadialGradientContext::RadialGradientContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 173 | const SkRadialGradient& shader, const ContextRec& rec) |
| 174 | : INHERITED(shader, rec) {} |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 175 | |
| 176 | void SkRadialGradient::RadialGradientContext::shadeSpan16(int x, int y, uint16_t* dstCParam, |
| 177 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 178 | SkASSERT(count > 0); |
| 179 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 180 | const SkRadialGradient& radialGradient = static_cast<const SkRadialGradient&>(fShader); |
| 181 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 182 | uint16_t* SK_RESTRICT dstC = dstCParam; |
| 183 | |
| 184 | SkPoint srcPt; |
| 185 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 186 | TileProc proc = radialGradient.fTileProc; |
| 187 | const uint16_t* SK_RESTRICT cache = fCache->getCache16(); |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 188 | int toggle = init_dither_toggle16(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 189 | |
| 190 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 191 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 192 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 193 | |
| 194 | SkScalar sdx = fDstToIndex.getScaleX(); |
| 195 | SkScalar sdy = fDstToIndex.getSkewY(); |
| 196 | |
| 197 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 198 | SkFixed storage[2]; |
| 199 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), |
| 200 | &storage[0], &storage[1]); |
| 201 | sdx = SkFixedToScalar(storage[0]); |
| 202 | sdy = SkFixedToScalar(storage[1]); |
| 203 | } else { |
| 204 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 205 | } |
| 206 | |
| 207 | RadialShade16Proc shadeProc = shadeSpan16_radial_repeat; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 208 | if (SkShader::kClamp_TileMode == radialGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 209 | shadeProc = shadeSpan16_radial_clamp; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 210 | } else if (SkShader::kMirror_TileMode == radialGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 211 | shadeProc = shadeSpan16_radial_mirror; |
| 212 | } else { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 213 | SkASSERT(SkShader::kRepeat_TileMode == radialGradient.fTileMode); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 214 | } |
| 215 | (*shadeProc)(srcPt.fX, sdx, srcPt.fY, sdy, dstC, |
| 216 | cache, toggle, count); |
| 217 | } else { // perspective case |
| 218 | SkScalar dstX = SkIntToScalar(x); |
| 219 | SkScalar dstY = SkIntToScalar(y); |
| 220 | do { |
| 221 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 222 | unsigned fi = proc(SkScalarToFixed(srcPt.length())); |
| 223 | SkASSERT(fi <= 0xFFFF); |
| 224 | |
| 225 | int index = fi >> (16 - kCache16Bits); |
| 226 | *dstC++ = cache[toggle + index]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 227 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 228 | |
| 229 | dstX += SK_Scalar1; |
| 230 | } while (--count != 0); |
| 231 | } |
| 232 | } |
| 233 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 234 | SkShader::BitmapType SkRadialGradient::asABitmap(SkBitmap* bitmap, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 235 | SkMatrix* matrix, SkShader::TileMode* xy) const { |
| 236 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 237 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 238 | } |
| 239 | if (matrix) { |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 240 | matrix->setScale(SkIntToScalar(kCache32Count), |
| 241 | SkIntToScalar(kCache32Count)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 242 | matrix->preConcat(fPtsToUnit); |
| 243 | } |
| 244 | if (xy) { |
| 245 | xy[0] = fTileMode; |
| 246 | xy[1] = kClamp_TileMode; |
| 247 | } |
| 248 | return kRadial_BitmapType; |
| 249 | } |
| 250 | |
| 251 | SkShader::GradientType SkRadialGradient::asAGradient(GradientInfo* info) const { |
| 252 | if (info) { |
| 253 | commonAsAGradient(info); |
| 254 | info->fPoint[0] = fCenter; |
| 255 | info->fRadius[0] = fRadius; |
| 256 | } |
| 257 | return kRadial_GradientType; |
| 258 | } |
| 259 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 260 | SkRadialGradient::SkRadialGradient(SkReadBuffer& buffer) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 261 | : INHERITED(buffer), |
| 262 | fCenter(buffer.readPoint()), |
| 263 | fRadius(buffer.readScalar()) { |
| 264 | } |
| 265 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 266 | void SkRadialGradient::flatten(SkWriteBuffer& buffer) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 267 | this->INHERITED::flatten(buffer); |
| 268 | buffer.writePoint(fCenter); |
| 269 | buffer.writeScalar(fRadius); |
| 270 | } |
| 271 | |
| 272 | namespace { |
| 273 | |
| 274 | inline bool radial_completely_pinned(int fx, int dx, int fy, int dy) { |
| 275 | // fast, overly-conservative test: checks unit square instead |
| 276 | // of unit circle |
| 277 | bool xClamped = (fx >= SK_FixedHalf && dx >= 0) || |
| 278 | (fx <= -SK_FixedHalf && dx <= 0); |
| 279 | bool yClamped = (fy >= SK_FixedHalf && dy >= 0) || |
| 280 | (fy <= -SK_FixedHalf && dy <= 0); |
| 281 | |
| 282 | return xClamped || yClamped; |
| 283 | } |
| 284 | |
| 285 | // Return true if (fx * fy) is always inside the unit circle |
| 286 | // SkPin32 is expensive, but so are all the SkFixedMul in this test, |
| 287 | // so it shouldn't be run if count is small. |
| 288 | inline bool no_need_for_radial_pin(int fx, int dx, |
| 289 | int fy, int dy, int count) { |
| 290 | SkASSERT(count > 0); |
| 291 | if (SkAbs32(fx) > 0x7FFF || SkAbs32(fy) > 0x7FFF) { |
| 292 | return false; |
| 293 | } |
| 294 | if (fx*fx + fy*fy > 0x7FFF*0x7FFF) { |
| 295 | return false; |
| 296 | } |
| 297 | fx += (count - 1) * dx; |
| 298 | fy += (count - 1) * dy; |
| 299 | if (SkAbs32(fx) > 0x7FFF || SkAbs32(fy) > 0x7FFF) { |
| 300 | return false; |
| 301 | } |
| 302 | return fx*fx + fy*fy <= 0x7FFF*0x7FFF; |
| 303 | } |
| 304 | |
| 305 | #define UNPINNED_RADIAL_STEP \ |
| 306 | fi = (fx * fx + fy * fy) >> (14 + 16 - kSQRT_TABLE_BITS); \ |
| 307 | *dstC++ = cache[toggle + \ |
| 308 | (sqrt_table[fi] >> SkGradientShaderBase::kSqrt32Shift)]; \ |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 309 | toggle = next_dither_toggle(toggle); \ |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 310 | fx += dx; \ |
| 311 | fy += dy; |
| 312 | |
| 313 | typedef void (* RadialShadeProc)(SkScalar sfx, SkScalar sdx, |
| 314 | SkScalar sfy, SkScalar sdy, |
| 315 | SkPMColor* dstC, const SkPMColor* cache, |
| 316 | int count, int toggle); |
| 317 | |
| 318 | // On Linux, this is faster with SkPMColor[] params than SkPMColor* SK_RESTRICT |
| 319 | void shadeSpan_radial_clamp(SkScalar sfx, SkScalar sdx, |
| 320 | SkScalar sfy, SkScalar sdy, |
| 321 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 322 | int count, int toggle) { |
| 323 | // Floating point seems to be slower than fixed point, |
| 324 | // even when we have float hardware. |
| 325 | const uint8_t* SK_RESTRICT sqrt_table = gSqrt8Table; |
| 326 | SkFixed fx = SkScalarToFixed(sfx) >> 1; |
| 327 | SkFixed dx = SkScalarToFixed(sdx) >> 1; |
| 328 | SkFixed fy = SkScalarToFixed(sfy) >> 1; |
| 329 | SkFixed dy = SkScalarToFixed(sdy) >> 1; |
| 330 | if ((count > 4) && radial_completely_pinned(fx, dx, fy, dy)) { |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 331 | unsigned fi = SkGradientShaderBase::kCache32Count - 1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 332 | sk_memset32_dither(dstC, |
| 333 | cache[toggle + fi], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 334 | cache[next_dither_toggle(toggle) + fi], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 335 | count); |
| 336 | } else if ((count > 4) && |
| 337 | no_need_for_radial_pin(fx, dx, fy, dy, count)) { |
| 338 | unsigned fi; |
| 339 | // 4x unroll appears to be no faster than 2x unroll on Linux |
| 340 | while (count > 1) { |
| 341 | UNPINNED_RADIAL_STEP; |
| 342 | UNPINNED_RADIAL_STEP; |
| 343 | count -= 2; |
| 344 | } |
| 345 | if (count) { |
| 346 | UNPINNED_RADIAL_STEP; |
| 347 | } |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 348 | } else { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 349 | // Specializing for dy == 0 gains us 25% on Skia benchmarks |
| 350 | if (dy == 0) { |
| 351 | unsigned yy = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1); |
| 352 | yy *= yy; |
| 353 | do { |
| 354 | unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1); |
| 355 | unsigned fi = (xx * xx + yy) >> (14 + 16 - kSQRT_TABLE_BITS); |
| 356 | fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); |
| 357 | *dstC++ = cache[toggle + (sqrt_table[fi] >> |
| 358 | SkGradientShaderBase::kSqrt32Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 359 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 360 | fx += dx; |
| 361 | } while (--count != 0); |
| 362 | } else { |
| 363 | do { |
| 364 | unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1); |
| 365 | unsigned fi = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1); |
| 366 | fi = (xx * xx + fi * fi) >> (14 + 16 - kSQRT_TABLE_BITS); |
| 367 | fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); |
| 368 | *dstC++ = cache[toggle + (sqrt_table[fi] >> |
| 369 | SkGradientShaderBase::kSqrt32Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 370 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 371 | fx += dx; |
| 372 | fy += dy; |
| 373 | } while (--count != 0); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Unrolling this loop doesn't seem to help (when float); we're stalling to |
| 379 | // get the results of the sqrt (?), and don't have enough extra registers to |
| 380 | // have many in flight. |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 381 | template <SkFixed (*TileProc)(SkFixed)> |
| 382 | void shadeSpan_radial(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar dy, |
| 383 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 384 | int count, int toggle) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 385 | do { |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 386 | const SkFixed dist = SkFloatToFixed(sk_float_sqrt(fx*fx + fy*fy)); |
| 387 | const unsigned fi = TileProc(dist); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 388 | SkASSERT(fi <= 0xFFFF); |
| 389 | *dstC++ = cache[toggle + (fi >> SkGradientShaderBase::kCache32Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 390 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 391 | fx += dx; |
| 392 | fy += dy; |
| 393 | } while (--count != 0); |
| 394 | } |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 395 | |
| 396 | void shadeSpan_radial_mirror(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar dy, |
| 397 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 398 | int count, int toggle) { |
| 399 | shadeSpan_radial<mirror_tileproc_nonstatic>(fx, dx, fy, dy, dstC, cache, count, toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 400 | } |
| 401 | |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 402 | void shadeSpan_radial_repeat(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar dy, |
| 403 | SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache, |
| 404 | int count, int toggle) { |
| 405 | shadeSpan_radial<repeat_tileproc_nonstatic>(fx, dx, fy, dy, dstC, cache, count, toggle); |
| 406 | } |
| 407 | |
| 408 | } // namespace |
| 409 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 410 | void SkRadialGradient::RadialGradientContext::shadeSpan(int x, int y, |
| 411 | SkPMColor* SK_RESTRICT dstC, int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 412 | SkASSERT(count > 0); |
| 413 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 414 | const SkRadialGradient& radialGradient = static_cast<const SkRadialGradient&>(fShader); |
| 415 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 416 | SkPoint srcPt; |
| 417 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 418 | TileProc proc = radialGradient.fTileProc; |
| 419 | const SkPMColor* SK_RESTRICT cache = fCache->getCache32(); |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 420 | int toggle = init_dither_toggle(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 421 | |
| 422 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 423 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 424 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 425 | SkScalar sdx = fDstToIndex.getScaleX(); |
| 426 | SkScalar sdy = fDstToIndex.getSkewY(); |
| 427 | |
| 428 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 429 | SkFixed storage[2]; |
| 430 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), |
| 431 | &storage[0], &storage[1]); |
| 432 | sdx = SkFixedToScalar(storage[0]); |
| 433 | sdy = SkFixedToScalar(storage[1]); |
| 434 | } else { |
| 435 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 436 | } |
| 437 | |
| 438 | RadialShadeProc shadeProc = shadeSpan_radial_repeat; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 439 | if (SkShader::kClamp_TileMode == radialGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 440 | shadeProc = shadeSpan_radial_clamp; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 441 | } else if (SkShader::kMirror_TileMode == radialGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 442 | shadeProc = shadeSpan_radial_mirror; |
| 443 | } else { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 444 | SkASSERT(SkShader::kRepeat_TileMode == radialGradient.fTileMode); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 445 | } |
| 446 | (*shadeProc)(srcPt.fX, sdx, srcPt.fY, sdy, dstC, cache, count, toggle); |
| 447 | } else { // perspective case |
| 448 | SkScalar dstX = SkIntToScalar(x); |
| 449 | SkScalar dstY = SkIntToScalar(y); |
| 450 | do { |
| 451 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 452 | unsigned fi = proc(SkScalarToFixed(srcPt.length())); |
| 453 | SkASSERT(fi <= 0xFFFF); |
| 454 | *dstC++ = cache[fi >> SkGradientShaderBase::kCache32Shift]; |
| 455 | dstX += SK_Scalar1; |
| 456 | } while (--count != 0); |
| 457 | } |
| 458 | } |
| 459 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 460 | ///////////////////////////////////////////////////////////////////// |
| 461 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 462 | #if SK_SUPPORT_GPU |
| 463 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 464 | #include "GrTBackendEffectFactory.h" |
| 465 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 466 | class GrGLRadialGradient : public GrGLGradientEffect { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 467 | public: |
| 468 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 469 | GrGLRadialGradient(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 470 | const GrDrawEffect&) : INHERITED (factory) { } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 471 | virtual ~GrGLRadialGradient() { } |
| 472 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 473 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 474 | const GrDrawEffect&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 475 | EffectKey, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 476 | const char* outputColor, |
| 477 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 478 | const TransformedCoordsArray&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 479 | const TextureSamplerArray&) SK_OVERRIDE; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 480 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 481 | static EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 482 | return GenBaseGradientKey(drawEffect); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 483 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 484 | |
| 485 | private: |
| 486 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 487 | typedef GrGLGradientEffect INHERITED; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 488 | |
| 489 | }; |
| 490 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 491 | ///////////////////////////////////////////////////////////////////// |
| 492 | |
| 493 | class GrRadialGradient : public GrGradientEffect { |
| 494 | public: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 495 | static GrEffectRef* Create(GrContext* ctx, |
| 496 | const SkRadialGradient& shader, |
| 497 | const SkMatrix& matrix, |
| 498 | SkShader::TileMode tm) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 499 | AutoEffectUnref effect(SkNEW_ARGS(GrRadialGradient, (ctx, shader, matrix, tm))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 500 | return CreateEffectRef(effect); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | virtual ~GrRadialGradient() { } |
| 504 | |
| 505 | static const char* Name() { return "Radial Gradient"; } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 506 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 507 | return GrTBackendEffectFactory<GrRadialGradient>::getInstance(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 508 | } |
| 509 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 510 | typedef GrGLRadialGradient GLEffect; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 511 | |
| 512 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 513 | GrRadialGradient(GrContext* ctx, |
| 514 | const SkRadialGradient& shader, |
| 515 | const SkMatrix& matrix, |
| 516 | SkShader::TileMode tm) |
| 517 | : INHERITED(ctx, shader, matrix, tm) { |
| 518 | } |
| 519 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 520 | GR_DECLARE_EFFECT_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 521 | |
| 522 | typedef GrGradientEffect INHERITED; |
| 523 | }; |
| 524 | |
| 525 | ///////////////////////////////////////////////////////////////////// |
| 526 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 527 | GR_DEFINE_EFFECT_TEST(GrRadialGradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 528 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 529 | GrEffectRef* GrRadialGradient::TestCreate(SkRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 530 | GrContext* context, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 531 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 532 | GrTexture**) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 533 | SkPoint center = {random->nextUScalar1(), random->nextUScalar1()}; |
| 534 | SkScalar radius = random->nextUScalar1(); |
| 535 | |
| 536 | SkColor colors[kMaxRandomGradientColors]; |
| 537 | SkScalar stopsArray[kMaxRandomGradientColors]; |
| 538 | SkScalar* stops = stopsArray; |
| 539 | SkShader::TileMode tm; |
| 540 | int colorCount = RandomGradientParams(random, colors, &stops, &tm); |
| 541 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateRadial(center, radius, |
| 542 | colors, stops, colorCount, |
| 543 | tm)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 544 | SkPaint paint; |
| 545 | return shader->asNewEffect(context, paint); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | ///////////////////////////////////////////////////////////////////// |
| 549 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 550 | void GrGLRadialGradient::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 551 | const GrDrawEffect&, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 552 | EffectKey key, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 553 | const char* outputColor, |
| 554 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 555 | const TransformedCoordsArray& coords, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 556 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 557 | this->emitUniforms(builder, key); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 558 | SkString t("length("); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 559 | t.append(builder->ensureFSCoords2D(coords, 0)); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 560 | t.append(")"); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 561 | this->emitColor(builder, t.c_str(), key, outputColor, inputColor, samplers); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 562 | } |
| 563 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 564 | ///////////////////////////////////////////////////////////////////// |
| 565 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 566 | GrEffectRef* SkRadialGradient::asNewEffect(GrContext* context, const SkPaint&) const { |
bsalomon@google.com | 00835cc | 2013-01-14 17:07:22 +0000 | [diff] [blame] | 567 | SkASSERT(NULL != context); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 568 | |
| 569 | SkMatrix matrix; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 570 | if (!this->getLocalMatrix().invert(&matrix)) { |
humper@google.com | 84831ac | 2013-01-14 22:09:54 +0000 | [diff] [blame] | 571 | return NULL; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 572 | } |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 573 | matrix.postConcat(fPtsToUnit); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 574 | return GrRadialGradient::Create(context, *this, matrix, fTileMode); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 575 | } |
| 576 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 577 | #else |
| 578 | |
bsalomon@google.com | 5d2cd20 | 2013-01-16 15:31:06 +0000 | [diff] [blame] | 579 | GrEffectRef* SkRadialGradient::asNewEffect(GrContext*, const SkPaint&) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 580 | SkDEBUGFAIL("Should not call in GPU-less build"); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 581 | return NULL; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 585 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 586 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 587 | void SkRadialGradient::toString(SkString* str) const { |
| 588 | str->append("SkRadialGradient: ("); |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 589 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 590 | str->append("center: ("); |
| 591 | str->appendScalar(fCenter.fX); |
| 592 | str->append(", "); |
| 593 | str->appendScalar(fCenter.fY); |
| 594 | str->append(") radius: "); |
| 595 | str->appendScalar(fRadius); |
| 596 | str->append(" "); |
| 597 | |
| 598 | this->INHERITED::toString(str); |
| 599 | |
| 600 | str->append(")"); |
| 601 | } |
| 602 | #endif |