Merge GrClipMaskManager into GrClipStackClip

TBR=bsalomon@google.com
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2196393007

Review-Url: https://codereview.chromium.org/2196393007
diff --git a/src/gpu/GrBlurUtils.cpp b/src/gpu/GrBlurUtils.cpp
index 441e566..d20226c 100644
--- a/src/gpu/GrBlurUtils.cpp
+++ b/src/gpu/GrBlurUtils.cpp
@@ -17,6 +17,7 @@
 #include "SkGrPriv.h"
 #include "SkMaskFilter.h"
 #include "SkPaint.h"
+#include "SkTLazy.h"
 
 static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
     return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
diff --git a/src/gpu/GrClip.cpp b/src/gpu/GrClip.cpp
index b0c8db3..3eccd80 100644
--- a/src/gpu/GrClip.cpp
+++ b/src/gpu/GrClip.cpp
@@ -7,7 +7,6 @@
 
 #include "GrClip.h"
 
-#include "GrClipMaskManager.h"
 #include "GrDrawContext.h"
 
 void GrNoClip::getConservativeBounds(int width, int height, SkIRect* devResult,
@@ -70,36 +69,3 @@
     out->makeStencil(fHasStencilClip, fDeviceBounds);
     return true;
 }
-
-bool GrClipStackClip::quickContains(const SkRect& rect) const {
-    if (!fStack) {
-        return true;
-    }
-    return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
-                                                 SkIntToScalar(fOrigin.y())));
-}
-
-void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
-                                            bool* isIntersectionOfRects) const {
-    if (!fStack) {
-        devResult->setXYWH(0, 0, width, height);
-        if (isIntersectionOfRects) {
-            *isIntersectionOfRects = true;
-        }
-        return;
-    }
-    SkRect devBounds;
-    fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
-                                  isIntersectionOfRects);
-    devBounds.roundOut(devResult);
-}
-
-bool GrClipStackClip::apply(GrContext* context,
-                            GrDrawContext* drawContext,
-                            const SkRect* devBounds,
-                            bool useHWAA,
-                            bool hasUserStencilSettings,
-                            GrAppliedClip* out) const {
-    return GrClipMaskManager::SetupClipping(context, drawContext, *this, devBounds,
-                                            useHWAA, hasUserStencilSettings, out);
-}
diff --git a/src/gpu/GrClipMaskManager.cpp b/src/gpu/GrClipStackClip.cpp
similarity index 88%
rename from src/gpu/GrClipMaskManager.cpp
rename to src/gpu/GrClipStackClip.cpp
index 8e57fc8..4bd6d55 100644
--- a/src/gpu/GrClipMaskManager.cpp
+++ b/src/gpu/GrClipStackClip.cpp
@@ -1,27 +1,18 @@
 /*
- * Copyright 2012 Google Inc.
+ * 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 "GrClipMaskManager.h"
-#include "GrCaps.h"
+#include "GrClipStackClip.h"
+
 #include "GrDrawingManager.h"
 #include "GrDrawContextPriv.h"
 #include "GrGpuResourcePriv.h"
-#include "GrPaint.h"
-#include "GrPathRenderer.h"
-#include "GrRenderTarget.h"
-#include "GrRenderTargetPriv.h"
-#include "GrResourceProvider.h"
 #include "GrStencilAttachment.h"
 #include "GrSWMaskHelper.h"
-#include "SkRasterClip.h"
-#include "SkTLazy.h"
-#include "batches/GrRectBatchFactory.h"
 #include "effects/GrConvexPolyEffect.h"
-#include "effects/GrPorterDuffXferProcessor.h"
 #include "effects/GrRRectEffect.h"
 #include "effects/GrTextureDomain.h"
 
@@ -30,6 +21,29 @@
 
 static const int kMaxAnalyticElements = 4;
 
+bool GrClipStackClip::quickContains(const SkRect& rect) const {
+    if (!fStack) {
+        return true;
+    }
+    return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
+                                                 SkIntToScalar(fOrigin.y())));
+}
+
+void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
+                                            bool* isIntersectionOfRects) const {
+    if (!fStack) {
+        devResult->setXYWH(0, 0, width, height);
+        if (isIntersectionOfRects) {
+            *isIntersectionOfRects = true;
+        }
+        return;
+    }
+    SkRect devBounds;
+    fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
+                                  isIntersectionOfRects);
+    devBounds.roundOut(devResult);
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // set up the draw state to enable the aa clipping mask. Besides setting up the
 // stage matrix this also alters the vertex layout
@@ -56,13 +70,13 @@
 // Does the path in 'element' require SW rendering? If so, return true (and,
 // optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
 // 'prOut' to the non-SW path renderer that will do the job).
-bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
-                                            bool hasUserStencilSettings,
-                                            const GrDrawContext* drawContext,
-                                            const SkMatrix& viewMatrix,
-                                            const Element* element,
-                                            GrPathRenderer** prOut,
-                                            bool needsStencil) {
+bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
+                                          bool hasUserStencilSettings,
+                                          const GrDrawContext* drawContext,
+                                          const SkMatrix& viewMatrix,
+                                          const Element* element,
+                                          GrPathRenderer** prOut,
+                                          bool needsStencil) {
     if (Element::kRect_Type == element->getType()) {
         // rects can always be drawn directly w/o using the software path
         // TODO: skip rrects once we're drawing them directly.
@@ -116,11 +130,11 @@
  * will be used on any element. If so, it returns true to indicate that the
  * entire clip should be rendered in SW and then uploaded en masse to the gpu.
  */
-bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
-                                      bool hasUserStencilSettings,
-                                      const GrDrawContext* drawContext,
-                                      const SkVector& clipToMaskOffset,
-                                      const GrReducedClip::ElementList& elements) {
+bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
+                                    bool hasUserStencilSettings,
+                                    const GrDrawContext* drawContext,
+                                    const SkVector& clipToMaskOffset,
+                                    const GrReducedClip::ElementList& elements) {
     // TODO: generalize this function so that when
     // a clip gets complex enough it can just be done in SW regardless
     // of whether it would invoke the GrSoftwarePathRenderer.
@@ -227,14 +241,13 @@
 ////////////////////////////////////////////////////////////////////////////////
 // sort out what kind of clip mask needs to be created: alpha, stencil,
 // scissor, or entirely software
-bool GrClipMaskManager::SetupClipping(GrContext* context,
-                                      GrDrawContext* drawContext,
-                                      const GrClipStackClip& clip,
-                                      const SkRect* origDevBounds,
-                                      bool useHWAA,
-                                      bool hasUserStencilSettings,
-                                      GrAppliedClip* out) {
-    if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
+bool GrClipStackClip::apply(GrContext* context,
+                            GrDrawContext* drawContext,
+                            const SkRect* origDevBounds,
+                            bool useHWAA,
+                            bool hasUserStencilSettings,
+                            GrAppliedClip* out) const {
+    if (!fStack || fStack->isWideOpen()) {
         return true;
     }
 
@@ -243,15 +256,15 @@
         return false;
     }
 
-    const SkScalar clipX = SkIntToScalar(clip.origin().x()),
-                   clipY = SkIntToScalar(clip.origin().y());
+    const SkScalar clipX = SkIntToScalar(fOrigin.x()),
+                   clipY = SkIntToScalar(fOrigin.y());
 
     GrReducedClip::ElementList elements;
     int32_t genID = 0;
     SkIRect clipSpaceIBounds;
     bool requiresAA = false;
 
-    InitialState initialState = GrReducedClip::ReduceClipStack(*clip.clipStack(),
+    InitialState initialState = GrReducedClip::ReduceClipStack(*fStack,
                                                                devBounds.makeOffset(clipX, clipY),
                                                                &elements,
                                                                &genID,
@@ -262,7 +275,7 @@
             return false;
         } else {
             SkIRect scissorSpaceIBounds(clipSpaceIBounds);
-            scissorSpaceIBounds.offset(-clip.origin());
+            scissorSpaceIBounds.offset(-fOrigin);
             if (!GrClip::IsInsideClip(scissorSpaceIBounds, devBounds)) {
                 out->makeScissored(scissorSpaceIBounds);
             }
@@ -293,7 +306,7 @@
             get_analytic_clip_processor(elements, disallowAnalyticAA, {-clipX, -clipY}, devBounds,
                                         &clipFP)) {
             SkIRect scissorSpaceIBounds(clipSpaceIBounds);
-            scissorSpaceIBounds.offset(-clip.origin());
+            scissorSpaceIBounds.offset(-fOrigin);
             if (GrClip::IsInsideClip(scissorSpaceIBounds, devBounds)) {
                 out->makeFPBased(std::move(clipFP), SkRect::Make(scissorSpaceIBounds));
             } else {
@@ -338,7 +351,7 @@
             // The mask's top left coord should be pinned to the rounded-out top left corner of
             // clipSpace bounds. We determine the mask's position WRT to the render target here.
             SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
-            rtSpaceMaskBounds.offset(-clip.origin());
+            rtSpaceMaskBounds.offset(-fOrigin);
             out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds),
                              SkRect::Make(rtSpaceMaskBounds));
             return true;
@@ -347,7 +360,7 @@
     }
 
     // use the stencil clip if we can't represent the clip as a rectangle.
-    SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
+    SkIPoint clipSpaceToStencilSpaceOffset = -fOrigin;
     CreateStencilClipMask(context,
                           drawContext,
                           genID,
@@ -443,12 +456,12 @@
     builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
 }
 
-sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
-                                                        int32_t elementsGenID,
-                                                        GrReducedClip::InitialState initialState,
-                                                        const GrReducedClip::ElementList& elements,
-                                                        const SkVector& clipToMaskOffset,
-                                                        const SkIRect& clipSpaceIBounds) {
+sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
+                                                      int32_t elementsGenID,
+                                                      GrReducedClip::InitialState initialState,
+                                                      const GrReducedClip::ElementList& elements,
+                                                      const SkVector& clipToMaskOffset,
+                                                      const SkIRect& clipSpaceIBounds) {
     GrResourceProvider* resourceProvider = context->resourceProvider();
     GrUniqueKey key;
     GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
@@ -548,13 +561,13 @@
 ////////////////////////////////////////////////////////////////////////////////
 // Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
 // (as opposed to canvas) coordinates
-bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
-                                              GrDrawContext* drawContext,
-                                              int32_t elementsGenID,
-                                              GrReducedClip::InitialState initialState,
-                                              const GrReducedClip::ElementList& elements,
-                                              const SkIRect& clipSpaceIBounds,
-                                              const SkIPoint& clipSpaceToStencilOffset) {
+bool GrClipStackClip::CreateStencilClipMask(GrContext* context,
+                                            GrDrawContext* drawContext,
+                                            int32_t elementsGenID,
+                                            GrReducedClip::InitialState initialState,
+                                            const GrReducedClip::ElementList& elements,
+                                            const SkIRect& clipSpaceIBounds,
+                                            const SkIPoint& clipSpaceToStencilOffset) {
     SkASSERT(drawContext);
 
     GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
@@ -620,9 +633,10 @@
                 canDrawArgs.fHasUserStencilSettings = false;
                 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
 
-                pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
-                                                                GrPathRendererChain::kStencilOnly_DrawType,
-                                                                &stencilSupport);
+                GrDrawingManager* dm = context->drawingManager();
+                pr = dm->getPathRenderer(canDrawArgs, false,
+                                         GrPathRendererChain::kStencilOnly_DrawType,
+                                         &stencilSupport);
                 if (!pr) {
                     return false;
                 }
@@ -731,13 +745,12 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
-                                                    GrTextureProvider* texProvider,
-                                                    int32_t elementsGenID,
-                                                    GrReducedClip::InitialState initialState,
-                                                    const GrReducedClip::ElementList& elements,
-                                                    const SkVector& clipToMaskOffset,
-                                                    const SkIRect& clipSpaceIBounds) {
+sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
+                                                         int32_t elementsGenID,
+                                                         GrReducedClip::InitialState initialState,
+                                                         const GrReducedClip::ElementList& elements,
+                                                         const SkVector& clipToMaskOffset,
+                                                         const SkIRect& clipSpaceIBounds) {
     GrUniqueKey key;
     GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
     if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
diff --git a/src/gpu/GrClipMaskManager.h b/src/gpu/GrClipStackClip.h
similarity index 66%
rename from src/gpu/GrClipMaskManager.h
rename to src/gpu/GrClipStackClip.h
index 5cf3569..38f9e2d 100644
--- a/src/gpu/GrClipMaskManager.h
+++ b/src/gpu/GrClipStackClip.h
@@ -1,50 +1,44 @@
 /*
- * Copyright 2012 Google Inc.
+ * Copyright 2016 Google Inc.
  *
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file.
  */
-#ifndef GrClipMaskManager_DEFINED
-#define GrClipMaskManager_DEFINED
+#ifndef GrClipStackClip_DEFINED
+#define GrClipStackClip_DEFINED
+
+#include "GrClip.h"
 
 #include "GrReducedClip.h"
 #include "SkClipStack.h"
-#include "SkTypes.h"
 
-class GrAppliedClip;
-class GrClipStackClip;
-class GrContext;
-class GrDrawContext;
-class GrPathRenderer;
-class GrTexture;
 class GrTextureProvider;
-class GrUniqueKey;
-
+class GrPathRenderer;
 
 /**
- * The clip mask creator handles the generation of the clip mask. If anti
- * aliasing is requested it will (in the future) generate a single channel
- * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
- * mask in the stencil buffer. In the non anti-aliasing case, if the clip
- * mask can be represented as a rectangle then scissoring is used. In all
- * cases scissoring is used to bound the range of the clip mask.
+ * GrClipStackClip can apply a generic SkClipStack to the draw state. It may need to generate an
+ * 8-bit alpha clip mask and/or modify the stencil buffer during apply().
  */
-// This has to remain a class, for now, so it can be friended (by GrDrawContext & GrContext)
-class GrClipMaskManager {
+class GrClipStackClip final : public GrClip {
 public:
-    /**
-     * Creates a clip mask if necessary as a stencil buffer or alpha texture
-     * and sets the GrGpu's scissor and stencil state. If the return is false
-     * then the draw can be skipped. devBounds is optional but can help optimize
-     * clipping.
-     */
-    static bool SetupClipping(GrContext*,
-                              GrDrawContext*,
-                              const GrClipStackClip&,
-                              const SkRect* devBounds,
-                              bool useHWAA,
-                              bool hasUserStencilSettings,
-                              GrAppliedClip* out);
+    GrClipStackClip(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) {
+        this->reset(stack, origin);
+    }
+
+    void reset(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) {
+        fOrigin = origin ? *origin : SkIPoint::Make(0, 0);
+        fStack.reset(SkSafeRef(stack));
+    }
+
+    bool quickContains(const SkRect&) const final;
+    void getConservativeBounds(int width, int height, SkIRect* devResult,
+                               bool* isIntersectionOfRects) const final;
+    bool apply(GrContext*,
+               GrDrawContext*,
+               const SkRect* devBounds,
+               bool useHWAA,
+               bool hasUserStencilSettings,
+               GrAppliedClip* out) const final;
 
 private:
     static bool PathNeedsSWRenderer(GrContext* context,
@@ -89,6 +83,9 @@
 
     static GrTexture* CreateCachedMask(int width, int height, const GrUniqueKey& key,
                                        bool renderTarget);
+
+    SkIPoint                          fOrigin;
+    SkAutoTUnref<const SkClipStack>   fStack;
 };
 
-#endif // GrClipMaskManager_DEFINED
+#endif // GrClipStackClip_DEFINED
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index bb473ca..4ab467c 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -49,7 +49,7 @@
     , fAuditTrail(auditTrail)
     , fFlags(0)
     , fRenderTarget(rt) {
-    // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
+    // TODO: Stop extracting the context (currently needed by GrClip)
     fContext = fGpu->getContext();
 
     fClipBatchToBounds = options.fClipBatchToBounds;
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index 02a7770..6149e92 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -214,7 +214,7 @@
         SkRect         fClippedBounds;
     };
     SkSTArray<256, RecordedBatch, true>             fRecordedBatches;
-    // The context is only in service of the clip mask manager, remove once CMM doesn't need this.
+    // The context is only in service of the GrClip, remove once it doesn't need this.
     GrContext*                                      fContext;
     GrGpu*                                          fGpu;
     GrResourceProvider*                             fResourceProvider;
diff --git a/src/gpu/GrSWMaskHelper.h b/src/gpu/GrSWMaskHelper.h
index 6d1d9b1..f2bb34b 100644
--- a/src/gpu/GrSWMaskHelper.h
+++ b/src/gpu/GrSWMaskHelper.h
@@ -75,7 +75,7 @@
                                              const SkMatrix* matrix);
 
     // This utility routine is used to add a shape's mask to some other draw.
-    // The ClipMaskManager uses it to accumulate clip masks while the
+    // The GrClipStackClip uses it to accumulate clip masks while the
     // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
     // It draws with "texture" as a path mask into "target" using "rect" as
     // geometry and the current drawState. The current drawState is altered to
diff --git a/src/gpu/GrStencilAttachment.h b/src/gpu/GrStencilAttachment.h
index 7573d23..8f41b57 100644
--- a/src/gpu/GrStencilAttachment.h
+++ b/src/gpu/GrStencilAttachment.h
@@ -9,8 +9,8 @@
 #ifndef GrStencilAttachment_DEFINED
 #define GrStencilAttachment_DEFINED
 
-#include "GrClip.h"
 #include "GrGpuResource.h"
+#include "SkClipStack.h"
 
 class GrRenderTarget;
 class GrResourceKey;
diff --git a/src/gpu/SkGpuDevice.h b/src/gpu/SkGpuDevice.h
index fbbcc4e..efb6744 100644
--- a/src/gpu/SkGpuDevice.h
+++ b/src/gpu/SkGpuDevice.h
@@ -15,6 +15,7 @@
 #include "SkPicture.h"
 #include "SkRegion.h"
 #include "SkSurface.h"
+#include "GrClipStackClip.h"
 #include "GrDrawContext.h"
 #include "GrContext.h"
 #include "GrSurfacePriv.h"