Merge "Bugfix: loosen the tolerance to match aspect ratio" into androidx-main
diff --git a/camera/camera-view/src/androidTest/java/androidx/camera/view/PreviewTransformationDeviceTest.kt b/camera/camera-view/src/androidTest/java/androidx/camera/view/PreviewTransformationDeviceTest.kt
index 43554b2..c1c1c81 100644
--- a/camera/camera-view/src/androidTest/java/androidx/camera/view/PreviewTransformationDeviceTest.kt
+++ b/camera/camera-view/src/androidTest/java/androidx/camera/view/PreviewTransformationDeviceTest.kt
@@ -85,7 +85,7 @@
                 Rect(
                     0,
                     0,
-                    PREVIEW_VIEW_SIZE.height,
+                    PREVIEW_VIEW_SIZE.height + 1,
                     PREVIEW_VIEW_SIZE.width - 1
                 )
             )
@@ -99,7 +99,7 @@
                 Rect(
                     0,
                     0,
-                    PREVIEW_VIEW_SIZE.height,
+                    PREVIEW_VIEW_SIZE.height + 2,
                     PREVIEW_VIEW_SIZE.width - 2
                 )
             )
diff --git a/camera/camera-view/src/androidTest/java/androidx/camera/view/TransformUtilsDeviceTest.java b/camera/camera-view/src/androidTest/java/androidx/camera/view/TransformUtilsDeviceTest.java
new file mode 100644
index 0000000..4b327c1
--- /dev/null
+++ b/camera/camera-view/src/androidTest/java/androidx/camera/view/TransformUtilsDeviceTest.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.camera.view;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.graphics.Rect;
+import android.graphics.RectF;
+import android.util.Size;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.LargeTest;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Instrument test for {@link TransformUtils}
+ */
+@LargeTest
+@RunWith(AndroidJUnit4.class)
+public class TransformUtilsDeviceTest {
+
+    @Test
+    public void viewPortMatchAllowRoundingError() {
+        // Arrange: create two 1:1 crop rect. Due to rounding error, one is 11:9 and another is
+        // 9:11.
+        Rect cropRect1 = new Rect();
+        new RectF(0.4999f, 0.5f, 10.5f, 10.4999f).round(cropRect1);
+        Rect cropRect2 = new Rect();
+        new RectF(0.5f, 0.4999f, 10.4999f, 10.5f).round(cropRect2);
+
+        // Assert: they are within rounding error.
+        assertThat(TransformUtils.isAspectRatioMatchingWithRoundingError(
+                new Size(cropRect1.width(), cropRect1.height()), false,
+                new Size(cropRect2.width(), cropRect2.height()), false)).isTrue();
+    }
+}
diff --git a/camera/camera-view/src/androidTest/java/androidx/camera/view/transform/CoordinateTransformDeviceTest.kt b/camera/camera-view/src/androidTest/java/androidx/camera/view/transform/CoordinateTransformDeviceTest.kt
index 84e21f7..07163dc 100644
--- a/camera/camera-view/src/androidTest/java/androidx/camera/view/transform/CoordinateTransformDeviceTest.kt
+++ b/camera/camera-view/src/androidTest/java/androidx/camera/view/transform/CoordinateTransformDeviceTest.kt
@@ -32,21 +32,6 @@
 @RunWith(AndroidJUnit4::class)
 public class CoordinateTransformDeviceTest {
 
-    @Test(expected = IllegalArgumentException::class)
-    public fun mismatchViewPort_throwsException() {
-        // Arrange: create 2 imageProxy with mismatched viewport aspect ratio.
-        val imageProxyTransformFactory = ImageProxyTransformFactory.Builder().build()
-        val source = imageProxyTransformFactory.getOutputTransform(
-            createFakeImageProxy(300, 400, 0, Rect(0, 0, 300, 400))
-        )
-        val target = imageProxyTransformFactory.getOutputTransform(
-            createFakeImageProxy(300, 400, 0, Rect(0, 0, 200, 400))
-        )
-
-        // Act: creating CoordinateTransform throws exception.
-        CoordinateTransform(source, target)
-    }
-
     @Test
     public fun sameSourceAndTarget_getsIdentityMatrix() {
         // Arrange.
diff --git a/camera/camera-view/src/main/java/androidx/camera/view/TransformUtils.java b/camera/camera-view/src/main/java/androidx/camera/view/TransformUtils.java
index 4e06287..c0dc449 100644
--- a/camera/camera-view/src/main/java/androidx/camera/view/TransformUtils.java
+++ b/camera/camera-view/src/main/java/androidx/camera/view/TransformUtils.java
@@ -178,8 +178,9 @@
      */
     public static boolean isAspectRatioMatchingWithRoundingError(
             @NonNull Size size1, boolean isAccurate1, @NonNull Size size2, boolean isAccurate2) {
-        // The input width/height are rounded values, so they are at most .5 away from their
-        // true values.
+        // The crop rect coordinates are rounded values. Each value is at most .5 away from their
+        // true values. So the width/height, which is the difference of 2 coordinates, are at most
+        // 1.0 away from their true value.
         // First figure out the possible range of the aspect ratio's ture value.
         float ratio1UpperBound;
         float ratio1LowerBound;
@@ -187,8 +188,8 @@
             ratio1UpperBound = (float) size1.getWidth() / size1.getHeight();
             ratio1LowerBound = ratio1UpperBound;
         } else {
-            ratio1UpperBound = (size1.getWidth() + .5F) / (size1.getHeight() - .5F);
-            ratio1LowerBound = (size1.getWidth() - .5F) / (size1.getHeight() + .5F);
+            ratio1UpperBound = (size1.getWidth() + 1F) / (size1.getHeight() - 1F);
+            ratio1LowerBound = (size1.getWidth() - 1F) / (size1.getHeight() + 1F);
         }
         float ratio2UpperBound;
         float ratio2LowerBound;
@@ -196,8 +197,8 @@
             ratio2UpperBound = (float) size2.getWidth() / size2.getHeight();
             ratio2LowerBound = ratio2UpperBound;
         } else {
-            ratio2UpperBound = (size2.getWidth() + .5F) / (size2.getHeight() - .5F);
-            ratio2LowerBound = (size2.getWidth() - .5F) / (size2.getHeight() + .5F);
+            ratio2UpperBound = (size2.getWidth() + 1F) / (size2.getHeight() - 1F);
+            ratio2LowerBound = (size2.getWidth() - 1F) / (size2.getHeight() + 1F);
         }
         // Then we check if the true value range overlaps.
         return ratio1UpperBound >= ratio2LowerBound && ratio2UpperBound >= ratio1LowerBound;
diff --git a/camera/camera-view/src/main/java/androidx/camera/view/transform/CoordinateTransform.java b/camera/camera-view/src/main/java/androidx/camera/view/transform/CoordinateTransform.java
index 9c118dc..2b15595 100644
--- a/camera/camera-view/src/main/java/androidx/camera/view/transform/CoordinateTransform.java
+++ b/camera/camera-view/src/main/java/androidx/camera/view/transform/CoordinateTransform.java
@@ -23,12 +23,12 @@
 import androidx.annotation.NonNull;
 import androidx.annotation.RestrictTo;
 import androidx.camera.core.ImageAnalysis;
+import androidx.camera.core.Logger;
 import androidx.camera.core.UseCase;
 import androidx.camera.core.UseCaseGroup;
 import androidx.camera.core.ViewPort;
 import androidx.camera.view.PreviewView;
 import androidx.camera.view.TransformExperimental;
-import androidx.core.util.Preconditions;
 
 /**
  * This class represents the transform from one {@link OutputTransform} to another.
@@ -48,8 +48,9 @@
 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
 public class CoordinateTransform {
 
-    private static final String MISMATCH_MSG = "The source viewport does not match the target "
-            + "viewport. Please make sure they are from the same UseCaseGroup.";
+    private static final String TAG = "CoordinateTransform";
+    private static final String MISMATCH_MSG = "The source viewport (%s) does not match the target "
+            + "viewport (%s). Please make sure they are from the same UseCaseGroup.";
 
     private final Matrix mMatrix;
 
@@ -64,12 +65,18 @@
      */
     public CoordinateTransform(@NonNull OutputTransform source,
             @NonNull OutputTransform target) {
-        // Mismatched aspect ratio means the outputs are not from the same UseCaseGroup
-        Preconditions.checkArgument(
-                isAspectRatioMatchingWithRoundingError(
-                        source.getViewPortSize(), /* isAccurate1= */ false,
-                        target.getViewPortSize(), /* isAccurate2= */ false),
-                MISMATCH_MSG);
+        // TODO(b/137515129): This is a poor way to check if the two outputs are based on
+        //  the same viewport. A better way is to add a matrix in use case output that represents
+        //  the transform from sensor to surface. But it will require the view artifact to
+        //  depend on a new internal API in the core artifact, which we can't do at the
+        //  moment because of the version mismatch between view and core.
+        if (!isAspectRatioMatchingWithRoundingError(
+                source.getViewPortSize(), /* isAccurate1= */ false,
+                target.getViewPortSize(), /* isAccurate2= */ false)) {
+            // Mismatched aspect ratio means the outputs are not from the same UseCaseGroup
+            Logger.w(TAG, String.format(MISMATCH_MSG, source.getViewPortSize(),
+                    target.getViewPortSize()));
+        }
 
         // Concatenate the source transform with the target transform.
         mMatrix = new Matrix();