Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 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 | |
Taran Singh | 85661e3 | 2020-05-07 14:45:34 -0700 | [diff] [blame] | 19 | import static android.view.InsetsController.DEBUG; |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 20 | import static android.view.SyncRtSurfaceTransactionApplier.applyParams; |
| 21 | |
| 22 | import android.annotation.UiThread; |
| 23 | import android.graphics.Rect; |
| 24 | import android.os.Handler; |
Jorim Jaggi | cb28ae6 | 2020-05-14 17:46:32 +0200 | [diff] [blame] | 25 | import android.os.Trace; |
Taran Singh | 85661e3 | 2020-05-07 14:45:34 -0700 | [diff] [blame] | 26 | import android.util.Log; |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 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 | |
Taran Singh | 85661e3 | 2020-05-07 14:45:34 -0700 | [diff] [blame] | 42 | private static final String TAG = "InsetsAnimThreadRunner"; |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 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 |
Adrian Roos | 6a4448f | 2020-04-01 15:01:08 +0200 | [diff] [blame] | 61 | public void scheduleApplyChangeInsets(InsetsAnimationControlRunner runner) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 62 | mControl.applyChangeInsets(mState); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) { |
Jorim Jaggi | cb28ae6 | 2020-05-14 17:46:32 +0200 | [diff] [blame] | 67 | Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, |
| 68 | "InsetsAsyncAnimation: " + WindowInsets.Type.toString(runner.getTypes()), |
| 69 | runner.getTypes()); |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 70 | releaseControls(mControl.getControls()); |
| 71 | mMainThreadHandler.post(() -> |
| 72 | mOuterCallbacks.notifyFinished(InsetsAnimationThreadControlRunner.this, shown)); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void applySurfaceParams(SurfaceParams... params) { |
Taran Singh | 85661e3 | 2020-05-07 14:45:34 -0700 | [diff] [blame] | 77 | if (DEBUG) Log.d(TAG, "applySurfaceParams"); |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 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 | } |
Rob Carr | 3a367c4b | 2020-03-10 15:51:35 -0700 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | public void releaseSurfaceControlFromRt(SurfaceControl sc) { |
Taran Singh | 85661e3 | 2020-05-07 14:45:34 -0700 | [diff] [blame] | 89 | if (DEBUG) Log.d(TAG, "releaseSurfaceControlFromRt"); |
Rob Carr | 3a367c4b | 2020-03-10 15:51:35 -0700 | [diff] [blame] | 90 | // Since we don't push the SurfaceParams to the RT we can release directly |
| 91 | sc.release(); |
| 92 | } |
Adrian Roos | c22eec9 | 2020-06-12 18:48:10 +0200 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | public void reportPerceptible(int types, boolean perceptible) { |
| 96 | mMainThreadHandler.post(() -> mOuterCallbacks.reportPerceptible(types, perceptible)); |
| 97 | } |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | @UiThread |
| 101 | public InsetsAnimationThreadControlRunner(SparseArray<InsetsSourceControl> controls, Rect frame, |
| 102 | InsetsState state, WindowInsetsAnimationControlListener listener, |
| 103 | @InsetsType int types, |
| 104 | InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 105 | @AnimationType int animationType, Handler mainThreadHandler) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 106 | mMainThreadHandler = mainThreadHandler; |
| 107 | mOuterCallbacks = controller; |
Robert Carr | 53be42a | 2020-03-31 14:44:36 -0700 | [diff] [blame] | 108 | mControl = new InsetsAnimationControlImpl(controls, frame, state, listener, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 109 | types, mCallbacks, durationMs, interpolator, animationType); |
Jorim Jaggi | cb28ae6 | 2020-05-14 17:46:32 +0200 | [diff] [blame] | 110 | InsetsAnimationThread.getHandler().post(() -> { |
| 111 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, |
| 112 | "InsetsAsyncAnimation: " + WindowInsets.Type.toString(types), types); |
| 113 | listener.onReady(mControl, types); |
| 114 | }); |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private void releaseControls(SparseArray<InsetsSourceControl> controls) { |
| 118 | for (int i = controls.size() - 1; i >= 0; i--) { |
| 119 | controls.valueAt(i).release(SurfaceControl::release); |
| 120 | } |
| 121 | } |
| 122 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 123 | @Override |
| 124 | @UiThread |
| 125 | public int getTypes() { |
| 126 | return mControl.getTypes(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | @UiThread |
| 131 | public void cancel() { |
| 132 | InsetsAnimationThread.getHandler().post(mControl::cancel); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | @UiThread |
| 137 | public WindowInsetsAnimation getAnimation() { |
| 138 | return mControl.getAnimation(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int getAnimationType() { |
| 143 | return mControl.getAnimationType(); |
| 144 | } |
| 145 | } |