Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.whitebalance; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.util.Slog; |
Daniel Solomon | fb393e5 | 2019-02-08 20:26:27 -0800 | [diff] [blame] | 21 | import android.util.Spline; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 22 | |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 24 | import com.android.internal.util.Preconditions; |
| 25 | import com.android.server.LocalServices; |
Christine Franks | 0ada277 | 2019-02-25 13:54:57 -0800 | [diff] [blame] | 26 | import com.android.server.display.color.ColorDisplayService.ColorDisplayServiceInternal; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 27 | import com.android.server.display.utils.History; |
| 28 | |
| 29 | import java.io.PrintWriter; |
| 30 | |
| 31 | /** |
| 32 | * The DisplayWhiteBalanceController drives display white-balance (automatically correcting the |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 33 | * display color temperature depending on the ambient color temperature). |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 34 | * |
| 35 | * The DisplayWhiteBalanceController: |
| 36 | * - Uses the AmbientColorTemperatureSensor to detect changes in the ambient color temperature; |
| 37 | * - Uses the AmbientColorTemperatureFilter to average these changes over time, filter out the |
| 38 | * noise, and arrive at an estimate of the actual ambient color temperature; |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 39 | * - Uses the DisplayWhiteBalanceThrottler to decide whether the display color tempearture should |
| 40 | * be updated, suppressing changes that are too frequent or too minor. |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 41 | */ |
| 42 | public class DisplayWhiteBalanceController implements |
| 43 | AmbientSensor.AmbientBrightnessSensor.Callbacks, |
| 44 | AmbientSensor.AmbientColorTemperatureSensor.Callbacks { |
| 45 | |
| 46 | protected static final String TAG = "DisplayWhiteBalanceController"; |
| 47 | protected boolean mLoggingEnabled; |
| 48 | |
| 49 | private boolean mEnabled; |
| 50 | |
| 51 | // To decouple the DisplayPowerController from the DisplayWhiteBalanceController, the DPC |
| 52 | // implements Callbacks and passes itself to the DWBC so it can call back into it without |
| 53 | // knowing about it. |
| 54 | private Callbacks mCallbacks; |
| 55 | |
| 56 | private AmbientSensor.AmbientBrightnessSensor mBrightnessSensor; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 57 | |
| 58 | @VisibleForTesting |
| 59 | AmbientFilter mBrightnessFilter; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 60 | private AmbientSensor.AmbientColorTemperatureSensor mColorTemperatureSensor; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 61 | |
| 62 | @VisibleForTesting |
| 63 | AmbientFilter mColorTemperatureFilter; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 64 | private DisplayWhiteBalanceThrottler mThrottler; |
| 65 | |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 66 | private final float mLowLightAmbientColorTemperature; |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 67 | private final float mHighLightAmbientColorTemperature; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 68 | |
| 69 | private float mAmbientColorTemperature; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 70 | |
| 71 | @VisibleForTesting |
| 72 | float mPendingAmbientColorTemperature; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 73 | private float mLastAmbientColorTemperature; |
| 74 | |
| 75 | private ColorDisplayServiceInternal mColorDisplayServiceInternal; |
| 76 | |
| 77 | // The most recent ambient color temperature values are kept for debugging purposes. |
| 78 | private static final int HISTORY_SIZE = 50; |
| 79 | private History mAmbientColorTemperatureHistory; |
| 80 | |
| 81 | // Override the ambient color temperature for debugging purposes. |
| 82 | private float mAmbientColorTemperatureOverride; |
| 83 | |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 84 | // A piecewise linear relationship between ambient and display color temperatures. |
| 85 | private Spline.LinearSpline mAmbientToDisplayColorTemperatureSpline; |
Daniel Solomon | fb393e5 | 2019-02-08 20:26:27 -0800 | [diff] [blame] | 86 | |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 87 | // In very low or very high brightness conditions ambient EQ should to set to a default |
| 88 | // instead of using mAmbientToDisplayColorTemperatureSpline. However, setting ambient EQ |
| 89 | // based on thresholds can cause the display to rapidly change color temperature. To solve |
| 90 | // this, mLowLightAmbientBrightnessToBiasSpline and mHighLightAmbientBrightnessToBiasSpline |
| 91 | // are used to smoothly interpolate from ambient color temperature to the defaults. |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 92 | // A piecewise linear relationship between low light brightness and low light bias. |
| 93 | private Spline.LinearSpline mLowLightAmbientBrightnessToBiasSpline; |
| 94 | |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 95 | // A piecewise linear relationship between high light brightness and high light bias. |
| 96 | private Spline.LinearSpline mHighLightAmbientBrightnessToBiasSpline; |
| 97 | |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 98 | /** |
| 99 | * @param brightnessSensor |
| 100 | * The sensor used to detect changes in the ambient brightness. |
| 101 | * @param brightnessFilter |
| 102 | * The filter used to avergae ambient brightness changes over time, filter out the noise |
| 103 | * and arrive at an estimate of the actual ambient brightness. |
| 104 | * @param colorTemperatureSensor |
| 105 | * The sensor used to detect changes in the ambient color temperature. |
| 106 | * @param colorTemperatureFilter |
| 107 | * The filter used to average ambient color temperature changes over time, filter out the |
| 108 | * noise and arrive at an estimate of the actual ambient color temperature. |
| 109 | * @param throttler |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 110 | * The throttler used to determine whether the new display color temperature should be |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 111 | * updated or not. |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 112 | * @param lowLightAmbientBrightnesses |
| 113 | * The ambient brightness used to map the ambient brightnesses to the biases used to |
| 114 | * interpolate to lowLightAmbientColorTemperature. |
| 115 | * @param lowLightAmbientBiases |
| 116 | * The biases used to map the ambient brightnesses to the biases used to interpolate to |
| 117 | * lowLightAmbientColorTemperature. |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 118 | * @param lowLightAmbientColorTemperature |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 119 | * The ambient color temperature to which we interpolate to based on the low light curve. |
| 120 | * @param highLightAmbientBrightnesses |
| 121 | * The ambient brightness used to map the ambient brightnesses to the biases used to |
| 122 | * interpolate to highLightAmbientColorTemperature. |
| 123 | * @param highLightAmbientBiases |
| 124 | * The biases used to map the ambient brightnesses to the biases used to interpolate to |
| 125 | * highLightAmbientColorTemperature. |
| 126 | * @param highLightAmbientColorTemperature |
| 127 | * The ambient color temperature to which we interpolate to based on the high light curve. |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 128 | * @param ambientColorTemperatures |
| 129 | * The ambient color tempeartures used to map the ambient color temperature to the display |
| 130 | * color temperature (or null if no mapping is necessary). |
| 131 | * @param displayColorTemperatures |
| 132 | * The display color temperatures used to map the ambient color temperature to the display |
| 133 | * color temperature (or null if no mapping is necessary). |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 134 | * |
| 135 | * @throws NullPointerException |
| 136 | * - brightnessSensor is null; |
| 137 | * - brightnessFilter is null; |
| 138 | * - colorTemperatureSensor is null; |
| 139 | * - colorTemperatureFilter is null; |
| 140 | * - throttler is null. |
| 141 | */ |
| 142 | public DisplayWhiteBalanceController( |
| 143 | @NonNull AmbientSensor.AmbientBrightnessSensor brightnessSensor, |
| 144 | @NonNull AmbientFilter brightnessFilter, |
| 145 | @NonNull AmbientSensor.AmbientColorTemperatureSensor colorTemperatureSensor, |
| 146 | @NonNull AmbientFilter colorTemperatureFilter, |
| 147 | @NonNull DisplayWhiteBalanceThrottler throttler, |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 148 | float[] lowLightAmbientBrightnesses, float[] lowLightAmbientBiases, |
| 149 | float lowLightAmbientColorTemperature, |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 150 | float[] highLightAmbientBrightnesses, float[] highLightAmbientBiases, |
| 151 | float highLightAmbientColorTemperature, |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 152 | float[] ambientColorTemperatures, float[] displayColorTemperatures) { |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 153 | validateArguments(brightnessSensor, brightnessFilter, colorTemperatureSensor, |
| 154 | colorTemperatureFilter, throttler); |
| 155 | mLoggingEnabled = false; |
| 156 | mEnabled = false; |
| 157 | mCallbacks = null; |
| 158 | mBrightnessSensor = brightnessSensor; |
| 159 | mBrightnessFilter = brightnessFilter; |
| 160 | mColorTemperatureSensor = colorTemperatureSensor; |
| 161 | mColorTemperatureFilter = colorTemperatureFilter; |
| 162 | mThrottler = throttler; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 163 | mLowLightAmbientColorTemperature = lowLightAmbientColorTemperature; |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 164 | mHighLightAmbientColorTemperature = highLightAmbientColorTemperature; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 165 | mAmbientColorTemperature = -1.0f; |
| 166 | mPendingAmbientColorTemperature = -1.0f; |
| 167 | mLastAmbientColorTemperature = -1.0f; |
| 168 | mAmbientColorTemperatureHistory = new History(HISTORY_SIZE); |
| 169 | mAmbientColorTemperatureOverride = -1.0f; |
Daniel Solomon | fb393e5 | 2019-02-08 20:26:27 -0800 | [diff] [blame] | 170 | |
| 171 | try { |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 172 | mLowLightAmbientBrightnessToBiasSpline = new Spline.LinearSpline( |
| 173 | lowLightAmbientBrightnesses, lowLightAmbientBiases); |
| 174 | } catch (Exception e) { |
| 175 | Slog.e(TAG, "failed to create low light ambient brightness to bias spline.", e); |
| 176 | mLowLightAmbientBrightnessToBiasSpline = null; |
| 177 | } |
| 178 | if (mLowLightAmbientBrightnessToBiasSpline != null) { |
| 179 | if (mLowLightAmbientBrightnessToBiasSpline.interpolate(0.0f) != 0.0f || |
| 180 | mLowLightAmbientBrightnessToBiasSpline.interpolate(Float.POSITIVE_INFINITY) |
| 181 | != 1.0f) { |
| 182 | Slog.d(TAG, "invalid low light ambient brightness to bias spline, " |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 183 | + "bias must begin at 0.0 and end at 1.0."); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 184 | mLowLightAmbientBrightnessToBiasSpline = null; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | try { |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 189 | mHighLightAmbientBrightnessToBiasSpline = new Spline.LinearSpline( |
| 190 | highLightAmbientBrightnesses, highLightAmbientBiases); |
| 191 | } catch (Exception e) { |
| 192 | Slog.e(TAG, "failed to create high light ambient brightness to bias spline.", e); |
| 193 | mHighLightAmbientBrightnessToBiasSpline = null; |
| 194 | } |
| 195 | if (mHighLightAmbientBrightnessToBiasSpline != null) { |
| 196 | if (mHighLightAmbientBrightnessToBiasSpline.interpolate(0.0f) != 0.0f || |
| 197 | mHighLightAmbientBrightnessToBiasSpline.interpolate(Float.POSITIVE_INFINITY) |
| 198 | != 1.0f) { |
| 199 | Slog.d(TAG, "invalid high light ambient brightness to bias spline, " |
| 200 | + "bias must begin at 0.0 and end at 1.0."); |
| 201 | mHighLightAmbientBrightnessToBiasSpline = null; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (mLowLightAmbientBrightnessToBiasSpline != null && |
| 206 | mHighLightAmbientBrightnessToBiasSpline != null) { |
| 207 | if (lowLightAmbientBrightnesses[lowLightAmbientBrightnesses.length - 1] > |
| 208 | highLightAmbientBrightnesses[0]) { |
| 209 | Slog.d(TAG, "invalid low light and high light ambient brightness to bias spline " |
| 210 | + "combination, defined domains must not intersect."); |
| 211 | mLowLightAmbientBrightnessToBiasSpline = null; |
| 212 | mHighLightAmbientBrightnessToBiasSpline = null; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | try { |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 217 | mAmbientToDisplayColorTemperatureSpline = new Spline.LinearSpline( |
| 218 | ambientColorTemperatures, displayColorTemperatures); |
Daniel Solomon | fb393e5 | 2019-02-08 20:26:27 -0800 | [diff] [blame] | 219 | } catch (Exception e) { |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 220 | Slog.e(TAG, "failed to create ambient to display color temperature spline.", e); |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 221 | mAmbientToDisplayColorTemperatureSpline = null; |
Daniel Solomon | fb393e5 | 2019-02-08 20:26:27 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 224 | mColorDisplayServiceInternal = LocalServices.getService(ColorDisplayServiceInternal.class); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Enable/disable the controller. |
| 229 | * |
| 230 | * @param enabled |
| 231 | * Whether the controller should be on/off. |
| 232 | * |
| 233 | * @return Whether the method succeeded or not. |
| 234 | */ |
| 235 | public boolean setEnabled(boolean enabled) { |
| 236 | if (enabled) { |
| 237 | return enable(); |
| 238 | } else { |
| 239 | return disable(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 244 | * Set an object to call back to when the display color temperature should be updated. |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 245 | * |
| 246 | * @param callbacks |
| 247 | * The object to call back to. |
| 248 | * |
| 249 | * @return Whether the method succeeded or not. |
| 250 | */ |
| 251 | public boolean setCallbacks(Callbacks callbacks) { |
| 252 | if (mCallbacks == callbacks) { |
| 253 | return false; |
| 254 | } |
| 255 | mCallbacks = callbacks; |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Enable/disable logging. |
| 261 | * |
| 262 | * @param loggingEnabled |
| 263 | * Whether logging should be on/off. |
| 264 | * |
| 265 | * @return Whether the method succeeded or not. |
| 266 | */ |
| 267 | public boolean setLoggingEnabled(boolean loggingEnabled) { |
| 268 | if (mLoggingEnabled == loggingEnabled) { |
| 269 | return false; |
| 270 | } |
| 271 | mLoggingEnabled = loggingEnabled; |
| 272 | mBrightnessSensor.setLoggingEnabled(loggingEnabled); |
| 273 | mBrightnessFilter.setLoggingEnabled(loggingEnabled); |
| 274 | mColorTemperatureSensor.setLoggingEnabled(loggingEnabled); |
| 275 | mColorTemperatureFilter.setLoggingEnabled(loggingEnabled); |
| 276 | mThrottler.setLoggingEnabled(loggingEnabled); |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Set the ambient color temperature override. |
| 282 | * |
| 283 | * This is only applied when the ambient color temperature changes or is updated (in which case |
| 284 | * it overrides the ambient color temperature estimate); in other words, it doesn't necessarily |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 285 | * change the display color temperature immediately. |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 286 | * |
| 287 | * @param ambientColorTemperatureOverride |
| 288 | * The ambient color temperature override. |
| 289 | * |
| 290 | * @return Whether the method succeeded or not. |
| 291 | */ |
| 292 | public boolean setAmbientColorTemperatureOverride(float ambientColorTemperatureOverride) { |
| 293 | if (mAmbientColorTemperatureOverride == ambientColorTemperatureOverride) { |
| 294 | return false; |
| 295 | } |
| 296 | mAmbientColorTemperatureOverride = ambientColorTemperatureOverride; |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Dump the state. |
| 302 | * |
| 303 | * @param writer |
| 304 | * The writer used to dump the state. |
| 305 | */ |
| 306 | public void dump(PrintWriter writer) { |
| 307 | writer.println("DisplayWhiteBalanceController"); |
| 308 | writer.println(" mLoggingEnabled=" + mLoggingEnabled); |
| 309 | writer.println(" mEnabled=" + mEnabled); |
| 310 | writer.println(" mCallbacks=" + mCallbacks); |
| 311 | mBrightnessSensor.dump(writer); |
| 312 | mBrightnessFilter.dump(writer); |
| 313 | mColorTemperatureSensor.dump(writer); |
| 314 | mColorTemperatureFilter.dump(writer); |
| 315 | mThrottler.dump(writer); |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 316 | writer.println(" mLowLightAmbientColorTemperature=" + mLowLightAmbientColorTemperature); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 317 | writer.println(" mHighLightAmbientColorTemperature=" + mHighLightAmbientColorTemperature); |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 318 | writer.println(" mAmbientColorTemperature=" + mAmbientColorTemperature); |
| 319 | writer.println(" mPendingAmbientColorTemperature=" + mPendingAmbientColorTemperature); |
| 320 | writer.println(" mLastAmbientColorTemperature=" + mLastAmbientColorTemperature); |
| 321 | writer.println(" mAmbientColorTemperatureHistory=" + mAmbientColorTemperatureHistory); |
| 322 | writer.println(" mAmbientColorTemperatureOverride=" + mAmbientColorTemperatureOverride); |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 323 | writer.println(" mAmbientToDisplayColorTemperatureSpline=" |
| 324 | + mAmbientToDisplayColorTemperatureSpline); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 325 | writer.println(" mLowLightAmbientBrightnessToBiasSpline=" |
| 326 | + mLowLightAmbientBrightnessToBiasSpline); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 327 | writer.println(" mHighLightAmbientBrightnessToBiasSpline=" |
| 328 | + mHighLightAmbientBrightnessToBiasSpline); |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override // AmbientSensor.AmbientBrightnessSensor.Callbacks |
| 332 | public void onAmbientBrightnessChanged(float value) { |
| 333 | final long time = System.currentTimeMillis(); |
| 334 | mBrightnessFilter.addValue(time, value); |
| 335 | updateAmbientColorTemperature(); |
| 336 | } |
| 337 | |
| 338 | @Override // AmbientSensor.AmbientColorTemperatureSensor.Callbacks |
| 339 | public void onAmbientColorTemperatureChanged(float value) { |
| 340 | final long time = System.currentTimeMillis(); |
| 341 | mColorTemperatureFilter.addValue(time, value); |
| 342 | updateAmbientColorTemperature(); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Updates the ambient color temperature. |
| 347 | */ |
| 348 | public void updateAmbientColorTemperature() { |
| 349 | final long time = System.currentTimeMillis(); |
| 350 | float ambientColorTemperature = mColorTemperatureFilter.getEstimate(time); |
| 351 | |
Daniel Solomon | dff8b1b | 2019-04-08 14:30:51 -0700 | [diff] [blame] | 352 | if (mAmbientToDisplayColorTemperatureSpline != null && ambientColorTemperature != -1.0f) { |
Daniel Solomon | fb393e5 | 2019-02-08 20:26:27 -0800 | [diff] [blame] | 353 | ambientColorTemperature = |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 354 | mAmbientToDisplayColorTemperatureSpline.interpolate(ambientColorTemperature); |
Daniel Solomon | fb393e5 | 2019-02-08 20:26:27 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 357 | float ambientBrightness = mBrightnessFilter.getEstimate(time); |
| 358 | |
Anthony Han | 35df7e2 | 2019-06-27 17:27:24 -0700 | [diff] [blame] | 359 | if (ambientColorTemperature != -1.0f && |
| 360 | mLowLightAmbientBrightnessToBiasSpline != null) { |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 361 | float bias = mLowLightAmbientBrightnessToBiasSpline.interpolate(ambientBrightness); |
| 362 | ambientColorTemperature = |
| 363 | bias * ambientColorTemperature + (1.0f - bias) |
| 364 | * mLowLightAmbientColorTemperature; |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 365 | } |
Anthony Han | 35df7e2 | 2019-06-27 17:27:24 -0700 | [diff] [blame] | 366 | if (ambientColorTemperature != -1.0f && |
| 367 | mHighLightAmbientBrightnessToBiasSpline != null) { |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 368 | float bias = mHighLightAmbientBrightnessToBiasSpline.interpolate(ambientBrightness); |
| 369 | ambientColorTemperature = |
| 370 | (1.0f - bias) * ambientColorTemperature + bias |
| 371 | * mHighLightAmbientColorTemperature; |
| 372 | } |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 373 | |
| 374 | if (mAmbientColorTemperatureOverride != -1.0f) { |
| 375 | if (mLoggingEnabled) { |
| 376 | Slog.d(TAG, "override ambient color temperature: " + ambientColorTemperature |
| 377 | + " => " + mAmbientColorTemperatureOverride); |
| 378 | } |
| 379 | ambientColorTemperature = mAmbientColorTemperatureOverride; |
| 380 | } |
| 381 | |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 382 | // When the display color temperature needs to be updated, we call DisplayPowerController to |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 383 | // call our updateColorTemperature. The reason we don't call it directly is that we want |
| 384 | // all changes to the system to happen in a predictable order in DPC's main loop |
| 385 | // (updatePowerState). |
| 386 | if (ambientColorTemperature == -1.0f || mThrottler.throttle(ambientColorTemperature)) { |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | if (mLoggingEnabled) { |
| 391 | Slog.d(TAG, "pending ambient color temperature: " + ambientColorTemperature); |
| 392 | } |
| 393 | mPendingAmbientColorTemperature = ambientColorTemperature; |
| 394 | if (mCallbacks != null) { |
| 395 | mCallbacks.updateWhiteBalance(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 400 | * Updates the display color temperature. |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 401 | */ |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 402 | public void updateDisplayColorTemperature() { |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 403 | float ambientColorTemperature = -1.0f; |
| 404 | |
| 405 | // If both the pending and the current ambient color temperatures are -1, it means the DWBC |
| 406 | // was just enabled, and we use the last ambient color temperature until new sensor events |
| 407 | // give us a better estimate. |
| 408 | if (mAmbientColorTemperature == -1.0f && mPendingAmbientColorTemperature == -1.0f) { |
| 409 | ambientColorTemperature = mLastAmbientColorTemperature; |
| 410 | } |
| 411 | |
| 412 | // Otherwise, we use the pending ambient color temperature, but only if it's non-trivial |
| 413 | // and different than the current one. |
| 414 | if (mPendingAmbientColorTemperature != -1.0f |
| 415 | && mPendingAmbientColorTemperature != mAmbientColorTemperature) { |
| 416 | ambientColorTemperature = mPendingAmbientColorTemperature; |
| 417 | } |
| 418 | |
| 419 | if (ambientColorTemperature == -1.0f) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | mAmbientColorTemperature = ambientColorTemperature; |
| 424 | if (mLoggingEnabled) { |
| 425 | Slog.d(TAG, "ambient color temperature: " + mAmbientColorTemperature); |
| 426 | } |
| 427 | mPendingAmbientColorTemperature = -1.0f; |
| 428 | mAmbientColorTemperatureHistory.add(mAmbientColorTemperature); |
| 429 | mColorDisplayServiceInternal.setDisplayWhiteBalanceColorTemperature( |
| 430 | (int) mAmbientColorTemperature); |
| 431 | mLastAmbientColorTemperature = mAmbientColorTemperature; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * The DisplayWhiteBalanceController decouples itself from its parent (DisplayPowerController) |
| 436 | * by providing this interface to implement (and a method to set its callbacks object), and |
| 437 | * calling these methods. |
| 438 | */ |
| 439 | public interface Callbacks { |
| 440 | |
| 441 | /** |
| 442 | * Called whenever the display white-balance state has changed. |
| 443 | * |
| 444 | * Usually, this means the estimated ambient color temperature has changed enough, and the |
Dan Gittik | 1151ac0 | 2019-02-13 14:05:37 +0000 | [diff] [blame] | 445 | * display color temperature should be updated; but it is also called if settings change. |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 446 | */ |
| 447 | void updateWhiteBalance(); |
| 448 | } |
| 449 | |
| 450 | private void validateArguments(AmbientSensor.AmbientBrightnessSensor brightnessSensor, |
| 451 | AmbientFilter brightnessFilter, |
| 452 | AmbientSensor.AmbientColorTemperatureSensor colorTemperatureSensor, |
| 453 | AmbientFilter colorTemperatureFilter, |
| 454 | DisplayWhiteBalanceThrottler throttler) { |
| 455 | Preconditions.checkNotNull(brightnessSensor, "brightnessSensor must not be null"); |
| 456 | Preconditions.checkNotNull(brightnessFilter, "brightnessFilter must not be null"); |
| 457 | Preconditions.checkNotNull(colorTemperatureSensor, |
| 458 | "colorTemperatureSensor must not be null"); |
| 459 | Preconditions.checkNotNull(colorTemperatureFilter, |
| 460 | "colorTemperatureFilter must not be null"); |
| 461 | Preconditions.checkNotNull(throttler, "throttler cannot be null"); |
| 462 | } |
| 463 | |
| 464 | private boolean enable() { |
| 465 | if (mEnabled) { |
| 466 | return false; |
| 467 | } |
| 468 | if (mLoggingEnabled) { |
| 469 | Slog.d(TAG, "enabling"); |
| 470 | } |
| 471 | mEnabled = true; |
| 472 | mBrightnessSensor.setEnabled(true); |
| 473 | mColorTemperatureSensor.setEnabled(true); |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | private boolean disable() { |
| 478 | if (!mEnabled) { |
| 479 | return false; |
| 480 | } |
| 481 | if (mLoggingEnabled) { |
| 482 | Slog.d(TAG, "disabling"); |
| 483 | } |
| 484 | mEnabled = false; |
| 485 | mBrightnessSensor.setEnabled(false); |
| 486 | mBrightnessFilter.clear(); |
| 487 | mColorTemperatureSensor.setEnabled(false); |
| 488 | mColorTemperatureFilter.clear(); |
| 489 | mThrottler.clear(); |
| 490 | mAmbientColorTemperature = -1.0f; |
| 491 | mPendingAmbientColorTemperature = -1.0f; |
Daniel Solomon | 3781641 | 2019-04-10 15:17:41 -0700 | [diff] [blame] | 492 | mColorDisplayServiceInternal.resetDisplayWhiteBalanceColorTemperature(); |
Dan Gittik | 8dbd7e9 | 2018-12-03 15:35:53 +0000 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
| 496 | } |