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" |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 10 | #include "SkXfermode.h" |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 11 | |
| 12 | namespace { |
| 13 | |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 14 | template<DstType dstType, ApplyPremul premul> |
| 15 | void ramp(const Sk4f& c, const Sk4f& dc, typename DstTraits<dstType, premul>::Type dst[], int n) { |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 16 | SkASSERT(n > 0); |
| 17 | |
| 18 | const Sk4f dc2 = dc + dc; |
| 19 | const Sk4f dc4 = dc2 + dc2; |
| 20 | |
| 21 | Sk4f c0 = c ; |
| 22 | Sk4f c1 = c + dc; |
| 23 | Sk4f c2 = c0 + dc2; |
| 24 | Sk4f c3 = c1 + dc2; |
| 25 | |
| 26 | while (n >= 4) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 27 | DstTraits<dstType, premul>::store4x(c0, c1, c2, c3, dst); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 28 | dst += 4; |
| 29 | |
| 30 | c0 = c0 + dc4; |
| 31 | c1 = c1 + dc4; |
| 32 | c2 = c2 + dc4; |
| 33 | c3 = c3 + dc4; |
| 34 | n -= 4; |
| 35 | } |
| 36 | if (n & 2) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 37 | DstTraits<dstType, premul>::store(c0, dst++); |
| 38 | DstTraits<dstType, premul>::store(c1, dst++); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 39 | c0 = c0 + dc2; |
| 40 | } |
| 41 | if (n & 1) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 42 | DstTraits<dstType, premul>::store(c0, dst); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
fmalita | 83aa920 | 2016-03-23 12:28:14 -0700 | [diff] [blame] | 46 | // Planar version of ramp (S32 no-premul only). |
| 47 | template<> |
| 48 | void ramp<DstType::S32, ApplyPremul::False>(const Sk4f& c, const Sk4f& dc, SkPMColor dst[], int n) { |
| 49 | SkASSERT(n > 0); |
| 50 | |
| 51 | const Sk4f dc4 = dc * 4; |
| 52 | const Sk4x4f dc4x = { Sk4f(dc4[0]), Sk4f(dc4[1]), Sk4f(dc4[2]), Sk4f(dc4[3]) }; |
| 53 | Sk4x4f c4x = Sk4x4f::Transpose(c, c + dc, c + dc * 2, c + dc * 3); |
| 54 | |
| 55 | while (n >= 4) { |
mtklein | 0c90247 | 2016-07-20 18:10:07 -0700 | [diff] [blame] | 56 | ( sk_linear_to_srgb(c4x.r) << 0 |
| 57 | | sk_linear_to_srgb(c4x.g) << 8 |
| 58 | | sk_linear_to_srgb(c4x.b) << 16 |
| 59 | | Sk4f_round(255.0f*c4x.a) << 24).store(dst); |
fmalita | 83aa920 | 2016-03-23 12:28:14 -0700 | [diff] [blame] | 60 | |
| 61 | c4x.r += dc4x.r; |
| 62 | c4x.g += dc4x.g; |
| 63 | c4x.b += dc4x.b; |
| 64 | c4x.a += dc4x.a; |
| 65 | |
| 66 | dst += 4; |
| 67 | n -= 4; |
| 68 | } |
| 69 | |
| 70 | if (n & 2) { |
| 71 | DstTraits<DstType::S32, ApplyPremul::False> |
| 72 | ::store(Sk4f(c4x.r[0], c4x.g[0], c4x.b[0], c4x.a[0]), dst++); |
| 73 | DstTraits<DstType::S32, ApplyPremul::False> |
| 74 | ::store(Sk4f(c4x.r[1], c4x.g[1], c4x.b[1], c4x.a[1]), dst++); |
| 75 | } |
| 76 | |
| 77 | if (n & 1) { |
| 78 | DstTraits<DstType::S32, ApplyPremul::False> |
| 79 | ::store(Sk4f(c4x.r[n & 2], c4x.g[n & 2], c4x.b[n & 2], c4x.a[n & 2]), dst); |
| 80 | } |
| 81 | } |
| 82 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 83 | template<SkShader::TileMode> |
| 84 | SkScalar pinFx(SkScalar); |
| 85 | |
| 86 | template<> |
| 87 | SkScalar pinFx<SkShader::kClamp_TileMode>(SkScalar fx) { |
| 88 | return fx; |
| 89 | } |
| 90 | |
| 91 | template<> |
| 92 | SkScalar pinFx<SkShader::kRepeat_TileMode>(SkScalar fx) { |
| 93 | const SkScalar f = SkScalarFraction(fx); |
| 94 | return f < 0 ? f + 1 : f; |
| 95 | } |
| 96 | |
| 97 | template<> |
| 98 | SkScalar pinFx<SkShader::kMirror_TileMode>(SkScalar fx) { |
| 99 | const SkScalar f = SkScalarMod(fx, 2.0f); |
| 100 | return f < 0 ? f + 2 : f; |
| 101 | } |
| 102 | |
fmalita | 6d7e4e8 | 2016-09-20 06:55:16 -0700 | [diff] [blame^] | 103 | // true when x is in [k1,k2), or [k2, k1) when the interval is reversed. |
| 104 | // TODO(fmalita): hoist the reversed interval check out of this helper. |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 105 | bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { |
| 106 | SkASSERT(k1 != k2); |
| 107 | return (k1 < k2) |
fmalita | 6d7e4e8 | 2016-09-20 06:55:16 -0700 | [diff] [blame^] | 108 | ? (x >= k1 && x < k2) |
| 109 | : (x > k2 && x <= k1); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 110 | } |
| 111 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 112 | } // anonymous namespace |
| 113 | |
| 114 | SkLinearGradient:: |
| 115 | LinearGradient4fContext::LinearGradient4fContext(const SkLinearGradient& shader, |
| 116 | const ContextRec& rec) |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 117 | : INHERITED(shader, rec) { |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 118 | |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 119 | // Our fast path expects interval points to be monotonically increasing in x. |
fmalita | 6d7e4e8 | 2016-09-20 06:55:16 -0700 | [diff] [blame^] | 120 | const bool reverseIntervals = this->isFast() && signbit(fDstToPos.getScaleX()); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 121 | this->buildIntervals(shader, rec, reverseIntervals); |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 122 | |
| 123 | SkASSERT(fIntervals.count() > 0); |
| 124 | fCachedInterval = fIntervals.begin(); |
| 125 | } |
| 126 | |
fmalita | 7520fc4 | 2016-03-04 11:01:24 -0800 | [diff] [blame] | 127 | const SkGradientShaderBase::GradientShaderBase4fContext::Interval* |
| 128 | SkLinearGradient::LinearGradient4fContext::findInterval(SkScalar fx) const { |
| 129 | SkASSERT(in_range(fx, fIntervals.front().fP0, fIntervals.back().fP1)); |
| 130 | |
| 131 | if (1) { |
| 132 | // Linear search, using the last scanline interval as a starting point. |
| 133 | SkASSERT(fCachedInterval >= fIntervals.begin()); |
| 134 | SkASSERT(fCachedInterval < fIntervals.end()); |
| 135 | const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1; |
| 136 | while (!in_range(fx, fCachedInterval->fP0, fCachedInterval->fP1)) { |
| 137 | fCachedInterval += search_dir; |
| 138 | if (fCachedInterval >= fIntervals.end()) { |
| 139 | fCachedInterval = fIntervals.begin(); |
| 140 | } else if (fCachedInterval < fIntervals.begin()) { |
| 141 | fCachedInterval = fIntervals.end() - 1; |
| 142 | } |
| 143 | } |
| 144 | return fCachedInterval; |
| 145 | } else { |
| 146 | // Binary search. Seems less effective than linear + caching. |
| 147 | const Interval* i0 = fIntervals.begin(); |
| 148 | const Interval* i1 = fIntervals.end() - 1; |
| 149 | |
| 150 | while (i0 != i1) { |
| 151 | SkASSERT(i0 < i1); |
| 152 | SkASSERT(in_range(fx, i0->fP0, i1->fP1)); |
| 153 | |
| 154 | const Interval* i = i0 + ((i1 - i0) >> 1); |
| 155 | |
| 156 | if (in_range(fx, i0->fP0, i->fP1)) { |
| 157 | i1 = i; |
| 158 | } else { |
| 159 | SkASSERT(in_range(fx, i->fP1, i1->fP1)); |
| 160 | i0 = i + 1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | SkASSERT(in_range(fx, i0->fP0, i0->fP1)); |
| 165 | return i0; |
| 166 | } |
| 167 | } |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 168 | |
| 169 | void SkLinearGradient:: |
| 170 | LinearGradient4fContext::shadeSpan(int x, int y, SkPMColor dst[], int count) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 171 | if (!this->isFast()) { |
| 172 | this->INHERITED::shadeSpan(x, y, dst, count); |
| 173 | return; |
| 174 | } |
| 175 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 176 | // TODO: plumb dithering |
| 177 | SkASSERT(count > 0); |
| 178 | if (fColorsArePremul) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 179 | this->shadePremulSpan<DstType::L32, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 180 | ApplyPremul::False>(x, y, dst, count); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 181 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 182 | this->shadePremulSpan<DstType::L32, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 183 | ApplyPremul::True>(x, y, dst, count); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void SkLinearGradient:: |
| 188 | LinearGradient4fContext::shadeSpan4f(int x, int y, SkPM4f dst[], int count) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 189 | if (!this->isFast()) { |
| 190 | this->INHERITED::shadeSpan4f(x, y, dst, count); |
| 191 | return; |
| 192 | } |
| 193 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 194 | // TONOTDO: plumb dithering |
| 195 | SkASSERT(count > 0); |
| 196 | if (fColorsArePremul) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 197 | this->shadePremulSpan<DstType::F32, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 198 | ApplyPremul::False>(x, y, dst, count); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 199 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 200 | this->shadePremulSpan<DstType::F32, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 201 | ApplyPremul::True>(x, y, dst, count); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 205 | template<DstType dstType, ApplyPremul premul> |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 206 | void SkLinearGradient:: |
| 207 | LinearGradient4fContext::shadePremulSpan(int x, int y, |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 208 | typename DstTraits<dstType, premul>::Type dst[], |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 209 | int count) const { |
| 210 | const SkLinearGradient& shader = |
| 211 | static_cast<const SkLinearGradient&>(fShader); |
| 212 | switch (shader.fTileMode) { |
| 213 | case kClamp_TileMode: |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 214 | this->shadeSpanInternal<dstType, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 215 | premul, |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 216 | kClamp_TileMode>(x, y, dst, count); |
| 217 | break; |
| 218 | case kRepeat_TileMode: |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 219 | this->shadeSpanInternal<dstType, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 220 | premul, |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 221 | kRepeat_TileMode>(x, y, dst, count); |
| 222 | break; |
| 223 | case kMirror_TileMode: |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 224 | this->shadeSpanInternal<dstType, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 225 | premul, |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 226 | kMirror_TileMode>(x, y, dst, count); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 231 | template<DstType dstType, ApplyPremul premul, SkShader::TileMode tileMode> |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 232 | void SkLinearGradient:: |
| 233 | LinearGradient4fContext::shadeSpanInternal(int x, int y, |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 234 | typename DstTraits<dstType, premul>::Type dst[], |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 235 | int count) const { |
| 236 | SkPoint pt; |
| 237 | fDstToPosProc(fDstToPos, |
| 238 | x + SK_ScalarHalf, |
| 239 | y + SK_ScalarHalf, |
| 240 | &pt); |
| 241 | const SkScalar fx = pinFx<tileMode>(pt.x()); |
| 242 | const SkScalar dx = fDstToPos.getScaleX(); |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 243 | LinearIntervalProcessor<dstType, tileMode> proc(fIntervals.begin(), |
| 244 | fIntervals.end() - 1, |
| 245 | this->findInterval(fx), |
| 246 | fx, |
| 247 | dx, |
| 248 | SkScalarNearlyZero(dx * count)); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 249 | while (count > 0) { |
| 250 | // What we really want here is SkTPin(advance, 1, count) |
| 251 | // but that's a significant perf hit for >> stops; investigate. |
| 252 | const int n = SkScalarTruncToInt( |
| 253 | SkTMin<SkScalar>(proc.currentAdvance() + 1, SkIntToScalar(count))); |
| 254 | |
| 255 | // The current interval advance can be +inf (e.g. when reaching |
| 256 | // the clamp mode end intervals) - when that happens, we expect to |
| 257 | // a) consume all remaining count in one swoop |
| 258 | // b) return a zero color gradient |
| 259 | SkASSERT(SkScalarIsFinite(proc.currentAdvance()) |
| 260 | || (n == count && proc.currentRampIsZero())); |
| 261 | |
| 262 | if (proc.currentRampIsZero()) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 263 | DstTraits<dstType, premul>::store(proc.currentColor(), |
| 264 | dst, n); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 265 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 266 | ramp<dstType, premul>(proc.currentColor(), |
| 267 | proc.currentColorGrad(), |
| 268 | dst, n); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | proc.advance(SkIntToScalar(n)); |
| 272 | count -= n; |
| 273 | dst += n; |
| 274 | } |
| 275 | } |
| 276 | |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 277 | template<DstType dstType, SkShader::TileMode tileMode> |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 278 | class SkLinearGradient:: |
| 279 | LinearGradient4fContext::LinearIntervalProcessor { |
| 280 | public: |
| 281 | LinearIntervalProcessor(const Interval* firstInterval, |
| 282 | const Interval* lastInterval, |
| 283 | const Interval* i, |
| 284 | SkScalar fx, |
| 285 | SkScalar dx, |
| 286 | bool is_vertical) |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 287 | : fAdvX((i->fP1 - fx) / dx) |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 288 | , fFirstInterval(firstInterval) |
| 289 | , fLastInterval(lastInterval) |
| 290 | , fInterval(i) |
| 291 | , fDx(dx) |
| 292 | , fIsVertical(is_vertical) |
| 293 | { |
fmalita | 6d7e4e8 | 2016-09-20 06:55:16 -0700 | [diff] [blame^] | 294 | SkASSERT(fAdvX >= 0); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 295 | SkASSERT(firstInterval <= lastInterval); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 296 | SkASSERT(in_range(fx, i->fP0, i->fP1)); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 297 | this->compute_interval_props(fx - i->fP0); |
| 298 | } |
| 299 | |
| 300 | SkScalar currentAdvance() const { |
| 301 | SkASSERT(fAdvX >= 0); |
| 302 | SkASSERT(fAdvX <= (fInterval->fP1 - fInterval->fP0) / fDx); |
| 303 | return fAdvX; |
| 304 | } |
| 305 | |
| 306 | bool currentRampIsZero() const { return fZeroRamp; } |
| 307 | const Sk4f& currentColor() const { return fCc; } |
| 308 | const Sk4f& currentColorGrad() const { return fDcDx; } |
| 309 | |
| 310 | void advance(SkScalar advX) { |
| 311 | SkASSERT(advX > 0); |
| 312 | SkASSERT(fAdvX >= 0); |
| 313 | |
| 314 | if (advX >= fAdvX) { |
| 315 | advX = this->advance_interval(advX); |
| 316 | } |
| 317 | SkASSERT(advX < fAdvX); |
| 318 | |
| 319 | fCc = fCc + fDcDx * Sk4f(advX); |
| 320 | fAdvX -= advX; |
| 321 | } |
| 322 | |
| 323 | private: |
| 324 | void compute_interval_props(SkScalar t) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 325 | const Sk4f dC = DstTraits<dstType>::load(fInterval->fDc); |
| 326 | fCc = DstTraits<dstType>::load(fInterval->fC0); |
| 327 | fCc = fCc + dC * Sk4f(t); |
| 328 | fDcDx = dC * fDx; |
| 329 | fZeroRamp = fIsVertical || fInterval->isZeroRamp(); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | const Interval* next_interval(const Interval* i) const { |
| 333 | SkASSERT(i >= fFirstInterval); |
| 334 | SkASSERT(i <= fLastInterval); |
| 335 | i++; |
| 336 | |
| 337 | if (tileMode == kClamp_TileMode) { |
| 338 | SkASSERT(i <= fLastInterval); |
| 339 | return i; |
| 340 | } |
| 341 | |
| 342 | return (i <= fLastInterval) ? i : fFirstInterval; |
| 343 | } |
| 344 | |
| 345 | SkScalar advance_interval(SkScalar advX) { |
| 346 | SkASSERT(advX >= fAdvX); |
| 347 | |
| 348 | do { |
| 349 | advX -= fAdvX; |
| 350 | fInterval = this->next_interval(fInterval); |
| 351 | fAdvX = (fInterval->fP1 - fInterval->fP0) / fDx; |
| 352 | SkASSERT(fAdvX > 0); |
| 353 | } while (advX >= fAdvX); |
| 354 | |
| 355 | compute_interval_props(0); |
| 356 | |
| 357 | SkASSERT(advX >= 0); |
| 358 | return advX; |
| 359 | } |
| 360 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 361 | // Current interval properties. |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 362 | Sk4f fDcDx; // dst color gradient (dc/dx) |
| 363 | Sk4f fCc; // current color, interpolated in dst |
| 364 | SkScalar fAdvX; // remaining interval advance in dst |
| 365 | bool fZeroRamp; // current interval color grad is 0 |
| 366 | |
| 367 | const Interval* fFirstInterval; |
| 368 | const Interval* fLastInterval; |
| 369 | const Interval* fInterval; // current interval |
| 370 | const SkScalar fDx; // 'dx' for consistency with other impls; actually dt/dx |
| 371 | const bool fIsVertical; |
| 372 | }; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 373 | |
| 374 | void SkLinearGradient:: |
| 375 | LinearGradient4fContext::mapTs(int x, int y, SkScalar ts[], int count) const { |
| 376 | SkASSERT(count > 0); |
| 377 | SkASSERT(fDstToPosClass != kLinear_MatrixClass); |
| 378 | |
| 379 | SkScalar sx = x + SK_ScalarHalf; |
| 380 | const SkScalar sy = y + SK_ScalarHalf; |
| 381 | SkPoint pt; |
| 382 | |
| 383 | if (fDstToPosClass != kPerspective_MatrixClass) { |
| 384 | // kLinear_MatrixClass, kFixedStepInX_MatrixClass => fixed dt per scanline |
| 385 | const SkScalar dtdx = fDstToPos.fixedStepInX(sy).x(); |
| 386 | fDstToPosProc(fDstToPos, sx, sy, &pt); |
| 387 | |
| 388 | const Sk4f dtdx4 = Sk4f(4 * dtdx); |
| 389 | Sk4f t4 = Sk4f(pt.x() + 0 * dtdx, |
| 390 | pt.x() + 1 * dtdx, |
| 391 | pt.x() + 2 * dtdx, |
| 392 | pt.x() + 3 * dtdx); |
| 393 | |
| 394 | while (count >= 4) { |
| 395 | t4.store(ts); |
| 396 | t4 = t4 + dtdx4; |
| 397 | ts += 4; |
| 398 | count -= 4; |
| 399 | } |
| 400 | |
| 401 | if (count & 2) { |
| 402 | *ts++ = t4[0]; |
| 403 | *ts++ = t4[1]; |
| 404 | t4 = SkNx_shuffle<2, 0, 1, 3>(t4); |
| 405 | } |
| 406 | |
| 407 | if (count & 1) { |
| 408 | *ts++ = t4[0]; |
| 409 | } |
| 410 | } else { |
| 411 | for (int i = 0; i < count; ++i) { |
| 412 | fDstToPosProc(fDstToPos, sx, sy, &pt); |
| 413 | ts[i] = pt.x(); |
| 414 | sx += SK_Scalar1; |
| 415 | } |
| 416 | } |
| 417 | } |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 418 | |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 419 | bool SkLinearGradient::LinearGradient4fContext::onChooseBlitProcs(const SkImageInfo& info, |
| 420 | BlitState* state) { |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 421 | SkXfermode::Mode mode; |
| 422 | if (!SkXfermode::AsMode(state->fXfer, &mode)) { |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 423 | return false; |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 424 | } |
| 425 | |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 426 | if (mode != SkXfermode::kSrc_Mode && |
fmalita | eadf3cf | 2016-04-07 13:16:13 -0700 | [diff] [blame] | 427 | !(mode == SkXfermode::kSrcOver_Mode && (fFlags & kOpaqueAlpha_Flag))) { |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 428 | return false; |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | switch (info.colorType()) { |
| 432 | case kN32_SkColorType: |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 433 | state->fBlitBW = D32_BlitBW; |
| 434 | return true; |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 435 | case kRGBA_F16_SkColorType: |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 436 | state->fBlitBW = D64_BlitBW; |
| 437 | return true; |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 438 | default: |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 439 | return false; |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | void SkLinearGradient:: |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 444 | LinearGradient4fContext::D32_BlitBW(BlitState* state, int x, int y, const SkPixmap& dst, |
| 445 | int count) { |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 446 | // FIXME: ignoring coverage for now |
| 447 | const LinearGradient4fContext* ctx = |
| 448 | static_cast<const LinearGradient4fContext*>(state->fCtx); |
| 449 | |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 450 | if (!dst.info().gammaCloseToSRGB()) { |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 451 | if (ctx->fColorsArePremul) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 452 | ctx->shadePremulSpan<DstType::L32, ApplyPremul::False>( |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 453 | x, y, dst.writable_addr32(x, y), count); |
| 454 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 455 | ctx->shadePremulSpan<DstType::L32, ApplyPremul::True>( |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 456 | x, y, dst.writable_addr32(x, y), count); |
| 457 | } |
| 458 | } else { |
| 459 | if (ctx->fColorsArePremul) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 460 | ctx->shadePremulSpan<DstType::S32, ApplyPremul::False>( |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 461 | x, y, dst.writable_addr32(x, y), count); |
| 462 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 463 | ctx->shadePremulSpan<DstType::S32, ApplyPremul::True>( |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 464 | x, y, dst.writable_addr32(x, y), count); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void SkLinearGradient:: |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 470 | LinearGradient4fContext::D64_BlitBW(BlitState* state, int x, int y, const SkPixmap& dst, |
| 471 | int count) { |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 472 | // FIXME: ignoring coverage for now |
| 473 | const LinearGradient4fContext* ctx = |
| 474 | static_cast<const LinearGradient4fContext*>(state->fCtx); |
| 475 | |
| 476 | if (ctx->fColorsArePremul) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 477 | ctx->shadePremulSpan<DstType::F16, ApplyPremul::False>( |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 478 | x, y, dst.writable_addr64(x, y), count); |
| 479 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 480 | ctx->shadePremulSpan<DstType::F16, ApplyPremul::True>( |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 481 | x, y, dst.writable_addr64(x, y), count); |
| 482 | } |
| 483 | } |