diff --git a/core/java/android/view/Display.java b/core/java/android/view/Display.java index 5476088992594..baefd853876b2 100644 --- a/core/java/android/view/Display.java +++ b/core/java/android/view/Display.java @@ -1218,47 +1218,51 @@ public final class Display { } /** - * Returns the display's HDR capabilities. + * Returns the current display mode's HDR capabilities. * * @see #isHdr() */ public HdrCapabilities getHdrCapabilities() { synchronized (mLock) { updateDisplayInfoLocked(); - if (mDisplayInfo.userDisabledHdrTypes.length == 0) { - return mDisplayInfo.hdrCapabilities; - } - if (mDisplayInfo.hdrCapabilities == null) { return null; } - - ArraySet enabledTypesSet = new ArraySet<>(); - for (int supportedType : mDisplayInfo.hdrCapabilities.getSupportedHdrTypes()) { - boolean typeDisabled = false; - for (int userDisabledType : mDisplayInfo.userDisabledHdrTypes) { - if (supportedType == userDisabledType) { - typeDisabled = true; - break; + int[] supportedHdrTypes; + if (mDisplayInfo.userDisabledHdrTypes.length == 0) { + int[] modeSupportedHdrTypes = getMode().getSupportedHdrTypes(); + supportedHdrTypes = Arrays.copyOf(modeSupportedHdrTypes, + modeSupportedHdrTypes.length); + } else { + ArraySet enabledTypesSet = new ArraySet<>(); + for (int supportedType : getMode().getSupportedHdrTypes()) { + if (!contains(mDisplayInfo.userDisabledHdrTypes, supportedType)) { + enabledTypesSet.add(supportedType); } } - if (!typeDisabled) { - enabledTypesSet.add(supportedType); + + supportedHdrTypes = new int[enabledTypesSet.size()]; + int index = 0; + for (int enabledType : enabledTypesSet) { + supportedHdrTypes[index++] = enabledType; } } - - int[] enabledTypes = new int[enabledTypesSet.size()]; - int index = 0; - for (int enabledType : enabledTypesSet) { - enabledTypes[index++] = enabledType; - } - return new HdrCapabilities(enabledTypes, + return new HdrCapabilities(supportedHdrTypes, mDisplayInfo.hdrCapabilities.mMaxLuminance, mDisplayInfo.hdrCapabilities.mMaxAverageLuminance, mDisplayInfo.hdrCapabilities.mMinLuminance); } } + private boolean contains(int[] disabledHdrTypes, int hdrType) { + for (Integer disabledHdrFormat : disabledHdrTypes) { + if (disabledHdrFormat == hdrType) { + return true; + } + } + return false; + } + /** * @hide * Returns the current mode's supported HDR types. diff --git a/core/tests/mockingcoretests/src/android/view/DisplayTest.java b/core/tests/mockingcoretests/src/android/view/DisplayTest.java index 3b8b8c7972415..4a12bb374cb20 100644 --- a/core/tests/mockingcoretests/src/android/view/DisplayTest.java +++ b/core/tests/mockingcoretests/src/android/view/DisplayTest.java @@ -141,6 +141,31 @@ public class DisplayTest { assertArrayEquals(hdrTypesWithoutDv, display.getReportedHdrTypes()); } + @Test + public void testGetHdrCapabilities_getSupportedHdrTypes_returns_mode_specific_hdr_types() { + setDisplayInfoPortrait(mDisplayInfo); + float[] alternativeRefreshRates = new float[0]; + int[] hdrTypesWithDv = new int[] {1, 2, 3, 4}; + Display.Mode modeWithDv = new Display.Mode(/* modeId= */ 0, 0, 0, 0f, + alternativeRefreshRates, hdrTypesWithDv); + + int[] hdrTypesWithoutDv = new int[]{2, 3, 4}; + Display.Mode modeWithoutDv = new Display.Mode(/* modeId= */ 1, 0, 0, 0f, + alternativeRefreshRates, hdrTypesWithoutDv); + + mDisplayInfo.supportedModes = new Display.Mode[] {modeWithoutDv, modeWithDv}; + mDisplayInfo.hdrCapabilities = new Display.HdrCapabilities(hdrTypesWithDv, 0, 0, 0); + + final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, + DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); + + mDisplayInfo.modeId = 0; + assertArrayEquals(hdrTypesWithDv, display.getHdrCapabilities().getSupportedHdrTypes()); + + mDisplayInfo.modeId = 1; + assertArrayEquals(hdrTypesWithoutDv, display.getHdrCapabilities().getSupportedHdrTypes()); + } + @Test public void testConstructor_defaultDisplayAdjustments_matchesDisplayInfo() { setDisplayInfoPortrait(mDisplayInfo);