| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "SkRadialGradient.h" | 9 #include "SkRadialGradient.h" |
| 10 #include "SkRadialGradient_Table.h" | |
| 11 #include "SkNx.h" | 10 #include "SkNx.h" |
| 12 | 11 |
| 13 #define kSQRT_TABLE_BITS 11 | |
| 14 #define kSQRT_TABLE_SIZE (1 << kSQRT_TABLE_BITS) | |
| 15 | |
| 16 static_assert(sizeof(gSqrt8Table) == kSQRT_TABLE_SIZE, "SqrtTableSizesMatch"); | |
| 17 | |
| 18 #if 0 | |
| 19 | |
| 20 #include <stdio.h> | |
| 21 | |
| 22 void SkRadialGradient_BuildTable() { | |
| 23 // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fi
xed" table | |
| 24 | |
| 25 FILE* file = ::fopen("SkRadialGradient_Table.h", "w"); | |
| 26 SkASSERT(file); | |
| 27 ::fprintf(file, "static const uint8_t gSqrt8Table[] = {\n"); | |
| 28 | |
| 29 for (int i = 0; i < kSQRT_TABLE_SIZE; i++) { | |
| 30 if ((i & 15) == 0) { | |
| 31 ::fprintf(file, "\t"); | |
| 32 } | |
| 33 | |
| 34 uint8_t value = SkToU8(SkFixedSqrt(i * SK_Fixed1 / kSQRT_TABLE_SIZE) >>
8); | |
| 35 | |
| 36 ::fprintf(file, "0x%02X", value); | |
| 37 if (i < kSQRT_TABLE_SIZE-1) { | |
| 38 ::fprintf(file, ", "); | |
| 39 } | |
| 40 if ((i & 15) == 15) { | |
| 41 ::fprintf(file, "\n"); | |
| 42 } | |
| 43 } | |
| 44 ::fprintf(file, "};\n"); | |
| 45 ::fclose(file); | |
| 46 } | |
| 47 | |
| 48 #endif | |
| 49 | |
| 50 namespace { | 12 namespace { |
| 51 | 13 |
| 52 // GCC doesn't like using static functions as template arguments. So force thes
e to be non-static. | 14 // GCC doesn't like using static functions as template arguments. So force thes
e to be non-static. |
| 53 inline SkFixed mirror_tileproc_nonstatic(SkFixed x) { | 15 inline SkFixed mirror_tileproc_nonstatic(SkFixed x) { |
| 54 return mirror_tileproc(x); | 16 return mirror_tileproc(x); |
| 55 } | 17 } |
| 56 | 18 |
| 57 inline SkFixed repeat_tileproc_nonstatic(SkFixed x) { | 19 inline SkFixed repeat_tileproc_nonstatic(SkFixed x) { |
| 58 return repeat_tileproc(x); | 20 return repeat_tileproc(x); |
| 59 } | 21 } |
| 60 | 22 |
| 61 SkMatrix rad_to_unit_matrix(const SkPoint& center, SkScalar radius) { | 23 SkMatrix rad_to_unit_matrix(const SkPoint& center, SkScalar radius) { |
| 62 SkScalar inv = SkScalarInvert(radius); | 24 SkScalar inv = SkScalarInvert(radius); |
| 63 | 25 |
| 64 SkMatrix matrix; | 26 SkMatrix matrix; |
| 65 matrix.setTranslate(-center.fX, -center.fY); | 27 matrix.setTranslate(-center.fX, -center.fY); |
| 66 matrix.postScale(inv, inv); | 28 matrix.postScale(inv, inv); |
| 67 return matrix; | 29 return matrix; |
| 68 } | 30 } |
| 69 | 31 |
| 70 typedef void (* RadialShade16Proc)(SkScalar sfx, SkScalar sdx, | |
| 71 SkScalar sfy, SkScalar sdy, | |
| 72 uint16_t* dstC, const uint16_t* cache, | |
| 73 int toggle, int count); | |
| 74 | |
| 75 void shadeSpan16_radial_clamp(SkScalar sfx, SkScalar sdx, | |
| 76 SkScalar sfy, SkScalar sdy, | |
| 77 uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache, | |
| 78 int toggle, int count) { | |
| 79 const uint8_t* SK_RESTRICT sqrt_table = gSqrt8Table; | |
| 80 | |
| 81 /* knock these down so we can pin against +- 0x7FFF, which is an | |
| 82 immediate load, rather than 0xFFFF which is slower. This is a | |
| 83 compromise, since it reduces our precision, but that appears | |
| 84 to be visually OK. If we decide this is OK for all of our cases, | |
| 85 we could (it seems) put this scale-down into fDstToIndex, | |
| 86 to avoid having to do these extra shifts each time. | |
| 87 */ | |
| 88 SkFixed fx = SkScalarToFixed(sfx) >> 1; | |
| 89 SkFixed dx = SkScalarToFixed(sdx) >> 1; | |
| 90 SkFixed fy = SkScalarToFixed(sfy) >> 1; | |
| 91 SkFixed dy = SkScalarToFixed(sdy) >> 1; | |
| 92 // might perform this check for the other modes, | |
| 93 // but the win will be a smaller % of the total | |
| 94 if (dy == 0) { | |
| 95 fy = SkTPin(fy, -0xFFFF >> 1, 0xFFFF >> 1); | |
| 96 fy *= fy; | |
| 97 do { | |
| 98 unsigned xx = SkTPin(fx, -0xFFFF >> 1, 0xFFFF >> 1); | |
| 99 unsigned fi = (xx * xx + fy) >> (14 + 16 - kSQRT_TABLE_BITS); | |
| 100 fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); | |
| 101 fx += dx; | |
| 102 *dstC++ = cache[toggle + | |
| 103 (sqrt_table[fi] >> SkGradientShaderBase::kSqrt16Shif
t)]; | |
| 104 toggle = next_dither_toggle16(toggle); | |
| 105 } while (--count != 0); | |
| 106 } else { | |
| 107 do { | |
| 108 unsigned xx = SkTPin(fx, -0xFFFF >> 1, 0xFFFF >> 1); | |
| 109 unsigned fi = SkTPin(fy, -0xFFFF >> 1, 0xFFFF >> 1); | |
| 110 fi = (xx * xx + fi * fi) >> (14 + 16 - kSQRT_TABLE_BITS); | |
| 111 fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); | |
| 112 fx += dx; | |
| 113 fy += dy; | |
| 114 *dstC++ = cache[toggle + | |
| 115 (sqrt_table[fi] >> SkGradientShaderBase::kSqrt16Shif
t)]; | |
| 116 toggle = next_dither_toggle16(toggle); | |
| 117 } while (--count != 0); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 template <SkFixed (*TileProc)(SkFixed)> | |
| 122 void shadeSpan16_radial(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar dy, | |
| 123 uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT
cache, | |
| 124 int toggle, int count) { | |
| 125 do { | |
| 126 const SkFixed dist = SkFloatToFixed(sk_float_sqrt(fx*fx + fy*fy)); | |
| 127 const unsigned fi = TileProc(dist); | |
| 128 SkASSERT(fi <= 0xFFFF); | |
| 129 *dstC++ = cache[toggle + (fi >> SkGradientShaderBase::kCache16Shift)]; | |
| 130 toggle = next_dither_toggle16(toggle); | |
| 131 fx += dx; | |
| 132 fy += dy; | |
| 133 } while (--count != 0); | |
| 134 } | |
| 135 | |
| 136 void shadeSpan16_radial_mirror(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar d
y, | |
| 137 uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RE
STRICT cache, | |
| 138 int toggle, int count) { | |
| 139 shadeSpan16_radial<mirror_tileproc_nonstatic>(fx, dx, fy, dy, dstC, cache, t
oggle, count); | |
| 140 } | |
| 141 | |
| 142 void shadeSpan16_radial_repeat(SkScalar fx, SkScalar dx, SkScalar fy, SkScalar d
y, | |
| 143 uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RE
STRICT cache, | |
| 144 int toggle, int count) { | |
| 145 shadeSpan16_radial<repeat_tileproc_nonstatic>(fx, dx, fy, dy, dstC, cache, t
oggle, count); | |
| 146 } | |
| 147 | 32 |
| 148 } // namespace | 33 } // namespace |
| 149 | 34 |
| 150 ///////////////////////////////////////////////////////////////////// | 35 ///////////////////////////////////////////////////////////////////// |
| 151 | 36 |
| 152 SkRadialGradient::SkRadialGradient(const SkPoint& center, SkScalar radius, const
Descriptor& desc) | 37 SkRadialGradient::SkRadialGradient(const SkPoint& center, SkScalar radius, const
Descriptor& desc) |
| 153 : SkGradientShaderBase(desc, rad_to_unit_matrix(center, radius)) | 38 : SkGradientShaderBase(desc, rad_to_unit_matrix(center, radius)) |
| 154 , fCenter(center) | 39 , fCenter(center) |
| 155 , fRadius(radius) { | 40 , fRadius(radius) { |
| 156 } | 41 } |
| 157 | 42 |
| 158 size_t SkRadialGradient::contextSize() const { | 43 size_t SkRadialGradient::contextSize() const { |
| 159 return sizeof(RadialGradientContext); | 44 return sizeof(RadialGradientContext); |
| 160 } | 45 } |
| 161 | 46 |
| 162 SkShader::Context* SkRadialGradient::onCreateContext(const ContextRec& rec, void
* storage) const { | 47 SkShader::Context* SkRadialGradient::onCreateContext(const ContextRec& rec, void
* storage) const { |
| 163 return new (storage) RadialGradientContext(*this, rec); | 48 return new (storage) RadialGradientContext(*this, rec); |
| 164 } | 49 } |
| 165 | 50 |
| 166 SkRadialGradient::RadialGradientContext::RadialGradientContext( | 51 SkRadialGradient::RadialGradientContext::RadialGradientContext( |
| 167 const SkRadialGradient& shader, const ContextRec& rec) | 52 const SkRadialGradient& shader, const ContextRec& rec) |
| 168 : INHERITED(shader, rec) {} | 53 : INHERITED(shader, rec) {} |
| 169 | 54 |
| 170 void SkRadialGradient::RadialGradientContext::shadeSpan16(int x, int y, uint16_t
* dstCParam, | |
| 171 int count) { | |
| 172 SkASSERT(count > 0); | |
| 173 | |
| 174 const SkRadialGradient& radialGradient = static_cast<const SkRadialGradient&
>(fShader); | |
| 175 | |
| 176 uint16_t* SK_RESTRICT dstC = dstCParam; | |
| 177 | |
| 178 SkPoint srcPt; | |
| 179 SkMatrix::MapXYProc dstProc = fDstToIndexProc; | |
| 180 TileProc proc = radialGradient.fTileProc; | |
| 181 const uint16_t* SK_RESTRICT cache = fCache->getCache16(); | |
| 182 int toggle = init_dither_toggle16(x, y); | |
| 183 | |
| 184 if (fDstToIndexClass != kPerspective_MatrixClass) { | |
| 185 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, | |
| 186 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | |
| 187 | |
| 188 SkScalar sdx = fDstToIndex.getScaleX(); | |
| 189 SkScalar sdy = fDstToIndex.getSkewY(); | |
| 190 | |
| 191 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { | |
| 192 SkFixed storage[2]; | |
| 193 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), | |
| 194 &storage[0], &storage[1]); | |
| 195 sdx = SkFixedToScalar(storage[0]); | |
| 196 sdy = SkFixedToScalar(storage[1]); | |
| 197 } else { | |
| 198 SkASSERT(fDstToIndexClass == kLinear_MatrixClass); | |
| 199 } | |
| 200 | |
| 201 RadialShade16Proc shadeProc = shadeSpan16_radial_repeat; | |
| 202 if (SkShader::kClamp_TileMode == radialGradient.fTileMode) { | |
| 203 shadeProc = shadeSpan16_radial_clamp; | |
| 204 } else if (SkShader::kMirror_TileMode == radialGradient.fTileMode) { | |
| 205 shadeProc = shadeSpan16_radial_mirror; | |
| 206 } else { | |
| 207 SkASSERT(SkShader::kRepeat_TileMode == radialGradient.fTileMode); | |
| 208 } | |
| 209 (*shadeProc)(srcPt.fX, sdx, srcPt.fY, sdy, dstC, | |
| 210 cache, toggle, count); | |
| 211 } else { // perspective case | |
| 212 SkScalar dstX = SkIntToScalar(x); | |
| 213 SkScalar dstY = SkIntToScalar(y); | |
| 214 do { | |
| 215 dstProc(fDstToIndex, dstX, dstY, &srcPt); | |
| 216 unsigned fi = proc(SkScalarToFixed(srcPt.length())); | |
| 217 SkASSERT(fi <= 0xFFFF); | |
| 218 | |
| 219 int index = fi >> (16 - kCache16Bits); | |
| 220 *dstC++ = cache[toggle + index]; | |
| 221 toggle = next_dither_toggle16(toggle); | |
| 222 | |
| 223 dstX += SK_Scalar1; | |
| 224 } while (--count != 0); | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 SkShader::GradientType SkRadialGradient::asAGradient(GradientInfo* info) const { | 55 SkShader::GradientType SkRadialGradient::asAGradient(GradientInfo* info) const { |
| 229 if (info) { | 56 if (info) { |
| 230 commonAsAGradient(info); | 57 commonAsAGradient(info); |
| 231 info->fPoint[0] = fCenter; | 58 info->fPoint[0] = fCenter; |
| 232 info->fRadius[0] = fRadius; | 59 info->fRadius[0] = fRadius; |
| 233 } | 60 } |
| 234 return kRadial_GradientType; | 61 return kRadial_GradientType; |
| 235 } | 62 } |
| 236 | 63 |
| 237 SkFlattenable* SkRadialGradient::CreateProc(SkReadBuffer& buffer) { | 64 SkFlattenable* SkRadialGradient::CreateProc(SkReadBuffer& buffer) { |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 str->appendScalar(fCenter.fY); | 378 str->appendScalar(fCenter.fY); |
| 552 str->append(") radius: "); | 379 str->append(") radius: "); |
| 553 str->appendScalar(fRadius); | 380 str->appendScalar(fRadius); |
| 554 str->append(" "); | 381 str->append(" "); |
| 555 | 382 |
| 556 this->INHERITED::toString(str); | 383 this->INHERITED::toString(str); |
| 557 | 384 |
| 558 str->append(")"); | 385 str->append(")"); |
| 559 } | 386 } |
| 560 #endif | 387 #endif |
| OLD | NEW |