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