fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #include "Sk4fLinearGradient.h" |
fmalita | 83aa920 | 2016-03-23 12:28:14 -0700 | [diff] [blame] | 9 | #include "Sk4x4f.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 10 | #include "SkPaint.h" |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 11 | |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 12 | #include <cmath> |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 13 | #include <utility> |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 14 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 15 | namespace { |
| 16 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 17 | template<typename dstType, ApplyPremul premul> |
| 18 | void ramp(const Sk4f& c, const Sk4f& dc, dstType dst[], int n, |
| 19 | const Sk4f& bias0, const Sk4f& bias1) { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 20 | SkASSERT(n > 0); |
| 21 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 22 | const Sk4f dc2 = dc + dc, |
| 23 | dc4 = dc2 + dc2; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 24 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 25 | Sk4f c0 = c + DstTraits<dstType, premul>::pre_lerp_bias(bias0), |
| 26 | c1 = c + dc + DstTraits<dstType, premul>::pre_lerp_bias(bias1), |
| 27 | c2 = c0 + dc2, |
| 28 | c3 = c1 + dc2; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 29 | |
| 30 | while (n >= 4) { |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 31 | DstTraits<dstType, premul>::store4x(c0, c1, c2, c3, dst, bias0, bias1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 32 | dst += 4; |
| 33 | |
| 34 | c0 = c0 + dc4; |
| 35 | c1 = c1 + dc4; |
| 36 | c2 = c2 + dc4; |
| 37 | c3 = c3 + dc4; |
| 38 | n -= 4; |
| 39 | } |
| 40 | if (n & 2) { |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 41 | DstTraits<dstType, premul>::store(c0, dst++, bias0); |
| 42 | DstTraits<dstType, premul>::store(c1, dst++, bias1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 43 | c0 = c0 + dc2; |
| 44 | } |
| 45 | if (n & 1) { |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 46 | DstTraits<dstType, premul>::store(c0, dst, bias0); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | template<SkShader::TileMode> |
| 51 | SkScalar pinFx(SkScalar); |
| 52 | |
| 53 | template<> |
| 54 | SkScalar pinFx<SkShader::kClamp_TileMode>(SkScalar fx) { |
| 55 | return fx; |
| 56 | } |
| 57 | |
| 58 | template<> |
| 59 | SkScalar pinFx<SkShader::kRepeat_TileMode>(SkScalar fx) { |
Florin Malita | 40481bb | 2018-03-13 11:17:52 -0400 | [diff] [blame] | 60 | SkScalar f = SkScalarIsFinite(fx) ? SkScalarFraction(fx) : 0; |
Florin Malita | 0fdde54 | 2016-11-14 15:54:04 -0500 | [diff] [blame] | 61 | if (f < 0) { |
| 62 | f = SkTMin(f + 1, nextafterf(1, 0)); |
| 63 | } |
| 64 | SkASSERT(f >= 0); |
| 65 | SkASSERT(f < 1.0f); |
| 66 | return f; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | template<> |
| 70 | SkScalar pinFx<SkShader::kMirror_TileMode>(SkScalar fx) { |
Florin Malita | 40481bb | 2018-03-13 11:17:52 -0400 | [diff] [blame] | 71 | SkScalar f = SkScalarIsFinite(fx) ? SkScalarMod(fx, 2.0f) : 0; |
Florin Malita | 0fdde54 | 2016-11-14 15:54:04 -0500 | [diff] [blame] | 72 | if (f < 0) { |
| 73 | f = SkTMin(f + 2, nextafterf(2, 0)); |
| 74 | } |
| 75 | SkASSERT(f >= 0); |
| 76 | SkASSERT(f < 2.0f); |
| 77 | return f; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 80 | // true when x is in [k1,k2], or [k2, k1] when the interval is reversed. |
fmalita | 6d7e4e8 | 2016-09-20 06:55:16 -0700 | [diff] [blame] | 81 | // TODO(fmalita): hoist the reversed interval check out of this helper. |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 82 | bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { |
| 83 | SkASSERT(k1 != k2); |
| 84 | return (k1 < k2) |
Florin Malita | e659c7f | 2017-02-09 13:46:55 -0500 | [diff] [blame] | 85 | ? (x >= k1 && x <= k2) |
| 86 | : (x >= k2 && x <= k1); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 87 | } |
| 88 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 89 | } // anonymous namespace |
| 90 | |
| 91 | SkLinearGradient:: |
| 92 | LinearGradient4fContext::LinearGradient4fContext(const SkLinearGradient& shader, |
| 93 | const ContextRec& rec) |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 94 | : INHERITED(shader, rec) { |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 95 | |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 96 | // Our fast path expects interval points to be monotonically increasing in x. |
Florin Malita | 4d41b8f | 2017-07-12 14:35:46 -0400 | [diff] [blame] | 97 | const bool reverseIntervals = std::signbit(fDstToPos.getScaleX()); |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame] | 98 | fIntervals.init(shader, rec.fDstColorSpace, shader.fTileMode, |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 99 | fColorsArePremul, rec.fPaint->getAlpha() * (1.0f / 255), reverseIntervals); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 100 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 101 | SkASSERT(fIntervals->count() > 0); |
| 102 | fCachedInterval = fIntervals->begin(); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 105 | const Sk4fGradientInterval* |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 106 | SkLinearGradient::LinearGradient4fContext::findInterval(SkScalar fx) const { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 107 | SkASSERT(in_range(fx, fIntervals->front().fT0, fIntervals->back().fT1)); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 108 | |
| 109 | if (1) { |
| 110 | // Linear search, using the last scanline interval as a starting point. |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 111 | SkASSERT(fCachedInterval >= fIntervals->begin()); |
| 112 | SkASSERT(fCachedInterval < fIntervals->end()); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 113 | const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1; |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 114 | while (!in_range(fx, fCachedInterval->fT0, fCachedInterval->fT1)) { |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 115 | fCachedInterval += search_dir; |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 116 | if (fCachedInterval >= fIntervals->end()) { |
| 117 | fCachedInterval = fIntervals->begin(); |
| 118 | } else if (fCachedInterval < fIntervals->begin()) { |
| 119 | fCachedInterval = fIntervals->end() - 1; |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | return fCachedInterval; |
| 123 | } else { |
| 124 | // Binary search. Seems less effective than linear + caching. |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 125 | const auto* i0 = fIntervals->begin(); |
| 126 | const auto* i1 = fIntervals->end() - 1; |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 127 | |
| 128 | while (i0 != i1) { |
| 129 | SkASSERT(i0 < i1); |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 130 | SkASSERT(in_range(fx, i0->fT0, i1->fT1)); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 131 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 132 | const auto* i = i0 + ((i1 - i0) >> 1); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 133 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 134 | if (in_range(fx, i0->fT0, i->fT1)) { |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 135 | i1 = i; |
| 136 | } else { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 137 | SkASSERT(in_range(fx, i->fT1, i1->fT1)); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 138 | i0 = i + 1; |
| 139 | } |
| 140 | } |
| 141 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 142 | SkASSERT(in_range(fx, i0->fT0, i0->fT1)); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 143 | return i0; |
| 144 | } |
| 145 | } |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 146 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 147 | |
| 148 | void SkLinearGradient:: |
| 149 | LinearGradient4fContext::shadeSpan(int x, int y, SkPMColor dst[], int count) { |
| 150 | SkASSERT(count > 0); |
| 151 | |
| 152 | float bias0 = 0, |
| 153 | bias1 = 0; |
| 154 | |
| 155 | if (fDither) { |
| 156 | static constexpr float dither_cell[] = { |
| 157 | -3/8.0f, 1/8.0f, |
| 158 | 3/8.0f, -1/8.0f, |
| 159 | }; |
| 160 | |
| 161 | const int rowIndex = (y & 1) << 1; |
| 162 | bias0 = dither_cell[rowIndex + 0]; |
| 163 | bias1 = dither_cell[rowIndex + 1]; |
| 164 | |
| 165 | if (x & 1) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 166 | using std::swap; |
| 167 | swap(bias0, bias1); |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | if (fColorsArePremul) { |
| 172 | // In premul interpolation mode, components are pre-scaled by 255 and the store |
| 173 | // op is truncating. We pre-bias here to achieve rounding. |
| 174 | bias0 += 0.5f; |
| 175 | bias1 += 0.5f; |
| 176 | |
| 177 | this->shadePremulSpan<SkPMColor, ApplyPremul::False>(x, y, dst, count, bias0, bias1); |
| 178 | } else { |
| 179 | // In unpremul interpolation mode, Components are not pre-scaled. |
| 180 | bias0 *= 1/255.0f; |
| 181 | bias1 *= 1/255.0f; |
| 182 | |
| 183 | this->shadePremulSpan<SkPMColor, ApplyPremul::True >(x, y, dst, count, bias0, bias1); |
| 184 | } |
| 185 | } |
| 186 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 187 | void SkLinearGradient:: |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 188 | LinearGradient4fContext::shadeSpan4f(int x, int y, SkPM4f dst[], int count) { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 189 | SkASSERT(count > 0); |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 190 | |
| 191 | // 4f dests are dithered at a later stage, if needed. |
| 192 | static constexpr float bias0 = 0, |
| 193 | bias1 = 0; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 194 | if (fColorsArePremul) { |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 195 | this->shadePremulSpan<SkPM4f, ApplyPremul::False>(x, y, dst, count, bias0, bias1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 196 | } else { |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 197 | this->shadePremulSpan<SkPM4f, ApplyPremul::True >(x, y, dst, count, bias0, bias1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 201 | template<typename dstType, ApplyPremul premul> |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 202 | void SkLinearGradient:: |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 203 | LinearGradient4fContext::shadePremulSpan(int x, int y, dstType dst[], int count, |
| 204 | float bias0, float bias1) const { |
| 205 | const SkLinearGradient& shader = static_cast<const SkLinearGradient&>(fShader); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 206 | switch (shader.fTileMode) { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 207 | case kDecal_TileMode: |
| 208 | SkASSERT(false); // decal only supported via stages |
| 209 | // fall-through |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 210 | case kClamp_TileMode: |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 211 | this->shadeSpanInternal<dstType, premul, kClamp_TileMode >(x, y, dst, count, bias0, bias1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 212 | break; |
| 213 | case kRepeat_TileMode: |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 214 | this->shadeSpanInternal<dstType, premul, kRepeat_TileMode>(x, y, dst, count, bias0, bias1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 215 | break; |
| 216 | case kMirror_TileMode: |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 217 | this->shadeSpanInternal<dstType, premul, kMirror_TileMode>(x, y, dst, count, bias0, bias1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 222 | template<typename dstType, ApplyPremul premul, SkShader::TileMode tileMode> |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 223 | void SkLinearGradient:: |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 224 | LinearGradient4fContext::shadeSpanInternal(int x, int y, dstType dst[], int count, |
| 225 | float bias0, float bias1) const { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 226 | SkPoint pt; |
| 227 | fDstToPosProc(fDstToPos, |
| 228 | x + SK_ScalarHalf, |
| 229 | y + SK_ScalarHalf, |
| 230 | &pt); |
| 231 | const SkScalar fx = pinFx<tileMode>(pt.x()); |
| 232 | const SkScalar dx = fDstToPos.getScaleX(); |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 233 | LinearIntervalProcessor<dstType, premul, tileMode> proc(fIntervals->begin(), |
| 234 | fIntervals->end() - 1, |
| 235 | this->findInterval(fx), |
| 236 | fx, |
| 237 | dx, |
| 238 | SkScalarNearlyZero(dx * count)); |
| 239 | Sk4f bias4f0(bias0), |
| 240 | bias4f1(bias1); |
| 241 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 242 | while (count > 0) { |
| 243 | // What we really want here is SkTPin(advance, 1, count) |
| 244 | // but that's a significant perf hit for >> stops; investigate. |
| 245 | const int n = SkScalarTruncToInt( |
| 246 | SkTMin<SkScalar>(proc.currentAdvance() + 1, SkIntToScalar(count))); |
| 247 | |
| 248 | // The current interval advance can be +inf (e.g. when reaching |
| 249 | // the clamp mode end intervals) - when that happens, we expect to |
| 250 | // a) consume all remaining count in one swoop |
| 251 | // b) return a zero color gradient |
| 252 | SkASSERT(SkScalarIsFinite(proc.currentAdvance()) |
| 253 | || (n == count && proc.currentRampIsZero())); |
| 254 | |
| 255 | if (proc.currentRampIsZero()) { |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 256 | DstTraits<dstType, premul>::store(proc.currentColor(), dst, n); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 257 | } else { |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 258 | ramp<dstType, premul>(proc.currentColor(), proc.currentColorGrad(), dst, n, |
| 259 | bias4f0, bias4f1); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | proc.advance(SkIntToScalar(n)); |
| 263 | count -= n; |
| 264 | dst += n; |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 265 | |
| 266 | if (n & 1) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 267 | using std::swap; |
| 268 | swap(bias4f0, bias4f1); |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 269 | } |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 273 | template<typename dstType, ApplyPremul premul, SkShader::TileMode tileMode> |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 274 | class SkLinearGradient:: |
| 275 | LinearGradient4fContext::LinearIntervalProcessor { |
| 276 | public: |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 277 | LinearIntervalProcessor(const Sk4fGradientInterval* firstInterval, |
| 278 | const Sk4fGradientInterval* lastInterval, |
| 279 | const Sk4fGradientInterval* i, |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 280 | SkScalar fx, |
| 281 | SkScalar dx, |
| 282 | bool is_vertical) |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 283 | : fAdvX(is_vertical ? SK_ScalarInfinity : (i->fT1 - fx) / dx) |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 284 | , fFirstInterval(firstInterval) |
| 285 | , fLastInterval(lastInterval) |
| 286 | , fInterval(i) |
| 287 | , fDx(dx) |
| 288 | , fIsVertical(is_vertical) |
| 289 | { |
fmalita | 6d7e4e8 | 2016-09-20 06:55:16 -0700 | [diff] [blame] | 290 | SkASSERT(fAdvX >= 0); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 291 | SkASSERT(firstInterval <= lastInterval); |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 292 | |
| 293 | if (tileMode != kClamp_TileMode && !is_vertical) { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 294 | const auto spanX = (lastInterval->fT1 - firstInterval->fT0) / dx; |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 295 | SkASSERT(spanX >= 0); |
| 296 | |
| 297 | // If we're in a repeating tile mode and the whole gradient is compressed into a |
| 298 | // fraction of a pixel, we just use the average color in zero-ramp mode. |
| 299 | // This also avoids cases where we make no progress due to interval advances being |
| 300 | // close to zero. |
| 301 | static constexpr SkScalar kMinSpanX = .25f; |
| 302 | if (spanX < kMinSpanX) { |
| 303 | this->init_average_props(); |
| 304 | return; |
| 305 | } |
| 306 | } |
| 307 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 308 | this->compute_interval_props(fx); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | SkScalar currentAdvance() const { |
| 312 | SkASSERT(fAdvX >= 0); |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 313 | SkASSERT(fAdvX <= (fInterval->fT1 - fInterval->fT0) / fDx || !std::isfinite(fAdvX)); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 314 | return fAdvX; |
| 315 | } |
| 316 | |
| 317 | bool currentRampIsZero() const { return fZeroRamp; } |
| 318 | const Sk4f& currentColor() const { return fCc; } |
| 319 | const Sk4f& currentColorGrad() const { return fDcDx; } |
| 320 | |
| 321 | void advance(SkScalar advX) { |
| 322 | SkASSERT(advX > 0); |
| 323 | SkASSERT(fAdvX >= 0); |
| 324 | |
| 325 | if (advX >= fAdvX) { |
| 326 | advX = this->advance_interval(advX); |
| 327 | } |
| 328 | SkASSERT(advX < fAdvX); |
| 329 | |
| 330 | fCc = fCc + fDcDx * Sk4f(advX); |
| 331 | fAdvX -= advX; |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | void compute_interval_props(SkScalar t) { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 336 | SkASSERT(in_range(t, fInterval->fT0, fInterval->fT1)); |
| 337 | |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 338 | const Sk4f dc = DstTraits<dstType, premul>::load(fInterval->fCg); |
| 339 | fCc = DstTraits<dstType, premul>::load(fInterval->fCb) + dc * Sk4f(t); |
Florin Malita | 63f717d | 2017-05-08 09:53:11 -0400 | [diff] [blame] | 340 | fDcDx = dc * fDx; |
| 341 | fZeroRamp = fIsVertical || (dc == 0).allTrue(); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 342 | } |
| 343 | |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 344 | void init_average_props() { |
| 345 | fAdvX = SK_ScalarInfinity; |
| 346 | fZeroRamp = true; |
| 347 | fDcDx = 0; |
| 348 | fCc = Sk4f(0); |
| 349 | |
| 350 | // TODO: precompute the average at interval setup time? |
| 351 | for (const auto* i = fFirstInterval; i <= fLastInterval; ++i) { |
| 352 | // Each interval contributes its average color to the total/weighted average: |
| 353 | // |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 354 | // C = (c0 + c1) / 2 = (Cb + Cg * t0 + Cb + Cg * t1) / 2 = Cb + Cg *(t0 + t1) / 2 |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 355 | // |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 356 | // Avg += C * (t1 - t0) |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 357 | // |
Florin Malita | aa0ce82 | 2017-08-28 12:50:26 -0400 | [diff] [blame] | 358 | const auto c = DstTraits<dstType, premul>::load(i->fCb) |
| 359 | + DstTraits<dstType, premul>::load(i->fCg) * (i->fT0 + i->fT1) * 0.5f; |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 360 | fCc = fCc + c * (i->fT1 - i->fT0); |
fmalita | afac581 | 2016-11-01 13:41:34 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 364 | const Sk4fGradientInterval* next_interval(const Sk4fGradientInterval* i) const { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 365 | SkASSERT(i >= fFirstInterval); |
| 366 | SkASSERT(i <= fLastInterval); |
| 367 | i++; |
| 368 | |
| 369 | if (tileMode == kClamp_TileMode) { |
| 370 | SkASSERT(i <= fLastInterval); |
| 371 | return i; |
| 372 | } |
| 373 | |
| 374 | return (i <= fLastInterval) ? i : fFirstInterval; |
| 375 | } |
| 376 | |
| 377 | SkScalar advance_interval(SkScalar advX) { |
| 378 | SkASSERT(advX >= fAdvX); |
| 379 | |
| 380 | do { |
| 381 | advX -= fAdvX; |
| 382 | fInterval = this->next_interval(fInterval); |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 383 | fAdvX = (fInterval->fT1 - fInterval->fT0) / fDx; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 384 | SkASSERT(fAdvX > 0); |
| 385 | } while (advX >= fAdvX); |
| 386 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 387 | compute_interval_props(fInterval->fT0); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 388 | |
| 389 | SkASSERT(advX >= 0); |
| 390 | return advX; |
| 391 | } |
| 392 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 393 | // Current interval properties. |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 394 | Sk4f fDcDx; // dst color gradient (dc/dx) |
| 395 | Sk4f fCc; // current color, interpolated in dst |
| 396 | SkScalar fAdvX; // remaining interval advance in dst |
| 397 | bool fZeroRamp; // current interval color grad is 0 |
| 398 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 399 | const Sk4fGradientInterval* fFirstInterval; |
| 400 | const Sk4fGradientInterval* fLastInterval; |
| 401 | const Sk4fGradientInterval* fInterval; // current interval |
| 402 | const SkScalar fDx; // 'dx' for consistency with other impls; actually dt/dx |
| 403 | const bool fIsVertical; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 404 | }; |