Merge cherrypicks of [15687253, 15687254, 15687255, 15687532, 15687533] into sc-release

Change-Id: Iffd3766bba50471fa624ab2a441bb3995b7ea649
diff --git a/tests/tests/keystore/src/android/keystore/cts/ECDSASignatureTest.java b/tests/tests/keystore/src/android/keystore/cts/ECDSASignatureTest.java
index de60e37..3919f90 100644
--- a/tests/tests/keystore/src/android/keystore/cts/ECDSASignatureTest.java
+++ b/tests/tests/keystore/src/android/keystore/cts/ECDSASignatureTest.java
@@ -43,6 +43,12 @@
     private void assertNONEwithECDSATruncatesInputToFieldSize(KeyPair keyPair)
             throws Exception {
         int keySizeBits = TestUtils.getKeySizeBits(keyPair.getPublic());
+        if (keySizeBits == 521) {
+            /*
+             * Skip P521 test until b/184307265 is fixed.
+             */
+            return;
+        }
         byte[] message = new byte[(keySizeBits * 3) / 8];
         for (int i = 0; i < message.length; i++) {
             message[i] = (byte) (i + 1);
diff --git a/tests/video/src/android/video/cts/CodecEncoderPerformanceTestBase.java b/tests/video/src/android/video/cts/CodecEncoderPerformanceTestBase.java
index 647effe..d95e069 100644
--- a/tests/video/src/android/video/cts/CodecEncoderPerformanceTestBase.java
+++ b/tests/video/src/android/video/cts/CodecEncoderPerformanceTestBase.java
@@ -28,6 +28,7 @@
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
 
 class CodecEncoderPerformanceTestBase extends CodecPerformanceTestBase {
     private static final String LOG_TAG = CodecEncoderPerformanceTest.class.getSimpleName();
@@ -160,6 +161,14 @@
     public void encode() throws IOException {
         MediaFormat format = setUpDecoderInput();
         assertNotNull("Video track not present in " + mTestFile, format);
+
+        if (EXCLUDE_ENCODER_MAX_RESOLUTION) {
+            int maxFrameSize = getMaxFrameSize(mEncoderName, mEncoderMime);
+            assumeTrue(mWidth + "x" + mHeight + " is skipped as it not less than half of " +
+                    "maximum frame size: " + maxFrameSize + " supported by the encoder.",
+                    mWidth * mHeight < maxFrameSize / 2);
+        }
+
         setUpFormats(format);
         mDecoder = MediaCodec.createByCodecName(mDecoderName);
         mEncoder = MediaCodec.createByCodecName(mEncoderName);
diff --git a/tests/video/src/android/video/cts/CodecPerformanceTestBase.java b/tests/video/src/android/video/cts/CodecPerformanceTestBase.java
index 01670342..5a34fce 100644
--- a/tests/video/src/android/video/cts/CodecPerformanceTestBase.java
+++ b/tests/video/src/android/video/cts/CodecPerformanceTestBase.java
@@ -50,6 +50,14 @@
     // passing the test
     static final double FPS_TOLERANCE_FACTOR;
     static final boolean IS_AT_LEAST_VNDK_S;
+
+    static final int DEVICE_INITIAL_SDK;
+
+    // Some older devices can not support concurrent instances of both decoder and encoder
+    // at max resolution. To handle such cases, this test is limited to test the
+    // resolutions that are less than half of max supported frame sizes of encoder.
+    static final boolean EXCLUDE_ENCODER_MAX_RESOLUTION;
+
     static final String mInputPrefix = WorkDir.getMediaDirString();
 
     ArrayList<MediaCodec.BufferInfo> mBufferInfos;
@@ -83,18 +91,30 @@
         // os.Build.VERSION.DEVICE_INITIAL_SDK_INT can be used here, but it was called
         // os.Build.VERSION.FIRST_SDK_INT in Android R and below. Using DEVICE_INITIAL_SDK_INT
         // will mean that the tests built in Android S can't be run on Android R and below.
-        int deviceInitialSdk = SystemProperties.getInt("ro.product.first_api_level", 0);
+        DEVICE_INITIAL_SDK = SystemProperties.getInt("ro.product.first_api_level", 0);
 
         // fps tolerance factor is kept quite low for devices launched on Android R and lower
-        FPS_TOLERANCE_FACTOR = deviceInitialSdk <= Build.VERSION_CODES.R ? 0.67 : 0.95;
+        FPS_TOLERANCE_FACTOR = DEVICE_INITIAL_SDK <= Build.VERSION_CODES.R ? 0.67 : 0.95;
 
         IS_AT_LEAST_VNDK_S = SystemProperties.getInt("ro.vndk.version", 0) > Build.VERSION_CODES.R;
+
+        // Encoders on devices launched on Android Q and lower aren't tested at maximum resolution
+        EXCLUDE_ENCODER_MAX_RESOLUTION = DEVICE_INITIAL_SDK <= Build.VERSION_CODES.Q;
     }
 
     @Before
     public void prologue() {
         assumeTrue("For VNDK R and below, operating rate <= 0 isn't tested",
                 IS_AT_LEAST_VNDK_S || mMaxOpRateScalingFactor > 0.0);
+
+        assumeTrue("For devices launched on Android P and below, operating rate tests are disabled",
+                DEVICE_INITIAL_SDK > Build.VERSION_CODES.P);
+
+        if (DEVICE_INITIAL_SDK <= Build.VERSION_CODES.Q) {
+            assumeTrue("For devices launched with Android Q and below, operating rate tests are " +
+                            "limited to operating rate scaling factor > 0.0 and <= 1.25",
+                    mMaxOpRateScalingFactor > 0.0 && mMaxOpRateScalingFactor <= 1.25);
+        }
     }
 
     public CodecPerformanceTestBase(String decoderName, String testFile, int keyPriority,
@@ -283,6 +303,18 @@
         return minComplexity;
     }
 
+    static int getMaxFrameSize(String codecName, String mime) throws IOException {
+        MediaCodec codec = MediaCodec.createByCodecName(codecName);
+        MediaCodecInfo.CodecCapabilities codecCapabilities =
+                codec.getCodecInfo().getCapabilitiesForType(mime);
+        MediaCodecInfo.VideoCapabilities vc = codecCapabilities.getVideoCapabilities();
+        Range<Integer> heights = vc.getSupportedHeights();
+        Range<Integer> widths = vc.getSupportedWidthsFor(heights.getUpper());
+        int maxFrameSize = heights.getUpper() * widths.getUpper();
+        codec.release();
+        return maxFrameSize;
+    }
+
     void enqueueDecoderInput(int bufferIndex) {
         MediaCodec.BufferInfo info = mBufferInfos.get(mSampleIndex++);
         if (info.size > 0 && (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) {