Fixing a timeout issue, added a target workload and FullPipeline benchmark.

Change-Id: I7ba15060d6db1e48d033deae0150731bf949ad55
diff --git a/suite/pts/deviceTests/opengl/jni/contextswitch/ContextSwitchRenderer.cpp b/suite/pts/deviceTests/opengl/jni/contextswitch/ContextSwitchRenderer.cpp
index df31cbc..d64ca3f 100644
--- a/suite/pts/deviceTests/opengl/jni/contextswitch/ContextSwitchRenderer.cpp
+++ b/suite/pts/deviceTests/opengl/jni/contextswitch/ContextSwitchRenderer.cpp
@@ -26,14 +26,17 @@
 static const EGLint contextAttribs[] =
         { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
 
-static const float csVertices[] = {
+static const int CS_NUM_VERTICES = 6;
+
+static const float CS_VERTICES[CS_NUM_VERTICES * 3] = {
         1.0f, 1.0f, -1.0f,
         -1.0f, 1.0f, -1.0f,
         -1.0f, -1.0f, -1.0f,
         -1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f, 1.0f, -1.0f };
-static const float csTexCoords[] = {
+
+static const float CS_TEX_COORDS[CS_NUM_VERTICES * 2] = {
         1.0f, 1.0f,
         0.0f, 1.0f,
         0.0f, 0.0f,
@@ -41,7 +44,7 @@
         1.0f, 0.0f,
         1.0f, 1.0f };
 
-static const char* csVertex =
+static const char* CS_VERTEX =
         "attribute vec4 a_Position;"
         "attribute vec2 a_TexCoord;"
         "varying vec2 v_TexCoord;"
@@ -50,7 +53,7 @@
         "  gl_Position = a_Position;"
         "}";
 
-static const char* csFragment =
+static const char* CS_FRAGMENT =
         "precision mediump float;"
         "uniform sampler2D u_Texture;"
         "varying vec2 v_TexCoord;"
@@ -96,7 +99,7 @@
     }
 
     // Create program.
-    mProgram = GLUtils::createProgram(&csVertex, &csFragment);
+    mProgram = GLUtils::createProgram(&CS_VERTEX, &CS_FRAGMENT);
     if (mProgram == 0)
         return false;
     // Bind attributes.
@@ -142,11 +145,11 @@
         glEnableVertexAttribArray(mPositionHandle);
         glEnableVertexAttribArray(mTexCoordHandle);
         glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, false, 0,
-                csVertices);
+                CS_VERTICES);
         glVertexAttribPointer(mTexCoordHandle, 2, GL_FLOAT, false, 0,
-                csTexCoords);
+                CS_TEX_COORDS);
 
-        glDrawArrays(GL_TRIANGLES, 0, 6);
+        glDrawArrays(GL_TRIANGLES, 0, CS_NUM_VERTICES);
     }
     return eglSwapBuffers(mEglDisplay, mEglSurface);
 }
diff --git a/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineMesh.cpp b/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineMesh.cpp
index 895df9f..097dcdd 100644
--- a/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineMesh.cpp
+++ b/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineMesh.cpp
@@ -30,9 +30,12 @@
     glUniform1i(prog.mTextureUniformHandle, 0);
 
     glEnableVertexAttribArray(prog.mPositionHandle);
+    glEnableVertexAttribArray(prog.mNormalHandle);
     glEnableVertexAttribArray(prog.mTexCoordHandle);
     glVertexAttribPointer(prog.mPositionHandle, 3, GL_FLOAT, false, 0,
             mMesh->mVertices);
+    glVertexAttribPointer(prog.mNormalHandle, 3, GL_FLOAT, false, 0,
+            mMesh->mNormals);
     glVertexAttribPointer(prog.mTexCoordHandle, 2, GL_FLOAT, false, 0,
             mMesh->mTexCoords);
 
