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