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