blob: bcb6e6736d04bf38fd369fd1c79c81f0d10f7a36 [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;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070063import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi879ff722016-11-04 18:08:17 -070065
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 WindowManagerService mService;
105
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700106 private boolean mWallpaperForceHidingChanged = false;
107 private Object mLastWindowFreezeSource = null;
108 private Session mHoldScreen = null;
109 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700110 private long mUserActivityTimeout = -1;
111 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700112 // Following variables are for debugging screen wakelock only.
113 // Last window that requires screen wakelock
114 WindowState mHoldScreenWindow = null;
115 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700116 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700117 // Only set while traversing the default display based on its content.
118 // Affects the behavior of mirroring on secondary displays.
119 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
120
121 private boolean mSustainedPerformanceModeEnabled = false;
122 private boolean mSustainedPerformanceModeCurrent = false;
123
124 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700125 // During an orientation change, we track whether all windows have rendered
126 // at the new orientation, and this will be false from changing orientation until that occurs.
127 // For seamless rotation cases this always stays true, as the windows complete their orientation
128 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700129 boolean mOrientationChangeComplete = true;
130 boolean mWallpaperActionPending = false;
131
132 private final ArrayList<Integer> mChangedStackList = new ArrayList();
133
134 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
135 // instances will be replaced with an instance that writes a binary representation of all
136 // commands to mSurfaceTraceFd.
137 boolean mSurfaceTraceEnabled;
138 ParcelFileDescriptor mSurfaceTraceFd;
139 RemoteEventTrace mRemoteEventTrace;
140
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700141 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700142 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700143
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200144 private final Handler mHandler;
145
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800146 private String mCloseSystemDialogsReason;
147 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
148 if (w.mHasSurface) {
149 try {
150 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
151 } catch (RemoteException e) {
152 }
153 }
154 };
155
156 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
157 final AppWindowToken aToken = w.mAppToken;
158 if (aToken != null) {
159 aToken.removeReplacedWindowIfNeeded(w);
160 }
161 };
162
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700163 RootWindowContainer(WindowManagerService service) {
164 mService = service;
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200165 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700166 mLayersController = new WindowLayersController(mService);
Wale Ogunwale0303c572016-10-20 10:16:29 -0700167 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700168 }
169
170 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700171 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700172 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700173 final WindowState win = dc.findFocusedWindow();
174 if (win != null) {
175 return win;
176 }
177 }
178 return null;
179 }
180
181 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800182 * Get an array with display ids ordered by focus priority - last items should be given
183 * focus first. Sparse array just maps position to displayId.
184 */
185 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
186 displaysInFocusOrder.clear();
187
188 final int size = mChildren.size();
189 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700190 final DisplayContent displayContent = mChildren.get(i);
191 if (displayContent.isRemovalDeferred()) {
192 // Don't report displays that are going to be removed soon.
193 continue;
194 }
195 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800196 }
197 }
198
199 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700200 * Retrieve the DisplayContent for the specified displayId. Will create a new DisplayContent if
201 * there is a Display for the displayId.
202 *
203 * @param displayId The display the caller is interested in.
204 * @return The DisplayContent associated with displayId or null if there is no Display for it.
205 */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700206 DisplayContent getDisplayContentOrCreate(int displayId) {
207 DisplayContent dc = getDisplayContent(displayId);
208
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700209 if (dc == null) {
210 final Display display = mService.mDisplayManager.getDisplay(displayId);
211 if (display != null) {
Andrii Kulian325cac42017-04-20 16:02:47 -0700212 final long callingIdentity = Binder.clearCallingIdentity();
213 try {
214 dc = createDisplayContent(display);
215 } finally {
216 Binder.restoreCallingIdentity(callingIdentity);
217 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700218 }
219 }
220 return dc;
221 }
222
Wale Ogunwale02319a62016-09-26 15:21:22 -0700223 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700224 for (int i = mChildren.size() - 1; i >= 0; --i) {
225 final DisplayContent current = mChildren.get(i);
226 if (current.getDisplayId() == displayId) {
227 return current;
228 }
229 }
230 return null;
231 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700232
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700233 private DisplayContent createDisplayContent(final Display display) {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700234 final DisplayContent dc = new DisplayContent(display, mService, mLayersController,
235 mWallpaperController);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700236 final int displayId = display.getDisplayId();
237
238 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700239
240 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700241 final Rect rect = new Rect();
242 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
243 displayInfo.overscanLeft = rect.left;
244 displayInfo.overscanTop = rect.top;
245 displayInfo.overscanRight = rect.right;
246 displayInfo.overscanBottom = rect.bottom;
247 if (mService.mDisplayManagerInternal != null) {
248 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
249 displayId, displayInfo);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700250 mService.configureDisplayPolicyLocked(dc);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700251
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700252 // Tap Listeners are supported for:
253 // 1. All physical displays (multi-display).
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -0700254 // 2. VirtualDisplays on VR, AA (and everything else).
255 if (mService.canDispatchPointerEvents()) {
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700256 if (DEBUG_DISPLAY) {
257 Slog.d(TAG,
258 "Registering PointerEventListener for DisplayId: " + displayId);
259 }
260 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700261 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700262 if (displayId == DEFAULT_DISPLAY) {
263 mService.registerPointerEventListener(mService.mMousePositionTracker);
264 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700265 }
266 }
267
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700268 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700269 }
270
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700271 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700272 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700273 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700274 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700275 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700276 return true;
277 }
278 }
279 return false;
280 }
281
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000282 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700283 int objectId = 0;
284 // See if this is an object ID.
285 try {
286 objectId = Integer.parseInt(name, 16);
287 name = null;
288 } catch (RuntimeException e) {
289 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800290
291 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700292 }
293
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000294 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800295 forAllWindows((w) -> {
296 if (name != null) {
297 if (w.mAttrs.getTitle().toString().contains(name)) {
298 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700299 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800300 } else if (System.identityHashCode(w) == objectId) {
301 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700302 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800303 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700304 }
305
Wale Ogunwale02319a62016-09-26 15:21:22 -0700306 /**
307 * Returns the app window token for the input binder if it exist in the system.
308 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
309 * AppWindowToken represents an activity which can only exist on one display.
310 */
311 AppWindowToken getAppWindowToken(IBinder binder) {
312 for (int i = mChildren.size() - 1; i >= 0; --i) {
313 final DisplayContent dc = mChildren.get(i);
314 final AppWindowToken atoken = dc.getAppWindowToken(binder);
315 if (atoken != null) {
316 return atoken;
317 }
318 }
319 return null;
320 }
321
322 /** Returns the display object the input window token is currently mapped on. */
323 DisplayContent getWindowTokenDisplay(WindowToken token) {
324 if (token == null) {
325 return null;
326 }
327
328 for (int i = mChildren.size() - 1; i >= 0; --i) {
329 final DisplayContent dc = mChildren.get(i);
330 final WindowToken current = dc.getWindowToken(token.token);
331 if (current == token) {
332 return dc;
333 }
334 }
335
336 return null;
337 }
338
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700339 /**
340 * Set new display override config and return array of ids of stacks that were changed during
341 * update. If called for the default display, global configuration will also be updated.
342 */
343 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
344 final DisplayContent displayContent = getDisplayContent(displayId);
345 if (displayContent == null) {
346 throw new IllegalArgumentException("Display not found for id: " + displayId);
347 }
348
349 final Configuration currentConfig = displayContent.getOverrideConfiguration();
350 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
351 if (!configChanged) {
352 return null;
353 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800354 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700355
356 if (displayId == DEFAULT_DISPLAY) {
357 // Override configuration of the default display duplicates global config. In this case
358 // we also want to update the global config.
359 return setGlobalConfigurationIfNeeded(newConfiguration);
360 } else {
361 return updateStackBoundsAfterConfigChange(displayId);
362 }
363 }
364
365 private int[] setGlobalConfigurationIfNeeded(Configuration newConfiguration) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700366 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
367 if (!configChanged) {
368 return null;
369 }
370 onConfigurationChanged(newConfiguration);
371 return updateStackBoundsAfterConfigChange();
372 }
373
374 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700375 public void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700376 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700377 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700378
379 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700380 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700381
Andrii Kulian441e4492016-09-29 15:25:00 -0700382 /**
383 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
384 * bounds were updated.
385 */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700386 private int[] updateStackBoundsAfterConfigChange() {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700387 mChangedStackList.clear();
388
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700389 final int numDisplays = mChildren.size();
390 for (int i = 0; i < numDisplays; ++i) {
391 final DisplayContent dc = mChildren.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700392 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700393 }
394
395 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
396 }
397
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700398 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
399 private int[] updateStackBoundsAfterConfigChange(int displayId) {
400 mChangedStackList.clear();
401
402 final DisplayContent dc = getDisplayContent(displayId);
403 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
404
405 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
406 }
407
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700408 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700409 for (int i = mChildren.size() - 1; i >= 0; i--) {
410 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700411 }
412 }
413
Wale Ogunwale68278562017-09-23 17:13:55 -0700414 TaskStack getStack(int windowingMode, int activityType) {
415 for (int i = mChildren.size() - 1; i >= 0; i--) {
416 final DisplayContent dc = mChildren.get(i);
417 final TaskStack stack = dc.getStack(windowingMode, activityType);
418 if (stack != null) {
419 return stack;
420 }
421 }
422 return null;
423 }
424
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700425 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800426 forAllWindows((w) -> {
427 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
428 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700429 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800430 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700431 }
432
433 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800434 forAllWindows((w) -> {
435 if (w.mAppOp == OP_NONE) {
436 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700437 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800438 final int mode = mService.mAppOps.checkOpNoThrow(w.mAppOp, w.getOwningUid(),
439 w.getOwningPackage());
440 w.setAppOpVisibilityLw(mode == MODE_ALLOWED || mode == MODE_DEFAULT);
441 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700442 }
443
444 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800445 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
446 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700447 }
448
449 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800450 mCloseSystemDialogsReason = reason;
451 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700452 }
453
454 void removeReplacedWindows() {
455 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
456 mService.openSurfaceTransaction();
457 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800458 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700459 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200460 mService.closeSurfaceTransaction("removeReplacedWindows");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700461 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
462 }
463 }
464
465 boolean hasPendingLayoutChanges(WindowAnimator animator) {
466 boolean hasChanges = false;
467
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700468 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700469 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700470 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700471 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
472 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
473 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
474 }
475 if (pendingChanges != 0) {
476 hasChanges = true;
477 }
478 }
479
480 return hasChanges;
481 }
482
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700483 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
484 boolean secure) {
485 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
486 boolean leakedSurface = false;
487 boolean killedApps = false;
488
489 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
490 winAnimator.mSession.mPid, operation);
491
492 final long callingIdentity = Binder.clearCallingIdentity();
493 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700494 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700495 // we haven't left any dangling surfaces around.
496
497 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700498 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700499 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700500 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700501 }
502
503 if (!leakedSurface) {
504 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700505 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700506 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800507 mChildren.get(displayNdx).forAllWindows((w) -> {
508 if (mService.mForceRemoves.contains(w)) {
509 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700510 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800511 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700512 if (wsa.mSurfaceController != null) {
513 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
514 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800515 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700516
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700517 if (pidCandidates.size() > 0) {
518 int[] pids = new int[pidCandidates.size()];
519 for (int i = 0; i < pids.length; i++) {
520 pids[i] = pidCandidates.keyAt(i);
521 }
522 try {
523 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
524 killedApps = true;
525 }
526 } catch (RemoteException e) {
527 }
528 }
529 }
530 }
531
532 if (leakedSurface || killedApps) {
533 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
534 // app to request another one.
535 Slog.w(TAG_WM,
536 "Looks like we have reclaimed some memory, clearing surface for retry.");
537 if (surfaceController != null) {
538 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
539 "RECOVER DESTROY", false);
540 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800541 if (winAnimator.mWin.mAppToken != null
542 && winAnimator.mWin.mAppToken.getController() != null) {
543 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
544 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700545 }
546
547 try {
548 winAnimator.mWin.mClient.dispatchGetNewSurface();
549 } catch (RemoteException e) {
550 }
551 }
552 } finally {
553 Binder.restoreCallingIdentity(callingIdentity);
554 }
555
556 return leakedSurface || killedApps;
557 }
558
559 // "Something has changed! Let's make it correct now."
560 // TODO: Super crazy long method that should be broken down...
561 void performSurfacePlacement(boolean recoveringMemory) {
562 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
563 + Debug.getCallers(3));
564
565 int i;
566 boolean updateInputWindowsNeeded = false;
567
568 if (mService.mFocusMayChange) {
569 mService.mFocusMayChange = false;
570 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
571 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
572 }
573
574 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700575 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700576 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700577 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800578 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700579 }
580
581 mHoldScreen = null;
582 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700583 mUserActivityTimeout = -1;
584 mObscureApplicationContentOnSecondaryDisplays = false;
585 mSustainedPerformanceModeCurrent = false;
586 mService.mTransactionSequence++;
587
588 // TODO(multi-display):
589 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
590 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
591 final int defaultDw = defaultInfo.logicalWidth;
592 final int defaultDh = defaultInfo.logicalHeight;
593
594 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
595 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
596 mService.openSurfaceTransaction();
597 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700598 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700599 } catch (RuntimeException e) {
600 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
601 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200602 mService.closeSurfaceTransaction("performLayoutAndPlaceSurfaces");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700603 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
604 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
605 }
606
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700607 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
608
609 // If we are ready to perform an app transition, check through all of the app tokens to be
610 // shown and see if they are ready to go.
611 if (mService.mAppTransition.isReady()) {
612 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700613 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700614 if (DEBUG_LAYOUT_REPEATS)
615 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
616 defaultDisplay.pendingLayoutChanges);
617 }
618
619 if (!mService.mAnimator.mAppWindowAnimating && mService.mAppTransition.isRunning()) {
620 // We have finished the animation of an app transition. To do this, we have delayed a
621 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
622 // token list reflects the correct Z-order, but the window list may now be out of sync
623 // with it. So here we will just rebuild the entire app window list. Fun!
624 defaultDisplay.pendingLayoutChanges |=
625 mService.handleAnimatingStoppedAndTransitionLocked();
626 if (DEBUG_LAYOUT_REPEATS)
627 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
628 defaultDisplay.pendingLayoutChanges);
629 }
630
631 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
632 && !mService.mAppTransition.isReady()) {
633 // At this point, there was a window with a wallpaper that was force hiding other
634 // windows behind it, but now it is going away. This may be simple -- just animate away
635 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
636 // shown behind something that was hidden.
637 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
638 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
639 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
640 }
641 mWallpaperForceHidingChanged = false;
642
643 if (mWallpaperMayChange) {
644 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
645 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
646 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
647 defaultDisplay.pendingLayoutChanges);
648 }
649
650 if (mService.mFocusMayChange) {
651 mService.mFocusMayChange = false;
652 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
653 false /*updateInputWindows*/)) {
654 updateInputWindowsNeeded = true;
655 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
656 }
657 }
658
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700659 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700660 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
661 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
662 defaultDisplay.pendingLayoutChanges);
663 }
664
David Stevensf833ba92017-03-16 19:00:20 -0700665 final ArraySet<DisplayContent> touchExcludeRegionUpdateDisplays = handleResizingWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700666
667 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
668 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
669 if (mOrientationChangeComplete) {
670 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
671 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
672 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
673 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
674 }
675 mService.stopFreezingDisplayLocked();
676 }
677
678 // Destroy the surface of any windows that are no longer visible.
679 boolean wallpaperDestroyed = false;
680 i = mService.mDestroySurface.size();
681 if (i > 0) {
682 do {
683 i--;
684 WindowState win = mService.mDestroySurface.get(i);
685 win.mDestroying = false;
686 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000687 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700688 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700689 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700690 wallpaperDestroyed = true;
691 }
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200692 win.destroySurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700693 } while (i > 0);
694 mService.mDestroySurface.clear();
695 }
696
697 // Time to remove any exiting tokens?
698 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700699 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800700 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700701 }
702
703 if (wallpaperDestroyed) {
704 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700705 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700706 }
707
708 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700709 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700710 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700711 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700712 }
713 }
714
715 // Finally update all input windows now that the window changes have stabilized.
716 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
717
718 mService.setHoldScreenLocked(mHoldScreen);
719 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200720 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
721 ? -1 : toBrightnessOverride(mScreenBrightness);
722
723 // Post these on a handler such that we don't call into power manager service while
724 // holding the window manager lock to avoid lock contention with power manager lock.
725 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
726 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700727 }
728
729 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
730 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
731 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700732 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700733 (mSustainedPerformanceModeEnabled ? 1 : 0));
734 }
735
736 if (mService.mTurnOnScreen) {
737 if (mService.mAllowTheaterModeWakeFromLayout
738 || Settings.Global.getInt(mService.mContext.getContentResolver(),
739 Settings.Global.THEATER_MODE_ON, 0) == 0) {
740 if (DEBUG_VISIBILITY || DEBUG_POWER) {
741 Slog.v(TAG, "Turning screen on after layout!");
742 }
743 mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
744 "android.server.wm:TURN_ON");
745 }
746 mService.mTurnOnScreen = false;
747 }
748
749 if (mUpdateRotation) {
750 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700751 // TODO(multi-display): Update rotation for different displays separately.
752 final int displayId = defaultDisplay.getDisplayId();
753 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
754 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
755 } else {
756 mUpdateRotation = false;
757 }
758 // Update rotation of VR virtual display separately. Currently this is the only kind of
759 // secondary display that can be rotated because of the single-display limitations in
760 // PhoneWindowManager.
761 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
762 ? getDisplayContent(mService.mVr2dDisplayId) : null;
763 if (vrDisplay != null && vrDisplay.updateRotationUnchecked(false /* inTransaction */)) {
764 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
765 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700766 }
767 }
768
769 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700770 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
771 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700772 mService.checkDrawnWindowsLocked();
773 }
774
775 final int N = mService.mPendingRemove.size();
776 if (N > 0) {
777 if (mService.mPendingRemoveTmp.length < N) {
778 mService.mPendingRemoveTmp = new WindowState[N+10];
779 }
780 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
781 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700782 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700783 for (i = 0; i < N; i++) {
784 final WindowState w = mService.mPendingRemoveTmp[i];
785 w.removeImmediately();
786 final DisplayContent displayContent = w.getDisplayContent();
787 if (displayContent != null && !displayList.contains(displayContent)) {
788 displayList.add(displayContent);
789 }
790 }
791
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700792 for (int j = displayList.size() - 1; j >= 0; --j) {
793 final DisplayContent dc = displayList.get(j);
794 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700795 }
796 }
797
798 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700799 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
800 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700801 }
802
803 if (updateInputWindowsNeeded) {
804 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
805 }
David Stevensee9e2772017-02-09 16:30:27 -0800806 mService.setFocusTaskRegionLocked(null);
David Stevensf833ba92017-03-16 19:00:20 -0700807 if (touchExcludeRegionUpdateDisplays != null) {
808 final DisplayContent focusedDc = mService.mFocusedApp != null
809 ? mService.mFocusedApp.getDisplayContent() : null;
810 for (DisplayContent dc : touchExcludeRegionUpdateDisplays) {
811 // The focused DisplayContent was recalcuated in setFocusTaskRegionLocked
812 if (focusedDc != dc) {
813 dc.setTouchExcludeRegion(null /* focusedTask */);
814 }
815 }
816 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700817
818 // Check to see if we are now in a state where the screen should
819 // be enabled, because the window obscured flags have changed.
820 mService.enableScreenIfNeededLocked();
821
822 mService.scheduleAnimationLocked();
823 mService.mWindowPlacerLocked.destroyPendingSurfaces();
824
825 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
826 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
827 }
828
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700829 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
830 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700831 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700832 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700833
Andrii Kulian8ee72852017-03-10 10:36:45 -0800834 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700835 if (mService.mWatermark != null) {
836 mService.mWatermark.positionSurface(defaultDw, defaultDh);
837 }
838 if (mService.mStrictModeFlash != null) {
839 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
840 }
841 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800842 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
843 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700844 }
845 if (mService.mEmulatorDisplayOverlay != null) {
846 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800847 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700848 }
849
850 boolean focusDisplayed = false;
851
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700852 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700853 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700854 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700855 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700856 }
857
858 if (focusDisplayed) {
859 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
860 }
861
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700862 // Give the display manager a chance to adjust properties like display rotation if it needs
863 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700864 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
865 }
866
867 /**
David Stevensf833ba92017-03-16 19:00:20 -0700868 * Handles resizing windows during surface placement.
869 *
870 * @return A set of any DisplayContent whose touch exclude region needs to be recalculated due
871 * to a tap-exclude window resizing, or null if no such DisplayContents were found.
872 */
873 private ArraySet<DisplayContent> handleResizingWindows() {
874 ArraySet<DisplayContent> touchExcludeRegionUpdateSet = null;
875 for (int i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
876 WindowState win = mService.mResizingWindows.get(i);
877 if (win.mAppFreezing) {
878 // Don't remove this window until rotation has completed.
879 continue;
880 }
881 win.reportResized();
882 mService.mResizingWindows.remove(i);
883 if (WindowManagerService.excludeWindowTypeFromTapOutTask(win.mAttrs.type)) {
884 final DisplayContent dc = win.getDisplayContent();
885 if (touchExcludeRegionUpdateSet == null) {
886 touchExcludeRegionUpdateSet = new ArraySet<>();
887 }
888 touchExcludeRegionUpdateSet.add(dc);
889 }
890 }
891 return touchExcludeRegionUpdateSet;
892 }
893
894 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700895 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700896 * @param obscured True if there is a window on top of this obscuring the display.
897 * @param syswin System window?
898 * @return True when the display contains content to show the user. When false, the display
899 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700900 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700901 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700902 final WindowManager.LayoutParams attrs = w.mAttrs;
903 final int attrFlags = attrs.flags;
904 final boolean canBeSeen = w.isDisplayedLw();
905 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700906 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700907
908 if (w.mHasSurface && canBeSeen) {
909 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
910 mHoldScreen = w.mSession;
911 mHoldScreenWindow = w;
912 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
913 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000914 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
915 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700916 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700917 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700918 mScreenBrightness = w.mAttrs.screenBrightness;
919 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700920 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700921 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
922 }
923
924 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700925 // This function assumes that the contents of the default display are processed first
926 // before secondary displays.
927 final DisplayContent displayContent = w.getDisplayContent();
928 if (displayContent != null && displayContent.isDefaultDisplay) {
929 // While a dream or keyguard is showing, obscure ordinary application content on
930 // secondary displays (by forcibly enabling mirroring unless there is other content
931 // we want to show) but still allow opaque keyguard dialogs to be shown.
932 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
933 mObscureApplicationContentOnSecondaryDisplays = true;
934 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700935 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700936 } else if (displayContent != null &&
937 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700938 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700939 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700940 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700941 }
942 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
943 mSustainedPerformanceModeCurrent = true;
944 }
945 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700946
947 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700948 }
949
950 boolean copyAnimToLayoutParams() {
951 boolean doRequest = false;
952
953 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
954 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
955 mUpdateRotation = true;
956 doRequest = true;
957 }
958 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
959 mWallpaperMayChange = true;
960 doRequest = true;
961 }
962 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
963 mWallpaperForceHidingChanged = true;
964 doRequest = true;
965 }
966 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
967 mOrientationChangeComplete = false;
968 } else {
969 mOrientationChangeComplete = true;
970 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
971 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
972 doRequest = true;
973 }
974 }
975 if ((bulkUpdateParams & SET_TURN_ON_SCREEN) != 0) {
976 mService.mTurnOnScreen = true;
977 }
978 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
979 mWallpaperActionPending = true;
980 }
981
982 return doRequest;
983 }
984
985 private static int toBrightnessOverride(float value) {
986 return (int)(value * PowerManager.BRIGHTNESS_ON);
987 }
988
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200989 private final class MyHandler extends Handler {
990
991 public MyHandler(Looper looper) {
992 super(looper);
993 }
994
995 @Override
996 public void handleMessage(Message msg) {
997 switch (msg.what) {
998 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
999 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
1000 msg.arg1);
1001 break;
1002 case SET_USER_ACTIVITY_TIMEOUT:
1003 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
1004 (Long) msg.obj);
1005 break;
1006 default:
1007 break;
1008 }
1009 }
1010 }
1011
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001012 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
1013 final FileDescriptor fd = pfd.getFileDescriptor();
1014 if (mSurfaceTraceEnabled) {
1015 disableSurfaceTrace();
1016 }
1017 mSurfaceTraceEnabled = true;
1018 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
1019 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001020 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1021 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001022 dc.enableSurfaceTrace(fd);
1023 }
1024 }
1025
1026 void disableSurfaceTrace() {
1027 mSurfaceTraceEnabled = false;
1028 mRemoteEventTrace = null;
1029 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001030 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1031 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001032 dc.disableSurfaceTrace();
1033 }
1034 }
1035
1036 void dumpDisplayContents(PrintWriter pw) {
1037 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1038 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001039 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001040 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001041 final DisplayContent displayContent = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001042 displayContent.dump(" ", pw);
1043 }
1044 } else {
1045 pw.println(" NO DISPLAY");
1046 }
1047 }
1048
1049 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001050 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001051 return;
1052 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001053 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001054 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001055 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001056 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001057 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001058 pw.print(displayContent.getDisplayId());
1059 }
1060 }
1061 pw.println();
1062 }
1063
1064 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001065 final int[] index = new int[1];
1066 forAllWindows((w) -> {
1067 if (windows == null || windows.contains(w)) {
1068 pw.println(" Window #" + index[0] + " " + w + ":");
1069 w.dump(pw, " ", dumpAll || windows != null);
1070 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001071 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001072 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001073 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001074
Wale Ogunwale02319a62016-09-26 15:21:22 -07001075 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1076 pw.println(" All tokens:");
1077 for (int i = mChildren.size() - 1; i >= 0; --i) {
1078 mChildren.get(i).dumpTokens(pw, dumpAll);
1079 }
1080 }
1081
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001082 @CallSuper
1083 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001084 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001085 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001086 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001087 if (mService.mDisplayReady) {
1088 final int count = mChildren.size();
1089 for (int i = 0; i < count; ++i) {
1090 final DisplayContent displayContent = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001091 displayContent.writeToProto(proto, DISPLAYS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001092 }
1093 }
Adrian Roos4921ccf2017-09-28 16:54:06 +02001094 if (!trim) {
1095 forAllWindows((w) -> {
1096 w.writeIdentifierToProto(proto, WINDOWS);
1097 }, true);
1098 }
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001099 proto.end(token);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001100 }
1101
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001102 @Override
1103 String getName() {
1104 return "ROOT";
1105 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001106}