Add TextureView stress test.

This uses a TextureView with a GL producer on another thread
that continuously produces frames, incl. heavy overrendering.

BUG=6055356,6034123

Change-Id: I49e629aaa7c94517a6637b290796812794383092
diff --git a/CtsTestCaseList.mk b/CtsTestCaseList.mk
index b4dc4dd..c4b892e 100644
--- a/CtsTestCaseList.mk
+++ b/CtsTestCaseList.mk
@@ -69,6 +69,7 @@
 	CtsTelephonyTestCases \
 	CtsTestStubs \
 	CtsTextTestCases \
+	CtsTextureViewTestCases \
 	CtsUtilTestCases \
 	CtsViewTestCases \
 	CtsWebkitTestCases \
diff --git a/development/ide/eclipse/.classpath b/development/ide/eclipse/.classpath
index 314dd50..e24e988 100644
--- a/development/ide/eclipse/.classpath
+++ b/development/ide/eclipse/.classpath
@@ -58,6 +58,7 @@
     <classpathentry kind="src" path="cts/tests/tests/speech/src"/>
     <classpathentry kind="src" path="cts/tests/tests/telephony/src"/>
     <classpathentry kind="src" path="cts/tests/tests/text/src"/>
+    <classpathentry kind="src" path="cts/tests/tests/textureview/src"/>
     <classpathentry kind="src" path="cts/tests/tests/util/src"/>
     <classpathentry kind="src" path="cts/tests/tests/view/src"/>
     <classpathentry kind="src" path="cts/tests/tests/webkit/src"/>
