Expose OverlayProperties class and some APIs.

 - Expose Display#getOverlaySupport
 - Expose OverlayProperties#supportMixedColorSpaces
 - For virtual displays, we provide a default overlay properties with
   RGBA8888, SRGB and true for mixed color spaces support because it's
   always GPU.
 - Add @FlaggedApi for trunk stable release

Bug: 267234573
Test: builds
Change-Id: If2701d536cd1e2ff5d0d95993a3d2b00bff541c5
diff --git a/AconfigFlags.bp b/AconfigFlags.bp
index b924ac8..4f5e79c 100644
--- a/AconfigFlags.bp
+++ b/AconfigFlags.bp
@@ -17,6 +17,7 @@
     ":android.companion.flags-aconfig-java{.generated_srcjars}",
     ":android.content.pm.flags-aconfig-java{.generated_srcjars}",
     ":android.content.res.flags-aconfig-java{.generated_srcjars}",
+    ":android.hardware.flags-aconfig-java{.generated_srcjars}",
     ":android.hardware.radio.flags-aconfig-java{.generated_srcjars}",
     ":android.nfc.flags-aconfig-java{.generated_srcjars}",
     ":android.os.flags-aconfig-java{.generated_srcjars}",
@@ -276,6 +277,19 @@
     aconfig_declarations: "android.view.accessibility.flags-aconfig",
 }
 
+// Hardware
+aconfig_declarations {
+    name: "android.hardware.flags-aconfig",
+    package: "android.hardware.flags",
+    srcs: ["core/java/android/hardware/flags/*.aconfig"],
+}
+
+java_aconfig_library {
+    name: "android.hardware.flags-aconfig-java",
+    aconfig_declarations: "android.hardware.flags-aconfig",
+    defaults: ["framework-minus-apex-aconfig-java-defaults"],
+}
+
 // Widget
 aconfig_declarations {
     name: "android.widget.flags-aconfig",
diff --git a/core/api/current.txt b/core/api/current.txt
index 00f0718..eb78d59 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -18172,6 +18172,13 @@
     field public static final int YCBCR_P010 = 54; // 0x36
   }
 
+  @FlaggedApi("android.hardware.flags.overlayproperties_class_api") public final class OverlayProperties implements android.os.Parcelable {
+    method @FlaggedApi("android.hardware.flags.overlayproperties_class_api") public int describeContents();
+    method @FlaggedApi("android.hardware.flags.overlayproperties_class_api") public boolean supportMixedColorSpaces();
+    method @FlaggedApi("android.hardware.flags.overlayproperties_class_api") public void writeToParcel(@NonNull android.os.Parcel, int);
+    field @FlaggedApi("android.hardware.flags.overlayproperties_class_api") @NonNull public static final android.os.Parcelable.Creator<android.hardware.OverlayProperties> CREATOR;
+  }
+
   public final class Sensor {
     method public int getFifoMaxEventCount();
     method public int getFifoReservedEventCount();
@@ -49851,6 +49858,7 @@
     method public android.view.Display.Mode getMode();
     method public String getName();
     method @Deprecated public int getOrientation();
+    method @FlaggedApi("android.hardware.flags.overlayproperties_class_api") @NonNull public android.hardware.OverlayProperties getOverlaySupport();
     method @Deprecated public int getPixelFormat();
     method @Nullable public android.graphics.ColorSpace getPreferredWideGamutColorSpace();
     method public long getPresentationDeadlineNanos();
diff --git a/core/java/android/hardware/OverlayProperties.java b/core/java/android/hardware/OverlayProperties.java
index 8bfc2f7..014cf6d 100644
--- a/core/java/android/hardware/OverlayProperties.java
+++ b/core/java/android/hardware/OverlayProperties.java
@@ -16,21 +16,28 @@
 
 package android.hardware;
 
+import android.annotation.FlaggedApi;
 import android.annotation.NonNull;
+import android.hardware.flags.Flags;
 import android.os.Parcel;
 import android.os.Parcelable;
 
 import libcore.util.NativeAllocationRegistry;
 
 /**
- * The class provides overlay properties of the device. OverlayProperties
- * exposes some capabilities from HWC e.g. if fp16 can be supported for HWUI.
+ * Provides supported overlay properties of the device.
  *
- * In the future, more capabilities can be added, e.g., whether or not
- * per-layer colorspaces are supported.
- *
- * @hide
+ * <p>
+ * Hardware overlay is a technique to composite different buffers directly
+ * to the screen using display hardware rather than the GPU.
+ * The system compositor is able to assign any content managed by a
+ * {@link android.view.SurfaceControl} onto a hardware overlay if possible.
+ * Applications may be interested in the display hardware capabilities exposed
+ * by this class as a hint to determine if their {@link android.view.SurfaceControl}
+ * tree is power-efficient and performant.
+ * </p>
  */
+@FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API)
 public final class OverlayProperties implements Parcelable {
 
     private static final NativeAllocationRegistry sRegistry =
@@ -38,10 +45,12 @@
             nGetDestructor());
 
     private long mNativeObject;
+    // only for virtual displays
+    private static OverlayProperties sDefaultOverlayProperties;
     // Invoked on destruction
     private Runnable mCloser;
 
