Make GrDashEffect take a AA mode enum.
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2042923003

Review-Url: https://codereview.chromium.org/2042923003
diff --git a/src/gpu/batches/GrDashLinePathRenderer.cpp b/src/gpu/batches/GrDashLinePathRenderer.cpp
index 6a05458..51ef052 100644
--- a/src/gpu/batches/GrDashLinePathRenderer.cpp
+++ b/src/gpu/batches/GrDashLinePathRenderer.cpp
@@ -23,13 +23,22 @@
     GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
                               "GrDashLinePathRenderer::onDrawPath");
     bool msaaIsEnabled = args.fDrawContext->isUnifiedMultisampled();
+    GrDashingEffect::AAMode aaMode;
+    if (msaaIsEnabled) {
+        // We ignore args.fAntiAlias here and force anti aliasing when using MSAA. Otherwise,
+        // we can wind up with external edges antialiased and internal edges unantialiased.
+        aaMode = GrDashingEffect::AAMode::kCoverageWithMSAA;
+    } else if (args.fAntiAlias) {
+        aaMode = GrDashingEffect::AAMode::kCoverage;
+    } else {
+        aaMode = GrDashingEffect::AAMode::kNone;
+    }
     SkPoint pts[2];
     SkAssertResult(args.fPath->isLine(pts));
     SkAutoTUnref<GrDrawBatch> batch(GrDashingEffect::CreateDashLineBatch(args.fColor,
                                                                          *args.fViewMatrix,
                                                                          pts,
-                                                                         args.fAntiAlias,
-                                                                         msaaIsEnabled,
+                                                                         aaMode,
                                                                          *args.fStyle));
     if (!batch) {
         return false;
diff --git a/src/gpu/effects/GrDashingEffect.cpp b/src/gpu/effects/GrDashingEffect.cpp
index 8a38ebe..8a7cb5e 100644
--- a/src/gpu/effects/GrDashingEffect.cpp
+++ b/src/gpu/effects/GrDashingEffect.cpp
@@ -26,6 +26,8 @@
 #include "glsl/GrGLSLVarying.h"
 #include "glsl/GrGLSLVertexShaderBuilder.h"
 
+using AAMode = GrDashingEffect::AAMode;
+
 ///////////////////////////////////////////////////////////////////////////////
 
 // Returns whether or not the gpu can fast path the dash line effect.
@@ -74,14 +76,6 @@
     SkScalar fRadius;
     SkScalar fCenterX;
 };
-
-enum DashAAMode {
-    kBW_DashAAMode,
-    kEdgeAA_DashAAMode,
-    kMSAA_DashAAMode,
-
-    kDashAAModeCount,
-};
 };
 
 static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
@@ -239,7 +233,7 @@
  * position relative to the dashed line.
  */
 static GrGeometryProcessor* create_dash_gp(GrColor,
-                                           DashAAMode aaMode,
+                                           AAMode aaMode,
                                            DashCap cap,
                                            const SkMatrix& localMatrix,
                                            bool usesLocalCoords);
@@ -247,7 +241,6 @@
 class DashBatch : public GrVertexBatch {
 public:
     DEFINE_BATCH_CLASS_ID
-
     struct Geometry {
         SkMatrix fViewMatrix;
         SkMatrix fSrcRotInv;
@@ -260,7 +253,7 @@
         GrColor fColor;
     };
 
-    static GrDrawBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode,
+    static GrDrawBatch* Create(const Geometry& geometry, SkPaint::Cap cap, AAMode aaMode,
                                bool fullDash) {
         return new DashBatch(geometry, cap, aaMode, fullDash);
     }
@@ -278,7 +271,7 @@
     SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
 
 private:
-    DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash)
+    DashBatch(const Geometry& geometry, SkPaint::Cap cap, AAMode aaMode, bool fullDash)
         : INHERITED(ClassID()) {
         fGeoData.push_back(geometry);
 
@@ -359,7 +352,7 @@
         }
 
         // useAA here means Edge AA or MSAA
-        bool useAA = this->aaMode() != kBW_DashAAMode;
+        bool useAA = this->aaMode() != AAMode::kNone;
         bool fullDash = this->fullDash();
 
         // We do two passes over all of the dashes.  First we setup the start, end, and bounds,
