Switch to cos interpolation of shadow alpha

bug:16852257

Updates default shadow opacities to compensate.

Also, update variable/constant naming related to vertex alpha.

Change-Id: I9055b4ac3c9ac305ca9d515f21b52d6aa6dc9c5c
diff --git a/core/res/res/values/styles.xml b/core/res/res/values/styles.xml
index 35baf9c..8fc9bac 100644
--- a/core/res/res/values/styles.xml
+++ b/core/res/res/values/styles.xml
@@ -1360,7 +1360,7 @@
         <item name="lightZ">800dp</item>
         <item name="lightRadius">800dp</item>
         <item name="ambientShadowAlpha">0.06</item>
-        <item name="spotShadowAlpha">0.22</item>
+        <item name="spotShadowAlpha">0.16</item>
     </style>
 
 </resources>
diff --git a/libs/hwui/AmbientShadow.cpp b/libs/hwui/AmbientShadow.cpp
index 181230a..9cc83ed 100644
--- a/libs/hwui/AmbientShadow.cpp
+++ b/libs/hwui/AmbientShadow.cpp
@@ -117,10 +117,13 @@
 
         // inner ring of points
         float opacity = 1.0 / (1 + rayHeight[rayIndex] * heightFactor);
+        // NOTE: Shadow alpha values are transformed when stored in alphavertices,
+        // so that they can be consumed directly by gFS_Main_ApplyVertexAlphaShadowInterp
+        float transformedOpacity = acos(1.0f - 2.0f * opacity);
         AlphaVertex::set(&shadowVertices[rays + rayIndex],
                 intersection.x,
                 intersection.y,
-                opacity);
+                transformedOpacity);
     }
 
     if (isCasterOpaque) {
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 7123bfe..d1522b6 100755
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -1641,9 +1641,9 @@
     mCaches.disableTexCoordsVertexArray();
 }
 
