Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 20 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 22 | import static com.android.server.wm.proto.AnimationAdapterProto.REMOTE; |
| 23 | import static com.android.server.wm.proto.RemoteAnimationAdapterWrapperProto.TARGET; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 24 | |
| 25 | import android.graphics.Point; |
| 26 | import android.graphics.Rect; |
| 27 | import android.os.Handler; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.SystemClock; |
| 30 | import android.util.Slog; |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 31 | import android.util.proto.ProtoOutputStream; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 32 | import android.view.IRemoteAnimationFinishedCallback; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 33 | import android.view.RemoteAnimationAdapter; |
| 34 | import android.view.RemoteAnimationTarget; |
| 35 | import android.view.SurfaceControl; |
| 36 | import android.view.SurfaceControl.Transaction; |
| 37 | |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 38 | import com.android.internal.util.FastPrintWriter; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 39 | import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback; |
| 40 | |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 41 | import java.io.PrintWriter; |
| 42 | import java.io.StringWriter; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 43 | import java.util.ArrayList; |
| 44 | |
| 45 | /** |
| 46 | * Helper class to run app animations in a remote process. |
| 47 | */ |
| 48 | class RemoteAnimationController { |
| 49 | private static final String TAG = TAG_WITH_CLASS_NAME ? "RemoteAnimationController" : TAG_WM; |
| 50 | private static final long TIMEOUT_MS = 2000; |
| 51 | |
| 52 | private final WindowManagerService mService; |
| 53 | private final RemoteAnimationAdapter mRemoteAnimationAdapter; |
| 54 | private final ArrayList<RemoteAnimationAdapterWrapper> mPendingAnimations = new ArrayList<>(); |
| 55 | private final Rect mTmpRect = new Rect(); |
| 56 | private final Handler mHandler; |
Jorim Jaggi | 91f9f348 | 2018-02-14 10:58:26 -0800 | [diff] [blame] | 57 | private FinishedCallback mFinishedCallback; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 58 | |
| 59 | private final Runnable mTimeoutRunnable = () -> { |
| 60 | onAnimationFinished(); |
| 61 | invokeAnimationCancelled(); |
| 62 | }; |
| 63 | |
| 64 | RemoteAnimationController(WindowManagerService service, |
| 65 | RemoteAnimationAdapter remoteAnimationAdapter, Handler handler) { |
| 66 | mService = service; |
| 67 | mRemoteAnimationAdapter = remoteAnimationAdapter; |
| 68 | mHandler = handler; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates an animation for each individual {@link AppWindowToken}. |
| 73 | * |
| 74 | * @param appWindowToken The app to animate. |
| 75 | * @param position The position app bounds, in screen coordinates. |
| 76 | * @param stackBounds The stack bounds of the app. |
| 77 | * @return The adapter to be run on the app. |
| 78 | */ |
| 79 | AnimationAdapter createAnimationAdapter(AppWindowToken appWindowToken, Point position, |
| 80 | Rect stackBounds) { |
| 81 | final RemoteAnimationAdapterWrapper adapter = new RemoteAnimationAdapterWrapper( |
| 82 | appWindowToken, position, stackBounds); |
| 83 | mPendingAnimations.add(adapter); |
| 84 | return adapter; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Called when the transition is ready to be started, and all leashes have been set up. |
| 89 | */ |
| 90 | void goodToGo() { |
Jorim Jaggi | 93f9fe3 | 2018-01-25 15:06:13 +0100 | [diff] [blame] | 91 | if (mPendingAnimations.isEmpty()) { |
| 92 | onAnimationFinished(); |
| 93 | return; |
| 94 | } |
Jorim Jaggi | a19d781 | 2018-02-01 15:03:59 +0100 | [diff] [blame] | 95 | |
| 96 | // Scale the timeout with the animator scale the controlling app is using. |
| 97 | mHandler.postDelayed(mTimeoutRunnable, |
| 98 | (long) (TIMEOUT_MS * mService.getCurrentAnimatorScale())); |
Jorim Jaggi | 91f9f348 | 2018-02-14 10:58:26 -0800 | [diff] [blame] | 99 | mFinishedCallback = new FinishedCallback(this); |
Chavi Weingarten | 16d0d07 | 2018-02-12 23:50:28 +0000 | [diff] [blame] | 100 | |
| 101 | final RemoteAnimationTarget[] animations = createAnimations(); |
| 102 | mService.mAnimator.addAfterPrepareSurfacesRunnable(() -> { |
| 103 | try { |
| 104 | mRemoteAnimationAdapter.getRunner().onAnimationStart(animations, |
| 105 | mFinishedCallback); |
| 106 | } catch (RemoteException e) { |
| 107 | Slog.e(TAG, "Failed to start remote animation", e); |
| 108 | onAnimationFinished(); |
| 109 | } |
| 110 | }); |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 111 | sendRunningRemoteAnimation(true); |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 112 | if (DEBUG_APP_TRANSITIONS) { |
| 113 | writeStartDebugStatement(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private void writeStartDebugStatement() { |
| 118 | Slog.i(TAG, "Starting remote animation"); |
| 119 | final StringWriter sw = new StringWriter(); |
| 120 | final FastPrintWriter pw = new FastPrintWriter(sw); |
| 121 | for (int i = mPendingAnimations.size() - 1; i >= 0; i--) { |
| 122 | mPendingAnimations.get(i).dump(pw, ""); |
| 123 | } |
| 124 | pw.close(); |
| 125 | Slog.i(TAG, sw.toString()); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private RemoteAnimationTarget[] createAnimations() { |
Jorim Jaggi | 1777021 | 2018-01-22 20:01:21 +0100 | [diff] [blame] | 129 | final ArrayList<RemoteAnimationTarget> targets = new ArrayList<>(); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 130 | for (int i = mPendingAnimations.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 1777021 | 2018-01-22 20:01:21 +0100 | [diff] [blame] | 131 | final RemoteAnimationTarget target = |
| 132 | mPendingAnimations.get(i).createRemoteAppAnimation(); |
| 133 | if (target != null) { |
| 134 | targets.add(target); |
| 135 | } |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 136 | } |
Jorim Jaggi | 1777021 | 2018-01-22 20:01:21 +0100 | [diff] [blame] | 137 | return targets.toArray(new RemoteAnimationTarget[targets.size()]); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private void onAnimationFinished() { |
| 141 | mHandler.removeCallbacks(mTimeoutRunnable); |
| 142 | synchronized (mService.mWindowMap) { |
Jorim Jaggi | 91f9f348 | 2018-02-14 10:58:26 -0800 | [diff] [blame] | 143 | releaseFinishedCallback(); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 144 | mService.openSurfaceTransaction(); |
| 145 | try { |
| 146 | for (int i = mPendingAnimations.size() - 1; i >= 0; i--) { |
| 147 | final RemoteAnimationAdapterWrapper adapter = mPendingAnimations.get(i); |
| 148 | adapter.mCapturedFinishCallback.onAnimationFinished(adapter); |
| 149 | } |
| 150 | } finally { |
| 151 | mService.closeSurfaceTransaction("RemoteAnimationController#finished"); |
| 152 | } |
| 153 | } |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 154 | sendRunningRemoteAnimation(false); |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 155 | if (DEBUG_APP_TRANSITIONS) Slog.i(TAG, "Finishing remote animation"); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | private void invokeAnimationCancelled() { |
| 159 | try { |
| 160 | mRemoteAnimationAdapter.getRunner().onAnimationCancelled(); |
| 161 | } catch (RemoteException e) { |
| 162 | Slog.e(TAG, "Failed to notify cancel", e); |
| 163 | } |
| 164 | } |
| 165 | |
Jorim Jaggi | 91f9f348 | 2018-02-14 10:58:26 -0800 | [diff] [blame] | 166 | private void releaseFinishedCallback() { |
| 167 | if (mFinishedCallback != null) { |
| 168 | mFinishedCallback.release(); |
| 169 | mFinishedCallback = null; |
| 170 | } |
| 171 | } |
| 172 | |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 173 | private void sendRunningRemoteAnimation(boolean running) { |
| 174 | final int pid = mRemoteAnimationAdapter.getCallingPid(); |
| 175 | if (pid == 0) { |
| 176 | throw new RuntimeException("Calling pid of remote animation was null"); |
| 177 | } |
| 178 | mService.sendSetRunningRemoteAnimation(pid, running); |
| 179 | } |
| 180 | |
Jorim Jaggi | 91f9f348 | 2018-02-14 10:58:26 -0800 | [diff] [blame] | 181 | private static final class FinishedCallback extends IRemoteAnimationFinishedCallback.Stub { |
| 182 | |
| 183 | RemoteAnimationController mOuter; |
| 184 | |
| 185 | FinishedCallback(RemoteAnimationController outer) { |
| 186 | mOuter = outer; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void onAnimationFinished() throws RemoteException { |
| 191 | if (mOuter != null) { |
| 192 | mOuter.onAnimationFinished(); |
| 193 | |
| 194 | // In case the client holds on to the finish callback, make sure we don't leak |
| 195 | // RemoteAnimationController which in turn would leak the runner on the client. |
| 196 | mOuter = null; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Marks this callback as not be used anymore by releasing the reference to the outer class |
| 202 | * to prevent memory leak. |
| 203 | */ |
| 204 | void release() { |
| 205 | mOuter = null; |
| 206 | } |
| 207 | }; |
| 208 | |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 209 | private class RemoteAnimationAdapterWrapper implements AnimationAdapter { |
| 210 | |
| 211 | private final AppWindowToken mAppWindowToken; |
| 212 | private SurfaceControl mCapturedLeash; |
| 213 | private OnAnimationFinishedCallback mCapturedFinishCallback; |
| 214 | private final Point mPosition = new Point(); |
| 215 | private final Rect mStackBounds = new Rect(); |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 216 | private RemoteAnimationTarget mTarget; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 217 | |
| 218 | RemoteAnimationAdapterWrapper(AppWindowToken appWindowToken, Point position, |
| 219 | Rect stackBounds) { |
| 220 | mAppWindowToken = appWindowToken; |
| 221 | mPosition.set(position.x, position.y); |
| 222 | mStackBounds.set(stackBounds); |
| 223 | } |
| 224 | |
| 225 | RemoteAnimationTarget createRemoteAppAnimation() { |
Jorim Jaggi | 1777021 | 2018-01-22 20:01:21 +0100 | [diff] [blame] | 226 | final Task task = mAppWindowToken.getTask(); |
| 227 | final WindowState mainWindow = mAppWindowToken.findMainWindow(); |
| 228 | if (task == null) { |
| 229 | return null; |
| 230 | } |
| 231 | if (mainWindow == null) { |
| 232 | return null; |
| 233 | } |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 234 | mTarget = new RemoteAnimationTarget(task.mTaskId, getMode(), |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 235 | mCapturedLeash, !mAppWindowToken.fillsParent(), |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 236 | mainWindow.mWinAnimator.mLastClipRect, mainWindow.mContentInsets, |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 237 | mAppWindowToken.getPrefixOrderIndex(), mPosition, mStackBounds, |
Vadim Tryshev | 593e956 | 2018-03-08 17:15:45 -0800 | [diff] [blame] | 238 | task.getWindowConfiguration(), false /*isNotInRecents*/); |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 239 | return mTarget; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | private int getMode() { |
| 243 | if (mService.mOpeningApps.contains(mAppWindowToken)) { |
| 244 | return RemoteAnimationTarget.MODE_OPENING; |
| 245 | } else { |
| 246 | return RemoteAnimationTarget.MODE_CLOSING; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public boolean getDetachWallpaper() { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | @Override |
Jorim Jaggi | 82c1786 | 2018-02-21 17:50:18 +0100 | [diff] [blame] | 256 | public boolean getShowWallpaper() { |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | @Override |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 261 | public int getBackgroundColor() { |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void startAnimation(SurfaceControl animationLeash, Transaction t, |
| 267 | OnAnimationFinishedCallback finishCallback) { |
| 268 | |
| 269 | // Restore z-layering, position and stack crop until client has a chance to modify it. |
| 270 | t.setLayer(animationLeash, mAppWindowToken.getPrefixOrderIndex()); |
| 271 | t.setPosition(animationLeash, mPosition.x, mPosition.y); |
| 272 | mTmpRect.set(mStackBounds); |
| 273 | mTmpRect.offsetTo(0, 0); |
| 274 | t.setWindowCrop(animationLeash, mTmpRect); |
| 275 | mCapturedLeash = animationLeash; |
| 276 | mCapturedFinishCallback = finishCallback; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void onAnimationCancelled(SurfaceControl animationLeash) { |
| 281 | mPendingAnimations.remove(this); |
| 282 | if (mPendingAnimations.isEmpty()) { |
| 283 | mHandler.removeCallbacks(mTimeoutRunnable); |
Jorim Jaggi | 91f9f348 | 2018-02-14 10:58:26 -0800 | [diff] [blame] | 284 | releaseFinishedCallback(); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 285 | invokeAnimationCancelled(); |
Jorim Jaggi | bc2aabe | 2018-03-08 17:27:43 +0100 | [diff] [blame] | 286 | sendRunningRemoteAnimation(false); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public long getDurationHint() { |
| 292 | return mRemoteAnimationAdapter.getDuration(); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public long getStatusBarTransitionsStartTime() { |
| 297 | return SystemClock.uptimeMillis() |
| 298 | + mRemoteAnimationAdapter.getStatusBarTransitionDelay(); |
| 299 | } |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame^] | 300 | |
| 301 | @Override |
| 302 | public void dump(PrintWriter pw, String prefix) { |
| 303 | pw.print(prefix); pw.print("token="); pw.println(mAppWindowToken); |
| 304 | if (mTarget != null) { |
| 305 | pw.print(prefix); pw.println("Target:"); |
| 306 | mTarget.dump(pw, prefix + " "); |
| 307 | } else { |
| 308 | pw.print(prefix); pw.println("Target: null"); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public void writeToProto(ProtoOutputStream proto) { |
| 314 | final long token = proto.start(REMOTE); |
| 315 | if (mTarget != null) { |
| 316 | mTarget.writeToProto(proto, TARGET); |
| 317 | } |
| 318 | proto.end(token); |
| 319 | } |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 320 | } |
| 321 | } |