| Chavi Weingarten | d57801e | 2024-03-04 22:49:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 | #include <android/choreographer.h> |
| 18 | #include <android/surface_control_input_receiver.h> |
| 19 | #include <binder/Binder.h> |
| 20 | #include <gui/Choreographer.h> |
| 21 | #include <gui/InputTransferToken.h> |
| 22 | #include <input/Input.h> |
| 23 | #include <input/InputConsumerNoResampling.h> |
| 24 | |
| 25 | #include "android_view_WindowManagerGlobal.h" |
| 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | extern void InputTransferToken_acquire(InputTransferToken* inputTransferToken); |
| 30 | |
| 31 | struct AInputReceiverCallbacks { |
| 32 | AInputReceiverCallbacks(void* context) : context(context) {} |
| 33 | void* context; |
| 34 | AInputReceiver_onMotionEvent onMotionEvent = nullptr; |
| 35 | AInputReceiver_onKeyEvent onKeyEvent = nullptr; |
| 36 | }; |
| 37 | |
| 38 | class InputReceiver : public InputConsumerCallbacks { |
| 39 | public: |
| 40 | InputReceiver(const sp<Looper>& looper, const std::shared_ptr<InputChannel>& inputChannel, |
| 41 | const sp<IBinder>& clientToken, const sp<InputTransferToken>& inputTransferToken, |
| 42 | AInputReceiverCallbacks* callbacks) |
| 43 | : mCallbacks(callbacks), |
| 44 | mInputConsumer(inputChannel, looper, *this), |
| 45 | mClientToken(clientToken), |
| 46 | mInputTransferToken(inputTransferToken) {} |
| 47 | |
| Chavi Weingarten | 57a62b3 | 2024-03-18 20:02:54 +0000 | [diff] [blame^] | 48 | // The InputConsumer does not keep the InputReceiver alive so the receiver is cleared once the |
| 49 | // owner releases it. |
| Chavi Weingarten | d57801e | 2024-03-04 22:49:03 +0000 | [diff] [blame] | 50 | ~InputReceiver() { |
| 51 | remove(); |
| 52 | } |
| 53 | |
| 54 | void onKeyEvent(std::unique_ptr<KeyEvent> event, uint32_t seq) override { |
| 55 | if (mCallbacks->onKeyEvent != nullptr) { |
| 56 | const bool handled = mCallbacks->onKeyEvent(mCallbacks->context, |
| 57 | static_cast<AInputEvent*>(event.release())); |
| 58 | mInputConsumer.finishInputEvent(seq, handled); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void onMotionEvent(std::unique_ptr<MotionEvent> event, uint32_t seq) override { |
| 63 | if (mCallbacks->onMotionEvent != nullptr) { |
| 64 | const bool handled = |
| 65 | mCallbacks->onMotionEvent(mCallbacks->context, |
| 66 | static_cast<AInputEvent*>(event.release())); |
| 67 | mInputConsumer.finishInputEvent(seq, handled); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void onFocusEvent(std::unique_ptr<FocusEvent>, uint32_t seq) override { |
| 72 | mInputConsumer.finishInputEvent(seq, false); |
| 73 | } |
| 74 | void onCaptureEvent(std::unique_ptr<CaptureEvent>, uint32_t seq) override { |
| 75 | mInputConsumer.finishInputEvent(seq, false); |
| 76 | } |
| 77 | void onDragEvent(std::unique_ptr<DragEvent>, uint32_t seq) override { |
| 78 | mInputConsumer.finishInputEvent(seq, false); |
| 79 | } |
| 80 | void onTouchModeEvent(std::unique_ptr<TouchModeEvent>, uint32_t seq) override { |
| 81 | mInputConsumer.finishInputEvent(seq, false); |
| 82 | } |
| 83 | |
| 84 | virtual void onBatchedInputEventPending(int32_t) override { |
| 85 | mInputConsumer.consumeBatchedInputEvents(std::nullopt); |
| 86 | } |
| 87 | |
| 88 | const AInputTransferToken* getInputTransferToken() { |
| 89 | InputTransferToken_acquire(mInputTransferToken.get()); |
| 90 | return reinterpret_cast<const AInputTransferToken*>(mInputTransferToken.get()); |
| 91 | } |
| 92 | |
| 93 | void remove() { |
| 94 | removeInputChannel(mClientToken); |
| 95 | } |
| 96 | |
| 97 | AInputReceiverCallbacks* mCallbacks; |
| 98 | |
| 99 | protected: |
| 100 | InputConsumerNoResampling mInputConsumer; |
| 101 | |
| 102 | private: |
| 103 | const sp<IBinder> mClientToken; |
| 104 | const sp<InputTransferToken> mInputTransferToken; |
| 105 | }; |
| 106 | |
| 107 | class BatchedInputReceiver : public InputReceiver { |
| 108 | public: |
| 109 | BatchedInputReceiver(Choreographer& choreographer, |
| 110 | const std::shared_ptr<InputChannel>& inputChannel, |
| 111 | const sp<IBinder>& clientToken, |
| 112 | const sp<InputTransferToken>& inputTransferToken, |
| 113 | AInputReceiverCallbacks* callbacks) |
| 114 | : InputReceiver(choreographer.getLooper(), inputChannel, clientToken, inputTransferToken, |
| 115 | callbacks), |
| 116 | mChoreographer(choreographer) {} |
| 117 | |
| 118 | static void vsyncCallback(const AChoreographerFrameCallbackData* callbackData, void* data) { |
| 119 | BatchedInputReceiver* receiver = static_cast<BatchedInputReceiver*>(data); |
| 120 | receiver->onVsyncCallback(callbackData); |
| 121 | } |
| 122 | |
| 123 | void onVsyncCallback(const AChoreographerFrameCallbackData* callbackData) { |
| 124 | int64_t frameTimeNanos = AChoreographerFrameCallbackData_getFrameTimeNanos(callbackData); |
| 125 | mInputConsumer.consumeBatchedInputEvents(frameTimeNanos); |
| 126 | mBatchedInputScheduled = false; |
| 127 | } |
| 128 | |
| 129 | void onBatchedInputEventPending(int32_t) override { |
| 130 | scheduleBatchedInput(); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | Choreographer& mChoreographer; |
| 135 | bool mBatchedInputScheduled = false; |
| 136 | |
| 137 | void scheduleBatchedInput() { |
| 138 | if (!mBatchedInputScheduled) { |
| 139 | mBatchedInputScheduled = true; |
| 140 | mChoreographer.postFrameCallbackDelayed(nullptr, nullptr, vsyncCallback, this, 0, |
| 141 | CallbackType::CALLBACK_INPUT); |
| 142 | } |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | static inline AInputReceiver* InputReceiver_to_AInputReceiver(InputReceiver* inputReceiver) { |
| 147 | return reinterpret_cast<AInputReceiver*>(inputReceiver); |
| 148 | } |
| 149 | |
| 150 | static inline InputReceiver* AInputReceiver_to_InputReceiver(AInputReceiver* aInputReceiver) { |
| 151 | return reinterpret_cast<InputReceiver*>(aInputReceiver); |
| 152 | } |
| 153 | |
| 154 | AInputReceiver* AInputReceiver_createBatchedInputReceiver(AChoreographer* aChoreographer, |
| 155 | const AInputTransferToken* hostToken, |
| 156 | const ASurfaceControl* aSurfaceControl, |
| 157 | AInputReceiverCallbacks* callbacks) { |
| 158 | // create input channel here through WMS |
| 159 | sp<IBinder> clientToken = sp<BBinder>::make(); |
| 160 | sp<InputTransferToken> clientInputTransferToken = sp<InputTransferToken>::make(); |
| 161 | |
| 162 | std::shared_ptr<InputChannel> inputChannel = |
| 163 | createInputChannel(clientToken, reinterpret_cast<const InputTransferToken&>(*hostToken), |
| 164 | reinterpret_cast<const SurfaceControl&>(*aSurfaceControl), |
| 165 | *clientInputTransferToken); |
| 166 | return InputReceiver_to_AInputReceiver( |
| 167 | new BatchedInputReceiver(reinterpret_cast<Choreographer&>(*aChoreographer), |
| 168 | inputChannel, clientToken, clientInputTransferToken, |
| 169 | callbacks)); |
| 170 | } |
| 171 | |
| 172 | AInputReceiver* AInputReceiver_createUnbatchedInputReceiver(ALooper* aLooper, |
| 173 | const AInputTransferToken* hostToken, |
| 174 | const ASurfaceControl* aSurfaceControl, |
| 175 | AInputReceiverCallbacks* callbacks) { |
| 176 | // create input channel here through WMS |
| 177 | sp<IBinder> clientToken = sp<BBinder>::make(); |
| 178 | sp<InputTransferToken> clientInputTransferToken = sp<InputTransferToken>::make(); |
| 179 | |
| 180 | std::shared_ptr<InputChannel> inputChannel = |
| 181 | createInputChannel(clientToken, reinterpret_cast<const InputTransferToken&>(*hostToken), |
| 182 | reinterpret_cast<const SurfaceControl&>(*aSurfaceControl), |
| 183 | *clientInputTransferToken); |
| 184 | return InputReceiver_to_AInputReceiver(new InputReceiver(reinterpret_cast<Looper*>(aLooper), |
| 185 | inputChannel, clientToken, |
| 186 | clientInputTransferToken, callbacks)); |
| 187 | } |
| 188 | |
| 189 | const AInputTransferToken* AInputReceiver_getInputTransferToken(AInputReceiver* aInputReceiver) { |
| 190 | return AInputReceiver_to_InputReceiver(aInputReceiver)->getInputTransferToken(); |
| 191 | } |
| 192 | |
| 193 | void AInputReceiver_release(AInputReceiver* aInputReceiver) { |
| 194 | InputReceiver* inputReceiver = AInputReceiver_to_InputReceiver(aInputReceiver); |
| 195 | inputReceiver->remove(); |
| 196 | delete inputReceiver; |
| 197 | } |
| 198 | |
| 199 | void AInputReceiverCallbacks_setMotionEventCallback(AInputReceiverCallbacks* callbacks, |
| 200 | AInputReceiver_onMotionEvent onMotionEvent) { |
| 201 | callbacks->onMotionEvent = onMotionEvent; |
| 202 | } |
| 203 | |
| 204 | void AInputReceiverCallbacks_setKeyEventCallback(AInputReceiverCallbacks* callbacks, |
| 205 | AInputReceiver_onKeyEvent onKeyEvent) { |
| 206 | callbacks->onKeyEvent = onKeyEvent; |
| 207 | } |
| 208 | |
| 209 | AInputReceiverCallbacks* AInputReceiverCallbacks_create(void* context) { |
| 210 | return new AInputReceiverCallbacks(context); |
| 211 | } |
| 212 | |
| 213 | void AInputReceiverCallbacks_release(AInputReceiverCallbacks* callbacks) { |
| 214 | delete callbacks; |
| 215 | } |