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