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 "SkLinearGradient.h" |
| 10 | |
| 11 | static inline int repeat_bits(int x, const int bits) { |
| 12 | return x & ((1 << bits) - 1); |
| 13 | } |
| 14 | |
| 15 | static inline int repeat_8bits(int x) { |
| 16 | return x & 0xFF; |
| 17 | } |
| 18 | |
| 19 | // Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly. |
| 20 | // See http://code.google.com/p/skia/issues/detail?id=472 |
| 21 | #if defined(_MSC_VER) && (_MSC_VER >= 1600) |
| 22 | #pragma optimize("", off) |
| 23 | #endif |
| 24 | |
| 25 | static inline int mirror_bits(int x, const int bits) { |
| 26 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 27 | if (x & (1 << bits)) |
| 28 | x = ~x; |
| 29 | return x & ((1 << bits) - 1); |
| 30 | #else |
| 31 | int s = x << (31 - bits) >> 31; |
| 32 | return (x ^ s) & ((1 << bits) - 1); |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | static inline int mirror_8bits(int x) { |
| 37 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 38 | if (x & 256) { |
| 39 | x = ~x; |
| 40 | } |
| 41 | return x & 255; |
| 42 | #else |
| 43 | int s = x << 23 >> 31; |
| 44 | return (x ^ s) & 0xFF; |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | #if defined(_MSC_VER) && (_MSC_VER >= 1600) |
| 49 | #pragma optimize("", on) |
| 50 | #endif |
| 51 | |
| 52 | static void pts_to_unit_matrix(const SkPoint pts[2], SkMatrix* matrix) { |
| 53 | SkVector vec = pts[1] - pts[0]; |
| 54 | SkScalar mag = vec.length(); |
| 55 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 56 | |
| 57 | vec.scale(inv); |
| 58 | matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 59 | matrix->postTranslate(-pts[0].fX, -pts[0].fY); |
| 60 | matrix->postScale(inv, inv); |
| 61 | } |
| 62 | |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | SkLinearGradient::SkLinearGradient(const SkPoint pts[2], |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 66 | const SkColor colors[], |
| 67 | const SkScalar pos[], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 68 | int colorCount, |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 69 | SkShader::TileMode mode, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 70 | SkUnitMapper* mapper) |
| 71 | : SkGradientShaderBase(colors, pos, colorCount, mode, mapper) |
| 72 | , fStart(pts[0]) |
| 73 | , fEnd(pts[1]) { |
| 74 | pts_to_unit_matrix(pts, &fPtsToUnit); |
| 75 | } |
| 76 | |
| 77 | SkLinearGradient::SkLinearGradient(SkFlattenableReadBuffer& buffer) |
| 78 | : INHERITED(buffer) |
| 79 | , fStart(buffer.readPoint()) |
| 80 | , fEnd(buffer.readPoint()) { |
| 81 | } |
| 82 | |
| 83 | void SkLinearGradient::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 84 | this->INHERITED::flatten(buffer); |
| 85 | buffer.writePoint(fStart); |
| 86 | buffer.writePoint(fEnd); |
| 87 | } |
| 88 | |
| 89 | bool SkLinearGradient::setContext(const SkBitmap& device, const SkPaint& paint, |
| 90 | const SkMatrix& matrix) { |
| 91 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; |
| 96 | if ((fDstToIndex.getType() & ~mask) == 0) { |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame^] | 97 | #ifdef SK_IGNORE_GRADIENT_DITHER_FIX |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 98 | fFlags |= SkShader::kConstInY32_Flag; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame^] | 99 | #else |
| 100 | // when we dither, we are (usually) not const-in-Y |
| 101 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 102 | if ((fFlags & SkShader::kHasSpan16_Flag) && !paint.isDither()) { |
| 103 | // only claim this if we do have a 16bit mode (i.e. none of our |
| 104 | // colors have alpha), and if we are not dithering (which obviously |
| 105 | // is not const in Y). |
| 106 | fFlags |= SkShader::kConstInY16_Flag; |
| 107 | } |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | #define NO_CHECK_ITER \ |
| 113 | do { \ |
| 114 | unsigned fi = fx >> SkGradientShaderBase::kCache32Shift; \ |
| 115 | SkASSERT(fi <= 0xFF); \ |
| 116 | fx += dx; \ |
| 117 | *dstC++ = cache[toggle + fi]; \ |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 118 | toggle = next_dither_toggle(toggle); \ |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 119 | } while (0) |
| 120 | |
| 121 | namespace { |
| 122 | |
| 123 | typedef void (*LinearShadeProc)(TileProc proc, SkFixed dx, SkFixed fx, |
| 124 | SkPMColor* dstC, const SkPMColor* cache, |
| 125 | int toggle, int count); |
| 126 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 127 | // Linear interpolation (lerp) is unnecessary if there are no sharp |
| 128 | // discontinuities in the gradient - which must be true if there are |
| 129 | // only 2 colors - but it's cheap. |
| 130 | void shadeSpan_linear_vertical_lerp(TileProc proc, SkFixed dx, SkFixed fx, |
| 131 | SkPMColor* SK_RESTRICT dstC, |
| 132 | const SkPMColor* SK_RESTRICT cache, |
| 133 | int toggle, int count) { |
| 134 | // We're a vertical gradient, so no change in a span. |
| 135 | // If colors change sharply across the gradient, dithering is |
| 136 | // insufficient (it subsamples the color space) and we need to lerp. |
| 137 | unsigned fullIndex = proc(fx); |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 138 | unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift; |
| 139 | unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1); |
skia.committer@gmail.com | 9dde018 | 2013-02-04 12:57:42 +0000 | [diff] [blame] | 140 | |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 141 | int index0 = fi + toggle; |
| 142 | int index1 = index0; |
| 143 | if (fi < SkGradientShaderBase::kCache32Count - 1) { |
| 144 | index1 += 1; |
| 145 | } |
| 146 | SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder); |
| 147 | index0 ^= SkGradientShaderBase::kDitherStride32; |
| 148 | index1 ^= SkGradientShaderBase::kDitherStride32; |
| 149 | SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 150 | sk_memset32_dither(dstC, lerp, dlerp, count); |
| 151 | } |
| 152 | |
| 153 | void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, |
| 154 | SkPMColor* SK_RESTRICT dstC, |
| 155 | const SkPMColor* SK_RESTRICT cache, |
| 156 | int toggle, int count) { |
| 157 | SkClampRange range; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 158 | range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 159 | |
| 160 | if ((count = range.fCount0) > 0) { |
| 161 | sk_memset32_dither(dstC, |
| 162 | cache[toggle + range.fV0], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 163 | cache[next_dither_toggle(toggle) + range.fV0], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 164 | count); |
| 165 | dstC += count; |
| 166 | } |
| 167 | if ((count = range.fCount1) > 0) { |
| 168 | int unroll = count >> 3; |
| 169 | fx = range.fFx1; |
| 170 | for (int i = 0; i < unroll; i++) { |
| 171 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 172 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 173 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 174 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 175 | } |
| 176 | if ((count &= 7) > 0) { |
| 177 | do { |
| 178 | NO_CHECK_ITER; |
| 179 | } while (--count != 0); |
| 180 | } |
| 181 | } |
| 182 | if ((count = range.fCount2) > 0) { |
| 183 | sk_memset32_dither(dstC, |
| 184 | cache[toggle + range.fV1], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 185 | cache[next_dither_toggle(toggle) + range.fV1], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 186 | count); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx, |
| 191 | SkPMColor* SK_RESTRICT dstC, |
| 192 | const SkPMColor* SK_RESTRICT cache, |
| 193 | int toggle, int count) { |
| 194 | do { |
| 195 | unsigned fi = mirror_8bits(fx >> 8); |
| 196 | SkASSERT(fi <= 0xFF); |
| 197 | fx += dx; |
| 198 | *dstC++ = cache[toggle + fi]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 199 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 200 | } while (--count != 0); |
| 201 | } |
| 202 | |
| 203 | void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx, |
| 204 | SkPMColor* SK_RESTRICT dstC, |
| 205 | const SkPMColor* SK_RESTRICT cache, |
| 206 | int toggle, int count) { |
| 207 | do { |
| 208 | unsigned fi = repeat_8bits(fx >> 8); |
| 209 | SkASSERT(fi <= 0xFF); |
| 210 | fx += dx; |
| 211 | *dstC++ = cache[toggle + fi]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 212 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 213 | } while (--count != 0); |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | void SkLinearGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, |
| 219 | int count) { |
| 220 | SkASSERT(count > 0); |
| 221 | |
| 222 | SkPoint srcPt; |
| 223 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
| 224 | TileProc proc = fTileProc; |
| 225 | const SkPMColor* SK_RESTRICT cache = this->getCache32(); |
| 226 | #ifdef USE_DITHER_32BIT_GRADIENT |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 227 | int toggle = init_dither_toggle(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 228 | #else |
| 229 | int toggle = 0; |
| 230 | #endif |
| 231 | |
| 232 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 233 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 234 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 235 | SkFixed dx, fx = SkScalarToFixed(srcPt.fX); |
| 236 | |
| 237 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 238 | SkFixed dxStorage[1]; |
| 239 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL); |
| 240 | dx = dxStorage[0]; |
| 241 | } else { |
| 242 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 243 | dx = SkScalarToFixed(fDstToIndex.getScaleX()); |
| 244 | } |
| 245 | |
| 246 | LinearShadeProc shadeProc = shadeSpan_linear_repeat; |
| 247 | if (SkFixedNearlyZero(dx)) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 248 | shadeProc = shadeSpan_linear_vertical_lerp; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 249 | } else if (SkShader::kClamp_TileMode == fTileMode) { |
| 250 | shadeProc = shadeSpan_linear_clamp; |
| 251 | } else if (SkShader::kMirror_TileMode == fTileMode) { |
| 252 | shadeProc = shadeSpan_linear_mirror; |
| 253 | } else { |
| 254 | SkASSERT(SkShader::kRepeat_TileMode == fTileMode); |
| 255 | } |
| 256 | (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count); |
| 257 | } else { |
| 258 | SkScalar dstX = SkIntToScalar(x); |
| 259 | SkScalar dstY = SkIntToScalar(y); |
| 260 | do { |
| 261 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 262 | unsigned fi = proc(SkScalarToFixed(srcPt.fX)); |
| 263 | SkASSERT(fi <= 0xFFFF); |
| 264 | *dstC++ = cache[toggle + (fi >> kCache32Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 265 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 266 | dstX += SK_Scalar1; |
| 267 | } while (--count != 0); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | SkShader::BitmapType SkLinearGradient::asABitmap(SkBitmap* bitmap, |
| 272 | SkMatrix* matrix, |
| 273 | TileMode xy[]) const { |
| 274 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 275 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 276 | } |
| 277 | if (matrix) { |
| 278 | matrix->preConcat(fPtsToUnit); |
| 279 | } |
| 280 | if (xy) { |
| 281 | xy[0] = fTileMode; |
| 282 | xy[1] = kClamp_TileMode; |
| 283 | } |
| 284 | return kLinear_BitmapType; |
| 285 | } |
| 286 | |
| 287 | SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const { |
| 288 | if (info) { |
| 289 | commonAsAGradient(info); |
| 290 | info->fPoint[0] = fStart; |
| 291 | info->fPoint[1] = fEnd; |
| 292 | } |
| 293 | return kLinear_GradientType; |
| 294 | } |
| 295 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 296 | static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other, |
| 297 | int count) { |
| 298 | if (reinterpret_cast<uintptr_t>(dst) & 2) { |
| 299 | *dst++ = value; |
| 300 | count -= 1; |
| 301 | SkTSwap(value, other); |
| 302 | } |
| 303 | |
| 304 | sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1); |
| 305 | |
| 306 | if (count & 1) { |
| 307 | dst[count - 1] = value; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | #define NO_CHECK_ITER_16 \ |
| 312 | do { \ |
| 313 | unsigned fi = fx >> SkGradientShaderBase::kCache16Shift; \ |
| 314 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); \ |
| 315 | fx += dx; \ |
| 316 | *dstC++ = cache[toggle + fi]; \ |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 317 | toggle = next_dither_toggle16(toggle); \ |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 318 | } while (0) |
| 319 | |
| 320 | namespace { |
| 321 | |
| 322 | typedef void (*LinearShade16Proc)(TileProc proc, SkFixed dx, SkFixed fx, |
| 323 | uint16_t* dstC, const uint16_t* cache, |
| 324 | int toggle, int count); |
| 325 | |
| 326 | void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx, |
| 327 | uint16_t* SK_RESTRICT dstC, |
| 328 | const uint16_t* SK_RESTRICT cache, |
| 329 | int toggle, int count) { |
| 330 | // we're a vertical gradient, so no change in a span |
| 331 | unsigned fi = proc(fx) >> SkGradientShaderBase::kCache16Shift; |
| 332 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); |
| 333 | dither_memset16(dstC, cache[toggle + fi], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 334 | cache[next_dither_toggle16(toggle) + fi], count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, |
| 338 | uint16_t* SK_RESTRICT dstC, |
| 339 | const uint16_t* SK_RESTRICT cache, |
| 340 | int toggle, int count) { |
| 341 | SkClampRange range; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 342 | range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 343 | |
| 344 | if ((count = range.fCount0) > 0) { |
| 345 | dither_memset16(dstC, |
| 346 | cache[toggle + range.fV0], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 347 | cache[next_dither_toggle16(toggle) + range.fV0], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 348 | count); |
| 349 | dstC += count; |
| 350 | } |
| 351 | if ((count = range.fCount1) > 0) { |
| 352 | int unroll = count >> 3; |
| 353 | fx = range.fFx1; |
| 354 | for (int i = 0; i < unroll; i++) { |
| 355 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 356 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 357 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 358 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 359 | } |
| 360 | if ((count &= 7) > 0) { |
| 361 | do { |
| 362 | NO_CHECK_ITER_16; |
| 363 | } while (--count != 0); |
| 364 | } |
| 365 | } |
| 366 | if ((count = range.fCount2) > 0) { |
| 367 | dither_memset16(dstC, |
| 368 | cache[toggle + range.fV1], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 369 | cache[next_dither_toggle16(toggle) + range.fV1], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 370 | count); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx, |
| 375 | uint16_t* SK_RESTRICT dstC, |
| 376 | const uint16_t* SK_RESTRICT cache, |
| 377 | int toggle, int count) { |
| 378 | do { |
| 379 | unsigned fi = mirror_bits(fx >> SkGradientShaderBase::kCache16Shift, |
| 380 | SkGradientShaderBase::kCache16Bits); |
| 381 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); |
| 382 | fx += dx; |
| 383 | *dstC++ = cache[toggle + fi]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 384 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 385 | } while (--count != 0); |
| 386 | } |
| 387 | |
| 388 | void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx, |
| 389 | uint16_t* SK_RESTRICT dstC, |
| 390 | const uint16_t* SK_RESTRICT cache, |
| 391 | int toggle, int count) { |
| 392 | do { |
| 393 | unsigned fi = repeat_bits(fx >> SkGradientShaderBase::kCache16Shift, |
| 394 | SkGradientShaderBase::kCache16Bits); |
| 395 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); |
| 396 | fx += dx; |
| 397 | *dstC++ = cache[toggle + fi]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 398 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 399 | } while (--count != 0); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void SkLinearGradient::shadeSpan16(int x, int y, |
| 404 | uint16_t* SK_RESTRICT dstC, int count) { |
| 405 | SkASSERT(count > 0); |
| 406 | |
| 407 | SkPoint srcPt; |
| 408 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
| 409 | TileProc proc = fTileProc; |
| 410 | const uint16_t* SK_RESTRICT cache = this->getCache16(); |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 411 | int toggle = init_dither_toggle16(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 412 | |
| 413 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 414 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 415 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 416 | SkFixed dx, fx = SkScalarToFixed(srcPt.fX); |
| 417 | |
| 418 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 419 | SkFixed dxStorage[1]; |
| 420 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL); |
| 421 | dx = dxStorage[0]; |
| 422 | } else { |
| 423 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 424 | dx = SkScalarToFixed(fDstToIndex.getScaleX()); |
| 425 | } |
| 426 | |
| 427 | LinearShade16Proc shadeProc = shadeSpan16_linear_repeat; |
| 428 | if (SkFixedNearlyZero(dx)) { |
| 429 | shadeProc = shadeSpan16_linear_vertical; |
| 430 | } else if (SkShader::kClamp_TileMode == fTileMode) { |
| 431 | shadeProc = shadeSpan16_linear_clamp; |
| 432 | } else if (SkShader::kMirror_TileMode == fTileMode) { |
| 433 | shadeProc = shadeSpan16_linear_mirror; |
| 434 | } else { |
| 435 | SkASSERT(SkShader::kRepeat_TileMode == fTileMode); |
| 436 | } |
| 437 | (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count); |
| 438 | } else { |
| 439 | SkScalar dstX = SkIntToScalar(x); |
| 440 | SkScalar dstY = SkIntToScalar(y); |
| 441 | do { |
| 442 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 443 | unsigned fi = proc(SkScalarToFixed(srcPt.fX)); |
| 444 | SkASSERT(fi <= 0xFFFF); |
| 445 | |
| 446 | int index = fi >> kCache16Shift; |
| 447 | *dstC++ = cache[toggle + index]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 448 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 449 | |
| 450 | dstX += SK_Scalar1; |
| 451 | } while (--count != 0); |
| 452 | } |
| 453 | } |
| 454 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 455 | #if SK_SUPPORT_GPU |
| 456 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 457 | #include "GrTBackendEffectFactory.h" |
| 458 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 459 | ///////////////////////////////////////////////////////////////////// |
| 460 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 461 | class GrGLLinearGradient : public GrGLGradientEffect { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 462 | public: |
| 463 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 464 | GrGLLinearGradient(const GrBackendEffectFactory& factory, const GrEffectRef&) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 465 | : INHERITED (factory) { } |
| 466 | |
| 467 | virtual ~GrGLLinearGradient() { } |
| 468 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 469 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 470 | const GrEffectStage&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 471 | EffectKey, |
| 472 | const char* vertexCoords, |
| 473 | const char* outputColor, |
| 474 | const char* inputColor, |
| 475 | const TextureSamplerArray&) SK_OVERRIDE; |
| 476 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 477 | static EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
| 478 | return GenMatrixKey(stage); |
| 479 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 480 | |
| 481 | private: |
| 482 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 483 | typedef GrGLGradientEffect INHERITED; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 486 | ///////////////////////////////////////////////////////////////////// |
| 487 | |
| 488 | class GrLinearGradient : public GrGradientEffect { |
| 489 | public: |
| 490 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 491 | static GrEffectRef* Create(GrContext* ctx, |
| 492 | const SkLinearGradient& shader, |
| 493 | const SkMatrix& matrix, |
| 494 | SkShader::TileMode tm) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 495 | AutoEffectUnref effect(SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 496 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 497 | } |
| 498 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 499 | virtual ~GrLinearGradient() { } |
| 500 | |
| 501 | static const char* Name() { return "Linear Gradient"; } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 502 | const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 503 | return GrTBackendEffectFactory<GrLinearGradient>::getInstance(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 504 | } |
| 505 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 506 | typedef GrGLLinearGradient GLEffect; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 507 | |
| 508 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 509 | GrLinearGradient(GrContext* ctx, |
| 510 | const SkLinearGradient& shader, |
| 511 | const SkMatrix& matrix, |
| 512 | SkShader::TileMode tm) |
| 513 | : INHERITED(ctx, shader, matrix, tm) { } |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 514 | GR_DECLARE_EFFECT_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 515 | |
| 516 | typedef GrGradientEffect INHERITED; |
| 517 | }; |
| 518 | |
| 519 | ///////////////////////////////////////////////////////////////////// |
| 520 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 521 | GR_DEFINE_EFFECT_TEST(GrLinearGradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 522 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 523 | GrEffectRef* GrLinearGradient::TestCreate(SkRandom* random, |
| 524 | GrContext* context, |
| 525 | GrTexture**) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 526 | SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()}, |
| 527 | {random->nextUScalar1(), random->nextUScalar1()}}; |
| 528 | |
| 529 | SkColor colors[kMaxRandomGradientColors]; |
| 530 | SkScalar stopsArray[kMaxRandomGradientColors]; |
| 531 | SkScalar* stops = stopsArray; |
| 532 | SkShader::TileMode tm; |
| 533 | int colorCount = RandomGradientParams(random, colors, &stops, &tm); |
| 534 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, |
| 535 | colors, stops, colorCount, |
| 536 | tm)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 537 | SkPaint paint; |
| 538 | return shader->asNewEffect(context, paint); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | ///////////////////////////////////////////////////////////////////// |
| 542 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 543 | void GrGLLinearGradient::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 544 | const GrEffectStage& stage, |
| 545 | EffectKey key, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 546 | const char* vertexCoords, |
| 547 | const char* outputColor, |
| 548 | const char* inputColor, |
| 549 | const TextureSamplerArray& samplers) { |
| 550 | this->emitYCoordUniform(builder); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 551 | const char* coords; |
| 552 | this->setupMatrix(builder, key, vertexCoords, &coords); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 553 | SkString t; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 554 | t.append(coords); |
| 555 | t.append(".x"); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 556 | this->emitColorLookup(builder, t.c_str(), outputColor, inputColor, samplers[0]); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | ///////////////////////////////////////////////////////////////////// |
| 560 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 561 | GrEffectRef* SkLinearGradient::asNewEffect(GrContext* context, const SkPaint&) const { |
bsalomon@google.com | 00835cc | 2013-01-14 17:07:22 +0000 | [diff] [blame] | 562 | SkASSERT(NULL != context); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 563 | SkMatrix matrix; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 564 | if (!this->getLocalMatrix().invert(&matrix)) { |
humper@google.com | 84831ac | 2013-01-14 22:09:54 +0000 | [diff] [blame] | 565 | return NULL; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 566 | } |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 567 | matrix.postConcat(fPtsToUnit); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 568 | return GrLinearGradient::Create(context, *this, matrix, fTileMode); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 569 | } |
| 570 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 571 | #else |
| 572 | |
bsalomon@google.com | 5d2cd20 | 2013-01-16 15:31:06 +0000 | [diff] [blame] | 573 | GrEffectRef* SkLinearGradient::asNewEffect(GrContext*, const SkPaint&) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 574 | SkDEBUGFAIL("Should not call in GPU-less build"); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 575 | return NULL; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 579 | |
| 580 | #ifdef SK_DEVELOPER |
| 581 | void SkLinearGradient::toString(SkString* str) const { |
| 582 | str->append("SkLinearGradient ("); |
| 583 | |
| 584 | str->appendf("start: (%f, %f)", fStart.fX, fStart.fY); |
| 585 | str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY); |
| 586 | |
| 587 | this->INHERITED::toString(str); |
| 588 | |
| 589 | str->append(")"); |
| 590 | } |
| 591 | #endif |