Add setColorSpaceAgnostic API to SurfaceControl.

Some layers, for example ScreenDecorOverlay*, only carry black, white or gray
with some transpanrency, these values are special as they are color space
agnostic. We don't need to do color conversion on them, instead we want to
intercept the color space before we send to hardware composer for validation.
This patch adds an API to allow this to happen, layers that can be considered
color space agnostic can call this API.

BUG: 126616348
Test: Build, flash and boot. Verify by calling in Letterbox.
Change-Id: I2e1384865bbae6c1bca5ab55962af6a4d529d81d
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index 54e70ea..5e2aaae 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -117,6 +117,8 @@
             float dtdy, float dsdy);
     private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
             float[] matrix, float[] translation);
+    private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject,
+            boolean agnostic);
     private static native void nativeSetGeometry(long transactionObj, long nativeObject,
             Rect sourceCrop, Rect dest, long orientation);
     private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
@@ -1207,6 +1209,19 @@
     }
 
     /**
+     * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
+     * the color can be interpreted in any color space.
+     * @param agnostic A boolean to indicate whether the surface is color space agnostic
+     * @hide
+     */
+    public void setColorSpaceAgnostic(boolean agnostic) {
+        checkNotReleased();
+        synchronized (SurfaceControl.class) {
+            sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
+        }
+    }
+
+    /**
      * Bounds the surface and its children to the bounds specified. Size of the surface will be
      * ignored and only the crop and buffer size will be used to determine the bounds of the
      * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
@@ -2227,6 +2242,18 @@
         }
 
         /**
+         * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
+         * the color can be interpreted in any color space.
+         * @param agnostic A boolean to indicate whether the surface is color space agnostic
+         * @hide
+         */
+        public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
+            sc.checkNotReleased();
+            nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
+            return this;
+        }
+
+        /**
          * @hide
          */
         @UnsupportedAppUsage
diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp
index af2bf2d..89d908b 100644
--- a/core/jni/android_view_SurfaceControl.cpp
+++ b/core/jni/android_view_SurfaceControl.cpp
@@ -463,6 +463,13 @@
     transaction->setColorTransform(surfaceControl, matrix, translation);
 }
 
+static void nativeSetColorSpaceAgnostic(JNIEnv* env, jclass clazz, jlong transactionObj,
+        jlong nativeObject, jboolean agnostic) {
+    auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
+    SurfaceControl* const surfaceControl = reinterpret_cast<SurfaceControl*>(nativeObject);
+    transaction->setColorSpaceAgnostic(surfaceControl, agnostic);
+}
+
 static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong transactionObj,
         jlong nativeObject,
         jint l, jint t, jint r, jint b) {
@@ -1206,6 +1213,8 @@
             (void*)nativeSetMatrix },
     {"nativeSetColorTransform", "(JJ[F[F)V",
             (void*)nativeSetColorTransform },
+    {"nativeSetColorSpaceAgnostic", "(JJZ)V",
+            (void*)nativeSetColorSpaceAgnostic },
     {"nativeSetFlags", "(JJII)V",
             (void*)nativeSetFlags },
     {"nativeSetWindowCrop", "(JJIIII)V",
diff --git a/services/core/java/com/android/server/wm/Letterbox.java b/services/core/java/com/android/server/wm/Letterbox.java
index f8f693c..3110fb9 100644
--- a/services/core/java/com/android/server/wm/Letterbox.java
+++ b/services/core/java/com/android/server/wm/Letterbox.java
@@ -236,6 +236,7 @@
                     .setFlags(HIDDEN).setColorLayer().build();
             mSurface.setLayer(-1);
             mSurface.setColor(new float[]{0, 0, 0});
+            mSurface.setColorSpaceAgnostic(true);
         }
 
         void attachInput(WindowState win) {