DO NOT MERGE. Backport test fixes from lmp-mr1-dev

CameraITS: disable slow filters in burst tests

So those filters won't slow down FPS

Bug: 18505630

Camera2: wait for settings applied before check FPS

Bug: 18506078

Camera: free images instantly in testMandatoryOutputCombinations

Bug: 18528472
diff --git a/apps/CameraITS/pymodules/its/objects.py b/apps/CameraITS/pymodules/its/objects.py
index a531f3b..22540b8 100644
--- a/apps/CameraITS/pymodules/its/objects.py
+++ b/apps/CameraITS/pymodules/its/objects.py
@@ -123,6 +123,21 @@
         "android.tonemap.mode": 1,
         }
 
+def fastest_auto_capture_request(props):
+    """Return an auto capture request for the fastest capture.
+
+    Args:
+        props: the object returned from its.device.get_camera_properties().
+
+    Returns:
+        A capture request with everything set to auto and all filters that
+            may slow down capture set to OFF or FAST if possible
+    """
+    req = auto_capture_request()
+    turn_slow_filters_off(props, req)
+
+    return req
+
 def get_available_output_sizes(fmt, props):
     """Return a sorted list of available output sizes for a given format.
 
@@ -143,16 +158,16 @@
     return out_sizes
 
 def set_filter_off_or_fast_if_possible(props, req, available_modes, filter):
-    """ Check and set controlKey to off or fast in req
+    """Check and set controlKey to off or fast in req.
 
     Args:
         props: the object returned from its.device.get_camera_properties().
-        req: the input request.
+        req: the input request. filter will be set to OFF or FAST if possible.
         available_modes: the key to check available modes.
         filter: the filter key
 
     Returns:
-        None. control_key will be set to OFF or FAST if possible.
+        Nothing.
     """
     if props.has_key(available_modes):
         if 0 in props[available_modes]:
@@ -160,6 +175,33 @@
         elif 1 in props[available_modes]:
             req[filter] = 1
 
