Added distance vector support for CircleGeometryProcessor
authordvonbeck <dvonbeck@google.com>
Fri, 29 Jul 2016 18:11:51 +0000 (11:11 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 29 Jul 2016 18:11:51 +0000 (11:11 -0700)
This CL's base is the CL that sets up the distance vector field req. exposure: https://codereview.chromium.org/2114993002/

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

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

src/gpu/GrOvalRenderer.cpp

index aec9b76ba85f986236a0382fcb451f84beb5e2c2..527b96e2c64a7f88e8637f8458e1311ef6c99ae9 100644 (file)
@@ -83,10 +83,13 @@ public:
         fStroke = stroke;
     }
 
+    bool implementsDistanceVector() const override { return true; };
+
     const Attribute* inPosition() const { return fInPosition; }
     const Attribute* inColor() const { return fInColor; }
     const Attribute* inCircleEdge() const { return fInCircleEdge; }
     const SkMatrix& localMatrix() const { return fLocalMatrix; }
+
     virtual ~CircleGeometryProcessor() {}
 
     const char* name() const override { return "CircleEdge"; }
@@ -126,14 +129,24 @@ public:
                                  args.fTransformsOut);
 
             fragBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn());
-            fragBuilder->codeAppendf("float edgeAlpha = clamp(%s.z * (1.0 - d), 0.0, 1.0);",
-                                     v.fsIn());
+            fragBuilder->codeAppendf("float distanceToEdge = %s.z * (1.0 - d);", v.fsIn());
+            fragBuilder->codeAppendf("float edgeAlpha = clamp(distanceToEdge, 0.0, 1.0);");
             if (cgp.fStroke) {
                 fragBuilder->codeAppendf("float innerAlpha = clamp(%s.z * (d - %s.w), 0.0, 1.0);",
                                          v.fsIn(), v.fsIn());
                 fragBuilder->codeAppend("edgeAlpha *= innerAlpha;");
             }
 
+            if (args.fDistanceVectorName) {
+                fragBuilder->codeAppend ("if (d == 0.0) {"); // if on the center of the circle
+                fragBuilder->codeAppendf("    %s = vec2(distanceToEdge, 0.0);", // avoid normalizing
+                                         args.fDistanceVectorName);
+                fragBuilder->codeAppend ("} else {");
+                fragBuilder->codeAppendf("    %s = normalize(%s.xy) * distanceToEdge;",
+                                         args.fDistanceVectorName, v.fsIn());
+                fragBuilder->codeAppend ("}");
+            }
+
             fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
         }