-    public OverlayProperties(long nativeObject) {
+    private OverlayProperties(long nativeObject) {
         if (nativeObject != 0) {
             mCloser = sRegistry.registerNativeAllocation(this, nativeObject);
         }
@@ -49,7 +58,20 @@
     }
 
     /**
+     * For virtual displays, we provide an overlay properties object
+     * with RGBA 8888 only, sRGB only, true for mixed color spaces.
+     * @hide
+     */
+    public static OverlayProperties getDefault() {
+        if (sDefaultOverlayProperties == null) {
+            sDefaultOverlayProperties = new OverlayProperties(nCreateDefault());
+        }
+        return sDefaultOverlayProperties;
+    }
+
+    /**
      * @return True if the device can support fp16, false otherwise.
+     * @hide
      */
     public boolean supportFp16ForHdr() {
         if (mNativeObject == 0) {
@@ -59,8 +81,13 @@
     }
 
     /**
-     * @return True if the device can support mixed colorspaces, false otherwise.
+     * Indicates that hw composition of two or more overlays
+     * with different colorspaces is supported on the device.
+     *
+     * @return True if the device can support mixed colorspaces efficiently,
+     *         false if GPU composition fallback is otherwise required.
      */
+    @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API)
     public boolean supportMixedColorSpaces() {
         if (mNativeObject == 0) {
             return false;
@@ -68,28 +95,14 @@
         return nSupportMixedColorSpaces(mNativeObject);
     }
 
-    /**
-     * Release the local reference.
-     */
-    public void release() {
-        if (mNativeObject != 0) {
-            mCloser.run();
-            mNativeObject = 0;
-        }
-    }
 
+    @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API)
     @Override
     public int describeContents() {
         return 0;
     }
 
-    /**
-     * Flatten this object in to a Parcel.
-     *
-     * @param dest The Parcel in which the object should be written.
-     * @param flags Additional flags about how the object should be written.
-     *              May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
-     */
+    @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API)
     @Override
     public void writeToParcel(@NonNull Parcel dest, int flags) {
         if (mNativeObject == 0) {
@@ -100,6 +113,7 @@
         nWriteOverlayPropertiesToParcel(mNativeObject, dest);
     }
 
+    @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API)
     public static final @NonNull Parcelable.Creator<OverlayProperties> CREATOR =
             new Parcelable.Creator<OverlayProperties>() {
         public OverlayProperties createFromParcel(Parcel in) {
@@ -115,6 +129,7 @@
     };
 
     private static native long nGetDestructor();
+    private static native long nCreateDefault();
     private static native boolean nSupportFp16ForHdr(long nativeObject);
     private static native boolean nSupportMixedColorSpaces(long nativeObject);
     private static native void nWriteOverlayPropertiesToParcel(long nativeObject, Parcel dest);
diff --git a/core/java/android/hardware/flags/overlayproperties_flags.aconfig b/core/java/android/hardware/flags/overlayproperties_flags.aconfig
new file mode 100644
index 0000000..c6a352e
--- /dev/null
+++ b/core/java/android/hardware/flags/overlayproperties_flags.aconfig
@@ -0,0 +1,8 @@
+package: "android.hardware.flags"
+
+flag {
+    name: "overlayproperties_class_api"
+    namespace: "core_graphics"
+    description: "public OverlayProperties class, OverlayProperties#supportMixedColorSpaces and Display#getOverlaySupport API"
+    bug: "267234573"
+}
diff --git a/core/java/android/view/Display.java b/core/java/android/view/Display.java
index 17c7fcc..9bee077 100644
--- a/core/java/android/view/Display.java
+++ b/core/java/android/view/Display.java
@@ -18,8 +18,10 @@
 
 import static android.Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE;
 import static android.Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS;
+import static android.hardware.flags.Flags.FLAG_OVERLAYPROPERTIES_CLASS_API;
 
 import android.Manifest;
+import android.annotation.FlaggedApi;
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -1468,17 +1470,18 @@
     }
 
     /**
-     * Returns null if it's virtual display.
-     * @hide
+     * Returns the {@link OverlayProperties} of the display.
      */
-    @Nullable
+    @FlaggedApi(FLAG_OVERLAYPROPERTIES_CLASS_API)
+    @NonNull
     public OverlayProperties getOverlaySupport() {
         synchronized (mLock) {
             updateDisplayInfoLocked();
-            if (mDisplayInfo.type != TYPE_VIRTUAL) {
+            if (mDisplayInfo.type == TYPE_INTERNAL
+                    || mDisplayInfo.type == TYPE_EXTERNAL) {
                 return mGlobal.getOverlaySupport();
             }
-            return null;
+            return OverlayProperties.getDefault();
         }
     }
 
diff --git a/core/jni/android_hardware_OverlayProperties.cpp b/core/jni/android_hardware_OverlayProperties.cpp
index 34ef7b3..5b95ee7 100644
--- a/core/jni/android_hardware_OverlayProperties.cpp
+++ b/core/jni/android_hardware_OverlayProperties.cpp
@@ -85,6 +85,18 @@
     return false;
 }
 
+static jlong android_hardware_OverlayProperties_createDefault(JNIEnv* env, jobject thiz) {
+    gui::OverlayProperties* overlayProperties = new gui::OverlayProperties;
+    gui::OverlayProperties::SupportedBufferCombinations combination;
+    combination.pixelFormats = {HAL_PIXEL_FORMAT_RGBA_8888};
+    combination.standards = {HAL_DATASPACE_BT709};
+    combination.transfers = {HAL_DATASPACE_TRANSFER_SRGB};
+    combination.ranges = {HAL_DATASPACE_RANGE_FULL};
+    overlayProperties->combinations.emplace_back(combination);
+    overlayProperties->supportMixedColorSpaces = true;
+    return reinterpret_cast<jlong>(overlayProperties);
+}
+
 // ----------------------------------------------------------------------------
 // Serialization
 // ----------------------------------------------------------------------------
@@ -150,6 +162,7 @@
             (void*) android_hardware_OverlayProperties_write },
     { "nReadOverlayPropertiesFromParcel", "(Landroid/os/Parcel;)J",
             (void*) android_hardware_OverlayProperties_read },
+    {"nCreateDefault", "()J", (void*) android_hardware_OverlayProperties_createDefault },
 };
 // clang-format on