diff --git a/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineRenderer.cpp b/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineRenderer.cpp
index 1769701..f85a173 100644
--- a/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineRenderer.cpp
+++ b/suite/pts/deviceTests/opengl/jni/fullpipeline/FullPipelineRenderer.cpp
@@ -11,7 +11,7 @@
  * or implied. See the License for the specific language governing permissions and limitations under
  * the License.
  */
-
+#include <math.h>
 #include <EGL/egl.h>
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
@@ -23,21 +23,25 @@
 #include <graphics/TransformationNode.h>
 #include <GLUtils.h>
 
-static const float fullVertices[] = {
-        1.0f, 1.0f, -1.0f,
-        -1.0f, 1.0f, -1.0f,
-        -1.0f, -1.0f, -1.0f,
-        -1.0f, -1.0f, -1.0f,
-        1.0f, -1.0f, -1.0f,
-        1.0f, 1.0f, -1.0f };
-static const float fullNormals[] = {
+static const int FP_NUM_VERTICES = 6;
+
+static const float FP_VERTICES[FP_NUM_VERTICES * 3] = {
+        1.0f, 1.0f, 0.0f,
+        0.0f, 1.0f, 0.0f,
+        0.0f, 0.0f, 0.0f,
+        0.0f, 0.0f, 0.0f,
+        1.0f, 0.0f, 0.0f,
+        1.0f, 1.0f, 0.0f };
+
+static const float FP_NORMALS[FP_NUM_VERTICES * 3] = {
         0.0f, 0.0f, 1.0f,
         0.0f, 0.0f, 1.0f,
         0.0f, 0.0f, 1.0f,
         0.0f, 0.0f, 1.0f,
         0.0f, 0.0f, 1.0f,
         0.0f, 0.0f, 1.0f };
-static const float fullTexCoords[] = {
+
+static const float FP_TEX_COORDS[FP_NUM_VERTICES * 2] = {
         1.0f, 1.0f,
         0.0f, 1.0f,
         0.0f, 0.0f,
@@ -45,7 +49,7 @@
         1.0f, 0.0f,
         1.0f, 1.0f };
 
-static const char* fullVertex =
+static const char* FP_VERTEX =
         "uniform mat4 u_MVPMatrix;"
         "uniform mat4 u_MVMatrix;"
         "attribute vec4 a_Position;"
@@ -65,7 +69,7 @@
         "  gl_Position = u_MVPMatrix * a_Position;\n"
         "}";
 
-static const char* fullFragment =
+static const char* FP_FRAGMENT =
         "precision mediump float;"
         "uniform vec3 u_LightPos;"
         "uniform sampler2D u_Texture;"
@@ -96,7 +100,7 @@
     if (!Renderer::setUp()) {
         return false;
     }
-    GLuint programId = GLUtils::createProgram(&fullVertex, &fullFragment);
+    GLuint programId = GLUtils::createProgram(&FP_VERTEX, &FP_FRAGMENT);
     if (programId == 0)
         return false;
     mProgram = new FullPipelineProgram(programId);
@@ -107,15 +111,12 @@
     // Use culling to remove back faces.
     glEnable (GL_CULL_FACE);
 
-    // Enable depth testing
-    glEnable (GL_DEPTH_TEST);
-
     mModelMatrix = new Matrix();
 
     // Position the eye in front of the origin.
     float eyeX = 0.0f;
     float eyeY = 0.0f;
-    float eyeZ = 6.0f;
+    float eyeZ = 2.0f;
 
     // We are looking at the origin
     float centerX = 0.0f;
@@ -139,7 +140,7 @@
     float bottom = -1.0f;
     float top = 1.0f;
     float near = 1.0f;
-    float far = 10.0f;
+    float far = 3.0f;
 
     mProjectionMatrix = Matrix::newFrustum(left, right, bottom, top, near, far);
 
@@ -148,15 +149,22 @@
         return false;
     }
 
+    float count = pow(2, mWorkload-1);
+    float middle = count / 2.0f;
+    float scale = 1.0f / count;
+
+    mMesh = new Mesh(FP_VERTICES, FP_NORMALS, FP_TEX_COORDS, FP_NUM_VERTICES, textureId);
     mSceneGraph = new ProgramNode();
-    mMesh = new Mesh(fullVertices, fullNormals, fullTexCoords, 6, textureId);
-    for (int i = 0; i < mWorkload; i++) {
-        Matrix* transformMatrix = Matrix::newRotate(45.0f, 0.0f, 1.0f, 0.0f);
-        TransformationNode* transformNode = new TransformationNode(
-                transformMatrix);
-        mSceneGraph->addChild(transformNode);
-        FullPipelineMesh* meshNode = new FullPipelineMesh(mMesh);
-        transformNode->addChild(meshNode);
+
+    for (int i = 0; i < count; i++) {
+        for (int j = 0; j < count; j++) {
+            Matrix* transformMatrix = Matrix::newScale(scale, scale, scale);
+            transformMatrix->translate(i - middle, j - middle, 0.0f);
+            TransformationNode* transformNode = new TransformationNode(transformMatrix);
+            mSceneGraph->addChild(transformNode);
+            FullPipelineMesh* meshNode = new FullPipelineMesh(mMesh);
+            transformNode->addChild(meshNode);
+        }
     }
     return true;
 }
@@ -183,7 +191,6 @@
 bool FullPipelineRenderer::draw() {
     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
     mModelMatrix->identity();
-    mSceneGraph->draw(*mProgram, *mModelMatrix, *mViewMatrix,
-            *mProjectionMatrix);
+    mSceneGraph->draw(*mProgram, *mModelMatrix, *mViewMatrix, *mProjectionMatrix);
     return eglSwapBuffers(mEglDisplay, mEglSurface);
 }
diff --git a/suite/pts/deviceTests/opengl/jni/graphics/Matrix.cpp b/suite/pts/deviceTests/opengl/jni/graphics/Matrix.cpp
index a23326b..83591e8 100644
--- a/suite/pts/deviceTests/opengl/jni/graphics/Matrix.cpp
+++ b/suite/pts/deviceTests/opengl/jni/graphics/Matrix.cpp
@@ -16,6 +16,10 @@
 #include <string.h>
 #include <math.h>
 
+#define LOG_TAG "PTS_OPENGL"
+#define LOG_NDEBUG 0
+#include "utils/Log.h"
+
 Matrix::Matrix() {
     identity();
 }
@@ -24,6 +28,14 @@
     loadWith(src);
 }
 