-void OpenGLRenderer::setupDrawAA(bool useShadowInterp) {
-    mDescription.isAA = true;
-    mDescription.isShadowAA = useShadowInterp;
+void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
+    mDescription.hasVertexAlpha = true;
+    mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
 }
 
 void OpenGLRenderer::setupDrawColor(int color, int alpha) {
@@ -2382,7 +2382,7 @@
 
     setupDraw();
     setupDrawNoTexture();
-    if (isAA) setupDrawAA((displayFlags & kVertexBuffer_ShadowAA));
+    if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
     setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
     setupDrawColorFilter(getColorFilter(paint));
     setupDrawShader(getShader(paint));
@@ -3168,12 +3168,12 @@
 
     if (ambientShadowVertexBuffer && mAmbientShadowAlpha > 0) {
         paint.setARGB(casterAlpha * mAmbientShadowAlpha, 0, 0, 0);
-        drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowAA);
+        drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
     }
 
     if (spotShadowVertexBuffer && mSpotShadowAlpha > 0) {
         paint.setARGB(casterAlpha * mSpotShadowAlpha, 0, 0, 0);
-        drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowAA);
+        drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
     }
 
     return DrawGlInfo::kStatusDrew;
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 2a9badd..fc95c18 100755
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -96,7 +96,7 @@
 
 enum VertexBufferDisplayFlags {
     kVertexBuffer_Offset = 0x1,
-    kVertexBuffer_ShadowAA = 0x2,
+    kVertexBuffer_ShadowInterp = 0x2,
 };
 
 /**
@@ -847,7 +847,7 @@
     void setupDrawWithTextureAndColor(bool isAlpha8 = false);
     void setupDrawWithExternalTexture();
     void setupDrawNoTexture();
-    void setupDrawAA(bool useShadowInterp);
+    void setupDrawVertexAlpha(bool useShadowAlphaInterp);
     void setupDrawColor(int color, int alpha);
     void setupDrawColor(float r, float g, float b, float a);
     void setupDrawAlpha8Color(int color, int alpha);
diff --git a/libs/hwui/Program.h b/libs/hwui/Program.h
index 1d95c40..56773f4 100644
--- a/libs/hwui/Program.h
+++ b/libs/hwui/Program.h
@@ -71,8 +71,8 @@
 #define PROGRAM_GRADIENT_TYPE_SHIFT 33 // 2 bits for gradient type
 #define PROGRAM_MODULATE_SHIFT 35
 
-#define PROGRAM_HAS_AA_SHIFT 36
-#define PROGRAM_HAS_SHADOW_AA_SHIFT 37
+#define PROGRAM_HAS_VERTEX_ALPHA_SHIFT 36
+#define PROGRAM_USE_SHADOW_ALPHA_INTERP_SHIFT 37
 
 #define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 38
 #define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 39
@@ -135,8 +135,8 @@
     bool hasBitmap;
     bool isBitmapNpot;
 
-    bool isAA; // drawing with a per-vertex alpha
-    bool isShadowAA; // drawing per vertex alpha with shadow interpolation
+    bool hasVertexAlpha;
+    bool useShadowAlphaInterp;
 
     bool hasGradient;
     Gradient gradientType;
@@ -176,8 +176,8 @@
 
         hasColors = false;
 
-        isAA = false;
-        isShadowAA = false;
+        hasVertexAlpha = false;
+        useShadowAlphaInterp = false;
 
         modulate = false;
 
@@ -264,8 +264,8 @@
         key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
         if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST;
         if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT;
-        if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT;
-        if (isShadowAA) key |= programid(0x1) << PROGRAM_HAS_SHADOW_AA_SHIFT;
+        if (hasVertexAlpha) key |= programid(0x1) << PROGRAM_HAS_VERTEX_ALPHA_SHIFT;
+        if (useShadowAlphaInterp) key |= programid(0x1) << PROGRAM_USE_SHADOW_ALPHA_INTERP_SHIFT;
         if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
         if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
         if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION;
diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp
index 2dd89b8..c802b18 100644
--- a/libs/hwui/ProgramCache.cpp
+++ b/libs/hwui/ProgramCache.cpp
@@ -46,7 +46,7 @@
         "attribute vec2 texCoords;\n";
 const char* gVS_Header_Attributes_Colors =
         "attribute vec4 colors;\n";
-const char* gVS_Header_Attributes_AAVertexShapeParameters =
+const char* gVS_Header_Attributes_VertexAlphaParameters =
         "attribute float vtxAlpha;\n";
 const char* gVS_Header_Uniforms_TextureTransform =
         "uniform mat4 mainTextureTransform;\n";
@@ -64,7 +64,7 @@
         "varying vec2 outTexCoords;\n";
 const char* gVS_Header_Varyings_HasColors =
         "varying vec4 outColors;\n";
-const char* gVS_Header_Varyings_IsAAVertexShape =
+const char* gVS_Header_Varyings_HasVertexAlpha =
         "varying float alpha;\n";
 const char* gVS_Header_Varyings_HasBitmap =
         "varying highp vec2 outBitmapTexCoords;\n";
@@ -122,9 +122,7 @@
         "    vec4 transformedPosition = projection * transform * position;\n"
         "    gl_Position = transformedPosition;\n";
 
-const char* gVS_Main_ShadowAAVertexShape =
-        "    alpha = pow(vtxAlpha, 0.667);\n";
-const char* gVS_Main_AAVertexShape =
+const char* gVS_Main_VertexAlpha =
         "    alpha = vtxAlpha;\n";
 
 const char* gVS_Main_HasRoundRectClip =
@@ -239,10 +237,10 @@
         "    fragColor = color;\n";
 const char* gFS_Main_ModulateColor =
         "    fragColor *= color.a;\n";
-const char* gFS_Main_AccountForAAVertexShape =
+const char* gFS_Main_ApplyVertexAlphaLinearInterp =
         "    fragColor *= alpha;\n";
-const char* gFS_Main_AccountForShadowAAVertexShape =
-        "    fragColor *= pow(alpha, 1.5);\n";
+const char* gFS_Main_ApplyVertexAlphaShadowInterp =
+        "    fragColor *= (1.0 - cos(alpha)) / 2.0;\n";
 
 const char* gFS_Main_FetchTexture[2] = {
         // Don't modulate
@@ -473,8 +471,8 @@
     if (description.hasTexture || description.hasExternalTexture) {
         shader.append(gVS_Header_Attributes_TexCoords);
     }
-    if (description.isAA) {
-        shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
+    if (description.hasVertexAlpha) {
+        shader.append(gVS_Header_Attributes_VertexAlphaParameters);
     }
     if (description.hasColors) {
         shader.append(gVS_Header_Attributes_Colors);
@@ -497,8 +495,8 @@
     if (description.hasTexture || description.hasExternalTexture) {
         shader.append(gVS_Header_Varyings_HasTexture);
     }
-    if (description.isAA) {
-        shader.append(gVS_Header_Varyings_IsAAVertexShape);
+    if (description.hasVertexAlpha) {
+        shader.append(gVS_Header_Varyings_HasVertexAlpha);
     }
     if (description.hasColors) {
         shader.append(gVS_Header_Varyings_HasColors);
@@ -520,12 +518,8 @@
         } else if (description.hasTexture || description.hasExternalTexture) {
             shader.append(gVS_Main_OutTexCoords);
         }
-        if (description.isAA) {
-            if (description.isShadowAA) {
-                shader.append(gVS_Main_ShadowAAVertexShape);
-            } else {
-                shader.append(gVS_Main_AAVertexShape);
-            }
+        if (description.hasVertexAlpha) {
+            shader.append(gVS_Main_VertexAlpha);
         }
         if (description.hasColors) {
             shader.append(gVS_Main_OutColors);
@@ -575,8 +569,8 @@
     if (description.hasTexture || description.hasExternalTexture) {
         shader.append(gVS_Header_Varyings_HasTexture);
     }
-    if (description.isAA) {
-        shader.append(gVS_Header_Varyings_IsAAVertexShape);
+    if (description.hasVertexAlpha) {
+        shader.append(gVS_Header_Varyings_HasVertexAlpha);
     }
     if (description.hasColors) {
         shader.append(gVS_Header_Varyings_HasColors);
@@ -617,7 +611,7 @@
     }
 
     // Optimization for common cases
-    if (!description.isAA
+    if (!description.hasVertexAlpha
             && !blendFramebuffer
             && !description.hasColors
             && description.colorOp == ProgramDescription::kColorNone
@@ -759,11 +753,11 @@
         // Apply the color op if needed
         shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
 
-        if (description.isAA) {
-            if (description.isShadowAA) {
-                shader.append(gFS_Main_AccountForShadowAAVertexShape);
+        if (description.hasVertexAlpha) {
+            if (description.useShadowAlphaInterp) {
+                shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
             } else {
-                shader.append(gFS_Main_AccountForAAVertexShape);
+                shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
             }
         }
 
diff --git a/libs/hwui/SpotShadow.cpp b/libs/hwui/SpotShadow.cpp
index 8c3077b..d726538 100644
--- a/libs/hwui/SpotShadow.cpp
+++ b/libs/hwui/SpotShadow.cpp
@@ -754,9 +754,13 @@
     AlphaVertex* shadowVertices =
             shadowTriangleStrip.alloc<AlphaVertex>(SHADOW_VERTEX_COUNT);
 
+    // NOTE: Shadow alpha values are transformed when stored in alphavertices,
+    // so that they can be consumed directly by gFS_Main_ApplyVertexAlphaShadowInterp
+    float transformedMaxAlpha = M_PI;
+
     // Calculate the vertices (x, y, alpha) in the shadow area.
     AlphaVertex centroidXYA;
-    AlphaVertex::set(&centroidXYA, centroid.x, centroid.y, 1.0f);
+    AlphaVertex::set(&centroidXYA, centroid.x, centroid.y, transformedMaxAlpha);
     for (int rayIndex = 0; rayIndex < rays; rayIndex++) {
         float dx = cosf(step * rayIndex);
         float dy = sinf(step * rayIndex);
@@ -770,14 +774,16 @@
         // umbra ring
         float umbraDistance = umbraDistPerRay[rayIndex];
         AlphaVertex::set(&shadowVertices[rays + rayIndex],
-                dx * umbraDistance + centroid.x, dy * umbraDistance + centroid.y, 1.0f);
+                dx * umbraDistance + centroid.x,
+                dy * umbraDistance + centroid.y,
+                transformedMaxAlpha);
 
         // occluded umbra ring
         if (hasOccludedUmbraArea) {
             float occludedUmbraDistance = occludedUmbraDistPerRay[rayIndex];
             AlphaVertex::set(&shadowVertices[2 * rays + rayIndex],
                     dx * occludedUmbraDistance + centroid.x,
-                    dy * occludedUmbraDistance + centroid.y, 1.0f);
+                    dy * occludedUmbraDistance + centroid.y, transformedMaxAlpha);
         } else {
             // Put all vertices of the occluded umbra ring at the centroid.
             shadowVertices[2 * rays + rayIndex] = centroidXYA;