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