1 /*
2  * Copyright (C) 2014 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.hardware.SensorManager;
20 import android.os.Handler;
21 import android.os.PowerManager;
22 import android.util.IntArray;
23 import android.util.SparseArray;
24 import android.view.Display;
25 import android.view.DisplayInfo;
26 import android.view.SurfaceControl;
27 
28 /**
29  * Display manager local system service interface.
30  *
31  * @hide Only for use within the system server.
32  */
33 public abstract class DisplayManagerInternal {
34     /**
35      * Called by the power manager to initialize power management facilities.
36      */
initPowerManagement(DisplayPowerCallbacks callbacks, Handler handler, SensorManager sensorManager)37     public abstract void initPowerManagement(DisplayPowerCallbacks callbacks,
38             Handler handler, SensorManager sensorManager);
39 
40     /**
41      * Called by the power manager to request a new power state.
42      * <p>
43      * The display power controller makes a copy of the provided object and then
44      * begins adjusting the power state to match what was requested.
45      * </p>
46      *
47      * @param request The requested power state.
48      * @param waitForNegativeProximity If true, issues a request to wait for
49      * negative proximity before turning the screen back on, assuming the screen
50      * was turned off by the proximity sensor.
51      * @return True if display is ready, false if there are important changes that must
52      * be made asynchronously (such as turning the screen on), in which case the caller
53      * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()}
54      * then try the request again later until the state converges.
55      */
requestPowerState(DisplayPowerRequest request, boolean waitForNegativeProximity)56     public abstract boolean requestPowerState(DisplayPowerRequest request,
57             boolean waitForNegativeProximity);
58 
59     /**
60      * Returns true if the proximity sensor screen-off function is available.
61      */
isProximitySensorAvailable()62     public abstract boolean isProximitySensorAvailable();
63 
64     /**
65      * Returns information about the specified logical display.
66      *
67      * @param displayId The logical display id.
68      * @return The logical display info, or null if the display does not exist.  The
69      * returned object must be treated as immutable.
70      */
getDisplayInfo(int displayId)71     public abstract DisplayInfo getDisplayInfo(int displayId);
72 
73     /**
74      * Registers a display transaction listener to provide the client a chance to
75      * update its surfaces within the same transaction as any display layout updates.
76      *
77      * @param listener The listener to register.
78      */
registerDisplayTransactionListener(DisplayTransactionListener listener)79     public abstract void registerDisplayTransactionListener(DisplayTransactionListener listener);
80 
81     /**
82      * Unregisters a display transaction listener to provide the client a chance to
83      * update its surfaces within the same transaction as any display layout updates.
84      *
85      * @param listener The listener to unregister.
86      */
unregisterDisplayTransactionListener(DisplayTransactionListener listener)87     public abstract void unregisterDisplayTransactionListener(DisplayTransactionListener listener);
88 
89     /**
90      * Overrides the display information of a particular logical display.
91      * This is used by the window manager to control the size and characteristics
92      * of the default display.  It is expected to apply the requested change
93      * to the display information synchronously so that applications will immediately
94      * observe the new state.
95      *
96      * NOTE: This method must be the only entry point by which the window manager
97      * influences the logical configuration of displays.
98      *
99      * @param displayId The logical display id.
100      * @param info The new data to be stored.
101      */
setDisplayInfoOverrideFromWindowManager( int displayId, DisplayInfo info)102     public abstract void setDisplayInfoOverrideFromWindowManager(
103             int displayId, DisplayInfo info);
104 
105     /**
106      * Get current display info without override from WindowManager.
107      * Current implementation of LogicalDisplay#getDisplayInfoLocked() always returns display info
108      * with overrides from WM if set. This method can be used for getting real display size without
109      * overrides to determine if real changes to display metrics happened.
110      * @param displayId Id of the target display.
111      * @param outInfo {@link DisplayInfo} to fill.
112      */
getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo)113     public abstract void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo);
114 
115     /**
116      * Called by the window manager to perform traversals while holding a
117      * surface flinger transaction.
118      */
performTraversal(SurfaceControl.Transaction t)119     public abstract void performTraversal(SurfaceControl.Transaction t);
120 
121     /**
122      * Tells the display manager about properties of the display that depend on the windows on it.
123      * This includes whether there is interesting unique content on the specified logical display,
124      * and whether the one of the windows has a preferred refresh rate.
125      * <p>
126      * If the display has unique content, then the display manager arranges for it
127      * to be presented on a physical display if appropriate.  Otherwise, the display manager
128      * may choose to make the physical display mirror some other logical display.
129      * </p>
130      *
131      * <p>
132      * If one of the windows on the display has a preferred refresh rate that's supported by the
133      * display, then the display manager will request its use.
134      * </p>
135      *
136      * @param displayId The logical display id to update.
137      * @param hasContent True if the logical display has content. This is used to control automatic
138      * mirroring.
139      * @param requestedRefreshRate The preferred refresh rate for the top-most visible window that
140      * has a preference.
141      * @param requestedModeId The preferred mode id for the top-most visible window that has a
142      * preference.
143      * @param inTraversal True if called from WindowManagerService during a window traversal
144      * prior to call to performTraversalInTransactionFromWindowManager.
145      */
setDisplayProperties(int displayId, boolean hasContent, float requestedRefreshRate, int requestedModeId, boolean inTraversal)146     public abstract void setDisplayProperties(int displayId, boolean hasContent,
147             float requestedRefreshRate, int requestedModeId, boolean inTraversal);
148 
149     /**
150      * Applies an offset to the contents of a display, for example to avoid burn-in.
151      * <p>
152      * TODO: Technically this should be associated with a physical rather than logical
153      * display but this is good enough for now.
154      * </p>
155      *
156      * @param displayId The logical display id to update.
157      * @param x The X offset by which to shift the contents of the display.
158      * @param y The Y offset by which to shift the contents of the display.
159      */
setDisplayOffsets(int displayId, int x, int y)160     public abstract void setDisplayOffsets(int displayId, int x, int y);
161 
162     /**
163      * Provide a list of UIDs that are present on the display and are allowed to access it.
164      *
165      * @param displayAccessUIDs Mapping displayId -> int array of UIDs.
166      */
setDisplayAccessUIDs(SparseArray<IntArray> displayAccessUIDs)167     public abstract void setDisplayAccessUIDs(SparseArray<IntArray> displayAccessUIDs);
168 
169     /**
170      * Check if specified UID's content is present on display and should be granted access to it.
171      *
172      * @param uid UID to be checked.
173      * @param displayId id of the display where presence of the content is checked.
174      * */
isUidPresentOnDisplay(int uid, int displayId)175     public abstract boolean isUidPresentOnDisplay(int uid, int displayId);
176 
177     /**
178      * Persist brightness slider events and ambient brightness stats.
179      */
persistBrightnessTrackerState()180     public abstract void persistBrightnessTrackerState();
181 
182     /**
183      * Notifies the display manager that resource overlays have changed.
184      */
onOverlayChanged()185     public abstract void onOverlayChanged();
186 
187     /**
188      * Describes the requested power state of the display.
189      *
190      * This object is intended to describe the general characteristics of the
191      * power state, such as whether the screen should be on or off and the current
192      * brightness controls leaving the DisplayPowerController to manage the
193      * details of how the transitions between states should occur.  The goal is for
194      * the PowerManagerService to focus on the global power state and not
195      * have to micro-manage screen off animations, auto-brightness and other effects.
196      */
197     public static final class DisplayPowerRequest {
198         // Policy: Turn screen off as if the user pressed the power button
199         // including playing a screen off animation if applicable.
200         public static final int POLICY_OFF = 0;
201         // Policy: Enable dozing and always-on display functionality.
202         public static final int POLICY_DOZE = 1;
203         // Policy: Make the screen dim when the user activity timeout is
204         // about to expire.
205         public static final int POLICY_DIM = 2;
206         // Policy: Make the screen bright as usual.
207         public static final int POLICY_BRIGHT = 3;
208         // Policy: Keep the screen and display optimized for VR mode.
209         public static final int POLICY_VR = 4;
210 
211         // The basic overall policy to apply: off, doze, dim or bright.
212         public int policy;
213 
214         // If true, the proximity sensor overrides the screen state when an object is
215         // nearby, turning it off temporarily until the object is moved away.
216         public boolean useProximitySensor;
217 
218         // An override of the screen brightness. Set to -1 is used if there's no override.
219         public int screenBrightnessOverride;
220 
221         // An override of the screen auto-brightness adjustment factor in the range -1 (dimmer) to
222         // 1 (brighter). Set to Float.NaN if there's no override.
223         public float screenAutoBrightnessAdjustmentOverride;
224 
225         // If true, enables automatic brightness control.
226         public boolean useAutoBrightness;
227 
228         // If true, scales the brightness to half of desired.
229         public boolean lowPowerMode;
230 
231         // The factor to adjust the screen brightness in low power mode in the range
232         // 0 (screen off) to 1 (no change)
233         public float screenLowPowerBrightnessFactor;
234 
235         // If true, applies a brightness boost.
236         public boolean boostScreenBrightness;
237 
238         // If true, prevents the screen from completely turning on if it is currently off.
239         // The display does not enter a "ready" state if this flag is true and screen on is
240         // blocked.  The window manager policy blocks screen on while it prepares the keyguard to
241         // prevent the user from seeing intermediate updates.
242         //
243         // Technically, we may not block the screen itself from turning on (because that introduces
244         // extra unnecessary latency) but we do prevent content on screen from becoming
245         // visible to the user.
246         public boolean blockScreenOn;
247 
248         // Overrides the policy for adjusting screen brightness and state while dozing.
249         public int dozeScreenBrightness;
250         public int dozeScreenState;
251 
DisplayPowerRequest()252         public DisplayPowerRequest() {
253             policy = POLICY_BRIGHT;
254             useProximitySensor = false;
255             screenBrightnessOverride = -1;
256             useAutoBrightness = false;
257             screenAutoBrightnessAdjustmentOverride = Float.NaN;
258             screenLowPowerBrightnessFactor = 0.5f;
259             blockScreenOn = false;
260             dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
261             dozeScreenState = Display.STATE_UNKNOWN;
262         }
263 
DisplayPowerRequest(DisplayPowerRequest other)264         public DisplayPowerRequest(DisplayPowerRequest other) {
265             copyFrom(other);
266         }
267 
isBrightOrDim()268         public boolean isBrightOrDim() {
269             return policy == POLICY_BRIGHT || policy == POLICY_DIM;
270         }
271 
isVr()272         public boolean isVr() {
273             return policy == POLICY_VR;
274         }
275 
copyFrom(DisplayPowerRequest other)276         public void copyFrom(DisplayPowerRequest other) {
277             policy = other.policy;
278             useProximitySensor = other.useProximitySensor;
279             screenBrightnessOverride = other.screenBrightnessOverride;
280             useAutoBrightness = other.useAutoBrightness;
281             screenAutoBrightnessAdjustmentOverride = other.screenAutoBrightnessAdjustmentOverride;
282             screenLowPowerBrightnessFactor = other.screenLowPowerBrightnessFactor;
283             blockScreenOn = other.blockScreenOn;
284             lowPowerMode = other.lowPowerMode;
285             boostScreenBrightness = other.boostScreenBrightness;
286             dozeScreenBrightness = other.dozeScreenBrightness;
287             dozeScreenState = other.dozeScreenState;
288         }
289 
290         @Override
equals(Object o)291         public boolean equals(Object o) {
292             return o instanceof DisplayPowerRequest
293                     && equals((DisplayPowerRequest)o);
294         }
295 
equals(DisplayPowerRequest other)296         public boolean equals(DisplayPowerRequest other) {
297             return other != null
298                     && policy == other.policy
299                     && useProximitySensor == other.useProximitySensor
300                     && screenBrightnessOverride == other.screenBrightnessOverride
301                     && useAutoBrightness == other.useAutoBrightness
302                     && floatEquals(screenAutoBrightnessAdjustmentOverride,
303                             other.screenAutoBrightnessAdjustmentOverride)
304                     && screenLowPowerBrightnessFactor
305                     == other.screenLowPowerBrightnessFactor
306                     && blockScreenOn == other.blockScreenOn
307                     && lowPowerMode == other.lowPowerMode
308                     && boostScreenBrightness == other.boostScreenBrightness
309                     && dozeScreenBrightness == other.dozeScreenBrightness
310                     && dozeScreenState == other.dozeScreenState;
311         }
312 
floatEquals(float f1, float f2)313         private boolean floatEquals(float f1, float f2) {
314             return f1 == f2 || Float.isNaN(f1) && Float.isNaN(f2);
315         }
316 
317         @Override
hashCode()318         public int hashCode() {
319             return 0; // don't care
320         }
321 
322         @Override
toString()323         public String toString() {
324             return "policy=" + policyToString(policy)
325                     + ", useProximitySensor=" + useProximitySensor
326                     + ", screenBrightnessOverride=" + screenBrightnessOverride
327                     + ", useAutoBrightness=" + useAutoBrightness
328                     + ", screenAutoBrightnessAdjustmentOverride="
329                     + screenAutoBrightnessAdjustmentOverride
330                     + ", screenLowPowerBrightnessFactor=" + screenLowPowerBrightnessFactor
331                     + ", blockScreenOn=" + blockScreenOn
332                     + ", lowPowerMode=" + lowPowerMode
333                     + ", boostScreenBrightness=" + boostScreenBrightness
334                     + ", dozeScreenBrightness=" + dozeScreenBrightness
335                     + ", dozeScreenState=" + Display.stateToString(dozeScreenState);
336         }
337 
policyToString(int policy)338         public static String policyToString(int policy) {
339             switch (policy) {
340                 case POLICY_OFF:
341                     return "OFF";
342                 case POLICY_DOZE:
343                     return "DOZE";
344                 case POLICY_DIM:
345                     return "DIM";
346                 case POLICY_BRIGHT:
347                     return "BRIGHT";
348                 case POLICY_VR:
349                     return "VR";
350                 default:
351                     return Integer.toString(policy);
352             }
353         }
354     }
355 
356     /**
357      * Asynchronous callbacks from the power controller to the power manager service.
358      */
359     public interface DisplayPowerCallbacks {
onStateChanged()360         void onStateChanged();
onProximityPositive()361         void onProximityPositive();
onProximityNegative()362         void onProximityNegative();
onDisplayStateChange(int state)363         void onDisplayStateChange(int state); // one of the Display state constants
364 
acquireSuspendBlocker()365         void acquireSuspendBlocker();
releaseSuspendBlocker()366         void releaseSuspendBlocker();
367     }
368 
369     /**
370      * Called within a Surface transaction whenever the size or orientation of a
371      * display may have changed.  Provides an opportunity for the client to
372      * update the position of its surfaces as part of the same transaction.
373      */
374     public interface DisplayTransactionListener {
onDisplayTransaction()375         void onDisplayTransaction();
376     }
377 }
378