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