blob: 36d331da19e41b8e1450db012c7743cd283e0a84 [file] [log] [blame]
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001/*
2 * Copyright (C) 2016 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.wm;
18
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070019import android.annotation.CallSuper;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070020import android.content.res.Configuration;
21import android.graphics.Rect;
Ruchi Kandoi0d434042016-10-03 09:12:02 -070022import android.hardware.power.V1_0.PowerHint;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070023import android.os.Binder;
24import android.os.Debug;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020025import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -070026import android.os.IBinder;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020027import android.os.Looper;
28import android.os.Message;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070029import android.os.ParcelFileDescriptor;
30import android.os.PowerManager;
31import android.os.RemoteException;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070032import android.os.UserHandle;
David Stevensf833ba92017-03-16 19:00:20 -070033import android.util.ArraySet;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070034import android.util.EventLog;
35import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070036import android.util.SparseIntArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -070037import android.util.proto.ProtoOutputStream;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070038import android.view.Display;
39import android.view.DisplayInfo;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070040import android.view.WindowManager;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020041
Wale Ogunwalee05f5012016-09-16 16:27:29 -070042import com.android.internal.util.ArrayUtils;
43import com.android.server.EventLogTags;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070044
45import java.io.FileDescriptor;
46import java.io.PrintWriter;
47import java.util.ArrayList;
Bryce Leeaea60d22018-01-31 14:42:15 -080048import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080049import java.util.function.Consumer;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070050
Wale Ogunwale02319a62016-09-26 15:21:22 -070051import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian48146fd2017-08-07 11:22:50 -070052import static android.view.Display.INVALID_DISPLAY;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070053import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070054import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070055import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE;
56import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
57import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
Jorim Jaggi879ff722016-11-04 18:08:17 -070058
Adrian Roose99bc052017-11-20 17:55:31 +010059import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
60import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
61import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
63import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070064import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
65import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
67import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
68import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
69import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
70import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
71import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
72import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
73import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070074import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
75import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070076import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
77import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
78import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
79import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
80import static com.android.server.wm.WindowManagerService.logSurface;
81import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
82import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070083import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
84import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
85import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070086import static com.android.server.wm.proto.RootWindowContainerProto.DISPLAYS;
87import static com.android.server.wm.proto.RootWindowContainerProto.WINDOWS;
88import static com.android.server.wm.proto.RootWindowContainerProto.WINDOW_CONTAINER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070089
90/** Root {@link WindowContainer} for the device. */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -070091class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -070092 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
93
Jorim Jaggi86c39f92017-05-02 18:02:46 +020094 private static final int SET_SCREEN_BRIGHTNESS_OVERRIDE = 1;
95 private static final int SET_USER_ACTIVITY_TIMEOUT = 2;
96
Wale Ogunwalee05f5012016-09-16 16:27:29 -070097 private boolean mWallpaperForceHidingChanged = false;
98 private Object mLastWindowFreezeSource = null;
99 private Session mHoldScreen = null;
100 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700101 private long mUserActivityTimeout = -1;
102 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700103 // Following variables are for debugging screen wakelock only.
104 // Last window that requires screen wakelock
105 WindowState mHoldScreenWindow = null;
106 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700107 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700108 // Only set while traversing the default display based on its content.
109 // Affects the behavior of mirroring on secondary displays.
110 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
111
112 private boolean mSustainedPerformanceModeEnabled = false;
113 private boolean mSustainedPerformanceModeCurrent = false;
114
115 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700116 // During an orientation change, we track whether all windows have rendered
117 // at the new orientation, and this will be false from changing orientation until that occurs.
118 // For seamless rotation cases this always stays true, as the windows complete their orientation
119 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700120 boolean mOrientationChangeComplete = true;
121 boolean mWallpaperActionPending = false;
122
Bryce Leeaea60d22018-01-31 14:42:15 -0800123 private final ArrayList<TaskStack> mTmpStackList = new ArrayList();
124 private final ArrayList<Integer> mTmpStackIds = new ArrayList<>();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700125
126 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
127 // instances will be replaced with an instance that writes a binary representation of all
128 // commands to mSurfaceTraceFd.
129 boolean mSurfaceTraceEnabled;
130 ParcelFileDescriptor mSurfaceTraceFd;
131 RemoteEventTrace mRemoteEventTrace;
132
Wale Ogunwale0303c572016-10-20 10:16:29 -0700133 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700134
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200135 private final Handler mHandler;
136
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800137 private String mCloseSystemDialogsReason;
138 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
139 if (w.mHasSurface) {
140 try {
141 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
142 } catch (RemoteException e) {
143 }
144 }
145 };
146
147 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
148 final AppWindowToken aToken = w.mAppToken;
149 if (aToken != null) {
150 aToken.removeReplacedWindowIfNeeded(w);
151 }
152 };
153
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700154 RootWindowContainer(WindowManagerService service) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100155 super(service);
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200156 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwale0303c572016-10-20 10:16:29 -0700157 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700158 }
159
160 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700161 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700162 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700163 final WindowState win = dc.findFocusedWindow();
164 if (win != null) {
165 return win;
166 }
167 }
168 return null;
169 }
170
171 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800172 * Get an array with display ids ordered by focus priority - last items should be given
173 * focus first. Sparse array just maps position to displayId.
174 */
175 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
176 displaysInFocusOrder.clear();
177
178 final int size = mChildren.size();
179 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700180 final DisplayContent displayContent = mChildren.get(i);
181 if (displayContent.isRemovalDeferred()) {
182 // Don't report displays that are going to be removed soon.
183 continue;
184 }
185 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800186 }
187 }
188
Wale Ogunwale02319a62016-09-26 15:21:22 -0700189 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700190 for (int i = mChildren.size() - 1; i >= 0; --i) {
191 final DisplayContent current = mChildren.get(i);
192 if (current.getDisplayId() == displayId) {
193 return current;
194 }
195 }
196 return null;
197 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700198
Bryce Leef19cbe22018-02-02 15:09:21 -0800199 DisplayContent createDisplayContent(final Display display, DisplayWindowController controller) {
Bryce Lee1cddfdc2018-02-20 10:41:58 -0800200 final int displayId = display.getDisplayId();
201
202 // In select scenarios, it is possible that a DisplayContent will be created on demand
203 // rather than waiting for the controller. In this case, associate the controller and return
204 // the existing display.
205 final DisplayContent existing = getDisplayContent(displayId);
206
207 if (existing != null) {
208 existing.setController(controller);
209 return existing;
210 }
211
Bryce Leef19cbe22018-02-02 15:09:21 -0800212 final DisplayContent dc =
213 new DisplayContent(display, mService, mWallpaperController, controller);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700214
215 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700216
217 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700218 final Rect rect = new Rect();
219 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
220 displayInfo.overscanLeft = rect.left;
221 displayInfo.overscanTop = rect.top;
222 displayInfo.overscanRight = rect.right;
223 displayInfo.overscanBottom = rect.bottom;
224 if (mService.mDisplayManagerInternal != null) {
225 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
226 displayId, displayInfo);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000227 dc.configureDisplayPolicy();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700228
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700229 // Tap Listeners are supported for:
230 // 1. All physical displays (multi-display).
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -0700231 // 2. VirtualDisplays on VR, AA (and everything else).
232 if (mService.canDispatchPointerEvents()) {
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700233 if (DEBUG_DISPLAY) {
234 Slog.d(TAG,
235 "Registering PointerEventListener for DisplayId: " + displayId);
236 }
237 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700238 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700239 if (displayId == DEFAULT_DISPLAY) {
240 mService.registerPointerEventListener(mService.mMousePositionTracker);
241 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700242 }
243 }
244
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700245 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700246 }
247
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700248 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700249 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700250 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700251 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700252 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700253 return true;
254 }
255 }
256 return false;
257 }
258
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000259 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700260 int objectId = 0;
261 // See if this is an object ID.
262 try {
263 objectId = Integer.parseInt(name, 16);
264 name = null;
265 } catch (RuntimeException e) {
266 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800267
268 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700269 }
270
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000271 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800272 forAllWindows((w) -> {
273 if (name != null) {
274 if (w.mAttrs.getTitle().toString().contains(name)) {
275 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700276 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800277 } else if (System.identityHashCode(w) == objectId) {
278 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700279 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800280 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700281 }
282
Wale Ogunwale02319a62016-09-26 15:21:22 -0700283 /**
284 * Returns the app window token for the input binder if it exist in the system.
285 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
286 * AppWindowToken represents an activity which can only exist on one display.
287 */
288 AppWindowToken getAppWindowToken(IBinder binder) {
289 for (int i = mChildren.size() - 1; i >= 0; --i) {
290 final DisplayContent dc = mChildren.get(i);
291 final AppWindowToken atoken = dc.getAppWindowToken(binder);
292 if (atoken != null) {
293 return atoken;
294 }
295 }
296 return null;
297 }
298
299 /** Returns the display object the input window token is currently mapped on. */
300 DisplayContent getWindowTokenDisplay(WindowToken token) {
301 if (token == null) {
302 return null;
303 }
304
305 for (int i = mChildren.size() - 1; i >= 0; --i) {
306 final DisplayContent dc = mChildren.get(i);
307 final WindowToken current = dc.getWindowToken(token.token);
308 if (current == token) {
309 return dc;
310 }
311 }
312
313 return null;
314 }
315
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700316 /**
317 * Set new display override config and return array of ids of stacks that were changed during
Bryce Leeaea60d22018-01-31 14:42:15 -0800318 * update. If called for the default display, global configuration will also be updated. Stacks
319 * that are marked for deferred removal are excluded from the returned array.
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700320 */
321 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
322 final DisplayContent displayContent = getDisplayContent(displayId);
323 if (displayContent == null) {
324 throw new IllegalArgumentException("Display not found for id: " + displayId);
325 }
326
327 final Configuration currentConfig = displayContent.getOverrideConfiguration();
328 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
329 if (!configChanged) {
330 return null;
331 }
Bryce Leeaea60d22018-01-31 14:42:15 -0800332
Andrii Kuliand68501e2017-01-10 22:57:27 -0800333 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700334
Bryce Leeaea60d22018-01-31 14:42:15 -0800335 mTmpStackList.clear();
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700336 if (displayId == DEFAULT_DISPLAY) {
337 // Override configuration of the default display duplicates global config. In this case
338 // we also want to update the global config.
Bryce Leeaea60d22018-01-31 14:42:15 -0800339 setGlobalConfigurationIfNeeded(newConfiguration, mTmpStackList);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700340 } else {
Bryce Leeaea60d22018-01-31 14:42:15 -0800341 updateStackBoundsAfterConfigChange(displayId, mTmpStackList);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700342 }
Bryce Leeaea60d22018-01-31 14:42:15 -0800343
344 mTmpStackIds.clear();
345 final int stackCount = mTmpStackList.size();
346
347 for (int i = 0; i < stackCount; ++i) {
348 final TaskStack stack = mTmpStackList.get(i);
349
350 // We only include stacks that are not marked for removal as they do not exist outside
351 // of WindowManager at this point.
352 if (!stack.mDeferRemoval) {
353 mTmpStackIds.add(stack.mStackId);
354 }
355 }
356
357 return mTmpStackIds.isEmpty() ? null : ArrayUtils.convertToIntArray(mTmpStackIds);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700358 }
359
Bryce Leeaea60d22018-01-31 14:42:15 -0800360 private void setGlobalConfigurationIfNeeded(Configuration newConfiguration,
361 List<TaskStack> changedStacks) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700362 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
363 if (!configChanged) {
Bryce Leeaea60d22018-01-31 14:42:15 -0800364 return;
Andrii Kulian441e4492016-09-29 15:25:00 -0700365 }
366 onConfigurationChanged(newConfiguration);
Bryce Leeaea60d22018-01-31 14:42:15 -0800367 updateStackBoundsAfterConfigChange(changedStacks);
Andrii Kulian441e4492016-09-29 15:25:00 -0700368 }
369
370 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700371 public void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700372 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700373 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700374
375 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700376 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700377
Andrii Kulian441e4492016-09-29 15:25:00 -0700378 /**
379 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
380 * bounds were updated.
381 */
Bryce Leeaea60d22018-01-31 14:42:15 -0800382 private void updateStackBoundsAfterConfigChange(List<TaskStack> changedStacks) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700383 final int numDisplays = mChildren.size();
384 for (int i = 0; i < numDisplays; ++i) {
385 final DisplayContent dc = mChildren.get(i);
Bryce Leeaea60d22018-01-31 14:42:15 -0800386 dc.updateStackBoundsAfterConfigChange(changedStacks);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700387 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700388 }
389
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700390 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
Bryce Leeaea60d22018-01-31 14:42:15 -0800391 private void updateStackBoundsAfterConfigChange(int displayId, List<TaskStack> changedStacks) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700392 final DisplayContent dc = getDisplayContent(displayId);
Bryce Leeaea60d22018-01-31 14:42:15 -0800393 dc.updateStackBoundsAfterConfigChange(changedStacks);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700394 }
395
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700396 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700397 for (int i = mChildren.size() - 1; i >= 0; i--) {
398 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700399 }
400 }
401
Wale Ogunwale68278562017-09-23 17:13:55 -0700402 TaskStack getStack(int windowingMode, int activityType) {
403 for (int i = mChildren.size() - 1; i >= 0; i--) {
404 final DisplayContent dc = mChildren.get(i);
405 final TaskStack stack = dc.getStack(windowingMode, activityType);
406 if (stack != null) {
407 return stack;
408 }
409 }
410 return null;
411 }
412
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700413 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800414 forAllWindows((w) -> {
415 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
416 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700417 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800418 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700419 }
420
421 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800422 forAllWindows((w) -> {
Svet Ganovf7b47252018-02-26 11:11:27 -0800423 w.updateAppOpsState();
Wale Ogunwaled1880962016-11-08 10:31:59 -0800424 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700425 }
426
427 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800428 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
429 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700430 }
431
432 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800433 mCloseSystemDialogsReason = reason;
434 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700435 }
436
437 void removeReplacedWindows() {
438 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
439 mService.openSurfaceTransaction();
440 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800441 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700442 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200443 mService.closeSurfaceTransaction("removeReplacedWindows");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700444 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
445 }
446 }
447
448 boolean hasPendingLayoutChanges(WindowAnimator animator) {
449 boolean hasChanges = false;
450
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700451 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700452 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700453 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700454 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
455 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
456 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
457 }
458 if (pendingChanges != 0) {
459 hasChanges = true;
460 }
461 }
462
463 return hasChanges;
464 }
465
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700466 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
467 boolean secure) {
468 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
469 boolean leakedSurface = false;
470 boolean killedApps = false;
471
472 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
473 winAnimator.mSession.mPid, operation);
474
475 final long callingIdentity = Binder.clearCallingIdentity();
476 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700477 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700478 // we haven't left any dangling surfaces around.
479
480 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700481 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700482 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700483 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700484 }
485
486 if (!leakedSurface) {
487 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700488 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700489 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800490 mChildren.get(displayNdx).forAllWindows((w) -> {
491 if (mService.mForceRemoves.contains(w)) {
492 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700493 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800494 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700495 if (wsa.mSurfaceController != null) {
496 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
497 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800498 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700499
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700500 if (pidCandidates.size() > 0) {
501 int[] pids = new int[pidCandidates.size()];
502 for (int i = 0; i < pids.length; i++) {
503 pids[i] = pidCandidates.keyAt(i);
504 }
505 try {
506 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
507 killedApps = true;
508 }
509 } catch (RemoteException e) {
510 }
511 }
512 }
513 }
514
515 if (leakedSurface || killedApps) {
516 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
517 // app to request another one.
518 Slog.w(TAG_WM,
519 "Looks like we have reclaimed some memory, clearing surface for retry.");
520 if (surfaceController != null) {
521 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
522 "RECOVER DESTROY", false);
523 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800524 if (winAnimator.mWin.mAppToken != null
525 && winAnimator.mWin.mAppToken.getController() != null) {
526 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
527 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700528 }
529
530 try {
531 winAnimator.mWin.mClient.dispatchGetNewSurface();
532 } catch (RemoteException e) {
533 }
534 }
535 } finally {
536 Binder.restoreCallingIdentity(callingIdentity);
537 }
538
539 return leakedSurface || killedApps;
540 }
541
542 // "Something has changed! Let's make it correct now."
543 // TODO: Super crazy long method that should be broken down...
544 void performSurfacePlacement(boolean recoveringMemory) {
545 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
546 + Debug.getCallers(3));
547
548 int i;
549 boolean updateInputWindowsNeeded = false;
550
551 if (mService.mFocusMayChange) {
552 mService.mFocusMayChange = false;
553 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
554 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
555 }
556
557 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700558 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700559 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700560 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800561 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700562 }
563
564 mHoldScreen = null;
565 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700566 mUserActivityTimeout = -1;
567 mObscureApplicationContentOnSecondaryDisplays = false;
568 mSustainedPerformanceModeCurrent = false;
569 mService.mTransactionSequence++;
570
571 // TODO(multi-display):
572 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
573 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
574 final int defaultDw = defaultInfo.logicalWidth;
575 final int defaultDh = defaultInfo.logicalHeight;
576
577 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
578 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
579 mService.openSurfaceTransaction();
580 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700581 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700582 } catch (RuntimeException e) {
583 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
584 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200585 mService.closeSurfaceTransaction("performLayoutAndPlaceSurfaces");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700586 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
587 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
588 }
589
Chavi Weingarten16d0d072018-02-12 23:50:28 +0000590 mService.mAnimator.executeAfterPrepareSurfacesRunnables();
591
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700592 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
593
594 // If we are ready to perform an app transition, check through all of the app tokens to be
595 // shown and see if they are ready to go.
596 if (mService.mAppTransition.isReady()) {
Jorim Jaggi82c17862018-02-21 17:50:18 +0100597 // This needs to be split into two expressions, as handleAppTransitionReadyLocked may
598 // modify dc.pendingLayoutChanges, which would get lost when writing
599 // defaultDisplay.pendingLayoutChanges |= handleAppTransitionReadyLocked()
600 final int layoutChanges = surfacePlacer.handleAppTransitionReadyLocked();
601 defaultDisplay.pendingLayoutChanges |= layoutChanges;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700602 if (DEBUG_LAYOUT_REPEATS)
603 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
604 defaultDisplay.pendingLayoutChanges);
605 }
606
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200607 if (!isAppAnimating() && mService.mAppTransition.isRunning()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700608 // We have finished the animation of an app transition. To do this, we have delayed a
609 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
610 // token list reflects the correct Z-order, but the window list may now be out of sync
611 // with it. So here we will just rebuild the entire app window list. Fun!
612 defaultDisplay.pendingLayoutChanges |=
613 mService.handleAnimatingStoppedAndTransitionLocked();
614 if (DEBUG_LAYOUT_REPEATS)
615 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
616 defaultDisplay.pendingLayoutChanges);
617 }
618
Winson Chunge2d72172018-01-25 17:46:20 +0000619 // Defer starting the recents animation until the wallpaper has drawn
620 final RecentsAnimationController recentsAnimationController =
621 mService.getRecentsAnimationController();
622 if (recentsAnimationController != null) {
623 recentsAnimationController.checkAnimationReady(mWallpaperController);
624 }
625
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700626 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
627 && !mService.mAppTransition.isReady()) {
628 // At this point, there was a window with a wallpaper that was force hiding other
629 // windows behind it, but now it is going away. This may be simple -- just animate away
630 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
631 // shown behind something that was hidden.
632 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
633 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
634 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
635 }
636 mWallpaperForceHidingChanged = false;
637
638 if (mWallpaperMayChange) {
639 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
640 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
641 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
642 defaultDisplay.pendingLayoutChanges);
643 }
644
645 if (mService.mFocusMayChange) {
646 mService.mFocusMayChange = false;
647 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
648 false /*updateInputWindows*/)) {
649 updateInputWindowsNeeded = true;
650 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
651 }
652 }
653
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700654 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700655 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
656 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
657 defaultDisplay.pendingLayoutChanges);
658 }
659
David Stevensf833ba92017-03-16 19:00:20 -0700660 final ArraySet<DisplayContent> touchExcludeRegionUpdateDisplays = handleResizingWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700661
662 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
663 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
664 if (mOrientationChangeComplete) {
665 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
666 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
667 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
668 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
669 }
670 mService.stopFreezingDisplayLocked();
671 }
672
673 // Destroy the surface of any windows that are no longer visible.
674 boolean wallpaperDestroyed = false;
675 i = mService.mDestroySurface.size();
676 if (i > 0) {
677 do {
678 i--;
679 WindowState win = mService.mDestroySurface.get(i);
680 win.mDestroying = false;
681 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000682 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700683 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700684 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700685 wallpaperDestroyed = true;
686 }
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200687 win.destroySurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700688 } while (i > 0);
689 mService.mDestroySurface.clear();
690 }
691
692 // Time to remove any exiting tokens?
693 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700694 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800695 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700696 }
697
698 if (wallpaperDestroyed) {
699 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700700 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700701 }
702
703 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700704 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700705 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700706 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700707 }
708 }
709
710 // Finally update all input windows now that the window changes have stabilized.
711 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
712
713 mService.setHoldScreenLocked(mHoldScreen);
714 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200715 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
716 ? -1 : toBrightnessOverride(mScreenBrightness);
717
718 // Post these on a handler such that we don't call into power manager service while
719 // holding the window manager lock to avoid lock contention with power manager lock.
720 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
721 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700722 }
723
724 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
725 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
726 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700727 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700728 (mSustainedPerformanceModeEnabled ? 1 : 0));
729 }
730
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700731 if (mUpdateRotation) {
732 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700733 // TODO(multi-display): Update rotation for different displays separately.
734 final int displayId = defaultDisplay.getDisplayId();
735 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
736 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
737 } else {
738 mUpdateRotation = false;
739 }
740 // Update rotation of VR virtual display separately. Currently this is the only kind of
741 // secondary display that can be rotated because of the single-display limitations in
742 // PhoneWindowManager.
743 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
744 ? getDisplayContent(mService.mVr2dDisplayId) : null;
745 if (vrDisplay != null && vrDisplay.updateRotationUnchecked(false /* inTransaction */)) {
746 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
747 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700748 }
749 }
750
751 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700752 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
753 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700754 mService.checkDrawnWindowsLocked();
755 }
756
757 final int N = mService.mPendingRemove.size();
758 if (N > 0) {
759 if (mService.mPendingRemoveTmp.length < N) {
760 mService.mPendingRemoveTmp = new WindowState[N+10];
761 }
762 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
763 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700764 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700765 for (i = 0; i < N; i++) {
766 final WindowState w = mService.mPendingRemoveTmp[i];
767 w.removeImmediately();
768 final DisplayContent displayContent = w.getDisplayContent();
769 if (displayContent != null && !displayList.contains(displayContent)) {
770 displayList.add(displayContent);
771 }
772 }
773
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700774 for (int j = displayList.size() - 1; j >= 0; --j) {
775 final DisplayContent dc = displayList.get(j);
776 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700777 }
778 }
779
780 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700781 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
782 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700783 }
784
785 if (updateInputWindowsNeeded) {
786 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
787 }
David Stevensee9e2772017-02-09 16:30:27 -0800788 mService.setFocusTaskRegionLocked(null);
David Stevensf833ba92017-03-16 19:00:20 -0700789 if (touchExcludeRegionUpdateDisplays != null) {
790 final DisplayContent focusedDc = mService.mFocusedApp != null
791 ? mService.mFocusedApp.getDisplayContent() : null;
792 for (DisplayContent dc : touchExcludeRegionUpdateDisplays) {
793 // The focused DisplayContent was recalcuated in setFocusTaskRegionLocked
794 if (focusedDc != dc) {
795 dc.setTouchExcludeRegion(null /* focusedTask */);
796 }
797 }
798 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700799
800 // Check to see if we are now in a state where the screen should
801 // be enabled, because the window obscured flags have changed.
802 mService.enableScreenIfNeededLocked();
803
804 mService.scheduleAnimationLocked();
Chavi Weingarten38804382018-02-15 21:00:15 +0000805 mService.mWindowPlacerLocked.destroyPendingSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700806
807 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
808 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
809 }
810
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700811 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
812 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700813 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700814 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700815
Andrii Kulian8ee72852017-03-10 10:36:45 -0800816 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700817 if (mService.mWatermark != null) {
818 mService.mWatermark.positionSurface(defaultDw, defaultDh);
819 }
820 if (mService.mStrictModeFlash != null) {
821 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
822 }
823 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800824 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
825 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700826 }
827 if (mService.mEmulatorDisplayOverlay != null) {
828 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800829 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700830 }
831
832 boolean focusDisplayed = false;
833
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700834 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700835 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700836 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700837 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700838 }
839
840 if (focusDisplayed) {
841 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
842 }
843
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700844 // Give the display manager a chance to adjust properties like display rotation if it needs
845 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700846 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
847 }
848
849 /**
David Stevensf833ba92017-03-16 19:00:20 -0700850 * Handles resizing windows during surface placement.
851 *
852 * @return A set of any DisplayContent whose touch exclude region needs to be recalculated due
853 * to a tap-exclude window resizing, or null if no such DisplayContents were found.
854 */
855 private ArraySet<DisplayContent> handleResizingWindows() {
856 ArraySet<DisplayContent> touchExcludeRegionUpdateSet = null;
857 for (int i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
858 WindowState win = mService.mResizingWindows.get(i);
859 if (win.mAppFreezing) {
860 // Don't remove this window until rotation has completed.
861 continue;
862 }
863 win.reportResized();
864 mService.mResizingWindows.remove(i);
865 if (WindowManagerService.excludeWindowTypeFromTapOutTask(win.mAttrs.type)) {
866 final DisplayContent dc = win.getDisplayContent();
867 if (touchExcludeRegionUpdateSet == null) {
868 touchExcludeRegionUpdateSet = new ArraySet<>();
869 }
870 touchExcludeRegionUpdateSet.add(dc);
871 }
872 }
873 return touchExcludeRegionUpdateSet;
874 }
875
876 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700877 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700878 * @param obscured True if there is a window on top of this obscuring the display.
879 * @param syswin System window?
880 * @return True when the display contains content to show the user. When false, the display
881 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700882 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700883 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700884 final WindowManager.LayoutParams attrs = w.mAttrs;
885 final int attrFlags = attrs.flags;
Aaron Whyte8cbdf042018-01-23 12:06:02 -0800886 final boolean onScreen = w.isOnScreen();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700887 final boolean canBeSeen = w.isDisplayedLw();
888 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700889 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700890
Aaron Whyte8cbdf042018-01-23 12:06:02 -0800891 if (DEBUG_KEEP_SCREEN_ON) {
892 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked w: " + w
893 + ", w.mHasSurface: " + w.mHasSurface
894 + ", w.isOnScreen(): " + onScreen
895 + ", w.isDisplayedLw(): " + w.isDisplayedLw()
896 + ", w.mAttrs.userActivityTimeout: " + w.mAttrs.userActivityTimeout);
897 }
898 if (w.mHasSurface && onScreen) {
899 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
900 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
901 if (DEBUG_KEEP_SCREEN_ON) {
902 Slog.d(TAG, "mUserActivityTimeout set to " + mUserActivityTimeout);
903 }
904 }
905 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700906 if (w.mHasSurface && canBeSeen) {
907 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
908 mHoldScreen = w.mSession;
909 mHoldScreenWindow = w;
910 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
911 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000912 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
913 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700914 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700915 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700916 mScreenBrightness = w.mAttrs.screenBrightness;
917 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700918
919 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700920 // This function assumes that the contents of the default display are processed first
921 // before secondary displays.
922 final DisplayContent displayContent = w.getDisplayContent();
923 if (displayContent != null && displayContent.isDefaultDisplay) {
924 // While a dream or keyguard is showing, obscure ordinary application content on
925 // secondary displays (by forcibly enabling mirroring unless there is other content
926 // we want to show) but still allow opaque keyguard dialogs to be shown.
927 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
928 mObscureApplicationContentOnSecondaryDisplays = true;
929 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700930 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700931 } else if (displayContent != null &&
932 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700933 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700934 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700935 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700936 }
937 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
938 mSustainedPerformanceModeCurrent = true;
939 }
940 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700941
942 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700943 }
944
945 boolean copyAnimToLayoutParams() {
946 boolean doRequest = false;
947
948 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
949 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
950 mUpdateRotation = true;
951 doRequest = true;
952 }
953 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
954 mWallpaperMayChange = true;
955 doRequest = true;
956 }
957 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
958 mWallpaperForceHidingChanged = true;
959 doRequest = true;
960 }
961 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
962 mOrientationChangeComplete = false;
963 } else {
964 mOrientationChangeComplete = true;
965 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
966 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
967 doRequest = true;
968 }
969 }
chaviw042059d2018-01-11 11:24:08 -0800970
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700971 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
972 mWallpaperActionPending = true;
973 }
974
975 return doRequest;
976 }
977
978 private static int toBrightnessOverride(float value) {
979 return (int)(value * PowerManager.BRIGHTNESS_ON);
980 }
981
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200982 private final class MyHandler extends Handler {
983
984 public MyHandler(Looper looper) {
985 super(looper);
986 }
987
988 @Override
989 public void handleMessage(Message msg) {
990 switch (msg.what) {
991 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
992 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
993 msg.arg1);
994 break;
995 case SET_USER_ACTIVITY_TIMEOUT:
996 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
997 (Long) msg.obj);
998 break;
999 default:
1000 break;
1001 }
1002 }
1003 }
1004
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001005 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
1006 final FileDescriptor fd = pfd.getFileDescriptor();
1007 if (mSurfaceTraceEnabled) {
1008 disableSurfaceTrace();
1009 }
1010 mSurfaceTraceEnabled = true;
1011 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
1012 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001013 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1014 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001015 dc.enableSurfaceTrace(fd);
1016 }
1017 }
1018
1019 void disableSurfaceTrace() {
1020 mSurfaceTraceEnabled = false;
1021 mRemoteEventTrace = null;
1022 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001023 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1024 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001025 dc.disableSurfaceTrace();
1026 }
1027 }
1028
1029 void dumpDisplayContents(PrintWriter pw) {
1030 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1031 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001032 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001033 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001034 final DisplayContent displayContent = mChildren.get(i);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001035 displayContent.dump(pw, " ", true /* dumpAll */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001036 }
1037 } else {
1038 pw.println(" NO DISPLAY");
1039 }
1040 }
1041
1042 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001043 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001044 return;
1045 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001046 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001047 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001048 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001049 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001050 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001051 pw.print(displayContent.getDisplayId());
1052 }
1053 }
1054 pw.println();
1055 }
1056
1057 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001058 final int[] index = new int[1];
1059 forAllWindows((w) -> {
1060 if (windows == null || windows.contains(w)) {
1061 pw.println(" Window #" + index[0] + " " + w + ":");
1062 w.dump(pw, " ", dumpAll || windows != null);
1063 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001064 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001065 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001066 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001067
Wale Ogunwale02319a62016-09-26 15:21:22 -07001068 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1069 pw.println(" All tokens:");
1070 for (int i = mChildren.size() - 1; i >= 0; --i) {
1071 mChildren.get(i).dumpTokens(pw, dumpAll);
1072 }
1073 }
1074
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001075 @CallSuper
1076 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001077 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001078 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001079 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001080 if (mService.mDisplayReady) {
1081 final int count = mChildren.size();
1082 for (int i = 0; i < count; ++i) {
1083 final DisplayContent displayContent = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001084 displayContent.writeToProto(proto, DISPLAYS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001085 }
1086 }
Adrian Roos4921ccf2017-09-28 16:54:06 +02001087 if (!trim) {
1088 forAllWindows((w) -> {
1089 w.writeIdentifierToProto(proto, WINDOWS);
1090 }, true);
1091 }
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001092 proto.end(token);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001093 }
1094
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001095 @Override
1096 String getName() {
1097 return "ROOT";
1098 }
Robert Carrb1579c82017-09-05 14:54:47 -07001099
1100 @Override
1101 void scheduleAnimation() {
1102 mService.scheduleAnimationLocked();
1103 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001104}