+void Matrix::print(const char* label) {
+    ALOGI("%c", *label);
+    for (int i = 0; i < 4; i++) {
+        const float* d = &(mData[i * 4]);
+        ALOGI("%f %f %f %f\n", d[0], d[1], d[2], d[3]);
+    }
+}
+
 bool Matrix::equals(const Matrix& src) {
     bool equals = true;
     const float* d = src.mData;
@@ -214,9 +226,9 @@
     Matrix* m = new Matrix();
     if (m != NULL) {
         float* d = m->mData;
-        d[3] = x;
-        d[7] = y;
-        d[11] = z;
+        d[12] = x;
+        d[13] = y;
+        d[14] = z;
     }
     return m;
 }
diff --git a/suite/pts/deviceTests/opengl/jni/graphics/Matrix.h b/suite/pts/deviceTests/opengl/jni/graphics/Matrix.h
index 1586ed0..3484266 100644
--- a/suite/pts/deviceTests/opengl/jni/graphics/Matrix.h
+++ b/suite/pts/deviceTests/opengl/jni/graphics/Matrix.h
@@ -35,6 +35,8 @@
     // Sets this matrix to be the result of multiplying the given matrices.
     void multiply(const Matrix& l, const Matrix& r);
 
+    void print(const char* label);
+
     // Returns a new matrix representing the camera.
     static Matrix* newLookAt(float eyeX, float eyeY, float eyeZ, float centerX,
             float centerY, float centerZ, float upX, float upY, float upZ);
