blob: 02f9aa13c9393a9893a75ce1f5a34c246efe4b60 [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
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080019import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwalee7bf46b2015-09-30 09:19:28 -070020import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080021import static android.view.WindowManager.LayoutParams.FLAG_SCALED;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070022import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080023import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
24import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
25import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
27import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
28import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE;
29import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
30import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -080031import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_CROP;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080032import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
33import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
34import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
35import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
36import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070037import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080038import static com.android.server.wm.WindowManagerService.localLOGV;
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -080039import static com.android.server.wm.WindowManagerService.logWithStack;
Jorim Jaggidc249c42015-12-15 14:57:31 -080040import static com.android.server.wm.WindowState.DRAG_RESIZE_MODE_DOCKED_DIVIDER;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080041import static com.android.server.wm.WindowState.DRAG_RESIZE_MODE_FREEFORM;
Filip Gruszczynski4501d232015-09-02 13:00:02 -070042import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
43import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070044
Craig Mautnerc2f9be02012-03-27 17:32:29 -070045import android.content.Context;
46import android.graphics.Matrix;
47import android.graphics.PixelFormat;
Craig Mautner7358fbf2012-04-12 21:06:33 -070048import android.graphics.Point;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070049import android.graphics.Rect;
Craig Mautner48ba1e72012-04-02 13:18:16 -070050import android.graphics.Region;
Craig Mautnera51a9562012-04-17 17:05:26 -070051import android.os.Debug;
Craig Mautner9c795042014-10-28 19:59:59 -070052import android.os.RemoteException;
Craig Mautnera2c77052012-03-26 12:14:43 -070053import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070054import android.view.DisplayInfo;
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070055import android.view.MagnificationSpec;
Igor Murashkina86ab6402013-08-30 12:58:36 -070056import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080057import android.view.SurfaceControl;
Craig Mautnera2c77052012-03-26 12:14:43 -070058import android.view.WindowManager;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070059import android.view.WindowManager.LayoutParams;
Filip Gruszczynski69cbc352015-11-11 13:46:04 -080060import android.view.WindowManagerPolicy;
Craig Mautnera2c77052012-03-26 12:14:43 -070061import android.view.animation.Animation;
Jorim Jaggif8d77da2014-11-11 16:59:12 +010062import android.view.animation.AnimationSet;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070063import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070064import android.view.animation.Transformation;
65
66import com.android.server.wm.WindowManagerService.H;
67
68import java.io.PrintWriter;
69
70/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070071 * Keep track of animations and surface operations for a single WindowState.
72 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070073class WindowStateAnimator {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080074 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowStateAnimator" : TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070075 static final int WINDOW_FREEZE_LAYER = TYPE_LAYER_MULTIPLIER * 200;
Craig Mautnera2c77052012-03-26 12:14:43 -070076
Craig Mautner918b53b2012-07-09 14:15:54 -070077 // Unchanging local convenience fields.
Craig Mautnera2c77052012-03-26 12:14:43 -070078 final WindowManagerService mService;
79 final WindowState mWin;
Craig Mautner322e4032012-07-13 13:35:20 -070080 final WindowStateAnimator mAttachedWinAnimator;
Craig Mautnere7ae2502012-03-26 17:11:19 -070081 final WindowAnimator mAnimator;
Craig Mautner8863cca2012-09-18 15:04:34 -070082 AppWindowAnimator mAppAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070083 final Session mSession;
84 final WindowManagerPolicy mPolicy;
85 final Context mContext;
Craig Mautner918b53b2012-07-09 14:15:54 -070086 final boolean mIsWallpaper;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070087 final WallpaperController mWallpaperControllerLocked;
Craig Mautnera2c77052012-03-26 12:14:43 -070088
89 // Currently running animation.
90 boolean mAnimating;
91 boolean mLocalAnimating;
92 Animation mAnimation;
93 boolean mAnimationIsEntrance;
94 boolean mHasTransformation;
95 boolean mHasLocalTransformation;
96 final Transformation mTransformation = new Transformation();
97 boolean mWasAnimating; // Were we animating going into the most recent animation step?
Craig Mautnerc2f9be02012-03-27 17:32:29 -070098 int mAnimLayer;
99 int mLastLayer;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100100 long mAnimationStartTime;
Jorim Jaggif8d77da2014-11-11 16:59:12 +0100101 long mLastAnimationTime;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700102
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700103 /**
104 * Set when we have changed the size of the surface, to know that
105 * we must tell them application to resize (and thus redraw itself).
106 */
107 boolean mSurfaceResized;
Chong Zhang5b2f1992015-11-13 15:40:36 -0800108 /**
109 * Whether we should inform the client on next relayoutWindow that
110 * the surface has been resized since last time.
111 */
112 boolean mReportSurfaceResized;
Robert Carre6a83512015-11-03 16:09:21 -0800113 WindowSurfaceController mSurfaceController;
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800114 private WindowSurfaceController mPendingDestroySurface;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700115
116 /**
117 * Set if the client has asked that the destroy of its surface be delayed
118 * until it explicitly says it is okay.
119 */
120 boolean mSurfaceDestroyDeferred;
121
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800122 private boolean mDestroyPreservedSurfaceUponRedraw;
Craig Mautner7d8df392012-04-06 15:26:23 -0700123 float mShownAlpha = 0;
124 float mAlpha = 0;
125 float mLastAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700126
Winson Chung399f6202014-03-19 10:47:20 -0700127 boolean mHasClipRect;
128 Rect mClipRect = new Rect();
129 Rect mTmpClipRect = new Rect();
130 Rect mLastClipRect = new Rect();
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -0700131 Rect mTmpStackBounds = new Rect();
Winson Chung399f6202014-03-19 10:47:20 -0700132
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -0800133 /**
134 * This is rectangle of the window's surface that is not covered by
135 * system decorations.
136 */
137 private final Rect mSystemDecorRect = new Rect();
138 private final Rect mLastSystemDecorRect = new Rect();
139
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800140 // Used to save animation distances between the time they are calculated and when they are used.
141 private int mAnimDx;
142 private int mAnimDy;
Jorim Jaggic5af4f82015-07-01 17:16:27 -0700143
144 /** Is the next animation to be started a window move animation? */
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800145 private boolean mAnimateMove = false;
Jorim Jaggic5af4f82015-07-01 17:16:27 -0700146
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700147 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
148 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
149
150 boolean mHaveMatrix;
151
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700152 // Set to true if, when the window gets displayed, it should perform
153 // an enter animation.
154 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700155
Craig Mautner9c795042014-10-28 19:59:59 -0700156 /** Used to indicate that this window is undergoing an enter animation. Used for system
157 * windows to make the callback to View.dispatchOnWindowShownCallback(). Set when the
158 * window is first added or shown, cleared when the callback has been made. */
159 boolean mEnteringAnimation;
160
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100161 boolean mKeyguardGoingAwayAnimation;
Jorim Jaggi76a16232014-08-08 17:00:47 +0200162
John Reck80181b92015-05-19 11:09:32 -0700163 /** The pixel format of the underlying SurfaceControl */
164 int mSurfaceFormat;
165
Craig Mautner749a7bb2012-04-02 13:49:53 -0700166 /** This is set when there is no Surface */
167 static final int NO_SURFACE = 0;
168 /** This is set after the Surface has been created but before the window has been drawn. During
169 * this time the surface is hidden. */
170 static final int DRAW_PENDING = 1;
171 /** This is set after the window has finished drawing for the first time but before its surface
172 * is shown. The surface will be displayed when the next layout is run. */
173 static final int COMMIT_DRAW_PENDING = 2;
174 /** This is set during the time after the window's drawing has been committed, and before its
175 * surface is actually shown. It is used to delay showing the surface until all windows in a
176 * token are ready to be shown. */
177 static final int READY_TO_SHOW = 3;
178 /** Set when the window has been shown in the screen the first time. */
179 static final int HAS_DRAWN = 4;
Adrian Roos3eeb4e62014-05-19 12:43:26 +0200180
Craig Mautner276a6eb2014-11-04 15:32:57 -0800181 String drawStateToString() {
182 switch (mDrawState) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700183 case NO_SURFACE: return "NO_SURFACE";
184 case DRAW_PENDING: return "DRAW_PENDING";
185 case COMMIT_DRAW_PENDING: return "COMMIT_DRAW_PENDING";
186 case READY_TO_SHOW: return "READY_TO_SHOW";
187 case HAS_DRAWN: return "HAS_DRAWN";
Craig Mautner276a6eb2014-11-04 15:32:57 -0800188 default: return Integer.toString(mDrawState);
Craig Mautner6fbda632012-07-03 09:26:39 -0700189 }
190 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700191 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700192
Craig Mautner749a7bb2012-04-02 13:49:53 -0700193 /** Was this window last hidden? */
194 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700195
Craig Mautnerbec53f72012-04-05 11:49:05 -0700196 int mAttrType;
197
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800198 private final Rect mTmpSize = new Rect();
199
Craig Mautnerc431e892015-02-11 13:14:26 -0800200 WindowStateAnimator(final WindowState win) {
Craig Mautner918b53b2012-07-09 14:15:54 -0700201 final WindowManagerService service = win.mService;
202
Craig Mautnera2c77052012-03-26 12:14:43 -0700203 mService = service;
Craig Mautner918b53b2012-07-09 14:15:54 -0700204 mAnimator = service.mAnimator;
205 mPolicy = service.mPolicy;
206 mContext = service.mContext;
Craig Mautnerd3849f52014-03-27 13:19:29 -0700207 final DisplayContent displayContent = win.getDisplayContent();
208 if (displayContent != null) {
209 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800210 mAnimDx = displayInfo.appWidth;
211 mAnimDy = displayInfo.appHeight;
Craig Mautnerd3849f52014-03-27 13:19:29 -0700212 } else {
213 Slog.w(TAG, "WindowStateAnimator ctor: Display has been removed");
214 // This is checked on return and dealt with.
215 }
Craig Mautner918b53b2012-07-09 14:15:54 -0700216
217 mWin = win;
Craig Mautner322e4032012-07-13 13:35:20 -0700218 mAttachedWinAnimator = win.mAttachedWindow == null
219 ? null : win.mAttachedWindow.mWinAnimator;
220 mAppAnimator = win.mAppToken == null ? null : win.mAppToken.mAppAnimator;
Craig Mautner918b53b2012-07-09 14:15:54 -0700221 mSession = win.mSession;
Craig Mautner918b53b2012-07-09 14:15:54 -0700222 mAttrType = win.mAttrs.type;
223 mIsWallpaper = win.mIsWallpaper;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700224 mWallpaperControllerLocked = mService.mWallpaperControllerLocked;
Craig Mautnera2c77052012-03-26 12:14:43 -0700225 }
226
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100227 public void setAnimation(Animation anim, long startTime) {
Craig Mautnerbec53f72012-04-05 11:49:05 -0700228 if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700229 mAnimating = false;
230 mLocalAnimating = false;
231 mAnimation = anim;
232 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700233 mAnimation.scaleCurrentDuration(mService.getWindowAnimationScaleLocked());
Craig Mautnera2c77052012-03-26 12:14:43 -0700234 // Start out animation gone if window is gone, or visible if window is visible.
235 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700236 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700237 mHasLocalTransformation = true;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100238 mAnimationStartTime = startTime;
239 }
240
241 public void setAnimation(Animation anim) {
242 setAnimation(anim, -1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700243 }
244
245 public void clearAnimation() {
246 if (mAnimation != null) {
247 mAnimating = true;
248 mLocalAnimating = false;
249 mAnimation.cancel();
250 mAnimation = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100251 mKeyguardGoingAwayAnimation = false;
Craig Mautnera2c77052012-03-26 12:14:43 -0700252 }
253 }
254
255 /** Is the window or its container currently animating? */
256 boolean isAnimating() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700257 return mAnimation != null
Craig Mautner322e4032012-07-13 13:35:20 -0700258 || (mAttachedWinAnimator != null && mAttachedWinAnimator.mAnimation != null)
Jorim Jaggi37875612015-02-19 21:05:31 +0100259 || (mAppAnimator != null && mAppAnimator.isAnimating());
Craig Mautnera2c77052012-03-26 12:14:43 -0700260 }
261
Craig Mautner0afddcb2012-05-08 15:38:00 -0700262 /** Is the window animating the DummyAnimation? */
263 boolean isDummyAnimation() {
Craig Mautner322e4032012-07-13 13:35:20 -0700264 return mAppAnimator != null
265 && mAppAnimator.animation == AppWindowAnimator.sDummyAnimation;
Craig Mautner0afddcb2012-05-08 15:38:00 -0700266 }
267
Stefan Kuhne7dcf08c2015-06-05 16:25:10 -0700268 /** Is this window currently set to animate or currently animating? */
Craig Mautnera2c77052012-03-26 12:14:43 -0700269 boolean isWindowAnimating() {
270 return mAnimation != null;
271 }
272
Craig Mautnera2c77052012-03-26 12:14:43 -0700273 void cancelExitAnimationForNextAnimationLocked() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800274 if (DEBUG_ANIM) Slog.d(TAG,
275 "cancelExitAnimationForNextAnimationLocked: " + mWin);
276
Craig Mautnera2c77052012-03-26 12:14:43 -0700277 if (mAnimation != null) {
278 mAnimation.cancel();
279 mAnimation = null;
Craig Mautner4d7349b2012-04-20 14:52:47 -0700280 mLocalAnimating = false;
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800281 mWin.destroyOrSaveSurface();
Craig Mautnera2c77052012-03-26 12:14:43 -0700282 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700283 }
284
285 private boolean stepAnimation(long currentTime) {
286 if ((mAnimation == null) || !mLocalAnimating) {
287 return false;
288 }
289 mTransformation.clear();
290 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700291 if (false && DEBUG_ANIM) Slog.v(TAG, "Stepped animation in " + this + ": more=" + more
292 + ", xform=" + mTransformation);
Craig Mautnera2c77052012-03-26 12:14:43 -0700293 return more;
294 }
295
296 // This must be called while inside a transaction. Returns true if
297 // there is more animation to run.
298 boolean stepAnimationLocked(long currentTime) {
Chet Haase9c450412015-10-01 13:25:58 -0700299 // Save the animation state as it was before this step so WindowManagerService can tell if
300 // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
301 mWasAnimating = mAnimating;
Craig Mautnerdf88d732014-01-27 09:21:32 -0800302 final DisplayContent displayContent = mWin.getDisplayContent();
303 if (displayContent != null && mService.okToDisplay()) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700304 // We will run animations as long as the display isn't frozen.
305
306 if (mWin.isDrawnLw() && mAnimation != null) {
307 mHasTransformation = true;
308 mHasLocalTransformation = true;
309 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700310 if (DEBUG_ANIM) Slog.v(
311 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700312 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
313 " wh=" + mWin.mFrame.height() +
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800314 " dx=" + mAnimDx + " dy=" + mAnimDy +
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700315 " scale=" + mService.getWindowAnimationScaleLocked());
Craig Mautnerdf88d732014-01-27 09:21:32 -0800316 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
tiger_huangb7f3f922015-04-27 20:36:46 +0800317 if (mAnimateMove) {
318 mAnimateMove = false;
319 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800320 mAnimDx, mAnimDy);
tiger_huangb7f3f922015-04-27 20:36:46 +0800321 } else {
322 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
323 displayInfo.appWidth, displayInfo.appHeight);
324 }
Filip Gruszczynskif52dd202015-11-15 20:36:38 -0800325 mAnimDx = displayInfo.appWidth;
326 mAnimDy = displayInfo.appHeight;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100327 mAnimation.setStartTime(mAnimationStartTime != -1
328 ? mAnimationStartTime
329 : currentTime);
Craig Mautnera2c77052012-03-26 12:14:43 -0700330 mLocalAnimating = true;
331 mAnimating = true;
332 }
333 if ((mAnimation != null) && mLocalAnimating) {
Jorim Jaggif8d77da2014-11-11 16:59:12 +0100334 mLastAnimationTime = currentTime;
Craig Mautnera2c77052012-03-26 12:14:43 -0700335 if (stepAnimation(currentTime)) {
336 return true;
337 }
338 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700339 if (DEBUG_ANIM) Slog.v(
340 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700341 " @ " + currentTime);
342 //WindowManagerService.this.dump();
343 }
344 mHasLocalTransformation = false;
Craig Mautner322e4032012-07-13 13:35:20 -0700345 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppAnimator != null
346 && mAppAnimator.animation != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700347 // When our app token is animating, we kind-of pretend like
348 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
349 // part of this check means that we will only do this if
350 // our window is not currently exiting, or it is not
351 // locally animating itself. The idea being that one that
352 // is exiting and doing a local animation should be removed
353 // once that animation is done.
354 mAnimating = true;
355 mHasTransformation = true;
356 mTransformation.clear();
357 return false;
358 } else if (mHasTransformation) {
359 // Little trick to get through the path below to act like
360 // we have finished an animation.
361 mAnimating = true;
362 } else if (isAnimating()) {
363 mAnimating = true;
364 }
365 } else if (mAnimation != null) {
366 // If the display is frozen, and there is a pending animation,
367 // clear it and make sure we run the cleanup code.
368 mAnimating = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700369 }
370
371 if (!mAnimating && !mLocalAnimating) {
372 return false;
373 }
374
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700375 // Done animating, clean up.
376 if (DEBUG_ANIM) Slog.v(
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800377 TAG, "Animation done in " + this + ": exiting=" + mWin.mAnimatingExit
Craig Mautnera2c77052012-03-26 12:14:43 -0700378 + ", reportedVisible="
379 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
380
381 mAnimating = false;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +0100382 mKeyguardGoingAwayAnimation = false;
Craig Mautnera2c77052012-03-26 12:14:43 -0700383 mLocalAnimating = false;
384 if (mAnimation != null) {
385 mAnimation.cancel();
386 mAnimation = null;
387 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700388 if (mAnimator.mWindowDetachedWallpaper == mWin) {
389 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700390 }
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800391 mAnimLayer = mWin.mLayer
392 + mService.mLayersController.getSpecialWindowAnimLayerAdjustment(mWin);
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800393 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700394 mHasTransformation = false;
395 mHasLocalTransformation = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800396 mWin.checkPolicyVisibilityChange();
Craig Mautnera2c77052012-03-26 12:14:43 -0700397 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700398 if (mDrawState == HAS_DRAWN
Craig Mautnera2c77052012-03-26 12:14:43 -0700399 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
400 && mWin.mAppToken != null
401 && mWin.mAppToken.firstWindowDrawn
402 && mWin.mAppToken.startingData != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700403 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
Craig Mautnera2c77052012-03-26 12:14:43 -0700404 + mWin.mToken + ": first real window done animating");
405 mService.mFinishedStarting.add(mWin.mAppToken);
406 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
Craig Mautner81defc72013-10-29 11:10:42 -0700407 } else if (mAttrType == LayoutParams.TYPE_STATUS_BAR && mWin.mPolicyVisibility) {
408 // Upon completion of a not-visible to visible status bar animation a relayout is
409 // required.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800410 if (displayContent != null) {
411 displayContent.layoutNeeded = true;
412 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700413 }
414
415 finishExit();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800416 final int displayId = mWin.getDisplayId();
Craig Mautner76a71652012-09-03 23:23:58 -0700417 mAnimator.setPendingLayoutChanges(displayId, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800418 if (DEBUG_LAYOUT_REPEATS)
Filip Gruszczynski4501d232015-09-02 13:00:02 -0700419 mService.mWindowPlacerLocked.debugLayoutRepeats(
420 "WindowStateAnimator", mAnimator.getPendingLayoutChanges(displayId));
Craig Mautnera2c77052012-03-26 12:14:43 -0700421
422 if (mWin.mAppToken != null) {
423 mWin.mAppToken.updateReportedVisibilityLocked();
424 }
425
426 return false;
427 }
428
429 void finishExit() {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800430 if (DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700431 TAG, "finishExit in " + this
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800432 + ": exiting=" + mWin.mAnimatingExit
Craig Mautnera2c77052012-03-26 12:14:43 -0700433 + " remove=" + mWin.mRemoveOnExit
434 + " windowAnimating=" + isWindowAnimating());
435
436 final int N = mWin.mChildWindows.size();
437 for (int i=0; i<N; i++) {
438 mWin.mChildWindows.get(i).mWinAnimator.finishExit();
439 }
440
Filip Gruszczynski57f76f12015-11-04 16:10:54 -0800441 if (mEnteringAnimation) {
442 mEnteringAnimation = false;
443 mService.requestTraversal();
444 // System windows don't have an activity and an app token as a result, but need a way
445 // to be informed about their entrance animation end.
446 if (mWin.mAppToken == null) {
447 try {
448 mWin.mClient.dispatchWindowShown();
449 } catch (RemoteException e) {
450 }
Craig Mautner9c795042014-10-28 19:59:59 -0700451 }
452 }
453
Svetoslav75b55d02014-12-03 16:26:35 -0800454 if (!isWindowAnimating()) {
455 //TODO (multidisplay): Accessibility is supported only for the default display.
456 if (mService.mAccessibilityController != null
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800457 && mWin.getDisplayId() == DEFAULT_DISPLAY) {
Svetoslav75b55d02014-12-03 16:26:35 -0800458 mService.mAccessibilityController.onSomeWindowResizedOrMovedLocked();
459 }
460 }
461
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800462 if (!mWin.mAnimatingExit) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700463 return;
464 }
465
466 if (isWindowAnimating()) {
467 return;
468 }
469
Filip Gruszczynski19723a42015-11-25 15:01:48 -0800470 if (WindowManagerService.localLOGV) Slog.v(
471 TAG, "Exit animation finished in " + this
472 + ": remove=" + mWin.mRemoveOnExit);
Robert Carre12aece2016-02-02 22:43:27 -0800473
474
475 mWin.mDestroying = true;
476
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800477 final boolean hasSurface = hasSurface();
478 if (hasSurface) {
479 hide("finishExit");
480 }
481
Robert Carre12aece2016-02-02 22:43:27 -0800482 // If we have an app token, we ask it to destroy the surface for us,
483 // so that it can take care to ensure the activity has actually stopped
484 // and the surface is not still in use. Otherwise we add the service to
485 // mDestroySurface and allow it to be processed in our next transaction.
486 if (mWin.mAppToken != null) {
Robert Carre12aece2016-02-02 22:43:27 -0800487 mWin.mAppToken.destroySurfaces();
488 } else {
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800489 if (hasSurface) {
Robert Carre12aece2016-02-02 22:43:27 -0800490 mService.mDestroySurface.add(mWin);
Robert Carre12aece2016-02-02 22:43:27 -0800491 }
Robert Carre12aece2016-02-02 22:43:27 -0800492 if (mWin.mRemoveOnExit) {
493 mService.mPendingRemove.add(mWin);
494 mWin.mRemoveOnExit = false;
495 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700496 }
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800497 mWin.mAnimatingExit = false;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700498 mWallpaperControllerLocked.hideWallpapers(mWin);
Craig Mautner0afddcb2012-05-08 15:38:00 -0700499 }
500
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800501 void hide(String reason) {
Craig Mautner0afddcb2012-05-08 15:38:00 -0700502 if (!mLastHidden) {
503 //dump();
504 mLastHidden = true;
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800505 if (mSurfaceController != null) {
506 mSurfaceController.hideInTransaction(reason);
507 }
Craig Mautner0afddcb2012-05-08 15:38:00 -0700508 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700509 }
510
Craig Mautnera608b882012-03-30 13:03:49 -0700511 boolean finishDrawingLocked() {
Craig Mautner42d04db2014-11-06 12:13:23 -0800512 final boolean startingWindow =
513 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
514 if (DEBUG_STARTING_WINDOW && startingWindow) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700515 Slog.v(TAG, "Finishing drawing window " + mWin + ": mDrawState="
Craig Mautner276a6eb2014-11-04 15:32:57 -0800516 + drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700517 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800518 if (mWin.mAppToken != null && mWin.mAppToken.mAnimatingWithSavedSurface) {
Chong Zhangbef461f2015-10-27 11:38:24 -0700519 // App has drawn something to its windows, we're no longer animating with
520 // the saved surfaces. If the user exits now, we only want to save again
521 // if allDrawn is true.
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800522 if (DEBUG_ANIM) Slog.d(TAG,
523 "finishDrawingLocked: mAnimatingWithSavedSurface=false " + mWin);
Chong Zhangbef461f2015-10-27 11:38:24 -0700524 mWin.mAppToken.mAnimatingWithSavedSurface = false;
525 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700526 if (mDrawState == DRAW_PENDING) {
Craig Mautneref25d7a2012-05-15 23:01:47 -0700527 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Chong Zhang5b2f1992015-11-13 15:40:36 -0800528 Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + mWin + " in "
Robert Carre6a83512015-11-03 16:09:21 -0800529 + mSurfaceController);
Craig Mautner42d04db2014-11-06 12:13:23 -0800530 if (DEBUG_STARTING_WINDOW && startingWindow) {
Craig Mautner6fbda632012-07-03 09:26:39 -0700531 Slog.v(TAG, "Draw state now committed in " + mWin);
532 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700533 mDrawState = COMMIT_DRAW_PENDING;
Craig Mautnera608b882012-03-30 13:03:49 -0700534 return true;
535 }
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700536
Craig Mautnera608b882012-03-30 13:03:49 -0700537 return false;
538 }
539
540 // This must be called while inside a transaction.
Craig Mautnerc431e892015-02-11 13:14:26 -0800541 boolean commitFinishDrawingLocked() {
Craig Mautner6fbda632012-07-03 09:26:39 -0700542 if (DEBUG_STARTING_WINDOW &&
543 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
544 Slog.i(TAG, "commitFinishDrawingLocked: " + mWin + " cur mDrawState="
Craig Mautner276a6eb2014-11-04 15:32:57 -0800545 + drawStateToString());
Craig Mautner6fbda632012-07-03 09:26:39 -0700546 }
Craig Mautner276a6eb2014-11-04 15:32:57 -0800547 if (mDrawState != COMMIT_DRAW_PENDING && mDrawState != READY_TO_SHOW) {
Craig Mautnera608b882012-03-30 13:03:49 -0700548 return false;
549 }
Craig Mautner6fbda632012-07-03 09:26:39 -0700550 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM) {
Robert Carre6a83512015-11-03 16:09:21 -0800551 Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurfaceController);
Craig Mautner6fbda632012-07-03 09:26:39 -0700552 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700553 mDrawState = READY_TO_SHOW;
Chong Zhang0275e392015-09-17 10:41:44 -0700554 boolean result = false;
Craig Mautnera608b882012-03-30 13:03:49 -0700555 final AppWindowToken atoken = mWin.mAppToken;
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800556 if (atoken == null || atoken.allDrawn || mWin.mAttrs.type == TYPE_APPLICATION_STARTING) {
Chong Zhang0275e392015-09-17 10:41:44 -0700557 result = performShowLocked();
558 }
Chong Zhang97782b42015-10-07 16:01:23 -0700559 return result;
Craig Mautnera608b882012-03-30 13:03:49 -0700560 }
561
Chong Zhang97782b42015-10-07 16:01:23 -0700562 void preserveSurfaceLocked() {
563 if (mDestroyPreservedSurfaceUponRedraw) {
Chong Zhangb9b0fec2016-02-11 18:51:51 -0800564 // This could happen when switching the surface mode very fast. For example,
565 // we preserved a surface when dragResizing changed to true. Then before the
566 // preserved surface is removed, dragResizing changed to false again.
567 // In this case, we need to leave the preserved surface alone, and destroy
568 // the actual surface, so that the createSurface call could create a surface
569 // of the proper size. The preserved surface will still be removed when client
570 // finishes drawing to the new surface.
571 mSurfaceDestroyDeferred = false;
572 destroySurfaceLocked();
573 mSurfaceDestroyDeferred = true;
Chong Zhang97782b42015-10-07 16:01:23 -0700574 return;
575 }
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -0800576 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "SET FREEZE LAYER", false);
Wale Ogunwalef9e09782015-11-09 12:42:37 -0800577 if (mSurfaceController != null) {
578 mSurfaceController.setLayer(mAnimLayer + 1);
579 }
Chong Zhang97782b42015-10-07 16:01:23 -0700580 mDestroyPreservedSurfaceUponRedraw = true;
581 mSurfaceDestroyDeferred = true;
582 destroySurfaceLocked();
583 }
584
585 void destroyPreservedSurfaceLocked() {
586 if (!mDestroyPreservedSurfaceUponRedraw) {
587 return;
588 }
589 destroyDeferredSurfaceLocked();
590 mDestroyPreservedSurfaceUponRedraw = false;
591 }
592
Robert Carre6a83512015-11-03 16:09:21 -0800593 WindowSurfaceController createSurfaceLocked() {
Alan Viveretteccb11e12014-07-08 16:04:02 -0700594 final WindowState w = mWin;
Chong Zhanga8975bd2016-01-28 17:13:47 -0800595 if (w.hasSavedSurface()) {
Chong Zhang0464a932016-02-03 11:47:16 -0800596 if (DEBUG_ANIM) Slog.i(TAG,
597 "createSurface: " + this + ": called when we had a saved surface");
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800598 w.restoreSavedSurface();
599 return mSurfaceController;
Chong Zhanga8975bd2016-01-28 17:13:47 -0800600 }
601
Wale Ogunwale722ff892016-02-18 13:37:55 -0800602 if (mSurfaceController != null) {
603 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700604 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800605
606 w.setHasSurface(false);
607
608 if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,
609 "createSurface " + this + ": mDrawState=DRAW_PENDING");
610
611 mDrawState = DRAW_PENDING;
612 if (w.mAppToken != null) {
613 if (w.mAppToken.mAppAnimator.animation == null) {
614 w.mAppToken.allDrawn = false;
615 w.mAppToken.deferClearAllDrawn = false;
616 } else {
617 // Currently animating, persist current state of allDrawn until animation
618 // is complete.
619 w.mAppToken.deferClearAllDrawn = true;
620 }
621 }
622
623 mService.makeWindowFreezingScreenIfNeededLocked(w);
624
625 int flags = SurfaceControl.HIDDEN;
626 final WindowManager.LayoutParams attrs = w.mAttrs;
627
628 if (mService.isSecureLocked(w)) {
629 flags |= SurfaceControl.SECURE;
630 }
631
632 mTmpSize.set(w.mFrame.left + w.mXOffset, w.mFrame.top + w.mYOffset, 0, 0);
633 calculateSurfaceBounds(w, attrs);
634 final int width = mTmpSize.width();
635 final int height = mTmpSize.height();
636
637 if (DEBUG_VISIBILITY) {
638 Slog.v(TAG, "Creating surface in session "
639 + mSession.mSurfaceSession + " window " + this
640 + " w=" + width + " h=" + height
641 + " x=" + mTmpSize.left + " y=" + mTmpSize.top
642 + " format=" + attrs.format + " flags=" + flags);
643 }
644
645 // We may abort, so initialize to defaults.
646 mLastSystemDecorRect.set(0, 0, 0, 0);
647 mHasClipRect = false;
648 mClipRect.set(0, 0, 0, 0);
649 mLastClipRect.set(0, 0, 0, 0);
650
651 // Set up surface control with initial size.
652 try {
653
654 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
655 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
656 if (!PixelFormat.formatHasAlpha(attrs.format)
657 // Don't make surface with surfaceInsets opaque as they display a
658 // translucent shadow.
659 && attrs.surfaceInsets.left == 0
660 && attrs.surfaceInsets.top == 0
661 && attrs.surfaceInsets.right == 0
662 && attrs.surfaceInsets.bottom == 0
663 // Don't make surface opaque when resizing to reduce the amount of
664 // artifacts shown in areas the app isn't drawing content to.
665 && !w.isDragResizing()) {
666 flags |= SurfaceControl.OPAQUE;
667 }
668
669 mSurfaceController = new WindowSurfaceController(mSession.mSurfaceSession,
670 attrs.getTitle().toString(),
671 width, height, format, flags, this);
672
673 w.setHasSurface(true);
674
675 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
676 Slog.i(TAG, " CREATE SURFACE "
677 + mSurfaceController + " IN SESSION "
678 + mSession.mSurfaceSession
679 + ": pid=" + mSession.mPid + " format="
680 + attrs.format + " flags=0x"
681 + Integer.toHexString(flags)
682 + " / " + this);
683 }
684 } catch (OutOfResourcesException e) {
685 Slog.w(TAG, "OutOfResourcesException creating surface");
686 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
687 mDrawState = NO_SURFACE;
688 return null;
689 } catch (Exception e) {
690 Slog.e(TAG, "Exception creating surface", e);
691 mDrawState = NO_SURFACE;
692 return null;
693 }
694
695 if (WindowManagerService.localLOGV) Slog.v(TAG, "Got surface: " + mSurfaceController
696 + ", set left=" + w.mFrame.left + " top=" + w.mFrame.top
697 + ", animLayer=" + mAnimLayer);
698
699 if (SHOW_LIGHT_TRANSACTIONS) {
700 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
701 WindowManagerService.logSurface(w, "CREATE pos=("
702 + w.mFrame.left + "," + w.mFrame.top + ") ("
703 + width + "x" + height + "), layer=" + mAnimLayer + " HIDE", false);
704 }
705
706 // Start a new transaction and apply position & offset.
707 final int layerStack = w.getDisplayContent().getDisplay().getLayerStack();
708 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
709 "POS " + mTmpSize.left + ", " + mTmpSize.top, false);
710 mSurfaceController.setPositionAndLayer(mTmpSize.left, mTmpSize.top, layerStack, mAnimLayer);
711 mLastHidden = true;
712
713 if (WindowManagerService.localLOGV) Slog.v(TAG, "Created surface " + this);
Robert Carre6a83512015-11-03 16:09:21 -0800714 return mSurfaceController;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700715 }
716
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800717 private void calculateSurfaceBounds(WindowState w, LayoutParams attrs) {
718 if ((attrs.flags & FLAG_SCALED) != 0) {
719 // For a scaled surface, we always want the requested size.
720 mTmpSize.right = mTmpSize.left + w.mRequestedWidth;
721 mTmpSize.bottom = mTmpSize.top + w.mRequestedHeight;
722 } else {
723 // When we're doing a drag-resizing, request a surface that's fullscreen size,
724 // so that we don't need to reallocate during the process. This also prevents
725 // buffer drops due to size mismatch.
726 if (w.isDragResizing()) {
727 if (w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM) {
728 mTmpSize.left = 0;
729 mTmpSize.top = 0;
730 }
731 final DisplayInfo displayInfo = w.getDisplayInfo();
732 mTmpSize.right = mTmpSize.left + displayInfo.logicalWidth;
733 mTmpSize.bottom = mTmpSize.top + displayInfo.logicalHeight;
734 } else {
735 mTmpSize.right = mTmpSize.left + w.mCompatFrame.width();
736 mTmpSize.bottom = mTmpSize.top + w.mCompatFrame.height();
737 }
738 }
739
740 // Something is wrong and SurfaceFlinger will not like this, try to revert to sane values.
Filip Gruszczynskie6ed1952015-12-21 09:34:16 -0800741 // This doesn't necessarily mean that there is an error in the system. The sizes might be
742 // incorrect, because it is before the first layout or draw.
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800743 if (mTmpSize.width() < 1) {
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800744 mTmpSize.right = mTmpSize.left + 1;
745 }
746 if (mTmpSize.height() < 1) {
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800747 mTmpSize.bottom = mTmpSize.top + 1;
748 }
749
750 final int displayId = w.getDisplayId();
751 float scale = 1.0f;
752 // Magnification is supported only for the default display.
753 if (mService.mAccessibilityController != null && displayId == DEFAULT_DISPLAY) {
754 final MagnificationSpec spec =
755 mService.mAccessibilityController.getMagnificationSpecForWindowLocked(w);
756 if (spec != null && !spec.isNop()) {
757 scale = spec.scale;
758 }
759 }
760
761 // Adjust for surface insets.
762 mTmpSize.left -= scale * attrs.surfaceInsets.left;
763 mTmpSize.top -= scale * attrs.surfaceInsets.top;
764 mTmpSize.right += scale * (attrs.surfaceInsets.left + attrs.surfaceInsets.right);
765 mTmpSize.bottom += scale * (attrs.surfaceInsets.top + attrs.surfaceInsets.bottom);
766 }
767
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800768 boolean hasSurface() {
769 return !mWin.mSurfaceSaved
770 && mSurfaceController != null && mSurfaceController.hasSurface();
771 }
772
Craig Mautner96868332012-12-04 14:29:11 -0800773 void destroySurfaceLocked() {
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700774 final AppWindowToken wtoken = mWin.mAppToken;
775 if (wtoken != null) {
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700776 wtoken.mAnimatingWithSavedSurface = false;
777 if (mWin == wtoken.startingWindow) {
778 wtoken.startingDisplayed = false;
779 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700780 }
781
Robert Carr13f7be9e2015-12-02 18:39:45 -0800782 mWin.mSurfaceSaved = false;
783
Wale Ogunwale722ff892016-02-18 13:37:55 -0800784 if (mSurfaceController == null) {
785 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700786 }
Wale Ogunwale722ff892016-02-18 13:37:55 -0800787
788 int i = mWin.mChildWindows.size();
789 // When destroying a surface we want to make sure child windows are hidden. If we are
790 // preserving the surface until redraw though we intend to swap it out with another surface
791 // for resizing. In this case the window always remains visible to the user and the child
792 // windows should likewise remain visible.
793 while (!mDestroyPreservedSurfaceUponRedraw && i > 0) {
794 i--;
795 WindowState c = mWin.mChildWindows.get(i);
796 c.mAttachedHidden = true;
797 }
798
799 try {
800 if (DEBUG_VISIBILITY) logWithStack(TAG, "Window " + this + " destroying surface "
801 + mSurfaceController + ", session " + mSession);
802 if (mSurfaceDestroyDeferred) {
803 if (mSurfaceController != null && mPendingDestroySurface != mSurfaceController) {
804 if (mPendingDestroySurface != null) {
805 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
806 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true);
807 }
808 mPendingDestroySurface.destroyInTransaction();
809 }
810 mPendingDestroySurface = mSurfaceController;
811 }
812 } else {
813 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
814 WindowManagerService.logSurface(mWin, "DESTROY", true);
815 }
816 destroySurface();
817 }
818 // Don't hide wallpaper if we're deferring the surface destroy
819 // because of a surface change.
820 if (!mDestroyPreservedSurfaceUponRedraw) {
821 mWallpaperControllerLocked.hideWallpapers(mWin);
822 }
823 } catch (RuntimeException e) {
824 Slog.w(TAG, "Exception thrown when destroying Window " + this
825 + " surface " + mSurfaceController + " session " + mSession + ": " + e.toString());
826 }
827
828 // Whether the surface was preserved (and copied to mPendingDestroySurface) or not, it
829 // needs to be cleared to match the WindowState.mHasSurface state. It is also necessary
830 // so it can be recreated successfully in mPendingDestroySurface case.
831 mWin.setHasSurface(false);
832 if (mSurfaceController != null) {
833 mSurfaceController.setShown(false);
834 }
835 mSurfaceController = null;
836 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700837 }
838
Craig Mautner96868332012-12-04 14:29:11 -0800839 void destroyDeferredSurfaceLocked() {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700840 try {
841 if (mPendingDestroySurface != null) {
842 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -0800843 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700844 }
Robert Carre6a83512015-11-03 16:09:21 -0800845 mPendingDestroySurface.destroyInTransaction();
Chong Zhang6e21cf42015-11-09 14:41:42 -0800846 // Don't hide wallpaper if we're destroying a deferred surface
847 // after a surface mode change.
848 if (!mDestroyPreservedSurfaceUponRedraw) {
849 mWallpaperControllerLocked.hideWallpapers(mWin);
850 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700851 }
852 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700853 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700854 + this + " surface " + mPendingDestroySurface
855 + " session " + mSession + ": " + e.toString());
856 }
857 mSurfaceDestroyDeferred = false;
858 mPendingDestroySurface = null;
859 }
860
861 void computeShownFrameLocked() {
862 final boolean selfTransformation = mHasLocalTransformation;
863 Transformation attachedTransformation =
Craig Mautner322e4032012-07-13 13:35:20 -0700864 (mAttachedWinAnimator != null && mAttachedWinAnimator.mHasLocalTransformation)
865 ? mAttachedWinAnimator.mTransformation : null;
866 Transformation appTransformation = (mAppAnimator != null && mAppAnimator.hasTransformation)
867 ? mAppAnimator.transformation : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700868
869 // Wallpapers are animated based on the "real" window they
870 // are currently targeting.
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700871 final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget();
Craig Mautner798adef2013-10-22 14:29:01 -0700872 if (mIsWallpaper && wallpaperTarget != null && mService.mAnimateWallpaperWithTarget) {
873 final WindowStateAnimator wallpaperAnimator = wallpaperTarget.mWinAnimator;
Craig Mautner918b53b2012-07-09 14:15:54 -0700874 if (wallpaperAnimator.mHasLocalTransformation &&
875 wallpaperAnimator.mAnimation != null &&
876 !wallpaperAnimator.mAnimation.getDetachWallpaper()) {
877 attachedTransformation = wallpaperAnimator.mTransformation;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800878 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700879 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
880 }
881 }
Craig Mautner798adef2013-10-22 14:29:01 -0700882 final AppWindowAnimator wpAppAnimator = wallpaperTarget.mAppToken == null ?
883 null : wallpaperTarget.mAppToken.mAppAnimator;
884 if (wpAppAnimator != null && wpAppAnimator.hasTransformation
Craig Mautner918b53b2012-07-09 14:15:54 -0700885 && wpAppAnimator.animation != null
886 && !wpAppAnimator.animation.getDetachWallpaper()) {
Craig Mautner59431632012-04-04 11:56:44 -0700887 appTransformation = wpAppAnimator.transformation;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800888 if (DEBUG_WALLPAPER && appTransformation != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700889 Slog.v(TAG, "WP target app xform: " + appTransformation);
890 }
891 }
892 }
893
Craig Mautnera91f9e22012-09-14 16:22:08 -0700894 final int displayId = mWin.getDisplayId();
895 final ScreenRotationAnimation screenRotationAnimation =
896 mAnimator.getScreenRotationAnimationLocked(displayId);
897 final boolean screenAnimation =
898 screenRotationAnimation != null && screenRotationAnimation.isAnimating();
Robert Carr2f0fe622015-09-25 14:56:38 -0700899
900 mHasClipRect = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700901 if (selfTransformation || attachedTransformation != null
902 || appTransformation != null || screenAnimation) {
903 // cache often used attributes locally
904 final Rect frame = mWin.mFrame;
905 final float tmpFloats[] = mService.mTmpFloats;
906 final Matrix tmpMatrix = mWin.mTmpMatrix;
907
908 // Compute the desired transformation.
Dianne Hackborn4b169692012-11-29 17:51:24 -0800909 if (screenAnimation && screenRotationAnimation.isRotating()) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700910 // If we are doing a screen animation, the global rotation
911 // applied to windows can result in windows that are carefully
912 // aligned with each other to slightly separate, allowing you
913 // to see what is behind them. An unsightly mess. This...
914 // thing... magically makes it call good: scale each window
915 // slightly (two pixels larger in each dimension, from the
916 // window's center).
917 final float w = frame.width();
918 final float h = frame.height();
919 if (w>=1 && h>=1) {
920 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
921 } else {
922 tmpMatrix.reset();
923 }
924 } else {
925 tmpMatrix.reset();
926 }
927 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
928 if (selfTransformation) {
929 tmpMatrix.postConcat(mTransformation.getMatrix());
930 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700931 if (attachedTransformation != null) {
932 tmpMatrix.postConcat(attachedTransformation.getMatrix());
933 }
934 if (appTransformation != null) {
935 tmpMatrix.postConcat(appTransformation.getMatrix());
936 }
937 if (screenAnimation) {
Craig Mautnera91f9e22012-09-14 16:22:08 -0700938 tmpMatrix.postConcat(screenRotationAnimation.getEnterTransformation().getMatrix());
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700939 }
Filip Gruszczynski9ca40b72015-08-13 13:35:25 -0700940 // The translation that applies the position of the window needs to be applied at the
941 // end in case that other translations include scaling. Otherwise the scaling will
942 // affect this translation.
943 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
Winson Chung399f6202014-03-19 10:47:20 -0700944
Svetoslav Ganov545252f2012-12-10 18:29:24 -0800945 //TODO (multidisplay): Magnification is supported only for the default display.
Filip Gruszczynski69cbc352015-11-11 13:46:04 -0800946 if (mService.mAccessibilityController != null && displayId == DEFAULT_DISPLAY) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800947 MagnificationSpec spec = mService.mAccessibilityController
Svetoslav Ganov545252f2012-12-10 18:29:24 -0800948 .getMagnificationSpecForWindowLocked(mWin);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700949 if (spec != null && !spec.isNop()) {
950 tmpMatrix.postScale(spec.scale, spec.scale);
951 tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
952 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700953 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700954
955 // "convert" it into SurfaceFlinger's format
956 // (a 2x2 matrix + an offset)
957 // Here we must not transform the position of the surface
958 // since it is already included in the transformation.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800959 //Slog.i(TAG_WM, "Transform: " + matrix);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700960
961 mHaveMatrix = true;
962 tmpMatrix.getValues(tmpFloats);
963 mDsDx = tmpFloats[Matrix.MSCALE_X];
964 mDtDx = tmpFloats[Matrix.MSKEW_Y];
965 mDsDy = tmpFloats[Matrix.MSKEW_X];
966 mDtDy = tmpFloats[Matrix.MSCALE_Y];
967 float x = tmpFloats[Matrix.MTRANS_X];
968 float y = tmpFloats[Matrix.MTRANS_Y];
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -0700969 mWin.mShownPosition.set((int) x, (int) y);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700970
971 // Now set the alpha... but because our current hardware
972 // can't do alpha transformation on a non-opaque surface,
973 // turn it off if we are running an animation that is also
974 // transforming since it is more important to have that
975 // animation be smooth.
976 mShownAlpha = mAlpha;
977 if (!mService.mLimitedAlphaCompositing
978 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
979 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
980 && x == frame.left && y == frame.top))) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800981 //Slog.i(TAG_WM, "Applying alpha transform");
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700982 if (selfTransformation) {
983 mShownAlpha *= mTransformation.getAlpha();
984 }
985 if (attachedTransformation != null) {
986 mShownAlpha *= attachedTransformation.getAlpha();
987 }
988 if (appTransformation != null) {
989 mShownAlpha *= appTransformation.getAlpha();
Winson Chung399f6202014-03-19 10:47:20 -0700990 if (appTransformation.hasClipRect()) {
991 mClipRect.set(appTransformation.getClipRect());
992 mHasClipRect = true;
993 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700994 }
995 if (screenAnimation) {
Craig Mautnera91f9e22012-09-14 16:22:08 -0700996 mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha();
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700997 }
998 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800999 //Slog.i(TAG_WM, "Not applying alpha transform");
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001000 }
1001
Filip Gruszczynski19723a42015-11-25 15:01:48 -08001002 if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV)
Craig Mautnera91f9e22012-09-14 16:22:08 -07001003 && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v(
1004 TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha
1005 + " self=" + (selfTransformation ? mTransformation.getAlpha() : "null")
1006 + " attached=" + (attachedTransformation == null ?
1007 "null" : attachedTransformation.getAlpha())
1008 + " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha())
1009 + " screen=" + (screenAnimation ?
1010 screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001011 return;
Filip Gruszczynski4501d232015-09-02 13:00:02 -07001012 } else if (mIsWallpaper && mService.mWindowPlacerLocked.mWallpaperActionPending) {
Craig Mautner4d7349b2012-04-20 14:52:47 -07001013 return;
Chong Zhang3005e752015-09-18 18:46:28 -07001014 } else if (mWin.isDragResizeChanged()) {
1015 // This window is awaiting a relayout because user just started (or ended)
1016 // drag-resizing. The shown frame (which affects surface size and pos)
1017 // should not be updated until we get next finished draw with the new surface.
1018 // Otherwise one or two frames rendered with old settings would be displayed
1019 // with new geometry.
1020 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001021 }
1022
Filip Gruszczynski19723a42015-11-25 15:01:48 -08001023 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautner4d7349b2012-04-20 14:52:47 -07001024 TAG, "computeShownFrameLocked: " + this +
1025 " not attached, mAlpha=" + mAlpha);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001026
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001027 MagnificationSpec spec = null;
1028 //TODO (multidisplay): Magnification is supported only for the default display.
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001029 if (mService.mAccessibilityController != null && displayId == DEFAULT_DISPLAY) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001030 spec = mService.mAccessibilityController.getMagnificationSpecForWindowLocked(mWin);
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001031 }
Craig Mautner165be0c2015-01-27 15:16:58 -08001032 if (spec != null) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001033 final Rect frame = mWin.mFrame;
1034 final float tmpFloats[] = mService.mTmpFloats;
1035 final Matrix tmpMatrix = mWin.mTmpMatrix;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001036
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001037 tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
1038 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001039
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001040 if (spec != null && !spec.isNop()) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001041 tmpMatrix.postScale(spec.scale, spec.scale);
1042 tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001043 }
1044
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001045 tmpMatrix.getValues(tmpFloats);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001046
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001047 mHaveMatrix = true;
1048 mDsDx = tmpFloats[Matrix.MSCALE_X];
1049 mDtDx = tmpFloats[Matrix.MSKEW_Y];
1050 mDsDy = tmpFloats[Matrix.MSKEW_X];
1051 mDtDy = tmpFloats[Matrix.MSCALE_Y];
1052 float x = tmpFloats[Matrix.MTRANS_X];
1053 float y = tmpFloats[Matrix.MTRANS_Y];
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001054 mWin.mShownPosition.set((int) x, (int) y);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001055
1056 mShownAlpha = mAlpha;
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001057 } else {
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001058 mWin.mShownPosition.set(mWin.mFrame.left, mWin.mFrame.top);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001059 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001060 mWin.mShownPosition.offset(mWin.mXOffset, mWin.mYOffset);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001061 }
1062 mShownAlpha = mAlpha;
1063 mHaveMatrix = false;
1064 mDsDx = mWin.mGlobalScale;
1065 mDtDx = 0;
1066 mDsDy = 0;
1067 mDtDy = mWin.mGlobalScale;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001068 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001069 }
1070
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001071 private void calculateSystemDecorRect() {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001072 final WindowState w = mWin;
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001073 final Rect decorRect = w.mDecorFrame;
Alan Viverette49a22e82014-07-12 20:01:27 -07001074 final int width = w.mFrame.width();
1075 final int height = w.mFrame.height();
Alan Viveretteccb11e12014-07-08 16:04:02 -07001076
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001077 // Compute the offset of the window in relation to the decor rect.
Alan Viverette49a22e82014-07-12 20:01:27 -07001078 final int left = w.mXOffset + w.mFrame.left;
1079 final int top = w.mYOffset + w.mFrame.top;
Alan Viveretteccb11e12014-07-08 16:04:02 -07001080
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001081 // Initialize the decor rect to the entire frame.
Jorim Jaggidc249c42015-12-15 14:57:31 -08001082 if (w.isDragResizing() && w.getResizeMode() == DRAG_RESIZE_MODE_DOCKED_DIVIDER) {
1083
1084 // If we are resizing with the divider, the task bounds might be smaller than the
1085 // stack bounds. The system decor is used to clip to the task bounds, which we don't
1086 // want in this case in order to avoid holes.
1087 final DisplayInfo displayInfo = w.getDisplayContent().getDisplayInfo();
1088 mSystemDecorRect.set(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
1089 } else {
1090 mSystemDecorRect.set(0, 0, width, height);
1091 }
Alan Viveretteccb11e12014-07-08 16:04:02 -07001092
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001093 // If a freeform window is animating from a position where it would be cutoff, it would be
1094 // cutoff during the animation. We don't want that, so for the duration of the animation
1095 // we ignore the decor cropping and depend on layering to position windows correctly.
1096 final boolean cropToDecor = !(w.inFreeformWorkspace() && w.isAnimatingLw());
1097 if (cropToDecor) {
1098 // Intersect with the decor rect, offsetted by window position.
1099 mSystemDecorRect.intersect(decorRect.left - left, decorRect.top - top,
1100 decorRect.right - left, decorRect.bottom - top);
1101 }
Alan Viveretteccb11e12014-07-08 16:04:02 -07001102
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001103 // If size compatibility is being applied to the window, the
1104 // surface is scaled relative to the screen. Also apply this
1105 // scaling to the crop rect. We aren't using the standard rect
1106 // scale function because we want to round things to make the crop
1107 // always round to a larger rect to ensure we don't crop too
1108 // much and hide part of the window that should be seen.
1109 if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) {
1110 final float scale = w.mInvGlobalScale;
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001111 mSystemDecorRect.left = (int) (mSystemDecorRect.left * scale - 0.5f);
1112 mSystemDecorRect.top = (int) (mSystemDecorRect.top * scale - 0.5f);
1113 mSystemDecorRect.right = (int) ((mSystemDecorRect.right+1) * scale - 0.5f);
1114 mSystemDecorRect.bottom = (int) ((mSystemDecorRect.bottom+1) * scale - 0.5f);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001115 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001116 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001117
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001118 void updateSurfaceWindowCrop(final boolean recoveringMemory) {
1119 final WindowState w = mWin;
Craig Mautnerdf88d732014-01-27 09:21:32 -08001120 final DisplayContent displayContent = w.getDisplayContent();
1121 if (displayContent == null) {
1122 return;
1123 }
Wale Ogunwaleb3eba812015-07-30 20:20:16 -07001124 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001125 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Updating crop for window: " + w + ", " + "mLastCrop=" +
1126 mLastClipRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001127
1128 // Need to recompute a new system decor rect each time.
Robert Carr58f29132015-10-29 14:19:05 -07001129 if (!w.isDefaultDisplay()) {
Jeff Brown28e0b092012-09-25 11:31:28 -07001130 // On a different display there is no system decor. Crop the window
1131 // by the screen boundaries.
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001132 mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
1133 mSystemDecorRect.intersect(-w.mCompatFrame.left, -w.mCompatFrame.top,
Jeff Brown28e0b092012-09-25 11:31:28 -07001134 displayInfo.logicalWidth - w.mCompatFrame.left,
1135 displayInfo.logicalHeight - w.mCompatFrame.top);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001136 } else if (w.mLayer >= mService.mSystemDecorLayer) {
1137 // Above the decor layer is easy, just use the entire window.
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001138 mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
Craig Mautner165be0c2015-01-27 15:16:58 -08001139 } else if (w.mDecorFrame.isEmpty()) {
1140 // Windows without policy decor aren't cropped.
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001141 mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001142 } else if (w.mAttrs.type == LayoutParams.TYPE_WALLPAPER && mAnimator.isAnimating()) {
Adrian Roosd47ad032014-08-15 23:17:05 +02001143 // If we're animating, the wallpaper crop should only be updated at the end of the
1144 // animation.
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001145 mTmpClipRect.set(mSystemDecorRect);
1146 calculateSystemDecorRect();
1147 mSystemDecorRect.union(mTmpClipRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001148 } else {
John Spurlock46646232013-09-30 22:32:42 -04001149 // Crop to the system decor specified by policy.
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001150 calculateSystemDecorRect();
1151 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Applying decor to crop for " + w + ", mDecorFrame="
1152 + w.mDecorFrame + ", mSystemDecorRect=" + mSystemDecorRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001153 }
1154
Chong Zhang0abb20f2015-11-19 14:17:31 -08001155 final boolean fullscreen = w.isFrameFullscreen(displayInfo);
Jorim Jaggi253a20f2015-11-03 12:38:42 +01001156 final boolean isFreeformResizing =
Jorim Jaggidcf467c2015-11-05 13:59:32 +01001157 w.isDragResizing() && w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM;
Alan Viverette49a22e82014-07-12 20:01:27 -07001158 final Rect clipRect = mTmpClipRect;
Robert Carr51a1b872015-12-08 14:03:13 -08001159
1160 // We use the clip rect as provided by the tranformation for non-fullscreen windows to
1161 // avoid premature clipping with the system decor rect.
1162 clipRect.set((mHasClipRect && !fullscreen) ? mClipRect : mSystemDecorRect);
1163 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Initial clip rect: " + clipRect + ", mHasClipRect="
1164 + mHasClipRect + ", fullscreen=" + fullscreen);
1165
1166 if (isFreeformResizing && !w.isChildWindow()) {
1167 // For freeform resizing non child windows, we are using the big surface positioned
1168 // at 0,0. Thus we must express the crop in that coordinate space.
1169 clipRect.offset(w.mShownPosition.x, w.mShownPosition.y);
Chong Zhang09b21ef2015-09-14 10:20:21 -07001170 }
Robert Carr51a1b872015-12-08 14:03:13 -08001171
Alan Viverette49a22e82014-07-12 20:01:27 -07001172 // Expand the clip rect for surface insets.
1173 final WindowManager.LayoutParams attrs = w.mAttrs;
1174 clipRect.left -= attrs.surfaceInsets.left;
1175 clipRect.top -= attrs.surfaceInsets.top;
1176 clipRect.right += attrs.surfaceInsets.right;
1177 clipRect.bottom += attrs.surfaceInsets.bottom;
1178
Wale Ogunwaleb3eba812015-07-30 20:20:16 -07001179 if (mHasClipRect && fullscreen) {
1180 // We intersect the clip rect specified by the transformation with the expanded system
1181 // decor rect to prevent artifacts from drawing during animation if the transformation
1182 // clip rect extends outside the system decor rect.
1183 clipRect.intersect(mClipRect);
1184 }
Alan Viverette49a22e82014-07-12 20:01:27 -07001185 // The clip rect was generated assuming (0,0) as the window origin,
1186 // so we need to translate to match the actual surface coordinates.
1187 clipRect.offset(attrs.surfaceInsets.left, attrs.surfaceInsets.top);
Robert Carr58f29132015-10-29 14:19:05 -07001188
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001189 adjustCropToStackBounds(w, clipRect, isFreeformResizing);
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001190 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Clip rect after stack adjustment=" + mClipRect);
Robert Carr58f29132015-10-29 14:19:05 -07001191
1192 w.transformFromScreenToSurfaceSpace(clipRect);
1193
Jorim Jaggi5e6968d2016-02-19 18:02:13 -08001194 // See {@link WindowState#notifyMovedInStack} for why this is necessary.
1195 if (w.hasJustMovedInStack() && mLastClipRect.isEmpty() && !clipRect.isEmpty()) {
1196 clipRect.setEmpty();
1197 }
1198
Winson Chung399f6202014-03-19 10:47:20 -07001199 if (!clipRect.equals(mLastClipRect)) {
1200 mLastClipRect.set(clipRect);
Robert Carre6a83512015-11-03 16:09:21 -08001201 mSurfaceController.setCropInTransaction(clipRect, recoveringMemory);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001202 }
1203 }
1204
Jorim Jaggi253a20f2015-11-03 12:38:42 +01001205 private void adjustCropToStackBounds(WindowState w, Rect clipRect, boolean isFreeformResizing) {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001206 final Task task = w.getTask();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001207 if (task == null || !task.cropWindowsToStackBounds()) {
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001208 return;
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -07001209 }
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001210
1211 // We don't apply the stack bounds crop if:
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001212 // 1. The window is currently animating docked mode or in freeform mode, otherwise the
1213 // animating window will be suddenly (docked) or for whole animation (freeform) cut off.
Chong Zhang50bf96b2015-12-28 11:09:01 -08001214 // (Note that we still need to apply the crop if the task being docked is non-resizeable,
1215 // in which case the task is running in fullscreen size but cropped to stack bounds.)
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001216 // 2. The window that is being replaced during animation, because it was living in a
1217 // different stack. If we suddenly crop it to the new stack bounds, it might get cut off.
1218 // We don't want it to happen, so we let it ignore the stack bounds until it gets removed.
1219 // The window that will replace it will abide them.
Chong Zhang50bf96b2015-12-28 11:09:01 -08001220 if (isAnimating() && (w.mWillReplaceWindow
1221 || (w.inDockedWorkspace() && task.isResizeable())
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001222 || w.inFreeformWorkspace())) {
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001223 return;
1224 }
1225
Chong Zhangbcb8d822016-02-26 16:21:02 -08001226 final WindowState winShowWhenLocked = (WindowState) mPolicy.getWinShowWhenLockedLw();
1227 if (w == winShowWhenLocked) {
1228 return;
1229 }
1230
Wale Ogunwale69cf50f2015-11-13 11:08:36 -08001231 final TaskStack stack = task.mStack;
1232 stack.getDimBounds(mTmpStackBounds);
1233 // When we resize we use the big surface approach, which means we can't trust the
1234 // window frame bounds anymore. Instead, the window will be placed at 0, 0, but to avoid
1235 // hardcoding it, we use surface coordinates.
1236 final int frameX = isFreeformResizing ? (int) mSurfaceController.getX() :
1237 w.mFrame.left + mWin.mXOffset - w.getAttrs().surfaceInsets.left;
1238 final int frameY = isFreeformResizing ? (int) mSurfaceController.getY() :
1239 w.mFrame.top + mWin.mYOffset - w.getAttrs().surfaceInsets.top;
1240 // We need to do some acrobatics with surface position, because their clip region is
1241 // relative to the inside of the surface, but the stack bounds aren't.
1242 clipRect.left = Math.max(0,
1243 Math.max(mTmpStackBounds.left, frameX + clipRect.left) - frameX);
1244 clipRect.top = Math.max(0,
1245 Math.max(mTmpStackBounds.top, frameY + clipRect.top) - frameY);
1246 clipRect.right = Math.max(0,
1247 Math.min(mTmpStackBounds.right, frameX + clipRect.right) - frameX);
1248 clipRect.bottom = Math.max(0,
1249 Math.min(mTmpStackBounds.bottom, frameY + clipRect.bottom) - frameY);
Filip Gruszczynski4b8eea72015-09-14 18:16:19 -07001250 }
1251
Wale Ogunwale4c8b7952015-04-07 10:49:40 -07001252 void setSurfaceBoundariesLocked(final boolean recoveringMemory) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001253 final WindowState w = mWin;
Alan Viveretteccb11e12014-07-08 16:04:02 -07001254
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001255 mTmpSize.set(w.mShownPosition.x, w.mShownPosition.y, 0, 0);
1256 calculateSurfaceBounds(w, w.getAttrs());
Chong Zhang0275e392015-09-17 10:41:44 -07001257
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001258 mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top, recoveringMemory);
Robert Carre1034cc32016-02-01 13:08:15 -08001259
1260 mSurfaceController.setMatrixInTransaction(mDsDx * w.mHScale, mDtDx * w.mVScale,
1261 mDsDy * w.mHScale, mDtDy * w.mVScale, recoveringMemory);
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001262 mSurfaceResized = mSurfaceController.setSizeInTransaction(
1263 mTmpSize.width(), mTmpSize.height(),
Robert Carre6a83512015-11-03 16:09:21 -08001264 recoveringMemory);
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001265
Robert Carre6a83512015-11-03 16:09:21 -08001266 if (mSurfaceResized) {
Chong Zhang5b2f1992015-11-13 15:40:36 -08001267 mReportSurfaceResized = true;
Robert Carre6a83512015-11-03 16:09:21 -08001268 mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1269 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
1270 w.applyDimLayerIfNeeded();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001271 }
Dianne Hackborn85afd1b2012-05-13 13:31:06 -07001272
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001273 updateSurfaceWindowCrop(recoveringMemory);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001274 }
1275
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001276 void prepareSurfaceLocked(final boolean recoveringMemory) {
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001277 final WindowState w = mWin;
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001278 if (!hasSurface()) {
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001279 if (w.mOrientationChanging) {
1280 if (DEBUG_ORIENTATION) {
1281 Slog.v(TAG, "Orientation change skips hidden " + w);
1282 }
1283 w.mOrientationChanging = false;
1284 }
1285 return;
1286 }
1287
1288 boolean displayed = false;
1289
1290 computeShownFrameLocked();
1291
Craig Mautnera91f9e22012-09-14 16:22:08 -07001292 setSurfaceBoundariesLocked(recoveringMemory);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001293
Craig Mautner918b53b2012-07-09 14:15:54 -07001294 if (mIsWallpaper && !mWin.mWallpaperVisible) {
Craig Mautner0fa77c12012-06-11 15:57:19 -07001295 // Wallpaper is no longer visible and there is no wp target => hide it.
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001296 hide("prepareSurfaceLocked");
Craig Mautner72669d12012-12-18 17:23:54 -08001297 } else if (w.mAttachedHidden || !w.isOnScreen()) {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -08001298 hide("prepareSurfaceLocked");
Wale Ogunwalee8069dc2015-08-18 09:52:01 -07001299 mWallpaperControllerLocked.hideWallpapers(w);
Craig Mautnerb9836b92012-06-11 11:40:09 -07001300
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001301 // If we are waiting for this window to handle an
1302 // orientation change, well, it is hidden, so
1303 // doesn't really matter. Note that this does
1304 // introduce a potential glitch if the window
1305 // becomes unhidden before it has drawn for the
1306 // new orientation.
1307 if (w.mOrientationChanging) {
1308 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001309 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001310 "Orientation change skips hidden " + w);
1311 }
1312 } else if (mLastLayer != mAnimLayer
1313 || mLastAlpha != mShownAlpha
1314 || mLastDsDx != mDsDx
1315 || mLastDtDx != mDtDx
1316 || mLastDsDy != mDsDy
1317 || mLastDtDy != mDtDy
1318 || w.mLastHScale != w.mHScale
1319 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -07001320 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001321 displayed = true;
1322 mLastAlpha = mShownAlpha;
1323 mLastLayer = mAnimLayer;
1324 mLastDsDx = mDsDx;
1325 mLastDtDx = mDtDx;
1326 mLastDsDy = mDsDy;
1327 mLastDtDy = mDtDy;
1328 w.mLastHScale = w.mHScale;
1329 w.mLastVScale = w.mVScale;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001330 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
Robert Carre6a83512015-11-03 16:09:21 -08001331 "controller=" + mSurfaceController +
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001332 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
Craig Mautner78505d82014-09-02 14:36:31 -07001333 + " matrix=[" + mDsDx + "*" + w.mHScale
1334 + "," + mDtDx + "*" + w.mVScale
1335 + "][" + mDsDy + "*" + w.mHScale
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -08001336 + "," + mDtDy + "*" + w.mVScale + "]", false);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001337
Robert Carre6a83512015-11-03 16:09:21 -08001338 boolean prepared =
1339 mSurfaceController.prepareToShowInTransaction(mShownAlpha, mAnimLayer,
1340 mDsDx * w.mHScale, mDtDx * w.mVScale,
1341 mDsDy * w.mHScale, mDtDy * w.mVScale,
1342 recoveringMemory);
1343
1344 if (prepared && mLastHidden && mDrawState == HAS_DRAWN) {
1345 if (showSurfaceRobustlyLocked()) {
Chong Zhangb9b0fec2016-02-11 18:51:51 -08001346 if (mDestroyPreservedSurfaceUponRedraw) {
1347 mService.mDestroyPreservedSurface.add(mWin);
1348 }
Filip Gruszczynski78a08ee2015-11-08 18:04:32 -08001349 mAnimator.requestRemovalOfReplacedWindows(w);
Robert Carre6a83512015-11-03 16:09:21 -08001350 mLastHidden = false;
1351 if (mIsWallpaper) {
1352 mWallpaperControllerLocked.dispatchWallpaperVisibility(w, true);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001353 }
Robert Carre6a83512015-11-03 16:09:21 -08001354 // This draw means the difference between unique content and mirroring.
1355 // Run another pass through performLayout to set mHasContent in the
1356 // LogicalDisplay.
1357 mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1358 WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
1359 } else {
1360 w.mOrientationChanging = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001361 }
1362 }
Chong Zhangeb22e8e2016-01-20 19:52:22 -08001363 if (hasSurface()) {
Robert Carre6a83512015-11-03 16:09:21 -08001364 w.mToken.hasVisible = true;
1365 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001366 } else {
Craig Mautner9e809442012-06-22 17:13:04 -07001367 if (DEBUG_ANIM && isAnimating()) {
Chong Zhange05db742016-02-16 16:58:37 -08001368 Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
Craig Mautner83339b42012-05-01 22:13:23 -07001369 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001370 displayed = true;
1371 }
1372
1373 if (displayed) {
1374 if (w.mOrientationChanging) {
1375 if (!w.isDrawnLw()) {
Craig Mautner2639da52012-07-09 09:39:06 -07001376 mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE;
Dianne Hackborna57c6952013-03-29 14:46:40 -07001377 mAnimator.mLastWindowFreezeSource = w;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001378 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001379 "Orientation continue waiting for draw in " + w);
1380 } else {
1381 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001382 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001383 }
1384 }
1385 w.mToken.hasVisible = true;
Chong Zhangf66db432016-01-13 10:39:51 -08001386
1387 final Task task = w.getTask();
1388 if (task != null) {
1389 task.scheduleShowNonResizeableDockToastIfNeeded();
1390 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001391 }
1392 }
1393
Craig Mautneref655012013-01-03 11:20:24 -08001394 void setTransparentRegionHintLocked(final Region region) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001395 if (mSurfaceController == null) {
1396 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1397 return;
1398 }
Robert Carre6a83512015-11-03 16:09:21 -08001399 mSurfaceController.setTransparentRegionHint(region);
Craig Mautner48ba1e72012-04-02 13:18:16 -07001400 }
1401
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001402 void setWallpaperOffset(Point shownPosition) {
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001403 final LayoutParams attrs = mWin.getAttrs();
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -07001404 final int left = shownPosition.x - attrs.surfaceInsets.left;
1405 final int top = shownPosition.y - attrs.surfaceInsets.top;
Robert Carre6a83512015-11-03 16:09:21 -08001406
1407 try {
Craig Mautner71dd1b62014-02-18 15:48:52 -08001408 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setWallpaperOffset");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001409 SurfaceControl.openTransaction();
Robert Carre6a83512015-11-03 16:09:21 -08001410 mSurfaceController.setPositionInTransaction(mWin.mFrame.left + left,
1411 mWin.mFrame.top + top, false);
1412 updateSurfaceWindowCrop(false);
1413 } catch (RuntimeException e) {
1414 Slog.w(TAG, "Error positioning surface of " + mWin
1415 + " pos=(" + left + "," + top + ")", e);
1416 } finally {
1417 SurfaceControl.closeTransaction();
1418 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1419 "<<< CLOSE TRANSACTION setWallpaperOffset");
Craig Mautner48ba1e72012-04-02 13:18:16 -07001420 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001421 }
1422
John Reck80181b92015-05-19 11:09:32 -07001423 /**
1424 * Try to change the pixel format without recreating the surface. This
1425 * will be common in the case of changing from PixelFormat.OPAQUE to
1426 * PixelFormat.TRANSLUCENT in the hardware-accelerated case as both
1427 * requested formats resolve to the same underlying SurfaceControl format
1428 * @return True if format was succesfully changed, false otherwise
1429 */
1430 boolean tryChangeFormatInPlaceLocked() {
Robert Carre6a83512015-11-03 16:09:21 -08001431 if (mSurfaceController == null) {
John Reck80181b92015-05-19 11:09:32 -07001432 return false;
1433 }
1434 final LayoutParams attrs = mWin.getAttrs();
Wale Ogunwalee7bf46b2015-09-30 09:19:28 -07001435 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;
John Reck80181b92015-05-19 11:09:32 -07001436 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
1437 if (format == mSurfaceFormat) {
1438 setOpaqueLocked(!PixelFormat.formatHasAlpha(attrs.format));
1439 return true;
1440 }
1441 return false;
1442 }
1443
Craig Mautner6f612042014-09-07 13:13:23 -07001444 void setOpaqueLocked(boolean isOpaque) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001445 if (mSurfaceController == null) {
1446 return;
1447 }
Robert Carre6a83512015-11-03 16:09:21 -08001448 mSurfaceController.setOpaque(isOpaque);
Craig Mautner71dd1b62014-02-18 15:48:52 -08001449 }
1450
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001451 void setSecureLocked(boolean isSecure) {
Wale Ogunwalef9e09782015-11-09 12:42:37 -08001452 if (mSurfaceController == null) {
1453 return;
1454 }
Robert Carre6a83512015-11-03 16:09:21 -08001455 mSurfaceController.setSecure(isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001456 }
1457
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001458 // This must be called while inside a transaction.
1459 boolean performShowLocked() {
Craig Mautner5962b122012-10-05 14:45:52 -07001460 if (mWin.isHiddenFromUserLocked()) {
Wale Ogunwale4eef94f2015-07-17 22:29:54 -07001461 if (DEBUG_VISIBILITY) Slog.w(TAG, "hiding " + mWin + ", belonging to " + mWin.mOwnerUid);
1462 mWin.hideLw(false);
Craig Mautner9dc52bc2012-08-06 14:15:42 -07001463 return false;
1464 }
Craig Mautner6fbda632012-07-03 09:26:39 -07001465 if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
1466 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
Craig Mautnerd87946b2012-03-29 18:00:19 -07001467 Slog.v(TAG, "performShow on " + this
Craig Mautnerc431e892015-02-11 13:14:26 -08001468 + ": mDrawState=" + drawStateToString() + " readyForDisplay="
Dianne Hackborn6e2281d2012-06-19 17:48:32 -07001469 + mWin.isReadyForDisplayIgnoringKeyguard()
Craig Mautner6fbda632012-07-03 09:26:39 -07001470 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING)
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001471 + " during animation: policyVis=" + mWin.mPolicyVisibility
1472 + " attHidden=" + mWin.mAttachedHidden
1473 + " tok.hiddenRequested="
1474 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1475 + " tok.hidden="
1476 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1477 + " animating=" + mAnimating
1478 + " tok animating="
Craig Mautnerc431e892015-02-11 13:14:26 -08001479 + (mAppAnimator != null ? mAppAnimator.animating : false) + " Callers="
1480 + Debug.getCallers(3));
Craig Mautner6fbda632012-07-03 09:26:39 -07001481 }
1482 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplayIgnoringKeyguard()) {
Craig Mautner6fbda632012-07-03 09:26:39 -07001483 if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
1484 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
1485 Slog.v(TAG, "Showing " + this
1486 + " during animation: policyVis=" + mWin.mPolicyVisibility
1487 + " attHidden=" + mWin.mAttachedHidden
1488 + " tok.hiddenRequested="
1489 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1490 + " tok.hidden="
1491 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1492 + " animating=" + mAnimating
1493 + " tok animating="
Craig Mautner322e4032012-07-13 13:35:20 -07001494 + (mAppAnimator != null ? mAppAnimator.animating : false));
Craig Mautner6fbda632012-07-03 09:26:39 -07001495 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001496
1497 mService.enableScreenIfNeededLocked();
1498
1499 applyEnterAnimationLocked();
1500
Craig Mautnerde6198e2012-04-19 09:59:31 -07001501 // Force the show in the next prepareSurfaceLocked() call.
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001502 mLastAlpha = -1;
Craig Mautneref25d7a2012-05-15 23:01:47 -07001503 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
Chong Zhang5b2f1992015-11-13 15:40:36 -08001504 Slog.v(TAG, "performShowLocked: mDrawState=HAS_DRAWN in " + mWin);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001505 mDrawState = HAS_DRAWN;
Craig Mautner96868332012-12-04 14:29:11 -08001506 mService.scheduleAnimationLocked();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001507
1508 int i = mWin.mChildWindows.size();
1509 while (i > 0) {
1510 i--;
1511 WindowState c = mWin.mChildWindows.get(i);
1512 if (c.mAttachedHidden) {
1513 c.mAttachedHidden = false;
Robert Carre6a83512015-11-03 16:09:21 -08001514 if (c.mWinAnimator.mSurfaceController != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001515 c.mWinAnimator.performShowLocked();
Filip Gruszczynski921171a2015-09-30 22:02:57 +00001516 // It hadn't been shown, which means layout not
1517 // performed on it, so now we want to make sure to
1518 // do a layout. If called from within the transaction
1519 // loop, this will cause it to restart with a new
1520 // layout.
1521 final DisplayContent displayContent = c.getDisplayContent();
1522 if (displayContent != null) {
1523 displayContent.layoutNeeded = true;
1524 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001525 }
1526 }
1527 }
1528
Filip Gruszczynskib6e66622015-10-25 16:05:27 -07001529 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING && mWin.mAppToken != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001530 mWin.mAppToken.onFirstWindowDrawn(mWin, this);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001531 }
Chong Zhangc806d902015-11-30 09:44:27 -08001532
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001533 return true;
1534 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001535 return false;
1536 }
1537
1538 /**
1539 * Have the surface flinger show a surface, robustly dealing with
1540 * error conditions. In particular, if there is not enough memory
1541 * to show the surface, then we will try to get rid of other surfaces
1542 * in order to succeed.
1543 *
1544 * @return Returns true if the surface was successfully shown.
1545 */
Filip Gruszczynski24966d42015-09-05 15:00:00 -07001546 private boolean showSurfaceRobustlyLocked() {
Robert Carre6a83512015-11-03 16:09:21 -08001547 boolean shown = mSurfaceController.showRobustlyInTransaction();
1548 if (!shown)
1549 return false;
1550
1551 if (mWin.mTurnOnScreen) {
1552 if (DEBUG_VISIBILITY) Slog.v(TAG, "Show surface turning screen on: " + mWin);
1553 mWin.mTurnOnScreen = false;
1554 mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001555 }
Robert Carre6a83512015-11-03 16:09:21 -08001556 return true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001557 }
1558
1559 void applyEnterAnimationLocked() {
1560 final int transit;
1561 if (mEnterAnimationPending) {
1562 mEnterAnimationPending = false;
1563 transit = WindowManagerPolicy.TRANSIT_ENTER;
1564 } else {
1565 transit = WindowManagerPolicy.TRANSIT_SHOW;
1566 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001567 applyAnimationLocked(transit, true);
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001568 //TODO (multidisplay): Magnification is supported only for the default display.
Svetoslav8e3feb12014-02-24 13:46:47 -08001569 if (mService.mAccessibilityController != null
Filip Gruszczynski69cbc352015-11-11 13:46:04 -08001570 && mWin.getDisplayId() == DEFAULT_DISPLAY) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001571 mService.mAccessibilityController.onWindowTransitionLocked(mWin, transit);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001572 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001573 }
1574
1575 /**
1576 * Choose the correct animation and set it to the passed WindowState.
Craig Mautner4b71aa12012-12-27 17:20:01 -08001577 * @param transit If AppTransition.TRANSIT_PREVIEW_DONE and the app window has been drawn
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001578 * then the animation will be app_starting_exit. Any other value loads the animation from
1579 * the switch statement below.
1580 * @param isEntrance The animation type the last time this was called. Used to keep from
1581 * loading the same animation twice.
1582 * @return true if an animation has been loaded.
1583 */
1584 boolean applyAnimationLocked(int transit, boolean isEntrance) {
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001585 if ((mLocalAnimating && mAnimationIsEntrance == isEntrance)
1586 || mKeyguardGoingAwayAnimation) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001587 // If we are trying to apply an animation, but already running
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001588 // an animation of the same type, then just leave that one alone.
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001589
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001590 // If we are in a keyguard exit animation, and the window should animate away, modify
1591 // keyguard exit animation such that it also fades out.
1592 if (mAnimation != null && mKeyguardGoingAwayAnimation
1593 && transit == WindowManagerPolicy.TRANSIT_PREVIEW_DONE) {
1594 applyFadeoutDuringKeyguardExitAnimation();
1595 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001596 return true;
1597 }
1598
1599 // Only apply an animation if the display isn't frozen. If it is
1600 // frozen, there is no reason to animate and it can cause strange
1601 // artifacts when we unfreeze the display if some different animation
1602 // is running.
1603 if (mService.okToDisplay()) {
1604 int anim = mPolicy.selectAnimationLw(mWin, transit);
1605 int attr = -1;
1606 Animation a = null;
1607 if (anim != 0) {
Dianne Hackborn4c1e3182012-10-05 18:37:54 -07001608 a = anim != -1 ? AnimationUtils.loadAnimation(mContext, anim) : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001609 } else {
1610 switch (transit) {
1611 case WindowManagerPolicy.TRANSIT_ENTER:
1612 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1613 break;
1614 case WindowManagerPolicy.TRANSIT_EXIT:
1615 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1616 break;
1617 case WindowManagerPolicy.TRANSIT_SHOW:
1618 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1619 break;
1620 case WindowManagerPolicy.TRANSIT_HIDE:
1621 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1622 break;
1623 }
1624 if (attr >= 0) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -07001625 a = mService.mAppTransition.loadAnimationAttr(mWin.mAttrs, attr);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001626 }
1627 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001628 if (DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001629 "applyAnimation: win=" + this
1630 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
Craig Mautner4d7349b2012-04-20 14:52:47 -07001631 + " a=" + a
Craig Mautner8863cca2012-09-18 15:04:34 -07001632 + " transit=" + transit
Craig Mautner83339b42012-05-01 22:13:23 -07001633 + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001634 if (a != null) {
Filip Gruszczynski198dcbf2016-01-18 10:02:00 -08001635 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001636 setAnimation(a);
1637 mAnimationIsEntrance = isEntrance;
1638 }
1639 } else {
1640 clearAnimation();
1641 }
1642
1643 return mAnimation != null;
1644 }
1645
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001646 private void applyFadeoutDuringKeyguardExitAnimation() {
1647 long startTime = mAnimation.getStartTime();
1648 long duration = mAnimation.getDuration();
1649 long elapsed = mLastAnimationTime - startTime;
1650 long fadeDuration = duration - elapsed;
1651 if (fadeDuration <= 0) {
1652 // Never mind, this would be no visible animation, so abort the animation change.
1653 return;
1654 }
1655 AnimationSet newAnimation = new AnimationSet(false /* shareInterpolator */);
1656 newAnimation.setDuration(duration);
1657 newAnimation.setStartTime(startTime);
1658 newAnimation.addAnimation(mAnimation);
1659 Animation fadeOut = AnimationUtils.loadAnimation(
1660 mContext, com.android.internal.R.anim.app_starting_exit);
1661 fadeOut.setDuration(fadeDuration);
1662 fadeOut.setStartOffset(elapsed);
1663 newAnimation.addAnimation(fadeOut);
Filip Gruszczynskif52dd202015-11-15 20:36:38 -08001664 newAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(), mAnimDx, mAnimDy);
Jorim Jaggif8d77da2014-11-11 16:59:12 +01001665 mAnimation = newAnimation;
1666 }
1667
Craig Mautnera2c77052012-03-26 12:14:43 -07001668 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1669 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1670 || mAnimation != null) {
1671 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1672 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1673 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1674 pw.print(" mAnimation="); pw.println(mAnimation);
1675 }
1676 if (mHasTransformation || mHasLocalTransformation) {
1677 pw.print(prefix); pw.print("XForm: has=");
1678 pw.print(mHasTransformation);
1679 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1680 pw.print(" "); mTransformation.printShortString(pw);
1681 pw.println();
1682 }
Robert Carre6a83512015-11-03 16:09:21 -08001683 if (mSurfaceController != null) {
1684 mSurfaceController.dump(pw, prefix, dumpAll);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001685 }
Robert Carre6a83512015-11-03 16:09:21 -08001686 if (dumpAll) {
1687 pw.print(prefix); pw.print("mDrawState="); pw.print(drawStateToString());
1688 pw.print(prefix); pw.print(" mLastHidden="); pw.println(mLastHidden);
Filip Gruszczynskif34a04c2015-12-07 15:05:49 -08001689 pw.print(prefix); pw.print("mSystemDecorRect="); mSystemDecorRect.printShortString(pw);
1690 pw.print(" last="); mLastSystemDecorRect.printShortString(pw);
1691 if (mHasClipRect) {
1692 pw.print(" mLastClipRect="); mLastClipRect.printShortString(pw);
1693 }
1694 pw.println();
Robert Carre6a83512015-11-03 16:09:21 -08001695 }
1696
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001697 if (mPendingDestroySurface != null) {
1698 pw.print(prefix); pw.print("mPendingDestroySurface=");
1699 pw.println(mPendingDestroySurface);
1700 }
1701 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1702 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1703 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1704 }
1705 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1706 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1707 pw.print(" mAlpha="); pw.print(mAlpha);
1708 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1709 }
1710 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1711 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1712 pw.print(" mDsDx="); pw.print(mDsDx);
1713 pw.print(" mDtDx="); pw.print(mDtDx);
1714 pw.print(" mDsDy="); pw.print(mDsDy);
1715 pw.print(" mDtDy="); pw.println(mDtDy);
1716 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001717 }
1718
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001719 @Override
1720 public String toString() {
Dianne Hackborn529e7442012-11-01 14:22:28 -07001721 StringBuffer sb = new StringBuffer("WindowStateAnimator{");
1722 sb.append(Integer.toHexString(System.identityHashCode(this)));
1723 sb.append(' ');
1724 sb.append(mWin.mAttrs.getTitle());
1725 sb.append('}');
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001726 return sb.toString();
1727 }
Robert Carre6a83512015-11-03 16:09:21 -08001728
1729 void reclaimSomeSurfaceMemory(String operation, boolean secure) {
1730 mService.reclaimSomeSurfaceMemoryLocked(this, operation, secure);
1731 }
1732
1733 boolean getShown() {
1734 if (mSurfaceController != null) {
1735 return mSurfaceController.getShown();
1736 }
1737 return false;
1738 }
1739
1740 void destroySurface() {
Wale Ogunwale722ff892016-02-18 13:37:55 -08001741 try {
1742 if (mSurfaceController != null) {
1743 mSurfaceController.destroyInTransaction();
1744 }
1745 } catch (RuntimeException e) {
1746 Slog.w(TAG, "Exception thrown when destroying surface " + this
1747 + " surface " + mSurfaceController + " session " + mSession + ": " + e);
1748 } finally {
1749 mWin.setHasSurface(false);
1750 mSurfaceController = null;
1751 mDrawState = NO_SURFACE;
1752 }
Robert Carre6a83512015-11-03 16:09:21 -08001753 }
Filip Gruszczynskif52dd202015-11-15 20:36:38 -08001754
1755 void setMoveAnimation(int left, int top) {
1756 final Animation a = AnimationUtils.loadAnimation(mContext,
1757 com.android.internal.R.anim.window_move_from_decor);
1758 setAnimation(a);
1759 mAnimDx = mWin.mLastFrame.left - left;
1760 mAnimDy = mWin.mLastFrame.top - top;
1761 mAnimateMove = true;
1762 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001763}