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 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 29 | fCache16 = fCache16Storage = NULL; |
| 30 | fCache32 = NULL; |
| 31 | fCache32PixelRef = NULL; |
| 32 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 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) { |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +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 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 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() { |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 189 | if (fCache16Storage) { |
| 190 | sk_free(fCache16Storage); |
| 191 | } |
| 192 | SkSafeUnref(fCache32PixelRef); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 193 | if (fOrigColors != fStorage) { |
| 194 | sk_free(fOrigColors); |
| 195 | } |
| 196 | SkSafeUnref(fMapper); |
| 197 | } |
| 198 | |
| 199 | void SkGradientShaderBase::initCommon() { |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 200 | fFlags = 0; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 223 | SkGradientShaderBase::GpuColorType SkGradientShaderBase::getGpuColorType(SkColor colors[3]) const { |
| 224 | if (fColorCount <= 3) { |
| 225 | memcpy(colors, fOrigColors, fColorCount * sizeof(SkColor)); |
| 226 | } |
| 227 | |
| 228 | if (SkShader::kClamp_TileMode == fTileMode) { |
| 229 | if (2 == fColorCount) { |
| 230 | return kTwo_GpuColorType; |
| 231 | } else if (3 == fColorCount && |
| 232 | (SkScalarAbs( |
| 233 | SkFixedToScalar(fRecs[1].fPos) - SK_ScalarHalf) < SK_Scalar1 / 1000)) { |
| 234 | return kThree_GpuColorType; |
| 235 | } |
| 236 | } |
| 237 | return kTexture_GpuColorType; |
| 238 | } |
| 239 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 240 | bool SkGradientShaderBase::isOpaque() const { |
| 241 | return fColorsAreOpaque; |
| 242 | } |
| 243 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 244 | bool SkGradientShaderBase::setContext(const SkBitmap& device, |
| 245 | const SkPaint& paint, |
| 246 | const SkMatrix& matrix) { |
| 247 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 248 | return false; |
| 249 | } |
| 250 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 251 | const SkMatrix& inverse = this->getTotalInverse(); |
| 252 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 253 | fDstToIndex.setConcat(fPtsToUnit, inverse); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 254 | fDstToIndexProc = fDstToIndex.getMapXYProc(); |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 255 | fDstToIndexClass = (uint8_t)SkShader::ComputeMatrixClass(fDstToIndex); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 256 | |
| 257 | // now convert our colors in to PMColors |
| 258 | unsigned paintAlpha = this->getPaintAlpha(); |
| 259 | |
| 260 | fFlags = this->INHERITED::getFlags(); |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 261 | if (fColorsAreOpaque && paintAlpha == 0xFF) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 262 | fFlags |= kOpaqueAlpha_Flag; |
| 263 | } |
| 264 | // we can do span16 as long as our individual colors are opaque, |
| 265 | // regardless of the paint's alpha |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 266 | if (fColorsAreOpaque) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 267 | fFlags |= kHasSpan16_Flag; |
| 268 | } |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 269 | |
| 270 | this->setCacheAlpha(paintAlpha); |
| 271 | return true; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 272 | } |
| 273 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 274 | void SkGradientShaderBase::setCacheAlpha(U8CPU alpha) const { |
| 275 | // if the new alpha differs from the previous time we were called, inval our cache |
| 276 | // this will trigger the cache to be rebuilt. |
| 277 | // we don't care about the first time, since the cache ptrs will already be NULL |
| 278 | if (fCacheAlpha != alpha) { |
| 279 | fCache16 = NULL; // inval the cache |
| 280 | fCache32 = NULL; // inval the cache |
| 281 | fCacheAlpha = alpha; // record the new alpha |
| 282 | // inform our subclasses |
| 283 | if (fCache32PixelRef) { |
| 284 | fCache32PixelRef->notifyPixelsChanged(); |
| 285 | } |
| 286 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | #define Fixed_To_Dot8(x) (((x) + 0x80) >> 8) |
| 290 | |
| 291 | /** We take the original colors, not our premultiplied PMColors, since we can |
| 292 | build a 16bit table as long as the original colors are opaque, even if the |
| 293 | paint specifies a non-opaque alpha. |
| 294 | */ |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 295 | void SkGradientShaderBase::Build16bitCache(uint16_t cache[], SkColor c0, SkColor c1, |
| 296 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 297 | SkASSERT(count > 1); |
| 298 | SkASSERT(SkColorGetA(c0) == 0xFF); |
| 299 | SkASSERT(SkColorGetA(c1) == 0xFF); |
| 300 | |
| 301 | SkFixed r = SkColorGetR(c0); |
| 302 | SkFixed g = SkColorGetG(c0); |
| 303 | SkFixed b = SkColorGetB(c0); |
| 304 | |
| 305 | SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1); |
| 306 | SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1); |
| 307 | SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1); |
| 308 | |
| 309 | r = SkIntToFixed(r) + 0x8000; |
| 310 | g = SkIntToFixed(g) + 0x8000; |
| 311 | b = SkIntToFixed(b) + 0x8000; |
| 312 | |
| 313 | do { |
| 314 | unsigned rr = r >> 16; |
| 315 | unsigned gg = g >> 16; |
| 316 | unsigned bb = b >> 16; |
| 317 | cache[0] = SkPackRGB16(SkR32ToR16(rr), SkG32ToG16(gg), SkB32ToB16(bb)); |
| 318 | cache[kCache16Count] = SkDitherPack888ToRGB16(rr, gg, bb); |
| 319 | cache += 1; |
| 320 | r += dr; |
| 321 | g += dg; |
| 322 | b += db; |
| 323 | } while (--count != 0); |
| 324 | } |
| 325 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 326 | /* |
| 327 | * r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in |
| 328 | * release builds, we saw a compiler error where the 0xFF parameter in |
| 329 | * SkPackARGB32() was being totally ignored whenever it was called with |
| 330 | * a non-zero add (e.g. 0x8000). |
| 331 | * |
| 332 | * We found two work-arounds: |
| 333 | * 1. change r,g,b to unsigned (or just one of them) |
| 334 | * 2. change SkPackARGB32 to + its (a << SK_A32_SHIFT) value instead |
| 335 | * of using | |
| 336 | * |
| 337 | * We chose #1 just because it was more localized. |
| 338 | * See http://code.google.com/p/skia/issues/detail?id=1113 |
| 339 | * |
| 340 | * The type SkUFixed encapsulate this need for unsigned, but logically Fixed. |
| 341 | */ |
| 342 | typedef uint32_t SkUFixed; |
| 343 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 344 | void SkGradientShaderBase::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1, |
| 345 | int count, U8CPU paintAlpha, uint32_t gradFlags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 346 | SkASSERT(count > 1); |
| 347 | |
| 348 | // need to apply paintAlpha to our two endpoints |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 349 | uint32_t a0 = SkMulDiv255Round(SkColorGetA(c0), paintAlpha); |
| 350 | uint32_t a1 = SkMulDiv255Round(SkColorGetA(c1), paintAlpha); |
| 351 | |
| 352 | |
| 353 | const bool interpInPremul = SkToBool(gradFlags & |
| 354 | SkGradientShader::kInterpolateColorsInPremul_Flag); |
| 355 | |
| 356 | uint32_t r0 = SkColorGetR(c0); |
| 357 | uint32_t g0 = SkColorGetG(c0); |
| 358 | uint32_t b0 = SkColorGetB(c0); |
| 359 | |
| 360 | uint32_t r1 = SkColorGetR(c1); |
| 361 | uint32_t g1 = SkColorGetG(c1); |
| 362 | uint32_t b1 = SkColorGetB(c1); |
| 363 | |
| 364 | if (interpInPremul) { |
| 365 | r0 = SkMulDiv255Round(r0, a0); |
| 366 | g0 = SkMulDiv255Round(g0, a0); |
| 367 | b0 = SkMulDiv255Round(b0, a0); |
| 368 | |
| 369 | r1 = SkMulDiv255Round(r1, a1); |
| 370 | g1 = SkMulDiv255Round(g1, a1); |
| 371 | b1 = SkMulDiv255Round(b1, a1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 372 | } |
| 373 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 374 | SkFixed da = SkIntToFixed(a1 - a0) / (count - 1); |
| 375 | SkFixed dr = SkIntToFixed(r1 - r0) / (count - 1); |
| 376 | SkFixed dg = SkIntToFixed(g1 - g0) / (count - 1); |
| 377 | SkFixed db = SkIntToFixed(b1 - b0) / (count - 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 378 | |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 379 | /* We pre-add 1/8 to avoid having to add this to our [0] value each time |
| 380 | in the loop. Without this, the bias for each would be |
| 381 | 0x2000 0xA000 0xE000 0x6000 |
| 382 | With this trick, we can add 0 for the first (no-op) and just adjust the |
| 383 | others. |
| 384 | */ |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 385 | SkUFixed a = SkIntToFixed(a0) + 0x2000; |
| 386 | SkUFixed r = SkIntToFixed(r0) + 0x2000; |
| 387 | SkUFixed g = SkIntToFixed(g0) + 0x2000; |
| 388 | SkUFixed b = SkIntToFixed(b0) + 0x2000; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 389 | |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 390 | /* |
| 391 | * Our dither-cell (spatially) is |
| 392 | * 0 2 |
| 393 | * 3 1 |
| 394 | * Where |
| 395 | * [0] -> [-1/8 ... 1/8 ) values near 0 |
| 396 | * [1] -> [ 1/8 ... 3/8 ) values near 1/4 |
| 397 | * [2] -> [ 3/8 ... 5/8 ) values near 1/2 |
| 398 | * [3] -> [ 5/8 ... 7/8 ) values near 3/4 |
| 399 | */ |
| 400 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 401 | if (0xFF == a0 && 0 == da) { |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 402 | do { |
| 403 | cache[kCache32Count*0] = SkPackARGB32(0xFF, (r + 0 ) >> 16, |
| 404 | (g + 0 ) >> 16, |
| 405 | (b + 0 ) >> 16); |
| 406 | cache[kCache32Count*1] = SkPackARGB32(0xFF, (r + 0x8000) >> 16, |
| 407 | (g + 0x8000) >> 16, |
| 408 | (b + 0x8000) >> 16); |
| 409 | cache[kCache32Count*2] = SkPackARGB32(0xFF, (r + 0xC000) >> 16, |
| 410 | (g + 0xC000) >> 16, |
| 411 | (b + 0xC000) >> 16); |
| 412 | cache[kCache32Count*3] = SkPackARGB32(0xFF, (r + 0x4000) >> 16, |
| 413 | (g + 0x4000) >> 16, |
| 414 | (b + 0x4000) >> 16); |
| 415 | cache += 1; |
| 416 | r += dr; |
| 417 | g += dg; |
| 418 | b += db; |
| 419 | } while (--count != 0); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 420 | } else if (interpInPremul) { |
| 421 | do { |
| 422 | cache[kCache32Count*0] = SkPackARGB32((a + 0 ) >> 16, |
| 423 | (r + 0 ) >> 16, |
| 424 | (g + 0 ) >> 16, |
| 425 | (b + 0 ) >> 16); |
| 426 | cache[kCache32Count*1] = SkPackARGB32((a + 0x8000) >> 16, |
| 427 | (r + 0x8000) >> 16, |
| 428 | (g + 0x8000) >> 16, |
| 429 | (b + 0x8000) >> 16); |
| 430 | cache[kCache32Count*2] = SkPackARGB32((a + 0xC000) >> 16, |
| 431 | (r + 0xC000) >> 16, |
| 432 | (g + 0xC000) >> 16, |
| 433 | (b + 0xC000) >> 16); |
| 434 | cache[kCache32Count*3] = SkPackARGB32((a + 0x4000) >> 16, |
| 435 | (r + 0x4000) >> 16, |
| 436 | (g + 0x4000) >> 16, |
| 437 | (b + 0x4000) >> 16); |
| 438 | cache += 1; |
| 439 | a += da; |
| 440 | r += dr; |
| 441 | g += dg; |
| 442 | b += db; |
| 443 | } while (--count != 0); |
| 444 | } else { // interpolate in unpreml space |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 445 | do { |
| 446 | cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0 ) >> 16, |
| 447 | (r + 0 ) >> 16, |
| 448 | (g + 0 ) >> 16, |
| 449 | (b + 0 ) >> 16); |
| 450 | cache[kCache32Count*1] = SkPremultiplyARGBInline((a + 0x8000) >> 16, |
| 451 | (r + 0x8000) >> 16, |
| 452 | (g + 0x8000) >> 16, |
| 453 | (b + 0x8000) >> 16); |
| 454 | cache[kCache32Count*2] = SkPremultiplyARGBInline((a + 0xC000) >> 16, |
| 455 | (r + 0xC000) >> 16, |
| 456 | (g + 0xC000) >> 16, |
| 457 | (b + 0xC000) >> 16); |
| 458 | cache[kCache32Count*3] = SkPremultiplyARGBInline((a + 0x4000) >> 16, |
| 459 | (r + 0x4000) >> 16, |
| 460 | (g + 0x4000) >> 16, |
| 461 | (b + 0x4000) >> 16); |
| 462 | cache += 1; |
| 463 | a += da; |
| 464 | r += dr; |
| 465 | g += dg; |
| 466 | b += db; |
| 467 | } while (--count != 0); |
| 468 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static inline int SkFixedToFFFF(SkFixed x) { |
| 472 | SkASSERT((unsigned)x <= SK_Fixed1); |
| 473 | return x - (x >> 16); |
| 474 | } |
| 475 | |
| 476 | static inline U16CPU bitsTo16(unsigned x, const unsigned bits) { |
| 477 | SkASSERT(x < (1U << bits)); |
| 478 | if (6 == bits) { |
| 479 | return (x << 10) | (x << 4) | (x >> 2); |
| 480 | } |
| 481 | if (8 == bits) { |
| 482 | return (x << 8) | x; |
| 483 | } |
| 484 | sk_throw(); |
| 485 | return 0; |
| 486 | } |
| 487 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 488 | const uint16_t* SkGradientShaderBase::getCache16() const { |
| 489 | if (fCache16 == NULL) { |
| 490 | // double the count for dither entries |
| 491 | const int entryCount = kCache16Count * 2; |
| 492 | const size_t allocSize = sizeof(uint16_t) * entryCount; |
| 493 | |
| 494 | if (fCache16Storage == NULL) { // set the storage and our working ptr |
| 495 | fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); |
| 496 | } |
| 497 | fCache16 = fCache16Storage; |
| 498 | if (fColorCount == 2) { |
| 499 | Build16bitCache(fCache16, fOrigColors[0], fOrigColors[1], |
| 500 | kCache16Count); |
| 501 | } else { |
| 502 | Rec* rec = fRecs; |
| 503 | int prevIndex = 0; |
| 504 | for (int i = 1; i < fColorCount; i++) { |
| 505 | int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; |
| 506 | SkASSERT(nextIndex < kCache16Count); |
| 507 | |
| 508 | if (nextIndex > prevIndex) |
| 509 | Build16bitCache(fCache16 + prevIndex, fOrigColors[i-1], fOrigColors[i], nextIndex - prevIndex + 1); |
| 510 | prevIndex = nextIndex; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (fMapper) { |
| 515 | fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); |
| 516 | uint16_t* linear = fCache16; // just computed linear data |
| 517 | uint16_t* mapped = fCache16Storage; // storage for mapped data |
| 518 | SkUnitMapper* map = fMapper; |
| 519 | for (int i = 0; i < kCache16Count; i++) { |
| 520 | int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shift; |
| 521 | mapped[i] = linear[index]; |
| 522 | mapped[i + kCache16Count] = linear[index + kCache16Count]; |
| 523 | } |
| 524 | sk_free(fCache16); |
| 525 | fCache16 = fCache16Storage; |
| 526 | } |
| 527 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 528 | return fCache16; |
| 529 | } |
| 530 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 531 | const SkPMColor* SkGradientShaderBase::getCache32() const { |
| 532 | if (fCache32 == NULL) { |
| 533 | SkImageInfo info; |
| 534 | info.fWidth = kCache32Count; |
| 535 | info.fHeight = 4; // for our 4 dither rows |
| 536 | info.fAlphaType = kPremul_SkAlphaType; |
| 537 | info.fColorType = kN32_SkColorType; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 538 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 539 | if (NULL == fCache32PixelRef) { |
| 540 | fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL); |
| 541 | } |
| 542 | fCache32 = (SkPMColor*)fCache32PixelRef->getAddr(); |
| 543 | if (fColorCount == 2) { |
| 544 | Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1], |
| 545 | kCache32Count, fCacheAlpha, fGradFlags); |
| 546 | } else { |
| 547 | Rec* rec = fRecs; |
| 548 | int prevIndex = 0; |
| 549 | for (int i = 1; i < fColorCount; i++) { |
| 550 | int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; |
| 551 | SkASSERT(nextIndex < kCache32Count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 552 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 553 | if (nextIndex > prevIndex) |
| 554 | Build32bitCache(fCache32 + prevIndex, fOrigColors[i-1], |
| 555 | fOrigColors[i], nextIndex - prevIndex + 1, |
| 556 | fCacheAlpha, fGradFlags); |
| 557 | prevIndex = nextIndex; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (fMapper) { |
| 562 | SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, NULL); |
| 563 | SkPMColor* linear = fCache32; // just computed linear data |
| 564 | SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data |
| 565 | SkUnitMapper* map = fMapper; |
| 566 | for (int i = 0; i < kCache32Count; i++) { |
| 567 | int index = map->mapUnit16((i << 8) | i) >> 8; |
| 568 | mapped[i + kCache32Count*0] = linear[index + kCache32Count*0]; |
| 569 | mapped[i + kCache32Count*1] = linear[index + kCache32Count*1]; |
| 570 | mapped[i + kCache32Count*2] = linear[index + kCache32Count*2]; |
| 571 | mapped[i + kCache32Count*3] = linear[index + kCache32Count*3]; |
| 572 | } |
| 573 | fCache32PixelRef->unref(); |
| 574 | fCache32PixelRef = newPR; |
| 575 | fCache32 = (SkPMColor*)newPR->getAddr(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 576 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 577 | } |
| 578 | return fCache32; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Because our caller might rebuild the same (logically the same) gradient |
| 583 | * over and over, we'd like to return exactly the same "bitmap" if possible, |
| 584 | * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). |
| 585 | * To do that, we maintain a private cache of built-bitmaps, based on our |
| 586 | * colors and positions. Note: we don't try to flatten the fMapper, so if one |
| 587 | * is present, we skip the cache for now. |
| 588 | */ |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 589 | void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 590 | // our caller assumes no external alpha, so we ensure that our cache is |
| 591 | // built with 0xFF |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 592 | this->setCacheAlpha(0xFF); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 593 | |
| 594 | // don't have a way to put the mapper into our cache-key yet |
| 595 | if (fMapper) { |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 596 | // force our cahce32pixelref to be built |
| 597 | (void)this->getCache32(); |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 598 | bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 599 | bitmap->setPixelRef(fCache32PixelRef); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 600 | return; |
| 601 | } |
| 602 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 603 | // build our key: [numColors + colors[] + {positions[]} + flags ] |
| 604 | int count = 1 + fColorCount + 1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 605 | if (fColorCount > 2) { |
| 606 | count += fColorCount - 1; // fRecs[].fPos |
| 607 | } |
| 608 | |
| 609 | SkAutoSTMalloc<16, int32_t> storage(count); |
| 610 | int32_t* buffer = storage.get(); |
| 611 | |
| 612 | *buffer++ = fColorCount; |
| 613 | memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor)); |
| 614 | buffer += fColorCount; |
| 615 | if (fColorCount > 2) { |
| 616 | for (int i = 1; i < fColorCount; i++) { |
| 617 | *buffer++ = fRecs[i].fPos; |
| 618 | } |
| 619 | } |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 620 | *buffer++ = fGradFlags; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 621 | SkASSERT(buffer - storage.get() == count); |
| 622 | |
| 623 | /////////////////////////////////// |
| 624 | |
| 625 | SK_DECLARE_STATIC_MUTEX(gMutex); |
| 626 | static SkBitmapCache* gCache; |
| 627 | // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp |
| 628 | static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; |
| 629 | SkAutoMutexAcquire ama(gMutex); |
| 630 | |
| 631 | if (NULL == gCache) { |
| 632 | gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS)); |
| 633 | } |
| 634 | size_t size = count * sizeof(int32_t); |
| 635 | |
| 636 | if (!gCache->find(storage.get(), size, bitmap)) { |
| 637 | // force our cahce32pixelref to be built |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 638 | (void)this->getCache32(); |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 639 | bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 640 | bitmap->setPixelRef(fCache32PixelRef); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 641 | |
| 642 | gCache->add(storage.get(), size, *bitmap); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const { |
| 647 | if (info) { |
| 648 | if (info->fColorCount >= fColorCount) { |
| 649 | if (info->fColors) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 650 | memcpy(info->fColors, fOrigColors, fColorCount * sizeof(SkColor)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 651 | } |
| 652 | if (info->fColorOffsets) { |
| 653 | if (fColorCount == 2) { |
| 654 | info->fColorOffsets[0] = 0; |
| 655 | info->fColorOffsets[1] = SK_Scalar1; |
| 656 | } else if (fColorCount > 2) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 657 | for (int i = 0; i < fColorCount; ++i) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 658 | info->fColorOffsets[i] = SkFixedToScalar(fRecs[i].fPos); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 659 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | info->fColorCount = fColorCount; |
| 664 | info->fTileMode = fTileMode; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 665 | info->fGradientFlags = fGradFlags; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 669 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 670 | void SkGradientShaderBase::toString(SkString* str) const { |
| 671 | |
| 672 | str->appendf("%d colors: ", fColorCount); |
| 673 | |
| 674 | for (int i = 0; i < fColorCount; ++i) { |
| 675 | str->appendHex(fOrigColors[i]); |
| 676 | if (i < fColorCount-1) { |
| 677 | str->append(", "); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if (fColorCount > 2) { |
| 682 | str->append(" points: ("); |
| 683 | for (int i = 0; i < fColorCount; ++i) { |
| 684 | str->appendScalar(SkFixedToScalar(fRecs[i].fPos)); |
| 685 | if (i < fColorCount-1) { |
| 686 | str->append(", "); |
| 687 | } |
| 688 | } |
| 689 | str->append(")"); |
| 690 | } |
| 691 | |
| 692 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 693 | "clamp", "repeat", "mirror" |
| 694 | }; |
| 695 | |
| 696 | str->append(" "); |
| 697 | str->append(gTileModeName[fTileMode]); |
| 698 | |
| 699 | // TODO: add "fMapper->toString(str);" when SkUnitMapper::toString is added |
| 700 | |
| 701 | this->INHERITED::toString(str); |
| 702 | } |
| 703 | #endif |
| 704 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 705 | /////////////////////////////////////////////////////////////////////////////// |
| 706 | /////////////////////////////////////////////////////////////////////////////// |
| 707 | |
| 708 | #include "SkEmptyShader.h" |
| 709 | |
| 710 | // assumes colors is SkColor* and pos is SkScalar* |
| 711 | #define EXPAND_1_COLOR(count) \ |
| 712 | SkColor tmp[2]; \ |
| 713 | do { \ |
| 714 | if (1 == count) { \ |
| 715 | tmp[0] = tmp[1] = colors[0]; \ |
| 716 | colors = tmp; \ |
| 717 | pos = NULL; \ |
| 718 | count = 2; \ |
| 719 | } \ |
| 720 | } while (0) |
| 721 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 722 | static void desc_init(SkGradientShaderBase::Descriptor* desc, |
| 723 | const SkColor colors[], |
| 724 | const SkScalar pos[], int colorCount, |
| 725 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 726 | SkUnitMapper* mapper, uint32_t flags) { |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 727 | desc->fColors = colors; |
| 728 | desc->fPos = pos; |
| 729 | desc->fCount = colorCount; |
| 730 | desc->fTileMode = mode; |
| 731 | desc->fMapper = mapper; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 732 | desc->fFlags = flags; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 733 | } |
| 734 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 735 | SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], |
| 736 | const SkColor colors[], |
| 737 | const SkScalar pos[], int colorCount, |
| 738 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 739 | SkUnitMapper* mapper, |
| 740 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 741 | if (NULL == pts || NULL == colors || colorCount < 1) { |
| 742 | return NULL; |
| 743 | } |
| 744 | EXPAND_1_COLOR(colorCount); |
| 745 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 746 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 747 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 748 | return SkNEW_ARGS(SkLinearGradient, (pts, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, |
| 752 | const SkColor colors[], |
| 753 | const SkScalar pos[], int colorCount, |
| 754 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 755 | SkUnitMapper* mapper, |
| 756 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 757 | if (radius <= 0 || NULL == colors || colorCount < 1) { |
| 758 | return NULL; |
| 759 | } |
| 760 | EXPAND_1_COLOR(colorCount); |
| 761 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 762 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 763 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 764 | return SkNEW_ARGS(SkRadialGradient, (center, radius, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, |
| 768 | SkScalar startRadius, |
| 769 | const SkPoint& end, |
| 770 | SkScalar endRadius, |
| 771 | const SkColor colors[], |
| 772 | const SkScalar pos[], |
| 773 | int colorCount, |
| 774 | SkShader::TileMode mode, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 775 | SkUnitMapper* mapper, |
| 776 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 777 | if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
| 778 | return NULL; |
| 779 | } |
| 780 | EXPAND_1_COLOR(colorCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 781 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 782 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 783 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 784 | return SkNEW_ARGS(SkTwoPointRadialGradient, |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 785 | (start, startRadius, end, endRadius, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 789 | SkScalar startRadius, |
| 790 | const SkPoint& end, |
| 791 | SkScalar endRadius, |
| 792 | const SkColor colors[], |
| 793 | const SkScalar pos[], |
| 794 | int colorCount, |
| 795 | SkShader::TileMode mode, |
| 796 | SkUnitMapper* mapper, |
| 797 | uint32_t flags) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 798 | if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
| 799 | return NULL; |
| 800 | } |
| 801 | if (start == end && startRadius == endRadius) { |
| 802 | return SkNEW(SkEmptyShader); |
| 803 | } |
rileya@google.com | 1ee7c6a | 2012-07-31 16:00:13 +0000 | [diff] [blame] | 804 | EXPAND_1_COLOR(colorCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 805 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 806 | SkGradientShaderBase::Descriptor desc; |
commit-bot@chromium.org | 7518ff2 | 2014-04-17 13:57:31 +0000 | [diff] [blame] | 807 | desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
| 808 | return SkNEW_ARGS(SkTwoPointConicalGradient, |
| 809 | (start, startRadius, end, endRadius, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, |
| 813 | const SkColor colors[], |
| 814 | const SkScalar pos[], |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 815 | int colorCount, SkUnitMapper* mapper, |
| 816 | uint32_t flags) { |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 817 | if (NULL == colors || colorCount < 1) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 818 | return NULL; |
| 819 | } |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 820 | EXPAND_1_COLOR(colorCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 821 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 822 | SkGradientShaderBase::Descriptor desc; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 823 | desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, mapper, flags); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 824 | return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) |
| 828 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) |
| 829 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) |
| 830 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) |
| 831 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient) |
| 832 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient) |
| 833 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 834 | |
| 835 | /////////////////////////////////////////////////////////////////////////////// |
| 836 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 837 | #if SK_SUPPORT_GPU |
| 838 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 839 | #include "effects/GrTextureStripAtlas.h" |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 840 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 841 | #include "SkGr.h" |
| 842 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 843 | GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory) |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 844 | : INHERITED(factory) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 845 | , fCachedYCoord(SK_ScalarMax) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 846 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 847 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 848 | GrGLGradientEffect::~GrGLGradientEffect() { } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 849 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 850 | void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) { |
| 851 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 852 | if (SkGradientShaderBase::kTwo_GpuColorType == ColorTypeFromKey(key)) { // 2 Color case |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 853 | fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 854 | kVec4f_GrSLType, "GradientStartColor"); |
| 855 | fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 856 | kVec4f_GrSLType, "GradientEndColor"); |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 857 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 858 | } else if (SkGradientShaderBase::kThree_GpuColorType == ColorTypeFromKey(key)){ // 3 Color Case |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 859 | fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 860 | kVec4f_GrSLType, "GradientStartColor"); |
| 861 | fColorMidUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 862 | kVec4f_GrSLType, "GradientMidColor"); |
| 863 | fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 864 | kVec4f_GrSLType, "GradientEndColor"); |
| 865 | |
| 866 | } else { // if not a fast case |
| 867 | fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 868 | kFloat_GrSLType, "GradientYCoordFS"); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | static inline void set_color_uni(const GrGLUniformManager& uman, |
| 873 | const GrGLUniformManager::UniformHandle uni, |
| 874 | const SkColor* color) { |
| 875 | uman.set4f(uni, |
| 876 | SkColorGetR(*color) / 255.f, |
| 877 | SkColorGetG(*color) / 255.f, |
| 878 | SkColorGetB(*color) / 255.f, |
| 879 | SkColorGetA(*color) / 255.f); |
| 880 | } |
| 881 | |
| 882 | static inline void set_mul_color_uni(const GrGLUniformManager& uman, |
| 883 | const GrGLUniformManager::UniformHandle uni, |
| 884 | const SkColor* color){ |
| 885 | float a = SkColorGetA(*color) / 255.f; |
| 886 | float aDiv255 = a / 255.f; |
| 887 | uman.set4f(uni, |
| 888 | SkColorGetR(*color) * aDiv255, |
| 889 | SkColorGetG(*color) * aDiv255, |
| 890 | SkColorGetB(*color) * aDiv255, |
| 891 | a); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 892 | } |
| 893 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 894 | void GrGLGradientEffect::setData(const GrGLUniformManager& uman, |
| 895 | const GrDrawEffect& drawEffect) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 896 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 897 | const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
| 898 | |
| 899 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 900 | if (SkGradientShaderBase::kTwo_GpuColorType == e.getColorType()){ |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 901 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 902 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 903 | set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 904 | set_mul_color_uni(uman, fColorEndUni, e.getColors(1)); |
| 905 | } else { |
| 906 | set_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 907 | set_color_uni(uman, fColorEndUni, e.getColors(1)); |
| 908 | } |
| 909 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 910 | } else if (SkGradientShaderBase::kThree_GpuColorType == e.getColorType()){ |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 911 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 912 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 913 | set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 914 | set_mul_color_uni(uman, fColorMidUni, e.getColors(1)); |
| 915 | set_mul_color_uni(uman, fColorEndUni, e.getColors(2)); |
| 916 | } else { |
| 917 | set_color_uni(uman, fColorStartUni, e.getColors(0)); |
| 918 | set_color_uni(uman, fColorMidUni, e.getColors(1)); |
| 919 | set_color_uni(uman, fColorEndUni, e.getColors(2)); |
| 920 | } |
| 921 | } else { |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 922 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 923 | SkScalar yCoord = e.getYCoord(); |
| 924 | if (yCoord != fCachedYCoord) { |
| 925 | uman.set1f(fFSYUni, yCoord); |
| 926 | fCachedYCoord = yCoord; |
| 927 | } |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 931 | |
| 932 | GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 933 | const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 934 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 935 | EffectKey key = 0; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 936 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 937 | if (SkGradientShaderBase::kTwo_GpuColorType == e.getColorType()) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 938 | key |= kTwoColorKey; |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 939 | } else if (SkGradientShaderBase::kThree_GpuColorType == e.getColorType()){ |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 940 | key |= kThreeColorKey; |
| 941 | } |
| 942 | |
| 943 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 944 | key |= kPremulBeforeInterpKey; |
| 945 | } |
| 946 | |
| 947 | return key; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 948 | } |
| 949 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 950 | void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder, |
| 951 | const char* gradientTValue, |
| 952 | EffectKey key, |
| 953 | const char* outputColor, |
| 954 | const char* inputColor, |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 955 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 956 | if (SkGradientShaderBase::kTwo_GpuColorType == ColorTypeFromKey(key)){ |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 957 | builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1.0));\n", |
| 958 | builder->getUniformVariable(fColorStartUni).c_str(), |
| 959 | builder->getUniformVariable(fColorEndUni).c_str(), |
| 960 | gradientTValue); |
| 961 | // Note that we could skip this step if both colors are known to be opaque. Two |
| 962 | // considerations: |
| 963 | // The gradient SkShader reporting opaque is more restrictive than necessary in the two pt |
| 964 | // case. Make sure the key reflects this optimization (and note that it can use the same |
| 965 | // shader as thekBeforeIterp case). This same optimization applies to the 3 color case below. |
| 966 | if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) { |
| 967 | builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n"); |
| 968 | } |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 969 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 970 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 971 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 972 | } else if (SkGradientShaderBase::kThree_GpuColorType == ColorTypeFromKey(key)){ |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 973 | builder->fsCodeAppendf("\tfloat oneMinus2t = 1.0 - (2.0 * (%s));\n", |
| 974 | gradientTValue); |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 975 | builder->fsCodeAppendf("\tvec4 colorTemp = clamp(oneMinus2t, 0.0, 1.0) * %s;\n", |
| 976 | builder->getUniformVariable(fColorStartUni).c_str()); |
| 977 | if (kTegra3_GrGLRenderer == builder->ctxInfo().renderer()) { |
| 978 | // The Tegra3 compiler will sometimes never return if we have |
| 979 | // min(abs(oneMinus2t), 1.0), or do the abs first in a separate expression. |
| 980 | builder->fsCodeAppend("\tfloat minAbs = abs(oneMinus2t);\n"); |
| 981 | builder->fsCodeAppend("\tminAbs = minAbs > 1.0 ? 1.0 : minAbs;\n"); |
| 982 | builder->fsCodeAppendf("\tcolorTemp += (1.0 - minAbs) * %s;\n", |
| 983 | builder->getUniformVariable(fColorMidUni).c_str()); |
| 984 | } else { |
| 985 | builder->fsCodeAppendf("\tcolorTemp += (1.0 - min(abs(oneMinus2t), 1.0)) * %s;\n", |
| 986 | builder->getUniformVariable(fColorMidUni).c_str()); |
| 987 | } |
| 988 | builder->fsCodeAppendf("\tcolorTemp += clamp(-oneMinus2t, 0.0, 1.0) * %s;\n", |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 989 | builder->getUniformVariable(fColorEndUni).c_str()); |
| 990 | if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) { |
| 991 | builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n"); |
| 992 | } |
| 993 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 994 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 995 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 996 | } else { |
| 997 | builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", |
| 998 | gradientTValue, |
| 999 | builder->getUniformVariable(fFSYUni).c_str()); |
| 1000 | builder->fsCodeAppendf("\t%s = ", outputColor); |
| 1001 | builder->fsAppendTextureLookupAndModulate(inputColor, |
| 1002 | samplers[0], |
| 1003 | "coord"); |
| 1004 | builder->fsCodeAppend(";\n"); |
| 1005 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | ///////////////////////////////////////////////////////////////////// |
| 1009 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1010 | GrGradientEffect::GrGradientEffect(GrContext* ctx, |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 1011 | const SkGradientShaderBase& shader, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1012 | const SkMatrix& matrix, |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 1013 | SkShader::TileMode tileMode) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1014 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 1015 | fIsOpaque = shader.isOpaque(); |
| 1016 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 1017 | fColorType = shader.getGpuColorType(&fColors[0]); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1018 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1019 | // The two and three color specializations do not currently support tiling. |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 1020 | if (SkGradientShaderBase::kTwo_GpuColorType == fColorType || |
| 1021 | SkGradientShaderBase::kThree_GpuColorType == fColorType) { |
| 1022 | fRow = -1; |
skia.committer@gmail.com | 8a777a5 | 2014-04-19 03:04:56 +0000 | [diff] [blame^] | 1023 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 1024 | if (SkGradientShader::kInterpolateColorsInPremul_Flag & shader.getFlags()) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1025 | fPremulType = kBeforeInterp_PremulType; |
| 1026 | } else { |
| 1027 | fPremulType = kAfterInterp_PremulType; |
| 1028 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1029 | fCoordTransform.reset(kCoordSet, matrix); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1030 | } else { |
| 1031 | // doesn't matter how this is set, just be consistent because it is part of the effect key. |
| 1032 | fPremulType = kBeforeInterp_PremulType; |
| 1033 | SkBitmap bitmap; |
| 1034 | shader.getGradientTableBitmap(&bitmap); |
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 | |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 1083 | if (SkGradientShaderBase::kTwo_GpuColorType == fColorType) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1084 | if (*this->getColors(0) != *s.getColors(0) || |
| 1085 | *this->getColors(1) != *s.getColors(1)) { |
| 1086 | return false; |
| 1087 | } |
commit-bot@chromium.org | 996402b | 2014-04-18 14:42:11 +0000 | [diff] [blame] | 1088 | } else if (SkGradientShaderBase::kThree_GpuColorType == fColorType) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 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 |