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 "Sk4fGradientBase.h" |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 9 | |
| 10 | #include <functional> |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 11 | |
| 12 | namespace { |
| 13 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 14 | Sk4f pack_color(const SkColor4f& c4f, bool premul, const Sk4f& component_scale) { |
fmalita | b931336 | 2016-04-15 13:03:07 -0700 | [diff] [blame] | 15 | const Sk4f pm4f = premul |
| 16 | ? c4f.premul().to4f() |
| 17 | : Sk4f{c4f.fR, c4f.fG, c4f.fB, c4f.fA}; |
| 18 | |
| 19 | return pm4f * component_scale; |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 20 | } |
| 21 | |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 22 | class IntervalIterator { |
| 23 | public: |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 24 | IntervalIterator(const SkGradientShaderBase& shader, SkColorSpace* dstCS, bool reverse) |
| 25 | : fShader(shader) |
| 26 | , fDstCS(dstCS) |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 27 | , fFirstPos(reverse ? SK_Scalar1 : 0) |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 28 | , fBegin(reverse ? shader.fColorCount - 1 : 0) |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 29 | , fAdvance(reverse ? -1 : 1) { |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 30 | SkASSERT(shader.fColorCount > 0); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 33 | void iterate(std::function<void(const SkColor4f&, const SkColor4f&, |
| 34 | SkScalar, SkScalar)> func) const { |
| 35 | if (!fShader.fOrigPos) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 36 | this->iterateImplicitPos(func); |
| 37 | return; |
| 38 | } |
| 39 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 40 | const int end = fBegin + fAdvance * (fShader.fColorCount - 1); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 41 | const SkScalar lastPos = 1 - fFirstPos; |
| 42 | int prev = fBegin; |
| 43 | SkScalar prevPos = fFirstPos; |
| 44 | |
| 45 | do { |
| 46 | const int curr = prev + fAdvance; |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 47 | SkASSERT(curr >= 0 && curr < fShader.fColorCount); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 48 | |
| 49 | // TODO: this sanitization should be done in SkGradientShaderBase |
| 50 | const SkScalar currPos = (fAdvance > 0) |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 51 | ? SkTPin(fShader.fOrigPos[curr], prevPos, lastPos) |
| 52 | : SkTPin(fShader.fOrigPos[curr], lastPos, prevPos); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 53 | |
| 54 | if (currPos != prevPos) { |
| 55 | SkASSERT((currPos - prevPos > 0) == (fAdvance > 0)); |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 56 | func(fShader.getXformedColor(prev, fDstCS), fShader.getXformedColor(curr, fDstCS), |
| 57 | prevPos, currPos); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | prev = curr; |
| 61 | prevPos = currPos; |
| 62 | } while (prev != end); |
| 63 | } |
| 64 | |
| 65 | private: |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 66 | void iterateImplicitPos(std::function<void(const SkColor4f&, const SkColor4f&, |
| 67 | SkScalar, SkScalar)> func) const { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 68 | // When clients don't provide explicit color stop positions (fPos == nullptr), |
| 69 | // the color stops are distributed evenly across the unit interval |
| 70 | // (implicit positioning). |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 71 | const SkScalar dt = fAdvance * SK_Scalar1 / (fShader.fColorCount - 1); |
| 72 | const int end = fBegin + fAdvance * (fShader.fColorCount - 2); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 73 | int prev = fBegin; |
| 74 | SkScalar prevPos = fFirstPos; |
| 75 | |
| 76 | while (prev != end) { |
| 77 | const int curr = prev + fAdvance; |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 78 | SkASSERT(curr >= 0 && curr < fShader.fColorCount); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 79 | |
| 80 | const SkScalar currPos = prevPos + dt; |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 81 | func(fShader.getXformedColor(prev, fDstCS), |
| 82 | fShader.getXformedColor(curr, fDstCS), |
| 83 | prevPos, currPos); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 84 | prev = curr; |
| 85 | prevPos = currPos; |
| 86 | } |
| 87 | |
| 88 | // emit the last interval with a pinned end position, to avoid precision issues |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 89 | func(fShader.getXformedColor(prev, fDstCS), |
| 90 | fShader.getXformedColor(prev + fAdvance, fDstCS), |
| 91 | prevPos, 1 - fFirstPos); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 94 | const SkGradientShaderBase& fShader; |
| 95 | SkColorSpace* fDstCS; |
| 96 | const SkScalar fFirstPos; |
| 97 | const int fBegin; |
| 98 | const int fAdvance; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 101 | void addMirrorIntervals(const SkGradientShaderBase& shader, |
| 102 | SkColorSpace* dstCS, |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 103 | const Sk4f& componentScale, |
| 104 | bool premulColors, bool reverse, |
| 105 | Sk4fGradientIntervalBuffer::BufferType* buffer) { |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 106 | const IntervalIterator iter(shader, dstCS, reverse); |
| 107 | iter.iterate([&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 108 | SkASSERT(buffer->empty() || buffer->back().fT1 == 2 - t0); |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 109 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 110 | const auto mirror_t0 = 2 - t0; |
| 111 | const auto mirror_t1 = 2 - t1; |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 112 | // mirror_p1 & mirror_p1 may collapse for very small values - recheck to avoid |
| 113 | // triggering Interval asserts. |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 114 | if (mirror_t0 != mirror_t1) { |
| 115 | buffer->emplace_back(pack_color(c0, premulColors, componentScale), mirror_t0, |
| 116 | pack_color(c1, premulColors, componentScale), mirror_t1); |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 117 | } |
| 118 | }); |
| 119 | } |
| 120 | |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 121 | } // anonymous namespace |
| 122 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 123 | Sk4fGradientInterval::Sk4fGradientInterval(const Sk4f& c0, SkScalar t0, |
| 124 | const Sk4f& c1, SkScalar t1) |
| 125 | : fT0(t0) |
Florin Malita | 63f717d | 2017-05-08 09:53:11 -0400 | [diff] [blame] | 126 | , fT1(t1) { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 127 | SkASSERT(t0 != t1); |
fmalita | 8f45759 | 2016-10-21 06:02:22 -0700 | [diff] [blame] | 128 | // Either p0 or p1 can be (-)inf for synthetic clamp edge intervals. |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 129 | SkASSERT(SkScalarIsFinite(t0) || SkScalarIsFinite(t1)); |
fmalita | 8f45759 | 2016-10-21 06:02:22 -0700 | [diff] [blame] | 130 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 131 | const auto dt = t1 - t0; |
fmalita | 8f45759 | 2016-10-21 06:02:22 -0700 | [diff] [blame] | 132 | |
| 133 | // Clamp edge intervals are always zero-ramp. |
Florin Malita | 63f717d | 2017-05-08 09:53:11 -0400 | [diff] [blame] | 134 | SkASSERT(SkScalarIsFinite(dt) || (c0 == c1).allTrue()); |
| 135 | SkASSERT(SkScalarIsFinite(t0) || (c0 == c1).allTrue()); |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 136 | const Sk4f dc = SkScalarIsFinite(dt) ? (c1 - c0) / dt : 0; |
| 137 | const Sk4f bias = c0 - (SkScalarIsFinite(t0) ? t0 * dc : 0); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 138 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 139 | bias.store(&fCb.fVec); |
| 140 | dc.store(&fCg.fVec); |
fmalita | bc590c0 | 2016-02-22 09:12:33 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 143 | void Sk4fGradientIntervalBuffer::init(const SkGradientShaderBase& shader, SkColorSpace* dstCS, |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 144 | SkShader::TileMode tileMode, bool premulColors, |
| 145 | SkScalar alpha, bool reverse) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 146 | // The main job here is to build a specialized interval list: a different |
| 147 | // representation of the color stops data, optimized for efficient scan line |
| 148 | // access during shading. |
| 149 | // |
| 150 | // [{P0,C0} , {P1,C1}) [{P1,C2} , {P2,c3}) ... [{Pn,C2n} , {Pn+1,C2n+1}) |
| 151 | // |
| 152 | // The list may be inverted when requested (such that e.g. points are sorted |
| 153 | // in increasing x order when dx < 0). |
| 154 | // |
| 155 | // Note: the current representation duplicates pos data; we could refactor to |
| 156 | // avoid this if interval storage size becomes a concern. |
| 157 | // |
| 158 | // Aside from reordering, we also perform two more pre-processing steps at |
| 159 | // this stage: |
| 160 | // |
| 161 | // 1) scale the color components depending on paint alpha and the requested |
| 162 | // interpolation space (note: the interval color storage is SkPM4f, but |
| 163 | // that doesn't necessarily mean the colors are premultiplied; that |
| 164 | // property is tracked in fColorsArePremul) |
| 165 | // |
| 166 | // 2) inject synthetic intervals to support tiling. |
| 167 | // |
| 168 | // * for kRepeat, no extra intervals are needed - the iterator just |
| 169 | // wraps around at the end: |
| 170 | // |
| 171 | // ->[P0,P1)->..[Pn-1,Pn)-> |
| 172 | // |
| 173 | // * for kClamp, we add two "infinite" intervals before/after: |
| 174 | // |
| 175 | // [-/+inf , P0)->[P0 , P1)->..[Pn-1 , Pn)->[Pn , +/-inf) |
| 176 | // |
| 177 | // (the iterator should never run off the end in this mode) |
| 178 | // |
| 179 | // * for kMirror, we extend the range to [0..2] and add a flipped |
| 180 | // interval series - then the iterator operates just as in the |
| 181 | // kRepeat case: |
| 182 | // |
| 183 | // ->[P0,P1)->..[Pn-1,Pn)->[2 - Pn,2 - Pn-1)->..[2 - P1,2 - P0)-> |
| 184 | // |
| 185 | // TODO: investigate collapsing intervals << 1px. |
| 186 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 187 | const auto count = shader.fColorCount; |
| 188 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 189 | SkASSERT(count > 0); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 190 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 191 | fIntervals.reset(); |
| 192 | |
| 193 | const Sk4f componentScale = premulColors |
| 194 | ? Sk4f(alpha) |
| 195 | : Sk4f(1.0f, 1.0f, 1.0f, alpha); |
| 196 | const int first_index = reverse ? count - 1 : 0; |
| 197 | const int last_index = count - 1 - first_index; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 198 | const SkScalar first_pos = reverse ? SK_Scalar1 : 0; |
| 199 | const SkScalar last_pos = SK_Scalar1 - first_pos; |
| 200 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 201 | if (tileMode == SkShader::kClamp_TileMode) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 202 | // synthetic edge interval: -/+inf .. P0 |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 203 | const Sk4f clamp_color = pack_color(shader.getXformedColor(first_index, dstCS), |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 204 | premulColors, componentScale); |
fmalita | 8f45759 | 2016-10-21 06:02:22 -0700 | [diff] [blame] | 205 | const SkScalar clamp_pos = reverse ? SK_ScalarInfinity : SK_ScalarNegativeInfinity; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 206 | fIntervals.emplace_back(clamp_color, clamp_pos, |
fmalita | b931336 | 2016-04-15 13:03:07 -0700 | [diff] [blame] | 207 | clamp_color, first_pos); |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 208 | } else if (tileMode == SkShader::kMirror_TileMode && reverse) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 209 | // synthetic mirror intervals injected before main intervals: (2 .. 1] |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 210 | addMirrorIntervals(shader, dstCS, componentScale, premulColors, false, &fIntervals); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 213 | const IntervalIterator iter(shader, dstCS, reverse); |
| 214 | iter.iterate([&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 215 | SkASSERT(fIntervals.empty() || fIntervals.back().fT1 == t0); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 216 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 217 | fIntervals.emplace_back(pack_color(c0, premulColors, componentScale), t0, |
| 218 | pack_color(c1, premulColors, componentScale), t1); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 219 | }); |
| 220 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 221 | if (tileMode == SkShader::kClamp_TileMode) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 222 | // synthetic edge interval: Pn .. +/-inf |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 223 | const Sk4f clamp_color = pack_color(shader.getXformedColor(last_index, dstCS), |
| 224 | premulColors, componentScale); |
fmalita | 8f45759 | 2016-10-21 06:02:22 -0700 | [diff] [blame] | 225 | const SkScalar clamp_pos = reverse ? SK_ScalarNegativeInfinity : SK_ScalarInfinity; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 226 | fIntervals.emplace_back(clamp_color, last_pos, |
fmalita | b931336 | 2016-04-15 13:03:07 -0700 | [diff] [blame] | 227 | clamp_color, clamp_pos); |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 228 | } else if (tileMode == SkShader::kMirror_TileMode && !reverse) { |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 229 | // synthetic mirror intervals injected after main intervals: [1 .. 2) |
Florin Malita | 0e36b3f | 2017-06-05 23:33:45 -0400 | [diff] [blame^] | 230 | addMirrorIntervals(shader, dstCS, componentScale, premulColors, true, &fIntervals); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 234 | const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::find(SkScalar t) const { |
| 235 | // Binary search. |
| 236 | const auto* i0 = fIntervals.begin(); |
| 237 | const auto* i1 = fIntervals.end() - 1; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 238 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 239 | while (i0 != i1) { |
| 240 | SkASSERT(i0 < i1); |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 241 | SkASSERT(t >= i0->fT0 && t <= i1->fT1); |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 242 | |
| 243 | const auto* i = i0 + ((i1 - i0) >> 1); |
| 244 | |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 245 | if (t > i->fT1) { |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 246 | i0 = i + 1; |
| 247 | } else { |
| 248 | i1 = i; |
fmalita | cc34176 | 2016-11-02 13:10:51 -0700 | [diff] [blame] | 249 | } |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | SkASSERT(i0->contains(t)); |
| 253 | return i0; |
| 254 | } |
| 255 | |
| 256 | const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::findNext( |
| 257 | SkScalar t, const Sk4fGradientInterval* prev, bool increasing) const { |
| 258 | |
| 259 | SkASSERT(!prev->contains(t)); |
| 260 | SkASSERT(prev >= fIntervals.begin() && prev < fIntervals.end()); |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 261 | SkASSERT(t >= fIntervals.front().fT0 && t <= fIntervals.back().fT1); |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 262 | |
| 263 | const auto* i = prev; |
| 264 | |
| 265 | // Use the |increasing| signal to figure which direction we should search for |
| 266 | // the next interval, then perform a linear search. |
| 267 | if (increasing) { |
| 268 | do { |
| 269 | i += 1; |
| 270 | if (i >= fIntervals.end()) { |
| 271 | i = fIntervals.begin(); |
| 272 | } |
| 273 | } while (!i->contains(t)); |
| 274 | } else { |
| 275 | do { |
| 276 | i -= 1; |
| 277 | if (i < fIntervals.begin()) { |
| 278 | i = fIntervals.end() - 1; |
| 279 | } |
| 280 | } while (!i->contains(t)); |
| 281 | } |
| 282 | |
| 283 | return i; |
| 284 | } |
| 285 | |
| 286 | SkGradientShaderBase:: |
| 287 | GradientShaderBase4fContext::GradientShaderBase4fContext(const SkGradientShaderBase& shader, |
| 288 | const ContextRec& rec) |
| 289 | : INHERITED(shader, rec) |
| 290 | , fFlags(this->INHERITED::getFlags()) |
| 291 | #ifdef SK_SUPPORT_LEGACY_GRADIENT_DITHERING |
| 292 | , fDither(true) |
| 293 | #else |
| 294 | , fDither(rec.fPaint->isDither()) |
| 295 | #endif |
| 296 | { |
| 297 | const SkMatrix& inverse = this->getTotalInverse(); |
| 298 | fDstToPos.setConcat(shader.fPtsToUnit, inverse); |
| 299 | fDstToPosProc = fDstToPos.getMapXYProc(); |
| 300 | fDstToPosClass = static_cast<uint8_t>(INHERITED::ComputeMatrixClass(fDstToPos)); |
| 301 | |
| 302 | if (shader.fColorsAreOpaque && this->getPaintAlpha() == SK_AlphaOPAQUE) { |
| 303 | fFlags |= kOpaqueAlpha_Flag; |
| 304 | } |
| 305 | |
| 306 | fColorsArePremul = |
| 307 | (shader.fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag) |
| 308 | || shader.fColorsAreOpaque; |
| 309 | } |
| 310 | |
| 311 | bool SkGradientShaderBase:: |
| 312 | GradientShaderBase4fContext::isValid() const { |
| 313 | return fDstToPos.isFinite(); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void SkGradientShaderBase:: |
| 317 | GradientShaderBase4fContext::shadeSpan(int x, int y, SkPMColor dst[], int count) { |
| 318 | if (fColorsArePremul) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 319 | this->shadePremulSpan<DstType::L32, ApplyPremul::False>(x, y, dst, count); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 320 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 321 | this->shadePremulSpan<DstType::L32, ApplyPremul::True>(x, y, dst, count); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | void SkGradientShaderBase:: |
| 326 | GradientShaderBase4fContext::shadeSpan4f(int x, int y, SkPM4f dst[], int count) { |
| 327 | if (fColorsArePremul) { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 328 | this->shadePremulSpan<DstType::F32, ApplyPremul::False>(x, y, dst, count); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 329 | } else { |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 330 | this->shadePremulSpan<DstType::F32, ApplyPremul::True>(x, y, dst, count); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 334 | template<DstType dstType, ApplyPremul premul> |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 335 | void SkGradientShaderBase:: |
| 336 | GradientShaderBase4fContext::shadePremulSpan(int x, int y, |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 337 | typename DstTraits<dstType, premul>::Type dst[], |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 338 | int count) const { |
| 339 | const SkGradientShaderBase& shader = |
| 340 | static_cast<const SkGradientShaderBase&>(fShader); |
| 341 | |
| 342 | switch (shader.fTileMode) { |
| 343 | case kClamp_TileMode: |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 344 | this->shadeSpanInternal<dstType, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 345 | premul, |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 346 | kClamp_TileMode>(x, y, dst, count); |
| 347 | break; |
| 348 | case kRepeat_TileMode: |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 349 | this->shadeSpanInternal<dstType, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 350 | premul, |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 351 | kRepeat_TileMode>(x, y, dst, count); |
| 352 | break; |
| 353 | case kMirror_TileMode: |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 354 | this->shadeSpanInternal<dstType, |
fmalita | a928b28 | 2016-03-18 10:28:23 -0700 | [diff] [blame] | 355 | premul, |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 356 | kMirror_TileMode>(x, y, dst, count); |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 361 | template<DstType dstType, ApplyPremul premul, SkShader::TileMode tileMode> |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 362 | void SkGradientShaderBase:: |
| 363 | GradientShaderBase4fContext::shadeSpanInternal(int x, int y, |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 364 | typename DstTraits<dstType, premul>::Type dst[], |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 365 | int count) const { |
| 366 | static const int kBufSize = 128; |
| 367 | SkScalar ts[kBufSize]; |
fmalita | 3a2e45a | 2016-10-14 08:18:24 -0700 | [diff] [blame] | 368 | TSampler<dstType, premul, tileMode> sampler(*this); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 369 | |
| 370 | SkASSERT(count > 0); |
| 371 | do { |
| 372 | const int n = SkTMin(kBufSize, count); |
| 373 | this->mapTs(x, y, ts, n); |
| 374 | for (int i = 0; i < n; ++i) { |
| 375 | const Sk4f c = sampler.sample(ts[i]); |
fmalita | dc6c9bf | 2016-03-21 13:16:51 -0700 | [diff] [blame] | 376 | DstTraits<dstType, premul>::store(c, dst++); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 377 | } |
| 378 | x += n; |
| 379 | count -= n; |
| 380 | } while (count > 0); |
| 381 | } |
| 382 | |
fmalita | 3a2e45a | 2016-10-14 08:18:24 -0700 | [diff] [blame] | 383 | template<DstType dstType, ApplyPremul premul, SkShader::TileMode tileMode> |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 384 | class SkGradientShaderBase::GradientShaderBase4fContext::TSampler { |
| 385 | public: |
| 386 | TSampler(const GradientShaderBase4fContext& ctx) |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 387 | : fCtx(ctx) |
Florin Malita | 5b1a7c2 | 2016-11-20 19:10:59 -0500 | [diff] [blame] | 388 | , fInterval(nullptr) { |
Florin Malita | 5b1a7c2 | 2016-11-20 19:10:59 -0500 | [diff] [blame] | 389 | switch (tileMode) { |
| 390 | case kClamp_TileMode: |
| 391 | fLargestIntervalValue = SK_ScalarInfinity; |
| 392 | break; |
| 393 | case kRepeat_TileMode: |
| 394 | fLargestIntervalValue = nextafterf(1, 0); |
| 395 | break; |
| 396 | case kMirror_TileMode: |
| 397 | fLargestIntervalValue = nextafterf(2.0f, 0); |
| 398 | break; |
| 399 | } |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | Sk4f sample(SkScalar t) { |
fmalita | 8ffb3e5 | 2016-11-09 11:58:10 -0800 | [diff] [blame] | 403 | const auto tiled_t = tileProc(t); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 404 | |
| 405 | if (!fInterval) { |
| 406 | // Very first sample => locate the initial interval. |
| 407 | // TODO: maybe do this in ctor to remove a branch? |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 408 | fInterval = fCtx.fIntervals.find(tiled_t); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 409 | this->loadIntervalData(fInterval); |
Florin Malita | 3d1a6bc | 2017-02-09 15:05:15 -0500 | [diff] [blame] | 410 | } else if (!fInterval->contains(tiled_t)) { |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 411 | fInterval = fCtx.fIntervals.findNext(tiled_t, fInterval, t >= fPrevT); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 412 | this->loadIntervalData(fInterval); |
| 413 | } |
| 414 | |
| 415 | fPrevT = t; |
| 416 | return lerp(tiled_t); |
| 417 | } |
| 418 | |
| 419 | private: |
fmalita | 8ffb3e5 | 2016-11-09 11:58:10 -0800 | [diff] [blame] | 420 | SkScalar tileProc(SkScalar t) const { |
| 421 | switch (tileMode) { |
| 422 | case kClamp_TileMode: |
| 423 | // synthetic clamp-mode edge intervals allow for a free-floating t: |
| 424 | // [-inf..0)[0..1)[1..+inf) |
| 425 | return t; |
| 426 | case kRepeat_TileMode: |
| 427 | // t % 1 (intervals range: [0..1)) |
Florin Malita | 5b1a7c2 | 2016-11-20 19:10:59 -0500 | [diff] [blame] | 428 | // Due to the extra arithmetic, we must clamp to ensure the value remains less than 1. |
| 429 | return SkTMin(t - SkScalarFloorToScalar(t), fLargestIntervalValue); |
fmalita | 8ffb3e5 | 2016-11-09 11:58:10 -0800 | [diff] [blame] | 430 | case kMirror_TileMode: |
| 431 | // t % 2 (synthetic mirror intervals expand the range to [0..2) |
| 432 | // Due to the extra arithmetic, we must clamp to ensure the value remains less than 2. |
Florin Malita | 5b1a7c2 | 2016-11-20 19:10:59 -0500 | [diff] [blame] | 433 | return SkTMin(t - SkScalarFloorToScalar(t / 2) * 2, fLargestIntervalValue); |
fmalita | 8ffb3e5 | 2016-11-09 11:58:10 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | SK_ABORT("Unhandled tile mode."); |
| 437 | return 0; |
| 438 | } |
| 439 | |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 440 | Sk4f lerp(SkScalar t) { |
Florin Malita | 3d1a6bc | 2017-02-09 15:05:15 -0500 | [diff] [blame] | 441 | SkASSERT(fInterval->contains(t)); |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 442 | return fCb + fCg * t; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 445 | void loadIntervalData(const Sk4fGradientInterval* i) { |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 446 | fCb = DstTraits<dstType, premul>::load(i->fCb); |
| 447 | fCg = DstTraits<dstType, premul>::load(i->fCg); |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Florin Malita | da4545b | 2017-03-23 17:04:54 -0400 | [diff] [blame] | 450 | const GradientShaderBase4fContext& fCtx; |
| 451 | const Sk4fGradientInterval* fInterval; |
| 452 | SkScalar fPrevT; |
| 453 | SkScalar fLargestIntervalValue; |
Florin Malita | cf20f78 | 2017-04-07 14:56:14 -0400 | [diff] [blame] | 454 | Sk4f fCb; |
| 455 | Sk4f fCg; |
fmalita | 7e6fcf8 | 2016-03-10 11:18:43 -0800 | [diff] [blame] | 456 | }; |