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>
diff --git a/src/shaders/gradients/Sk4fGradientBase.cpp b/src/shaders/gradients/Sk4fGradientBase.cpp
index 42f30c4..ee701bd 100644
--- a/src/shaders/gradients/Sk4fGradientBase.cpp
+++ b/src/shaders/gradients/Sk4fGradientBase.cpp
@@ -38,7 +38,6 @@
         }
 
         const int end = fBegin + fAdvance * (fShader.fColorCount - 1);
-        const SkScalar lastPos = 1 - fFirstPos;
         int prev = fBegin;
         SkScalar prevPos = fFirstPos;
 
@@ -46,11 +45,7 @@
             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 03535ff..d2a3529 100644
--- a/src/shaders/gradients/SkGradientShader.cpp
+++ b/src/shaders/gradients/SkGradientShader.cpp
@@ -179,14 +179,16 @@
     }
 
     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 @@
             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;