Add extended range brightness APIs

SurfaceControl & Display APIs to enable
arbitrary RANGE_EXTENDED HDR handling

Bug: 241001465
Test: make && silkfx demo
Change-Id: I1002910bc020f7177e82b3d77559103c1c2732cc
diff --git a/core/api/current.txt b/core/api/current.txt
index de540bf..026eb3c 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -49805,6 +49805,7 @@
     method public int getDisplayId();
     method public int getFlags();
     method public android.view.Display.HdrCapabilities getHdrCapabilities();
+    method public float getHdrSdrRatio();
     method @Deprecated public int getHeight();
     method @Deprecated public void getMetrics(android.util.DisplayMetrics);
     method public android.view.Display.Mode getMode();
@@ -49826,9 +49827,12 @@
     method @Deprecated public float[] getSupportedRefreshRates();
     method @Deprecated public int getWidth();
     method public boolean isHdr();
+    method public boolean isHdrSdrRatioAvailable();
     method public boolean isMinimalPostProcessingSupported();
     method public boolean isValid();
     method public boolean isWideColorGamut();
+    method public void registerHdrSdrRatioChangedListener(@NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.view.Display>);
+    method public void unregisterHdrSdrRatioChangedListener(@NonNull java.util.function.Consumer<android.view.Display>);
     field public static final int DEFAULT_DISPLAY = 0; // 0x0
     field public static final int FLAG_PRESENTATION = 8; // 0x8
     field public static final int FLAG_PRIVATE = 4; // 0x4
@@ -51357,6 +51361,7 @@
     method @NonNull public android.view.SurfaceControl.Transaction setCrop(@NonNull android.view.SurfaceControl, @Nullable android.graphics.Rect);
     method @NonNull public android.view.SurfaceControl.Transaction setDamageRegion(@NonNull android.view.SurfaceControl, @Nullable android.graphics.Region);
     method @NonNull public android.view.SurfaceControl.Transaction setDataSpace(@NonNull android.view.SurfaceControl, int);
+    method @NonNull public android.view.SurfaceControl.Transaction setExtendedRangeBrightness(@NonNull android.view.SurfaceControl, float, float);
     method @NonNull public android.view.SurfaceControl.Transaction setFrameRate(@NonNull android.view.SurfaceControl, @FloatRange(from=0.0) float, int);
     method @NonNull public android.view.SurfaceControl.Transaction setFrameRate(@NonNull android.view.SurfaceControl, @FloatRange(from=0.0) float, int, int);
     method @Deprecated @NonNull public android.view.SurfaceControl.Transaction setGeometry(@NonNull android.view.SurfaceControl, @Nullable android.graphics.Rect, @Nullable android.graphics.Rect, int);
