Tweaks to CTS media tests

Minor tweaks:

- changed cleanup (again)
- moved entirely to EGL14

Change-Id: I8f61b0f45df2c10bec8e7aa54b48f2de42772ee2
diff --git a/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java b/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java
index be2dc54..c4a171e 100644
--- a/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java
+++ b/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java
@@ -31,8 +31,6 @@
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 
-import javax.microedition.khronos.opengles.GL10;
-
 
 /**
  * This test has three steps:
@@ -53,7 +51,6 @@
     private static final boolean WORK_AROUND_BUGS = false;  // avoid fatal codec bugs
     private static final boolean VERBOSE = false;           // lots of logging
     private static final boolean DEBUG_SAVE_FILE = false;   // save copy of encoded movie
-    private static final String DEBUG_FILE_NAME_BASE = "/sdcard/test.";
 
     // parameters for the encoder
     private static final String MIME_TYPE = "video/avc";    // H.264 Advanced Video Coding
@@ -766,7 +763,7 @@
                 y = mHeight / 4;
             }
 
-            GLES20.glReadPixels(x, y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelBuf);
+            GLES20.glReadPixels(x, y, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuf);
             int r = pixelBuf.get(0) & 0xff;
             int g = pixelBuf.get(1) & 0xff;
             int b = pixelBuf.get(2) & 0xff;
diff --git a/tests/tests/media/src/android/media/cts/InputSurface.java b/tests/tests/media/src/android/media/cts/InputSurface.java
index f23309d..5397295 100644
--- a/tests/tests/media/src/android/media/cts/InputSurface.java
+++ b/tests/tests/media/src/android/media/cts/InputSurface.java
@@ -39,9 +39,9 @@
 
     private static final int EGL_RECORDABLE_ANDROID = 0x3142;
 
-    private EGLDisplay mEGLDisplay;
-    private EGLContext mEGLContext;
-    private EGLSurface mEGLSurface;
+    private EGLDisplay mEGLDisplay = EGL14.EGL_NO_DISPLAY;
+    private EGLContext mEGLContext = EGL14.EGL_NO_CONTEXT;
+    private EGLSurface mEGLSurface = EGL14.EGL_NO_SURFACE;
 
     private Surface mSurface;
 
@@ -71,8 +71,8 @@
             throw new RuntimeException("unable to initialize EGL14");
         }
 
-        // Configure EGL for pbuffer and OpenGL ES 2.0.  We want enough RGB bits
-        // to be able to tell if the frame is reasonable.
+        // Configure EGL for recordable and OpenGL ES 2.0.  We want enough RGB bits
+        // to minimize artifacts from possible YUV conversion.
         int[] attribList = {
                 EGL14.EGL_RED_SIZE, 8,
                 EGL14.EGL_GREEN_SIZE, 8,
@@ -117,17 +117,18 @@
      * Surface that was passed to our constructor.
      */
     public void release() {
-        if (mEGLDisplay != null) {
+        if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) {
+            EGL14.eglDestroySurface(mEGLDisplay, mEGLSurface);
+            EGL14.eglDestroyContext(mEGLDisplay, mEGLContext);
             EGL14.eglReleaseThread();
             EGL14.eglTerminate(mEGLDisplay);
         }
 
         mSurface.release();
 
-        // null everything out so future attempts to use this object will cause an NPE
-        mEGLDisplay = null;
-        mEGLContext = null;
-        mEGLSurface = null;
+        mEGLDisplay = EGL14.EGL_NO_DISPLAY;
+        mEGLContext = EGL14.EGL_NO_CONTEXT;
+        mEGLSurface = EGL14.EGL_NO_SURFACE;
 
         mSurface = null;
     }
@@ -166,14 +167,9 @@
      * Checks for EGL errors.
      */
     private void checkEglError(String msg) {
-        boolean failed = false;
         int error;
-        while ((error = EGL14.eglGetError()) != EGL14.EGL_SUCCESS) {
-            Log.e(TAG, msg + ": EGL error: 0x" + Integer.toHexString(error));
-            failed = true;
-        }
-        if (failed) {
-            throw new RuntimeException("EGL error encountered (see log)");
+        if ((error = EGL14.eglGetError()) != EGL14.EGL_SUCCESS) {
+            throw new RuntimeException(msg + ": EGL error: 0x" + Integer.toHexString(error));
         }
     }
 }
