blob: 123604489da498468c5e5ce9997eedc1d3dfa597 [file] [log] [blame]
Jorim Jaggi6d5c8012020-02-28 01:40:27 +01001/*
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
17package android.view;
18
Taran Singh85661e32020-05-07 14:45:34 -070019import static android.view.InsetsController.DEBUG;
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010020import static android.view.SyncRtSurfaceTransactionApplier.applyParams;
21
22import android.annotation.UiThread;
23import android.graphics.Rect;
24import android.os.Handler;
Jorim Jaggicb28ae62020-05-14 17:46:32 +020025import android.os.Trace;
Taran Singh85661e32020-05-07 14:45:34 -070026import android.util.Log;
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010027import android.util.SparseArray;
28import android.view.InsetsController.AnimationType;
29import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams;
30import android.view.WindowInsets.Type.InsetsType;
31import android.view.WindowInsetsAnimation.Bounds;
32import 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 */
40public class InsetsAnimationThreadControlRunner implements InsetsAnimationControlRunner {
41
Taran Singh85661e32020-05-07 14:45:34 -070042 private static final String TAG = "InsetsAnimThreadRunner";
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010043 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 Roos6a4448f2020-04-01 15:01:08 +020061 public void scheduleApplyChangeInsets(InsetsAnimationControlRunner runner) {
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010062 mControl.applyChangeInsets(mState);
63 }
64
65 @Override
66 public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) {
Jorim Jaggicb28ae62020-05-14 17:46:32 +020067 Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW,
68 "InsetsAsyncAnimation: " + WindowInsets.Type.toString(runner.getTypes()),
69 runner.getTypes());
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010070 releaseControls(mControl.getControls());
71 mMainThreadHandler.post(() ->
72 mOuterCallbacks.notifyFinished(InsetsAnimationThreadControlRunner.this, shown));
73 }
74
75 @Override
76 public void applySurfaceParams(SurfaceParams... params) {
Taran Singh85661e32020-05-07 14:45:34 -070077 if (DEBUG) Log.d(TAG, "applySurfaceParams");
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010078 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 Carr3a367c42020-03-10 15:51:35 -070086
87 @Override
88 public void releaseSurfaceControlFromRt(SurfaceControl sc) {
Taran Singh85661e32020-05-07 14:45:34 -070089 if (DEBUG) Log.d(TAG, "releaseSurfaceControlFromRt");
Rob Carr3a367c42020-03-10 15:51:35 -070090 // Since we don't push the SurfaceParams to the RT we can release directly
91 sc.release();
92 }
Adrian Roosc22eec92020-06-12 18:48:10 +020093
94 @Override
95 public void reportPerceptible(int types, boolean perceptible) {
96 mMainThreadHandler.post(() -> mOuterCallbacks.reportPerceptible(types, perceptible));
97 }
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010098 };
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 Jaggi5875cca2020-03-17 13:44:57 +0100105 @AnimationType int animationType, Handler mainThreadHandler) {
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100106 mMainThreadHandler = mainThreadHandler;
107 mOuterCallbacks = controller;
Robert Carr53be42a2020-03-31 14:44:36 -0700108 mControl = new InsetsAnimationControlImpl(controls, frame, state, listener,
Jorim Jaggi5875cca2020-03-17 13:44:57 +0100109 types, mCallbacks, durationMs, interpolator, animationType);
Jorim Jaggicb28ae62020-05-14 17:46:32 +0200110 InsetsAnimationThread.getHandler().post(() -> {
111 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW,
112 "InsetsAsyncAnimation: " + WindowInsets.Type.toString(types), types);
113 listener.onReady(mControl, types);
114 });
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100115 }
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 Jaggi6d5c8012020-02-28 01:40:27 +0100123 @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}