DisplayDeviceInfo.java revision 4ccb823a9f62e57f9d221f83a97e82967e79a9e5
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.hardware.display.DisplayViewport;
20import android.util.DisplayMetrics;
21import android.view.Display;
22import android.view.Surface;
23
24import libcore.util.Objects;
25
26/**
27 * Describes the characteristics of a physical display device.
28 */
29final class DisplayDeviceInfo {
30    /**
31     * Flag: Indicates that this display device should be considered the default display
32     * device of the system.
33     */
34    public static final int FLAG_DEFAULT_DISPLAY = 1 << 0;
35
36    /**
37     * Flag: Indicates that the orientation of this display device is coupled to the
38     * rotation of its associated logical display.
39     * <p>
40     * This flag should be applied to the default display to indicate that the user
41     * physically rotates the display when content is presented in a different orientation.
42     * The display manager will apply a coordinate transformation assuming that the
43     * physical orientation of the display matches the logical orientation of its content.
44     * </p><p>
45     * The flag should not be set when the display device is mounted in a fixed orientation
46     * such as on a desk.  The display manager will apply a coordinate transformation
47     * such as a scale and translation to letterbox or pillarbox format under the
48     * assumption that the physical orientation of the display is invariant.
49     * </p>
50     */
51    public static final int FLAG_ROTATES_WITH_CONTENT = 1 << 1;
52
53    /**
54     * Flag: Indicates that this display device has secure video output, such as HDCP.
55     */
56    public static final int FLAG_SECURE = 1 << 2;
57
58    /**
59     * Flag: Indicates that this display device supports compositing
60     * from gralloc protected buffers.
61     */
62    public static final int FLAG_SUPPORTS_PROTECTED_BUFFERS = 1 << 3;
63
64    /**
65     * Flag: Indicates that the display device is owned by a particular application
66     * and that no other application should be able to interact with it.
67     * Should typically be used together with {@link #FLAG_OWN_CONTENT_ONLY}.
68     */
69    public static final int FLAG_PRIVATE = 1 << 4;
70
71    /**
72     * Flag: Indicates that the display device is not blanked automatically by
73     * the power manager.
74     */
75    public static final int FLAG_NEVER_BLANK = 1 << 5;
76
77    /**
78     * Flag: Indicates that the display is suitable for presentations.
79     */
80    public static final int FLAG_PRESENTATION = 1 << 6;
81
82    /**
83     * Flag: Only show this display's own content; do not mirror
84     * the content of another display.
85     */
86    public static final int FLAG_OWN_CONTENT_ONLY = 1 << 7;
87
88    /**
89     * Touch attachment: Display does not receive touch.
90     */
91    public static final int TOUCH_NONE = 0;
92
93    /**
94     * Touch attachment: Touch input is via the internal interface.
95     */
96    public static final int TOUCH_INTERNAL = 1;
97
98    /**
99     * Touch attachment: Touch input is via an external interface, such as USB.
100     */
101    public static final int TOUCH_EXTERNAL = 2;
102
103    /**
104     * Gets the name of the display device, which may be derived from
105     * EDID or other sources.  The name may be displayed to the user.
106     */
107    public String name;
108
109    /**
110     * The width of the display in its natural orientation, in pixels.
111     * This value is not affected by display rotation.
112     */
113    public int width;
114
115    /**
116     * The height of the display in its natural orientation, in pixels.
117     * This value is not affected by display rotation.
118     */
119    public int height;
120
121    /**
122     * The refresh rate of the display.
123     */
124    public float refreshRate;
125
126    /**
127     * The nominal apparent density of the display in DPI used for layout calculations.
128     * This density is sensitive to the viewing distance.  A big TV and a tablet may have
129     * the same apparent density even though the pixels on the TV are much bigger than
130     * those on the tablet.
131     */
132    public int densityDpi;
133
134    /**
135     * The physical density of the display in DPI in the X direction.
136     * This density should specify the physical size of each pixel.
137     */
138    public float xDpi;
139
140    /**
141     * The physical density of the display in DPI in the X direction.
142     * This density should specify the physical size of each pixel.
143     */
144    public float yDpi;
145
146    /**
147     * Display flags.
148     */
149    public int flags;
150
151    /**
152     * The touch attachment, per {@link DisplayViewport#touch}.
153     */
154    public int touch;
155
156    /**
157     * The additional rotation to apply to all content presented on the display device
158     * relative to its physical coordinate system.  Default is {@link Surface#ROTATION_0}.
159     * <p>
160     * This field can be used to compensate for the fact that the display has been
161     * physically rotated relative to its natural orientation such as an HDMI monitor
162     * that has been mounted sideways to appear to be portrait rather than landscape.
163     * </p>
164     */
165    public int rotation = Surface.ROTATION_0;
166
167    /**
168     * Display type.
169     */
170    public int type;
171
172    /**
173     * Display address, or null if none.
174     * Interpretation varies by display type.
175     */
176    public String address;
177
178    /**
179     * The UID of the application that owns this display, or zero if it is owned by the system.
180     * <p>
181     * If the display is private, then only the owner can use it.
182     * </p>
183     */
184    public int ownerUid;
185
186    /**
187     * The package name of the application that owns this display, or null if it is
188     * owned by the system.
189     * <p>
190     * If the display is private, then only the owner can use it.
191     * </p>
192     */
193    public String ownerPackageName;
194
195    public void setAssumedDensityForExternalDisplay(int width, int height) {
196        densityDpi = Math.min(width, height) * DisplayMetrics.DENSITY_XHIGH / 1080;
197        // Technically, these values should be smaller than the apparent density
198        // but we don't know the physical size of the display.
199        xDpi = densityDpi;
200        yDpi = densityDpi;
201    }
202
203    @Override
204    public boolean equals(Object o) {
205        return o instanceof DisplayDeviceInfo && equals((DisplayDeviceInfo)o);
206    }
207
208    public boolean equals(DisplayDeviceInfo other) {
209        return other != null
210                && Objects.equal(name, other.name)
211                && width == other.width
212                && height == other.height
213                && refreshRate == other.refreshRate
214                && densityDpi == other.densityDpi
215                && xDpi == other.xDpi
216                && yDpi == other.yDpi
217                && flags == other.flags
218                && touch == other.touch
219                && rotation == other.rotation
220                && type == other.type
221                && Objects.equal(address, other.address)
222                && ownerUid == other.ownerUid
223                && Objects.equal(ownerPackageName, other.ownerPackageName);
224    }
225
226    @Override
227    public int hashCode() {
228        return 0; // don't care
229    }
230
231    public void copyFrom(DisplayDeviceInfo other) {
232        name = other.name;
233        width = other.width;
234        height = other.height;
235        refreshRate = other.refreshRate;
236        densityDpi = other.densityDpi;
237        xDpi = other.xDpi;
238        yDpi = other.yDpi;
239        flags = other.flags;
240        touch = other.touch;
241        rotation = other.rotation;
242        type = other.type;
243        address = other.address;
244        ownerUid = other.ownerUid;
245        ownerPackageName = other.ownerPackageName;
246    }
247
248    // For debugging purposes
249    @Override
250    public String toString() {
251        StringBuilder sb = new StringBuilder();
252        sb.append("DisplayDeviceInfo{\"");
253        sb.append(name).append("\": ").append(width).append(" x ").append(height);
254        sb.append(", ").append(refreshRate).append(" fps, ");
255        sb.append("density ").append(densityDpi);
256        sb.append(", ").append(xDpi).append(" x ").append(yDpi).append(" dpi");
257        sb.append(", touch ").append(touchToString(touch));
258        sb.append(", rotation ").append(rotation);
259        sb.append(", type ").append(Display.typeToString(type));
260        if (address != null) {
261            sb.append(", address ").append(address);
262        }
263        if (ownerUid != 0 || ownerPackageName != null) {
264            sb.append(", owner ").append(ownerPackageName);
265            sb.append(" (uid ").append(ownerUid).append(")");
266        }
267        sb.append(flagsToString(flags));
268        sb.append("}");
269        return sb.toString();
270    }
271
272    private static String touchToString(int touch) {
273        switch (touch) {
274            case TOUCH_NONE:
275                return "NONE";
276            case TOUCH_INTERNAL:
277                return "INTERNAL";
278            case TOUCH_EXTERNAL:
279                return "EXTERNAL";
280            default:
281                return Integer.toString(touch);
282        }
283    }
284
285    private static String flagsToString(int flags) {
286        StringBuilder msg = new StringBuilder();
287        if ((flags & FLAG_DEFAULT_DISPLAY) != 0) {
288            msg.append(", FLAG_DEFAULT_DISPLAY");
289        }
290        if ((flags & FLAG_ROTATES_WITH_CONTENT) != 0) {
291            msg.append(", FLAG_ROTATES_WITH_CONTENT");
292        }
293        if ((flags & FLAG_SECURE) != 0) {
294            msg.append(", FLAG_SECURE");
295        }
296        if ((flags & FLAG_SUPPORTS_PROTECTED_BUFFERS) != 0) {
297            msg.append(", FLAG_SUPPORTS_PROTECTED_BUFFERS");
298        }
299        if ((flags & FLAG_PRIVATE) != 0) {
300            msg.append(", FLAG_PRIVATE");
301        }
302        if ((flags & FLAG_NEVER_BLANK) != 0) {
303            msg.append(", FLAG_NEVER_BLANK");
304        }
305        if ((flags & FLAG_PRESENTATION) != 0) {
306            msg.append(", FLAG_PRESENTATION");
307        }
308        if ((flags & FLAG_OWN_CONTENT_ONLY) != 0) {
309            msg.append(", FLAG_OWN_CONTENT_ONLY");
310        }
311        return msg.toString();
312    }
313}
314