rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkGradientShaderPriv.h" |
| 9 | #include "SkLinearGradient.h" |
| 10 | #include "SkRadialGradient.h" |
| 11 | #include "SkTwoPointRadialGradient.h" |
| 12 | #include "SkTwoPointConicalGradient.h" |
| 13 | #include "SkSweepGradient.h" |
| 14 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 15 | SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc) { |
| 16 | SkASSERT(desc.fCount > 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 17 | |
| 18 | fCacheAlpha = 256; // init to a value that paint.getAlpha() can't return |
| 19 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 20 | fMapper = desc.fMapper; |
| 21 | SkSafeRef(fMapper); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 22 | fGradFlags = SkToU8(desc.fFlags); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 23 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 24 | SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 25 | SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs)); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 26 | fTileMode = desc.fTileMode; |
| 27 | fTileProc = gTileProcs[desc.fTileMode]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 28 | |
| 29 | fCache16 = fCache16Storage = NULL; |
| 30 | fCache32 = NULL; |
| 31 | fCache32PixelRef = NULL; |
| 32 | |
| 33 | /* Note: we let the caller skip the first and/or last position. |
| 34 | i.e. pos[0] = 0.3, pos[1] = 0.7 |
| 35 | In these cases, we insert dummy entries to ensure that the final data |
| 36 | will be bracketed by [0, 1]. |
| 37 | i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1 |
| 38 | |
| 39 | Thus colorCount (the caller's value, and fColorCount (our value) may |
| 40 | differ by up to 2. In the above example: |
| 41 | colorCount = 2 |
| 42 | fColorCount = 4 |
| 43 | */ |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 44 | fColorCount = desc.fCount; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 45 | // check if we need to add in dummy start and/or end position/colors |
| 46 | bool dummyFirst = false; |
| 47 | bool dummyLast = false; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 48 | if (desc.fPos) { |
| 49 | dummyFirst = desc.fPos[0] != 0; |
| 50 | dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 51 | fColorCount += dummyFirst + dummyLast; |
| 52 | } |
| 53 | |
| 54 | if (fColorCount > kColorStorageCount) { |
| 55 | size_t size = sizeof(SkColor) + sizeof(Rec); |
| 56 | fOrigColors = reinterpret_cast<SkColor*>( |
| 57 | sk_malloc_throw(size * fColorCount)); |
| 58 | } |
| 59 | else { |
| 60 | fOrigColors = fStorage; |
| 61 | } |
| 62 | |
| 63 | // Now copy over the colors, adding the dummies as needed |
| 64 | { |
| 65 | SkColor* origColors = fOrigColors; |
| 66 | if (dummyFirst) { |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 67 | *origColors++ = desc.fColors[0]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 68 | } |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 69 | memcpy(origColors, desc.fColors, desc.fCount * sizeof(SkColor)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 70 | if (dummyLast) { |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 71 | origColors += desc.fCount; |
| 72 | *origColors = desc.fColors[desc.fCount - 1]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | fRecs = (Rec*)(fOrigColors + fColorCount); |
| 77 | if (fColorCount > 2) { |
| 78 | Rec* recs = fRecs; |
| 79 | recs->fPos = 0; |
| 80 | // recs->fScale = 0; // unused; |
| 81 | recs += 1; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 82 | if (desc.fPos) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 83 | /* We need to convert the user's array of relative positions into |
| 84 | fixed-point positions and scale factors. We need these results |
| 85 | to be strictly monotonic (no two values equal or out of order). |
| 86 | Hence this complex loop that just jams a zero for the scale |
| 87 | value if it sees a segment out of order, and it assures that |
| 88 | we start at 0 and end at 1.0 |
| 89 | */ |
| 90 | SkFixed prev = 0; |
| 91 | int startIndex = dummyFirst ? 0 : 1; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 92 | int count = desc.fCount + dummyLast; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 93 | for (int i = startIndex; i < count; i++) { |
| 94 | // force the last value to be 1.0 |
| 95 | SkFixed curr; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 96 | if (i == desc.fCount) { // we're really at the dummyLast |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 97 | curr = SK_Fixed1; |
| 98 | } else { |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 99 | curr = SkScalarToFixed(desc.fPos[i]); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 100 | } |
| 101 | // pin curr withing range |
| 102 | if (curr < 0) { |
| 103 | curr = 0; |
| 104 | } else if (curr > SK_Fixed1) { |
| 105 | curr = SK_Fixed1; |
| 106 | } |
| 107 | recs->fPos = curr; |
| 108 | if (curr > prev) { |
| 109 | recs->fScale = (1 << 24) / (curr - prev); |
| 110 | } else { |
| 111 | recs->fScale = 0; // ignore this segment |
| 112 | } |
| 113 | // get ready for the next value |
| 114 | prev = curr; |
| 115 | recs += 1; |
| 116 | } |
| 117 | } else { // assume even distribution |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 118 | SkFixed dp = SK_Fixed1 / (desc.fCount - 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 119 | SkFixed p = dp; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 120 | SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp |
| 121 | for (int i = 1; i < desc.fCount; i++) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 122 | recs->fPos = p; |
| 123 | recs->fScale = scale; |
| 124 | recs += 1; |
| 125 | p += dp; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | this->initCommon(); |
| 130 | } |
| 131 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 132 | static uint32_t pack_mode_flags(SkShader::TileMode mode, uint32_t flags) { |
| 133 | SkASSERT(0 == (flags >> 28)); |
| 134 | SkASSERT(0 == ((uint32_t)mode >> 4)); |
| 135 | return (flags << 4) | mode; |
| 136 | } |
| 137 | |
| 138 | static SkShader::TileMode unpack_mode(uint32_t packed) { |
| 139 | return (SkShader::TileMode)(packed & 0xF); |
| 140 | } |
| 141 | |
| 142 | static uint32_t unpack_flags(uint32_t packed) { |
| 143 | return packed >> 4; |
| 144 | } |
| 145 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 146 | SkGradientShaderBase::SkGradientShaderBase(SkReadBuffer& buffer) : INHERITED(buffer) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 147 | fCacheAlpha = 256; |
| 148 | |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 149 | fMapper = buffer.readUnitMapper(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 150 | |
| 151 | fCache16 = fCache16Storage = NULL; |
| 152 | fCache32 = NULL; |
| 153 | fCache32PixelRef = NULL; |
| 154 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 155 | int colorCount = fColorCount = buffer.getArrayCount(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 156 | if (colorCount > kColorStorageCount) { |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 157 | size_t allocSize = (sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec)) * colorCount; |
| 158 | if (buffer.validateAvailable(allocSize)) { |
| 159 | fOrigColors = reinterpret_cast<SkColor*>(sk_malloc_throw(allocSize)); |
| 160 | } else { |
| 161 | fOrigColors = NULL; |
| 162 | colorCount = fColorCount = 0; |
| 163 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 164 | } else { |
| 165 | fOrigColors = fStorage; |
| 166 | } |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 167 | buffer.readColorArray(fOrigColors, colorCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 168 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 169 | { |
| 170 | uint32_t packed = buffer.readUInt(); |
| 171 | fGradFlags = SkToU8(unpack_flags(packed)); |
| 172 | fTileMode = unpack_mode(packed); |
| 173 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 174 | fTileProc = gTileProcs[fTileMode]; |
| 175 | fRecs = (Rec*)(fOrigColors + colorCount); |
| 176 | if (colorCount > 2) { |
| 177 | Rec* recs = fRecs; |
| 178 | recs[0].fPos = 0; |
| 179 | for (int i = 1; i < colorCount; i++) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 180 | recs[i].fPos = buffer.readInt(); |
| 181 | recs[i].fScale = buffer.readUInt(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | buffer.readMatrix(&fPtsToUnit); |
| 185 | this->initCommon(); |
| 186 | } |
| 187 | |
| 188 | SkGradientShaderBase::~SkGradientShaderBase() { |
| 189 | if (fCache16Storage) { |
| 190 | sk_free(fCache16Storage); |
| 191 | } |
| 192 | SkSafeUnref(fCache32PixelRef); |
| 193 | if (fOrigColors != fStorage) { |
| 194 | sk_free(fOrigColors); |
| 195 | } |
| 196 | SkSafeUnref(fMapper); |
| 197 | } |
| 198 | |
| 199 | void SkGradientShaderBase::initCommon() { |
| 200 | fFlags = 0; |
| 201 | unsigned colorAlpha = 0xFF; |
| 202 | for (int i = 0; i < fColorCount; i++) { |
| 203 | colorAlpha &= SkColorGetA(fOrigColors[i]); |
| 204 | } |
| 205 | fColorsAreOpaque = colorAlpha == 0xFF; |
| 206 | } |
| 207 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 208 | void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 209 | this->INHERITED::flatten(buffer); |
| 210 | buffer.writeFlattenable(fMapper); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 211 | buffer.writeColorArray(fOrigColors, fColorCount); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 212 | buffer.writeUInt(pack_mode_flags(fTileMode, fGradFlags)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 213 | if (fColorCount > 2) { |
| 214 | Rec* recs = fRecs; |
| 215 | for (int i = 1; i < fColorCount; i++) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 216 | buffer.writeInt(recs[i].fPos); |
| 217 | buffer.writeUInt(recs[i].fScale); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | buffer.writeMatrix(fPtsToUnit); |
| 221 | } |
| 222 | |
| 223 | bool SkGradientShaderBase::isOpaque() const { |
| 224 | return fColorsAreOpaque; |
| 225 | } |
| 226 | |
| 227 | bool SkGradientShaderBase::setContext(const SkBitmap& device, |
| 228 | const SkPaint& paint, |
| 229 | const SkMatrix& matrix) { |
| 230 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | const SkMatrix& inverse = this->getTotalInverse(); |
| 235 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 236 | fDstToIndex.setConcat(fPtsToUnit, inverse); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 237 | fDstToIndexProc = fDstToIndex.getMapXYProc(); |
| 238 | fDstToIndexClass = (uint8_t)SkShader::ComputeMatrixClass(fDstToIndex); |
| 239 | |
| 240 | // now convert our colors in to PMColors |
| 241 | unsigned paintAlpha = this->getPaintAlpha(); |
| 242 | |
| 243 | fFlags = this->INHERITED::getFlags(); |
| 244 | if (fColorsAreOpaque && paintAlpha == 0xFF) { |
| 245 | fFlags |= kOpaqueAlpha_Flag; |
| 246 | } |
| 247 | // we can do span16 as long as our individual colors are opaque, |
| 248 | // regardless of the paint's alpha |
| 249 | if (fColorsAreOpaque) { |
| 250 | fFlags |= kHasSpan16_Flag; |
| 251 | } |
| 252 | |
| 253 | this->setCacheAlpha(paintAlpha); |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | void SkGradientShaderBase::setCacheAlpha(U8CPU alpha) const { |
| 258 | // if the new alpha differs from the previous time we were called, inval our cache |
| 259 | // this will trigger the cache to be rebuilt. |
| 260 | // we don't care about the first time, since the cache ptrs will already be NULL |
| 261 | if (fCacheAlpha != alpha) { |
| 262 | fCache16 = NULL; // inval the cache |
| 263 | fCache32 = NULL; // inval the cache |
| 264 | fCacheAlpha = alpha; // record the new alpha |
| 265 | // inform our subclasses |
| 266 | if (fCache32PixelRef) { |
| 267 | fCache32PixelRef->notifyPixelsChanged(); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | #define Fixed_To_Dot8(x) (((x) + 0x80) >> 8) |
| 273 | |
| 274 | /** We take the original colors, not our premultiplied PMColors, since we can |
| 275 | build a 16bit table as long as the original colors are opaque, even if the |
| 276 | paint specifies a non-opaque alpha. |
| 277 | */ |
| 278 | void SkGradientShaderBase::Build16bitCache(uint16_t cache[], SkColor c0, SkColor c1, |
| 279 | int count) { |
| 280 | SkASSERT(count > 1); |
| 281 | SkASSERT(SkColorGetA(c0) == 0xFF); |
| 282 | SkASSERT(SkColorGetA(c1) == 0xFF); |
| 283 | |
| 284 | SkFixed r = SkColorGetR(c0); |
| 285 | SkFixed g = SkColorGetG(c0); |
| 286 | SkFixed b = SkColorGetB(c0); |
| 287 | |
| 288 | SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1); |
| 289 | SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1); |
| 290 | SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1); |
| 291 | |
| 292 | r = SkIntToFixed(r) + 0x8000; |
| 293 | g = SkIntToFixed(g) + 0x8000; |
| 294 | b = SkIntToFixed(b) + 0x8000; |
| 295 | |
| 296 | do { |
| 297 | unsigned rr = r >> 16; |
| 298 | unsigned gg = g >> 16; |
| 299 | unsigned bb = b >> 16; |
| 300 | cache[0] = SkPackRGB16(SkR32ToR16(rr), SkG32ToG16(gg), SkB32ToB16(bb)); |
| 301 | cache[kCache16Count] = SkDitherPack888ToRGB16(rr, gg, bb); |
| 302 | cache += 1; |
| 303 | r += dr; |
| 304 | g += dg; |
| 305 | b += db; |
| 306 | } while (--count != 0); |
| 307 | } |
| 308 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 309 | /* |
| 310 | * r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in |
| 311 | * release builds, we saw a compiler error where the 0xFF parameter in |
| 312 | * SkPackARGB32() was being totally ignored whenever it was called with |
| 313 | * a non-zero add (e.g. 0x8000). |
| 314 | * |
| 315 | * We found two work-arounds: |
| 316 | * 1. change r,g,b to unsigned (or just one of them) |
| 317 | * 2. change SkPackARGB32 to + its (a << SK_A32_SHIFT) value instead |
| 318 | * of using | |
| 319 | * |
| 320 | * We chose #1 just because it was more localized. |
| 321 | * See http://code.google.com/p/skia/issues/detail?id=1113 |
| 322 | * |
| 323 | * The type SkUFixed encapsulate this need for unsigned, but logically Fixed. |
| 324 | */ |
| 325 | typedef uint32_t SkUFixed; |
| 326 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 327 | void SkGradientShaderBase::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 328 | int count, U8CPU paintAlpha, uint32_t gradFlags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 329 | SkASSERT(count > 1); |
| 330 | |
| 331 | // need to apply paintAlpha to our two endpoints |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 332 | uint32_t a0 = SkMulDiv255Round(SkColorGetA(c0), paintAlpha); |
| 333 | uint32_t a1 = SkMulDiv255Round(SkColorGetA(c1), paintAlpha); |
| 334 | |
| 335 | |
| 336 | const bool interpInPremul = SkToBool(gradFlags & |
| 337 | SkGradientShader::kInterpolateColorsInPremul_Flag); |
| 338 | |
| 339 | uint32_t r0 = SkColorGetR(c0); |
| 340 | uint32_t g0 = SkColorGetG(c0); |
| 341 | uint32_t b0 = SkColorGetB(c0); |
| 342 | |
| 343 | uint32_t r1 = SkColorGetR(c1); |
| 344 | uint32_t g1 = SkColorGetG(c1); |
| 345 | uint32_t b1 = SkColorGetB(c1); |
| 346 | |
| 347 | if (interpInPremul) { |
| 348 | r0 = SkMulDiv255Round(r0, a0); |
| 349 | g0 = SkMulDiv255Round(g0, a0); |
| 350 | b0 = SkMulDiv255Round(b0, a0); |
| 351 | |
| 352 | r1 = SkMulDiv255Round(r1, a1); |
| 353 | g1 = SkMulDiv255Round(g1, a1); |
| 354 | b1 = SkMulDiv255Round(b1, a1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 355 | } |
| 356 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 357 | SkFixed da = SkIntToFixed(a1 - a0) / (count - 1); |
| 358 | SkFixed dr = SkIntToFixed(r1 - r0) / (count - 1); |
| 359 | SkFixed dg = SkIntToFixed(g1 - g0) / (count - 1); |
| 360 | SkFixed db = SkIntToFixed(b1 - b0) / (count - 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 361 | |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 362 | /* We pre-add 1/8 to avoid having to add this to our [0] value each time |
| 363 | in the loop. Without this, the bias for each would be |
| 364 | 0x2000 0xA000 0xE000 0x6000 |
| 365 | With this trick, we can add 0 for the first (no-op) and just adjust the |
| 366 | others. |
| 367 | */ |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 368 | SkUFixed a = SkIntToFixed(a0) + 0x2000; |
| 369 | SkUFixed r = SkIntToFixed(r0) + 0x2000; |
| 370 | SkUFixed g = SkIntToFixed(g0) + 0x2000; |
| 371 | SkUFixed b = SkIntToFixed(b0) + 0x2000; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 372 | |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 373 | /* |
| 374 | * Our dither-cell (spatially) is |
| 375 | * 0 2 |
| 376 | * 3 1 |
| 377 | * Where |
| 378 | * [0] -> [-1/8 ... 1/8 ) values near 0 |
| 379 | * [1] -> [ 1/8 ... 3/8 ) values near 1/4 |
| 380 | * [2] -> [ 3/8 ... 5/8 ) values near 1/2 |
| 381 | * [3] -> [ 5/8 ... 7/8 ) values near 3/4 |
| 382 | */ |
| 383 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 384 | if (0xFF == a0 && 0 == da) { |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 385 | do { |
| 386 | cache[kCache32Count*0] = SkPackARGB32(0xFF, (r + 0 ) >> 16, |
| 387 | (g + 0 ) >> 16, |
| 388 | (b + 0 ) >> 16); |
| 389 | cache[kCache32Count*1] = SkPackARGB32(0xFF, (r + 0x8000) >> 16, |
| 390 | (g + 0x8000) >> 16, |
| 391 | (b + 0x8000) >> 16); |
| 392 | cache[kCache32Count*2] = SkPackARGB32(0xFF, (r + 0xC000) >> 16, |
| 393 | (g + 0xC000) >> 16, |
| 394 | (b + 0xC000) >> 16); |
| 395 | cache[kCache32Count*3] = SkPackARGB32(0xFF, (r + 0x4000) >> 16, |
| 396 | (g + 0x4000) >> 16, |
| 397 | (b + 0x4000) >> 16); |
| 398 | cache += 1; |
| 399 | r += dr; |
| 400 | g += dg; |
| 401 | b += db; |
| 402 | } while (--count != 0); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 403 | } else if (interpInPremul) { |
| 404 | do { |
| 405 | cache[kCache32Count*0] = SkPackARGB32((a + 0 ) >> 16, |
| 406 | (r + 0 ) >> 16, |
| 407 | (g + 0 ) >> 16, |
| 408 | (b + 0 ) >> 16); |
| 409 | cache[kCache32Count*1] = SkPackARGB32((a + 0x8000) >> 16, |
| 410 | (r + 0x8000) >> 16, |
| 411 | (g + 0x8000) >> 16, |
| 412 | (b + 0x8000) >> 16); |
| 413 | cache[kCache32Count*2] = SkPackARGB32((a + 0xC000) >> 16, |
| 414 | (r + 0xC000) >> 16, |
| 415 | (g + 0xC000) >> 16, |
| 416 | (b + 0xC000) >> 16); |
| 417 | cache[kCache32Count*3] = SkPackARGB32((a + 0x4000) >> 16, |
| 418 | (r + 0x4000) >> 16, |
| 419 | (g + 0x4000) >> 16, |
| 420 | (b + 0x4000) >> 16); |
| 421 | cache += 1; |
| 422 | a += da; |
| 423 | r += dr; |
| 424 | g += dg; |
| 425 | b += db; |
| 426 | } while (--count != 0); |
| 427 | } else { // interpolate in unpreml space |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 428 | do { |
| 429 | cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0 ) >> 16, |
| 430 | (r + 0 ) >> 16, |
| 431 | (g + 0 ) >> 16, |
| 432 | (b + 0 ) >> 16); |
| 433 | cache[kCache32Count*1] = SkPremultiplyARGBInline((a + 0x8000) >> 16, |
| 434 | (r + 0x8000) >> 16, |
| 435 | (g + 0x8000) >> 16, |
| 436 | (b + 0x8000) >> 16); |
| 437 | cache[kCache32Count*2] = SkPremultiplyARGBInline((a + 0xC000) >> 16, |
| 438 | (r + 0xC000) >> 16, |
| 439 | (g + 0xC000) >> 16, |
| 440 | (b + 0xC000) >> 16); |
| 441 | cache[kCache32Count*3] = SkPremultiplyARGBInline((a + 0x4000) >> 16, |
| 442 | (r + 0x4000) >> 16, |
| 443 | (g + 0x4000) >> 16, |
| 444 | (b + 0x4000) >> 16); |
| 445 | cache += 1; |
| 446 | a += da; |
| 447 | r += dr; |
| 448 | g += dg; |
| 449 | b += db; |
| 450 | } while (--count != 0); |
| 451 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static inline int SkFixedToFFFF(SkFixed x) { |
| 455 | SkASSERT((unsigned)x <= SK_Fixed1); |
| 456 | return x - (x >> 16); |
| 457 | } |
| 458 | |
| 459 | static inline U16CPU bitsTo16(unsigned x, const unsigned bits) { |
| 460 | SkASSERT(x < (1U << bits)); |
| 461 | if (6 == bits) { |
| 462 | return (x << 10) | (x << 4) | (x >> 2); |
| 463 | } |
| 464 | if (8 == bits) { |
| 465 | return (x << 8) | x; |
| 466 | } |
| 467 | sk_throw(); |
| 468 | return 0; |
| 469 | } |
| 470 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 471 | const uint16_t* SkGradientShaderBase::getCache16() const { |
| 472 | if (fCache16 == NULL) { |
| 473 | // double the count for dither entries |
| 474 | const int entryCount = kCache16Count * 2; |
| 475 | const size_t allocSize = sizeof(uint16_t) * entryCount; |
| 476 | |
| 477 | if (fCache16Storage == NULL) { // set the storage and our working ptr |
| 478 | fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); |
| 479 | } |
| 480 | fCache16 = fCache16Storage; |
| 481 | if (fColorCount == 2) { |
| 482 | Build16bitCache(fCache16, fOrigColors[0], fOrigColors[1], |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 483 | kCache16Count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 484 | } else { |
| 485 | Rec* rec = fRecs; |
| 486 | int prevIndex = 0; |
| 487 | for (int i = 1; i < fColorCount; i++) { |
| 488 | int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; |
| 489 | SkASSERT(nextIndex < kCache16Count); |
| 490 | |
| 491 | if (nextIndex > prevIndex) |
| 492 | Build16bitCache(fCache16 + prevIndex, fOrigColors[i-1], fOrigColors[i], nextIndex - prevIndex + 1); |
| 493 | prevIndex = nextIndex; |
| 494 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (fMapper) { |
| 498 | fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); |
| 499 | uint16_t* linear = fCache16; // just computed linear data |
| 500 | uint16_t* mapped = fCache16Storage; // storage for mapped data |
| 501 | SkUnitMapper* map = fMapper; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 502 | for (int i = 0; i < kCache16Count; i++) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 503 | int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shift; |
| 504 | mapped[i] = linear[index]; |
| 505 | mapped[i + kCache16Count] = linear[index + kCache16Count]; |
| 506 | } |
| 507 | sk_free(fCache16); |
| 508 | fCache16 = fCache16Storage; |
| 509 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 510 | } |
| 511 | return fCache16; |
| 512 | } |
| 513 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 514 | const SkPMColor* SkGradientShaderBase::getCache32() const { |
| 515 | if (fCache32 == NULL) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 516 | SkImageInfo info; |
| 517 | info.fWidth = kCache32Count; |
| 518 | info.fHeight = 4; // for our 4 dither rows |
| 519 | info.fAlphaType = kPremul_SkAlphaType; |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame^] | 520 | info.fColorType = kN32_SkColorType; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 521 | |
| 522 | if (NULL == fCache32PixelRef) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 523 | fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 524 | } |
| 525 | fCache32 = (SkPMColor*)fCache32PixelRef->getAddr(); |
| 526 | if (fColorCount == 2) { |
| 527 | Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1], |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 528 | kCache32Count, fCacheAlpha, fGradFlags); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 529 | } else { |
| 530 | Rec* rec = fRecs; |
| 531 | int prevIndex = 0; |
| 532 | for (int i = 1; i < fColorCount; i++) { |
| 533 | int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 534 | SkASSERT(nextIndex < kCache32Count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 535 | |
| 536 | if (nextIndex > prevIndex) |
| 537 | Build32bitCache(fCache32 + prevIndex, fOrigColors[i-1], |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 538 | fOrigColors[i], nextIndex - prevIndex + 1, |
| 539 | fCacheAlpha, fGradFlags); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 540 | prevIndex = nextIndex; |
| 541 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | if (fMapper) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 545 | SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, NULL); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 546 | SkPMColor* linear = fCache32; // just computed linear data |
| 547 | SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data |
| 548 | SkUnitMapper* map = fMapper; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 549 | for (int i = 0; i < kCache32Count; i++) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 550 | int index = map->mapUnit16((i << 8) | i) >> 8; |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 551 | mapped[i + kCache32Count*0] = linear[index + kCache32Count*0]; |
| 552 | mapped[i + kCache32Count*1] = linear[index + kCache32Count*1]; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 553 | mapped[i + kCache32Count*2] = linear[index + kCache32Count*2]; |
| 554 | mapped[i + kCache32Count*3] = linear[index + kCache32Count*3]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 555 | } |
| 556 | fCache32PixelRef->unref(); |
| 557 | fCache32PixelRef = newPR; |
| 558 | fCache32 = (SkPMColor*)newPR->getAddr(); |
| 559 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 560 | } |
| 561 | return fCache32; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Because our caller might rebuild the same (logically the same) gradient |
| 566 | * over and over, we'd like to return exactly the same "bitmap" if possible, |
| 567 | * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). |
| 568 | * To do that, we maintain a private cache of built-bitmaps, based on our |
| 569 | * colors and positions. Note: we don't try to flatten the fMapper, so if one |
| 570 | * is present, we skip the cache for now. |
| 571 | */ |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 572 | void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 573 | // our caller assumes no external alpha, so we ensure that our cache is |
| 574 | // built with 0xFF |
| 575 | this->setCacheAlpha(0xFF); |
| 576 | |
| 577 | // don't have a way to put the mapper into our cache-key yet |
| 578 | if (fMapper) { |
| 579 | // force our cahce32pixelref to be built |
| 580 | (void)this->getCache32(); |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 581 | bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 582 | bitmap->setPixelRef(fCache32PixelRef); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 586 | // build our key: [numColors + colors[] + {positions[]} + flags ] |
| 587 | int count = 1 + fColorCount + 1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 588 | if (fColorCount > 2) { |
| 589 | count += fColorCount - 1; // fRecs[].fPos |
| 590 | } |
| 591 | |
| 592 | SkAutoSTMalloc<16, int32_t> storage(count); |
| 593 | int32_t* buffer = storage.get(); |
| 594 | |
| 595 | *buffer++ = fColorCount; |
| 596 | memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor)); |
| 597 | buffer += fColorCount; |
| 598 | if (fColorCount > 2) { |
| 599 | for (int i = 1; i < fColorCount; i++) { |
| 600 | *buffer++ = fRecs[i].fPos; |
| 601 | } |
| 602 | } |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 603 | *buffer++ = fGradFlags; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 604 | SkASSERT(buffer - storage.get() == count); |
| 605 | |
| 606 | /////////////////////////////////// |
| 607 | |
| 608 | SK_DECLARE_STATIC_MUTEX(gMutex); |
| 609 | static SkBitmapCache* gCache; |
| 610 | // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp |
| 611 | static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; |
| 612 | SkAutoMutexAcquire ama(gMutex); |
| 613 | |
| 614 | if (NULL == gCache) { |
| 615 | gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS)); |
| 616 | } |
| 617 | size_t size = count * sizeof(int32_t); |
| 618 | |
| 619 | if (!gCache->find(storage.get(), size, bitmap)) { |
| 620 | // force our cahce32pixelref to be built |
| 621 | (void)this->getCache32(); |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 622 | bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 623 | bitmap->setPixelRef(fCache32PixelRef); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 624 | |
| 625 | gCache->add(storage.get(), size, *bitmap); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const { |
| 630 | if (info) { |
| 631 | if (info->fColorCount >= fColorCount) { |
| 632 | if (info->fColors) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 633 | memcpy(info->fColors, fOrigColors, fColorCount * sizeof(SkColor)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 634 | } |
| 635 | if (info->fColorOffsets) { |
| 636 | if (fColorCount == 2) { |
| 637 | info->fColorOffsets[0] = 0; |
| 638 | info->fColorOffsets[1] = SK_Scalar1; |
| 639 | } else if (fColorCount > 2) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 640 | for (int i = 0; i < fColorCount; ++i) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 641 | info->fColorOffsets[i] = SkFixedToScalar(fRecs[i].fPos); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 642 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | info->fColorCount = fColorCount; |
| 647 | info->fTileMode = fTileMode; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 648 | info->fGradientFlags = fGradFlags; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 652 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 653 | void SkGradientShaderBase::toString(SkString* str) const { |
| 654 | |
| 655 | str->appendf("%d colors: ", fColorCount); |
| 656 | |
| 657 | for (int i = 0; i < fColorCount; ++i) { |
| 658 | str->appendHex(fOrigColors[i]); |
| 659 | if (i < fColorCount-1) { |
| 660 | str->append(", "); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | if (fColorCount > 2) { |
| 665 | str->append(" points: ("); |
| 666 | for (int i = 0; i < fColorCount; ++i) { |
| 667 | str->appendScalar(SkFixedToScalar(fRecs[i].fPos)); |
| 668 | if (i < fColorCount-1) { |
| 669 | str->append(", "); |
| 670 | } |
| 671 | } |
| 672 | str->append(")"); |
| 673 | } |
| 674 | |
| 675 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 676 | "clamp", "repeat", "mirror" |
| 677 | }; |
| 678 | |
| 679 | str->append(" "); |
| 680 | str->append(gTileModeName[fTileMode]); |
| 681 | |
| 682 | // TODO: add "fMapper->toString(str);" when SkUnitMapper::toString is added |
| 683 | |
| 684 | this->INHERITED::toString(str); |
| 685 | } |
| 686 | #endif |
| 687 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 688 | /////////////////////////////////////////////////////////////////////////////// |
| 689 | /////////////////////////////////////////////////////////////////////////////// |
| 690 | |
| 691 | #include "SkEmptyShader.h" |
| 692 | |
| 693 | // assumes colors is SkColor* and pos is SkScalar* |
| 694 | #define EXPAND_1_COLOR(count) \ |
| 695 | SkColor tmp[2]; \ |
| 696 | do { \ |
| 697 | if (1 == count) { \ |
| 698 | tmp[0] = tmp[1] = colors[0]; \ |
| 699 | colors = tmp; \ |
| 700 | pos = NULL; \ |
| 701 | count = 2; \ |
| 702 | } \ |
| 703 | } while (0) |
| 704 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 705 | static void desc_init(SkGradientShaderBase::Descriptor* desc, |
| 706 | const SkColor colors[], |
| 707 | const SkScalar pos[], int colorCount, |
| 708 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 709 | SkUnitMapper* mapper, uint32_t flags) { |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 710 | desc->fColors = colors; |
| 711 | desc->fPos = pos; |
| 712 | desc->fCount = colorCount; |
| 713 | desc->fTileMode = mode; |
| 714 | desc->fMapper = mapper; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 715 | desc->fFlags = flags; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 716 | } |
| 717 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 718 | SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], |
| 719 | const SkColor colors[], |
| 720 | const SkScalar pos[], int colorCount, |
| 721 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 722 | SkUnitMapper* mapper, |
| 723 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 724 | if (NULL == pts || NULL == colors || colorCount < 1) { |
| 725 | return NULL; |
| 726 | } |
| 727 | EXPAND_1_COLOR(colorCount); |
| 728 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 729 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 730 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 731 | return SkNEW_ARGS(SkLinearGradient, (pts, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, |
| 735 | const SkColor colors[], |
| 736 | const SkScalar pos[], int colorCount, |
| 737 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 738 | SkUnitMapper* mapper, |
| 739 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 740 | if (radius <= 0 || NULL == colors || colorCount < 1) { |
| 741 | return NULL; |
| 742 | } |
| 743 | EXPAND_1_COLOR(colorCount); |
| 744 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 745 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 746 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 747 | return SkNEW_ARGS(SkRadialGradient, (center, radius, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, |
| 751 | SkScalar startRadius, |
| 752 | const SkPoint& end, |
| 753 | SkScalar endRadius, |
| 754 | const SkColor colors[], |
| 755 | const SkScalar pos[], |
| 756 | int colorCount, |
| 757 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 758 | SkUnitMapper* mapper, |
| 759 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 760 | if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
| 761 | return NULL; |
| 762 | } |
| 763 | EXPAND_1_COLOR(colorCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 764 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 765 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 766 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 767 | return SkNEW_ARGS(SkTwoPointRadialGradient, |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 768 | (start, startRadius, end, endRadius, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 772 | SkScalar startRadius, |
| 773 | const SkPoint& end, |
| 774 | SkScalar endRadius, |
| 775 | const SkColor colors[], |
| 776 | const SkScalar pos[], |
| 777 | int colorCount, |
| 778 | SkShader::TileMode mode, |
| 779 | SkUnitMapper* mapper, |
| 780 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 781 | if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
| 782 | return NULL; |
| 783 | } |
| 784 | if (start == end && startRadius == endRadius) { |
| 785 | return SkNEW(SkEmptyShader); |
| 786 | } |
rileya@google.com | 1ee7c6a | 2012-07-31 16:00:13 +0000 | [diff] [blame] | 787 | EXPAND_1_COLOR(colorCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 788 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 789 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 790 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 791 | return SkNEW_ARGS(SkTwoPointConicalGradient, |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 792 | (start, startRadius, end, endRadius, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, |
| 796 | const SkColor colors[], |
| 797 | const SkScalar pos[], |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 798 | int colorCount, SkUnitMapper* mapper, |
| 799 | uint32_t flags) { |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 800 | if (NULL == colors || colorCount < 1) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 801 | return NULL; |
| 802 | } |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 803 | EXPAND_1_COLOR(colorCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 804 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 805 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 806 | desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, mapper, flags); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 807 | return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) |
| 811 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) |
| 812 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) |
| 813 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) |
| 814 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient) |
| 815 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient) |
| 816 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 817 | |
| 818 | /////////////////////////////////////////////////////////////////////////////// |
| 819 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 820 | #if SK_SUPPORT_GPU |
| 821 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 822 | #include "effects/GrTextureStripAtlas.h" |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 823 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 824 | #include "SkGr.h" |
| 825 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 826 | GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory) |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 827 | : INHERITED(factory) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 828 | , fCachedYCoord(SK_ScalarMax) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 829 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 830 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 831 | GrGLGradientEffect::~GrGLGradientEffect() { } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 832 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 833 | void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) { |
| 834 | |
| 835 | if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)) { // 2 Color case |
| 836 | fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 837 | kVec4f_GrSLType, "GradientStartColor"); |
| 838 | fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 839 | kVec4f_GrSLType, "GradientEndColor"); |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 840 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 841 | } else if (GrGradientEffect::kThree_ColorType == ColorTypeFromKey(key)){ // 3 Color Case |
| 842 | fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 843 | kVec4f_GrSLType, "GradientStartColor"); |
| 844 | fColorMidUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 845 | kVec4f_GrSLType, "GradientMidColor"); |
| 846 | fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 847 | kVec4f_GrSLType, "GradientEndColor"); |
| 848 | |
| 849 | } else { // if not a fast case |
| 850 | fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 851 | kFloat_GrSLType, "GradientYCoordFS"); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | static inline void set_color_uni(const GrGLUniformManager& uman, |
| 856 | const GrGLUniformManager::UniformHandle uni, |
| 857 | const SkColor* color) { |
| 858 | uman.set4f(uni, |
| 859 | SkColorGetR(*color) / 255.f, |
| 860 | SkColorGetG(*color) / 255.f, |
| 861 | SkColorGetB(*color) / 255.f, |
| 862 | SkColorGetA(*color) / 255.f); |
| 863 | } |
| 864 | |
| 865 | static inline void set_mul_color_uni(const GrGLUniformManager& uman, |
| 866 | const GrGLUniformManager::UniformHandle uni, |
| 867 | const SkColor* color){ |
| 868 | float a = SkColorGetA(*color) / 255.f; |
| 869 | float aDiv255 = a / 255.f; |
| 870 | uman.set4f(uni, |
| 871 | SkColorGetR(*color) * aDiv255, |
| 872 | SkColorGetG(*color) * aDiv255, |
| 873 | SkColorGetB(*color) * aDiv255, |
| 874 | a); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 875 | } |
| 876 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 877 | void GrGLGradientEffect::setData(const GrGLUniformManager& uman, |
| 878 | const GrDrawEffect& drawEffect) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 879 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 880 | const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
| 881 | |
| 882 | |
| 883 | if (GrGradientEffect::kTwo_ColorType == e.getColorType()){ |
| 884 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 885 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 886 | set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 887 | set_mul_color_uni(uman, fColorEndUni, e.getColors(1)); |
| 888 | } else { |
| 889 | set_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 890 | set_color_uni(uman, fColorEndUni, e.getColors(1)); |
| 891 | } |
| 892 | |
| 893 | } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ |
| 894 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 895 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 896 | set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 897 | set_mul_color_uni(uman, fColorMidUni, e.getColors(1)); |
| 898 | set_mul_color_uni(uman, fColorEndUni, e.getColors(2)); |
| 899 | } else { |
| 900 | set_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 901 | set_color_uni(uman, fColorMidUni, e.getColors(1)); |
| 902 | set_color_uni(uman, fColorEndUni, e.getColors(2)); |
| 903 | } |
| 904 | } else { |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 905 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 906 | SkScalar yCoord = e.getYCoord(); |
| 907 | if (yCoord != fCachedYCoord) { |
| 908 | uman.set1f(fFSYUni, yCoord); |
| 909 | fCachedYCoord = yCoord; |
| 910 | } |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 914 | |
| 915 | GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 916 | const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 917 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 918 | EffectKey key = 0; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 919 | |
| 920 | if (GrGradientEffect::kTwo_ColorType == e.getColorType()) { |
| 921 | key |= kTwoColorKey; |
| 922 | } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ |
| 923 | key |= kThreeColorKey; |
| 924 | } |
| 925 | |
| 926 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 927 | key |= kPremulBeforeInterpKey; |
| 928 | } |
| 929 | |
| 930 | return key; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 931 | } |
| 932 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 933 | void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder, |
| 934 | const char* gradientTValue, |
| 935 | EffectKey key, |
| 936 | const char* outputColor, |
| 937 | const char* inputColor, |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 938 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 939 | if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)){ |
| 940 | builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1.0));\n", |
| 941 | builder->getUniformVariable(fColorStartUni).c_str(), |
| 942 | builder->getUniformVariable(fColorEndUni).c_str(), |
| 943 | gradientTValue); |
| 944 | // Note that we could skip this step if both colors are known to be opaque. Two |
| 945 | // considerations: |
| 946 | // The gradient SkShader reporting opaque is more restrictive than necessary in the two pt |
| 947 | // case. Make sure the key reflects this optimization (and note that it can use the same |
| 948 | // shader as thekBeforeIterp case). This same optimization applies to the 3 color case below. |
| 949 | if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) { |
| 950 | builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n"); |
| 951 | } |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 952 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 953 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 954 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 955 | } else if (GrGradientEffect::kThree_ColorType == ColorTypeFromKey(key)){ |
| 956 | builder->fsCodeAppendf("\tfloat oneMinus2t = 1.0 - (2.0 * (%s));\n", |
| 957 | gradientTValue); |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 958 | builder->fsCodeAppendf("\tvec4 colorTemp = clamp(oneMinus2t, 0.0, 1.0) * %s;\n", |
| 959 | builder->getUniformVariable(fColorStartUni).c_str()); |
| 960 | if (kTegra3_GrGLRenderer == builder->ctxInfo().renderer()) { |
| 961 | // The Tegra3 compiler will sometimes never return if we have |
| 962 | // min(abs(oneMinus2t), 1.0), or do the abs first in a separate expression. |
| 963 | builder->fsCodeAppend("\tfloat minAbs = abs(oneMinus2t);\n"); |
| 964 | builder->fsCodeAppend("\tminAbs = minAbs > 1.0 ? 1.0 : minAbs;\n"); |
| 965 | builder->fsCodeAppendf("\tcolorTemp += (1.0 - minAbs) * %s;\n", |
| 966 | builder->getUniformVariable(fColorMidUni).c_str()); |
| 967 | } else { |
| 968 | builder->fsCodeAppendf("\tcolorTemp += (1.0 - min(abs(oneMinus2t), 1.0)) * %s;\n", |
| 969 | builder->getUniformVariable(fColorMidUni).c_str()); |
| 970 | } |
| 971 | builder->fsCodeAppendf("\tcolorTemp += clamp(-oneMinus2t, 0.0, 1.0) * %s;\n", |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 972 | builder->getUniformVariable(fColorEndUni).c_str()); |
| 973 | if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) { |
| 974 | builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n"); |
| 975 | } |
| 976 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 977 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 978 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 979 | } else { |
| 980 | builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", |
| 981 | gradientTValue, |
| 982 | builder->getUniformVariable(fFSYUni).c_str()); |
| 983 | builder->fsCodeAppendf("\t%s = ", outputColor); |
| 984 | builder->fsAppendTextureLookupAndModulate(inputColor, |
| 985 | samplers[0], |
| 986 | "coord"); |
| 987 | builder->fsCodeAppend(";\n"); |
| 988 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | ///////////////////////////////////////////////////////////////////// |
| 992 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 993 | GrGradientEffect::GrGradientEffect(GrContext* ctx, |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 994 | const SkGradientShaderBase& shader, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 995 | const SkMatrix& matrix, |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 996 | SkShader::TileMode tileMode) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 997 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 998 | fIsOpaque = shader.isOpaque(); |
| 999 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1000 | SkShader::GradientInfo info; |
| 1001 | SkScalar pos[3] = {0}; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1002 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1003 | info.fColorCount = 3; |
| 1004 | info.fColors = &fColors[0]; |
| 1005 | info.fColorOffsets = &pos[0]; |
| 1006 | shader.asAGradient(&info); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1007 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1008 | // The two and three color specializations do not currently support tiling. |
| 1009 | bool foundSpecialCase = false; |
| 1010 | if (SkShader::kClamp_TileMode == info.fTileMode) { |
| 1011 | if (2 == info.fColorCount) { |
| 1012 | fRow = -1; // flag for no atlas |
| 1013 | fColorType = kTwo_ColorType; |
| 1014 | foundSpecialCase = true; |
| 1015 | } else if (3 == info.fColorCount && |
| 1016 | (SkScalarAbs(pos[1] - SK_ScalarHalf) < SK_Scalar1 / 1000)) { // 3 color symmetric |
| 1017 | fRow = -1; // flag for no atlas |
| 1018 | fColorType = kThree_ColorType; |
| 1019 | foundSpecialCase = true; |
| 1020 | } |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 1021 | } |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1022 | if (foundSpecialCase) { |
| 1023 | if (SkGradientShader::kInterpolateColorsInPremul_Flag & info.fGradientFlags) { |
| 1024 | fPremulType = kBeforeInterp_PremulType; |
| 1025 | } else { |
| 1026 | fPremulType = kAfterInterp_PremulType; |
| 1027 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1028 | fCoordTransform.reset(kCoordSet, matrix); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | // doesn't matter how this is set, just be consistent because it is part of the effect key. |
| 1031 | fPremulType = kBeforeInterp_PremulType; |
| 1032 | SkBitmap bitmap; |
| 1033 | shader.getGradientTableBitmap(&bitmap); |
| 1034 | fColorType = kTexture_ColorType; |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 1035 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1036 | GrTextureStripAtlas::Desc desc; |
| 1037 | desc.fWidth = bitmap.width(); |
| 1038 | desc.fHeight = 32; |
| 1039 | desc.fRowHeight = bitmap.height(); |
| 1040 | desc.fContext = ctx; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 1041 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.colorType(), bitmap.alphaType()); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1042 | fAtlas = GrTextureStripAtlas::GetAtlas(desc); |
| 1043 | SkASSERT(NULL != fAtlas); |
| 1044 | |
| 1045 | // We always filter the gradient table. Each table is one row of a texture, always y-clamp. |
| 1046 | GrTextureParams params; |
| 1047 | params.setFilterMode(GrTextureParams::kBilerp_FilterMode); |
| 1048 | params.setTileModeX(tileMode); |
| 1049 | |
| 1050 | fRow = fAtlas->lockRow(bitmap); |
| 1051 | if (-1 != fRow) { |
| 1052 | fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * |
| 1053 | fAtlas->getVerticalScaleFactor(); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1054 | fCoordTransform.reset(kCoordSet, matrix, fAtlas->getTexture()); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1055 | fTextureAccess.reset(fAtlas->getTexture(), params); |
| 1056 | } else { |
| 1057 | GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶ms); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1058 | fCoordTransform.reset(kCoordSet, matrix, texture); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1059 | fTextureAccess.reset(texture, params); |
| 1060 | fYCoord = SK_ScalarHalf; |
| 1061 | |
| 1062 | // Unlock immediately, this is not great, but we don't have a way of |
| 1063 | // knowing when else to unlock it currently, so it may get purged from |
| 1064 | // the cache, but it'll still be ref'd until it's no longer being used. |
| 1065 | GrUnlockAndUnrefCachedBitmapTexture(texture); |
| 1066 | } |
| 1067 | this->addTextureAccess(&fTextureAccess); |
| 1068 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1069 | this->addCoordTransform(&fCoordTransform); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | GrGradientEffect::~GrGradientEffect() { |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 1073 | if (this->useAtlas()) { |
| 1074 | fAtlas->unlockRow(fRow); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 1075 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 1078 | bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 1079 | const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1080 | |
| 1081 | if (this->fColorType == s.getColorType()){ |
| 1082 | |
| 1083 | if (kTwo_ColorType == fColorType) { |
| 1084 | if (*this->getColors(0) != *s.getColors(0) || |
| 1085 | *this->getColors(1) != *s.getColors(1)) { |
| 1086 | return false; |
| 1087 | } |
| 1088 | } else if (kThree_ColorType == fColorType) { |
| 1089 | if (*this->getColors(0) != *s.getColors(0) || |
| 1090 | *this->getColors(1) != *s.getColors(1) || |
| 1091 | *this->getColors(2) != *s.getColors(2)) { |
| 1092 | return false; |
| 1093 | } |
robertphillips@google.com | c0de5d6 | 2013-10-08 19:15:58 +0000 | [diff] [blame] | 1094 | } else { |
| 1095 | if (fYCoord != s.getYCoord()) { |
| 1096 | return false; |
| 1097 | } |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1098 | } |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 1099 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1100 | return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && |
| 1101 | fTextureAccess.getParams().getTileModeX() == |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1102 | s.fTextureAccess.getParams().getTileModeX() && |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1103 | this->useAtlas() == s.useAtlas() && |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1104 | fCoordTransform.getMatrix().cheapEqualTo(s.fCoordTransform.getMatrix()); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | return false; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 1111 | if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) { |
| 1112 | *validFlags = kA_GrColorComponentFlag; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1113 | } else { |
| 1114 | *validFlags = 0; |
| 1115 | } |
| 1116 | } |
| 1117 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1118 | int GrGradientEffect::RandomGradientParams(SkRandom* random, |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 1119 | SkColor colors[], |
| 1120 | SkScalar** stops, |
| 1121 | SkShader::TileMode* tm) { |
| 1122 | int outColors = random->nextRangeU(1, kMaxRandomGradientColors); |
| 1123 | |
| 1124 | // if one color, omit stops, otherwise randomly decide whether or not to |
| 1125 | if (outColors == 1 || (outColors >= 2 && random->nextBool())) { |
| 1126 | *stops = NULL; |
| 1127 | } |
| 1128 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 1129 | SkScalar stop = 0.f; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 1130 | for (int i = 0; i < outColors; ++i) { |
| 1131 | colors[i] = random->nextU(); |
| 1132 | if (NULL != *stops) { |
| 1133 | (*stops)[i] = stop; |
| 1134 | stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f; |
| 1135 | } |
| 1136 | } |
| 1137 | *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileModeCount)); |
| 1138 | |
| 1139 | return outColors; |
| 1140 | } |
| 1141 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1142 | #endif |