Converts a drawPaint through a rrect clip to a drawRRect in GrDrawContext.
authorbsalomon <bsalomon@google.com>
Fri, 26 Aug 2016 17:48:19 +0000 (10:48 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 26 Aug 2016 17:48:19 +0000 (10:48 -0700)
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2271053004

Review-Url: https://codereview.chromium.org/2271053004

gm/rrectclipdrawpaint.cpp [new file with mode: 0644]
include/core/SkClipStack.h
include/gpu/GrClip.h
src/core/SkClipStack.cpp
src/gpu/GrClipStackClip.cpp
src/gpu/GrClipStackClip.h
src/gpu/GrDrawContext.cpp
src/gpu/GrFixedClip.h

diff --git a/gm/rrectclipdrawpaint.cpp b/gm/rrectclipdrawpaint.cpp
new file mode 100644 (file)
index 0000000..3f5ded2
--- /dev/null
@@ -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();
+}
index fb0b278..f34283d 100644 (file)
@@ -365,6 +365,21 @@ public:
     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
      */
index 168827b..a7505f9 100644 (file)
@@ -33,6 +33,21 @@ public:
     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 @@ private:
     bool apply(GrContext*, GrDrawContext*, bool, bool, GrAppliedClip*) const final {
         return true;
     }
+    bool isRRect(const SkRect&, SkRRect*, bool*) const override { return false; };
 };
 
 #endif
index 8bab586..30f5a46 100644 (file)
@@ -862,6 +862,50 @@ void SkClipStack::getConservativeBounds(int offsetX,
     }
 }
 
+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);
index 7138f4b..8233786 100644 (file)
@@ -41,6 +41,27 @@ bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
                                                   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) {
index cb1f683..ad143d6 100644 (file)
@@ -38,6 +38,8 @@ public:
     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,
index 5afb2ea..2bb9caa 100644 (file)
@@ -251,6 +251,20 @@ void GrDrawContext::drawPaint(const GrClip& clip,
               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);
index 01498c1..705b2ea 100644 (file)
@@ -39,6 +39,22 @@ public:
     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;