Merge "Change VideoDecoderPerfTest semantics for mainline" am: 1dd6b532a5 am: bc1ad7d786

Original change: https://android-review.googlesource.com/c/platform/cts/+/2045377

Change-Id: Ia36f1b83cf94abd6817ca9a990c71eb9e867ec05
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/common/device-side/util-axt/src/com/android/compatibility/common/util/MediaPerfUtils.java b/common/device-side/util-axt/src/com/android/compatibility/common/util/MediaPerfUtils.java
index 7e02b85..06b8cc2 100644
--- a/common/device-side/util-axt/src/com/android/compatibility/common/util/MediaPerfUtils.java
+++ b/common/device-side/util-axt/src/com/android/compatibility/common/util/MediaPerfUtils.java
@@ -147,7 +147,7 @@
      *  one measurement falls within the margins of the reported range. Otherwise, returns
      *  an error message to display.*/
     public static String verifyAchievableFrameRates(
-            String name, String mime, int w, int h, double... measuredFps) {
+            String name, String mime, int w, int h, boolean fasterIsOk, double... measuredFps) {
         Range<Double> reported =
             MediaUtils.getVideoCapabilities(name, mime).getAchievableFrameRatesFor(w, h);
         String kind = "achievable frame rates for " + name + " " + mime + " " + w + "x" + h;
@@ -163,10 +163,20 @@
                 " lowerBoundary2 " + lowerBoundary2 + " upperBoundary2 " + upperBoundary2 +
                 " measured " + Arrays.toString(measuredFps));
 
