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