blob: 95b2c7019dbc8ce45c504d6a8f5860b7b184c000 [file] [log] [blame]
Wale Ogunwale4f52bc62015-09-24 13:47:31 -07001/*
2 * Copyright (C) 2015 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
Artur Satayevdf439592019-12-10 17:47:53 +000019import android.compat.annotation.UnsupportedAppUsage;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070020import android.os.Looper;
21
22/**
23 * Similar to {@link InputEventReceiver}, but batches events to vsync boundaries when possible.
24 * @hide
25 */
26public class BatchedInputEventReceiver extends InputEventReceiver {
27 Choreographer mChoreographer;
28 private boolean mBatchedInputScheduled;
29
Mathew Inwooda570dee2018-08-17 14:56:00 +010030 @UnsupportedAppUsage
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070031 public BatchedInputEventReceiver(
32 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
33 super(inputChannel, looper);
34 mChoreographer = choreographer;
35 }
36
37 @Override
38 public void onBatchedInputEventPending() {
39 scheduleBatchedInput();
40 }
41
42 @Override
43 public void dispose() {
44 unscheduleBatchedInput();
45 super.dispose();
46 }
47
48 void doConsumeBatchedInput(long frameTimeNanos) {
49 if (mBatchedInputScheduled) {
50 mBatchedInputScheduled = false;
51 if (consumeBatchedInputEvents(frameTimeNanos) && frameTimeNanos != -1) {
52 // If we consumed a batch here, we want to go ahead and schedule the
53 // consumption of batched input events on the next frame. Otherwise, we would
54 // wait until we have more input events pending and might get starved by other
55 // things occurring in the process. If the frame time is -1, however, then
56 // we're in a non-batching mode, so there's no need to schedule this.
57 scheduleBatchedInput();
58 }
59 }
60 }
61
62 private void scheduleBatchedInput() {
63 if (!mBatchedInputScheduled) {
64 mBatchedInputScheduled = true;
65 mChoreographer.postCallback(Choreographer.CALLBACK_INPUT, mBatchedInputRunnable, null);
66 }
67 }
68
69 private void unscheduleBatchedInput() {
70 if (mBatchedInputScheduled) {
71 mBatchedInputScheduled = false;
72 mChoreographer.removeCallbacks(
73 Choreographer.CALLBACK_INPUT, mBatchedInputRunnable, null);
74 }
75 }
76
77 private final class BatchedInputRunnable implements Runnable {
78 @Override
79 public void run() {
80 doConsumeBatchedInput(mChoreographer.getFrameTimeNanos());
81 }
82 }
83 private final BatchedInputRunnable mBatchedInputRunnable = new BatchedInputRunnable();
84}