diff --git a/core/java/android/content/pm/ActivityInfo.java b/core/java/android/content/pm/ActivityInfo.java index db72e29efde19..f8f2663063a6a 100644 --- a/core/java/android/content/pm/ActivityInfo.java +++ b/core/java/android/content/pm/ActivityInfo.java @@ -350,7 +350,14 @@ public class ActivityInfo extends ComponentInfo implements Parcelable { * @see android.R.attr#colorMode */ public static final int COLOR_MODE_HDR = 2; - // 3 Corresponds to android::uirenderer::ColorMode::Hdr10. + + /** + * Comparison point against COLOR_MODE_HDR that uses 1010102 + * Only for internal test usages + * @hide + */ + public static final int COLOR_MODE_HDR10 = 3; + /** * Value of {@link #colorMode} indicating that the activity should use an * 8 bit alpha buffer if the presentation display supports it. diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 480abe03b5f3a..6b0f8509b8aa7 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -683,9 +683,10 @@ public final class ViewRootImpl implements ViewParent, private BLASTBufferQueue mBlastBufferQueue; - private boolean mUpdateSdrHdrRatioInfo = false; - private float mDesiredSdrHdrRatio = 1f; - private float mRenderSdrHdrRatio = 1f; + private boolean mUpdateHdrSdrRatioInfo = false; + private float mDesiredHdrSdrRatio = 1f; + private float mRenderHdrSdrRatio = 1f; + private Consumer mHdrSdrRatioChangedListener = null; /** * Child container layer of {@code mSurface} with the same bounds as its parent, and cropped to @@ -1947,6 +1948,9 @@ public final class ViewRootImpl implements ViewParent, private void updateInternalDisplay(int displayId, Resources resources) { final Display preferredDisplay = ResourcesManager.getInstance().getAdjustedDisplay(displayId, resources); + if (mHdrSdrRatioChangedListener != null && mDisplay != null) { + mDisplay.unregisterHdrSdrRatioChangedListener(mHdrSdrRatioChangedListener); + } if (preferredDisplay == null) { // Fallback to use default display. Slog.w(TAG, "Cannot get desired display with Id: " + displayId); @@ -1955,6 +1959,9 @@ public final class ViewRootImpl implements ViewParent, } else { mDisplay = preferredDisplay; } + if (mHdrSdrRatioChangedListener != null && mDisplay != null) { + mDisplay.registerHdrSdrRatioChangedListener(mExecutor, mHdrSdrRatioChangedListener); + } mContext.updateDisplay(mDisplay.getDisplayId()); } @@ -4838,11 +4845,11 @@ public final class ViewRootImpl implements ViewParent, useAsyncReport = true; - if (mUpdateSdrHdrRatioInfo) { - mUpdateSdrHdrRatioInfo = false; + if (mUpdateHdrSdrRatioInfo) { + mUpdateHdrSdrRatioInfo = false; applyTransactionOnDraw(mTransaction.setExtendedRangeBrightness( - getSurfaceControl(), mRenderSdrHdrRatio, mDesiredSdrHdrRatio)); - mAttachInfo.mThreadedRenderer.setTargetSdrHdrRatio(mRenderSdrHdrRatio); + getSurfaceControl(), mRenderHdrSdrRatio, mDesiredHdrSdrRatio)); + mAttachInfo.mThreadedRenderer.setTargetHdrSdrRatio(mRenderHdrSdrRatio); } if (forceDraw) { @@ -5372,6 +5379,10 @@ public final class ViewRootImpl implements ViewParent, if (mAttachInfo.mThreadedRenderer == null) { return; } + if ((colorMode == ActivityInfo.COLOR_MODE_HDR || colorMode == ActivityInfo.COLOR_MODE_HDR10) + && !mDisplay.isHdrSdrRatioAvailable()) { + colorMode = ActivityInfo.COLOR_MODE_WIDE_COLOR_GAMUT; + } // TODO: Centralize this sanitization? Why do we let setting bad modes? // Alternatively, can we just let HWUI figure it out? Do we need to care here? if (colorMode != ActivityInfo.COLOR_MODE_A8 @@ -5379,17 +5390,28 @@ public final class ViewRootImpl implements ViewParent, colorMode = ActivityInfo.COLOR_MODE_DEFAULT; } float desiredRatio = mAttachInfo.mThreadedRenderer.setColorMode(colorMode); - if (desiredRatio != mDesiredSdrHdrRatio) { - mDesiredSdrHdrRatio = desiredRatio; - mUpdateSdrHdrRatioInfo = true; + if (desiredRatio != mDesiredHdrSdrRatio) { + mDesiredHdrSdrRatio = desiredRatio; + mRenderHdrSdrRatio = mDisplay.getHdrSdrRatio(); + mUpdateHdrSdrRatioInfo = true; + + if (mDesiredHdrSdrRatio < 1.01f) { + mDisplay.unregisterHdrSdrRatioChangedListener(mHdrSdrRatioChangedListener); + mHdrSdrRatioChangedListener = null; + } else { + mHdrSdrRatioChangedListener = display -> { + setTargetHdrSdrRatio(display.getHdrSdrRatio()); + }; + mDisplay.registerHdrSdrRatioChangedListener(mExecutor, mHdrSdrRatioChangedListener); + } } } /** happylint */ - public void setTargetSdrHdrRatio(float ratio) { - if (mRenderSdrHdrRatio != ratio) { - mRenderSdrHdrRatio = ratio; - mUpdateSdrHdrRatioInfo = true; + public void setTargetHdrSdrRatio(float ratio) { + if (mRenderHdrSdrRatio != ratio) { + mRenderHdrSdrRatio = ratio; + mUpdateHdrSdrRatioInfo = true; invalidate(); } } @@ -5965,6 +5987,9 @@ public final class ViewRootImpl implements ViewParent, } final ViewRootHandler mHandler = new ViewRootHandler(); + private final Executor mExecutor = (Runnable r) -> { + mHandler.post(r); + }; /** * Something in the current window tells us we need to change the touch mode. For @@ -8950,6 +8975,10 @@ public final class ViewRootImpl implements ViewParent, private void destroyHardwareRenderer() { ThreadedRenderer hardwareRenderer = mAttachInfo.mThreadedRenderer; + if (mHdrSdrRatioChangedListener != null) { + mDisplay.unregisterHdrSdrRatioChangedListener(mHdrSdrRatioChangedListener); + } + if (hardwareRenderer != null) { if (mHardwareRendererObserver != null) { hardwareRenderer.removeObserver(mHardwareRendererObserver); diff --git a/graphics/java/android/graphics/HardwareRenderer.java b/graphics/java/android/graphics/HardwareRenderer.java index 0488b9d8b2012..9ed3d9c3c94b6 100644 --- a/graphics/java/android/graphics/HardwareRenderer.java +++ b/graphics/java/android/graphics/HardwareRenderer.java @@ -667,7 +667,7 @@ public class HardwareRenderer { } /** @hide */ - public void setTargetSdrHdrRatio(float ratio) { + public void setTargetHdrSdrRatio(float ratio) { if (ratio < 1.f || !Float.isFinite(ratio)) ratio = 1.f; nSetTargetSdrHdrRatio(mNativeProxy, ratio); } diff --git a/tests/SilkFX/src/com/android/test/silkfx/common/ColorModeControls.kt b/tests/SilkFX/src/com/android/test/silkfx/common/ColorModeControls.kt index 1bd8f6a68cbd7..56ab755af47bb 100644 --- a/tests/SilkFX/src/com/android/test/silkfx/common/ColorModeControls.kt +++ b/tests/SilkFX/src/com/android/test/silkfx/common/ColorModeControls.kt @@ -18,10 +18,10 @@ package com.android.test.silkfx.common import android.content.Context import android.content.pm.ActivityInfo -import android.hardware.display.DisplayManager import android.util.AttributeSet import android.util.Log import android.view.Display +import android.view.View import android.view.Window import android.widget.Button import android.widget.LinearLayout @@ -31,22 +31,11 @@ import com.android.test.silkfx.app.WindowObserver import java.util.function.Consumer class ColorModeControls : LinearLayout, WindowObserver { - private val COLOR_MODE_HDR10 = 3 - private val SDR_WHITE_POINTS = floatArrayOf(200f, 250f, 300f, 350f, 400f, 100f, 150f) - constructor(context: Context) : this(context, null) - constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { - displayManager = context.getSystemService(DisplayManager::class.java)!! - displayId = context.getDisplayId() - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) private var window: Window? = null private var currentModeDisplay: TextView? = null - private val displayManager: DisplayManager - private var targetSdrWhitePointIndex = 0 - private var displayId: Int - - private val whitePoint get() = SDR_WHITE_POINTS[targetSdrWhitePointIndex] override fun onFinishInflate() { super.onFinishInflate() @@ -65,88 +54,53 @@ class ColorModeControls : LinearLayout, WindowObserver { setColorMode(ActivityInfo.COLOR_MODE_HDR) } findViewById