fix hairline clip

Don't remove inner clip if it's non-rectangular.
Add cubic hairline clip code to quad and conic cases.

R=reed@google.com
BUG=skia:5252
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1943403006

Review-Url: https://codereview.chromium.org/1943403006
diff --git a/gm/bug5252.cpp b/gm/bug5252.cpp
new file mode 100644
index 0000000..981aab5
--- /dev/null
+++ b/gm/bug5252.cpp
@@ -0,0 +1,43 @@
+/*
+* Copyright 2016 Google Inc.
+*
+* Use of this source code is governed by a BSD-style license that can be
+* found in the LICENSE file.
+*/
+
+#include "gm.h"
+#include "SkCanvas.h"
+#include "SkRect.h"
+#include "SkPath.h"
+
+DEF_SIMPLE_GM(bug5252, canvas, 500, 500) {
+	canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
+
+	SkPath clip1;
+	clip1.addOval(SkRect::MakeWH(225, 200));
+	canvas->clipPath(clip1); // bug
+
+	SkPath clip2;
+	clip2.addRect(SkRect::MakeWH(220, 200));
+	//canvas->clipPath(clip2); // ok
+
+	SkPaint pa;
+	pa.setStyle(SkPaint::kStroke_Style);
+	pa.setAntiAlias(true);
+	pa.setStrokeWidth(1.0f);
+	for (int i = 0; i < 15; i++)
+	{
+		for (int j = 0; j < 10; j++)
+		{
+			SkAutoCanvasRestore acs(canvas, true);
+
+			canvas->translate(i * 15.f, j * 20.f);
+			canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 15),pa);
+			SkPath path;
+			path.moveTo(6, 6);
+			path.cubicTo(14, 10, 13, 12, 10, 12);
+			path.cubicTo(7, 15, 8, 17, 14, 18);
+			canvas->drawPath(path, pa);
+		}
+	}
+}
diff --git a/src/core/SkScan_Hairline.cpp b/src/core/SkScan_Hairline.cpp
index 9b21c42..a023ca1 100644
--- a/src/core/SkScan_Hairline.cpp
+++ b/src/core/SkScan_Hairline.cpp
@@ -214,7 +214,7 @@
     }
 }
 
-static void hairquad(const SkPoint pts[3], const SkRegion* clip,
+static void hair_quad(const SkPoint pts[3], const SkRegion* clip,
                      SkBlitter* blitter, int level, SkScan::HairRgnProc lineproc) {
     SkASSERT(level <= kMaxQuadSubdivideLevel);
 
@@ -239,6 +239,54 @@
     lineproc(tmp, lines + 1, clip, blitter);
 }
 
+static SkRect compute_nocheck_quad_bounds(const SkPoint pts[3]) {
+    SkASSERT(SkScalarsAreFinite(&pts[0].fX, 6));
+
+    Sk2s min = Sk2s::Load(pts);
+    Sk2s max = min;
+    for (int i = 1; i < 3; ++i) {
+        Sk2s pair = Sk2s::Load(pts+i);
+        min = Sk2s::Min(min, pair);
+        max = Sk2s::Max(max, pair);
+    }
+    return { min[0], min[1], max[0], max[1] };
+}
+
+static bool is_inverted(const SkRect& r) {
+    return r.fLeft > r.fRight || r.fTop > r.fBottom;
+}
+
+// Can't call SkRect::intersects, since it cares about empty, and we don't (since we tracking
+// something to be stroked, so empty can still draw something (e.g. horizontal line)
+static bool geometric_overlap(const SkRect& a, const SkRect& b) {
+    SkASSERT(!is_inverted(a) && !is_inverted(b));
+    return a.fLeft < b.fRight && b.fLeft < a.fRight &&
+            a.fTop < b.fBottom && b.fTop < a.fBottom;
+}
+
+// Can't call SkRect::contains, since it cares about empty, and we don't (since we tracking
+// something to be stroked, so empty can still draw something (e.g. horizontal line)
+static bool geometric_contains(const SkRect& outer, const SkRect& inner) {
+    SkASSERT(!is_inverted(outer) && !is_inverted(inner));
+    return inner.fRight <= outer.fRight && inner.fLeft >= outer.fLeft &&
+            inner.fBottom <= outer.fBottom && inner.fTop >= outer.fTop;
+}
+
+static inline void hairquad(const SkPoint pts[3], const SkRegion* clip, const SkRect* insetClip, const SkRect* outsetClip,
+    SkBlitter* blitter, int level, SkScan::HairRgnProc lineproc) {
+    if (insetClip) {
+        SkASSERT(outsetClip);
+        SkRect bounds = compute_nocheck_quad_bounds(pts);
+        if (!geometric_overlap(*outsetClip, bounds)) {
+            return;
+        } else if (geometric_contains(*insetClip, bounds)) {
+            clip = nullptr;
+        }
+    }
+
+    hair_quad(pts, clip, blitter, level, lineproc);
+}
+
 static inline Sk2s abs(const Sk2s& value) {
     return Sk2s::Max(value, Sk2s(0)-value);
 }
@@ -329,54 +377,16 @@
     return { min[0], min[1], max[0], max[1] };
 }
 