diff --git a/tests/tests/media/src/android/media/cts/OutputSurface.java b/tests/tests/media/src/android/media/cts/OutputSurface.java
index 8a299e2..fd36d80 100644
--- a/tests/tests/media/src/android/media/cts/OutputSurface.java
+++ b/tests/tests/media/src/android/media/cts/OutputSurface.java
@@ -18,15 +18,13 @@
 
 import android.graphics.SurfaceTexture;
 import android.opengl.EGL14;
+import android.opengl.EGLConfig;
+import android.opengl.EGLContext;
+import android.opengl.EGLDisplay;
+import android.opengl.EGLSurface;
 import android.util.Log;
 import android.view.Surface;
 
-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;
-
 
 /**
  * Holds state associated with a Surface used for MediaCodec decoder output.
@@ -47,12 +45,9 @@
     private static final String TAG = "OutputSurface";
     private static final boolean VERBOSE = false;
 
-    private static final int EGL_OPENGL_ES2_BIT = 4;
-
-    private EGL10 mEGL;
-    private EGLDisplay mEGLDisplay;
-    private EGLContext mEGLContext;
-    private EGLSurface mEGLSurface;
+    private EGLDisplay mEGLDisplay = EGL14.EGL_NO_DISPLAY;
+    private EGLContext mEGLContext = EGL14.EGL_NO_CONTEXT;
+    private EGLSurface mEGLSurface = EGL14.EGL_NO_SURFACE;
 
     private SurfaceTexture mSurfaceTexture;
     private Surface mSurface;
@@ -79,8 +74,8 @@
     }
 
     /**
-     * Creates an OutputSurface using the current EGL context.  Creates a Surface that can be
-     * passed to MediaCodec.configure().
+     * Creates an OutputSurface using the current EGL context (rather than establishing a
+     * new one).  Creates a Surface that can be passed to MediaCodec.configure().
      */
     public OutputSurface() {
         setup();
@@ -121,35 +116,40 @@
      * Prepares EGL.  We want a GLES 2.0 context and a surface that supports pbuffer.
      */
     private void eglSetup(int width, int height) {
-        mEGL = (EGL10)EGLContext.getEGL();
-        mEGLDisplay = mEGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
-        if (!mEGL.eglInitialize(mEGLDisplay, null)) {
-            throw new RuntimeException("unable to initialize EGL10");
+        mEGLDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
+        if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) {
+            throw new RuntimeException("unable to get EGL14 display");
+        }
+        int[] version = new int[2];
+        if (!EGL14.eglInitialize(mEGLDisplay, version, 0, version, 1)) {
+            mEGLDisplay = null;
+            throw new RuntimeException("unable to initialize EGL14");
         }
 
         // Configure EGL for pbuffer and OpenGL ES 2.0.  We want enough RGB bits
         // to be able to tell if the frame is reasonable.
         int[] attribList = {
-                EGL10.EGL_RED_SIZE, 8,
-                EGL10.EGL_GREEN_SIZE, 8,
-                EGL10.EGL_BLUE_SIZE, 8,
-                EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
-                EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
-                EGL10.EGL_NONE
+                EGL14.EGL_RED_SIZE, 8,
+                EGL14.EGL_GREEN_SIZE, 8,
+                EGL14.EGL_BLUE_SIZE, 8,
+                EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
+                EGL14.EGL_SURFACE_TYPE, EGL14.EGL_PBUFFER_BIT,
+                EGL14.EGL_NONE
         };
         EGLConfig[] configs = new EGLConfig[1];
         int[] numConfigs = new int[1];
-        if (!mEGL.eglChooseConfig(mEGLDisplay, attribList, configs, 1, numConfigs)) {
-            throw new RuntimeException("unable to find RGB888+pbuffer EGL config");
+        if (!EGL14.eglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.length,
+                numConfigs, 0)) {
+            throw new RuntimeException("unable to find RGB888+recordable ES2 EGL config");
         }
 
         // Configure context for OpenGL ES 2.0.
         int[] attrib_list = {
                 EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
-                EGL10.EGL_NONE
+                EGL14.EGL_NONE
         };
-        mEGLContext = mEGL.eglCreateContext(mEGLDisplay, configs[0], EGL10.EGL_NO_CONTEXT,
-                attrib_list);
+        mEGLContext = EGL14.eglCreateContext(mEGLDisplay, configs[0], EGL14.EGL_NO_CONTEXT,
+                attrib_list, 0);
         checkEglError("eglCreateContext");
         if (mEGLContext == null) {
             throw new RuntimeException("null context");
@@ -158,11 +158,11 @@
         // Create a pbuffer surface.  By using this for output, we can use glReadPixels
         // to test values in the output.
         int[] surfaceAttribs = {
-                EGL10.EGL_WIDTH, width,
-                EGL10.EGL_HEIGHT, height,
-                EGL10.EGL_NONE
+                EGL14.EGL_WIDTH, width,
+                EGL14.EGL_HEIGHT, height,
+                EGL14.EGL_NONE
         };
-        mEGLSurface = mEGL.eglCreatePbufferSurface(mEGLDisplay, configs[0], surfaceAttribs);
+        mEGLSurface = EGL14.eglCreatePbufferSurface(mEGLDisplay, configs[0], surfaceAttribs, 0);
         checkEglError("eglCreatePbufferSurface");
         if (mEGLSurface == null) {
             throw new RuntimeException("surface was null");
@@ -173,9 +173,11 @@
      * Discard all resources held by this class, notably the EGL context.
      */
     public void release() {
-        if (mEGL != null && mEGLDisplay != null) {
-            mEGL.eglReleaseThread();
-            mEGL.eglTerminate(mEGLDisplay);
+        if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) {
+            EGL14.eglDestroySurface(mEGLDisplay, mEGLSurface);
+            EGL14.eglDestroyContext(mEGLDisplay, mEGLContext);
+            EGL14.eglReleaseThread();
+            EGL14.eglTerminate(mEGLDisplay);
         }
 
         mSurface.release();
@@ -184,11 +186,9 @@
         //  W BufferQueue: [unnamed-3997-2] cancelBuffer: BufferQueue has been abandoned!
         //mSurfaceTexture.release();
 
-        // null everything out so future attempts to use this object will cause an NPE
-        mEGLDisplay = null;
-        mEGLContext = null;
-        mEGLSurface = null;
-        mEGL = null;
+        mEGLDisplay = EGL14.EGL_NO_DISPLAY;
+        mEGLContext = EGL14.EGL_NO_CONTEXT;
+        mEGLSurface = EGL14.EGL_NO_SURFACE;
 
         mTextureRender = null;
         mSurface = null;
@@ -199,11 +199,7 @@
      * Makes our EGL context and surface current.
      */
     public void makeCurrent() {
-        if (mEGL == null) {
-            throw new RuntimeException("not configured for makeCurrent");
-        }
-        checkEglError("before makeCurrent");
-        if (!mEGL.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext)) {
+        if (!EGL14.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext)) {
             throw new RuntimeException("eglMakeCurrent failed");
         }
     }
@@ -276,14 +272,9 @@
      * Checks for EGL errors.
      */
     private void checkEglError(String msg) {
-        boolean failed = false;
         int error;
-        while ((error = mEGL.eglGetError()) != EGL10.EGL_SUCCESS) {
-            Log.e(TAG, msg + ": EGL error: 0x" + Integer.toHexString(error));
-            failed = true;
-        }
-        if (failed) {
-            throw new RuntimeException("EGL error encountered (see log)");
+        if ((error = EGL14.eglGetError()) != EGL14.EGL_SUCCESS) {
+            throw new RuntimeException(msg + ": EGL error: 0x" + Integer.toHexString(error));
         }
     }
 }