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 
17 package com.android.server.display;
18 
19 import android.graphics.Rect;
20 import android.view.Display;
21 import android.view.DisplayInfo;
22 import android.view.Surface;
23 
24 import java.io.PrintWriter;
25 import java.util.Arrays;
26 import java.util.List;
27 
28 import libcore.util.Objects;
29 
30 /**
31  * Describes how a logical display is configured.
32  * <p>
33  * At this time, we only support logical displays that are coupled to a particular
34  * primary display device from which the logical display derives its basic properties
35  * such as its size, density and refresh rate.
36  * </p><p>
37  * A logical display may be mirrored onto multiple display devices in addition to its
38  * primary display device.  Note that the contents of a logical display may not
39  * always be visible, even on its primary display device, such as in the case where
40  * the primary display device is currently mirroring content from a different
41  * logical display.
42  * </p><p>
43  * This object is designed to encapsulate as much of the policy of logical
44  * displays as possible.  The idea is to make it easy to implement new kinds of
45  * logical displays mostly by making local changes to this class.
46  * </p><p>
47  * Note: The display manager architecture does not actually require logical displays
48  * to be associated with any individual display device.  Logical displays and
49  * display devices are orthogonal concepts.  Some mapping will exist between
50  * logical displays and display devices but it can be many-to-many and
51  * and some might have no relation at all.
52  * </p><p>
53  * Logical displays are guarded by the {@link DisplayManagerService.SyncRoot} lock.
54  * </p>
55  */
56 final class LogicalDisplay {
57     private final DisplayInfo mBaseDisplayInfo = new DisplayInfo();
58 
59     // The layer stack we use when the display has been blanked to prevent any
60     // of its content from appearing.
61     private static final int BLANK_LAYER_STACK = -1;
62 
63     private final int mDisplayId;
64     private final int mLayerStack;
65     private DisplayInfo mOverrideDisplayInfo; // set by the window manager
66     private DisplayInfo mInfo;
67 
68     // The display device that this logical display is based on and which
69     // determines the base metrics that it uses.
70     private DisplayDevice mPrimaryDisplayDevice;
71     private DisplayDeviceInfo mPrimaryDisplayDeviceInfo;
72 
73     // True if the logical display has unique content.
74     private boolean mHasContent;
75 
76     private int mRequestedModeId;
77     private int mRequestedColorTransformId;
78 
79     // The display offsets to apply to the display projection.
80     private int mDisplayOffsetX;
81     private int mDisplayOffsetY;
82 
83     // Temporary rectangle used when needed.
84     private final Rect mTempLayerStackRect = new Rect();
85     private final Rect mTempDisplayRect = new Rect();
86 
LogicalDisplay(int displayId, int layerStack, DisplayDevice primaryDisplayDevice)87     public LogicalDisplay(int displayId, int layerStack, DisplayDevice primaryDisplayDevice) {
88         mDisplayId = displayId;
89         mLayerStack = layerStack;
90         mPrimaryDisplayDevice = primaryDisplayDevice;
91     }
92 
93     /**
94      * Gets the logical display id of this logical display.
95      *
96      * @return The logical display id.
97      */
getDisplayIdLocked()98     public int getDisplayIdLocked() {
99         return mDisplayId;
100     }
101 
102     /**
103      * Gets the primary display device associated with this logical display.
104      *
105      * @return The primary display device.
106      */
getPrimaryDisplayDeviceLocked()107     public DisplayDevice getPrimaryDisplayDeviceLocked() {
108         return mPrimaryDisplayDevice;
109     }
110 
111     /**
112      * Gets information about the logical display.
113      *
114      * @return The device info, which should be treated as immutable by the caller.
115      * The logical display should allocate a new display info object whenever
116      * the data changes.
117      */
getDisplayInfoLocked()118     public DisplayInfo getDisplayInfoLocked() {
119         if (mInfo == null) {
120             mInfo = new DisplayInfo();
121             mInfo.copyFrom(mBaseDisplayInfo);
122             if (mOverrideDisplayInfo != null) {
123                 mInfo.appWidth = mOverrideDisplayInfo.appWidth;
124                 mInfo.appHeight = mOverrideDisplayInfo.appHeight;
125                 mInfo.smallestNominalAppWidth = mOverrideDisplayInfo.smallestNominalAppWidth;
126                 mInfo.smallestNominalAppHeight = mOverrideDisplayInfo.smallestNominalAppHeight;
127                 mInfo.largestNominalAppWidth = mOverrideDisplayInfo.largestNominalAppWidth;
128                 mInfo.largestNominalAppHeight = mOverrideDisplayInfo.largestNominalAppHeight;
129                 mInfo.logicalWidth = mOverrideDisplayInfo.logicalWidth;
130                 mInfo.logicalHeight = mOverrideDisplayInfo.logicalHeight;
131                 mInfo.overscanLeft = mOverrideDisplayInfo.overscanLeft;
132                 mInfo.overscanTop = mOverrideDisplayInfo.overscanTop;
133                 mInfo.overscanRight = mOverrideDisplayInfo.overscanRight;
134                 mInfo.overscanBottom = mOverrideDisplayInfo.overscanBottom;
135                 mInfo.rotation = mOverrideDisplayInfo.rotation;
136                 mInfo.logicalDensityDpi = mOverrideDisplayInfo.logicalDensityDpi;
137                 mInfo.physicalXDpi = mOverrideDisplayInfo.physicalXDpi;
138                 mInfo.physicalYDpi = mOverrideDisplayInfo.physicalYDpi;
139             }
140         }
141         return mInfo;
142     }
143 
144     /**
145      * Sets overridden logical display information from the window manager.
146      * This method can be used to adjust application insets, rotation, and other
147      * properties that the window manager takes care of.
148      *
149      * @param info The logical display information, may be null.
150      */
setDisplayInfoOverrideFromWindowManagerLocked(DisplayInfo info)151     public boolean setDisplayInfoOverrideFromWindowManagerLocked(DisplayInfo info) {
152         if (info != null) {
153             if (mOverrideDisplayInfo == null) {
154                 mOverrideDisplayInfo = new DisplayInfo(info);
155                 mInfo = null;
156                 return true;
157             }
158             if (!mOverrideDisplayInfo.equals(info)) {
159                 mOverrideDisplayInfo.copyFrom(info);
160                 mInfo = null;
161                 return true;
162             }
163         } else if (mOverrideDisplayInfo != null) {
164             mOverrideDisplayInfo = null;
165             mInfo = null;
166             return true;
167         }
168         return false;
169     }
170 
171     /**
172      * Returns true if the logical display is in a valid state.
173      * This method should be checked after calling {@link #updateLocked} to handle the
174      * case where a logical display should be removed because all of its associated
175      * display devices are gone or if it is otherwise no longer needed.
176      *
177      * @return True if the logical display is still valid.
178      */
isValidLocked()179     public boolean isValidLocked() {
180         return mPrimaryDisplayDevice != null;
181     }
182 
183     /**
184      * Updates the state of the logical display based on the available display devices.
185      * The logical display might become invalid if it is attached to a display device
186      * that no longer exists.
187      *
188      * @param devices The list of all connected display devices.
189      */
updateLocked(List<DisplayDevice> devices)190     public void updateLocked(List<DisplayDevice> devices) {
191         // Nothing to update if already invalid.
192         if (mPrimaryDisplayDevice == null) {
193             return;
194         }
195 
196         // Check whether logical display has become invalid.
197         if (!devices.contains(mPrimaryDisplayDevice)) {
198             mPrimaryDisplayDevice = null;
199             return;
200         }
201 
202         // Bootstrap the logical display using its associated primary physical display.
203         // We might use more elaborate configurations later.  It's possible that the
204         // configuration of several physical displays might be used to determine the
205         // logical display that they are sharing.  (eg. Adjust size for pixel-perfect
206         // mirroring over HDMI.)
207         DisplayDeviceInfo deviceInfo = mPrimaryDisplayDevice.getDisplayDeviceInfoLocked();
208         if (!Objects.equal(mPrimaryDisplayDeviceInfo, deviceInfo)) {
209             mBaseDisplayInfo.layerStack = mLayerStack;
210             mBaseDisplayInfo.flags = 0;
211             if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS) != 0) {
212                 mBaseDisplayInfo.flags |= Display.FLAG_SUPPORTS_PROTECTED_BUFFERS;
213             }
214             if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_SECURE) != 0) {
215                 mBaseDisplayInfo.flags |= Display.FLAG_SECURE;
216             }
217             if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_PRIVATE) != 0) {
218                 mBaseDisplayInfo.flags |= Display.FLAG_PRIVATE;
219             }
220             if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_PRESENTATION) != 0) {
221                 mBaseDisplayInfo.flags |= Display.FLAG_PRESENTATION;
222             }
223             if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_ROUND) != 0) {
224                 mBaseDisplayInfo.flags |= Display.FLAG_ROUND;
225             }
226             mBaseDisplayInfo.type = deviceInfo.type;
227             mBaseDisplayInfo.address = deviceInfo.address;
228             mBaseDisplayInfo.name = deviceInfo.name;
229             mBaseDisplayInfo.uniqueId = deviceInfo.uniqueId;
230             mBaseDisplayInfo.appWidth = deviceInfo.width;
231             mBaseDisplayInfo.appHeight = deviceInfo.height;
232             mBaseDisplayInfo.logicalWidth = deviceInfo.width;
233             mBaseDisplayInfo.logicalHeight = deviceInfo.height;
234             mBaseDisplayInfo.rotation = Surface.ROTATION_0;
235             mBaseDisplayInfo.modeId = deviceInfo.modeId;
236             mBaseDisplayInfo.defaultModeId = deviceInfo.defaultModeId;
237             mBaseDisplayInfo.supportedModes = Arrays.copyOf(
238                     deviceInfo.supportedModes, deviceInfo.supportedModes.length);
239             mBaseDisplayInfo.colorTransformId = deviceInfo.colorTransformId;
240             mBaseDisplayInfo.defaultColorTransformId = deviceInfo.defaultColorTransformId;
241             mBaseDisplayInfo.supportedColorTransforms = Arrays.copyOf(
242                     deviceInfo.supportedColorTransforms,
243                     deviceInfo.supportedColorTransforms.length);
244             mBaseDisplayInfo.hdrCapabilities = deviceInfo.hdrCapabilities;
245             mBaseDisplayInfo.logicalDensityDpi = deviceInfo.densityDpi;
246             mBaseDisplayInfo.physicalXDpi = deviceInfo.xDpi;
247             mBaseDisplayInfo.physicalYDpi = deviceInfo.yDpi;
248             mBaseDisplayInfo.appVsyncOffsetNanos = deviceInfo.appVsyncOffsetNanos;
249             mBaseDisplayInfo.presentationDeadlineNanos = deviceInfo.presentationDeadlineNanos;
250             mBaseDisplayInfo.state = deviceInfo.state;
251             mBaseDisplayInfo.smallestNominalAppWidth = deviceInfo.width;
252             mBaseDisplayInfo.smallestNominalAppHeight = deviceInfo.height;
253             mBaseDisplayInfo.largestNominalAppWidth = deviceInfo.width;
254             mBaseDisplayInfo.largestNominalAppHeight = deviceInfo.height;
255             mBaseDisplayInfo.ownerUid = deviceInfo.ownerUid;
256             mBaseDisplayInfo.ownerPackageName = deviceInfo.ownerPackageName;
257 
258             mPrimaryDisplayDeviceInfo = deviceInfo;
259             mInfo = null;
260         }
261     }
262 
263     /**
264      * Applies the layer stack and transformation to the given display device
265      * so that it shows the contents of this logical display.
266      *
267      * We know that the given display device is only ever showing the contents of
268      * a single logical display, so this method is expected to blow away all of its
269      * transformation properties to make it happen regardless of what the
270      * display device was previously showing.
271      *
272      * The caller must have an open Surface transaction.
273      *
274      * The display device may not be the primary display device, in the case
275      * where the display is being mirrored.
276      *
277      * @param device The display device to modify.
278      * @param isBlanked True if the device is being blanked.
279      */
configureDisplayInTransactionLocked(DisplayDevice device, boolean isBlanked)280     public void configureDisplayInTransactionLocked(DisplayDevice device,
281             boolean isBlanked) {
282         // Set the layer stack.
283         device.setLayerStackInTransactionLocked(isBlanked ? BLANK_LAYER_STACK : mLayerStack);
284 
285         // Set the color transform and mode.
286         if (device == mPrimaryDisplayDevice) {
287             device.requestColorTransformAndModeInTransactionLocked(
288                     mRequestedColorTransformId, mRequestedModeId);
289         } else {
290             device.requestColorTransformAndModeInTransactionLocked(0, 0);  // Revert to default.
291         }
292 
293         // Only grab the display info now as it may have been changed based on the requests above.
294         final DisplayInfo displayInfo = getDisplayInfoLocked();
295         final DisplayDeviceInfo displayDeviceInfo = device.getDisplayDeviceInfoLocked();
296 
297         // Set the viewport.
298         // This is the area of the logical display that we intend to show on the
299         // display device.  For now, it is always the full size of the logical display.
300         mTempLayerStackRect.set(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
301 
302         // Set the orientation.
303         // The orientation specifies how the physical coordinate system of the display
304         // is rotated when the contents of the logical display are rendered.
305         int orientation = Surface.ROTATION_0;
306         if ((displayDeviceInfo.flags & DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT) != 0) {
307             orientation = displayInfo.rotation;
308         }
309 
310         // Apply the physical rotation of the display device itself.
311         orientation = (orientation + displayDeviceInfo.rotation) % 4;
312 
313         // Set the frame.
314         // The frame specifies the rotated physical coordinates into which the viewport
315         // is mapped.  We need to take care to preserve the aspect ratio of the viewport.
316         // Currently we maximize the area to fill the display, but we could try to be
317         // more clever and match resolutions.
318         boolean rotated = (orientation == Surface.ROTATION_90
319                 || orientation == Surface.ROTATION_270);
320         int physWidth = rotated ? displayDeviceInfo.height : displayDeviceInfo.width;
321         int physHeight = rotated ? displayDeviceInfo.width : displayDeviceInfo.height;
322 
323         // Determine whether the width or height is more constrained to be scaled.
324         //    physWidth / displayInfo.logicalWidth    => letter box
325         // or physHeight / displayInfo.logicalHeight  => pillar box
326         //
327         // We avoid a division (and possible floating point imprecision) here by
328         // multiplying the fractions by the product of their denominators before
329         // comparing them.
330         int displayRectWidth, displayRectHeight;
331         if ((displayInfo.flags & Display.FLAG_SCALING_DISABLED) != 0) {
332             displayRectWidth = displayInfo.logicalWidth;
333             displayRectHeight = displayInfo.logicalHeight;
334         } else if (physWidth * displayInfo.logicalHeight
335                 < physHeight * displayInfo.logicalWidth) {
336             // Letter box.
337             displayRectWidth = physWidth;
338             displayRectHeight = displayInfo.logicalHeight * physWidth / displayInfo.logicalWidth;
339         } else {
340             // Pillar box.
341             displayRectWidth = displayInfo.logicalWidth * physHeight / displayInfo.logicalHeight;
342             displayRectHeight = physHeight;
343         }
344         int displayRectTop = (physHeight - displayRectHeight) / 2;
345         int displayRectLeft = (physWidth - displayRectWidth) / 2;
346         mTempDisplayRect.set(displayRectLeft, displayRectTop,
347                 displayRectLeft + displayRectWidth, displayRectTop + displayRectHeight);
348 
349         mTempDisplayRect.left += mDisplayOffsetX;
350         mTempDisplayRect.right += mDisplayOffsetX;
351         mTempDisplayRect.top += mDisplayOffsetY;
352         mTempDisplayRect.bottom += mDisplayOffsetY;
353         device.setProjectionInTransactionLocked(orientation, mTempLayerStackRect, mTempDisplayRect);
354     }
355 
356     /**
357      * Returns true if the logical display has unique content.
358      * <p>
359      * If the display has unique content then we will try to ensure that it is
360      * visible on at least its primary display device.  Otherwise we will ignore the
361      * logical display and perhaps show mirrored content on the primary display device.
362      * </p>
363      *
364      * @return True if the display has unique content.
365      */
hasContentLocked()366     public boolean hasContentLocked() {
367         return mHasContent;
368     }
369 
370     /**
371      * Sets whether the logical display has unique content.
372      *
373      * @param hasContent True if the display has unique content.
374      */
setHasContentLocked(boolean hasContent)375     public void setHasContentLocked(boolean hasContent) {
376         mHasContent = hasContent;
377     }
378 
379     /**
380      * Requests the given mode.
381      */
setRequestedModeIdLocked(int modeId)382     public void setRequestedModeIdLocked(int modeId) {
383         mRequestedModeId = modeId;
384     }
385 
386     /**
387      * Returns the pending requested mode.
388      */
getRequestedModeIdLocked()389     public int getRequestedModeIdLocked() {
390         return mRequestedModeId;
391     }
392 
393     /**
394      * Requests the given color transform.
395      */
setRequestedColorTransformIdLocked(int colorTransformId)396     public void setRequestedColorTransformIdLocked(int colorTransformId) {
397         mRequestedColorTransformId = colorTransformId;
398     }
399 
400     /** Returns the pending requested color transform. */
getRequestedColorTransformIdLocked()401     public int getRequestedColorTransformIdLocked() {
402         return mRequestedColorTransformId;
403     }
404 
405     /**
406      * Gets the burn-in offset in X.
407      */
getDisplayOffsetXLocked()408     public int getDisplayOffsetXLocked() {
409         return mDisplayOffsetX;
410     }
411 
412     /**
413      * Gets the burn-in offset in Y.
414      */
getDisplayOffsetYLocked()415     public int getDisplayOffsetYLocked() {
416         return mDisplayOffsetY;
417     }
418 
419     /**
420      * Sets the burn-in offsets.
421      */
setDisplayOffsetsLocked(int x, int y)422     public void setDisplayOffsetsLocked(int x, int y) {
423         mDisplayOffsetX = x;
424         mDisplayOffsetY = y;
425     }
426 
dumpLocked(PrintWriter pw)427     public void dumpLocked(PrintWriter pw) {
428         pw.println("mDisplayId=" + mDisplayId);
429         pw.println("mLayerStack=" + mLayerStack);
430         pw.println("mHasContent=" + mHasContent);
431         pw.println("mRequestedMode=" + mRequestedModeId);
432         pw.println("mRequestedColorTransformId=" + mRequestedColorTransformId);
433         pw.println("mDisplayOffset=(" + mDisplayOffsetX + ", " + mDisplayOffsetY + ")");
434         pw.println("mPrimaryDisplayDevice=" + (mPrimaryDisplayDevice != null ?
435                 mPrimaryDisplayDevice.getNameLocked() : "null"));
436         pw.println("mBaseDisplayInfo=" + mBaseDisplayInfo);
437         pw.println("mOverrideDisplayInfo=" + mOverrideDisplayInfo);
438     }
439 }
440