Line endcaps for AA lines are now antialiased.

Also fixed other minor issues with AA and line rendering.

Change-Id: Icd4638d27c70e2ee0f28b5d9a2b97d8b29e8ac4d
diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp
index 62ac2ba..5bfe7a3 100644
--- a/libs/hwui/ProgramCache.cpp
+++ b/libs/hwui/ProgramCache.cpp
@@ -39,8 +39,9 @@
         "attribute vec4 position;\n";
 const char* gVS_Header_Attributes_TexCoords =
         "attribute vec2 texCoords;\n";
-const char* gVS_Header_Attributes_Distance =
-        "attribute float vtxDistance;\n";
+const char* gVS_Header_Attributes_AAParameters =
+        "attribute float vtxWidth;\n"
+        "attribute float vtxLength;\n";
 const char* gVS_Header_Uniforms_TextureTransform =
         "uniform mat4 mainTextureTransform;\n";
 const char* gVS_Header_Uniforms =
@@ -60,8 +61,9 @@
         "uniform mediump vec2 textureDimension;\n";
 const char* gVS_Header_Varyings_HasTexture =
         "varying vec2 outTexCoords;\n";
-const char* gVS_Header_Varyings_HasWidth =
-        "varying float distance;\n";
+const char* gVS_Header_Varyings_IsAA =
+        "varying float widthProportion;\n"
+        "varying float lengthProportion;\n";
 const char* gVS_Header_Varyings_HasBitmap =
         "varying vec2 outBitmapTexCoords;\n";
 const char* gVS_Header_Varyings_PointHasBitmap =
@@ -96,8 +98,9 @@
         "    gl_Position = transform * position;\n";
 const char* gVS_Main_PointSize =
         "    gl_PointSize = pointSize;\n";
-const char* gVS_Main_Width =
-        "    distance = vtxDistance;\n";
+const char* gVS_Main_AA =
+        "    widthProportion = vtxWidth;\n"
+        "    lengthProportion = vtxLength;\n";
 const char* gVS_Footer =
         "}\n\n";
 
@@ -113,10 +116,11 @@
         "precision mediump float;\n\n";
 const char* gFS_Uniforms_Color =
         "uniform vec4 color;\n";
-const char* gFS_Uniforms_Width =
-        "uniform float width;\n"
+const char* gFS_Uniforms_AA =
         "uniform float boundaryWidth;\n"
-        "uniform float inverseBoundaryWidth;\n";
+        "uniform float inverseBoundaryWidth;\n"
+        "uniform float boundaryLength;\n"
+        "uniform float inverseBoundaryLength;\n";
 const char* gFS_Header_Uniforms_PointHasBitmap =
         "uniform vec2 textureDimension;\n"
         "uniform float pointSize;\n";
@@ -189,11 +193,16 @@
         "    fragColor = color;\n";
 const char* gFS_Main_ModulateColor =
         "    fragColor *= color.a;\n";
-const char* gFS_Main_AccountForWidth =
-        "    if (distance < boundaryWidth) {\n"
-        "        fragColor *= (distance * inverseBoundaryWidth);\n"
-        "    } else if (distance > (1.0 - boundaryWidth)) {\n"
-        "        fragColor *= ((1.0 - distance) * inverseBoundaryWidth);\n"
+const char* gFS_Main_AccountForAA =
+        "    if (widthProportion < boundaryWidth) {\n"
+        "        fragColor *= (widthProportion * inverseBoundaryWidth);\n"
+        "    } else if (widthProportion > (1.0 - boundaryWidth)) {\n"
+        "        fragColor *= ((1.0 - widthProportion) * inverseBoundaryWidth);\n"
+        "    }\n"
+        "    if (lengthProportion < boundaryLength) {\n"
+        "        fragColor *= (lengthProportion * inverseBoundaryLength);\n"
+        "    } else if (lengthProportion > (1.0 - boundaryLength)) {\n"
+        "        fragColor *= ((1.0 - lengthProportion) * inverseBoundaryLength);\n"
         "    }\n";
 const char* gFS_Main_FetchTexture[2] = {
         // Don't modulate
@@ -380,8 +389,8 @@
     if (description.hasTexture || description.hasExternalTexture) {
         shader.append(gVS_Header_Attributes_TexCoords);
     }
-    if (description.hasWidth) {
-        shader.append(gVS_Header_Attributes_Distance);
+    if (description.isAA) {
+        shader.append(gVS_Header_Attributes_AAParameters);
     }
     // Uniforms
     shader.append(gVS_Header_Uniforms);
@@ -401,8 +410,8 @@
     if (description.hasTexture || description.hasExternalTexture) {
         shader.append(gVS_Header_Varyings_HasTexture);
     }
-    if (description.hasWidth) {
-        shader.append(gVS_Header_Varyings_HasWidth);
+    if (description.isAA) {
+        shader.append(gVS_Header_Varyings_IsAA);
     }
     if (description.hasGradient) {
         shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
@@ -421,8 +430,8 @@
         if (description.hasExternalTexture) {
             shader.append(gVS_Main_OutTransformedTexCoords);
         }
-        if (description.hasWidth) {
-            shader.append(gVS_Main_Width);
+        if (description.isAA) {
+            shader.append(gVS_Main_AA);
         }
         if (description.hasGradient) {
             shader.append(gVS_Main_OutGradient[description.gradientType]);
@@ -464,8 +473,8 @@
     if (description.hasTexture || description.hasExternalTexture) {
         shader.append(gVS_Header_Varyings_HasTexture);
     }
-    if (description.hasWidth) {
-        shader.append(gVS_Header_Varyings_HasWidth);
+    if (description.isAA) {
+        shader.append(gVS_Header_Varyings_IsAA);
     }
     if (description.hasGradient) {
         shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
@@ -491,8 +500,8 @@
     if (description.hasExternalTexture) {
         shader.append(gFS_Uniforms_ExternalTextureSampler);
     }
-    if (description.hasWidth) {
-        shader.append(gFS_Uniforms_Width);
+    if (description.isAA) {
+        shader.append(gFS_Uniforms_AA);
     }
     if (description.hasGradient) {
         shader.append(gFS_Uniforms_GradientSampler[description.gradientType]);
@@ -502,7 +511,7 @@
     }
 
     // Optimization for common cases
-    if (!description.hasWidth && !blendFramebuffer &&
+    if (!description.isAA && !blendFramebuffer &&
             description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
         bool fast = false;
 
@@ -587,8 +596,8 @@
                 shader.append(gFS_Main_FetchColor);
             }
         }
-        if (description.hasWidth) {
-            shader.append(gFS_Main_AccountForWidth);
+        if (description.isAA) {
+            shader.append(gFS_Main_AccountForAA);
         }
         if (description.hasGradient) {
             shader.append(gFS_Main_FetchGradient[description.gradientType]);