Use path outlines to define shadow shapes

Fixes the simplifying assumption that shadow casters were always
rectangular.

Java side APIs + plumbing to pass down correct shapes still need to be added.

Change-Id: Ic4fee90af15679951a44bb5cc6ae454b98c4c194
diff --git a/libs/hwui/PathTessellator.cpp b/libs/hwui/PathTessellator.cpp
index c6ce67c..cc56333 100644
--- a/libs/hwui/PathTessellator.cpp
+++ b/libs/hwui/PathTessellator.cpp
@@ -53,7 +53,7 @@
 namespace android {
 namespace uirenderer {
 
-#define THRESHOLD 0.5f
+#define OUTLINE_REFINE_THRESHOLD_SQUARED (0.5f * 0.5f)
 #define ROUND_CAP_THRESH 0.25f
 #define PI 3.1415926535897932f
 
@@ -739,7 +739,8 @@
     // force close if we're filling the path, since fill path expects closed perimeter.
     bool forceClose = paintInfo.style != SkPaint::kStroke_Style;
     bool wasClosed = approximatePathOutlineVertices(path, forceClose,
-            threshInvScaleX * threshInvScaleX, threshInvScaleY * threshInvScaleY, tempVertices);
+            threshInvScaleX * threshInvScaleX, threshInvScaleY * threshInvScaleY,
+            OUTLINE_REFINE_THRESHOLD_SQUARED, tempVertices);
 
     if (!tempVertices.size()) {
         // path was empty, return without allocating vertex buffer
@@ -824,7 +825,8 @@
     Vector<Vertex> outlineVertices;
     approximatePathOutlineVertices(path, true,
             paintInfo.inverseScaleX * paintInfo.inverseScaleX,
-            paintInfo.inverseScaleY * paintInfo.inverseScaleY, outlineVertices);
+            paintInfo.inverseScaleY * paintInfo.inverseScaleY,
+            OUTLINE_REFINE_THRESHOLD_SQUARED, outlineVertices);
 
     if (!outlineVertices.size()) return;
 
@@ -897,6 +899,11 @@
 // Simple path line approximation
 ///////////////////////////////////////////////////////////////////////////////
 
+bool PathTessellator::approximatePathOutlineVertices(const SkPath& path, float thresholdSquared,
+        Vector<Vertex>& outputVertices) {
+    return approximatePathOutlineVertices(path, true, 1.0f, 1.0f, thresholdSquared, outputVertices);
+}
+
 void pushToVector(Vector<Vertex>& vertices, float x, float y) {
     // TODO: make this not yuck
     vertices.push();
@@ -905,7 +912,8 @@
 }
 
 bool PathTessellator::approximatePathOutlineVertices(const SkPath& path, bool forceClose,
-        float sqrInvScaleX, float sqrInvScaleY, Vector<Vertex>& outputVertices) {
+        float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared,
+        Vector<Vertex>& outputVertices) {
     ATRACE_CALL();
 
     // TODO: to support joins other than sharp miter, join vertices should be labelled in the
@@ -932,7 +940,7 @@
                         pts[0].x(), pts[0].y(),
                         pts[2].x(), pts[2].y(),
                         pts[1].x(), pts[1].y(),
-                        sqrInvScaleX, sqrInvScaleY, outputVertices);
+                        sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices);
                 break;
             case SkPath::kCubic_Verb:
                 ALOGV("kCubic_Verb");
@@ -941,7 +949,7 @@
                         pts[1].x(), pts[1].y(),
                         pts[3].x(), pts[3].y(),
                         pts[2].x(), pts[2].y(),
-                        sqrInvScaleX, sqrInvScaleY, outputVertices);
+                        sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices);
                 break;
             default:
                 break;
@@ -964,7 +972,8 @@
 void PathTessellator::recursiveCubicBezierVertices(
         float p1x, float p1y, float c1x, float c1y,
         float p2x, float p2y, float c2x, float c2y,
-        float sqrInvScaleX, float sqrInvScaleY, Vector<Vertex>& outputVertices) {
+        float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared,
+        Vector<Vertex>& outputVertices) {
     float dx = p2x - p1x;
     float dy = p2y - p1y;
     float d1 = fabs((c1x - p2x) * dy - (c1y - p2y) * dx);
@@ -973,7 +982,7 @@
 
     // multiplying by sqrInvScaleY/X equivalent to multiplying in dimensional scale factors
 
-    if (d * d < THRESHOLD * THRESHOLD * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) {
+    if (d * d < thresholdSquared * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) {
         // below thresh, draw line by adding endpoint
         pushToVector(outputVertices, p2x, p2y);
     } else {
@@ -997,11 +1006,11 @@
         recursiveCubicBezierVertices(
                 p1x, p1y, p1c1x, p1c1y,
                 mx, my, p1c1c2x, p1c1c2y,
-                sqrInvScaleX, sqrInvScaleY, outputVertices);
+                sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices);
         recursiveCubicBezierVertices(
                 mx, my, p2c1c2x, p2c1c2y,
                 p2x, p2y, p2c2x, p2c2y,
-                sqrInvScaleX, sqrInvScaleY, outputVertices);
+                sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices);
     }
 }
 
@@ -1009,12 +1018,13 @@
         float ax, float ay,
         float bx, float by,
         float cx, float cy,
-        float sqrInvScaleX, float sqrInvScaleY, Vector<Vertex>& outputVertices) {
+        float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared,
+        Vector<Vertex>& outputVertices) {
     float dx = bx - ax;
     float dy = by - ay;
     float d = (cx - bx) * dy - (cy - by) * dx;
 
-    if (d * d < THRESHOLD * THRESHOLD * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) {
+    if (d * d < thresholdSquared * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) {
         // below thresh, draw line by adding endpoint
         pushToVector(outputVertices, bx, by);
     } else {
@@ -1028,9 +1038,9 @@
         float my = (acy + bcy) * 0.5f;
 
         recursiveQuadraticBezierVertices(ax, ay, mx, my, acx, acy,
-                sqrInvScaleX, sqrInvScaleY, outputVertices);
+                sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices);
         recursiveQuadraticBezierVertices(mx, my, bx, by, bcx, bcy,
-                sqrInvScaleX, sqrInvScaleY, outputVertices);
+                sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices);
     }
 }