• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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;
18 
19 import android.annotation.NonNull;
20 import android.os.PowerManager;
21 
22 import com.android.server.display.DisplayDeviceConfig.BrightnessLimitMapType;
23 
24 import java.io.PrintWriter;
25 import java.util.HashMap;
26 import java.util.Map;
27 
28 /**
29  * Limits brightness for normal-brightness mode, based on ambient lux
30  **/
31 class NormalBrightnessModeController {
32     @NonNull
33     private Map<BrightnessLimitMapType, Map<Float, Float>> mMaxBrightnessLimits = new HashMap<>();
34     private float mAmbientLux = Float.MAX_VALUE;
35     private boolean mAutoBrightnessEnabled = false;
36 
37     // brightness limit in normal brightness mode, based on ambient lux.
38     private float mMaxBrightness = PowerManager.BRIGHTNESS_MAX;
39 
onAmbientLuxChange(float ambientLux)40     boolean onAmbientLuxChange(float ambientLux) {
41         mAmbientLux = ambientLux;
42         return recalculateMaxBrightness();
43     }
44 
setAutoBrightnessState(int state)45     boolean setAutoBrightnessState(int state) {
46         boolean isEnabled = state == AutomaticBrightnessController.AUTO_BRIGHTNESS_ENABLED;
47         if (isEnabled != mAutoBrightnessEnabled) {
48             mAutoBrightnessEnabled = isEnabled;
49             return recalculateMaxBrightness();
50         }
51         return false;
52     }
53 
getCurrentBrightnessMax()54     float getCurrentBrightnessMax() {
55         return mMaxBrightness;
56     }
57 
resetNbmData( @onNull Map<BrightnessLimitMapType, Map<Float, Float>> maxBrightnessLimits)58     boolean resetNbmData(
59             @NonNull Map<BrightnessLimitMapType, Map<Float, Float>> maxBrightnessLimits) {
60         mMaxBrightnessLimits = maxBrightnessLimits;
61         return recalculateMaxBrightness();
62     }
63 
dump(PrintWriter pw)64     void dump(PrintWriter pw) {
65         pw.println("NormalBrightnessModeController:");
66         pw.println("  mAutoBrightnessEnabled=" + mAutoBrightnessEnabled);
67         pw.println("  mAmbientLux=" + mAmbientLux);
68         pw.println("  mMaxBrightness=" + mMaxBrightness);
69         pw.println("  mMaxBrightnessLimits=" + mMaxBrightnessLimits);
70     }
71 
recalculateMaxBrightness()72     private boolean recalculateMaxBrightness() {
73         float foundAmbientBoundary = Float.MAX_VALUE;
74         float foundMaxBrightness = PowerManager.BRIGHTNESS_MAX;
75 
76         Map<Float, Float> maxBrightnessPoints = null;
77 
78         if (mAutoBrightnessEnabled) {
79             maxBrightnessPoints = mMaxBrightnessLimits.get(BrightnessLimitMapType.ADAPTIVE);
80         }
81 
82         // AutoBrightnessController sends ambientLux values *only* when auto brightness enabled.
83         // Temporary disabling this Controller if auto brightness is off, to avoid capping
84         // brightness based on stale ambient lux. The issue is tracked here: b/322445088
85         if (mAutoBrightnessEnabled && maxBrightnessPoints == null) {
86             maxBrightnessPoints = mMaxBrightnessLimits.get(BrightnessLimitMapType.DEFAULT);
87         }
88         if (maxBrightnessPoints != null) {
89             for (Map.Entry<Float, Float> brightnessPoint : maxBrightnessPoints.entrySet()) {
90                 float ambientBoundary = brightnessPoint.getKey();
91                 // find ambient lux upper boundary closest to current ambient lux
92                 if (ambientBoundary > mAmbientLux && ambientBoundary < foundAmbientBoundary) {
93                     foundMaxBrightness = brightnessPoint.getValue();
94                     foundAmbientBoundary = ambientBoundary;
95                 }
96             }
97         }
98 
99         if (mMaxBrightness != foundMaxBrightness) {
100             mMaxBrightness = foundMaxBrightness;
101             return true;
102         }
103         return false;
104     }
105 }
106