blob: d645110b6fd5377aeee14f9e9d71bc20fadfe315 [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 Jaggif75d1612018-02-27 15:05:21 +010019import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010020import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
21import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Jorim Jaggif75d1612018-02-27 15:05:21 +010022import static com.android.server.wm.proto.AnimationAdapterProto.REMOTE;
23import static com.android.server.wm.proto.RemoteAnimationAdapterWrapperProto.TARGET;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010024
25import android.graphics.Point;
26import android.graphics.Rect;
27import android.os.Handler;
28import android.os.RemoteException;
29import android.os.SystemClock;
30import android.util.Slog;
Jorim Jaggif75d1612018-02-27 15:05:21 +010031import android.util.proto.ProtoOutputStream;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010032import android.view.IRemoteAnimationFinishedCallback;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010033import android.view.RemoteAnimationAdapter;
34import android.view.RemoteAnimationTarget;
35import android.view.SurfaceControl;
36import android.view.SurfaceControl.Transaction;
37
Jorim Jaggif75d1612018-02-27 15:05:21 +010038import com.android.internal.util.FastPrintWriter;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010039import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
40
Jorim Jaggif75d1612018-02-27 15:05:21 +010041import java.io.PrintWriter;
42import java.io.StringWriter;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010043import java.util.ArrayList;
44
45/**
46 * Helper class to run app animations in a remote process.
47 */
48class 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 Jaggi91f9f3482018-02-14 10:58:26 -080057 private FinishedCallback mFinishedCallback;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010058
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 Jaggi93f9fe32018-01-25 15:06:13 +010091 if (mPendingAnimations.isEmpty()) {
92 onAnimationFinished();
93 return;
94 }
Jorim Jaggia19d7812018-02-01 15:03:59 +010095
96 // Scale the timeout with the animator scale the controlling app is using.
97 mHandler.postDelayed(mTimeoutRunnable,
98 (long) (TIMEOUT_MS * mService.getCurrentAnimatorScale()));
Jorim Jaggi91f9f3482018-02-14 10:58:26 -080099 mFinishedCallback = new FinishedCallback(this);
Chavi Weingarten16d0d072018-02-12 23:50:28 +0000100
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 Jaggibc2aabe2018-03-08 17:27:43 +0100111 sendRunningRemoteAnimation(true);
Jorim Jaggif75d1612018-02-27 15:05:21 +0100112 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 Jaggi33a701a2017-12-01 14:58:18 +0100126 }
127
128 private RemoteAnimationTarget[] createAnimations() {
Jorim Jaggi17770212018-01-22 20:01:21 +0100129 final ArrayList<RemoteAnimationTarget> targets = new ArrayList<>();
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100130 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
Jorim Jaggi17770212018-01-22 20:01:21 +0100131 final RemoteAnimationTarget target =
132 mPendingAnimations.get(i).createRemoteAppAnimation();
133 if (target != null) {
134 targets.add(target);
135 }
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100136 }
Jorim Jaggi17770212018-01-22 20:01:21 +0100137 return targets.toArray(new RemoteAnimationTarget[targets.size()]);
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100138 }
139
140 private void onAnimationFinished() {
141 mHandler.removeCallbacks(mTimeoutRunnable);
142 synchronized (mService.mWindowMap) {
Jorim Jaggi91f9f3482018-02-14 10:58:26 -0800143 releaseFinishedCallback();
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100144 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 Jaggibc2aabe2018-03-08 17:27:43 +0100154 sendRunningRemoteAnimation(false);
Jorim Jaggif75d1612018-02-27 15:05:21 +0100155 if (DEBUG_APP_TRANSITIONS) Slog.i(TAG, "Finishing remote animation");
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100156 }
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 Jaggi91f9f3482018-02-14 10:58:26 -0800166 private void releaseFinishedCallback() {
167 if (mFinishedCallback != null) {
168 mFinishedCallback.release();
169 mFinishedCallback = null;
170 }
171 }
172
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100173 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 Jaggi91f9f3482018-02-14 10:58:26 -0800181 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 Jaggi33a701a2017-12-01 14:58:18 +0100209 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 Jaggif75d1612018-02-27 15:05:21 +0100216 private RemoteAnimationTarget mTarget;
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100217
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 Jaggi17770212018-01-22 20:01:21 +0100226 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 Jaggif75d1612018-02-27 15:05:21 +0100234 mTarget = new RemoteAnimationTarget(task.mTaskId, getMode(),
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100235 mCapturedLeash, !mAppWindowToken.fillsParent(),
Winson Chung584d6522018-02-07 23:57:38 +0000236 mainWindow.mWinAnimator.mLastClipRect, mainWindow.mContentInsets,
Winson Chunge2d72172018-01-25 17:46:20 +0000237 mAppWindowToken.getPrefixOrderIndex(), mPosition, mStackBounds,
Vadim Tryshev593e9562018-03-08 17:15:45 -0800238 task.getWindowConfiguration(), false /*isNotInRecents*/);
Jorim Jaggif75d1612018-02-27 15:05:21 +0100239 return mTarget;
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100240 }
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 Jaggi82c17862018-02-21 17:50:18 +0100256 public boolean getShowWallpaper() {
257 return false;
258 }
259
260 @Override
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100261 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 Jaggi91f9f3482018-02-14 10:58:26 -0800284 releaseFinishedCallback();
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100285 invokeAnimationCancelled();
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100286 sendRunningRemoteAnimation(false);
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100287 }
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 Jaggif75d1612018-02-27 15:05:21 +0100300
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 Jaggi33a701a2017-12-01 14:58:18 +0100320 }
321}