1 /* 2 * Copyright (C) 2022 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.brightness; 18 19 import static android.hardware.display.DisplayManagerInternal.DisplayPowerRequest.POLICY_OFF; 20 import static android.hardware.display.DisplayManagerInternal.DisplayPowerRequest.policyToString; 21 22 import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DEFAULT; 23 import static com.android.server.display.config.DisplayBrightnessMappingConfig.autoBrightnessModeToString; 24 25 import android.hardware.display.BrightnessInfo; 26 import android.os.PowerManager; 27 import android.os.SystemClock; 28 import android.view.Display; 29 30 import com.android.internal.annotations.VisibleForTesting; 31 import com.android.server.display.AutomaticBrightnessController; 32 33 import java.text.SimpleDateFormat; 34 import java.util.Date; 35 36 /** 37 * Represents a particular brightness change event. 38 */ 39 public final class BrightnessEvent { 40 public static final int FLAG_RBC = 0x1; 41 public static final int FLAG_INVALID_LUX = 0x2; 42 public static final int FLAG_DOZE_SCALE = 0x4; 43 public static final int FLAG_USER_SET = 0x8; 44 public static final int FLAG_LOW_POWER_MODE = 0x20; 45 46 private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); 47 48 private BrightnessReason mReason = new BrightnessReason(); 49 private int mDisplayId; 50 private String mPhysicalDisplayId; 51 private int mDisplayState; 52 private int mDisplayPolicy; 53 private long mTime; 54 private float mLux; 55 private float mPreThresholdLux; 56 private float mInitialBrightness; 57 private float mBrightness; 58 private float mRecommendedBrightness; 59 private float mPreThresholdBrightness; 60 private int mHbmMode; 61 private float mHbmMax; 62 private int mRbcStrength; 63 private float mThermalMax; 64 private float mPowerFactor; 65 private boolean mWasShortTermModelActive; 66 private int mFlags; 67 private int mAdjustmentFlags; 68 private boolean mAutomaticBrightnessEnabled; 69 private String mDisplayBrightnessStrategyName; 70 @AutomaticBrightnessController.AutomaticBrightnessMode 71 private int mAutoBrightnessMode; 72 BrightnessEvent(BrightnessEvent that)73 public BrightnessEvent(BrightnessEvent that) { 74 copyFrom(that); 75 } 76 BrightnessEvent(int displayId)77 public BrightnessEvent(int displayId) { 78 this.mDisplayId = displayId; 79 reset(); 80 } 81 82 /** 83 * A utility to clone a brightness event into another event 84 * 85 * @param that BrightnessEvent which is to be copied 86 */ copyFrom(BrightnessEvent that)87 public void copyFrom(BrightnessEvent that) { 88 mReason.set(that.getReason()); 89 mDisplayId = that.getDisplayId(); 90 mPhysicalDisplayId = that.getPhysicalDisplayId(); 91 mDisplayState = that.mDisplayState; 92 mDisplayPolicy = that.mDisplayPolicy; 93 mTime = that.getTime(); 94 // Lux values 95 mLux = that.getLux(); 96 mPreThresholdLux = that.getPreThresholdLux(); 97 // Brightness values 98 mInitialBrightness = that.getInitialBrightness(); 99 mBrightness = that.getBrightness(); 100 mRecommendedBrightness = that.getRecommendedBrightness(); 101 mPreThresholdBrightness = that.getPreThresholdBrightness(); 102 // Different brightness modulations 103 mHbmMode = that.getHbmMode(); 104 mHbmMax = that.getHbmMax(); 105 mRbcStrength = that.getRbcStrength(); 106 mThermalMax = that.getThermalMax(); 107 mPowerFactor = that.getPowerFactor(); 108 mWasShortTermModelActive = that.wasShortTermModelActive(); 109 mFlags = that.getFlags(); 110 mAdjustmentFlags = that.getAdjustmentFlags(); 111 // Auto-brightness setting 112 mAutomaticBrightnessEnabled = that.isAutomaticBrightnessEnabled(); 113 mDisplayBrightnessStrategyName = that.getDisplayBrightnessStrategyName(); 114 mAutoBrightnessMode = that.mAutoBrightnessMode; 115 } 116 117 /** 118 * A utility to reset the BrightnessEvent to default values 119 */ reset()120 public void reset() { 121 mReason = new BrightnessReason(); 122 mTime = SystemClock.uptimeMillis(); 123 mPhysicalDisplayId = ""; 124 mDisplayState = Display.STATE_UNKNOWN; 125 mDisplayPolicy = POLICY_OFF; 126 // Lux values 127 mLux = 0; 128 mPreThresholdLux = 0; 129 // Brightness values 130 mInitialBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT; 131 mBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT; 132 mRecommendedBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT; 133 mPreThresholdBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT; 134 // Different brightness modulations 135 mHbmMode = BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF; 136 mHbmMax = PowerManager.BRIGHTNESS_MAX; 137 mRbcStrength = 0; 138 mThermalMax = PowerManager.BRIGHTNESS_MAX; 139 mPowerFactor = 1f; 140 mWasShortTermModelActive = false; 141 mFlags = 0; 142 mAdjustmentFlags = 0; 143 // Auto-brightness setting 144 mAutomaticBrightnessEnabled = true; 145 mDisplayBrightnessStrategyName = ""; 146 mAutoBrightnessMode = AUTO_BRIGHTNESS_MODE_DEFAULT; 147 } 148 149 /** 150 * A utility to compare two BrightnessEvents. This purposefully ignores comparing time as the 151 * two events might have been created at different times, but essentially hold the same 152 * underlying values 153 * 154 * @param that The brightnessEvent with which the current brightnessEvent is to be compared 155 * @return A boolean value representing if the two events are same or not. 156 */ equalsMainData(BrightnessEvent that)157 public boolean equalsMainData(BrightnessEvent that) { 158 // This equals comparison purposefully ignores time since it is regularly changing and 159 // we don't want to log a brightness event just because the time changed. 160 return mReason.equals(that.mReason) 161 && mDisplayId == that.mDisplayId 162 && mPhysicalDisplayId.equals(that.mPhysicalDisplayId) 163 && mDisplayState == that.mDisplayState 164 && mDisplayPolicy == that.mDisplayPolicy 165 && Float.floatToRawIntBits(mLux) == Float.floatToRawIntBits(that.mLux) 166 && Float.floatToRawIntBits(mPreThresholdLux) 167 == Float.floatToRawIntBits(that.mPreThresholdLux) 168 && Float.floatToRawIntBits(mBrightness) 169 == Float.floatToRawIntBits(that.mBrightness) 170 && Float.floatToRawIntBits(mRecommendedBrightness) 171 == Float.floatToRawIntBits(that.mRecommendedBrightness) 172 && Float.floatToRawIntBits(mPreThresholdBrightness) 173 == Float.floatToRawIntBits(that.mPreThresholdBrightness) 174 && mHbmMode == that.mHbmMode 175 && Float.floatToRawIntBits(mHbmMax) == Float.floatToRawIntBits(that.mHbmMax) 176 && mRbcStrength == that.mRbcStrength 177 && Float.floatToRawIntBits(mThermalMax) 178 == Float.floatToRawIntBits(that.mThermalMax) 179 && Float.floatToRawIntBits(mPowerFactor) 180 == Float.floatToRawIntBits(that.mPowerFactor) 181 && mWasShortTermModelActive == that.mWasShortTermModelActive 182 && mFlags == that.mFlags 183 && mAdjustmentFlags == that.mAdjustmentFlags 184 && mAutomaticBrightnessEnabled == that.mAutomaticBrightnessEnabled 185 && mDisplayBrightnessStrategyName.equals(that.mDisplayBrightnessStrategyName) 186 && mAutoBrightnessMode == that.mAutoBrightnessMode; 187 } 188 189 /** 190 * A utility to stringify a BrightnessEvent 191 * @param includeTime Indicates if the time field is to be added in the stringify version of the 192 * BrightnessEvent 193 * @return A stringified BrightnessEvent 194 */ toString(boolean includeTime)195 public String toString(boolean includeTime) { 196 return (includeTime ? FORMAT.format(new Date(mTime)) + " - " : "") 197 + "BrightnessEvent: " 198 + "disp=" + mDisplayId 199 + ", physDisp=" + mPhysicalDisplayId 200 + ", displayState=" + Display.stateToString(mDisplayState) 201 + ", displayPolicy=" + policyToString(mDisplayPolicy) 202 + ", brt=" + mBrightness + ((mFlags & FLAG_USER_SET) != 0 ? "(user_set)" : "") 203 + ", initBrt=" + mInitialBrightness 204 + ", rcmdBrt=" + mRecommendedBrightness 205 + ", preBrt=" + mPreThresholdBrightness 206 + ", lux=" + mLux 207 + ", preLux=" + mPreThresholdLux 208 + ", hbmMax=" + mHbmMax 209 + ", hbmMode=" + BrightnessInfo.hbmToString(mHbmMode) 210 + ", rbcStrength=" + mRbcStrength 211 + ", thrmMax=" + mThermalMax 212 + ", powerFactor=" + mPowerFactor 213 + ", wasShortTermModelActive=" + mWasShortTermModelActive 214 + ", flags=" + flagsToString() 215 + ", reason=" + mReason.toString(mAdjustmentFlags) 216 + ", autoBrightness=" + mAutomaticBrightnessEnabled 217 + ", strategy=" + mDisplayBrightnessStrategyName 218 + ", autoBrightnessMode=" + autoBrightnessModeToString(mAutoBrightnessMode); 219 } 220 221 @Override toString()222 public String toString() { 223 return toString(/* includeTime */ true); 224 } 225 getReason()226 public BrightnessReason getReason() { 227 return mReason; 228 } 229 setReason(BrightnessReason reason)230 public void setReason(BrightnessReason reason) { 231 this.mReason = reason; 232 } 233 getTime()234 public long getTime() { 235 return mTime; 236 } 237 setTime(long time)238 public void setTime(long time) { 239 this.mTime = time; 240 } 241 getDisplayId()242 public int getDisplayId() { 243 return mDisplayId; 244 } 245 setDisplayId(int displayId)246 public void setDisplayId(int displayId) { 247 this.mDisplayId = displayId; 248 } 249 getPhysicalDisplayId()250 public String getPhysicalDisplayId() { 251 return mPhysicalDisplayId; 252 } 253 setPhysicalDisplayId(String mPhysicalDisplayId)254 public void setPhysicalDisplayId(String mPhysicalDisplayId) { 255 this.mPhysicalDisplayId = mPhysicalDisplayId; 256 } 257 setDisplayState(int state)258 public void setDisplayState(int state) { 259 mDisplayState = state; 260 } 261 setDisplayPolicy(int policy)262 public void setDisplayPolicy(int policy) { 263 mDisplayPolicy = policy; 264 } 265 getLux()266 public float getLux() { 267 return mLux; 268 } 269 setLux(float lux)270 public void setLux(float lux) { 271 this.mLux = lux; 272 } 273 getPreThresholdLux()274 public float getPreThresholdLux() { 275 return mPreThresholdLux; 276 } 277 setPreThresholdLux(float preThresholdLux)278 public void setPreThresholdLux(float preThresholdLux) { 279 this.mPreThresholdLux = preThresholdLux; 280 } 281 getInitialBrightness()282 public float getInitialBrightness() { 283 return mInitialBrightness; 284 } 285 setInitialBrightness(float mInitialBrightness)286 public void setInitialBrightness(float mInitialBrightness) { 287 this.mInitialBrightness = mInitialBrightness; 288 } 289 getBrightness()290 public float getBrightness() { 291 return mBrightness; 292 } 293 setBrightness(float brightness)294 public void setBrightness(float brightness) { 295 this.mBrightness = brightness; 296 } 297 getRecommendedBrightness()298 public float getRecommendedBrightness() { 299 return mRecommendedBrightness; 300 } 301 setRecommendedBrightness(float recommendedBrightness)302 public void setRecommendedBrightness(float recommendedBrightness) { 303 this.mRecommendedBrightness = recommendedBrightness; 304 } 305 getPreThresholdBrightness()306 public float getPreThresholdBrightness() { 307 return mPreThresholdBrightness; 308 } 309 setPreThresholdBrightness(float preThresholdBrightness)310 public void setPreThresholdBrightness(float preThresholdBrightness) { 311 this.mPreThresholdBrightness = preThresholdBrightness; 312 } 313 getHbmMode()314 public int getHbmMode() { 315 return mHbmMode; 316 } 317 setHbmMode(int hbmMode)318 public void setHbmMode(int hbmMode) { 319 this.mHbmMode = hbmMode; 320 } 321 getHbmMax()322 public float getHbmMax() { 323 return mHbmMax; 324 } 325 setHbmMax(float hbmMax)326 public void setHbmMax(float hbmMax) { 327 this.mHbmMax = hbmMax; 328 } 329 getRbcStrength()330 public int getRbcStrength() { 331 return mRbcStrength; 332 } 333 setRbcStrength(int mRbcStrength)334 public void setRbcStrength(int mRbcStrength) { 335 this.mRbcStrength = mRbcStrength; 336 } 337 isRbcEnabled()338 public boolean isRbcEnabled() { 339 return (mFlags & FLAG_RBC) != 0; 340 } 341 getThermalMax()342 public float getThermalMax() { 343 return mThermalMax; 344 } 345 setThermalMax(float thermalMax)346 public void setThermalMax(float thermalMax) { 347 this.mThermalMax = thermalMax; 348 } 349 getPowerFactor()350 public float getPowerFactor() { 351 return mPowerFactor; 352 } 353 setPowerFactor(float mPowerFactor)354 public void setPowerFactor(float mPowerFactor) { 355 this.mPowerFactor = mPowerFactor; 356 } 357 isLowPowerModeSet()358 public boolean isLowPowerModeSet() { 359 return (mFlags & FLAG_LOW_POWER_MODE) != 0; 360 } 361 362 /** 363 * Set whether the short term model was active before the brightness event. 364 */ setWasShortTermModelActive(boolean wasShortTermModelActive)365 public boolean setWasShortTermModelActive(boolean wasShortTermModelActive) { 366 return this.mWasShortTermModelActive = wasShortTermModelActive; 367 } 368 369 /** 370 * Returns whether the short term model was active before the brightness event. 371 */ wasShortTermModelActive()372 public boolean wasShortTermModelActive() { 373 return this.mWasShortTermModelActive; 374 } 375 getFlags()376 public int getFlags() { 377 return mFlags; 378 } 379 setFlags(int flags)380 public void setFlags(int flags) { 381 this.mFlags = flags; 382 } 383 getAdjustmentFlags()384 public int getAdjustmentFlags() { 385 return mAdjustmentFlags; 386 } 387 setAdjustmentFlags(int adjustmentFlags)388 public void setAdjustmentFlags(int adjustmentFlags) { 389 this.mAdjustmentFlags = adjustmentFlags; 390 } 391 isAutomaticBrightnessEnabled()392 public boolean isAutomaticBrightnessEnabled() { 393 return mAutomaticBrightnessEnabled; 394 } 395 setDisplayBrightnessStrategyName(String displayBrightnessStrategyName)396 public void setDisplayBrightnessStrategyName(String displayBrightnessStrategyName) { 397 mDisplayBrightnessStrategyName = displayBrightnessStrategyName; 398 } 399 getDisplayBrightnessStrategyName()400 public String getDisplayBrightnessStrategyName() { 401 return mDisplayBrightnessStrategyName; 402 } 403 setAutomaticBrightnessEnabled(boolean mAutomaticBrightnessEnabled)404 public void setAutomaticBrightnessEnabled(boolean mAutomaticBrightnessEnabled) { 405 this.mAutomaticBrightnessEnabled = mAutomaticBrightnessEnabled; 406 } 407 408 @AutomaticBrightnessController.AutomaticBrightnessMode getAutoBrightnessMode()409 public int getAutoBrightnessMode() { 410 return mAutoBrightnessMode; 411 } 412 setAutoBrightnessMode( @utomaticBrightnessController.AutomaticBrightnessMode int mode)413 public void setAutoBrightnessMode( 414 @AutomaticBrightnessController.AutomaticBrightnessMode int mode) { 415 mAutoBrightnessMode = mode; 416 } 417 418 /** 419 * A utility to stringify flags from a BrightnessEvent 420 * @return Stringified flags from BrightnessEvent 421 */ 422 @VisibleForTesting flagsToString()423 public String flagsToString() { 424 return ((mFlags & FLAG_USER_SET) != 0 ? "user_set " : "") 425 + ((mFlags & FLAG_RBC) != 0 ? "rbc " : "") 426 + ((mFlags & FLAG_INVALID_LUX) != 0 ? "invalid_lux " : "") 427 + ((mFlags & FLAG_DOZE_SCALE) != 0 ? "doze_scale " : "") 428 + ((mFlags & FLAG_LOW_POWER_MODE) != 0 ? "low_power_mode " : ""); 429 } 430 } 431