@@ -486,9 +479,13 @@
 
             // For EdgeAA, we bloat in X & Y for both square and round caps.
             // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
-            SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
-            SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
-                                 ? 0.5f : devBloatX;
+            SkScalar devBloatX = this->aaMode() == AAMode::kCoverage ? 0.5f : 0.0f;
+            SkScalar devBloatY;
+            if (SkPaint::kRound_Cap == cap && this->aaMode() == AAMode::kCoverageWithMSAA) {
+                devBloatY = 0.5f;
+            } else {
+                devBloatY = devBloatX;
+            }
 
             SkScalar bloatX = devBloatX / args.fParallelScale;
             SkScalar bloatY = devBloatY / args.fPerpendicularScale;
@@ -665,7 +662,7 @@
     GrColor color() const { return fBatch.fColor; }
     bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
     const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
-    DashAAMode aaMode() const { return fBatch.fAAMode; }
+    AAMode aaMode() const { return fBatch.fAAMode; }
     bool fullDash() const { return fBatch.fFullDash; }
     SkPaint::Cap cap() const { return fBatch.fCap; }
     bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
@@ -676,7 +673,7 @@
         bool fColorIgnored;
         bool fCoverageIgnored;
         SkPaint::Cap fCap;
-        DashAAMode fAAMode;
+        AAMode fAAMode;
         bool fFullDash;
     };
 
@@ -689,8 +686,11 @@
     typedef GrVertexBatch INHERITED;
 };
 
-static GrDrawBatch* create_batch(GrColor color, const SkMatrix& viewMatrix, const SkPoint pts[2],
-                                 bool useAA, const GrStyle& style, bool msaaRT) {
+GrDrawBatch* GrDashingEffect::CreateDashLineBatch(GrColor color,
+                                                  const SkMatrix& viewMatrix,
+                                                  const SkPoint pts[2],
+                                                  AAMode aaMode,
+                                                  const GrStyle& style) {
     SkASSERT(GrDashingEffect::CanDrawDashLine(pts, style, viewMatrix));
     const SkScalar* intervals = style.dashIntervals();
     SkScalar phase = style.dashPhase();
@@ -728,11 +728,8 @@
         offInterval -= strokeWidth;
     }
 
-    DashAAMode aaMode = msaaRT ? kMSAA_DashAAMode :
-                                 useAA ? kEdgeAA_DashAAMode : kBW_DashAAMode;
-
     // TODO we can do a real rect call if not using fulldash(ie no off interval, not using AA)
-    bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode;
+    bool fullDash = offInterval > 0.f || aaMode != AAMode::kNone;
 
     geometry.fColor = color;
     geometry.fViewMatrix = viewMatrix;
@@ -743,15 +740,6 @@
     return DashBatch::Create(geometry, cap, aaMode, fullDash);
 }
 
-GrDrawBatch* GrDashingEffect::CreateDashLineBatch(GrColor color,
-                                                  const SkMatrix& viewMatrix,
-                                                  const SkPoint pts[2],
-                                                  bool useAA,
-                                                  bool msaaIsEnabled,
-                                                  const GrStyle& style) {
-    return create_batch(color, viewMatrix, pts, useAA, style, msaaIsEnabled);
-}
-
 //////////////////////////////////////////////////////////////////////////////
 
 class GLDashingCircleEffect;
@@ -770,7 +758,7 @@
     typedef SkPathEffect::DashInfo DashInfo;
 
     static GrGeometryProcessor* Create(GrColor,
-                                       DashAAMode aaMode,
+                                       AAMode aaMode,
                                        const SkMatrix& localMatrix,
                                        bool usesLocalCoords);
 
@@ -782,7 +770,7 @@
 
     const Attribute* inCircleParams() const { return fInCircleParams; }
 
-    DashAAMode aaMode() const { return fAAMode; }
+    AAMode aaMode() const { return fAAMode; }
 
     GrColor color() const { return fColor; }
 
@@ -797,13 +785,13 @@
     GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
 
 private:
-    DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix,
+    DashingCircleEffect(GrColor, AAMode aaMode, const SkMatrix& localMatrix,
                         bool usesLocalCoords);
 
     GrColor             fColor;
     SkMatrix            fLocalMatrix;
     bool                fUsesLocalCoords;
