Debian Codegraph @

core/java/android/hardware/display/BrightnessCorrection.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 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 android.hardware.display;
18
19 import android.annotation.FloatRange;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.MathUtils;
26 import android.util.TypedXmlPullParser;
27 import android.util.TypedXmlSerializer;
28
29 import com.android.internal.util.XmlUtils;
30
31 import org.xmlpull.v1.XmlPullParserException;
32
33 import java.io.IOException;
34
35 /**
36 * BrightnessCorrection encapsulates a correction to the brightness, without comitting to the
37 * actual correction scheme.
38 * It is used by the BrightnessConfiguration, which maps context (e.g. the foreground app's package
39 * name and category) to corrections that need to be applied to the brightness within that context.
40 * Corrections are currently done by the app that has the top activity of the focused stack, either
41 * by its package name, or (if its package name is not mapped to any correction) by its category.
42 *
43 * @hide
44 */
45 @SystemApi
46 public final class BrightnessCorrection implements Parcelable {
47
48 private static final int SCALE_AND_TRANSLATE_LOG = 1;
49
50 private static final String TAG_SCALE_AND_TRANSLATE_LOG = "scale-and-translate-log";
51
52 private BrightnessCorrectionImplementation mImplementation;
53
54 // Parcelable classes must be final, and protected methods are not allowed in APIs, so we can't
55 // make this class abstract and use composition instead of inheritence.
56 private BrightnessCorrection(BrightnessCorrectionImplementation implementation) {
57 mImplementation = implementation;
58 }
59
60 /**
61 * Creates a BrightnessCorrection that given {@code brightness}, corrects it to be
62 * {@code exp(scale * ln(brightness) + translate)}.
63 *
64 * @param scale
65 * How much to scale the log (base e) brightness.
66 * @param translate
67 * How much to translate the log (base e) brightness.
68 *
69 * @return A BrightnessCorrection that given {@code brightness}, corrects it to be
70 * {@code exp(scale * ln(brightness) + translate)}.
71 *
72 * @throws IllegalArgumentException
73 * - scale or translate are NaN.
74 */
75 @NonNull
76 public static BrightnessCorrection createScaleAndTranslateLog(float scale, float translate) {
77 BrightnessCorrectionImplementation implementation =
78 new ScaleAndTranslateLog(scale, translate);
79 return new BrightnessCorrection(implementation);
80 }
81
82 /**
83 * Applies the brightness correction to a given brightness.
84 *
85 * @param brightness
86 * The brightness.
87 *
88 * @return The corrected brightness.
89 */
90 @FloatRange(from = 0.0)
91 public float apply(@FloatRange(from = 0.0) float brightness) {
92 return mImplementation.apply(brightness);
93 }
94
95 /**
96 * Returns a string representation.
97 *
98 * @return A string representation.
99 */
100 @NonNull
101 public String toString() {
102 return mImplementation.toString();
103 }
104
105 @Override
106 public boolean equals(@Nullable Object o) {
107 if (o == this) {
108 return true;
109 }
110 if (!(o instanceof BrightnessCorrection)) {
111 return false;
112 }
113 BrightnessCorrection other = (BrightnessCorrection) o;
114 return other.mImplementation.equals(mImplementation);
115 }
116
117 @Override
118 public int hashCode() {
119 return mImplementation.hashCode();
120 }
121
122 public static final @android.annotation.NonNull Creator<BrightnessCorrection> CREATOR =
123 new Creator<BrightnessCorrection>() {
124 public BrightnessCorrection createFromParcel(Parcel in) {
125 final int type = in.readInt();
126 switch (type) {
127 case SCALE_AND_TRANSLATE_LOG:
128 return ScaleAndTranslateLog.readFromParcel(in);
129 }
130 return null;
131 }
132
133 public BrightnessCorrection[] newArray(int size) {
134 return new BrightnessCorrection[size];
135 }
136 };
137
138 @Override
139 public void writeToParcel(Parcel dest, int flags) {
140 mImplementation.writeToParcel(dest);
141 }
142
143 @Override
144 public int describeContents() {
145 return 0;
146 }
147
148 /**
149 * Writes the correction to an XML serializer.
150 *
151 * @param serializer
152 * The XML serializer.
153 *
154 * @hide
155 */
156 public void saveToXml(TypedXmlSerializer serializer) throws IOException {
157 mImplementation.saveToXml(serializer);
158 }
159
160 /**
161 * Read a correction from an XML parser.
162 *
163 * @param parser
164 * The XML parser.
165 *
166 * @throws IOException
167 * The parser failed to read the XML file.
168 * @throws XmlPullParserException
169 * The parser failed to parse the XML file.
170 *
171 * @hide
172 */
173 public static BrightnessCorrection loadFromXml(TypedXmlPullParser parser) throws IOException,
174 XmlPullParserException {
175 final int depth = parser.getDepth();
176 while (XmlUtils.nextElementWithin(parser, depth)) {
177 if (TAG_SCALE_AND_TRANSLATE_LOG.equals(parser.getName())) {
178 return ScaleAndTranslateLog.loadFromXml(parser);
179 }
180 }
181 return null;
182 }
183
184 private static float loadFloatFromXml(TypedXmlPullParser parser, String attribute) {
185 return parser.getAttributeFloat(null, attribute, Float.NaN);
186 }
187
188 private interface BrightnessCorrectionImplementation {
189 float apply(float brightness);
190 String toString();
191 void writeToParcel(Parcel dest);
192 void saveToXml(TypedXmlSerializer serializer) throws IOException;
193 // Package-private static methods:
194 // static BrightnessCorrection readFromParcel(Parcel in);
195 // static BrightnessCorrection loadFromXml(XmlPullParser parser) throws IOException,
196 // XmlPullParserException;
197 }
198
199 /**
200 * A BrightnessCorrection that given {@code brightness}, corrects it to be
201 * {@code exp(scale * ln(brightness) + translate)}.
202 */
203 private static class ScaleAndTranslateLog implements BrightnessCorrectionImplementation {
204 private static final float MIN_SCALE = 0.5f;
205 private static final float MAX_SCALE = 2.0f;
206 private static final float MIN_TRANSLATE = -0.6f;
207 private static final float MAX_TRANSLATE = 0.7f;
208
209 private static final String ATTR_SCALE = "scale";
210 private static final String ATTR_TRANSLATE = "translate";
211
212 private final float mScale;
213 private final float mTranslate;
214
215 ScaleAndTranslateLog(float scale, float translate) {
216 if (Float.isNaN(scale) || Float.isNaN(translate)) {
217 throw new IllegalArgumentException("scale and translate must be numbers");
218 }
219 mScale = MathUtils.constrain(scale, MIN_SCALE, MAX_SCALE);
220 mTranslate = MathUtils.constrain(translate, MIN_TRANSLATE, MAX_TRANSLATE);
221 }
222
223 @Override
224 public float apply(float brightness) {
225 return MathUtils.exp(mScale * MathUtils.log(brightness) + mTranslate);
226 }
227
228 @Override
229 public String toString() {
230 return "ScaleAndTranslateLog(" + mScale + ", " + mTranslate + ")";
231 }
232
233 @Override
234 public boolean equals(@Nullable Object o) {
235 if (o == this) {
236 return true;
237 }
238 if (!(o instanceof ScaleAndTranslateLog)) {
239 return false;
240 }
241 ScaleAndTranslateLog other = (ScaleAndTranslateLog) o;
242 return other.mScale == mScale && other.mTranslate == mTranslate;
243 }
244
245 @Override
246 public int hashCode() {
247 int result = 1;
248 result = result * 31 + Float.hashCode(mScale);
249 result = result * 31 + Float.hashCode(mTranslate);
250 return result;
251 }
252
253 @Override
254 public void writeToParcel(Parcel dest) {
255 dest.writeInt(SCALE_AND_TRANSLATE_LOG);
256 dest.writeFloat(mScale);
257 dest.writeFloat(mTranslate);
258 }
259
260 @Override
261 public void saveToXml(TypedXmlSerializer serializer) throws IOException {
262 serializer.startTag(null, TAG_SCALE_AND_TRANSLATE_LOG);
263 serializer.attributeFloat(null, ATTR_SCALE, mScale);
264 serializer.attributeFloat(null, ATTR_TRANSLATE, mTranslate);
265 serializer.endTag(null, TAG_SCALE_AND_TRANSLATE_LOG);
266 }
267
268 static BrightnessCorrection readFromParcel(Parcel in) {
269 float scale = in.readFloat();
270 float translate = in.readFloat();
271 return BrightnessCorrection.createScaleAndTranslateLog(scale, translate);
272 }
273
274 static BrightnessCorrection loadFromXml(TypedXmlPullParser parser) throws IOException,
275 XmlPullParserException {
276 final float scale = loadFloatFromXml(parser, ATTR_SCALE);
277 final float translate = loadFloatFromXml(parser, ATTR_TRANSLATE);
278 return BrightnessCorrection.createScaleAndTranslateLog(scale, translate);
279 }
280 }
281 }