blob: 9d3db7c662cecf03bcbf1d04cea19f034adcfe54 [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;
Steven Timotiusaf03df62017-07-18 16:56:43 -070049import static com.android.server.wm.proto.WindowStateAnimatorProto.LAST_CLIP_RECT;
50import static com.android.server.wm.proto.WindowStateAnimatorProto.SURFACE;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070051
Craig Mautnerc2f9be02012-03-27 17:32:29 -070052import android.content.Context;
53import android.graphics.Matrix;
54import android.graphics.PixelFormat;
Craig Mautner7358fbf2012-04-12 21:06:33 -070055import android.graphics.Point;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070056import android.graphics.Rect;
Robert Carr6da3cc02016-06-16 15:17:07 -070057import android.graphics.RectF;
Craig Mautner48ba1e72012-04-02 13:18:16 -070058import android.graphics.Region;
Craig Mautnera51a9562012-04-17 17:05:26 -070059import android.os.Debug;
Chong Zhang8784be62016-06-28 15:25:07 -070060import android.os.Trace;
Craig Mautnera2c77052012-03-26 12:14:43 -070061import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070062import android.util.proto.ProtoOutputStream;
Craig Mautner59c00972012-07-30 12:10:24 -070063import android.view.DisplayInfo;
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070064import android.view.MagnificationSpec;
Igor Murashkina86ab6402013-08-30 12:58:36 -070065import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080066import android.view.SurfaceControl;
Craig Mautnera2c77052012-03-26 12:14:43 -070067import android.view.WindowManager;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070068import android.view.WindowManager.LayoutParams;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080069import android.view.WindowManagerPolicy;
Craig Mautnera2c77052012-03-26 12:14:43 -070070import android.view.animation.Animation;
Jorim Jaggif8d77da2014-11-11 16:59:12 +010071import android.view.animation.AnimationSet;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070072import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070073import android.view.animation.Transformation;
74
Craig Mautnera2c77052012-03-26 12:14:43 -070075import java.io.PrintWriter;
Robert Carr3b716242016-08-16 16:02:21 -070076import java.io.FileDescriptor;
Craig Mautnera2c77052012-03-26 12:14:43 -070077
78/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070079 * Keep track of animations and surface operations for a single WindowState.
80 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070081class WindowStateAnimator {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080082 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowStateAnimator" : TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070083 static final int WINDOW_FREEZE_LAYER = TYPE_LAYER_MULTIPLIER * 200;
Craig Mautnera2c77052012-03-26 12:14:43 -070084
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +010085 /**
86 * Mode how the window gets clipped by the stack bounds during an animation: The clipping should
87 * be applied after applying the animation transformation, i.e. the stack bounds don't move
88 * during the animation.
89 */
90 static final int STACK_CLIP_AFTER_ANIM = 0;
91
92 /**
93 * Mode how the window gets clipped by the stack bounds: The clipping should be applied before
94 * applying the animation transformation, i.e. the stack bounds move with the window.
95 */
96 static final int STACK_CLIP_BEFORE_ANIM = 1;
97
98 /**
99 * Mode how window gets clipped by the stack bounds during an animation: Don't clip the window
100 * by the stack bounds.
101 */
102 static final int STACK_CLIP_NONE = 2;
103
Craig Mautner918b53b2012-07-09 14:15:54 -0700104 // Unchanging local convenience fields.
Craig Mautnera2c77052012-03-26 12:14:43 -0700105 final WindowManagerService mService;
106 final WindowState mWin;
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700107 private final WindowStateAnimator mParentWinAnimator;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700108 final WindowAnimator mAnimator;
Craig Mautner8863cca2012-09-18 15:04:34 -0700109 AppWindowAnimator mAppAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700110 final Session mSession;
111 final WindowManagerPolicy mPolicy;
112 final Context mContext;
Craig Mautner918b53b2012-07-09 14:15:54 -0700113 final boolean mIsWallpaper;
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700114 private final WallpaperController mWallpaperControllerLocked;
Craig Mautnera2c77052012-03-26 12:14:43 -0700115
116 // Currently running animation.
117 boolean mAnimating;
118 boolean mLocalAnimating;
119 Animation mAnimation;
120 boolean mAnimationIsEntrance;
121 boolean mHasTransformation;
122 boolean mHasLocalTransformation;
123 final Transformation mTransformation = new Transformation();
124 boolean mWasAnimating; // Were we animating going into the most recent animation step?
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700125 int mAnimLayer;
126 int mLastLayer;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100127 long mAnimationStartTime;
Jorim Jaggif8d77da2014-11-11 16:59:12 +0100128 long mLastAnimationTime;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100129 int mStackClip = STACK_CLIP_BEFORE_ANIM;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700130
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700131 /**
132 * Set when we have changed the size of the surface, to know that
133 * we must tell them application to resize (and thus redraw itself).
134 */
135 boolean mSurfaceResized;
Chong Zhang5b2f1992015-11-13 15:40:36 -0800136 /**
137 * Whether we should inform the client on next relayoutWindow that
138 * the surface has been resized since last time.
139 */
140 boolean mReportSurfaceResized;
Robert Carre6a83512015-11-03 16:09:21 -0800141 WindowSurfaceController mSurfaceController;
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800142 private WindowSurfaceController mPendingDestroySurface;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700143
144 /**
145 * Set if the client has asked that the destroy of its surface be delayed
146 * until it explicitly says it is okay.
147 */
148 boolean mSurfaceDestroyDeferred;
149
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800150 private boolean mDestroyPreservedSurfaceUponRedraw;
Craig Mautner7d8df392012-04-06 15:26:23 -0700151 float mShownAlpha = 0;
152 float mAlpha = 0;
153 float mLastAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700154
Winson Chung399f6202014-03-19 10:47:20 -0700155 boolean mHasClipRect;
156 Rect mClipRect = new Rect();
157 Rect mTmpClipRect = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100158 Rect mTmpFinalClipRect = new Rect();
Winson Chung399f6202014-03-19 10:47:20 -0700159 Rect mLastClipRect = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100160 Rect mLastFinalClipRect = new Rect();
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -0700161 Rect mTmpStackBounds = new Rect();
Winson Chung08f81892017-03-02 15:40:51 -0800162 private Rect mTmpAnimatingBounds = new Rect();
163 private Rect mTmpSourceBounds = new Rect();
Winson Chung399f6202014-03-19 10:47:20 -0700164
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -0800165 /**
166 * This is rectangle of the window's surface that is not covered by
167 * system decorations.
168 */
169 private final Rect mSystemDecorRect = new Rect();
170 private final Rect mLastSystemDecorRect = new Rect();
171
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800172 // Used to save animation distances between the time they are calculated and when they are used.
173 private int mAnimDx;
174 private int mAnimDy;
Jorim Jaggic5af4f82015-07-01 17:16:27 -0700175
176 /** Is the next animation to be started a window move animation? */
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800177 private boolean mAnimateMove = false;
Jorim Jaggic5af4f82015-07-01 17:16:27 -0700178
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700179 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
Robert Carr0edf18f2017-02-21 20:01:47 -0800180 private float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700181
182 boolean mHaveMatrix;
183
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700184 // Set to true if, when the window gets displayed, it should perform
185 // an enter animation.
186 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700187
Craig Mautner9c795042014-10-28 19:59:59 -0700188 /** Used to indicate that this window is undergoing an enter animation. Used for system
189 * windows to make the callback to View.dispatchOnWindowShownCallback(). Set when the
190 * window is first added or shown, cleared when the callback has been made. */
191 boolean mEnteringAnimation;
192
Jorim Jaggiff71d202016-04-14 13:12:36 -0700193 private boolean mAnimationStartDelayed;
194
John Reck80181b92015-05-19 11:09:32 -0700195 /** The pixel format of the underlying SurfaceControl */
196 int mSurfaceFormat;
197
Craig Mautner749a7bb2012-04-02 13:49:53 -0700198 /** This is set when there is no Surface */
199 static final int NO_SURFACE = 0;
200 /** This is set after the Surface has been created but before the window has been drawn. During
201 * this time the surface is hidden. */
202 static final int DRAW_PENDING = 1;
203 /** This is set after the window has finished drawing for the first time but before its surface
204 * is shown. The surface will be displayed when the next layout is run. */
205 static final int COMMIT_DRAW_PENDING = 2;
206 /** This is set during the time after the window's drawing has been committed, and before its
207 * surface is actually shown. It is used to delay showing the surface until all windows in a
208 * token are ready to be shown. */
209 static final int READY_TO_SHOW = 3;
210 /** Set when the window has been shown in the screen the first time. */
211 static final int HAS_DRAWN = 4;
Adrian Roos3eeb4e62014-05-19 12:43:26 +0200212
Craig Mautner276a6eb2014-11-04 15:32:57 -0800213 String drawStateToString() {
214 switch (mDrawState) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700215 case NO_SURFACE: return "NO_SURFACE";
216 case DRAW_PENDING: return "DRAW_PENDING";
217 case COMMIT_DRAW_PENDING: return "COMMIT_DRAW_PENDING";
218 case READY_TO_SHOW: return "READY_TO_SHOW";
219 case HAS_DRAWN: return "HAS_DRAWN";
Craig Mautner276a6eb2014-11-04 15:32:57 -0800220 default: return Integer.toString(mDrawState);
Craig Mautner6fbda632012-07-03 09:26:39 -0700221 }
222 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700223 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700224
Craig Mautner749a7bb2012-04-02 13:49:53 -0700225 /** Was this window last hidden? */
226 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700227
Craig Mautnerbec53f72012-04-05 11:49:05 -0700228 int mAttrType;
229
Robert Carr679c8072016-04-07 15:51:48 -0700230 static final long PENDING_TRANSACTION_FINISH_WAIT_TIME = 100;
Robert Carr679c8072016-04-07 15:51:48 -0700231
Robert Carrc7294602016-05-13 11:32:05 -0700232 boolean mForceScaleUntilResize;
233
Robert Carr04092112016-06-02 12:56:12 -0700234 // WindowState.mHScale and WindowState.mVScale contain the
235 // scale according to client specified layout parameters (e.g.
236 // one layout size, with another surface size, creates such scaling).
237 // Here we track an additional scaling factor used to follow stack
238 // scaling (as in the case of the Pinned stack animation).
239 float mExtraHScale = (float) 1.0;
240 float mExtraVScale = (float) 1.0;
241
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800242 private final Rect mTmpSize = new Rect();
243
Craig Mautnerc431e892015-02-11 13:14:26 -0800244 WindowStateAnimator(final WindowState win) {
Craig Mautner918b53b2012-07-09 14:15:54 -0700245 final WindowManagerService service = win.mService;
246
Craig Mautnera2c77052012-03-26 12:14:43 -0700247 mService = service;
Craig Mautner918b53b2012-07-09 14:15:54 -0700248 mAnimator = service.mAnimator;
249 mPolicy = service.mPolicy;
250 mContext = service.mContext;
Craig Mautnerd3849f52014-03-27 13:19:29 -0700251 final DisplayContent displayContent = win.getDisplayContent();
252 if (displayContent != null) {
253 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800254 mAnimDx = displayInfo.appWidth;
255 mAnimDy = displayInfo.appHeight;
Craig Mautnerd3849f52014-03-27 13:19:29 -0700256 } else {
257 Slog.w(TAG, "WindowStateAnimator ctor: Display has been removed");
258 // This is checked on return and dealt with.
259 }
Craig Mautner918b53b2012-07-09 14:15:54 -0700260
261 mWin = win;
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700262 mParentWinAnimator = !win.isChildWindow() ? null : win.getParentWindow().mWinAnimator;
Craig Mautner322e4032012-07-13 13:35:20 -0700263 mAppAnimator = win.mAppToken == null ? null : win.mAppToken.mAppAnimator;
Craig Mautner918b53b2012-07-09 14:15:54 -0700264 mSession = win.mSession;
Craig Mautner918b53b2012-07-09 14:15:54 -0700265 mAttrType = win.mAttrs.type;
266 mIsWallpaper = win.mIsWallpaper;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700267 mWallpaperControllerLocked = mService.mRoot.mWallpaperController;
Craig Mautnera2c77052012-03-26 12:14:43 -0700268 }
269
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100270 public void setAnimation(Animation anim, long startTime, int stackClip) {
Craig Mautnerbec53f72012-04-05 11:49:05 -0700271 if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700272 mAnimating = false;
273 mLocalAnimating = false;
274 mAnimation = anim;
275 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700276 mAnimation.scaleCurrentDuration(mService.getWindowAnimationScaleLocked());
Craig Mautnera2c77052012-03-26 12:14:43 -0700277 // Start out animation gone if window is gone, or visible if window is visible.
278 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700279 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700280 mHasLocalTransformation = true;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100281 mAnimationStartTime = startTime;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100282 mStackClip = stackClip;
283 }
284
285 public void setAnimation(Animation anim, int stackClip) {
286 setAnimation(anim, -1, stackClip);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100287 }
288
289 public void setAnimation(Animation anim) {
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100290 setAnimation(anim, -1, STACK_CLIP_AFTER_ANIM);
Craig Mautnera2c77052012-03-26 12:14:43 -0700291 }
292
293 public void clearAnimation() {
294 if (mAnimation != null) {
295 mAnimating = true;
296 mLocalAnimating = false;
297 mAnimation.cancel();
298 mAnimation = null;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100299 mStackClip = STACK_CLIP_BEFORE_ANIM;
Craig Mautnera2c77052012-03-26 12:14:43 -0700300 }
301 }
302
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700303 /**
304 * Is the window or its container currently set to animate or currently animating?
305 */
306 boolean isAnimationSet() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700307 return mAnimation != null
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700308 || (mParentWinAnimator != null && mParentWinAnimator.mAnimation != null)
Jorim Jaggi37875612015-02-19 21:05:31 +0100309 || (mAppAnimator != null && mAppAnimator.isAnimating());
Craig Mautnera2c77052012-03-26 12:14:43 -0700310 }
311
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700312 /**
313 * @return whether an animation is about to start, i.e. the animation is set already but we
314 * haven't processed the first frame yet.
315 */
316 boolean isAnimationStarting() {
317 return isAnimationSet() && !mAnimating;
318 }
319
Craig Mautner0afddcb2012-05-08 15:38:00 -0700320 /** Is the window animating the DummyAnimation? */
321 boolean isDummyAnimation() {
Craig Mautner322e4032012-07-13 13:35:20 -0700322 return mAppAnimator != null
Jorim Jaggiff71d202016-04-14 13:12:36 -0700323 && mAppAnimator.animation == sDummyAnimation;
Craig Mautner0afddcb2012-05-08 15:38:00 -0700324 }
325
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700326 /**
327 * Is this window currently set to animate or currently animating?
328 */
329 boolean isWindowAnimationSet() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700330 return mAnimation != null;
331 }
332
Chong Zhange22006d2016-05-09 10:59:59 -0700333 /**
334 * Is this window currently waiting to run an opening animation?
335 */
336 boolean isWaitingForOpening() {
337 return mService.mAppTransition.isTransitionSet() && isDummyAnimation()
338 && mService.mOpeningApps.contains(mWin.mAppToken);
339 }
340
Craig Mautnera2c77052012-03-26 12:14:43 -0700341 void cancelExitAnimationForNextAnimationLocked() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800342 if (DEBUG_ANIM) Slog.d(TAG,
343 "cancelExitAnimationForNextAnimationLocked: " + mWin);
344
Craig Mautnera2c77052012-03-26 12:14:43 -0700345 if (mAnimation != null) {
346 mAnimation.cancel();
347 mAnimation = null;
Craig Mautner4d7349b2012-04-20 14:52:47 -0700348 mLocalAnimating = false;
Robert Carr89a28ab2017-04-24 15:33:11 -0700349 mWin.destroyOrSaveSurfaceUnchecked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700350 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700351 }
352
353 private boolean stepAnimation(long currentTime) {
354 if ((mAnimation == null) || !mLocalAnimating) {
355 return false;
356 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700357 currentTime = getAnimationFrameTime(mAnimation, currentTime);
Craig Mautnera2c77052012-03-26 12:14:43 -0700358 mTransformation.clear();
359 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700360 if (mAnimationStartDelayed && mAnimationIsEntrance) {
361 mTransformation.setAlpha(0f);
362 }
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700363 if (false && DEBUG_ANIM) Slog.v(TAG, "Stepped animation in " + this + ": more=" + more
364 + ", xform=" + mTransformation);
Craig Mautnera2c77052012-03-26 12:14:43 -0700365 return more;
366 }
367
368 // This must be called while inside a transaction. Returns true if
369 // there is more animation to run.
370 boolean stepAnimationLocked(long currentTime) {
Chet Haase9c450412015-10-01 13:25:58 -0700371 // Save the animation state as it was before this step so WindowManagerService can tell if
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700372 // we just started or just stopped animating by comparing mWasAnimating with isAnimationSet().
Chet Haase9c450412015-10-01 13:25:58 -0700373 mWasAnimating = mAnimating;
Craig Mautnerdf88d732014-01-27 09:21:32 -0800374 final DisplayContent displayContent = mWin.getDisplayContent();
David Stevens9440dc82017-03-16 19:00:20 -0700375 if (mWin.mToken.okToAnimate()) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700376 // We will run animations as long as the display isn't frozen.
377
378 if (mWin.isDrawnLw() && mAnimation != null) {
379 mHasTransformation = true;
380 mHasLocalTransformation = true;
381 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700382 if (DEBUG_ANIM) Slog.v(
383 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700384 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
385 " wh=" + mWin.mFrame.height() +
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800386 " dx=" + mAnimDx + " dy=" + mAnimDy +
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700387 " scale=" + mService.getWindowAnimationScaleLocked());
Craig Mautnerdf88d732014-01-27 09:21:32 -0800388 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
tiger_huangb7f3f922015-04-27 20:36:46 +0800389 if (mAnimateMove) {
390 mAnimateMove = false;
391 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800392 mAnimDx, mAnimDy);
tiger_huangb7f3f922015-04-27 20:36:46 +0800393 } else {
394 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
395 displayInfo.appWidth, displayInfo.appHeight);
396 }
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800397 mAnimDx = displayInfo.appWidth;
398 mAnimDy = displayInfo.appHeight;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100399 mAnimation.setStartTime(mAnimationStartTime != -1
400 ? mAnimationStartTime
401 : currentTime);
Craig Mautnera2c77052012-03-26 12:14:43 -0700402 mLocalAnimating = true;
403 mAnimating = true;
404 }
405 if ((mAnimation != null) && mLocalAnimating) {
Jorim Jaggif8d77da2014-11-11 16:59:12 +0100406 mLastAnimationTime = currentTime;
Craig Mautnera2c77052012-03-26 12:14:43 -0700407 if (stepAnimation(currentTime)) {
408 return true;
409 }
410 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700411 if (DEBUG_ANIM) Slog.v(
412 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700413 " @ " + currentTime);
414 //WindowManagerService.this.dump();
415 }
416 mHasLocalTransformation = false;
Craig Mautner322e4032012-07-13 13:35:20 -0700417 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppAnimator != null
418 && mAppAnimator.animation != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700419 // When our app token is animating, we kind-of pretend like
420 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
421 // part of this check means that we will only do this if
422 // our window is not currently exiting, or it is not
423 // locally animating itself. The idea being that one that
424 // is exiting and doing a local animation should be removed
425 // once that animation is done.
426 mAnimating = true;
427 mHasTransformation = true;
428 mTransformation.clear();
429 return false;
430 } else if (mHasTransformation) {
431 // Little trick to get through the path below to act like
432 // we have finished an animation.
433 mAnimating = true;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700434 } else if (isAnimationSet()) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700435 mAnimating = true;
436 }
437 } else if (mAnimation != null) {
438 // If the display is frozen, and there is a pending animation,
439 // clear it and make sure we run the cleanup code.
440 mAnimating = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700441 }
442
443 if (!mAnimating && !mLocalAnimating) {
444 return false;
445 }
446
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700447 // Done animating, clean up.
448 if (DEBUG_ANIM) Slog.v(
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800449 TAG, "Animation done in " + this + ": exiting=" + mWin.mAnimatingExit
Craig Mautnera2c77052012-03-26 12:14:43 -0700450 + ", reportedVisible="
451 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
452
453 mAnimating = false;
454 mLocalAnimating = false;
455 if (mAnimation != null) {
456 mAnimation.cancel();
457 mAnimation = null;
458 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700459 if (mAnimator.mWindowDetachedWallpaper == mWin) {
460 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700461 }
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700462 mAnimLayer = mWin.getSpecialWindowAnimLayerAdjustment();
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800463 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700464 mHasTransformation = false;
465 mHasLocalTransformation = false;
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100466 mStackClip = STACK_CLIP_BEFORE_ANIM;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800467 mWin.checkPolicyVisibilityChange();
Craig Mautnera2c77052012-03-26 12:14:43 -0700468 mTransformation.clear();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800469 if (mAttrType == LayoutParams.TYPE_STATUS_BAR && mWin.mPolicyVisibility) {
Craig Mautner81defc72013-10-29 11:10:42 -0700470 // Upon completion of a not-visible to visible status bar animation a relayout is
471 // required.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800472 if (displayContent != null) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700473 displayContent.setLayoutNeeded();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800474 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700475 }
476
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700477 mWin.onExitAnimationDone();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800478 final int displayId = mWin.getDisplayId();
Craig Mautner76a71652012-09-03 23:23:58 -0700479 mAnimator.setPendingLayoutChanges(displayId, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800480 if (DEBUG_LAYOUT_REPEATS)
Filip Gruszczynski4501d232015-09-02 13:00:02 -0700481 mService.mWindowPlacerLocked.debugLayoutRepeats(
482 "WindowStateAnimator", mAnimator.getPendingLayoutChanges(displayId));
Craig Mautnera2c77052012-03-26 12:14:43 -0700483
484 if (mWin.mAppToken != null) {
485 mWin.mAppToken.updateReportedVisibilityLocked();
486 }
487
488 return false;
489 }
490
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800491 void hide(String reason) {
Craig Mautner0afddcb2012-05-08 15:38:00 -0700492 if (!mLastHidden) {
493 //dump();
494 mLastHidden = true;
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800495 if (mSurfaceController != null) {
496 mSurfaceController.hideInTransaction(reason);
497 }
Craig Mautner0afddcb2012-05-08 15:38:00 -0700498 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700499 }
500
Craig Mautnera608b882012-03-30 13:03:49 -0700501 boolean finishDrawingLocked() {
Craig Mautner42d04db2014-11-06 12:13:23 -0800502 final boolean startingWindow =
503 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
504 if (DEBUG_STARTING_WINDOW && startingWindow) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700505 Slog.v(TAG, "Finishing drawing window " + mWin + ": mDrawState="
Craig Mautner276a6eb2014-11-04 15:32:57 -0800506 + drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700507 }
Robert Carr1ca6a332016-04-11 18:00:43 -0700508
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700509 boolean layoutNeeded = mWin.clearAnimatingWithSavedSurface();
Chong Zhang92147042016-05-09 12:47:11 -0700510
Craig Mautner749a7bb2012-04-02 13:49:53 -0700511 if (mDrawState == DRAW_PENDING) {
Craig Mautneref25d7a2012-05-15 23:01:47 -0700512 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Chong Zhang5b2f1992015-11-13 15:40:36 -0800513 Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + mWin + " in "
Robert Carre6a83512015-11-03 16:09:21 -0800514 + mSurfaceController);
Craig Mautner42d04db2014-11-06 12:13:23 -0800515 if (DEBUG_STARTING_WINDOW && startingWindow) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700516 Slog.v(TAG, "Draw state now committed in " + mWin);
517 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700518 mDrawState = COMMIT_DRAW_PENDING;
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700519 layoutNeeded = true;
Craig Mautnera608b882012-03-30 13:03:49 -0700520 }
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700521
Chong Zhangcbbcc0f2016-05-17 20:46:58 -0700522 return layoutNeeded;
Craig Mautnera608b882012-03-30 13:03:49 -0700523 }
524
525 // This must be called while inside a transaction.
Craig Mautnerc431e892015-02-11 13:14:26 -0800526 boolean commitFinishDrawingLocked() {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200527 if (DEBUG_STARTING_WINDOW_VERBOSE &&
Craig Mautner6fbda632012-07-03 09:26:39 -0700528 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
529 Slog.i(TAG, "commitFinishDrawingLocked: " + mWin + " cur mDrawState="
Craig Mautner276a6eb2014-11-04 15:32:57 -0800530 + drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700531 }
Craig Mautner276a6eb2014-11-04 15:32:57 -0800532 if (mDrawState != COMMIT_DRAW_PENDING && mDrawState != READY_TO_SHOW) {
Craig Mautnera608b882012-03-30 13:03:49 -0700533 return false;
534 }
Craig Mautner6fbda632012-07-03 09:26:39 -0700535 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM) {
Robert Carre6a83512015-11-03 16:09:21 -0800536 Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurfaceController);
Craig Mautner6fbda632012-07-03 09:26:39 -0700537 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700538 mDrawState = READY_TO_SHOW;
Chong Zhang0275e392015-09-17 10:41:44 -0700539 boolean result = false;
Craig Mautnera608b882012-03-30 13:03:49 -0700540 final AppWindowToken atoken = mWin.mAppToken;
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800541 if (atoken == null || atoken.allDrawn || mWin.mAttrs.type == TYPE_APPLICATION_STARTING) {
Wale Ogunwale9d147902016-07-16 11:58:55 -0700542 result = mWin.performShowLocked();
Chong Zhang0275e392015-09-17 10:41:44 -0700543 }
Chong Zhang97782b42015-10-07 16:01:23 -0700544 return result;
Craig Mautnera608b882012-03-30 13:03:49 -0700545 }
546
Chong Zhang97782b42015-10-07 16:01:23 -0700547 void preserveSurfaceLocked() {
548 if (mDestroyPreservedSurfaceUponRedraw) {
Chong Zhangb9b0fec2016-02-11 18:51:51 -0800549 // This could happen when switching the surface mode very fast. For example,
550 // we preserved a surface when dragResizing changed to true. Then before the
551 // preserved surface is removed, dragResizing changed to false again.
552 // In this case, we need to leave the preserved surface alone, and destroy
553 // the actual surface, so that the createSurface call could create a surface
554 // of the proper size. The preserved surface will still be removed when client
555 // finishes drawing to the new surface.
556 mSurfaceDestroyDeferred = false;
557 destroySurfaceLocked();
558 mSurfaceDestroyDeferred = true;
Chong Zhang97782b42015-10-07 16:01:23 -0700559 return;
560 }
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -0800561 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "SET FREEZE LAYER", false);
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800562 if (mSurfaceController != null) {
563 mSurfaceController.setLayer(mAnimLayer + 1);
564 }
Chong Zhang97782b42015-10-07 16:01:23 -0700565 mDestroyPreservedSurfaceUponRedraw = true;
566 mSurfaceDestroyDeferred = true;
567 destroySurfaceLocked();
568 }
569
570 void destroyPreservedSurfaceLocked() {
571 if (!mDestroyPreservedSurfaceUponRedraw) {
572 return;
573 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800574 if (mSurfaceController != null) {
575 if (mPendingDestroySurface != null) {
576 // If we are preserving a surface but we aren't relaunching that means
577 // we are just doing an in-place switch. In that case any SurfaceFlinger side
578 // child layers need to be reparented to the new surface to make this
579 // transparent to the app.
580 if (mWin.mAppToken == null || mWin.mAppToken.isRelaunching() == false) {
581 SurfaceControl.openTransaction();
582 mPendingDestroySurface.reparentChildrenInTransaction(mSurfaceController);
583 SurfaceControl.closeTransaction();
584 }
585 }
586 }
587
Chong Zhang97782b42015-10-07 16:01:23 -0700588 destroyDeferredSurfaceLocked();
589 mDestroyPreservedSurfaceUponRedraw = false;
590 }
591
Chong Zhangeb665572016-05-09 18:28:27 -0700592 void markPreservedSurfaceForDestroy() {
593 if (mDestroyPreservedSurfaceUponRedraw
594 && !mService.mDestroyPreservedSurface.contains(mWin)) {
595 mService.mDestroyPreservedSurface.add(mWin);
596 }
597 }
598
Robert Carrda61ba92017-03-29 15:52:23 -0700599 private int getLayerStack() {
600 return mWin.getDisplayContent().getDisplay().getLayerStack();
601 }
602
603 void updateLayerStackInTransaction() {
604 if (mSurfaceController != null) {
605 mSurfaceController.setLayerStackInTransaction(
606 getLayerStack());
607 }
608 }
609
Robert Carrecc06b32017-04-18 14:25:10 -0700610 void resetDrawState() {
611 mDrawState = DRAW_PENDING;
612
613 if (mWin.mAppToken == null) {
614 return;
615 }
616
617 if (mWin.mAppToken.mAppAnimator.animation == null) {
618 mWin.mAppToken.clearAllDrawn();
619 } else {
620 // Currently animating, persist current state of allDrawn until animation
621 // is complete.
622 mWin.mAppToken.deferClearAllDrawn = true;
623 }
624 }
625
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500626 WindowSurfaceController createSurfaceLocked(int windowType, int ownerUid) {
Alan Viveretteccb11e12014-07-08 16:04:02 -0700627 final WindowState w = mWin;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700628 if (w.restoreSavedSurface()) {
Chong Zhang0464a932016-02-03 11:47:16 -0800629 if (DEBUG_ANIM) Slog.i(TAG,
630 "createSurface: " + this + ": called when we had a saved surface");
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800631 return mSurfaceController;
Chong Zhanga8975bd2016-01-28 17:13:47 -0800632 }
633
Wale Ogunwale722ff892016-02-18 13:37:55 -0800634 if (mSurfaceController != null) {
635 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700636 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800637
Robert Carr132c9f52017-07-31 17:02:30 -0700638 if ((mWin.mAttrs.privateFlags & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY) != 0) {
639 windowType = SurfaceControl.WINDOW_TYPE_DONT_SCREENSHOT;
640 }
641
Wale Ogunwale722ff892016-02-18 13:37:55 -0800642 w.setHasSurface(false);
643
644 if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,
645 "createSurface " + this + ": mDrawState=DRAW_PENDING");
646
Robert Carrecc06b32017-04-18 14:25:10 -0700647 resetDrawState();
Wale Ogunwale722ff892016-02-18 13:37:55 -0800648
649 mService.makeWindowFreezingScreenIfNeededLocked(w);
650
651 int flags = SurfaceControl.HIDDEN;
652 final WindowManager.LayoutParams attrs = w.mAttrs;
653
654 if (mService.isSecureLocked(w)) {
655 flags |= SurfaceControl.SECURE;
656 }
657
658 mTmpSize.set(w.mFrame.left + w.mXOffset, w.mFrame.top + w.mYOffset, 0, 0);
659 calculateSurfaceBounds(w, attrs);
660 final int width = mTmpSize.width();
661 final int height = mTmpSize.height();
662
663 if (DEBUG_VISIBILITY) {
664 Slog.v(TAG, "Creating surface in session "
665 + mSession.mSurfaceSession + " window " + this
666 + " w=" + width + " h=" + height
667 + " x=" + mTmpSize.left + " y=" + mTmpSize.top
668 + " format=" + attrs.format + " flags=" + flags);
669 }
670
671 // We may abort, so initialize to defaults.
672 mLastSystemDecorRect.set(0, 0, 0, 0);
673 mHasClipRect = false;
674 mClipRect.set(0, 0, 0, 0);
675 mLastClipRect.set(0, 0, 0, 0);
676
677 // Set up surface control with initial size.
678 try {
679
680 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
681 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
682 if (!PixelFormat.formatHasAlpha(attrs.format)
683 // Don't make surface with surfaceInsets opaque as they display a
684 // translucent shadow.
685 && attrs.surfaceInsets.left == 0
686 && attrs.surfaceInsets.top == 0
687 && attrs.surfaceInsets.right == 0
688 && attrs.surfaceInsets.bottom == 0
689 // Don't make surface opaque when resizing to reduce the amount of
690 // artifacts shown in areas the app isn't drawing content to.
691 && !w.isDragResizing()) {
692 flags |= SurfaceControl.OPAQUE;
693 }
694
695 mSurfaceController = new WindowSurfaceController(mSession.mSurfaceSession,
696 attrs.getTitle().toString(),
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500697 width, height, format, flags, this, windowType, ownerUid);
Robert Carr486bbb72017-05-30 11:25:22 -0700698 mSurfaceFormat = format;
Wale Ogunwale722ff892016-02-18 13:37:55 -0800699
700 w.setHasSurface(true);
701
702 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
703 Slog.i(TAG, " CREATE SURFACE "
704 + mSurfaceController + " IN SESSION "
705 + mSession.mSurfaceSession
706 + ": pid=" + mSession.mPid + " format="
707 + attrs.format + " flags=0x"
708 + Integer.toHexString(flags)
709 + " / " + this);
710 }
711 } catch (OutOfResourcesException e) {
712 Slog.w(TAG, "OutOfResourcesException creating surface");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700713 mService.mRoot.reclaimSomeSurfaceMemory(this, "create", true);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800714 mDrawState = NO_SURFACE;
715 return null;
716 } catch (Exception e) {
717 Slog.e(TAG, "Exception creating surface", e);
718 mDrawState = NO_SURFACE;
719 return null;
720 }
721
722 if (WindowManagerService.localLOGV) Slog.v(TAG, "Got surface: " + mSurfaceController
723 + ", set left=" + w.mFrame.left + " top=" + w.mFrame.top
724 + ", animLayer=" + mAnimLayer);
725
726 if (SHOW_LIGHT_TRANSACTIONS) {
727 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
728 WindowManagerService.logSurface(w, "CREATE pos=("
729 + w.mFrame.left + "," + w.mFrame.top + ") ("
730 + width + "x" + height + "), layer=" + mAnimLayer + " HIDE", false);
731 }
732
733 // Start a new transaction and apply position & offset.
Robert Carraf422a82017-04-10 18:34:33 -0700734
735 mService.openSurfaceTransaction();
736 try {
737 mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top, false);
738 mSurfaceController.setLayerStackInTransaction(getLayerStack());
739 mSurfaceController.setLayer(mAnimLayer);
740 } finally {
741 mService.closeSurfaceTransaction();
742 }
743
Wale Ogunwale722ff892016-02-18 13:37:55 -0800744 mLastHidden = true;
745
746 if (WindowManagerService.localLOGV) Slog.v(TAG, "Created surface " + this);
Robert Carre6a83512015-11-03 16:09:21 -0800747 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700748 }
749
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800750 private void calculateSurfaceBounds(WindowState w, LayoutParams attrs) {
751 if ((attrs.flags & FLAG_SCALED) != 0) {
752 // For a scaled surface, we always want the requested size.
753 mTmpSize.right = mTmpSize.left + w.mRequestedWidth;
754 mTmpSize.bottom = mTmpSize.top + w.mRequestedHeight;
755 } else {
756 // When we're doing a drag-resizing, request a surface that's fullscreen size,
757 // so that we don't need to reallocate during the process. This also prevents
758 // buffer drops due to size mismatch.
759 if (w.isDragResizing()) {
760 if (w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM) {
761 mTmpSize.left = 0;
762 mTmpSize.top = 0;
763 }
764 final DisplayInfo displayInfo = w.getDisplayInfo();
765 mTmpSize.right = mTmpSize.left + displayInfo.logicalWidth;
766 mTmpSize.bottom = mTmpSize.top + displayInfo.logicalHeight;
767 } else {
768 mTmpSize.right = mTmpSize.left + w.mCompatFrame.width();
769 mTmpSize.bottom = mTmpSize.top + w.mCompatFrame.height();
770 }
771 }
772
773 // Something is wrong and SurfaceFlinger will not like this, try to revert to sane values.
Filip Gruszczynskie6ed1952015-12-21 09:34:16 -0800774 // This doesn't necessarily mean that there is an error in the system. The sizes might be
775 // incorrect, because it is before the first layout or draw.
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800776 if (mTmpSize.width() < 1) {
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800777 mTmpSize.right = mTmpSize.left + 1;
778 }
779 if (mTmpSize.height() < 1) {
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800780 mTmpSize.bottom = mTmpSize.top + 1;
781 }
782
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800783 // Adjust for surface insets.
Phil Weavera4b32b92016-06-02 13:55:39 -0700784 mTmpSize.left -= attrs.surfaceInsets.left;
785 mTmpSize.top -= attrs.surfaceInsets.top;
786 mTmpSize.right += attrs.surfaceInsets.right;
787 mTmpSize.bottom += attrs.surfaceInsets.bottom;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800788 }
789
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800790 boolean hasSurface() {
Chong Zhang92147042016-05-09 12:47:11 -0700791 return !mWin.hasSavedSurface()
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800792 && mSurfaceController != null && mSurfaceController.hasSurface();
793 }
794
Craig Mautner96868332012-12-04 14:29:11 -0800795 void destroySurfaceLocked() {
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700796 final AppWindowToken wtoken = mWin.mAppToken;
797 if (wtoken != null) {
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700798 if (mWin == wtoken.startingWindow) {
799 wtoken.startingDisplayed = false;
800 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700801 }
802
Chong Zhang92147042016-05-09 12:47:11 -0700803 mWin.clearHasSavedSurface();
Robert Carr13f7be9e2015-12-02 18:39:45 -0800804
Wale Ogunwale722ff892016-02-18 13:37:55 -0800805 if (mSurfaceController == null) {
806 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700807 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800808
Wale Ogunwale722ff892016-02-18 13:37:55 -0800809 // When destroying a surface we want to make sure child windows are hidden. If we are
810 // preserving the surface until redraw though we intend to swap it out with another surface
811 // for resizing. In this case the window always remains visible to the user and the child
812 // windows should likewise remain visible.
Wale Ogunwale9d147902016-07-16 11:58:55 -0700813 if (!mDestroyPreservedSurfaceUponRedraw) {
814 mWin.mHidden = true;
Wale Ogunwale722ff892016-02-18 13:37:55 -0800815 }
816
817 try {
818 if (DEBUG_VISIBILITY) logWithStack(TAG, "Window " + this + " destroying surface "
819 + mSurfaceController + ", session " + mSession);
820 if (mSurfaceDestroyDeferred) {
821 if (mSurfaceController != null && mPendingDestroySurface != mSurfaceController) {
822 if (mPendingDestroySurface != null) {
823 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
824 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true);
825 }
826 mPendingDestroySurface.destroyInTransaction();
827 }
828 mPendingDestroySurface = mSurfaceController;
829 }
830 } else {
831 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
832 WindowManagerService.logSurface(mWin, "DESTROY", true);
833 }
834 destroySurface();
835 }
836 // Don't hide wallpaper if we're deferring the surface destroy
837 // because of a surface change.
838 if (!mDestroyPreservedSurfaceUponRedraw) {
839 mWallpaperControllerLocked.hideWallpapers(mWin);
840 }
841 } catch (RuntimeException e) {
842 Slog.w(TAG, "Exception thrown when destroying Window " + this
843 + " surface " + mSurfaceController + " session " + mSession + ": " + e.toString());
844 }
845
846 // Whether the surface was preserved (and copied to mPendingDestroySurface) or not, it
847 // needs to be cleared to match the WindowState.mHasSurface state. It is also necessary
848 // so it can be recreated successfully in mPendingDestroySurface case.
849 mWin.setHasSurface(false);
850 if (mSurfaceController != null) {
851 mSurfaceController.setShown(false);
852 }
853 mSurfaceController = null;
854 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700855 }
856
Craig Mautner96868332012-12-04 14:29:11 -0800857 void destroyDeferredSurfaceLocked() {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700858 try {
859 if (mPendingDestroySurface != null) {
860 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -0800861 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700862 }
Robert Carre6a83512015-11-03 16:09:21 -0800863 mPendingDestroySurface.destroyInTransaction();
Chong Zhang6e21cf42015-11-09 14:41:42 -0800864 // Don't hide wallpaper if we're destroying a deferred surface
865 // after a surface mode change.
866 if (!mDestroyPreservedSurfaceUponRedraw) {
867 mWallpaperControllerLocked.hideWallpapers(mWin);
868 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700869 }
870 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700871 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700872 + this + " surface " + mPendingDestroySurface
873 + " session " + mSession + ": " + e.toString());
874 }
875 mSurfaceDestroyDeferred = false;
876 mPendingDestroySurface = null;
877 }
878
Robert Carr298f9272016-08-09 13:08:01 -0700879 void applyMagnificationSpec(MagnificationSpec spec, Matrix transform) {
880 final int surfaceInsetLeft = mWin.mAttrs.surfaceInsets.left;
881 final int surfaceInsetTop = mWin.mAttrs.surfaceInsets.top;
882
883 if (spec != null && !spec.isNop()) {
884 float scale = spec.scale;
885 transform.postScale(scale, scale);
886 transform.postTranslate(spec.offsetX, spec.offsetY);
887
888 // As we are scaling the whole surface, to keep the content
889 // in the same position we will also have to scale the surfaceInsets.
890 transform.postTranslate(-(surfaceInsetLeft*scale - surfaceInsetLeft),
891 -(surfaceInsetTop*scale - surfaceInsetTop));
892 }
893 }
894
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700895 void computeShownFrameLocked() {
896 final boolean selfTransformation = mHasLocalTransformation;
897 Transformation attachedTransformation =
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700898 (mParentWinAnimator != null && mParentWinAnimator.mHasLocalTransformation)
899 ? mParentWinAnimator.mTransformation : null;
Craig Mautner322e4032012-07-13 13:35:20 -0700900 Transformation appTransformation = (mAppAnimator != null && mAppAnimator.hasTransformation)
901 ? mAppAnimator.transformation : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700902
903 // Wallpapers are animated based on the "real" window they
904 // are currently targeting.
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700905 final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget();
Craig Mautner798adef2013-10-22 14:29:01 -0700906 if (mIsWallpaper && wallpaperTarget != null && mService.mAnimateWallpaperWithTarget) {
907 final WindowStateAnimator wallpaperAnimator = wallpaperTarget.mWinAnimator;
Craig Mautner918b53b2012-07-09 14:15:54 -0700908 if (wallpaperAnimator.mHasLocalTransformation &&
909 wallpaperAnimator.mAnimation != null &&
910 !wallpaperAnimator.mAnimation.getDetachWallpaper()) {
911 attachedTransformation = wallpaperAnimator.mTransformation;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800912 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700913 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
914 }
915 }
Craig Mautner798adef2013-10-22 14:29:01 -0700916 final AppWindowAnimator wpAppAnimator = wallpaperTarget.mAppToken == null ?
917 null : wallpaperTarget.mAppToken.mAppAnimator;
918 if (wpAppAnimator != null && wpAppAnimator.hasTransformation
Craig Mautner918b53b2012-07-09 14:15:54 -0700919 && wpAppAnimator.animation != null
920 && !wpAppAnimator.animation.getDetachWallpaper()) {
Craig Mautner59431632012-04-04 11:56:44 -0700921 appTransformation = wpAppAnimator.transformation;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800922 if (DEBUG_WALLPAPER && appTransformation != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700923 Slog.v(TAG, "WP target app xform: " + appTransformation);
924 }
925 }
926 }
927
Craig Mautnera91f9e22012-09-14 16:22:08 -0700928 final int displayId = mWin.getDisplayId();
929 final ScreenRotationAnimation screenRotationAnimation =
930 mAnimator.getScreenRotationAnimationLocked(displayId);
931 final boolean screenAnimation =
932 screenRotationAnimation != null && screenRotationAnimation.isAnimating();
Robert Carr2f0fe622015-09-25 14:56:38 -0700933
934 mHasClipRect = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700935 if (selfTransformation || attachedTransformation != null
936 || appTransformation != null || screenAnimation) {
937 // cache often used attributes locally
938 final Rect frame = mWin.mFrame;
939 final float tmpFloats[] = mService.mTmpFloats;
940 final Matrix tmpMatrix = mWin.mTmpMatrix;
941
942 // Compute the desired transformation.
Dianne Hackborn4b169692012-11-29 17:51:24 -0800943 if (screenAnimation && screenRotationAnimation.isRotating()) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700944 // If we are doing a screen animation, the global rotation
945 // applied to windows can result in windows that are carefully
946 // aligned with each other to slightly separate, allowing you
947 // to see what is behind them. An unsightly mess. This...
948 // thing... magically makes it call good: scale each window
949 // slightly (two pixels larger in each dimension, from the
950 // window's center).
951 final float w = frame.width();
952 final float h = frame.height();
953 if (w>=1 && h>=1) {
954 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
955 } else {
956 tmpMatrix.reset();
957 }
958 } else {
959 tmpMatrix.reset();
960 }
961 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
962 if (selfTransformation) {
963 tmpMatrix.postConcat(mTransformation.getMatrix());
964 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700965 if (attachedTransformation != null) {
966 tmpMatrix.postConcat(attachedTransformation.getMatrix());
967 }
968 if (appTransformation != null) {
969 tmpMatrix.postConcat(appTransformation.getMatrix());
970 }
Matthew Ng34a06d12017-02-03 11:56:08 -0800971
972 // The translation that applies the position of the window needs to be applied at the
973 // end in case that other translations include scaling. Otherwise the scaling will
974 // affect this translation. But it needs to be set before the screen rotation animation
975 // so the pivot point is at the center of the screen for all windows.
976 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700977 if (screenAnimation) {
Craig Mautnera91f9e22012-09-14 16:22:08 -0700978 tmpMatrix.postConcat(screenRotationAnimation.getEnterTransformation().getMatrix());
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700979 }
Winson Chung399f6202014-03-19 10:47:20 -0700980
Robert Carref090ac2017-05-15 16:45:50 -0700981 MagnificationSpec spec = getMagnificationSpec();
982 if (spec != null) {
Robert Carr298f9272016-08-09 13:08:01 -0700983 applyMagnificationSpec(spec, tmpMatrix);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700984 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700985
986 // "convert" it into SurfaceFlinger's format
987 // (a 2x2 matrix + an offset)
988 // Here we must not transform the position of the surface
989 // since it is already included in the transformation.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800990 //Slog.i(TAG_WM, "Transform: " + matrix);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700991
992 mHaveMatrix = true;
993 tmpMatrix.getValues(tmpFloats);
994 mDsDx = tmpFloats[Matrix.MSCALE_X];
995 mDtDx = tmpFloats[Matrix.MSKEW_Y];
Robert Carr0edf18f2017-02-21 20:01:47 -0800996 mDtDy = tmpFloats[Matrix.MSKEW_X];
997 mDsDy = tmpFloats[Matrix.MSCALE_Y];
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700998 float x = tmpFloats[Matrix.MTRANS_X];
999 float y = tmpFloats[Matrix.MTRANS_Y];
Qiushi Han32fed962016-10-01 17:06:49 +08001000 mWin.mShownPosition.set(Math.round(x), Math.round(y));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001001
1002 // Now set the alpha... but because our current hardware
1003 // can't do alpha transformation on a non-opaque surface,
1004 // turn it off if we are running an animation that is also
1005 // transforming since it is more important to have that
1006 // animation be smooth.
1007 mShownAlpha = mAlpha;
1008 if (!mService.mLimitedAlphaCompositing
1009 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
Robert Carr0edf18f2017-02-21 20:01:47 -08001010 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDtDy, mDsDy)
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001011 && x == frame.left && y == frame.top))) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001012 //Slog.i(TAG_WM, "Applying alpha transform");
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001013 if (selfTransformation) {
1014 mShownAlpha *= mTransformation.getAlpha();
1015 }
1016 if (attachedTransformation != null) {
1017 mShownAlpha *= attachedTransformation.getAlpha();
1018 }
1019 if (appTransformation != null) {
1020 mShownAlpha *= appTransformation.getAlpha();
Winson Chung399f6202014-03-19 10:47:20 -07001021 if (appTransformation.hasClipRect()) {
1022 mClipRect.set(appTransformation.getClipRect());
1023 mHasClipRect = true;
Robert Carrf3b72c72016-03-21 18:16:39 -07001024 // The app transformation clip will be in the coordinate space of the main
1025 // activity window, which the animation correctly assumes will be placed at
1026 // (0,0)+(insets) relative to the containing frame. This isn't necessarily
1027 // true for child windows though which can have an arbitrary frame position
1028 // relative to their containing frame. We need to offset the difference
1029 // between the containing frame as used to calculate the crop and our
1030 // bounds to compensate for this.
Wale Ogunwale246c2092016-04-07 14:12:44 -07001031 if (mWin.layoutInParentFrame()) {
Robert Carrf3b72c72016-03-21 18:16:39 -07001032 mClipRect.offset( (mWin.mContainingFrame.left - mWin.mFrame.left),
1033 mWin.mContainingFrame.top - mWin.mFrame.top );
1034 }
Winson Chung399f6202014-03-19 10:47:20 -07001035 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001036 }
1037 if (screenAnimation) {
Craig Mautnera91f9e22012-09-14 16:22:08 -07001038 mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001039 }
1040 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001041 //Slog.i(TAG_WM, "Not applying alpha transform");
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001042 }
1043
Filip Gruszczynski19723a42015-11-25 15:01:48 -08001044 if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV)
Craig Mautnera91f9e22012-09-14 16:22:08 -07001045 && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v(
1046 TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha
1047 + " self=" + (selfTransformation ? mTransformation.getAlpha() : "null")
1048 + " attached=" + (attachedTransformation == null ?
1049 "null" : attachedTransformation.getAlpha())
1050 + " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha())
1051 + " screen=" + (screenAnimation ?
1052 screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001053 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001054 } else if (mIsWallpaper && mService.mRoot.mWallpaperActionPending) {
Craig Mautner4d7349b2012-04-20 14:52:47 -07001055 return;
Chong Zhang3005e752015-09-18 18:46:28 -07001056 } else if (mWin.isDragResizeChanged()) {
1057 // This window is awaiting a relayout because user just started (or ended)
1058 // drag-resizing. The shown frame (which affects surface size and pos)
1059 // should not be updated until we get next finished draw with the new surface.
1060 // Otherwise one or two frames rendered with old settings would be displayed
1061 // with new geometry.
1062 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001063 }
1064
Filip Gruszczynski19723a42015-11-25 15:01:48 -08001065 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautner4d7349b2012-04-20 14:52:47 -07001066 TAG, "computeShownFrameLocked: " + this +
1067 " not attached, mAlpha=" + mAlpha);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001068
Robert Carref090ac2017-05-15 16:45:50 -07001069 MagnificationSpec spec = getMagnificationSpec();
Craig Mautner165be0c2015-01-27 15:16:58 -08001070 if (spec != null) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001071 final Rect frame = mWin.mFrame;
1072 final float tmpFloats[] = mService.mTmpFloats;
1073 final Matrix tmpMatrix = mWin.mTmpMatrix;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001074
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001075 tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
1076 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001077
Robert Carr298f9272016-08-09 13:08:01 -07001078 applyMagnificationSpec(spec, tmpMatrix);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001079
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001080 tmpMatrix.getValues(tmpFloats);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001081
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001082 mHaveMatrix = true;
1083 mDsDx = tmpFloats[Matrix.MSCALE_X];
1084 mDtDx = tmpFloats[Matrix.MSKEW_Y];
Robert Carr0edf18f2017-02-21 20:01:47 -08001085 mDtDy = tmpFloats[Matrix.MSKEW_X];
1086 mDsDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001087 float x = tmpFloats[Matrix.MTRANS_X];
1088 float y = tmpFloats[Matrix.MTRANS_Y];
Qiushi Han32fed962016-10-01 17:06:49 +08001089 mWin.mShownPosition.set(Math.round(x), Math.round(y));
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001090
1091 mShownAlpha = mAlpha;
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001092 } else {
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001093 mWin.mShownPosition.set(mWin.mFrame.left, mWin.mFrame.top);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001094 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001095 mWin.mShownPosition.offset(mWin.mXOffset, mWin.mYOffset);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001096 }
1097 mShownAlpha = mAlpha;
1098 mHaveMatrix = false;
1099 mDsDx = mWin.mGlobalScale;
1100 mDtDx = 0;
Robert Carr0edf18f2017-02-21 20:01:47 -08001101 mDtDy = 0;
1102 mDsDy = mWin.mGlobalScale;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001103 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001104 }
1105
Robert Carref090ac2017-05-15 16:45:50 -07001106 private MagnificationSpec getMagnificationSpec() {
1107 //TODO (multidisplay): Magnification is supported only for the default display.
1108 if (mService.mAccessibilityController != null && mWin.getDisplayId() == DEFAULT_DISPLAY) {
1109 return mService.mAccessibilityController.getMagnificationSpecForWindowLocked(mWin);
1110 }
1111 return null;
1112 }
1113
Robert Carrfbbde852016-10-18 11:02:28 -07001114 /**
1115 * In some scenarios we use a screen space clip rect (so called, final clip rect)
1116 * to crop to stack bounds. Generally because it's easier to deal with while
1117 * animating.
1118 *
1119 * @return True in scenarios where we use the final clip rect for stack clipping.
1120 */
1121 private boolean useFinalClipRect() {
1122 return (isAnimationSet() && resolveStackClip() == STACK_CLIP_AFTER_ANIM)
1123 || mDestroyPreservedSurfaceUponRedraw || mWin.inPinnedWorkspace();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001124 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001125
Robert Carrfbbde852016-10-18 11:02:28 -07001126 /**
1127 * Calculate the screen-space crop rect and fill finalClipRect.
1128 * @return true if finalClipRect has been filled, otherwise,
1129 * no screen space crop should be applied.
1130 */
1131 private boolean calculateFinalCrop(Rect finalClipRect) {
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001132 final WindowState w = mWin;
Craig Mautnerdf88d732014-01-27 09:21:32 -08001133 final DisplayContent displayContent = w.getDisplayContent();
Robert Carrfbbde852016-10-18 11:02:28 -07001134 finalClipRect.setEmpty();
1135
Craig Mautnerdf88d732014-01-27 09:21:32 -08001136 if (displayContent == null) {
Robert Carrfbbde852016-10-18 11:02:28 -07001137 return false;
Craig Mautnerdf88d732014-01-27 09:21:32 -08001138 }
Robert Carrfbbde852016-10-18 11:02:28 -07001139
1140 if (!shouldCropToStackBounds() || !useFinalClipRect()) {
1141 return false;
1142 }
1143
1144 // Task is non-null per shouldCropToStackBounds
1145 final TaskStack stack = w.getTask().mStack;
1146 stack.getDimBounds(finalClipRect);
Robert Carr002c8e12017-04-27 15:32:22 -07001147
1148 if (StackId.tasksAreFloating(stack.mStackId)) {
1149 w.expandForSurfaceInsets(finalClipRect);
1150 }
Robert Carref090ac2017-05-15 16:45:50 -07001151
1152 // We may be applying a magnification spec to all windows,
1153 // simulating a transformation in screen space, in which case
1154 // we need to transform all other screen space values...including
1155 // the final crop. This is kind of messed up and we should look
1156 // in to actually transforming screen-space via a parent-layer.
1157 // b/38322835
1158 MagnificationSpec spec = getMagnificationSpec();
1159 if (spec != null && !spec.isNop()) {
1160 Matrix transform = mWin.mTmpMatrix;
1161 RectF finalCrop = mService.mTmpRectF;
1162 transform.reset();
1163 transform.postScale(spec.scale, spec.scale);
1164 transform.postTranslate(-spec.offsetX, -spec.offsetY);
1165 transform.mapRect(finalCrop);
1166 finalClipRect.top = (int)finalCrop.top;
1167 finalClipRect.left = (int)finalCrop.left;
1168 finalClipRect.right = (int)finalClipRect.right;
1169 finalClipRect.bottom = (int)finalClipRect.bottom;
1170 }
1171
Robert Carrfbbde852016-10-18 11:02:28 -07001172 return true;
1173 }
1174
1175 /**
1176 * Calculate the window-space crop rect and fill clipRect.
Wale Ogunwaleca9e0612016-12-02 07:45:59 -08001177 * @return true if clipRect has been filled otherwise, no window space crop should be applied.
Robert Carrfbbde852016-10-18 11:02:28 -07001178 */
Wale Ogunwaleca9e0612016-12-02 07:45:59 -08001179 private boolean calculateCrop(Rect clipRect) {
Robert Carrfbbde852016-10-18 11:02:28 -07001180 final WindowState w = mWin;
1181 final DisplayContent displayContent = w.getDisplayContent();
1182 clipRect.setEmpty();
1183
1184 if (displayContent == null) {
1185 return false;
1186 }
1187
1188 if (w.inPinnedWorkspace()) {
1189 return false;
1190 }
1191
1192 // If we're animating, the wallpaper should only
1193 // be updated at the end of the animation.
1194 if (w.mAttrs.type == TYPE_WALLPAPER) {
1195 return false;
1196 }
1197
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001198 if (DEBUG_WINDOW_CROP) Slog.d(TAG,
1199 "Updating crop win=" + w + " mLastCrop=" + mLastClipRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001200
Robert Carrfbbde852016-10-18 11:02:28 -07001201 w.calculatePolicyCrop(mSystemDecorRect);
1202
1203 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Applying decor to crop win=" + w + " mDecorFrame="
1204 + w.mDecorFrame + " mSystemDecorRect=" + mSystemDecorRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001205
Andrii Kulian283acd22017-08-03 04:03:51 -07001206 final Task task = w.getTask();
1207 final boolean fullscreen = w.fillsDisplay() || (task != null && task.isFullscreen());
Jorim Jaggi253a20f2015-11-03 12:38:42 +01001208 final boolean isFreeformResizing =
Jorim Jaggidcf467c2015-11-05 13:59:32 +01001209 w.isDragResizing() && w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM;
Robert Carr51a1b872015-12-08 14:03:13 -08001210
1211 // We use the clip rect as provided by the tranformation for non-fullscreen windows to
1212 // avoid premature clipping with the system decor rect.
1213 clipRect.set((mHasClipRect && !fullscreen) ? mClipRect : mSystemDecorRect);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001214 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "win=" + w + " Initial clip rect: " + clipRect
1215 + " mHasClipRect=" + mHasClipRect + " fullscreen=" + fullscreen);
Robert Carr51a1b872015-12-08 14:03:13 -08001216
1217 if (isFreeformResizing && !w.isChildWindow()) {
1218 // For freeform resizing non child windows, we are using the big surface positioned
1219 // at 0,0. Thus we must express the crop in that coordinate space.
1220 clipRect.offset(w.mShownPosition.x, w.mShownPosition.y);
Chong Zhang09b21ef2015-09-14 10:20:21 -07001221 }
Robert Carr51a1b872015-12-08 14:03:13 -08001222
Robert Carrfbbde852016-10-18 11:02:28 -07001223 w.expandForSurfaceInsets(clipRect);
Alan Viverette49a22e82014-07-12 20:01:27 -07001224
Wale Ogunwaleb3eba812015-07-30 20:20:16 -07001225 if (mHasClipRect && fullscreen) {
1226 // We intersect the clip rect specified by the transformation with the expanded system
1227 // decor rect to prevent artifacts from drawing during animation if the transformation
1228 // clip rect extends outside the system decor rect.
1229 clipRect.intersect(mClipRect);
1230 }
Alan Viverette49a22e82014-07-12 20:01:27 -07001231 // The clip rect was generated assuming (0,0) as the window origin,
1232 // so we need to translate to match the actual surface coordinates.
Robert Carrfbbde852016-10-18 11:02:28 -07001233 clipRect.offset(w.mAttrs.surfaceInsets.left, w.mAttrs.surfaceInsets.top);
Robert Carr58f29132015-10-29 14:19:05 -07001234
Robert Carrfbbde852016-10-18 11:02:28 -07001235 if (!useFinalClipRect()) {
1236 adjustCropToStackBounds(clipRect, isFreeformResizing);
1237 }
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001238 if (DEBUG_WINDOW_CROP) Slog.d(TAG,
1239 "win=" + w + " Clip rect after stack adjustment=" + clipRect);
Robert Carr58f29132015-10-29 14:19:05 -07001240
Chia-I Wue6bcaf12016-05-27 10:58:48 +08001241 w.transformClipRectFromScreenToSurfaceSpace(clipRect);
Robert Carr58f29132015-10-29 14:19:05 -07001242
Robert Carrfbbde852016-10-18 11:02:28 -07001243 return true;
Robert Carr0d00c2e2016-02-29 17:45:02 -08001244 }
1245
Robert Carrfbbde852016-10-18 11:02:28 -07001246 private void applyCrop(Rect clipRect, Rect finalClipRect, boolean recoveringMemory) {
1247 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "applyCrop: win=" + mWin
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001248 + " clipRect=" + clipRect + " finalClipRect=" + finalClipRect);
Robert Carr4320d332016-06-10 15:13:32 -07001249 if (clipRect != null) {
1250 if (!clipRect.equals(mLastClipRect)) {
1251 mLastClipRect.set(clipRect);
1252 mSurfaceController.setCropInTransaction(clipRect, recoveringMemory);
1253 }
1254 } else {
1255 mSurfaceController.clearCropInTransaction(recoveringMemory);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001256 }
Robert Carrfbbde852016-10-18 11:02:28 -07001257
1258 if (finalClipRect == null) {
1259 finalClipRect = mService.mTmpRect;
1260 finalClipRect.setEmpty();
1261 }
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001262 if (!finalClipRect.equals(mLastFinalClipRect)) {
1263 mLastFinalClipRect.set(finalClipRect);
1264 mSurfaceController.setFinalCropInTransaction(finalClipRect);
Chong Zhangcc3eccf2016-05-16 18:25:45 -07001265 if (mDestroyPreservedSurfaceUponRedraw && mPendingDestroySurface != null) {
1266 mPendingDestroySurface.setFinalCropInTransaction(finalClipRect);
1267 }
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001268 }
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001269 }
1270
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001271 private int resolveStackClip() {
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001272 // App animation overrides window animation stack clip mode.
1273 if (mAppAnimator != null && mAppAnimator.animation != null) {
1274 return mAppAnimator.getStackClip();
1275 } else {
1276 return mStackClip;
1277 }
1278 }
Wale Ogunwale4c5aa5172016-04-19 11:29:05 -07001279
Robert Carrfbbde852016-10-18 11:02:28 -07001280 private boolean shouldCropToStackBounds() {
1281 final WindowState w = mWin;
Wale Ogunwale4c5aa5172016-04-19 11:29:05 -07001282 final DisplayContent displayContent = w.getDisplayContent();
1283 if (displayContent != null && !displayContent.isDefaultDisplay) {
1284 // There are some windows that live on other displays while their app and main window
1285 // live on the default display (e.g. casting...). We don't want to crop this windows
1286 // to the stack bounds which is only currently supported on the default display.
1287 // TODO(multi-display): Need to support cropping to stack bounds on other displays
1288 // when we have stacks on other displays.
Robert Carrfbbde852016-10-18 11:02:28 -07001289 return false;
Wale Ogunwale4c5aa5172016-04-19 11:29:05 -07001290 }
1291
Chong Zhang112eb8c2015-11-02 11:17:00 -08001292 final Task task = w.getTask();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001293 if (task == null || !task.cropWindowsToStackBounds()) {
Robert Carrfbbde852016-10-18 11:02:28 -07001294 return false;
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -07001295 }
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001296
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001297 final int stackClip = resolveStackClip();
1298
1299 // It's animating and we don't want to clip it to stack bounds during animation - abort.
Jorim Jaggi5c80c412016-04-19 20:03:47 -07001300 if (isAnimationSet() && stackClip == STACK_CLIP_NONE) {
Robert Carrfbbde852016-10-18 11:02:28 -07001301 return false;
1302 }
1303 return true;
1304 }
1305
1306 private void adjustCropToStackBounds(Rect clipRect,
1307 boolean isFreeformResizing) {
1308 final WindowState w = mWin;
1309
1310 if (!shouldCropToStackBounds()) {
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001311 return;
1312 }
1313
Robert Carrfbbde852016-10-18 11:02:28 -07001314 final TaskStack stack = w.getTask().mStack;
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001315 stack.getDimBounds(mTmpStackBounds);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001316 final Rect surfaceInsets = w.getAttrs().surfaceInsets;
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001317 // When we resize we use the big surface approach, which means we can't trust the
1318 // window frame bounds anymore. Instead, the window will be placed at 0, 0, but to avoid
1319 // hardcoding it, we use surface coordinates.
1320 final int frameX = isFreeformResizing ? (int) mSurfaceController.getX() :
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001321 w.mFrame.left + mWin.mXOffset - surfaceInsets.left;
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001322 final int frameY = isFreeformResizing ? (int) mSurfaceController.getY() :
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001323 w.mFrame.top + mWin.mYOffset - surfaceInsets.top;
Robert Carr0d00c2e2016-02-29 17:45:02 -08001324
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001325 // We need to do some acrobatics with surface position, because their clip region is
1326 // relative to the inside of the surface, but the stack bounds aren't.
Robert Carrfbbde852016-10-18 11:02:28 -07001327 if (StackId.hasWindowShadow(stack.mStackId)
1328 && !StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001329 // The windows in this stack display drop shadows and the fill the entire stack
1330 // area. Adjust the stack bounds we will use to cropping take into account the
1331 // offsets we use to display the drop shadow so it doesn't get cropped.
1332 mTmpStackBounds.inset(-surfaceInsets.left, -surfaceInsets.top,
1333 -surfaceInsets.right, -surfaceInsets.bottom);
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001334 }
Robert Carrfbbde852016-10-18 11:02:28 -07001335
1336 clipRect.left = Math.max(0,
1337 Math.max(mTmpStackBounds.left, frameX + clipRect.left) - frameX);
1338 clipRect.top = Math.max(0,
1339 Math.max(mTmpStackBounds.top, frameY + clipRect.top) - frameY);
1340 clipRect.right = Math.max(0,
1341 Math.min(mTmpStackBounds.right, frameX + clipRect.right) - frameX);
1342 clipRect.bottom = Math.max(0,
1343 Math.min(mTmpStackBounds.bottom, frameY + clipRect.bottom) - frameY);
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -07001344 }
1345
Wale Ogunwale4c8b7952015-04-07 10:49:40 -07001346 void setSurfaceBoundariesLocked(final boolean recoveringMemory) {
Wale Ogunwale027f4752017-05-12 10:37:16 -07001347 if (mSurfaceController == null) {
1348 return;
1349 }
1350
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001351 final WindowState w = mWin;
Winson Chung08f81892017-03-02 15:40:51 -08001352 final LayoutParams attrs = mWin.getAttrs();
Robert Carr0d00c2e2016-02-29 17:45:02 -08001353 final Task task = w.getTask();
Alan Viveretteccb11e12014-07-08 16:04:02 -07001354
Jorim Jaggif3df0aa2016-04-06 15:56:33 -07001355 // We got resized, so block all updates until we got the new surface.
Andrii Kulianeb1d3222016-05-16 15:17:55 -07001356 if (w.isResizedWhileNotDragResizing() && !w.isGoneForLayoutLw()) {
Jorim Jaggif3df0aa2016-04-06 15:56:33 -07001357 return;
1358 }
1359
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001360 mTmpSize.set(w.mShownPosition.x, w.mShownPosition.y, 0, 0);
Winson Chung08f81892017-03-02 15:40:51 -08001361 calculateSurfaceBounds(w, attrs);
Chong Zhang0275e392015-09-17 10:41:44 -07001362
Robert Carr04092112016-06-02 12:56:12 -07001363 mExtraHScale = (float) 1.0;
1364 mExtraVScale = (float) 1.0;
Robert Carre1034cc32016-02-01 13:08:15 -08001365
Robert Carr6da3cc02016-06-16 15:17:07 -07001366 boolean wasForceScaled = mForceScaleUntilResize;
1367 boolean wasSeamlesslyRotated = w.mSeamlesslyRotated;
1368
Robert Carrfed10072016-05-26 11:48:49 -07001369 // Once relayout has been called at least once, we need to make sure
1370 // we only resize the client surface during calls to relayout. For
1371 // clients which use indeterminate measure specs (MATCH_PARENT),
1372 // we may try and change their window size without a call to relayout.
1373 // However, this would be unsafe, as the client may be in the middle
1374 // of producing a frame at the old size, having just completed layout
1375 // to find the surface size changed underneath it.
Robert Carr3ccc5272016-06-20 22:06:32 -07001376 if (!w.mRelayoutCalled || w.mInRelayout) {
Robert Carrfed10072016-05-26 11:48:49 -07001377 mSurfaceResized = mSurfaceController.setSizeInTransaction(
1378 mTmpSize.width(), mTmpSize.height(), recoveringMemory);
1379 } else {
1380 mSurfaceResized = false;
1381 }
Robert Carrc7294602016-05-13 11:32:05 -07001382 mForceScaleUntilResize = mForceScaleUntilResize && !mSurfaceResized;
Robert Carr6da3cc02016-06-16 15:17:07 -07001383 // If we are undergoing seamless rotation, the surface has already
1384 // been set up to persist at it's old location. We need to freeze
1385 // updates until a resize occurs.
Robert Carrb14d4abc2016-10-03 18:13:33 -07001386 mService.markForSeamlessRotation(w, w.mSeamlesslyRotated && !mSurfaceResized);
Robert Carrc7294602016-05-13 11:32:05 -07001387
Robert Carrfbbde852016-10-18 11:02:28 -07001388 Rect clipRect = null, finalClipRect = null;
1389 if (calculateCrop(mTmpClipRect)) {
1390 clipRect = mTmpClipRect;
1391 }
1392 if (calculateFinalCrop(mTmpFinalClipRect)) {
1393 finalClipRect = mTmpFinalClipRect;
1394 }
Robert Carra9408d42016-06-03 13:28:48 -07001395
1396 float surfaceWidth = mSurfaceController.getWidth();
1397 float surfaceHeight = mSurfaceController.getHeight();
1398
Robert Carr8f0a3ad2017-02-15 19:30:28 -08001399 if (isForceScaled()) {
Winson Chung08f81892017-03-02 15:40:51 -08001400 int hInsets = attrs.surfaceInsets.left + attrs.surfaceInsets.right;
1401 int vInsets = attrs.surfaceInsets.top + attrs.surfaceInsets.bottom;
1402 float surfaceContentWidth = surfaceWidth - hInsets;
1403 float surfaceContentHeight = surfaceHeight - vInsets;
Robert Carra9408d42016-06-03 13:28:48 -07001404 if (!mForceScaleUntilResize) {
1405 mSurfaceController.forceScaleableInTransaction(true);
1406 }
Robert Carrfd4c9892017-02-01 10:28:28 -08001407
Winson Chung08f81892017-03-02 15:40:51 -08001408 int posX = mTmpSize.left;
1409 int posY = mTmpSize.top;
Robert Carrfd4c9892017-02-01 10:28:28 -08001410 task.mStack.getDimBounds(mTmpStackBounds);
Robert Carr18f622f2017-05-08 11:20:43 -07001411
1412 boolean allowStretching = false;
Winson Chung8bca9e42017-04-16 15:59:43 -07001413 task.mStack.getFinalAnimationSourceHintBounds(mTmpSourceBounds);
Robert Carr18f622f2017-05-08 11:20:43 -07001414 // If we don't have source bounds, we can attempt to use the content insets
1415 // in the following scenario:
1416 // 1. We have content insets.
1417 // 2. We are not transitioning to full screen
1418 // We have to be careful to check "lastAnimatingBoundsWasToFullscreen" rather than
1419 // the mBoundsAnimating state, as we may have already left it and only be here
1420 // because of the force-scale until resize state.
1421 if (mTmpSourceBounds.isEmpty() && (mWin.mLastRelayoutContentInsets.width() > 0
1422 || mWin.mLastRelayoutContentInsets.height() > 0)
1423 && !task.mStack.lastAnimatingBoundsWasToFullscreen()) {
1424 mTmpSourceBounds.set(task.mStack.mPreAnimationBounds);
1425 mTmpSourceBounds.inset(mWin.mLastRelayoutContentInsets);
1426 allowStretching = true;
1427 }
Winson Chung08f81892017-03-02 15:40:51 -08001428 if (!mTmpSourceBounds.isEmpty()) {
1429 // Get the final target stack bounds, if we are not animating, this is just the
1430 // current stack bounds
Winson Chung40a5f932017-04-13 16:39:36 -07001431 task.mStack.getFinalAnimationBounds(mTmpAnimatingBounds);
Winson Chung08f81892017-03-02 15:40:51 -08001432
1433 // Calculate the current progress and interpolate the difference between the target
1434 // and source bounds
1435 float finalWidth = mTmpAnimatingBounds.width();
1436 float initialWidth = mTmpSourceBounds.width();
Robert Carr18f622f2017-05-08 11:20:43 -07001437 float tw = (surfaceContentWidth - mTmpStackBounds.width())
Winson Chung08f81892017-03-02 15:40:51 -08001438 / (surfaceContentWidth - mTmpAnimatingBounds.width());
Robert Carr18f622f2017-05-08 11:20:43 -07001439 float th = tw;
1440 mExtraHScale = (initialWidth + tw * (finalWidth - initialWidth)) / initialWidth;
1441 if (allowStretching) {
1442 float finalHeight = mTmpAnimatingBounds.height();
1443 float initialHeight = mTmpSourceBounds.height();
1444 th = (surfaceContentHeight - mTmpStackBounds.height())
1445 / (surfaceContentHeight - mTmpAnimatingBounds.height());
1446 mExtraVScale = (initialHeight + tw * (finalHeight - initialHeight))
1447 / initialHeight;
1448 } else {
1449 mExtraVScale = mExtraHScale;
1450 }
Winson Chung08f81892017-03-02 15:40:51 -08001451
1452 // Adjust the position to account for the inset bounds
Robert Carr18f622f2017-05-08 11:20:43 -07001453 posX -= (int) (tw * mExtraHScale * mTmpSourceBounds.left);
1454 posY -= (int) (th * mExtraVScale * mTmpSourceBounds.top);
Winson Chung08f81892017-03-02 15:40:51 -08001455
1456 // Always clip to the stack bounds since the surface can be larger with the current
1457 // scale
1458 clipRect = null;
1459 finalClipRect = mTmpStackBounds;
1460 } else {
1461 // We want to calculate the scaling based on the content area, not based on
1462 // the entire surface, so that we scale in sync with windows that don't have insets.
1463 mExtraHScale = mTmpStackBounds.width() / surfaceContentWidth;
1464 mExtraVScale = mTmpStackBounds.height() / surfaceContentHeight;
1465
1466 // Since we are scaled to fit in our previously desired crop, we can now
1467 // expose the whole window in buffer space, and not risk extending
1468 // past where the system would have cropped us
1469 clipRect = null;
1470 finalClipRect = null;
1471 }
Robert Carr0d00c2e2016-02-29 17:45:02 -08001472
Robert Carrfd4c9892017-02-01 10:28:28 -08001473 // In the case of ForceScaleToStack we scale entire tasks together,
Robert Carr0d00c2e2016-02-29 17:45:02 -08001474 // and so we need to scale our offsets relative to the task bounds
1475 // or parent and child windows would fall out of alignment.
Winson Chung08f81892017-03-02 15:40:51 -08001476 posX -= (int) (attrs.x * (1 - mExtraHScale));
1477 posY -= (int) (attrs.y * (1 - mExtraVScale));
1478
Robert Carrbc133762016-05-12 14:04:38 -07001479 // Imagine we are scaling down. As we scale the buffer down, we decrease the
1480 // distance between the surface top left, and the start of the surface contents
1481 // (previously it was surfaceInsets.left pixels in screen space but now it
Robert Carr04092112016-06-02 12:56:12 -07001482 // will be surfaceInsets.left*mExtraHScale). This means in order to keep the
Robert Carrbc133762016-05-12 14:04:38 -07001483 // non inset content at the same position, we have to shift the whole window
1484 // forward. Likewise for scaling up, we've increased this distance, and we need
1485 // to shift by a negative number to compensate.
Winson Chung08f81892017-03-02 15:40:51 -08001486 posX += attrs.surfaceInsets.left * (1 - mExtraHScale);
1487 posY += attrs.surfaceInsets.top * (1 - mExtraVScale);
Robert Carrbc133762016-05-12 14:04:38 -07001488
Winson Chung08f81892017-03-02 15:40:51 -08001489 mSurfaceController.setPositionInTransaction((float) Math.floor(posX),
1490 (float) Math.floor(posY), recoveringMemory);
Robert Carrc7294602016-05-13 11:32:05 -07001491
1492 // Various surfaces in the scaled stack may resize at different times.
1493 // We need to ensure for each surface, that we disable transformation matrix
1494 // scaling in the same transaction which we resize the surface in.
1495 // As we are in SCALING_MODE_SCALE_TO_WINDOW, SurfaceFlinger will
Robert Carra9408d42016-06-03 13:28:48 -07001496 // then take over the scaling until the new buffer arrives, and things
Robert Carrc7294602016-05-13 11:32:05 -07001497 // will be seamless.
1498 mForceScaleUntilResize = true;
Robert Carr0d00c2e2016-02-29 17:45:02 -08001499 } else {
Robert Carr6da3cc02016-06-16 15:17:07 -07001500 if (!w.mSeamlesslyRotated) {
1501 mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top,
1502 recoveringMemory);
1503 }
Robert Carr0d00c2e2016-02-29 17:45:02 -08001504 }
1505
Robert Carra9408d42016-06-03 13:28:48 -07001506 // If we are ending the scaling mode. We switch to SCALING_MODE_FREEZE
Robert Carr6da3cc02016-06-16 15:17:07 -07001507 // to prevent further updates until buffer latch.
1508 // When ending both force scaling, and seamless rotation, we need to freeze
1509 // the Surface geometry until a buffer comes in at the new size (normally position and crop
1510 // are unfrozen). setGeometryAppliesWithResizeInTransaction accomplishes this for us.
1511 if ((wasForceScaled && !mForceScaleUntilResize) ||
1512 (wasSeamlesslyRotated && !w.mSeamlesslyRotated)) {
1513 mSurfaceController.setGeometryAppliesWithResizeInTransaction(true);
Robert Carra9408d42016-06-03 13:28:48 -07001514 mSurfaceController.forceScaleableInTransaction(false);
1515 }
Robert Carr4320d332016-06-10 15:13:32 -07001516
Robert Carr6da3cc02016-06-16 15:17:07 -07001517 if (!w.mSeamlesslyRotated) {
Robert Carrfbbde852016-10-18 11:02:28 -07001518 applyCrop(clipRect, finalClipRect, recoveringMemory);
Robert Carr6da3cc02016-06-16 15:17:07 -07001519 mSurfaceController.setMatrixInTransaction(mDsDx * w.mHScale * mExtraHScale,
1520 mDtDx * w.mVScale * mExtraVScale,
Robert Carr0edf18f2017-02-21 20:01:47 -08001521 mDtDy * w.mHScale * mExtraHScale,
1522 mDsDy * w.mVScale * mExtraVScale, recoveringMemory);
Robert Carr6da3cc02016-06-16 15:17:07 -07001523 }
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001524
Robert Carre6a83512015-11-03 16:09:21 -08001525 if (mSurfaceResized) {
Chong Zhang5b2f1992015-11-13 15:40:36 -08001526 mReportSurfaceResized = true;
Robert Carre6a83512015-11-03 16:09:21 -08001527 mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1528 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
1529 w.applyDimLayerIfNeeded();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001530 }
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001531 }
1532
Andrii Kulian283acd22017-08-03 04:03:51 -07001533 /**
1534 * Get rect of the task this window is currently in. If there is no task, rect will be set to
1535 * empty.
1536 */
1537 void getContainerRect(Rect rect) {
1538 final Task task = mWin.getTask();
1539 if (task != null) {
1540 task.getDimBounds(rect);
1541 } else {
1542 rect.left = rect.top = rect.right = rect.bottom = 0;
1543 }
1544 }
1545
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001546 void prepareSurfaceLocked(final boolean recoveringMemory) {
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001547 final WindowState w = mWin;
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001548 if (!hasSurface()) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001549
1550 // There is no need to wait for an animation change if our window is gone for layout
1551 // already as we'll never be visible.
Bryce Lee8c3cf382017-07-06 19:47:10 -07001552 if (w.getOrientationChanging() && w.isGoneForLayoutLw()) {
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001553 if (DEBUG_ORIENTATION) {
1554 Slog.v(TAG, "Orientation change skips hidden " + w);
1555 }
Bryce Lee8c3cf382017-07-06 19:47:10 -07001556 w.setOrientationChanging(false);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001557 }
1558 return;
1559 }
1560
Chong Zhang70211742016-04-28 18:27:53 -07001561 // Do not change surface properties of opening apps if we are waiting for the
1562 // transition to be ready. transitionGoodToGo could be not ready even after all
1563 // opening apps are drawn. It's only waiting on isFetchingAppTransitionsSpecs()
1564 // to get the animation spec. (For example, go into Recents and immediately open
1565 // the same app again before the app's surface is destroyed or saved, the surface
1566 // is always ready in the whole process.) If we go ahead here, the opening app
1567 // will be shown with the full size before the correct animation spec arrives.
Chong Zhange22006d2016-05-09 10:59:59 -07001568 if (isWaitingForOpening()) {
Chong Zhang70211742016-04-28 18:27:53 -07001569 return;
1570 }
1571
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001572 boolean displayed = false;
1573
1574 computeShownFrameLocked();
1575
Craig Mautnera91f9e22012-09-14 16:22:08 -07001576 setSurfaceBoundariesLocked(recoveringMemory);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001577
Craig Mautner918b53b2012-07-09 14:15:54 -07001578 if (mIsWallpaper && !mWin.mWallpaperVisible) {
Craig Mautner0fa77c12012-06-11 15:57:19 -07001579 // Wallpaper is no longer visible and there is no wp target => hide it.
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001580 hide("prepareSurfaceLocked");
Wale Ogunwale9d147902016-07-16 11:58:55 -07001581 } else if (w.isParentWindowHidden() || !w.isOnScreen()) {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001582 hide("prepareSurfaceLocked");
Wale Ogunwalee8069dc2015-08-18 09:52:01 -07001583 mWallpaperControllerLocked.hideWallpapers(w);
Craig Mautnerb9836b92012-06-11 11:40:09 -07001584
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001585 // If we are waiting for this window to handle an orientation change. If this window is
1586 // really hidden (gone for layout), there is no point in still waiting for it.
1587 // Note that this does introduce a potential glitch if the window becomes unhidden
1588 // before it has drawn for the new orientation.
Bryce Lee8c3cf382017-07-06 19:47:10 -07001589 if (w.getOrientationChanging() && w.isGoneForLayoutLw()) {
1590 w.setOrientationChanging(false);
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001591 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001592 "Orientation change skips hidden " + w);
1593 }
1594 } else if (mLastLayer != mAnimLayer
1595 || mLastAlpha != mShownAlpha
1596 || mLastDsDx != mDsDx
1597 || mLastDtDx != mDtDx
1598 || mLastDsDy != mDsDy
1599 || mLastDtDy != mDtDy
1600 || w.mLastHScale != w.mHScale
1601 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -07001602 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001603 displayed = true;
1604 mLastAlpha = mShownAlpha;
1605 mLastLayer = mAnimLayer;
1606 mLastDsDx = mDsDx;
1607 mLastDtDx = mDtDx;
1608 mLastDsDy = mDsDy;
1609 mLastDtDy = mDtDy;
1610 w.mLastHScale = w.mHScale;
1611 w.mLastVScale = w.mVScale;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001612 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
Robert Carre6a83512015-11-03 16:09:21 -08001613 "controller=" + mSurfaceController +
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001614 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
Craig Mautner78505d82014-09-02 14:36:31 -07001615 + " matrix=[" + mDsDx + "*" + w.mHScale
1616 + "," + mDtDx + "*" + w.mVScale
Robert Carr0edf18f2017-02-21 20:01:47 -08001617 + "][" + mDtDy + "*" + w.mHScale
1618 + "," + mDsDy + "*" + w.mVScale + "]", false);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001619
Robert Carre6a83512015-11-03 16:09:21 -08001620 boolean prepared =
Robert Carraf422a82017-04-10 18:34:33 -07001621 mSurfaceController.prepareToShowInTransaction(mShownAlpha,
Robert Carr04092112016-06-02 12:56:12 -07001622 mDsDx * w.mHScale * mExtraHScale,
1623 mDtDx * w.mVScale * mExtraVScale,
Robert Carr0edf18f2017-02-21 20:01:47 -08001624 mDtDy * w.mHScale * mExtraHScale,
1625 mDsDy * w.mVScale * mExtraVScale,
Robert Carre6a83512015-11-03 16:09:21 -08001626 recoveringMemory);
Robert Carraf422a82017-04-10 18:34:33 -07001627 mSurfaceController.setLayer(mAnimLayer);
Robert Carre6a83512015-11-03 16:09:21 -08001628
Robert Carr03206af2017-07-10 18:24:21 -07001629 if (prepared && mDrawState == HAS_DRAWN) {
1630 if (mLastHidden) {
1631 if (showSurfaceRobustlyLocked()) {
1632 markPreservedSurfaceForDestroy();
1633 mAnimator.requestRemovalOfReplacedWindows(w);
1634 mLastHidden = false;
1635 if (mIsWallpaper) {
1636 w.dispatchWallpaperVisibility(true);
1637 }
1638 // This draw means the difference between unique content and mirroring.
1639 // Run another pass through performLayout to set mHasContent in the
1640 // LogicalDisplay.
1641 mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1642 WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
1643 } else {
1644 w.setOrientationChanging(false);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001645 }
Robert Carr03206af2017-07-10 18:24:21 -07001646 }
1647 // We process mTurnOnScreen even for windows which have already
1648 // been shown, to handle cases where windows are not necessarily
1649 // hidden while the screen is turning off.
1650 // TODO(b/63773439): These cases should be eliminated, though we probably still
1651 // want to process mTurnOnScreen in this way for clarity.
chaviwd3bf08d2017-08-01 17:24:59 -07001652 if (mWin.mTurnOnScreen && mWin.mAppToken.canTurnScreenOn()) {
Robert Carr03206af2017-07-10 18:24:21 -07001653 if (DEBUG_VISIBILITY) Slog.v(TAG, "Show surface turning screen on: " + mWin);
1654 mWin.mTurnOnScreen = false;
chaviwd3bf08d2017-08-01 17:24:59 -07001655
1656 // The window should only turn the screen on once per resume, but
1657 // prepareSurfaceLocked can be called multiple times. Set canTurnScreenOn to
1658 // false so the window doesn't turn the screen on again during this resume.
1659 mWin.mAppToken.setCanTurnScreenOn(false);
Robert Carr03206af2017-07-10 18:24:21 -07001660 mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001661 }
1662 }
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001663 if (hasSurface()) {
Robert Carre6a83512015-11-03 16:09:21 -08001664 w.mToken.hasVisible = true;
1665 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001666 } else {
Jorim Jaggi5c80c412016-04-19 20:03:47 -07001667 if (DEBUG_ANIM && isAnimationSet()) {
Chong Zhange05db742016-02-16 16:58:37 -08001668 Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
Craig Mautner83339b42012-05-01 22:13:23 -07001669 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001670 displayed = true;
1671 }
1672
Bryce Lee8c3cf382017-07-06 19:47:10 -07001673 if (w.getOrientationChanging()) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001674 if (!w.isDrawnLw()) {
1675 mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE;
1676 mAnimator.mLastWindowFreezeSource = w;
1677 if (DEBUG_ORIENTATION) Slog.v(TAG,
1678 "Orientation continue waiting for draw in " + w);
1679 } else {
Bryce Lee8c3cf382017-07-06 19:47:10 -07001680 w.setOrientationChanging(false);
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001681 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001682 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -07001683 }
1684
1685 if (displayed) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001686 w.mToken.hasVisible = true;
1687 }
1688 }
1689
Craig Mautneref655012013-01-03 11:20:24 -08001690 void setTransparentRegionHintLocked(final Region region) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001691 if (mSurfaceController == null) {
1692 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1693 return;
1694 }
Robert Carre6a83512015-11-03 16:09:21 -08001695 mSurfaceController.setTransparentRegionHint(region);
Craig Mautner48ba1e72012-04-02 13:18:16 -07001696 }
1697
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001698 void setWallpaperOffset(Point shownPosition) {
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001699 final LayoutParams attrs = mWin.getAttrs();
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001700 final int left = shownPosition.x - attrs.surfaceInsets.left;
1701 final int top = shownPosition.y - attrs.surfaceInsets.top;
Robert Carre6a83512015-11-03 16:09:21 -08001702
1703 try {
Craig Mautner71dd1b62014-02-18 15:48:52 -08001704 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setWallpaperOffset");
Robert Carr68e5c9e2016-09-14 10:50:09 -07001705 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -08001706 mSurfaceController.setPositionInTransaction(mWin.mFrame.left + left,
1707 mWin.mFrame.top + top, false);
Robert Carrfbbde852016-10-18 11:02:28 -07001708 applyCrop(null, null, false);
Robert Carre6a83512015-11-03 16:09:21 -08001709 } catch (RuntimeException e) {
1710 Slog.w(TAG, "Error positioning surface of " + mWin
1711 + " pos=(" + left + "," + top + ")", e);
1712 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -07001713 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -08001714 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1715 "<<< CLOSE TRANSACTION setWallpaperOffset");
Craig Mautner48ba1e72012-04-02 13:18:16 -07001716 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001717 }
1718
John Reck80181b92015-05-19 11:09:32 -07001719 /**
1720 * Try to change the pixel format without recreating the surface. This
1721 * will be common in the case of changing from PixelFormat.OPAQUE to
1722 * PixelFormat.TRANSLUCENT in the hardware-accelerated case as both
1723 * requested formats resolve to the same underlying SurfaceControl format
1724 * @return True if format was succesfully changed, false otherwise
1725 */
1726 boolean tryChangeFormatInPlaceLocked() {
Robert Carre6a83512015-11-03 16:09:21 -08001727 if (mSurfaceController == null) {
John Reck80181b92015-05-19 11:09:32 -07001728 return false;
1729 }
1730 final LayoutParams attrs = mWin.getAttrs();
Wale Ogunwalee7bf46b2015-09-30 09:19:28 -07001731 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
John Reck80181b92015-05-19 11:09:32 -07001732 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
1733 if (format == mSurfaceFormat) {
1734 setOpaqueLocked(!PixelFormat.formatHasAlpha(attrs.format));
1735 return true;
1736 }
1737 return false;
1738 }
1739
Craig Mautner6f612042014-09-07 13:13:23 -07001740 void setOpaqueLocked(boolean isOpaque) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001741 if (mSurfaceController == null) {
1742 return;
1743 }
Robert Carre6a83512015-11-03 16:09:21 -08001744 mSurfaceController.setOpaque(isOpaque);
Craig Mautner71dd1b62014-02-18 15:48:52 -08001745 }
1746
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001747 void setSecureLocked(boolean isSecure) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001748 if (mSurfaceController == null) {
1749 return;
1750 }
Robert Carre6a83512015-11-03 16:09:21 -08001751 mSurfaceController.setSecure(isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001752 }
1753
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001754 /**
1755 * Have the surface flinger show a surface, robustly dealing with
1756 * error conditions. In particular, if there is not enough memory
1757 * to show the surface, then we will try to get rid of other surfaces
1758 * in order to succeed.
1759 *
1760 * @return Returns true if the surface was successfully shown.
1761 */
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001762 private boolean showSurfaceRobustlyLocked() {
Robert Carr1b5ea722016-04-20 13:23:28 -07001763 final Task task = mWin.getTask();
1764 if (task != null && StackId.windowsAreScaleable(task.mStack.mStackId)) {
1765 mSurfaceController.forceScaleableInTransaction(true);
1766 }
1767
Robert Carre6a83512015-11-03 16:09:21 -08001768 boolean shown = mSurfaceController.showRobustlyInTransaction();
1769 if (!shown)
1770 return false;
1771
Robert Carre6a83512015-11-03 16:09:21 -08001772 return true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001773 }
1774
1775 void applyEnterAnimationLocked() {
Robert Carrb439a632016-04-07 22:52:10 -07001776 // If we are the new part of a window replacement transition and we have requested
1777 // not to animate, we instead want to make it seamless, so we don't want to apply
1778 // an enter transition.
1779 if (mWin.mSkipEnterAnimationForSeamlessReplacement) {
1780 return;
1781 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001782 final int transit;
1783 if (mEnterAnimationPending) {
1784 mEnterAnimationPending = false;
1785 transit = WindowManagerPolicy.TRANSIT_ENTER;
1786 } else {
1787 transit = WindowManagerPolicy.TRANSIT_SHOW;
1788 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001789 applyAnimationLocked(transit, true);
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001790 //TODO (multidisplay): Magnification is supported only for the default display.
Svetoslav8e3feb12014-02-24 13:46:47 -08001791 if (mService.mAccessibilityController != null
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001792 && mWin.getDisplayId() == DEFAULT_DISPLAY) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001793 mService.mAccessibilityController.onWindowTransitionLocked(mWin, transit);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001794 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001795 }
1796
1797 /**
1798 * Choose the correct animation and set it to the passed WindowState.
Craig Mautner4b71aa12012-12-27 17:20:01 -08001799 * @param transit If AppTransition.TRANSIT_PREVIEW_DONE and the app window has been drawn
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001800 * then the animation will be app_starting_exit. Any other value loads the animation from
1801 * the switch statement below.
1802 * @param isEntrance The animation type the last time this was called. Used to keep from
1803 * loading the same animation twice.
1804 * @return true if an animation has been loaded.
1805 */
1806 boolean applyAnimationLocked(int transit, boolean isEntrance) {
Jorim Jaggife762342016-10-13 14:33:27 +02001807 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001808 // If we are trying to apply an animation, but already running
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001809 // an animation of the same type, then just leave that one alone.
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001810 return true;
1811 }
1812
1813 // Only apply an animation if the display isn't frozen. If it is
1814 // frozen, there is no reason to animate and it can cause strange
1815 // artifacts when we unfreeze the display if some different animation
1816 // is running.
Chong Zhang8784be62016-06-28 15:25:07 -07001817 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "WSA#applyAnimationLocked");
David Stevens9440dc82017-03-16 19:00:20 -07001818 if (mWin.mToken.okToAnimate()) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001819 int anim = mPolicy.selectAnimationLw(mWin, transit);
1820 int attr = -1;
1821 Animation a = null;
1822 if (anim != 0) {
Dianne Hackborn4c1e3182012-10-05 18:37:54 -07001823 a = anim != -1 ? AnimationUtils.loadAnimation(mContext, anim) : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001824 } else {
1825 switch (transit) {
1826 case WindowManagerPolicy.TRANSIT_ENTER:
1827 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1828 break;
1829 case WindowManagerPolicy.TRANSIT_EXIT:
1830 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1831 break;
1832 case WindowManagerPolicy.TRANSIT_SHOW:
1833 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1834 break;
1835 case WindowManagerPolicy.TRANSIT_HIDE:
1836 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1837 break;
1838 }
1839 if (attr >= 0) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001840 a = mService.mAppTransition.loadAnimationAttr(mWin.mAttrs, attr);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001841 }
1842 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001843 if (DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001844 "applyAnimation: win=" + this
1845 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
Craig Mautner4d7349b2012-04-20 14:52:47 -07001846 + " a=" + a
Craig Mautner8863cca2012-09-18 15:04:34 -07001847 + " transit=" + transit
Craig Mautner83339b42012-05-01 22:13:23 -07001848 + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001849 if (a != null) {
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -08001850 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001851 setAnimation(a);
1852 mAnimationIsEntrance = isEntrance;
1853 }
1854 } else {
1855 clearAnimation();
1856 }
Chong Zhang8784be62016-06-28 15:25:07 -07001857 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
1858
Jorim Jaggieb88d832016-04-13 20:17:43 -07001859 if (mWin.mAttrs.type == TYPE_INPUT_METHOD) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001860 mWin.getDisplayContent().adjustForImeIfNeeded();
Jorim Jaggi7a4fd5e2016-04-14 19:55:35 -07001861 if (isEntrance) {
1862 mWin.setDisplayLayoutNeeded();
1863 mService.mWindowPlacerLocked.requestTraversal();
1864 }
Jorim Jaggieb88d832016-04-13 20:17:43 -07001865 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001866 return mAnimation != null;
1867 }
1868
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001869 private void applyFadeoutDuringKeyguardExitAnimation() {
1870 long startTime = mAnimation.getStartTime();
1871 long duration = mAnimation.getDuration();
1872 long elapsed = mLastAnimationTime - startTime;
1873 long fadeDuration = duration - elapsed;
1874 if (fadeDuration <= 0) {
1875 // Never mind, this would be no visible animation, so abort the animation change.
1876 return;
1877 }
1878 AnimationSet newAnimation = new AnimationSet(false /* shareInterpolator */);
1879 newAnimation.setDuration(duration);
1880 newAnimation.setStartTime(startTime);
1881 newAnimation.addAnimation(mAnimation);
1882 Animation fadeOut = AnimationUtils.loadAnimation(
1883 mContext, com.android.internal.R.anim.app_starting_exit);
1884 fadeOut.setDuration(fadeDuration);
1885 fadeOut.setStartOffset(elapsed);
1886 newAnimation.addAnimation(fadeOut);
Filip Gruszczynskif52dd202015-11-15 20:36:38 -08001887 newAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(), mAnimDx, mAnimDy);
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001888 mAnimation = newAnimation;
1889 }
1890
Steven Timotiusaf03df62017-07-18 16:56:43 -07001891 void writeToProto(ProtoOutputStream proto, long fieldId) {
1892 final long token = proto.start(fieldId);
1893 mLastClipRect.writeToProto(proto, LAST_CLIP_RECT);
1894 if (mSurfaceController != null) {
1895 mSurfaceController.writeToProto(proto, SURFACE);
1896 }
1897 proto.end(token);
1898 }
1899
Craig Mautnera2c77052012-03-26 12:14:43 -07001900 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1901 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1902 || mAnimation != null) {
1903 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1904 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1905 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001906 pw.print(" mAnimation="); pw.print(mAnimation);
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001907 pw.print(" mStackClip="); pw.println(mStackClip);
Craig Mautnera2c77052012-03-26 12:14:43 -07001908 }
1909 if (mHasTransformation || mHasLocalTransformation) {
1910 pw.print(prefix); pw.print("XForm: has=");
1911 pw.print(mHasTransformation);
1912 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1913 pw.print(" "); mTransformation.printShortString(pw);
1914 pw.println();
1915 }
Robert Carre6a83512015-11-03 16:09:21 -08001916 if (mSurfaceController != null) {
1917 mSurfaceController.dump(pw, prefix, dumpAll);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001918 }
Robert Carre6a83512015-11-03 16:09:21 -08001919 if (dumpAll) {
1920 pw.print(prefix); pw.print("mDrawState="); pw.print(drawStateToString());
1921 pw.print(prefix); pw.print(" mLastHidden="); pw.println(mLastHidden);
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001922 pw.print(prefix); pw.print("mSystemDecorRect="); mSystemDecorRect.printShortString(pw);
1923 pw.print(" last="); mLastSystemDecorRect.printShortString(pw);
Wale Ogunwale6bab4cf2016-04-07 12:23:08 -07001924 pw.print(" mHasClipRect="); pw.print(mHasClipRect);
1925 pw.print(" mLastClipRect="); mLastClipRect.printShortString(pw);
1926
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +01001927 if (!mLastFinalClipRect.isEmpty()) {
1928 pw.print(" mLastFinalClipRect="); mLastFinalClipRect.printShortString(pw);
1929 }
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001930 pw.println();
Robert Carre6a83512015-11-03 16:09:21 -08001931 }
1932
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001933 if (mPendingDestroySurface != null) {
1934 pw.print(prefix); pw.print("mPendingDestroySurface=");
1935 pw.println(mPendingDestroySurface);
1936 }
1937 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1938 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1939 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1940 }
1941 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1942 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1943 pw.print(" mAlpha="); pw.print(mAlpha);
1944 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1945 }
1946 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1947 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1948 pw.print(" mDsDx="); pw.print(mDsDx);
1949 pw.print(" mDtDx="); pw.print(mDtDx);
Robert Carr0edf18f2017-02-21 20:01:47 -08001950 pw.print(" mDtDy="); pw.print(mDtDy);
1951 pw.print(" mDsDy="); pw.println(mDsDy);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001952 }
Jorim Jaggiff71d202016-04-14 13:12:36 -07001953 if (mAnimationStartDelayed) {
1954 pw.print(prefix); pw.print("mAnimationStartDelayed="); pw.print(mAnimationStartDelayed);
1955 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001956 }
1957
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001958 @Override
1959 public String toString() {
Dianne Hackborn529e7442012-11-01 14:22:28 -07001960 StringBuffer sb = new StringBuffer("WindowStateAnimator{");
1961 sb.append(Integer.toHexString(System.identityHashCode(this)));
1962 sb.append(' ');
1963 sb.append(mWin.mAttrs.getTitle());
1964 sb.append('}');
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001965 return sb.toString();
1966 }
Robert Carre6a83512015-11-03 16:09:21 -08001967
1968 void reclaimSomeSurfaceMemory(String operation, boolean secure) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001969 mService.mRoot.reclaimSomeSurfaceMemory(this, operation, secure);
Robert Carre6a83512015-11-03 16:09:21 -08001970 }
1971
1972 boolean getShown() {
1973 if (mSurfaceController != null) {
1974 return mSurfaceController.getShown();
1975 }
1976 return false;
1977 }
1978
1979 void destroySurface() {
Wale Ogunwale722ff892016-02-18 13:37:55 -08001980 try {
1981 if (mSurfaceController != null) {
1982 mSurfaceController.destroyInTransaction();
1983 }
1984 } catch (RuntimeException e) {
1985 Slog.w(TAG, "Exception thrown when destroying surface " + this
1986 + " surface " + mSurfaceController + " session " + mSession + ": " + e);
1987 } finally {
1988 mWin.setHasSurface(false);
1989 mSurfaceController = null;
1990 mDrawState = NO_SURFACE;
1991 }
Robert Carre6a83512015-11-03 16:09:21 -08001992 }
Filip Gruszczynskif52dd202015-11-15 20:36:38 -08001993
1994 void setMoveAnimation(int left, int top) {
1995 final Animation a = AnimationUtils.loadAnimation(mContext,
1996 com.android.internal.R.anim.window_move_from_decor);
1997 setAnimation(a);
1998 mAnimDx = mWin.mLastFrame.left - left;
1999 mAnimDy = mWin.mLastFrame.top - top;
2000 mAnimateMove = true;
2001 }
Robert Carr679c8072016-04-07 15:51:48 -07002002
2003 void deferTransactionUntilParentFrame(long frameNumber) {
2004 if (!mWin.isChildWindow()) {
2005 return;
2006 }
Robert Carr679c8072016-04-07 15:51:48 -07002007 mSurfaceController.deferTransactionUntil(
Wale Ogunwalecaa53af2016-07-17 14:50:26 -07002008 mWin.getParentWindow().mWinAnimator.mSurfaceController.getHandle(), frameNumber);
Robert Carr679c8072016-04-07 15:51:48 -07002009 }
2010
Jorim Jaggiff71d202016-04-14 13:12:36 -07002011 /**
2012 * Sometimes we need to synchronize the first frame of animation with some external event.
2013 * To achieve this, we prolong the start of the animation and keep producing the first frame of
2014 * the animation.
2015 */
2016 private long getAnimationFrameTime(Animation animation, long currentTime) {
2017 if (mAnimationStartDelayed) {
2018 animation.setStartTime(currentTime);
2019 return currentTime + 1;
2020 }
2021 return currentTime;
2022 }
2023
2024 void startDelayingAnimationStart() {
2025 mAnimationStartDelayed = true;
2026 }
2027
2028 void endDelayingAnimationStart() {
2029 mAnimationStartDelayed = false;
2030 }
Robert Carr6da3cc02016-06-16 15:17:07 -07002031
2032 void seamlesslyRotateWindow(int oldRotation, int newRotation) {
2033 final WindowState w = mWin;
2034 if (!w.isVisibleNow() || w.mIsWallpaper) {
2035 return;
2036 }
2037
2038 final Rect cropRect = mService.mTmpRect;
2039 final Rect displayRect = mService.mTmpRect2;
2040 final RectF frameRect = mService.mTmpRectF;
2041 final Matrix transform = mService.mTmpTransform;
2042
2043 final float x = w.mFrame.left;
2044 final float y = w.mFrame.top;
2045 final float width = w.mFrame.width();
2046 final float height = w.mFrame.height();
2047
2048 mService.getDefaultDisplayContentLocked().getLogicalDisplayRect(displayRect);
2049 final float displayWidth = displayRect.width();
2050 final float displayHeight = displayRect.height();
2051
2052 // Compute a transform matrix to undo the coordinate space transformation,
2053 // and present the window at the same physical position it previously occupied.
2054 final int deltaRotation = DisplayContent.deltaRotation(newRotation, oldRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002055 DisplayContent.createRotationMatrix(deltaRotation, x, y, displayWidth, displayHeight,
2056 transform);
Robert Carr6da3cc02016-06-16 15:17:07 -07002057
Robert Carr897215d2017-03-29 12:25:50 -07002058 // We just need to apply a rotation matrix to the window. For example
2059 // if we have a portrait window and rotate to landscape, the window is still portrait
2060 // and now extends off the bottom of the screen (and only halfway across). Essentially we
2061 // apply a transform to display the current buffer at it's old position
2062 // (in the new coordinate space). We then freeze layer updates until the resize
2063 // occurs, at which point we undo, them.
2064 mService.markForSeamlessRotation(w, true);
2065 transform.getValues(mService.mTmpFloats);
Robert Carr6da3cc02016-06-16 15:17:07 -07002066
Robert Carr897215d2017-03-29 12:25:50 -07002067 float DsDx = mService.mTmpFloats[Matrix.MSCALE_X];
2068 float DtDx = mService.mTmpFloats[Matrix.MSKEW_Y];
2069 float DtDy = mService.mTmpFloats[Matrix.MSKEW_X];
2070 float DsDy = mService.mTmpFloats[Matrix.MSCALE_Y];
2071 float nx = mService.mTmpFloats[Matrix.MTRANS_X];
2072 float ny = mService.mTmpFloats[Matrix.MTRANS_Y];
2073 mSurfaceController.setPositionInTransaction(nx, ny, false);
2074 mSurfaceController.setMatrixInTransaction(DsDx * w.mHScale,
2075 DtDx * w.mVScale,
2076 DtDy * w.mHScale,
2077 DsDy * w.mVScale, false);
Robert Carr6da3cc02016-06-16 15:17:07 -07002078 }
Robert Carr3b716242016-08-16 16:02:21 -07002079
2080 void enableSurfaceTrace(FileDescriptor fd) {
2081 if (mSurfaceController != null) {
2082 mSurfaceController.installRemoteTrace(fd);
2083 }
2084 }
2085
2086 void disableSurfaceTrace() {
2087 if (mSurfaceController != null) {
2088 try {
2089 mSurfaceController.removeRemoteTrace();
2090 } catch (ClassCastException e) {
2091 Slog.e(TAG, "Disable surface trace for " + this + " but its not enabled");
2092 }
2093 }
2094 }
Robert Carr8f0a3ad2017-02-15 19:30:28 -08002095
2096 /** The force-scaled state for a given window can persist past
2097 * the state for it's stack as the windows complete resizing
2098 * independently of one another.
2099 */
2100 boolean isForceScaled() {
2101 final Task task = mWin.getTask();
2102 if (task != null && task.mStack.isForceScaled()) {
2103 return true;
2104 }
2105 return mForceScaleUntilResize;
2106 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08002107
2108 void detachChildren() {
2109 if (mSurfaceController != null) {
2110 mSurfaceController.detachChildren();
2111 }
2112 }
Craig Mautnera2c77052012-03-26 12:14:43 -07002113}