Fold analytic clip FPs into GrReducedClip

Perf result on Pixel phone (sorted by impact):

  GEOMEAN                              7.44 -> 6.92 ms   [ 93%]

  keymobi_cnn_com.skp                  3.55 -> 3.59 ms   [101%]
  keymobi_theverge_com.skp             4.08 -> 4.13 ms   [101%]
  desk_skbug6850autoscroll.skp         1.21 -> 1.22 ms   [101%]
  ...
  top25desk_weather_com.skp            14.2 -> 11.5 ms   [ 81%]
  keymobi_androidpolice_com_2012_.skp  3.84 -> 2.95 ms   [ 77%]
  keymobi_shop_mobileweb_ebay_com.skp  2.94 -> 2.26 ms   [ 77%]
  keymobi_boingboing_net.skp           3.08 -> 2.24 ms   [ 73%]
  desk_jsfiddlebigcar.skp              1.90 -> 1.25 ms   [ 66%]
  keymobi_m_youtube_com_watch_v_9.skp  13.5 -> 8.84 ms   [ 65%]
  keymobi_sfgate_com_.skp              8.55 -> 5.55 ms   [ 65%]
  keymobi_blogger.skp                  4.01 -> 2.60 ms   [ 65%]

Cleaner code, improved skps, slightly better geometric mean time.

Pixel C is mostly unaffected, presumably because it uses window
rectangles.

Bug: skia:7190
Change-Id: Ia93f68b2f971ea66b3ab19dc73557ea602932a92
Reviewed-on: https://skia-review.googlesource.com/67424
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrReducedClip.cpp b/src/gpu/GrReducedClip.cpp
index d37c83f..992411a 100644
--- a/src/gpu/GrReducedClip.cpp
+++ b/src/gpu/GrReducedClip.cpp
@@ -21,6 +21,8 @@
 #include "GrStyle.h"
 #include "GrUserStencilSettings.h"
 #include "SkClipOpPriv.h"
+#include "effects/GrConvexPolyEffect.h"
+#include "effects/GrRRectEffect.h"
 
 /**
  * There are plenty of optimizations that could be added here. Maybe flips could be folded into
@@ -30,8 +32,11 @@
  * take a rect in case the caller knows a bound on what is to be drawn through this clip.
  */
 GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
-                             int maxWindowRectangles) {
+                             int maxWindowRectangles, int maxAnalyticFPs)
+        : fMaxWindowRectangles(maxWindowRectangles)
+        , fMaxAnalyticFPs(maxAnalyticFPs) {
     SkASSERT(!queryBounds.isEmpty());
+    SkASSERT(fMaxWindowRectangles <= GrWindowRectangles::kMaxWindows);
     fHasScissor = false;
     fAAClipRectGenID = SK_InvalidGenID;
 
@@ -97,12 +102,13 @@
         }
         fHasScissor = true;
 
-        // Now that we have determined the bounds to use and filtered out the trivial cases, call the
-        // helper that actually walks the stack.
-        this->walkStack(stack, tighterQuery, maxWindowRectangles);
+        // Now that we have determined the bounds to use and filtered out the trivial cases, call
+        // the helper that actually walks the stack.
+        this->walkStack(stack, tighterQuery);
     }
 
-    if (SK_InvalidGenID != fAAClipRectGenID) { // Is there an AA clip rect?
+    if (SK_InvalidGenID != fAAClipRectGenID && // Is there an AA clip rect?
+        ClipResult::kNotClipped == this->addAnalyticFP(fAAClipRect, Invert::kNo, true)) {
         if (fMaskElements.isEmpty()) {
             // Use a replace since it is faster than intersect.
             fMaskElements.addToHead(fAAClipRect, SkMatrix::I(), kReplace_SkClipOp, true /*doAA*/);
@@ -112,12 +118,10 @@
         }
         fMaskRequiresAA = true;
         fMaskGenID = fAAClipRectGenID;
-        fAAClipRectGenID = SK_InvalidGenID;
     }
 }
 
-void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
-                              int maxWindowRectangles) {
+void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds) {
     // walk backwards until we get to:
     //  a) the beginning
     //  b) an operation that is known to make the bounds all inside/outside
@@ -180,7 +184,7 @@
                     } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
                         skippable = true;
                     } else if (!embiggens) {
-                        ClipResult result = this->clipOutsideElement(element, maxWindowRectangles);
+                        ClipResult result = this->clipOutsideElement(element);
                         if (ClipResult::kMadeEmpty == result) {
                             return;
                         }
@@ -480,34 +484,43 @@
             return ClipResult::kClipped;
 
         case Element::DeviceSpaceType::kRRect:
+            return this->addAnalyticFP(element->getDeviceSpaceRRect(), Invert::kNo,
+                                       element->isAA());
+
         case Element::DeviceSpaceType::kPath:
-            return ClipResult::kNotClipped;
+            return this->addAnalyticFP(element->getDeviceSpacePath(), Invert::kNo, element->isAA());
     }
 
     SK_ABORT("Unexpected DeviceSpaceType");
     return ClipResult::kNotClipped;
 }
 
-GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element,
-                                                            int maxWindowRectangles) {
-    if (fWindowRects.count() >= maxWindowRectangles) {
-        return ClipResult::kNotClipped;
-    }
-
+GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element) {
     switch (element->getDeviceSpaceType()) {
         case Element::DeviceSpaceType::kEmpty:
             return ClipResult::kMadeEmpty;
 
         case Element::DeviceSpaceType::kRect:
-            // Clip out the inside of every rect. We won't be able to entirely skip the AA ones, but
-            // it saves processing time.
-            this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
-            return !element->isAA() ? ClipResult::kClipped : ClipResult::kNotClipped;
+            if (fWindowRects.count() < fMaxWindowRectangles) {
+                // Clip out the inside of every rect. We won't be able to entirely skip the AA ones,
+                // but it saves processing time.
+                this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
+                if (!element->isAA()) {
+                    return ClipResult::kClipped;
+                }
+            }
+            return this->addAnalyticFP(element->getDeviceSpaceRect(), Invert::kYes,
+                                       element->isAA());
 
         case Element::DeviceSpaceType::kRRect: {
-            // Clip out the interiors of round rects with two window rectangles in the shape of a
-            // plus. It doesn't allow us to skip the clip element, but still saves processing time.
             const SkRRect& clipRRect = element->getDeviceSpaceRRect();
+            ClipResult clipResult = this->addAnalyticFP(clipRRect, Invert::kYes, element->isAA());
+            if (fWindowRects.count() >= fMaxWindowRectangles) {
+                return clipResult;
+            }
+
+            // Clip out the interiors of round rects with two window rectangles in the shape of a
+            // "plus". This doesn't let us skip the clip element, but still saves processing time.
             SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
             SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
             if (SkRRect::kComplex_Type == clipRRect.getType()) {
@@ -521,24 +534,25 @@
             const SkRect& bounds = clipRRect.getBounds();
             if (insetTL.x() + insetBR.x() >= bounds.width() ||
                 insetTL.y() + insetBR.y() >= bounds.height()) {
-                return ClipResult::kNotClipped; // The interior "plus" is empty.
+                return clipResult; // The interior "plus" is empty.
             }
 
             SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
                                                bounds.right(), bounds.bottom() - insetBR.y());
             this->addWindowRectangle(horzRect, element->isAA());
-            if (fWindowRects.count() >= maxWindowRectangles) {
-                return ClipResult::kNotClipped;
+
+            if (fWindowRects.count() < fMaxWindowRectangles) {
+                SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
+                                                   bounds.right() - insetBR.x(), bounds.bottom());
+                this->addWindowRectangle(vertRect, element->isAA());
             }
 
-            SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
-                                               bounds.right() - insetBR.x(), bounds.bottom());
-            this->addWindowRectangle(vertRect, element->isAA());
-            return ClipResult::kNotClipped;
+            return clipResult;
         }
 
         case Element::DeviceSpaceType::kPath:
-            return ClipResult::kNotClipped;
+            return this->addAnalyticFP(element->getDeviceSpacePath(), Invert::kYes,
+                                       element->isAA());
     }
 
     SK_ABORT("Unexpected DeviceSpaceType");
@@ -557,6 +571,43 @@
     }
 }
 
+template<typename T>
+inline GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const T& deviceSpaceShape,
+                                                              Invert invert, bool aa) {
+    if (fAnalyticFPs.count() >= fMaxAnalyticFPs) {
+        return ClipResult::kNotClipped;
+    }
+
+    GrClipEdgeType edgeType;
+    if (Invert::kNo == invert) {
+        edgeType = aa ? GrClipEdgeType::kFillAA : GrClipEdgeType::kFillBW;
+    } else {
+        edgeType = aa ? GrClipEdgeType::kInverseFillAA : GrClipEdgeType::kInverseFillBW;
+    }
+
+    if (auto fp = make_analytic_clip_fp(edgeType, deviceSpaceShape)) {
+        fAnalyticFPs.push_back(std::move(fp));
+        return ClipResult::kClipped;
+    }
+
+    return ClipResult::kNotClipped;
+}
+
+std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
+                                                           const SkRect& deviceSpaceRect) {
+    return GrConvexPolyEffect::Make(edgeType, deviceSpaceRect);
+}
+
+std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
+                                                           const SkRRect& deviceSpaceRRect) {
+    return GrRRectEffect::Make(edgeType, deviceSpaceRRect);
+}
+
+std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
+                                                           const SkPath& deviceSpacePath) {
+    return GrConvexPolyEffect::Make(edgeType, deviceSpacePath);
+}
+
 void GrReducedClip::makeEmpty() {
     fHasScissor = false;
     fAAClipRectGenID = SK_InvalidGenID;