diff --git a/suite/pts/deviceTests/opengl/jni/pixeloutput/PixelOutputRenderer.cpp b/suite/pts/deviceTests/opengl/jni/pixeloutput/PixelOutputRenderer.cpp
index 968fa26..c97b860 100644
--- a/suite/pts/deviceTests/opengl/jni/pixeloutput/PixelOutputRenderer.cpp
+++ b/suite/pts/deviceTests/opengl/jni/pixeloutput/PixelOutputRenderer.cpp
@@ -14,14 +14,16 @@
 #include "PixelOutputRenderer.h"
 #include <GLUtils.h>
 
-static const float poVertices[] = {
+static const int PO_NUM_VERTICES = 6;
+
+static const float PO_VERTICES[PO_NUM_VERTICES * 3] = {
         1.0f, 1.0f, -1.0f,
         -1.0f, 1.0f, -1.0f,
         -1.0f, -1.0f, -1.0f,
         -1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f, 1.0f, -1.0f };
-static const float poTexCoords[] = {
+static const float PO_TEX_COORDS[PO_NUM_VERTICES * 2] = {
         1.0f, 1.0f,
         0.0f, 1.0f,
         0.0f, 0.0f,
@@ -29,7 +31,7 @@
         1.0f, 0.0f,
         1.0f, 1.0f };
 
-static const char* poVertex =
+static const char* PO_VERTEX =
         "attribute vec4 a_Position;"
         "attribute vec2 a_TexCoord;"
         "varying vec2 v_TexCoord;"
@@ -38,7 +40,7 @@
         "  gl_Position = a_Position;"
         "}";
 
-static const char* poFragment =
+static const char* PO_FRAGMENT =
         "precision mediump float;"
         "uniform sampler2D u_Texture;"
         "varying vec2 v_TexCoord;"
@@ -56,7 +58,7 @@
     }
 
     // Create program.
-    mProgram = GLUtils::createProgram(&poVertex, &poFragment);
+    mProgram = GLUtils::createProgram(&PO_VERTEX, &PO_FRAGMENT);
     if (mProgram == 0)
         return false;
     // Bind attributes.
@@ -97,11 +99,11 @@
 
     glEnableVertexAttribArray(mPositionHandle);
     glEnableVertexAttribArray(mTexCoordHandle);
-    glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, false, 0, poVertices);
-    glVertexAttribPointer(mTexCoordHandle, 2, GL_FLOAT, false, 0, poTexCoords);
+    glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, false, 0, PO_VERTICES);
+    glVertexAttribPointer(mTexCoordHandle, 2, GL_FLOAT, false, 0, PO_TEX_COORDS);
 
     for (int i = 0; i < mWorkload; i++) {
-        glDrawArrays(GL_TRIANGLES, 0, 6);
+        glDrawArrays(GL_TRIANGLES, 0, PO_NUM_VERTICES);
     }
 
     return eglSwapBuffers(mEglDisplay, mEglSurface);
diff --git a/suite/pts/deviceTests/opengl/jni/shaderperf/ShaderPerfRenderer.cpp b/suite/pts/deviceTests/opengl/jni/shaderperf/ShaderPerfRenderer.cpp
index a7f07ba..11ca0a4 100644
--- a/suite/pts/deviceTests/opengl/jni/shaderperf/ShaderPerfRenderer.cpp
+++ b/suite/pts/deviceTests/opengl/jni/shaderperf/ShaderPerfRenderer.cpp
@@ -14,7 +14,9 @@
 #include "ShaderPerfRenderer.h"
 #include <GLUtils.h>
 
