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