-static bool is_inverted(const SkRect& r) {
-    return r.fLeft > r.fRight || r.fTop > r.fBottom;
-}
-
-// Can't call SkRect::intersects, since it cares about empty, and we don't (since we tracking
-// something to be stroked, so empty can still draw something (e.g. horizontal line)
-static bool geometric_overlap(const SkRect& a, const SkRect& b) {
-    SkASSERT(!is_inverted(a) && !is_inverted(b));
-    return a.fLeft < b.fRight && b.fLeft < a.fRight &&
-           a.fTop < b.fBottom && b.fTop < a.fBottom;
-}
-
-// Can't call SkRect::contains, since it cares about empty, and we don't (since we tracking
-// something to be stroked, so empty can still draw something (e.g. horizontal line)
-static bool geometric_contains(const SkRect& outer, const SkRect& inner) {
-    SkASSERT(!is_inverted(outer) && !is_inverted(inner));
-    return inner.fRight <= outer.fRight && inner.fLeft >= outer.fLeft &&
-           inner.fBottom <= outer.fBottom && inner.fTop >= outer.fTop;
-}
-
-//#define SK_SHOW_HAIRCLIP_STATS
-#ifdef SK_SHOW_HAIRCLIP_STATS
-static int gKillClip, gRejectClip, gClipCount;
-#endif
-
 static inline void haircubic(const SkPoint pts[4], const SkRegion* clip, const SkRect* insetClip, const SkRect* outsetClip,
                       SkBlitter* blitter, int level, SkScan::HairRgnProc lineproc) {
     if (insetClip) {
         SkASSERT(outsetClip);
-#ifdef SK_SHOW_HAIRCLIP_STATS
-        gClipCount += 1;
-#endif
         SkRect bounds = compute_nocheck_cubic_bounds(pts);
         if (!geometric_overlap(*outsetClip, bounds)) {
-#ifdef SK_SHOW_HAIRCLIP_STATS
-            gRejectClip += 1;
-#endif
             return;
         } else if (geometric_contains(*insetClip, bounds)) {
             clip = nullptr;
-#ifdef SK_SHOW_HAIRCLIP_STATS
-            gKillClip += 1;
-#endif
         }
-#ifdef SK_SHOW_HAIRCLIP_STATS
-        if (0 == gClipCount % 256)
-            SkDebugf("kill %g reject %g total %d\n", 1.0*gKillClip / gClipCount, 1.0*gRejectClip/gClipCount, gClipCount);
-#endif
     }
 
     if (quick_cubic_niceness_check(pts)) {
@@ -512,7 +522,9 @@
                  */
                 insetStorage.setEmpty();    // just so we don't pass an inverted rect
             }
-            insetClip = &insetStorage;
+            if (rclip.isRect()) {
+                insetClip = &insetStorage;
+            }
             outsetClip = &outsetStorage;
         }
     }
@@ -541,7 +553,7 @@
                 if (SkPaint::kButt_Cap != capStyle) {
                     extend_pts<capStyle>(prevVerb, iter.peek(), pts, 3);
                 }
-                hairquad(pts, clip, blitter, compute_quad_level(pts), lineproc);
+                hairquad(pts, clip, insetClip, outsetClip, blitter, compute_quad_level(pts), lineproc);
                 lastPt = pts[2];
                 break;
             case SkPath::kConic_Verb: {
@@ -554,7 +566,7 @@
                                                        iter.conicWeight(), tol);
                 for (int i = 0; i < converter.countQuads(); ++i) {
                     int level = compute_quad_level(quadPts);
-                    hairquad(quadPts, clip, blitter, level, lineproc);
+                    hairquad(quadPts, clip, insetClip, outsetClip, blitter, level, lineproc);
                     quadPts += 2;
                 }
                 lastPt = pts[2];