1 /* 2 * Copyright (C) 2020 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 android.view; 18 19 import static android.view.InsetsController.DEBUG; 20 import static android.view.SyncRtSurfaceTransactionApplier.applyParams; 21 22 import android.annotation.Nullable; 23 import android.annotation.UiThread; 24 import android.content.res.CompatibilityInfo; 25 import android.graphics.Rect; 26 import android.os.Handler; 27 import android.os.Trace; 28 import android.util.Log; 29 import android.util.SparseArray; 30 import android.util.proto.ProtoOutputStream; 31 import android.view.InsetsController.AnimationType; 32 import android.view.InsetsController.LayoutInsetsDuringAnimation; 33 import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; 34 import android.view.WindowInsets.Type.InsetsType; 35 import android.view.WindowInsetsAnimation.Bounds; 36 import android.view.animation.Interpolator; 37 import android.view.inputmethod.ImeTracker; 38 39 /** 40 * Insets animation runner that uses {@link InsetsAnimationThread} to run the animation off from the 41 * main thread. 42 * 43 * @hide 44 */ 45 public class InsetsAnimationThreadControlRunner implements InsetsAnimationControlRunner { 46 47 private static final String TAG = "InsetsAnimThreadRunner"; 48 private final InsetsAnimationControlImpl mControl; 49 private final InsetsAnimationControlCallbacks mOuterCallbacks; 50 private final Handler mMainThreadHandler; 51 private final InsetsAnimationControlCallbacks mCallbacks = 52 new InsetsAnimationControlCallbacks() { 53 54 private final float[] mTmpFloat9 = new float[9]; 55 56 @Override 57 @UiThread 58 public <T extends InsetsAnimationControlRunner & InternalInsetsAnimationController> 59 void startAnimation(T runner, WindowInsetsAnimationControlListener listener, int types, 60 WindowInsetsAnimation animation, Bounds bounds) { 61 // Animation will be started in constructor already. 62 } 63 64 @Override 65 public void scheduleApplyChangeInsets(InsetsAnimationControlRunner runner) { 66 synchronized (mControl) { 67 // This reads the surface position on the animation thread, but the surface position 68 // would be updated on the UI thread, so we need this critical section. 69 // See: updateSurfacePosition. 70 mControl.applyChangeInsets(null /* outState */); 71 } 72 } 73 74 @Override 75 public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) { 76 Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, 77 "InsetsAsyncAnimation: " + WindowInsets.Type.toString(runner.getTypes()), 78 runner.getTypes()); 79 releaseControls(mControl.getControls()); 80 mMainThreadHandler.post(() -> 81 mOuterCallbacks.notifyFinished(InsetsAnimationThreadControlRunner.this, shown)); 82 } 83 84 @Override 85 public void applySurfaceParams(SurfaceParams... params) { 86 if (DEBUG) Log.d(TAG, "applySurfaceParams"); 87 SurfaceControl.Transaction t = new SurfaceControl.Transaction(); 88 for (int i = params.length - 1; i >= 0; i--) { 89 SyncRtSurfaceTransactionApplier.SurfaceParams surfaceParams = params[i]; 90 applyParams(t, surfaceParams, mTmpFloat9); 91 } 92 t.setFrameTimelineVsync(Choreographer.getSfInstance().getVsyncId()); 93 t.apply(); 94 t.close(); 95 } 96 97 @Override 98 public void releaseSurfaceControlFromRt(SurfaceControl sc) { 99 if (DEBUG) Log.d(TAG, "releaseSurfaceControlFromRt"); 100 // Since we don't push the SurfaceParams to the RT we can release directly 101 sc.release(); 102 } 103 104 @Override 105 public void reportPerceptible(int types, boolean perceptible) { 106 mMainThreadHandler.post(() -> mOuterCallbacks.reportPerceptible(types, perceptible)); 107 } 108 }; 109 110 @UiThread InsetsAnimationThreadControlRunner(SparseArray<InsetsSourceControl> controls, @Nullable Rect frame, InsetsState state, WindowInsetsAnimationControlListener listener, @InsetsType int types, InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator, @AnimationType int animationType, @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation, CompatibilityInfo.Translator translator, Handler mainThreadHandler, @Nullable ImeTracker.Token statsToken)111 public InsetsAnimationThreadControlRunner(SparseArray<InsetsSourceControl> controls, 112 @Nullable Rect frame, InsetsState state, WindowInsetsAnimationControlListener listener, 113 @InsetsType int types, InsetsAnimationControlCallbacks controller, long durationMs, 114 Interpolator interpolator, @AnimationType int animationType, 115 @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation, 116 CompatibilityInfo.Translator translator, Handler mainThreadHandler, 117 @Nullable ImeTracker.Token statsToken) { 118 mMainThreadHandler = mainThreadHandler; 119 mOuterCallbacks = controller; 120 mControl = new InsetsAnimationControlImpl(controls, frame, state, listener, types, 121 mCallbacks, durationMs, interpolator, animationType, layoutInsetsDuringAnimation, 122 translator, statsToken); 123 InsetsAnimationThread.getHandler().post(() -> { 124 if (mControl.isCancelled()) { 125 return; 126 } 127 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, 128 "InsetsAsyncAnimation: " + WindowInsets.Type.toString(types), types); 129 listener.onReady(mControl, types); 130 }); 131 } 132 releaseControls(SparseArray<InsetsSourceControl> controls)133 private void releaseControls(SparseArray<InsetsSourceControl> controls) { 134 for (int i = controls.size() - 1; i >= 0; i--) { 135 controls.valueAt(i).release(SurfaceControl::release); 136 } 137 } 138 139 @Override 140 @UiThread dumpDebug(ProtoOutputStream proto, long fieldId)141 public void dumpDebug(ProtoOutputStream proto, long fieldId) { 142 mControl.dumpDebug(proto, fieldId); 143 } 144 145 @Override getStatsToken()146 public ImeTracker.Token getStatsToken() { 147 return mControl.getStatsToken(); 148 } 149 150 @Override 151 @UiThread getTypes()152 public int getTypes() { 153 return mControl.getTypes(); 154 } 155 156 @Override 157 @UiThread getControllingTypes()158 public int getControllingTypes() { 159 return mControl.getControllingTypes(); 160 } 161 162 @Override 163 @UiThread notifyControlRevoked(@nsetsType int types)164 public void notifyControlRevoked(@InsetsType int types) { 165 mControl.notifyControlRevoked(types); 166 } 167 168 @Override 169 @UiThread updateSurfacePosition(SparseArray<InsetsSourceControl> controls)170 public void updateSurfacePosition(SparseArray<InsetsSourceControl> controls) { 171 synchronized (mControl) { 172 // This is called from the UI thread, however, the surface position will be used on the 173 // animation thread, so we need this critical section. See: scheduleApplyChangeInsets. 174 mControl.updateSurfacePosition(controls); 175 } 176 } 177 178 @Override 179 @UiThread cancel()180 public void cancel() { 181 InsetsAnimationThread.getHandler().post(mControl::cancel); 182 } 183 184 @Override 185 @UiThread getAnimation()186 public WindowInsetsAnimation getAnimation() { 187 return mControl.getAnimation(); 188 } 189 190 @Override getAnimationType()191 public int getAnimationType() { 192 return mControl.getAnimationType(); 193 } 194 } 195