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