1 /* 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 17 package android.view; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.os.Looper; 21 22 /** 23 * Similar to {@link InputEventReceiver}, but batches events to vsync boundaries when possible. 24 * @hide 25 */ 26 public class BatchedInputEventReceiver extends InputEventReceiver { 27 private Choreographer mChoreographer; 28 private boolean mBatchingEnabled; 29 private boolean mBatchedInputScheduled; 30 31 @UnsupportedAppUsage BatchedInputEventReceiver( InputChannel inputChannel, Looper looper, Choreographer choreographer)32 public BatchedInputEventReceiver( 33 InputChannel inputChannel, Looper looper, Choreographer choreographer) { 34 super(inputChannel, looper); 35 mChoreographer = choreographer; 36 mBatchingEnabled = true; 37 } 38 39 @Override onBatchedInputEventPending(int source)40 public void onBatchedInputEventPending(int source) { 41 if (mBatchingEnabled) { 42 scheduleBatchedInput(); 43 } else { 44 consumeBatchedInputEvents(-1); 45 } 46 } 47 48 @Override dispose()49 public void dispose() { 50 unscheduleBatchedInput(); 51 consumeBatchedInputEvents(-1); 52 super.dispose(); 53 } 54 55 /** 56 * Sets whether to enable batching on this input event receiver. 57 * @hide 58 */ setBatchingEnabled(boolean batchingEnabled)59 public void setBatchingEnabled(boolean batchingEnabled) { 60 mBatchingEnabled = batchingEnabled; 61 if (!batchingEnabled) { 62 unscheduleBatchedInput(); 63 consumeBatchedInputEvents(-1); 64 } 65 } 66 doConsumeBatchedInput(long frameTimeNanos)67 void doConsumeBatchedInput(long frameTimeNanos) { 68 if (mBatchedInputScheduled) { 69 mBatchedInputScheduled = false; 70 if (consumeBatchedInputEvents(frameTimeNanos) && frameTimeNanos != -1) { 71 // If we consumed a batch here, we want to go ahead and schedule the 72 // consumption of batched input events on the next frame. Otherwise, we would 73 // wait until we have more input events pending and might get starved by other 74 // things occurring in the process. If the frame time is -1, however, then 75 // we're in a non-batching mode, so there's no need to schedule this. 76 scheduleBatchedInput(); 77 } 78 } 79 } 80 scheduleBatchedInput()81 private void scheduleBatchedInput() { 82 if (!mBatchedInputScheduled) { 83 mBatchedInputScheduled = true; 84 mChoreographer.postCallback(Choreographer.CALLBACK_INPUT, mBatchedInputRunnable, null); 85 } 86 } 87 unscheduleBatchedInput()88 private void unscheduleBatchedInput() { 89 if (mBatchedInputScheduled) { 90 mBatchedInputScheduled = false; 91 mChoreographer.removeCallbacks( 92 Choreographer.CALLBACK_INPUT, mBatchedInputRunnable, null); 93 } 94 } 95 96 private final class BatchedInputRunnable implements Runnable { 97 @Override run()98 public void run() { 99 doConsumeBatchedInput(mChoreographer.getFrameTimeNanos()); 100 } 101 } 102 private final BatchedInputRunnable mBatchedInputRunnable = new BatchedInputRunnable(); 103 } 104