blob: ee701bd57b0085bdb8a4cff66281966abc44e02a [file] [log] [blame]
fmalitabc590c02016-02-22 09:12:33 -08001/*
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"
fmalita7e6fcf82016-03-10 11:18:43 -08009
10#include <functional>
fmalitabc590c02016-02-22 09:12:33 -080011
12namespace {
13
Florin Malita0e36b3f2017-06-05 23:33:45 -040014Sk4f pack_color(const SkColor4f& c4f, bool premul, const Sk4f& component_scale) {
fmalitab9313362016-04-15 13:03:07 -070015 const Sk4f pm4f = premul
16 ? c4f.premul().to4f()
17 : Sk4f{c4f.fR, c4f.fG, c4f.fB, c4f.fA};
18
19 return pm4f * component_scale;
fmalitabc590c02016-02-22 09:12:33 -080020}
21
fmalita7e6fcf82016-03-10 11:18:43 -080022class IntervalIterator {
23public:
Florin Malita0e36b3f2017-06-05 23:33:45 -040024 IntervalIterator(const SkGradientShaderBase& shader, SkColorSpace* dstCS, bool reverse)
25 : fShader(shader)
26 , fDstCS(dstCS)
fmalita7e6fcf82016-03-10 11:18:43 -080027 , fFirstPos(reverse ? SK_Scalar1 : 0)
Florin Malita0e36b3f2017-06-05 23:33:45 -040028 , fBegin(reverse ? shader.fColorCount - 1 : 0)
fmalita7e6fcf82016-03-10 11:18:43 -080029 , fAdvance(reverse ? -1 : 1) {
Florin Malita0e36b3f2017-06-05 23:33:45 -040030 SkASSERT(shader.fColorCount > 0);
fmalita7e6fcf82016-03-10 11:18:43 -080031 }
32
Florin Malita0e36b3f2017-06-05 23:33:45 -040033 void iterate(std::function<void(const SkColor4f&, const SkColor4f&,
34 SkScalar, SkScalar)> func) const {
35 if (!fShader.fOrigPos) {
fmalita7e6fcf82016-03-10 11:18:43 -080036 this->iterateImplicitPos(func);
37 return;
38 }
39
Florin Malita0e36b3f2017-06-05 23:33:45 -040040 const int end = fBegin + fAdvance * (fShader.fColorCount - 1);
fmalita7e6fcf82016-03-10 11:18:43 -080041 int prev = fBegin;
42 SkScalar prevPos = fFirstPos;
43
44 do {
45 const int curr = prev + fAdvance;
Florin Malita0e36b3f2017-06-05 23:33:45 -040046 SkASSERT(curr >= 0 && curr < fShader.fColorCount);
fmalita7e6fcf82016-03-10 11:18:43 -080047
Florin Malita3e20d022017-11-03 12:11:38 -040048 const SkScalar currPos = fShader.fOrigPos[curr];
fmalita7e6fcf82016-03-10 11:18:43 -080049 if (currPos != prevPos) {
50 SkASSERT((currPos - prevPos > 0) == (fAdvance > 0));
Florin Malita0e36b3f2017-06-05 23:33:45 -040051 func(fShader.getXformedColor(prev, fDstCS), fShader.getXformedColor(curr, fDstCS),
52 prevPos, currPos);
fmalita7e6fcf82016-03-10 11:18:43 -080053 }
54
55 prev = curr;
56 prevPos = currPos;
57 } while (prev != end);
58 }
59
60private:
Florin Malita0e36b3f2017-06-05 23:33:45 -040061 void iterateImplicitPos(std::function<void(const SkColor4f&, const SkColor4f&,
62 SkScalar, SkScalar)> func) const {
fmalita7e6fcf82016-03-10 11:18:43 -080063 // When clients don't provide explicit color stop positions (fPos == nullptr),
64 // the color stops are distributed evenly across the unit interval
65 // (implicit positioning).
Florin Malita0e36b3f2017-06-05 23:33:45 -040066 const SkScalar dt = fAdvance * SK_Scalar1 / (fShader.fColorCount - 1);
67 const int end = fBegin + fAdvance * (fShader.fColorCount - 2);
fmalita7e6fcf82016-03-10 11:18:43 -080068 int prev = fBegin;
69 SkScalar prevPos = fFirstPos;
70
71 while (prev != end) {
72 const int curr = prev + fAdvance;
Florin Malita0e36b3f2017-06-05 23:33:45 -040073 SkASSERT(curr >= 0 && curr < fShader.fColorCount);
fmalita7e6fcf82016-03-10 11:18:43 -080074
75 const SkScalar currPos = prevPos + dt;
Florin Malita0e36b3f2017-06-05 23:33:45 -040076 func(fShader.getXformedColor(prev, fDstCS),
77 fShader.getXformedColor(curr, fDstCS),
78 prevPos, currPos);
fmalita7e6fcf82016-03-10 11:18:43 -080079 prev = curr;
80 prevPos = currPos;
81 }
82
83 // emit the last interval with a pinned end position, to avoid precision issues
Florin Malita0e36b3f2017-06-05 23:33:45 -040084 func(fShader.getXformedColor(prev, fDstCS),
85 fShader.getXformedColor(prev + fAdvance, fDstCS),
86 prevPos, 1 - fFirstPos);
fmalita7e6fcf82016-03-10 11:18:43 -080087 }
88
Florin Malita0e36b3f2017-06-05 23:33:45 -040089 const SkGradientShaderBase& fShader;
90 SkColorSpace* fDstCS;
91 const SkScalar fFirstPos;
92 const int fBegin;
93 const int fAdvance;
fmalita7e6fcf82016-03-10 11:18:43 -080094};
95
Florin Malita0e36b3f2017-06-05 23:33:45 -040096void addMirrorIntervals(const SkGradientShaderBase& shader,
97 SkColorSpace* dstCS,
Florin Malitada4545b2017-03-23 17:04:54 -040098 const Sk4f& componentScale,
99 bool premulColors, bool reverse,
100 Sk4fGradientIntervalBuffer::BufferType* buffer) {
Florin Malita0e36b3f2017-06-05 23:33:45 -0400101 const IntervalIterator iter(shader, dstCS, reverse);
102 iter.iterate([&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) {
Florin Malitacf20f782017-04-07 14:56:14 -0400103 SkASSERT(buffer->empty() || buffer->back().fT1 == 2 - t0);
Florin Malitada4545b2017-03-23 17:04:54 -0400104
Florin Malitacf20f782017-04-07 14:56:14 -0400105 const auto mirror_t0 = 2 - t0;
106 const auto mirror_t1 = 2 - t1;
Florin Malitada4545b2017-03-23 17:04:54 -0400107 // mirror_p1 & mirror_p1 may collapse for very small values - recheck to avoid
108 // triggering Interval asserts.
Florin Malitacf20f782017-04-07 14:56:14 -0400109 if (mirror_t0 != mirror_t1) {
110 buffer->emplace_back(pack_color(c0, premulColors, componentScale), mirror_t0,
111 pack_color(c1, premulColors, componentScale), mirror_t1);
Florin Malitada4545b2017-03-23 17:04:54 -0400112 }
113 });
114}
115
fmalitabc590c02016-02-22 09:12:33 -0800116} // anonymous namespace
117
Florin Malitacf20f782017-04-07 14:56:14 -0400118Sk4fGradientInterval::Sk4fGradientInterval(const Sk4f& c0, SkScalar t0,
119 const Sk4f& c1, SkScalar t1)
120 : fT0(t0)
Florin Malita63f717d2017-05-08 09:53:11 -0400121 , fT1(t1) {
Florin Malitacf20f782017-04-07 14:56:14 -0400122 SkASSERT(t0 != t1);
fmalita8f457592016-10-21 06:02:22 -0700123 // Either p0 or p1 can be (-)inf for synthetic clamp edge intervals.
Florin Malitacf20f782017-04-07 14:56:14 -0400124 SkASSERT(SkScalarIsFinite(t0) || SkScalarIsFinite(t1));
fmalita8f457592016-10-21 06:02:22 -0700125
Florin Malitacf20f782017-04-07 14:56:14 -0400126 const auto dt = t1 - t0;
fmalita8f457592016-10-21 06:02:22 -0700127
128 // Clamp edge intervals are always zero-ramp.
Florin Malita63f717d2017-05-08 09:53:11 -0400129 SkASSERT(SkScalarIsFinite(dt) || (c0 == c1).allTrue());
130 SkASSERT(SkScalarIsFinite(t0) || (c0 == c1).allTrue());
Florin Malitacf20f782017-04-07 14:56:14 -0400131 const Sk4f dc = SkScalarIsFinite(dt) ? (c1 - c0) / dt : 0;
132 const Sk4f bias = c0 - (SkScalarIsFinite(t0) ? t0 * dc : 0);
fmalitabc590c02016-02-22 09:12:33 -0800133
Florin Malitacf20f782017-04-07 14:56:14 -0400134 bias.store(&fCb.fVec);
135 dc.store(&fCg.fVec);
fmalitabc590c02016-02-22 09:12:33 -0800136}
137
Florin Malita0e36b3f2017-06-05 23:33:45 -0400138void Sk4fGradientIntervalBuffer::init(const SkGradientShaderBase& shader, SkColorSpace* dstCS,
Florin Malitada4545b2017-03-23 17:04:54 -0400139 SkShader::TileMode tileMode, bool premulColors,
140 SkScalar alpha, bool reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800141 // The main job here is to build a specialized interval list: a different
142 // representation of the color stops data, optimized for efficient scan line
143 // access during shading.
144 //
145 // [{P0,C0} , {P1,C1}) [{P1,C2} , {P2,c3}) ... [{Pn,C2n} , {Pn+1,C2n+1})
146 //
147 // The list may be inverted when requested (such that e.g. points are sorted
148 // in increasing x order when dx < 0).
149 //
150 // Note: the current representation duplicates pos data; we could refactor to
151 // avoid this if interval storage size becomes a concern.
152 //
153 // Aside from reordering, we also perform two more pre-processing steps at
154 // this stage:
155 //
156 // 1) scale the color components depending on paint alpha and the requested
157 // interpolation space (note: the interval color storage is SkPM4f, but
158 // that doesn't necessarily mean the colors are premultiplied; that
159 // property is tracked in fColorsArePremul)
160 //
161 // 2) inject synthetic intervals to support tiling.
162 //
163 // * for kRepeat, no extra intervals are needed - the iterator just
164 // wraps around at the end:
165 //
166 // ->[P0,P1)->..[Pn-1,Pn)->
167 //
168 // * for kClamp, we add two "infinite" intervals before/after:
169 //
170 // [-/+inf , P0)->[P0 , P1)->..[Pn-1 , Pn)->[Pn , +/-inf)
171 //
172 // (the iterator should never run off the end in this mode)
173 //
174 // * for kMirror, we extend the range to [0..2] and add a flipped
175 // interval series - then the iterator operates just as in the
176 // kRepeat case:
177 //
178 // ->[P0,P1)->..[Pn-1,Pn)->[2 - Pn,2 - Pn-1)->..[2 - P1,2 - P0)->
179 //
180 // TODO: investigate collapsing intervals << 1px.
181
Florin Malita0e36b3f2017-06-05 23:33:45 -0400182 const auto count = shader.fColorCount;
183
Florin Malitada4545b2017-03-23 17:04:54 -0400184 SkASSERT(count > 0);
fmalita7e6fcf82016-03-10 11:18:43 -0800185
Florin Malitada4545b2017-03-23 17:04:54 -0400186 fIntervals.reset();
187
188 const Sk4f componentScale = premulColors
189 ? Sk4f(alpha)
190 : Sk4f(1.0f, 1.0f, 1.0f, alpha);
191 const int first_index = reverse ? count - 1 : 0;
192 const int last_index = count - 1 - first_index;
fmalita7e6fcf82016-03-10 11:18:43 -0800193 const SkScalar first_pos = reverse ? SK_Scalar1 : 0;
194 const SkScalar last_pos = SK_Scalar1 - first_pos;
195
Florin Malitada4545b2017-03-23 17:04:54 -0400196 if (tileMode == SkShader::kClamp_TileMode) {
fmalita7e6fcf82016-03-10 11:18:43 -0800197 // synthetic edge interval: -/+inf .. P0
Florin Malita0e36b3f2017-06-05 23:33:45 -0400198 const Sk4f clamp_color = pack_color(shader.getXformedColor(first_index, dstCS),
Florin Malitada4545b2017-03-23 17:04:54 -0400199 premulColors, componentScale);
fmalita8f457592016-10-21 06:02:22 -0700200 const SkScalar clamp_pos = reverse ? SK_ScalarInfinity : SK_ScalarNegativeInfinity;
fmalita7e6fcf82016-03-10 11:18:43 -0800201 fIntervals.emplace_back(clamp_color, clamp_pos,
fmalitab9313362016-04-15 13:03:07 -0700202 clamp_color, first_pos);
Florin Malitada4545b2017-03-23 17:04:54 -0400203 } else if (tileMode == SkShader::kMirror_TileMode && reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800204 // synthetic mirror intervals injected before main intervals: (2 .. 1]
Florin Malita0e36b3f2017-06-05 23:33:45 -0400205 addMirrorIntervals(shader, dstCS, componentScale, premulColors, false, &fIntervals);
fmalita7e6fcf82016-03-10 11:18:43 -0800206 }
207
Florin Malita0e36b3f2017-06-05 23:33:45 -0400208 const IntervalIterator iter(shader, dstCS, reverse);
209 iter.iterate([&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) {
Florin Malitacf20f782017-04-07 14:56:14 -0400210 SkASSERT(fIntervals.empty() || fIntervals.back().fT1 == t0);
fmalita7e6fcf82016-03-10 11:18:43 -0800211
Florin Malitacf20f782017-04-07 14:56:14 -0400212 fIntervals.emplace_back(pack_color(c0, premulColors, componentScale), t0,
213 pack_color(c1, premulColors, componentScale), t1);
fmalita7e6fcf82016-03-10 11:18:43 -0800214 });
215
Florin Malitada4545b2017-03-23 17:04:54 -0400216 if (tileMode == SkShader::kClamp_TileMode) {
fmalita7e6fcf82016-03-10 11:18:43 -0800217 // synthetic edge interval: Pn .. +/-inf
Florin Malita0e36b3f2017-06-05 23:33:45 -0400218 const Sk4f clamp_color = pack_color(shader.getXformedColor(last_index, dstCS),
219 premulColors, componentScale);
fmalita8f457592016-10-21 06:02:22 -0700220 const SkScalar clamp_pos = reverse ? SK_ScalarNegativeInfinity : SK_ScalarInfinity;
fmalita7e6fcf82016-03-10 11:18:43 -0800221 fIntervals.emplace_back(clamp_color, last_pos,
fmalitab9313362016-04-15 13:03:07 -0700222 clamp_color, clamp_pos);
Florin Malitada4545b2017-03-23 17:04:54 -0400223 } else if (tileMode == SkShader::kMirror_TileMode && !reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800224 // synthetic mirror intervals injected after main intervals: [1 .. 2)
Florin Malita0e36b3f2017-06-05 23:33:45 -0400225 addMirrorIntervals(shader, dstCS, componentScale, premulColors, true, &fIntervals);
fmalita7e6fcf82016-03-10 11:18:43 -0800226 }
227}
228
Florin Malitada4545b2017-03-23 17:04:54 -0400229const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::find(SkScalar t) const {
230 // Binary search.
231 const auto* i0 = fIntervals.begin();
232 const auto* i1 = fIntervals.end() - 1;
fmalita7e6fcf82016-03-10 11:18:43 -0800233
Florin Malitada4545b2017-03-23 17:04:54 -0400234 while (i0 != i1) {
235 SkASSERT(i0 < i1);
Florin Malitacf20f782017-04-07 14:56:14 -0400236 SkASSERT(t >= i0->fT0 && t <= i1->fT1);
Florin Malitada4545b2017-03-23 17:04:54 -0400237
238 const auto* i = i0 + ((i1 - i0) >> 1);
239
Florin Malitacf20f782017-04-07 14:56:14 -0400240 if (t > i->fT1) {
Florin Malitada4545b2017-03-23 17:04:54 -0400241 i0 = i + 1;
242 } else {
243 i1 = i;
fmalitacc341762016-11-02 13:10:51 -0700244 }
Florin Malitada4545b2017-03-23 17:04:54 -0400245 }
246
247 SkASSERT(i0->contains(t));
248 return i0;
249}
250
251const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::findNext(
252 SkScalar t, const Sk4fGradientInterval* prev, bool increasing) const {
253
254 SkASSERT(!prev->contains(t));
255 SkASSERT(prev >= fIntervals.begin() && prev < fIntervals.end());
Florin Malitacf20f782017-04-07 14:56:14 -0400256 SkASSERT(t >= fIntervals.front().fT0 && t <= fIntervals.back().fT1);
Florin Malitada4545b2017-03-23 17:04:54 -0400257
258 const auto* i = prev;
259
260 // Use the |increasing| signal to figure which direction we should search for
261 // the next interval, then perform a linear search.
262 if (increasing) {
263 do {
264 i += 1;
265 if (i >= fIntervals.end()) {
266 i = fIntervals.begin();
267 }
268 } while (!i->contains(t));
269 } else {
270 do {
271 i -= 1;
272 if (i < fIntervals.begin()) {
273 i = fIntervals.end() - 1;
274 }
275 } while (!i->contains(t));
276 }
277
278 return i;
279}
280
281SkGradientShaderBase::
282GradientShaderBase4fContext::GradientShaderBase4fContext(const SkGradientShaderBase& shader,
283 const ContextRec& rec)
284 : INHERITED(shader, rec)
285 , fFlags(this->INHERITED::getFlags())
Florin Malitaaa0ce822017-08-28 12:50:26 -0400286 , fDither(rec.fPaint->isDither())
Florin Malitada4545b2017-03-23 17:04:54 -0400287{
288 const SkMatrix& inverse = this->getTotalInverse();
289 fDstToPos.setConcat(shader.fPtsToUnit, inverse);
Florin Malita4d41b8f2017-07-12 14:35:46 -0400290 SkASSERT(!fDstToPos.hasPerspective());
Cary Clark9480d822017-10-19 18:01:13 -0400291 fDstToPosProc = SkMatrixPriv::GetMapXYProc(fDstToPos);
Florin Malitada4545b2017-03-23 17:04:54 -0400292
293 if (shader.fColorsAreOpaque && this->getPaintAlpha() == SK_AlphaOPAQUE) {
294 fFlags |= kOpaqueAlpha_Flag;
295 }
296
297 fColorsArePremul =
298 (shader.fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag)
299 || shader.fColorsAreOpaque;
300}
301
302bool SkGradientShaderBase::
303GradientShaderBase4fContext::isValid() const {
304 return fDstToPos.isFinite();
fmalita7e6fcf82016-03-10 11:18:43 -0800305}