-    DashAAMode          fAAMode;
+    AAMode              fAAMode;
     const Attribute*    fInPosition;
     const Attribute*    fInDashParams;
     const Attribute*    fInCircleParams;
@@ -896,7 +884,7 @@
     fragBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
     fragBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
     fragBuilder->codeAppend("float dist = length(center - fragPosShifted);");
-    if (dce.aaMode() != kBW_DashAAMode) {
+    if (dce.aaMode() != AAMode::kNone) {
         fragBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
         fragBuilder->codeAppend("diff = 1.0 - diff;");
         fragBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
@@ -925,14 +913,14 @@
     uint32_t key = 0;
     key |= dce.usesLocalCoords() && dce.localMatrix().hasPerspective() ? 0x1 : 0x0;
     key |= dce.colorIgnored() ? 0x2 : 0x0;
-    key |= dce.aaMode() << 8;
+    key |= static_cast<uint32_t>(dce.aaMode()) << 8;
     b->add32(key);
 }
 
 //////////////////////////////////////////////////////////////////////////////
 
 GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
-                                                 DashAAMode aaMode,
+                                                 AAMode aaMode,
                                                  const SkMatrix& localMatrix,
                                                  bool usesLocalCoords) {
     return new DashingCircleEffect(color, aaMode, localMatrix, usesLocalCoords);
@@ -948,7 +936,7 @@
 }
 
 DashingCircleEffect::DashingCircleEffect(GrColor color,
-                                         DashAAMode aaMode,
+                                         AAMode aaMode,
                                          const SkMatrix& localMatrix,
                                          bool usesLocalCoords)
     : fColor(color)
@@ -965,7 +953,7 @@
 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
 
 const GrGeometryProcessor* DashingCircleEffect::TestCreate(GrProcessorTestData* d) {
-    DashAAMode aaMode = static_cast<DashAAMode>(d->fRandom->nextULessThan(kDashAAModeCount));
+    AAMode aaMode = static_cast<AAMode>(d->fRandom->nextULessThan(GrDashingEffect::kAAModeCnt));
     return DashingCircleEffect::Create(GrRandomColor(d->fRandom),
                                       aaMode, GrTest::TestMatrix(d->fRandom),
                                       d->fRandom->nextBool());
@@ -989,7 +977,7 @@
     typedef SkPathEffect::DashInfo DashInfo;
 
     static GrGeometryProcessor* Create(GrColor,
-                                       DashAAMode aaMode,
+                                       AAMode aaMode,
                                        const SkMatrix& localMatrix,
                                        bool usesLocalCoords);
 
@@ -1001,7 +989,7 @@
 
     const Attribute* inRectParams() const { return fInRectParams; }
 
-    DashAAMode aaMode() const { return fAAMode; }
+    AAMode aaMode() const { return fAAMode; }
 
     GrColor color() const { return fColor; }
 
@@ -1016,13 +1004,13 @@
     GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
 
 private:
-    DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix,
+    DashingLineEffect(GrColor, AAMode aaMode, const SkMatrix& localMatrix,
                       bool usesLocalCoords);
 
     GrColor             fColor;
     SkMatrix            fLocalMatrix;
     bool                fUsesLocalCoords;
-    DashAAMode          fAAMode;
+    AAMode              fAAMode;
     const Attribute*    fInPosition;
     const Attribute*    fInDashParams;
     const Attribute*    fInRectParams;
@@ -1108,7 +1096,7 @@
                              inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
                              inDashParams.fsIn());
     fragBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
