Edge antialiasing for convex shapes in Ganesh

This patch implements edge antialiasing for convex shapes, using the fragment
shader to compare against the edge equations for each triangle.  Currently, it
only works for flat shaded primitives (i.e., it was not integrated into the
"active stages" path).  The skia.gyp changes cause this code to be compiled into
SampleApp, but do not enable the tesselated path by default.

Notes:  the SkOSWindow_Unix.cpp change is to silence a valgrind warning about
memcpy() with overlapping regions.  The GrBinHashKey change is to avoid running
a two-pass hash (GrProgramDesc is now 52 bytes or so, exceeding the 32 byte
default size).

Review URL:  http://codereview.appspot.com/4519054/



git-svn-id: http://skia.googlecode.com/svn/trunk@1314 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/src/GrGLProgram.cpp b/gpu/src/GrGLProgram.cpp
index 1c0db25..ce4e7db 100644
--- a/gpu/src/GrGLProgram.cpp
+++ b/gpu/src/GrGLProgram.cpp
@@ -54,6 +54,7 @@
 #define POS_ATTR_NAME "aPosition"
 #define COL_ATTR_NAME "aColor"
 #define COL_UNI_NAME "uColor"
+#define EDGES_UNI_NAME "uEdges"
 #define COL_FILTER_UNI_NAME "uColorFilter"
 
 static inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
@@ -269,6 +270,10 @@
         break;
     }
 
+    if (fProgramDesc.fUsesEdgeAA) {
+        segments.fFSUnis.append("uniform vec3 " EDGES_UNI_NAME "[6];\n");
+    }
+
     if (fProgramDesc.fEmitsPointSize){
         segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
     }
@@ -352,6 +357,23 @@
         }
 
     } else {
+        if (fProgramDesc.fUsesEdgeAA) {
+            // FIXME:  put the a's in a loop
+            segments.fFSCode.append(
+                "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n"
+                "\tfloat a0 = clamp(dot(uEdges[0], pos), 0.0, 1.0);\n"
+                "\tfloat a1 = clamp(dot(uEdges[1], pos), 0.0, 1.0);\n"
+                "\tfloat a2 = clamp(dot(uEdges[2], pos), 0.0, 1.0);\n"
+                "\tfloat a3 = clamp(dot(uEdges[3], pos), 0.0, 1.0);\n"
+                "\tfloat a4 = clamp(dot(uEdges[4], pos), 0.0, 1.0);\n"
+                "\tfloat a5 = clamp(dot(uEdges[5], pos), 0.0, 1.0);\n"
+                "\tfloat edgeAlpha = min(min(a0 * a1, a2 * a3), a4 * a5);\n");
+            if (inColor.size()) {
+                inColor.append(" * edgeAlpha");
+            } else {
+                inColor = "vec4(edgeAlpha)";
+            }
+        }
         // we may not have any incoming color
         const char * incomingColor = (inColor.size() ? inColor.c_str()
                 : "vec4(1,1,1,1)");
@@ -583,6 +605,14 @@
         GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
     }
 
+    if (fProgramDesc.fUsesEdgeAA) {
+        programData->fUniLocations.fEdgesUni = 
+            GR_GL(GetUniformLocation(progID, EDGES_UNI_NAME));
+        GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
+    } else {
+        programData->fUniLocations.fEdgesUni = kUnusedUniform;
+    }
+
     for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
         StageUniLocations& locations = programData->fUniLocations.fStages[s];
         if (fProgramDesc.fStages[s].fEnabled) {