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