-        for (double measured : measuredFps) {
-            if (measured >= lowerBoundary1 && measured <= upperBoundary1
-                    && measured >= lowerBoundary2 && measured <= upperBoundary2) {
-                return null;
+        if (fasterIsOk) {
+            double lower = Math.max(lowerBoundary1, lowerBoundary2);
+            for (double measured : measuredFps) {
+                if (measured >= lower) {
+                    return null;
+                }
+            }
+        } else {
+            double lower = Math.max(lowerBoundary1, lowerBoundary2);
+            double upper = Math.min(upperBoundary1, upperBoundary2);
+            for (double measured : measuredFps) {
+                if (measured >= lower && measured <= upper) {
+                    return null;
+                }
             }
         }
 
diff --git a/tests/tests/media/common/src/android/media/cts/TestUtils.java b/tests/tests/media/common/src/android/media/cts/TestUtils.java
index d6c898b2..1ae40ae 100644
--- a/tests/tests/media/common/src/android/media/cts/TestUtils.java
+++ b/tests/tests/media/common/src/android/media/cts/TestUtils.java
@@ -21,6 +21,7 @@
 import static org.junit.Assume.assumeTrue;
 
 import android.content.Context;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
@@ -123,13 +124,30 @@
     private static long getModuleVersion(String module)
             throws PackageManager.NameNotFoundException {
         Context context = ApplicationProvider.getApplicationContext();
-        PackageInfo info;
-        info = context.getPackageManager().getPackageInfo(module,
+        PackageInfo info = context.getPackageManager().getPackageInfo(module,
                 MATCH_APEX);
         return info.getLongVersionCode();
     }
 
 
+    /**
+     * Reports whether {@code module} is the version shipped with the original system image
+     * or if it has been updated via a mainline update.
+     *
+     * @param module     the apex module name
+     * @return {@code true} if the apex module is the original version shipped with the device.
+     */
+    public static boolean isMainlineModuleFactoryVersion(String module)
+            throws PackageManager.NameNotFoundException {
+        Context context = ApplicationProvider.getApplicationContext();
+        PackageInfo info = context.getPackageManager().getPackageInfo(module,
+                MATCH_APEX);
+        if (info == null) {
+            return true;
+        }
+        return (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
+    }
+
     private TestUtils() {
     }
 
diff --git a/tests/tests/media/decoder/src/android/media/decoder/cts/VideoDecoderPerfTest.java b/tests/tests/media/decoder/src/android/media/decoder/cts/VideoDecoderPerfTest.java
index 8eb38e7..61f6cdd 100644
--- a/tests/tests/media/decoder/src/android/media/decoder/cts/VideoDecoderPerfTest.java
+++ b/tests/tests/media/decoder/src/android/media/decoder/cts/VideoDecoderPerfTest.java
@@ -28,6 +28,7 @@
 import android.media.cts.MediaHeavyPresubmitTest;
 import android.media.cts.MediaTestBase;
 import android.media.cts.Preconditions;
+import android.media.cts.TestUtils;
 import android.os.Bundle;
 import android.platform.test.annotations.AppModeFull;
 import android.text.TextUtils;
@@ -45,6 +46,7 @@
 import com.android.compatibility.common.util.ResultUnit;
 
 import org.junit.After;
+import org.junit.Assume;
 import org.junit.Before;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -97,6 +99,7 @@
     private int mBitrate;
 
     private boolean mSkipRateChecking = false;
+    private boolean mUpdatedSwCodec = false;
     static final String mInpPrefix = WorkDir.getMediaDirString();
 
     static private List<Object[]> prepareParamList(List<Object[]> exhaustiveArgsList) {
@@ -173,6 +176,9 @@
         super.setUp();
         Bundle bundle = InstrumentationRegistry.getArguments();
         mSkipRateChecking = TextUtils.equals("true", bundle.getString("mts-media"));
+
+        mUpdatedSwCodec =
+                !TestUtils.isMainlineModuleFactoryVersion("com.google.android.media.swcodec");
     }
 
     @After
@@ -207,8 +213,11 @@
             // doDecode(name, video, width, height, null, i, maxTimeMs);
         }
 
+        // allow improvements in mainline-updated google-supplied software codecs.
+        boolean fasterIsOk = mUpdatedSwCodec & name.startsWith("c2.android.");
         String error =
-            MediaPerfUtils.verifyAchievableFrameRates(name, mime, width, height, measuredFps);
+            MediaPerfUtils.verifyAchievableFrameRates(name, mime, width, height,
+                           fasterIsOk,  measuredFps);
         // Performance numbers only make sense on real devices, so skip on non-real devices
         if ((MediaUtils.onFrankenDevice() || mSkipRateChecking) && error != null) {
             // ensure there is data, but don't insist that it is correct
diff --git a/tests/video/Android.bp b/tests/video/Android.bp
index 55eaa74..b41a32c 100644
--- a/tests/video/Android.bp
+++ b/tests/video/Android.bp
@@ -24,6 +24,7 @@
         "ctsmediautil",
         "compatibility-device-util-axt",
         "ctstestrunner-axt",
+        "cts-media-common",
     ],
     libs: [
         "android.test.runner",
diff --git a/tests/video/src/android/video/cts/VideoEncoderDecoderTest.java b/tests/video/src/android/video/cts/VideoEncoderDecoderTest.java
index 4755b9a..401468f 100644
--- a/tests/video/src/android/video/cts/VideoEncoderDecoderTest.java
+++ b/tests/video/src/android/video/cts/VideoEncoderDecoderTest.java
@@ -27,6 +27,7 @@
 import android.media.MediaFormat;
 import android.media.cts.CodecImage;
 import android.media.cts.CodecUtils;
+import android.media.cts.TestUtils;
 import android.media.cts.YUVImage;
 import android.os.Build;
 import android.util.Log;
@@ -124,6 +125,8 @@
     private double mRmsErrorMargin;
     private Random mRandom;
 
+    private boolean mUpdatedSwCodec = false;
+
     private class TestConfig {
         public boolean mTestPixels = true;
         public boolean mReportFrameTime = false;
@@ -157,6 +160,8 @@
     protected void setUp() throws Exception {
         mEncodedOutputBuffer = new LinkedList<Pair<ByteBuffer, BufferInfo>>();
         mRmsErrorMargin = PIXEL_RMS_ERROR_MARGIN;
+        mUpdatedSwCodec =
+                !TestUtils.isMainlineModuleFactoryVersion("com.google.android.media.swcodec");
         // Use time as a seed, hoping to prevent checking pixels in the same pattern
         long now = System.currentTimeMillis();
         mRandom = new Random(now);
@@ -819,8 +824,10 @@
         }
 
         if (isPerf) {
+            // allow improvements in mainline-updated google-supplied software codecs.
+            boolean fasterIsOk =  mUpdatedSwCodec & encoderName.startsWith("c2.android.");
             String error = MediaPerfUtils.verifyAchievableFrameRates(
-                    encoderName, mimeType, w, h, measuredFps);
+                    encoderName, mimeType, w, h, fasterIsOk, measuredFps);
             // Performance numbers only make sense on real devices, so skip on non-real devices
             //
             // Also ignore verification on non-preferred ABIs due to the possibility of