blob: 7ee842260d3bfd3cb94532885b32833f301f1c18 [file] [log] [blame]
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001/*
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;
Andrii Kuliancd097992017-03-23 18:31:59 -070020import android.hardware.display.DisplayManagerInternal;
Jeff Brownc5df37c2012-09-13 11:45:07 -070021import android.view.Display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070022import android.view.DisplayInfo;
23import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080024import android.view.SurfaceControl;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070025
Adrian Roos8c28c7c2018-08-20 13:43:38 +020026import com.android.server.wm.utils.InsetUtils;
27
Jeff Brown4ed8fe72012-08-30 18:18:29 -070028import java.io.PrintWriter;
Michael Wright3f145a22014-07-22 19:46:03 -070029import java.util.Arrays;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070030import java.util.List;
Narayan Kamath607223f2018-02-19 14:09:02 +000031import java.util.Objects;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070032
33/**
34 * Describes how a logical display is configured.
35 * <p>
36 * At this time, we only support logical displays that are coupled to a particular
37 * primary display device from which the logical display derives its basic properties
38 * such as its size, density and refresh rate.
39 * </p><p>
40 * A logical display may be mirrored onto multiple display devices in addition to its
41 * primary display device. Note that the contents of a logical display may not
42 * always be visible, even on its primary display device, such as in the case where
43 * the primary display device is currently mirroring content from a different
44 * logical display.
45 * </p><p>
46 * This object is designed to encapsulate as much of the policy of logical
47 * displays as possible. The idea is to make it easy to implement new kinds of
48 * logical displays mostly by making local changes to this class.
49 * </p><p>
50 * Note: The display manager architecture does not actually require logical displays
51 * to be associated with any individual display device. Logical displays and
52 * display devices are orthogonal concepts. Some mapping will exist between
53 * logical displays and display devices but it can be many-to-many and
54 * and some might have no relation at all.
55 * </p><p>
56 * Logical displays are guarded by the {@link DisplayManagerService.SyncRoot} lock.
57 * </p>
58 */
59final class LogicalDisplay {
60 private final DisplayInfo mBaseDisplayInfo = new DisplayInfo();
61
Jeff Brown8ec09432012-10-16 16:19:23 -070062 // The layer stack we use when the display has been blanked to prevent any
63 // of its content from appearing.
64 private static final int BLANK_LAYER_STACK = -1;
65
Jeff Brownd728bf52012-09-08 18:05:28 -070066 private final int mDisplayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070067 private final int mLayerStack;
Andrii Kuliancd097992017-03-23 18:31:59 -070068 /**
69 * Override information set by the window manager. Will be reported instead of {@link #mInfo}
70 * if not null.
71 * @see #setDisplayInfoOverrideFromWindowManagerLocked(DisplayInfo)
72 * @see #getDisplayInfoLocked()
73 */
74 private DisplayInfo mOverrideDisplayInfo;
75 /**
76 * Current display info. Initialized with {@link #mBaseDisplayInfo}. Set to {@code null} if
77 * needs to be updated.
78 * @see #getDisplayInfoLocked()
79 */
Jeff Brown4ed8fe72012-08-30 18:18:29 -070080 private DisplayInfo mInfo;
81
82 // The display device that this logical display is based on and which
83 // determines the base metrics that it uses.
84 private DisplayDevice mPrimaryDisplayDevice;
85 private DisplayDeviceInfo mPrimaryDisplayDeviceInfo;
86
Craig Mautner722285e2012-09-07 13:55:58 -070087 // True if the logical display has unique content.
88 private boolean mHasContent;
89
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070090 private int mRequestedModeId;
Michael Wright1c9977b2016-07-12 13:30:10 -070091 private int mRequestedColorMode;
Michael Wright3f145a22014-07-22 19:46:03 -070092
Filip Gruszczynskid2e86402015-02-19 13:05:03 -080093 // The display offsets to apply to the display projection.
94 private int mDisplayOffsetX;
95 private int mDisplayOffsetY;
96
Sam Lin4c3ac2b2019-02-18 04:50:26 -080097 /**
98 * {@code true} if display scaling is disabled, or {@code false} if the default scaling mode
99 * is used.
100 * @see #isDisplayScalingDisabled()
101 * @see #setDisplayScalingDisabledLocked(boolean)
102 */
103 private boolean mDisplayScalingDisabled;
104
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700105 // Temporary rectangle used when needed.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700106 private final Rect mTempLayerStackRect = new Rect();
107 private final Rect mTempDisplayRect = new Rect();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700108
Jeff Brownd728bf52012-09-08 18:05:28 -0700109 public LogicalDisplay(int displayId, int layerStack, DisplayDevice primaryDisplayDevice) {
110 mDisplayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700111 mLayerStack = layerStack;
112 mPrimaryDisplayDevice = primaryDisplayDevice;
113 }
114
115 /**
Jeff Brownd728bf52012-09-08 18:05:28 -0700116 * Gets the logical display id of this logical display.
117 *
118 * @return The logical display id.
119 */
120 public int getDisplayIdLocked() {
121 return mDisplayId;
122 }
123
124 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700125 * Gets the primary display device associated with this logical display.
126 *
127 * @return The primary display device.
128 */
129 public DisplayDevice getPrimaryDisplayDeviceLocked() {
130 return mPrimaryDisplayDevice;
131 }
132
133 /**
134 * Gets information about the logical display.
135 *
136 * @return The device info, which should be treated as immutable by the caller.
137 * The logical display should allocate a new display info object whenever
138 * the data changes.
139 */
140 public DisplayInfo getDisplayInfoLocked() {
141 if (mInfo == null) {
142 mInfo = new DisplayInfo();
P.Y. Laliganded896d42015-05-12 10:43:38 -0700143 mInfo.copyFrom(mBaseDisplayInfo);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700144 if (mOverrideDisplayInfo != null) {
P.Y. Laliganded896d42015-05-12 10:43:38 -0700145 mInfo.appWidth = mOverrideDisplayInfo.appWidth;
146 mInfo.appHeight = mOverrideDisplayInfo.appHeight;
147 mInfo.smallestNominalAppWidth = mOverrideDisplayInfo.smallestNominalAppWidth;
148 mInfo.smallestNominalAppHeight = mOverrideDisplayInfo.smallestNominalAppHeight;
149 mInfo.largestNominalAppWidth = mOverrideDisplayInfo.largestNominalAppWidth;
150 mInfo.largestNominalAppHeight = mOverrideDisplayInfo.largestNominalAppHeight;
151 mInfo.logicalWidth = mOverrideDisplayInfo.logicalWidth;
152 mInfo.logicalHeight = mOverrideDisplayInfo.logicalHeight;
153 mInfo.overscanLeft = mOverrideDisplayInfo.overscanLeft;
154 mInfo.overscanTop = mOverrideDisplayInfo.overscanTop;
155 mInfo.overscanRight = mOverrideDisplayInfo.overscanRight;
156 mInfo.overscanBottom = mOverrideDisplayInfo.overscanBottom;
157 mInfo.rotation = mOverrideDisplayInfo.rotation;
Adrian Roos1cf585052018-01-03 18:43:27 +0100158 mInfo.displayCutout = mOverrideDisplayInfo.displayCutout;
P.Y. Laliganded896d42015-05-12 10:43:38 -0700159 mInfo.logicalDensityDpi = mOverrideDisplayInfo.logicalDensityDpi;
Jeff Brownfe4ad332015-06-09 18:26:31 -0700160 mInfo.physicalXDpi = mOverrideDisplayInfo.physicalXDpi;
161 mInfo.physicalYDpi = mOverrideDisplayInfo.physicalYDpi;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700162 }
163 }
164 return mInfo;
165 }
166
167 /**
Andrii Kuliancd097992017-03-23 18:31:59 -0700168 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
169 */
170 void getNonOverrideDisplayInfoLocked(DisplayInfo outInfo) {
171 outInfo.copyFrom(mBaseDisplayInfo);
172 }
173
174 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700175 * Sets overridden logical display information from the window manager.
176 * This method can be used to adjust application insets, rotation, and other
177 * properties that the window manager takes care of.
178 *
179 * @param info The logical display information, may be null.
180 */
Jeff Brownef981a42013-08-07 14:13:37 -0700181 public boolean setDisplayInfoOverrideFromWindowManagerLocked(DisplayInfo info) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700182 if (info != null) {
183 if (mOverrideDisplayInfo == null) {
184 mOverrideDisplayInfo = new DisplayInfo(info);
185 mInfo = null;
Jeff Brownef981a42013-08-07 14:13:37 -0700186 return true;
187 }
188 if (!mOverrideDisplayInfo.equals(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700189 mOverrideDisplayInfo.copyFrom(info);
190 mInfo = null;
Jeff Brownef981a42013-08-07 14:13:37 -0700191 return true;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700192 }
193 } else if (mOverrideDisplayInfo != null) {
194 mOverrideDisplayInfo = null;
195 mInfo = null;
Jeff Brownef981a42013-08-07 14:13:37 -0700196 return true;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700197 }
Jeff Brownef981a42013-08-07 14:13:37 -0700198 return false;
Dianne Hackbornc652de82013-02-15 16:32:56 -0800199 }
200
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700201 /**
202 * Returns true if the logical display is in a valid state.
Craig Mautner722285e2012-09-07 13:55:58 -0700203 * This method should be checked after calling {@link #updateLocked} to handle the
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700204 * case where a logical display should be removed because all of its associated
205 * display devices are gone or if it is otherwise no longer needed.
206 *
207 * @return True if the logical display is still valid.
208 */
209 public boolean isValidLocked() {
210 return mPrimaryDisplayDevice != null;
211 }
212
213 /**
214 * Updates the state of the logical display based on the available display devices.
215 * The logical display might become invalid if it is attached to a display device
216 * that no longer exists.
217 *
218 * @param devices The list of all connected display devices.
219 */
220 public void updateLocked(List<DisplayDevice> devices) {
221 // Nothing to update if already invalid.
222 if (mPrimaryDisplayDevice == null) {
223 return;
224 }
225
226 // Check whether logical display has become invalid.
227 if (!devices.contains(mPrimaryDisplayDevice)) {
228 mPrimaryDisplayDevice = null;
229 return;
230 }
231
232 // Bootstrap the logical display using its associated primary physical display.
233 // We might use more elaborate configurations later. It's possible that the
234 // configuration of several physical displays might be used to determine the
235 // logical display that they are sharing. (eg. Adjust size for pixel-perfect
236 // mirroring over HDMI.)
237 DisplayDeviceInfo deviceInfo = mPrimaryDisplayDevice.getDisplayDeviceInfoLocked();
Narayan Kamath607223f2018-02-19 14:09:02 +0000238 if (!Objects.equals(mPrimaryDisplayDeviceInfo, deviceInfo)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700239 mBaseDisplayInfo.layerStack = mLayerStack;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700240 mBaseDisplayInfo.flags = 0;
Jeff Brown77aebfd2012-10-01 21:07:03 -0700241 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS) != 0) {
242 mBaseDisplayInfo.flags |= Display.FLAG_SUPPORTS_PROTECTED_BUFFERS;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700243 }
Jeff Brownf0681b32012-10-23 17:35:57 -0700244 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_SECURE) != 0) {
245 mBaseDisplayInfo.flags |= Display.FLAG_SECURE;
246 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700247 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_PRIVATE) != 0) {
248 mBaseDisplayInfo.flags |= Display.FLAG_PRIVATE;
Andrii Kulian250d6532017-02-08 23:30:45 -0800249 // For private displays by default content is destroyed on removal.
250 mBaseDisplayInfo.removeMode = Display.REMOVE_MODE_DESTROY_CONTENT;
Jeff Browna506a6e2013-06-04 00:02:38 -0700251 }
rongliu1e90fc32017-10-04 17:30:30 -0700252 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_DESTROY_CONTENT_ON_REMOVAL) != 0) {
253 mBaseDisplayInfo.removeMode = Display.REMOVE_MODE_DESTROY_CONTENT;
254 }
Jeff Brown7d00aff2013-08-02 19:03:49 -0700255 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_PRESENTATION) != 0) {
256 mBaseDisplayInfo.flags |= Display.FLAG_PRESENTATION;
257 }
Adam Powell49e7ff92015-05-14 16:18:53 -0700258 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_ROUND) != 0) {
259 mBaseDisplayInfo.flags |= Display.FLAG_ROUND;
260 }
Andrii Kulian7211d2e2017-01-27 15:58:05 -0800261 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
262 mBaseDisplayInfo.flags |= Display.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800263 }
Louis Changbd48dca2018-08-29 17:44:34 +0800264 if ((deviceInfo.flags & DisplayDeviceInfo.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0) {
265 mBaseDisplayInfo.flags |= Display.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
266 }
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200267 Rect maskingInsets = getMaskingInsets(deviceInfo);
268 int maskedWidth = deviceInfo.width - maskingInsets.left - maskingInsets.right;
269 int maskedHeight = deviceInfo.height - maskingInsets.top - maskingInsets.bottom;
270
Jeff Brown92130f62012-10-24 21:28:33 -0700271 mBaseDisplayInfo.type = deviceInfo.type;
272 mBaseDisplayInfo.address = deviceInfo.address;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700273 mBaseDisplayInfo.name = deviceInfo.name;
Wale Ogunwale361ca212014-11-20 11:42:38 -0800274 mBaseDisplayInfo.uniqueId = deviceInfo.uniqueId;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200275 mBaseDisplayInfo.appWidth = maskedWidth;
276 mBaseDisplayInfo.appHeight = maskedHeight;
277 mBaseDisplayInfo.logicalWidth = maskedWidth;
278 mBaseDisplayInfo.logicalHeight = maskedHeight;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700279 mBaseDisplayInfo.rotation = Surface.ROTATION_0;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700280 mBaseDisplayInfo.modeId = deviceInfo.modeId;
281 mBaseDisplayInfo.defaultModeId = deviceInfo.defaultModeId;
282 mBaseDisplayInfo.supportedModes = Arrays.copyOf(
283 deviceInfo.supportedModes, deviceInfo.supportedModes.length);
Michael Wright1c9977b2016-07-12 13:30:10 -0700284 mBaseDisplayInfo.colorMode = deviceInfo.colorMode;
285 mBaseDisplayInfo.supportedColorModes = Arrays.copyOf(
286 deviceInfo.supportedColorModes,
287 deviceInfo.supportedColorModes.length);
Michael Wright9ff94c02016-03-30 18:05:40 -0700288 mBaseDisplayInfo.hdrCapabilities = deviceInfo.hdrCapabilities;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700289 mBaseDisplayInfo.logicalDensityDpi = deviceInfo.densityDpi;
290 mBaseDisplayInfo.physicalXDpi = deviceInfo.xDpi;
291 mBaseDisplayInfo.physicalYDpi = deviceInfo.yDpi;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700292 mBaseDisplayInfo.appVsyncOffsetNanos = deviceInfo.appVsyncOffsetNanos;
293 mBaseDisplayInfo.presentationDeadlineNanos = deviceInfo.presentationDeadlineNanos;
Jeff Brown037c33e2014-04-09 00:31:55 -0700294 mBaseDisplayInfo.state = deviceInfo.state;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200295 mBaseDisplayInfo.smallestNominalAppWidth = maskedWidth;
296 mBaseDisplayInfo.smallestNominalAppHeight = maskedHeight;
297 mBaseDisplayInfo.largestNominalAppWidth = maskedWidth;
298 mBaseDisplayInfo.largestNominalAppHeight = maskedHeight;
Jeff Browna506a6e2013-06-04 00:02:38 -0700299 mBaseDisplayInfo.ownerUid = deviceInfo.ownerUid;
300 mBaseDisplayInfo.ownerPackageName = deviceInfo.ownerPackageName;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200301 boolean maskCutout =
302 (deviceInfo.flags & DisplayDeviceInfo.FLAG_MASK_DISPLAY_CUTOUT) != 0;
303 mBaseDisplayInfo.displayCutout = maskCutout ? null : deviceInfo.displayCutout;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700304
305 mPrimaryDisplayDeviceInfo = deviceInfo;
306 mInfo = null;
307 }
308 }
309
310 /**
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200311 * Return the insets currently applied to the display.
312 *
313 * Note that the base DisplayInfo already takes these insets into account, so if you want to
314 * find out the <b>true</b> size of the display, you need to add them back to the logical
315 * dimensions.
316 */
317 public Rect getInsets() {
318 return getMaskingInsets(mPrimaryDisplayDeviceInfo);
319 }
320
321 /**
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200322 * Returns insets in ROTATION_0 for areas that are masked.
323 */
324 private static Rect getMaskingInsets(DisplayDeviceInfo deviceInfo) {
325 boolean maskCutout = (deviceInfo.flags & DisplayDeviceInfo.FLAG_MASK_DISPLAY_CUTOUT) != 0;
326 if (maskCutout && deviceInfo.displayCutout != null) {
327 return deviceInfo.displayCutout.getSafeInsets();
328 } else {
329 return new Rect();
330 }
331 }
332
333 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700334 * Applies the layer stack and transformation to the given display device
335 * so that it shows the contents of this logical display.
336 *
337 * We know that the given display device is only ever showing the contents of
338 * a single logical display, so this method is expected to blow away all of its
339 * transformation properties to make it happen regardless of what the
340 * display device was previously showing.
341 *
342 * The caller must have an open Surface transaction.
343 *
344 * The display device may not be the primary display device, in the case
345 * where the display is being mirrored.
346 *
347 * @param device The display device to modify.
Jeff Brown8ec09432012-10-16 16:19:23 -0700348 * @param isBlanked True if the device is being blanked.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700349 */
Robert Carrae606b42018-02-15 15:36:23 -0800350 public void configureDisplayLocked(SurfaceControl.Transaction t,
351 DisplayDevice device,
Jeff Brown8ec09432012-10-16 16:19:23 -0700352 boolean isBlanked) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700353 // Set the layer stack.
Robert Carrae606b42018-02-15 15:36:23 -0800354 device.setLayerStackLocked(t, isBlanked ? BLANK_LAYER_STACK : mLayerStack);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700355
Michael Wright1c9977b2016-07-12 13:30:10 -0700356 // Set the color mode and mode.
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700357 if (device == mPrimaryDisplayDevice) {
Robert Carrae606b42018-02-15 15:36:23 -0800358 device.requestDisplayModesLocked(
Michael Wright1c9977b2016-07-12 13:30:10 -0700359 mRequestedColorMode, mRequestedModeId);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700360 } else {
Robert Carrae606b42018-02-15 15:36:23 -0800361 device.requestDisplayModesLocked(0, 0); // Revert to default.
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700362 }
363
364 // Only grab the display info now as it may have been changed based on the requests above.
365 final DisplayInfo displayInfo = getDisplayInfoLocked();
366 final DisplayDeviceInfo displayDeviceInfo = device.getDisplayDeviceInfoLocked();
Michael Wright3f145a22014-07-22 19:46:03 -0700367
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700368 // Set the viewport.
369 // This is the area of the logical display that we intend to show on the
370 // display device. For now, it is always the full size of the logical display.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700371 mTempLayerStackRect.set(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700372
373 // Set the orientation.
374 // The orientation specifies how the physical coordinate system of the display
375 // is rotated when the contents of the logical display are rendered.
376 int orientation = Surface.ROTATION_0;
Scott Anderson8786ed92013-11-01 13:27:39 -0700377 if ((displayDeviceInfo.flags & DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT) != 0) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700378 orientation = displayInfo.rotation;
379 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700380
Jeff Brown27f1d672012-10-17 18:32:34 -0700381 // Apply the physical rotation of the display device itself.
382 orientation = (orientation + displayDeviceInfo.rotation) % 4;
383
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700384 // Set the frame.
385 // The frame specifies the rotated physical coordinates into which the viewport
386 // is mapped. We need to take care to preserve the aspect ratio of the viewport.
387 // Currently we maximize the area to fill the display, but we could try to be
388 // more clever and match resolutions.
389 boolean rotated = (orientation == Surface.ROTATION_90
390 || orientation == Surface.ROTATION_270);
391 int physWidth = rotated ? displayDeviceInfo.height : displayDeviceInfo.width;
392 int physHeight = rotated ? displayDeviceInfo.width : displayDeviceInfo.height;
393
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200394 Rect maskingInsets = getMaskingInsets(displayDeviceInfo);
395 InsetUtils.rotateInsets(maskingInsets, orientation);
396 // Don't consider the masked area as available when calculating the scaling below.
397 physWidth -= maskingInsets.left + maskingInsets.right;
398 physHeight -= maskingInsets.top + maskingInsets.bottom;
399
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700400 // Determine whether the width or height is more constrained to be scaled.
401 // physWidth / displayInfo.logicalWidth => letter box
402 // or physHeight / displayInfo.logicalHeight => pillar box
403 //
404 // We avoid a division (and possible floating point imprecision) here by
405 // multiplying the fractions by the product of their denominators before
406 // comparing them.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700407 int displayRectWidth, displayRectHeight;
Sam Lin4c3ac2b2019-02-18 04:50:26 -0800408 if ((displayInfo.flags & Display.FLAG_SCALING_DISABLED) != 0 || mDisplayScalingDisabled) {
Jeff Brownd46747a2015-04-15 19:02:36 -0700409 displayRectWidth = displayInfo.logicalWidth;
410 displayRectHeight = displayInfo.logicalHeight;
411 } else if (physWidth * displayInfo.logicalHeight
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700412 < physHeight * displayInfo.logicalWidth) {
413 // Letter box.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700414 displayRectWidth = physWidth;
415 displayRectHeight = displayInfo.logicalHeight * physWidth / displayInfo.logicalWidth;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700416 } else {
417 // Pillar box.
Mathias Agopian63f1c432012-09-04 19:29:13 -0700418 displayRectWidth = displayInfo.logicalWidth * physHeight / displayInfo.logicalHeight;
419 displayRectHeight = physHeight;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700420 }
Mathias Agopian63f1c432012-09-04 19:29:13 -0700421 int displayRectTop = (physHeight - displayRectHeight) / 2;
422 int displayRectLeft = (physWidth - displayRectWidth) / 2;
423 mTempDisplayRect.set(displayRectLeft, displayRectTop,
424 displayRectLeft + displayRectWidth, displayRectTop + displayRectHeight);
425
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200426 // Now add back the offset for the masked area.
427 mTempDisplayRect.offset(maskingInsets.left, maskingInsets.top);
428
Adrian Roos595416b2018-10-14 14:50:33 +0200429 if (orientation == Surface.ROTATION_0) {
430 mTempDisplayRect.offset(mDisplayOffsetX, mDisplayOffsetY);
431 } else if (orientation == Surface.ROTATION_90) {
432 mTempDisplayRect.offset(mDisplayOffsetY, -mDisplayOffsetX);
433 } else if (orientation == Surface.ROTATION_180) {
434 mTempDisplayRect.offset(-mDisplayOffsetX, -mDisplayOffsetY);
435 } else { // Surface.ROTATION_270
436 mTempDisplayRect.offset(-mDisplayOffsetY, mDisplayOffsetX);
437 }
Robert Carrae606b42018-02-15 15:36:23 -0800438 device.setProjectionLocked(t, orientation, mTempLayerStackRect, mTempDisplayRect);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700439 }
440
Craig Mautner722285e2012-09-07 13:55:58 -0700441 /**
442 * Returns true if the logical display has unique content.
443 * <p>
444 * If the display has unique content then we will try to ensure that it is
445 * visible on at least its primary display device. Otherwise we will ignore the
446 * logical display and perhaps show mirrored content on the primary display device.
447 * </p>
448 *
449 * @return True if the display has unique content.
450 */
451 public boolean hasContentLocked() {
452 return mHasContent;
453 }
454
455 /**
456 * Sets whether the logical display has unique content.
457 *
458 * @param hasContent True if the display has unique content.
459 */
460 public void setHasContentLocked(boolean hasContent) {
461 mHasContent = hasContent;
462 }
463
Michael Wright3f145a22014-07-22 19:46:03 -0700464 /**
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700465 * Requests the given mode.
Michael Wright3f145a22014-07-22 19:46:03 -0700466 */
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700467 public void setRequestedModeIdLocked(int modeId) {
468 mRequestedModeId = modeId;
Michael Wright3f145a22014-07-22 19:46:03 -0700469 }
470
471 /**
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700472 * Returns the pending requested mode.
Michael Wright3f145a22014-07-22 19:46:03 -0700473 */
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700474 public int getRequestedModeIdLocked() {
475 return mRequestedModeId;
Michael Wright3f145a22014-07-22 19:46:03 -0700476 }
477
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800478 /**
Michael Wright1c9977b2016-07-12 13:30:10 -0700479 * Requests the given color mode.
Michael Wright58e829f2015-09-15 00:13:26 +0100480 */
Michael Wright1c9977b2016-07-12 13:30:10 -0700481 public void setRequestedColorModeLocked(int colorMode) {
482 mRequestedColorMode = colorMode;
Michael Wright58e829f2015-09-15 00:13:26 +0100483 }
484
Michael Wright1c9977b2016-07-12 13:30:10 -0700485 /** Returns the pending requested color mode. */
486 public int getRequestedColorModeLocked() {
487 return mRequestedColorMode;
Michael Wright58e829f2015-09-15 00:13:26 +0100488 }
489
490 /**
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800491 * Gets the burn-in offset in X.
492 */
493 public int getDisplayOffsetXLocked() {
494 return mDisplayOffsetX;
495 }
496
497 /**
498 * Gets the burn-in offset in Y.
499 */
500 public int getDisplayOffsetYLocked() {
501 return mDisplayOffsetY;
502 }
503
504 /**
505 * Sets the burn-in offsets.
506 */
507 public void setDisplayOffsetsLocked(int x, int y) {
508 mDisplayOffsetX = x;
509 mDisplayOffsetY = y;
510 }
511
Sam Lin4c3ac2b2019-02-18 04:50:26 -0800512 /**
513 * @return {@code true} if display scaling is disabled, or {@code false} if the default scaling
514 * mode is used.
515 */
516 public boolean isDisplayScalingDisabled() {
517 return mDisplayScalingDisabled;
518 }
519
520 /**
521 * Disables scaling for a display.
522 *
523 * @param disableScaling {@code true} to disable scaling,
524 * {@code false} to use the default scaling behavior of the logical display.
525 */
526 public void setDisplayScalingDisabledLocked(boolean disableScaling) {
527 mDisplayScalingDisabled = disableScaling;
528 }
529
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700530 public void dumpLocked(PrintWriter pw) {
Jeff Brownc2726642012-10-02 16:17:31 -0700531 pw.println("mDisplayId=" + mDisplayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700532 pw.println("mLayerStack=" + mLayerStack);
Jeff Brownc2726642012-10-02 16:17:31 -0700533 pw.println("mHasContent=" + mHasContent);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700534 pw.println("mRequestedMode=" + mRequestedModeId);
Michael Wright1c9977b2016-07-12 13:30:10 -0700535 pw.println("mRequestedColorMode=" + mRequestedColorMode);
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800536 pw.println("mDisplayOffset=(" + mDisplayOffsetX + ", " + mDisplayOffsetY + ")");
Sam Lin4c3ac2b2019-02-18 04:50:26 -0800537 pw.println("mDisplayScalingDisabled=" + mDisplayScalingDisabled);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700538 pw.println("mPrimaryDisplayDevice=" + (mPrimaryDisplayDevice != null ?
539 mPrimaryDisplayDevice.getNameLocked() : "null"));
540 pw.println("mBaseDisplayInfo=" + mBaseDisplayInfo);
541 pw.println("mOverrideDisplayInfo=" + mOverrideDisplayInfo);
542 }
Michael Wrightc39d47a2014-07-08 18:07:36 -0700543}