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