diff --git a/core/java/android/view/Display.java b/core/java/android/view/Display.java
index 1563fc0..20be9d6 100644
--- a/core/java/android/view/Display.java
+++ b/core/java/android/view/Display.java
@@ -1298,8 +1298,6 @@
     /**
      * @return Whether the display supports reporting an hdr/sdr ratio. If this is false,
      *         {@link #getHdrSdrRatio()} will always be 1.0f
-     * @hide
-     * TODO: make public
      */
     public boolean isHdrSdrRatioAvailable() {
         synchronized (mLock) {
@@ -1312,9 +1310,6 @@
      * @return The current hdr/sdr ratio expressed as the ratio of targetHdrPeakBrightnessInNits /
      *         targetSdrWhitePointInNits. If {@link #isHdrSdrRatioAvailable()} is false, this
      *         always returns 1.0f.
-     *
-     * @hide
-     * TODO: make public
      */
     public float getHdrSdrRatio() {
         synchronized (mLock) {
@@ -1344,8 +1339,6 @@
      * @param executor The executor to invoke the listener on
      * @param listener The listener to invoke when the HDR/SDR ratio changes
      * @throws IllegalStateException if {@link #isHdrSdrRatioAvailable()} is false
-     * @hide
-     * TODO: Make public
      */
     public void registerHdrSdrRatioChangedListener(@NonNull Executor executor,
             @NonNull Consumer<Display> listener) {
@@ -1375,10 +1368,8 @@
      *                  hdr/sdr ratio listener to remove.
      *
      * @see #registerHdrSdrRatioChangedListener(Executor, Consumer)
-     * @hide
-     * TODO: Make public
      */
-    public void unregisterHdrSdrRatioChangedListener(Consumer<Display> listener) {
+    public void unregisterHdrSdrRatioChangedListener(@NonNull Consumer<Display> listener) {
         HdrSdrRatioListenerWrapper toRemove = null;
         synchronized (mLock) {
             int index = findHdrSdrRatioListenerLocked(listener);
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index 8663013..458e8c1 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -3789,7 +3789,7 @@
          * whose dataspace has RANGE_EXTENDED.
          *
          * @param sc The layer whose extended range brightness is being specified
-         * @param currentBufferRatio The current sdr/hdr ratio of the current buffer. For example
+         * @param currentBufferRatio The current hdr/sdr ratio of the current buffer. For example
          *                           if the buffer was rendered with a target SDR whitepoint of
          *                           100 nits and a max display brightness of 200 nits, this should
          *                           be set to 2.0f.
@@ -3801,7 +3801,9 @@
          *                           communicate extended content brightness information via
          *                           metadata such as CTA861_3 or SMPTE2086.
          *
-         * @param desiredRatio The desired sdr/hdr ratio. This can be used to communicate the max
+         *                           Must be finite && >= 1.0f
+         *
+         * @param desiredRatio The desired hdr/sdr ratio. This can be used to communicate the max
          *                     desired brightness range. This is similar to the "max luminance"
          *                     value in other HDR metadata formats, but represented as a ratio of
          *                     the target SDR whitepoint to the max display brightness. The system
@@ -3813,12 +3815,19 @@
          *                     voluntarily reducing the requested range can help improve battery
          *                     life as well as can improve quality by ensuring greater bit depth
          *                     is allocated to the luminance range in use.
+         *
+         *                     Must be finite && >= 1.0f
          * @return this
-         * @hide
          **/
         public @NonNull Transaction setExtendedRangeBrightness(@NonNull SurfaceControl sc,
                 float currentBufferRatio, float desiredRatio) {
             checkPreconditions(sc);
+            if (!Float.isFinite(currentBufferRatio) || currentBufferRatio < 1.0f) {
+                throw new IllegalArgumentException("currentBufferRatio must be finite && >= 1.0f");
+            }
+            if (!Float.isFinite(desiredRatio) || desiredRatio < 1.0f) {
+                throw new IllegalArgumentException("desiredRatio must be finite && >= 1.0f");
+            }
             nativeSetExtendedRangeBrightness(mNativeObject, sc.mNativeObject, currentBufferRatio,
                     desiredRatio);
             return this;
diff --git a/native/android/libandroid.map.txt b/native/android/libandroid.map.txt
index 987b23f..28fd040 100644
--- a/native/android/libandroid.map.txt
+++ b/native/android/libandroid.map.txt
@@ -275,6 +275,7 @@
     ASurfaceTransaction_setGeometry; # introduced=29
     ASurfaceTransaction_setHdrMetadata_cta861_3; # introduced=29
     ASurfaceTransaction_setHdrMetadata_smpte2086; # introduced=29
+    ASurfaceTransaction_setExtendedRangeBrightness; # introduced=UpsideDownCake
     ASurfaceTransaction_setOnComplete; # introduced=29
     ASurfaceTransaction_setOnCommit; # introduced=31
     ASurfaceTransaction_setPosition; # introduced=31
diff --git a/native/android/surface_control.cpp b/native/android/surface_control.cpp
index ea20c6c..58de02c 100644
--- a/native/android/surface_control.cpp
+++ b/native/android/surface_control.cpp
@@ -609,6 +609,30 @@
     transaction->setHdrMetadata(surfaceControl, hdrMetadata);
 }
 
+void ASurfaceTransaction_setExtendedRangeBrightness(ASurfaceTransaction* aSurfaceTransaction,
+                                                    ASurfaceControl* aSurfaceControl,
+                                                    float currentBufferRatio, float desiredRatio) {
+    CHECK_NOT_NULL(aSurfaceTransaction);
+    CHECK_NOT_NULL(aSurfaceControl);
+
+    if (!isfinite(currentBufferRatio) || currentBufferRatio < 1.0f) {
+        ALOGE("Ignore setExtendedRangeBrightness, currentBufferRatio %f isn't finite or >= 1.0f",
+              currentBufferRatio);
+        return;
+    }
+
+    if (!isfinite(desiredRatio) || desiredRatio < 1.0f) {
+        ALOGE("Ignore setExtendedRangeBrightness, desiredRatio %f isn't finite or >= 1.0f",
+              desiredRatio);
+        return;
+    }
+
+    sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
+    Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
+
+    transaction->setExtendedRangeBrightness(surfaceControl, currentBufferRatio, desiredRatio);
+}
+
 void ASurfaceTransaction_setColor(ASurfaceTransaction* aSurfaceTransaction,
                                   ASurfaceControl* aSurfaceControl,
                                   float r, float g, float b, float alpha,