-static const float spVertices[] = {
+static const int SP_NUM_VERTICES = 6;
+
+static const float SP_VERTICES[SP_NUM_VERTICES * 3] = {
         1.0f, 1.0f, -1.0f,
         -1.0f, 1.0f, -1.0f,
         -1.0f, -1.0f, -1.0f,
@@ -22,7 +24,7 @@
         1.0f, -1.0f, -1.0f,
         1.0f, 1.0f, -1.0f };
 
-static const char* spVertex = "attribute vec4 a_Position;"
+static const char* SP_VERTEX = "attribute vec4 a_Position;"
                               "varying vec4 v_Position;"
                               "void main() {"
                               "  v_Position = a_Position;"
@@ -30,7 +32,7 @@
                               "}";
 
 // TODO At the moment this a very simple shader. Later on this will get more complex.
-static const char* spFragment = "precision mediump float;"
+static const char* SP_FRAGMENT = "precision mediump float;"
                                 "varying vec4 v_Position;"
                                 "void main() {"
                                 "  gl_FragColor = v_Position;"
@@ -45,7 +47,7 @@
         return false;
     }
     // Create program.
-    mProgram = GLUtils::createProgram(&spVertex, &spFragment);
+    mProgram = GLUtils::createProgram(&SP_VERTEX, &SP_FRAGMENT);
     if (mProgram == 0)
         return false;
     // Bind attributes.
@@ -65,9 +67,9 @@
     glDisable (GL_DEPTH_TEST);
 
     glEnableVertexAttribArray(mPositionHandle);
-    glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, false, 0, spVertices);
+    glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, false, 0, SP_VERTICES);
 
-    glDrawArrays(GL_TRIANGLES, 0, 6);
+    glDrawArrays(GL_TRIANGLES, 0, SP_NUM_VERTICES);
 
     return eglSwapBuffers(mEglDisplay, mEglSurface);
 }
diff --git a/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLActivity.java b/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLActivity.java
index 0caa576..5f75cbb 100644
--- a/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLActivity.java
+++ b/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLActivity.java
@@ -119,7 +119,6 @@
         public void run() {
             // Creates a watchdog to ensure a iteration doesn't exceed the timeout.
             watchDog = new WatchDog(mTimeout);
-            watchDog.start();
             // Used to record the start and end time of the iteration.
             double[] times = new double[2];
             while (repeat) {
@@ -140,8 +139,11 @@
                         setupContextSwitchBenchmark(mSurface, wl);
                         break;
                 }
+                watchDog.start();
+                boolean success = startBenchmark(mNumFrames, times);
+                watchDog.stop();
                 // Start benchmark.
-                if (!startBenchmark(mNumFrames, times)) {
+                if (!success) {
                     mException = new Exception("Could not run benchmark");
                     repeat = false;
                 } else {
@@ -149,7 +151,7 @@
                     double totalTimeTaken = times[1] - times[0];
                     double meanFps = mNumFrames * 1000.0f / totalTimeTaken;
                     Log.i(TAG, "Workload: " + wl);
-                    Log.i(TAG, "MeanFPS: " + meanFps);
+                    Log.i(TAG, "Mean FPS: " + meanFps);
                     if (meanFps >= mMinFps) {
                         // Iteration passed, proceed to next one.
                         mWorkload++;
@@ -158,10 +160,7 @@
                         repeat = false;
                     }
                 }
-                // Resets the watchdog for the next iteration.
-                watchDog.reset();
             }
-            watchDog.stop();
         }
 
     }
diff --git a/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLBenchmark.java b/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLBenchmark.java
index ad3159f..e944b9a 100644
--- a/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLBenchmark.java
+++ b/suite/pts/deviceTests/opengl/src/com/android/pts/opengl/primitive/GLBenchmark.java
@@ -19,6 +19,9 @@
 import com.android.pts.util.ResultType;
 import com.android.pts.util.ResultUnit;
 
+import android.opengl.Matrix;
+import android.util.Log;
+import java.util.Arrays;
 /**
  * Runs the Primitive OpenGL ES 2.0 Benchmarks.
  */
