LogicalDisplay.java revision 722285e199a9fc74b9b3343b7505c00666848c88
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.graphics.Rect;
20import android.view.DisplayInfo;
21import android.view.Surface;
22
23import java.io.PrintWriter;
24import java.util.List;
25
26import libcore.util.Objects;
27
28/**
29 * Describes how a logical display is configured.
30 * <p>
31 * At this time, we only support logical displays that are coupled to a particular
32 * primary display device from which the logical display derives its basic properties
33 * such as its size, density and refresh rate.
34 * </p><p>
35 * A logical display may be mirrored onto multiple display devices in addition to its
36 * primary display device.  Note that the contents of a logical display may not
37 * always be visible, even on its primary display device, such as in the case where
38 * the primary display device is currently mirroring content from a different
39 * logical display.
40 * </p><p>
41 * This object is designed to encapsulate as much of the policy of logical
42 * displays as possible.  The idea is to make it easy to implement new kinds of
43 * logical displays mostly by making local changes to this class.
44 * </p><p>
45 * Note: The display manager architecture does not actually require logical displays
46 * to be associated with any individual display device.  Logical displays and
47 * display devices are orthogonal concepts.  Some mapping will exist between
48 * logical displays and display devices but it can be many-to-many and
49 * and some might have no relation at all.
50 * </p><p>
51 * Logical displays are guarded by the {@link DisplayManagerService.SyncRoot} lock.
52 * </p>
53 */
54final class LogicalDisplay {
55    private final DisplayInfo mBaseDisplayInfo = new DisplayInfo();
56
57    private final int mLayerStack;
58    private DisplayInfo mOverrideDisplayInfo; // set by the window manager
59    private DisplayInfo mInfo;
60
61    // The display device that this logical display is based on and which
62    // determines the base metrics that it uses.
63    private DisplayDevice mPrimaryDisplayDevice;
64    private DisplayDeviceInfo mPrimaryDisplayDeviceInfo;
65
66    // True if the logical display has unique content.
67    private boolean mHasContent;
68
69    // Temporary rectangle used when needed.
70    private final Rect mTempLayerStackRect = new Rect();
71    private final Rect mTempDisplayRect = new Rect();
72
73    public LogicalDisplay(int layerStack, DisplayDevice primaryDisplayDevice) {
74        mLayerStack = layerStack;
75        mPrimaryDisplayDevice = primaryDisplayDevice;
76    }
77
78    /**
79     * Gets the primary display device associated with this logical display.
80     *
81     * @return The primary display device.
82     */
83    public DisplayDevice getPrimaryDisplayDeviceLocked() {
84        return mPrimaryDisplayDevice;
85    }
86
87    /**
88     * Gets information about the logical display.
89     *
90     * @return The device info, which should be treated as immutable by the caller.
91     * The logical display should allocate a new display info object whenever
92     * the data changes.
93     */
94    public DisplayInfo getDisplayInfoLocked() {
95        if (mInfo == null) {
96            mInfo = new DisplayInfo();
97            if (mOverrideDisplayInfo != null) {
98                mInfo.copyFrom(mOverrideDisplayInfo);
99                mInfo.layerStack = mBaseDisplayInfo.layerStack;
100                mInfo.name = mBaseDisplayInfo.name;
101            } else {
102                mInfo.copyFrom(mBaseDisplayInfo);
103            }
104        }
105        return mInfo;
106    }
107
108    /**
109     * Sets overridden logical display information from the window manager.
110     * This method can be used to adjust application insets, rotation, and other
111     * properties that the window manager takes care of.
112     *
113     * @param info The logical display information, may be null.
114     */
115    public void setDisplayInfoOverrideFromWindowManagerLocked(DisplayInfo info) {
116        if (info != null) {
117            if (mOverrideDisplayInfo == null) {
118                mOverrideDisplayInfo = new DisplayInfo(info);
119                mInfo = null;
120            } else if (!mOverrideDisplayInfo.equals(info)) {
121                mOverrideDisplayInfo.copyFrom(info);
122                mInfo = null;
123            }
124        } else if (mOverrideDisplayInfo != null) {
125            mOverrideDisplayInfo = null;
126            mInfo = null;
127        }
128    }
129
130    /**
131     * Returns true if the logical display is in a valid state.
132     * This method should be checked after calling {@link #updateLocked} to handle the
133     * case where a logical display should be removed because all of its associated
134     * display devices are gone or if it is otherwise no longer needed.
135     *
136     * @return True if the logical display is still valid.
137     */
138    public boolean isValidLocked() {
139        return mPrimaryDisplayDevice != null;
140    }
141
142    /**
143     * Updates the state of the logical display based on the available display devices.
144     * The logical display might become invalid if it is attached to a display device
145     * that no longer exists.
146     *
147     * @param devices The list of all connected display devices.
148     */
149    public void updateLocked(List<DisplayDevice> devices) {
150        // Nothing to update if already invalid.
151        if (mPrimaryDisplayDevice == null) {
152            return;
153        }
154
155        // Check whether logical display has become invalid.
156        if (!devices.contains(mPrimaryDisplayDevice)) {
157            mPrimaryDisplayDevice = null;
158            return;
159        }
160
161        // Bootstrap the logical display using its associated primary physical display.
162        // We might use more elaborate configurations later.  It's possible that the
163        // configuration of several physical displays might be used to determine the
164        // logical display that they are sharing.  (eg. Adjust size for pixel-perfect
165        // mirroring over HDMI.)
166        DisplayDeviceInfo deviceInfo = mPrimaryDisplayDevice.getDisplayDeviceInfoLocked();
167        if (!Objects.equal(mPrimaryDisplayDeviceInfo, deviceInfo)) {
168            mBaseDisplayInfo.layerStack = mLayerStack;
169            mBaseDisplayInfo.name = deviceInfo.name;
170            mBaseDisplayInfo.appWidth = deviceInfo.width;
171            mBaseDisplayInfo.appHeight = deviceInfo.height;
172            mBaseDisplayInfo.logicalWidth = deviceInfo.width;
173            mBaseDisplayInfo.logicalHeight = deviceInfo.height;
174            mBaseDisplayInfo.rotation = Surface.ROTATION_0;
175            mBaseDisplayInfo.refreshRate = deviceInfo.refreshRate;
176            mBaseDisplayInfo.logicalDensityDpi = deviceInfo.densityDpi;
177            mBaseDisplayInfo.physicalXDpi = deviceInfo.xDpi;
178            mBaseDisplayInfo.physicalYDpi = deviceInfo.yDpi;
179            mBaseDisplayInfo.smallestNominalAppWidth = deviceInfo.width;
180            mBaseDisplayInfo.smallestNominalAppHeight = deviceInfo.height;
181            mBaseDisplayInfo.largestNominalAppWidth = deviceInfo.width;
182            mBaseDisplayInfo.largestNominalAppHeight = deviceInfo.height;
183
184            mPrimaryDisplayDeviceInfo = deviceInfo;
185            mInfo = null;
186        }
187    }
188
189    /**
190     * Applies the layer stack and transformation to the given display device
191     * so that it shows the contents of this logical display.
192     *
193     * We know that the given display device is only ever showing the contents of
194     * a single logical display, so this method is expected to blow away all of its
195     * transformation properties to make it happen regardless of what the
196     * display device was previously showing.
197     *
198     * The caller must have an open Surface transaction.
199     *
200     * The display device may not be the primary display device, in the case
201     * where the display is being mirrored.
202     *
203     * @param device The display device to modify.
204     */
205    public void configureDisplayInTransactionLocked(DisplayDevice device) {
206        final DisplayInfo displayInfo = getDisplayInfoLocked();
207        final DisplayDeviceInfo displayDeviceInfo = device.getDisplayDeviceInfoLocked();
208
209        // Set the layer stack.
210        device.setLayerStackInTransactionLocked(mLayerStack);
211
212        // Set the viewport.
213        // This is the area of the logical display that we intend to show on the
214        // display device.  For now, it is always the full size of the logical display.
215        mTempLayerStackRect.set(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
216
217        // Set the orientation.
218        // The orientation specifies how the physical coordinate system of the display
219        // is rotated when the contents of the logical display are rendered.
220        int orientation = Surface.ROTATION_0;
221        if (device == mPrimaryDisplayDevice
222                && (displayDeviceInfo.flags & DisplayDeviceInfo.FLAG_SUPPORTS_ROTATION) != 0) {
223            orientation = displayInfo.rotation;
224        }
225
226        // Set the frame.
227        // The frame specifies the rotated physical coordinates into which the viewport
228        // is mapped.  We need to take care to preserve the aspect ratio of the viewport.
229        // Currently we maximize the area to fill the display, but we could try to be
230        // more clever and match resolutions.
231        boolean rotated = (orientation == Surface.ROTATION_90
232                || orientation == Surface.ROTATION_270);
233        int physWidth = rotated ? displayDeviceInfo.height : displayDeviceInfo.width;
234        int physHeight = rotated ? displayDeviceInfo.width : displayDeviceInfo.height;
235
236        // Determine whether the width or height is more constrained to be scaled.
237        //    physWidth / displayInfo.logicalWidth    => letter box
238        // or physHeight / displayInfo.logicalHeight  => pillar box
239        //
240        // We avoid a division (and possible floating point imprecision) here by
241        // multiplying the fractions by the product of their denominators before
242        // comparing them.
243        int displayRectWidth, displayRectHeight;
244        if (physWidth * displayInfo.logicalHeight
245                < physHeight * displayInfo.logicalWidth) {
246            // Letter box.
247            displayRectWidth = physWidth;
248            displayRectHeight = displayInfo.logicalHeight * physWidth / displayInfo.logicalWidth;
249        } else {
250            // Pillar box.
251            displayRectWidth = displayInfo.logicalWidth * physHeight / displayInfo.logicalHeight;
252            displayRectHeight = physHeight;
253        }
254        int displayRectTop = (physHeight - displayRectHeight) / 2;
255        int displayRectLeft = (physWidth - displayRectWidth) / 2;
256        mTempDisplayRect.set(displayRectLeft, displayRectTop,
257                displayRectLeft + displayRectWidth, displayRectTop + displayRectHeight);
258
259        device.setProjectionInTransactionLocked(orientation, mTempLayerStackRect, mTempDisplayRect);
260    }
261
262    /**
263     * Returns true if the logical display has unique content.
264     * <p>
265     * If the display has unique content then we will try to ensure that it is
266     * visible on at least its primary display device.  Otherwise we will ignore the
267     * logical display and perhaps show mirrored content on the primary display device.
268     * </p>
269     *
270     * @return True if the display has unique content.
271     */
272    public boolean hasContentLocked() {
273        return mHasContent;
274    }
275
276    /**
277     * Sets whether the logical display has unique content.
278     *
279     * @param hasContent True if the display has unique content.
280     */
281    public void setHasContentLocked(boolean hasContent) {
282        mHasContent = hasContent;
283    }
284
285    public void dumpLocked(PrintWriter pw) {
286        pw.println("mLayerStack=" + mLayerStack);
287        pw.println("mPrimaryDisplayDevice=" + (mPrimaryDisplayDevice != null ?
288                mPrimaryDisplayDevice.getNameLocked() : "null"));
289        pw.println("mBaseDisplayInfo=" + mBaseDisplayInfo);
290        pw.println("mOverrideDisplayInfo=" + mOverrideDisplayInfo);
291    }
292}