Enforce gradient color stop monotonicity in ctor

From the SVG 1.1 spec:
Each gradient offset value is required to be equal to or greater than the previous 
gradient stop's offset value. If a given gradient stop's offset value is not
equal to or greater than all previous offset values, then the offset
value is adjusted to be equal to the largest of all previous offset values.

Change-Id: I797369a1e14dc776ceb6478ac9fcdd4792e65562
Reviewed-on: https://skia-review.googlesource.com/67761
Commit-Queue: Florin Malita <fmalita@chromium.org>
Reviewed-by: Herb Derby <herb@google.com>
Reviewed-by: Mike Klein <mtklein@chromium.org>
This commit is contained in:
Florin Malita 2017-11-03 12:11:38 -04:00 committed by Skia Commit-Bot
parent c2d18f34de
commit 3e20d023bf
2 changed files with 7 additions and 9 deletions

View File

@ -38,7 +38,6 @@ public:
}
const int end = fBegin + fAdvance * (fShader.fColorCount - 1);
const SkScalar lastPos = 1 - fFirstPos;
int prev = fBegin;
SkScalar prevPos = fFirstPos;
@ -46,11 +45,7 @@ public:
const int curr = prev + fAdvance;
SkASSERT(curr >= 0 && curr < fShader.fColorCount);
// TODO: this sanitization should be done in SkGradientShaderBase
const SkScalar currPos = (fAdvance > 0)
? SkTPin(fShader.fOrigPos[curr], prevPos, lastPos)
: SkTPin(fShader.fOrigPos[curr], lastPos, prevPos);
const SkScalar currPos = fShader.fOrigPos[curr];
if (currPos != prevPos) {
SkASSERT((currPos - prevPos > 0) == (fAdvance > 0));
func(fShader.getXformedColor(prev, fDstCS), fShader.getXformedColor(curr, fDstCS),

View File

@ -179,14 +179,16 @@ SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatri
}
if (desc.fPos) {
SkScalar pos = 0;
SkScalar* origPosPtr = fOrigPos;
*origPosPtr++ = 0; // force the first pos to 0
*origPosPtr++ = pos; // force the first pos to 0
int startIndex = dummyFirst ? 0 : 1;
int count = desc.fCount + dummyLast;
for (int i = startIndex; i < count; i++) {
// force the last value to be 1.0
*origPosPtr++ = (i == desc.fCount) ? 1 : SkScalarPin(desc.fPos[i], 0, 1);
// Pin the last value to 1.0, and make sure pos is monotonic.
pos = (i == desc.fCount) ? 1 : SkScalarPin(desc.fPos[i], pos, 1);
*origPosPtr++ = pos;
}
}
}
@ -364,6 +366,7 @@ bool SkGradientShaderBase::onAppendStages(const StageRec& rec) const {
for (int i = firstStop; i < lastStop; i++) {
float t_r = fOrigPos[i + 1];
SkPM4f c_r = prepareColor(i + 1);
SkASSERT(t_l <= t_r);
if (t_l < t_r) {
init_stop_pos(ctx, stopCount, t_l, t_r, c_l, c_r);
stopCount += 1;