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