blob: cb5aeb434b76ad8e64a2ca0c8542be9aa4e4f982 [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"
Mike Reed75ae4212018-01-23 11:24:08 -05009#include "SkPaint.h"
fmalita7e6fcf82016-03-10 11:18:43 -080010#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) {
Mike Klein7780c4c2018-09-20 15:11:31 -040015 Sk4f pm4f = premul
fmalitab9313362016-04-15 13:03:07 -070016 ? c4f.premul().to4f()
17 : Sk4f{c4f.fR, c4f.fG, c4f.fB, c4f.fA};
18
Mike Klein7780c4c2018-09-20 15:11:31 -040019 if (premul) {
20 // If the stops are premul, we clamp them to gamut now.
21 // If the stops are unpremul, the colors will eventually go through Sk4f_toL32(),
22 // which ends up clamping to gamut then.
23 pm4f = Sk4f::Max(0, Sk4f::Min(pm4f, pm4f[3]));
24 }
25
fmalitab9313362016-04-15 13:03:07 -070026 return pm4f * component_scale;
fmalitabc590c02016-02-22 09:12:33 -080027}
28
fmalita7e6fcf82016-03-10 11:18:43 -080029class IntervalIterator {
30public:
Brian Osman6667fb12018-07-03 16:44:02 -040031 IntervalIterator(const SkGradientShaderBase& shader, bool reverse)
Florin Malita0e36b3f2017-06-05 23:33:45 -040032 : fShader(shader)
fmalita7e6fcf82016-03-10 11:18:43 -080033 , fFirstPos(reverse ? SK_Scalar1 : 0)
Florin Malita0e36b3f2017-06-05 23:33:45 -040034 , fBegin(reverse ? shader.fColorCount - 1 : 0)
fmalita7e6fcf82016-03-10 11:18:43 -080035 , fAdvance(reverse ? -1 : 1) {
Florin Malita0e36b3f2017-06-05 23:33:45 -040036 SkASSERT(shader.fColorCount > 0);
fmalita7e6fcf82016-03-10 11:18:43 -080037 }
38
Brian Osman6667fb12018-07-03 16:44:02 -040039 void iterate(const SkColor4f* colors,
40 std::function<void(const SkColor4f&, const SkColor4f&,
Florin Malita0e36b3f2017-06-05 23:33:45 -040041 SkScalar, SkScalar)> func) const {
42 if (!fShader.fOrigPos) {
Brian Osman6667fb12018-07-03 16:44:02 -040043 this->iterateImplicitPos(colors, func);
fmalita7e6fcf82016-03-10 11:18:43 -080044 return;
45 }
46
Florin Malita0e36b3f2017-06-05 23:33:45 -040047 const int end = fBegin + fAdvance * (fShader.fColorCount - 1);
fmalita7e6fcf82016-03-10 11:18:43 -080048 int prev = fBegin;
49 SkScalar prevPos = fFirstPos;
50
51 do {
52 const int curr = prev + fAdvance;
Florin Malita0e36b3f2017-06-05 23:33:45 -040053 SkASSERT(curr >= 0 && curr < fShader.fColorCount);
fmalita7e6fcf82016-03-10 11:18:43 -080054
Florin Malita3e20d022017-11-03 12:11:38 -040055 const SkScalar currPos = fShader.fOrigPos[curr];
fmalita7e6fcf82016-03-10 11:18:43 -080056 if (currPos != prevPos) {
57 SkASSERT((currPos - prevPos > 0) == (fAdvance > 0));
Brian Osman6667fb12018-07-03 16:44:02 -040058 func(colors[prev], colors[curr], prevPos, currPos);
fmalita7e6fcf82016-03-10 11:18:43 -080059 }
60
61 prev = curr;
62 prevPos = currPos;
63 } while (prev != end);
64 }
65
66private:
Brian Osman6667fb12018-07-03 16:44:02 -040067 void iterateImplicitPos(const SkColor4f* colors,
68 std::function<void(const SkColor4f&, const SkColor4f&,
Florin Malita0e36b3f2017-06-05 23:33:45 -040069 SkScalar, SkScalar)> func) const {
fmalita7e6fcf82016-03-10 11:18:43 -080070 // When clients don't provide explicit color stop positions (fPos == nullptr),
71 // the color stops are distributed evenly across the unit interval
72 // (implicit positioning).
Florin Malita0e36b3f2017-06-05 23:33:45 -040073 const SkScalar dt = fAdvance * SK_Scalar1 / (fShader.fColorCount - 1);
74 const int end = fBegin + fAdvance * (fShader.fColorCount - 2);
fmalita7e6fcf82016-03-10 11:18:43 -080075 int prev = fBegin;
76 SkScalar prevPos = fFirstPos;
77
78 while (prev != end) {
79 const int curr = prev + fAdvance;
Florin Malita0e36b3f2017-06-05 23:33:45 -040080 SkASSERT(curr >= 0 && curr < fShader.fColorCount);
fmalita7e6fcf82016-03-10 11:18:43 -080081
82 const SkScalar currPos = prevPos + dt;
Brian Osman6667fb12018-07-03 16:44:02 -040083 func(colors[prev], colors[curr], prevPos, currPos);
fmalita7e6fcf82016-03-10 11:18:43 -080084 prev = curr;
85 prevPos = currPos;
86 }
87
88 // emit the last interval with a pinned end position, to avoid precision issues
Brian Osman6667fb12018-07-03 16:44:02 -040089 func(colors[prev], colors[prev + fAdvance], prevPos, 1 - fFirstPos);
fmalita7e6fcf82016-03-10 11:18:43 -080090 }
91
Florin Malita0e36b3f2017-06-05 23:33:45 -040092 const SkGradientShaderBase& fShader;
Florin Malita0e36b3f2017-06-05 23:33:45 -040093 const SkScalar fFirstPos;
94 const int fBegin;
95 const int fAdvance;
fmalita7e6fcf82016-03-10 11:18:43 -080096};
97
Florin Malita0e36b3f2017-06-05 23:33:45 -040098void addMirrorIntervals(const SkGradientShaderBase& shader,
Brian Osman6667fb12018-07-03 16:44:02 -040099 const SkColor4f* colors,
Florin Malitada4545b2017-03-23 17:04:54 -0400100 const Sk4f& componentScale,
101 bool premulColors, bool reverse,
102 Sk4fGradientIntervalBuffer::BufferType* buffer) {
Brian Osman6667fb12018-07-03 16:44:02 -0400103 const IntervalIterator iter(shader, reverse);
104 iter.iterate(colors, [&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) {
Florin Malitacf20f782017-04-07 14:56:14 -0400105 SkASSERT(buffer->empty() || buffer->back().fT1 == 2 - t0);
Florin Malitada4545b2017-03-23 17:04:54 -0400106
Florin Malitacf20f782017-04-07 14:56:14 -0400107 const auto mirror_t0 = 2 - t0;
108 const auto mirror_t1 = 2 - t1;
Florin Malitada4545b2017-03-23 17:04:54 -0400109 // mirror_p1 & mirror_p1 may collapse for very small values - recheck to avoid
110 // triggering Interval asserts.
Florin Malitacf20f782017-04-07 14:56:14 -0400111 if (mirror_t0 != mirror_t1) {
112 buffer->emplace_back(pack_color(c0, premulColors, componentScale), mirror_t0,
113 pack_color(c1, premulColors, componentScale), mirror_t1);
Florin Malitada4545b2017-03-23 17:04:54 -0400114 }
115 });
116}
117
fmalitabc590c02016-02-22 09:12:33 -0800118} // anonymous namespace
119
Florin Malitacf20f782017-04-07 14:56:14 -0400120Sk4fGradientInterval::Sk4fGradientInterval(const Sk4f& c0, SkScalar t0,
121 const Sk4f& c1, SkScalar t1)
122 : fT0(t0)
Florin Malita63f717d2017-05-08 09:53:11 -0400123 , fT1(t1) {
Florin Malitacf20f782017-04-07 14:56:14 -0400124 SkASSERT(t0 != t1);
fmalita8f457592016-10-21 06:02:22 -0700125 // Either p0 or p1 can be (-)inf for synthetic clamp edge intervals.
Florin Malitacf20f782017-04-07 14:56:14 -0400126 SkASSERT(SkScalarIsFinite(t0) || SkScalarIsFinite(t1));
fmalita8f457592016-10-21 06:02:22 -0700127
Florin Malitacf20f782017-04-07 14:56:14 -0400128 const auto dt = t1 - t0;
fmalita8f457592016-10-21 06:02:22 -0700129
130 // Clamp edge intervals are always zero-ramp.
Florin Malita63f717d2017-05-08 09:53:11 -0400131 SkASSERT(SkScalarIsFinite(dt) || (c0 == c1).allTrue());
132 SkASSERT(SkScalarIsFinite(t0) || (c0 == c1).allTrue());
Florin Malitacf20f782017-04-07 14:56:14 -0400133 const Sk4f dc = SkScalarIsFinite(dt) ? (c1 - c0) / dt : 0;
134 const Sk4f bias = c0 - (SkScalarIsFinite(t0) ? t0 * dc : 0);
fmalitabc590c02016-02-22 09:12:33 -0800135
Florin Malitacf20f782017-04-07 14:56:14 -0400136 bias.store(&fCb.fVec);
137 dc.store(&fCg.fVec);
fmalitabc590c02016-02-22 09:12:33 -0800138}
139
Florin Malita0e36b3f2017-06-05 23:33:45 -0400140void Sk4fGradientIntervalBuffer::init(const SkGradientShaderBase& shader, SkColorSpace* dstCS,
Florin Malitada4545b2017-03-23 17:04:54 -0400141 SkShader::TileMode tileMode, bool premulColors,
142 SkScalar alpha, bool reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800143 // The main job here is to build a specialized interval list: a different
144 // representation of the color stops data, optimized for efficient scan line
145 // access during shading.
146 //
147 // [{P0,C0} , {P1,C1}) [{P1,C2} , {P2,c3}) ... [{Pn,C2n} , {Pn+1,C2n+1})
148 //
149 // The list may be inverted when requested (such that e.g. points are sorted
150 // in increasing x order when dx < 0).
151 //
152 // Note: the current representation duplicates pos data; we could refactor to
153 // avoid this if interval storage size becomes a concern.
154 //
155 // Aside from reordering, we also perform two more pre-processing steps at
156 // this stage:
157 //
158 // 1) scale the color components depending on paint alpha and the requested
159 // interpolation space (note: the interval color storage is SkPM4f, but
160 // that doesn't necessarily mean the colors are premultiplied; that
161 // property is tracked in fColorsArePremul)
162 //
163 // 2) inject synthetic intervals to support tiling.
164 //
165 // * for kRepeat, no extra intervals are needed - the iterator just
166 // wraps around at the end:
167 //
168 // ->[P0,P1)->..[Pn-1,Pn)->
169 //
170 // * for kClamp, we add two "infinite" intervals before/after:
171 //
172 // [-/+inf , P0)->[P0 , P1)->..[Pn-1 , Pn)->[Pn , +/-inf)
173 //
174 // (the iterator should never run off the end in this mode)
175 //
176 // * for kMirror, we extend the range to [0..2] and add a flipped
177 // interval series - then the iterator operates just as in the
178 // kRepeat case:
179 //
180 // ->[P0,P1)->..[Pn-1,Pn)->[2 - Pn,2 - Pn-1)->..[2 - P1,2 - P0)->
181 //
182 // TODO: investigate collapsing intervals << 1px.
183
Florin Malita0e36b3f2017-06-05 23:33:45 -0400184 const auto count = shader.fColorCount;
185
Florin Malitada4545b2017-03-23 17:04:54 -0400186 SkASSERT(count > 0);
fmalita7e6fcf82016-03-10 11:18:43 -0800187
Florin Malitada4545b2017-03-23 17:04:54 -0400188 fIntervals.reset();
189
190 const Sk4f componentScale = premulColors
191 ? Sk4f(alpha)
192 : Sk4f(1.0f, 1.0f, 1.0f, alpha);
193 const int first_index = reverse ? count - 1 : 0;
194 const int last_index = count - 1 - first_index;
fmalita7e6fcf82016-03-10 11:18:43 -0800195 const SkScalar first_pos = reverse ? SK_Scalar1 : 0;
196 const SkScalar last_pos = SK_Scalar1 - first_pos;
197
Brian Osman6667fb12018-07-03 16:44:02 -0400198 // Transform all of the colors to destination color space
199 SkColor4fXformer xformedColors(shader.fOrigColors4f, count, shader.fColorSpace.get(), dstCS);
200
Florin Malitada4545b2017-03-23 17:04:54 -0400201 if (tileMode == SkShader::kClamp_TileMode) {
fmalita7e6fcf82016-03-10 11:18:43 -0800202 // synthetic edge interval: -/+inf .. P0
Brian Osman6667fb12018-07-03 16:44:02 -0400203 const Sk4f clamp_color = pack_color(xformedColors.fColors[first_index],
Florin Malitada4545b2017-03-23 17:04:54 -0400204 premulColors, componentScale);
fmalita8f457592016-10-21 06:02:22 -0700205 const SkScalar clamp_pos = reverse ? SK_ScalarInfinity : SK_ScalarNegativeInfinity;
fmalita7e6fcf82016-03-10 11:18:43 -0800206 fIntervals.emplace_back(clamp_color, clamp_pos,
fmalitab9313362016-04-15 13:03:07 -0700207 clamp_color, first_pos);
Florin Malitada4545b2017-03-23 17:04:54 -0400208 } else if (tileMode == SkShader::kMirror_TileMode && reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800209 // synthetic mirror intervals injected before main intervals: (2 .. 1]
Brian Osman6667fb12018-07-03 16:44:02 -0400210 addMirrorIntervals(shader, xformedColors.fColors, componentScale, premulColors, false,
211 &fIntervals);
fmalita7e6fcf82016-03-10 11:18:43 -0800212 }
213
Brian Osman6667fb12018-07-03 16:44:02 -0400214 const IntervalIterator iter(shader, reverse);
215 iter.iterate(xformedColors.fColors,
216 [&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) {
Florin Malitacf20f782017-04-07 14:56:14 -0400217 SkASSERT(fIntervals.empty() || fIntervals.back().fT1 == t0);
fmalita7e6fcf82016-03-10 11:18:43 -0800218
Florin Malitacf20f782017-04-07 14:56:14 -0400219 fIntervals.emplace_back(pack_color(c0, premulColors, componentScale), t0,
220 pack_color(c1, premulColors, componentScale), t1);
fmalita7e6fcf82016-03-10 11:18:43 -0800221 });
222
Florin Malitada4545b2017-03-23 17:04:54 -0400223 if (tileMode == SkShader::kClamp_TileMode) {
fmalita7e6fcf82016-03-10 11:18:43 -0800224 // synthetic edge interval: Pn .. +/-inf
Brian Osman6667fb12018-07-03 16:44:02 -0400225 const Sk4f clamp_color = pack_color(xformedColors.fColors[last_index],
Florin Malita0e36b3f2017-06-05 23:33:45 -0400226 premulColors, componentScale);
fmalita8f457592016-10-21 06:02:22 -0700227 const SkScalar clamp_pos = reverse ? SK_ScalarNegativeInfinity : SK_ScalarInfinity;
fmalita7e6fcf82016-03-10 11:18:43 -0800228 fIntervals.emplace_back(clamp_color, last_pos,
fmalitab9313362016-04-15 13:03:07 -0700229 clamp_color, clamp_pos);
Florin Malitada4545b2017-03-23 17:04:54 -0400230 } else if (tileMode == SkShader::kMirror_TileMode && !reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800231 // synthetic mirror intervals injected after main intervals: [1 .. 2)
Brian Osman6667fb12018-07-03 16:44:02 -0400232 addMirrorIntervals(shader, xformedColors.fColors, componentScale, premulColors, true,
233 &fIntervals);
fmalita7e6fcf82016-03-10 11:18:43 -0800234 }
235}
236
Florin Malitada4545b2017-03-23 17:04:54 -0400237const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::find(SkScalar t) const {
238 // Binary search.
239 const auto* i0 = fIntervals.begin();
240 const auto* i1 = fIntervals.end() - 1;
fmalita7e6fcf82016-03-10 11:18:43 -0800241
Florin Malitada4545b2017-03-23 17:04:54 -0400242 while (i0 != i1) {
243 SkASSERT(i0 < i1);
Florin Malitacf20f782017-04-07 14:56:14 -0400244 SkASSERT(t >= i0->fT0 && t <= i1->fT1);
Florin Malitada4545b2017-03-23 17:04:54 -0400245
246 const auto* i = i0 + ((i1 - i0) >> 1);
247
Florin Malitacf20f782017-04-07 14:56:14 -0400248 if (t > i->fT1) {
Florin Malitada4545b2017-03-23 17:04:54 -0400249 i0 = i + 1;
250 } else {
251 i1 = i;
fmalitacc341762016-11-02 13:10:51 -0700252 }
Florin Malitada4545b2017-03-23 17:04:54 -0400253 }
254
255 SkASSERT(i0->contains(t));
256 return i0;
257}
258
259const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::findNext(
260 SkScalar t, const Sk4fGradientInterval* prev, bool increasing) const {
261
262 SkASSERT(!prev->contains(t));
263 SkASSERT(prev >= fIntervals.begin() && prev < fIntervals.end());
Florin Malitacf20f782017-04-07 14:56:14 -0400264 SkASSERT(t >= fIntervals.front().fT0 && t <= fIntervals.back().fT1);
Florin Malitada4545b2017-03-23 17:04:54 -0400265
266 const auto* i = prev;
267
268 // Use the |increasing| signal to figure which direction we should search for
269 // the next interval, then perform a linear search.
270 if (increasing) {
271 do {
272 i += 1;
273 if (i >= fIntervals.end()) {
274 i = fIntervals.begin();
275 }
276 } while (!i->contains(t));
277 } else {
278 do {
279 i -= 1;
280 if (i < fIntervals.begin()) {
281 i = fIntervals.end() - 1;
282 }
283 } while (!i->contains(t));
284 }
285
286 return i;
287}
288
289SkGradientShaderBase::
290GradientShaderBase4fContext::GradientShaderBase4fContext(const SkGradientShaderBase& shader,
291 const ContextRec& rec)
292 : INHERITED(shader, rec)
293 , fFlags(this->INHERITED::getFlags())
Florin Malitaaa0ce822017-08-28 12:50:26 -0400294 , fDither(rec.fPaint->isDither())
Florin Malitada4545b2017-03-23 17:04:54 -0400295{
296 const SkMatrix& inverse = this->getTotalInverse();
297 fDstToPos.setConcat(shader.fPtsToUnit, inverse);
Florin Malita4d41b8f2017-07-12 14:35:46 -0400298 SkASSERT(!fDstToPos.hasPerspective());
Cary Clark9480d822017-10-19 18:01:13 -0400299 fDstToPosProc = SkMatrixPriv::GetMapXYProc(fDstToPos);
Florin Malitada4545b2017-03-23 17:04:54 -0400300
301 if (shader.fColorsAreOpaque && this->getPaintAlpha() == SK_AlphaOPAQUE) {
302 fFlags |= kOpaqueAlpha_Flag;
303 }
304
305 fColorsArePremul =
306 (shader.fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag)
307 || shader.fColorsAreOpaque;
308}
309
310bool SkGradientShaderBase::
311GradientShaderBase4fContext::isValid() const {
312 return fDstToPos.isFinite();
fmalita7e6fcf82016-03-10 11:18:43 -0800313}