Merge "CTS: fix VulkanPreTransformTest on HiKey960"
diff --git a/tests/tests/graphics/AndroidManifest.xml b/tests/tests/graphics/AndroidManifest.xml
index dd80315..76552a1 100644
--- a/tests/tests/graphics/AndroidManifest.xml
+++ b/tests/tests/graphics/AndroidManifest.xml
@@ -38,7 +38,6 @@
 
         <activity android:name="android.graphics.cts.VulkanPreTransformCtsActivity"
             android:label="VulkanPreTransformCtsActivity"
-            android:screenOrientation="landscape"
             android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
         </activity>
 
diff --git a/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformCtsActivity.java b/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformCtsActivity.java
index 591c280..4b9aa0e 100644
--- a/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformCtsActivity.java
+++ b/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformCtsActivity.java
@@ -17,11 +17,15 @@
 package android.graphics.cts;
 
 import android.app.Activity;
+import android.content.Context;
+import android.content.pm.ActivityInfo;
 import android.content.res.AssetManager;
+import android.content.res.Configuration;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Surface;
 import android.view.SurfaceView;
+import android.view.WindowManager;
 
 /**
  * Activity for VulkanPreTransformTest.
@@ -33,19 +37,51 @@
 
     private static final String TAG = VulkanPreTransformCtsActivity.class.getSimpleName();
 
+    private static boolean sOrientationRequested = false;
+    private static boolean sClockwiseRotation = false;
+
     protected Surface mSurface;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         Log.d(TAG, "onCreate!");
+        setActivityOrientation();
         setContentView(R.layout.vulkan_pretransform_layout);
         SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
         mSurface = surfaceView.getHolder().getSurface();
     }
 
+    private void setActivityOrientation() {
+        if (sOrientationRequested) {
+            // it might be called again because changing the orientation kicks off onCreate again!.
+            return;
+        }
+
+        if (((WindowManager) getSystemService(Context.WINDOW_SERVICE))
+                        .getDefaultDisplay()
+                        .getRotation()
+                != Surface.ROTATION_0) {
+            throw new RuntimeException("Display not in natural orientation");
+        }
+
+        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
+            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+            sClockwiseRotation = true;
+        } else {
+            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
+        }
+        sOrientationRequested = true;
+    }
+
+    public boolean getRotation() {
+        return sClockwiseRotation;
+    }
+
     public void testVulkanPreTransform(boolean setPreTransform) {
         nCreateNativeTest(getAssets(), mSurface, setPreTransform);
+        sOrientationRequested = false;
+        sClockwiseRotation = false;
     }
 
     private static native void nCreateNativeTest(
diff --git a/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformTest.java b/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformTest.java
index c220867..7b87948 100644
--- a/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformTest.java
+++ b/tests/tests/graphics/src/android/graphics/cts/VulkanPreTransformTest.java
@@ -38,10 +38,11 @@
 import org.junit.runner.RunWith;
 
 /*
- * This test case runs in landscape mode
- *
  * testVulkanPreTransformSetToMatchCurrentTransform()
  *
+ *   This test case runs in landscape mode for portrait device (default orientation is portrait),
+ *   the result is 90 degree CCW rotation.
+ *
  *      Buffer          ExpectedScreen
  *      ---------       ---------------
  *      | R | G |       | GGGG | YYYY |
@@ -49,6 +50,18 @@
  *      | B | Y |       | RRRR | BBBB |
  *      ---------       ---------------
  *
+ *   This test case runs in portrait mode for landscape device (default orientation is landscape),
+ *   the result is 90 degree CW rotation.
+ *
+ *      Buffer          ExpectedScreen
+ *      ---------       -----------
+ *      | R | G |       | BB | RR |
+ *      ---------       | BB | RR |
+ *      | B | Y |       -----------
+ *      ---------       | YY | GG |
+ *                      | YY | GG |
+ *                      -----------
+ *
  * testVulkanPreTransformNotSetToMatchCurrentTransform()
  *
  *      Buffer          ExpectedScreen
@@ -126,11 +139,19 @@
         int width = bitmap.getWidth();
         int height = bitmap.getHeight();
         int diff = 0;
-        if (setPreTransform) {
+        boolean clockwiseRotation = sActivity.getRotation();
+        if (setPreTransform && !clockwiseRotation) {
+            // app orientation landscape
             diff += pixelDiff(bitmap.getPixel(0, 0), 0, 255, 0);
             diff += pixelDiff(bitmap.getPixel(width - 1, 0), 255, 255, 0);
             diff += pixelDiff(bitmap.getPixel(0, height - 1), 255, 0, 0);
             diff += pixelDiff(bitmap.getPixel(width - 1, height - 1), 0, 0, 255);
+        } else if (setPreTransform && clockwiseRotation) {
+            // app orientation portrait
+            diff += pixelDiff(bitmap.getPixel(0, 0), 0, 0, 255);
+            diff += pixelDiff(bitmap.getPixel(width - 1, 0), 255, 0, 0);
+            diff += pixelDiff(bitmap.getPixel(0, height - 1), 255, 255, 0);
+            diff += pixelDiff(bitmap.getPixel(width - 1, height - 1), 0, 255, 0);
         } else {
             diff += pixelDiff(bitmap.getPixel(0, 0), 255, 0, 0);
             diff += pixelDiff(bitmap.getPixel(width - 1, 0), 0, 255, 0);