blob: ed619d9c60cb379059659b0126a2a0e02c265ad1 [file] [log] [blame]
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001/*
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.graphics.Rect;
Jeff Brownc5df37c2012-09-13 11:45:07 -070020import android.view.Display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070021import android.view.DisplayInfo;
22import android.view.Surface;
23
24import java.io.PrintWriter;
25import java.util.List;
26
27import libcore.util.Objects;
28
29/**
30 * Describes how a logical display is configured.
31 * <p>
32 * At this time, we only support logical displays that are coupled to a particular
33 * primary display device from which the logical display derives its basic properties
34 * such as its size, density and refresh rate.
35 * </p><p>
36 * A logical display may be mirrored onto multiple display devices in addition to its
37 * primary display device. Note that the contents of a logical display may not
38 * always be visible, even on its primary display device, such as in the case where
39 * the primary display device is currently mirroring content from a different
40 * logical display.
41 * </p><p>
42 * This object is designed to encapsulate as much of the policy of logical
43 * displays as possible. The idea is to make it easy to implement new kinds of
44 * logical displays mostly by making local changes to this class.
45 * </p><p>
46 * Note: The display manager architecture does not actually require logical displays
47 * to be associated with any individual display device. Logical displays and
48 * display devices are orthogonal concepts. Some mapping will exist between
49 * logical displays and display devices but it can be many-to-many and
50 * and some might have no relation at all.
51 * </p><p>
52 * Logical displays are guarded by the {@link DisplayManagerService.SyncRoot} lock.
53 * </p>
54 */
55final class LogicalDisplay {
56 private final DisplayInfo mBaseDisplayInfo = new DisplayInfo();
57
Jeff Brown8ec09432012-10-16 16:19:23 -070058 // The layer stack we use when the display has been blanked to prevent any
59 // of its content from appearing.
60 private static final int BLANK_LAYER_STACK = -1;
61
Jeff Brownd728bf52012-09-08 18:05:28 -070062 private final int mDisplayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070063 private final int mLayerStack;
64 private DisplayInfo mOverrideDisplayInfo; // set by the window manager
65 private DisplayInfo mInfo;
66
67 // The display device that this logical display is based on and which
68 // determines the base metrics that it uses.
69 private DisplayDevice mPrimaryDisplayDevice;
70 private DisplayDeviceInfo mPrimaryDisplayDeviceInfo;
71
Craig Mautner722285e2012-09-07 13:55:58 -070072 // True if the logical display has unique content.
73 private boolean mHasContent;
74
Jeff Brown4ed8fe72012-08-30 18:18:29 -070075 // Temporary rectangle used when needed.
Mathias Agopian63f1c432012-09-04 19:29:13 -070076 private final Rect mTempLayerStackRect = new Rect();
77 private final Rect mTempDisplayRect = new Rect();
Jeff Brown4ed8fe72012-08-30 18:18:29 -070078
Jeff Brownd728bf52012-09-08 18:05:28 -070079 public LogicalDisplay(int displayId, int layerStack, DisplayDevice primaryDisplayDevice) {
80 mDisplayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070081 mLayerStack = layerStack;
82 mPrimaryDisplayDevice = primaryDisplayDevice;
83 }
84
85 /**
Jeff Brownd728bf52012-09-08 18:05:28 -070086 * Gets the logical display id of this logical display.
87 *
88 * @return The logical display id.
89 */
90 public int getDisplayIdLocked() {
91 return mDisplayId;
92 }
93
94 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -070095 * Gets the primary display device associated with this logical display.
96 *
97 * @return The primary display device.
98 */
99 public DisplayDevice getPrimaryDisplayDeviceLocked() {
100 return mPrimaryDisplayDevice;
101 }
102
103 /**
104 * Gets information about the logical display.
105 *
106 * @return The device info, which should be treated as immutable by the caller.
107 * The logical display should allocate a new display info object whenever
108 * the data changes.
109 */
110 public DisplayInfo getDisplayInfoLocked() {
111 if (mInfo == null) {
112 mInfo = new DisplayInfo();
113 if (mOverrideDisplayInfo != null) {
114 mInfo.copyFrom(mOverrideDisplayInfo);
115 mInfo.layerStack = mBaseDisplayInfo.layerStack;
116 mInfo.name = mBaseDisplayInfo.name;
Jeff Brown037c33e2014-04-09 00:31:55 -0700117 mInfo.state = mBaseDisplayInfo.state;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700118 } else {
119 mInfo.copyFrom(mBaseDisplayInfo);
120 }
121 }
122 return mInfo;
123 }
124
125 /**
126 * Sets overridden logical display information from the window manager.
127 * This method can be used to adjust application insets, rotation, and other
128 * properties that the window manager takes care of.
129 *
130 * @param info The logical display information, may be null.
131 */
Jeff Brownef981a42013-08-07 14:13:37 -0700132 public boolean setDisplayInfoOverrideFromWindowManagerLocked(DisplayInfo info) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700133 if (info != null) {
134 if (mOverrideDisplayInfo == null) {
135 mOverrideDisplayInfo = new DisplayInfo(info);
136 mInfo = null;
Jeff Brownef981a42013-08-07 14:13:37 -0700137 return true;
138 }
139 if (!mOverrideDisplayInfo.equals(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700140 mOverrideDisplayInfo.copyFrom(info);
141 mInfo = null;
Jeff Brownef981a42013-08-07 14:13:37 -0700142 return true;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 }
144 } else if (mOverrideDisplayInfo != null) {
145 mOverrideDisplayInfo = null;
146 mInfo = null;
Jeff Brownef981a42013-08-07 14:13:37 -0700147 return true;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700148 }
Jeff Brownef981a42013-08-07 14:13:37 -0700149 return false;
Dianne Hackbornc652de82013-02-15 16:32:56 -0800150 }
151
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700152 /**
153 * Returns true if the logical display is in a valid state.
Craig Mautner722285e2012-09-07 13:55:58 -0700154 * This method should be checked after calling {@link #updateLocked} to handle the
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700155 * case where a logical display should be removed because all of its associated
156 * display devices are gone or if it is otherwise no longer needed.
157 *
158 * @return True if the logical display is still valid.
159 */
160 public boolean isValidLocked() {
161 return mPrimaryDisplayDevice != null;
162 }
163
164 /**
165 * Updates the state of the logical display based on the available display devices.
166 * The logical display might become invalid if it is attached to a display device
167 * that no longer exists.
168 *
169 * @param devices The list of all connected display devices.
170 */
171 public void updateLocked(List<DisplayDevice> devices) {
172 // Nothing to update if already invalid.
173 if (mPrimaryDisplayDevice == null) {
174 return;
175 }
176
177 // Check whether logical display has become invalid.
178 if (!devices.contains(mPrimaryDisplayDevice)) {
179 mPrimaryDisplayDevice = null;
180 return;
181 }
182
183 // Bootstrap the logical display using its associated primary physical display.
184 // We might use more elaborate configurations later. It's possible that the
185 // configuration of several physical displays might be used to determine the
186 // logical display that they are sharing. (eg. Adjust size for pixel-perfect
187 // mirroring over HDMI.)
188 DisplayDeviceInfo deviceInfo = mPrimaryDisplayDevice.getDisplayDeviceInfoLocked();
189 if (!Objects.equal(mPrimaryDisplayDeviceInfo, deviceInfo)) {
190 mBaseDisplayInfo.layerStack = mLayerStack;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700191 mBaseDisplayInfo.flags = 0;
Jeff Brown77aebfd2012-10-01 21:07:03 -0700192 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS) != 0) {
193 mBaseDisplayInfo.flags |= Display.FLAG_SUPPORTS_PROTECTED_BUFFERS;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700194 }
Jeff Brownf0681b32012-10-23 17:35:57 -0700195 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_SECURE) != 0) {
196 mBaseDisplayInfo.flags |= Display.FLAG_SECURE;
197 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700198 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_PRIVATE) != 0) {
199 mBaseDisplayInfo.flags |= Display.FLAG_PRIVATE;
200 }
Jeff Brown7d00aff2013-08-02 19:03:49 -0700201 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_PRESENTATION) != 0) {
202 mBaseDisplayInfo.flags |= Display.FLAG_PRESENTATION;
203 }
Jeff Brown92130f62012-10-24 21:28:33 -0700204 mBaseDisplayInfo.type = deviceInfo.type;
205 mBaseDisplayInfo.address = deviceInfo.address;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700206 mBaseDisplayInfo.name = deviceInfo.name;
207 mBaseDisplayInfo.appWidth = deviceInfo.width;
208 mBaseDisplayInfo.appHeight = deviceInfo.height;
209 mBaseDisplayInfo.logicalWidth = deviceInfo.width;
210 mBaseDisplayInfo.logicalHeight = deviceInfo.height;
211 mBaseDisplayInfo.rotation = Surface.ROTATION_0;
212 mBaseDisplayInfo.refreshRate = deviceInfo.refreshRate;
213 mBaseDisplayInfo.logicalDensityDpi = deviceInfo.densityDpi;
214 mBaseDisplayInfo.physicalXDpi = deviceInfo.xDpi;
215 mBaseDisplayInfo.physicalYDpi = deviceInfo.yDpi;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700216 mBaseDisplayInfo.appVsyncOffsetNanos = deviceInfo.appVsyncOffsetNanos;
217 mBaseDisplayInfo.presentationDeadlineNanos = deviceInfo.presentationDeadlineNanos;
Jeff Brown037c33e2014-04-09 00:31:55 -0700218 mBaseDisplayInfo.state = deviceInfo.state;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700219 mBaseDisplayInfo.smallestNominalAppWidth = deviceInfo.width;
220 mBaseDisplayInfo.smallestNominalAppHeight = deviceInfo.height;
221 mBaseDisplayInfo.largestNominalAppWidth = deviceInfo.width;
222 mBaseDisplayInfo.largestNominalAppHeight = deviceInfo.height;
Jeff Browna506a6e2013-06-04 00:02:38 -0700223 mBaseDisplayInfo.ownerUid = deviceInfo.ownerUid;
224 mBaseDisplayInfo.ownerPackageName = deviceInfo.ownerPackageName;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700225
226 mPrimaryDisplayDeviceInfo = deviceInfo;
227 mInfo = null;
228 }
229 }
230
231 /**
232 * Applies the layer stack and transformation to the given display device
233 * so that it shows the contents of this logical display.
234 *
235 * We know that the given display device is only ever showing the contents of
236 * a single logical display, so this method is expected to blow away all of its
237 * transformation properties to make it happen regardless of what the
238 * display device was previously showing.
239 *
240 * The caller must have an open Surface transaction.
241 *
242 * The display device may not be the primary display device, in the case
243 * where the display is being mirrored.
244 *
245 * @param device The display device to modify.
Jeff Brown8ec09432012-10-16 16:19:23 -0700246 * @param isBlanked True if the device is being blanked.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700247 */
Jeff Brown8ec09432012-10-16 16:19:23 -0700248 public void configureDisplayInTransactionLocked(DisplayDevice device,
249 boolean isBlanked) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700250 final DisplayInfo displayInfo = getDisplayInfoLocked();
251 final DisplayDeviceInfo displayDeviceInfo = device.getDisplayDeviceInfoLocked();
252
253 // Set the layer stack.
Jeff Brown8ec09432012-10-16 16:19:23 -0700254 device.setLayerStackInTransactionLocked(isBlanked ? BLANK_LAYER_STACK : mLayerStack);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700255
256 // Set the viewport.
257 // This is the area of the logical display that we intend to show on the
258 // display device. For now, it is always the full size of the logical display.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700259 mTempLayerStackRect.set(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700260
261 // Set the orientation.
262 // The orientation specifies how the physical coordinate system of the display
263 // is rotated when the contents of the logical display are rendered.
264 int orientation = Surface.ROTATION_0;
Scott Anderson8786ed92013-11-01 13:27:39 -0700265 if ((displayDeviceInfo.flags & DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT) != 0) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700266 orientation = displayInfo.rotation;
267 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700268
Jeff Brown27f1d672012-10-17 18:32:34 -0700269 // Apply the physical rotation of the display device itself.
270 orientation = (orientation + displayDeviceInfo.rotation) % 4;
271
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700272 // Set the frame.
273 // The frame specifies the rotated physical coordinates into which the viewport
274 // is mapped. We need to take care to preserve the aspect ratio of the viewport.
275 // Currently we maximize the area to fill the display, but we could try to be
276 // more clever and match resolutions.
277 boolean rotated = (orientation == Surface.ROTATION_90
278 || orientation == Surface.ROTATION_270);
279 int physWidth = rotated ? displayDeviceInfo.height : displayDeviceInfo.width;
280 int physHeight = rotated ? displayDeviceInfo.width : displayDeviceInfo.height;
281
282 // Determine whether the width or height is more constrained to be scaled.
283 // physWidth / displayInfo.logicalWidth => letter box
284 // or physHeight / displayInfo.logicalHeight => pillar box
285 //
286 // We avoid a division (and possible floating point imprecision) here by
287 // multiplying the fractions by the product of their denominators before
288 // comparing them.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700289 int displayRectWidth, displayRectHeight;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700290 if (physWidth * displayInfo.logicalHeight
291 < physHeight * displayInfo.logicalWidth) {
292 // Letter box.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700293 displayRectWidth = physWidth;
294 displayRectHeight = displayInfo.logicalHeight * physWidth / displayInfo.logicalWidth;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700295 } else {
296 // Pillar box.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700297 displayRectWidth = displayInfo.logicalWidth * physHeight / displayInfo.logicalHeight;
298 displayRectHeight = physHeight;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700299 }
Mathias Agopian63f1c432012-09-04 19:29:13 -0700300 int displayRectTop = (physHeight - displayRectHeight) / 2;
301 int displayRectLeft = (physWidth - displayRectWidth) / 2;
302 mTempDisplayRect.set(displayRectLeft, displayRectTop,
303 displayRectLeft + displayRectWidth, displayRectTop + displayRectHeight);
304
305 device.setProjectionInTransactionLocked(orientation, mTempLayerStackRect, mTempDisplayRect);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700306 }
307
Craig Mautner722285e2012-09-07 13:55:58 -0700308 /**
309 * Returns true if the logical display has unique content.
310 * <p>
311 * If the display has unique content then we will try to ensure that it is
312 * visible on at least its primary display device. Otherwise we will ignore the
313 * logical display and perhaps show mirrored content on the primary display device.
314 * </p>
315 *
316 * @return True if the display has unique content.
317 */
318 public boolean hasContentLocked() {
319 return mHasContent;
320 }
321
322 /**
323 * Sets whether the logical display has unique content.
324 *
325 * @param hasContent True if the display has unique content.
326 */
327 public void setHasContentLocked(boolean hasContent) {
328 mHasContent = hasContent;
329 }
330
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700331 public void dumpLocked(PrintWriter pw) {
Jeff Brownc2726642012-10-02 16:17:31 -0700332 pw.println("mDisplayId=" + mDisplayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700333 pw.println("mLayerStack=" + mLayerStack);
Jeff Brownc2726642012-10-02 16:17:31 -0700334 pw.println("mHasContent=" + mHasContent);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700335 pw.println("mPrimaryDisplayDevice=" + (mPrimaryDisplayDevice != null ?
336 mPrimaryDisplayDevice.getNameLocked() : "null"));
337 pw.println("mBaseDisplayInfo=" + mBaseDisplayInfo);
338 pw.println("mOverrideDisplayInfo=" + mOverrideDisplayInfo);
339 }
340}