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