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