-    if (de.aaMode() == kEdgeAA_DashAAMode) {
+    if (de.aaMode() == AAMode::kCoverage) {
         // The amount of coverage removed in x and y by the edges is computed as a pair of negative
         // numbers, xSub and ySub.
         fragBuilder->codeAppend("float xSub, ySub;");
@@ -1120,7 +1108,7 @@
         // covered.
         fragBuilder->codeAppendf(
             "float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
-    } else if (de.aaMode() == kMSAA_DashAAMode) {
+    } else if (de.aaMode() == AAMode::kCoverageWithMSAA) {
         // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
         // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
         fragBuilder->codeAppend("float xSub;");
@@ -1157,14 +1145,14 @@
     uint32_t key = 0;
     key |= de.usesLocalCoords() && de.localMatrix().hasPerspective() ? 0x1 : 0x0;
     key |= de.colorIgnored() ? 0x2 : 0x0;
-    key |= de.aaMode() << 8;
+    key |= static_cast<int>(de.aaMode()) << 8;
     b->add32(key);
 }
 
 //////////////////////////////////////////////////////////////////////////////
 
 GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
-                                               DashAAMode aaMode,
+                                               AAMode aaMode,
                                                const SkMatrix& localMatrix,
                                                bool usesLocalCoords) {
     return new DashingLineEffect(color, aaMode, localMatrix, usesLocalCoords);
@@ -1180,7 +1168,7 @@
 }
 
 DashingLineEffect::DashingLineEffect(GrColor color,
-                                     DashAAMode aaMode,
+                                     AAMode aaMode,
                                      const SkMatrix& localMatrix,
                                      bool usesLocalCoords)
     : fColor(color)
@@ -1196,7 +1184,7 @@
 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
 
 const GrGeometryProcessor* DashingLineEffect::TestCreate(GrProcessorTestData* d) {
-    DashAAMode aaMode = static_cast<DashAAMode>(d->fRandom->nextULessThan(kDashAAModeCount));
+    AAMode aaMode = static_cast<AAMode>(d->fRandom->nextULessThan(GrDashingEffect::kAAModeCnt));
     return DashingLineEffect::Create(GrRandomColor(d->fRandom),
                                      aaMode, GrTest::TestMatrix(d->fRandom),
                                      d->fRandom->nextBool());
@@ -1205,7 +1193,7 @@
 //////////////////////////////////////////////////////////////////////////////
 
 static GrGeometryProcessor* create_dash_gp(GrColor color,
-                                           DashAAMode dashAAMode,
+                                           AAMode aaMode,
                                            DashCap cap,
                                            const SkMatrix& viewMatrix,
                                            bool usesLocalCoords) {
@@ -1217,9 +1205,9 @@
 
     switch (cap) {
         case kRound_DashCap:
-            return DashingCircleEffect::Create(color, dashAAMode, invert, usesLocalCoords);
+            return DashingCircleEffect::Create(color, aaMode, invert, usesLocalCoords);
         case kNonRound_DashCap:
-            return DashingLineEffect::Create(color, dashAAMode, invert, usesLocalCoords);
+            return DashingLineEffect::Create(color, aaMode, invert, usesLocalCoords);
     }
     return nullptr;
 }
@@ -1231,8 +1219,7 @@
 DRAW_BATCH_TEST_DEFINE(DashBatch) {
     GrColor color = GrRandomColor(random);
     SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
-    bool useAA = random->nextBool();
-    bool msaaRT = random->nextBool();
+    AAMode aaMode = static_cast<AAMode>(random->nextULessThan(GrDashingEffect::kAAModeCnt));
 
     // We can only dash either horizontal or vertical lines
     SkPoint pts[2];
@@ -1294,7 +1281,7 @@
 
     GrStyle style(p);
 
-    return create_batch(color, viewMatrix, pts, useAA, style, msaaRT);
+    return GrDashingEffect::CreateDashLineBatch(color, viewMatrix, pts, aaMode, style);
 }
 
 #endif
diff --git a/src/gpu/effects/GrDashingEffect.h b/src/gpu/effects/GrDashingEffect.h
index 8899820..b2d0523 100644
--- a/src/gpu/effects/GrDashingEffect.h
+++ b/src/gpu/effects/GrDashingEffect.h
@@ -17,11 +17,17 @@
 class GrStyle;
 
 namespace GrDashingEffect {
+    enum class AAMode {
+        kNone,
+        kCoverage,
+        kCoverageWithMSAA,
+    };
+    static const int kAAModeCnt = static_cast<int>(AAMode::kCoverageWithMSAA) + 1;
+
     GrDrawBatch* CreateDashLineBatch(GrColor,
                                      const SkMatrix& viewMatrix,
                                      const SkPoint pts[2],
-                                     bool useAA,
-                                     bool msaaIsEnabled,
+                                     AAMode,
                                      const GrStyle& style);
     bool CanDrawDashLine(const SkPoint pts[2], const GrStyle& style,
                          const SkMatrix& viewMatrix);