@@ -36,7 +39,7 @@
      * @throws Exception If the benchmark could not be run.
      */
     public void testFullPipeline() throws Exception {
-        runBenchmark(Benchmark.FullPipeline, 500, 10000);
+        runBenchmark(Benchmark.FullPipeline, 500, 50000, 1);
     }
 
     /**
@@ -45,7 +48,7 @@
      * @throws Exception If the benchmark could not be run.
      */
     public void testPixelOutput() throws Exception {
-        runBenchmark(Benchmark.PixelOutput, 500, 10000);
+        runBenchmark(Benchmark.PixelOutput, 500, 50000, 1);
     }
 
     /**
@@ -55,7 +58,7 @@
      */
     public void testShaderPerf() throws Exception {
         // TODO(stuartscott): Not yet implemented
-        // runBenchmark(Benchmark.ShaderPerf, 500, 10000);
+        // runBenchmark(Benchmark.ShaderPerf, 500, 50000, 1);
     }
 
     /**
@@ -64,7 +67,7 @@
      * @throws Exception If the benchmark could not be run.
      */
     public void testContextSwitch() throws Exception {
-        runBenchmark(Benchmark.ContextSwitch, 500, 10000);
+        runBenchmark(Benchmark.ContextSwitch, 500, 50000, 1);
     }
 
     /**
@@ -75,7 +78,8 @@
      * @param timeout The milliseconds to wait for an iteration of the benchmark before timing out.
      * @throws Exception If the benchmark could not be run.
      */
-    private void runBenchmark(Benchmark benchmark, int numFrames, int timeout) throws Exception {
+    private void runBenchmark(Benchmark benchmark, int numFrames, int timeout, int target)
+            throws Exception {
         String benchmarkName = benchmark.toString();
         Intent intent = new Intent();
         intent.putExtra(GLActivity.INTENT_EXTRA_BENCHMARK_NAME, benchmarkName);
@@ -87,8 +91,12 @@
         setActivityIntent(intent);
         try {
             activity = getActivity();
+            // Represents the maximum workload it can do whilst maintaining MIN_FPS.
             int workload = activity.waitForCompletion();
-            // represents the maximum workload it can do whilst maintaining MIN_FPS.
+            if (workload < target) {
+                throw new Exception("Benchmark did not reach target. Got " + workload
+                        + ", target was " + target);
+            }
             getReportLog()
                     .printSummary("Workload", workload, ResultType.HIGHER_BETTER, ResultUnit.SCORE);
         } finally {
diff --git a/suite/pts/deviceTests/opengl/test/MatrixTest.cpp b/suite/pts/deviceTests/opengl/test/MatrixTest.cpp
index b94f133..d78dfc4 100644
--- a/suite/pts/deviceTests/opengl/test/MatrixTest.cpp
+++ b/suite/pts/deviceTests/opengl/test/MatrixTest.cpp
@@ -157,8 +157,8 @@
     float expected[] = {
         1.0f, 0.0f, 0.0f, 0.0f,
         0.0f, 1.0f, 0.0f, 0.0f,
-        0.0f, 0.0f, 1.0f, -6.0f,
-        0.0f, 0.0f, 0.0f, 1.0f};
+        0.0f, 0.0f, 1.0f, 0.0f,
+        0.0f, 0.0f, -6.0f, 1.0f};
     // Check values
     checkValues(m->mData, expected, Matrix::MATRIX_SIZE);
     delete m;
@@ -189,10 +189,10 @@
     Matrix* m = Matrix::newTranslate(5, 6, 8);
     ASSERT_TRUE(m != NULL);
     float expected[] = {
-        1.0f, 0.0f, 0.0f, 5.0f,
-        0.0f, 1.0f, 0.0f, 6.0f,
-        0.0f, 0.0f, 1.0f, 8.0f,
-        0.0f, 0.0f, 0.0f, 1.0f};
+        1.0f, 0.0f, 0.0f, 0.0f,
+        0.0f, 1.0f, 0.0f, 0.0f,
+        0.0f, 0.0f, 1.0f, 0.0f,
+        5.0f, 6.0f, 8.0f, 1.0f};
     // Check values
     checkValues(m->mData, expected, Matrix::MATRIX_SIZE);
     delete m;