DisplayDeviceInfo.java revision 27f1d674bf9fb53af7facdcb746912e036d5bf75
1/*
2 * Copyright (C) 2012 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
17package com.android.server.display;
18
19import android.util.DisplayMetrics;
20import android.view.Surface;
21
22import libcore.util.Objects;
23
24/**
25 * Describes the characteristics of a physical display device.
26 */
27final class DisplayDeviceInfo {
28    /**
29     * Flag: Indicates that this display device should be considered the default display
30     * device of the system.
31     */
32    public static final int FLAG_DEFAULT_DISPLAY = 1 << 0;
33
34    /**
35     * Flag: Indicates that the orientation of this display device is coupled to the
36     * rotation of its associated logical display.
37     * <p>
38     * This flag should be applied to the default display to indicate that the user
39     * physically rotates the display when content is presented in a different orientation.
40     * The display manager will apply a coordinate transformation assuming that the
41     * physical orientation of the display matches the logical orientation of its content.
42     * </p><p>
43     * The flag should not be set when the display device is mounted in a fixed orientation
44     * such as on a desk.  The display manager will apply a coordinate transformation
45     * such as a scale and translation to letterbox or pillarbox format under the
46     * assumption that the physical orientation of the display is invariant.
47     * </p>
48     */
49    public static final int FLAG_ROTATES_WITH_CONTENT = 1 << 1;
50
51    /**
52     * Flag: Indicates that this display device has secure video output, such as HDCP.
53     */
54    public static final int FLAG_SECURE = 1 << 2;
55
56    /**
57     * Flag: Indicates that this display device supports compositing
58     * from gralloc protected buffers.
59     */
60    public static final int FLAG_SUPPORTS_PROTECTED_BUFFERS = 1 << 3;
61
62    /**
63     * Touch attachment: Display does not receive touch.
64     */
65    public static final int TOUCH_NONE = 0;
66
67    /**
68     * Touch attachment: Touch input is via the internal interface.
69     */
70    public static final int TOUCH_INTERNAL = 1;
71
72    /**
73     * Touch attachment: Touch input is via an external interface, such as USB.
74     */
75    public static final int TOUCH_EXTERNAL = 2;
76
77    /**
78     * Gets the name of the display device, which may be derived from
79     * EDID or other sources.  The name may be displayed to the user.
80     */
81    public String name;
82
83    /**
84     * The width of the display in its natural orientation, in pixels.
85     * This value is not affected by display rotation.
86     */
87    public int width;
88
89    /**
90     * The height of the display in its natural orientation, in pixels.
91     * This value is not affected by display rotation.
92     */
93    public int height;
94
95    /**
96     * The refresh rate of the display.
97     */
98    public float refreshRate;
99
100    /**
101     * The nominal apparent density of the display in DPI used for layout calculations.
102     * This density is sensitive to the viewing distance.  A big TV and a tablet may have
103     * the same apparent density even though the pixels on the TV are much bigger than
104     * those on the tablet.
105     */
106    public int densityDpi;
107
108    /**
109     * The physical density of the display in DPI in the X direction.
110     * This density should specify the physical size of each pixel.
111     */
112    public float xDpi;
113
114    /**
115     * The physical density of the display in DPI in the X direction.
116     * This density should specify the physical size of each pixel.
117     */
118    public float yDpi;
119
120    /**
121     * Display flags.
122     */
123    public int flags;
124
125    /**
126     * The touch attachment, per {@link DisplayViewport#touch}.
127     */
128    public int touch;
129
130    /**
131     * The additional rotation to apply to all content presented on the display device
132     * relative to its physical coordinate system.  Default is {@link Surface#ROTATION_0}.
133     * <p>
134     * This field can be used to compensate for the fact that the display has been
135     * physically rotated relative to its natural orientation such as an HDMI monitor
136     * that has been mounted sideways to appear to be portrait rather than landscape.
137     * </p>
138     */
139    public int rotation = Surface.ROTATION_0;
140
141    public void setAssumedDensityForExternalDisplay(int width, int height) {
142        densityDpi = Math.min(width, height) * DisplayMetrics.DENSITY_XHIGH / 1080;
143        // Technically, these values should be smaller than the apparent density
144        // but we don't know the physical size of the display.
145        xDpi = densityDpi;
146        yDpi = densityDpi;
147    }
148
149    @Override
150    public boolean equals(Object o) {
151        return o instanceof DisplayDeviceInfo && equals((DisplayDeviceInfo)o);
152    }
153
154    public boolean equals(DisplayDeviceInfo other) {
155        return other != null
156                && Objects.equal(name, other.name)
157                && width == other.width
158                && height == other.height
159                && refreshRate == other.refreshRate
160                && densityDpi == other.densityDpi
161                && xDpi == other.xDpi
162                && yDpi == other.yDpi
163                && flags == other.flags
164                && touch == other.touch
165                && rotation == other.rotation;
166    }
167
168    @Override
169    public int hashCode() {
170        return 0; // don't care
171    }
172
173    public void copyFrom(DisplayDeviceInfo other) {
174        name = other.name;
175        width = other.width;
176        height = other.height;
177        refreshRate = other.refreshRate;
178        densityDpi = other.densityDpi;
179        xDpi = other.xDpi;
180        yDpi = other.yDpi;
181        flags = other.flags;
182        touch = other.touch;
183        rotation = other.rotation;
184    }
185
186    // For debugging purposes
187    @Override
188    public String toString() {
189        return "DisplayDeviceInfo{\"" + name + "\": " + width + " x " + height + ", "
190                + refreshRate + " fps, "
191                + "density " + densityDpi + ", " + xDpi + " x " + yDpi + " dpi"
192                + ", touch " + touchToString(touch) + flagsToString(flags)
193                + ", rotation " + rotation
194                + "}";
195    }
196
197    private static String touchToString(int touch) {
198        switch (touch) {
199            case TOUCH_NONE:
200                return "NONE";
201            case TOUCH_INTERNAL:
202                return "INTERNAL";
203            case TOUCH_EXTERNAL:
204                return "EXTERNAL";
205            default:
206                return Integer.toString(touch);
207        }
208    }
209
210    private static String flagsToString(int flags) {
211        StringBuilder msg = new StringBuilder();
212        if ((flags & FLAG_DEFAULT_DISPLAY) != 0) {
213            msg.append(", FLAG_DEFAULT_DISPLAY");
214        }
215        if ((flags & FLAG_ROTATES_WITH_CONTENT) != 0) {
216            msg.append(", FLAG_ROTATES_WITH_CONTENT");
217        }
218        if ((flags & FLAG_SECURE) != 0) {
219            msg.append(", FLAG_SECURE");
220        }
221        if ((flags & FLAG_SUPPORTS_PROTECTED_BUFFERS) != 0) {
222            msg.append(", FLAG_SUPPORTS_PROTECTED_BUFFERS");
223        }
224        return msg.toString();
225    }
226}
227