Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 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 | |
Paul Ramirez | e2bb187 | 2024-08-12 20:21:13 +0000 | [diff] [blame] | 17 | #define LOG_TAG "InputConsumerNoResampling" |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 20 | #include <chrono> |
| 21 | |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | #include <android-base/properties.h> |
| 26 | #include <android-base/stringprintf.h> |
| 27 | #include <cutils/properties.h> |
| 28 | #include <ftl/enum.h> |
| 29 | #include <utils/Trace.h> |
| 30 | |
| 31 | #include <com_android_input_flags.h> |
| 32 | #include <input/InputConsumerNoResampling.h> |
| 33 | #include <input/PrintTools.h> |
| 34 | #include <input/TraceTools.h> |
| 35 | |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | |
| 38 | namespace { |
| 39 | |
Paul Ramirez | cd7488c | 2024-09-13 23:01:12 +0000 | [diff] [blame^] | 40 | using std::chrono::nanoseconds; |
| 41 | |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Log debug messages relating to the consumer end of the transport channel. |
| 44 | * Enable this via "adb shell setprop log.tag.InputTransportConsumer DEBUG" (requires restart) |
| 45 | */ |
| 46 | const bool DEBUG_TRANSPORT_CONSUMER = |
| 47 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Consumer", ANDROID_LOG_INFO); |
| 48 | |
Paul Ramirez | e2bb187 | 2024-08-12 20:21:13 +0000 | [diff] [blame] | 49 | /** |
| 50 | * RealLooper is a wrapper of Looper. All the member functions exclusively call the internal looper. |
| 51 | * This class' behavior is the same as Looper. |
| 52 | */ |
| 53 | class RealLooper final : public LooperInterface { |
| 54 | public: |
| 55 | RealLooper(sp<Looper> looper) : mLooper{looper} {} |
| 56 | |
| 57 | int addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, |
| 58 | void* data) override { |
| 59 | return mLooper->addFd(fd, ident, events, callback, data); |
| 60 | } |
| 61 | |
| 62 | int removeFd(int fd) override { return mLooper->removeFd(fd); } |
| 63 | |
| 64 | sp<Looper> getLooper() const override { return mLooper; } |
| 65 | |
| 66 | private: |
| 67 | sp<Looper> mLooper; |
| 68 | }; |
| 69 | |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 70 | std::unique_ptr<KeyEvent> createKeyEvent(const InputMessage& msg) { |
| 71 | std::unique_ptr<KeyEvent> event = std::make_unique<KeyEvent>(); |
| 72 | event->initialize(msg.body.key.eventId, msg.body.key.deviceId, msg.body.key.source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 73 | ui::LogicalDisplayId{msg.body.key.displayId}, msg.body.key.hmac, |
| 74 | msg.body.key.action, msg.body.key.flags, msg.body.key.keyCode, |
| 75 | msg.body.key.scanCode, msg.body.key.metaState, msg.body.key.repeatCount, |
| 76 | msg.body.key.downTime, msg.body.key.eventTime); |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 77 | return event; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 80 | std::unique_ptr<FocusEvent> createFocusEvent(const InputMessage& msg) { |
| 81 | std::unique_ptr<FocusEvent> event = std::make_unique<FocusEvent>(); |
| 82 | event->initialize(msg.body.focus.eventId, msg.body.focus.hasFocus); |
| 83 | return event; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 86 | std::unique_ptr<CaptureEvent> createCaptureEvent(const InputMessage& msg) { |
| 87 | std::unique_ptr<CaptureEvent> event = std::make_unique<CaptureEvent>(); |
| 88 | event->initialize(msg.body.capture.eventId, msg.body.capture.pointerCaptureEnabled); |
| 89 | return event; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 92 | std::unique_ptr<DragEvent> createDragEvent(const InputMessage& msg) { |
| 93 | std::unique_ptr<DragEvent> event = std::make_unique<DragEvent>(); |
| 94 | event->initialize(msg.body.drag.eventId, msg.body.drag.x, msg.body.drag.y, |
| 95 | msg.body.drag.isExiting); |
| 96 | return event; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 99 | std::unique_ptr<MotionEvent> createMotionEvent(const InputMessage& msg) { |
| 100 | std::unique_ptr<MotionEvent> event = std::make_unique<MotionEvent>(); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 101 | const uint32_t pointerCount = msg.body.motion.pointerCount; |
| 102 | std::vector<PointerProperties> pointerProperties; |
| 103 | pointerProperties.reserve(pointerCount); |
| 104 | std::vector<PointerCoords> pointerCoords; |
| 105 | pointerCoords.reserve(pointerCount); |
| 106 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 107 | pointerProperties.push_back(msg.body.motion.pointers[i].properties); |
| 108 | pointerCoords.push_back(msg.body.motion.pointers[i].coords); |
| 109 | } |
| 110 | |
| 111 | ui::Transform transform; |
| 112 | transform.set({msg.body.motion.dsdx, msg.body.motion.dtdx, msg.body.motion.tx, |
| 113 | msg.body.motion.dtdy, msg.body.motion.dsdy, msg.body.motion.ty, 0, 0, 1}); |
| 114 | ui::Transform displayTransform; |
| 115 | displayTransform.set({msg.body.motion.dsdxRaw, msg.body.motion.dtdxRaw, msg.body.motion.txRaw, |
| 116 | msg.body.motion.dtdyRaw, msg.body.motion.dsdyRaw, msg.body.motion.tyRaw, |
| 117 | 0, 0, 1}); |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 118 | event->initialize(msg.body.motion.eventId, msg.body.motion.deviceId, msg.body.motion.source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 119 | ui::LogicalDisplayId{msg.body.motion.displayId}, msg.body.motion.hmac, |
| 120 | msg.body.motion.action, msg.body.motion.actionButton, msg.body.motion.flags, |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 121 | msg.body.motion.edgeFlags, msg.body.motion.metaState, |
| 122 | msg.body.motion.buttonState, msg.body.motion.classification, transform, |
| 123 | msg.body.motion.xPrecision, msg.body.motion.yPrecision, |
| 124 | msg.body.motion.xCursorPosition, msg.body.motion.yCursorPosition, |
| 125 | displayTransform, msg.body.motion.downTime, msg.body.motion.eventTime, |
| 126 | pointerCount, pointerProperties.data(), pointerCoords.data()); |
| 127 | return event; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void addSample(MotionEvent& event, const InputMessage& msg) { |
| 131 | uint32_t pointerCount = msg.body.motion.pointerCount; |
| 132 | std::vector<PointerCoords> pointerCoords; |
| 133 | pointerCoords.reserve(pointerCount); |
| 134 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 135 | pointerCoords.push_back(msg.body.motion.pointers[i].coords); |
| 136 | } |
| 137 | |
| 138 | // TODO(b/329770983): figure out if it's safe to combine events with mismatching metaState |
| 139 | event.setMetaState(event.getMetaState() | msg.body.motion.metaState); |
jioana | 71c6f73 | 2024-07-16 15:42:56 +0000 | [diff] [blame] | 140 | event.addSample(msg.body.motion.eventTime, pointerCoords.data(), msg.body.motion.eventId); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 143 | std::unique_ptr<TouchModeEvent> createTouchModeEvent(const InputMessage& msg) { |
| 144 | std::unique_ptr<TouchModeEvent> event = std::make_unique<TouchModeEvent>(); |
| 145 | event->initialize(msg.body.touchMode.eventId, msg.body.touchMode.isInTouchMode); |
| 146 | return event; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | std::string outboundMessageToString(const InputMessage& outboundMsg) { |
| 150 | switch (outboundMsg.header.type) { |
| 151 | case InputMessage::Type::FINISHED: { |
| 152 | return android::base::StringPrintf(" Finish: seq=%" PRIu32 " handled=%s", |
| 153 | outboundMsg.header.seq, |
| 154 | toString(outboundMsg.body.finished.handled)); |
| 155 | } |
| 156 | case InputMessage::Type::TIMELINE: { |
| 157 | return android::base:: |
| 158 | StringPrintf(" Timeline: inputEventId=%" PRId32 " gpuCompletedTime=%" PRId64 |
| 159 | ", presentTime=%" PRId64, |
| 160 | outboundMsg.body.timeline.eventId, |
| 161 | outboundMsg.body.timeline |
| 162 | .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME], |
| 163 | outboundMsg.body.timeline |
| 164 | .graphicsTimeline[GraphicsTimeline::PRESENT_TIME]); |
| 165 | } |
| 166 | default: { |
| 167 | LOG(FATAL) << "Outbound message must be FINISHED or TIMELINE, got " |
| 168 | << ftl::enum_string(outboundMsg.header.type); |
| 169 | return "Unreachable"; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | InputMessage createFinishedMessage(uint32_t seq, bool handled, nsecs_t consumeTime) { |
| 175 | InputMessage msg; |
| 176 | msg.header.type = InputMessage::Type::FINISHED; |
| 177 | msg.header.seq = seq; |
| 178 | msg.body.finished.handled = handled; |
| 179 | msg.body.finished.consumeTime = consumeTime; |
| 180 | return msg; |
| 181 | } |
| 182 | |
| 183 | InputMessage createTimelineMessage(int32_t inputEventId, nsecs_t gpuCompletedTime, |
| 184 | nsecs_t presentTime) { |
| 185 | InputMessage msg; |
| 186 | msg.header.type = InputMessage::Type::TIMELINE; |
| 187 | msg.header.seq = 0; |
| 188 | msg.body.timeline.eventId = inputEventId; |
| 189 | msg.body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = gpuCompletedTime; |
| 190 | msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = presentTime; |
| 191 | return msg; |
| 192 | } |
| 193 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 194 | bool isPointerEvent(const MotionEvent& motionEvent) { |
| 195 | return (motionEvent.getSource() & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER; |
| 196 | } |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 197 | } // namespace |
| 198 | |
| 199 | using android::base::Result; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 200 | |
| 201 | // --- InputConsumerNoResampling --- |
| 202 | |
| 203 | InputConsumerNoResampling::InputConsumerNoResampling(const std::shared_ptr<InputChannel>& channel, |
Paul Ramirez | e2bb187 | 2024-08-12 20:21:13 +0000 | [diff] [blame] | 204 | std::shared_ptr<LooperInterface> looper, |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 205 | InputConsumerCallbacks& callbacks, |
| 206 | std::unique_ptr<Resampler> resampler) |
Paul Ramirez | e2bb187 | 2024-08-12 20:21:13 +0000 | [diff] [blame] | 207 | : mChannel{channel}, |
| 208 | mLooper{looper}, |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 209 | mCallbacks(callbacks), |
Paul Ramirez | e2bb187 | 2024-08-12 20:21:13 +0000 | [diff] [blame] | 210 | mResampler{std::move(resampler)}, |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 211 | mFdEvents(0) { |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 212 | LOG_ALWAYS_FATAL_IF(mLooper == nullptr); |
| 213 | mCallback = sp<LooperEventCallback>::make( |
| 214 | std::bind(&InputConsumerNoResampling::handleReceiveCallback, this, |
| 215 | std::placeholders::_1)); |
| 216 | // In the beginning, there are no pending outbounds events; we only care about receiving |
| 217 | // incoming data. |
| 218 | setFdEvents(ALOOPER_EVENT_INPUT); |
| 219 | } |
| 220 | |
Paul Ramirez | e2bb187 | 2024-08-12 20:21:13 +0000 | [diff] [blame] | 221 | InputConsumerNoResampling::InputConsumerNoResampling(const std::shared_ptr<InputChannel>& channel, |
| 222 | sp<Looper> looper, |
| 223 | InputConsumerCallbacks& callbacks, |
| 224 | std::unique_ptr<Resampler> resampler) |
| 225 | : InputConsumerNoResampling(channel, std::make_shared<RealLooper>(looper), callbacks, |
| 226 | std::move(resampler)) {} |
| 227 | |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 228 | InputConsumerNoResampling::~InputConsumerNoResampling() { |
| 229 | ensureCalledOnLooperThread(__func__); |
| 230 | consumeBatchedInputEvents(std::nullopt); |
| 231 | while (!mOutboundQueue.empty()) { |
| 232 | processOutboundEvents(); |
| 233 | // This is our last chance to ack the events. If we don't ack them here, we will get an ANR, |
| 234 | // so keep trying to send the events as long as they are present in the queue. |
| 235 | } |
| 236 | setFdEvents(0); |
| 237 | } |
| 238 | |
| 239 | int InputConsumerNoResampling::handleReceiveCallback(int events) { |
| 240 | // Allowed return values of this function as documented in LooperCallback::handleEvent |
| 241 | constexpr int REMOVE_CALLBACK = 0; |
| 242 | constexpr int KEEP_CALLBACK = 1; |
| 243 | |
| 244 | if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) { |
| 245 | // This error typically occurs when the publisher has closed the input channel |
| 246 | // as part of removing a window or finishing an IME session, in which case |
| 247 | // the consumer will soon be disposed as well. |
| 248 | if (DEBUG_TRANSPORT_CONSUMER) { |
| 249 | LOG(INFO) << "The channel was hung up or an error occurred: " << mChannel->getName(); |
| 250 | } |
| 251 | return REMOVE_CALLBACK; |
| 252 | } |
| 253 | |
| 254 | int handledEvents = 0; |
| 255 | if (events & ALOOPER_EVENT_INPUT) { |
| 256 | std::vector<InputMessage> messages = readAllMessages(); |
| 257 | handleMessages(std::move(messages)); |
| 258 | handledEvents |= ALOOPER_EVENT_INPUT; |
| 259 | } |
| 260 | |
| 261 | if (events & ALOOPER_EVENT_OUTPUT) { |
| 262 | processOutboundEvents(); |
| 263 | handledEvents |= ALOOPER_EVENT_OUTPUT; |
| 264 | } |
| 265 | if (handledEvents != events) { |
| 266 | LOG(FATAL) << "Mismatch: handledEvents=" << handledEvents << ", events=" << events; |
| 267 | } |
| 268 | return KEEP_CALLBACK; |
| 269 | } |
| 270 | |
| 271 | void InputConsumerNoResampling::processOutboundEvents() { |
| 272 | while (!mOutboundQueue.empty()) { |
| 273 | const InputMessage& outboundMsg = mOutboundQueue.front(); |
| 274 | |
| 275 | const status_t result = mChannel->sendMessage(&outboundMsg); |
| 276 | if (result == OK) { |
| 277 | if (outboundMsg.header.type == InputMessage::Type::FINISHED) { |
| 278 | ATRACE_ASYNC_END("InputConsumer processing", /*cookie=*/outboundMsg.header.seq); |
| 279 | } |
| 280 | // Successful send. Erase the entry and keep trying to send more |
| 281 | mOutboundQueue.pop(); |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | // Publisher is busy, try again later. Keep this entry (do not erase) |
| 286 | if (result == WOULD_BLOCK) { |
| 287 | setFdEvents(ALOOPER_EVENT_INPUT | ALOOPER_EVENT_OUTPUT); |
| 288 | return; // try again later |
| 289 | } |
| 290 | |
| 291 | // Some other error. Give up |
| 292 | LOG(FATAL) << "Failed to send outbound event on channel '" << mChannel->getName() |
| 293 | << "'. status=" << statusToString(result) << "(" << result << ")"; |
| 294 | } |
| 295 | |
| 296 | // The queue is now empty. Tell looper there's no more output to expect. |
| 297 | setFdEvents(ALOOPER_EVENT_INPUT); |
| 298 | } |
| 299 | |
| 300 | void InputConsumerNoResampling::finishInputEvent(uint32_t seq, bool handled) { |
| 301 | ensureCalledOnLooperThread(__func__); |
| 302 | mOutboundQueue.push(createFinishedMessage(seq, handled, popConsumeTime(seq))); |
| 303 | // also produce finish events for all batches for this seq (if any) |
| 304 | const auto it = mBatchedSequenceNumbers.find(seq); |
| 305 | if (it != mBatchedSequenceNumbers.end()) { |
| 306 | for (uint32_t subSeq : it->second) { |
| 307 | mOutboundQueue.push(createFinishedMessage(subSeq, handled, popConsumeTime(subSeq))); |
| 308 | } |
| 309 | mBatchedSequenceNumbers.erase(it); |
| 310 | } |
| 311 | processOutboundEvents(); |
| 312 | } |
| 313 | |
| 314 | bool InputConsumerNoResampling::probablyHasInput() const { |
| 315 | // Ideally, this would only be allowed to run on the looper thread, and in production, it will. |
| 316 | // However, for testing, it's convenient to call this while the looper thread is blocked, so |
| 317 | // we do not call ensureCalledOnLooperThread here. |
| 318 | return (!mBatches.empty()) || mChannel->probablyHasInput(); |
| 319 | } |
| 320 | |
| 321 | void InputConsumerNoResampling::reportTimeline(int32_t inputEventId, nsecs_t gpuCompletedTime, |
| 322 | nsecs_t presentTime) { |
| 323 | ensureCalledOnLooperThread(__func__); |
| 324 | mOutboundQueue.push(createTimelineMessage(inputEventId, gpuCompletedTime, presentTime)); |
| 325 | processOutboundEvents(); |
| 326 | } |
| 327 | |
| 328 | nsecs_t InputConsumerNoResampling::popConsumeTime(uint32_t seq) { |
| 329 | auto it = mConsumeTimes.find(seq); |
| 330 | // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was |
| 331 | // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed. |
| 332 | LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32, |
| 333 | seq); |
| 334 | nsecs_t consumeTime = it->second; |
| 335 | mConsumeTimes.erase(it); |
| 336 | return consumeTime; |
| 337 | } |
| 338 | |
| 339 | void InputConsumerNoResampling::setFdEvents(int events) { |
| 340 | if (mFdEvents != events) { |
| 341 | mFdEvents = events; |
| 342 | if (events != 0) { |
| 343 | mLooper->addFd(mChannel->getFd(), 0, events, mCallback, nullptr); |
| 344 | } else { |
| 345 | mLooper->removeFd(mChannel->getFd()); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | void InputConsumerNoResampling::handleMessages(std::vector<InputMessage>&& messages) { |
| 351 | // TODO(b/297226446) : add resampling |
| 352 | for (const InputMessage& msg : messages) { |
| 353 | if (msg.header.type == InputMessage::Type::MOTION) { |
| 354 | const int32_t action = msg.body.motion.action; |
| 355 | const DeviceId deviceId = msg.body.motion.deviceId; |
| 356 | const int32_t source = msg.body.motion.source; |
| 357 | const bool batchableEvent = (action == AMOTION_EVENT_ACTION_MOVE || |
| 358 | action == AMOTION_EVENT_ACTION_HOVER_MOVE) && |
| 359 | (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER) || |
| 360 | isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK)); |
| 361 | if (batchableEvent) { |
| 362 | // add it to batch |
| 363 | mBatches[deviceId].emplace(msg); |
| 364 | } else { |
| 365 | // consume all pending batches for this event immediately |
| 366 | // TODO(b/329776327): figure out if this could be smarter by limiting the |
| 367 | // consumption only to the current device. |
| 368 | consumeBatchedInputEvents(std::nullopt); |
| 369 | handleMessage(msg); |
| 370 | } |
| 371 | } else { |
| 372 | // Non-motion events shouldn't force the consumption of pending batched events |
| 373 | handleMessage(msg); |
| 374 | } |
| 375 | } |
| 376 | // At the end of this, if we still have pending batches, notify the receiver about it. |
| 377 | |
| 378 | // We need to carefully notify the InputConsumerCallbacks about the pending batch. The receiver |
| 379 | // could choose to consume all events when notified about the batch. That means that the |
| 380 | // "mBatches" variable could change when 'InputConsumerCallbacks::onBatchedInputEventPending' is |
| 381 | // invoked. We also can't notify the InputConsumerCallbacks in a while loop until mBatches is |
| 382 | // empty, because the receiver could choose to not consume the batch immediately. |
| 383 | std::set<int32_t> pendingBatchSources; |
| 384 | for (const auto& [_, pendingMessages] : mBatches) { |
| 385 | // Assume that all messages for a given device has the same source. |
| 386 | pendingBatchSources.insert(pendingMessages.front().body.motion.source); |
| 387 | } |
| 388 | for (const int32_t source : pendingBatchSources) { |
| 389 | const bool sourceStillRemaining = |
| 390 | std::any_of(mBatches.begin(), mBatches.end(), [=](const auto& pair) { |
| 391 | return pair.second.front().body.motion.source == source; |
| 392 | }); |
| 393 | if (sourceStillRemaining) { |
| 394 | mCallbacks.onBatchedInputEventPending(source); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | std::vector<InputMessage> InputConsumerNoResampling::readAllMessages() { |
| 400 | std::vector<InputMessage> messages; |
| 401 | while (true) { |
Paul Ramirez | 0c25e86 | 2024-06-18 21:33:33 +0000 | [diff] [blame] | 402 | android::base::Result<InputMessage> result = mChannel->receiveMessage(); |
| 403 | if (result.ok()) { |
| 404 | const InputMessage& msg = *result; |
| 405 | const auto [_, inserted] = |
| 406 | mConsumeTimes.emplace(msg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC)); |
| 407 | LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32, |
| 408 | msg.header.seq); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 409 | |
Paul Ramirez | 0c25e86 | 2024-06-18 21:33:33 +0000 | [diff] [blame] | 410 | // Trace the event processing timeline - event was just read from the socket |
| 411 | // TODO(b/329777420): distinguish between multiple instances of InputConsumer |
| 412 | // in the same process. |
| 413 | ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/msg.header.seq); |
| 414 | messages.push_back(msg); |
| 415 | } else { // !result.ok() |
| 416 | switch (result.error().code()) { |
| 417 | case WOULD_BLOCK: { |
| 418 | return messages; |
| 419 | } |
| 420 | case DEAD_OBJECT: { |
| 421 | LOG(FATAL) << "Got a dead object for " << mChannel->getName(); |
| 422 | break; |
| 423 | } |
| 424 | case BAD_VALUE: { |
| 425 | LOG(FATAL) << "Got a bad value for " << mChannel->getName(); |
| 426 | break; |
| 427 | } |
| 428 | default: { |
| 429 | LOG(FATAL) << "Unexpected error: " << result.error().message(); |
| 430 | break; |
| 431 | } |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void InputConsumerNoResampling::handleMessage(const InputMessage& msg) const { |
| 438 | switch (msg.header.type) { |
| 439 | case InputMessage::Type::KEY: { |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 440 | std::unique_ptr<KeyEvent> keyEvent = createKeyEvent(msg); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 441 | mCallbacks.onKeyEvent(std::move(keyEvent), msg.header.seq); |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | case InputMessage::Type::MOTION: { |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 446 | std::unique_ptr<MotionEvent> motionEvent = createMotionEvent(msg); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 447 | mCallbacks.onMotionEvent(std::move(motionEvent), msg.header.seq); |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | case InputMessage::Type::FINISHED: |
| 452 | case InputMessage::Type::TIMELINE: { |
Siarhei Vishniakou | 11d223b | 2024-03-26 21:52:38 +0000 | [diff] [blame] | 453 | LOG(FATAL) << "Consumed a " << ftl::enum_string(msg.header.type) |
| 454 | << " message, which should never be seen by InputConsumer on " |
| 455 | << mChannel->getName(); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | |
| 459 | case InputMessage::Type::FOCUS: { |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 460 | std::unique_ptr<FocusEvent> focusEvent = createFocusEvent(msg); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 461 | mCallbacks.onFocusEvent(std::move(focusEvent), msg.header.seq); |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | case InputMessage::Type::CAPTURE: { |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 466 | std::unique_ptr<CaptureEvent> captureEvent = createCaptureEvent(msg); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 467 | mCallbacks.onCaptureEvent(std::move(captureEvent), msg.header.seq); |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | case InputMessage::Type::DRAG: { |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 472 | std::unique_ptr<DragEvent> dragEvent = createDragEvent(msg); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 473 | mCallbacks.onDragEvent(std::move(dragEvent), msg.header.seq); |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | case InputMessage::Type::TOUCH_MODE: { |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 478 | std::unique_ptr<TouchModeEvent> touchModeEvent = createTouchModeEvent(msg); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 479 | mCallbacks.onTouchModeEvent(std::move(touchModeEvent), msg.header.seq); |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
Paul Ramirez | b41d38e | 2024-07-16 17:44:20 +0000 | [diff] [blame] | 485 | std::pair<std::unique_ptr<MotionEvent>, std::optional<uint32_t>> |
| 486 | InputConsumerNoResampling::createBatchedMotionEvent(const nsecs_t frameTime, |
| 487 | std::queue<InputMessage>& messages) { |
| 488 | std::unique_ptr<MotionEvent> motionEvent; |
| 489 | std::optional<uint32_t> firstSeqForBatch; |
Paul Ramirez | cd7488c | 2024-09-13 23:01:12 +0000 | [diff] [blame^] | 490 | const nanoseconds resampleLatency = |
| 491 | (mResampler != nullptr) ? mResampler->getResampleLatency() : nanoseconds{0}; |
| 492 | const nanoseconds adjustedFrameTime = nanoseconds{frameTime} - resampleLatency; |
| 493 | |
| 494 | while (!messages.empty() && |
| 495 | (messages.front().body.motion.eventTime <= adjustedFrameTime.count())) { |
Paul Ramirez | b41d38e | 2024-07-16 17:44:20 +0000 | [diff] [blame] | 496 | if (motionEvent == nullptr) { |
| 497 | motionEvent = createMotionEvent(messages.front()); |
| 498 | firstSeqForBatch = messages.front().header.seq; |
| 499 | const auto [_, inserted] = mBatchedSequenceNumbers.insert({*firstSeqForBatch, {}}); |
| 500 | LOG_IF(FATAL, !inserted) |
| 501 | << "The sequence " << messages.front().header.seq << " was already present!"; |
| 502 | } else { |
| 503 | addSample(*motionEvent, messages.front()); |
| 504 | mBatchedSequenceNumbers[*firstSeqForBatch].push_back(messages.front().header.seq); |
| 505 | } |
| 506 | messages.pop(); |
| 507 | } |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 508 | // Check if resampling should be performed. |
| 509 | if (motionEvent != nullptr && isPointerEvent(*motionEvent) && mResampler != nullptr) { |
| 510 | InputMessage* futureSample = nullptr; |
| 511 | if (!messages.empty()) { |
| 512 | futureSample = &messages.front(); |
| 513 | } |
Paul Ramirez | cd7488c | 2024-09-13 23:01:12 +0000 | [diff] [blame^] | 514 | mResampler->resampleMotionEvent(nanoseconds{frameTime}, *motionEvent, futureSample); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 515 | } |
Paul Ramirez | b41d38e | 2024-07-16 17:44:20 +0000 | [diff] [blame] | 516 | return std::make_pair(std::move(motionEvent), firstSeqForBatch); |
| 517 | } |
| 518 | |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 519 | bool InputConsumerNoResampling::consumeBatchedInputEvents( |
| 520 | std::optional<nsecs_t> requestedFrameTime) { |
| 521 | ensureCalledOnLooperThread(__func__); |
| 522 | // When batching is not enabled, we want to consume all events. That's equivalent to having an |
| 523 | // infinite frameTime. |
| 524 | const nsecs_t frameTime = requestedFrameTime.value_or(std::numeric_limits<nsecs_t>::max()); |
| 525 | bool producedEvents = false; |
Paul Ramirez | b41d38e | 2024-07-16 17:44:20 +0000 | [diff] [blame] | 526 | for (auto& [_, messages] : mBatches) { |
| 527 | auto [motion, firstSeqForBatch] = createBatchedMotionEvent(frameTime, messages); |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 528 | if (motion != nullptr) { |
| 529 | LOG_ALWAYS_FATAL_IF(!firstSeqForBatch.has_value()); |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 530 | mCallbacks.onMotionEvent(std::move(motion), *firstSeqForBatch); |
| 531 | producedEvents = true; |
| 532 | } else { |
| 533 | // This is OK, it just means that the frameTime is too old (all events that we have |
| 534 | // pending are in the future of the frametime). Maybe print a |
| 535 | // warning? If there are multiple devices active though, this might be normal and can |
| 536 | // just be ignored, unless none of them resulted in any consumption (in that case, this |
| 537 | // function would already return "false" so we could just leave it up to the caller). |
| 538 | } |
| 539 | } |
| 540 | std::erase_if(mBatches, [](const auto& pair) { return pair.second.empty(); }); |
| 541 | return producedEvents; |
| 542 | } |
| 543 | |
| 544 | void InputConsumerNoResampling::ensureCalledOnLooperThread(const char* func) const { |
| 545 | sp<Looper> callingThreadLooper = Looper::getForThread(); |
Paul Ramirez | e2bb187 | 2024-08-12 20:21:13 +0000 | [diff] [blame] | 546 | if (callingThreadLooper != mLooper->getLooper()) { |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 547 | LOG(FATAL) << "The function " << func << " can only be called on the looper thread"; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | std::string InputConsumerNoResampling::dump() const { |
| 552 | ensureCalledOnLooperThread(__func__); |
| 553 | std::string out; |
| 554 | if (mOutboundQueue.empty()) { |
| 555 | out += "mOutboundQueue: <empty>\n"; |
| 556 | } else { |
| 557 | out += "mOutboundQueue:\n"; |
| 558 | // Make a copy of mOutboundQueue for printing destructively. Unfortunately std::queue |
| 559 | // doesn't provide a good way to iterate over the entire container. |
| 560 | std::queue<InputMessage> tmpQueue = mOutboundQueue; |
| 561 | while (!tmpQueue.empty()) { |
| 562 | out += std::string(" ") + outboundMessageToString(tmpQueue.front()) + "\n"; |
| 563 | tmpQueue.pop(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (mBatches.empty()) { |
| 568 | out += "mBatches: <empty>\n"; |
| 569 | } else { |
| 570 | out += "mBatches:\n"; |
| 571 | for (const auto& [deviceId, messages] : mBatches) { |
| 572 | out += " Device id "; |
| 573 | out += std::to_string(deviceId); |
| 574 | out += ":\n"; |
| 575 | // Make a copy of mOutboundQueue for printing destructively. Unfortunately std::queue |
| 576 | // doesn't provide a good way to iterate over the entire container. |
| 577 | std::queue<InputMessage> tmpQueue = messages; |
| 578 | while (!tmpQueue.empty()) { |
| 579 | LOG_ALWAYS_FATAL_IF(tmpQueue.front().header.type != InputMessage::Type::MOTION); |
Siarhei Vishniakou | 3891ec9 | 2024-03-15 13:29:57 -0700 | [diff] [blame] | 580 | std::unique_ptr<MotionEvent> motion = createMotionEvent(tmpQueue.front()); |
| 581 | out += std::string(" ") + streamableToString(*motion) + "\n"; |
Siarhei Vishniakou | 2b92027 | 2024-02-27 19:49:51 -0800 | [diff] [blame] | 582 | tmpQueue.pop(); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return out; |
| 588 | } |
| 589 | |
| 590 | } // namespace android |