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.UiThread; 23 import android.graphics.Rect; 24 import android.os.Handler; 25 import android.os.Trace; 26 import android.util.Log; 27 import android.util.SparseArray; 28 import android.view.InsetsController.AnimationType; 29 import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; 30 import android.view.WindowInsets.Type.InsetsType; 31 import android.view.WindowInsetsAnimation.Bounds; 32 import android.view.animation.Interpolator; 33 34 /** 35 * Insets animation runner that uses {@link InsetsAnimationThread} to run the animation off from the 36 * main thread. 37 * 38 * @hide 39 */ 40 public class InsetsAnimationThreadControlRunner implements InsetsAnimationControlRunner { 41 42 private static final String TAG = "InsetsAnimThreadRunner"; 43 private final InsetsAnimationControlImpl mControl; 44 private final InsetsAnimationControlCallbacks mOuterCallbacks; 45 private final Handler mMainThreadHandler; 46 private final InsetsState mState = new InsetsState(); 47 private final InsetsAnimationControlCallbacks mCallbacks = 48 new InsetsAnimationControlCallbacks() { 49 50 private final float[] mTmpFloat9 = new float[9]; 51 52 @Override 53 @UiThread 54 public void startAnimation(InsetsAnimationControlImpl controller, 55 WindowInsetsAnimationControlListener listener, int types, 56 WindowInsetsAnimation animation, Bounds bounds) { 57 // Animation will be started in constructor already. 58 } 59 60 @Override 61 public void scheduleApplyChangeInsets(InsetsAnimationControlRunner runner) { 62 mControl.applyChangeInsets(mState); 63 } 64 65 @Override 66 public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) { 67 Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, 68 "InsetsAsyncAnimation: " + WindowInsets.Type.toString(runner.getTypes()), 69 runner.getTypes()); 70 releaseControls(mControl.getControls()); 71 mMainThreadHandler.post(() -> 72 mOuterCallbacks.notifyFinished(InsetsAnimationThreadControlRunner.this, shown)); 73 } 74 75 @Override 76 public void applySurfaceParams(SurfaceParams... params) { 77 if (DEBUG) Log.d(TAG, "applySurfaceParams"); 78 SurfaceControl.Transaction t = new SurfaceControl.Transaction(); 79 for (int i = params.length - 1; i >= 0; i--) { 80 SyncRtSurfaceTransactionApplier.SurfaceParams surfaceParams = params[i]; 81 applyParams(t, surfaceParams, mTmpFloat9); 82 } 83 t.apply(); 84 t.close(); 85 } 86 87 @Override 88 public void releaseSurfaceControlFromRt(SurfaceControl sc) { 89 if (DEBUG) Log.d(TAG, "releaseSurfaceControlFromRt"); 90 // Since we don't push the SurfaceParams to the RT we can release directly 91 sc.release(); 92 } 93 94 @Override 95 public void reportPerceptible(int types, boolean perceptible) { 96 mMainThreadHandler.post(() -> mOuterCallbacks.reportPerceptible(types, perceptible)); 97 } 98 }; 99 100 @UiThread InsetsAnimationThreadControlRunner(SparseArray<InsetsSourceControl> controls, Rect frame, InsetsState state, WindowInsetsAnimationControlListener listener, @InsetsType int types, InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator, @AnimationType int animationType, Handler mainThreadHandler)101 public InsetsAnimationThreadControlRunner(SparseArray<InsetsSourceControl> controls, Rect frame, 102 InsetsState state, WindowInsetsAnimationControlListener listener, 103 @InsetsType int types, 104 InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator, 105 @AnimationType int animationType, Handler mainThreadHandler) { 106 mMainThreadHandler = mainThreadHandler; 107 mOuterCallbacks = controller; 108 mControl = new InsetsAnimationControlImpl(controls, frame, state, listener, 109 types, mCallbacks, durationMs, interpolator, animationType); 110 InsetsAnimationThread.getHandler().post(() -> { 111 if (mControl.isCancelled()) { 112 return; 113 } 114 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, 115 "InsetsAsyncAnimation: " + WindowInsets.Type.toString(types), types); 116 listener.onReady(mControl, types); 117 }); 118 } 119 releaseControls(SparseArray<InsetsSourceControl> controls)120 private void releaseControls(SparseArray<InsetsSourceControl> controls) { 121 for (int i = controls.size() - 1; i >= 0; i--) { 122 controls.valueAt(i).release(SurfaceControl::release); 123 } 124 } 125 126 @Override 127 @UiThread getTypes()128 public int getTypes() { 129 return mControl.getTypes(); 130 } 131 132 @Override 133 @UiThread cancel()134 public void cancel() { 135 InsetsAnimationThread.getHandler().post(mControl::cancel); 136 } 137 138 @Override 139 @UiThread getAnimation()140 public WindowInsetsAnimation getAnimation() { 141 return mControl.getAnimation(); 142 } 143 144 @Override getAnimationType()145 public int getAnimationType() { 146 return mControl.getAnimationType(); 147 } 148 } 149