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