Fix orthographic shadows projection, simplify shadow reordering

Separate matrix passed to shadow system into two parts, one for
transforming the polygon XY points (using the actual draw matrix) and
a separate one which respects correct 4x4 3d rotations and
translations for determining Z values.

Change-Id: I7e30a84774a8709df6b2241e8f51fc5583648fe8
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 7002e26..83de772 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -3190,8 +3190,16 @@
     return drawColorRects(rects, count, paint, false, true, true);
 }
 
-status_t OpenGLRenderer::drawShadow(const mat4& casterTransform, float casterAlpha,
-        const SkPath* casterOutline) {
+static void mapPointFakeZ(Vector3& point, const mat4& transformXY, const mat4& transformZ) {
+    // map z coordinate with true 3d matrix
+    point.z = transformZ.mapZ(point);
+
+    // map x,y coordinates with draw/Skia matrix
+    transformXY.mapPoint(point.x, point.y);
+}
+
+status_t OpenGLRenderer::drawShadow(const mat4& casterTransformXY, const mat4& casterTransformZ,
+        float casterAlpha, const SkPath* casterOutline) {
     if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
 
     // TODO: use quickRejectWithScissor. For now, always force enable scissor.
@@ -3217,7 +3225,7 @@
     for (int i = 0; i < casterVertexCount; i++) {
         const Vertex& point2d = casterVertices2d[i];
         casterPolygon[i] = Vector3(point2d.x, point2d.y, 0);
-        casterTransform.mapPoint3d(casterPolygon[i]);
+        mapPointFakeZ(casterPolygon[i], casterTransformXY, casterTransformZ);
     }
 
     // map the centroid of the caster into 3d
@@ -3225,7 +3233,7 @@
             reinterpret_cast<const Vector2*>(casterVertices2d.array()),
             casterVertexCount);
     Vector3 centroid3d(centroid.x, centroid.y, 0);
-    casterTransform.mapPoint3d(centroid3d);
+    mapPointFakeZ(centroid3d, casterTransformXY, casterTransformZ);
 
     // draw caster's shadows
     if (mCaches.propertyAmbientShadowStrength > 0) {
@@ -3244,7 +3252,6 @@
         ShadowTessellator::tessellateSpotShadow(casterPolygon, casterVertexCount,
                 lightPosScale, *currentTransform(), getWidth(), getHeight(),
                 spotShadowVertexBuffer);
-
         drawVertexBuffer(kVertexBufferMode_Shadow, spotShadowVertexBuffer, &paint);
     }