1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.power;
import android.os.Looper;
import android.os.PowerManager;
import android.util.FloatProperty;
import android.util.IntProperty;
import android.util.Slog;
import android.view.Choreographer;
import java.io.PrintWriter;
/**
* Represents the current display power state and realizes it.
*
* This component is similar in nature to a {@link View} except that it describes
* the properties of a display. When properties are changed, the component
* invalidates itself and posts a callback to the {@link Choreographer} to
* apply the changes. This mechanism enables the display power state to be
* animated smoothly by the animation framework.
*
* This component must only be created or accessed by the {@link Looper} thread
* that belongs to the {@link DisplayPowerController}.
*
* We don't need to worry about holding a suspend blocker here because the
* {@link DisplayPowerController} does that for us whenever there is a pending invalidate.
*/
final class DisplayPowerState {
private static final String TAG = "DisplayPowerState";
private static boolean DEBUG = false;
private static final int DIRTY_SCREEN_ON = 1 << 0;
private static final int DIRTY_ELECTRON_BEAM = 1 << 1;
private static final int DIRTY_BRIGHTNESS = 1 << 2;
private final Choreographer mChoreographer;
private final ElectronBeam mElectronBeam;
private final PhotonicModulator mScreenBrightnessModulator;
private int mDirty;
private boolean mScreenOn;
private float mElectronBeamLevel;
private int mScreenBrightness;
private Runnable mCleanListener;
public DisplayPowerState(ElectronBeam electronBean,
PhotonicModulator screenBrightnessModulator) {
mChoreographer = Choreographer.getInstance();
mElectronBeam = electronBean;
mScreenBrightnessModulator = screenBrightnessModulator;
// At boot time, we know that the screen is on and the electron beam
// animation is not playing. We don't know the screen's brightness though,
// so prepare to set it to a known state when the state is next applied.
// Although we set the brightness to full on here, the display power controller
// will reset the brightness to a new level immediately before the changes
// actually have a chance to be applied.
mScreenOn = true;
mElectronBeamLevel = 1.0f;
mScreenBrightness = PowerManager.BRIGHTNESS_ON;
invalidate(DIRTY_BRIGHTNESS);
}
public static final FloatProperty<DisplayPowerState> ELECTRON_BEAM_LEVEL =
new FloatProperty<DisplayPowerState>("electronBeamLevel") {
@Override
public void setValue(DisplayPowerState object, float value) {
object.setElectronBeamLevel(value);
}
@Override
public Float get(DisplayPowerState object) {
return object.getElectronBeamLevel();
}
};
public static final IntProperty<DisplayPowerState> SCREEN_BRIGHTNESS =
new IntProperty<DisplayPowerState>("screenBrightness") {
@Override
public void setValue(DisplayPowerState object, int value) {
object.setScreenBrightness(value);
}
@Override
public Integer get(DisplayPowerState object) {
return object.getScreenBrightness();
}
};
/**
* Sets whether the screen is on or off.
*/
public void setScreenOn(boolean on) {
if (mScreenOn != on) {
if (DEBUG) {
Slog.d(TAG, "setScreenOn: on=" + on);
}
mScreenOn = on;
invalidate(DIRTY_SCREEN_ON);
}
}
/**
* Returns true if the screen is on.
*/
public boolean isScreenOn() {
return mScreenOn;
}
/**
* Prepares the electron beam to turn on or off.
* This method should be called before starting an animation because it
* can take a fair amount of time to prepare the electron beam surface.
*
* @param warmUp True if the electron beam should start warming up.
* @return True if the electron beam was prepared.
*/
public boolean prepareElectronBeam(boolean warmUp) {
boolean success = mElectronBeam.prepare(warmUp);
invalidate(DIRTY_ELECTRON_BEAM);
return success;
}
/**
* Dismisses the electron beam surface.
*/
public void dismissElectronBeam() {
mElectronBeam.dismiss();
}
/**
* Sets the level of the electron beam steering current.
*
* The display is blanked when the level is 0.0. In normal use, the electron
* beam should have a value of 1.0. The electron beam is unstable in between
* these states and the picture quality may be compromised. For best effect,
* the electron beam should be warmed up or cooled off slowly.
*
* Warning: Electron beam emits harmful radiation. Avoid direct exposure to
* skin or eyes.
*
* @param level The level, ranges from 0.0 (full off) to 1.0 (full on).
*/
public void setElectronBeamLevel(float level) {
if (mElectronBeamLevel != level) {
if (DEBUG) {
Slog.d(TAG, "setElectronBeamLevel: level=" + level);
}
mElectronBeamLevel = level;
invalidate(DIRTY_ELECTRON_BEAM);
}
}
/**
* Gets the level of the electron beam steering current.
*/
public float getElectronBeamLevel() {
return mElectronBeamLevel;
}
/**
* Sets the display brightness.
*
* @param brightness The brightness, ranges from 0 (minimum / off) to 255 (brightest).
*/
public void setScreenBrightness(int brightness) {
if (mScreenBrightness != brightness) {
if (DEBUG) {
Slog.d(TAG, "setScreenBrightness: brightness=" + brightness);
}
mScreenBrightness = brightness;
invalidate(DIRTY_BRIGHTNESS);
}
}
/**
* Gets the screen brightness.
*/
public int getScreenBrightness() {
return mScreenBrightness;
}
/**
* Returns true if no properties have been invalidated.
* Otherwise, returns false and promises to invoke the specified listener
* when the properties have all been applied.
* The listener always overrides any previously set listener.
*/
public boolean waitUntilClean(Runnable listener) {
if (mDirty != 0) {
mCleanListener = listener;
return false;
} else {
mCleanListener = null;
return true;
}
}
public void dump(PrintWriter pw) {
pw.println();
pw.println("Display Power State:");
pw.println(" mDirty=" + Integer.toHexString(mDirty));
pw.println(" mScreenOn=" + mScreenOn);
pw.println(" mScreenBrightness=" + mScreenBrightness);
pw.println(" mElectronBeamLevel=" + mElectronBeamLevel);
mElectronBeam.dump(pw);
}
private void invalidate(int dirty) {
if (mDirty == 0) {
mChoreographer.postCallback(Choreographer.CALLBACK_TRAVERSAL,
mTraversalRunnable, null);
}
mDirty |= dirty;
}
private void apply() {
if (mDirty != 0) {
if ((mDirty & DIRTY_SCREEN_ON) != 0 && !mScreenOn) {
mScreenBrightnessModulator.setBrightness(0, true /*sync*/);
PowerManagerService.nativeSetScreenState(false);
}
if ((mDirty & DIRTY_ELECTRON_BEAM) != 0) {
mElectronBeam.draw(mElectronBeamLevel);
}
if ((mDirty & DIRTY_SCREEN_ON) != 0 && mScreenOn) {
PowerManagerService.nativeSetScreenState(true);
}
if ((mDirty & (DIRTY_BRIGHTNESS | DIRTY_SCREEN_ON | DIRTY_ELECTRON_BEAM)) != 0
&& mScreenOn) {
mScreenBrightnessModulator.setBrightness(
(int)(mScreenBrightness * mElectronBeamLevel), false /*sync*/);
}
mDirty = 0;
if (mCleanListener != null) {
mCleanListener.run();
}
}
}
private final Runnable mTraversalRunnable = new Runnable() {
@Override
public void run() {
if (mDirty != 0) {
apply();
}
}
};
}
|