1 /*
2  * Copyright (C) 2017 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 
19 import static com.android.server.wm.AnimationAdapterProto.LOCAL;
20 import static com.android.server.wm.LocalAnimationAdapterProto.ANIMATION_SPEC;
21 
22 import android.os.SystemClock;
23 import android.util.proto.ProtoOutputStream;
24 import android.view.SurfaceControl;
25 import android.view.SurfaceControl.Transaction;
26 
27 import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
28 
29 import java.io.PrintWriter;
30 
31 /**
32  * Animation that can be executed without holding the window manager lock. See
33  * {@link SurfaceAnimationRunner}.
34  */
35 class LocalAnimationAdapter implements AnimationAdapter {
36 
37     private final AnimationSpec mSpec;
38 
39     private final SurfaceAnimationRunner mAnimator;
40 
LocalAnimationAdapter(AnimationSpec spec, SurfaceAnimationRunner animator)41     LocalAnimationAdapter(AnimationSpec spec, SurfaceAnimationRunner animator) {
42         mSpec = spec;
43         mAnimator = animator;
44     }
45 
46     @Override
getShowWallpaper()47     public boolean getShowWallpaper() {
48         return mSpec.getShowWallpaper();
49     }
50 
51     @Override
getBackgroundColor()52     public int getBackgroundColor() {
53         return mSpec.getBackgroundColor();
54     }
55 
56     @Override
startAnimation(SurfaceControl animationLeash, Transaction t, OnAnimationFinishedCallback finishCallback)57     public void startAnimation(SurfaceControl animationLeash, Transaction t,
58             OnAnimationFinishedCallback finishCallback) {
59         mAnimator.startAnimation(mSpec, animationLeash, t,
60                 () -> finishCallback.onAnimationFinished(this));
61     }
62 
63     @Override
onAnimationCancelled(SurfaceControl animationLeash)64     public void onAnimationCancelled(SurfaceControl animationLeash) {
65         mAnimator.onAnimationCancelled(animationLeash);
66     }
67 
68     @Override
getDurationHint()69     public long getDurationHint() {
70         return mSpec.getDuration();
71     }
72 
73     @Override
getStatusBarTransitionsStartTime()74     public long getStatusBarTransitionsStartTime() {
75         return mSpec.calculateStatusBarTransitionStartTime();
76     }
77 
78     @Override
dump(PrintWriter pw, String prefix)79     public void dump(PrintWriter pw, String prefix) {
80         mSpec.dump(pw, prefix);
81     }
82 
83     @Override
writeToProto(ProtoOutputStream proto)84     public void writeToProto(ProtoOutputStream proto) {
85         final long token = proto.start(LOCAL);
86         mSpec.writeToProto(proto, ANIMATION_SPEC);
87         proto.end(token);
88     }
89 
90     /**
91      * Describes how to apply an animation.
92      */
93     interface AnimationSpec {
94 
95         /**
96          * @see AnimationAdapter#getShowWallpaper
97          */
getShowWallpaper()98         default boolean getShowWallpaper() {
99             return false;
100         }
101 
102         /**
103          * @see AnimationAdapter#getBackgroundColor
104          */
getBackgroundColor()105         default int getBackgroundColor() {
106             return 0;
107         }
108 
109         /**
110          * @see AnimationAdapter#getStatusBarTransitionsStartTime
111          */
calculateStatusBarTransitionStartTime()112         default long calculateStatusBarTransitionStartTime() {
113             return SystemClock.uptimeMillis();
114         }
115 
116         /**
117          * @return The duration of the animation.
118          */
getDuration()119         long getDuration();
120 
121         /**
122          * Called when the spec needs to apply the current animation state to the leash.
123          *
124          * @param t               The transaction to use to apply a transform.
125          * @param leash           The leash to apply the state to.
126          * @param currentPlayTime The current time of the animation.
127          */
apply(Transaction t, SurfaceControl leash, long currentPlayTime)128         void apply(Transaction t, SurfaceControl leash, long currentPlayTime);
129 
130         /**
131          * @see AppTransition#canSkipFirstFrame
132          */
canSkipFirstFrame()133         default boolean canSkipFirstFrame() {
134             return false;
135         }
136 
137         /**
138          * @return {@code true} if we need to wake-up SurfaceFlinger earlier during this animation.
139          *
140          * @see Transaction#setEarlyWakeup
141          */
needsEarlyWakeup()142         default boolean needsEarlyWakeup() { return false; }
143 
dump(PrintWriter pw, String prefix)144         void dump(PrintWriter pw, String prefix);
145 
writeToProto(ProtoOutputStream proto, long fieldId)146         default void writeToProto(ProtoOutputStream proto, long fieldId) {
147             final long token = proto.start(fieldId);
148             writeToProtoInner(proto);
149             proto.end(token);
150         }
151 
writeToProtoInner(ProtoOutputStream proto)152         void writeToProtoInner(ProtoOutputStream proto);
153     }
154 }
155