Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.display; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertNull; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
| 24 | import android.os.PowerManager; |
| 25 | import android.support.test.filters.SmallTest; |
| 26 | import android.support.test.runner.AndroidJUnit4; |
| 27 | import android.util.Spline; |
| 28 | |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | |
| 32 | import java.util.Arrays; |
| 33 | |
| 34 | @SmallTest |
| 35 | @RunWith(AndroidJUnit4.class) |
| 36 | public class BrightnessMappingStrategyTest { |
| 37 | |
| 38 | private static final float[] LUX_LEVELS = { |
| 39 | 0f, |
| 40 | 5f, |
| 41 | 20f, |
| 42 | 40f, |
| 43 | 100f, |
| 44 | 325f, |
| 45 | 600f, |
| 46 | 1250f, |
| 47 | 2200f, |
| 48 | 4000f, |
| 49 | 5000f |
| 50 | }; |
| 51 | |
| 52 | private static final float[] DISPLAY_LEVELS_NITS = { |
| 53 | 13.25f, |
| 54 | 54.0f, |
| 55 | 78.85f, |
| 56 | 105.02f, |
| 57 | 132.7f, |
| 58 | 170.12f, |
| 59 | 212.1f, |
| 60 | 265.2f, |
| 61 | 335.8f, |
| 62 | 415.2f, |
| 63 | 478.5f, |
| 64 | }; |
| 65 | |
| 66 | private static final int[] DISPLAY_LEVELS_BACKLIGHT = { |
| 67 | 9, |
| 68 | 30, |
| 69 | 45, |
| 70 | 62, |
| 71 | 78, |
| 72 | 96, |
| 73 | 119, |
| 74 | 146, |
| 75 | 178, |
| 76 | 221, |
| 77 | 255 |
| 78 | }; |
| 79 | |
| 80 | private static final float[] DISPLAY_RANGE_NITS = { 2.685f, 478.5f }; |
| 81 | private static final int[] BACKLIGHT_RANGE = { 1, 255 }; |
| 82 | |
| 83 | @Test |
| 84 | public void testSimpleStrategyMappingAtControlPoints() { |
| 85 | BrightnessMappingStrategy simple = BrightnessMappingStrategy.create( |
| 86 | LUX_LEVELS, DISPLAY_LEVELS_BACKLIGHT, |
| 87 | null /*brightnessLevelsNits*/, null /*nitsRange*/, null /*backlightRange*/); |
| 88 | assertNotNull("BrightnessMappingStrategy should not be null", simple); |
| 89 | for (int i = 0; i < LUX_LEVELS.length; i++) { |
| 90 | final float expectedLevel = |
| 91 | (float) DISPLAY_LEVELS_BACKLIGHT[i] / PowerManager.BRIGHTNESS_ON; |
| 92 | assertEquals(expectedLevel, |
| 93 | simple.getBrightness(LUX_LEVELS[i]), 0.01f /*tolerance*/); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testSimpleStrategyMappingBetweenControlPoints() { |
| 99 | BrightnessMappingStrategy simple = BrightnessMappingStrategy.create( |
| 100 | LUX_LEVELS, DISPLAY_LEVELS_BACKLIGHT, |
| 101 | null /*brightnessLevelsNits*/, null /*nitsRange*/, null /*backlightRange*/); |
| 102 | assertNotNull("BrightnessMappingStrategy should not be null", simple); |
| 103 | for (int i = 1; i < LUX_LEVELS.length; i++) { |
| 104 | final float lux = (LUX_LEVELS[i - 1] + LUX_LEVELS[i]) / 2; |
| 105 | final float backlight = simple.getBrightness(lux) * PowerManager.BRIGHTNESS_ON; |
| 106 | assertTrue("Desired brightness should be between adjacent control points.", |
| 107 | backlight > DISPLAY_LEVELS_BACKLIGHT[i-1] |
| 108 | && backlight < DISPLAY_LEVELS_BACKLIGHT[i]); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testPhysicalStrategyMappingAtControlPoints() { |
| 114 | BrightnessMappingStrategy physical = BrightnessMappingStrategy.create( |
| 115 | LUX_LEVELS, null /*brightnessLevelsBacklight*/, |
| 116 | DISPLAY_LEVELS_NITS, DISPLAY_RANGE_NITS, BACKLIGHT_RANGE); |
| 117 | assertNotNull("BrightnessMappingStrategy should not be null", physical); |
| 118 | for (int i = 0; i < LUX_LEVELS.length; i++) { |
| 119 | final float expectedLevel = DISPLAY_LEVELS_NITS[i] / DISPLAY_RANGE_NITS[1]; |
| 120 | assertEquals(expectedLevel, |
| 121 | physical.getBrightness(LUX_LEVELS[i]), 0.01f /*tolerance*/); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testPhysicalStrategyMappingBetweenControlPoints() { |
| 127 | BrightnessMappingStrategy physical = BrightnessMappingStrategy.create( |
| 128 | LUX_LEVELS, null /*brightnessLevelsBacklight*/, |
| 129 | DISPLAY_LEVELS_NITS, DISPLAY_RANGE_NITS, BACKLIGHT_RANGE); |
| 130 | assertNotNull("BrightnessMappingStrategy should not be null", physical); |
| 131 | Spline backlightToBrightness = |
| 132 | Spline.createSpline(toFloatArray(BACKLIGHT_RANGE), DISPLAY_RANGE_NITS); |
| 133 | for (int i = 1; i < LUX_LEVELS.length; i++) { |
| 134 | final float lux = (LUX_LEVELS[i - 1] + LUX_LEVELS[i]) / 2; |
| 135 | final float backlight = physical.getBrightness(lux) * PowerManager.BRIGHTNESS_ON; |
| 136 | final float nits = backlightToBrightness.interpolate(backlight); |
| 137 | assertTrue("Desired brightness should be between adjacent control points.", |
| 138 | nits > DISPLAY_LEVELS_NITS[i-1] && nits < DISPLAY_LEVELS_NITS[i]); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testDefaultStrategyIsPhysical() { |
| 144 | BrightnessMappingStrategy strategy = BrightnessMappingStrategy.create( |
| 145 | LUX_LEVELS, DISPLAY_LEVELS_BACKLIGHT, |
| 146 | DISPLAY_LEVELS_NITS, DISPLAY_RANGE_NITS, BACKLIGHT_RANGE); |
| 147 | assertTrue(strategy instanceof BrightnessMappingStrategy.PhysicalMappingStrategy); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testNonStrictlyIncreasingLuxLevelsFails() { |
| 152 | final float[] lux = Arrays.copyOf(LUX_LEVELS, LUX_LEVELS.length); |
| 153 | final int idx = lux.length / 2; |
| 154 | float tmp = lux[idx]; |
| 155 | lux[idx] = lux[idx+1]; |
| 156 | lux[idx+1] = tmp; |
| 157 | BrightnessMappingStrategy strategy = BrightnessMappingStrategy.create( |
| 158 | lux, null /*brightnessLevelsBacklight*/, |
| 159 | DISPLAY_LEVELS_NITS, DISPLAY_RANGE_NITS, BACKLIGHT_RANGE); |
| 160 | assertNull(strategy); |
| 161 | |
| 162 | // And make sure we get the same result even if it's monotone but not increasing. |
| 163 | lux[idx] = lux[idx+1]; |
| 164 | strategy = BrightnessMappingStrategy.create( |
| 165 | lux, null /*brightnessLevelsBacklight*/, |
| 166 | DISPLAY_LEVELS_NITS, DISPLAY_RANGE_NITS, BACKLIGHT_RANGE); |
| 167 | assertNull(strategy); |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testDifferentNumberOfControlPointValuesFails() { |
| 172 | //Extra lux level |
| 173 | final float[] lux = Arrays.copyOf(LUX_LEVELS, LUX_LEVELS.length+1); |
| 174 | // Make sure it's strictly increasing so that the only failure is the differing array |
| 175 | // lengths |
| 176 | lux[lux.length - 1] = lux[lux.length - 2] + 1; |
| 177 | BrightnessMappingStrategy strategy = BrightnessMappingStrategy.create( |
| 178 | lux, null /*brightnessLevelsBacklight*/, |
| 179 | DISPLAY_LEVELS_NITS, DISPLAY_RANGE_NITS, BACKLIGHT_RANGE); |
| 180 | assertNull(strategy); |
| 181 | |
| 182 | strategy = BrightnessMappingStrategy.create( |
| 183 | lux, DISPLAY_LEVELS_BACKLIGHT, |
| 184 | null /*brightnessLevelsNits*/, null /*nitsRange*/, null /*backlightRange*/); |
| 185 | assertNull(strategy); |
| 186 | |
| 187 | // Extra backlight level |
| 188 | final int[] backlight = Arrays.copyOf( |
| 189 | DISPLAY_LEVELS_BACKLIGHT, DISPLAY_LEVELS_BACKLIGHT.length+1); |
| 190 | backlight[backlight.length - 1] = backlight[backlight.length - 2] + 1; |
| 191 | strategy = BrightnessMappingStrategy.create( |
| 192 | LUX_LEVELS, backlight, |
| 193 | null /*brightnessLevelsNits*/, null /*nitsRange*/, null /*backlightRange*/); |
| 194 | assertNull(strategy); |
| 195 | |
| 196 | // Extra nits level |
| 197 | final float[] nits = Arrays.copyOf(DISPLAY_RANGE_NITS, DISPLAY_LEVELS_NITS.length+1); |
| 198 | nits[nits.length - 1] = nits[nits.length - 2] + 1; |
| 199 | strategy = BrightnessMappingStrategy.create( |
| 200 | LUX_LEVELS, null /*brightnessLevelsBacklight*/, |
| 201 | nits, DISPLAY_RANGE_NITS, BACKLIGHT_RANGE); |
| 202 | assertNull(strategy); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void testPhysicalStrategyRequiresNitsMapping() { |
| 207 | BrightnessMappingStrategy physical = BrightnessMappingStrategy.create( |
| 208 | LUX_LEVELS, null /*brightnessLevelsBacklight*/, |
| 209 | DISPLAY_LEVELS_NITS, null, BACKLIGHT_RANGE); |
| 210 | assertNull(physical); |
| 211 | |
| 212 | physical = BrightnessMappingStrategy.create( |
| 213 | LUX_LEVELS, null /*brightnessLevelsBacklight*/, |
| 214 | DISPLAY_LEVELS_NITS, DISPLAY_RANGE_NITS, null); |
| 215 | assertNull(physical); |
| 216 | |
| 217 | physical = BrightnessMappingStrategy.create( |
| 218 | LUX_LEVELS, null /*brightnessLevelsBacklight*/, |
| 219 | DISPLAY_LEVELS_NITS, null, null); |
| 220 | assertNull(physical); |
| 221 | } |
| 222 | |
| 223 | private static float[] toFloatArray(int[] vals) { |
| 224 | float[] newVals = new float[vals.length]; |
| 225 | for (int i = 0; i < vals.length; i++) { |
| 226 | newVals[i] = (float) vals[i]; |
| 227 | } |
| 228 | return newVals; |
| 229 | } |
| 230 | } |