Converts a drawPaint through a rrect clip to a drawRRect in GrDrawContext.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2271053004

Review-Url: https://codereview.chromium.org/2271053004
diff --git a/gm/rrectclipdrawpaint.cpp b/gm/rrectclipdrawpaint.cpp
new file mode 100644
index 0000000..3f5ded2
--- /dev/null
+++ b/gm/rrectclipdrawpaint.cpp
@@ -0,0 +1,53 @@
+/*
+ * 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 "SkPath.h"
+#include "SkGradientShader.h"
+
+// Exercises code in GrDrawContext that attempts to replace a rrect clip/draw paint with draw rrect.
+DEF_SIMPLE_GM(rrect_clip_draw_paint, canvas, 256, 256) {
+    SkRRect rrect = SkRRect::MakeRectXY(SkRect::MakeXYWH(10.f, 10.f, 236.f, 236.f), 30.f, 40.f);
+
+    SkPaint p;
+    p.setColor(SK_ColorRED);
+
+    SkMatrix zoomOut;
+    zoomOut.setScale(0.7f, 0.7f, 128.f, 128.f);
+
+    const SkRect layerRect = SkRect::MakeWH(256.f, 256.f);
+    canvas->saveLayer(layerRect, nullptr);
+    canvas->clipRRect(rrect, SkRegion::kIntersect_Op, true);
+    canvas->drawPaint(p);
+    canvas->restore();
+
+    canvas->concat(zoomOut);
+    p.setColor(SK_ColorBLUE);
+    canvas->saveLayer(layerRect, nullptr);
+    canvas->clipRRect(rrect, SkRegion::kIntersect_Op, false);
+    canvas->drawPaint(p);
+    canvas->restore();
+
+    static constexpr SkPoint kPts[] = {{0.f, 0.f}, {256.f, 256.f}};
+    static constexpr SkColor kColors1[] = {SK_ColorCYAN, SK_ColorGREEN};
+    p.setShader(SkGradientShader::MakeLinear(kPts, kColors1, nullptr, 2,
+                                             SkShader::kClamp_TileMode));
+    canvas->concat(zoomOut);
+    canvas->saveLayer(layerRect, nullptr);
+    canvas->clipRRect(rrect, SkRegion::kIntersect_Op, true);
+    canvas->drawPaint(p);
+    canvas->restore();
+
+    static constexpr SkColor kColors2[] = {SK_ColorMAGENTA, SK_ColorGRAY};
+    p.setShader(SkGradientShader::MakeRadial({128.f, 128.f}, 128.f, kColors2, nullptr, 2,
+                                             SkShader::kClamp_TileMode));
+    canvas->concat(zoomOut);
+    canvas->saveLayer(layerRect, nullptr);
+    canvas->clipRRect(rrect, SkRegion::kIntersect_Op, false);
+    canvas->drawPaint(p);
+    canvas->restore();
+}
diff --git a/include/core/SkClipStack.h b/include/core/SkClipStack.h
index fb0b278..f34283d 100644
--- a/include/core/SkClipStack.h
+++ b/include/core/SkClipStack.h
@@ -365,6 +365,21 @@
     bool isWideOpen() const { return this->getTopmostGenID() == kWideOpenGenID; }
 
     /**
+     * This method quickly and conservatively determines whether the entire stack is equivalent to
+     * intersection with a rrect given a bounds, where the rrect must not contain the entire bounds.
+     *
+     * @param bounds   A bounds on what will be drawn through the clip. The clip only need be
+     *                 equivalent to a intersection with a rrect for draws within the bounds. The
+     *                 returned rrect must intersect the bounds but need not be contained by the
+     *                 bounds.
+     * @param rrect    If return is true rrect will contain the rrect equivalent to the stack.
+     * @param aa       If return is true aa will indicate whether the equivalent rrect clip is
+     *                 antialiased.
+     * @return true if the stack is equivalent to a single rrect intersect clip, false otherwise.
+     */
+    bool isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const;
+
+    /**
      * The generation ID has three reserved values to indicate special
      * (potentially ignorable) cases
      */
