From 3e20d023bfa6e534ee0abacdba225dd140ae471c Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Fri, 3 Nov 2017 12:11:38 -0400 Subject: [PATCH] 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 Reviewed-by: Herb Derby Reviewed-by: Mike Klein --- src/shaders/gradients/Sk4fGradientBase.cpp | 7 +------ src/shaders/gradients/SkGradientShader.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/shaders/gradients/Sk4fGradientBase.cpp b/src/shaders/gradients/Sk4fGradientBase.cpp index 42f30c4a5c..ee701bd57b 100644 --- a/src/shaders/gradients/Sk4fGradientBase.cpp +++ b/src/shaders/gradients/Sk4fGradientBase.cpp @@ -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), diff --git a/src/shaders/gradients/SkGradientShader.cpp b/src/shaders/gradients/SkGradientShader.cpp index 03535ffd98..d2a3529132 100644 --- a/src/shaders/gradients/SkGradientShader.cpp +++ b/src/shaders/gradients/SkGradientShader.cpp @@ -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;