blob: 0f6da52d1a2f4041afa416e153cdcb39e4171992 [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) {
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:
Brian Osman6667fb12018-07-03 16:44:02 -040024 IntervalIterator(const SkGradientShaderBase& shader, bool reverse)
Florin Malita0e36b3f2017-06-05 23:33:45 -040025 : fShader(shader)
fmalita7e6fcf82016-03-10 11:18:43 -080026 , fFirstPos(reverse ? SK_Scalar1 : 0)
Florin Malita0e36b3f2017-06-05 23:33:45 -040027 , fBegin(reverse ? shader.fColorCount - 1 : 0)
fmalita7e6fcf82016-03-10 11:18:43 -080028 , fAdvance(reverse ? -1 : 1) {
Florin Malita0e36b3f2017-06-05 23:33:45 -040029 SkASSERT(shader.fColorCount > 0);
fmalita7e6fcf82016-03-10 11:18:43 -080030 }
31
Brian Osman6667fb12018-07-03 16:44:02 -040032 void iterate(const SkColor4f* colors,
33 std::function<void(const SkColor4f&, const SkColor4f&,
Florin Malita0e36b3f2017-06-05 23:33:45 -040034 SkScalar, SkScalar)> func) const {
35 if (!fShader.fOrigPos) {
Brian Osman6667fb12018-07-03 16:44:02 -040036 this->iterateImplicitPos(colors, func);
fmalita7e6fcf82016-03-10 11:18:43 -080037 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));
Brian Osman6667fb12018-07-03 16:44:02 -040051 func(colors[prev], colors[curr], prevPos, currPos);
fmalita7e6fcf82016-03-10 11:18:43 -080052 }
53
54 prev = curr;
55 prevPos = currPos;
56 } while (prev != end);
57 }
58
59private:
Brian Osman6667fb12018-07-03 16:44:02 -040060 void iterateImplicitPos(const SkColor4f* colors,
61 std::function<void(const SkColor4f&, const SkColor4f&,
Florin Malita0e36b3f2017-06-05 23:33:45 -040062 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;
Brian Osman6667fb12018-07-03 16:44:02 -040076 func(colors[prev], colors[curr], prevPos, currPos);
fmalita7e6fcf82016-03-10 11:18:43 -080077 prev = curr;
78 prevPos = currPos;
79 }
80
81 // emit the last interval with a pinned end position, to avoid precision issues
Brian Osman6667fb12018-07-03 16:44:02 -040082 func(colors[prev], colors[prev + fAdvance], prevPos, 1 - fFirstPos);
fmalita7e6fcf82016-03-10 11:18:43 -080083 }
84
Florin Malita0e36b3f2017-06-05 23:33:45 -040085 const SkGradientShaderBase& fShader;
Florin Malita0e36b3f2017-06-05 23:33:45 -040086 const SkScalar fFirstPos;
87 const int fBegin;
88 const int fAdvance;
fmalita7e6fcf82016-03-10 11:18:43 -080089};
90
Florin Malita0e36b3f2017-06-05 23:33:45 -040091void addMirrorIntervals(const SkGradientShaderBase& shader,
Brian Osman6667fb12018-07-03 16:44:02 -040092 const SkColor4f* colors,
Florin Malitada4545b2017-03-23 17:04:54 -040093 const Sk4f& componentScale,
94 bool premulColors, bool reverse,
95 Sk4fGradientIntervalBuffer::BufferType* buffer) {
Brian Osman6667fb12018-07-03 16:44:02 -040096 const IntervalIterator iter(shader, reverse);
97 iter.iterate(colors, [&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) {
Florin Malitacf20f782017-04-07 14:56:14 -040098 SkASSERT(buffer->empty() || buffer->back().fT1 == 2 - t0);
Florin Malitada4545b2017-03-23 17:04:54 -040099
Florin Malitacf20f782017-04-07 14:56:14 -0400100 const auto mirror_t0 = 2 - t0;
101 const auto mirror_t1 = 2 - t1;
Florin Malitada4545b2017-03-23 17:04:54 -0400102 // mirror_p1 & mirror_p1 may collapse for very small values - recheck to avoid
103 // triggering Interval asserts.
Florin Malitacf20f782017-04-07 14:56:14 -0400104 if (mirror_t0 != mirror_t1) {
105 buffer->emplace_back(pack_color(c0, premulColors, componentScale), mirror_t0,
106 pack_color(c1, premulColors, componentScale), mirror_t1);
Florin Malitada4545b2017-03-23 17:04:54 -0400107 }
108 });
109}
110
fmalitabc590c02016-02-22 09:12:33 -0800111} // anonymous namespace
112
Florin Malitacf20f782017-04-07 14:56:14 -0400113Sk4fGradientInterval::Sk4fGradientInterval(const Sk4f& c0, SkScalar t0,
114 const Sk4f& c1, SkScalar t1)
115 : fT0(t0)
Florin Malita63f717d2017-05-08 09:53:11 -0400116 , fT1(t1) {
Florin Malitacf20f782017-04-07 14:56:14 -0400117 SkASSERT(t0 != t1);
fmalita8f457592016-10-21 06:02:22 -0700118 // Either p0 or p1 can be (-)inf for synthetic clamp edge intervals.
Florin Malitacf20f782017-04-07 14:56:14 -0400119 SkASSERT(SkScalarIsFinite(t0) || SkScalarIsFinite(t1));
fmalita8f457592016-10-21 06:02:22 -0700120
Florin Malitacf20f782017-04-07 14:56:14 -0400121 const auto dt = t1 - t0;
fmalita8f457592016-10-21 06:02:22 -0700122
123 // Clamp edge intervals are always zero-ramp.
Florin Malita63f717d2017-05-08 09:53:11 -0400124 SkASSERT(SkScalarIsFinite(dt) || (c0 == c1).allTrue());
125 SkASSERT(SkScalarIsFinite(t0) || (c0 == c1).allTrue());
Florin Malitacf20f782017-04-07 14:56:14 -0400126 const Sk4f dc = SkScalarIsFinite(dt) ? (c1 - c0) / dt : 0;
127 const Sk4f bias = c0 - (SkScalarIsFinite(t0) ? t0 * dc : 0);
fmalitabc590c02016-02-22 09:12:33 -0800128
Florin Malitacf20f782017-04-07 14:56:14 -0400129 bias.store(&fCb.fVec);
130 dc.store(&fCg.fVec);
fmalitabc590c02016-02-22 09:12:33 -0800131}
132
Florin Malita0e36b3f2017-06-05 23:33:45 -0400133void Sk4fGradientIntervalBuffer::init(const SkGradientShaderBase& shader, SkColorSpace* dstCS,
Florin Malitada4545b2017-03-23 17:04:54 -0400134 SkShader::TileMode tileMode, bool premulColors,
135 SkScalar alpha, bool reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800136 // The main job here is to build a specialized interval list: a different
137 // representation of the color stops data, optimized for efficient scan line
138 // access during shading.
139 //
140 // [{P0,C0} , {P1,C1}) [{P1,C2} , {P2,c3}) ... [{Pn,C2n} , {Pn+1,C2n+1})
141 //
142 // The list may be inverted when requested (such that e.g. points are sorted
143 // in increasing x order when dx < 0).
144 //
145 // Note: the current representation duplicates pos data; we could refactor to
146 // avoid this if interval storage size becomes a concern.
147 //
148 // Aside from reordering, we also perform two more pre-processing steps at
149 // this stage:
150 //
151 // 1) scale the color components depending on paint alpha and the requested
152 // interpolation space (note: the interval color storage is SkPM4f, but
153 // that doesn't necessarily mean the colors are premultiplied; that
154 // property is tracked in fColorsArePremul)
155 //
156 // 2) inject synthetic intervals to support tiling.
157 //
158 // * for kRepeat, no extra intervals are needed - the iterator just
159 // wraps around at the end:
160 //
161 // ->[P0,P1)->..[Pn-1,Pn)->
162 //
163 // * for kClamp, we add two "infinite" intervals before/after:
164 //
165 // [-/+inf , P0)->[P0 , P1)->..[Pn-1 , Pn)->[Pn , +/-inf)
166 //
167 // (the iterator should never run off the end in this mode)
168 //
169 // * for kMirror, we extend the range to [0..2] and add a flipped
170 // interval series - then the iterator operates just as in the
171 // kRepeat case:
172 //
173 // ->[P0,P1)->..[Pn-1,Pn)->[2 - Pn,2 - Pn-1)->..[2 - P1,2 - P0)->
174 //
175 // TODO: investigate collapsing intervals << 1px.
176
Florin Malita0e36b3f2017-06-05 23:33:45 -0400177 const auto count = shader.fColorCount;
178
Florin Malitada4545b2017-03-23 17:04:54 -0400179 SkASSERT(count > 0);
fmalita7e6fcf82016-03-10 11:18:43 -0800180
Florin Malitada4545b2017-03-23 17:04:54 -0400181 fIntervals.reset();
182
183 const Sk4f componentScale = premulColors
184 ? Sk4f(alpha)
185 : Sk4f(1.0f, 1.0f, 1.0f, alpha);
186 const int first_index = reverse ? count - 1 : 0;
187 const int last_index = count - 1 - first_index;
fmalita7e6fcf82016-03-10 11:18:43 -0800188 const SkScalar first_pos = reverse ? SK_Scalar1 : 0;
189 const SkScalar last_pos = SK_Scalar1 - first_pos;
190
Brian Osman6667fb12018-07-03 16:44:02 -0400191 // Transform all of the colors to destination color space
192 SkColor4fXformer xformedColors(shader.fOrigColors4f, count, shader.fColorSpace.get(), dstCS);
193
Florin Malitada4545b2017-03-23 17:04:54 -0400194 if (tileMode == SkShader::kClamp_TileMode) {
fmalita7e6fcf82016-03-10 11:18:43 -0800195 // synthetic edge interval: -/+inf .. P0
Brian Osman6667fb12018-07-03 16:44:02 -0400196 const Sk4f clamp_color = pack_color(xformedColors.fColors[first_index],
Florin Malitada4545b2017-03-23 17:04:54 -0400197 premulColors, componentScale);
fmalita8f457592016-10-21 06:02:22 -0700198 const SkScalar clamp_pos = reverse ? SK_ScalarInfinity : SK_ScalarNegativeInfinity;
fmalita7e6fcf82016-03-10 11:18:43 -0800199 fIntervals.emplace_back(clamp_color, clamp_pos,
fmalitab9313362016-04-15 13:03:07 -0700200 clamp_color, first_pos);
Florin Malitada4545b2017-03-23 17:04:54 -0400201 } else if (tileMode == SkShader::kMirror_TileMode && reverse) {
fmalita7e6fcf82016-03-10 11:18:43 -0800202 // synthetic mirror intervals injected before main intervals: (2 .. 1]
Brian Osman6667fb12018-07-03 16:44:02 -0400203 addMirrorIntervals(shader, xformedColors.fColors, componentScale, premulColors, false,
204 &fIntervals);
fmalita7e6fcf82016-03-10 11:18:43 -0800205 }
206
Brian Osman6667fb12018-07-03 16:44:02 -0400207 const IntervalIterator iter(shader, reverse);
208 iter.iterate(xformedColors.fColors,
209 [&] (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
Brian Osman6667fb12018-07-03 16:44:02 -0400218 const Sk4f clamp_color = pack_color(xformedColors.fColors[last_index],
Florin Malita0e36b3f2017-06-05 23:33:45 -0400219 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)
Brian Osman6667fb12018-07-03 16:44:02 -0400225 addMirrorIntervals(shader, xformedColors.fColors, componentScale, premulColors, true,
226 &fIntervals);
fmalita7e6fcf82016-03-10 11:18:43 -0800227 }
228}
229
Florin Malitada4545b2017-03-23 17:04:54 -0400230const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::find(SkScalar t) const {
231 // Binary search.
232 const auto* i0 = fIntervals.begin();
233 const auto* i1 = fIntervals.end() - 1;
fmalita7e6fcf82016-03-10 11:18:43 -0800234
Florin Malitada4545b2017-03-23 17:04:54 -0400235 while (i0 != i1) {
236 SkASSERT(i0 < i1);
Florin Malitacf20f782017-04-07 14:56:14 -0400237 SkASSERT(t >= i0->fT0 && t <= i1->fT1);
Florin Malitada4545b2017-03-23 17:04:54 -0400238
239 const auto* i = i0 + ((i1 - i0) >> 1);
240
Florin Malitacf20f782017-04-07 14:56:14 -0400241 if (t > i->fT1) {
Florin Malitada4545b2017-03-23 17:04:54 -0400242 i0 = i + 1;
243 } else {
244 i1 = i;
fmalitacc341762016-11-02 13:10:51 -0700245 }
Florin Malitada4545b2017-03-23 17:04:54 -0400246 }
247
248 SkASSERT(i0->contains(t));
249 return i0;
250}
251
252const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::findNext(
253 SkScalar t, const Sk4fGradientInterval* prev, bool increasing) const {
254
255 SkASSERT(!prev->contains(t));
256 SkASSERT(prev >= fIntervals.begin() && prev < fIntervals.end());
Florin Malitacf20f782017-04-07 14:56:14 -0400257 SkASSERT(t >= fIntervals.front().fT0 && t <= fIntervals.back().fT1);
Florin Malitada4545b2017-03-23 17:04:54 -0400258
259 const auto* i = prev;
260
261 // Use the |increasing| signal to figure which direction we should search for
262 // the next interval, then perform a linear search.
263 if (increasing) {
264 do {
265 i += 1;
266 if (i >= fIntervals.end()) {
267 i = fIntervals.begin();
268 }
269 } while (!i->contains(t));
270 } else {
271 do {
272 i -= 1;
273 if (i < fIntervals.begin()) {
274 i = fIntervals.end() - 1;
275 }
276 } while (!i->contains(t));
277 }
278
279 return i;
280}
281
282SkGradientShaderBase::
283GradientShaderBase4fContext::GradientShaderBase4fContext(const SkGradientShaderBase& shader,
284 const ContextRec& rec)
285 : INHERITED(shader, rec)
286 , fFlags(this->INHERITED::getFlags())
Florin Malitaaa0ce822017-08-28 12:50:26 -0400287 , fDither(rec.fPaint->isDither())
Florin Malitada4545b2017-03-23 17:04:54 -0400288{
289 const SkMatrix& inverse = this->getTotalInverse();
290 fDstToPos.setConcat(shader.fPtsToUnit, inverse);
Florin Malita4d41b8f2017-07-12 14:35:46 -0400291 SkASSERT(!fDstToPos.hasPerspective());
Cary Clark9480d822017-10-19 18:01:13 -0400292 fDstToPosProc = SkMatrixPriv::GetMapXYProc(fDstToPos);
Florin Malitada4545b2017-03-23 17:04:54 -0400293
294 if (shader.fColorsAreOpaque && this->getPaintAlpha() == SK_AlphaOPAQUE) {
295 fFlags |= kOpaqueAlpha_Flag;
296 }
297
298 fColorsArePremul =
299 (shader.fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag)
300 || shader.fColorsAreOpaque;
301}
302
303bool SkGradientShaderBase::
304GradientShaderBase4fContext::isValid() const {
305 return fDstToPos.isFinite();
fmalita7e6fcf82016-03-10 11:18:43 -0800306}