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