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