Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/activityembedding/ActivityEmbeddingAnimationAdapter.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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.activityembedding;
18
19 import static android.graphics.Matrix.MTRANS_X;
20 import static android.graphics.Matrix.MTRANS_Y;
21
22 import android.annotation.CallSuper;
23 import android.graphics.Point;
24 import android.graphics.Rect;
25 import android.view.Choreographer;
26 import android.view.SurfaceControl;
27 import android.view.animation.Animation;
28 import android.view.animation.Transformation;
29 import android.window.TransitionInfo;
30
31 import androidx.annotation.NonNull;
32
33 import com.android.wm.shell.transition.Transitions;
34
35 /**
36 * Wrapper to handle the ActivityEmbedding animation update in one
37 * {@link SurfaceControl.Transaction}.
38 */
39 class ActivityEmbeddingAnimationAdapter {
40
41 /**
42 * If {@link #mOverrideLayer} is set to this value, we don't want to override the surface layer.
43 */
44 private static final int LAYER_NO_OVERRIDE = -1;
45
46 @NonNull
47 final Animation mAnimation;
48 @NonNull
49 final TransitionInfo.Change mChange;
50 @NonNull
51 final SurfaceControl mLeash;
52 /** Area in absolute coordinate that the animation surface shouldn't go beyond. */
53 @NonNull
54 private final Rect mWholeAnimationBounds = new Rect();
55 /**
56 * Area in absolute coordinate that should represent all the content to show for this window.
57 * This should be the end bounds for opening window, and start bounds for closing window in case
58 * the window is resizing during the open/close transition.
59 */
60 @NonNull
61 private final Rect mContentBounds = new Rect();
62 /** Offset relative to the window parent surface for {@link #mContentBounds}. */
63 @NonNull
64 private final Point mContentRelOffset = new Point();
65
66 @NonNull
67 final Transformation mTransformation = new Transformation();
68 @NonNull
69 final float[] mMatrix = new float[9];
70 @NonNull
71 final float[] mVecs = new float[4];
72 @NonNull
73 final Rect mRect = new Rect();
74 private boolean mIsFirstFrame = true;
75 private int mOverrideLayer = LAYER_NO_OVERRIDE;
76
77 ActivityEmbeddingAnimationAdapter(@NonNull Animation animation,
78 @NonNull TransitionInfo.Change change) {
79 this(animation, change, change.getLeash(), change.getEndAbsBounds());
80 }
81
82 /**
83 * @param leash the surface to animate, which is not necessary the same as
84 * {@link TransitionInfo.Change#getLeash()}, it can be a screenshot for example.
85 * @param wholeAnimationBounds area in absolute coordinate that the animation surface shouldn't
86 * go beyond.
87 */
88 ActivityEmbeddingAnimationAdapter(@NonNull Animation animation,
89 @NonNull TransitionInfo.Change change, @NonNull SurfaceControl leash,
90 @NonNull Rect wholeAnimationBounds) {
91 mAnimation = animation;
92 mChange = change;
93 mLeash = leash;
94 mWholeAnimationBounds.set(wholeAnimationBounds);
95 if (Transitions.isClosingType(change.getMode())) {
96 // When it is closing, we want to show the content at the start position in case the
97 // window is resizing as well. For example, when the activities is changing from split
98 // to stack, the bottom TaskFragment will be resized to fullscreen when hiding.
99 final Rect startBounds = change.getStartAbsBounds();
100 final Rect endBounds = change.getEndAbsBounds();
101 mContentBounds.set(startBounds);
102 mContentRelOffset.set(change.getEndRelOffset());
103 mContentRelOffset.offset(
104 startBounds.left - endBounds.left,
105 startBounds.top - endBounds.top);
106 } else {
107 mContentBounds.set(change.getEndAbsBounds());
108 mContentRelOffset.set(change.getEndRelOffset());
109 }
110 }
111
112 /**
113 * Surface layer to be set at the first frame of the animation. We will not set the layer if it
114 * is set to {@link #LAYER_NO_OVERRIDE}.
115 */
116 final void overrideLayer(int layer) {
117 mOverrideLayer = layer;
118 }
119
120 /** Called on frame update. */
121 final void onAnimationUpdate(@NonNull SurfaceControl.Transaction t, long currentPlayTime) {
122 if (mIsFirstFrame) {
123 t.show(mLeash);
124 if (mOverrideLayer != LAYER_NO_OVERRIDE) {
125 t.setLayer(mLeash, mOverrideLayer);
126 }
127 mIsFirstFrame = false;
128 }
129
130 // Extract the transformation to the current time.
131 mAnimation.getTransformation(Math.min(currentPlayTime, mAnimation.getDuration()),
132 mTransformation);
133 t.setFrameTimelineVsync(Choreographer.getInstance().getVsyncId());
134 onAnimationUpdateInner(t);
135 }
136
137 /** To be overridden by subclasses to adjust the animation surface change. */
138 void onAnimationUpdateInner(@NonNull SurfaceControl.Transaction t) {
139 // Update the surface position and alpha.
140 mTransformation.getMatrix().postTranslate(mContentRelOffset.x, mContentRelOffset.y);
141 t.setMatrix(mLeash, mTransformation.getMatrix(), mMatrix);
142 t.setAlpha(mLeash, mTransformation.getAlpha());
143
144 // Get current surface bounds in absolute coordinate.
145 // positionX/Y are in local coordinate, so minus the local offset to get the slide amount.
146 final int positionX = Math.round(mMatrix[MTRANS_X]);
147 final int positionY = Math.round(mMatrix[MTRANS_Y]);
148 final Rect cropRect = new Rect(mContentBounds);
149 cropRect.offset(positionX - mContentRelOffset.x, positionY - mContentRelOffset.y);
150
151 // Store the current offset of the surface top left from (0,0) in absolute coordinate.
152 final int offsetX = cropRect.left;
153 final int offsetY = cropRect.top;
154
155 // Intersect to make sure the animation happens within the whole animation bounds.
156 if (!cropRect.intersect(mWholeAnimationBounds)) {
157 // Hide the surface when it is outside of the animation area.
158 t.setAlpha(mLeash, 0);
159 } else if (mAnimation.hasExtension()) {
160 // Allow the surface to be shown in its original bounds in case we want to use edge
161 // extensions.
162 cropRect.union(mContentBounds);
163 }
164
165 // cropRect is in absolute coordinate, so we need to translate it to surface top left.
166 cropRect.offset(-offsetX, -offsetY);
167 t.setCrop(mLeash, cropRect);
168 }
169
170 /** Called after animation finished. */
171 @CallSuper
172 void onAnimationEnd(@NonNull SurfaceControl.Transaction t) {
173 onAnimationUpdate(t, mAnimation.getDuration());
174 }
175
176 final long getDurationHint() {
177 return mAnimation.computeDurationHint();
178 }
179
180 /**
181 * Should be used for the animation of the snapshot of a {@link TransitionInfo.Change} that has
182 * size change.
183 */
184 static class SnapshotAdapter extends ActivityEmbeddingAnimationAdapter {
185
186 SnapshotAdapter(@NonNull Animation animation, @NonNull TransitionInfo.Change change,
187 @NonNull SurfaceControl snapshotLeash) {
188 super(animation, change, snapshotLeash, change.getEndAbsBounds());
189 }
190
191 @Override
192 void onAnimationUpdateInner(@NonNull SurfaceControl.Transaction t) {
193 // Snapshot should always be placed at the top left of the animation leash.
194 mTransformation.getMatrix().postTranslate(0, 0);
195 t.setMatrix(mLeash, mTransformation.getMatrix(), mMatrix);
196 t.setAlpha(mLeash, mTransformation.getAlpha());
197 }
198
199 @Override
200 void onAnimationEnd(@NonNull SurfaceControl.Transaction t) {
201 super.onAnimationEnd(t);
202 // Remove the screenshot leash after animation is finished.
203 if (mLeash.isValid()) {
204 t.remove(mLeash);
205 }
206 }
207 }
208
209 /**
210 * Should be used for the animation of the {@link TransitionInfo.Change} that has size change.
211 */
212 static class BoundsChangeAdapter extends ActivityEmbeddingAnimationAdapter {
213
214 BoundsChangeAdapter(@NonNull Animation animation, @NonNull TransitionInfo.Change change) {
215 super(animation, change);
216 }
217
218 @Override
219 void onAnimationUpdateInner(@NonNull SurfaceControl.Transaction t) {
220 final Point offset = mChange.getEndRelOffset();
221 mTransformation.getMatrix().postTranslate(offset.x, offset.y);
222 t.setMatrix(mLeash, mTransformation.getMatrix(), mMatrix);
223 t.setAlpha(mLeash, mTransformation.getAlpha());
224
225 // The following applies an inverse scale to the clip-rect so that it crops "after" the
226 // scale instead of before.
227 mVecs[1] = mVecs[2] = 0;
228 mVecs[0] = mVecs[3] = 1;
229 mTransformation.getMatrix().mapVectors(mVecs);
230 mVecs[0] = 1.f / mVecs[0];
231 mVecs[3] = 1.f / mVecs[3];
232 final Rect clipRect = mTransformation.getClipRect();
233 mRect.left = (int) (clipRect.left * mVecs[0] + 0.5f);
234 mRect.right = (int) (clipRect.right * mVecs[0] + 0.5f);
235 mRect.top = (int) (clipRect.top * mVecs[3] + 0.5f);
236 mRect.bottom = (int) (clipRect.bottom * mVecs[3] + 0.5f);
237 t.setCrop(mLeash, mRect);
238 }
239 }
240 }