+def turn_slow_filters_off(props, req):
+    """Turn filters that may slow FPS down to OFF or FAST in input request.
+
+    This function modifies the request argument, such that filters that may
+    reduce the frames-per-second throughput of the camera device will be set to
+    OFF or FAST if possible.
+
+    Args:
+        props: the object returned from its.device.get_camera_properties().
+        req: the input request.
+
+    Returns:
+        Nothing.
+    """
+    set_filter_off_or_fast_if_possible(props, req,
+        "android.noiseReduction.availableNoiseReductionModes",
+        "android.noiseReduction.mode")
+    set_filter_off_or_fast_if_possible(props, req,
+        "android.colorCorrection.availableAberrationModes",
+        "android.colorCorrection.aberrationMode")
+    set_filter_off_or_fast_if_possible(props, req,
+        "android.hotPixel.availableHotPixelModes",
+        "android.hotPixel.mode")
+    set_filter_off_or_fast_if_possible(props, req,
+        "android.edge.availableEdgeModes",
+        "android.edge.mode")
+
 def get_fastest_manual_capture_settings(props):
     """Return a capture request and format spec for the fastest capture.
 
@@ -178,18 +220,7 @@
     e = min(props['android.sensor.info.exposureTimeRange'])
     req = manual_capture_request(s,e)
 
-    set_filter_off_or_fast_if_possible(props, req,
-        "android.noiseReduction.availableNoiseReductionModes",
-        "android.noiseReduction.mode")
-    set_filter_off_or_fast_if_possible(props, req,
-        "android.colorCorrection.availableAberrationModes",
-        "android.colorCorrection.aberrationMode")
-    set_filter_off_or_fast_if_possible(props, req,
-        "android.hotPixel.availableHotPixelModes",
-        "android.hotPixel.mode")
-    set_filter_off_or_fast_if_possible(props, req,
-        "android.edge.availableEdgeModes",
-        "android.edge.mode")
+    turn_slow_filters_off(props, req)
 
     return req, out_spec
 
diff --git a/apps/CameraITS/tests/inprog/test_burst_sameness_auto.py b/apps/CameraITS/tests/inprog/test_burst_sameness_auto.py
index fdf72be..87500c7 100644
--- a/apps/CameraITS/tests/inprog/test_burst_sameness_auto.py
+++ b/apps/CameraITS/tests/inprog/test_burst_sameness_auto.py
@@ -48,7 +48,7 @@
         cam.do_3a(lock_ae=True, lock_awb=True)
 
         # After 3A has converged, lock AE+AWB for the duration of the test.
-        req = its.objects.auto_capture_request()
+        req = its.objects.fastest_auto_capture_request(props)
         req["android.blackLevel.lock"] = True
         req["android.control.awbLock"] = True
         req["android.control.aeLock"] = True
diff --git a/apps/CameraITS/tests/inprog/test_burst_sameness_fullres_auto.py b/apps/CameraITS/tests/inprog/test_burst_sameness_fullres_auto.py
index a8d1d45..932c051 100644
--- a/apps/CameraITS/tests/inprog/test_burst_sameness_fullres_auto.py
+++ b/apps/CameraITS/tests/inprog/test_burst_sameness_fullres_auto.py
@@ -47,7 +47,7 @@
         cam.do_3a(lock_ae=True, lock_awb=True)
 
         # After 3A has converged, lock AE+AWB for the duration of the test.
-        req = its.objects.auto_capture_request()
+        req = its.objects.fastest_auto_capture_request(props)
         req["android.blackLevel.lock"] = True
         req["android.control.awbLock"] = True
         req["android.control.aeLock"] = True
diff --git a/apps/CameraITS/tests/scene1/test_locked_burst.py b/apps/CameraITS/tests/scene1/test_locked_burst.py
index 5cea30c..90662db 100644
--- a/apps/CameraITS/tests/scene1/test_locked_burst.py
+++ b/apps/CameraITS/tests/scene1/test_locked_burst.py
@@ -41,7 +41,7 @@
         cam.do_3a(do_af=True, lock_ae=True, lock_awb=True)
 
         # After 3A has converged, lock AE+AWB for the duration of the test.
-        req = its.objects.auto_capture_request()
+        req = its.objects.fastest_auto_capture_request(props)
         req["android.control.awbLock"] = True
         req["android.control.aeLock"] = True
 
diff --git a/tests/tests/hardware/src/android/hardware/camera2/cts/CaptureRequestTest.java b/tests/tests/hardware/src/android/hardware/camera2/cts/CaptureRequestTest.java
index ec2f95b..097d409 100644
--- a/tests/tests/hardware/src/android/hardware/camera2/cts/CaptureRequestTest.java
+++ b/tests/tests/hardware/src/android/hardware/camera2/cts/CaptureRequestTest.java
@@ -2108,15 +2108,18 @@
 
             resultListener = new SimpleCaptureCallback();
             startPreview(requestBuilder, previewSz, resultListener);
-            long[] frameDurationRange =
-                    new long[]{(long) (1e9 / fpsRange.getUpper()), (long) (1e9 / fpsRange.getLower())};
+            waitForSettingsApplied(resultListener, NUM_FRAMES_WAITED_FOR_UNKNOWN_LATENCY);
+
+            long[] frameDurationRange = new long[]{
+                    (long) (1e9 / fpsRange.getUpper()), (long) (1e9 / fpsRange.getLower())};
             for (int j = 0; j < numFramesVerified; j++) {
                 CaptureResult result =
                         resultListener.getCaptureResult(WAIT_FOR_RESULT_TIMEOUT_MS);
                 validatePipelineDepth(result);
                 long frameDuration = getValueNotNull(result, CaptureResult.SENSOR_FRAME_DURATION);
                 mCollector.expectInRange(
-                        "Frame duration must be in the range of " + Arrays.toString(frameDurationRange),
+                        "Frame duration must be in the range of " +
+                                Arrays.toString(frameDurationRange),
                         frameDuration,
                         (long) (frameDurationRange[0] * (1 - FRAME_DURATION_ERROR_MARGIN)),
                         (long) (frameDurationRange[1] * (1 + FRAME_DURATION_ERROR_MARGIN)));
diff --git a/tests/tests/hardware/src/android/hardware/camera2/cts/RobustnessTest.java b/tests/tests/hardware/src/android/hardware/camera2/cts/RobustnessTest.java
index 7960200..a6a7d10 100644
--- a/tests/tests/hardware/src/android/hardware/camera2/cts/RobustnessTest.java
+++ b/tests/tests/hardware/src/android/hardware/camera2/cts/RobustnessTest.java
@@ -21,7 +21,6 @@
 
 import android.graphics.ImageFormat;
 import android.graphics.SurfaceTexture;
-import android.hardware.camera2.CameraAccessException;
 import android.hardware.camera2.CameraCaptureSession;
 import android.hardware.camera2.CameraCharacteristics;
 import android.hardware.camera2.CameraDevice;
@@ -33,13 +32,12 @@
 import android.hardware.camera2.cts.helpers.StaticMetadata;
 import android.hardware.camera2.cts.testcases.Camera2AndroidTestCase;
 import android.media.CamcorderProfile;
+import android.media.Image;
 import android.media.ImageReader;
 import android.util.Log;
 import android.util.Size;
 import android.view.Surface;
 
-import com.android.ex.camera2.blocking.BlockingSessionCallback;
-
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.List;
@@ -324,6 +322,16 @@
         }
     }
 
+    private final class ImageCloser implements ImageReader.OnImageAvailableListener {
+        @Override
+        public void onImageAvailable(ImageReader reader) {
+            Image image = reader.acquireLatestImage();
+            if (image != null) {
+                image.close();
+            }
+        }
+    }
+
     private void testOutputCombination(String cameraId, int[] config, MaxOutputSizes maxSizes)
             throws Exception {
 
@@ -333,6 +341,7 @@
         final int TIMEOUT_FOR_RESULT_MS = 1000;
         final int MIN_RESULT_COUNT = 3;
 
+        ImageCloser imageCloser = new ImageCloser();
         // Set up outputs
         List<Object> outputTargets = new ArrayList<>();
         List<Surface> outputSurfaces = new ArrayList<>();
@@ -358,6 +367,7 @@
                     Size targetSize = maxSizes.maxJpegSizes[sizeLimit];
                     ImageReader target = ImageReader.newInstance(
                         targetSize.getWidth(), targetSize.getHeight(), JPEG, MIN_RESULT_COUNT);
+                    target.setOnImageAvailableListener(imageCloser, mHandler);
                     outputTargets.add(target);
                     outputSurfaces.add(target.getSurface());
                     jpegTargets.add(target);
@@ -367,6 +377,7 @@
                     Size targetSize = maxSizes.maxYuvSizes[sizeLimit];
                     ImageReader target = ImageReader.newInstance(
                         targetSize.getWidth(), targetSize.getHeight(), YUV, MIN_RESULT_COUNT);
+                    target.setOnImageAvailableListener(imageCloser, mHandler);
                     outputTargets.add(target);
                     outputSurfaces.add(target.getSurface());
                     yuvTargets.add(target);
@@ -376,6 +387,7 @@
                     Size targetSize = maxSizes.maxRawSize;
                     ImageReader target = ImageReader.newInstance(
                         targetSize.getWidth(), targetSize.getHeight(), RAW, MIN_RESULT_COUNT);
+                    target.setOnImageAvailableListener(imageCloser, mHandler);
                     outputTargets.add(target);
                     outputSurfaces.add(target.getSurface());
                     rawTargets.add(target);