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" |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 9 | #include "SkColorSpace_XYZ.h" |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 10 | #include "SkGradientShaderPriv.h" |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 11 | #include "SkHalf.h" |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 12 | #include "SkLinearGradient.h" |
| 13 | #include "SkRadialGradient.h" |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 14 | #include "SkTwoPointConicalGradient.h" |
| 15 | #include "SkSweepGradient.h" |
| 16 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 17 | enum GradientSerializationFlags { |
| 18 | // Bits 29:31 used for various boolean flags |
| 19 | kHasPosition_GSF = 0x80000000, |
| 20 | kHasLocalMatrix_GSF = 0x40000000, |
| 21 | kHasColorSpace_GSF = 0x20000000, |
| 22 | |
| 23 | // Bits 12:28 unused |
| 24 | |
| 25 | // Bits 8:11 for fTileMode |
| 26 | kTileModeShift_GSF = 8, |
| 27 | kTileModeMask_GSF = 0xF, |
| 28 | |
| 29 | // Bits 0:7 for fGradFlags (note that kForce4fContext_PrivateFlag is 0x80) |
| 30 | kGradFlagsShift_GSF = 0, |
| 31 | kGradFlagsMask_GSF = 0xFF, |
| 32 | }; |
| 33 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 34 | void SkGradientShaderBase::Descriptor::flatten(SkWriteBuffer& buffer) const { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 35 | uint32_t flags = 0; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 36 | if (fPos) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 37 | flags |= kHasPosition_GSF; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 38 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 39 | if (fLocalMatrix) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 40 | flags |= kHasLocalMatrix_GSF; |
| 41 | } |
| 42 | sk_sp<SkData> colorSpaceData = fColorSpace ? fColorSpace->serialize() : nullptr; |
| 43 | if (colorSpaceData) { |
| 44 | flags |= kHasColorSpace_GSF; |
| 45 | } |
| 46 | SkASSERT(static_cast<uint32_t>(fTileMode) <= kTileModeMask_GSF); |
| 47 | flags |= (fTileMode << kTileModeShift_GSF); |
| 48 | SkASSERT(fGradFlags <= kGradFlagsMask_GSF); |
| 49 | flags |= (fGradFlags << kGradFlagsShift_GSF); |
| 50 | |
| 51 | buffer.writeUInt(flags); |
| 52 | |
| 53 | buffer.writeColor4fArray(fColors, fCount); |
| 54 | if (colorSpaceData) { |
| 55 | buffer.writeDataAsByteArray(colorSpaceData.get()); |
| 56 | } |
| 57 | if (fPos) { |
| 58 | buffer.writeScalarArray(fPos, fCount); |
| 59 | } |
| 60 | if (fLocalMatrix) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 61 | buffer.writeMatrix(*fLocalMatrix); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | bool SkGradientShaderBase::DescriptorScope::unflatten(SkReadBuffer& buffer) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 66 | if (buffer.isVersionLT(SkReadBuffer::kGradientShaderFloatColor_Version)) { |
| 67 | fCount = buffer.getArrayCount(); |
| 68 | if (fCount > kStorageCount) { |
| 69 | size_t allocSize = (sizeof(SkColor4f) + sizeof(SkScalar)) * fCount; |
| 70 | fDynamicStorage.reset(allocSize); |
| 71 | fColors = (SkColor4f*)fDynamicStorage.get(); |
| 72 | fPos = (SkScalar*)(fColors + fCount); |
| 73 | } else { |
| 74 | fColors = fColorStorage; |
| 75 | fPos = fPosStorage; |
| 76 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 77 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 78 | // Old gradients serialized SkColor. Read that to a temporary location, then convert. |
| 79 | SkSTArray<2, SkColor, true> colors; |
| 80 | colors.resize_back(fCount); |
| 81 | if (!buffer.readColorArray(colors.begin(), fCount)) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 84 | for (int i = 0; i < fCount; ++i) { |
| 85 | mutableColors()[i] = SkColor4f::FromColor(colors[i]); |
| 86 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 87 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 88 | if (buffer.readBool()) { |
| 89 | if (!buffer.readScalarArray(const_cast<SkScalar*>(fPos), fCount)) { |
| 90 | return false; |
| 91 | } |
| 92 | } else { |
| 93 | fPos = nullptr; |
| 94 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 95 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 96 | fColorSpace = nullptr; |
| 97 | fTileMode = (SkShader::TileMode)buffer.read32(); |
| 98 | fGradFlags = buffer.read32(); |
| 99 | |
| 100 | if (buffer.readBool()) { |
| 101 | fLocalMatrix = &fLocalMatrixStorage; |
| 102 | buffer.readMatrix(&fLocalMatrixStorage); |
| 103 | } else { |
| 104 | fLocalMatrix = nullptr; |
| 105 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 106 | } else { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 107 | // New gradient format. Includes floating point color, color space, densely packed flags |
| 108 | uint32_t flags = buffer.readUInt(); |
| 109 | |
| 110 | fTileMode = (SkShader::TileMode)((flags >> kTileModeShift_GSF) & kTileModeMask_GSF); |
| 111 | fGradFlags = (flags >> kGradFlagsShift_GSF) & kGradFlagsMask_GSF; |
| 112 | |
| 113 | fCount = buffer.getArrayCount(); |
| 114 | if (fCount > kStorageCount) { |
| 115 | size_t allocSize = (sizeof(SkColor4f) + sizeof(SkScalar)) * fCount; |
| 116 | fDynamicStorage.reset(allocSize); |
| 117 | fColors = (SkColor4f*)fDynamicStorage.get(); |
| 118 | fPos = (SkScalar*)(fColors + fCount); |
| 119 | } else { |
| 120 | fColors = fColorStorage; |
| 121 | fPos = fPosStorage; |
| 122 | } |
| 123 | if (!buffer.readColor4fArray(mutableColors(), fCount)) { |
| 124 | return false; |
| 125 | } |
| 126 | if (SkToBool(flags & kHasColorSpace_GSF)) { |
| 127 | sk_sp<SkData> data = buffer.readByteArrayAsData(); |
| 128 | fColorSpace = SkColorSpace::Deserialize(data->data(), data->size()); |
| 129 | } else { |
| 130 | fColorSpace = nullptr; |
| 131 | } |
| 132 | if (SkToBool(flags & kHasPosition_GSF)) { |
| 133 | if (!buffer.readScalarArray(mutablePos(), fCount)) { |
| 134 | return false; |
| 135 | } |
| 136 | } else { |
| 137 | fPos = nullptr; |
| 138 | } |
| 139 | if (SkToBool(flags & kHasLocalMatrix_GSF)) { |
| 140 | fLocalMatrix = &fLocalMatrixStorage; |
| 141 | buffer.readMatrix(&fLocalMatrixStorage); |
| 142 | } else { |
| 143 | fLocalMatrix = nullptr; |
| 144 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 145 | } |
| 146 | return buffer.isValid(); |
| 147 | } |
| 148 | |
| 149 | //////////////////////////////////////////////////////////////////////////////////////////// |
| 150 | |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 151 | SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatrix& ptsToUnit) |
reed | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 152 | : INHERITED(desc.fLocalMatrix) |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 153 | , fPtsToUnit(ptsToUnit) |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 154 | { |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 155 | fPtsToUnit.getType(); // Precache so reads are threadsafe. |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 156 | SkASSERT(desc.fCount > 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 157 | |
fmalita | 6d7e4e8 | 2016-09-20 06:55:16 -0700 | [diff] [blame] | 158 | fGradFlags = static_cast<uint8_t>(desc.fGradFlags); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 159 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 160 | SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 161 | SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs)); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 162 | fTileMode = desc.fTileMode; |
| 163 | fTileProc = gTileProcs[desc.fTileMode]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 164 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 165 | /* Note: we let the caller skip the first and/or last position. |
| 166 | i.e. pos[0] = 0.3, pos[1] = 0.7 |
| 167 | In these cases, we insert dummy entries to ensure that the final data |
| 168 | will be bracketed by [0, 1]. |
| 169 | i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1 |
| 170 | |
| 171 | Thus colorCount (the caller's value, and fColorCount (our value) may |
| 172 | differ by up to 2. In the above example: |
| 173 | colorCount = 2 |
| 174 | fColorCount = 4 |
| 175 | */ |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 176 | fColorCount = desc.fCount; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 177 | // check if we need to add in dummy start and/or end position/colors |
| 178 | bool dummyFirst = false; |
| 179 | bool dummyLast = false; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 180 | if (desc.fPos) { |
| 181 | dummyFirst = desc.fPos[0] != 0; |
| 182 | dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 183 | fColorCount += dummyFirst + dummyLast; |
| 184 | } |
| 185 | |
| 186 | if (fColorCount > kColorStorageCount) { |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 187 | size_t size = sizeof(SkColor) + sizeof(SkColor4f) + sizeof(Rec); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 188 | if (desc.fPos) { |
| 189 | size += sizeof(SkScalar); |
| 190 | } |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 191 | fOrigColors = reinterpret_cast<SkColor*>(sk_malloc_throw(size * fColorCount)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 192 | } |
| 193 | else { |
| 194 | fOrigColors = fStorage; |
| 195 | } |
| 196 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 197 | fOrigColors4f = (SkColor4f*)(fOrigColors + fColorCount); |
| 198 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 199 | // Now copy over the colors, adding the dummies as needed |
| 200 | SkColor4f* origColors = fOrigColors4f; |
| 201 | if (dummyFirst) { |
| 202 | *origColors++ = desc.fColors[0]; |
| 203 | } |
| 204 | memcpy(origColors, desc.fColors, desc.fCount * sizeof(SkColor4f)); |
| 205 | if (dummyLast) { |
| 206 | origColors += desc.fCount; |
| 207 | *origColors = desc.fColors[desc.fCount - 1]; |
| 208 | } |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 209 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 210 | // Convert our SkColor4f colors to SkColor as well. Note that this is incorrect if the |
| 211 | // source colors are not in sRGB gamut. We would need to do a gamut transformation, but |
| 212 | // SkColorSpaceXform can't do that (yet). GrColorSpaceXform can, but we may not have GPU |
| 213 | // support compiled in here. For the common case (sRGB colors), this does the right thing. |
| 214 | for (int i = 0; i < fColorCount; ++i) { |
| 215 | fOrigColors[i] = fOrigColors4f[i].toSkColor(); |
| 216 | } |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 217 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 218 | if (!desc.fColorSpace) { |
| 219 | // This happens if we were constructed from SkColors, so our colors are really sRGB |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 220 | fColorSpace = SkColorSpace::MakeSRGBLinear(); |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 221 | } else { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 222 | // The color space refers to the float colors, so it must be linear gamma |
| 223 | SkASSERT(desc.fColorSpace->gammaIsLinear()); |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 224 | fColorSpace = desc.fColorSpace; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 225 | } |
| 226 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 227 | if (desc.fPos && fColorCount) { |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 228 | fOrigPos = (SkScalar*)(fOrigColors4f + fColorCount); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 229 | fRecs = (Rec*)(fOrigPos + fColorCount); |
| 230 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 231 | fOrigPos = nullptr; |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 232 | fRecs = (Rec*)(fOrigColors4f + fColorCount); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 233 | } |
| 234 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 235 | if (fColorCount > 2) { |
| 236 | Rec* recs = fRecs; |
| 237 | recs->fPos = 0; |
| 238 | // recs->fScale = 0; // unused; |
| 239 | recs += 1; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 240 | if (desc.fPos) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 241 | SkScalar* origPosPtr = fOrigPos; |
| 242 | *origPosPtr++ = 0; |
| 243 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 244 | /* We need to convert the user's array of relative positions into |
| 245 | fixed-point positions and scale factors. We need these results |
| 246 | to be strictly monotonic (no two values equal or out of order). |
| 247 | Hence this complex loop that just jams a zero for the scale |
| 248 | value if it sees a segment out of order, and it assures that |
| 249 | we start at 0 and end at 1.0 |
| 250 | */ |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 251 | SkScalar prev = 0; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 252 | int startIndex = dummyFirst ? 0 : 1; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 253 | int count = desc.fCount + dummyLast; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 254 | for (int i = startIndex; i < count; i++) { |
| 255 | // force the last value to be 1.0 |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 256 | SkScalar curr; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 257 | if (i == desc.fCount) { // we're really at the dummyLast |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 258 | curr = 1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 259 | } else { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 260 | curr = SkScalarPin(desc.fPos[i], 0, 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 261 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 262 | *origPosPtr++ = curr; |
mtklein | 55de40b | 2014-08-21 11:52:36 -0700 | [diff] [blame] | 263 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 264 | recs->fPos = SkScalarToFixed(curr); |
mtklein | 55de40b | 2014-08-21 11:52:36 -0700 | [diff] [blame] | 265 | SkFixed diff = SkScalarToFixed(curr - prev); |
| 266 | if (diff > 0) { |
| 267 | recs->fScale = (1 << 24) / diff; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 268 | } else { |
| 269 | recs->fScale = 0; // ignore this segment |
| 270 | } |
| 271 | // get ready for the next value |
| 272 | prev = curr; |
| 273 | recs += 1; |
| 274 | } |
| 275 | } else { // assume even distribution |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 276 | fOrigPos = nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 277 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 278 | SkFixed dp = SK_Fixed1 / (desc.fCount - 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 279 | SkFixed p = dp; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 280 | SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 281 | for (int i = 1; i < desc.fCount - 1; i++) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 282 | recs->fPos = p; |
| 283 | recs->fScale = scale; |
| 284 | recs += 1; |
| 285 | p += dp; |
| 286 | } |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 287 | recs->fPos = SK_Fixed1; |
commit-bot@chromium.org | 811f20d | 2014-04-21 21:09:49 +0000 | [diff] [blame] | 288 | recs->fScale = scale; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 289 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 290 | } else if (desc.fPos) { |
| 291 | SkASSERT(2 == fColorCount); |
| 292 | fOrigPos[0] = SkScalarPin(desc.fPos[0], 0, 1); |
| 293 | fOrigPos[1] = SkScalarPin(desc.fPos[1], fOrigPos[0], 1); |
| 294 | if (0 == fOrigPos[0] && 1 == fOrigPos[1]) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 295 | fOrigPos = nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 296 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 297 | } |
| 298 | this->initCommon(); |
| 299 | } |
| 300 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 301 | SkGradientShaderBase::~SkGradientShaderBase() { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 302 | if (fOrigColors != fStorage) { |
| 303 | sk_free(fOrigColors); |
| 304 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void SkGradientShaderBase::initCommon() { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 308 | unsigned colorAlpha = 0xFF; |
| 309 | for (int i = 0; i < fColorCount; i++) { |
| 310 | colorAlpha &= SkColorGetA(fOrigColors[i]); |
| 311 | } |
| 312 | fColorsAreOpaque = colorAlpha == 0xFF; |
| 313 | } |
| 314 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 315 | void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 316 | Descriptor desc; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 317 | desc.fColors = fOrigColors4f; |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 318 | desc.fColorSpace = fColorSpace; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 319 | desc.fPos = fOrigPos; |
| 320 | desc.fCount = fColorCount; |
| 321 | desc.fTileMode = fTileMode; |
| 322 | desc.fGradFlags = fGradFlags; |
| 323 | |
| 324 | const SkMatrix& m = this->getLocalMatrix(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 325 | desc.fLocalMatrix = m.isIdentity() ? nullptr : &m; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 326 | desc.flatten(buffer); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 327 | } |
| 328 | |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 329 | void SkGradientShaderBase::FlipGradientColors(SkColor* colorDst, Rec* recDst, |
| 330 | SkColor* colorSrc, Rec* recSrc, |
| 331 | int count) { |
| 332 | SkAutoSTArray<8, SkColor> colorsTemp(count); |
| 333 | for (int i = 0; i < count; ++i) { |
| 334 | int offset = count - i - 1; |
| 335 | colorsTemp[i] = colorSrc[offset]; |
| 336 | } |
| 337 | if (count > 2) { |
| 338 | SkAutoSTArray<8, Rec> recsTemp(count); |
| 339 | for (int i = 0; i < count; ++i) { |
| 340 | int offset = count - i - 1; |
| 341 | recsTemp[i].fPos = SK_Fixed1 - recSrc[offset].fPos; |
| 342 | recsTemp[i].fScale = recSrc[offset].fScale; |
| 343 | } |
| 344 | memcpy(recDst, recsTemp.get(), count * sizeof(Rec)); |
| 345 | } |
| 346 | memcpy(colorDst, colorsTemp.get(), count * sizeof(SkColor)); |
| 347 | } |
| 348 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 349 | bool SkGradientShaderBase::isOpaque() const { |
| 350 | return fColorsAreOpaque; |
| 351 | } |
| 352 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 353 | static unsigned rounded_divide(unsigned numer, unsigned denom) { |
| 354 | return (numer + (denom >> 1)) / denom; |
| 355 | } |
| 356 | |
| 357 | bool SkGradientShaderBase::onAsLuminanceColor(SkColor* lum) const { |
| 358 | // we just compute an average color. |
| 359 | // possibly we could weight this based on the proportional width for each color |
| 360 | // assuming they are not evenly distributed in the fPos array. |
| 361 | int r = 0; |
| 362 | int g = 0; |
| 363 | int b = 0; |
| 364 | const int n = fColorCount; |
| 365 | for (int i = 0; i < n; ++i) { |
| 366 | SkColor c = fOrigColors[i]; |
| 367 | r += SkColorGetR(c); |
| 368 | g += SkColorGetG(c); |
| 369 | b += SkColorGetB(c); |
| 370 | } |
| 371 | *lum = SkColorSetRGB(rounded_divide(r, n), rounded_divide(g, n), rounded_divide(b, n)); |
| 372 | return true; |
| 373 | } |
| 374 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 375 | SkGradientShaderBase::GradientShaderBaseContext::GradientShaderBaseContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 376 | const SkGradientShaderBase& shader, const ContextRec& rec) |
| 377 | : INHERITED(shader, rec) |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 378 | #ifdef SK_SUPPORT_LEGACY_GRADIENT_DITHERING |
| 379 | , fDither(true) |
| 380 | #else |
| 381 | , fDither(rec.fPaint->isDither()) |
| 382 | #endif |
| 383 | , fCache(shader.refCache(getPaintAlpha(), fDither)) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 384 | { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 385 | const SkMatrix& inverse = this->getTotalInverse(); |
| 386 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 387 | fDstToIndex.setConcat(shader.fPtsToUnit, inverse); |
| 388 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 389 | fDstToIndexProc = fDstToIndex.getMapXYProc(); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 390 | fDstToIndexClass = (uint8_t)SkShader::Context::ComputeMatrixClass(fDstToIndex); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 391 | |
| 392 | // now convert our colors in to PMColors |
| 393 | unsigned paintAlpha = this->getPaintAlpha(); |
| 394 | |
| 395 | fFlags = this->INHERITED::getFlags(); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 396 | if (shader.fColorsAreOpaque && paintAlpha == 0xFF) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 397 | fFlags |= kOpaqueAlpha_Flag; |
| 398 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 399 | } |
| 400 | |
fmalita | 088e21b | 2016-10-05 09:28:42 -0700 | [diff] [blame] | 401 | bool SkGradientShaderBase::GradientShaderBaseContext::isValid() const { |
| 402 | return fDstToIndex.isFinite(); |
| 403 | } |
| 404 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 405 | SkGradientShaderBase::GradientShaderCache::GradientShaderCache( |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 406 | U8CPU alpha, bool dither, const SkGradientShaderBase& shader) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 407 | : fCacheAlpha(alpha) |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 408 | , fCacheDither(dither) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 409 | , fShader(shader) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 410 | { |
brianosman | 93110a8 | 2016-09-15 08:40:21 -0700 | [diff] [blame] | 411 | // Only initialize the cache in getCache32. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 412 | fCache32 = nullptr; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 413 | fCache32PixelRef = nullptr; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | SkGradientShaderBase::GradientShaderCache::~GradientShaderCache() { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 417 | SkSafeUnref(fCache32PixelRef); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 418 | } |
| 419 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 420 | /* |
| 421 | * r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in |
| 422 | * release builds, we saw a compiler error where the 0xFF parameter in |
| 423 | * SkPackARGB32() was being totally ignored whenever it was called with |
| 424 | * a non-zero add (e.g. 0x8000). |
| 425 | * |
| 426 | * We found two work-arounds: |
| 427 | * 1. change r,g,b to unsigned (or just one of them) |
| 428 | * 2. change SkPackARGB32 to + its (a << SK_A32_SHIFT) value instead |
| 429 | * of using | |
| 430 | * |
| 431 | * We chose #1 just because it was more localized. |
| 432 | * See http://code.google.com/p/skia/issues/detail?id=1113 |
| 433 | * |
| 434 | * The type SkUFixed encapsulate this need for unsigned, but logically Fixed. |
| 435 | */ |
| 436 | typedef uint32_t SkUFixed; |
| 437 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 438 | void SkGradientShaderBase::GradientShaderCache::Build32bitCache( |
| 439 | SkPMColor cache[], SkColor c0, SkColor c1, |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 440 | int count, U8CPU paintAlpha, uint32_t gradFlags, bool dither) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 441 | SkASSERT(count > 1); |
| 442 | |
| 443 | // need to apply paintAlpha to our two endpoints |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 444 | uint32_t a0 = SkMulDiv255Round(SkColorGetA(c0), paintAlpha); |
| 445 | uint32_t a1 = SkMulDiv255Round(SkColorGetA(c1), paintAlpha); |
| 446 | |
| 447 | |
| 448 | const bool interpInPremul = SkToBool(gradFlags & |
| 449 | SkGradientShader::kInterpolateColorsInPremul_Flag); |
| 450 | |
| 451 | uint32_t r0 = SkColorGetR(c0); |
| 452 | uint32_t g0 = SkColorGetG(c0); |
| 453 | uint32_t b0 = SkColorGetB(c0); |
| 454 | |
| 455 | uint32_t r1 = SkColorGetR(c1); |
| 456 | uint32_t g1 = SkColorGetG(c1); |
| 457 | uint32_t b1 = SkColorGetB(c1); |
| 458 | |
| 459 | if (interpInPremul) { |
| 460 | r0 = SkMulDiv255Round(r0, a0); |
| 461 | g0 = SkMulDiv255Round(g0, a0); |
| 462 | b0 = SkMulDiv255Round(b0, a0); |
| 463 | |
| 464 | r1 = SkMulDiv255Round(r1, a1); |
| 465 | g1 = SkMulDiv255Round(g1, a1); |
| 466 | b1 = SkMulDiv255Round(b1, a1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 467 | } |
| 468 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 469 | SkFixed da = SkIntToFixed(a1 - a0) / (count - 1); |
| 470 | SkFixed dr = SkIntToFixed(r1 - r0) / (count - 1); |
| 471 | SkFixed dg = SkIntToFixed(g1 - g0) / (count - 1); |
| 472 | SkFixed db = SkIntToFixed(b1 - b0) / (count - 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 473 | |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 474 | /* We pre-add 1/8 to avoid having to add this to our [0] value each time |
| 475 | in the loop. Without this, the bias for each would be |
| 476 | 0x2000 0xA000 0xE000 0x6000 |
| 477 | With this trick, we can add 0 for the first (no-op) and just adjust the |
| 478 | others. |
| 479 | */ |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 480 | const SkUFixed bias0 = dither ? 0x2000 : 0x8000; |
| 481 | const SkUFixed bias1 = dither ? 0x8000 : 0; |
| 482 | const SkUFixed bias2 = dither ? 0xC000 : 0; |
| 483 | const SkUFixed bias3 = dither ? 0x4000 : 0; |
| 484 | |
| 485 | SkUFixed a = SkIntToFixed(a0) + bias0; |
| 486 | SkUFixed r = SkIntToFixed(r0) + bias0; |
| 487 | SkUFixed g = SkIntToFixed(g0) + bias0; |
| 488 | SkUFixed b = SkIntToFixed(b0) + bias0; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 489 | |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 490 | /* |
| 491 | * Our dither-cell (spatially) is |
| 492 | * 0 2 |
| 493 | * 3 1 |
| 494 | * Where |
| 495 | * [0] -> [-1/8 ... 1/8 ) values near 0 |
| 496 | * [1] -> [ 1/8 ... 3/8 ) values near 1/4 |
| 497 | * [2] -> [ 3/8 ... 5/8 ) values near 1/2 |
| 498 | * [3] -> [ 5/8 ... 7/8 ) values near 3/4 |
| 499 | */ |
| 500 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 501 | if (0xFF == a0 && 0 == da) { |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 502 | do { |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 503 | cache[kCache32Count*0] = SkPackARGB32(0xFF, (r + 0 ) >> 16, |
| 504 | (g + 0 ) >> 16, |
| 505 | (b + 0 ) >> 16); |
| 506 | cache[kCache32Count*1] = SkPackARGB32(0xFF, (r + bias1) >> 16, |
| 507 | (g + bias1) >> 16, |
| 508 | (b + bias1) >> 16); |
| 509 | cache[kCache32Count*2] = SkPackARGB32(0xFF, (r + bias2) >> 16, |
| 510 | (g + bias2) >> 16, |
| 511 | (b + bias2) >> 16); |
| 512 | cache[kCache32Count*3] = SkPackARGB32(0xFF, (r + bias3) >> 16, |
| 513 | (g + bias3) >> 16, |
| 514 | (b + bias3) >> 16); |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 515 | cache += 1; |
| 516 | r += dr; |
| 517 | g += dg; |
| 518 | b += db; |
| 519 | } while (--count != 0); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 520 | } else if (interpInPremul) { |
| 521 | do { |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 522 | cache[kCache32Count*0] = SkPackARGB32((a + 0 ) >> 16, |
| 523 | (r + 0 ) >> 16, |
| 524 | (g + 0 ) >> 16, |
| 525 | (b + 0 ) >> 16); |
| 526 | cache[kCache32Count*1] = SkPackARGB32((a + bias1) >> 16, |
| 527 | (r + bias1) >> 16, |
| 528 | (g + bias1) >> 16, |
| 529 | (b + bias1) >> 16); |
| 530 | cache[kCache32Count*2] = SkPackARGB32((a + bias2) >> 16, |
| 531 | (r + bias2) >> 16, |
| 532 | (g + bias2) >> 16, |
| 533 | (b + bias2) >> 16); |
| 534 | cache[kCache32Count*3] = SkPackARGB32((a + bias3) >> 16, |
| 535 | (r + bias3) >> 16, |
| 536 | (g + bias3) >> 16, |
| 537 | (b + bias3) >> 16); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 538 | cache += 1; |
| 539 | a += da; |
| 540 | r += dr; |
| 541 | g += dg; |
| 542 | b += db; |
| 543 | } while (--count != 0); |
| 544 | } else { // interpolate in unpreml space |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 545 | do { |
| 546 | cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0 ) >> 16, |
| 547 | (r + 0 ) >> 16, |
| 548 | (g + 0 ) >> 16, |
| 549 | (b + 0 ) >> 16); |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 550 | cache[kCache32Count*1] = SkPremultiplyARGBInline((a + bias1) >> 16, |
| 551 | (r + bias1) >> 16, |
| 552 | (g + bias1) >> 16, |
| 553 | (b + bias1) >> 16); |
| 554 | cache[kCache32Count*2] = SkPremultiplyARGBInline((a + bias2) >> 16, |
| 555 | (r + bias2) >> 16, |
| 556 | (g + bias2) >> 16, |
| 557 | (b + bias2) >> 16); |
| 558 | cache[kCache32Count*3] = SkPremultiplyARGBInline((a + bias3) >> 16, |
| 559 | (r + bias3) >> 16, |
| 560 | (g + bias3) >> 16, |
| 561 | (b + bias3) >> 16); |
reed@google.com | d8e0d6a | 2013-02-08 19:48:12 +0000 | [diff] [blame] | 562 | cache += 1; |
| 563 | a += da; |
| 564 | r += dr; |
| 565 | g += dg; |
| 566 | b += db; |
| 567 | } while (--count != 0); |
| 568 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | static inline int SkFixedToFFFF(SkFixed x) { |
| 572 | SkASSERT((unsigned)x <= SK_Fixed1); |
| 573 | return x - (x >> 16); |
| 574 | } |
| 575 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 576 | const SkPMColor* SkGradientShaderBase::GradientShaderCache::getCache32() { |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 577 | fCache32InitOnce(SkGradientShaderBase::GradientShaderCache::initCache32, this); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 578 | SkASSERT(fCache32); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 579 | return fCache32; |
| 580 | } |
| 581 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 582 | void SkGradientShaderBase::GradientShaderCache::initCache32(GradientShaderCache* cache) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 583 | const int kNumberOfDitherRows = 4; |
| 584 | const SkImageInfo info = SkImageInfo::MakeN32Premul(kCache32Count, kNumberOfDitherRows); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 585 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 586 | SkASSERT(nullptr == cache->fCache32PixelRef); |
| 587 | cache->fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, nullptr); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 588 | cache->fCache32 = (SkPMColor*)cache->fCache32PixelRef->getAddr(); |
| 589 | if (cache->fShader.fColorCount == 2) { |
| 590 | Build32bitCache(cache->fCache32, cache->fShader.fOrigColors[0], |
| 591 | cache->fShader.fOrigColors[1], kCache32Count, cache->fCacheAlpha, |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 592 | cache->fShader.fGradFlags, cache->fCacheDither); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 593 | } else { |
| 594 | Rec* rec = cache->fShader.fRecs; |
| 595 | int prevIndex = 0; |
| 596 | for (int i = 1; i < cache->fShader.fColorCount; i++) { |
| 597 | int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; |
| 598 | SkASSERT(nextIndex < kCache32Count); |
| 599 | |
| 600 | if (nextIndex > prevIndex) |
| 601 | Build32bitCache(cache->fCache32 + prevIndex, cache->fShader.fOrigColors[i-1], |
| 602 | cache->fShader.fOrigColors[i], nextIndex - prevIndex + 1, |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 603 | cache->fCacheAlpha, cache->fShader.fGradFlags, cache->fCacheDither); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 604 | prevIndex = nextIndex; |
| 605 | } |
| 606 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 607 | } |
| 608 | |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 609 | void SkGradientShaderBase::initLinearBitmap(SkBitmap* bitmap) const { |
| 610 | const bool interpInPremul = SkToBool(fGradFlags & |
| 611 | SkGradientShader::kInterpolateColorsInPremul_Flag); |
| 612 | bitmap->lockPixels(); |
| 613 | SkHalf* pixelsF16 = reinterpret_cast<SkHalf*>(bitmap->getPixels()); |
| 614 | uint32_t* pixelsS32 = reinterpret_cast<uint32_t*>(bitmap->getPixels()); |
| 615 | |
| 616 | typedef std::function<void(const Sk4f&, int)> pixelWriteFn_t; |
| 617 | |
| 618 | pixelWriteFn_t writeF16Pixel = [&](const Sk4f& x, int index) { |
| 619 | Sk4h c = SkFloatToHalf_finite_ftz(x); |
| 620 | pixelsF16[4*index+0] = c[0]; |
| 621 | pixelsF16[4*index+1] = c[1]; |
| 622 | pixelsF16[4*index+2] = c[2]; |
| 623 | pixelsF16[4*index+3] = c[3]; |
| 624 | }; |
| 625 | pixelWriteFn_t writeS32Pixel = [&](const Sk4f& c, int index) { |
| 626 | pixelsS32[index] = Sk4f_toS32(c); |
| 627 | }; |
| 628 | |
| 629 | pixelWriteFn_t writeSizedPixel = |
| 630 | (kRGBA_F16_SkColorType == bitmap->colorType()) ? writeF16Pixel : writeS32Pixel; |
| 631 | pixelWriteFn_t writeUnpremulPixel = [&](const Sk4f& c, int index) { |
| 632 | writeSizedPixel(c * Sk4f(c[3], c[3], c[3], 1.0f), index); |
| 633 | }; |
| 634 | |
| 635 | pixelWriteFn_t writePixel = interpInPremul ? writeSizedPixel : writeUnpremulPixel; |
| 636 | |
| 637 | int prevIndex = 0; |
| 638 | for (int i = 1; i < fColorCount; i++) { |
| 639 | int nextIndex = (fColorCount == 2) ? (kCache32Count - 1) |
| 640 | : SkFixedToFFFF(fRecs[i].fPos) >> kCache32Shift; |
| 641 | SkASSERT(nextIndex < kCache32Count); |
| 642 | |
| 643 | if (nextIndex > prevIndex) { |
| 644 | Sk4f c0 = Sk4f::Load(fOrigColors4f[i - 1].vec()); |
| 645 | Sk4f c1 = Sk4f::Load(fOrigColors4f[i].vec()); |
| 646 | if (interpInPremul) { |
| 647 | c0 = c0 * Sk4f(c0[3], c0[3], c0[3], 1.0f); |
| 648 | c1 = c1 * Sk4f(c1[3], c1[3], c1[3], 1.0f); |
| 649 | } |
| 650 | |
| 651 | Sk4f step = Sk4f(1.0f / static_cast<float>(nextIndex - prevIndex)); |
| 652 | Sk4f delta = (c1 - c0) * step; |
| 653 | |
| 654 | for (int curIndex = prevIndex; curIndex <= nextIndex; ++curIndex) { |
| 655 | writePixel(c0, curIndex); |
| 656 | c0 += delta; |
| 657 | } |
| 658 | } |
| 659 | prevIndex = nextIndex; |
| 660 | } |
| 661 | SkASSERT(prevIndex == kCache32Count - 1); |
| 662 | bitmap->unlockPixels(); |
| 663 | } |
| 664 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 665 | /* |
| 666 | * The gradient holds a cache for the most recent value of alpha. Successive |
| 667 | * callers with the same alpha value will share the same cache. |
| 668 | */ |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 669 | sk_sp<SkGradientShaderBase::GradientShaderCache> SkGradientShaderBase::refCache(U8CPU alpha, |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 670 | bool dither) const { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 671 | SkAutoMutexAcquire ama(fCacheMutex); |
fmalita | 37d8688 | 2015-10-09 10:22:46 -0700 | [diff] [blame] | 672 | if (!fCache || fCache->getAlpha() != alpha || fCache->getDither() != dither) { |
| 673 | fCache.reset(new GradientShaderCache(alpha, dither, *this)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 674 | } |
| 675 | // Increment the ref counter inside the mutex to ensure the returned pointer is still valid. |
| 676 | // Otherwise, the pointer may have been overwritten on a different thread before the object's |
| 677 | // ref count was incremented. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 678 | return fCache; |
| 679 | } |
| 680 | |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 681 | SK_DECLARE_STATIC_MUTEX(gGradientCacheMutex); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 682 | /* |
| 683 | * Because our caller might rebuild the same (logically the same) gradient |
| 684 | * over and over, we'd like to return exactly the same "bitmap" if possible, |
| 685 | * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). |
| 686 | * To do that, we maintain a private cache of built-bitmaps, based on our |
| 687 | * colors and positions. Note: we don't try to flatten the fMapper, so if one |
| 688 | * is present, we skip the cache for now. |
| 689 | */ |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 690 | void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap, |
| 691 | GradientBitmapType bitmapType) const { |
| 692 | // our caller assumes no external alpha, so we ensure that our cache is built with 0xFF |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 693 | sk_sp<GradientShaderCache> cache(this->refCache(0xFF, true)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 694 | |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 695 | // build our key: [numColors + colors[] + {positions[]} + flags + colorType ] |
| 696 | int count = 1 + fColorCount + 1 + 1; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 697 | if (fColorCount > 2) { |
| 698 | count += fColorCount - 1; // fRecs[].fPos |
| 699 | } |
| 700 | |
| 701 | SkAutoSTMalloc<16, int32_t> storage(count); |
| 702 | int32_t* buffer = storage.get(); |
| 703 | |
| 704 | *buffer++ = fColorCount; |
| 705 | memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor)); |
| 706 | buffer += fColorCount; |
| 707 | if (fColorCount > 2) { |
| 708 | for (int i = 1; i < fColorCount; i++) { |
| 709 | *buffer++ = fRecs[i].fPos; |
| 710 | } |
| 711 | } |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 712 | *buffer++ = fGradFlags; |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 713 | *buffer++ = static_cast<int32_t>(bitmapType); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 714 | SkASSERT(buffer - storage.get() == count); |
| 715 | |
| 716 | /////////////////////////////////// |
| 717 | |
reed | a6cac4c | 2014-08-21 10:50:25 -0700 | [diff] [blame] | 718 | static SkGradientBitmapCache* gCache; |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 719 | // each cache cost 1K or 2K of RAM, since each bitmap will be 1x256 at either 32bpp or 64bpp |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 720 | static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; |
bungeman | d6aeb6d | 2014-07-25 11:52:47 -0700 | [diff] [blame] | 721 | SkAutoMutexAcquire ama(gGradientCacheMutex); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 722 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 723 | if (nullptr == gCache) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 724 | gCache = new SkGradientBitmapCache(MAX_NUM_CACHED_GRADIENT_BITMAPS); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 725 | } |
| 726 | size_t size = count * sizeof(int32_t); |
| 727 | |
| 728 | if (!gCache->find(storage.get(), size, bitmap)) { |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 729 | if (GradientBitmapType::kLegacy == bitmapType) { |
| 730 | // force our cache32pixelref to be built |
| 731 | (void)cache->getCache32(); |
| 732 | bitmap->setInfo(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 733 | bitmap->setPixelRef(sk_ref_sp(cache->getCache32PixelRef()), 0, 0); |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 734 | } else { |
| 735 | // For these cases we use the bitmap cache, but not the GradientShaderCache. So just |
| 736 | // allocate and populate the bitmap's data directly. |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 737 | |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 738 | SkImageInfo info; |
| 739 | switch (bitmapType) { |
| 740 | case GradientBitmapType::kSRGB: |
| 741 | info = SkImageInfo::Make(kCache32Count, 1, kRGBA_8888_SkColorType, |
| 742 | kPremul_SkAlphaType, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 743 | SkColorSpace::MakeSRGB()); |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 744 | break; |
| 745 | case GradientBitmapType::kHalfFloat: |
brianosman | eec8304 | 2016-09-27 15:11:47 -0700 | [diff] [blame] | 746 | info = SkImageInfo::Make( |
| 747 | kCache32Count, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 748 | SkColorSpace::MakeSRGBLinear()); |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 749 | break; |
| 750 | default: |
| 751 | SkFAIL("Unexpected bitmap type"); |
| 752 | return; |
| 753 | } |
| 754 | bitmap->allocPixels(info); |
| 755 | this->initLinearBitmap(bitmap); |
| 756 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 757 | gCache->add(storage.get(), size, *bitmap); |
| 758 | } |
| 759 | } |
| 760 | |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 761 | void SkGradientShaderBase::commonAsAGradient(GradientInfo* info, bool flipGrad) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 762 | if (info) { |
| 763 | if (info->fColorCount >= fColorCount) { |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 764 | SkColor* colorLoc; |
| 765 | Rec* recLoc; |
Hal Canary | ab8e02a | 2017-02-27 09:59:39 -0500 | [diff] [blame] | 766 | SkAutoSTArray<8, SkColor> colorStorage; |
| 767 | SkAutoSTArray<8, Rec> recStorage; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 768 | if (flipGrad && (info->fColors || info->fColorOffsets)) { |
Hal Canary | ab8e02a | 2017-02-27 09:59:39 -0500 | [diff] [blame] | 769 | colorStorage.reset(fColorCount); |
| 770 | recStorage.reset(fColorCount); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 771 | colorLoc = colorStorage.get(); |
| 772 | recLoc = recStorage.get(); |
| 773 | FlipGradientColors(colorLoc, recLoc, fOrigColors, fRecs, fColorCount); |
| 774 | } else { |
| 775 | colorLoc = fOrigColors; |
| 776 | recLoc = fRecs; |
| 777 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 778 | if (info->fColors) { |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 779 | memcpy(info->fColors, colorLoc, fColorCount * sizeof(SkColor)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 780 | } |
| 781 | if (info->fColorOffsets) { |
| 782 | if (fColorCount == 2) { |
| 783 | info->fColorOffsets[0] = 0; |
| 784 | info->fColorOffsets[1] = SK_Scalar1; |
| 785 | } else if (fColorCount > 2) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 786 | for (int i = 0; i < fColorCount; ++i) { |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 787 | info->fColorOffsets[i] = SkFixedToScalar(recLoc[i].fPos); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 788 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | } |
| 792 | info->fColorCount = fColorCount; |
| 793 | info->fTileMode = fTileMode; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 794 | info->fGradientFlags = fGradFlags; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 798 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 799 | void SkGradientShaderBase::toString(SkString* str) const { |
| 800 | |
| 801 | str->appendf("%d colors: ", fColorCount); |
| 802 | |
| 803 | for (int i = 0; i < fColorCount; ++i) { |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 804 | str->appendHex(fOrigColors[i], 8); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 805 | if (i < fColorCount-1) { |
| 806 | str->append(", "); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | if (fColorCount > 2) { |
| 811 | str->append(" points: ("); |
| 812 | for (int i = 0; i < fColorCount; ++i) { |
| 813 | str->appendScalar(SkFixedToScalar(fRecs[i].fPos)); |
| 814 | if (i < fColorCount-1) { |
| 815 | str->append(", "); |
| 816 | } |
| 817 | } |
| 818 | str->append(")"); |
| 819 | } |
| 820 | |
| 821 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 822 | "clamp", "repeat", "mirror" |
| 823 | }; |
| 824 | |
| 825 | str->append(" "); |
| 826 | str->append(gTileModeName[fTileMode]); |
| 827 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 828 | this->INHERITED::toString(str); |
| 829 | } |
| 830 | #endif |
| 831 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 832 | /////////////////////////////////////////////////////////////////////////////// |
| 833 | /////////////////////////////////////////////////////////////////////////////// |
| 834 | |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 835 | // Return true if these parameters are valid/legal/safe to construct a gradient |
| 836 | // |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 837 | static bool valid_grad(const SkColor4f colors[], const SkScalar pos[], int count, |
| 838 | unsigned tileMode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 839 | return nullptr != colors && count >= 1 && tileMode < (unsigned)SkShader::kTileModeCount; |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 840 | } |
| 841 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 842 | static void desc_init(SkGradientShaderBase::Descriptor* desc, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 843 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 844 | const SkScalar pos[], int colorCount, |
reed | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 845 | SkShader::TileMode mode, uint32_t flags, const SkMatrix* localMatrix) { |
fmalita | 748d620 | 2016-05-11 11:39:58 -0700 | [diff] [blame] | 846 | SkASSERT(colorCount > 1); |
| 847 | |
commit-bot@chromium.org | 6c5aea2 | 2014-04-22 16:25:15 +0000 | [diff] [blame] | 848 | desc->fColors = colors; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 849 | desc->fColorSpace = std::move(colorSpace); |
commit-bot@chromium.org | 6c5aea2 | 2014-04-22 16:25:15 +0000 | [diff] [blame] | 850 | desc->fPos = pos; |
| 851 | desc->fCount = colorCount; |
| 852 | desc->fTileMode = mode; |
commit-bot@chromium.org | 6c5aea2 | 2014-04-22 16:25:15 +0000 | [diff] [blame] | 853 | desc->fGradFlags = flags; |
reed | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 854 | desc->fLocalMatrix = localMatrix; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 855 | } |
| 856 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 857 | // assumes colors is SkColor4f* and pos is SkScalar* |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 858 | #define EXPAND_1_COLOR(count) \ |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 859 | SkColor4f tmp[2]; \ |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 860 | do { \ |
| 861 | if (1 == count) { \ |
| 862 | tmp[0] = tmp[1] = colors[0]; \ |
| 863 | colors = tmp; \ |
| 864 | pos = nullptr; \ |
| 865 | count = 2; \ |
| 866 | } \ |
| 867 | } while (0) |
| 868 | |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 869 | struct ColorStopOptimizer { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 870 | ColorStopOptimizer(const SkColor4f* colors, const SkScalar* pos, |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 871 | int count, SkShader::TileMode mode) |
| 872 | : fColors(colors) |
| 873 | , fPos(pos) |
| 874 | , fCount(count) { |
| 875 | |
| 876 | if (!pos || count != 3) { |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | if (SkScalarNearlyEqual(pos[0], 0.0f) && |
| 881 | SkScalarNearlyEqual(pos[1], 0.0f) && |
| 882 | SkScalarNearlyEqual(pos[2], 1.0f)) { |
| 883 | |
| 884 | if (SkShader::kRepeat_TileMode == mode || |
| 885 | SkShader::kMirror_TileMode == mode || |
| 886 | colors[0] == colors[1]) { |
| 887 | |
fmalita | 582a656 | 2016-08-22 06:28:57 -0700 | [diff] [blame] | 888 | // Ignore the leftmost color/pos. |
| 889 | fColors += 1; |
| 890 | fPos += 1; |
| 891 | fCount = 2; |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 892 | } |
| 893 | } else if (SkScalarNearlyEqual(pos[0], 0.0f) && |
| 894 | SkScalarNearlyEqual(pos[1], 1.0f) && |
| 895 | SkScalarNearlyEqual(pos[2], 1.0f)) { |
| 896 | |
| 897 | if (SkShader::kRepeat_TileMode == mode || |
| 898 | SkShader::kMirror_TileMode == mode || |
| 899 | colors[1] == colors[2]) { |
| 900 | |
fmalita | 582a656 | 2016-08-22 06:28:57 -0700 | [diff] [blame] | 901 | // Ignore the rightmost color/pos. |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 902 | fCount = 2; |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 907 | const SkColor4f* fColors; |
| 908 | const SkScalar* fPos; |
| 909 | int fCount; |
| 910 | }; |
| 911 | |
| 912 | struct ColorConverter { |
| 913 | ColorConverter(const SkColor* colors, int count) { |
| 914 | for (int i = 0; i < count; ++i) { |
| 915 | fColors4f.push_back(SkColor4f::FromColor(colors[i])); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | SkSTArray<2, SkColor4f, true> fColors4f; |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 920 | }; |
| 921 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 922 | sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2], |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 923 | const SkColor colors[], |
| 924 | const SkScalar pos[], int colorCount, |
| 925 | SkShader::TileMode mode, |
| 926 | uint32_t flags, |
| 927 | const SkMatrix* localMatrix) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 928 | ColorConverter converter(colors, colorCount); |
| 929 | return MakeLinear(pts, converter.fColors4f.begin(), nullptr, pos, colorCount, mode, flags, |
| 930 | localMatrix); |
| 931 | } |
| 932 | |
| 933 | sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2], |
| 934 | const SkColor4f colors[], |
| 935 | sk_sp<SkColorSpace> colorSpace, |
| 936 | const SkScalar pos[], int colorCount, |
| 937 | SkShader::TileMode mode, |
| 938 | uint32_t flags, |
| 939 | const SkMatrix* localMatrix) { |
fmalita | c523104 | 2016-08-10 05:45:50 -0700 | [diff] [blame] | 940 | if (!pts || !SkScalarIsFinite((pts[1] - pts[0]).length())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 941 | return nullptr; |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 942 | } |
| 943 | if (!valid_grad(colors, pos, colorCount, mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 944 | return nullptr; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 945 | } |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 946 | if (1 == colorCount) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 947 | return SkShader::MakeColorShader(colors[0], std::move(colorSpace)); |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 948 | } |
Florin Malita | 8d3ffad | 2017-02-03 18:21:17 +0000 | [diff] [blame] | 949 | if (localMatrix && !localMatrix->invert(nullptr)) { |
| 950 | return nullptr; |
| 951 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 952 | |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 953 | ColorStopOptimizer opt(colors, pos, colorCount, mode); |
| 954 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 955 | SkGradientShaderBase::Descriptor desc; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 956 | desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags, |
| 957 | localMatrix); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 958 | return sk_make_sp<SkLinearGradient>(pts, desc); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 959 | } |
| 960 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 961 | sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar radius, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 962 | const SkColor colors[], |
| 963 | const SkScalar pos[], int colorCount, |
| 964 | SkShader::TileMode mode, |
| 965 | uint32_t flags, |
| 966 | const SkMatrix* localMatrix) { |
| 967 | ColorConverter converter(colors, colorCount); |
| 968 | return MakeRadial(center, radius, converter.fColors4f.begin(), nullptr, pos, colorCount, mode, |
| 969 | flags, localMatrix); |
| 970 | } |
| 971 | |
| 972 | sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar radius, |
| 973 | const SkColor4f colors[], |
| 974 | sk_sp<SkColorSpace> colorSpace, |
| 975 | const SkScalar pos[], int colorCount, |
| 976 | SkShader::TileMode mode, |
| 977 | uint32_t flags, |
| 978 | const SkMatrix* localMatrix) { |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 979 | if (radius <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 980 | return nullptr; |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 981 | } |
| 982 | if (!valid_grad(colors, pos, colorCount, mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 983 | return nullptr; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 984 | } |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 985 | if (1 == colorCount) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 986 | return SkShader::MakeColorShader(colors[0], std::move(colorSpace)); |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 987 | } |
Florin Malita | 8d3ffad | 2017-02-03 18:21:17 +0000 | [diff] [blame] | 988 | if (localMatrix && !localMatrix->invert(nullptr)) { |
| 989 | return nullptr; |
| 990 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 991 | |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 992 | ColorStopOptimizer opt(colors, pos, colorCount, mode); |
| 993 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 994 | SkGradientShaderBase::Descriptor desc; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 995 | desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags, |
| 996 | localMatrix); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 997 | return sk_make_sp<SkRadialGradient>(center, radius, desc); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 998 | } |
| 999 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 1000 | sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1001 | SkScalar startRadius, |
| 1002 | const SkPoint& end, |
| 1003 | SkScalar endRadius, |
| 1004 | const SkColor colors[], |
| 1005 | const SkScalar pos[], |
| 1006 | int colorCount, |
| 1007 | SkShader::TileMode mode, |
| 1008 | uint32_t flags, |
| 1009 | const SkMatrix* localMatrix) { |
| 1010 | ColorConverter converter(colors, colorCount); |
| 1011 | return MakeTwoPointConical(start, startRadius, end, endRadius, converter.fColors4f.begin(), |
| 1012 | nullptr, pos, colorCount, mode, flags, localMatrix); |
| 1013 | } |
| 1014 | |
| 1015 | sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start, |
| 1016 | SkScalar startRadius, |
| 1017 | const SkPoint& end, |
| 1018 | SkScalar endRadius, |
| 1019 | const SkColor4f colors[], |
| 1020 | sk_sp<SkColorSpace> colorSpace, |
| 1021 | const SkScalar pos[], |
| 1022 | int colorCount, |
| 1023 | SkShader::TileMode mode, |
| 1024 | uint32_t flags, |
| 1025 | const SkMatrix* localMatrix) { |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 1026 | if (startRadius < 0 || endRadius < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1027 | return nullptr; |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 1028 | } |
| 1029 | if (!valid_grad(colors, pos, colorCount, mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1030 | return nullptr; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1031 | } |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 1032 | if (startRadius == endRadius) { |
| 1033 | if (start == end || startRadius == 0) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 1034 | return SkShader::MakeEmptyShader(); |
fmalita | 5edf82e | 2016-03-03 06:41:54 -0800 | [diff] [blame] | 1035 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1036 | } |
Florin Malita | 8d3ffad | 2017-02-03 18:21:17 +0000 | [diff] [blame] | 1037 | if (localMatrix && !localMatrix->invert(nullptr)) { |
| 1038 | return nullptr; |
| 1039 | } |
reed | 6b7a6c7 | 2016-08-18 16:13:50 -0700 | [diff] [blame] | 1040 | EXPAND_1_COLOR(colorCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1041 | |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 1042 | ColorStopOptimizer opt(colors, pos, colorCount, mode); |
| 1043 | |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1044 | bool flipGradient = startRadius > endRadius; |
| 1045 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 1046 | SkGradientShaderBase::Descriptor desc; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (!flipGradient) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1049 | desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags, |
| 1050 | localMatrix); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 1051 | return sk_make_sp<SkTwoPointConicalGradient>(start, startRadius, end, endRadius, |
| 1052 | flipGradient, desc); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1053 | } else { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1054 | SkAutoSTArray<8, SkColor4f> colorsNew(opt.fCount); |
tomhudson | 54d9b66 | 2016-09-16 14:22:49 -0700 | [diff] [blame] | 1055 | SkAutoSTArray<8, SkScalar> posNew(opt.fCount); |
| 1056 | for (int i = 0; i < opt.fCount; ++i) { |
| 1057 | colorsNew[i] = opt.fColors[opt.fCount - i - 1]; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | if (pos) { |
tomhudson | 54d9b66 | 2016-09-16 14:22:49 -0700 | [diff] [blame] | 1061 | for (int i = 0; i < opt.fCount; ++i) { |
| 1062 | posNew[i] = 1 - opt.fPos[opt.fCount - i - 1]; |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1063 | } |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1064 | desc_init(&desc, colorsNew.get(), std::move(colorSpace), posNew.get(), opt.fCount, mode, |
| 1065 | flags, localMatrix); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1066 | } else { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1067 | desc_init(&desc, colorsNew.get(), std::move(colorSpace), nullptr, opt.fCount, mode, |
| 1068 | flags, localMatrix); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 1071 | return sk_make_sp<SkTwoPointConicalGradient>(end, endRadius, start, startRadius, |
| 1072 | flipGradient, desc); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1073 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 1076 | sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1077 | const SkColor colors[], |
| 1078 | const SkScalar pos[], |
| 1079 | int colorCount, |
| 1080 | uint32_t flags, |
| 1081 | const SkMatrix* localMatrix) { |
| 1082 | ColorConverter converter(colors, colorCount); |
| 1083 | return MakeSweep(cx, cy, converter.fColors4f.begin(), nullptr, pos, colorCount, flags, |
| 1084 | localMatrix); |
| 1085 | } |
| 1086 | |
| 1087 | sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy, |
| 1088 | const SkColor4f colors[], |
| 1089 | sk_sp<SkColorSpace> colorSpace, |
| 1090 | const SkScalar pos[], |
| 1091 | int colorCount, |
| 1092 | uint32_t flags, |
| 1093 | const SkMatrix* localMatrix) { |
reed | 1b74730 | 2015-01-06 07:13:19 -0800 | [diff] [blame] | 1094 | if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1095 | return nullptr; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1096 | } |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 1097 | if (1 == colorCount) { |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1098 | return SkShader::MakeColorShader(colors[0], std::move(colorSpace)); |
fmenozzi | e9fd0f8 | 2016-08-19 07:50:57 -0700 | [diff] [blame] | 1099 | } |
Florin Malita | 8d3ffad | 2017-02-03 18:21:17 +0000 | [diff] [blame] | 1100 | if (localMatrix && !localMatrix->invert(nullptr)) { |
| 1101 | return nullptr; |
| 1102 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1103 | |
fmenozzi | 68d952c | 2016-08-19 08:56:56 -0700 | [diff] [blame] | 1104 | auto mode = SkShader::kClamp_TileMode; |
| 1105 | |
| 1106 | ColorStopOptimizer opt(colors, pos, colorCount, mode); |
| 1107 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 1108 | SkGradientShaderBase::Descriptor desc; |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 1109 | desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags, |
| 1110 | localMatrix); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 1111 | return sk_make_sp<SkSweepGradient>(cx, cy, desc); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) |
| 1115 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) |
| 1116 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) |
| 1117 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1118 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient) |
| 1119 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1120 | |
| 1121 | /////////////////////////////////////////////////////////////////////////////// |
| 1122 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1123 | #if SK_SUPPORT_GPU |
| 1124 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 1125 | #include "GrContext.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1126 | #include "GrShaderCaps.h" |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 1127 | #include "GrTextureStripAtlas.h" |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 1128 | #include "gl/GrGLContext.h" |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1129 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 1130 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1131 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1132 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1133 | #include "SkGr.h" |
| 1134 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1135 | static inline bool close_to_one_half(const SkFixed& val) { |
| 1136 | return SkScalarNearlyEqual(SkFixedToScalar(val), SK_ScalarHalf); |
| 1137 | } |
| 1138 | |
| 1139 | static inline int color_type_to_color_count(GrGradientEffect::ColorType colorType) { |
| 1140 | switch (colorType) { |
| 1141 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1142 | case GrGradientEffect::kSingleHardStop_ColorType: |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1143 | return 4; |
| 1144 | case GrGradientEffect::kHardStopLeftEdged_ColorType: |
| 1145 | case GrGradientEffect::kHardStopRightEdged_ColorType: |
| 1146 | return 3; |
| 1147 | #endif |
| 1148 | case GrGradientEffect::kTwo_ColorType: |
| 1149 | return 2; |
| 1150 | case GrGradientEffect::kThree_ColorType: |
| 1151 | return 3; |
| 1152 | case GrGradientEffect::kTexture_ColorType: |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | SkDEBUGFAIL("Unhandled ColorType in color_type_to_color_count()"); |
| 1157 | return -1; |
| 1158 | } |
| 1159 | |
| 1160 | GrGradientEffect::ColorType GrGradientEffect::determineColorType( |
| 1161 | const SkGradientShaderBase& shader) { |
| 1162 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
| 1163 | if (shader.fOrigPos) { |
| 1164 | if (4 == shader.fColorCount) { |
| 1165 | if (SkScalarNearlyEqual(shader.fOrigPos[0], 0.0f) && |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1166 | SkScalarNearlyEqual(shader.fOrigPos[1], shader.fOrigPos[2]) && |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1167 | SkScalarNearlyEqual(shader.fOrigPos[3], 1.0f)) { |
| 1168 | |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1169 | return kSingleHardStop_ColorType; |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1170 | } |
| 1171 | } else if (3 == shader.fColorCount) { |
| 1172 | if (SkScalarNearlyEqual(shader.fOrigPos[0], 0.0f) && |
| 1173 | SkScalarNearlyEqual(shader.fOrigPos[1], 0.0f) && |
| 1174 | SkScalarNearlyEqual(shader.fOrigPos[2], 1.0f)) { |
| 1175 | |
| 1176 | return kHardStopLeftEdged_ColorType; |
| 1177 | } else if (SkScalarNearlyEqual(shader.fOrigPos[0], 0.0f) && |
| 1178 | SkScalarNearlyEqual(shader.fOrigPos[1], 1.0f) && |
| 1179 | SkScalarNearlyEqual(shader.fOrigPos[2], 1.0f)) { |
| 1180 | |
| 1181 | return kHardStopRightEdged_ColorType; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | #endif |
| 1186 | |
| 1187 | if (SkShader::kClamp_TileMode == shader.getTileMode()) { |
| 1188 | if (2 == shader.fColorCount) { |
| 1189 | return kTwo_ColorType; |
| 1190 | } else if (3 == shader.fColorCount && |
| 1191 | close_to_one_half(shader.getRecs()[1].fPos)) { |
| 1192 | return kThree_ColorType; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | return kTexture_ColorType; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1197 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1198 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1199 | void GrGradientEffect::GLSLProcessor::emitUniforms(GrGLSLUniformHandler* uniformHandler, |
| 1200 | const GrGradientEffect& ge) { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1201 | if (int colorCount = color_type_to_color_count(ge.getColorType())) { |
| 1202 | fColorsUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, |
| 1203 | kVec4f_GrSLType, |
| 1204 | kDefault_GrSLPrecision, |
| 1205 | "Colors", |
| 1206 | colorCount); |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1207 | if (ge.fColorType == kSingleHardStop_ColorType) { |
| 1208 | fHardStopT = uniformHandler->addUniform(kFragment_GrShaderFlag, kFloat_GrSLType, |
| 1209 | kDefault_GrSLPrecision, "HardStopT"); |
| 1210 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1211 | } else { |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1212 | fFSYUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1213 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 1214 | "GradientYCoordFS"); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 1218 | static inline void set_after_interp_color_uni_array( |
| 1219 | const GrGLSLProgramDataManager& pdman, |
| 1220 | const GrGLSLProgramDataManager::UniformHandle uni, |
| 1221 | const SkTDArray<SkColor4f>& colors, |
| 1222 | const GrColorSpaceXform* colorSpaceXform) { |
| 1223 | int count = colors.count(); |
| 1224 | if (colorSpaceXform) { |
| 1225 | constexpr int kSmallCount = 10; |
| 1226 | SkAutoSTArray<4 * kSmallCount, float> vals(4 * count); |
| 1227 | |
| 1228 | for (int i = 0; i < count; i++) { |
| 1229 | colorSpaceXform->srcToDst().mapScalars(colors[i].vec(), &vals[4 * i]); |
| 1230 | } |
| 1231 | |
| 1232 | pdman.set4fv(uni, count, vals.get()); |
| 1233 | } else { |
| 1234 | pdman.set4fv(uni, count, (float*)&colors[0]); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | static inline void set_before_interp_color_uni_array( |
| 1239 | const GrGLSLProgramDataManager& pdman, |
| 1240 | const GrGLSLProgramDataManager::UniformHandle uni, |
| 1241 | const SkTDArray<SkColor4f>& colors, |
| 1242 | const GrColorSpaceXform* colorSpaceXform) { |
| 1243 | int count = colors.count(); |
| 1244 | constexpr int kSmallCount = 10; |
| 1245 | SkAutoSTArray<4 * kSmallCount, float> vals(4 * count); |
| 1246 | |
| 1247 | for (int i = 0; i < count; i++) { |
| 1248 | float a = colors[i].fA; |
| 1249 | vals[4 * i + 0] = colors[i].fR * a; |
| 1250 | vals[4 * i + 1] = colors[i].fG * a; |
| 1251 | vals[4 * i + 2] = colors[i].fB * a; |
| 1252 | vals[4 * i + 3] = a; |
| 1253 | } |
| 1254 | |
| 1255 | if (colorSpaceXform) { |
| 1256 | for (int i = 0; i < count; i++) { |
| 1257 | colorSpaceXform->srcToDst().mapScalars(&vals[4 * i]); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | pdman.set4fv(uni, count, vals.get()); |
| 1262 | } |
| 1263 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1264 | static inline void set_after_interp_color_uni_array(const GrGLSLProgramDataManager& pdman, |
| 1265 | const GrGLSLProgramDataManager::UniformHandle uni, |
| 1266 | const SkTDArray<SkColor>& colors) { |
| 1267 | int count = colors.count(); |
| 1268 | constexpr int kSmallCount = 10; |
| 1269 | |
| 1270 | SkAutoSTArray<4*kSmallCount, float> vals(4*count); |
| 1271 | |
| 1272 | for (int i = 0; i < colors.count(); i++) { |
| 1273 | // RGBA |
| 1274 | vals[4*i + 0] = SkColorGetR(colors[i]) / 255.f; |
| 1275 | vals[4*i + 1] = SkColorGetG(colors[i]) / 255.f; |
| 1276 | vals[4*i + 2] = SkColorGetB(colors[i]) / 255.f; |
| 1277 | vals[4*i + 3] = SkColorGetA(colors[i]) / 255.f; |
| 1278 | } |
| 1279 | |
| 1280 | pdman.set4fv(uni, colors.count(), vals.get()); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1283 | static inline void set_before_interp_color_uni_array(const GrGLSLProgramDataManager& pdman, |
| 1284 | const GrGLSLProgramDataManager::UniformHandle uni, |
| 1285 | const SkTDArray<SkColor>& colors) { |
| 1286 | int count = colors.count(); |
| 1287 | constexpr int kSmallCount = 10; |
| 1288 | |
| 1289 | SkAutoSTArray<4*kSmallCount, float> vals(4*count); |
| 1290 | |
| 1291 | for (int i = 0; i < count; i++) { |
| 1292 | float a = SkColorGetA(colors[i]) / 255.f; |
| 1293 | float aDiv255 = a / 255.f; |
| 1294 | |
| 1295 | // RGBA |
| 1296 | vals[4*i + 0] = SkColorGetR(colors[i]) * aDiv255; |
| 1297 | vals[4*i + 1] = SkColorGetG(colors[i]) * aDiv255; |
| 1298 | vals[4*i + 2] = SkColorGetB(colors[i]) * aDiv255; |
| 1299 | vals[4*i + 3] = a; |
| 1300 | } |
| 1301 | |
| 1302 | pdman.set4fv(uni, count, vals.get()); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1305 | void GrGradientEffect::GLSLProcessor::onSetData(const GrGLSLProgramDataManager& pdman, |
| 1306 | const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1307 | const GrGradientEffect& e = processor.cast<GrGradientEffect>(); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1308 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1309 | switch (e.getColorType()) { |
| 1310 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1311 | case GrGradientEffect::kSingleHardStop_ColorType: |
| 1312 | pdman.set1f(fHardStopT, e.fPositions[1]); |
| 1313 | // fall through |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1314 | case GrGradientEffect::kHardStopLeftEdged_ColorType: |
| 1315 | case GrGradientEffect::kHardStopRightEdged_ColorType: |
| 1316 | #endif |
| 1317 | case GrGradientEffect::kTwo_ColorType: |
| 1318 | case GrGradientEffect::kThree_ColorType: { |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 1319 | if (e.fColors4f.count() > 0) { |
| 1320 | // Gamma-correct / color-space aware |
| 1321 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 1322 | set_before_interp_color_uni_array(pdman, fColorsUni, e.fColors4f, |
| 1323 | e.fColorSpaceXform.get()); |
| 1324 | } else { |
| 1325 | set_after_interp_color_uni_array(pdman, fColorsUni, e.fColors4f, |
| 1326 | e.fColorSpaceXform.get()); |
| 1327 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1328 | } else { |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 1329 | // Legacy mode. Would be nice if we had converted the 8-bit colors to float earlier |
| 1330 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 1331 | set_before_interp_color_uni_array(pdman, fColorsUni, e.fColors); |
| 1332 | } else { |
| 1333 | set_after_interp_color_uni_array(pdman, fColorsUni, e.fColors); |
| 1334 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1335 | } |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1336 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1337 | break; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1340 | case GrGradientEffect::kTexture_ColorType: { |
| 1341 | SkScalar yCoord = e.getYCoord(); |
| 1342 | if (yCoord != fCachedYCoord) { |
| 1343 | pdman.set1f(fFSYUni, yCoord); |
| 1344 | fCachedYCoord = yCoord; |
| 1345 | } |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1346 | if (SkToBool(e.fColorSpaceXform)) { |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame] | 1347 | fColorSpaceHelper.setData(pdman, e.fColorSpaceXform.get()); |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1348 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1349 | break; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1350 | } |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1354 | uint32_t GrGradientEffect::GLSLProcessor::GenBaseGradientKey(const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1355 | const GrGradientEffect& e = processor.cast<GrGradientEffect>(); |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 1356 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1357 | uint32_t key = 0; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1358 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1359 | if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { |
| 1360 | key |= kPremulBeforeInterpKey; |
| 1361 | } |
| 1362 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1363 | if (GrGradientEffect::kTwo_ColorType == e.getColorType()) { |
| 1364 | key |= kTwoColorKey; |
| 1365 | } else if (GrGradientEffect::kThree_ColorType == e.getColorType()) { |
| 1366 | key |= kThreeColorKey; |
| 1367 | } |
| 1368 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1369 | else if (GrGradientEffect::kSingleHardStop_ColorType == e.getColorType()) { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1370 | key |= kHardStopCenteredKey; |
| 1371 | } else if (GrGradientEffect::kHardStopLeftEdged_ColorType == e.getColorType()) { |
| 1372 | key |= kHardStopZeroZeroOneKey; |
| 1373 | } else if (GrGradientEffect::kHardStopRightEdged_ColorType == e.getColorType()) { |
| 1374 | key |= kHardStopZeroOneOneKey; |
| 1375 | } |
| 1376 | |
| 1377 | if (SkShader::TileMode::kClamp_TileMode == e.fTileMode) { |
| 1378 | key |= kClampTileMode; |
| 1379 | } else if (SkShader::TileMode::kRepeat_TileMode == e.fTileMode) { |
| 1380 | key |= kRepeatTileMode; |
| 1381 | } else { |
| 1382 | key |= kMirrorTileMode; |
| 1383 | } |
| 1384 | #endif |
| 1385 | |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1386 | key |= GrColorSpaceXform::XformKey(e.fColorSpaceXform.get()) << kReservedBits; |
| 1387 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1388 | return key; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1391 | void GrGradientEffect::GLSLProcessor::emitColor(GrGLSLFPFragmentBuilder* fragBuilder, |
| 1392 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 1393 | const GrShaderCaps* shaderCaps, |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1394 | const GrGradientEffect& ge, |
| 1395 | const char* gradientTValue, |
| 1396 | const char* outputColor, |
| 1397 | const char* inputColor, |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 1398 | const TextureSamplers& texSamplers) { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1399 | switch (ge.getColorType()) { |
| 1400 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1401 | case kSingleHardStop_ColorType: { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1402 | const char* t = gradientTValue; |
| 1403 | const char* colors = uniformHandler->getUniformCStr(fColorsUni); |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1404 | const char* stopT = uniformHandler->getUniformCStr(fHardStopT); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1405 | |
| 1406 | fragBuilder->codeAppendf("float clamp_t = clamp(%s, 0.0, 1.0);", t); |
| 1407 | |
| 1408 | // Account for tile mode |
| 1409 | if (SkShader::kRepeat_TileMode == ge.fTileMode) { |
| 1410 | fragBuilder->codeAppendf("clamp_t = fract(%s);", t); |
| 1411 | } else if (SkShader::kMirror_TileMode == ge.fTileMode) { |
| 1412 | fragBuilder->codeAppendf("if (%s < 0.0 || %s > 1.0) {", t, t); |
| 1413 | fragBuilder->codeAppendf(" if (mod(floor(%s), 2.0) == 0.0) {", t); |
| 1414 | fragBuilder->codeAppendf(" clamp_t = fract(%s);", t); |
| 1415 | fragBuilder->codeAppendf(" } else {"); |
| 1416 | fragBuilder->codeAppendf(" clamp_t = 1.0 - fract(%s);", t); |
| 1417 | fragBuilder->codeAppendf(" }"); |
| 1418 | fragBuilder->codeAppendf("}"); |
| 1419 | } |
| 1420 | |
| 1421 | // Calculate color |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1422 | fragBuilder->codeAppend ("vec4 start, end;"); |
| 1423 | fragBuilder->codeAppend ("float relative_t;"); |
| 1424 | fragBuilder->codeAppendf("if (clamp_t < %s) {", stopT); |
| 1425 | fragBuilder->codeAppendf(" start = %s[0];", colors); |
| 1426 | fragBuilder->codeAppendf(" end = %s[1];", colors); |
| 1427 | fragBuilder->codeAppendf(" relative_t = clamp_t / %s;", stopT); |
| 1428 | fragBuilder->codeAppend ("} else {"); |
fmenozzi | af23ee5 | 2016-08-16 09:24:52 -0700 | [diff] [blame] | 1429 | fragBuilder->codeAppendf(" start = %s[2];", colors); |
| 1430 | fragBuilder->codeAppendf(" end = %s[3];", colors); |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1431 | fragBuilder->codeAppendf(" relative_t = (clamp_t - %s) / (1 - %s);", stopT, stopT); |
| 1432 | fragBuilder->codeAppend ("}"); |
| 1433 | fragBuilder->codeAppend ("vec4 colorTemp = mix(start, end, relative_t);"); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1434 | |
| 1435 | if (GrGradientEffect::kAfterInterp_PremulType == ge.getPremulType()) { |
| 1436 | fragBuilder->codeAppend("colorTemp.rgb *= colorTemp.a;"); |
| 1437 | } |
Brian Salomon | c5ca7f9 | 2017-02-21 14:35:11 -0500 | [diff] [blame] | 1438 | if (ge.fColorSpaceXform) { |
| 1439 | fragBuilder->codeAppend("colorTemp.rgb = clamp(colorTemp.rgb, 0, colorTemp.a);"); |
| 1440 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1441 | fragBuilder->codeAppendf("%s = %s;", outputColor, |
| 1442 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
| 1443 | |
| 1444 | break; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1445 | } |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 1446 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1447 | case kHardStopLeftEdged_ColorType: { |
| 1448 | const char* t = gradientTValue; |
| 1449 | const char* colors = uniformHandler->getUniformCStr(fColorsUni); |
| 1450 | |
| 1451 | fragBuilder->codeAppendf("float clamp_t = clamp(%s, 0.0, 1.0);", t); |
| 1452 | |
| 1453 | // Account for tile mode |
| 1454 | if (SkShader::kRepeat_TileMode == ge.fTileMode) { |
| 1455 | fragBuilder->codeAppendf("clamp_t = fract(%s);", t); |
| 1456 | } else if (SkShader::kMirror_TileMode == ge.fTileMode) { |
| 1457 | fragBuilder->codeAppendf("if (%s < 0.0 || %s > 1.0) {", t, t); |
| 1458 | fragBuilder->codeAppendf(" if (mod(floor(%s), 2.0) == 0.0) {", t); |
| 1459 | fragBuilder->codeAppendf(" clamp_t = fract(%s);", t); |
| 1460 | fragBuilder->codeAppendf(" } else {"); |
| 1461 | fragBuilder->codeAppendf(" clamp_t = 1.0 - fract(%s);", t); |
| 1462 | fragBuilder->codeAppendf(" }"); |
| 1463 | fragBuilder->codeAppendf("}"); |
| 1464 | } |
| 1465 | |
| 1466 | fragBuilder->codeAppendf("vec4 colorTemp = mix(%s[1], %s[2], clamp_t);", colors, |
| 1467 | colors); |
| 1468 | if (SkShader::kClamp_TileMode == ge.fTileMode) { |
| 1469 | fragBuilder->codeAppendf("if (%s < 0.0) {", t); |
| 1470 | fragBuilder->codeAppendf(" colorTemp = %s[0];", colors); |
| 1471 | fragBuilder->codeAppendf("}"); |
| 1472 | } |
| 1473 | |
| 1474 | if (GrGradientEffect::kAfterInterp_PremulType == ge.getPremulType()) { |
| 1475 | fragBuilder->codeAppend("colorTemp.rgb *= colorTemp.a;"); |
| 1476 | } |
Brian Salomon | c5ca7f9 | 2017-02-21 14:35:11 -0500 | [diff] [blame] | 1477 | if (ge.fColorSpaceXform) { |
| 1478 | fragBuilder->codeAppend("colorTemp.rgb = clamp(colorTemp.rgb, 0, colorTemp.a);"); |
| 1479 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1480 | fragBuilder->codeAppendf("%s = %s;", outputColor, |
| 1481 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
| 1482 | |
| 1483 | break; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1486 | case kHardStopRightEdged_ColorType: { |
| 1487 | const char* t = gradientTValue; |
| 1488 | const char* colors = uniformHandler->getUniformCStr(fColorsUni); |
| 1489 | |
| 1490 | fragBuilder->codeAppendf("float clamp_t = clamp(%s, 0.0, 1.0);", t); |
| 1491 | |
| 1492 | // Account for tile mode |
| 1493 | if (SkShader::kRepeat_TileMode == ge.fTileMode) { |
| 1494 | fragBuilder->codeAppendf("clamp_t = fract(%s);", t); |
| 1495 | } else if (SkShader::kMirror_TileMode == ge.fTileMode) { |
| 1496 | fragBuilder->codeAppendf("if (%s < 0.0 || %s > 1.0) {", t, t); |
| 1497 | fragBuilder->codeAppendf(" if (mod(floor(%s), 2.0) == 0.0) {", t); |
| 1498 | fragBuilder->codeAppendf(" clamp_t = fract(%s);", t); |
| 1499 | fragBuilder->codeAppendf(" } else {"); |
| 1500 | fragBuilder->codeAppendf(" clamp_t = 1.0 - fract(%s);", t); |
| 1501 | fragBuilder->codeAppendf(" }"); |
| 1502 | fragBuilder->codeAppendf("}"); |
| 1503 | } |
| 1504 | |
| 1505 | fragBuilder->codeAppendf("vec4 colorTemp = mix(%s[0], %s[1], clamp_t);", colors, |
| 1506 | colors); |
| 1507 | if (SkShader::kClamp_TileMode == ge.fTileMode) { |
| 1508 | fragBuilder->codeAppendf("if (%s > 1.0) {", t); |
| 1509 | fragBuilder->codeAppendf(" colorTemp = %s[2];", colors); |
| 1510 | fragBuilder->codeAppendf("}"); |
| 1511 | } |
| 1512 | |
| 1513 | if (GrGradientEffect::kAfterInterp_PremulType == ge.getPremulType()) { |
| 1514 | fragBuilder->codeAppend("colorTemp.rgb *= colorTemp.a;"); |
| 1515 | } |
Brian Salomon | c5ca7f9 | 2017-02-21 14:35:11 -0500 | [diff] [blame] | 1516 | if (ge.fColorSpaceXform) { |
| 1517 | fragBuilder->codeAppend("colorTemp.rgb = clamp(colorTemp.rgb, 0, colorTemp.a);"); |
| 1518 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1519 | fragBuilder->codeAppendf("%s = %s;", outputColor, |
| 1520 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
| 1521 | |
| 1522 | break; |
| 1523 | } |
| 1524 | #endif |
| 1525 | |
| 1526 | case kTwo_ColorType: { |
| 1527 | const char* t = gradientTValue; |
| 1528 | const char* colors = uniformHandler->getUniformCStr(fColorsUni); |
| 1529 | |
| 1530 | fragBuilder->codeAppendf("vec4 colorTemp = mix(%s[0], %s[1], clamp(%s, 0.0, 1.0));", |
| 1531 | colors, colors, t); |
| 1532 | |
| 1533 | // We could skip this step if both colors are known to be opaque. Two |
| 1534 | // considerations: |
| 1535 | // The gradient SkShader reporting opaque is more restrictive than necessary in the two |
| 1536 | // pt case. Make sure the key reflects this optimization (and note that it can use the |
| 1537 | // same shader as thekBeforeIterp case). This same optimization applies to the 3 color |
| 1538 | // case below. |
| 1539 | if (GrGradientEffect::kAfterInterp_PremulType == ge.getPremulType()) { |
| 1540 | fragBuilder->codeAppend("colorTemp.rgb *= colorTemp.a;"); |
| 1541 | } |
Brian Salomon | c5ca7f9 | 2017-02-21 14:35:11 -0500 | [diff] [blame] | 1542 | if (ge.fColorSpaceXform) { |
| 1543 | fragBuilder->codeAppend("colorTemp.rgb = clamp(colorTemp.rgb, 0, colorTemp.a);"); |
| 1544 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1545 | |
| 1546 | fragBuilder->codeAppendf("%s = %s;", outputColor, |
| 1547 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
| 1548 | |
| 1549 | break; |
| 1550 | } |
| 1551 | |
| 1552 | case kThree_ColorType: { |
| 1553 | const char* t = gradientTValue; |
| 1554 | const char* colors = uniformHandler->getUniformCStr(fColorsUni); |
| 1555 | |
| 1556 | fragBuilder->codeAppendf("float oneMinus2t = 1.0 - (2.0 * %s);", t); |
| 1557 | fragBuilder->codeAppendf("vec4 colorTemp = clamp(oneMinus2t, 0.0, 1.0) * %s[0];", |
| 1558 | colors); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 1559 | if (!shaderCaps->canUseMinAndAbsTogether()) { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1560 | // The Tegra3 compiler will sometimes never return if we have |
| 1561 | // min(abs(oneMinus2t), 1.0), or do the abs first in a separate expression. |
| 1562 | fragBuilder->codeAppendf("float minAbs = abs(oneMinus2t);"); |
| 1563 | fragBuilder->codeAppendf("minAbs = minAbs > 1.0 ? 1.0 : minAbs;"); |
| 1564 | fragBuilder->codeAppendf("colorTemp += (1.0 - minAbs) * %s[1];", colors); |
| 1565 | } else { |
| 1566 | fragBuilder->codeAppendf("colorTemp += (1.0 - min(abs(oneMinus2t), 1.0)) * %s[1];", |
| 1567 | colors); |
| 1568 | } |
| 1569 | fragBuilder->codeAppendf("colorTemp += clamp(-oneMinus2t, 0.0, 1.0) * %s[2];", colors); |
| 1570 | |
| 1571 | if (GrGradientEffect::kAfterInterp_PremulType == ge.getPremulType()) { |
| 1572 | fragBuilder->codeAppend("colorTemp.rgb *= colorTemp.a;"); |
| 1573 | } |
Brian Salomon | c5ca7f9 | 2017-02-21 14:35:11 -0500 | [diff] [blame] | 1574 | if (ge.fColorSpaceXform) { |
| 1575 | fragBuilder->codeAppend("colorTemp.rgb = clamp(colorTemp.rgb, 0, colorTemp.a);"); |
| 1576 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1577 | |
| 1578 | fragBuilder->codeAppendf("%s = %s;", outputColor, |
| 1579 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); |
| 1580 | |
| 1581 | break; |
| 1582 | } |
| 1583 | |
| 1584 | case kTexture_ColorType: { |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame] | 1585 | fColorSpaceHelper.emitCode(uniformHandler, ge.fColorSpaceXform.get()); |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1586 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1587 | const char* fsyuni = uniformHandler->getUniformCStr(fFSYUni); |
| 1588 | |
| 1589 | fragBuilder->codeAppendf("vec2 coord = vec2(%s, %s);", gradientTValue, fsyuni); |
| 1590 | fragBuilder->codeAppendf("%s = ", outputColor); |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1591 | fragBuilder->appendTextureLookupAndModulate(inputColor, texSamplers[0], "coord", |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame] | 1592 | kVec2f_GrSLType, &fColorSpaceHelper); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1593 | fragBuilder->codeAppend(";"); |
| 1594 | |
| 1595 | break; |
| 1596 | } |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1597 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | ///////////////////////////////////////////////////////////////////// |
| 1601 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 1602 | inline GrFragmentProcessor::OptimizationFlags GrGradientEffect::OptFlags(bool isOpaque) { |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 1603 | return isOpaque |
| 1604 | ? kPreservesOpaqueInput_OptimizationFlag | |
| 1605 | kCompatibleWithCoverageAsAlpha_OptimizationFlag |
| 1606 | : kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | GrGradientEffect::GrGradientEffect(const CreateArgs& args, bool isOpaque) |
| 1610 | : INHERITED(OptFlags(isOpaque)) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1611 | const SkGradientShaderBase& shader(*args.fShader); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1612 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 1613 | fIsOpaque = shader.isOpaque(); |
| 1614 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1615 | fColorType = this->determineColorType(shader); |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1616 | fColorSpaceXform = std::move(args.fColorSpaceXform); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 1617 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1618 | if (kTexture_ColorType != fColorType) { |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 1619 | SkASSERT(shader.fOrigColors && shader.fOrigColors4f); |
| 1620 | if (args.fGammaCorrect) { |
| 1621 | fColors4f = SkTDArray<SkColor4f>(shader.fOrigColors4f, shader.fColorCount); |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 1622 | } else { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1623 | fColors = SkTDArray<SkColor>(shader.fOrigColors, shader.fColorCount); |
halcanary | 8a822ba | 2016-08-12 12:35:51 -0700 | [diff] [blame] | 1624 | } |
fmenozzi | 2a49591 | 2016-08-12 06:33:52 -0700 | [diff] [blame] | 1625 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1626 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
| 1627 | if (shader.fOrigPos) { |
| 1628 | fPositions = SkTDArray<SkScalar>(shader.fOrigPos, shader.fColorCount); |
halcanary | 8a822ba | 2016-08-12 12:35:51 -0700 | [diff] [blame] | 1629 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1630 | #endif |
fmenozzi | 2a49591 | 2016-08-12 06:33:52 -0700 | [diff] [blame] | 1631 | } |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1632 | |
| 1633 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1634 | fTileMode = args.fTileMode; |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1635 | #endif |
| 1636 | |
| 1637 | switch (fColorType) { |
| 1638 | // The two and three color specializations do not currently support tiling. |
| 1639 | case kTwo_ColorType: |
| 1640 | case kThree_ColorType: |
| 1641 | #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS |
| 1642 | case kHardStopLeftEdged_ColorType: |
| 1643 | case kHardStopRightEdged_ColorType: |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1644 | case kSingleHardStop_ColorType: |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1645 | #endif |
| 1646 | fRow = -1; |
| 1647 | |
| 1648 | if (SkGradientShader::kInterpolateColorsInPremul_Flag & shader.getGradFlags()) { |
| 1649 | fPremulType = kBeforeInterp_PremulType; |
| 1650 | } else { |
| 1651 | fPremulType = kAfterInterp_PremulType; |
| 1652 | } |
| 1653 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 1654 | fCoordTransform.reset(*args.fMatrix); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1655 | |
| 1656 | break; |
| 1657 | case kTexture_ColorType: |
| 1658 | // doesn't matter how this is set, just be consistent because it is part of the |
| 1659 | // effect key. |
| 1660 | fPremulType = kBeforeInterp_PremulType; |
| 1661 | |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1662 | SkGradientShaderBase::GradientBitmapType bitmapType = |
| 1663 | SkGradientShaderBase::GradientBitmapType::kLegacy; |
| 1664 | if (args.fGammaCorrect) { |
| 1665 | // Try to use F16 if we can |
| 1666 | if (args.fContext->caps()->isConfigTexturable(kRGBA_half_GrPixelConfig)) { |
| 1667 | bitmapType = SkGradientShaderBase::GradientBitmapType::kHalfFloat; |
| 1668 | } else if (args.fContext->caps()->isConfigTexturable(kSRGBA_8888_GrPixelConfig)) { |
| 1669 | bitmapType = SkGradientShaderBase::GradientBitmapType::kSRGB; |
| 1670 | } else { |
brianosman | c68e892 | 2016-09-22 14:27:34 -0700 | [diff] [blame] | 1671 | // This can happen, but only if someone explicitly creates an unsupported |
| 1672 | // (eg sRGB) surface. Just fall back to legacy behavior. |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1676 | SkBitmap bitmap; |
brianosman | d454609 | 2016-09-22 12:31:58 -0700 | [diff] [blame] | 1677 | shader.getGradientTableBitmap(&bitmap, bitmapType); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 1678 | SkASSERT(1 == bitmap.height() && SkIsPow2(bitmap.width())); |
| 1679 | |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1680 | |
| 1681 | GrTextureStripAtlas::Desc desc; |
| 1682 | desc.fWidth = bitmap.width(); |
| 1683 | desc.fHeight = 32; |
| 1684 | desc.fRowHeight = bitmap.height(); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1685 | desc.fContext = args.fContext; |
| 1686 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info(), *args.fContext->caps()); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1687 | fAtlas = GrTextureStripAtlas::GetAtlas(desc); |
| 1688 | SkASSERT(fAtlas); |
| 1689 | |
| 1690 | // We always filter the gradient table. Each table is one row of a texture, always |
| 1691 | // y-clamp. |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 1692 | GrSamplerParams params; |
| 1693 | params.setFilterMode(GrSamplerParams::kBilerp_FilterMode); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1694 | params.setTileModeX(args.fTileMode); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1695 | |
| 1696 | fRow = fAtlas->lockRow(bitmap); |
| 1697 | if (-1 != fRow) { |
| 1698 | fYCoord = fAtlas->getYOffset(fRow)+SK_ScalarHalf*fAtlas->getNormalizedTexelHeight(); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 1699 | // This is 1/2 places where auto-normalization is disabled |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 1700 | fCoordTransform.reset(args.fContext, *args.fMatrix, |
| 1701 | fAtlas->asTextureProxyRef().get(), |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 1702 | params.filterMode(), false); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 1703 | fTextureSampler.reset(args.fContext->resourceProvider(), |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 1704 | fAtlas->asTextureProxyRef(), params); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1705 | } else { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 1706 | // In this instance we know the params are: |
| 1707 | // clampY, bilerp |
| 1708 | // and the proxy is: |
| 1709 | // exact fit, power of two in both dimensions |
| 1710 | // Only the x-tileMode is unknown. However, given all the other knowns we know |
| 1711 | // that GrMakeCachedBitmapProxy is sufficient (i.e., it won't need to be |
| 1712 | // extracted to a subset or mipmapped). |
| 1713 | sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(args.fContext, bitmap); |
| 1714 | if (!proxy) { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1715 | return; |
| 1716 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 1717 | // This is 2/2 places where auto-normalization is disabled |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 1718 | fCoordTransform.reset(args.fContext, *args.fMatrix, |
| 1719 | proxy.get(), params.filterMode(), false); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 1720 | fTextureSampler.reset(args.fContext->resourceProvider(), |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 1721 | std::move(proxy), params); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1722 | fYCoord = SK_ScalarHalf; |
| 1723 | } |
| 1724 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 1725 | this->addTextureSampler(&fTextureSampler); |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1726 | |
| 1727 | break; |
| 1728 | } |
| 1729 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1730 | this->addCoordTransform(&fCoordTransform); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | GrGradientEffect::~GrGradientEffect() { |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 1734 | if (this->useAtlas()) { |
| 1735 | fAtlas->unlockRow(fRow); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 1736 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 1739 | bool GrGradientEffect::onIsEqual(const GrFragmentProcessor& processor) const { |
fmenozzi | cd9a1d0 | 2016-08-15 07:03:47 -0700 | [diff] [blame] | 1740 | const GrGradientEffect& ge = processor.cast<GrGradientEffect>(); |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1741 | |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1742 | if (this->fColorType != ge.getColorType()) { |
| 1743 | return false; |
| 1744 | } |
| 1745 | SkASSERT(this->useAtlas() == ge.useAtlas()); |
| 1746 | if (kTexture_ColorType == fColorType) { |
| 1747 | if (fYCoord != ge.getYCoord()) { |
| 1748 | return false; |
| 1749 | } |
| 1750 | } else { |
| 1751 | if (kSingleHardStop_ColorType == fColorType) { |
| 1752 | if (!SkScalarNearlyEqual(ge.fPositions[1], fPositions[1])) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1753 | return false; |
| 1754 | } |
| 1755 | } |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1756 | if (this->getPremulType() != ge.getPremulType() || |
| 1757 | this->fColors.count() != ge.fColors.count() || |
| 1758 | this->fColors4f.count() != ge.fColors4f.count()) { |
| 1759 | return false; |
| 1760 | } |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 1761 | |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1762 | for (int i = 0; i < this->fColors.count(); i++) { |
| 1763 | if (*this->getColors(i) != *ge.getColors(i)) { |
| 1764 | return false; |
| 1765 | } |
| 1766 | } |
| 1767 | for (int i = 0; i < this->fColors4f.count(); i++) { |
| 1768 | if (*this->getColors4f(i) != *ge.getColors4f(i)) { |
| 1769 | return false; |
| 1770 | } |
| 1771 | } |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 1772 | } |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 1773 | return GrColorSpaceXform::Equals(this->fColorSpaceXform.get(), ge.fColorSpaceXform.get()); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1776 | #if GR_TEST_UTILS |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 1777 | GrGradientEffect::RandomGradientParams::RandomGradientParams(SkRandom* random) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 1778 | // Set color count to min of 2 so that we don't trigger the const color optimization and make |
| 1779 | // a non-gradient processor. |
| 1780 | fColorCount = random->nextRangeU(2, kMaxRandomGradientColors); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 1781 | fUseColors4f = random->nextBool(); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 1782 | |
| 1783 | // if one color, omit stops, otherwise randomly decide whether or not to |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 1784 | if (fColorCount == 1 || (fColorCount >= 2 && random->nextBool())) { |
| 1785 | fStops = nullptr; |
| 1786 | } else { |
| 1787 | fStops = fStopStorage; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 1790 | // if using SkColor4f, attach a random (possibly null) color space (with linear gamma) |
| 1791 | if (fUseColors4f) { |
| 1792 | fColorSpace = GrTest::TestColorSpace(random); |
| 1793 | if (fColorSpace) { |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 1794 | SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(fColorSpace)->type()); |
| 1795 | fColorSpace = static_cast<SkColorSpace_XYZ*>(fColorSpace.get())->makeLinearGamma(); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 1796 | } |
| 1797 | } |
| 1798 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 1799 | SkScalar stop = 0.f; |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 1800 | for (int i = 0; i < fColorCount; ++i) { |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 1801 | if (fUseColors4f) { |
| 1802 | fColors4f[i].fR = random->nextUScalar1(); |
| 1803 | fColors4f[i].fG = random->nextUScalar1(); |
| 1804 | fColors4f[i].fB = random->nextUScalar1(); |
| 1805 | fColors4f[i].fA = random->nextUScalar1(); |
| 1806 | } else { |
| 1807 | fColors[i] = random->nextU(); |
| 1808 | } |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 1809 | if (fStops) { |
| 1810 | fStops[i] = stop; |
| 1811 | stop = i < fColorCount - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 1814 | fTileMode = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileModeCount)); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 1815 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1816 | #endif |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 1817 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1818 | #endif |