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