diff --git a/tests/tests/textureview/Android.mk b/tests/tests/textureview/Android.mk
new file mode 100644
index 0000000..d379e66
--- /dev/null
+++ b/tests/tests/textureview/Android.mk
@@ -0,0 +1,32 @@
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# don't include this package in any target
+LOCAL_MODULE_TAGS := optional
+# and when built explicitly put it in the data partition
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_JAVA_LIBRARIES := android.test.runner
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := CtsTextureViewTestCases
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/tests/tests/textureview/AndroidManifest.xml b/tests/tests/textureview/AndroidManifest.xml
new file mode 100644
index 0000000..0ed1d46
--- /dev/null
+++ b/tests/tests/textureview/AndroidManifest.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.cts.textureview"
+    >
+
+    <uses-feature android:glEsVersion="0x00020000" android:required="true" />
+    <uses-permission android:name="android.permission.GET_TASKS" />
+    <uses-permission android:name="android.permission.REORDER_TASKS" />
+    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
+
+    <instrumentation
+        android:targetPackage="com.android.cts.textureview"
+        android:name="android.test.InstrumentationTestRunner" />
+
+    <application
+        android:label="@string/app_name"
+        android:hardwareAccelerated="true">
+        <activity
+            android:name="android.textureview.cts.TextureViewTestActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+</manifest>
diff --git a/tests/tests/textureview/res/values/strings.xml b/tests/tests/textureview/res/values/strings.xml
new file mode 100644
index 0000000..f4d9f96
--- /dev/null
+++ b/tests/tests/textureview/res/values/strings.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <string name="app_name">TextureViewTest</string>
+</resources>
diff --git a/tests/tests/textureview/src/android/textureview/cts/GLProducerThread.java b/tests/tests/textureview/src/android/textureview/cts/GLProducerThread.java
new file mode 100644
index 0000000..3248597
--- /dev/null
+++ b/tests/tests/textureview/src/android/textureview/cts/GLProducerThread.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.textureview.cts;
+
+import android.graphics.SurfaceTexture;
+import android.opengl.GLUtils;
+
+import java.lang.Thread;
+import java.util.concurrent.Semaphore;
+
+import junit.framework.Assert;
+
+import javax.microedition.khronos.egl.EGL10;
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.egl.EGLContext;
+import javax.microedition.khronos.egl.EGLDisplay;
+import javax.microedition.khronos.egl.EGLSurface;
+import javax.microedition.khronos.opengles.GL;
+
+import static android.opengl.GLES20.*;
+
+public class GLProducerThread extends Thread {
+    private Thread mProducerThread;
+    private final int mFrames;
+    private final int mDelayMs;
+    private final Semaphore mSemaphore;
+    private final SurfaceTexture mSurfaceTexture;
+
+    private EGL10 mEgl;
+    private EGLDisplay mEglDisplay = EGL10.EGL_NO_DISPLAY;
+    private EGLContext mEglContext = EGL10.EGL_NO_CONTEXT;
+    private EGLSurface mEglSurface = EGL10.EGL_NO_SURFACE;
+    private GL mGl;
+
+    private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
+    private static final int EGL_OPENGL_ES2_BIT = 4;
+
+    GLProducerThread(SurfaceTexture surfaceTexture, int frames, int delayMs, Semaphore semaphore) {
+        mFrames = frames;
+        mDelayMs = delayMs;
+        mSemaphore = semaphore;
+        mSurfaceTexture = surfaceTexture;
+    }
+
+    private void initGL() {
+        mEgl = (EGL10) EGLContext.getEGL();
+
+        mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
+        if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
+            throw new RuntimeException("eglGetDisplay() failed "
+                                       + GLUtils.getEGLErrorString(mEgl.eglGetError()));
+        }
+
+        int[] version = new int[2];
+        if (!mEgl.eglInitialize(mEglDisplay, version)) {
+            throw new RuntimeException("eglInitialize() failed " +
+                                       GLUtils.getEGLErrorString(mEgl.eglGetError()));
+        }
+
+        int[] configAttribs = {
+            EGL10.EGL_BUFFER_SIZE, 32,
+            EGL10.EGL_ALPHA_SIZE, 8,
+            EGL10.EGL_BLUE_SIZE, 8,
+            EGL10.EGL_GREEN_SIZE, 8,
+            EGL10.EGL_RED_SIZE, 8,
+            EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
+            EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT,
+            EGL10.EGL_NONE
+        };
+
+        int[] numConfigs = new int[1];
+        EGLConfig[] configs = new EGLConfig[1];
+        if (!mEgl.eglChooseConfig(mEglDisplay, configAttribs, configs, 1, numConfigs) || numConfigs[0] == 0) {
+            throw new RuntimeException("eglChooseConfig() failed");
+        }
+
+        int[] contextAttribs = {
+            EGL_CONTEXT_CLIENT_VERSION, 2,
+            EGL10.EGL_NONE };
+
+        mEglContext = mEgl.eglCreateContext(mEglDisplay, configs[0], EGL10.EGL_NO_CONTEXT, contextAttribs);
+
+        mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, configs[0], mSurfaceTexture, null);
+
+        if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
+            int error = mEgl.eglGetError();
+            if (error == EGL10.EGL_BAD_NATIVE_WINDOW) {
+                throw new RuntimeException("eglCreateWindowSurface() returned EGL_BAD_NATIVE_WINDOW.");
+            }
+            throw new RuntimeException("eglCreateWindowSurface() failed "
+                                       + GLUtils.getEGLErrorString(error));
+        }
+
+        if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
+            throw new RuntimeException("eglMakeCurrent() failed "
+                                       + GLUtils.getEGLErrorString(mEgl.eglGetError()));
+        }
+
+        mGl = mEglContext.getGL();
+    }
+
+    void destroyGL() {
+        mEgl.eglDestroyContext(mEglDisplay, mEglContext);
+        mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
+        mEglContext = EGL10.EGL_NO_CONTEXT;
+        mEglSurface = EGL10.EGL_NO_SURFACE;
+    }
+
+    @Override
+    public void run() {
+        initGL();
+        final int numColors = 4;
+        final float[][] color =
+            { { 1.0f, 0.0f, 0.0f },
+              { 0.0f, 1.0f, 0.0f },
+              { 0.0f, 0.0f, 1.0f },
+              { 1.0f, 1.0f, 1.0f } };
+
+        for (int index = 0, frame = 0;
+             frame < mFrames; index = (index + 1) % numColors, frame++) {
+            glClearColor(color[index][0], color[index][1], color[index][2], 1.0f);
+            glClear(GL_COLOR_BUFFER_BIT);
+            mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
+            Assert.assertEquals(EGL10.EGL_SUCCESS, mEgl.eglGetError());
+            try {
+                sleep(mDelayMs);
+            } catch (InterruptedException e) {
+            }
+        }
+
+        mSemaphore.release();
+        destroyGL();
+    }
+}
diff --git a/tests/tests/textureview/src/android/textureview/cts/TextureViewTest.java b/tests/tests/textureview/src/android/textureview/cts/TextureViewTest.java
new file mode 100644
index 0000000..ee5ab71
--- /dev/null
+++ b/tests/tests/textureview/src/android/textureview/cts/TextureViewTest.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.textureview.cts;
+
+import android.test.ActivityInstrumentationTestCase2;
+
+public class TextureViewTest extends
+        ActivityInstrumentationTestCase2<TextureViewTestActivity> {
+
+    public TextureViewTest() {
+        super(TextureViewTestActivity.class);
+    }
+
+    public void testTextureViewStress48Hz() {
+        TextureViewTestActivity.mFrames = 600;
+        TextureViewTestActivity.mDelayMs = 1000/48;
+        if (!getActivity().waitForCompletion())
+            fail("Did not complete 48Hz test.");
+    }
+
+    public void testTextureViewStress60Hz() {
+        TextureViewTestActivity.mFrames = 600;
+        TextureViewTestActivity.mDelayMs = 1000/60;
+        if (!getActivity().waitForCompletion())
+            fail("Did not complete 60Hz test.");
+    }
+
+    public void testTextureViewStress70Hz()  {
+        TextureViewTestActivity.mFrames = 600;
+        TextureViewTestActivity.mDelayMs = 1000/70;
+        if (!getActivity().waitForCompletion())
+            fail("Did not complete 70Hz test.");
+    }
+
+    public void testTextureViewStress200Hz() {
+        TextureViewTestActivity.mFrames = 600;
+        TextureViewTestActivity.mDelayMs = 1000/200;
+        if (!getActivity().waitForCompletion())
+            fail("Did not complete 200Hz test.");
+    }
+
+}
diff --git a/tests/tests/textureview/src/android/textureview/cts/TextureViewTestActivity.java b/tests/tests/textureview/src/android/textureview/cts/TextureViewTestActivity.java
new file mode 100644
index 0000000..f2a698b
--- /dev/null
+++ b/tests/tests/textureview/src/android/textureview/cts/TextureViewTestActivity.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.textureview.cts;
+
+import android.animation.ObjectAnimator;
+import android.animation.AnimatorSet;
+import android.app.Activity;
+import android.graphics.SurfaceTexture;
+import android.os.Bundle;
+import android.view.TextureView;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import junit.framework.Assert;
+
+public class TextureViewTestActivity extends Activity implements TextureView.SurfaceTextureListener {
+    public static int mFrames = -1;
+    public static int mDelayMs = -1;
+
+    private TextureView mTexView;
+    private Thread mProducerThread;
+    private final Semaphore mSemaphore = new Semaphore(0);
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Assert.assertTrue(mFrames > 0);
+        Assert.assertTrue(mDelayMs > 0);
+        mTexView = new TextureView(this);
+        mTexView.setSurfaceTextureListener(this);
+        setContentView(mTexView);
+        ObjectAnimator rotate = ObjectAnimator.ofFloat(mTexView, "rotationY", 180);
+        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(mTexView, "alpha", 0.3f, 1f);
+        ObjectAnimator scaleY = ObjectAnimator.ofFloat(mTexView, "scaleY", 0.3f, 1f);
+        AnimatorSet animSet = new AnimatorSet();
+        animSet.play(rotate).with(fadeIn).with(scaleY);
+        animSet.setDuration(mFrames * mDelayMs);
+        animSet.start();
+    }
+
+    public Boolean waitForCompletion() {
+        Boolean success = false;
+        int timeout = mFrames * mDelayMs * 4;
+        try {
+            success = mSemaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+            Assert.fail();
+        }
+        return success;
+    }
+
+    @Override
+    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
+        mProducerThread = new GLProducerThread(surface, mFrames, mDelayMs, mSemaphore);
+        mProducerThread.start();
+    }
+
+    @Override
+    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
+    }
+
+    @Override
+    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
+        mProducerThread = null;
+        return true;
+    }
+
+    @Override
+    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
+    }
+}