epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 10 | #include "SkScalar.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkShader.h" |
| 12 | #include "SkPaint.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 13 | #include "SkMallocPixelRef.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 15 | SK_DEFINE_INST_COUNT(SkShader) |
| 16 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | SkShader::SkShader() : fLocalMatrix(NULL) { |
| 18 | SkDEBUGCODE(fInSession = false;) |
| 19 | } |
| 20 | |
| 21 | SkShader::SkShader(SkFlattenableReadBuffer& buffer) |
| 22 | : INHERITED(buffer), fLocalMatrix(NULL) { |
| 23 | if (buffer.readBool()) { |
| 24 | SkMatrix matrix; |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 25 | buffer.readMatrix(&matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | setLocalMatrix(matrix); |
| 27 | } |
| 28 | SkDEBUGCODE(fInSession = false;) |
| 29 | } |
| 30 | |
| 31 | SkShader::~SkShader() { |
| 32 | SkASSERT(!fInSession); |
| 33 | sk_free(fLocalMatrix); |
| 34 | } |
| 35 | |
| 36 | void SkShader::beginSession() { |
| 37 | SkASSERT(!fInSession); |
| 38 | SkDEBUGCODE(fInSession = true;) |
| 39 | } |
| 40 | |
| 41 | void SkShader::endSession() { |
| 42 | SkASSERT(fInSession); |
| 43 | SkDEBUGCODE(fInSession = false;) |
| 44 | } |
| 45 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 46 | void SkShader::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | this->INHERITED::flatten(buffer); |
| 48 | buffer.writeBool(fLocalMatrix != NULL); |
| 49 | if (fLocalMatrix) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 50 | buffer.writeMatrix(*fLocalMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | bool SkShader::getLocalMatrix(SkMatrix* localM) const { |
| 55 | if (fLocalMatrix) { |
| 56 | if (localM) { |
| 57 | *localM = *fLocalMatrix; |
| 58 | } |
| 59 | return true; |
| 60 | } else { |
| 61 | if (localM) { |
| 62 | localM->reset(); |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void SkShader::setLocalMatrix(const SkMatrix& localM) { |
| 69 | if (localM.isIdentity()) { |
| 70 | this->resetLocalMatrix(); |
| 71 | } else { |
| 72 | if (fLocalMatrix == NULL) { |
| 73 | fLocalMatrix = (SkMatrix*)sk_malloc_throw(sizeof(SkMatrix)); |
| 74 | } |
| 75 | *fLocalMatrix = localM; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void SkShader::resetLocalMatrix() { |
| 80 | if (fLocalMatrix) { |
| 81 | sk_free(fLocalMatrix); |
| 82 | fLocalMatrix = NULL; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool SkShader::setContext(const SkBitmap& device, |
| 87 | const SkPaint& paint, |
| 88 | const SkMatrix& matrix) { |
| 89 | const SkMatrix* m = &matrix; |
| 90 | SkMatrix total; |
| 91 | |
| 92 | fDeviceConfig = SkToU8(device.getConfig()); |
| 93 | fPaintAlpha = paint.getAlpha(); |
| 94 | if (fLocalMatrix) { |
| 95 | total.setConcat(matrix, *fLocalMatrix); |
| 96 | m = &total; |
| 97 | } |
| 98 | if (m->invert(&fTotalInverse)) { |
| 99 | fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse); |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | #include "SkColorPriv.h" |
| 106 | |
| 107 | void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) { |
| 108 | SkASSERT(span16); |
| 109 | SkASSERT(count > 0); |
| 110 | SkASSERT(this->canCallShadeSpan16()); |
| 111 | |
| 112 | // basically, if we get here, the subclass screwed up |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 113 | SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | #define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 117 | #define kTempColorCount (kTempColorQuadCount << 2) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | |
| 119 | #ifdef SK_CPU_BENDIAN |
| 120 | #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3)) |
| 121 | #else |
| 122 | #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3) |
| 123 | #endif |
| 124 | |
| 125 | void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { |
| 126 | SkASSERT(count > 0); |
| 127 | |
| 128 | SkPMColor colors[kTempColorCount]; |
| 129 | |
| 130 | while ((count -= kTempColorCount) >= 0) { |
| 131 | this->shadeSpan(x, y, colors, kTempColorCount); |
| 132 | x += kTempColorCount; |
| 133 | |
| 134 | const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT); |
| 135 | int quads = kTempColorQuadCount; |
| 136 | do { |
| 137 | U8CPU a0 = srcA[0]; |
| 138 | U8CPU a1 = srcA[4]; |
| 139 | U8CPU a2 = srcA[8]; |
| 140 | U8CPU a3 = srcA[12]; |
| 141 | srcA += 4*4; |
| 142 | *alpha++ = SkToU8(a0); |
| 143 | *alpha++ = SkToU8(a1); |
| 144 | *alpha++ = SkToU8(a2); |
| 145 | *alpha++ = SkToU8(a3); |
| 146 | } while (--quads != 0); |
| 147 | } |
| 148 | SkASSERT(count < 0); |
| 149 | SkASSERT(count + kTempColorCount >= 0); |
| 150 | if (count += kTempColorCount) { |
| 151 | this->shadeSpan(x, y, colors, count); |
| 152 | |
| 153 | const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT); |
| 154 | do { |
| 155 | *alpha++ = *srcA; |
| 156 | srcA += 4; |
| 157 | } while (--count != 0); |
| 158 | } |
| 159 | #if 0 |
| 160 | do { |
| 161 | int n = count; |
| 162 | if (n > kTempColorCount) |
| 163 | n = kTempColorCount; |
| 164 | SkASSERT(n > 0); |
| 165 | |
| 166 | this->shadeSpan(x, y, colors, n); |
| 167 | x += n; |
| 168 | count -= n; |
| 169 | |
| 170 | const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT); |
| 171 | do { |
| 172 | *alpha++ = *srcA; |
| 173 | srcA += 4; |
| 174 | } while (--n != 0); |
| 175 | } while (count > 0); |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) { |
| 180 | MatrixClass mc = kLinear_MatrixClass; |
| 181 | |
tomhudson@google.com | 8d43018 | 2011-06-06 19:11:19 +0000 | [diff] [blame] | 182 | if (mat.hasPerspective()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | if (mat.fixedStepInX(0, NULL, NULL)) { |
| 184 | mc = kFixedStepInX_MatrixClass; |
| 185 | } else { |
| 186 | mc = kPerspective_MatrixClass; |
| 187 | } |
| 188 | } |
| 189 | return mc; |
| 190 | } |
| 191 | |
| 192 | ////////////////////////////////////////////////////////////////////////////// |
| 193 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 194 | SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 195 | TileMode*, SkScalar*) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 196 | return kNone_BitmapType; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | } |
| 198 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 199 | SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const { |
| 200 | return kNone_GradientType; |
| 201 | } |
| 202 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | SkShader* SkShader::CreateBitmapShader(const SkBitmap& src, |
| 204 | TileMode tmx, TileMode tmy) { |
| 205 | return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0); |
| 206 | } |
| 207 | |
| 208 | ////////////////////////////////////////////////////////////////////////////// |
| 209 | |
| 210 | #include "SkColorShader.h" |
| 211 | #include "SkUtils.h" |
| 212 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 213 | SkColorShader::SkColorShader() { |
| 214 | fFlags = 0; |
| 215 | fInheritColor = true; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | SkColorShader::SkColorShader(SkColor c) { |
| 219 | fFlags = 0; |
| 220 | fColor = c; |
| 221 | fInheritColor = false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 222 | } |
| 223 | |
reed@google.com | 2be9e8b | 2011-07-06 21:18:09 +0000 | [diff] [blame] | 224 | SkColorShader::~SkColorShader() {} |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 225 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 226 | bool SkColorShader::isOpaque() const { |
| 227 | if (fInheritColor) { |
| 228 | return true; // using paint's alpha |
| 229 | } |
| 230 | return SkColorGetA(fColor) == 255; |
| 231 | } |
| 232 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) { |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 234 | fFlags = 0; // computed in setContext |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 235 | |
| 236 | fInheritColor = b.readU8(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | if (fInheritColor) { |
| 238 | return; |
| 239 | } |
| 240 | fColor = b.readU32(); |
| 241 | } |
| 242 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 243 | void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | this->INHERITED::flatten(buffer); |
| 245 | buffer.write8(fInheritColor); |
| 246 | if (fInheritColor) { |
| 247 | return; |
| 248 | } |
| 249 | buffer.write32(fColor); |
| 250 | } |
| 251 | |
reed@google.com | 59ccef6 | 2011-12-07 14:59:50 +0000 | [diff] [blame] | 252 | uint32_t SkColorShader::getFlags() { |
| 253 | return fFlags; |
| 254 | } |
| 255 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | uint8_t SkColorShader::getSpan16Alpha() const { |
| 257 | return SkGetPackedA32(fPMColor); |
| 258 | } |
| 259 | |
| 260 | bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint, |
| 261 | const SkMatrix& matrix) { |
| 262 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 263 | return false; |
| 264 | } |
| 265 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | unsigned a; |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 267 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | if (fInheritColor) { |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 269 | fColor = paint.getColor(); |
| 270 | a = SkColorGetA(fColor); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | } else { |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 272 | a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 273 | } |
| 274 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 275 | unsigned r = SkColorGetR(fColor); |
| 276 | unsigned g = SkColorGetG(fColor); |
| 277 | unsigned b = SkColorGetB(fColor); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 278 | |
| 279 | // we want this before we apply any alpha |
| 280 | fColor16 = SkPack888ToRGB16(r, g, b); |
| 281 | |
| 282 | if (a != 255) { |
reed@android.com | 8f07338 | 2010-03-11 21:56:16 +0000 | [diff] [blame] | 283 | r = SkMulDiv255Round(r, a); |
| 284 | g = SkMulDiv255Round(g, a); |
| 285 | b = SkMulDiv255Round(b, a); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | } |
| 287 | fPMColor = SkPackARGB32(a, r, g, b); |
| 288 | |
reed@android.com | 8f07338 | 2010-03-11 21:56:16 +0000 | [diff] [blame] | 289 | fFlags = kConstInY32_Flag; |
reed@android.com | 5b81535 | 2010-03-11 22:20:43 +0000 | [diff] [blame] | 290 | if (255 == a) { |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 291 | fFlags |= kOpaqueAlpha_Flag; |
reed@android.com | 5b81535 | 2010-03-11 22:20:43 +0000 | [diff] [blame] | 292 | if (paint.isDither() == false) { |
| 293 | fFlags |= kHasSpan16_Flag; |
| 294 | } |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 295 | } |
| 296 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | |
| 300 | void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) { |
| 301 | sk_memset32(span, fPMColor, count); |
| 302 | } |
| 303 | |
| 304 | void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) { |
| 305 | sk_memset16(span, fColor16, count); |
| 306 | } |
| 307 | |
| 308 | void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { |
| 309 | memset(alpha, SkGetPackedA32(fPMColor), count); |
| 310 | } |
| 311 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 312 | // if we had a asAColor method, that would be more efficient... |
| 313 | SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix, |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 314 | TileMode modes[], |
reed@google.com | 8cad586 | 2011-03-08 14:51:44 +0000 | [diff] [blame] | 315 | SkScalar* twoPointRadialParams) const { |
reed@google.com | 2be9e8b | 2011-07-06 21:18:09 +0000 | [diff] [blame] | 316 | return kNone_BitmapType; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 317 | } |
| 318 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 319 | SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const { |
| 320 | if (info) { |
| 321 | if (info->fColors && info->fColorCount >= 1) { |
| 322 | info->fColors[0] = fColor; |
| 323 | } |
| 324 | info->fColorCount = 1; |
| 325 | info->fTileMode = SkShader::kRepeat_TileMode; |
| 326 | } |
| 327 | return kColor_GradientType; |
| 328 | } |
reed@google.com | 37a2012 | 2011-07-05 18:54:12 +0000 | [diff] [blame] | 329 | |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 330 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkColorShader) |
| 331 | |
reed@google.com | 37a2012 | 2011-07-05 18:54:12 +0000 | [diff] [blame] | 332 | /////////////////////////////////////////////////////////////////////////////// |
| 333 | |
| 334 | #include "SkEmptyShader.h" |
| 335 | |
reed@google.com | 37a2012 | 2011-07-05 18:54:12 +0000 | [diff] [blame] | 336 | uint32_t SkEmptyShader::getFlags() { return 0; } |
| 337 | uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; } |
reed@google.com | 59ccef6 | 2011-12-07 14:59:50 +0000 | [diff] [blame] | 338 | |
| 339 | bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&, |
| 340 | const SkMatrix&) { return false; } |
| 341 | |
| 342 | void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 343 | SkDEBUGFAIL("should never get called, since setContext() returned false"); |
reed@google.com | 37a2012 | 2011-07-05 18:54:12 +0000 | [diff] [blame] | 344 | } |
reed@google.com | 59ccef6 | 2011-12-07 14:59:50 +0000 | [diff] [blame] | 345 | |
| 346 | void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 347 | SkDEBUGFAIL("should never get called, since setContext() returned false"); |
reed@google.com | 59ccef6 | 2011-12-07 14:59:50 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 351 | SkDEBUGFAIL("should never get called, since setContext() returned false"); |
reed@google.com | 59ccef6 | 2011-12-07 14:59:50 +0000 | [diff] [blame] | 352 | } |
reed@google.com | 37a2012 | 2011-07-05 18:54:12 +0000 | [diff] [blame] | 353 | |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 354 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkEmptyShader) |