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