diff --git a/include/gpu/GrClip.h b/include/gpu/GrClip.h
index 168827b..a7505f9 100644
--- a/include/gpu/GrClip.h
+++ b/include/gpu/GrClip.h
@@ -33,6 +33,21 @@
     virtual ~GrClip() {}
 
     /**
+     * This method quickly and conservatively determines whether the entire clip is equivalent to
+     * intersection with a rrect. This will only return true if the rrect does not fully contain
+     * the render target bounds. Moreover, the returned rrect need not be contained by the render
+     * target bounds. We assume all draws will be implicitly clipped by the render target bounds.
+     *
+     * @param rtBounds The bounds of the render target that the clip will be applied to.
+     * @param rrect    If return is true rrect will contain the rrect equivalent to the clip within
+     *                 rtBounds.
+     * @param aa       If return is true aa will indicate whether the rrect clip is antialiased.
+     * @return true if the clip is equivalent to a single rrect, false otherwise.
+     *
+     */
+    virtual bool isRRect(const SkRect& rtBounds, SkRRect* rrect, bool* aa) const = 0;
+
+    /**
      * This is the maximum distance that a draw may extend beyond a clip's boundary and still count
      * count as "on the other side". We leave some slack because floating point rounding error is
      * likely to blame. The rationale for 1e-3 is that in the coverage case (and barring unexpected
@@ -122,6 +137,7 @@
     bool apply(GrContext*, GrDrawContext*, bool, bool, GrAppliedClip*) const final {
         return true;
     }
+    bool isRRect(const SkRect&, SkRRect*, bool*) const override { return false; };
 };
 
 #endif
diff --git a/src/core/SkClipStack.cpp b/src/core/SkClipStack.cpp
index 8bab586..30f5a46 100644
--- a/src/core/SkClipStack.cpp
+++ b/src/core/SkClipStack.cpp
@@ -862,6 +862,50 @@
     }
 }
 
+bool SkClipStack::isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const {
+    // We limit to 5 elements. This means the back element will be bounds checked at most 4 times if
+    // it is an rrect.
+    int cnt = fDeque.count();
+    if (!cnt || cnt > 5) {
+        return false;
+    }
+    const Element* back = static_cast<const Element*>(fDeque.back());
+    if (back->getType() != SkClipStack::Element::kRect_Type &&
+        back->getType() != SkClipStack::Element::kRRect_Type) {
+        return false;
+    }
+    if (back->getOp() == SkRegion::kReplace_Op) {
+        *rrect = back->asRRect();
+        *aa = back->isAA();
+        return true;
+    }
+
+    if (back->getOp() == SkRegion::kIntersect_Op) {
+        SkRect backBounds;
+        if (!backBounds.intersect(bounds, back->asRRect().rect())) {
+            return false;
+        }
+        if (cnt > 1) {
+            SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
+            SkAssertResult(static_cast<const Element*>(iter.prev()) == back);
+            while (const Element* prior = (const Element*)iter.prev()) {
+                if ((prior->getOp() != SkRegion::kIntersect_Op &&
+                     prior->getOp() != SkRegion::kReplace_Op) ||
+                    !prior->contains(backBounds)) {
+                    return false;
+                }
+                if (prior->getOp() == SkRegion::kReplace_Op) {
+                    break;
+                }
+            }
+        }
+        *rrect = back->asRRect();
+        *aa = back->isAA();
+        return true;
+    }
+    return false;
+}
+
 int32_t SkClipStack::GetNextGenID() {
     // TODO: handle overflow.
     return sk_atomic_inc(&gGenID);
diff --git a/src/gpu/GrClipStackClip.cpp b/src/gpu/GrClipStackClip.cpp
index 7138f4b..8233786 100644
--- a/src/gpu/GrClipStackClip.cpp
+++ b/src/gpu/GrClipStackClip.cpp
@@ -41,6 +41,27 @@
                                                   SkIntToScalar(fOrigin.fY)));
 }
 
+bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, bool* aa) const {
+    if (!fStack) {
+        return false;
+    }
+    const SkRect* rtBounds = &origRTBounds;
+    SkRect tempRTBounds;
+    bool origin = fOrigin.fX || fOrigin.fY;
+    if (origin) {
+        tempRTBounds = origRTBounds;
+        tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
+        rtBounds = &tempRTBounds;
+    }
+    if (fStack->isRRect(*rtBounds, rr, aa)) {
+        if (origin) {
+            rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
+        }
+        return true;
+    }
+    return false;
+}
+
 void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
                                             bool* isIntersectionOfRects) const {
     if (!fStack) {
diff --git a/src/gpu/GrClipStackClip.h b/src/gpu/GrClipStackClip.h
index cb1f683..ad143d6 100644
--- a/src/gpu/GrClipStackClip.h
+++ b/src/gpu/GrClipStackClip.h
@@ -38,6 +38,8 @@
     bool apply(GrContext*, GrDrawContext*, bool useHWAA, bool hasUserStencilSettings,
                GrAppliedClip* out) const final;
 
+    bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override;
+
 private:
     static bool PathNeedsSWRenderer(GrContext* context,
                                     bool hasUserStencilSettings,
diff --git a/src/gpu/GrDrawContext.cpp b/src/gpu/GrDrawContext.cpp
index 5afb2ea..2bb9caa 100644
--- a/src/gpu/GrDrawContext.cpp
+++ b/src/gpu/GrDrawContext.cpp
@@ -251,6 +251,20 @@
               SkIntToScalar(fRenderTarget->height()));
     SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
 
+    SkRRect rrect;
+    bool aaRRect;
+    // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
+    // transformation for non-rect rrects. Rects caused a performance regression on an Android
+    // test that needs investigation. We also skip cases where there are fragment processors
+    // because they may depend on having correct local coords and this path draws in device space
+    // without a local matrix.
+    if (!paint->numTotalFragmentProcessors() &&
+        clip.isRRect(r, &rrect, &aaRRect) && !rrect.isRect()) {
+        paint.writable()->setAntiAlias(aaRRect);
+        this->drawRRect(GrNoClip(), *paint, SkMatrix::I(), rrect, GrStyle::SimpleFill());
+        return;
+    }
+
     // by definition this fills the entire clip, no need for AA
     if (paint->isAntiAlias()) {
         paint.writable()->setAntiAlias(false);
diff --git a/src/gpu/GrFixedClip.h b/src/gpu/GrFixedClip.h
index 01498c1..705b2ea 100644
--- a/src/gpu/GrFixedClip.h
+++ b/src/gpu/GrFixedClip.h
@@ -39,6 +39,22 @@
     void getConservativeBounds(int width, int height, SkIRect* devResult,
                                bool* isIntersectionOfRects) const final;
 
+    bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override {
+        if (fHasStencilClip) {
+            return false;
+        }
+        if (fScissorState.enabled()) {
+            SkRect rect = SkRect::Make(fScissorState.rect());
+            if (!rect.intersects(rtBounds)) {
+                return false;
+            }
+            rr->setRect(rect);
+            *aa = false;
+            return true;
+        }
+        return false;
+    };
+
 private:
     bool apply(GrContext*, GrDrawContext*, bool useHWAA, bool hasUserStencilSettings,
                GrAppliedClip* out) const final;