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