blob: 053ee970f9ae8f34257a26b7a144b5751922f8c9 [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
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -070019import static android.app.ActivityManager.StackId;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080020import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwalee7bf46b2015-09-30 09:19:28 -070021import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080022import static android.view.WindowManager.LayoutParams.FLAG_SCALED;
Robert Carr132c9f52017-07-31 17:02:30 -070023import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070024import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Jorim Jaggieb88d832016-04-13 20:17:43 -070025import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Robert Carrfbbde852016-10-18 11:02:28 -070026import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Jorim Jaggiff71d202016-04-14 13:12:36 -070027import static com.android.server.wm.AppWindowAnimator.sDummyAnimation;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010028import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080029import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
30import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
32import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
33import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020034import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW_VERBOSE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080035import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE;
36import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
37import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -080038import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_CROP;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080039import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
40import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
41import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
42import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
43import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070044import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080045import static com.android.server.wm.WindowManagerService.localLOGV;
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -080046import static com.android.server.wm.WindowManagerService.logWithStack;
Filip Gruszczynski4501d232015-09-02 13:00:02 -070047import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
48import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070049
Craig Mautnerc2f9be02012-03-27 17:32:29 -070050import android.content.Context;
51import android.graphics.Matrix;
52import android.graphics.PixelFormat;
Craig Mautner7358fbf2012-04-12 21:06:33 -070053import android.graphics.Point;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070054import android.graphics.Rect;
Robert Carr6da3cc02016-06-16 15:17:07 -070055import android.graphics.RectF;
Craig Mautner48ba1e72012-04-02 13:18:16 -070056import android.graphics.Region;
Craig Mautnera51a9562012-04-17 17:05:26 -070057import android.os.Debug;
Chong Zhang8784be62016-06-28 15:25:07 -070058import android.os.Trace;
Craig Mautnera2c77052012-03-26 12:14:43 -070059import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070060import android.view.DisplayInfo;
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070061import android.view.MagnificationSpec;
Igor Murashkina86ab6402013-08-30 12:58:36 -070062import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080063import android.view.SurfaceControl;
Craig Mautnera2c77052012-03-26 12:14:43 -070064import android.view.WindowManager;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070065import android.view.WindowManager.LayoutParams;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080066import android.view.WindowManagerPolicy;
Craig Mautnera2c77052012-03-26 12:14:43 -070067import android.view.animation.Animation;
Jorim Jaggif8d77da2014-11-11 16:59:12 +010068import android.view.animation.AnimationSet;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070069import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070070import android.view.animation.Transformation;
71
Craig Mautnera2c77052012-03-26 12:14:43 -070072import java.io.PrintWriter;
Robert Carr3b716242016-08-16 16:02:21 -070073import java.io.FileDescriptor;
Craig Mautnera2c77052012-03-26 12:14:43 -070074
75/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070076 * Keep track of animations and surface operations for a single WindowState.
77 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070078class WindowStateAnimator {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080079 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowStateAnimator" : TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070080 static final int WINDOW_FREEZE_LAYER = TYPE_LAYER_MULTIPLIER * 200;
Craig Mautnera2c77052012-03-26 12:14:43 -070081
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +010082 /**
83 * Mode how the window gets clipped by the stack bounds during an animation: The clipping should
84 * be applied after applying the animation transformation, i.e. the stack bounds don't move
85 * during the animation.
86 */
87 static final int STACK_CLIP_AFTER_ANIM = 0;
88
89 /**
90 * Mode how the window gets clipped by the stack bounds: The clipping should be applied before
91 * applying the animation transformation, i.e. the stack bounds move with the window.
92 */
93 static final int STACK_CLIP_BEFORE_ANIM = 1;
94
95 /**
96 * Mode how window gets clipped by the stack bounds during an animation: Don't clip the window
97 * by the stack bounds.
98 */
99 static final int STACK_CLIP_NONE = 2;
100
Craig Mautner918b53b2012-07-09 14:15:54 -0700101 // Unchanging local convenience fields.
Craig Mautnera2c77052012-03-26 12:14:43 -0700102 final WindowManagerService mService;
103 final WindowState mWin;
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700104 private final WindowStateAnimator mParentWinAnimator;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700105 final WindowAnimator mAnimator;
Craig Mautner8863cca2012-09-18 15:04:34 -0700106 AppWindowAnimator mAppAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700107 final Session mSession;
108 final WindowManagerPolicy mPolicy;
109 final Context mContext;
Craig Mautner918b53b2012-07-09 14:15:54 -0700110 final boolean mIsWallpaper;
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700111 private final WallpaperController mWallpaperControllerLocked;
Craig Mautnera2c77052012-03-26 12:14:43 -0700112
113 // Currently running animation.
114 boolean mAnimating;
115 boolean mLocalAnimating;
116 Animation mAnimation;
117 boolean mAnimationIsEntrance;
118 boolean mHasTransformation;
119 boolean mHasLocalTransformation;
120 final Transformation mTransformation = new Transformation();
121 boolean mWasAnimating; // Were we animating going into the most recent animation step?
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700122 int mAnimLayer;
123 int mLastLayer;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100124 long mAnimationStartTime;
Jorim Jaggif8d77da2014-11-11 16:59:12 +0100125 long mLastAnimationTime;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100126 int mStackClip = STACK_CLIP_BEFORE_ANIM;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700127
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700128 /**
129 * Set when we have changed the size of the surface, to know that
130 * we must tell them application to resize (and thus redraw itself).
131 */
132 boolean mSurfaceResized;
Chong Zhang5b2f1992015-11-13 15:40:36 -0800133 /**
134 * Whether we should inform the client on next relayoutWindow that
135 * the surface has been resized since last time.
136 */
137 boolean mReportSurfaceResized;
Robert Carre6a83512015-11-03 16:09:21 -0800138 WindowSurfaceController mSurfaceController;
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800139 private WindowSurfaceController mPendingDestroySurface;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700140
141 /**
142 * Set if the client has asked that the destroy of its surface be delayed
143 * until it explicitly says it is okay.
144 */
145 boolean mSurfaceDestroyDeferred;
146
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800147 private boolean mDestroyPreservedSurfaceUponRedraw;
Craig Mautner7d8df392012-04-06 15:26:23 -0700148 float mShownAlpha = 0;
149 float mAlpha = 0;
150 float mLastAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700151
Winson Chung399f6202014-03-19 10:47:20 -0700152 boolean mHasClipRect;
153 Rect mClipRect = new Rect();
154 Rect mTmpClipRect = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100155 Rect mTmpFinalClipRect = new Rect();
Winson Chung399f6202014-03-19 10:47:20 -0700156 Rect mLastClipRect = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100157 Rect mLastFinalClipRect = new Rect();
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -0700158 Rect mTmpStackBounds = new Rect();
Winson Chung08f81892017-03-02 15:40:51 -0800159 private Rect mTmpAnimatingBounds = new Rect();
160 private Rect mTmpSourceBounds = new Rect();
Winson Chung399f6202014-03-19 10:47:20 -0700161
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -0800162 /**
163 * This is rectangle of the window's surface that is not covered by
164 * system decorations.
165 */
166 private final Rect mSystemDecorRect = new Rect();
167 private final Rect mLastSystemDecorRect = new Rect();
168
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800169 // Used to save animation distances between the time they are calculated and when they are used.
170 private int mAnimDx;
171 private int mAnimDy;
Jorim Jaggic5af4f82015-07-01 17:16:27 -0700172
173 /** Is the next animation to be started a window move animation? */
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800174 private boolean mAnimateMove = false;
Jorim Jaggic5af4f82015-07-01 17:16:27 -0700175
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700176 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
Robert Carr0edf18f2017-02-21 20:01:47 -0800177 private float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700178
179 boolean mHaveMatrix;
180
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700181 // Set to true if, when the window gets displayed, it should perform
182 // an enter animation.
183 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700184
Craig Mautner9c795042014-10-28 19:59:59 -0700185 /** Used to indicate that this window is undergoing an enter animation. Used for system
186 * windows to make the callback to View.dispatchOnWindowShownCallback(). Set when the
187 * window is first added or shown, cleared when the callback has been made. */
188 boolean mEnteringAnimation;
189
Jorim Jaggiff71d202016-04-14 13:12:36 -0700190 private boolean mAnimationStartDelayed;
191
John Reck80181b92015-05-19 11:09:32 -0700192 /** The pixel format of the underlying SurfaceControl */
193 int mSurfaceFormat;
194
Craig Mautner749a7bb2012-04-02 13:49:53 -0700195 /** This is set when there is no Surface */
196 static final int NO_SURFACE = 0;
197 /** This is set after the Surface has been created but before the window has been drawn. During
198 * this time the surface is hidden. */
199 static final int DRAW_PENDING = 1;
200 /** This is set after the window has finished drawing for the first time but before its surface
201 * is shown. The surface will be displayed when the next layout is run. */
202 static final int COMMIT_DRAW_PENDING = 2;
203 /** This is set during the time after the window's drawing has been committed, and before its
204 * surface is actually shown. It is used to delay showing the surface until all windows in a
205 * token are ready to be shown. */
206 static final int READY_TO_SHOW = 3;
207 /** Set when the window has been shown in the screen the first time. */
208 static final int HAS_DRAWN = 4;
Adrian Roos3eeb4e62014-05-19 12:43:26 +0200209
Craig Mautner276a6eb2014-11-04 15:32:57 -0800210 String drawStateToString() {
211 switch (mDrawState) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700212 case NO_SURFACE: return "NO_SURFACE";
213 case DRAW_PENDING: return "DRAW_PENDING";
214 case COMMIT_DRAW_PENDING: return "COMMIT_DRAW_PENDING";
215 case READY_TO_SHOW: return "READY_TO_SHOW";
216 case HAS_DRAWN: return "HAS_DRAWN";
Craig Mautner276a6eb2014-11-04 15:32:57 -0800217 default: return Integer.toString(mDrawState);
Craig Mautner6fbda632012-07-03 09:26:39 -0700218 }
219 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700220 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700221
Craig Mautner749a7bb2012-04-02 13:49:53 -0700222 /** Was this window last hidden? */
223 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700224
Craig Mautnerbec53f72012-04-05 11:49:05 -0700225 int mAttrType;
226
Robert Carr679c8072016-04-07 15:51:48 -0700227 static final long PENDING_TRANSACTION_FINISH_WAIT_TIME = 100;
Robert Carr679c8072016-04-07 15:51:48 -0700228
Robert Carrc7294602016-05-13 11:32:05 -0700229 boolean mForceScaleUntilResize;
230
Robert Carr04092112016-06-02 12:56:12 -0700231 // WindowState.mHScale and WindowState.mVScale contain the
232 // scale according to client specified layout parameters (e.g.
233 // one layout size, with another surface size, creates such scaling).
234 // Here we track an additional scaling factor used to follow stack
235 // scaling (as in the case of the Pinned stack animation).
236 float mExtraHScale = (float) 1.0;
237 float mExtraVScale = (float) 1.0;
238
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800239 private final Rect mTmpSize = new Rect();
240
Craig Mautnerc431e892015-02-11 13:14:26 -0800241 WindowStateAnimator(final WindowState win) {
Craig Mautner918b53b2012-07-09 14:15:54 -0700242 final WindowManagerService service = win.mService;
243
Craig Mautnera2c77052012-03-26 12:14:43 -0700244 mService = service;
Craig Mautner918b53b2012-07-09 14:15:54 -0700245 mAnimator = service.mAnimator;
246 mPolicy = service.mPolicy;
247 mContext = service.mContext;
Craig Mautnerd3849f52014-03-27 13:19:29 -0700248 final DisplayContent displayContent = win.getDisplayContent();
249 if (displayContent != null) {
250 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800251 mAnimDx = displayInfo.appWidth;
252 mAnimDy = displayInfo.appHeight;
Craig Mautnerd3849f52014-03-27 13:19:29 -0700253 } else {
254 Slog.w(TAG, "WindowStateAnimator ctor: Display has been removed");
255 // This is checked on return and dealt with.
256 }
Craig Mautner918b53b2012-07-09 14:15:54 -0700257
258 mWin = win;
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700259 mParentWinAnimator = !win.isChildWindow() ? null : win.getParentWindow().mWinAnimator;
Craig Mautner322e4032012-07-13 13:35:20 -0700260 mAppAnimator = win.mAppToken == null ? null : win.mAppToken.mAppAnimator;
Craig Mautner918b53b2012-07-09 14:15:54 -0700261 mSession = win.mSession;
Craig Mautner918b53b2012-07-09 14:15:54 -0700262 mAttrType = win.mAttrs.type;
263 mIsWallpaper = win.mIsWallpaper;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700264 mWallpaperControllerLocked = mService.mRoot.mWallpaperController;
Craig Mautnera2c77052012-03-26 12:14:43 -0700265 }
266
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100267 public void setAnimation(Animation anim, long startTime, int stackClip) {
Craig Mautnerbec53f72012-04-05 11:49:05 -0700268 if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700269 mAnimating = false;
270 mLocalAnimating = false;
271 mAnimation = anim;
272 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700273 mAnimation.scaleCurrentDuration(mService.getWindowAnimationScaleLocked());
Craig Mautnera2c77052012-03-26 12:14:43 -0700274 // Start out animation gone if window is gone, or visible if window is visible.
275 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700276 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700277 mHasLocalTransformation = true;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100278 mAnimationStartTime = startTime;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100279 mStackClip = stackClip;
280 }
281
282 public void setAnimation(Animation anim, int stackClip) {
283 setAnimation(anim, -1, stackClip);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100284 }
285
286 public void setAnimation(Animation anim) {
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100287 setAnimation(anim, -1, STACK_CLIP_AFTER_ANIM);
Craig Mautnera2c77052012-03-26 12:14:43 -0700288 }
289
290 public void clearAnimation() {
291 if (mAnimation != null) {
292 mAnimating = true;
293 mLocalAnimating = false;
294 mAnimation.cancel();
295 mAnimation = null;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100296 mStackClip = STACK_CLIP_BEFORE_ANIM;
Craig Mautnera2c77052012-03-26 12:14:43 -0700297 }
298 }
299
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700300 /**
301 * Is the window or its container currently set to animate or currently animating?
302 */
303 boolean isAnimationSet() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700304 return mAnimation != null
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700305 || (mParentWinAnimator != null && mParentWinAnimator.mAnimation != null)
Jorim Jaggi37875612015-02-19 21:05:31 +0100306 || (mAppAnimator != null && mAppAnimator.isAnimating());
Craig Mautnera2c77052012-03-26 12:14:43 -0700307 }
308
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700309 /**
310 * @return whether an animation is about to start, i.e. the animation is set already but we
311 * haven't processed the first frame yet.
312 */
313 boolean isAnimationStarting() {
314 return isAnimationSet() && !mAnimating;
315 }
316
Craig Mautner0afddcb2012-05-08 15:38:00 -0700317 /** Is the window animating the DummyAnimation? */
318 boolean isDummyAnimation() {
Craig Mautner322e4032012-07-13 13:35:20 -0700319 return mAppAnimator != null
Jorim Jaggiff71d202016-04-14 13:12:36 -0700320 && mAppAnimator.animation == sDummyAnimation;
Craig Mautner0afddcb2012-05-08 15:38:00 -0700321 }
322
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700323 /**
324 * Is this window currently set to animate or currently animating?
325 */
326 boolean isWindowAnimationSet() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700327 return mAnimation != null;
328 }
329
Chong Zhange22006d2016-05-09 10:59:59 -0700330 /**
331 * Is this window currently waiting to run an opening animation?
332 */
333 boolean isWaitingForOpening() {
334 return mService.mAppTransition.isTransitionSet() && isDummyAnimation()
335 && mService.mOpeningApps.contains(mWin.mAppToken);
336 }
337
Craig Mautnera2c77052012-03-26 12:14:43 -0700338 void cancelExitAnimationForNextAnimationLocked() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800339 if (DEBUG_ANIM) Slog.d(TAG,
340 "cancelExitAnimationForNextAnimationLocked: " + mWin);
341
Craig Mautnera2c77052012-03-26 12:14:43 -0700342 if (mAnimation != null) {
343 mAnimation.cancel();
344 mAnimation = null;
Craig Mautner4d7349b2012-04-20 14:52:47 -0700345 mLocalAnimating = false;
Robert Carr89a28ab2017-04-24 15:33:11 -0700346 mWin.destroyOrSaveSurfaceUnchecked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700347 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700348 }
349
350 private boolean stepAnimation(long currentTime) {
351 if ((mAnimation == null) || !mLocalAnimating) {
352 return false;
353 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700354 currentTime = getAnimationFrameTime(mAnimation, currentTime);
Craig Mautnera2c77052012-03-26 12:14:43 -0700355 mTransformation.clear();
356 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700357 if (mAnimationStartDelayed && mAnimationIsEntrance) {
358 mTransformation.setAlpha(0f);
359 }
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700360 if (false && DEBUG_ANIM) Slog.v(TAG, "Stepped animation in " + this + ": more=" + more
361 + ", xform=" + mTransformation);
Craig Mautnera2c77052012-03-26 12:14:43 -0700362 return more;
363 }
364
365 // This must be called while inside a transaction. Returns true if
366 // there is more animation to run.
367 boolean stepAnimationLocked(long currentTime) {
Chet Haase9c450412015-10-01 13:25:58 -0700368 // Save the animation state as it was before this step so WindowManagerService can tell if
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700369 // we just started or just stopped animating by comparing mWasAnimating with isAnimationSet().
Chet Haase9c450412015-10-01 13:25:58 -0700370 mWasAnimating = mAnimating;
Craig Mautnerdf88d732014-01-27 09:21:32 -0800371 final DisplayContent displayContent = mWin.getDisplayContent();
David Stevensf62360c2017-03-16 19:00:20 -0700372 if (mWin.mToken.okToAnimate()) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700373 // We will run animations as long as the display isn't frozen.
374
375 if (mWin.isDrawnLw() && mAnimation != null) {
376 mHasTransformation = true;
377 mHasLocalTransformation = true;
378 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700379 if (DEBUG_ANIM) Slog.v(
380 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700381 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
382 " wh=" + mWin.mFrame.height() +
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800383 " dx=" + mAnimDx + " dy=" + mAnimDy +
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700384 " scale=" + mService.getWindowAnimationScaleLocked());
Craig Mautnerdf88d732014-01-27 09:21:32 -0800385 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
tiger_huangb7f3f922015-04-27 20:36:46 +0800386 if (mAnimateMove) {
387 mAnimateMove = false;
388 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800389 mAnimDx, mAnimDy);
tiger_huangb7f3f922015-04-27 20:36:46 +0800390 } else {
391 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
392 displayInfo.appWidth, displayInfo.appHeight);
393 }
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800394 mAnimDx = displayInfo.appWidth;
395 mAnimDy = displayInfo.appHeight;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100396 mAnimation.setStartTime(mAnimationStartTime != -1
397 ? mAnimationStartTime
398 : currentTime);
Craig Mautnera2c77052012-03-26 12:14:43 -0700399 mLocalAnimating = true;
400 mAnimating = true;
401 }
402 if ((mAnimation != null) && mLocalAnimating) {
Jorim Jaggif8d77da2014-11-11 16:59:12 +0100403 mLastAnimationTime = currentTime;
Craig Mautnera2c77052012-03-26 12:14:43 -0700404 if (stepAnimation(currentTime)) {
405 return true;
406 }
407 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700408 if (DEBUG_ANIM) Slog.v(
409 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700410 " @ " + currentTime);
411 //WindowManagerService.this.dump();
412 }
413 mHasLocalTransformation = false;
Craig Mautner322e4032012-07-13 13:35:20 -0700414 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppAnimator != null
415 && mAppAnimator.animation != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700416 // When our app token is animating, we kind-of pretend like
417 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
418 // part of this check means that we will only do this if
419 // our window is not currently exiting, or it is not
420 // locally animating itself. The idea being that one that
421 // is exiting and doing a local animation should be removed
422 // once that animation is done.
423 mAnimating = true;
424 mHasTransformation = true;
425 mTransformation.clear();
426 return false;
427 } else if (mHasTransformation) {
428 // Little trick to get through the path below to act like
429 // we have finished an animation.
430 mAnimating = true;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700431 } else if (isAnimationSet()) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700432 mAnimating = true;
433 }
434 } else if (mAnimation != null) {
435 // If the display is frozen, and there is a pending animation,
436 // clear it and make sure we run the cleanup code.
437 mAnimating = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700438 }
439
440 if (!mAnimating && !mLocalAnimating) {
441 return false;
442 }
443
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700444 // Done animating, clean up.
445 if (DEBUG_ANIM) Slog.v(
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800446 TAG, "Animation done in " + this + ": exiting=" + mWin.mAnimatingExit
Craig Mautnera2c77052012-03-26 12:14:43 -0700447 + ", reportedVisible="
448 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
449
450 mAnimating = false;
451 mLocalAnimating = false;
452 if (mAnimation != null) {
453 mAnimation.cancel();
454 mAnimation = null;
455 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700456 if (mAnimator.mWindowDetachedWallpaper == mWin) {
457 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700458 }
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700459 mAnimLayer = mWin.getSpecialWindowAnimLayerAdjustment();
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800460 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700461 mHasTransformation = false;
462 mHasLocalTransformation = false;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100463 mStackClip = STACK_CLIP_BEFORE_ANIM;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800464 mWin.checkPolicyVisibilityChange();
Craig Mautnera2c77052012-03-26 12:14:43 -0700465 mTransformation.clear();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800466 if (mAttrType == LayoutParams.TYPE_STATUS_BAR && mWin.mPolicyVisibility) {
Craig Mautner81defc72013-10-29 11:10:42 -0700467 // Upon completion of a not-visible to visible status bar animation a relayout is
468 // required.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800469 if (displayContent != null) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700470 displayContent.setLayoutNeeded();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800471 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700472 }
473
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700474 mWin.onExitAnimationDone();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800475 final int displayId = mWin.getDisplayId();
Craig Mautner76a71652012-09-03 23:23:58 -0700476 mAnimator.setPendingLayoutChanges(displayId, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800477 if (DEBUG_LAYOUT_REPEATS)
Filip Gruszczynski4501d232015-09-02 13:00:02 -0700478 mService.mWindowPlacerLocked.debugLayoutRepeats(
479 "WindowStateAnimator", mAnimator.getPendingLayoutChanges(displayId));
Craig Mautnera2c77052012-03-26 12:14:43 -0700480
481 if (mWin.mAppToken != null) {
482 mWin.mAppToken.updateReportedVisibilityLocked();
483 }
484
485 return false;
486 }
487
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800488 void hide(String reason) {
Craig Mautner0afddcb2012-05-08 15:38:00 -0700489 if (!mLastHidden) {
490 //dump();
491 mLastHidden = true;
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800492 if (mSurfaceController != null) {
493 mSurfaceController.hideInTransaction(reason);
494 }
Craig Mautner0afddcb2012-05-08 15:38:00 -0700495 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700496 }
497
Craig Mautnera608b882012-03-30 13:03:49 -0700498 boolean finishDrawingLocked() {
Craig Mautner42d04db2014-11-06 12:13:23 -0800499 final boolean startingWindow =
500 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
501 if (DEBUG_STARTING_WINDOW && startingWindow) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700502 Slog.v(TAG, "Finishing drawing window " + mWin + ": mDrawState="
Craig Mautner276a6eb2014-11-04 15:32:57 -0800503 + drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700504 }
Robert Carr1ca6a332016-04-11 18:00:43 -0700505
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700506 boolean layoutNeeded = mWin.clearAnimatingWithSavedSurface();
Chong Zhang92147042016-05-09 12:47:11 -0700507
Craig Mautner749a7bb2012-04-02 13:49:53 -0700508 if (mDrawState == DRAW_PENDING) {
Craig Mautneref25d7a2012-05-15 23:01:47 -0700509 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Chong Zhang5b2f1992015-11-13 15:40:36 -0800510 Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + mWin + " in "
Robert Carre6a83512015-11-03 16:09:21 -0800511 + mSurfaceController);
Craig Mautner42d04db2014-11-06 12:13:23 -0800512 if (DEBUG_STARTING_WINDOW && startingWindow) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700513 Slog.v(TAG, "Draw state now committed in " + mWin);
514 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700515 mDrawState = COMMIT_DRAW_PENDING;
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700516 layoutNeeded = true;
Craig Mautnera608b882012-03-30 13:03:49 -0700517 }
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700518
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700519 return layoutNeeded;
Craig Mautnera608b882012-03-30 13:03:49 -0700520 }
521
522 // This must be called while inside a transaction.
Craig Mautnerc431e892015-02-11 13:14:26 -0800523 boolean commitFinishDrawingLocked() {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200524 if (DEBUG_STARTING_WINDOW_VERBOSE &&
Craig Mautner6fbda632012-07-03 09:26:39 -0700525 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
526 Slog.i(TAG, "commitFinishDrawingLocked: " + mWin + " cur mDrawState="
Craig Mautner276a6eb2014-11-04 15:32:57 -0800527 + drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700528 }
Craig Mautner276a6eb2014-11-04 15:32:57 -0800529 if (mDrawState != COMMIT_DRAW_PENDING && mDrawState != READY_TO_SHOW) {
Craig Mautnera608b882012-03-30 13:03:49 -0700530 return false;
531 }
Craig Mautner6fbda632012-07-03 09:26:39 -0700532 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM) {
Robert Carre6a83512015-11-03 16:09:21 -0800533 Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurfaceController);
Craig Mautner6fbda632012-07-03 09:26:39 -0700534 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700535 mDrawState = READY_TO_SHOW;
Chong Zhang0275e392015-09-17 10:41:44 -0700536 boolean result = false;
Craig Mautnera608b882012-03-30 13:03:49 -0700537 final AppWindowToken atoken = mWin.mAppToken;
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800538 if (atoken == null || atoken.allDrawn || mWin.mAttrs.type == TYPE_APPLICATION_STARTING) {
Wale Ogunwale9d147902016-07-16 11:58:55 -0700539 result = mWin.performShowLocked();
Chong Zhang0275e392015-09-17 10:41:44 -0700540 }
Chong Zhang97782b42015-10-07 16:01:23 -0700541 return result;
Craig Mautnera608b882012-03-30 13:03:49 -0700542 }
543
Chong Zhang97782b42015-10-07 16:01:23 -0700544 void preserveSurfaceLocked() {
545 if (mDestroyPreservedSurfaceUponRedraw) {
Chong Zhangb9b0fec2016-02-11 18:51:51 -0800546 // This could happen when switching the surface mode very fast. For example,
547 // we preserved a surface when dragResizing changed to true. Then before the
548 // preserved surface is removed, dragResizing changed to false again.
549 // In this case, we need to leave the preserved surface alone, and destroy
550 // the actual surface, so that the createSurface call could create a surface
551 // of the proper size. The preserved surface will still be removed when client
552 // finishes drawing to the new surface.
553 mSurfaceDestroyDeferred = false;
554 destroySurfaceLocked();
555 mSurfaceDestroyDeferred = true;
Chong Zhang97782b42015-10-07 16:01:23 -0700556 return;
557 }
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -0800558 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "SET FREEZE LAYER", false);
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800559 if (mSurfaceController != null) {
560 mSurfaceController.setLayer(mAnimLayer + 1);
561 }
Chong Zhang97782b42015-10-07 16:01:23 -0700562 mDestroyPreservedSurfaceUponRedraw = true;
563 mSurfaceDestroyDeferred = true;
564 destroySurfaceLocked();
565 }
566
567 void destroyPreservedSurfaceLocked() {
568 if (!mDestroyPreservedSurfaceUponRedraw) {
569 return;
570 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800571 if (mSurfaceController != null) {
572 if (mPendingDestroySurface != null) {
573 // If we are preserving a surface but we aren't relaunching that means
574 // we are just doing an in-place switch. In that case any SurfaceFlinger side
575 // child layers need to be reparented to the new surface to make this
576 // transparent to the app.
577 if (mWin.mAppToken == null || mWin.mAppToken.isRelaunching() == false) {
578 SurfaceControl.openTransaction();
579 mPendingDestroySurface.reparentChildrenInTransaction(mSurfaceController);
580 SurfaceControl.closeTransaction();
581 }
582 }
583 }
584
Chong Zhang97782b42015-10-07 16:01:23 -0700585 destroyDeferredSurfaceLocked();
586 mDestroyPreservedSurfaceUponRedraw = false;
587 }
588
Chong Zhangeb665572016-05-09 18:28:27 -0700589 void markPreservedSurfaceForDestroy() {
590 if (mDestroyPreservedSurfaceUponRedraw
591 && !mService.mDestroyPreservedSurface.contains(mWin)) {
592 mService.mDestroyPreservedSurface.add(mWin);
593 }
594 }
595
Robert Carrda61ba92017-03-29 15:52:23 -0700596 private int getLayerStack() {
597 return mWin.getDisplayContent().getDisplay().getLayerStack();
598 }
599
600 void updateLayerStackInTransaction() {
601 if (mSurfaceController != null) {
602 mSurfaceController.setLayerStackInTransaction(
603 getLayerStack());
604 }
605 }
606
Robert Carrecc06b32017-04-18 14:25:10 -0700607 void resetDrawState() {
608 mDrawState = DRAW_PENDING;
609
610 if (mWin.mAppToken == null) {
611 return;
612 }
613
614 if (mWin.mAppToken.mAppAnimator.animation == null) {
615 mWin.mAppToken.clearAllDrawn();
616 } else {
617 // Currently animating, persist current state of allDrawn until animation
618 // is complete.
619 mWin.mAppToken.deferClearAllDrawn = true;
620 }
621 }
622
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500623 WindowSurfaceController createSurfaceLocked(int windowType, int ownerUid) {
Alan Viveretteccb11e12014-07-08 16:04:02 -0700624 final WindowState w = mWin;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700625 if (w.restoreSavedSurface()) {
Chong Zhang0464a932016-02-03 11:47:16 -0800626 if (DEBUG_ANIM) Slog.i(TAG,
627 "createSurface: " + this + ": called when we had a saved surface");
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800628 return mSurfaceController;
Chong Zhanga8975bd2016-01-28 17:13:47 -0800629 }
630
Wale Ogunwale722ff892016-02-18 13:37:55 -0800631 if (mSurfaceController != null) {
632 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700633 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800634
Robert Carr132c9f52017-07-31 17:02:30 -0700635 if ((mWin.mAttrs.privateFlags & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY) != 0) {
636 windowType = SurfaceControl.WINDOW_TYPE_DONT_SCREENSHOT;
637 }
638
Wale Ogunwale722ff892016-02-18 13:37:55 -0800639 w.setHasSurface(false);
640
641 if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,
642 "createSurface " + this + ": mDrawState=DRAW_PENDING");
643
Robert Carrecc06b32017-04-18 14:25:10 -0700644 resetDrawState();
Wale Ogunwale722ff892016-02-18 13:37:55 -0800645
646 mService.makeWindowFreezingScreenIfNeededLocked(w);
647
648 int flags = SurfaceControl.HIDDEN;
649 final WindowManager.LayoutParams attrs = w.mAttrs;
650
651 if (mService.isSecureLocked(w)) {
652 flags |= SurfaceControl.SECURE;
653 }
654
655 mTmpSize.set(w.mFrame.left + w.mXOffset, w.mFrame.top + w.mYOffset, 0, 0);
656 calculateSurfaceBounds(w, attrs);
657 final int width = mTmpSize.width();
658 final int height = mTmpSize.height();
659
660 if (DEBUG_VISIBILITY) {
661 Slog.v(TAG, "Creating surface in session "
662 + mSession.mSurfaceSession + " window " + this
663 + " w=" + width + " h=" + height
664 + " x=" + mTmpSize.left + " y=" + mTmpSize.top
665 + " format=" + attrs.format + " flags=" + flags);
666 }
667
668 // We may abort, so initialize to defaults.
669 mLastSystemDecorRect.set(0, 0, 0, 0);
670 mHasClipRect = false;
671 mClipRect.set(0, 0, 0, 0);
672 mLastClipRect.set(0, 0, 0, 0);
673
674 // Set up surface control with initial size.
675 try {
676
677 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
678 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
679 if (!PixelFormat.formatHasAlpha(attrs.format)
680 // Don't make surface with surfaceInsets opaque as they display a
681 // translucent shadow.
682 && attrs.surfaceInsets.left == 0
683 && attrs.surfaceInsets.top == 0
684 && attrs.surfaceInsets.right == 0
685 && attrs.surfaceInsets.bottom == 0
686 // Don't make surface opaque when resizing to reduce the amount of
687 // artifacts shown in areas the app isn't drawing content to.
688 && !w.isDragResizing()) {
689 flags |= SurfaceControl.OPAQUE;
690 }
691
692 mSurfaceController = new WindowSurfaceController(mSession.mSurfaceSession,
693 attrs.getTitle().toString(),
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500694 width, height, format, flags, this, windowType, ownerUid);
Robert Carr486bbb72017-05-30 11:25:22 -0700695 mSurfaceFormat = format;
Wale Ogunwale722ff892016-02-18 13:37:55 -0800696
697 w.setHasSurface(true);
698
699 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
700 Slog.i(TAG, " CREATE SURFACE "
701 + mSurfaceController + " IN SESSION "
702 + mSession.mSurfaceSession
703 + ": pid=" + mSession.mPid + " format="
704 + attrs.format + " flags=0x"
705 + Integer.toHexString(flags)
706 + " / " + this);
707 }
708 } catch (OutOfResourcesException e) {
709 Slog.w(TAG, "OutOfResourcesException creating surface");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700710 mService.mRoot.reclaimSomeSurfaceMemory(this, "create", true);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800711 mDrawState = NO_SURFACE;
712 return null;
713 } catch (Exception e) {
714 Slog.e(TAG, "Exception creating surface", e);
715 mDrawState = NO_SURFACE;
716 return null;
717 }
718
719 if (WindowManagerService.localLOGV) Slog.v(TAG, "Got surface: " + mSurfaceController
720 + ", set left=" + w.mFrame.left + " top=" + w.mFrame.top
721 + ", animLayer=" + mAnimLayer);
722
723 if (SHOW_LIGHT_TRANSACTIONS) {
724 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
725 WindowManagerService.logSurface(w, "CREATE pos=("
726 + w.mFrame.left + "," + w.mFrame.top + ") ("
727 + width + "x" + height + "), layer=" + mAnimLayer + " HIDE", false);
728 }
729
730 // Start a new transaction and apply position & offset.
Robert Carraf422a82017-04-10 18:34:33 -0700731
732 mService.openSurfaceTransaction();
733 try {
734 mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top, false);
735 mSurfaceController.setLayerStackInTransaction(getLayerStack());
736 mSurfaceController.setLayer(mAnimLayer);
737 } finally {
738 mService.closeSurfaceTransaction();
739 }
740
Wale Ogunwale722ff892016-02-18 13:37:55 -0800741 mLastHidden = true;
742
743 if (WindowManagerService.localLOGV) Slog.v(TAG, "Created surface " + this);
Robert Carre6a83512015-11-03 16:09:21 -0800744 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700745 }
746
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800747 private void calculateSurfaceBounds(WindowState w, LayoutParams attrs) {
748 if ((attrs.flags & FLAG_SCALED) != 0) {
749 // For a scaled surface, we always want the requested size.
750 mTmpSize.right = mTmpSize.left + w.mRequestedWidth;
751 mTmpSize.bottom = mTmpSize.top + w.mRequestedHeight;
752 } else {
753 // When we're doing a drag-resizing, request a surface that's fullscreen size,
754 // so that we don't need to reallocate during the process. This also prevents
755 // buffer drops due to size mismatch.
756 if (w.isDragResizing()) {
757 if (w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM) {
758 mTmpSize.left = 0;
759 mTmpSize.top = 0;
760 }
761 final DisplayInfo displayInfo = w.getDisplayInfo();
762 mTmpSize.right = mTmpSize.left + displayInfo.logicalWidth;
763 mTmpSize.bottom = mTmpSize.top + displayInfo.logicalHeight;
764 } else {
765 mTmpSize.right = mTmpSize.left + w.mCompatFrame.width();
766 mTmpSize.bottom = mTmpSize.top + w.mCompatFrame.height();
767 }
768 }
769
770 // Something is wrong and SurfaceFlinger will not like this, try to revert to sane values.
Filip Gruszczynskie6ed1952015-12-21 09:34:16 -0800771 // This doesn't necessarily mean that there is an error in the system. The sizes might be
772 // incorrect, because it is before the first layout or draw.
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800773 if (mTmpSize.width() < 1) {
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800774 mTmpSize.right = mTmpSize.left + 1;
775 }
776 if (mTmpSize.height() < 1) {
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800777 mTmpSize.bottom = mTmpSize.top + 1;
778 }
779
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800780 // Adjust for surface insets.
Phil Weavera4b32b92016-06-02 13:55:39 -0700781 mTmpSize.left -= attrs.surfaceInsets.left;
782 mTmpSize.top -= attrs.surfaceInsets.top;
783 mTmpSize.right += attrs.surfaceInsets.right;
784 mTmpSize.bottom += attrs.surfaceInsets.bottom;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800785 }
786
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800787 boolean hasSurface() {
Chong Zhang92147042016-05-09 12:47:11 -0700788 return !mWin.hasSavedSurface()
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800789 && mSurfaceController != null && mSurfaceController.hasSurface();
790 }
791
Craig Mautner96868332012-12-04 14:29:11 -0800792 void destroySurfaceLocked() {
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700793 final AppWindowToken wtoken = mWin.mAppToken;
794 if (wtoken != null) {
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700795 if (mWin == wtoken.startingWindow) {
796 wtoken.startingDisplayed = false;
797 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700798 }
799
Chong Zhang92147042016-05-09 12:47:11 -0700800 mWin.clearHasSavedSurface();
Robert Carr13f7be9e2015-12-02 18:39:45 -0800801
Wale Ogunwale722ff892016-02-18 13:37:55 -0800802 if (mSurfaceController == null) {
803 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700804 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800805
Wale Ogunwale722ff892016-02-18 13:37:55 -0800806 // When destroying a surface we want to make sure child windows are hidden. If we are
807 // preserving the surface until redraw though we intend to swap it out with another surface
808 // for resizing. In this case the window always remains visible to the user and the child
809 // windows should likewise remain visible.
Wale Ogunwale9d147902016-07-16 11:58:55 -0700810 if (!mDestroyPreservedSurfaceUponRedraw) {
811 mWin.mHidden = true;
Wale Ogunwale722ff892016-02-18 13:37:55 -0800812 }
813
814 try {
815 if (DEBUG_VISIBILITY) logWithStack(TAG, "Window " + this + " destroying surface "
816 + mSurfaceController + ", session " + mSession);
817 if (mSurfaceDestroyDeferred) {
818 if (mSurfaceController != null && mPendingDestroySurface != mSurfaceController) {
819 if (mPendingDestroySurface != null) {
820 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
821 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true);
822 }
823 mPendingDestroySurface.destroyInTransaction();
824 }
825 mPendingDestroySurface = mSurfaceController;
826 }
827 } else {
828 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
829 WindowManagerService.logSurface(mWin, "DESTROY", true);
830 }
831 destroySurface();
832 }
833 // Don't hide wallpaper if we're deferring the surface destroy
834 // because of a surface change.
835 if (!mDestroyPreservedSurfaceUponRedraw) {
836 mWallpaperControllerLocked.hideWallpapers(mWin);
837 }
838 } catch (RuntimeException e) {
839 Slog.w(TAG, "Exception thrown when destroying Window " + this
840 + " surface " + mSurfaceController + " session " + mSession + ": " + e.toString());
841 }
842
843 // Whether the surface was preserved (and copied to mPendingDestroySurface) or not, it
844 // needs to be cleared to match the WindowState.mHasSurface state. It is also necessary
845 // so it can be recreated successfully in mPendingDestroySurface case.
846 mWin.setHasSurface(false);
847 if (mSurfaceController != null) {
848 mSurfaceController.setShown(false);
849 }
850 mSurfaceController = null;
851 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700852 }
853
Craig Mautner96868332012-12-04 14:29:11 -0800854 void destroyDeferredSurfaceLocked() {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700855 try {
856 if (mPendingDestroySurface != null) {
857 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -0800858 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700859 }
Robert Carre6a83512015-11-03 16:09:21 -0800860 mPendingDestroySurface.destroyInTransaction();
Chong Zhang6e21cf42015-11-09 14:41:42 -0800861 // Don't hide wallpaper if we're destroying a deferred surface
862 // after a surface mode change.
863 if (!mDestroyPreservedSurfaceUponRedraw) {
864 mWallpaperControllerLocked.hideWallpapers(mWin);
865 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700866 }
867 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700868 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700869 + this + " surface " + mPendingDestroySurface
870 + " session " + mSession + ": " + e.toString());
871 }
872 mSurfaceDestroyDeferred = false;
873 mPendingDestroySurface = null;
874 }
875
Robert Carr298f9272016-08-09 13:08:01 -0700876 void applyMagnificationSpec(MagnificationSpec spec, Matrix transform) {
877 final int surfaceInsetLeft = mWin.mAttrs.surfaceInsets.left;
878 final int surfaceInsetTop = mWin.mAttrs.surfaceInsets.top;
879
880 if (spec != null && !spec.isNop()) {
881 float scale = spec.scale;
882 transform.postScale(scale, scale);
883 transform.postTranslate(spec.offsetX, spec.offsetY);
884
885 // As we are scaling the whole surface, to keep the content
886 // in the same position we will also have to scale the surfaceInsets.
887 transform.postTranslate(-(surfaceInsetLeft*scale - surfaceInsetLeft),
888 -(surfaceInsetTop*scale - surfaceInsetTop));
889 }
890 }
891
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700892 void computeShownFrameLocked() {
893 final boolean selfTransformation = mHasLocalTransformation;
894 Transformation attachedTransformation =
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700895 (mParentWinAnimator != null && mParentWinAnimator.mHasLocalTransformation)
896 ? mParentWinAnimator.mTransformation : null;
Craig Mautner322e4032012-07-13 13:35:20 -0700897 Transformation appTransformation = (mAppAnimator != null && mAppAnimator.hasTransformation)
898 ? mAppAnimator.transformation : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700899
900 // Wallpapers are animated based on the "real" window they
901 // are currently targeting.
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700902 final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget();
Craig Mautner798adef2013-10-22 14:29:01 -0700903 if (mIsWallpaper && wallpaperTarget != null && mService.mAnimateWallpaperWithTarget) {
904 final WindowStateAnimator wallpaperAnimator = wallpaperTarget.mWinAnimator;
Craig Mautner918b53b2012-07-09 14:15:54 -0700905 if (wallpaperAnimator.mHasLocalTransformation &&
906 wallpaperAnimator.mAnimation != null &&
907 !wallpaperAnimator.mAnimation.getDetachWallpaper()) {
908 attachedTransformation = wallpaperAnimator.mTransformation;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800909 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700910 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
911 }
912 }
Craig Mautner798adef2013-10-22 14:29:01 -0700913 final AppWindowAnimator wpAppAnimator = wallpaperTarget.mAppToken == null ?
914 null : wallpaperTarget.mAppToken.mAppAnimator;
915 if (wpAppAnimator != null && wpAppAnimator.hasTransformation
Craig Mautner918b53b2012-07-09 14:15:54 -0700916 && wpAppAnimator.animation != null
917 && !wpAppAnimator.animation.getDetachWallpaper()) {
Craig Mautner59431632012-04-04 11:56:44 -0700918 appTransformation = wpAppAnimator.transformation;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800919 if (DEBUG_WALLPAPER && appTransformation != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700920 Slog.v(TAG, "WP target app xform: " + appTransformation);
921 }
922 }
923 }
924
Craig Mautnera91f9e22012-09-14 16:22:08 -0700925 final int displayId = mWin.getDisplayId();
926 final ScreenRotationAnimation screenRotationAnimation =
927 mAnimator.getScreenRotationAnimationLocked(displayId);
928 final boolean screenAnimation =
929 screenRotationAnimation != null && screenRotationAnimation.isAnimating();
Robert Carr2f0fe622015-09-25 14:56:38 -0700930
931 mHasClipRect = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700932 if (selfTransformation || attachedTransformation != null
933 || appTransformation != null || screenAnimation) {
934 // cache often used attributes locally
935 final Rect frame = mWin.mFrame;
936 final float tmpFloats[] = mService.mTmpFloats;
937 final Matrix tmpMatrix = mWin.mTmpMatrix;
938
939 // Compute the desired transformation.
Dianne Hackborn4b169692012-11-29 17:51:24 -0800940 if (screenAnimation && screenRotationAnimation.isRotating()) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700941 // If we are doing a screen animation, the global rotation
942 // applied to windows can result in windows that are carefully
943 // aligned with each other to slightly separate, allowing you
944 // to see what is behind them. An unsightly mess. This...
945 // thing... magically makes it call good: scale each window
946 // slightly (two pixels larger in each dimension, from the
947 // window's center).
948 final float w = frame.width();
949 final float h = frame.height();
950 if (w>=1 && h>=1) {
951 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
952 } else {
953 tmpMatrix.reset();
954 }
955 } else {
956 tmpMatrix.reset();
957 }
958 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
959 if (selfTransformation) {
960 tmpMatrix.postConcat(mTransformation.getMatrix());
961 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700962 if (attachedTransformation != null) {
963 tmpMatrix.postConcat(attachedTransformation.getMatrix());
964 }
965 if (appTransformation != null) {
966 tmpMatrix.postConcat(appTransformation.getMatrix());
967 }
Matthew Ng34a06d12017-02-03 11:56:08 -0800968
969 // The translation that applies the position of the window needs to be applied at the
970 // end in case that other translations include scaling. Otherwise the scaling will
971 // affect this translation. But it needs to be set before the screen rotation animation
972 // so the pivot point is at the center of the screen for all windows.
973 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700974 if (screenAnimation) {
Craig Mautnera91f9e22012-09-14 16:22:08 -0700975 tmpMatrix.postConcat(screenRotationAnimation.getEnterTransformation().getMatrix());
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700976 }
Winson Chung399f6202014-03-19 10:47:20 -0700977
Robert Carref090ac2017-05-15 16:45:50 -0700978 MagnificationSpec spec = getMagnificationSpec();
979 if (spec != null) {
Robert Carr298f9272016-08-09 13:08:01 -0700980 applyMagnificationSpec(spec, tmpMatrix);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700981 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700982
983 // "convert" it into SurfaceFlinger's format
984 // (a 2x2 matrix + an offset)
985 // Here we must not transform the position of the surface
986 // since it is already included in the transformation.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800987 //Slog.i(TAG_WM, "Transform: " + matrix);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700988
989 mHaveMatrix = true;
990 tmpMatrix.getValues(tmpFloats);
991 mDsDx = tmpFloats[Matrix.MSCALE_X];
992 mDtDx = tmpFloats[Matrix.MSKEW_Y];
Robert Carr0edf18f2017-02-21 20:01:47 -0800993 mDtDy = tmpFloats[Matrix.MSKEW_X];
994 mDsDy = tmpFloats[Matrix.MSCALE_Y];
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700995 float x = tmpFloats[Matrix.MTRANS_X];
996 float y = tmpFloats[Matrix.MTRANS_Y];
Qiushi Han32fed962016-10-01 17:06:49 +0800997 mWin.mShownPosition.set(Math.round(x), Math.round(y));
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700998
999 // Now set the alpha... but because our current hardware
1000 // can't do alpha transformation on a non-opaque surface,
1001 // turn it off if we are running an animation that is also
1002 // transforming since it is more important to have that
1003 // animation be smooth.
1004 mShownAlpha = mAlpha;
1005 if (!mService.mLimitedAlphaCompositing
1006 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
Robert Carr0edf18f2017-02-21 20:01:47 -08001007 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDtDy, mDsDy)
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001008 && x == frame.left && y == frame.top))) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001009 //Slog.i(TAG_WM, "Applying alpha transform");
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001010 if (selfTransformation) {
1011 mShownAlpha *= mTransformation.getAlpha();
1012 }
1013 if (attachedTransformation != null) {
1014 mShownAlpha *= attachedTransformation.getAlpha();
1015 }
1016 if (appTransformation != null) {
1017 mShownAlpha *= appTransformation.getAlpha();
Winson Chung399f6202014-03-19 10:47:20 -07001018 if (appTransformation.hasClipRect()) {
1019 mClipRect.set(appTransformation.getClipRect());
1020 mHasClipRect = true;
Robert Carrf3b72c72016-03-21 18:16:39 -07001021 // The app transformation clip will be in the coordinate space of the main
1022 // activity window, which the animation correctly assumes will be placed at
1023 // (0,0)+(insets) relative to the containing frame. This isn't necessarily
1024 // true for child windows though which can have an arbitrary frame position
1025 // relative to their containing frame. We need to offset the difference
1026 // between the containing frame as used to calculate the crop and our
1027 // bounds to compensate for this.
Wale Ogunwale246c2092016-04-07 14:12:44 -07001028 if (mWin.layoutInParentFrame()) {
Robert Carrf3b72c72016-03-21 18:16:39 -07001029 mClipRect.offset( (mWin.mContainingFrame.left - mWin.mFrame.left),
1030 mWin.mContainingFrame.top - mWin.mFrame.top );
1031 }
Winson Chung399f6202014-03-19 10:47:20 -07001032 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001033 }
1034 if (screenAnimation) {
Craig Mautnera91f9e22012-09-14 16:22:08 -07001035 mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001036 }
1037 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001038 //Slog.i(TAG_WM, "Not applying alpha transform");
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001039 }
1040
Filip Gruszczynski19723a42015-11-25 15:01:48 -08001041 if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV)
Craig Mautnera91f9e22012-09-14 16:22:08 -07001042 && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v(
1043 TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha
1044 + " self=" + (selfTransformation ? mTransformation.getAlpha() : "null")
1045 + " attached=" + (attachedTransformation == null ?
1046 "null" : attachedTransformation.getAlpha())
1047 + " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha())
1048 + " screen=" + (screenAnimation ?
1049 screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001050 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001051 } else if (mIsWallpaper && mService.mRoot.mWallpaperActionPending) {
Craig Mautner4d7349b2012-04-20 14:52:47 -07001052 return;
Chong Zhang3005e752015-09-18 18:46:28 -07001053 } else if (mWin.isDragResizeChanged()) {
1054 // This window is awaiting a relayout because user just started (or ended)
1055 // drag-resizing. The shown frame (which affects surface size and pos)
1056 // should not be updated until we get next finished draw with the new surface.
1057 // Otherwise one or two frames rendered with old settings would be displayed
1058 // with new geometry.
1059 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001060 }
1061
Filip Gruszczynski19723a42015-11-25 15:01:48 -08001062 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautner4d7349b2012-04-20 14:52:47 -07001063 TAG, "computeShownFrameLocked: " + this +
1064 " not attached, mAlpha=" + mAlpha);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001065
Robert Carref090ac2017-05-15 16:45:50 -07001066 MagnificationSpec spec = getMagnificationSpec();
Craig Mautner165be0c2015-01-27 15:16:58 -08001067 if (spec != null) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001068 final Rect frame = mWin.mFrame;
1069 final float tmpFloats[] = mService.mTmpFloats;
1070 final Matrix tmpMatrix = mWin.mTmpMatrix;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001071
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001072 tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
1073 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001074
Robert Carr298f9272016-08-09 13:08:01 -07001075 applyMagnificationSpec(spec, tmpMatrix);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001076
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001077 tmpMatrix.getValues(tmpFloats);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001078
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001079 mHaveMatrix = true;
1080 mDsDx = tmpFloats[Matrix.MSCALE_X];
1081 mDtDx = tmpFloats[Matrix.MSKEW_Y];
Robert Carr0edf18f2017-02-21 20:01:47 -08001082 mDtDy = tmpFloats[Matrix.MSKEW_X];
1083 mDsDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001084 float x = tmpFloats[Matrix.MTRANS_X];
1085 float y = tmpFloats[Matrix.MTRANS_Y];
Qiushi Han32fed962016-10-01 17:06:49 +08001086 mWin.mShownPosition.set(Math.round(x), Math.round(y));
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001087
1088 mShownAlpha = mAlpha;
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001089 } else {
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001090 mWin.mShownPosition.set(mWin.mFrame.left, mWin.mFrame.top);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001091 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001092 mWin.mShownPosition.offset(mWin.mXOffset, mWin.mYOffset);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001093 }
1094 mShownAlpha = mAlpha;
1095 mHaveMatrix = false;
1096 mDsDx = mWin.mGlobalScale;
1097 mDtDx = 0;
Robert Carr0edf18f2017-02-21 20:01:47 -08001098 mDtDy = 0;
1099 mDsDy = mWin.mGlobalScale;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001100 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001101 }
1102
Robert Carref090ac2017-05-15 16:45:50 -07001103 private MagnificationSpec getMagnificationSpec() {
1104 //TODO (multidisplay): Magnification is supported only for the default display.
1105 if (mService.mAccessibilityController != null && mWin.getDisplayId() == DEFAULT_DISPLAY) {
1106 return mService.mAccessibilityController.getMagnificationSpecForWindowLocked(mWin);
1107 }
1108 return null;
1109 }
1110
Robert Carrfbbde852016-10-18 11:02:28 -07001111 /**
1112 * In some scenarios we use a screen space clip rect (so called, final clip rect)
1113 * to crop to stack bounds. Generally because it's easier to deal with while
1114 * animating.
1115 *
1116 * @return True in scenarios where we use the final clip rect for stack clipping.
1117 */
1118 private boolean useFinalClipRect() {
1119 return (isAnimationSet() && resolveStackClip() == STACK_CLIP_AFTER_ANIM)
1120 || mDestroyPreservedSurfaceUponRedraw || mWin.inPinnedWorkspace();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001121 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001122
Robert Carrfbbde852016-10-18 11:02:28 -07001123 /**
1124 * Calculate the screen-space crop rect and fill finalClipRect.
1125 * @return true if finalClipRect has been filled, otherwise,
1126 * no screen space crop should be applied.
1127 */
1128 private boolean calculateFinalCrop(Rect finalClipRect) {
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001129 final WindowState w = mWin;
Craig Mautnerdf88d732014-01-27 09:21:32 -08001130 final DisplayContent displayContent = w.getDisplayContent();
Robert Carrfbbde852016-10-18 11:02:28 -07001131 finalClipRect.setEmpty();
1132
Craig Mautnerdf88d732014-01-27 09:21:32 -08001133 if (displayContent == null) {
Robert Carrfbbde852016-10-18 11:02:28 -07001134 return false;
Craig Mautnerdf88d732014-01-27 09:21:32 -08001135 }
Robert Carrfbbde852016-10-18 11:02:28 -07001136
1137 if (!shouldCropToStackBounds() || !useFinalClipRect()) {
1138 return false;
1139 }
1140
1141 // Task is non-null per shouldCropToStackBounds
1142 final TaskStack stack = w.getTask().mStack;
1143 stack.getDimBounds(finalClipRect);
Robert Carr002c8e12017-04-27 15:32:22 -07001144
1145 if (StackId.tasksAreFloating(stack.mStackId)) {
1146 w.expandForSurfaceInsets(finalClipRect);
1147 }
Robert Carref090ac2017-05-15 16:45:50 -07001148
1149 // We may be applying a magnification spec to all windows,
1150 // simulating a transformation in screen space, in which case
1151 // we need to transform all other screen space values...including
1152 // the final crop. This is kind of messed up and we should look
1153 // in to actually transforming screen-space via a parent-layer.
1154 // b/38322835
1155 MagnificationSpec spec = getMagnificationSpec();
1156 if (spec != null && !spec.isNop()) {
1157 Matrix transform = mWin.mTmpMatrix;
1158 RectF finalCrop = mService.mTmpRectF;
1159 transform.reset();
1160 transform.postScale(spec.scale, spec.scale);
1161 transform.postTranslate(-spec.offsetX, -spec.offsetY);
1162 transform.mapRect(finalCrop);
1163 finalClipRect.top = (int)finalCrop.top;
1164 finalClipRect.left = (int)finalCrop.left;
1165 finalClipRect.right = (int)finalClipRect.right;
1166 finalClipRect.bottom = (int)finalClipRect.bottom;
1167 }
1168
Robert Carrfbbde852016-10-18 11:02:28 -07001169 return true;
1170 }
1171
1172 /**
1173 * Calculate the window-space crop rect and fill clipRect.
Wale Ogunwaleca9e0612016-12-02 07:45:59 -08001174 * @return true if clipRect has been filled otherwise, no window space crop should be applied.
Robert Carrfbbde852016-10-18 11:02:28 -07001175 */
Wale Ogunwaleca9e0612016-12-02 07:45:59 -08001176 private boolean calculateCrop(Rect clipRect) {
Robert Carrfbbde852016-10-18 11:02:28 -07001177 final WindowState w = mWin;
1178 final DisplayContent displayContent = w.getDisplayContent();
1179 clipRect.setEmpty();
1180
1181 if (displayContent == null) {
1182 return false;
1183 }
1184
1185 if (w.inPinnedWorkspace()) {
1186 return false;
1187 }
1188
1189 // If we're animating, the wallpaper should only
1190 // be updated at the end of the animation.
1191 if (w.mAttrs.type == TYPE_WALLPAPER) {
1192 return false;
1193 }
1194
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001195 if (DEBUG_WINDOW_CROP) Slog.d(TAG,
1196 "Updating crop win=" + w + " mLastCrop=" + mLastClipRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001197
Robert Carrfbbde852016-10-18 11:02:28 -07001198 w.calculatePolicyCrop(mSystemDecorRect);
1199
1200 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Applying decor to crop win=" + w + " mDecorFrame="
1201 + w.mDecorFrame + " mSystemDecorRect=" + mSystemDecorRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001202
Andrii Kulian283acd22017-08-03 04:03:51 -07001203 final Task task = w.getTask();
1204 final boolean fullscreen = w.fillsDisplay() || (task != null && task.isFullscreen());
Jorim Jaggi253a20f2015-11-03 12:38:42 +01001205 final boolean isFreeformResizing =
Jorim Jaggidcf467c2015-11-05 13:59:32 +01001206 w.isDragResizing() && w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM;
Robert Carr51a1b872015-12-08 14:03:13 -08001207
1208 // We use the clip rect as provided by the tranformation for non-fullscreen windows to
1209 // avoid premature clipping with the system decor rect.
1210 clipRect.set((mHasClipRect && !fullscreen) ? mClipRect : mSystemDecorRect);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001211 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "win=" + w + " Initial clip rect: " + clipRect
1212 + " mHasClipRect=" + mHasClipRect + " fullscreen=" + fullscreen);
Robert Carr51a1b872015-12-08 14:03:13 -08001213
1214 if (isFreeformResizing && !w.isChildWindow()) {
1215 // For freeform resizing non child windows, we are using the big surface positioned
1216 // at 0,0. Thus we must express the crop in that coordinate space.
1217 clipRect.offset(w.mShownPosition.x, w.mShownPosition.y);
Chong Zhang09b21ef2015-09-14 10:20:21 -07001218 }
Robert Carr51a1b872015-12-08 14:03:13 -08001219
Robert Carrfbbde852016-10-18 11:02:28 -07001220 w.expandForSurfaceInsets(clipRect);
Alan Viverette49a22e82014-07-12 20:01:27 -07001221
Wale Ogunwaleb3eba812015-07-30 20:20:16 -07001222 if (mHasClipRect && fullscreen) {
1223 // We intersect the clip rect specified by the transformation with the expanded system
1224 // decor rect to prevent artifacts from drawing during animation if the transformation
1225 // clip rect extends outside the system decor rect.
1226 clipRect.intersect(mClipRect);
1227 }
Alan Viverette49a22e82014-07-12 20:01:27 -07001228 // The clip rect was generated assuming (0,0) as the window origin,
1229 // so we need to translate to match the actual surface coordinates.
Robert Carrfbbde852016-10-18 11:02:28 -07001230 clipRect.offset(w.mAttrs.surfaceInsets.left, w.mAttrs.surfaceInsets.top);
Robert Carr58f29132015-10-29 14:19:05 -07001231
Robert Carrfbbde852016-10-18 11:02:28 -07001232 if (!useFinalClipRect()) {
1233 adjustCropToStackBounds(clipRect, isFreeformResizing);
1234 }
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001235 if (DEBUG_WINDOW_CROP) Slog.d(TAG,
1236 "win=" + w + " Clip rect after stack adjustment=" + clipRect);
Robert Carr58f29132015-10-29 14:19:05 -07001237
Chia-I Wue6bcaf12016-05-27 10:58:48 +08001238 w.transformClipRectFromScreenToSurfaceSpace(clipRect);
Robert Carr58f29132015-10-29 14:19:05 -07001239
Robert Carrfbbde852016-10-18 11:02:28 -07001240 return true;
Robert Carr0d00c2e2016-02-29 17:45:02 -08001241 }
1242
Robert Carrfbbde852016-10-18 11:02:28 -07001243 private void applyCrop(Rect clipRect, Rect finalClipRect, boolean recoveringMemory) {
1244 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "applyCrop: win=" + mWin
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001245 + " clipRect=" + clipRect + " finalClipRect=" + finalClipRect);
Robert Carr4320d332016-06-10 15:13:32 -07001246 if (clipRect != null) {
1247 if (!clipRect.equals(mLastClipRect)) {
1248 mLastClipRect.set(clipRect);
1249 mSurfaceController.setCropInTransaction(clipRect, recoveringMemory);
1250 }
1251 } else {
1252 mSurfaceController.clearCropInTransaction(recoveringMemory);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001253 }
Robert Carrfbbde852016-10-18 11:02:28 -07001254
1255 if (finalClipRect == null) {
1256 finalClipRect = mService.mTmpRect;
1257 finalClipRect.setEmpty();
1258 }
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001259 if (!finalClipRect.equals(mLastFinalClipRect)) {
1260 mLastFinalClipRect.set(finalClipRect);
1261 mSurfaceController.setFinalCropInTransaction(finalClipRect);
Chong Zhangcc3eccf2016-05-16 18:25:45 -07001262 if (mDestroyPreservedSurfaceUponRedraw && mPendingDestroySurface != null) {
1263 mPendingDestroySurface.setFinalCropInTransaction(finalClipRect);
1264 }
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001265 }
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001266 }
1267
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001268 private int resolveStackClip() {
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001269 // App animation overrides window animation stack clip mode.
1270 if (mAppAnimator != null && mAppAnimator.animation != null) {
1271 return mAppAnimator.getStackClip();
1272 } else {
1273 return mStackClip;
1274 }
1275 }
Wale Ogunwale4c5aa5172016-04-19 11:29:05 -07001276
Robert Carrfbbde852016-10-18 11:02:28 -07001277 private boolean shouldCropToStackBounds() {
1278 final WindowState w = mWin;
Wale Ogunwale4c5aa5172016-04-19 11:29:05 -07001279 final DisplayContent displayContent = w.getDisplayContent();
1280 if (displayContent != null && !displayContent.isDefaultDisplay) {
1281 // There are some windows that live on other displays while their app and main window
1282 // live on the default display (e.g. casting...). We don't want to crop this windows
1283 // to the stack bounds which is only currently supported on the default display.
1284 // TODO(multi-display): Need to support cropping to stack bounds on other displays
1285 // when we have stacks on other displays.
Robert Carrfbbde852016-10-18 11:02:28 -07001286 return false;
Wale Ogunwale4c5aa5172016-04-19 11:29:05 -07001287 }
1288
Chong Zhang112eb8c2015-11-02 11:17:00 -08001289 final Task task = w.getTask();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001290 if (task == null || !task.cropWindowsToStackBounds()) {
Robert Carrfbbde852016-10-18 11:02:28 -07001291 return false;
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -07001292 }
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001293
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001294 final int stackClip = resolveStackClip();
1295
1296 // It's animating and we don't want to clip it to stack bounds during animation - abort.
Jorim Jaggi5c80c412016-04-19 20:03:47 -07001297 if (isAnimationSet() && stackClip == STACK_CLIP_NONE) {
Robert Carrfbbde852016-10-18 11:02:28 -07001298 return false;
1299 }
1300 return true;
1301 }
1302
1303 private void adjustCropToStackBounds(Rect clipRect,
1304 boolean isFreeformResizing) {
1305 final WindowState w = mWin;
1306
1307 if (!shouldCropToStackBounds()) {
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001308 return;
1309 }
1310
Robert Carrfbbde852016-10-18 11:02:28 -07001311 final TaskStack stack = w.getTask().mStack;
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001312 stack.getDimBounds(mTmpStackBounds);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001313 final Rect surfaceInsets = w.getAttrs().surfaceInsets;
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001314 // When we resize we use the big surface approach, which means we can't trust the
1315 // window frame bounds anymore. Instead, the window will be placed at 0, 0, but to avoid
1316 // hardcoding it, we use surface coordinates.
1317 final int frameX = isFreeformResizing ? (int) mSurfaceController.getX() :
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001318 w.mFrame.left + mWin.mXOffset - surfaceInsets.left;
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001319 final int frameY = isFreeformResizing ? (int) mSurfaceController.getY() :
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001320 w.mFrame.top + mWin.mYOffset - surfaceInsets.top;
Robert Carr0d00c2e2016-02-29 17:45:02 -08001321
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001322 // We need to do some acrobatics with surface position, because their clip region is
1323 // relative to the inside of the surface, but the stack bounds aren't.
Robert Carrfbbde852016-10-18 11:02:28 -07001324 if (StackId.hasWindowShadow(stack.mStackId)
1325 && !StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001326 // The windows in this stack display drop shadows and the fill the entire stack
1327 // area. Adjust the stack bounds we will use to cropping take into account the
1328 // offsets we use to display the drop shadow so it doesn't get cropped.
1329 mTmpStackBounds.inset(-surfaceInsets.left, -surfaceInsets.top,
1330 -surfaceInsets.right, -surfaceInsets.bottom);
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001331 }
Robert Carrfbbde852016-10-18 11:02:28 -07001332
1333 clipRect.left = Math.max(0,
1334 Math.max(mTmpStackBounds.left, frameX + clipRect.left) - frameX);
1335 clipRect.top = Math.max(0,
1336 Math.max(mTmpStackBounds.top, frameY + clipRect.top) - frameY);
1337 clipRect.right = Math.max(0,
1338 Math.min(mTmpStackBounds.right, frameX + clipRect.right) - frameX);
1339 clipRect.bottom = Math.max(0,
1340 Math.min(mTmpStackBounds.bottom, frameY + clipRect.bottom) - frameY);
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -07001341 }
1342
Wale Ogunwale4c8b7952015-04-07 10:49:40 -07001343 void setSurfaceBoundariesLocked(final boolean recoveringMemory) {
Wale Ogunwale027f4752017-05-12 10:37:16 -07001344 if (mSurfaceController == null) {
1345 return;
1346 }
1347
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001348 final WindowState w = mWin;
Winson Chung08f81892017-03-02 15:40:51 -08001349 final LayoutParams attrs = mWin.getAttrs();
Robert Carr0d00c2e2016-02-29 17:45:02 -08001350 final Task task = w.getTask();
Alan Viveretteccb11e12014-07-08 16:04:02 -07001351
Jorim Jaggif3df0aa2016-04-06 15:56:33 -07001352 // We got resized, so block all updates until we got the new surface.
Andrii Kulianeb1d3222016-05-16 15:17:55 -07001353 if (w.isResizedWhileNotDragResizing() && !w.isGoneForLayoutLw()) {
Jorim Jaggif3df0aa2016-04-06 15:56:33 -07001354 return;
1355 }
1356
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001357 mTmpSize.set(w.mShownPosition.x, w.mShownPosition.y, 0, 0);
Winson Chung08f81892017-03-02 15:40:51 -08001358 calculateSurfaceBounds(w, attrs);
Chong Zhang0275e392015-09-17 10:41:44 -07001359
Robert Carr04092112016-06-02 12:56:12 -07001360 mExtraHScale = (float) 1.0;
1361 mExtraVScale = (float) 1.0;
Robert Carre1034cc32016-02-01 13:08:15 -08001362
Robert Carr6da3cc02016-06-16 15:17:07 -07001363 boolean wasForceScaled = mForceScaleUntilResize;
1364 boolean wasSeamlesslyRotated = w.mSeamlesslyRotated;
1365
Robert Carrfed10072016-05-26 11:48:49 -07001366 // Once relayout has been called at least once, we need to make sure
1367 // we only resize the client surface during calls to relayout. For
1368 // clients which use indeterminate measure specs (MATCH_PARENT),
1369 // we may try and change their window size without a call to relayout.
1370 // However, this would be unsafe, as the client may be in the middle
1371 // of producing a frame at the old size, having just completed layout
1372 // to find the surface size changed underneath it.
Robert Carr3ccc5272016-06-20 22:06:32 -07001373 if (!w.mRelayoutCalled || w.mInRelayout) {
Robert Carrfed10072016-05-26 11:48:49 -07001374 mSurfaceResized = mSurfaceController.setSizeInTransaction(
1375 mTmpSize.width(), mTmpSize.height(), recoveringMemory);
1376 } else {
1377 mSurfaceResized = false;
1378 }
Robert Carrc7294602016-05-13 11:32:05 -07001379 mForceScaleUntilResize = mForceScaleUntilResize && !mSurfaceResized;
Robert Carr6da3cc02016-06-16 15:17:07 -07001380 // If we are undergoing seamless rotation, the surface has already
1381 // been set up to persist at it's old location. We need to freeze
1382 // updates until a resize occurs.
Robert Carrb14d4abc2016-10-03 18:13:33 -07001383 mService.markForSeamlessRotation(w, w.mSeamlesslyRotated && !mSurfaceResized);
Robert Carrc7294602016-05-13 11:32:05 -07001384
Robert Carrfbbde852016-10-18 11:02:28 -07001385 Rect clipRect = null, finalClipRect = null;
1386 if (calculateCrop(mTmpClipRect)) {
1387 clipRect = mTmpClipRect;
1388 }
1389 if (calculateFinalCrop(mTmpFinalClipRect)) {
1390 finalClipRect = mTmpFinalClipRect;
1391 }
Robert Carra9408d42016-06-03 13:28:48 -07001392
1393 float surfaceWidth = mSurfaceController.getWidth();
1394 float surfaceHeight = mSurfaceController.getHeight();
1395
Robert Carr8f0a3ad2017-02-15 19:30:28 -08001396 if (isForceScaled()) {
Winson Chung08f81892017-03-02 15:40:51 -08001397 int hInsets = attrs.surfaceInsets.left + attrs.surfaceInsets.right;
1398 int vInsets = attrs.surfaceInsets.top + attrs.surfaceInsets.bottom;
1399 float surfaceContentWidth = surfaceWidth - hInsets;
1400 float surfaceContentHeight = surfaceHeight - vInsets;
Robert Carra9408d42016-06-03 13:28:48 -07001401 if (!mForceScaleUntilResize) {
1402 mSurfaceController.forceScaleableInTransaction(true);
1403 }
Robert Carrfd4c9892017-02-01 10:28:28 -08001404
Winson Chung08f81892017-03-02 15:40:51 -08001405 int posX = mTmpSize.left;
1406 int posY = mTmpSize.top;
Robert Carrfd4c9892017-02-01 10:28:28 -08001407 task.mStack.getDimBounds(mTmpStackBounds);
Robert Carr18f622f2017-05-08 11:20:43 -07001408
1409 boolean allowStretching = false;
Winson Chung8bca9e42017-04-16 15:59:43 -07001410 task.mStack.getFinalAnimationSourceHintBounds(mTmpSourceBounds);
Robert Carr18f622f2017-05-08 11:20:43 -07001411 // If we don't have source bounds, we can attempt to use the content insets
1412 // in the following scenario:
1413 // 1. We have content insets.
1414 // 2. We are not transitioning to full screen
1415 // We have to be careful to check "lastAnimatingBoundsWasToFullscreen" rather than
1416 // the mBoundsAnimating state, as we may have already left it and only be here
1417 // because of the force-scale until resize state.
1418 if (mTmpSourceBounds.isEmpty() && (mWin.mLastRelayoutContentInsets.width() > 0
1419 || mWin.mLastRelayoutContentInsets.height() > 0)
1420 && !task.mStack.lastAnimatingBoundsWasToFullscreen()) {
1421 mTmpSourceBounds.set(task.mStack.mPreAnimationBounds);
1422 mTmpSourceBounds.inset(mWin.mLastRelayoutContentInsets);
1423 allowStretching = true;
1424 }
Winson Chung08f81892017-03-02 15:40:51 -08001425 if (!mTmpSourceBounds.isEmpty()) {
1426 // Get the final target stack bounds, if we are not animating, this is just the
1427 // current stack bounds
Winson Chung40a5f932017-04-13 16:39:36 -07001428 task.mStack.getFinalAnimationBounds(mTmpAnimatingBounds);
Winson Chung08f81892017-03-02 15:40:51 -08001429
1430 // Calculate the current progress and interpolate the difference between the target
1431 // and source bounds
1432 float finalWidth = mTmpAnimatingBounds.width();
1433 float initialWidth = mTmpSourceBounds.width();
Robert Carr18f622f2017-05-08 11:20:43 -07001434 float tw = (surfaceContentWidth - mTmpStackBounds.width())
Winson Chung08f81892017-03-02 15:40:51 -08001435 / (surfaceContentWidth - mTmpAnimatingBounds.width());
Robert Carr18f622f2017-05-08 11:20:43 -07001436 float th = tw;
1437 mExtraHScale = (initialWidth + tw * (finalWidth - initialWidth)) / initialWidth;
1438 if (allowStretching) {
1439 float finalHeight = mTmpAnimatingBounds.height();
1440 float initialHeight = mTmpSourceBounds.height();
1441 th = (surfaceContentHeight - mTmpStackBounds.height())
1442 / (surfaceContentHeight - mTmpAnimatingBounds.height());
1443 mExtraVScale = (initialHeight + tw * (finalHeight - initialHeight))
1444 / initialHeight;
1445 } else {
1446 mExtraVScale = mExtraHScale;
1447 }
Winson Chung08f81892017-03-02 15:40:51 -08001448
1449 // Adjust the position to account for the inset bounds
Robert Carr18f622f2017-05-08 11:20:43 -07001450 posX -= (int) (tw * mExtraHScale * mTmpSourceBounds.left);
1451 posY -= (int) (th * mExtraVScale * mTmpSourceBounds.top);
Winson Chung08f81892017-03-02 15:40:51 -08001452
1453 // Always clip to the stack bounds since the surface can be larger with the current
1454 // scale
1455 clipRect = null;
1456 finalClipRect = mTmpStackBounds;
1457 } else {
1458 // We want to calculate the scaling based on the content area, not based on
1459 // the entire surface, so that we scale in sync with windows that don't have insets.
1460 mExtraHScale = mTmpStackBounds.width() / surfaceContentWidth;
1461 mExtraVScale = mTmpStackBounds.height() / surfaceContentHeight;
1462
1463 // Since we are scaled to fit in our previously desired crop, we can now
1464 // expose the whole window in buffer space, and not risk extending
1465 // past where the system would have cropped us
1466 clipRect = null;
1467 finalClipRect = null;
1468 }
Robert Carr0d00c2e2016-02-29 17:45:02 -08001469
Robert Carrfd4c9892017-02-01 10:28:28 -08001470 // In the case of ForceScaleToStack we scale entire tasks together,
Robert Carr0d00c2e2016-02-29 17:45:02 -08001471 // and so we need to scale our offsets relative to the task bounds
1472 // or parent and child windows would fall out of alignment.
Winson Chung08f81892017-03-02 15:40:51 -08001473 posX -= (int) (attrs.x * (1 - mExtraHScale));
1474 posY -= (int) (attrs.y * (1 - mExtraVScale));
1475
Robert Carrbc133762016-05-12 14:04:38 -07001476 // Imagine we are scaling down. As we scale the buffer down, we decrease the
1477 // distance between the surface top left, and the start of the surface contents
1478 // (previously it was surfaceInsets.left pixels in screen space but now it
Robert Carr04092112016-06-02 12:56:12 -07001479 // will be surfaceInsets.left*mExtraHScale). This means in order to keep the
Robert Carrbc133762016-05-12 14:04:38 -07001480 // non inset content at the same position, we have to shift the whole window
1481 // forward. Likewise for scaling up, we've increased this distance, and we need
1482 // to shift by a negative number to compensate.
Winson Chung08f81892017-03-02 15:40:51 -08001483 posX += attrs.surfaceInsets.left * (1 - mExtraHScale);
1484 posY += attrs.surfaceInsets.top * (1 - mExtraVScale);
Robert Carrbc133762016-05-12 14:04:38 -07001485
Winson Chung08f81892017-03-02 15:40:51 -08001486 mSurfaceController.setPositionInTransaction((float) Math.floor(posX),
1487 (float) Math.floor(posY), recoveringMemory);
Robert Carrc7294602016-05-13 11:32:05 -07001488
1489 // Various surfaces in the scaled stack may resize at different times.
1490 // We need to ensure for each surface, that we disable transformation matrix
1491 // scaling in the same transaction which we resize the surface in.
1492 // As we are in SCALING_MODE_SCALE_TO_WINDOW, SurfaceFlinger will
Robert Carra9408d42016-06-03 13:28:48 -07001493 // then take over the scaling until the new buffer arrives, and things
Robert Carrc7294602016-05-13 11:32:05 -07001494 // will be seamless.
1495 mForceScaleUntilResize = true;
Robert Carr0d00c2e2016-02-29 17:45:02 -08001496 } else {
Robert Carr6da3cc02016-06-16 15:17:07 -07001497 if (!w.mSeamlesslyRotated) {
1498 mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top,
1499 recoveringMemory);
1500 }
Robert Carr0d00c2e2016-02-29 17:45:02 -08001501 }
1502
Robert Carra9408d42016-06-03 13:28:48 -07001503 // If we are ending the scaling mode. We switch to SCALING_MODE_FREEZE
Robert Carr6da3cc02016-06-16 15:17:07 -07001504 // to prevent further updates until buffer latch.
1505 // When ending both force scaling, and seamless rotation, we need to freeze
1506 // the Surface geometry until a buffer comes in at the new size (normally position and crop
1507 // are unfrozen). setGeometryAppliesWithResizeInTransaction accomplishes this for us.
1508 if ((wasForceScaled && !mForceScaleUntilResize) ||
1509 (wasSeamlesslyRotated && !w.mSeamlesslyRotated)) {
1510 mSurfaceController.setGeometryAppliesWithResizeInTransaction(true);
Robert Carra9408d42016-06-03 13:28:48 -07001511 mSurfaceController.forceScaleableInTransaction(false);
1512 }
Robert Carr4320d332016-06-10 15:13:32 -07001513
Robert Carr6da3cc02016-06-16 15:17:07 -07001514 if (!w.mSeamlesslyRotated) {
Robert Carrfbbde852016-10-18 11:02:28 -07001515 applyCrop(clipRect, finalClipRect, recoveringMemory);
Robert Carr6da3cc02016-06-16 15:17:07 -07001516 mSurfaceController.setMatrixInTransaction(mDsDx * w.mHScale * mExtraHScale,
1517 mDtDx * w.mVScale * mExtraVScale,
Robert Carr0edf18f2017-02-21 20:01:47 -08001518 mDtDy * w.mHScale * mExtraHScale,
1519 mDsDy * w.mVScale * mExtraVScale, recoveringMemory);
Robert Carr6da3cc02016-06-16 15:17:07 -07001520 }
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001521
Robert Carre6a83512015-11-03 16:09:21 -08001522 if (mSurfaceResized) {
Chong Zhang5b2f1992015-11-13 15:40:36 -08001523 mReportSurfaceResized = true;
Robert Carre6a83512015-11-03 16:09:21 -08001524 mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1525 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
1526 w.applyDimLayerIfNeeded();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001527 }
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001528 }
1529
Andrii Kulian283acd22017-08-03 04:03:51 -07001530 /**
1531 * Get rect of the task this window is currently in. If there is no task, rect will be set to
1532 * empty.
1533 */
1534 void getContainerRect(Rect rect) {
1535 final Task task = mWin.getTask();
1536 if (task != null) {
1537 task.getDimBounds(rect);
1538 } else {
1539 rect.left = rect.top = rect.right = rect.bottom = 0;
1540 }
1541 }
1542
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001543 void prepareSurfaceLocked(final boolean recoveringMemory) {
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001544 final WindowState w = mWin;
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001545 if (!hasSurface()) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001546
1547 // There is no need to wait for an animation change if our window is gone for layout
1548 // already as we'll never be visible.
Bryce Lee8c3cf382017-07-06 19:47:10 -07001549 if (w.getOrientationChanging() && w.isGoneForLayoutLw()) {
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001550 if (DEBUG_ORIENTATION) {
1551 Slog.v(TAG, "Orientation change skips hidden " + w);
1552 }
Bryce Lee8c3cf382017-07-06 19:47:10 -07001553 w.setOrientationChanging(false);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001554 }
1555 return;
1556 }
1557
Chong Zhang70211742016-04-28 18:27:53 -07001558 // Do not change surface properties of opening apps if we are waiting for the
1559 // transition to be ready. transitionGoodToGo could be not ready even after all
1560 // opening apps are drawn. It's only waiting on isFetchingAppTransitionsSpecs()
1561 // to get the animation spec. (For example, go into Recents and immediately open
1562 // the same app again before the app's surface is destroyed or saved, the surface
1563 // is always ready in the whole process.) If we go ahead here, the opening app
1564 // will be shown with the full size before the correct animation spec arrives.
Chong Zhange22006d2016-05-09 10:59:59 -07001565 if (isWaitingForOpening()) {
Chong Zhang70211742016-04-28 18:27:53 -07001566 return;
1567 }
1568
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001569 boolean displayed = false;
1570
1571 computeShownFrameLocked();
1572
Craig Mautnera91f9e22012-09-14 16:22:08 -07001573 setSurfaceBoundariesLocked(recoveringMemory);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001574
Craig Mautner918b53b2012-07-09 14:15:54 -07001575 if (mIsWallpaper && !mWin.mWallpaperVisible) {
Craig Mautner0fa77c12012-06-11 15:57:19 -07001576 // Wallpaper is no longer visible and there is no wp target => hide it.
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001577 hide("prepareSurfaceLocked");
Wale Ogunwale9d147902016-07-16 11:58:55 -07001578 } else if (w.isParentWindowHidden() || !w.isOnScreen()) {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001579 hide("prepareSurfaceLocked");
Wale Ogunwalee8069dc2015-08-18 09:52:01 -07001580 mWallpaperControllerLocked.hideWallpapers(w);
Craig Mautnerb9836b92012-06-11 11:40:09 -07001581
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001582 // If we are waiting for this window to handle an orientation change. If this window is
1583 // really hidden (gone for layout), there is no point in still waiting for it.
1584 // Note that this does introduce a potential glitch if the window becomes unhidden
1585 // before it has drawn for the new orientation.
Bryce Lee8c3cf382017-07-06 19:47:10 -07001586 if (w.getOrientationChanging() && w.isGoneForLayoutLw()) {
1587 w.setOrientationChanging(false);
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001588 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001589 "Orientation change skips hidden " + w);
1590 }
1591 } else if (mLastLayer != mAnimLayer
1592 || mLastAlpha != mShownAlpha
1593 || mLastDsDx != mDsDx
1594 || mLastDtDx != mDtDx
1595 || mLastDsDy != mDsDy
1596 || mLastDtDy != mDtDy
1597 || w.mLastHScale != w.mHScale
1598 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -07001599 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001600 displayed = true;
1601 mLastAlpha = mShownAlpha;
1602 mLastLayer = mAnimLayer;
1603 mLastDsDx = mDsDx;
1604 mLastDtDx = mDtDx;
1605 mLastDsDy = mDsDy;
1606 mLastDtDy = mDtDy;
1607 w.mLastHScale = w.mHScale;
1608 w.mLastVScale = w.mVScale;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001609 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
Robert Carre6a83512015-11-03 16:09:21 -08001610 "controller=" + mSurfaceController +
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001611 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
Craig Mautner78505d82014-09-02 14:36:31 -07001612 + " matrix=[" + mDsDx + "*" + w.mHScale
1613 + "," + mDtDx + "*" + w.mVScale
Robert Carr0edf18f2017-02-21 20:01:47 -08001614 + "][" + mDtDy + "*" + w.mHScale
1615 + "," + mDsDy + "*" + w.mVScale + "]", false);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001616
Robert Carre6a83512015-11-03 16:09:21 -08001617 boolean prepared =
Robert Carraf422a82017-04-10 18:34:33 -07001618 mSurfaceController.prepareToShowInTransaction(mShownAlpha,
Robert Carr04092112016-06-02 12:56:12 -07001619 mDsDx * w.mHScale * mExtraHScale,
1620 mDtDx * w.mVScale * mExtraVScale,
Robert Carr0edf18f2017-02-21 20:01:47 -08001621 mDtDy * w.mHScale * mExtraHScale,
1622 mDsDy * w.mVScale * mExtraVScale,
Robert Carre6a83512015-11-03 16:09:21 -08001623 recoveringMemory);
Robert Carraf422a82017-04-10 18:34:33 -07001624 mSurfaceController.setLayer(mAnimLayer);
Robert Carre6a83512015-11-03 16:09:21 -08001625
Robert Carr03206af2017-07-10 18:24:21 -07001626 if (prepared && mDrawState == HAS_DRAWN) {
1627 if (mLastHidden) {
1628 if (showSurfaceRobustlyLocked()) {
1629 markPreservedSurfaceForDestroy();
1630 mAnimator.requestRemovalOfReplacedWindows(w);
1631 mLastHidden = false;
1632 if (mIsWallpaper) {
1633 w.dispatchWallpaperVisibility(true);
1634 }
1635 // This draw means the difference between unique content and mirroring.
1636 // Run another pass through performLayout to set mHasContent in the
1637 // LogicalDisplay.
1638 mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1639 WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
1640 } else {
1641 w.setOrientationChanging(false);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001642 }
Robert Carr03206af2017-07-10 18:24:21 -07001643 }
1644 // We process mTurnOnScreen even for windows which have already
1645 // been shown, to handle cases where windows are not necessarily
1646 // hidden while the screen is turning off.
1647 // TODO(b/63773439): These cases should be eliminated, though we probably still
1648 // want to process mTurnOnScreen in this way for clarity.
chaviwd3bf08d2017-08-01 17:24:59 -07001649 if (mWin.mTurnOnScreen && mWin.mAppToken.canTurnScreenOn()) {
Robert Carr03206af2017-07-10 18:24:21 -07001650 if (DEBUG_VISIBILITY) Slog.v(TAG, "Show surface turning screen on: " + mWin);
1651 mWin.mTurnOnScreen = false;
chaviwd3bf08d2017-08-01 17:24:59 -07001652
1653 // The window should only turn the screen on once per resume, but
1654 // prepareSurfaceLocked can be called multiple times. Set canTurnScreenOn to
1655 // false so the window doesn't turn the screen on again during this resume.
1656 mWin.mAppToken.setCanTurnScreenOn(false);
Robert Carr03206af2017-07-10 18:24:21 -07001657 mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001658 }
1659 }
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001660 if (hasSurface()) {
Robert Carre6a83512015-11-03 16:09:21 -08001661 w.mToken.hasVisible = true;
1662 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001663 } else {
Jorim Jaggi5c80c412016-04-19 20:03:47 -07001664 if (DEBUG_ANIM && isAnimationSet()) {
Chong Zhange05db742016-02-16 16:58:37 -08001665 Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
Craig Mautner83339b42012-05-01 22:13:23 -07001666 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001667 displayed = true;
1668 }
1669
Bryce Lee8c3cf382017-07-06 19:47:10 -07001670 if (w.getOrientationChanging()) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001671 if (!w.isDrawnLw()) {
1672 mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE;
1673 mAnimator.mLastWindowFreezeSource = w;
1674 if (DEBUG_ORIENTATION) Slog.v(TAG,
1675 "Orientation continue waiting for draw in " + w);
1676 } else {
Bryce Lee8c3cf382017-07-06 19:47:10 -07001677 w.setOrientationChanging(false);
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001678 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001679 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001680 }
1681
1682 if (displayed) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001683 w.mToken.hasVisible = true;
1684 }
1685 }
1686
Craig Mautneref655012013-01-03 11:20:24 -08001687 void setTransparentRegionHintLocked(final Region region) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001688 if (mSurfaceController == null) {
1689 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1690 return;
1691 }
Robert Carre6a83512015-11-03 16:09:21 -08001692 mSurfaceController.setTransparentRegionHint(region);
Craig Mautner48ba1e72012-04-02 13:18:16 -07001693 }
1694
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001695 void setWallpaperOffset(Point shownPosition) {
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001696 final LayoutParams attrs = mWin.getAttrs();
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001697 final int left = shownPosition.x - attrs.surfaceInsets.left;
1698 final int top = shownPosition.y - attrs.surfaceInsets.top;
Robert Carre6a83512015-11-03 16:09:21 -08001699
1700 try {
Craig Mautner71dd1b62014-02-18 15:48:52 -08001701 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setWallpaperOffset");
Robert Carr68e5c9e2016-09-14 10:50:09 -07001702 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -08001703 mSurfaceController.setPositionInTransaction(mWin.mFrame.left + left,
1704 mWin.mFrame.top + top, false);
Robert Carrfbbde852016-10-18 11:02:28 -07001705 applyCrop(null, null, false);
Robert Carre6a83512015-11-03 16:09:21 -08001706 } catch (RuntimeException e) {
1707 Slog.w(TAG, "Error positioning surface of " + mWin
1708 + " pos=(" + left + "," + top + ")", e);
1709 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -07001710 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -08001711 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1712 "<<< CLOSE TRANSACTION setWallpaperOffset");
Craig Mautner48ba1e72012-04-02 13:18:16 -07001713 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001714 }
1715
John Reck80181b92015-05-19 11:09:32 -07001716 /**
1717 * Try to change the pixel format without recreating the surface. This
1718 * will be common in the case of changing from PixelFormat.OPAQUE to
1719 * PixelFormat.TRANSLUCENT in the hardware-accelerated case as both
1720 * requested formats resolve to the same underlying SurfaceControl format
1721 * @return True if format was succesfully changed, false otherwise
1722 */
1723 boolean tryChangeFormatInPlaceLocked() {
Robert Carre6a83512015-11-03 16:09:21 -08001724 if (mSurfaceController == null) {
John Reck80181b92015-05-19 11:09:32 -07001725 return false;
1726 }
1727 final LayoutParams attrs = mWin.getAttrs();
Wale Ogunwalee7bf46b2015-09-30 09:19:28 -07001728 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
John Reck80181b92015-05-19 11:09:32 -07001729 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
1730 if (format == mSurfaceFormat) {
1731 setOpaqueLocked(!PixelFormat.formatHasAlpha(attrs.format));
1732 return true;
1733 }
1734 return false;
1735 }
1736
Craig Mautner6f612042014-09-07 13:13:23 -07001737 void setOpaqueLocked(boolean isOpaque) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001738 if (mSurfaceController == null) {
1739 return;
1740 }
Robert Carre6a83512015-11-03 16:09:21 -08001741 mSurfaceController.setOpaque(isOpaque);
Craig Mautner71dd1b62014-02-18 15:48:52 -08001742 }
1743
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001744 void setSecureLocked(boolean isSecure) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001745 if (mSurfaceController == null) {
1746 return;
1747 }
Robert Carre6a83512015-11-03 16:09:21 -08001748 mSurfaceController.setSecure(isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001749 }
1750
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001751 /**
1752 * Have the surface flinger show a surface, robustly dealing with
1753 * error conditions. In particular, if there is not enough memory
1754 * to show the surface, then we will try to get rid of other surfaces
1755 * in order to succeed.
1756 *
1757 * @return Returns true if the surface was successfully shown.
1758 */
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001759 private boolean showSurfaceRobustlyLocked() {
Robert Carr1b5ea722016-04-20 13:23:28 -07001760 final Task task = mWin.getTask();
1761 if (task != null && StackId.windowsAreScaleable(task.mStack.mStackId)) {
1762 mSurfaceController.forceScaleableInTransaction(true);
1763 }
1764
Robert Carre6a83512015-11-03 16:09:21 -08001765 boolean shown = mSurfaceController.showRobustlyInTransaction();
1766 if (!shown)
1767 return false;
1768
Robert Carre6a83512015-11-03 16:09:21 -08001769 return true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001770 }
1771
1772 void applyEnterAnimationLocked() {
Robert Carrb439a632016-04-07 22:52:10 -07001773 // If we are the new part of a window replacement transition and we have requested
1774 // not to animate, we instead want to make it seamless, so we don't want to apply
1775 // an enter transition.
1776 if (mWin.mSkipEnterAnimationForSeamlessReplacement) {
1777 return;
1778 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001779 final int transit;
1780 if (mEnterAnimationPending) {
1781 mEnterAnimationPending = false;
1782 transit = WindowManagerPolicy.TRANSIT_ENTER;
1783 } else {
1784 transit = WindowManagerPolicy.TRANSIT_SHOW;
1785 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001786 applyAnimationLocked(transit, true);
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001787 //TODO (multidisplay): Magnification is supported only for the default display.
Svetoslav8e3feb12014-02-24 13:46:47 -08001788 if (mService.mAccessibilityController != null
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001789 && mWin.getDisplayId() == DEFAULT_DISPLAY) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001790 mService.mAccessibilityController.onWindowTransitionLocked(mWin, transit);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001791 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001792 }
1793
1794 /**
1795 * Choose the correct animation and set it to the passed WindowState.
Craig Mautner4b71aa12012-12-27 17:20:01 -08001796 * @param transit If AppTransition.TRANSIT_PREVIEW_DONE and the app window has been drawn
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001797 * then the animation will be app_starting_exit. Any other value loads the animation from
1798 * the switch statement below.
1799 * @param isEntrance The animation type the last time this was called. Used to keep from
1800 * loading the same animation twice.
1801 * @return true if an animation has been loaded.
1802 */
1803 boolean applyAnimationLocked(int transit, boolean isEntrance) {
Jorim Jaggife762342016-10-13 14:33:27 +02001804 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001805 // If we are trying to apply an animation, but already running
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001806 // an animation of the same type, then just leave that one alone.
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001807 return true;
1808 }
1809
1810 // Only apply an animation if the display isn't frozen. If it is
1811 // frozen, there is no reason to animate and it can cause strange
1812 // artifacts when we unfreeze the display if some different animation
1813 // is running.
Chong Zhang8784be62016-06-28 15:25:07 -07001814 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "WSA#applyAnimationLocked");
David Stevensf62360c2017-03-16 19:00:20 -07001815 if (mWin.mToken.okToAnimate()) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001816 int anim = mPolicy.selectAnimationLw(mWin, transit);
1817 int attr = -1;
1818 Animation a = null;
1819 if (anim != 0) {
Dianne Hackborn4c1e3182012-10-05 18:37:54 -07001820 a = anim != -1 ? AnimationUtils.loadAnimation(mContext, anim) : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001821 } else {
1822 switch (transit) {
1823 case WindowManagerPolicy.TRANSIT_ENTER:
1824 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1825 break;
1826 case WindowManagerPolicy.TRANSIT_EXIT:
1827 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1828 break;
1829 case WindowManagerPolicy.TRANSIT_SHOW:
1830 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1831 break;
1832 case WindowManagerPolicy.TRANSIT_HIDE:
1833 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1834 break;
1835 }
1836 if (attr >= 0) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001837 a = mService.mAppTransition.loadAnimationAttr(mWin.mAttrs, attr);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001838 }
1839 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001840 if (DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001841 "applyAnimation: win=" + this
1842 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
Craig Mautner4d7349b2012-04-20 14:52:47 -07001843 + " a=" + a
Craig Mautner8863cca2012-09-18 15:04:34 -07001844 + " transit=" + transit
Craig Mautner83339b42012-05-01 22:13:23 -07001845 + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001846 if (a != null) {
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -08001847 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001848 setAnimation(a);
1849 mAnimationIsEntrance = isEntrance;
1850 }
1851 } else {
1852 clearAnimation();
1853 }
Chong Zhang8784be62016-06-28 15:25:07 -07001854 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
1855
Jorim Jaggieb88d832016-04-13 20:17:43 -07001856 if (mWin.mAttrs.type == TYPE_INPUT_METHOD) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001857 mWin.getDisplayContent().adjustForImeIfNeeded();
Jorim Jaggi7a4fd5e2016-04-14 19:55:35 -07001858 if (isEntrance) {
1859 mWin.setDisplayLayoutNeeded();
1860 mService.mWindowPlacerLocked.requestTraversal();
1861 }
Jorim Jaggieb88d832016-04-13 20:17:43 -07001862 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001863 return mAnimation != null;
1864 }
1865
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001866 private void applyFadeoutDuringKeyguardExitAnimation() {
1867 long startTime = mAnimation.getStartTime();
1868 long duration = mAnimation.getDuration();
1869 long elapsed = mLastAnimationTime - startTime;
1870 long fadeDuration = duration - elapsed;
1871 if (fadeDuration <= 0) {
1872 // Never mind, this would be no visible animation, so abort the animation change.
1873 return;
1874 }
1875 AnimationSet newAnimation = new AnimationSet(false /* shareInterpolator */);
1876 newAnimation.setDuration(duration);
1877 newAnimation.setStartTime(startTime);
1878 newAnimation.addAnimation(mAnimation);
1879 Animation fadeOut = AnimationUtils.loadAnimation(
1880 mContext, com.android.internal.R.anim.app_starting_exit);
1881 fadeOut.setDuration(fadeDuration);
1882 fadeOut.setStartOffset(elapsed);
1883 newAnimation.addAnimation(fadeOut);
Filip Gruszczynskif52dd202015-11-15 20:36:38 -08001884 newAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(), mAnimDx, mAnimDy);
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001885 mAnimation = newAnimation;
1886 }
1887
Craig Mautnera2c77052012-03-26 12:14:43 -07001888 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1889 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1890 || mAnimation != null) {
1891 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1892 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1893 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001894 pw.print(" mAnimation="); pw.print(mAnimation);
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001895 pw.print(" mStackClip="); pw.println(mStackClip);
Craig Mautnera2c77052012-03-26 12:14:43 -07001896 }
1897 if (mHasTransformation || mHasLocalTransformation) {
1898 pw.print(prefix); pw.print("XForm: has=");
1899 pw.print(mHasTransformation);
1900 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1901 pw.print(" "); mTransformation.printShortString(pw);
1902 pw.println();
1903 }
Robert Carre6a83512015-11-03 16:09:21 -08001904 if (mSurfaceController != null) {
1905 mSurfaceController.dump(pw, prefix, dumpAll);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001906 }
Robert Carre6a83512015-11-03 16:09:21 -08001907 if (dumpAll) {
1908 pw.print(prefix); pw.print("mDrawState="); pw.print(drawStateToString());
1909 pw.print(prefix); pw.print(" mLastHidden="); pw.println(mLastHidden);
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001910 pw.print(prefix); pw.print("mSystemDecorRect="); mSystemDecorRect.printShortString(pw);
1911 pw.print(" last="); mLastSystemDecorRect.printShortString(pw);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001912 pw.print(" mHasClipRect="); pw.print(mHasClipRect);
1913 pw.print(" mLastClipRect="); mLastClipRect.printShortString(pw);
1914
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001915 if (!mLastFinalClipRect.isEmpty()) {
1916 pw.print(" mLastFinalClipRect="); mLastFinalClipRect.printShortString(pw);
1917 }
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001918 pw.println();
Robert Carre6a83512015-11-03 16:09:21 -08001919 }
1920
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001921 if (mPendingDestroySurface != null) {
1922 pw.print(prefix); pw.print("mPendingDestroySurface=");
1923 pw.println(mPendingDestroySurface);
1924 }
1925 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1926 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1927 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1928 }
1929 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1930 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1931 pw.print(" mAlpha="); pw.print(mAlpha);
1932 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1933 }
1934 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1935 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1936 pw.print(" mDsDx="); pw.print(mDsDx);
1937 pw.print(" mDtDx="); pw.print(mDtDx);
Robert Carr0edf18f2017-02-21 20:01:47 -08001938 pw.print(" mDtDy="); pw.print(mDtDy);
1939 pw.print(" mDsDy="); pw.println(mDsDy);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001940 }
Jorim Jaggiff71d202016-04-14 13:12:36 -07001941 if (mAnimationStartDelayed) {
1942 pw.print(prefix); pw.print("mAnimationStartDelayed="); pw.print(mAnimationStartDelayed);
1943 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001944 }
1945
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001946 @Override
1947 public String toString() {
Dianne Hackborn529e7442012-11-01 14:22:28 -07001948 StringBuffer sb = new StringBuffer("WindowStateAnimator{");
1949 sb.append(Integer.toHexString(System.identityHashCode(this)));
1950 sb.append(' ');
1951 sb.append(mWin.mAttrs.getTitle());
1952 sb.append('}');
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001953 return sb.toString();
1954 }
Robert Carre6a83512015-11-03 16:09:21 -08001955
1956 void reclaimSomeSurfaceMemory(String operation, boolean secure) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001957 mService.mRoot.reclaimSomeSurfaceMemory(this, operation, secure);
Robert Carre6a83512015-11-03 16:09:21 -08001958 }
1959
1960 boolean getShown() {
1961 if (mSurfaceController != null) {
1962 return mSurfaceController.getShown();
1963 }
1964 return false;
1965 }
1966
1967 void destroySurface() {
Wale Ogunwale722ff892016-02-18 13:37:55 -08001968 try {
1969 if (mSurfaceController != null) {
1970 mSurfaceController.destroyInTransaction();
1971 }
1972 } catch (RuntimeException e) {
1973 Slog.w(TAG, "Exception thrown when destroying surface " + this
1974 + " surface " + mSurfaceController + " session " + mSession + ": " + e);
1975 } finally {
1976 mWin.setHasSurface(false);
1977 mSurfaceController = null;
1978 mDrawState = NO_SURFACE;
1979 }
Robert Carre6a83512015-11-03 16:09:21 -08001980 }
Filip Gruszczynskif52dd202015-11-15 20:36:38 -08001981
1982 void setMoveAnimation(int left, int top) {
1983 final Animation a = AnimationUtils.loadAnimation(mContext,
1984 com.android.internal.R.anim.window_move_from_decor);
1985 setAnimation(a);
1986 mAnimDx = mWin.mLastFrame.left - left;
1987 mAnimDy = mWin.mLastFrame.top - top;
1988 mAnimateMove = true;
1989 }
Robert Carr679c8072016-04-07 15:51:48 -07001990
1991 void deferTransactionUntilParentFrame(long frameNumber) {
1992 if (!mWin.isChildWindow()) {
1993 return;
1994 }
Robert Carr679c8072016-04-07 15:51:48 -07001995 mSurfaceController.deferTransactionUntil(
Wale Ogunwalecaa53af2016-07-17 14:50:26 -07001996 mWin.getParentWindow().mWinAnimator.mSurfaceController.getHandle(), frameNumber);
Robert Carr679c8072016-04-07 15:51:48 -07001997 }
1998
Jorim Jaggiff71d202016-04-14 13:12:36 -07001999 /**
2000 * Sometimes we need to synchronize the first frame of animation with some external event.
2001 * To achieve this, we prolong the start of the animation and keep producing the first frame of
2002 * the animation.
2003 */
2004 private long getAnimationFrameTime(Animation animation, long currentTime) {
2005 if (mAnimationStartDelayed) {
2006 animation.setStartTime(currentTime);
2007 return currentTime + 1;
2008 }
2009 return currentTime;
2010 }
2011
2012 void startDelayingAnimationStart() {
2013 mAnimationStartDelayed = true;
2014 }
2015
2016 void endDelayingAnimationStart() {
2017 mAnimationStartDelayed = false;
2018 }
Robert Carr6da3cc02016-06-16 15:17:07 -07002019
2020 void seamlesslyRotateWindow(int oldRotation, int newRotation) {
2021 final WindowState w = mWin;
2022 if (!w.isVisibleNow() || w.mIsWallpaper) {
2023 return;
2024 }
2025
2026 final Rect cropRect = mService.mTmpRect;
2027 final Rect displayRect = mService.mTmpRect2;
2028 final RectF frameRect = mService.mTmpRectF;
2029 final Matrix transform = mService.mTmpTransform;
2030
2031 final float x = w.mFrame.left;
2032 final float y = w.mFrame.top;
2033 final float width = w.mFrame.width();
2034 final float height = w.mFrame.height();
2035
2036 mService.getDefaultDisplayContentLocked().getLogicalDisplayRect(displayRect);
2037 final float displayWidth = displayRect.width();
2038 final float displayHeight = displayRect.height();
2039
2040 // Compute a transform matrix to undo the coordinate space transformation,
2041 // and present the window at the same physical position it previously occupied.
2042 final int deltaRotation = DisplayContent.deltaRotation(newRotation, oldRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002043 DisplayContent.createRotationMatrix(deltaRotation, x, y, displayWidth, displayHeight,
2044 transform);
Robert Carr6da3cc02016-06-16 15:17:07 -07002045
Robert Carr897215d2017-03-29 12:25:50 -07002046 // We just need to apply a rotation matrix to the window. For example
2047 // if we have a portrait window and rotate to landscape, the window is still portrait
2048 // and now extends off the bottom of the screen (and only halfway across). Essentially we
2049 // apply a transform to display the current buffer at it's old position
2050 // (in the new coordinate space). We then freeze layer updates until the resize
2051 // occurs, at which point we undo, them.
2052 mService.markForSeamlessRotation(w, true);
2053 transform.getValues(mService.mTmpFloats);
Robert Carr6da3cc02016-06-16 15:17:07 -07002054
Robert Carr897215d2017-03-29 12:25:50 -07002055 float DsDx = mService.mTmpFloats[Matrix.MSCALE_X];
2056 float DtDx = mService.mTmpFloats[Matrix.MSKEW_Y];
2057 float DtDy = mService.mTmpFloats[Matrix.MSKEW_X];
2058 float DsDy = mService.mTmpFloats[Matrix.MSCALE_Y];
2059 float nx = mService.mTmpFloats[Matrix.MTRANS_X];
2060 float ny = mService.mTmpFloats[Matrix.MTRANS_Y];
2061 mSurfaceController.setPositionInTransaction(nx, ny, false);
2062 mSurfaceController.setMatrixInTransaction(DsDx * w.mHScale,
2063 DtDx * w.mVScale,
2064 DtDy * w.mHScale,
2065 DsDy * w.mVScale, false);
Robert Carr6da3cc02016-06-16 15:17:07 -07002066 }
Robert Carr3b716242016-08-16 16:02:21 -07002067
2068 void enableSurfaceTrace(FileDescriptor fd) {
2069 if (mSurfaceController != null) {
2070 mSurfaceController.installRemoteTrace(fd);
2071 }
2072 }
2073
2074 void disableSurfaceTrace() {
2075 if (mSurfaceController != null) {
2076 try {
2077 mSurfaceController.removeRemoteTrace();
2078 } catch (ClassCastException e) {
2079 Slog.e(TAG, "Disable surface trace for " + this + " but its not enabled");
2080 }
2081 }
2082 }
Robert Carr8f0a3ad2017-02-15 19:30:28 -08002083
2084 /** The force-scaled state for a given window can persist past
2085 * the state for it's stack as the windows complete resizing
2086 * independently of one another.
2087 */
2088 boolean isForceScaled() {
2089 final Task task = mWin.getTask();
2090 if (task != null && task.mStack.isForceScaled()) {
2091 return true;
2092 }
2093 return mForceScaleUntilResize;
2094 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08002095
2096 void detachChildren() {
2097 if (mSurfaceController != null) {
2098 mSurfaceController.detachChildren();
2099 }
2100 }
Craig Mautnera2c77052012-03-26 12:14:43 -07002101}