blob: 0e71b7643b7d9bb1b599fed15af18a642d5d1a15 [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 }
Jorim Jaggi6d5c8012020-02-28 01:40:27 +010093 };
94
95 @UiThread
96 public InsetsAnimationThreadControlRunner(SparseArray<InsetsSourceControl> controls, Rect frame,
97 InsetsState state, WindowInsetsAnimationControlListener listener,
98 @InsetsType int types,
99 InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator,
Jorim Jaggi5875cca2020-03-17 13:44:57 +0100100 @AnimationType int animationType, Handler mainThreadHandler) {
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100101 mMainThreadHandler = mainThreadHandler;
102 mOuterCallbacks = controller;
Robert Carr53be42a2020-03-31 14:44:36 -0700103 mControl = new InsetsAnimationControlImpl(controls, frame, state, listener,
Jorim Jaggi5875cca2020-03-17 13:44:57 +0100104 types, mCallbacks, durationMs, interpolator, animationType);
Jorim Jaggicb28ae62020-05-14 17:46:32 +0200105 InsetsAnimationThread.getHandler().post(() -> {
106 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW,
107 "InsetsAsyncAnimation: " + WindowInsets.Type.toString(types), types);
108 listener.onReady(mControl, types);
109 });
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100110 }
111
112 private void releaseControls(SparseArray<InsetsSourceControl> controls) {
113 for (int i = controls.size() - 1; i >= 0; i--) {
114 controls.valueAt(i).release(SurfaceControl::release);
115 }
116 }
117
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100118 @Override
119 @UiThread
120 public int getTypes() {
121 return mControl.getTypes();
122 }
123
124 @Override
125 @UiThread
126 public void cancel() {
127 InsetsAnimationThread.getHandler().post(mControl::cancel);
128 }
129
130 @Override
131 @UiThread
132 public WindowInsetsAnimation getAnimation() {
133 return mControl.getAnimation();
134 }
135
136 @Override
137 public int getAnimationType() {
138 return mControl.getAnimationType();
139 }
140}