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