Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/transition/OneShotRemoteHandler.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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.wm.shell.transition;
18
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.IBinder;
22 import android.os.RemoteException;
23 import android.util.Log;
24 import android.view.SurfaceControl;
25 import android.window.IRemoteTransition;
26 import android.window.IRemoteTransitionFinishedCallback;
27 import android.window.RemoteTransition;
28 import android.window.TransitionInfo;
29 import android.window.TransitionRequestInfo;
30 import android.window.WindowContainerTransaction;
31
32 import com.android.internal.protolog.common.ProtoLog;
33 import com.android.wm.shell.common.ShellExecutor;
34 import com.android.wm.shell.protolog.ShellProtoLogGroup;
35
36 /**
37 * Handler that forwards to a RemoteTransition. It is designed for one-shot use to attach a
38 * specific remote animation to a specific transition.
39 */
40 public class OneShotRemoteHandler implements Transitions.TransitionHandler {
41 private final ShellExecutor mMainExecutor;
42
43 /** The specific transition that this handler is associated with. Just for validation. */
44 private IBinder mTransition = null;
45
46 /** The remote to delegate animation to */
47 private final RemoteTransition mRemote;
48
49 public OneShotRemoteHandler(@NonNull ShellExecutor mainExecutor,
50 @NonNull RemoteTransition remote) {
51 mMainExecutor = mainExecutor;
52 mRemote = remote;
53 }
54
55 public void setTransition(@NonNull IBinder transition) {
56 mTransition = transition;
57 }
58
59 @Override
60 public boolean startAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info,
61 @NonNull SurfaceControl.Transaction startTransaction,
62 @NonNull SurfaceControl.Transaction finishTransaction,
63 @NonNull Transitions.TransitionFinishCallback finishCallback) {
64 if (mTransition != transition) return false;
65 ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Using registered One-shot remote"
66 + " transition %s for %s.", mRemote, transition);
67
68 final IBinder.DeathRecipient remoteDied = () -> {
69 Log.e(Transitions.TAG, "Remote transition died, finishing");
70 mMainExecutor.execute(
71 () -> finishCallback.onTransitionFinished(null /* wct */, null /* wctCB */));
72 };
73 IRemoteTransitionFinishedCallback cb = new IRemoteTransitionFinishedCallback.Stub() {
74 @Override
75 public void onTransitionFinished(WindowContainerTransaction wct,
76 SurfaceControl.Transaction sct) {
77 if (mRemote.asBinder() != null) {
78 mRemote.asBinder().unlinkToDeath(remoteDied, 0 /* flags */);
79 }
80 if (sct != null) {
81 finishTransaction.merge(sct);
82 }
83 mMainExecutor.execute(() -> {
84 finishCallback.onTransitionFinished(wct, null /* wctCB */);
85 });
86 }
87 };
88 Transitions.setRunningRemoteTransitionDelegate(mRemote.getAppThread());
89 try {
90 if (mRemote.asBinder() != null) {
91 mRemote.asBinder().linkToDeath(remoteDied, 0 /* flags */);
92 }
93 // If the remote is actually in the same process, then make a copy of parameters since
94 // remote impls assume that they have to clean-up native references.
95 final SurfaceControl.Transaction remoteStartT = RemoteTransitionHandler.copyIfLocal(
96 startTransaction, mRemote.getRemoteTransition());
97 final TransitionInfo remoteInfo =
98 remoteStartT == startTransaction ? info : info.localRemoteCopy();
99 mRemote.getRemoteTransition().startAnimation(transition, remoteInfo, remoteStartT, cb);
100 // assume that remote will apply the start transaction.
101 startTransaction.clear();
102 } catch (RemoteException e) {
103 Log.e(Transitions.TAG, "Error running remote transition.", e);
104 if (mRemote.asBinder() != null) {
105 mRemote.asBinder().unlinkToDeath(remoteDied, 0 /* flags */);
106 }
107 finishCallback.onTransitionFinished(null /* wct */, null /* wctCB */);
108 }
109 return true;
110 }
111
112 @Override
113 public void mergeAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info,
114 @NonNull SurfaceControl.Transaction t, @NonNull IBinder mergeTarget,
115 @NonNull Transitions.TransitionFinishCallback finishCallback) {
116 ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Using registered One-shot remote"
117 + " transition %s for %s.", mRemote, transition);
118
119 IRemoteTransitionFinishedCallback cb = new IRemoteTransitionFinishedCallback.Stub() {
120 @Override
121 public void onTransitionFinished(WindowContainerTransaction wct,
122 SurfaceControl.Transaction sct) {
123 // We have merged, since we sent the transaction over binder, the one in this
124 // process won't be cleared if the remote applied it. We don't actually know if the
125 // remote applied the transaction, but applying twice will break surfaceflinger
126 // so just assume the worst-case and clear the local transaction.
127 t.clear();
128 mMainExecutor.execute(
129 () -> finishCallback.onTransitionFinished(wct, null /* wctCB */));
130 }
131 };
132 try {
133 // If the remote is actually in the same process, then make a copy of parameters since
134 // remote impls assume that they have to clean-up native references.
135 final SurfaceControl.Transaction remoteT =
136 RemoteTransitionHandler.copyIfLocal(t, mRemote.getRemoteTransition());
137 final TransitionInfo remoteInfo = remoteT == t ? info : info.localRemoteCopy();
138 mRemote.getRemoteTransition().mergeAnimation(
139 transition, remoteInfo, remoteT, mergeTarget, cb);
140 } catch (RemoteException e) {
141 Log.e(Transitions.TAG, "Error merging remote transition.", e);
142 }
143 }
144
145 @Override
146 @Nullable
147 public WindowContainerTransaction handleRequest(@NonNull IBinder transition,
148 @Nullable TransitionRequestInfo request) {
149 RemoteTransition remote = request.getRemoteTransition();
150 IRemoteTransition iRemote = remote != null ? remote.getRemoteTransition() : null;
151 if (iRemote != mRemote.getRemoteTransition()) return null;
152 mTransition = transition;
153 ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "RemoteTransition directly requested"
154 + " for %s: %s", transition, remote);
155 return new WindowContainerTransaction();
156 }
157 }