blob: 2a77c92b5c87df8455f38af6508300e8bf0aeed5 [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;
32import android.os.SystemClock;
33import android.os.UserHandle;
34import android.provider.Settings;
David Stevensf833ba92017-03-16 19:00:20 -070035import android.util.ArraySet;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070036import android.util.EventLog;
37import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070038import android.util.SparseIntArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -070039import android.util.proto.ProtoOutputStream;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070040import android.view.Display;
41import android.view.DisplayInfo;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070042import android.view.WindowManager;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020043
Wale Ogunwalee05f5012016-09-16 16:27:29 -070044import com.android.internal.util.ArrayUtils;
45import com.android.server.EventLogTags;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070046
47import java.io.FileDescriptor;
48import java.io.PrintWriter;
49import java.util.ArrayList;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080050import java.util.function.Consumer;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070051
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.app.AppOpsManager.MODE_ALLOWED;
53import static android.app.AppOpsManager.MODE_DEFAULT;
54import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwale02319a62016-09-26 15:21:22 -070055import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian48146fd2017-08-07 11:22:50 -070056import static android.view.Display.INVALID_DISPLAY;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070057import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070058import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070059import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE;
60import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
61import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
Jorim Jaggi879ff722016-11-04 18:08:17 -070062
Adrian Roose99bc052017-11-20 17:55:31 +010063import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
67import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
69import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_POWER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
72import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
73import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
74import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
75import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
76import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
77import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
78import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
79import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070080import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
81import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070082import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
83import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
84import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
85import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
86import static com.android.server.wm.WindowManagerService.logSurface;
87import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
88import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
89import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
90import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
91import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
92import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070093import static com.android.server.wm.proto.RootWindowContainerProto.DISPLAYS;
94import static com.android.server.wm.proto.RootWindowContainerProto.WINDOWS;
95import static com.android.server.wm.proto.RootWindowContainerProto.WINDOW_CONTAINER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070096
97/** Root {@link WindowContainer} for the device. */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -070098class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -070099 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
100
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200101 private static final int SET_SCREEN_BRIGHTNESS_OVERRIDE = 1;
102 private static final int SET_USER_ACTIVITY_TIMEOUT = 2;
103
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700104 private boolean mWallpaperForceHidingChanged = false;
105 private Object mLastWindowFreezeSource = null;
106 private Session mHoldScreen = null;
107 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700108 private long mUserActivityTimeout = -1;
109 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700110 // Following variables are for debugging screen wakelock only.
111 // Last window that requires screen wakelock
112 WindowState mHoldScreenWindow = null;
113 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700114 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700115 // Only set while traversing the default display based on its content.
116 // Affects the behavior of mirroring on secondary displays.
117 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
118
119 private boolean mSustainedPerformanceModeEnabled = false;
120 private boolean mSustainedPerformanceModeCurrent = false;
121
122 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700123 // During an orientation change, we track whether all windows have rendered
124 // at the new orientation, and this will be false from changing orientation until that occurs.
125 // For seamless rotation cases this always stays true, as the windows complete their orientation
126 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700127 boolean mOrientationChangeComplete = true;
128 boolean mWallpaperActionPending = false;
129
130 private final ArrayList<Integer> mChangedStackList = new ArrayList();
131
132 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
133 // instances will be replaced with an instance that writes a binary representation of all
134 // commands to mSurfaceTraceFd.
135 boolean mSurfaceTraceEnabled;
136 ParcelFileDescriptor mSurfaceTraceFd;
137 RemoteEventTrace mRemoteEventTrace;
138
Wale Ogunwale0303c572016-10-20 10:16:29 -0700139 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700140
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200141 private final Handler mHandler;
142
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800143 private String mCloseSystemDialogsReason;
144 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
145 if (w.mHasSurface) {
146 try {
147 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
148 } catch (RemoteException e) {
149 }
150 }
151 };
152
153 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
154 final AppWindowToken aToken = w.mAppToken;
155 if (aToken != null) {
156 aToken.removeReplacedWindowIfNeeded(w);
157 }
158 };
159
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700160 RootWindowContainer(WindowManagerService service) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100161 super(service);
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200162 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwale0303c572016-10-20 10:16:29 -0700163 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700164 }
165
166 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700167 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700168 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700169 final WindowState win = dc.findFocusedWindow();
170 if (win != null) {
171 return win;
172 }
173 }
174 return null;
175 }
176
177 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800178 * Get an array with display ids ordered by focus priority - last items should be given
179 * focus first. Sparse array just maps position to displayId.
180 */
181 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
182 displaysInFocusOrder.clear();
183
184 final int size = mChildren.size();
185 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700186 final DisplayContent displayContent = mChildren.get(i);
187 if (displayContent.isRemovalDeferred()) {
188 // Don't report displays that are going to be removed soon.
189 continue;
190 }
191 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800192 }
193 }
194
195 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700196 * Retrieve the DisplayContent for the specified displayId. Will create a new DisplayContent if
197 * there is a Display for the displayId.
198 *
199 * @param displayId The display the caller is interested in.
200 * @return The DisplayContent associated with displayId or null if there is no Display for it.
201 */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700202 DisplayContent getDisplayContentOrCreate(int displayId) {
203 DisplayContent dc = getDisplayContent(displayId);
204
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700205 if (dc == null) {
206 final Display display = mService.mDisplayManager.getDisplay(displayId);
207 if (display != null) {
Andrii Kulian325cac42017-04-20 16:02:47 -0700208 final long callingIdentity = Binder.clearCallingIdentity();
209 try {
210 dc = createDisplayContent(display);
211 } finally {
212 Binder.restoreCallingIdentity(callingIdentity);
213 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700214 }
215 }
216 return dc;
217 }
218
Wale Ogunwale02319a62016-09-26 15:21:22 -0700219 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700220 for (int i = mChildren.size() - 1; i >= 0; --i) {
221 final DisplayContent current = mChildren.get(i);
222 if (current.getDisplayId() == displayId) {
223 return current;
224 }
225 }
226 return null;
227 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700228
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700229 private DisplayContent createDisplayContent(final Display display) {
Robert Carrb1579c82017-09-05 14:54:47 -0700230 final DisplayContent dc = new DisplayContent(display, mService,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700231 mWallpaperController);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700232 final int displayId = display.getDisplayId();
233
234 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700235
236 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700237 final Rect rect = new Rect();
238 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
239 displayInfo.overscanLeft = rect.left;
240 displayInfo.overscanTop = rect.top;
241 displayInfo.overscanRight = rect.right;
242 displayInfo.overscanBottom = rect.bottom;
243 if (mService.mDisplayManagerInternal != null) {
244 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
245 displayId, displayInfo);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000246 dc.configureDisplayPolicy();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700247
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700248 // Tap Listeners are supported for:
249 // 1. All physical displays (multi-display).
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -0700250 // 2. VirtualDisplays on VR, AA (and everything else).
251 if (mService.canDispatchPointerEvents()) {
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700252 if (DEBUG_DISPLAY) {
253 Slog.d(TAG,
254 "Registering PointerEventListener for DisplayId: " + displayId);
255 }
256 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700257 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700258 if (displayId == DEFAULT_DISPLAY) {
259 mService.registerPointerEventListener(mService.mMousePositionTracker);
260 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700261 }
262 }
263
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700264 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700265 }
266
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700267 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700268 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700269 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700270 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700271 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700272 return true;
273 }
274 }
275 return false;
276 }
277
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000278 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700279 int objectId = 0;
280 // See if this is an object ID.
281 try {
282 objectId = Integer.parseInt(name, 16);
283 name = null;
284 } catch (RuntimeException e) {
285 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800286
287 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700288 }
289
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000290 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800291 forAllWindows((w) -> {
292 if (name != null) {
293 if (w.mAttrs.getTitle().toString().contains(name)) {
294 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700295 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800296 } else if (System.identityHashCode(w) == objectId) {
297 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700298 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800299 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700300 }
301
Wale Ogunwale02319a62016-09-26 15:21:22 -0700302 /**
303 * Returns the app window token for the input binder if it exist in the system.
304 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
305 * AppWindowToken represents an activity which can only exist on one display.
306 */
307 AppWindowToken getAppWindowToken(IBinder binder) {
308 for (int i = mChildren.size() - 1; i >= 0; --i) {
309 final DisplayContent dc = mChildren.get(i);
310 final AppWindowToken atoken = dc.getAppWindowToken(binder);
311 if (atoken != null) {
312 return atoken;
313 }
314 }
315 return null;
316 }
317
318 /** Returns the display object the input window token is currently mapped on. */
319 DisplayContent getWindowTokenDisplay(WindowToken token) {
320 if (token == null) {
321 return null;
322 }
323
324 for (int i = mChildren.size() - 1; i >= 0; --i) {
325 final DisplayContent dc = mChildren.get(i);
326 final WindowToken current = dc.getWindowToken(token.token);
327 if (current == token) {
328 return dc;
329 }
330 }
331
332 return null;
333 }
334
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700335 /**
336 * Set new display override config and return array of ids of stacks that were changed during
337 * update. If called for the default display, global configuration will also be updated.
338 */
339 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
340 final DisplayContent displayContent = getDisplayContent(displayId);
341 if (displayContent == null) {
342 throw new IllegalArgumentException("Display not found for id: " + displayId);
343 }
344
345 final Configuration currentConfig = displayContent.getOverrideConfiguration();
346 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
347 if (!configChanged) {
348 return null;
349 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800350 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700351
352 if (displayId == DEFAULT_DISPLAY) {
353 // Override configuration of the default display duplicates global config. In this case
354 // we also want to update the global config.
355 return setGlobalConfigurationIfNeeded(newConfiguration);
356 } else {
357 return updateStackBoundsAfterConfigChange(displayId);
358 }
359 }
360
361 private int[] setGlobalConfigurationIfNeeded(Configuration newConfiguration) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700362 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
363 if (!configChanged) {
364 return null;
365 }
366 onConfigurationChanged(newConfiguration);
367 return updateStackBoundsAfterConfigChange();
368 }
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 */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700382 private int[] updateStackBoundsAfterConfigChange() {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700383 mChangedStackList.clear();
384
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700385 final int numDisplays = mChildren.size();
386 for (int i = 0; i < numDisplays; ++i) {
387 final DisplayContent dc = mChildren.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700388 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700389 }
390
391 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
392 }
393
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700394 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
395 private int[] updateStackBoundsAfterConfigChange(int displayId) {
396 mChangedStackList.clear();
397
398 final DisplayContent dc = getDisplayContent(displayId);
399 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
400
401 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
402 }
403
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700404 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700405 for (int i = mChildren.size() - 1; i >= 0; i--) {
406 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700407 }
408 }
409
Wale Ogunwale68278562017-09-23 17:13:55 -0700410 TaskStack getStack(int windowingMode, int activityType) {
411 for (int i = mChildren.size() - 1; i >= 0; i--) {
412 final DisplayContent dc = mChildren.get(i);
413 final TaskStack stack = dc.getStack(windowingMode, activityType);
414 if (stack != null) {
415 return stack;
416 }
417 }
418 return null;
419 }
420
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700421 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800422 forAllWindows((w) -> {
423 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
424 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700425 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800426 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700427 }
428
429 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800430 forAllWindows((w) -> {
431 if (w.mAppOp == OP_NONE) {
432 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700433 }
Peter Visontay96449f62017-12-11 18:50:03 +0000434 final int mode = mService.mAppOps.noteOpNoThrow(w.mAppOp, w.getOwningUid(),
Wale Ogunwaled1880962016-11-08 10:31:59 -0800435 w.getOwningPackage());
436 w.setAppOpVisibilityLw(mode == MODE_ALLOWED || mode == MODE_DEFAULT);
437 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700438 }
439
440 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800441 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
442 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700443 }
444
445 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800446 mCloseSystemDialogsReason = reason;
447 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700448 }
449
450 void removeReplacedWindows() {
451 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
452 mService.openSurfaceTransaction();
453 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800454 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700455 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200456 mService.closeSurfaceTransaction("removeReplacedWindows");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700457 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
458 }
459 }
460
461 boolean hasPendingLayoutChanges(WindowAnimator animator) {
462 boolean hasChanges = false;
463
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700464 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700465 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700466 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700467 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
468 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
469 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
470 }
471 if (pendingChanges != 0) {
472 hasChanges = true;
473 }
474 }
475
476 return hasChanges;
477 }
478
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700479 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
480 boolean secure) {
481 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
482 boolean leakedSurface = false;
483 boolean killedApps = false;
484
485 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
486 winAnimator.mSession.mPid, operation);
487
488 final long callingIdentity = Binder.clearCallingIdentity();
489 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700490 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700491 // we haven't left any dangling surfaces around.
492
493 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700494 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700495 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700496 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700497 }
498
499 if (!leakedSurface) {
500 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700501 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700502 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800503 mChildren.get(displayNdx).forAllWindows((w) -> {
504 if (mService.mForceRemoves.contains(w)) {
505 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700506 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800507 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700508 if (wsa.mSurfaceController != null) {
509 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
510 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800511 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700512
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700513 if (pidCandidates.size() > 0) {
514 int[] pids = new int[pidCandidates.size()];
515 for (int i = 0; i < pids.length; i++) {
516 pids[i] = pidCandidates.keyAt(i);
517 }
518 try {
519 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
520 killedApps = true;
521 }
522 } catch (RemoteException e) {
523 }
524 }
525 }
526 }
527
528 if (leakedSurface || killedApps) {
529 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
530 // app to request another one.
531 Slog.w(TAG_WM,
532 "Looks like we have reclaimed some memory, clearing surface for retry.");
533 if (surfaceController != null) {
534 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
535 "RECOVER DESTROY", false);
536 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800537 if (winAnimator.mWin.mAppToken != null
538 && winAnimator.mWin.mAppToken.getController() != null) {
539 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
540 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700541 }
542
543 try {
544 winAnimator.mWin.mClient.dispatchGetNewSurface();
545 } catch (RemoteException e) {
546 }
547 }
548 } finally {
549 Binder.restoreCallingIdentity(callingIdentity);
550 }
551
552 return leakedSurface || killedApps;
553 }
554
555 // "Something has changed! Let's make it correct now."
556 // TODO: Super crazy long method that should be broken down...
557 void performSurfacePlacement(boolean recoveringMemory) {
558 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
559 + Debug.getCallers(3));
560
561 int i;
562 boolean updateInputWindowsNeeded = false;
563
564 if (mService.mFocusMayChange) {
565 mService.mFocusMayChange = false;
566 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
567 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
568 }
569
570 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700571 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700572 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700573 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800574 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700575 }
576
577 mHoldScreen = null;
578 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700579 mUserActivityTimeout = -1;
580 mObscureApplicationContentOnSecondaryDisplays = false;
581 mSustainedPerformanceModeCurrent = false;
582 mService.mTransactionSequence++;
583
584 // TODO(multi-display):
585 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
586 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
587 final int defaultDw = defaultInfo.logicalWidth;
588 final int defaultDh = defaultInfo.logicalHeight;
589
590 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
591 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
592 mService.openSurfaceTransaction();
593 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700594 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700595 } catch (RuntimeException e) {
596 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
597 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200598 mService.closeSurfaceTransaction("performLayoutAndPlaceSurfaces");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700599 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
600 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
601 }
602
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700603 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
604
605 // If we are ready to perform an app transition, check through all of the app tokens to be
606 // shown and see if they are ready to go.
607 if (mService.mAppTransition.isReady()) {
608 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700609 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700610 if (DEBUG_LAYOUT_REPEATS)
611 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
612 defaultDisplay.pendingLayoutChanges);
613 }
614
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200615 if (!isAppAnimating() && mService.mAppTransition.isRunning()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700616 // We have finished the animation of an app transition. To do this, we have delayed a
617 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
618 // token list reflects the correct Z-order, but the window list may now be out of sync
619 // with it. So here we will just rebuild the entire app window list. Fun!
620 defaultDisplay.pendingLayoutChanges |=
621 mService.handleAnimatingStoppedAndTransitionLocked();
622 if (DEBUG_LAYOUT_REPEATS)
623 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
624 defaultDisplay.pendingLayoutChanges);
625 }
626
627 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
628 && !mService.mAppTransition.isReady()) {
629 // At this point, there was a window with a wallpaper that was force hiding other
630 // windows behind it, but now it is going away. This may be simple -- just animate away
631 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
632 // shown behind something that was hidden.
633 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
634 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
635 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
636 }
637 mWallpaperForceHidingChanged = false;
638
639 if (mWallpaperMayChange) {
640 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
641 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
642 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
643 defaultDisplay.pendingLayoutChanges);
644 }
645
646 if (mService.mFocusMayChange) {
647 mService.mFocusMayChange = false;
648 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
649 false /*updateInputWindows*/)) {
650 updateInputWindowsNeeded = true;
651 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
652 }
653 }
654
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700655 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700656 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
657 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
658 defaultDisplay.pendingLayoutChanges);
659 }
660
David Stevensf833ba92017-03-16 19:00:20 -0700661 final ArraySet<DisplayContent> touchExcludeRegionUpdateDisplays = handleResizingWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700662
663 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
664 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
665 if (mOrientationChangeComplete) {
666 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
667 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
668 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
669 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
670 }
671 mService.stopFreezingDisplayLocked();
672 }
673
674 // Destroy the surface of any windows that are no longer visible.
675 boolean wallpaperDestroyed = false;
676 i = mService.mDestroySurface.size();
677 if (i > 0) {
678 do {
679 i--;
680 WindowState win = mService.mDestroySurface.get(i);
681 win.mDestroying = false;
682 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000683 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700684 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700685 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700686 wallpaperDestroyed = true;
687 }
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200688 win.destroySurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700689 } while (i > 0);
690 mService.mDestroySurface.clear();
691 }
692
693 // Time to remove any exiting tokens?
694 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700695 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800696 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700697 }
698
699 if (wallpaperDestroyed) {
700 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700701 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700702 }
703
704 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700705 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700706 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700707 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700708 }
709 }
710
711 // Finally update all input windows now that the window changes have stabilized.
712 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
713
714 mService.setHoldScreenLocked(mHoldScreen);
715 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200716 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
717 ? -1 : toBrightnessOverride(mScreenBrightness);
718
719 // Post these on a handler such that we don't call into power manager service while
720 // holding the window manager lock to avoid lock contention with power manager lock.
721 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
722 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700723 }
724
725 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
726 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
727 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700728 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700729 (mSustainedPerformanceModeEnabled ? 1 : 0));
730 }
731
732 if (mService.mTurnOnScreen) {
733 if (mService.mAllowTheaterModeWakeFromLayout
734 || Settings.Global.getInt(mService.mContext.getContentResolver(),
735 Settings.Global.THEATER_MODE_ON, 0) == 0) {
736 if (DEBUG_VISIBILITY || DEBUG_POWER) {
737 Slog.v(TAG, "Turning screen on after layout!");
738 }
739 mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
740 "android.server.wm:TURN_ON");
741 }
742 mService.mTurnOnScreen = false;
743 }
744
745 if (mUpdateRotation) {
746 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700747 // TODO(multi-display): Update rotation for different displays separately.
748 final int displayId = defaultDisplay.getDisplayId();
749 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
750 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
751 } else {
752 mUpdateRotation = false;
753 }
754 // Update rotation of VR virtual display separately. Currently this is the only kind of
755 // secondary display that can be rotated because of the single-display limitations in
756 // PhoneWindowManager.
757 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
758 ? getDisplayContent(mService.mVr2dDisplayId) : null;
759 if (vrDisplay != null && vrDisplay.updateRotationUnchecked(false /* inTransaction */)) {
760 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
761 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700762 }
763 }
764
765 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700766 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
767 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700768 mService.checkDrawnWindowsLocked();
769 }
770
771 final int N = mService.mPendingRemove.size();
772 if (N > 0) {
773 if (mService.mPendingRemoveTmp.length < N) {
774 mService.mPendingRemoveTmp = new WindowState[N+10];
775 }
776 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
777 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700778 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700779 for (i = 0; i < N; i++) {
780 final WindowState w = mService.mPendingRemoveTmp[i];
781 w.removeImmediately();
782 final DisplayContent displayContent = w.getDisplayContent();
783 if (displayContent != null && !displayList.contains(displayContent)) {
784 displayList.add(displayContent);
785 }
786 }
787
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700788 for (int j = displayList.size() - 1; j >= 0; --j) {
789 final DisplayContent dc = displayList.get(j);
790 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700791 }
792 }
793
794 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700795 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
796 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700797 }
798
799 if (updateInputWindowsNeeded) {
800 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
801 }
David Stevensee9e2772017-02-09 16:30:27 -0800802 mService.setFocusTaskRegionLocked(null);
David Stevensf833ba92017-03-16 19:00:20 -0700803 if (touchExcludeRegionUpdateDisplays != null) {
804 final DisplayContent focusedDc = mService.mFocusedApp != null
805 ? mService.mFocusedApp.getDisplayContent() : null;
806 for (DisplayContent dc : touchExcludeRegionUpdateDisplays) {
807 // The focused DisplayContent was recalcuated in setFocusTaskRegionLocked
808 if (focusedDc != dc) {
809 dc.setTouchExcludeRegion(null /* focusedTask */);
810 }
811 }
812 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700813
814 // Check to see if we are now in a state where the screen should
815 // be enabled, because the window obscured flags have changed.
816 mService.enableScreenIfNeededLocked();
817
818 mService.scheduleAnimationLocked();
819 mService.mWindowPlacerLocked.destroyPendingSurfaces();
820
821 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
822 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
823 }
824
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700825 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
826 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700827 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700828 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700829
Andrii Kulian8ee72852017-03-10 10:36:45 -0800830 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700831 if (mService.mWatermark != null) {
832 mService.mWatermark.positionSurface(defaultDw, defaultDh);
833 }
834 if (mService.mStrictModeFlash != null) {
835 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
836 }
837 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800838 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
839 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700840 }
841 if (mService.mEmulatorDisplayOverlay != null) {
842 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800843 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700844 }
845
846 boolean focusDisplayed = false;
847
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700848 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700849 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700850 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700851 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700852 }
853
854 if (focusDisplayed) {
855 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
856 }
857
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700858 // Give the display manager a chance to adjust properties like display rotation if it needs
859 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700860 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
861 }
862
863 /**
David Stevensf833ba92017-03-16 19:00:20 -0700864 * Handles resizing windows during surface placement.
865 *
866 * @return A set of any DisplayContent whose touch exclude region needs to be recalculated due
867 * to a tap-exclude window resizing, or null if no such DisplayContents were found.
868 */
869 private ArraySet<DisplayContent> handleResizingWindows() {
870 ArraySet<DisplayContent> touchExcludeRegionUpdateSet = null;
871 for (int i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
872 WindowState win = mService.mResizingWindows.get(i);
873 if (win.mAppFreezing) {
874 // Don't remove this window until rotation has completed.
875 continue;
876 }
877 win.reportResized();
878 mService.mResizingWindows.remove(i);
879 if (WindowManagerService.excludeWindowTypeFromTapOutTask(win.mAttrs.type)) {
880 final DisplayContent dc = win.getDisplayContent();
881 if (touchExcludeRegionUpdateSet == null) {
882 touchExcludeRegionUpdateSet = new ArraySet<>();
883 }
884 touchExcludeRegionUpdateSet.add(dc);
885 }
886 }
887 return touchExcludeRegionUpdateSet;
888 }
889
890 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700891 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700892 * @param obscured True if there is a window on top of this obscuring the display.
893 * @param syswin System window?
894 * @return True when the display contains content to show the user. When false, the display
895 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700896 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700897 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700898 final WindowManager.LayoutParams attrs = w.mAttrs;
899 final int attrFlags = attrs.flags;
900 final boolean canBeSeen = w.isDisplayedLw();
901 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700902 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700903
904 if (w.mHasSurface && canBeSeen) {
905 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
906 mHoldScreen = w.mSession;
907 mHoldScreenWindow = w;
908 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
909 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000910 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
911 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700912 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700913 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700914 mScreenBrightness = w.mAttrs.screenBrightness;
915 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700916 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700917 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
918 }
919
920 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700921 // This function assumes that the contents of the default display are processed first
922 // before secondary displays.
923 final DisplayContent displayContent = w.getDisplayContent();
924 if (displayContent != null && displayContent.isDefaultDisplay) {
925 // While a dream or keyguard is showing, obscure ordinary application content on
926 // secondary displays (by forcibly enabling mirroring unless there is other content
927 // we want to show) but still allow opaque keyguard dialogs to be shown.
928 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
929 mObscureApplicationContentOnSecondaryDisplays = true;
930 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700931 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700932 } else if (displayContent != null &&
933 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700934 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700935 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700936 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700937 }
938 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
939 mSustainedPerformanceModeCurrent = true;
940 }
941 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700942
943 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700944 }
945
946 boolean copyAnimToLayoutParams() {
947 boolean doRequest = false;
948
949 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
950 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
951 mUpdateRotation = true;
952 doRequest = true;
953 }
954 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
955 mWallpaperMayChange = true;
956 doRequest = true;
957 }
958 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
959 mWallpaperForceHidingChanged = true;
960 doRequest = true;
961 }
962 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
963 mOrientationChangeComplete = false;
964 } else {
965 mOrientationChangeComplete = true;
966 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
967 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
968 doRequest = true;
969 }
970 }
971 if ((bulkUpdateParams & SET_TURN_ON_SCREEN) != 0) {
972 mService.mTurnOnScreen = true;
973 }
974 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
975 mWallpaperActionPending = true;
976 }
977
978 return doRequest;
979 }
980
981 private static int toBrightnessOverride(float value) {
982 return (int)(value * PowerManager.BRIGHTNESS_ON);
983 }
984
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200985 private final class MyHandler extends Handler {
986
987 public MyHandler(Looper looper) {
988 super(looper);
989 }
990
991 @Override
992 public void handleMessage(Message msg) {
993 switch (msg.what) {
994 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
995 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
996 msg.arg1);
997 break;
998 case SET_USER_ACTIVITY_TIMEOUT:
999 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
1000 (Long) msg.obj);
1001 break;
1002 default:
1003 break;
1004 }
1005 }
1006 }
1007
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001008 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
1009 final FileDescriptor fd = pfd.getFileDescriptor();
1010 if (mSurfaceTraceEnabled) {
1011 disableSurfaceTrace();
1012 }
1013 mSurfaceTraceEnabled = true;
1014 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
1015 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001016 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1017 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001018 dc.enableSurfaceTrace(fd);
1019 }
1020 }
1021
1022 void disableSurfaceTrace() {
1023 mSurfaceTraceEnabled = false;
1024 mRemoteEventTrace = null;
1025 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001026 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1027 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001028 dc.disableSurfaceTrace();
1029 }
1030 }
1031
1032 void dumpDisplayContents(PrintWriter pw) {
1033 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1034 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001035 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001036 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001037 final DisplayContent displayContent = mChildren.get(i);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001038 displayContent.dump(pw, " ", true /* dumpAll */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001039 }
1040 } else {
1041 pw.println(" NO DISPLAY");
1042 }
1043 }
1044
1045 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001046 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001047 return;
1048 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001049 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001050 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001051 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001052 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001053 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001054 pw.print(displayContent.getDisplayId());
1055 }
1056 }
1057 pw.println();
1058 }
1059
1060 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001061 final int[] index = new int[1];
1062 forAllWindows((w) -> {
1063 if (windows == null || windows.contains(w)) {
1064 pw.println(" Window #" + index[0] + " " + w + ":");
1065 w.dump(pw, " ", dumpAll || windows != null);
1066 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001067 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001068 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001069 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001070
Wale Ogunwale02319a62016-09-26 15:21:22 -07001071 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1072 pw.println(" All tokens:");
1073 for (int i = mChildren.size() - 1; i >= 0; --i) {
1074 mChildren.get(i).dumpTokens(pw, dumpAll);
1075 }
1076 }
1077
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001078 @CallSuper
1079 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001080 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001081 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001082 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001083 if (mService.mDisplayReady) {
1084 final int count = mChildren.size();
1085 for (int i = 0; i < count; ++i) {
1086 final DisplayContent displayContent = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001087 displayContent.writeToProto(proto, DISPLAYS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001088 }
1089 }
Adrian Roos4921ccf2017-09-28 16:54:06 +02001090 if (!trim) {
1091 forAllWindows((w) -> {
1092 w.writeIdentifierToProto(proto, WINDOWS);
1093 }, true);
1094 }
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001095 proto.end(token);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001096 }
1097
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001098 @Override
1099 String getName() {
1100 return "ROOT";
1101 }
Robert Carrb1579c82017-09-05 14:54:47 -07001102
1103 @Override
1104 void scheduleAnimation() {
1105 mService.scheduleAnimationLocked();
1106 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001107}