rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 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" |
Matt Sarett | 6cc6ae75 | 2017-04-18 18:29:12 -0400 | [diff] [blame] | 9 | #include "SkColorSpaceXformer.h" |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 10 | #include "SkLinearGradient.h" |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 11 | #include "SkRefCnt.h" |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 12 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 13 | // define to test the 4f gradient path |
fmalita | 55430a6 | 2016-02-23 13:26:28 -0800 | [diff] [blame] | 14 | // #define FORCE_4F_CONTEXT |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 15 | |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 16 | static const float kInv255Float = 1.0f / 255; |
| 17 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 18 | static inline int repeat_8bits(int x) { |
| 19 | return x & 0xFF; |
| 20 | } |
| 21 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 22 | static inline int mirror_8bits(int x) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 23 | if (x & 256) { |
| 24 | x = ~x; |
| 25 | } |
| 26 | return x & 255; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 27 | } |
| 28 | |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 29 | static SkMatrix pts_to_unit_matrix(const SkPoint pts[2]) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 30 | SkVector vec = pts[1] - pts[0]; |
| 31 | SkScalar mag = vec.length(); |
| 32 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 33 | |
| 34 | vec.scale(inv); |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 35 | SkMatrix matrix; |
| 36 | matrix.setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 37 | matrix.postTranslate(-pts[0].fX, -pts[0].fY); |
| 38 | matrix.postScale(inv, inv); |
| 39 | return matrix; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Florin Malita | 58a7564 | 2017-05-24 15:33:28 -0400 | [diff] [blame^] | 42 | static bool use_4f_context(const SkShaderBase::ContextRec& rec, uint32_t flags) { |
fmalita | 55430a6 | 2016-02-23 13:26:28 -0800 | [diff] [blame] | 43 | #ifdef FORCE_4F_CONTEXT |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 44 | return true; |
| 45 | #else |
Florin Malita | 58a7564 | 2017-05-24 15:33:28 -0400 | [diff] [blame^] | 46 | return rec.fPreferredDstType == SkShaderBase::ContextRec::kPM4f_DstType |
fmalita | 55430a6 | 2016-02-23 13:26:28 -0800 | [diff] [blame] | 47 | || SkToBool(flags & SkLinearGradient::kForce4fContext_PrivateFlag); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 48 | #endif |
| 49 | } |
| 50 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
reed | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 53 | SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const Descriptor& desc) |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 54 | : SkGradientShaderBase(desc, pts_to_unit_matrix(pts)) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 55 | , fStart(pts[0]) |
mtklein | cc695fe | 2014-12-10 10:29:19 -0800 | [diff] [blame] | 56 | , fEnd(pts[1]) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 59 | sk_sp<SkFlattenable> SkLinearGradient::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 60 | DescriptorScope desc; |
| 61 | if (!desc.unflatten(buffer)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 63 | } |
| 64 | SkPoint pts[2]; |
| 65 | pts[0] = buffer.readPoint(); |
| 66 | pts[1] = buffer.readPoint(); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 67 | return SkGradientShader::MakeLinear(pts, desc.fColors, std::move(desc.fColorSpace), desc.fPos, |
| 68 | desc.fCount, desc.fTileMode, desc.fGradFlags, |
| 69 | desc.fLocalMatrix); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 70 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 72 | void SkLinearGradient::flatten(SkWriteBuffer& buffer) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 73 | this->INHERITED::flatten(buffer); |
| 74 | buffer.writePoint(fStart); |
| 75 | buffer.writePoint(fEnd); |
| 76 | } |
| 77 | |
Florin Malita | 58a7564 | 2017-05-24 15:33:28 -0400 | [diff] [blame^] | 78 | SkShaderBase::Context* SkLinearGradient::onMakeContext( |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 79 | const ContextRec& rec, SkArenaAlloc* alloc) const |
| 80 | { |
fmalita | 55430a6 | 2016-02-23 13:26:28 -0800 | [diff] [blame] | 81 | return use_4f_context(rec, fGradFlags) |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 82 | ? CheckedMakeContext<LinearGradient4fContext>(alloc, *this, rec) |
| 83 | : CheckedMakeContext< LinearGradientContext>(alloc, *this, rec); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Mike Klein | a377184 | 2017-05-04 19:38:48 -0400 | [diff] [blame] | 86 | bool SkLinearGradient::adjustMatrixAndAppendStages(SkArenaAlloc* alloc, |
| 87 | SkMatrix* matrix, |
| 88 | SkRasterPipeline* p) const { |
| 89 | *matrix = SkMatrix::Concat(fPtsToUnit, *matrix); |
| 90 | // If the gradient is less than a quarter of a pixel, this falls into the |
| 91 | // subpixel gradient code handled on a different path. |
| 92 | SkVector dx = matrix->mapVector(1, 0); |
Mike Klein | e759853 | 2017-05-11 11:29:29 -0400 | [diff] [blame] | 93 | if (dx.fX >= 4) { |
| 94 | return false; |
Herb Derby | 7b4202d | 2017-04-10 10:52:34 -0400 | [diff] [blame] | 95 | } |
Florin Malita | c86e470 | 2017-01-20 08:41:34 -0500 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
Matt Sarett | 6cc6ae75 | 2017-04-18 18:29:12 -0400 | [diff] [blame] | 99 | sk_sp<SkShader> SkLinearGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
| 100 | SkPoint pts[2] = { fStart, fEnd }; |
| 101 | SkSTArray<8, SkColor> xformedColors(fColorCount); |
| 102 | xformer->apply(xformedColors.begin(), fOrigColors, fColorCount); |
| 103 | return SkGradientShader::MakeLinear(pts, xformedColors.begin(), fOrigPos, fColorCount, |
| 104 | fTileMode, fGradFlags, &this->getLocalMatrix()); |
| 105 | } |
| 106 | |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 107 | // This swizzles SkColor into the same component order as SkPMColor, but does not actually |
| 108 | // "pre" multiply the color components. |
| 109 | // |
| 110 | // This allows us to map directly to Sk4f, and eventually scale down to bytes to output a |
| 111 | // SkPMColor from the floats, without having to swizzle each time. |
| 112 | // |
| 113 | static uint32_t SkSwizzle_Color_to_PMColor(SkColor c) { |
| 114 | return SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c)); |
| 115 | } |
| 116 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 117 | SkLinearGradient::LinearGradientContext::LinearGradientContext( |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 118 | const SkLinearGradient& shader, const ContextRec& ctx) |
| 119 | : INHERITED(shader, ctx) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 120 | { |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 121 | // setup for Sk4f |
fmalita | 748d620 | 2016-05-11 11:39:58 -0700 | [diff] [blame] | 122 | const int count = shader.fColorCount; |
| 123 | SkASSERT(count > 1); |
| 124 | |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 125 | fRecs.setCount(count); |
| 126 | Rec* rec = fRecs.begin(); |
| 127 | if (shader.fOrigPos) { |
| 128 | rec[0].fPos = 0; |
| 129 | SkDEBUGCODE(rec[0].fPosScale = SK_FloatNaN;) // should never get used |
| 130 | for (int i = 1; i < count; ++i) { |
| 131 | rec[i].fPos = SkTPin(shader.fOrigPos[i], rec[i - 1].fPos, 1.0f); |
lsalzman | f2b8662 | 2016-01-22 14:03:02 -0800 | [diff] [blame] | 132 | float diff = rec[i].fPos - rec[i - 1].fPos; |
| 133 | if (diff > 0) { |
| 134 | rec[i].fPosScale = 1.0f / diff; |
| 135 | } else { |
| 136 | rec[i].fPosScale = 0; |
| 137 | } |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 138 | } |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 139 | } else { |
| 140 | // no pos specified, so we compute evenly spaced values |
| 141 | const float scale = float(count - 1); |
fmalita | 748d620 | 2016-05-11 11:39:58 -0700 | [diff] [blame] | 142 | const float invScale = 1.0f / scale; |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 143 | for (int i = 0; i < count; ++i) { |
| 144 | rec[i].fPos = i * invScale; |
| 145 | rec[i].fPosScale = scale; |
| 146 | } |
| 147 | } |
fmalita | 748d620 | 2016-05-11 11:39:58 -0700 | [diff] [blame] | 148 | rec[count - 1].fPos = 1; // overwrite the last value just to be sure we end at 1.0 |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 149 | |
| 150 | fApplyAlphaAfterInterp = true; |
| 151 | if ((shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag) || |
| 152 | shader.colorsAreOpaque()) |
| 153 | { |
| 154 | fApplyAlphaAfterInterp = false; |
| 155 | } |
| 156 | |
| 157 | if (fApplyAlphaAfterInterp) { |
| 158 | // Our fColor values are in PMColor order, but are still unpremultiplied, allowing us to |
| 159 | // interpolate in unpremultiplied space first, and then scale by alpha right before we |
| 160 | // convert to SkPMColor bytes. |
| 161 | const float paintAlpha = ctx.fPaint->getAlpha() * kInv255Float; |
| 162 | const Sk4f scale(1, 1, 1, paintAlpha); |
| 163 | for (int i = 0; i < count; ++i) { |
| 164 | uint32_t c = SkSwizzle_Color_to_PMColor(shader.fOrigColors[i]); |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 165 | rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&c)) * scale; |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 166 | if (i > 0) { |
| 167 | SkASSERT(rec[i - 1].fPos <= rec[i].fPos); |
| 168 | } |
| 169 | } |
| 170 | } else { |
| 171 | // Our fColor values are premultiplied, so converting to SkPMColor is just a matter |
| 172 | // of converting the floats down to bytes. |
| 173 | unsigned alphaScale = ctx.fPaint->getAlpha() + (ctx.fPaint->getAlpha() >> 7); |
| 174 | for (int i = 0; i < count; ++i) { |
| 175 | SkPMColor pmc = SkPreMultiplyColor(shader.fOrigColors[i]); |
| 176 | pmc = SkAlphaMulQ(pmc, alphaScale); |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 177 | rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&pmc)); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 178 | if (i > 0) { |
| 179 | SkASSERT(rec[i - 1].fPos <= rec[i].fPos); |
| 180 | } |
| 181 | } |
| 182 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | #define NO_CHECK_ITER \ |
| 186 | do { \ |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 187 | unsigned fi = SkGradFixedToFixed(fx) >> SkGradientShaderBase::kCache32Shift; \ |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 188 | SkASSERT(fi <= 0xFF); \ |
| 189 | fx += dx; \ |
| 190 | *dstC++ = cache[toggle + fi]; \ |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 191 | toggle = next_dither_toggle(toggle); \ |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 192 | } while (0) |
| 193 | |
| 194 | namespace { |
| 195 | |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 196 | typedef void (*LinearShadeProc)(TileProc proc, SkGradFixed dx, SkGradFixed fx, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 197 | SkPMColor* dstC, const SkPMColor* cache, |
| 198 | int toggle, int count); |
| 199 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 200 | // Linear interpolation (lerp) is unnecessary if there are no sharp |
| 201 | // discontinuities in the gradient - which must be true if there are |
| 202 | // only 2 colors - but it's cheap. |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 203 | void shadeSpan_linear_vertical_lerp(TileProc proc, SkGradFixed dx, SkGradFixed fx, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 204 | SkPMColor* SK_RESTRICT dstC, |
| 205 | const SkPMColor* SK_RESTRICT cache, |
| 206 | int toggle, int count) { |
| 207 | // We're a vertical gradient, so no change in a span. |
| 208 | // If colors change sharply across the gradient, dithering is |
| 209 | // insufficient (it subsamples the color space) and we need to lerp. |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 210 | unsigned fullIndex = proc(SkGradFixedToFixed(fx)); |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 211 | unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift; |
| 212 | unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1); |
skia.committer@gmail.com | 9dde018 | 2013-02-04 12:57:42 +0000 | [diff] [blame] | 213 | |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 214 | int index0 = fi + toggle; |
| 215 | int index1 = index0; |
| 216 | if (fi < SkGradientShaderBase::kCache32Count - 1) { |
| 217 | index1 += 1; |
| 218 | } |
| 219 | SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder); |
| 220 | index0 ^= SkGradientShaderBase::kDitherStride32; |
| 221 | index1 ^= SkGradientShaderBase::kDitherStride32; |
| 222 | SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 223 | sk_memset32_dither(dstC, lerp, dlerp, count); |
| 224 | } |
| 225 | |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 226 | void shadeSpan_linear_clamp(TileProc proc, SkGradFixed dx, SkGradFixed fx, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 227 | SkPMColor* SK_RESTRICT dstC, |
| 228 | const SkPMColor* SK_RESTRICT cache, |
| 229 | int toggle, int count) { |
| 230 | SkClampRange range; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 231 | range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1); |
reed | 9d91eb3 | 2015-01-28 11:44:48 -0800 | [diff] [blame] | 232 | range.validate(count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 233 | |
| 234 | if ((count = range.fCount0) > 0) { |
| 235 | sk_memset32_dither(dstC, |
| 236 | cache[toggle + range.fV0], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 237 | cache[next_dither_toggle(toggle) + range.fV0], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 238 | count); |
| 239 | dstC += count; |
| 240 | } |
| 241 | if ((count = range.fCount1) > 0) { |
| 242 | int unroll = count >> 3; |
| 243 | fx = range.fFx1; |
| 244 | for (int i = 0; i < unroll; i++) { |
| 245 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 246 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 247 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 248 | NO_CHECK_ITER; NO_CHECK_ITER; |
| 249 | } |
| 250 | if ((count &= 7) > 0) { |
| 251 | do { |
| 252 | NO_CHECK_ITER; |
| 253 | } while (--count != 0); |
| 254 | } |
| 255 | } |
| 256 | if ((count = range.fCount2) > 0) { |
| 257 | sk_memset32_dither(dstC, |
| 258 | cache[toggle + range.fV1], |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 259 | cache[next_dither_toggle(toggle) + range.fV1], |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 260 | count); |
| 261 | } |
| 262 | } |
| 263 | |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 264 | void shadeSpan_linear_mirror(TileProc proc, SkGradFixed dx, SkGradFixed fx, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 265 | SkPMColor* SK_RESTRICT dstC, |
| 266 | const SkPMColor* SK_RESTRICT cache, |
| 267 | int toggle, int count) { |
| 268 | do { |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 269 | unsigned fi = mirror_8bits(SkGradFixedToFixed(fx) >> 8); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 270 | SkASSERT(fi <= 0xFF); |
| 271 | fx += dx; |
| 272 | *dstC++ = cache[toggle + fi]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 273 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 274 | } while (--count != 0); |
| 275 | } |
| 276 | |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 277 | void shadeSpan_linear_repeat(TileProc proc, SkGradFixed dx, SkGradFixed fx, |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 278 | SkPMColor* SK_RESTRICT dstC, |
| 279 | const SkPMColor* SK_RESTRICT cache, |
| 280 | int toggle, int count) { |
| 281 | do { |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 282 | unsigned fi = repeat_8bits(SkGradFixedToFixed(fx) >> 8); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 283 | SkASSERT(fi <= 0xFF); |
| 284 | fx += dx; |
| 285 | *dstC++ = cache[toggle + fi]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 286 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 287 | } while (--count != 0); |
| 288 | } |
| 289 | |
| 290 | } |
| 291 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 292 | void SkLinearGradient::LinearGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, |
| 293 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 294 | SkASSERT(count > 0); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 295 | const SkLinearGradient& linearGradient = static_cast<const SkLinearGradient&>(fShader); |
| 296 | |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 297 | if (SkShader::kClamp_TileMode == linearGradient.fTileMode && |
| 298 | kLinear_MatrixClass == fDstToIndexClass) |
| 299 | { |
| 300 | this->shade4_clamp(x, y, dstC, count); |
| 301 | return; |
| 302 | } |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 303 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 304 | SkPoint srcPt; |
| 305 | SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 306 | TileProc proc = linearGradient.fTileProc; |
| 307 | const SkPMColor* SK_RESTRICT cache = fCache->getCache32(); |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 308 | int toggle = init_dither_toggle(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 309 | |
| 310 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 311 | dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
| 312 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
Florin Malita | 63b6156 | 2017-02-10 10:42:49 -0500 | [diff] [blame] | 313 | SkGradFixed dx, fx = SkScalarPinToGradFixed(srcPt.fX); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 314 | |
| 315 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
benjaminwagner | 8e17556 | 2016-02-16 10:09:40 -0800 | [diff] [blame] | 316 | const auto step = fDstToIndex.fixedStepInX(SkIntToScalar(y)); |
reed | caf7e93 | 2014-12-18 12:43:08 -0800 | [diff] [blame] | 317 | // todo: do we need a real/high-precision value for dx here? |
Florin Malita | 63b6156 | 2017-02-10 10:42:49 -0500 | [diff] [blame] | 318 | dx = SkScalarPinToGradFixed(step.fX); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 319 | } else { |
| 320 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
Florin Malita | 63b6156 | 2017-02-10 10:42:49 -0500 | [diff] [blame] | 321 | dx = SkScalarPinToGradFixed(fDstToIndex.getScaleX()); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | LinearShadeProc shadeProc = shadeSpan_linear_repeat; |
reed@google.com | 53009ba | 2013-02-07 20:28:49 +0000 | [diff] [blame] | 325 | if (0 == dx) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 326 | shadeProc = shadeSpan_linear_vertical_lerp; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 327 | } else if (SkShader::kClamp_TileMode == linearGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 328 | shadeProc = shadeSpan_linear_clamp; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 329 | } else if (SkShader::kMirror_TileMode == linearGradient.fTileMode) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 330 | shadeProc = shadeSpan_linear_mirror; |
| 331 | } else { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 332 | SkASSERT(SkShader::kRepeat_TileMode == linearGradient.fTileMode); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 333 | } |
| 334 | (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count); |
| 335 | } else { |
| 336 | SkScalar dstX = SkIntToScalar(x); |
| 337 | SkScalar dstY = SkIntToScalar(y); |
| 338 | do { |
| 339 | dstProc(fDstToIndex, dstX, dstY, &srcPt); |
| 340 | unsigned fi = proc(SkScalarToFixed(srcPt.fX)); |
| 341 | SkASSERT(fi <= 0xFFFF); |
| 342 | *dstC++ = cache[toggle + (fi >> kCache32Shift)]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 343 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 344 | dstX += SK_Scalar1; |
| 345 | } while (--count != 0); |
| 346 | } |
| 347 | } |
| 348 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 349 | SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const { |
| 350 | if (info) { |
| 351 | commonAsAGradient(info); |
| 352 | info->fPoint[0] = fStart; |
| 353 | info->fPoint[1] = fEnd; |
| 354 | } |
| 355 | return kLinear_GradientType; |
| 356 | } |
| 357 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 358 | #if SK_SUPPORT_GPU |
| 359 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 360 | #include "GrColorSpaceXform.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 361 | #include "GrShaderCaps.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 362 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 363 | #include "SkGr.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 364 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 365 | ///////////////////////////////////////////////////////////////////// |
| 366 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 367 | class GrLinearGradient : public GrGradientEffect { |
| 368 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 369 | class GLSLLinearProcessor; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 370 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 371 | static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) { |
| 372 | return sk_sp<GrFragmentProcessor>(new GrLinearGradient(args)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 375 | ~GrLinearGradient() override {} |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 376 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 377 | const char* name() const override { return "Linear Gradient"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 378 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 379 | private: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 380 | GrLinearGradient(const CreateArgs& args) : INHERITED(args, args.fShader->colorsAreOpaque()) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 381 | this->initClassID<GrLinearGradient>(); |
| 382 | } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 383 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 384 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 385 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 386 | virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps, |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 387 | GrProcessorKeyBuilder* b) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 388 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 389 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 390 | |
| 391 | typedef GrGradientEffect INHERITED; |
| 392 | }; |
| 393 | |
| 394 | ///////////////////////////////////////////////////////////////////// |
| 395 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 396 | class GrLinearGradient::GLSLLinearProcessor : public GrGradientEffect::GLSLProcessor { |
| 397 | public: |
| 398 | GLSLLinearProcessor(const GrProcessor&) {} |
| 399 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 400 | ~GLSLLinearProcessor() override {} |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 401 | |
| 402 | virtual void emitCode(EmitArgs&) override; |
| 403 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 404 | static void GenKey(const GrProcessor& processor, const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 405 | b->add32(GenBaseGradientKey(processor)); |
| 406 | } |
| 407 | |
| 408 | private: |
| 409 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
| 410 | }; |
| 411 | |
| 412 | ///////////////////////////////////////////////////////////////////// |
| 413 | |
| 414 | GrGLSLFragmentProcessor* GrLinearGradient::onCreateGLSLInstance() const { |
| 415 | return new GrLinearGradient::GLSLLinearProcessor(*this); |
| 416 | } |
| 417 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 418 | void GrLinearGradient::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 419 | GrProcessorKeyBuilder* b) const { |
| 420 | GrLinearGradient::GLSLLinearProcessor::GenKey(*this, caps, b); |
| 421 | } |
| 422 | |
| 423 | ///////////////////////////////////////////////////////////////////// |
| 424 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 425 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrLinearGradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 426 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 427 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 428 | sk_sp<GrFragmentProcessor> GrLinearGradient::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 429 | SkPoint points[] = {{d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}, |
| 430 | {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}}; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 431 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 432 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 433 | auto shader = params.fUseColors4f ? |
| 434 | SkGradientShader::MakeLinear(points, params.fColors4f, params.fColorSpace, params.fStops, |
| 435 | params.fColorCount, params.fTileMode) : |
| 436 | SkGradientShader::MakeLinear(points, params.fColors, params.fStops, |
| 437 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 438 | GrTest::TestAsFPArgs asFPArgs(d); |
Florin Malita | 58a7564 | 2017-05-24 15:33:28 -0400 | [diff] [blame^] | 439 | sk_sp<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 440 | GrAlwaysAssert(fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 441 | return fp; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 442 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 443 | #endif |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 444 | |
| 445 | ///////////////////////////////////////////////////////////////////// |
| 446 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 447 | void GrLinearGradient::GLSLLinearProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 448 | const GrLinearGradient& ge = args.fFp.cast<GrLinearGradient>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 449 | this->emitUniforms(args.fUniformHandler, ge); |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 450 | SkString t = args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 451 | t.append(".x"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 452 | this->emitColor(args.fFragBuilder, |
| 453 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 454 | args.fShaderCaps, |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 455 | ge, |
| 456 | t.c_str(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 457 | args.fOutputColor, |
| 458 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 459 | args.fTexSamplers); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | ///////////////////////////////////////////////////////////////////// |
| 463 | |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 464 | sk_sp<GrFragmentProcessor> SkLinearGradient::asFragmentProcessor(const AsFPArgs& args) const { |
| 465 | SkASSERT(args.fContext); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 466 | |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 467 | SkMatrix matrix; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 468 | if (!this->getLocalMatrix().invert(&matrix)) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 469 | return nullptr; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 470 | } |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 471 | if (args.fLocalMatrix) { |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 472 | SkMatrix inv; |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 473 | if (!args.fLocalMatrix->invert(&inv)) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 474 | return nullptr; |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 475 | } |
| 476 | matrix.postConcat(inv); |
| 477 | } |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 478 | matrix.postConcat(fPtsToUnit); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 479 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 480 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(), |
| 481 | args.fDstColorSpace); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 482 | sk_sp<GrFragmentProcessor> inner(GrLinearGradient::Make( |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 483 | GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode, |
| 484 | std::move(colorSpaceXform), SkToBool(args.fDstColorSpace)))); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 485 | return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 486 | } |
| 487 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 488 | |
| 489 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 490 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 491 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 492 | void SkLinearGradient::toString(SkString* str) const { |
| 493 | str->append("SkLinearGradient ("); |
| 494 | |
| 495 | str->appendf("start: (%f, %f)", fStart.fX, fStart.fY); |
| 496 | str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY); |
| 497 | |
| 498 | this->INHERITED::toString(str); |
| 499 | |
| 500 | str->append(")"); |
| 501 | } |
| 502 | #endif |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 503 | |
| 504 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 505 | |
| 506 | #include "SkNx.h" |
| 507 | |
| 508 | static const SkLinearGradient::LinearGradientContext::Rec* |
| 509 | find_forward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) { |
| 510 | SkASSERT(tiledX >= 0 && tiledX <= 1); |
| 511 | |
| 512 | SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1); |
| 513 | SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1); |
| 514 | SkASSERT(rec[0].fPos <= rec[1].fPos); |
| 515 | rec += 1; |
lsalzman | f2b8662 | 2016-01-22 14:03:02 -0800 | [diff] [blame] | 516 | while (rec->fPos < tiledX || rec->fPosScale == 0) { |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 517 | SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1); |
| 518 | SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1); |
| 519 | SkASSERT(rec[0].fPos <= rec[1].fPos); |
| 520 | rec += 1; |
| 521 | } |
| 522 | return rec - 1; |
| 523 | } |
| 524 | |
| 525 | static const SkLinearGradient::LinearGradientContext::Rec* |
| 526 | find_backward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) { |
| 527 | SkASSERT(tiledX >= 0 && tiledX <= 1); |
| 528 | |
| 529 | SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1); |
| 530 | SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1); |
| 531 | SkASSERT(rec[0].fPos <= rec[1].fPos); |
lsalzman | f2b8662 | 2016-01-22 14:03:02 -0800 | [diff] [blame] | 532 | while (tiledX < rec->fPos || rec[1].fPosScale == 0) { |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 533 | rec -= 1; |
| 534 | SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1); |
| 535 | SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1); |
| 536 | SkASSERT(rec[0].fPos <= rec[1].fPos); |
| 537 | } |
| 538 | return rec; |
| 539 | } |
| 540 | |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 541 | // As an optimization, we can apply the dither bias before interpolation -- but only when |
| 542 | // operating in premul space (apply_alpha == false). When apply_alpha == true, we must |
| 543 | // defer the bias application until after premul. |
| 544 | // |
| 545 | // The following two helpers encapsulate this logic: pre_bias is called before interpolation, |
| 546 | // and effects the bias when apply_alpha == false, while post_bias is called after premul and |
| 547 | // effects the bias for the apply_alpha == true case. |
| 548 | |
| 549 | template <bool apply_alpha> |
| 550 | Sk4f pre_bias(const Sk4f& x, const Sk4f& bias) { |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 551 | return apply_alpha ? x : x + bias; |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | template <bool apply_alpha> |
| 555 | Sk4f post_bias(const Sk4f& x, const Sk4f& bias) { |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 556 | return apply_alpha ? x + bias : x; |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | template <bool apply_alpha> SkPMColor trunc_from_255(const Sk4f& x, const Sk4f& bias) { |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 560 | SkPMColor c; |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 561 | Sk4f c4f255 = x; |
| 562 | if (apply_alpha) { |
| 563 | const float scale = x[SkPM4f::A] * (1 / 255.f); |
| 564 | c4f255 *= Sk4f(scale, scale, scale, 1); |
| 565 | } |
| 566 | SkNx_cast<uint8_t>(post_bias<apply_alpha>(c4f255, bias)).store(&c); |
Florin Malita | aaa6d77 | 2016-12-07 14:57:04 -0500 | [diff] [blame] | 567 | |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 568 | return c; |
| 569 | } |
| 570 | |
| 571 | template <bool apply_alpha> void fill(SkPMColor dst[], int count, |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 572 | const Sk4f& c4, const Sk4f& bias0, const Sk4f& bias1) { |
| 573 | const SkPMColor c0 = trunc_from_255<apply_alpha>(pre_bias<apply_alpha>(c4, bias0), bias0); |
| 574 | const SkPMColor c1 = trunc_from_255<apply_alpha>(pre_bias<apply_alpha>(c4, bias1), bias1); |
| 575 | sk_memset32_dither(dst, c0, c1, count); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | template <bool apply_alpha> void fill(SkPMColor dst[], int count, const Sk4f& c4) { |
| 579 | // Assumes that c4 does not need to be dithered. |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 580 | sk_memset32(dst, trunc_from_255<apply_alpha>(c4, 0), count); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
| 584 | * TODOs |
| 585 | * |
| 586 | * - tilemodes |
| 587 | * - interp before or after premul |
| 588 | * - perspective |
| 589 | * - optimizations |
| 590 | * - use fixed (32bit or 16bit) instead of floats? |
| 591 | */ |
| 592 | |
| 593 | static Sk4f lerp_color(float fx, const SkLinearGradient::LinearGradientContext::Rec* rec) { |
reed | de3aac8 | 2015-11-22 13:00:04 -0800 | [diff] [blame] | 594 | SkASSERT(fx >= rec[0].fPos); |
| 595 | SkASSERT(fx <= rec[1].fPos); |
| 596 | |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 597 | const float p0 = rec[0].fPos; |
| 598 | const Sk4f c0 = rec[0].fColor; |
| 599 | const Sk4f c1 = rec[1].fColor; |
| 600 | const Sk4f diffc = c1 - c0; |
| 601 | const float scale = rec[1].fPosScale; |
| 602 | const float t = (fx - p0) * scale; |
| 603 | return c0 + Sk4f(t) * diffc; |
| 604 | } |
| 605 | |
| 606 | template <bool apply_alpha> void ramp(SkPMColor dstC[], int n, const Sk4f& c, const Sk4f& dc, |
| 607 | const Sk4f& dither0, const Sk4f& dither1) { |
| 608 | Sk4f dc2 = dc + dc; |
| 609 | Sk4f dc4 = dc2 + dc2; |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 610 | Sk4f cd0 = pre_bias<apply_alpha>(c , dither0); |
| 611 | Sk4f cd1 = pre_bias<apply_alpha>(c + dc, dither1); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 612 | Sk4f cd2 = cd0 + dc2; |
| 613 | Sk4f cd3 = cd1 + dc2; |
| 614 | while (n >= 4) { |
mtklein | 9db43ac | 2015-12-01 07:10:21 -0800 | [diff] [blame] | 615 | if (!apply_alpha) { |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 616 | Sk4f_ToBytes((uint8_t*)dstC, cd0, cd1, cd2, cd3); |
mtklein | 9db43ac | 2015-12-01 07:10:21 -0800 | [diff] [blame] | 617 | dstC += 4; |
| 618 | } else { |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 619 | *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0); |
| 620 | *dstC++ = trunc_from_255<apply_alpha>(cd1, dither1); |
| 621 | *dstC++ = trunc_from_255<apply_alpha>(cd2, dither0); |
| 622 | *dstC++ = trunc_from_255<apply_alpha>(cd3, dither1); |
mtklein | 9db43ac | 2015-12-01 07:10:21 -0800 | [diff] [blame] | 623 | } |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 624 | cd0 = cd0 + dc4; |
| 625 | cd1 = cd1 + dc4; |
| 626 | cd2 = cd2 + dc4; |
| 627 | cd3 = cd3 + dc4; |
| 628 | n -= 4; |
| 629 | } |
| 630 | if (n & 2) { |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 631 | *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0); |
| 632 | *dstC++ = trunc_from_255<apply_alpha>(cd1, dither1); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 633 | cd0 = cd0 + dc2; |
| 634 | } |
| 635 | if (n & 1) { |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 636 | *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | template <bool apply_alpha, bool dx_is_pos> |
| 641 | void SkLinearGradient::LinearGradientContext::shade4_dx_clamp(SkPMColor dstC[], int count, |
| 642 | float fx, float dx, float invDx, |
| 643 | const float dither[2]) { |
| 644 | Sk4f dither0(dither[0]); |
| 645 | Sk4f dither1(dither[1]); |
| 646 | const Rec* rec = fRecs.begin(); |
| 647 | |
| 648 | const Sk4f dx4 = Sk4f(dx); |
| 649 | SkDEBUGCODE(SkPMColor* endDstC = dstC + count;) |
| 650 | |
| 651 | if (dx_is_pos) { |
| 652 | if (fx < 0) { |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 653 | // count is guaranteed to be positive, but the first arg may overflow int32 after |
| 654 | // increment => casting to uint32 ensures correct clamping. |
fmalita | 7dcb131 | 2016-05-26 18:10:24 -0700 | [diff] [blame] | 655 | int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor(-fx * invDx)) + 1, |
| 656 | count); |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 657 | SkASSERT(n > 0); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 658 | fill<apply_alpha>(dstC, n, rec[0].fColor); |
| 659 | count -= n; |
| 660 | dstC += n; |
| 661 | fx += n * dx; |
| 662 | SkASSERT(0 == count || fx >= 0); |
| 663 | if (n & 1) { |
| 664 | SkTSwap(dither0, dither1); |
| 665 | } |
| 666 | } |
| 667 | } else { // dx < 0 |
| 668 | if (fx > 1) { |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 669 | // count is guaranteed to be positive, but the first arg may overflow int32 after |
| 670 | // increment => casting to uint32 ensures correct clamping. |
fmalita | 7dcb131 | 2016-05-26 18:10:24 -0700 | [diff] [blame] | 671 | int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor((1 - fx) * invDx)) + 1, |
| 672 | count); |
fmalita | 7b38e3c | 2016-05-26 11:13:52 -0700 | [diff] [blame] | 673 | SkASSERT(n > 0); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 674 | fill<apply_alpha>(dstC, n, rec[fRecs.count() - 1].fColor); |
| 675 | count -= n; |
| 676 | dstC += n; |
| 677 | fx += n * dx; |
| 678 | SkASSERT(0 == count || fx <= 1); |
| 679 | if (n & 1) { |
| 680 | SkTSwap(dither0, dither1); |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | SkASSERT(count >= 0); |
| 685 | |
| 686 | const Rec* r; |
| 687 | if (dx_is_pos) { |
| 688 | r = fRecs.begin(); // start at the beginning |
| 689 | } else { |
| 690 | r = fRecs.begin() + fRecs.count() - 2; // start at the end |
| 691 | } |
| 692 | |
| 693 | while (count > 0) { |
| 694 | if (dx_is_pos) { |
| 695 | if (fx >= 1) { |
| 696 | fill<apply_alpha>(dstC, count, rec[fRecs.count() - 1].fColor); |
| 697 | return; |
| 698 | } |
| 699 | } else { // dx < 0 |
| 700 | if (fx <= 0) { |
| 701 | fill<apply_alpha>(dstC, count, rec[0].fColor); |
| 702 | return; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (dx_is_pos) { |
| 707 | r = find_forward(r, fx); |
| 708 | } else { |
| 709 | r = find_backward(r, fx); |
| 710 | } |
| 711 | SkASSERT(r >= fRecs.begin() && r < fRecs.begin() + fRecs.count() - 1); |
| 712 | |
| 713 | const float p0 = r[0].fPos; |
| 714 | const Sk4f c0 = r[0].fColor; |
| 715 | const float p1 = r[1].fPos; |
| 716 | const Sk4f diffc = Sk4f(r[1].fColor) - c0; |
| 717 | const float scale = r[1].fPosScale; |
| 718 | const float t = (fx - p0) * scale; |
| 719 | const Sk4f c = c0 + Sk4f(t) * diffc; |
| 720 | const Sk4f dc = diffc * dx4 * Sk4f(scale); |
| 721 | |
| 722 | int n; |
| 723 | if (dx_is_pos) { |
| 724 | n = SkTMin((int)((p1 - fx) * invDx) + 1, count); |
| 725 | } else { |
| 726 | n = SkTMin((int)((p0 - fx) * invDx) + 1, count); |
| 727 | } |
| 728 | |
| 729 | fx += n * dx; |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 730 | // fx should now outside of the p0..p1 interval. However, due to float precision loss, |
| 731 | // its possible that fx is slightly too small/large, so we clamp it. |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 732 | if (dx_is_pos) { |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 733 | fx = SkTMax(fx, p1); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 734 | } else { |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 735 | fx = SkTMin(fx, p0); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | ramp<apply_alpha>(dstC, n, c, dc, dither0, dither1); |
| 739 | dstC += n; |
| 740 | SkASSERT(dstC <= endDstC); |
mtklein | 9db43ac | 2015-12-01 07:10:21 -0800 | [diff] [blame] | 741 | |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 742 | if (n & 1) { |
| 743 | SkTSwap(dither0, dither1); |
| 744 | } |
reed | aeab8ea | 2016-01-05 10:01:38 -0800 | [diff] [blame] | 745 | |
| 746 | count -= n; |
| 747 | SkASSERT(count >= 0); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | void SkLinearGradient::LinearGradientContext::shade4_clamp(int x, int y, SkPMColor dstC[], |
| 752 | int count) { |
| 753 | SkASSERT(count > 0); |
| 754 | SkASSERT(kLinear_MatrixClass == fDstToIndexClass); |
| 755 | |
| 756 | SkPoint srcPt; |
| 757 | fDstToIndexProc(fDstToIndex, x + SK_ScalarHalf, y + SK_ScalarHalf, &srcPt); |
| 758 | float fx = srcPt.x(); |
| 759 | const float dx = fDstToIndex.getScaleX(); |
| 760 | |
| 761 | // Default our dither bias values to 1/2, (rounding), which is no dithering |
| 762 | float dither0 = 0.5f; |
| 763 | float dither1 = 0.5f; |
| 764 | if (fDither) { |
| 765 | const float ditherCell[] = { |
| 766 | 1/8.0f, 5/8.0f, |
| 767 | 7/8.0f, 3/8.0f, |
| 768 | }; |
| 769 | const int rowIndex = (y & 1) << 1; |
| 770 | dither0 = ditherCell[rowIndex]; |
| 771 | dither1 = ditherCell[rowIndex + 1]; |
| 772 | if (x & 1) { |
| 773 | SkTSwap(dither0, dither1); |
| 774 | } |
| 775 | } |
| 776 | const float dither[2] = { dither0, dither1 }; |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 777 | |
fmalita | 2b46913 | 2015-11-23 08:30:23 -0800 | [diff] [blame] | 778 | if (SkScalarNearlyZero(dx * count)) { // gradient is vertical |
reed | de3aac8 | 2015-11-22 13:00:04 -0800 | [diff] [blame] | 779 | const float pinFx = SkTPin(fx, 0.0f, 1.0f); |
| 780 | Sk4f c = lerp_color(pinFx, find_forward(fRecs.begin(), pinFx)); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 781 | if (fApplyAlphaAfterInterp) { |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 782 | fill<true>(dstC, count, c, dither0, dither1); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 783 | } else { |
fmalita | 0ce4f23 | 2016-12-06 08:57:47 -0800 | [diff] [blame] | 784 | fill<false>(dstC, count, c, dither0, dither1); |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 785 | } |
| 786 | return; |
| 787 | } |
| 788 | |
James Zern | 44e91c9 | 2016-11-09 19:22:46 -0800 | [diff] [blame] | 789 | SkASSERT(0.f != dx); |
| 790 | const float invDx = 1 / dx; |
reed | f3182eb | 2015-11-17 08:12:19 -0800 | [diff] [blame] | 791 | if (dx > 0) { |
| 792 | if (fApplyAlphaAfterInterp) { |
| 793 | this->shade4_dx_clamp<true, true>(dstC, count, fx, dx, invDx, dither); |
| 794 | } else { |
| 795 | this->shade4_dx_clamp<false, true>(dstC, count, fx, dx, invDx, dither); |
| 796 | } |
| 797 | } else { |
| 798 | if (fApplyAlphaAfterInterp) { |
| 799 | this->shade4_dx_clamp<true, false>(dstC, count, fx, dx, invDx, dither); |
| 800 | } else { |
| 801 | this->shade4_dx_clamp<false, false>(dstC, count, fx, dx, invDx, dither); |
| 802 | } |
| 803 | } |
| 804 | } |