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.brightness.strategy;
18 
19 import android.os.PowerManager;
20 
21 import com.android.server.display.DisplayBrightnessState;
22 import com.android.server.display.brightness.BrightnessReason;
23 import com.android.server.display.brightness.StrategyExecutionRequest;
24 import com.android.server.display.brightness.StrategySelectionNotifyRequest;
25 import com.android.server.display.feature.DisplayManagerFlags;
26 
27 import java.io.PrintWriter;
28 
29 /**
30  * Manages the brightness of the display when auto-brightness is on, the screen has just turned on
31  * and there is no available lux reading yet. The brightness value is read from the offload chip.
32  */
33 public class OffloadBrightnessStrategy implements DisplayBrightnessStrategy {
34 
35     private float mOffloadScreenBrightness;
36     private final DisplayManagerFlags mDisplayManagerFlags;
37 
OffloadBrightnessStrategy(DisplayManagerFlags displayManagerFlags)38     public OffloadBrightnessStrategy(DisplayManagerFlags displayManagerFlags) {
39         mDisplayManagerFlags = displayManagerFlags;
40         mOffloadScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
41     }
42 
43     @Override
updateBrightness( StrategyExecutionRequest strategyExecutionRequest)44     public DisplayBrightnessState updateBrightness(
45             StrategyExecutionRequest strategyExecutionRequest) {
46         float offloadBrightness = mOffloadScreenBrightness;
47         if (mDisplayManagerFlags.isRefactorDisplayPowerControllerEnabled()) {
48             // We reset the offload brightness to invalid so that there is no stale value lingering
49             // around. After this request is processed, the current brightness will be set to
50             // offload brightness. Hence even if the lux values don't become valid for the next
51             // request, we will fallback to the current brightness anyways.
52             mOffloadScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
53         }
54         BrightnessReason brightnessReason = new BrightnessReason();
55         brightnessReason.setReason(BrightnessReason.REASON_OFFLOAD);
56         return new DisplayBrightnessState.Builder()
57                 .setBrightness(offloadBrightness)
58                 .setSdrBrightness(offloadBrightness)
59                 .setBrightnessReason(brightnessReason)
60                 .setDisplayBrightnessStrategyName(getName())
61                 .setIsSlowChange(false)
62                 .setShouldUpdateScreenBrightnessSetting(true)
63                 .build();
64     }
65 
66     @Override
getName()67     public String getName() {
68         return "OffloadBrightnessStrategy";
69     }
70 
getOffloadScreenBrightness()71     public float getOffloadScreenBrightness() {
72         return mOffloadScreenBrightness;
73     }
74 
setOffloadScreenBrightness(float offloadScreenBrightness)75     public void setOffloadScreenBrightness(float offloadScreenBrightness) {
76         mOffloadScreenBrightness = offloadScreenBrightness;
77     }
78 
79     /**
80      * Dumps the state of this class.
81      */
82     @Override
dump(PrintWriter writer)83     public void dump(PrintWriter writer) {
84         writer.println("OffloadBrightnessStrategy:");
85         writer.println("  mOffloadScreenBrightness:" + mOffloadScreenBrightness);
86     }
87 
88     @Override
strategySelectionPostProcessor( StrategySelectionNotifyRequest strategySelectionNotifyRequest)89     public void strategySelectionPostProcessor(
90             StrategySelectionNotifyRequest strategySelectionNotifyRequest) {
91         // We reset the offload brightness only if the selected strategy is not offload or invalid,
92         // as we are yet to use the brightness to evaluate the brightness state.
93         if (!strategySelectionNotifyRequest.getSelectedDisplayBrightnessStrategy().getName()
94                 .equals(getName())
95                 && !strategySelectionNotifyRequest.getSelectedDisplayBrightnessStrategy().getName()
96                 .equals(
97                 DisplayBrightnessStrategyConstants.INVALID_BRIGHTNESS_STRATEGY_NAME)) {
98             mOffloadScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
99         }
100     }
101 
102     @Override
getReason()103     public int getReason() {
104         return BrightnessReason.REASON_OFFLOAD;
105     }
106 }
107