Initializing EGL protected extension for secure decode

 - ACodec checks for secure attributes for suface when
   used with secure codec. We properly check for secure
   extension support from EGL and then decides to go
   forward with test

Bug: 298168826
Bug: 300140376
Bug: 301045787
Change-Id: I06881e41e2de26d0ee8b7d288536795a04975a5d
diff --git a/tests/tests/media/common/src/android/media/cts/MediaCodecCryptoAsyncHelper.java b/tests/tests/media/common/src/android/media/cts/MediaCodecCryptoAsyncHelper.java
index 2ecd83a..c6d1479 100644
--- a/tests/tests/media/common/src/android/media/cts/MediaCodecCryptoAsyncHelper.java
+++ b/tests/tests/media/common/src/android/media/cts/MediaCodecCryptoAsyncHelper.java
@@ -192,7 +192,7 @@
                 slotQueue = new LinkedBlockingQueue<>();
         boolean isSecureDecodeASuccess = false;
         try {
-            outputSurface = new OutputSurface(1, 1);
+            outputSurface = new OutputSurface(1, 1, false, secure);
             MediaFormat mediaFormat = mediaExtractor.getTrackFormat(
                     mediaExtractor.getSampleTrackIndex());
             String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
@@ -310,7 +310,7 @@
         final LinkedBlockingQueue<MediaCodecAsyncHelper.SlotEvent>
                 slotQueue = new LinkedBlockingQueue<>();
         try {
-            outputSurface = new OutputSurface(1, 1);
+            outputSurface = new OutputSurface(1, 1, false, secure);
             MediaFormat mediaFormat = mediaExtractor.getTrackFormat(
                     mediaExtractor.getSampleTrackIndex());
             String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
@@ -438,7 +438,7 @@
        final LinkedBlockingQueue<MediaCodec.CryptoInfo>
               cryptoInfoQueue = new LinkedBlockingQueue<>();
         try {
-            outputSurface = new OutputSurface(1, 1);
+            outputSurface = new OutputSurface(1, 1, false, secure);
             MediaFormat mediaFormat = mediaExtractor.getTrackFormat(
                     mediaExtractor.getSampleTrackIndex());
             String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
diff --git a/tests/tests/media/common/src/android/media/cts/OutputSurface.java b/tests/tests/media/common/src/android/media/cts/OutputSurface.java
index a25f2ba..efd464d 100644
--- a/tests/tests/media/common/src/android/media/cts/OutputSurface.java
+++ b/tests/tests/media/common/src/android/media/cts/OutputSurface.java
@@ -27,6 +27,8 @@
 import android.util.Log;
 import android.view.Surface;
 
+import org.junit.Assume;
+
 
 /**
  * Holds state associated with a Surface used for MediaCodec decoder output.
@@ -47,6 +49,11 @@
     private static final String TAG = "OutputSurface";
     private static final boolean VERBOSE = false;
 
+    public static int EGL_PROTECTED_CONTENT_EXT = 0x32C0;
+
+    // https://www.khronos.org/registry/EGL/extensions/EXT/EGL_EXT_protected_content.txt
+    private static final String EXTENSION_PROTECTED_CONTENT = "EGL_EXT_protected_content";
+
     private EGLDisplay mEGLDisplay = EGL14.EGL_NO_DISPLAY;
     private EGLContext mEGLContext = EGL14.EGL_NO_CONTEXT;
     private EGLSurface mEGLSurface = EGL14.EGL_NO_SURFACE;
@@ -56,6 +63,7 @@
 
     private Object mFrameSyncObject = new Object();     // guards mFrameAvailable
     private boolean mFrameAvailable;
+    private boolean mSecureMode = false;
 
     private TextureRender mTextureRender;
 
@@ -65,13 +73,18 @@
      * to MediaCodec.configure().
      */
     public OutputSurface(int width, int height) {
-        this(width, height, false);
+        this(width, height, false, false);
     }
 
     public OutputSurface(int width, int height, boolean useHighBitDepth) {
+        this(width, height, useHighBitDepth, false);
+    }
+
+    public OutputSurface(int width, int height, boolean useHighBitDepth, boolean secure) {
         if (width <= 0 || height <= 0) {
             throw new IllegalArgumentException();
         }
+        mSecureMode = secure;
 
         eglSetup(width, height, useHighBitDepth);
         makeCurrent();
@@ -91,12 +104,18 @@
         setup(listener);
     }
 
+    private boolean isSecureSurfaceSupported() {
+        EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
+        String eglExtensions = EGL14.eglQueryString(display, EGL14.EGL_EXTENSIONS);
+        return eglExtensions != null && eglExtensions.contains(EXTENSION_PROTECTED_CONTENT);
+    }
+
     /**
      * Creates instances of TextureRender and SurfaceTexture, and a Surface associated
      * with the SurfaceTexture.
      */
     private void setup(SurfaceTexture.OnFrameAvailableListener listener) {
-        mTextureRender = new TextureRender();
+        mTextureRender = new TextureRender(mSecureMode);
         mTextureRender.surfaceCreated();
 
         // Even if we don't access the SurfaceTexture after the constructor returns, we
@@ -135,6 +154,11 @@
             mEGLDisplay = null;
             throw new RuntimeException("unable to initialize EGL14");
         }
+        if (mSecureMode) {
+            Assume.assumeTrue("EGL_PROTECTED_CONTENT_EXT not supported",
+                    isSecureSurfaceSupported());
+            checkEglError("EGL_PROTECTED_CONTENT_EXT not supported");
+        }
 
         // Configure EGL for pbuffer and OpenGL ES 2.0.  We want enough RGB bits
         // to be able to tell if the frame is reasonable.
@@ -163,10 +187,19 @@
         }
 
         // Configure context for OpenGL ES 2.0.
-        int[] contextAttribList = {
-                EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
-                EGL14.EGL_NONE
-        };
+        int[] contextAttribList;
+        if (!mSecureMode) {
+            contextAttribList =  new int [] {
+                    EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
+                    EGL14.EGL_NONE
+            };
+        } else {
+            contextAttribList =  new int [] {
+                    EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
+                    EGL_PROTECTED_CONTENT_EXT, EGL14.EGL_TRUE,
+                    EGL14.EGL_NONE
+            };
+        }
         mEGLContext = EGL14.eglCreateContext(mEGLDisplay, configs[0], EGL14.EGL_NO_CONTEXT,
                 contextAttribList, 0);
         checkEglError("eglCreateContext");
@@ -176,11 +209,22 @@
 
         // Create a pbuffer surface.  By using this for output, we can use glReadPixels
         // to test values in the output.
-        int[] surfaceAttribs = {
-                EGL14.EGL_WIDTH, width,
-                EGL14.EGL_HEIGHT, height,
-                EGL14.EGL_NONE
-        };
+        int[] surfaceAttribs;
+
+        if (!mSecureMode) {
+            surfaceAttribs = new int[] {
+                    EGL14.EGL_WIDTH, width,
+                    EGL14.EGL_HEIGHT, height,
+                    EGL14.EGL_NONE
+            };
+        } else {
+            surfaceAttribs = new int[] {
+                    EGL14.EGL_WIDTH, width,
+                    EGL14.EGL_HEIGHT, height,
+                    EGL_PROTECTED_CONTENT_EXT, EGL14.EGL_TRUE,
+                    EGL14.EGL_NONE
+            };
+        }
         mEGLSurface = EGL14.eglCreatePbufferSurface(mEGLDisplay, configs[0], surfaceAttribs, 0);
         checkEglError("eglCreatePbufferSurface");
         if (mEGLSurface == null) {
diff --git a/tests/tests/media/common/src/android/media/cts/TextureRender.java b/tests/tests/media/common/src/android/media/cts/TextureRender.java
index daf5892..1d02be9 100644
--- a/tests/tests/media/common/src/android/media/cts/TextureRender.java
+++ b/tests/tests/media/common/src/android/media/cts/TextureRender.java
@@ -79,8 +79,16 @@
     private int muSTMatrixHandle;
     private int maPositionHandle;
     private int maTextureHandle;
+    private boolean mSecureMode = false;
+
+    private static int GL_TEXTURE_PROTECTED_EXT = 0x8BFA;
 
     public TextureRender() {
+        this(false);
+    }
+
+    public TextureRender(boolean secure) {
+        mSecureMode = secure;
         mTriangleVertices = ByteBuffer.allocateDirect(
             mTriangleVerticesData.length * FLOAT_SIZE_BYTES)
                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
@@ -176,6 +184,10 @@
                 GLES20.GL_CLAMP_TO_EDGE);
         GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,
                 GLES20.GL_CLAMP_TO_EDGE);
+        if (mSecureMode) {
+            GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_PROTECTED_EXT,
+                    GLES20.GL_TRUE);
+        }
         checkGlError("glTexParameter");
     }