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) { |
| 97 | fFlags |= SkShader::kConstInY32_Flag; |
| 98 | if ((fFlags & SkShader::kHasSpan16_Flag) && !paint.isDither()) { |
| 99 | // only claim this if we do have a 16bit mode (i.e. none of our |
| 100 | // colors have alpha), and if we are not dithering (which obviously |
| 101 | // is not const in Y). |
| 102 | fFlags |= SkShader::kConstInY16_Flag; |
| 103 | } |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | #define NO_CHECK_ITER \ |
| 109 | do { \ |
| 110 | unsigned fi = fx >> SkGradientShaderBase::kCache32Shift; \ |
| 111 | SkASSERT(fi <= 0xFF); \ |
| 112 | fx += dx; \ |
| 113 | *dstC++ = cache[toggle + fi]; \ |
| 114 | toggle ^= SkGradientShaderBase::kDitherStride32; \ |
| 115 | } while (0) |
| 116 | |
| 117 | namespace { |
| 118 | |
| 119 | typedef void (*LinearShadeProc)(TileProc proc, SkFixed dx, SkFixed fx, |
| 120 | SkPMColor* dstC, const SkPMColor* cache, |
| 121 | int toggle, int count); |
| 122 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 123 | // This function is deprecated, and will be replaced by |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 124 | // shadeSpan_linear_vertical_lerp() once Chrome has been weaned off of it. |
| 125 | void shadeSpan_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx, |
| 126 | SkPMColor* SK_RESTRICT dstC, |
| 127 | const SkPMColor* SK_RESTRICT cache, |
| 128 | int toggle, int count) { |
| 129 | // We're a vertical gradient, so no change in a span. |
| 130 | // If colors change sharply across the gradient, dithering is |
| 131 | // insufficient (it subsamples the color space) and we need to lerp. |
| 132 | unsigned fullIndex = proc(fx); |
| 133 | unsigned fi = fullIndex >> (16 - SkGradientShaderBase::kCache32Bits); |
| 134 | sk_memset32_dither(dstC, |
| 135 | cache[toggle + fi], |
| 136 | cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + fi], |
| 137 | count); |
| 138 | } |
| 139 | |
| 140 | // Linear interpolation (lerp) is unnecessary if there are no sharp |
| 141 | // discontinuities in the gradient - which must be true if there are |
| 142 | // only 2 colors - but it's cheap. |
| 143 | void shadeSpan_linear_vertical_lerp(TileProc proc, SkFixed dx, SkFixed fx, |
| 144 | SkPMColor* SK_RESTRICT dstC, |
| 145 | const SkPMColor* SK_RESTRICT cache, |
| 146 | int toggle, int count) { |
| 147 | // We're a vertical gradient, so no change in a span. |
| 148 | // If colors change sharply across the gradient, dithering is |
| 149 | // insufficient (it subsamples the color space) and we need to lerp. |
| 150 | unsigned fullIndex = proc(fx); |
| 151 | unsigned fi = fullIndex >> (16 - SkGradientShaderBase::kCache32Bits); |
| 152 | unsigned remainder = fullIndex & SkGradientShaderBase::kLerpRemainderMask32; |
| 153 | SkPMColor lerp = |
| 154 | SkFastFourByteInterp( |
| 155 | cache[toggle + fi + 1], |
| 156 | cache[toggle + fi], remainder); |
| 157 | SkPMColor dlerp = |
| 158 | SkFastFourByteInterp( |
| 159 | cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + fi + 1], |
| 160 | cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + fi], remainder); |
| 161 | sk_memset32_dither(dstC, lerp, dlerp, count); |
| 162 | } |
| 163 | |
| 164 | void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, |
| 165 | SkPMColor* SK_RESTRICT dstC, |
| 166 | const SkPMColor* SK_RESTRICT cache, |
| 167 | int toggle, int count) { |
| 168 | SkClampRange range; |
| 169 | range.init(fx, dx, count, 0, SkGradientShaderBase::kGradient32Length); |
| 170 | |
| 171 | if ((count = range.fCount0) > 0) { |
| 172 | sk_memset32_dither(dstC, |
| 173 | cache[toggle + range.fV0], |
| 174 | cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + range.fV0], |
| 175 | count); |
| 176 | dstC += count; |
| 177 | } |
| 178 | if ((count = range.fCount1) > 0) { |
| 179 | int unroll = count >> 3; |
| 180 | fx = range.fFx1; |
| 181 | for (int i = 0; i < unroll; i++) { |
| 182 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 183 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 184 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 185 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 186 | } |
| 187 | if ((count &= 7) > 0) { |
| 188 | do { |
| 189 | NO_CHECK_ITER; |
| 190 | } while (--count != 0); |
| 191 | } |
| 192 | } |
| 193 | if ((count = range.fCount2) > 0) { |
| 194 | sk_memset32_dither(dstC, |
| 195 | cache[toggle + range.fV1], |
| 196 | cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + range.fV1], |
| 197 | count); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx, |
| 202 | SkPMColor* SK_RESTRICT dstC, |
| 203 | const SkPMColor* SK_RESTRICT cache, |
| 204 | int toggle, int count) { |
| 205 | do { |
| 206 | unsigned fi = mirror_8bits(fx >> 8); |
| 207 | SkASSERT(fi <= 0xFF); |
| 208 | fx += dx; |
| 209 | *dstC++ = cache[toggle + fi]; |
| 210 | toggle ^= SkGradientShaderBase::kDitherStride32; |
| 211 | } while (--count != 0); |
| 212 | } |
| 213 | |
| 214 | void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx, |
| 215 | SkPMColor* SK_RESTRICT dstC, |
| 216 | const SkPMColor* SK_RESTRICT cache, |
| 217 | int toggle, int count) { |
| 218 | do { |
| 219 | unsigned fi = repeat_8bits(fx >> 8); |
| 220 | SkASSERT(fi <= 0xFF); |
| 221 | fx += dx; |
| 222 | *dstC++ = cache[toggle + fi]; |
| 223 | toggle ^= SkGradientShaderBase::kDitherStride32; |
| 224 | } while (--count != 0); |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | |
| 229 | void SkLinearGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, |
| 230 | int count) { |
| 231 | SkASSERT(count > 0); |
| 232 | |
| 233 | SkPoint srcPt; |
| 234 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
| 235 | TileProc proc = fTileProc; |
| 236 | const SkPMColor* SK_RESTRICT cache = this->getCache32(); |
| 237 | #ifdef USE_DITHER_32BIT_GRADIENT |
| 238 | int toggle = ((x ^ y) & 1) * kDitherStride32; |
| 239 | #else |
| 240 | int toggle = 0; |
| 241 | #endif |
| 242 | |
| 243 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 244 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 245 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 246 | SkFixed dx, fx = SkScalarToFixed(srcPt.fX); |
| 247 | |
| 248 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 249 | SkFixed dxStorage[1]; |
| 250 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL); |
| 251 | dx = dxStorage[0]; |
| 252 | } else { |
| 253 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 254 | dx = SkScalarToFixed(fDstToIndex.getScaleX()); |
| 255 | } |
| 256 | |
| 257 | LinearShadeProc shadeProc = shadeSpan_linear_repeat; |
| 258 | if (SkFixedNearlyZero(dx)) { |
| 259 | #ifdef SK_SIMPLE_TWOCOLOR_VERTICAL_GRADIENTS |
| 260 | if (fColorCount > 2) { |
| 261 | shadeProc = shadeSpan_linear_vertical_lerp; |
| 262 | } else { |
| 263 | shadeProc = shadeSpan_linear_vertical; |
| 264 | } |
| 265 | #else |
| 266 | shadeProc = shadeSpan_linear_vertical_lerp; |
| 267 | #endif |
| 268 | } else if (SkShader::kClamp_TileMode == fTileMode) { |
| 269 | shadeProc = shadeSpan_linear_clamp; |
| 270 | } else if (SkShader::kMirror_TileMode == fTileMode) { |
| 271 | shadeProc = shadeSpan_linear_mirror; |
| 272 | } else { |
| 273 | SkASSERT(SkShader::kRepeat_TileMode == fTileMode); |
| 274 | } |
| 275 | (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count); |
| 276 | } else { |
| 277 | SkScalar dstX = SkIntToScalar(x); |
| 278 | SkScalar dstY = SkIntToScalar(y); |
| 279 | do { |
| 280 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 281 | unsigned fi = proc(SkScalarToFixed(srcPt.fX)); |
| 282 | SkASSERT(fi <= 0xFFFF); |
| 283 | *dstC++ = cache[toggle + (fi >> kCache32Shift)]; |
| 284 | toggle ^= SkGradientShaderBase::kDitherStride32; |
| 285 | dstX += SK_Scalar1; |
| 286 | } while (--count != 0); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | SkShader::BitmapType SkLinearGradient::asABitmap(SkBitmap* bitmap, |
| 291 | SkMatrix* matrix, |
| 292 | TileMode xy[]) const { |
| 293 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 294 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 295 | } |
| 296 | if (matrix) { |
| 297 | matrix->preConcat(fPtsToUnit); |
| 298 | } |
| 299 | if (xy) { |
| 300 | xy[0] = fTileMode; |
| 301 | xy[1] = kClamp_TileMode; |
| 302 | } |
| 303 | return kLinear_BitmapType; |
| 304 | } |
| 305 | |
| 306 | SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const { |
| 307 | if (info) { |
| 308 | commonAsAGradient(info); |
| 309 | info->fPoint[0] = fStart; |
| 310 | info->fPoint[1] = fEnd; |
| 311 | } |
| 312 | return kLinear_GradientType; |
| 313 | } |
| 314 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 315 | static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other, |
| 316 | int count) { |
| 317 | if (reinterpret_cast<uintptr_t>(dst) & 2) { |
| 318 | *dst++ = value; |
| 319 | count -= 1; |
| 320 | SkTSwap(value, other); |
| 321 | } |
| 322 | |
| 323 | sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1); |
| 324 | |
| 325 | if (count & 1) { |
| 326 | dst[count - 1] = value; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | #define NO_CHECK_ITER_16 \ |
| 331 | do { \ |
| 332 | unsigned fi = fx >> SkGradientShaderBase::kCache16Shift; \ |
| 333 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); \ |
| 334 | fx += dx; \ |
| 335 | *dstC++ = cache[toggle + fi]; \ |
| 336 | toggle ^= SkGradientShaderBase::kDitherStride16; \ |
| 337 | } while (0) |
| 338 | |
| 339 | namespace { |
| 340 | |
| 341 | typedef void (*LinearShade16Proc)(TileProc proc, SkFixed dx, SkFixed fx, |
| 342 | uint16_t* dstC, const uint16_t* cache, |
| 343 | int toggle, int count); |
| 344 | |
| 345 | void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx, |
| 346 | uint16_t* SK_RESTRICT dstC, |
| 347 | const uint16_t* SK_RESTRICT cache, |
| 348 | int toggle, int count) { |
| 349 | // we're a vertical gradient, so no change in a span |
| 350 | unsigned fi = proc(fx) >> SkGradientShaderBase::kCache16Shift; |
| 351 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); |
| 352 | dither_memset16(dstC, cache[toggle + fi], |
| 353 | cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + fi], count); |
| 354 | |
| 355 | } |
| 356 | |
| 357 | void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, |
| 358 | uint16_t* SK_RESTRICT dstC, |
| 359 | const uint16_t* SK_RESTRICT cache, |
| 360 | int toggle, int count) { |
| 361 | SkClampRange range; |
| 362 | range.init(fx, dx, count, 0, SkGradientShaderBase::kGradient16Length); |
| 363 | |
| 364 | if ((count = range.fCount0) > 0) { |
| 365 | dither_memset16(dstC, |
| 366 | cache[toggle + range.fV0], |
| 367 | cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + range.fV0], |
| 368 | count); |
| 369 | dstC += count; |
| 370 | } |
| 371 | if ((count = range.fCount1) > 0) { |
| 372 | int unroll = count >> 3; |
| 373 | fx = range.fFx1; |
| 374 | for (int i = 0; i < unroll; i++) { |
| 375 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 376 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 377 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 378 | NO_CHECK_ITER_16; NO_CHECK_ITER_16; |
| 379 | } |
| 380 | if ((count &= 7) > 0) { |
| 381 | do { |
| 382 | NO_CHECK_ITER_16; |
| 383 | } while (--count != 0); |
| 384 | } |
| 385 | } |
| 386 | if ((count = range.fCount2) > 0) { |
| 387 | dither_memset16(dstC, |
| 388 | cache[toggle + range.fV1], |
| 389 | cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + range.fV1], |
| 390 | count); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx, |
| 395 | uint16_t* SK_RESTRICT dstC, |
| 396 | const uint16_t* SK_RESTRICT cache, |
| 397 | int toggle, int count) { |
| 398 | do { |
| 399 | unsigned fi = mirror_bits(fx >> SkGradientShaderBase::kCache16Shift, |
| 400 | SkGradientShaderBase::kCache16Bits); |
| 401 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); |
| 402 | fx += dx; |
| 403 | *dstC++ = cache[toggle + fi]; |
| 404 | toggle ^= SkGradientShaderBase::kDitherStride16; |
| 405 | } while (--count != 0); |
| 406 | } |
| 407 | |
| 408 | void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx, |
| 409 | uint16_t* SK_RESTRICT dstC, |
| 410 | const uint16_t* SK_RESTRICT cache, |
| 411 | int toggle, int count) { |
| 412 | do { |
| 413 | unsigned fi = repeat_bits(fx >> SkGradientShaderBase::kCache16Shift, |
| 414 | SkGradientShaderBase::kCache16Bits); |
| 415 | SkASSERT(fi < SkGradientShaderBase::kCache16Count); |
| 416 | fx += dx; |
| 417 | *dstC++ = cache[toggle + fi]; |
| 418 | toggle ^= SkGradientShaderBase::kDitherStride16; |
| 419 | } while (--count != 0); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void SkLinearGradient::shadeSpan16(int x, int y, |
| 424 | uint16_t* SK_RESTRICT dstC, int count) { |
| 425 | SkASSERT(count > 0); |
| 426 | |
| 427 | SkPoint srcPt; |
| 428 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
| 429 | TileProc proc = fTileProc; |
| 430 | const uint16_t* SK_RESTRICT cache = this->getCache16(); |
| 431 | int toggle = ((x ^ y) & 1) * kDitherStride16; |
| 432 | |
| 433 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 434 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 435 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 436 | SkFixed dx, fx = SkScalarToFixed(srcPt.fX); |
| 437 | |
| 438 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 439 | SkFixed dxStorage[1]; |
| 440 | (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL); |
| 441 | dx = dxStorage[0]; |
| 442 | } else { |
| 443 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 444 | dx = SkScalarToFixed(fDstToIndex.getScaleX()); |
| 445 | } |
| 446 | |
| 447 | LinearShade16Proc shadeProc = shadeSpan16_linear_repeat; |
| 448 | if (SkFixedNearlyZero(dx)) { |
| 449 | shadeProc = shadeSpan16_linear_vertical; |
| 450 | } else if (SkShader::kClamp_TileMode == fTileMode) { |
| 451 | shadeProc = shadeSpan16_linear_clamp; |
| 452 | } else if (SkShader::kMirror_TileMode == fTileMode) { |
| 453 | shadeProc = shadeSpan16_linear_mirror; |
| 454 | } else { |
| 455 | SkASSERT(SkShader::kRepeat_TileMode == fTileMode); |
| 456 | } |
| 457 | (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count); |
| 458 | } else { |
| 459 | SkScalar dstX = SkIntToScalar(x); |
| 460 | SkScalar dstY = SkIntToScalar(y); |
| 461 | do { |
| 462 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 463 | unsigned fi = proc(SkScalarToFixed(srcPt.fX)); |
| 464 | SkASSERT(fi <= 0xFFFF); |
| 465 | |
| 466 | int index = fi >> kCache16Shift; |
| 467 | *dstC++ = cache[toggle + index]; |
| 468 | toggle ^= SkGradientShaderBase::kDitherStride16; |
| 469 | |
| 470 | dstX += SK_Scalar1; |
| 471 | } while (--count != 0); |
| 472 | } |
| 473 | } |
| 474 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 475 | #if SK_SUPPORT_GPU |
| 476 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 477 | ///////////////////////////////////////////////////////////////////// |
| 478 | |
| 479 | class GrGLLinearGradient : public GrGLGradientStage { |
| 480 | public: |
| 481 | |
| 482 | GrGLLinearGradient(const GrProgramStageFactory& factory, |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 483 | const GrEffect&) |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 484 | : INHERITED (factory) { } |
| 485 | |
| 486 | virtual ~GrGLLinearGradient() { } |
| 487 | |
| 488 | virtual void emitVS(GrGLShaderBuilder* builder, |
| 489 | const char* vertexCoords) SK_OVERRIDE { } |
| 490 | virtual void emitFS(GrGLShaderBuilder* builder, |
| 491 | const char* outputColor, |
| 492 | const char* inputColor, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 493 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 494 | static StageKey GenKey(const GrEffect& s, const GrGLCaps& caps) { return 0; } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 495 | |
| 496 | private: |
| 497 | |
| 498 | typedef GrGLGradientStage INHERITED; |
| 499 | }; |
| 500 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 501 | ///////////////////////////////////////////////////////////////////// |
| 502 | |
| 503 | class GrLinearGradient : public GrGradientEffect { |
| 504 | public: |
| 505 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 506 | GrLinearGradient(GrContext* ctx, const SkLinearGradient& shader, SkShader::TileMode tm) |
| 507 | : INHERITED(ctx, shader, tm) { } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 508 | virtual ~GrLinearGradient() { } |
| 509 | |
| 510 | static const char* Name() { return "Linear Gradient"; } |
| 511 | const GrProgramStageFactory& getFactory() const SK_OVERRIDE { |
| 512 | return GrTProgramStageFactory<GrLinearGradient>::getInstance(); |
| 513 | } |
| 514 | |
| 515 | typedef GrGLLinearGradient GLProgramStage; |
| 516 | |
| 517 | private: |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 518 | GR_DECLARE_EFFECT_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 519 | |
| 520 | typedef GrGradientEffect INHERITED; |
| 521 | }; |
| 522 | |
| 523 | ///////////////////////////////////////////////////////////////////// |
| 524 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 525 | GR_DEFINE_EFFECT_TEST(GrLinearGradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 526 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 527 | GrEffect* GrLinearGradient::TestCreate(SkRandom* random, |
| 528 | GrContext* context, |
| 529 | GrTexture**) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 530 | SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()}, |
| 531 | {random->nextUScalar1(), random->nextUScalar1()}}; |
| 532 | |
| 533 | SkColor colors[kMaxRandomGradientColors]; |
| 534 | SkScalar stopsArray[kMaxRandomGradientColors]; |
| 535 | SkScalar* stops = stopsArray; |
| 536 | SkShader::TileMode tm; |
| 537 | int colorCount = RandomGradientParams(random, colors, &stops, &tm); |
| 538 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, |
| 539 | colors, stops, colorCount, |
| 540 | tm)); |
| 541 | GrSamplerState sampler; |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame^] | 542 | shader->asNewEffect(context, &sampler); |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 543 | GrAssert(NULL != sampler.getEffect()); |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame^] | 544 | // const_cast and ref is a hack! Will remove when asNewEffect returns GrEffect* |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 545 | sampler.getEffect()->ref(); |
| 546 | return const_cast<GrEffect*>(sampler.getEffect()); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | ///////////////////////////////////////////////////////////////////// |
| 550 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 551 | void GrGLLinearGradient::emitFS(GrGLShaderBuilder* builder, |
| 552 | const char* outputColor, |
| 553 | const char* inputColor, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 554 | const TextureSamplerArray& samplers) { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 555 | SkString t; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 556 | t.printf("%s.x", builder->defaultTexCoordsName()); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 557 | this->emitColorLookup(builder, t.c_str(), outputColor, inputColor, samplers[0]); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | ///////////////////////////////////////////////////////////////////// |
| 561 | |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame^] | 562 | bool SkLinearGradient::asNewEffect(GrContext* context, GrSamplerState* sampler) const { |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 563 | SkASSERT(NULL != context && NULL != sampler); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 564 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 565 | SkAutoTUnref<GrEffect> stage(SkNEW_ARGS(GrLinearGradient, (context, *this, fTileMode))); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 566 | |
| 567 | SkMatrix matrix; |
| 568 | if (this->getLocalMatrix(&matrix)) { |
| 569 | if (!matrix.invert(&matrix)) { |
| 570 | return false; |
| 571 | } |
| 572 | matrix.postConcat(fPtsToUnit); |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 573 | sampler->setEffect(stage, matrix); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 574 | } else { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 575 | sampler->setEffect(stage, fPtsToUnit); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 576 | } |
skia.committer@gmail.com | 20c301b | 2012-10-17 02:01:13 +0000 | [diff] [blame] | 577 | |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 578 | return true; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 579 | } |
| 580 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 581 | #else |
| 582 | |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame^] | 583 | bool SkLinearGradient::asNewEffect(GrContext*, GrSamplerState*) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 584 | SkDEBUGFAIL("Should not call in GPU-less build"); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 585 | return false; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | #endif |