2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.annotation.IntDef;
20 import android.annotation.RequiresPermission;
21 import android.content.Context;
22 import android.content.res.CompatibilityInfo;
23 import android.content.res.Resources;
24 import android.graphics.PixelFormat;
25 import android.graphics.Point;
26 import android.graphics.Rect;
27 import android.hardware.display.DisplayManagerGlobal;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 import android.os.Process;
31 import android.os.SystemClock;
32 import android.util.DisplayMetrics;
33 import android.util.Log;
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37 import java.util.Arrays;
39 import static android.Manifest.permission.CONFIGURE_DISPLAY_COLOR_TRANSFORM;
42 * Provides information about the size and density of a logical display.
44 * The display area is described in two different ways.
46 * <li>The application display area specifies the part of the display that may contain
47 * an application window, excluding the system decorations. The application display area may
48 * be smaller than the real display area because the system subtracts the space needed
49 * for decor elements such as the status bar. Use the following methods to query the
50 * application display area: {@link #getSize}, {@link #getRectSize} and {@link #getMetrics}.</li>
51 * <li>The real display area specifies the part of the display that contains content
52 * including the system decorations. Even so, the real display area may be smaller than the
53 * physical size of the display if the window manager is emulating a smaller display
54 * using (adb shell am display-size). Use the following methods to query the
55 * real display area: {@link #getRealSize}, {@link #getRealMetrics}.</li>
58 * A logical display does not necessarily represent a particular physical display device
59 * such as the built-in screen or an external monitor. The contents of a logical
60 * display may be presented on one or more physical displays according to the devices
61 * that are currently attached and whether mirroring has been enabled.
64 public final class Display {
65 private static final String TAG = "Display";
66 private static final boolean DEBUG = false;
68 private final DisplayManagerGlobal mGlobal;
69 private final int mDisplayId;
70 private final int mLayerStack;
71 private final int mFlags;
72 private final int mType;
73 private final String mAddress;
74 private final int mOwnerUid;
75 private final String mOwnerPackageName;
76 private final DisplayAdjustments mDisplayAdjustments;
78 private DisplayInfo mDisplayInfo; // never null
79 private boolean mIsValid;
81 // Temporary display metrics structure used for compatibility mode.
82 private final DisplayMetrics mTempMetrics = new DisplayMetrics();
84 // We cache the app width and height properties briefly between calls
85 // to getHeight() and getWidth() to ensure that applications perceive
86 // consistent results when the size changes (most of the time).
87 // Applications should now be using getSize() instead.
88 private static final int CACHED_APP_SIZE_DURATION_MILLIS = 20;
89 private long mLastCachedAppSizeUpdate;
90 private int mCachedAppWidthCompat;
91 private int mCachedAppHeightCompat;
94 * The default Display id, which is the id of the built-in primary display
95 * assuming there is one.
97 public static final int DEFAULT_DISPLAY = 0;
100 * Invalid display id.
102 public static final int INVALID_DISPLAY = -1;
105 * Display flag: Indicates that the display supports compositing content
106 * that is stored in protected graphics buffers.
108 * If this flag is set then the display device supports compositing protected buffers.
110 * If this flag is not set then the display device may not support compositing
111 * protected buffers; the user may see a blank region on the screen instead of
112 * the protected content.
114 * Secure (DRM) video decoders may allocate protected graphics buffers to request that
115 * a hardware-protected path be provided between the video decoder and the external
116 * display sink. If a hardware-protected path is not available, then content stored
117 * in protected graphics buffers may not be composited.
119 * An application can use the absence of this flag as a hint that it should not use protected
120 * buffers for this display because the content may not be visible. For example,
121 * if the flag is not set then the application may choose not to show content on this
122 * display, show an informative error message, select an alternate content stream
123 * or adopt a different strategy for decoding content that does not rely on
129 public static final int FLAG_SUPPORTS_PROTECTED_BUFFERS = 1 << 0;
132 * Display flag: Indicates that the display has a secure video output and
133 * supports compositing secure surfaces.
135 * If this flag is set then the display device has a secure video output
136 * and is capable of showing secure surfaces. It may also be capable of
137 * showing {@link #FLAG_SUPPORTS_PROTECTED_BUFFERS protected buffers}.
139 * If this flag is not set then the display device may not have a secure video
140 * output; the user may see a blank region on the screen instead of
141 * the contents of secure surfaces or protected buffers.
143 * Secure surfaces are used to prevent content rendered into those surfaces
144 * by applications from appearing in screenshots or from being viewed
145 * on non-secure displays. Protected buffers are used by secure video decoders
146 * for a similar purpose.
148 * An application creates a window with a secure surface by specifying the
149 * {@link WindowManager.LayoutParams#FLAG_SECURE} window flag.
150 * Likewise, an application creates a {@link SurfaceView} with a secure surface
151 * by calling {@link SurfaceView#setSecure} before attaching the secure view to
152 * its containing window.
154 * An application can use the absence of this flag as a hint that it should not create
155 * secure surfaces or protected buffers on this display because the content may
156 * not be visible. For example, if the flag is not set then the application may
157 * choose not to show content on this display, show an informative error message,
158 * select an alternate content stream or adopt a different strategy for decoding
159 * content that does not rely on secure surfaces or protected buffers.
164 public static final int FLAG_SECURE = 1 << 1;
167 * Display flag: Indicates that the display is private. Only the application that
168 * owns the display can create windows on it.
172 public static final int FLAG_PRIVATE = 1 << 2;
175 * Display flag: Indicates that the display is a presentation display.
177 * This flag identifies secondary displays that are suitable for
178 * use as presentation displays such as HDMI or Wireless displays. Applications
179 * may automatically project their content to presentation displays to provide
180 * richer second screen experiences.
185 public static final int FLAG_PRESENTATION = 1 << 3;
188 * Display flag: Indicates that the display has a round shape.
190 * This flag identifies displays that are circular, elliptical or otherwise
191 * do not permit the user to see all the way to the logical corners of the display.
196 public static final int FLAG_ROUND = 1 << 4;
199 * Display flag: Indicates that the contents of the display should not be scaled
200 * to fit the physical screen dimensions. Used for development only to emulate
201 * devices with smaller physicals screens while preserving density.
205 public static final int FLAG_SCALING_DISABLED = 1 << 30;
208 * Display type: Unknown display type.
211 public static final int TYPE_UNKNOWN = 0;
214 * Display type: Built-in display.
217 public static final int TYPE_BUILT_IN = 1;
220 * Display type: HDMI display.
223 public static final int TYPE_HDMI = 2;
226 * Display type: WiFi display.
229 public static final int TYPE_WIFI = 3;
232 * Display type: Overlay display.
235 public static final int TYPE_OVERLAY = 4;
238 * Display type: Virtual display.
241 public static final int TYPE_VIRTUAL = 5;
244 * Display state: The display state is unknown.
248 public static final int STATE_UNKNOWN = 0;
251 * Display state: The display is off.
255 public static final int STATE_OFF = 1;
258 * Display state: The display is on.
262 public static final int STATE_ON = 2;
265 * Display state: The display is dozing in a low power state; it is still
266 * on but is optimized for showing system-provided content while the
267 * device is non-interactive.
270 * @see android.os.PowerManager#isInteractive
272 public static final int STATE_DOZE = 3;
275 * Display state: The display is dozing in a suspended low power state; it is still
276 * on but is optimized for showing static system-provided content while the device
277 * is non-interactive. This mode may be used to conserve even more power by allowing
278 * the hardware to stop applying frame buffer updates from the graphics subsystem or
279 * to take over the display and manage it autonomously to implement low power always-on
280 * display functionality.
283 * @see android.os.PowerManager#isInteractive
285 public static final int STATE_DOZE_SUSPEND = 4;
288 * Internal method to create a display.
289 * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
290 * or {@link android.hardware.display.DisplayManager#getDisplay}
291 * to get a display object.
295 public Display(DisplayManagerGlobal global,
296 int displayId, DisplayInfo displayInfo /*not null*/,
297 DisplayAdjustments daj) {
299 mDisplayId = displayId;
300 mDisplayInfo = displayInfo;
301 mDisplayAdjustments = new DisplayAdjustments(daj);
304 // Cache properties that cannot change as long as the display is valid.
305 mLayerStack = displayInfo.layerStack;
306 mFlags = displayInfo.flags;
307 mType = displayInfo.type;
308 mAddress = displayInfo.address;
309 mOwnerUid = displayInfo.ownerUid;
310 mOwnerPackageName = displayInfo.ownerPackageName;
314 * Gets the display id.
316 * Each logical display has a unique id.
317 * The default display has id {@link #DEFAULT_DISPLAY}.
320 public int getDisplayId() {
325 * Returns true if this display is still valid, false if the display has been removed.
327 * If the display is invalid, then the methods of this class will
328 * continue to report the most recently observed display information.
329 * However, it is unwise (and rather fruitless) to continue using a
330 * {@link Display} object after the display's demise.
332 * It's possible for a display that was previously invalid to become
333 * valid again if a display with the same id is reconnected.
335 * @return True if the display is still valid.
337 public boolean isValid() {
338 synchronized (this) {
339 updateDisplayInfoLocked();
345 * Gets a full copy of the display information.
347 * @param outDisplayInfo The object to receive the copy of the display information.
348 * @return True if the display is still valid.
351 public boolean getDisplayInfo(DisplayInfo outDisplayInfo) {
352 synchronized (this) {
353 updateDisplayInfoLocked();
354 outDisplayInfo.copyFrom(mDisplayInfo);
360 * Gets the display's layer stack.
362 * Each display has its own independent layer stack upon which surfaces
363 * are placed to be managed by surface flinger.
365 * @return The display's layer stack number.
368 public int getLayerStack() {
373 * Returns a combination of flags that describe the capabilities of the display.
375 * @return The display flags.
377 * @see #FLAG_SUPPORTS_PROTECTED_BUFFERS
381 public int getFlags() {
386 * Gets the display type.
388 * @return The display type.
391 * @see #TYPE_BUILT_IN
398 public int getType() {
403 * Gets the display address, or null if none.
404 * Interpretation varies by display type.
406 * @return The display address.
409 public String getAddress() {
414 * Gets the UID of the application that owns this display, or zero if it is
415 * owned by the system.
417 * If the display is private, then only the owner can use it.
422 public int getOwnerUid() {
427 * Gets the package name of the application that owns this display, or null if it is
428 * owned by the system.
430 * If the display is private, then only the owner can use it.
435 public String getOwnerPackageName() {
436 return mOwnerPackageName;
440 * Gets the compatibility info used by this display instance.
442 * @return The display adjustments holder, or null if none is required.
445 public DisplayAdjustments getDisplayAdjustments() {
446 return mDisplayAdjustments;
450 * Gets the name of the display.
452 * Note that some displays may be renamed by the user.
455 * @return The display's name.
457 public String getName() {
458 synchronized (this) {
459 updateDisplayInfoLocked();
460 return mDisplayInfo.name;
465 * Gets the size of the display, in pixels.
467 * Note that this value should <em>not</em> be used for computing layouts,
468 * since a device will typically have screen decoration (such as a status bar)
469 * along the edges of the display that reduce the amount of application
470 * space available from the size returned here. Layouts should instead use
473 * The size is adjusted based on the current rotation of the display.
475 * The size returned by this method does not necessarily represent the
476 * actual raw size (native resolution) of the display. The returned size may
477 * be adjusted to exclude certain system decoration elements that are always visible.
478 * It may also be scaled to provide compatibility with older applications that
479 * were originally designed for smaller displays.
482 * @param outSize A {@link Point} object to receive the size information.
484 public void getSize(Point outSize) {
485 synchronized (this) {
486 updateDisplayInfoLocked();
487 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
488 outSize.x = mTempMetrics.widthPixels;
489 outSize.y = mTempMetrics.heightPixels;
494 * Gets the size of the display as a rectangle, in pixels.
496 * @param outSize A {@link Rect} object to receive the size information.
497 * @see #getSize(Point)
499 public void getRectSize(Rect outSize) {
500 synchronized (this) {
501 updateDisplayInfoLocked();
502 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
503 outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
508 * Return the range of display sizes an application can expect to encounter
509 * under normal operation, as long as there is no physical change in screen
510 * size. This is basically the sizes you will see as the orientation
511 * changes, taking into account whatever screen decoration there is in
512 * each rotation. For example, the status bar is always at the top of the
513 * screen, so it will reduce the height both in landscape and portrait, and
514 * the smallest height returned here will be the smaller of the two.
516 * This is intended for applications to get an idea of the range of sizes
517 * they will encounter while going through device rotations, to provide a
518 * stable UI through rotation. The sizes here take into account all standard
519 * system decorations that reduce the size actually available to the
520 * application: the status bar, navigation bar, system bar, etc. It does
521 * <em>not</em> take into account more transient elements like an IME
524 * @param outSmallestSize Filled in with the smallest width and height
525 * that the application will encounter, in pixels (not dp units). The x
526 * (width) dimension here directly corresponds to
527 * {@link android.content.res.Configuration#smallestScreenWidthDp
528 * Configuration.smallestScreenWidthDp}, except the value here is in raw
529 * screen pixels rather than dp units. Your application may of course
530 * still get smaller space yet if, for example, a soft keyboard is
532 * @param outLargestSize Filled in with the largest width and height
533 * that the application will encounter, in pixels (not dp units). Your
534 * application may of course still get larger space than this if,
535 * for example, screen decorations like the status bar are being hidden.
537 public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
538 synchronized (this) {
539 updateDisplayInfoLocked();
540 outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
541 outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
542 outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
543 outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
548 * Return the maximum screen size dimension that will happen. This is
549 * mostly for wallpapers.
552 public int getMaximumSizeDimension() {
553 synchronized (this) {
554 updateDisplayInfoLocked();
555 return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
560 * @deprecated Use {@link #getSize(Point)} instead.
563 public int getWidth() {
564 synchronized (this) {
565 updateCachedAppSizeIfNeededLocked();
566 return mCachedAppWidthCompat;
571 * @deprecated Use {@link #getSize(Point)} instead.
574 public int getHeight() {
575 synchronized (this) {
576 updateCachedAppSizeIfNeededLocked();
577 return mCachedAppHeightCompat;
583 * Return a rectangle defining the insets of the overscan region of the display.
584 * Each field of the rectangle is the number of pixels the overscan area extends
585 * into the display on that side.
587 public void getOverscanInsets(Rect outRect) {
588 synchronized (this) {
589 updateDisplayInfoLocked();
590 outRect.set(mDisplayInfo.overscanLeft, mDisplayInfo.overscanTop,
591 mDisplayInfo.overscanRight, mDisplayInfo.overscanBottom);
596 * Returns the rotation of the screen from its "natural" orientation.
597 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
598 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
599 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
600 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For
601 * example, if a device has a naturally tall screen, and the user has
602 * turned it on its side to go into a landscape orientation, the value
603 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
604 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
605 * the direction it was turned. The angle is the rotation of the drawn
606 * graphics on the screen, which is the opposite direction of the physical
607 * rotation of the device. For example, if the device is rotated 90
608 * degrees counter-clockwise, to compensate rendering will be rotated by
609 * 90 degrees clockwise and thus the returned value here will be
610 * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
613 public int getRotation() {
614 synchronized (this) {
615 updateDisplayInfoLocked();
616 return mDisplayInfo.rotation;
621 * @deprecated use {@link #getRotation}
622 * @return orientation of this display.
626 public int getOrientation() {
627 return getRotation();
631 * Gets the pixel format of the display.
632 * @return One of the constants defined in {@link android.graphics.PixelFormat}.
634 * @deprecated This method is no longer supported.
635 * The result is always {@link PixelFormat#RGBA_8888}.
638 public int getPixelFormat() {
639 return PixelFormat.RGBA_8888;
643 * Gets the refresh rate of this display in frames per second.
645 public float getRefreshRate() {
646 synchronized (this) {
647 updateDisplayInfoLocked();
648 return mDisplayInfo.getMode().getRefreshRate();
653 * Get the supported refresh rates of this display in frames per second.
655 * This method only returns refresh rates for the display's default modes. For more options, use
656 * {@link #getSupportedModes()}.
658 * @deprecated use {@link #getSupportedModes()} instead
661 public float[] getSupportedRefreshRates() {
662 synchronized (this) {
663 updateDisplayInfoLocked();
664 return mDisplayInfo.getDefaultRefreshRates();
669 * Returns the active mode of the display.
671 public Mode getMode() {
672 synchronized (this) {
673 updateDisplayInfoLocked();
674 return mDisplayInfo.getMode();
679 * Gets the supported modes of this display.
681 public Mode[] getSupportedModes() {
682 synchronized (this) {
683 updateDisplayInfoLocked();
684 final Display.Mode[] modes = mDisplayInfo.supportedModes;
685 return Arrays.copyOf(modes, modes.length);
690 * Request the display applies a color transform.
693 @RequiresPermission(CONFIGURE_DISPLAY_COLOR_TRANSFORM)
694 public void requestColorTransform(ColorTransform colorTransform) {
695 mGlobal.requestColorTransform(mDisplayId, colorTransform.getId());
699 * Returns the active color transform of this display
702 public ColorTransform getColorTransform() {
703 synchronized (this) {
704 updateDisplayInfoLocked();
705 return mDisplayInfo.getColorTransform();
710 * Returns the default color transform of this display
713 public ColorTransform getDefaultColorTransform() {
714 synchronized (this) {
715 updateDisplayInfoLocked();
716 return mDisplayInfo.getDefaultColorTransform();
721 * Returns the display's HDR capabilities.
723 public HdrCapabilities getHdrCapabilities() {
724 synchronized (this) {
725 updateDisplayInfoLocked();
726 return mDisplayInfo.hdrCapabilities;
731 * Gets the supported color transforms of this device.
734 public ColorTransform[] getSupportedColorTransforms() {
735 synchronized (this) {
736 updateDisplayInfoLocked();
737 ColorTransform[] transforms = mDisplayInfo.supportedColorTransforms;
738 return Arrays.copyOf(transforms, transforms.length);
743 * Gets the app VSYNC offset, in nanoseconds. This is a positive value indicating
744 * the phase offset of the VSYNC events provided by Choreographer relative to the
745 * display refresh. For example, if Choreographer reports that the refresh occurred
746 * at time N, it actually occurred at (N - appVsyncOffset).
748 * Apps generally do not need to be aware of this. It's only useful for fine-grained
749 * A/V synchronization.
751 public long getAppVsyncOffsetNanos() {
752 synchronized (this) {
753 updateDisplayInfoLocked();
754 return mDisplayInfo.appVsyncOffsetNanos;
759 * This is how far in advance a buffer must be queued for presentation at
760 * a given time. If you want a buffer to appear on the screen at
761 * time N, you must submit the buffer before (N - presentationDeadline).
763 * The desired presentation time for GLES rendering may be set with
764 * {@link android.opengl.EGLExt#eglPresentationTimeANDROID}. For video decoding, use
765 * {@link android.media.MediaCodec#releaseOutputBuffer(int, long)}. Times are
766 * expressed in nanoseconds, using the system monotonic clock
767 * ({@link System#nanoTime}).
769 public long getPresentationDeadlineNanos() {
770 synchronized (this) {
771 updateDisplayInfoLocked();
772 return mDisplayInfo.presentationDeadlineNanos;
777 * Gets display metrics that describe the size and density of this display.
778 * The size returned by this method does not necessarily represent the
779 * actual raw size (native resolution) of the display.
781 * 1. The returned size may be adjusted to exclude certain system decor elements
782 * that are always visible.
784 * 2. It may be scaled to provide compatibility with older applications that
785 * were originally designed for smaller displays.
787 * 3. It can be different depending on the WindowManager to which the display belongs.
789 * - If requested from non-Activity context (e.g. Application context via
790 * {@code (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)})
791 * metrics will report real size of the display based on current rotation.
792 * - If requested from activity resulting metrics will correspond to current window metrics.
793 * In this case the size can be smaller than physical size in multi-window mode.
797 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
799 public void getMetrics(DisplayMetrics outMetrics) {
800 synchronized (this) {
801 updateDisplayInfoLocked();
802 mDisplayInfo.getAppMetrics(outMetrics, mDisplayAdjustments);
807 * Gets the real size of the display without subtracting any window decor or
808 * applying any compatibility scale factors.
810 * The size is adjusted based on the current rotation of the display.
812 * The real size may be smaller than the physical size of the screen when the
813 * window manager is emulating a smaller display (using adb shell am display-size).
816 * @param outSize Set to the real size of the display.
818 public void getRealSize(Point outSize) {
819 synchronized (this) {
820 updateDisplayInfoLocked();
821 outSize.x = mDisplayInfo.logicalWidth;
822 outSize.y = mDisplayInfo.logicalHeight;
827 * Gets display metrics based on the real size of this display.
829 * The size is adjusted based on the current rotation of the display.
831 * The real size may be smaller than the physical size of the screen when the
832 * window manager is emulating a smaller display (using adb shell wm size).
835 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
837 public void getRealMetrics(DisplayMetrics outMetrics) {
838 synchronized (this) {
839 updateDisplayInfoLocked();
840 mDisplayInfo.getLogicalMetrics(outMetrics,
841 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
846 * Gets the state of the display, such as whether it is on or off.
848 * @return The state of the display: one of {@link #STATE_OFF}, {@link #STATE_ON},
849 * {@link #STATE_DOZE}, {@link #STATE_DOZE_SUSPEND}, or {@link #STATE_UNKNOWN}.
851 public int getState() {
852 synchronized (this) {
853 updateDisplayInfoLocked();
854 return mIsValid ? mDisplayInfo.state : STATE_UNKNOWN;
859 * Returns true if the specified UID has access to this display.
862 public boolean hasAccess(int uid) {
863 return Display.hasAccess(uid, mFlags, mOwnerUid);
867 public static boolean hasAccess(int uid, int flags, int ownerUid) {
868 return (flags & Display.FLAG_PRIVATE) == 0
870 || uid == Process.SYSTEM_UID
875 * Returns true if the display is a public presentation display.
878 public boolean isPublicPresentation() {
879 return (mFlags & (Display.FLAG_PRIVATE | Display.FLAG_PRESENTATION)) ==
880 Display.FLAG_PRESENTATION;
883 private void updateDisplayInfoLocked() {
884 // Note: The display manager caches display info objects on our behalf.
885 DisplayInfo newInfo = mGlobal.getDisplayInfo(mDisplayId);
886 if (newInfo == null) {
887 // Preserve the old mDisplayInfo after the display is removed.
891 Log.d(TAG, "Logical display " + mDisplayId + " was removed.");
895 // Use the new display info. (It might be the same object if nothing changed.)
896 mDisplayInfo = newInfo;
900 Log.d(TAG, "Logical display " + mDisplayId + " was recreated.");
906 private void updateCachedAppSizeIfNeededLocked() {
907 long now = SystemClock.uptimeMillis();
908 if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
909 updateDisplayInfoLocked();
910 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
911 mCachedAppWidthCompat = mTempMetrics.widthPixels;
912 mCachedAppHeightCompat = mTempMetrics.heightPixels;
913 mLastCachedAppSizeUpdate = now;
917 // For debugging purposes
919 public String toString() {
920 synchronized (this) {
921 updateDisplayInfoLocked();
922 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
923 return "Display id " + mDisplayId + ": " + mDisplayInfo
924 + ", " + mTempMetrics + ", isValid=" + mIsValid;
931 public static String typeToString(int type) {
946 return Integer.toString(type);
953 public static String stateToString(int state) {
963 case STATE_DOZE_SUSPEND:
964 return "DOZE_SUSPEND";
966 return Integer.toString(state);
971 * Returns true if display updates may be suspended while in the specified
972 * display power state.
975 public static boolean isSuspendedState(int state) {
976 return state == STATE_OFF || state == STATE_DOZE_SUSPEND;
980 * A mode supported by a given display.
982 * @see Display#getSupportedModes()
984 public static final class Mode implements Parcelable {
988 public static final Mode[] EMPTY_ARRAY = new Mode[0];
990 private final int mModeId;
991 private final int mWidth;
992 private final int mHeight;
993 private final float mRefreshRate;
998 public Mode(int modeId, int width, int height, float refreshRate) {
1002 mRefreshRate = refreshRate;
1006 * Returns this mode's id.
1008 public int getModeId() {
1013 * Returns the physical width of the display in pixels when configured in this mode's
1016 * Note that due to application UI scaling, the number of pixels made available to
1017 * applications when the mode is active (as reported by {@link Display#getWidth()} may
1018 * differ from the mode's actual resolution (as reported by this function).
1020 * For example, applications running on a 4K display may have their UI laid out and rendered
1021 * in 1080p and then scaled up. Applications can take advantage of the extra resolution by
1022 * rendering content through a {@link android.view.SurfaceView} using full size buffers.
1024 public int getPhysicalWidth() {
1029 * Returns the physical height of the display in pixels when configured in this mode's
1032 * Note that due to application UI scaling, the number of pixels made available to
1033 * applications when the mode is active (as reported by {@link Display#getHeight()} may
1034 * differ from the mode's actual resolution (as reported by this function).
1036 * For example, applications running on a 4K display may have their UI laid out and rendered
1037 * in 1080p and then scaled up. Applications can take advantage of the extra resolution by
1038 * rendering content through a {@link android.view.SurfaceView} using full size buffers.
1040 public int getPhysicalHeight() {
1045 * Returns the refresh rate in frames per second.
1047 public float getRefreshRate() {
1048 return mRefreshRate;
1052 * Returns {@code true} if this mode matches the given parameters.
1056 public boolean matches(int width, int height, float refreshRate) {
1057 return mWidth == width &&
1058 mHeight == height &&
1059 Float.floatToIntBits(mRefreshRate) == Float.floatToIntBits(refreshRate);
1063 public boolean equals(Object other) {
1064 if (this == other) {
1067 if (!(other instanceof Mode)) {
1070 Mode that = (Mode) other;
1071 return mModeId == that.mModeId && matches(that.mWidth, that.mHeight, that.mRefreshRate);
1075 public int hashCode() {
1077 hash = hash * 17 + mModeId;
1078 hash = hash * 17 + mWidth;
1079 hash = hash * 17 + mHeight;
1080 hash = hash * 17 + Float.floatToIntBits(mRefreshRate);
1085 public String toString() {
1086 return new StringBuilder("{")
1087 .append("id=").append(mModeId)
1088 .append(", width=").append(mWidth)
1089 .append(", height=").append(mHeight)
1090 .append(", fps=").append(mRefreshRate)
1096 public int describeContents() {
1100 private Mode(Parcel in) {
1101 this(in.readInt(), in.readInt(), in.readInt(), in.readFloat());
1105 public void writeToParcel(Parcel out, int parcelableFlags) {
1106 out.writeInt(mModeId);
1107 out.writeInt(mWidth);
1108 out.writeInt(mHeight);
1109 out.writeFloat(mRefreshRate);
1112 @SuppressWarnings("hiding")
1113 public static final Parcelable.Creator<Mode> CREATOR
1114 = new Parcelable.Creator<Mode>() {
1116 public Mode createFromParcel(Parcel in) {
1117 return new Mode(in);
1121 public Mode[] newArray(int size) {
1122 return new Mode[size];
1128 * Encapsulates the HDR capabilities of a given display.
1129 * For example, what HDR types it supports and details about the desired luminance data.
1130 * <p>You can get an instance for a given {@link Display} object with
1131 * {@link Display#getHdrCapabilities getHdrCapabilities()}.
1133 public static final class HdrCapabilities implements Parcelable {
1135 * Invalid luminance value.
1137 public static final float INVALID_LUMINANCE = -1;
1139 * Dolby Vision high dynamic range (HDR) display.
1141 public static final int HDR_TYPE_DOLBY_VISION = 1;
1145 public static final int HDR_TYPE_HDR10 = 2;
1147 * Hybrid Log-Gamma HDR display.
1149 public static final int HDR_TYPE_HLG = 3;
1153 HDR_TYPE_DOLBY_VISION,
1157 @Retention(RetentionPolicy.SOURCE)
1158 public @interface HdrType {}
1160 private @HdrType int[] mSupportedHdrTypes = new int[0];
1161 private float mMaxLuminance = INVALID_LUMINANCE;
1162 private float mMaxAverageLuminance = INVALID_LUMINANCE;
1163 private float mMinLuminance = INVALID_LUMINANCE;
1168 public HdrCapabilities() {
1174 public HdrCapabilities(int[] supportedHdrTypes, float maxLuminance,
1175 float maxAverageLuminance, float minLuminance) {
1176 mSupportedHdrTypes = supportedHdrTypes;
1177 mMaxLuminance = maxLuminance;
1178 mMaxAverageLuminance = maxAverageLuminance;
1179 mMinLuminance = minLuminance;
1183 * Gets the supported HDR types of this display.
1184 * Returns empty array if HDR is not supported by the display.
1186 public @HdrType int[] getSupportedHdrTypes() {
1187 return mSupportedHdrTypes;
1190 * Returns the desired content max luminance data in cd/m2 for this display.
1192 public float getDesiredMaxLuminance() {
1193 return mMaxLuminance;
1196 * Returns the desired content max frame-average luminance data in cd/m2 for this display.
1198 public float getDesiredMaxAverageLuminance() {
1199 return mMaxAverageLuminance;
1202 * Returns the desired content min luminance data in cd/m2 for this display.
1204 public float getDesiredMinLuminance() {
1205 return mMinLuminance;
1208 public static final Creator<HdrCapabilities> CREATOR = new Creator<HdrCapabilities>() {
1210 public HdrCapabilities createFromParcel(Parcel source) {
1211 return new HdrCapabilities(source);
1215 public HdrCapabilities[] newArray(int size) {
1216 return new HdrCapabilities[size];
1220 private HdrCapabilities(Parcel source) {
1221 readFromParcel(source);
1227 public void readFromParcel(Parcel source) {
1228 int types = source.readInt();
1229 mSupportedHdrTypes = new int[types];
1230 for (int i = 0; i < types; ++i) {
1231 mSupportedHdrTypes[i] = source.readInt();
1233 mMaxLuminance = source.readFloat();
1234 mMaxAverageLuminance = source.readFloat();
1235 mMinLuminance = source.readFloat();
1239 public void writeToParcel(Parcel dest, int flags) {
1240 dest.writeInt(mSupportedHdrTypes.length);
1241 for (int i = 0; i < mSupportedHdrTypes.length; ++i) {
1242 dest.writeInt(mSupportedHdrTypes[i]);
1244 dest.writeFloat(mMaxLuminance);
1245 dest.writeFloat(mMaxAverageLuminance);
1246 dest.writeFloat(mMinLuminance);
1250 public int describeContents() {
1256 * A color transform supported by a given display.
1258 * @see Display#getSupportedColorTransforms()
1261 public static final class ColorTransform implements Parcelable {
1262 public static final ColorTransform[] EMPTY_ARRAY = new ColorTransform[0];
1264 private final int mId;
1265 private final int mColorTransform;
1267 public ColorTransform(int id, int colorTransform) {
1269 mColorTransform = colorTransform;
1272 public int getId() {
1276 public int getColorTransform() {
1277 return mColorTransform;
1281 public boolean equals(Object other) {
1282 if (this == other) {
1285 if (!(other instanceof ColorTransform)) {
1288 ColorTransform that = (ColorTransform) other;
1289 return mId == that.mId
1290 && mColorTransform == that.mColorTransform;
1294 public int hashCode() {
1296 hash = hash * 17 + mId;
1297 hash = hash * 17 + mColorTransform;
1302 public String toString() {
1303 return new StringBuilder("{")
1304 .append("id=").append(mId)
1305 .append(", colorTransform=").append(mColorTransform)
1311 public int describeContents() {
1315 private ColorTransform(Parcel in) {
1316 this(in.readInt(), in.readInt());
1320 public void writeToParcel(Parcel out, int parcelableFlags) {
1322 out.writeInt(mColorTransform);
1325 @SuppressWarnings("hiding")
1326 public static final Parcelable.Creator<ColorTransform> CREATOR
1327 = new Parcelable.Creator<ColorTransform>() {
1329 public ColorTransform createFromParcel(Parcel in) {
1330 return new ColorTransform(in);
1334 public ColorTransform[] newArray(int size) {
1335 return new ColorTransform[size];