blob: b02a74e17f43a3594383b7a517456e7b05ef351f [file] [log] [blame]
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001/*
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
17package com.android.server.wm;
18
Jorim Jaggiab20e162018-03-27 13:06:09 +020019import static com.android.server.wm.AnimationAdapterProto.REMOTE;
20import static com.android.server.wm.RemoteAnimationAdapterWrapperProto.TARGET;
Jorim Jaggif75d1612018-02-27 15:05:21 +010021import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010022import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
24
25import android.graphics.Point;
26import android.graphics.Rect;
Jorim Jaggiab20e162018-03-27 13:06:09 +020027import android.os.Binder;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010028import android.os.Handler;
29import android.os.RemoteException;
30import android.os.SystemClock;
31import android.util.Slog;
Jorim Jaggif75d1612018-02-27 15:05:21 +010032import android.util.proto.ProtoOutputStream;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010033import android.view.IRemoteAnimationFinishedCallback;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010034import android.view.RemoteAnimationAdapter;
35import android.view.RemoteAnimationTarget;
36import android.view.SurfaceControl;
37import android.view.SurfaceControl.Transaction;
38
Jorim Jaggif75d1612018-02-27 15:05:21 +010039import com.android.internal.util.FastPrintWriter;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010040import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
41
Jorim Jaggif75d1612018-02-27 15:05:21 +010042import java.io.PrintWriter;
43import java.io.StringWriter;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010044import java.util.ArrayList;
45
46/**
47 * Helper class to run app animations in a remote process.
48 */
49class RemoteAnimationController {
50 private static final String TAG = TAG_WITH_CLASS_NAME ? "RemoteAnimationController" : TAG_WM;
51 private static final long TIMEOUT_MS = 2000;
52
53 private final WindowManagerService mService;
54 private final RemoteAnimationAdapter mRemoteAnimationAdapter;
55 private final ArrayList<RemoteAnimationAdapterWrapper> mPendingAnimations = new ArrayList<>();
56 private final Rect mTmpRect = new Rect();
57 private final Handler mHandler;
Jorim Jaggi91f9f3482018-02-14 10:58:26 -080058 private FinishedCallback mFinishedCallback;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010059
60 private final Runnable mTimeoutRunnable = () -> {
61 onAnimationFinished();
62 invokeAnimationCancelled();
63 };
64
65 RemoteAnimationController(WindowManagerService service,
66 RemoteAnimationAdapter remoteAnimationAdapter, Handler handler) {
67 mService = service;
68 mRemoteAnimationAdapter = remoteAnimationAdapter;
69 mHandler = handler;
70 }
71
72 /**
73 * Creates an animation for each individual {@link AppWindowToken}.
74 *
75 * @param appWindowToken The app to animate.
76 * @param position The position app bounds, in screen coordinates.
77 * @param stackBounds The stack bounds of the app.
78 * @return The adapter to be run on the app.
79 */
80 AnimationAdapter createAnimationAdapter(AppWindowToken appWindowToken, Point position,
81 Rect stackBounds) {
82 final RemoteAnimationAdapterWrapper adapter = new RemoteAnimationAdapterWrapper(
83 appWindowToken, position, stackBounds);
84 mPendingAnimations.add(adapter);
85 return adapter;
86 }
87
88 /**
89 * Called when the transition is ready to be started, and all leashes have been set up.
90 */
91 void goodToGo() {
Jorim Jaggi93f9fe32018-01-25 15:06:13 +010092 if (mPendingAnimations.isEmpty()) {
93 onAnimationFinished();
94 return;
95 }
Jorim Jaggia19d7812018-02-01 15:03:59 +010096
97 // Scale the timeout with the animator scale the controlling app is using.
98 mHandler.postDelayed(mTimeoutRunnable,
99 (long) (TIMEOUT_MS * mService.getCurrentAnimatorScale()));
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800100 mFinishedCallback = new FinishedCallback(this);
Chavi Weingarten16d0d072018-02-12 23:50:28 +0000101
102 final RemoteAnimationTarget[] animations = createAnimations();
Jorim Jaggic4d29f22018-03-22 16:30:56 +0100103 if (animations.length == 0) {
104 onAnimationFinished();
105 return;
106 }
Chavi Weingarten16d0d072018-02-12 23:50:28 +0000107 mService.mAnimator.addAfterPrepareSurfacesRunnable(() -> {
108 try {
109 mRemoteAnimationAdapter.getRunner().onAnimationStart(animations,
110 mFinishedCallback);
111 } catch (RemoteException e) {
112 Slog.e(TAG, "Failed to start remote animation", e);
113 onAnimationFinished();
114 }
115 });
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100116 sendRunningRemoteAnimation(true);
Jorim Jaggif75d1612018-02-27 15:05:21 +0100117 if (DEBUG_APP_TRANSITIONS) {
118 writeStartDebugStatement();
119 }
120 }
121
122 private void writeStartDebugStatement() {
123 Slog.i(TAG, "Starting remote animation");
124 final StringWriter sw = new StringWriter();
125 final FastPrintWriter pw = new FastPrintWriter(sw);
126 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
127 mPendingAnimations.get(i).dump(pw, "");
128 }
129 pw.close();
130 Slog.i(TAG, sw.toString());
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100131 }
132
133 private RemoteAnimationTarget[] createAnimations() {
Jorim Jaggi17770212018-01-22 20:01:21 +0100134 final ArrayList<RemoteAnimationTarget> targets = new ArrayList<>();
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100135 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
Jorim Jaggi17770212018-01-22 20:01:21 +0100136 final RemoteAnimationTarget target =
137 mPendingAnimations.get(i).createRemoteAppAnimation();
138 if (target != null) {
139 targets.add(target);
Jorim Jaggic4d29f22018-03-22 16:30:56 +0100140 } else {
141 mPendingAnimations.remove(i);
Jorim Jaggi17770212018-01-22 20:01:21 +0100142 }
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100143 }
Jorim Jaggi17770212018-01-22 20:01:21 +0100144 return targets.toArray(new RemoteAnimationTarget[targets.size()]);
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100145 }
146
147 private void onAnimationFinished() {
148 mHandler.removeCallbacks(mTimeoutRunnable);
149 synchronized (mService.mWindowMap) {
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800150 releaseFinishedCallback();
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100151 mService.openSurfaceTransaction();
152 try {
153 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
154 final RemoteAnimationAdapterWrapper adapter = mPendingAnimations.get(i);
155 adapter.mCapturedFinishCallback.onAnimationFinished(adapter);
156 }
157 } finally {
158 mService.closeSurfaceTransaction("RemoteAnimationController#finished");
159 }
160 }
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100161 sendRunningRemoteAnimation(false);
Jorim Jaggif75d1612018-02-27 15:05:21 +0100162 if (DEBUG_APP_TRANSITIONS) Slog.i(TAG, "Finishing remote animation");
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100163 }
164
165 private void invokeAnimationCancelled() {
166 try {
167 mRemoteAnimationAdapter.getRunner().onAnimationCancelled();
168 } catch (RemoteException e) {
169 Slog.e(TAG, "Failed to notify cancel", e);
170 }
171 }
172
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800173 private void releaseFinishedCallback() {
174 if (mFinishedCallback != null) {
175 mFinishedCallback.release();
176 mFinishedCallback = null;
177 }
178 }
179
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100180 private void sendRunningRemoteAnimation(boolean running) {
181 final int pid = mRemoteAnimationAdapter.getCallingPid();
182 if (pid == 0) {
183 throw new RuntimeException("Calling pid of remote animation was null");
184 }
185 mService.sendSetRunningRemoteAnimation(pid, running);
186 }
187
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800188 private static final class FinishedCallback extends IRemoteAnimationFinishedCallback.Stub {
189
190 RemoteAnimationController mOuter;
191
192 FinishedCallback(RemoteAnimationController outer) {
193 mOuter = outer;
194 }
195
196 @Override
197 public void onAnimationFinished() throws RemoteException {
Jorim Jaggiab20e162018-03-27 13:06:09 +0200198 final long token = Binder.clearCallingIdentity();
199 try {
200 if (mOuter != null) {
201 mOuter.onAnimationFinished();
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800202
Jorim Jaggiab20e162018-03-27 13:06:09 +0200203 // In case the client holds on to the finish callback, make sure we don't leak
204 // RemoteAnimationController which in turn would leak the runner on the client.
205 mOuter = null;
206 }
207 } finally {
208 Binder.restoreCallingIdentity(token);
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800209 }
210 }
211
212 /**
213 * Marks this callback as not be used anymore by releasing the reference to the outer class
214 * to prevent memory leak.
215 */
216 void release() {
217 mOuter = null;
218 }
219 };
220
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100221 private class RemoteAnimationAdapterWrapper implements AnimationAdapter {
222
223 private final AppWindowToken mAppWindowToken;
224 private SurfaceControl mCapturedLeash;
225 private OnAnimationFinishedCallback mCapturedFinishCallback;
226 private final Point mPosition = new Point();
227 private final Rect mStackBounds = new Rect();
Jorim Jaggif75d1612018-02-27 15:05:21 +0100228 private RemoteAnimationTarget mTarget;
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100229
230 RemoteAnimationAdapterWrapper(AppWindowToken appWindowToken, Point position,
231 Rect stackBounds) {
232 mAppWindowToken = appWindowToken;
233 mPosition.set(position.x, position.y);
234 mStackBounds.set(stackBounds);
235 }
236
237 RemoteAnimationTarget createRemoteAppAnimation() {
Jorim Jaggi17770212018-01-22 20:01:21 +0100238 final Task task = mAppWindowToken.getTask();
239 final WindowState mainWindow = mAppWindowToken.findMainWindow();
Jorim Jaggic4d29f22018-03-22 16:30:56 +0100240 if (task == null || mainWindow == null || mCapturedFinishCallback == null
241 || mCapturedLeash == null) {
Jorim Jaggi17770212018-01-22 20:01:21 +0100242 return null;
243 }
Jorim Jaggif75d1612018-02-27 15:05:21 +0100244 mTarget = new RemoteAnimationTarget(task.mTaskId, getMode(),
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100245 mCapturedLeash, !mAppWindowToken.fillsParent(),
Winson Chung584d6522018-02-07 23:57:38 +0000246 mainWindow.mWinAnimator.mLastClipRect, mainWindow.mContentInsets,
Winson Chunge2d72172018-01-25 17:46:20 +0000247 mAppWindowToken.getPrefixOrderIndex(), mPosition, mStackBounds,
Vadim Tryshev593e9562018-03-08 17:15:45 -0800248 task.getWindowConfiguration(), false /*isNotInRecents*/);
Jorim Jaggif75d1612018-02-27 15:05:21 +0100249 return mTarget;
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100250 }
251
252 private int getMode() {
253 if (mService.mOpeningApps.contains(mAppWindowToken)) {
254 return RemoteAnimationTarget.MODE_OPENING;
255 } else {
256 return RemoteAnimationTarget.MODE_CLOSING;
257 }
258 }
259
260 @Override
261 public boolean getDetachWallpaper() {
262 return false;
263 }
264
265 @Override
Jorim Jaggi82c17862018-02-21 17:50:18 +0100266 public boolean getShowWallpaper() {
267 return false;
268 }
269
270 @Override
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100271 public int getBackgroundColor() {
272 return 0;
273 }
274
275 @Override
276 public void startAnimation(SurfaceControl animationLeash, Transaction t,
277 OnAnimationFinishedCallback finishCallback) {
278
279 // Restore z-layering, position and stack crop until client has a chance to modify it.
280 t.setLayer(animationLeash, mAppWindowToken.getPrefixOrderIndex());
281 t.setPosition(animationLeash, mPosition.x, mPosition.y);
282 mTmpRect.set(mStackBounds);
283 mTmpRect.offsetTo(0, 0);
284 t.setWindowCrop(animationLeash, mTmpRect);
285 mCapturedLeash = animationLeash;
286 mCapturedFinishCallback = finishCallback;
287 }
288
289 @Override
290 public void onAnimationCancelled(SurfaceControl animationLeash) {
291 mPendingAnimations.remove(this);
292 if (mPendingAnimations.isEmpty()) {
293 mHandler.removeCallbacks(mTimeoutRunnable);
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800294 releaseFinishedCallback();
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100295 invokeAnimationCancelled();
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100296 sendRunningRemoteAnimation(false);
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100297 }
298 }
299
300 @Override
301 public long getDurationHint() {
302 return mRemoteAnimationAdapter.getDuration();
303 }
304
305 @Override
306 public long getStatusBarTransitionsStartTime() {
307 return SystemClock.uptimeMillis()
308 + mRemoteAnimationAdapter.getStatusBarTransitionDelay();
309 }
Jorim Jaggif75d1612018-02-27 15:05:21 +0100310
311 @Override
312 public void dump(PrintWriter pw, String prefix) {
313 pw.print(prefix); pw.print("token="); pw.println(mAppWindowToken);
314 if (mTarget != null) {
315 pw.print(prefix); pw.println("Target:");
316 mTarget.dump(pw, prefix + " ");
317 } else {
318 pw.print(prefix); pw.println("Target: null");
319 }
320 }
321
322 @Override
323 public void writeToProto(ProtoOutputStream proto) {
324 final long token = proto.start(REMOTE);
325 if (mTarget != null) {
326 mTarget.writeToProto(proto, TARGET);
327 }
328 proto.end(token);
329 }
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100330 }
331}