blob: c570cf1d949f79164d2e22fc7ad7276eb7a1f285 [file] [log] [blame]
Craig Mautnerc2c0a612014-02-20 20:25:41 -08001/*
2 * Copyright (C) 2014 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 */
Craig Mautnera2c77052012-03-26 12:14:43 -070016
17package com.android.server.wm;
18
Lucas Dupin13f4b8a2020-02-19 13:41:52 -080019import static android.graphics.Matrix.MSCALE_X;
20import static android.graphics.Matrix.MSCALE_Y;
21import static android.graphics.Matrix.MSKEW_X;
22import static android.graphics.Matrix.MSKEW_Y;
23import static android.graphics.Matrix.MTRANS_X;
24import static android.graphics.Matrix.MTRANS_Y;
Wale Ogunwalee7bf46b2015-09-30 09:19:28 -070025import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080026import static android.view.WindowManager.LayoutParams.FLAG_SCALED;
Robert Carr132c9f52017-07-31 17:02:30 -070027import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070028import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
lumarkce596d32019-06-12 16:58:35 +080029import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Jorim Jaggieb88d832016-04-13 20:17:43 -070030import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Robert Carrfbbde852016-10-18 11:02:28 -070031import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Jorim Jaggi0a1523d2018-04-19 17:48:38 +020032import static android.view.WindowManager.TRANSIT_NONE;
chaviw9c81e632018-07-31 11:17:52 -070033
Jorim Jaggia5e10572017-11-15 14:36:26 +010034import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
35import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Adrian Roosb125e0b2019-10-02 14:55:14 +020036import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_DRAW;
37import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION;
38import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_STARTING_WINDOW;
39import static com.android.server.wm.ProtoLogGroup.WM_SHOW_SURFACE_ALLOC;
40import static com.android.server.wm.ProtoLogGroup.WM_SHOW_TRANSACTIONS;
lumark9bca6b42019-10-17 18:35:22 +080041import static com.android.server.wm.WindowContainer.AnimationFlags.PARENTS;
42import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION;
Adam Pardyl8c2d19c2019-09-16 17:15:38 +020043import static com.android.server.wm.WindowManagerDebugConfig.DEBUG;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080044import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080045import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020046import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW_VERBOSE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080047import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -080048import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_CROP;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080049import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080050import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
51import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070052import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -080053import static com.android.server.wm.WindowManagerService.logWithStack;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070054import static com.android.server.wm.WindowStateAnimatorProto.DRAW_STATE;
55import static com.android.server.wm.WindowStateAnimatorProto.LAST_CLIP_RECT;
56import static com.android.server.wm.WindowStateAnimatorProto.SURFACE;
57import static com.android.server.wm.WindowStateAnimatorProto.SYSTEM_DECOR_RECT;
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +020058import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070059
Evan Roskye067ddf2020-04-24 18:28:25 -070060import android.app.WindowConfiguration;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070061import android.content.Context;
62import android.graphics.Matrix;
63import android.graphics.PixelFormat;
Craig Mautner7358fbf2012-04-12 21:06:33 -070064import android.graphics.Point;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070065import android.graphics.Rect;
Craig Mautner48ba1e72012-04-02 13:18:16 -070066import android.graphics.Region;
Craig Mautnera51a9562012-04-17 17:05:26 -070067import android.os.Debug;
Chong Zhang8784be62016-06-28 15:25:07 -070068import android.os.Trace;
Craig Mautnera2c77052012-03-26 12:14:43 -070069import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070070import android.util.proto.ProtoOutputStream;
Craig Mautner59c00972012-07-30 12:10:24 -070071import android.view.DisplayInfo;
Igor Murashkina86ab6402013-08-30 12:58:36 -070072import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080073import android.view.SurfaceControl;
Craig Mautnera2c77052012-03-26 12:14:43 -070074import android.view.WindowManager;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070075import android.view.WindowManager.LayoutParams;
Craig Mautnera2c77052012-03-26 12:14:43 -070076import android.view.animation.Animation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070077import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070078
Adrian Roose99bc052017-11-20 17:55:31 +010079import com.android.server.policy.WindowManagerPolicy;
Adrian Roosb125e0b2019-10-02 14:55:14 +020080import com.android.server.protolog.common.ProtoLog;
Adrian Roose99bc052017-11-20 17:55:31 +010081
Jorim Jaggia5e10572017-11-15 14:36:26 +010082import java.io.PrintWriter;
Craig Mautnera2c77052012-03-26 12:14:43 -070083
84/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070085 * Keep track of animations and surface operations for a single WindowState.
86 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070087class WindowStateAnimator {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080088 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowStateAnimator" : TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070089 static final int WINDOW_FREEZE_LAYER = TYPE_LAYER_MULTIPLIER * 200;
Tiger Huang9c8ee262019-02-19 20:44:27 +080090 static final int PRESERVED_SURFACE_LAYER = 1;
Craig Mautnera2c77052012-03-26 12:14:43 -070091
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +010092 /**
93 * Mode how the window gets clipped by the stack bounds during an animation: The clipping should
94 * be applied after applying the animation transformation, i.e. the stack bounds don't move
95 * during the animation.
96 */
97 static final int STACK_CLIP_AFTER_ANIM = 0;
98
99 /**
100 * Mode how the window gets clipped by the stack bounds: The clipping should be applied before
101 * applying the animation transformation, i.e. the stack bounds move with the window.
102 */
103 static final int STACK_CLIP_BEFORE_ANIM = 1;
104
105 /**
106 * Mode how window gets clipped by the stack bounds during an animation: Don't clip the window
107 * by the stack bounds.
108 */
109 static final int STACK_CLIP_NONE = 2;
110
Craig Mautner918b53b2012-07-09 14:15:54 -0700111 // Unchanging local convenience fields.
Craig Mautnera2c77052012-03-26 12:14:43 -0700112 final WindowManagerService mService;
113 final WindowState mWin;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700114 final WindowAnimator mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700115 final Session mSession;
116 final WindowManagerPolicy mPolicy;
117 final Context mContext;
Craig Mautner918b53b2012-07-09 14:15:54 -0700118 final boolean mIsWallpaper;
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700119 private final WallpaperController mWallpaperControllerLocked;
Craig Mautnera2c77052012-03-26 12:14:43 -0700120
Craig Mautnera2c77052012-03-26 12:14:43 -0700121 boolean mAnimationIsEntrance;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700122
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700123 /**
124 * Set when we have changed the size of the surface, to know that
125 * we must tell them application to resize (and thus redraw itself).
126 */
127 boolean mSurfaceResized;
Chong Zhang5b2f1992015-11-13 15:40:36 -0800128 /**
129 * Whether we should inform the client on next relayoutWindow that
130 * the surface has been resized since last time.
131 */
132 boolean mReportSurfaceResized;
Robert Carre6a83512015-11-03 16:09:21 -0800133 WindowSurfaceController mSurfaceController;
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800134 private WindowSurfaceController mPendingDestroySurface;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700135
136 /**
137 * Set if the client has asked that the destroy of its surface be delayed
138 * until it explicitly says it is okay.
139 */
140 boolean mSurfaceDestroyDeferred;
141
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800142 private boolean mDestroyPreservedSurfaceUponRedraw;
Craig Mautner7d8df392012-04-06 15:26:23 -0700143 float mShownAlpha = 0;
144 float mAlpha = 0;
145 float mLastAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700146
Winson Chung399f6202014-03-19 10:47:20 -0700147 Rect mTmpClipRect = new Rect();
148 Rect mLastClipRect = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100149 Rect mLastFinalClipRect = new Rect();
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -0700150 Rect mTmpStackBounds = new Rect();
Winson Chung08f81892017-03-02 15:40:51 -0800151 private Rect mTmpAnimatingBounds = new Rect();
152 private Rect mTmpSourceBounds = new Rect();
Winson Chung399f6202014-03-19 10:47:20 -0700153
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -0800154 /**
155 * This is rectangle of the window's surface that is not covered by
156 * system decorations.
157 */
158 private final Rect mSystemDecorRect = new Rect();
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -0800159
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700160 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
Robert Carr0edf18f2017-02-21 20:01:47 -0800161 private float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700162
163 boolean mHaveMatrix;
164
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700165 // Set to true if, when the window gets displayed, it should perform
166 // an enter animation.
167 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700168
Craig Mautner9c795042014-10-28 19:59:59 -0700169 /** Used to indicate that this window is undergoing an enter animation. Used for system
170 * windows to make the callback to View.dispatchOnWindowShownCallback(). Set when the
171 * window is first added or shown, cleared when the callback has been made. */
172 boolean mEnteringAnimation;
173
Vishnu Nair33197392019-08-30 10:29:37 -0700174 private final SurfaceControl.Transaction mTmpTransaction;
Chavi Weingartenb736e322018-02-23 00:27:54 +0000175
John Reck80181b92015-05-19 11:09:32 -0700176 /** The pixel format of the underlying SurfaceControl */
177 int mSurfaceFormat;
178
Craig Mautner749a7bb2012-04-02 13:49:53 -0700179 /** This is set when there is no Surface */
180 static final int NO_SURFACE = 0;
181 /** This is set after the Surface has been created but before the window has been drawn. During
182 * this time the surface is hidden. */
183 static final int DRAW_PENDING = 1;
184 /** This is set after the window has finished drawing for the first time but before its surface
185 * is shown. The surface will be displayed when the next layout is run. */
186 static final int COMMIT_DRAW_PENDING = 2;
187 /** This is set during the time after the window's drawing has been committed, and before its
188 * surface is actually shown. It is used to delay showing the surface until all windows in a
189 * token are ready to be shown. */
190 static final int READY_TO_SHOW = 3;
191 /** Set when the window has been shown in the screen the first time. */
192 static final int HAS_DRAWN = 4;
Adrian Roos3eeb4e62014-05-19 12:43:26 +0200193
Craig Mautner276a6eb2014-11-04 15:32:57 -0800194 String drawStateToString() {
195 switch (mDrawState) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700196 case NO_SURFACE: return "NO_SURFACE";
197 case DRAW_PENDING: return "DRAW_PENDING";
198 case COMMIT_DRAW_PENDING: return "COMMIT_DRAW_PENDING";
199 case READY_TO_SHOW: return "READY_TO_SHOW";
200 case HAS_DRAWN: return "HAS_DRAWN";
Craig Mautner276a6eb2014-11-04 15:32:57 -0800201 default: return Integer.toString(mDrawState);
Craig Mautner6fbda632012-07-03 09:26:39 -0700202 }
203 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700204 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700205
Craig Mautner749a7bb2012-04-02 13:49:53 -0700206 /** Was this window last hidden? */
207 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700208
Craig Mautnerbec53f72012-04-05 11:49:05 -0700209 int mAttrType;
210
Robert Carrc7294602016-05-13 11:32:05 -0700211 boolean mForceScaleUntilResize;
212
Robert Carr04092112016-06-02 12:56:12 -0700213 // WindowState.mHScale and WindowState.mVScale contain the
214 // scale according to client specified layout parameters (e.g.
215 // one layout size, with another surface size, creates such scaling).
216 // Here we track an additional scaling factor used to follow stack
217 // scaling (as in the case of the Pinned stack animation).
218 float mExtraHScale = (float) 1.0;
219 float mExtraVScale = (float) 1.0;
220
Robert Carr217e7cc2018-01-31 18:08:39 -0800221 // An offset in pixel of the surface contents from the window position. Used for Wallpaper
222 // to provide the effect of scrolling within a large surface. We just use these values as
223 // a cache.
224 int mXOffset = 0;
225 int mYOffset = 0;
226
Lucas Dupin13f4b8a2020-02-19 13:41:52 -0800227 // A scale factor for the surface contents, that will be applied from the center of the visible
228 // region.
229 float mWallpaperScale = 1f;
230
chaviwbe43ac82018-04-04 15:14:49 -0700231 /**
232 * A flag to determine if the WSA needs to offset its position to compensate for the stack's
233 * position update before the WSA surface has resized.
234 */
235 private boolean mOffsetPositionForStackResize;
236
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800237 private final Rect mTmpSize = new Rect();
238
Vishnu Nairf7645aa2019-06-18 11:14:01 -0700239 /**
240 * Handles surface changes synchronized to after the client has drawn the surface. This
241 * transaction is currently used to reparent the old surface children to the new surface once
242 * the client has completed drawing to the new surface.
243 * This transaction is also used to merge transactions parceled in by the client. The client
244 * uses the transaction to update the relative z of its children from the old parent surface
245 * to the new parent surface once window manager reparents its children.
246 */
247 private final SurfaceControl.Transaction mPostDrawTransaction =
248 new SurfaceControl.Transaction();
Robert Carrca4c5a62018-02-05 16:07:55 -0800249
Robert Carr7b3d11d2018-03-15 14:34:45 -0700250 // Used to track whether we have called detach children on the way to invisibility, in which
251 // case we need to give the client a new Surface if it lays back out to a visible state.
252 boolean mChildrenDetached = false;
253
Robert Carr2025bf842018-03-19 13:25:05 -0700254 // Set to true after the first frame of the Pinned stack animation
255 // and reset after the last to ensure we only reset mForceScaleUntilResize
256 // once per animation.
257 boolean mPipAnimationStarted = false;
258
chaviwbe43ac82018-04-04 15:14:49 -0700259 private final Point mTmpPos = new Point();
260
Craig Mautnerc431e892015-02-11 13:14:26 -0800261 WindowStateAnimator(final WindowState win) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800262 final WindowManagerService service = win.mWmService;
Craig Mautner918b53b2012-07-09 14:15:54 -0700263
Craig Mautnera2c77052012-03-26 12:14:43 -0700264 mService = service;
Vishnu Nair33197392019-08-30 10:29:37 -0700265 mTmpTransaction = service.mTransactionFactory.get();
Craig Mautner918b53b2012-07-09 14:15:54 -0700266 mAnimator = service.mAnimator;
267 mPolicy = service.mPolicy;
268 mContext = service.mContext;
Craig Mautner918b53b2012-07-09 14:15:54 -0700269
270 mWin = win;
Craig Mautner918b53b2012-07-09 14:15:54 -0700271 mSession = win.mSession;
Craig Mautner918b53b2012-07-09 14:15:54 -0700272 mAttrType = win.mAttrs.type;
273 mIsWallpaper = win.mIsWallpaper;
wilsonshihc32538e2018-11-07 17:27:34 +0800274 mWallpaperControllerLocked = win.getDisplayContent().mWallpaperController;
Craig Mautnera2c77052012-03-26 12:14:43 -0700275 }
276
Jorim Jaggia5e10572017-11-15 14:36:26 +0100277 void onAnimationFinished() {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700278 // Done animating, clean up.
279 if (DEBUG_ANIM) Slog.v(
Jorim Jaggia5e10572017-11-15 14:36:26 +0100280 TAG, "Animation done in " + this + ": exiting=" + mWin.mAnimatingExit
281 + ", reportedVisible="
Garfield Tane8d84ab2019-10-11 09:49:40 -0700282 + (mWin.mActivityRecord != null ? mWin.mActivityRecord.reportedVisible : false));
Craig Mautnera2c77052012-03-26 12:14:43 -0700283
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800284 mWin.checkPolicyVisibilityChange();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100285 final DisplayContent displayContent = mWin.getDisplayContent();
wilsonshihe8321942019-10-18 18:39:46 +0800286 if ((mAttrType == LayoutParams.TYPE_STATUS_BAR
287 || mAttrType == LayoutParams.TYPE_NOTIFICATION_SHADE) && mWin.isVisibleByPolicy()) {
Craig Mautner81defc72013-10-29 11:10:42 -0700288 // Upon completion of a not-visible to visible status bar animation a relayout is
289 // required.
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800290 displayContent.setLayoutNeeded();
Craig Mautnera2c77052012-03-26 12:14:43 -0700291 }
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700292 mWin.onExitAnimationDone();
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800293 displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100294 if (displayContent.mWallpaperController.isWallpaperTarget(mWin)) {
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800295 displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100296 }
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800297 if (DEBUG_LAYOUT_REPEATS) {
Filip Gruszczynski4501d232015-09-02 13:00:02 -0700298 mService.mWindowPlacerLocked.debugLayoutRepeats(
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800299 "WindowStateAnimator", displayContent.pendingLayoutChanges);
300 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700301
Garfield Tane8d84ab2019-10-11 09:49:40 -0700302 if (mWin.mActivityRecord != null) {
303 mWin.mActivityRecord.updateReportedVisibilityLocked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700304 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700305 }
306
Chavi Weingartenb736e322018-02-23 00:27:54 +0000307 void hide(SurfaceControl.Transaction transaction, String reason) {
Craig Mautner0afddcb2012-05-08 15:38:00 -0700308 if (!mLastHidden) {
309 //dump();
310 mLastHidden = true;
Jorim Jaggi50575902018-04-10 17:49:30 +0200311
312 // We may have a preserved surface which we no longer need. If there was a quick
313 // VISIBLE, GONE, VISIBLE, GONE sequence, the surface may never draw, so we don't mark
314 // it to be destroyed in prepareSurfaceLocked.
315 markPreservedSurfaceForDestroy();
316
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800317 if (mSurfaceController != null) {
Chavi Weingartenb736e322018-02-23 00:27:54 +0000318 mSurfaceController.hide(transaction, reason);
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800319 }
Craig Mautner0afddcb2012-05-08 15:38:00 -0700320 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700321 }
322
Chavi Weingartenb736e322018-02-23 00:27:54 +0000323 void hide(String reason) {
324 hide(mTmpTransaction, reason);
325 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
326 }
327
Vishnu Nairf7645aa2019-06-18 11:14:01 -0700328 boolean finishDrawingLocked(SurfaceControl.Transaction postDrawTransaction) {
Craig Mautner42d04db2014-11-06 12:13:23 -0800329 final boolean startingWindow =
330 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200331 if (startingWindow) {
332 ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Finishing drawing window %s: mDrawState=%s",
333 mWin, drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700334 }
Robert Carr1ca6a332016-04-11 18:00:43 -0700335
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200336 boolean layoutNeeded = false;
Chong Zhang92147042016-05-09 12:47:11 -0700337
Craig Mautner749a7bb2012-04-02 13:49:53 -0700338 if (mDrawState == DRAW_PENDING) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200339 ProtoLog.v(WM_DEBUG_DRAW,
340 "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING %s in %s", mWin,
341 mSurfaceController);
342 if (startingWindow) {
343 ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Draw state now committed in %s", mWin);
Craig Mautner6fbda632012-07-03 09:26:39 -0700344 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700345 mDrawState = COMMIT_DRAW_PENDING;
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700346 layoutNeeded = true;
Vishnu Nair8cb00ae2019-08-02 15:20:29 -0700347
348 if (postDrawTransaction != null) {
349 mPostDrawTransaction.merge(postDrawTransaction);
350 }
351 } else if (postDrawTransaction != null) {
352 // If draw state is not pending we may delay applying this transaction from the client,
353 // so apply it now.
354 postDrawTransaction.apply();
Vishnu Nairf7645aa2019-06-18 11:14:01 -0700355 }
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700356
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700357 return layoutNeeded;
Craig Mautnera608b882012-03-30 13:03:49 -0700358 }
359
360 // This must be called while inside a transaction.
Craig Mautnerc431e892015-02-11 13:14:26 -0800361 boolean commitFinishDrawingLocked() {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200362 if (DEBUG_STARTING_WINDOW_VERBOSE &&
Craig Mautner6fbda632012-07-03 09:26:39 -0700363 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
364 Slog.i(TAG, "commitFinishDrawingLocked: " + mWin + " cur mDrawState="
Craig Mautner276a6eb2014-11-04 15:32:57 -0800365 + drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700366 }
Craig Mautner276a6eb2014-11-04 15:32:57 -0800367 if (mDrawState != COMMIT_DRAW_PENDING && mDrawState != READY_TO_SHOW) {
Craig Mautnera608b882012-03-30 13:03:49 -0700368 return false;
369 }
Robert Carre13b58e2017-08-31 14:50:44 -0700370 if (DEBUG_ANIM) {
Robert Carre6a83512015-11-03 16:09:21 -0800371 Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurfaceController);
Craig Mautner6fbda632012-07-03 09:26:39 -0700372 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700373 mDrawState = READY_TO_SHOW;
Chong Zhang0275e392015-09-17 10:41:44 -0700374 boolean result = false;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700375 final ActivityRecord activity = mWin.mActivityRecord;
376 if (activity == null || activity.canShowWindows()
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200377 || mWin.mAttrs.type == TYPE_APPLICATION_STARTING) {
Wale Ogunwale9d147902016-07-16 11:58:55 -0700378 result = mWin.performShowLocked();
Chong Zhang0275e392015-09-17 10:41:44 -0700379 }
Chong Zhang97782b42015-10-07 16:01:23 -0700380 return result;
Craig Mautnera608b882012-03-30 13:03:49 -0700381 }
382
Chong Zhang97782b42015-10-07 16:01:23 -0700383 void preserveSurfaceLocked() {
384 if (mDestroyPreservedSurfaceUponRedraw) {
Chong Zhangb9b0fec2016-02-11 18:51:51 -0800385 // This could happen when switching the surface mode very fast. For example,
386 // we preserved a surface when dragResizing changed to true. Then before the
387 // preserved surface is removed, dragResizing changed to false again.
388 // In this case, we need to leave the preserved surface alone, and destroy
389 // the actual surface, so that the createSurface call could create a surface
390 // of the proper size. The preserved surface will still be removed when client
391 // finishes drawing to the new surface.
392 mSurfaceDestroyDeferred = false;
Vishnu Nair7c861472019-07-25 09:57:12 -0700393
394 // Make sure to reparent any children of the new surface back to the preserved
395 // surface before destroying it.
396 if (mSurfaceController != null && mPendingDestroySurface != null) {
Rob Carr698b6842020-03-06 15:33:31 -0800397 mPostDrawTransaction.reparentChildren(
398 mSurfaceController.getClientViewRootSurface(),
399 mPendingDestroySurface.mSurfaceControl).apply();
Vishnu Nair7c861472019-07-25 09:57:12 -0700400 }
Chong Zhangb9b0fec2016-02-11 18:51:51 -0800401 destroySurfaceLocked();
402 mSurfaceDestroyDeferred = true;
Chong Zhang97782b42015-10-07 16:01:23 -0700403 return;
404 }
Adrian Roosb125e0b2019-10-02 14:55:14 +0200405 ProtoLog.i(WM_SHOW_TRANSACTIONS, "SURFACE SET FREEZE LAYER: %s", mWin);
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800406 if (mSurfaceController != null) {
Robert Carrb1579c82017-09-05 14:54:47 -0700407 // Our SurfaceControl is always at layer 0 within the parent Surface managed by
408 // window-state. We want this old Surface to stay on top of the new one
Tiger Huang9c8ee262019-02-19 20:44:27 +0800409 // until we do the swap, so we place it at a positive layer.
410 mSurfaceController.mSurfaceControl.setLayer(PRESERVED_SURFACE_LAYER);
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800411 }
Chong Zhang97782b42015-10-07 16:01:23 -0700412 mDestroyPreservedSurfaceUponRedraw = true;
413 mSurfaceDestroyDeferred = true;
414 destroySurfaceLocked();
415 }
416
417 void destroyPreservedSurfaceLocked() {
418 if (!mDestroyPreservedSurfaceUponRedraw) {
419 return;
420 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800421 if (mSurfaceController != null) {
422 if (mPendingDestroySurface != null) {
423 // If we are preserving a surface but we aren't relaunching that means
424 // we are just doing an in-place switch. In that case any SurfaceFlinger side
425 // child layers need to be reparented to the new surface to make this
426 // transparent to the app.
Garfield Tane8d84ab2019-10-11 09:49:40 -0700427 if (mWin.mActivityRecord == null || mWin.mActivityRecord.isRelaunching() == false) {
Rob Carr698b6842020-03-06 15:33:31 -0800428 mPostDrawTransaction.reparentChildren(
429 mPendingDestroySurface.getClientViewRootSurface(),
430 mSurfaceController.mSurfaceControl).apply();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800431 }
432 }
433 }
434
Chong Zhang97782b42015-10-07 16:01:23 -0700435 destroyDeferredSurfaceLocked();
436 mDestroyPreservedSurfaceUponRedraw = false;
437 }
438
Chong Zhangeb665572016-05-09 18:28:27 -0700439 void markPreservedSurfaceForDestroy() {
440 if (mDestroyPreservedSurfaceUponRedraw
441 && !mService.mDestroyPreservedSurface.contains(mWin)) {
442 mService.mDestroyPreservedSurface.add(mWin);
443 }
444 }
445
Robert Carrecc06b32017-04-18 14:25:10 -0700446 void resetDrawState() {
447 mDrawState = DRAW_PENDING;
448
Garfield Tane8d84ab2019-10-11 09:49:40 -0700449 if (mWin.mActivityRecord == null) {
Robert Carrecc06b32017-04-18 14:25:10 -0700450 return;
451 }
452
lumark9bca6b42019-10-17 18:35:22 +0800453 if (!mWin.mActivityRecord.isAnimating(TRANSITION)) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700454 mWin.mActivityRecord.clearAllDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -0700455 }
456 }
457
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500458 WindowSurfaceController createSurfaceLocked(int windowType, int ownerUid) {
Alan Viveretteccb11e12014-07-08 16:04:02 -0700459 final WindowState w = mWin;
Chong Zhanga8975bd2016-01-28 17:13:47 -0800460
Wale Ogunwale722ff892016-02-18 13:37:55 -0800461 if (mSurfaceController != null) {
462 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700463 }
Robert Carr7b3d11d2018-03-15 14:34:45 -0700464 mChildrenDetached = false;
Wale Ogunwale722ff892016-02-18 13:37:55 -0800465
Robert Carr132c9f52017-07-31 17:02:30 -0700466 if ((mWin.mAttrs.privateFlags & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY) != 0) {
467 windowType = SurfaceControl.WINDOW_TYPE_DONT_SCREENSHOT;
468 }
469
Wale Ogunwale722ff892016-02-18 13:37:55 -0800470 w.setHasSurface(false);
471
Adrian Roosb125e0b2019-10-02 14:55:14 +0200472 if (DEBUG_ANIM) {
473 Slog.i(TAG, "createSurface " + this + ": mDrawState=DRAW_PENDING");
474 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800475
Robert Carrecc06b32017-04-18 14:25:10 -0700476 resetDrawState();
Wale Ogunwale722ff892016-02-18 13:37:55 -0800477
478 mService.makeWindowFreezingScreenIfNeededLocked(w);
479
480 int flags = SurfaceControl.HIDDEN;
481 final WindowManager.LayoutParams attrs = w.mAttrs;
482
483 if (mService.isSecureLocked(w)) {
484 flags |= SurfaceControl.SECURE;
485 }
486
chaviw9c81e632018-07-31 11:17:52 -0700487 calculateSurfaceBounds(w, attrs, mTmpSize);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800488 final int width = mTmpSize.width();
489 final int height = mTmpSize.height();
490
491 if (DEBUG_VISIBILITY) {
492 Slog.v(TAG, "Creating surface in session "
493 + mSession.mSurfaceSession + " window " + this
494 + " w=" + width + " h=" + height
495 + " x=" + mTmpSize.left + " y=" + mTmpSize.top
496 + " format=" + attrs.format + " flags=" + flags);
497 }
498
499 // We may abort, so initialize to defaults.
Wale Ogunwale722ff892016-02-18 13:37:55 -0800500 mLastClipRect.set(0, 0, 0, 0);
501
502 // Set up surface control with initial size.
503 try {
504
505 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
506 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
507 if (!PixelFormat.formatHasAlpha(attrs.format)
508 // Don't make surface with surfaceInsets opaque as they display a
509 // translucent shadow.
510 && attrs.surfaceInsets.left == 0
511 && attrs.surfaceInsets.top == 0
512 && attrs.surfaceInsets.right == 0
513 && attrs.surfaceInsets.bottom == 0
514 // Don't make surface opaque when resizing to reduce the amount of
515 // artifacts shown in areas the app isn't drawing content to.
516 && !w.isDragResizing()) {
517 flags |= SurfaceControl.OPAQUE;
518 }
519
chaviw619da692019-06-10 15:39:40 -0700520 mSurfaceController = new WindowSurfaceController(attrs.getTitle().toString(), width,
521 height, format, flags, this, windowType, ownerUid);
Peiyong Lin75045382019-03-04 19:22:33 -0800522 mSurfaceController.setColorSpaceAgnostic((attrs.privateFlags
523 & WindowManager.LayoutParams.PRIVATE_FLAG_COLOR_SPACE_AGNOSTIC) != 0);
chaviwbe43ac82018-04-04 15:14:49 -0700524
525 setOffsetPositionForStackResize(false);
Robert Carr486bbb72017-05-30 11:25:22 -0700526 mSurfaceFormat = format;
Wale Ogunwale722ff892016-02-18 13:37:55 -0800527
528 w.setHasSurface(true);
529
Adrian Roosb125e0b2019-10-02 14:55:14 +0200530 ProtoLog.i(WM_SHOW_SURFACE_ALLOC,
531 " CREATE SURFACE %s IN SESSION %s: pid=%d format=%d flags=0x%x / %s",
532 mSurfaceController, mSession.mSurfaceSession, mSession.mPid, attrs.format,
533 flags, this);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800534 } catch (OutOfResourcesException e) {
535 Slog.w(TAG, "OutOfResourcesException creating surface");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700536 mService.mRoot.reclaimSomeSurfaceMemory(this, "create", true);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800537 mDrawState = NO_SURFACE;
538 return null;
539 } catch (Exception e) {
Robert Carrb0f39362018-03-14 13:52:25 -0700540 Slog.e(TAG, "Exception creating surface (parent dead?)", e);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800541 mDrawState = NO_SURFACE;
542 return null;
543 }
544
Adam Pardyl8c2d19c2019-09-16 17:15:38 +0200545 if (DEBUG) {
546 Slog.v(TAG, "Got surface: " + mSurfaceController
547 + ", set left=" + w.getFrameLw().left + " top=" + w.getFrameLw().top);
548 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800549
550 if (SHOW_LIGHT_TRANSACTIONS) {
551 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
552 WindowManagerService.logSurface(w, "CREATE pos=("
chaviw492139a2018-07-16 16:07:35 -0700553 + w.getFrameLw().left + "," + w.getFrameLw().top + ") ("
Jorim Jaggi35d328a2018-08-14 17:00:20 +0200554 + width + "x" + height + ")" + " HIDE", false);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800555 }
556
Wale Ogunwale722ff892016-02-18 13:37:55 -0800557 mLastHidden = true;
558
Adam Pardyl8c2d19c2019-09-16 17:15:38 +0200559 if (DEBUG) Slog.v(TAG, "Created surface " + this);
Robert Carre6a83512015-11-03 16:09:21 -0800560 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700561 }
562
chaviw9c81e632018-07-31 11:17:52 -0700563 private void calculateSurfaceBounds(WindowState w, LayoutParams attrs, Rect outSize) {
564 outSize.setEmpty();
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800565 if ((attrs.flags & FLAG_SCALED) != 0) {
566 // For a scaled surface, we always want the requested size.
chaviw9c81e632018-07-31 11:17:52 -0700567 outSize.right = w.mRequestedWidth;
568 outSize.bottom = w.mRequestedHeight;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800569 } else {
570 // When we're doing a drag-resizing, request a surface that's fullscreen size,
571 // so that we don't need to reallocate during the process. This also prevents
572 // buffer drops due to size mismatch.
573 if (w.isDragResizing()) {
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800574 final DisplayInfo displayInfo = w.getDisplayInfo();
chaviw9c81e632018-07-31 11:17:52 -0700575 outSize.right = displayInfo.logicalWidth;
576 outSize.bottom = displayInfo.logicalHeight;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800577 } else {
chaviw9c81e632018-07-31 11:17:52 -0700578 w.getCompatFrameSize(outSize);
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800579 }
580 }
581
582 // Something is wrong and SurfaceFlinger will not like this, try to revert to sane values.
Filip Gruszczynskie6ed1952015-12-21 09:34:16 -0800583 // This doesn't necessarily mean that there is an error in the system. The sizes might be
584 // incorrect, because it is before the first layout or draw.
chaviw9c81e632018-07-31 11:17:52 -0700585 if (outSize.width() < 1) {
586 outSize.right = 1;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800587 }
chaviw9c81e632018-07-31 11:17:52 -0700588 if (outSize.height() < 1) {
589 outSize.bottom = 1;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800590 }
591
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800592 // Adjust for surface insets.
chaviw9c81e632018-07-31 11:17:52 -0700593 outSize.inset(-attrs.surfaceInsets.left, -attrs.surfaceInsets.top,
594 -attrs.surfaceInsets.right, -attrs.surfaceInsets.bottom);
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800595 }
596
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800597 boolean hasSurface() {
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200598 return mSurfaceController != null && mSurfaceController.hasSurface();
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800599 }
600
Craig Mautner96868332012-12-04 14:29:11 -0800601 void destroySurfaceLocked() {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700602 final ActivityRecord activity = mWin.mActivityRecord;
603 if (activity != null) {
604 if (mWin == activity.startingWindow) {
605 activity.startingDisplayed = false;
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700606 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700607 }
608
Wale Ogunwale722ff892016-02-18 13:37:55 -0800609 if (mSurfaceController == null) {
610 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700611 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800612
Wale Ogunwale722ff892016-02-18 13:37:55 -0800613 // When destroying a surface we want to make sure child windows are hidden. If we are
614 // preserving the surface until redraw though we intend to swap it out with another surface
615 // for resizing. In this case the window always remains visible to the user and the child
616 // windows should likewise remain visible.
Wale Ogunwale9d147902016-07-16 11:58:55 -0700617 if (!mDestroyPreservedSurfaceUponRedraw) {
618 mWin.mHidden = true;
Wale Ogunwale722ff892016-02-18 13:37:55 -0800619 }
620
621 try {
622 if (DEBUG_VISIBILITY) logWithStack(TAG, "Window " + this + " destroying surface "
623 + mSurfaceController + ", session " + mSession);
624 if (mSurfaceDestroyDeferred) {
625 if (mSurfaceController != null && mPendingDestroySurface != mSurfaceController) {
626 if (mPendingDestroySurface != null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200627 ProtoLog.i(WM_SHOW_SURFACE_ALLOC, "SURFACE DESTROY PENDING: %s. %s",
628 mWin, new RuntimeException().fillInStackTrace());
Robert Carra8828862018-02-05 16:17:36 -0800629 mPendingDestroySurface.destroyNotInTransaction();
Wale Ogunwale722ff892016-02-18 13:37:55 -0800630 }
631 mPendingDestroySurface = mSurfaceController;
632 }
633 } else {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200634 ProtoLog.i(WM_SHOW_SURFACE_ALLOC, "SURFACE DESTROY: %s. %s",
635 mWin, new RuntimeException().fillInStackTrace());
Wale Ogunwale722ff892016-02-18 13:37:55 -0800636 destroySurface();
637 }
638 // Don't hide wallpaper if we're deferring the surface destroy
639 // because of a surface change.
640 if (!mDestroyPreservedSurfaceUponRedraw) {
641 mWallpaperControllerLocked.hideWallpapers(mWin);
642 }
643 } catch (RuntimeException e) {
644 Slog.w(TAG, "Exception thrown when destroying Window " + this
645 + " surface " + mSurfaceController + " session " + mSession + ": " + e.toString());
646 }
647
648 // Whether the surface was preserved (and copied to mPendingDestroySurface) or not, it
649 // needs to be cleared to match the WindowState.mHasSurface state. It is also necessary
650 // so it can be recreated successfully in mPendingDestroySurface case.
651 mWin.setHasSurface(false);
652 if (mSurfaceController != null) {
653 mSurfaceController.setShown(false);
654 }
655 mSurfaceController = null;
656 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700657 }
658
Craig Mautner96868332012-12-04 14:29:11 -0800659 void destroyDeferredSurfaceLocked() {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700660 try {
661 if (mPendingDestroySurface != null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200662 ProtoLog.i(WM_SHOW_SURFACE_ALLOC, "SURFACE DESTROY PENDING: %s. %s",
663 mWin, new RuntimeException().fillInStackTrace());
Robert Carra8828862018-02-05 16:17:36 -0800664 mPendingDestroySurface.destroyNotInTransaction();
Chong Zhang6e21cf42015-11-09 14:41:42 -0800665 // Don't hide wallpaper if we're destroying a deferred surface
666 // after a surface mode change.
667 if (!mDestroyPreservedSurfaceUponRedraw) {
668 mWallpaperControllerLocked.hideWallpapers(mWin);
669 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700670 }
671 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700672 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700673 + this + " surface " + mPendingDestroySurface
674 + " session " + mSession + ": " + e.toString());
675 }
676 mSurfaceDestroyDeferred = false;
677 mPendingDestroySurface = null;
678 }
679
680 void computeShownFrameLocked() {
Craig Mautnera91f9e22012-09-14 16:22:08 -0700681 final ScreenRotationAnimation screenRotationAnimation =
Vadim Caenb3715832019-08-13 17:06:38 +0200682 mWin.getDisplayContent().getRotationAnimation();
Vishnu Nair83537a72018-07-19 21:27:48 -0700683 final boolean windowParticipatesInScreenRotationAnimation =
684 !mWin.mForceSeamlesslyRotate;
685 final boolean screenAnimation = screenRotationAnimation != null
686 && screenRotationAnimation.isAnimating()
687 && windowParticipatesInScreenRotationAnimation;
Robert Carr2f0fe622015-09-25 14:56:38 -0700688
Robert Carr44643c12017-09-27 14:57:38 -0700689 if (screenAnimation) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700690 // cache often used attributes locally
chaviw492139a2018-07-16 16:07:35 -0700691 final Rect frame = mWin.getFrameLw();
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700692 final float tmpFloats[] = mService.mTmpFloats;
693 final Matrix tmpMatrix = mWin.mTmpMatrix;
694
695 // Compute the desired transformation.
Robert Carr44643c12017-09-27 14:57:38 -0700696 if (screenRotationAnimation.isRotating()) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700697 // If we are doing a screen animation, the global rotation
698 // applied to windows can result in windows that are carefully
699 // aligned with each other to slightly separate, allowing you
700 // to see what is behind them. An unsightly mess. This...
701 // thing... magically makes it call good: scale each window
702 // slightly (two pixels larger in each dimension, from the
703 // window's center).
704 final float w = frame.width();
705 final float h = frame.height();
706 if (w>=1 && h>=1) {
707 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
708 } else {
709 tmpMatrix.reset();
710 }
711 } else {
712 tmpMatrix.reset();
713 }
Robert Carr44643c12017-09-27 14:57:38 -0700714
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700715 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
Robert Carr29d196f2017-11-28 12:39:46 -0800716
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100717 // WindowState.prepareSurfaces expands for surface insets (in order they don't get
718 // clipped by the WindowState surface), so we need to go into the other direction here.
Robert Carr217e7cc2018-01-31 18:08:39 -0800719 tmpMatrix.postTranslate(mWin.mAttrs.surfaceInsets.left,
720 mWin.mAttrs.surfaceInsets.top);
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100721
Matthew Ng34a06d12017-02-03 11:56:08 -0800722
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700723 // "convert" it into SurfaceFlinger's format
724 // (a 2x2 matrix + an offset)
725 // Here we must not transform the position of the surface
726 // since it is already included in the transformation.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800727 //Slog.i(TAG_WM, "Transform: " + matrix);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700728
729 mHaveMatrix = true;
730 tmpMatrix.getValues(tmpFloats);
731 mDsDx = tmpFloats[Matrix.MSCALE_X];
732 mDtDx = tmpFloats[Matrix.MSKEW_Y];
Robert Carr0edf18f2017-02-21 20:01:47 -0800733 mDtDy = tmpFloats[Matrix.MSKEW_X];
734 mDsDy = tmpFloats[Matrix.MSCALE_Y];
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700735
736 // Now set the alpha... but because our current hardware
737 // can't do alpha transformation on a non-opaque surface,
738 // turn it off if we are running an animation that is also
739 // transforming since it is more important to have that
740 // animation be smooth.
741 mShownAlpha = mAlpha;
742 if (!mService.mLimitedAlphaCompositing
743 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
Robert Carr217e7cc2018-01-31 18:08:39 -0800744 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDtDy, mDsDy)))) {
Vadim Caen641d39d2019-07-23 11:29:45 +0200745 mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha();
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700746 }
747
Adam Pardyl8c2d19c2019-09-16 17:15:38 +0200748 if ((DEBUG_ANIM || DEBUG) && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) {
749 Slog.v(TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha
750 + " screen=" + (screenAnimation
751 ? screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
752 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700753 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700754 } else if (mIsWallpaper && mService.mRoot.mWallpaperActionPending) {
Craig Mautner4d7349b2012-04-20 14:52:47 -0700755 return;
Chong Zhang3005e752015-09-18 18:46:28 -0700756 } else if (mWin.isDragResizeChanged()) {
757 // This window is awaiting a relayout because user just started (or ended)
758 // drag-resizing. The shown frame (which affects surface size and pos)
759 // should not be updated until we get next finished draw with the new surface.
760 // Otherwise one or two frames rendered with old settings would be displayed
761 // with new geometry.
762 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700763 }
764
Adam Pardyl8c2d19c2019-09-16 17:15:38 +0200765 if (DEBUG) {
766 Slog.v(TAG, "computeShownFrameLocked: " + this
767 + " not attached, mAlpha=" + mAlpha);
768 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700769
Robert Carrb1579c82017-09-05 14:54:47 -0700770 mShownAlpha = mAlpha;
771 mHaveMatrix = false;
772 mDsDx = mWin.mGlobalScale;
773 mDtDx = 0;
774 mDtDy = 0;
775 mDsDy = mWin.mGlobalScale;
Robert Carref090ac2017-05-15 16:45:50 -0700776 }
777
Robert Carrfbbde852016-10-18 11:02:28 -0700778 /**
Robert Carrfbbde852016-10-18 11:02:28 -0700779 * Calculate the window-space crop rect and fill clipRect.
Wale Ogunwaleca9e0612016-12-02 07:45:59 -0800780 * @return true if clipRect has been filled otherwise, no window space crop should be applied.
Robert Carrfbbde852016-10-18 11:02:28 -0700781 */
Wale Ogunwaleca9e0612016-12-02 07:45:59 -0800782 private boolean calculateCrop(Rect clipRect) {
Robert Carrfbbde852016-10-18 11:02:28 -0700783 final WindowState w = mWin;
784 final DisplayContent displayContent = w.getDisplayContent();
785 clipRect.setEmpty();
786
787 if (displayContent == null) {
788 return false;
789 }
790
Evan Roskye067ddf2020-04-24 18:28:25 -0700791 if (w.getWindowConfiguration().tasksAreFloating()
792 || WindowConfiguration.isSplitScreenWindowingMode(w.getWindowingMode())) {
Robert Carrfbbde852016-10-18 11:02:28 -0700793 return false;
794 }
795
Vishnu Nair83537a72018-07-19 21:27:48 -0700796 // During forced seamless rotation, the surface bounds get updated with the crop in the
797 // new rotation, which is not compatible with showing the surface in the old rotation.
798 // To work around that we disable cropping for such windows, as it is not necessary anyways.
799 if (w.mForceSeamlesslyRotate) {
800 return false;
801 }
802
Robert Carrfbbde852016-10-18 11:02:28 -0700803 // If we're animating, the wallpaper should only
804 // be updated at the end of the animation.
805 if (w.mAttrs.type == TYPE_WALLPAPER) {
806 return false;
807 }
808
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -0700809 if (DEBUG_WINDOW_CROP) Slog.d(TAG,
810 "Updating crop win=" + w + " mLastCrop=" + mLastClipRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -0700811
Robert Carrfbbde852016-10-18 11:02:28 -0700812 w.calculatePolicyCrop(mSystemDecorRect);
813
814 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Applying decor to crop win=" + w + " mDecorFrame="
chaviw553b0212018-07-12 13:37:01 -0700815 + w.getDecorFrame() + " mSystemDecorRect=" + mSystemDecorRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -0700816
Robert Carr51a1b872015-12-08 14:03:13 -0800817 // We use the clip rect as provided by the tranformation for non-fullscreen windows to
818 // avoid premature clipping with the system decor rect.
Robert Carrf0586622018-01-29 13:03:43 -0800819 clipRect.set(mSystemDecorRect);
Evan Rosky4fb1e912019-03-06 13:54:43 -0800820 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "win=" + w + " Initial clip rect: " + clipRect);
Robert Carr51a1b872015-12-08 14:03:13 -0800821
Robert Carrfbbde852016-10-18 11:02:28 -0700822 w.expandForSurfaceInsets(clipRect);
Alan Viverette49a22e82014-07-12 20:01:27 -0700823
Alan Viverette49a22e82014-07-12 20:01:27 -0700824 // The clip rect was generated assuming (0,0) as the window origin,
825 // so we need to translate to match the actual surface coordinates.
Robert Carrfbbde852016-10-18 11:02:28 -0700826 clipRect.offset(w.mAttrs.surfaceInsets.left, w.mAttrs.surfaceInsets.top);
Robert Carr58f29132015-10-29 14:19:05 -0700827
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -0700828 if (DEBUG_WINDOW_CROP) Slog.d(TAG,
829 "win=" + w + " Clip rect after stack adjustment=" + clipRect);
Robert Carr58f29132015-10-29 14:19:05 -0700830
Chia-I Wue6bcaf12016-05-27 10:58:48 +0800831 w.transformClipRectFromScreenToSurfaceSpace(clipRect);
Robert Carr58f29132015-10-29 14:19:05 -0700832
Robert Carrfbbde852016-10-18 11:02:28 -0700833 return true;
Robert Carr0d00c2e2016-02-29 17:45:02 -0800834 }
835
chaviw23012112017-12-20 15:29:04 -0800836 private void applyCrop(Rect clipRect, boolean recoveringMemory) {
Robert Carrfbbde852016-10-18 11:02:28 -0700837 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "applyCrop: win=" + mWin
chaviw23012112017-12-20 15:29:04 -0800838 + " clipRect=" + clipRect);
Robert Carr4320d332016-06-10 15:13:32 -0700839 if (clipRect != null) {
840 if (!clipRect.equals(mLastClipRect)) {
841 mLastClipRect.set(clipRect);
842 mSurfaceController.setCropInTransaction(clipRect, recoveringMemory);
843 }
844 } else {
845 mSurfaceController.clearCropInTransaction(recoveringMemory);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -0700846 }
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -0700847 }
848
Robert Carr2162a2f2020-05-05 13:46:48 -0700849 private boolean shouldConsumeMainWindowSizeTransaction() {
850 // We only consume the transaction when the client is calling relayout
851 // because this is the only time we know the frameNumber will be valid
852 // due to the client renderer being paused. Put otherwise, only when
853 // mInRelayout is true can we guarantee the next frame will contain
854 // the most recent configuration.
855 if (!mWin.mInRelayout) return false;
856 // Since we can only do this for one window, we focus on the main application window
857 if (mAttrType != TYPE_BASE_APPLICATION) return false;
858 final Task task = mWin.getTask();
859 if (task == null) return false;
860 if (task.getMainWindowSizeChangeTransaction() == null) return false;
861 // Likewise we only focus on the task root, since we can only use one window
862 if (!mWin.mActivityRecord.isRootOfTask()) return false;
863 return true;
864 }
865
Wale Ogunwale4c8b7952015-04-07 10:49:40 -0700866 void setSurfaceBoundariesLocked(final boolean recoveringMemory) {
Wale Ogunwale027f4752017-05-12 10:37:16 -0700867 if (mSurfaceController == null) {
868 return;
869 }
870
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700871 final WindowState w = mWin;
Winson Chung08f81892017-03-02 15:40:51 -0800872 final LayoutParams attrs = mWin.getAttrs();
Robert Carr0d00c2e2016-02-29 17:45:02 -0800873 final Task task = w.getTask();
Alan Viveretteccb11e12014-07-08 16:04:02 -0700874
chaviw9c81e632018-07-31 11:17:52 -0700875 calculateSurfaceBounds(w, attrs, mTmpSize);
Chong Zhang0275e392015-09-17 10:41:44 -0700876
Robert Carr04092112016-06-02 12:56:12 -0700877 mExtraHScale = (float) 1.0;
878 mExtraVScale = (float) 1.0;
Robert Carre1034cc32016-02-01 13:08:15 -0800879
Robert Carr6da3cc02016-06-16 15:17:07 -0700880 boolean wasForceScaled = mForceScaleUntilResize;
Robert Carr6da3cc02016-06-16 15:17:07 -0700881
Robert Carrfed10072016-05-26 11:48:49 -0700882 // Once relayout has been called at least once, we need to make sure
883 // we only resize the client surface during calls to relayout. For
884 // clients which use indeterminate measure specs (MATCH_PARENT),
885 // we may try and change their window size without a call to relayout.
886 // However, this would be unsafe, as the client may be in the middle
887 // of producing a frame at the old size, having just completed layout
888 // to find the surface size changed underneath it.
chaviwbe43ac82018-04-04 15:14:49 -0700889 final boolean relayout = !w.mRelayoutCalled || w.mInRelayout;
890 if (relayout) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800891 mSurfaceResized = mSurfaceController.setBufferSizeInTransaction(
Robert Carrfed10072016-05-26 11:48:49 -0700892 mTmpSize.width(), mTmpSize.height(), recoveringMemory);
893 } else {
894 mSurfaceResized = false;
895 }
Robert Carrc7294602016-05-13 11:32:05 -0700896 mForceScaleUntilResize = mForceScaleUntilResize && !mSurfaceResized;
Robert Carr6da3cc02016-06-16 15:17:07 -0700897 // If we are undergoing seamless rotation, the surface has already
898 // been set up to persist at it's old location. We need to freeze
899 // updates until a resize occurs.
Robert Carrc7294602016-05-13 11:32:05 -0700900
chaviw23012112017-12-20 15:29:04 -0800901 Rect clipRect = null;
Robert Carrfbbde852016-10-18 11:02:28 -0700902 if (calculateCrop(mTmpClipRect)) {
903 clipRect = mTmpClipRect;
904 }
Robert Carra9408d42016-06-03 13:28:48 -0700905
Robert Carr2162a2f2020-05-05 13:46:48 -0700906 if (shouldConsumeMainWindowSizeTransaction()) {
907 task.getSurfaceControl().deferTransactionUntil(mWin.getClientViewRootSurface(),
908 mWin.getFrameNumber());
Rob Carr698b6842020-03-06 15:33:31 -0800909 mSurfaceController.deferTransactionUntil(mWin.getClientViewRootSurface(),
Robert Carr711e7052020-02-19 11:14:33 -0800910 mWin.getFrameNumber());
911 SurfaceControl.mergeToGlobalTransaction(task.getMainWindowSizeChangeTransaction());
912 task.setMainWindowSizeChangeTransaction(null);
913 }
914
Robert Carra9408d42016-06-03 13:28:48 -0700915 float surfaceWidth = mSurfaceController.getWidth();
916 float surfaceHeight = mSurfaceController.getHeight();
917
Robert Carr74a66a22018-02-23 12:17:51 -0800918 final Rect insets = attrs.surfaceInsets;
919
Robert Carr8f0a3ad2017-02-15 19:30:28 -0800920 if (isForceScaled()) {
Robert Carr74a66a22018-02-23 12:17:51 -0800921 int hInsets = insets.left + insets.right;
922 int vInsets = insets.top + insets.bottom;
Winson Chung08f81892017-03-02 15:40:51 -0800923 float surfaceContentWidth = surfaceWidth - hInsets;
924 float surfaceContentHeight = surfaceHeight - vInsets;
Robert Carra9408d42016-06-03 13:28:48 -0700925 if (!mForceScaleUntilResize) {
926 mSurfaceController.forceScaleableInTransaction(true);
927 }
Robert Carrfd4c9892017-02-01 10:28:28 -0800928
Robert Carr74a66a22018-02-23 12:17:51 -0800929 int posX = 0;
930 int posY = 0;
Wale Ogunwale0b3d2922019-12-30 08:55:07 -0800931 task.getStack().getDimBounds(mTmpStackBounds);
Robert Carr18f622f2017-05-08 11:20:43 -0700932
933 boolean allowStretching = false;
Wale Ogunwale0b3d2922019-12-30 08:55:07 -0800934 task.getStack().getFinalAnimationSourceHintBounds(mTmpSourceBounds);
Robert Carr18f622f2017-05-08 11:20:43 -0700935 // If we don't have source bounds, we can attempt to use the content insets
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800936 // if we have content insets.
Robert Carr18f622f2017-05-08 11:20:43 -0700937 if (mTmpSourceBounds.isEmpty() && (mWin.mLastRelayoutContentInsets.width() > 0
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800938 || mWin.mLastRelayoutContentInsets.height() > 0)) {
Wale Ogunwale0b3d2922019-12-30 08:55:07 -0800939 mTmpSourceBounds.set(task.getStack().mPreAnimationBounds);
Robert Carr18f622f2017-05-08 11:20:43 -0700940 mTmpSourceBounds.inset(mWin.mLastRelayoutContentInsets);
941 allowStretching = true;
942 }
Adrian Roos604ef952018-05-15 20:13:13 +0200943
944 // Make sure that what we're animating to and from is actually the right size in case
945 // the window cannot take up the full screen.
chaviw553b0212018-07-12 13:37:01 -0700946 mTmpStackBounds.intersectUnchecked(w.getParentFrame());
947 mTmpSourceBounds.intersectUnchecked(w.getParentFrame());
948 mTmpAnimatingBounds.intersectUnchecked(w.getParentFrame());
Adrian Roos604ef952018-05-15 20:13:13 +0200949
Winson Chung08f81892017-03-02 15:40:51 -0800950 if (!mTmpSourceBounds.isEmpty()) {
951 // Get the final target stack bounds, if we are not animating, this is just the
952 // current stack bounds
Wale Ogunwale0b3d2922019-12-30 08:55:07 -0800953 task.getStack().getFinalAnimationBounds(mTmpAnimatingBounds);
Winson Chung08f81892017-03-02 15:40:51 -0800954
955 // Calculate the current progress and interpolate the difference between the target
956 // and source bounds
957 float finalWidth = mTmpAnimatingBounds.width();
958 float initialWidth = mTmpSourceBounds.width();
Robert Carr18f622f2017-05-08 11:20:43 -0700959 float tw = (surfaceContentWidth - mTmpStackBounds.width())
Winson Chung08f81892017-03-02 15:40:51 -0800960 / (surfaceContentWidth - mTmpAnimatingBounds.width());
Robert Carr18f622f2017-05-08 11:20:43 -0700961 float th = tw;
962 mExtraHScale = (initialWidth + tw * (finalWidth - initialWidth)) / initialWidth;
963 if (allowStretching) {
964 float finalHeight = mTmpAnimatingBounds.height();
965 float initialHeight = mTmpSourceBounds.height();
966 th = (surfaceContentHeight - mTmpStackBounds.height())
967 / (surfaceContentHeight - mTmpAnimatingBounds.height());
968 mExtraVScale = (initialHeight + tw * (finalHeight - initialHeight))
969 / initialHeight;
970 } else {
971 mExtraVScale = mExtraHScale;
972 }
Winson Chung08f81892017-03-02 15:40:51 -0800973
974 // Adjust the position to account for the inset bounds
Robert Carr18f622f2017-05-08 11:20:43 -0700975 posX -= (int) (tw * mExtraHScale * mTmpSourceBounds.left);
976 posY -= (int) (th * mExtraVScale * mTmpSourceBounds.top);
Winson Chung08f81892017-03-02 15:40:51 -0800977
Robert Carr74a66a22018-02-23 12:17:51 -0800978 // In pinned mode the clip rectangle applied to us by our stack has been
979 // expanded outwards to allow for shadows. However in case of source bounds set
980 // we need to crop to within the surface. The code above has scaled and positioned
981 // the surface to fit the unexpanded stack bounds, but now we need to reapply
982 // the cropping that the stack would have applied if it weren't expanded. This
983 // can be different in each direction based on the source bounds.
984 clipRect = mTmpClipRect;
985 clipRect.set((int)((insets.left + mTmpSourceBounds.left) * tw),
986 (int)((insets.top + mTmpSourceBounds.top) * th),
987 insets.left + (int)(surfaceWidth
988 - (tw* (surfaceWidth - mTmpSourceBounds.right))),
989 insets.top + (int)(surfaceHeight
990 - (th * (surfaceHeight - mTmpSourceBounds.bottom))));
Winson Chung08f81892017-03-02 15:40:51 -0800991 } else {
992 // We want to calculate the scaling based on the content area, not based on
993 // the entire surface, so that we scale in sync with windows that don't have insets.
994 mExtraHScale = mTmpStackBounds.width() / surfaceContentWidth;
995 mExtraVScale = mTmpStackBounds.height() / surfaceContentHeight;
996
997 // Since we are scaled to fit in our previously desired crop, we can now
998 // expose the whole window in buffer space, and not risk extending
999 // past where the system would have cropped us
1000 clipRect = null;
Winson Chung08f81892017-03-02 15:40:51 -08001001 }
Robert Carr0d00c2e2016-02-29 17:45:02 -08001002
Robert Carrfd4c9892017-02-01 10:28:28 -08001003 // In the case of ForceScaleToStack we scale entire tasks together,
Robert Carr0d00c2e2016-02-29 17:45:02 -08001004 // and so we need to scale our offsets relative to the task bounds
1005 // or parent and child windows would fall out of alignment.
Winson Chung08f81892017-03-02 15:40:51 -08001006 posX -= (int) (attrs.x * (1 - mExtraHScale));
1007 posY -= (int) (attrs.y * (1 - mExtraVScale));
1008
Robert Carrbc133762016-05-12 14:04:38 -07001009 // Imagine we are scaling down. As we scale the buffer down, we decrease the
1010 // distance between the surface top left, and the start of the surface contents
1011 // (previously it was surfaceInsets.left pixels in screen space but now it
Robert Carr04092112016-06-02 12:56:12 -07001012 // will be surfaceInsets.left*mExtraHScale). This means in order to keep the
Robert Carrbc133762016-05-12 14:04:38 -07001013 // non inset content at the same position, we have to shift the whole window
1014 // forward. Likewise for scaling up, we've increased this distance, and we need
1015 // to shift by a negative number to compensate.
Robert Carr74a66a22018-02-23 12:17:51 -08001016 posX += insets.left * (1 - mExtraHScale);
1017 posY += insets.top * (1 - mExtraVScale);
Robert Carrbc133762016-05-12 14:04:38 -07001018
Winson Chung08f81892017-03-02 15:40:51 -08001019 mSurfaceController.setPositionInTransaction((float) Math.floor(posX),
1020 (float) Math.floor(posY), recoveringMemory);
Robert Carrc7294602016-05-13 11:32:05 -07001021
1022 // Various surfaces in the scaled stack may resize at different times.
1023 // We need to ensure for each surface, that we disable transformation matrix
1024 // scaling in the same transaction which we resize the surface in.
1025 // As we are in SCALING_MODE_SCALE_TO_WINDOW, SurfaceFlinger will
Robert Carra9408d42016-06-03 13:28:48 -07001026 // then take over the scaling until the new buffer arrives, and things
Robert Carrc7294602016-05-13 11:32:05 -07001027 // will be seamless.
Robert Carr2025bf842018-03-19 13:25:05 -07001028 if (mPipAnimationStarted == false) {
1029 mForceScaleUntilResize = true;
1030 mPipAnimationStarted = true;
1031 }
Rob Carr06be6bb2018-02-06 16:26:06 +00001032 } else {
Robert Carr2025bf842018-03-19 13:25:05 -07001033 mPipAnimationStarted = false;
1034
Rob Carr06be6bb2018-02-06 16:26:06 +00001035 if (!w.mSeamlesslyRotated) {
chaviwbe43ac82018-04-04 15:14:49 -07001036 // Used to offset the WSA when stack position changes before a resize.
1037 int xOffset = mXOffset;
1038 int yOffset = mYOffset;
1039 if (mOffsetPositionForStackResize) {
1040 if (relayout) {
1041 // Once a relayout is called, reset the offset back to 0 and defer
1042 // setting it until a new frame with the updated size. This ensures that
1043 // the WS position is reset (so the stack position is shown) at the same
1044 // time that the buffer size changes.
1045 setOffsetPositionForStackResize(false);
Rob Carr698b6842020-03-06 15:33:31 -08001046 mSurfaceController.deferTransactionUntil(mWin.getClientViewRootSurface(),
chaviwbe43ac82018-04-04 15:14:49 -07001047 mWin.getFrameNumber());
1048 } else {
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08001049 final ActivityStack stack = mWin.getRootTask();
chaviwbe43ac82018-04-04 15:14:49 -07001050 mTmpPos.x = 0;
1051 mTmpPos.y = 0;
1052 if (stack != null) {
Evan Rosky6ecd67c2020-04-14 11:50:43 -07001053 stack.getRelativePosition(mTmpPos);
chaviwbe43ac82018-04-04 15:14:49 -07001054 }
1055
1056 xOffset = -mTmpPos.x;
1057 yOffset = -mTmpPos.y;
1058
1059 // Crop also needs to be extended so the bottom isn't cut off when the WSA
1060 // position is moved.
1061 if (clipRect != null) {
1062 clipRect.right += mTmpPos.x;
1063 clipRect.bottom += mTmpPos.y;
1064 }
1065 }
1066 }
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001067 if (!mIsWallpaper) {
1068 mSurfaceController.setPositionInTransaction(xOffset, yOffset, recoveringMemory);
1069 } else {
1070 setWallpaperPositionAndScale(
1071 xOffset, yOffset, mWallpaperScale, recoveringMemory);
1072 }
Rob Carr06be6bb2018-02-06 16:26:06 +00001073 }
Robert Carr0d00c2e2016-02-29 17:45:02 -08001074 }
1075
Robert Carra9408d42016-06-03 13:28:48 -07001076 // If we are ending the scaling mode. We switch to SCALING_MODE_FREEZE
Robert Carr6da3cc02016-06-16 15:17:07 -07001077 // to prevent further updates until buffer latch.
Vishnu Nairddd80742018-08-21 14:12:46 -07001078 // We also need to freeze the Surface geometry until a buffer
1079 // comes in at the new size (normally position and crop are unfrozen).
chaviw007340c2019-09-04 16:07:37 -07001080 // deferTransactionUntil accomplishes this for us.
Vishnu Nairddd80742018-08-21 14:12:46 -07001081 if (wasForceScaled && !mForceScaleUntilResize) {
Rob Carr698b6842020-03-06 15:33:31 -08001082 mSurfaceController.deferTransactionUntil(mWin.getClientViewRootSurface(),
Vishnu Nairb040c012018-09-07 11:38:32 -07001083 mWin.getFrameNumber());
Robert Carra9408d42016-06-03 13:28:48 -07001084 mSurfaceController.forceScaleableInTransaction(false);
1085 }
Robert Carr4320d332016-06-10 15:13:32 -07001086
Vishnu Nairddd80742018-08-21 14:12:46 -07001087
Robert Carr6da3cc02016-06-16 15:17:07 -07001088 if (!w.mSeamlesslyRotated) {
Santiago Etchebeherea0876642020-03-30 11:28:49 -07001089 // Wallpaper is already updated above when calling setWallpaperPositionAndScale so
1090 // we only need to consider the non-wallpaper case here.
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001091 if (!mIsWallpaper) {
1092 applyCrop(clipRect, recoveringMemory);
Santiago Etchebeherea0876642020-03-30 11:28:49 -07001093 mSurfaceController.setMatrixInTransaction(
1094 mDsDx * w.mHScale * mExtraHScale,
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001095 mDtDx * w.mVScale * mExtraVScale,
1096 mDtDy * w.mHScale * mExtraHScale,
1097 mDsDy * w.mVScale * mExtraVScale, recoveringMemory);
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001098 }
Robert Carr6da3cc02016-06-16 15:17:07 -07001099 }
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001100
Robert Carre6a83512015-11-03 16:09:21 -08001101 if (mSurfaceResized) {
Chong Zhang5b2f1992015-11-13 15:40:36 -08001102 mReportSurfaceResized = true;
Riddle Hsu8419e4b2019-09-18 23:28:01 +08001103 mWin.getDisplayContent().pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001104 }
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001105 }
1106
Andrii Kulian283acd22017-08-03 04:03:51 -07001107 /**
1108 * Get rect of the task this window is currently in. If there is no task, rect will be set to
1109 * empty.
1110 */
1111 void getContainerRect(Rect rect) {
1112 final Task task = mWin.getTask();
1113 if (task != null) {
1114 task.getDimBounds(rect);
1115 } else {
1116 rect.left = rect.top = rect.right = rect.bottom = 0;
1117 }
1118 }
1119
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001120 void prepareSurfaceLocked(final boolean recoveringMemory) {
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001121 final WindowState w = mWin;
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001122 if (!hasSurface()) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001123
1124 // There is no need to wait for an animation change if our window is gone for layout
1125 // already as we'll never be visible.
Bryce Lee8c3cf382017-07-06 19:47:10 -07001126 if (w.getOrientationChanging() && w.isGoneForLayoutLw()) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001127 ProtoLog.v(WM_DEBUG_ORIENTATION, "Orientation change skips hidden %s", w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001128 w.setOrientationChanging(false);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001129 }
1130 return;
1131 }
1132
1133 boolean displayed = false;
1134
1135 computeShownFrameLocked();
1136
Craig Mautnera91f9e22012-09-14 16:22:08 -07001137 setSurfaceBoundariesLocked(recoveringMemory);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001138
Tiger Huang50d45462018-05-25 22:57:52 +08001139 if (mIsWallpaper && !w.mWallpaperVisible) {
Craig Mautner0fa77c12012-06-11 15:57:19 -07001140 // Wallpaper is no longer visible and there is no wp target => hide it.
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001141 hide("prepareSurfaceLocked");
Wale Ogunwale9d147902016-07-16 11:58:55 -07001142 } else if (w.isParentWindowHidden() || !w.isOnScreen()) {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001143 hide("prepareSurfaceLocked");
Wale Ogunwalee8069dc2015-08-18 09:52:01 -07001144 mWallpaperControllerLocked.hideWallpapers(w);
Craig Mautnerb9836b92012-06-11 11:40:09 -07001145
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001146 // If we are waiting for this window to handle an orientation change. If this window is
1147 // really hidden (gone for layout), there is no point in still waiting for it.
1148 // Note that this does introduce a potential glitch if the window becomes unhidden
1149 // before it has drawn for the new orientation.
Bryce Lee8c3cf382017-07-06 19:47:10 -07001150 if (w.getOrientationChanging() && w.isGoneForLayoutLw()) {
1151 w.setOrientationChanging(false);
Adrian Roosb125e0b2019-10-02 14:55:14 +02001152 ProtoLog.v(WM_DEBUG_ORIENTATION,
1153 "Orientation change skips hidden %s", w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001154 }
Jorim Jaggi35d328a2018-08-14 17:00:20 +02001155 } else if (mLastAlpha != mShownAlpha
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001156 || mLastDsDx != mDsDx
1157 || mLastDtDx != mDtDx
1158 || mLastDsDy != mDsDy
1159 || mLastDtDy != mDtDy
1160 || w.mLastHScale != w.mHScale
1161 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -07001162 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001163 displayed = true;
1164 mLastAlpha = mShownAlpha;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001165 mLastDsDx = mDsDx;
1166 mLastDtDx = mDtDx;
1167 mLastDsDy = mDsDy;
1168 mLastDtDy = mDtDy;
1169 w.mLastHScale = w.mHScale;
1170 w.mLastVScale = w.mVScale;
Adrian Roosb125e0b2019-10-02 14:55:14 +02001171 ProtoLog.i(WM_SHOW_TRANSACTIONS,
1172 "SURFACE controller=%s alpha=%f matrix=[%f*%f,%f*%f][%f*%f,%f*%f]: %s",
1173 mSurfaceController, mShownAlpha, mDsDx, w.mHScale, mDtDx, w.mVScale,
1174 mDtDy, w.mHScale, mDsDy, w.mVScale, w);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001175
Santiago Etchebeherea0876642020-03-30 11:28:49 -07001176 boolean prepared = true;
1177
1178 if (mIsWallpaper) {
1179 setWallpaperPositionAndScale(
1180 mXOffset, mYOffset, mWallpaperScale, recoveringMemory);
1181 } else {
1182 prepared =
1183 mSurfaceController.prepareToShowInTransaction(mShownAlpha,
Robert Carr04092112016-06-02 12:56:12 -07001184 mDsDx * w.mHScale * mExtraHScale,
1185 mDtDx * w.mVScale * mExtraVScale,
Robert Carr0edf18f2017-02-21 20:01:47 -08001186 mDtDy * w.mHScale * mExtraHScale,
1187 mDsDy * w.mVScale * mExtraVScale,
Robert Carre6a83512015-11-03 16:09:21 -08001188 recoveringMemory);
Santiago Etchebeherea0876642020-03-30 11:28:49 -07001189 }
Robert Carre6a83512015-11-03 16:09:21 -08001190
Robert Carr03206af2017-07-10 18:24:21 -07001191 if (prepared && mDrawState == HAS_DRAWN) {
1192 if (mLastHidden) {
1193 if (showSurfaceRobustlyLocked()) {
1194 markPreservedSurfaceForDestroy();
1195 mAnimator.requestRemovalOfReplacedWindows(w);
1196 mLastHidden = false;
1197 if (mIsWallpaper) {
1198 w.dispatchWallpaperVisibility(true);
1199 }
Riddle Hsu8419e4b2019-09-18 23:28:01 +08001200 final DisplayContent displayContent = w.getDisplayContent();
1201 if (!displayContent.getLastHasContent()) {
Jorim Jaggif1292892018-09-10 11:58:13 +02001202 // This draw means the difference between unique content and mirroring.
1203 // Run another pass through performLayout to set mHasContent in the
1204 // LogicalDisplay.
Riddle Hsu8419e4b2019-09-18 23:28:01 +08001205 displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
Vishnu Nairf7645aa2019-06-18 11:14:01 -07001206 if (DEBUG_LAYOUT_REPEATS) {
Jorim Jaggif1292892018-09-10 11:58:13 +02001207 mService.mWindowPlacerLocked.debugLayoutRepeats(
1208 "showSurfaceRobustlyLocked " + w,
Riddle Hsu8419e4b2019-09-18 23:28:01 +08001209 displayContent.pendingLayoutChanges);
Jorim Jaggif1292892018-09-10 11:58:13 +02001210 }
Tiger Huang50d45462018-05-25 22:57:52 +08001211 }
Robert Carr03206af2017-07-10 18:24:21 -07001212 } else {
1213 w.setOrientationChanging(false);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001214 }
Robert Carr03206af2017-07-10 18:24:21 -07001215 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001216 }
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001217 if (hasSurface()) {
Robert Carre6a83512015-11-03 16:09:21 -08001218 w.mToken.hasVisible = true;
1219 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001220 } else {
lumark9bca6b42019-10-17 18:35:22 +08001221 if (DEBUG_ANIM && mWin.isAnimating(TRANSITION | PARENTS)) {
Chong Zhange05db742016-02-16 16:58:37 -08001222 Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
Craig Mautner83339b42012-05-01 22:13:23 -07001223 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001224 displayed = true;
1225 }
1226
Bryce Lee8c3cf382017-07-06 19:47:10 -07001227 if (w.getOrientationChanging()) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001228 if (!w.isDrawnLw()) {
1229 mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE;
1230 mAnimator.mLastWindowFreezeSource = w;
Adrian Roosb125e0b2019-10-02 14:55:14 +02001231 ProtoLog.v(WM_DEBUG_ORIENTATION,
1232 "Orientation continue waiting for draw in %s", w);
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001233 } else {
Bryce Lee8c3cf382017-07-06 19:47:10 -07001234 w.setOrientationChanging(false);
Adrian Roosb125e0b2019-10-02 14:55:14 +02001235 ProtoLog.v(WM_DEBUG_ORIENTATION, "Orientation change complete in %s", w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001236 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001237 }
1238
1239 if (displayed) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001240 w.mToken.hasVisible = true;
1241 }
1242 }
1243
Craig Mautneref655012013-01-03 11:20:24 -08001244 void setTransparentRegionHintLocked(final Region region) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001245 if (mSurfaceController == null) {
1246 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1247 return;
1248 }
Robert Carre6a83512015-11-03 16:09:21 -08001249 mSurfaceController.setTransparentRegionHint(region);
Craig Mautner48ba1e72012-04-02 13:18:16 -07001250 }
1251
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001252 boolean setWallpaperOffset(int dx, int dy, float scale) {
1253 if (mXOffset == dx && mYOffset == dy && Float.compare(mWallpaperScale, scale) == 0) {
Robert Carr217e7cc2018-01-31 18:08:39 -08001254 return false;
1255 }
1256 mXOffset = dx;
1257 mYOffset = dy;
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001258 mWallpaperScale = scale;
Robert Carre6a83512015-11-03 16:09:21 -08001259
1260 try {
Craig Mautner71dd1b62014-02-18 15:48:52 -08001261 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setWallpaperOffset");
Robert Carr68e5c9e2016-09-14 10:50:09 -07001262 mService.openSurfaceTransaction();
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001263 setWallpaperPositionAndScale(dx, dy, scale, false);
Robert Carre6a83512015-11-03 16:09:21 -08001264 } catch (RuntimeException e) {
1265 Slog.w(TAG, "Error positioning surface of " + mWin
Robert Carr217e7cc2018-01-31 18:08:39 -08001266 + " pos=(" + dx + "," + dy + ")", e);
Robert Carre6a83512015-11-03 16:09:21 -08001267 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +02001268 mService.closeSurfaceTransaction("setWallpaperOffset");
Robert Carre6a83512015-11-03 16:09:21 -08001269 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1270 "<<< CLOSE TRANSACTION setWallpaperOffset");
Robert Carr217e7cc2018-01-31 18:08:39 -08001271 return true;
Craig Mautner48ba1e72012-04-02 13:18:16 -07001272 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001273 }
1274
Lucas Dupin13f4b8a2020-02-19 13:41:52 -08001275 private void setWallpaperPositionAndScale(int dx, int dy, float scale,
1276 boolean recoveringMemory) {
1277 DisplayInfo displayInfo = mWin.getDisplayInfo();
1278 Matrix matrix = mWin.mTmpMatrix;
1279 matrix.setTranslate(dx, dy);
1280 matrix.postScale(scale, scale, displayInfo.logicalWidth / 2f,
1281 displayInfo.logicalHeight / 2f);
1282 matrix.getValues(mWin.mTmpMatrixArray);
1283 matrix.reset();
1284
1285 mSurfaceController.setPositionInTransaction(mWin.mTmpMatrixArray[MTRANS_X],
1286 mWin.mTmpMatrixArray[MTRANS_Y], recoveringMemory);
1287 mSurfaceController.setMatrixInTransaction(
1288 mDsDx * mWin.mTmpMatrixArray[MSCALE_X] * mWin.mHScale * mExtraHScale,
1289 mDtDx * mWin.mTmpMatrixArray[MSKEW_Y] * mWin.mVScale * mExtraVScale,
1290 mDtDy * mWin.mTmpMatrixArray[MSKEW_X] * mWin.mHScale * mExtraHScale,
1291 mDsDy * mWin.mTmpMatrixArray[MSCALE_Y] * mWin.mVScale * mExtraVScale,
1292 recoveringMemory);
1293 applyCrop(null, recoveringMemory);
1294 }
1295
John Reck80181b92015-05-19 11:09:32 -07001296 /**
1297 * Try to change the pixel format without recreating the surface. This
1298 * will be common in the case of changing from PixelFormat.OPAQUE to
1299 * PixelFormat.TRANSLUCENT in the hardware-accelerated case as both
1300 * requested formats resolve to the same underlying SurfaceControl format
1301 * @return True if format was succesfully changed, false otherwise
1302 */
1303 boolean tryChangeFormatInPlaceLocked() {
Robert Carre6a83512015-11-03 16:09:21 -08001304 if (mSurfaceController == null) {
John Reck80181b92015-05-19 11:09:32 -07001305 return false;
1306 }
1307 final LayoutParams attrs = mWin.getAttrs();
Wale Ogunwalee7bf46b2015-09-30 09:19:28 -07001308 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
John Reck80181b92015-05-19 11:09:32 -07001309 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
1310 if (format == mSurfaceFormat) {
1311 setOpaqueLocked(!PixelFormat.formatHasAlpha(attrs.format));
1312 return true;
1313 }
1314 return false;
1315 }
1316
Craig Mautner6f612042014-09-07 13:13:23 -07001317 void setOpaqueLocked(boolean isOpaque) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001318 if (mSurfaceController == null) {
1319 return;
1320 }
Robert Carre6a83512015-11-03 16:09:21 -08001321 mSurfaceController.setOpaque(isOpaque);
Craig Mautner71dd1b62014-02-18 15:48:52 -08001322 }
1323
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001324 void setSecureLocked(boolean isSecure) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001325 if (mSurfaceController == null) {
1326 return;
1327 }
Robert Carre6a83512015-11-03 16:09:21 -08001328 mSurfaceController.setSecure(isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001329 }
1330
Peiyong Lin75045382019-03-04 19:22:33 -08001331 void setColorSpaceAgnosticLocked(boolean agnostic) {
1332 if (mSurfaceController == null) {
1333 return;
1334 }
1335 mSurfaceController.setColorSpaceAgnostic(agnostic);
1336 }
1337
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001338 /**
1339 * Have the surface flinger show a surface, robustly dealing with
1340 * error conditions. In particular, if there is not enough memory
1341 * to show the surface, then we will try to get rid of other surfaces
1342 * in order to succeed.
1343 *
1344 * @return Returns true if the surface was successfully shown.
1345 */
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001346 private boolean showSurfaceRobustlyLocked() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001347 if (mWin.getWindowConfiguration().windowsAreScaleable()) {
Robert Carr1b5ea722016-04-20 13:23:28 -07001348 mSurfaceController.forceScaleableInTransaction(true);
1349 }
1350
Robert Carre6a83512015-11-03 16:09:21 -08001351 boolean shown = mSurfaceController.showRobustlyInTransaction();
1352 if (!shown)
1353 return false;
1354
Robert Carr849d2932018-05-01 12:14:48 -07001355 // If we had a preserved surface it's no longer needed, and it may be harmful
1356 // if we are transparent.
1357 if (mPendingDestroySurface != null && mDestroyPreservedSurfaceUponRedraw) {
Vishnu Nairf7645aa2019-06-18 11:14:01 -07001358 final SurfaceControl pendingSurfaceControl = mPendingDestroySurface.mSurfaceControl;
Vishnu Nair8cb00ae2019-08-02 15:20:29 -07001359 mPostDrawTransaction.reparent(pendingSurfaceControl, null);
Rob Carr698b6842020-03-06 15:33:31 -08001360 mPostDrawTransaction.reparentChildren(
1361 mPendingDestroySurface.getClientViewRootSurface(),
1362 mSurfaceController.mSurfaceControl);
Robert Carr849d2932018-05-01 12:14:48 -07001363 }
1364
Vishnu Nair3b037f72019-07-22 12:55:58 -07001365 SurfaceControl.mergeToGlobalTransaction(mPostDrawTransaction);
Robert Carre6a83512015-11-03 16:09:21 -08001366 return true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001367 }
1368
1369 void applyEnterAnimationLocked() {
Robert Carrb439a632016-04-07 22:52:10 -07001370 // If we are the new part of a window replacement transition and we have requested
1371 // not to animate, we instead want to make it seamless, so we don't want to apply
1372 // an enter transition.
1373 if (mWin.mSkipEnterAnimationForSeamlessReplacement) {
1374 return;
1375 }
lumarkce596d32019-06-12 16:58:35 +08001376
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001377 final int transit;
1378 if (mEnterAnimationPending) {
1379 mEnterAnimationPending = false;
1380 transit = WindowManagerPolicy.TRANSIT_ENTER;
1381 } else {
1382 transit = WindowManagerPolicy.TRANSIT_SHOW;
1383 }
lumark2726eb62019-06-19 21:25:54 +08001384
1385 // We don't apply animation for application main window here since this window type
1386 // should be controlled by AppWindowToken in general.
1387 if (mAttrType != TYPE_BASE_APPLICATION) {
1388 applyAnimationLocked(transit, true);
1389 }
1390
Rhed Jao02655dc2018-10-30 20:44:52 +08001391 if (mService.mAccessibilityController != null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001392 mService.mAccessibilityController.onWindowTransitionLocked(mWin, transit);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001393 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001394 }
1395
1396 /**
1397 * Choose the correct animation and set it to the passed WindowState.
Craig Mautner4b71aa12012-12-27 17:20:01 -08001398 * @param transit If AppTransition.TRANSIT_PREVIEW_DONE and the app window has been drawn
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001399 * then the animation will be app_starting_exit. Any other value loads the animation from
1400 * the switch statement below.
1401 * @param isEntrance The animation type the last time this was called. Used to keep from
1402 * loading the same animation twice.
1403 * @return true if an animation has been loaded.
1404 */
1405 boolean applyAnimationLocked(int transit, boolean isEntrance) {
lumark9bca6b42019-10-17 18:35:22 +08001406 if (mWin.isAnimating() && mAnimationIsEntrance == isEntrance) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001407 // If we are trying to apply an animation, but already running
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001408 // an animation of the same type, then just leave that one alone.
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001409 return true;
1410 }
1411
Taran Singhf69425e2020-04-01 16:57:50 -07001412 final boolean isImeWindow = mWin.mAttrs.type == TYPE_INPUT_METHOD;
1413 if (isEntrance && isImeWindow) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01001414 mWin.getDisplayContent().adjustForImeIfNeeded();
1415 mWin.setDisplayLayoutNeeded();
1416 mService.mWindowPlacerLocked.requestTraversal();
1417 }
1418
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001419 // Only apply an animation if the display isn't frozen. If it is
1420 // frozen, there is no reason to animate and it can cause strange
1421 // artifacts when we unfreeze the display if some different animation
1422 // is running.
David Stevens9440dc82017-03-16 19:00:20 -07001423 if (mWin.mToken.okToAnimate()) {
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001424 int anim = mWin.getDisplayContent().getDisplayPolicy().selectAnimation(mWin, transit);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001425 int attr = -1;
1426 Animation a = null;
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001427 if (anim != DisplayPolicy.ANIMATION_STYLEABLE) {
1428 if (anim != DisplayPolicy.ANIMATION_NONE) {
1429 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "WSA#loadAnimation");
1430 a = AnimationUtils.loadAnimation(mContext, anim);
1431 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
1432 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001433 } else {
1434 switch (transit) {
1435 case WindowManagerPolicy.TRANSIT_ENTER:
1436 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1437 break;
1438 case WindowManagerPolicy.TRANSIT_EXIT:
1439 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1440 break;
1441 case WindowManagerPolicy.TRANSIT_SHOW:
1442 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1443 break;
1444 case WindowManagerPolicy.TRANSIT_HIDE:
1445 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1446 break;
1447 }
1448 if (attr >= 0) {
lumark588a3e82018-07-20 18:53:54 +08001449 a = mWin.getDisplayContent().mAppTransition.loadAnimationAttr(
1450 mWin.mAttrs, attr, TRANSIT_NONE);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001451 }
1452 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001453 if (DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001454 "applyAnimation: win=" + this
1455 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
Craig Mautner4d7349b2012-04-20 14:52:47 -07001456 + " a=" + a
Craig Mautner8863cca2012-09-18 15:04:34 -07001457 + " transit=" + transit
lumarkce596d32019-06-12 16:58:35 +08001458 + " type=" + mAttrType
Craig Mautner83339b42012-05-01 22:13:23 -07001459 + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001460 if (a != null) {
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -08001461 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001462 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "WSA#startAnimation");
Jorim Jaggia5e10572017-11-15 14:36:26 +01001463 mWin.startAnimation(a);
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001464 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001465 mAnimationIsEntrance = isEntrance;
1466 }
Taran Singhf69425e2020-04-01 16:57:50 -07001467 } else if (!isImeWindow) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01001468 mWin.cancelAnimation();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001469 }
Chong Zhang8784be62016-06-28 15:25:07 -07001470
Taran Singhf69425e2020-04-01 16:57:50 -07001471 if (!isEntrance && isImeWindow) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001472 mWin.getDisplayContent().adjustForImeIfNeeded();
Jorim Jaggieb88d832016-04-13 20:17:43 -07001473 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001474
lumark5341d1c2019-12-14 01:54:02 +08001475 return mWin.isAnimating(PARENTS);
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001476 }
1477
Jeffrey Huangcb782852019-12-05 11:28:11 -08001478 void dumpDebug(ProtoOutputStream proto, long fieldId) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07001479 final long token = proto.start(fieldId);
Jeffrey Huangcb782852019-12-05 11:28:11 -08001480 mLastClipRect.dumpDebug(proto, LAST_CLIP_RECT);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001481 if (mSurfaceController != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08001482 mSurfaceController.dumpDebug(proto, SURFACE);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001483 }
Vishnu Nair1d0fa072018-01-04 07:53:00 -08001484 proto.write(DRAW_STATE, mDrawState);
Jeffrey Huangcb782852019-12-05 11:28:11 -08001485 mSystemDecorRect.dumpDebug(proto, SYSTEM_DECOR_RECT);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001486 proto.end(token);
1487 }
1488
Craig Mautnera2c77052012-03-26 12:14:43 -07001489 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01001490 if (mAnimationIsEntrance) {
1491 pw.print(prefix); pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
Craig Mautnera2c77052012-03-26 12:14:43 -07001492 }
Robert Carre6a83512015-11-03 16:09:21 -08001493 if (mSurfaceController != null) {
1494 mSurfaceController.dump(pw, prefix, dumpAll);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001495 }
Robert Carre6a83512015-11-03 16:09:21 -08001496 if (dumpAll) {
1497 pw.print(prefix); pw.print("mDrawState="); pw.print(drawStateToString());
1498 pw.print(prefix); pw.print(" mLastHidden="); pw.println(mLastHidden);
minchelia8733092020-04-16 14:52:27 +08001499 pw.print(prefix); pw.print("mEnterAnimationPending=" + mEnterAnimationPending);
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001500 pw.print(prefix); pw.print("mSystemDecorRect="); mSystemDecorRect.printShortString(pw);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001501 pw.print(" mLastClipRect="); mLastClipRect.printShortString(pw);
1502
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001503 if (!mLastFinalClipRect.isEmpty()) {
1504 pw.print(" mLastFinalClipRect="); mLastFinalClipRect.printShortString(pw);
1505 }
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001506 pw.println();
Robert Carre6a83512015-11-03 16:09:21 -08001507 }
1508
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001509 if (mPendingDestroySurface != null) {
1510 pw.print(prefix); pw.print("mPendingDestroySurface=");
1511 pw.println(mPendingDestroySurface);
1512 }
1513 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1514 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1515 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1516 }
1517 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1518 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1519 pw.print(" mAlpha="); pw.print(mAlpha);
1520 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1521 }
1522 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1523 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1524 pw.print(" mDsDx="); pw.print(mDsDx);
1525 pw.print(" mDtDx="); pw.print(mDtDx);
Robert Carr0edf18f2017-02-21 20:01:47 -08001526 pw.print(" mDtDy="); pw.print(mDtDy);
1527 pw.print(" mDsDy="); pw.println(mDsDy);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001528 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001529 }
1530
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001531 @Override
1532 public String toString() {
Dianne Hackborn529e7442012-11-01 14:22:28 -07001533 StringBuffer sb = new StringBuffer("WindowStateAnimator{");
1534 sb.append(Integer.toHexString(System.identityHashCode(this)));
1535 sb.append(' ');
1536 sb.append(mWin.mAttrs.getTitle());
1537 sb.append('}');
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001538 return sb.toString();
1539 }
Robert Carre6a83512015-11-03 16:09:21 -08001540
1541 void reclaimSomeSurfaceMemory(String operation, boolean secure) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001542 mService.mRoot.reclaimSomeSurfaceMemory(this, operation, secure);
Robert Carre6a83512015-11-03 16:09:21 -08001543 }
1544
1545 boolean getShown() {
1546 if (mSurfaceController != null) {
1547 return mSurfaceController.getShown();
1548 }
1549 return false;
1550 }
1551
1552 void destroySurface() {
Wale Ogunwale722ff892016-02-18 13:37:55 -08001553 try {
1554 if (mSurfaceController != null) {
Robert Carra8828862018-02-05 16:17:36 -08001555 mSurfaceController.destroyNotInTransaction();
Wale Ogunwale722ff892016-02-18 13:37:55 -08001556 }
1557 } catch (RuntimeException e) {
1558 Slog.w(TAG, "Exception thrown when destroying surface " + this
1559 + " surface " + mSurfaceController + " session " + mSession + ": " + e);
1560 } finally {
1561 mWin.setHasSurface(false);
1562 mSurfaceController = null;
1563 mDrawState = NO_SURFACE;
1564 }
Robert Carre6a83512015-11-03 16:09:21 -08001565 }
Filip Gruszczynskif52dd202015-11-15 20:36:38 -08001566
Robert Carr8f0a3ad2017-02-15 19:30:28 -08001567 /** The force-scaled state for a given window can persist past
1568 * the state for it's stack as the windows complete resizing
1569 * independently of one another.
1570 */
1571 boolean isForceScaled() {
1572 final Task task = mWin.getTask();
Wale Ogunwale0b3d2922019-12-30 08:55:07 -08001573 if (task != null && task.getStack().isForceScaled()) {
Robert Carr8f0a3ad2017-02-15 19:30:28 -08001574 return true;
1575 }
1576 return mForceScaleUntilResize;
1577 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001578
1579 void detachChildren() {
Jorim Jaggi35e049a2019-07-16 15:03:14 +02001580
1581 // Do not detach children of starting windows, as their lifecycle is well under control and
1582 // it may lead to issues in case we relaunch when we just added the starting window.
1583 if (mWin.mAttrs.type == TYPE_APPLICATION_STARTING) {
1584 return;
1585 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001586 if (mSurfaceController != null) {
1587 mSurfaceController.detachChildren();
1588 }
Robert Carr7b3d11d2018-03-15 14:34:45 -07001589 mChildrenDetached = true;
Robert Carrd5c7dd62017-03-08 10:39:30 -08001590 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01001591
chaviwbe43ac82018-04-04 15:14:49 -07001592 void setOffsetPositionForStackResize(boolean offsetPositionForStackResize) {
1593 mOffsetPositionForStackResize = offsetPositionForStackResize;
1594 }
Robert Carr2e20bcd2020-01-22 13:32:38 -08001595
Rob Carr698b6842020-03-06 15:33:31 -08001596 SurfaceControl getClientViewRootSurface() {
Robert Carr2e20bcd2020-01-22 13:32:38 -08001597 if (!hasSurface()) {
1598 return null;
1599 }
Rob Carr698b6842020-03-06 15:33:31 -08001600 return mSurfaceController.getClientViewRootSurface();
Robert Carr2e20bcd2020-01-22 13:32:38 -08001601 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001602}