Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2010 The Android Open Source Project |
| 3 | // |
| 4 | // Provides a shared memory transport for input events. |
| 5 | // |
| 6 | #define LOG_TAG "InputTransport" |
Zim | d8402b6 | 2023-06-02 11:56:26 +0100 | [diff] [blame] | 7 | #define ATRACE_TAG ATRACE_TAG_INPUT |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 8 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <fcntl.h> |
Michael Wright | d0a4a62 | 2014-06-09 19:03:32 -0700 | [diff] [blame] | 11 | #include <inttypes.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 12 | #include <math.h> |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 13 | #include <poll.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 14 | #include <sys/socket.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 15 | #include <sys/types.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 18 | #include <android-base/logging.h> |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 19 | #include <android-base/properties.h> |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
| 21 | #include <binder/Parcel.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 23 | #include <ftl/enum.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 24 | #include <log/log.h> |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 25 | #include <utils/Trace.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 26 | |
Siarhei Vishniakou | 9681896 | 2023-08-23 10:19:02 -0700 | [diff] [blame] | 27 | #include <com_android_input_flags.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 28 | #include <input/InputTransport.h> |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 29 | #include <input/PrintTools.h> |
Prabir Pradhan | a37bad1 | 2023-08-18 15:55:32 +0000 | [diff] [blame] | 30 | #include <input/TraceTools.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 31 | |
Siarhei Vishniakou | 9681896 | 2023-08-23 10:19:02 -0700 | [diff] [blame] | 32 | namespace input_flags = com::android::input::flags; |
| 33 | |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | /** |
| 39 | * Log debug messages about channel messages (send message, receive message). |
| 40 | * Enable this via "adb shell setprop log.tag.InputTransportMessages DEBUG" |
| 41 | * (requires restart) |
| 42 | */ |
| 43 | const bool DEBUG_CHANNEL_MESSAGES = |
| 44 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Messages", ANDROID_LOG_INFO); |
| 45 | |
| 46 | /** |
| 47 | * Log debug messages whenever InputChannel objects are created/destroyed. |
| 48 | * Enable this via "adb shell setprop log.tag.InputTransportLifecycle DEBUG" |
| 49 | * (requires restart) |
| 50 | */ |
| 51 | const bool DEBUG_CHANNEL_LIFECYCLE = |
| 52 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Lifecycle", ANDROID_LOG_INFO); |
| 53 | |
| 54 | /** |
| 55 | * Log debug messages relating to the consumer end of the transport channel. |
| 56 | * Enable this via "adb shell setprop log.tag.InputTransportConsumer DEBUG" (requires restart) |
| 57 | */ |
| 58 | |
| 59 | const bool DEBUG_TRANSPORT_CONSUMER = |
| 60 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Consumer", ANDROID_LOG_INFO); |
| 61 | |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 62 | const bool IS_DEBUGGABLE_BUILD = |
| 63 | #if defined(__ANDROID__) |
| 64 | android::base::GetBoolProperty("ro.debuggable", false); |
| 65 | #else |
| 66 | true; |
| 67 | #endif |
| 68 | |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 69 | /** |
| 70 | * Log debug messages relating to the producer end of the transport channel. |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 71 | * Enable this via "adb shell setprop log.tag.InputTransportPublisher DEBUG". |
| 72 | * This requires a restart on non-debuggable (e.g. user) builds, but should take effect immediately |
| 73 | * on debuggable builds (e.g. userdebug). |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 74 | */ |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 75 | bool debugTransportPublisher() { |
| 76 | if (!IS_DEBUGGABLE_BUILD) { |
| 77 | static const bool DEBUG_TRANSPORT_PUBLISHER = |
| 78 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Publisher", ANDROID_LOG_INFO); |
| 79 | return DEBUG_TRANSPORT_PUBLISHER; |
| 80 | } |
| 81 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Publisher", ANDROID_LOG_INFO); |
| 82 | } |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Log debug messages about touch event resampling. |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 86 | * |
| 87 | * Enable this via "adb shell setprop log.tag.InputTransportResampling DEBUG". |
| 88 | * This requires a restart on non-debuggable (e.g. user) builds, but should take effect immediately |
| 89 | * on debuggable builds (e.g. userdebug). |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 90 | */ |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 91 | bool debugResampling() { |
| 92 | if (!IS_DEBUGGABLE_BUILD) { |
| 93 | static const bool DEBUG_TRANSPORT_RESAMPLING = |
| 94 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", |
| 95 | ANDROID_LOG_INFO); |
| 96 | return DEBUG_TRANSPORT_RESAMPLING; |
| 97 | } |
| 98 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO); |
| 99 | } |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 100 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 101 | android::base::unique_fd dupChannelFd(int fd) { |
| 102 | android::base::unique_fd newFd(::dup(fd)); |
| 103 | if (!newFd.ok()) { |
| 104 | ALOGE("Could not duplicate fd %i : %s", fd, strerror(errno)); |
| 105 | const bool hitFdLimit = errno == EMFILE || errno == ENFILE; |
| 106 | // If this process is out of file descriptors, then throwing that might end up exploding |
| 107 | // on the other side of a binder call, which isn't really helpful. |
| 108 | // Better to just crash here and hope that the FD leak is slow. |
| 109 | // Other failures could be client errors, so we still propagate those back to the caller. |
| 110 | LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel"); |
| 111 | return {}; |
| 112 | } |
| 113 | return newFd; |
| 114 | } |
| 115 | |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 116 | void initializeKeyEvent(KeyEvent& event, const InputMessage& msg) { |
| 117 | event.initialize(msg.body.key.eventId, msg.body.key.deviceId, msg.body.key.source, |
| 118 | msg.body.key.displayId, msg.body.key.hmac, msg.body.key.action, |
| 119 | msg.body.key.flags, msg.body.key.keyCode, msg.body.key.scanCode, |
| 120 | msg.body.key.metaState, msg.body.key.repeatCount, msg.body.key.downTime, |
| 121 | msg.body.key.eventTime); |
| 122 | } |
| 123 | |
| 124 | void initializeFocusEvent(FocusEvent& event, const InputMessage& msg) { |
| 125 | event.initialize(msg.body.focus.eventId, msg.body.focus.hasFocus); |
| 126 | } |
| 127 | |
| 128 | void initializeCaptureEvent(CaptureEvent& event, const InputMessage& msg) { |
| 129 | event.initialize(msg.body.capture.eventId, msg.body.capture.pointerCaptureEnabled); |
| 130 | } |
| 131 | |
| 132 | void initializeDragEvent(DragEvent& event, const InputMessage& msg) { |
| 133 | event.initialize(msg.body.drag.eventId, msg.body.drag.x, msg.body.drag.y, |
| 134 | msg.body.drag.isExiting); |
| 135 | } |
| 136 | |
| 137 | void initializeMotionEvent(MotionEvent& event, const InputMessage& msg) { |
| 138 | uint32_t pointerCount = msg.body.motion.pointerCount; |
| 139 | PointerProperties pointerProperties[pointerCount]; |
| 140 | PointerCoords pointerCoords[pointerCount]; |
| 141 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 142 | pointerProperties[i] = msg.body.motion.pointers[i].properties; |
| 143 | pointerCoords[i] = msg.body.motion.pointers[i].coords; |
| 144 | } |
| 145 | |
| 146 | ui::Transform transform; |
| 147 | transform.set({msg.body.motion.dsdx, msg.body.motion.dtdx, msg.body.motion.tx, |
| 148 | msg.body.motion.dtdy, msg.body.motion.dsdy, msg.body.motion.ty, 0, 0, 1}); |
| 149 | ui::Transform displayTransform; |
| 150 | displayTransform.set({msg.body.motion.dsdxRaw, msg.body.motion.dtdxRaw, msg.body.motion.txRaw, |
| 151 | msg.body.motion.dtdyRaw, msg.body.motion.dsdyRaw, msg.body.motion.tyRaw, |
| 152 | 0, 0, 1}); |
| 153 | event.initialize(msg.body.motion.eventId, msg.body.motion.deviceId, msg.body.motion.source, |
| 154 | msg.body.motion.displayId, msg.body.motion.hmac, msg.body.motion.action, |
| 155 | msg.body.motion.actionButton, msg.body.motion.flags, msg.body.motion.edgeFlags, |
| 156 | msg.body.motion.metaState, msg.body.motion.buttonState, |
| 157 | msg.body.motion.classification, transform, msg.body.motion.xPrecision, |
| 158 | msg.body.motion.yPrecision, msg.body.motion.xCursorPosition, |
| 159 | msg.body.motion.yCursorPosition, displayTransform, msg.body.motion.downTime, |
| 160 | msg.body.motion.eventTime, pointerCount, pointerProperties, pointerCoords); |
| 161 | } |
| 162 | |
| 163 | void addSample(MotionEvent& event, const InputMessage& msg) { |
| 164 | uint32_t pointerCount = msg.body.motion.pointerCount; |
| 165 | PointerCoords pointerCoords[pointerCount]; |
| 166 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 167 | pointerCoords[i] = msg.body.motion.pointers[i].coords; |
| 168 | } |
| 169 | |
| 170 | event.setMetaState(event.getMetaState() | msg.body.motion.metaState); |
| 171 | event.addSample(msg.body.motion.eventTime, pointerCoords); |
| 172 | } |
| 173 | |
| 174 | void initializeTouchModeEvent(TouchModeEvent& event, const InputMessage& msg) { |
| 175 | event.initialize(msg.body.touchMode.eventId, msg.body.touchMode.isInTouchMode); |
| 176 | } |
| 177 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 178 | // Socket buffer size. The default is typically about 128KB, which is much larger than |
| 179 | // we really need. So we make it smaller. It just needs to be big enough to hold |
| 180 | // a few dozen large multi-finger motion events in the case where an application gets |
| 181 | // behind processing touches. |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 182 | static constexpr size_t SOCKET_BUFFER_SIZE = 32 * 1024; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 183 | |
| 184 | // Nanoseconds per milliseconds. |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 185 | static constexpr nsecs_t NANOS_PER_MS = 1000000; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 186 | |
| 187 | // Latency added during resampling. A few milliseconds doesn't hurt much but |
| 188 | // reduces the impact of mispredicted touch positions. |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 189 | const std::chrono::duration RESAMPLE_LATENCY = 5ms; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 190 | |
| 191 | // Minimum time difference between consecutive samples before attempting to resample. |
| 192 | static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS; |
| 193 | |
Andrew de los Reyes | de18f6c | 2015-10-01 15:57:25 -0700 | [diff] [blame] | 194 | // Maximum time difference between consecutive samples before attempting to resample |
| 195 | // by extrapolation. |
| 196 | static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS; |
| 197 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 198 | // Maximum time to predict forward from the last known state, to avoid predicting too |
| 199 | // far into the future. This time is further bounded by 50% of the last time delta. |
| 200 | static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS; |
| 201 | |
Siarhei Vishniakou | b5433e9 | 2019-02-21 09:27:39 -0600 | [diff] [blame] | 202 | /** |
| 203 | * System property for enabling / disabling touch resampling. |
| 204 | * Resampling extrapolates / interpolates the reported touch event coordinates to better |
| 205 | * align them to the VSYNC signal, thus resulting in smoother scrolling performance. |
| 206 | * Resampling is not needed (and should be disabled) on hardware that already |
| 207 | * has touch events triggered by VSYNC. |
| 208 | * Set to "1" to enable resampling (default). |
| 209 | * Set to "0" to disable resampling. |
| 210 | * Resampling is enabled by default. |
| 211 | */ |
| 212 | static const char* PROPERTY_RESAMPLING_ENABLED = "ro.input.resampling"; |
| 213 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Crash if the events that are getting sent to the InputPublisher are inconsistent. |
| 216 | * Enable this via "adb shell setprop log.tag.InputTransportVerifyEvents DEBUG" |
| 217 | */ |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 218 | bool verifyEvents() { |
Siarhei Vishniakou | 9681896 | 2023-08-23 10:19:02 -0700 | [diff] [blame] | 219 | return input_flags::enable_outbound_event_verification() || |
| 220 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "VerifyEvents", ANDROID_LOG_INFO); |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 223 | inline float lerp(float a, float b, float alpha) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 224 | return a + alpha * (b - a); |
| 225 | } |
| 226 | |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 227 | inline bool isPointerEvent(int32_t source) { |
Siarhei Vishniakou | 128eab1 | 2019-05-23 10:25:59 +0800 | [diff] [blame] | 228 | return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER; |
| 229 | } |
| 230 | |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 231 | bool shouldResampleTool(ToolType toolType) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 232 | return toolType == ToolType::FINGER || toolType == ToolType::UNKNOWN; |
| 233 | } |
| 234 | |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 235 | } // namespace |
| 236 | |
| 237 | using android::base::Result; |
| 238 | using android::base::StringPrintf; |
| 239 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 240 | // --- InputMessage --- |
| 241 | |
| 242 | bool InputMessage::isValid(size_t actualSize) const { |
Siarhei Vishniakou | dbdb673 | 2021-04-26 19:40:26 +0000 | [diff] [blame] | 243 | if (size() != actualSize) { |
| 244 | ALOGE("Received message of incorrect size %zu (expected %zu)", actualSize, size()); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | switch (header.type) { |
| 249 | case Type::KEY: |
| 250 | return true; |
| 251 | case Type::MOTION: { |
| 252 | const bool valid = |
| 253 | body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS; |
| 254 | if (!valid) { |
| 255 | ALOGE("Received invalid MOTION: pointerCount = %" PRIu32, body.motion.pointerCount); |
| 256 | } |
| 257 | return valid; |
| 258 | } |
| 259 | case Type::FINISHED: |
| 260 | case Type::FOCUS: |
| 261 | case Type::CAPTURE: |
| 262 | case Type::DRAG: |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 263 | case Type::TOUCH_MODE: |
Siarhei Vishniakou | dbdb673 | 2021-04-26 19:40:26 +0000 | [diff] [blame] | 264 | return true; |
| 265 | case Type::TIMELINE: { |
| 266 | const nsecs_t gpuCompletedTime = |
| 267 | body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME]; |
| 268 | const nsecs_t presentTime = |
| 269 | body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME]; |
| 270 | const bool valid = presentTime > gpuCompletedTime; |
| 271 | if (!valid) { |
| 272 | ALOGE("Received invalid TIMELINE: gpuCompletedTime = %" PRId64 |
| 273 | " presentTime = %" PRId64, |
| 274 | gpuCompletedTime, presentTime); |
| 275 | } |
| 276 | return valid; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 279 | ALOGE("Invalid message type: %s", ftl::enum_string(header.type).c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
| 283 | size_t InputMessage::size() const { |
| 284 | switch (header.type) { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 285 | case Type::KEY: |
| 286 | return sizeof(Header) + body.key.size(); |
| 287 | case Type::MOTION: |
| 288 | return sizeof(Header) + body.motion.size(); |
| 289 | case Type::FINISHED: |
| 290 | return sizeof(Header) + body.finished.size(); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 291 | case Type::FOCUS: |
| 292 | return sizeof(Header) + body.focus.size(); |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 293 | case Type::CAPTURE: |
| 294 | return sizeof(Header) + body.capture.size(); |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 295 | case Type::DRAG: |
| 296 | return sizeof(Header) + body.drag.size(); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 297 | case Type::TIMELINE: |
| 298 | return sizeof(Header) + body.timeline.size(); |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 299 | case Type::TOUCH_MODE: |
| 300 | return sizeof(Header) + body.touchMode.size(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 301 | } |
| 302 | return sizeof(Header); |
| 303 | } |
| 304 | |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 305 | /** |
| 306 | * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire |
| 307 | * memory to zero, then only copy the valid bytes on a per-field basis. |
| 308 | */ |
| 309 | void InputMessage::getSanitizedCopy(InputMessage* msg) const { |
| 310 | memset(msg, 0, sizeof(*msg)); |
| 311 | |
| 312 | // Write the header |
| 313 | msg->header.type = header.type; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 314 | msg->header.seq = header.seq; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 315 | |
| 316 | // Write the body |
| 317 | switch(header.type) { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 318 | case InputMessage::Type::KEY: { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 319 | // int32_t eventId |
| 320 | msg->body.key.eventId = body.key.eventId; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 321 | // nsecs_t eventTime |
| 322 | msg->body.key.eventTime = body.key.eventTime; |
| 323 | // int32_t deviceId |
| 324 | msg->body.key.deviceId = body.key.deviceId; |
| 325 | // int32_t source |
| 326 | msg->body.key.source = body.key.source; |
| 327 | // int32_t displayId |
| 328 | msg->body.key.displayId = body.key.displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 329 | // std::array<uint8_t, 32> hmac |
| 330 | msg->body.key.hmac = body.key.hmac; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 331 | // int32_t action |
| 332 | msg->body.key.action = body.key.action; |
| 333 | // int32_t flags |
| 334 | msg->body.key.flags = body.key.flags; |
| 335 | // int32_t keyCode |
| 336 | msg->body.key.keyCode = body.key.keyCode; |
| 337 | // int32_t scanCode |
| 338 | msg->body.key.scanCode = body.key.scanCode; |
| 339 | // int32_t metaState |
| 340 | msg->body.key.metaState = body.key.metaState; |
| 341 | // int32_t repeatCount |
| 342 | msg->body.key.repeatCount = body.key.repeatCount; |
| 343 | // nsecs_t downTime |
| 344 | msg->body.key.downTime = body.key.downTime; |
| 345 | break; |
| 346 | } |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 347 | case InputMessage::Type::MOTION: { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 348 | // int32_t eventId |
| 349 | msg->body.motion.eventId = body.motion.eventId; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 350 | // uint32_t pointerCount |
| 351 | msg->body.motion.pointerCount = body.motion.pointerCount; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 352 | // nsecs_t eventTime |
| 353 | msg->body.motion.eventTime = body.motion.eventTime; |
| 354 | // int32_t deviceId |
| 355 | msg->body.motion.deviceId = body.motion.deviceId; |
| 356 | // int32_t source |
| 357 | msg->body.motion.source = body.motion.source; |
| 358 | // int32_t displayId |
| 359 | msg->body.motion.displayId = body.motion.displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 360 | // std::array<uint8_t, 32> hmac |
| 361 | msg->body.motion.hmac = body.motion.hmac; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 362 | // int32_t action |
| 363 | msg->body.motion.action = body.motion.action; |
| 364 | // int32_t actionButton |
| 365 | msg->body.motion.actionButton = body.motion.actionButton; |
| 366 | // int32_t flags |
| 367 | msg->body.motion.flags = body.motion.flags; |
| 368 | // int32_t metaState |
| 369 | msg->body.motion.metaState = body.motion.metaState; |
| 370 | // int32_t buttonState |
| 371 | msg->body.motion.buttonState = body.motion.buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 372 | // MotionClassification classification |
| 373 | msg->body.motion.classification = body.motion.classification; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 374 | // int32_t edgeFlags |
| 375 | msg->body.motion.edgeFlags = body.motion.edgeFlags; |
| 376 | // nsecs_t downTime |
| 377 | msg->body.motion.downTime = body.motion.downTime; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 378 | |
| 379 | msg->body.motion.dsdx = body.motion.dsdx; |
| 380 | msg->body.motion.dtdx = body.motion.dtdx; |
| 381 | msg->body.motion.dtdy = body.motion.dtdy; |
| 382 | msg->body.motion.dsdy = body.motion.dsdy; |
| 383 | msg->body.motion.tx = body.motion.tx; |
| 384 | msg->body.motion.ty = body.motion.ty; |
| 385 | |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 386 | // float xPrecision |
| 387 | msg->body.motion.xPrecision = body.motion.xPrecision; |
| 388 | // float yPrecision |
| 389 | msg->body.motion.yPrecision = body.motion.yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 390 | // float xCursorPosition |
| 391 | msg->body.motion.xCursorPosition = body.motion.xCursorPosition; |
| 392 | // float yCursorPosition |
| 393 | msg->body.motion.yCursorPosition = body.motion.yCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 394 | |
| 395 | msg->body.motion.dsdxRaw = body.motion.dsdxRaw; |
| 396 | msg->body.motion.dtdxRaw = body.motion.dtdxRaw; |
| 397 | msg->body.motion.dtdyRaw = body.motion.dtdyRaw; |
| 398 | msg->body.motion.dsdyRaw = body.motion.dsdyRaw; |
| 399 | msg->body.motion.txRaw = body.motion.txRaw; |
| 400 | msg->body.motion.tyRaw = body.motion.tyRaw; |
| 401 | |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 402 | //struct Pointer pointers[MAX_POINTERS] |
| 403 | for (size_t i = 0; i < body.motion.pointerCount; i++) { |
| 404 | // PointerProperties properties |
| 405 | msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id; |
| 406 | msg->body.motion.pointers[i].properties.toolType = |
| 407 | body.motion.pointers[i].properties.toolType, |
| 408 | // PointerCoords coords |
| 409 | msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits; |
| 410 | const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits); |
| 411 | memcpy(&msg->body.motion.pointers[i].coords.values[0], |
| 412 | &body.motion.pointers[i].coords.values[0], |
| 413 | count * (sizeof(body.motion.pointers[i].coords.values[0]))); |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame] | 414 | msg->body.motion.pointers[i].coords.isResampled = |
| 415 | body.motion.pointers[i].coords.isResampled; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 416 | } |
| 417 | break; |
| 418 | } |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 419 | case InputMessage::Type::FINISHED: { |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 420 | msg->body.finished.handled = body.finished.handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 421 | msg->body.finished.consumeTime = body.finished.consumeTime; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 422 | break; |
| 423 | } |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 424 | case InputMessage::Type::FOCUS: { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 425 | msg->body.focus.eventId = body.focus.eventId; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 426 | msg->body.focus.hasFocus = body.focus.hasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 427 | break; |
| 428 | } |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 429 | case InputMessage::Type::CAPTURE: { |
| 430 | msg->body.capture.eventId = body.capture.eventId; |
| 431 | msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled; |
| 432 | break; |
| 433 | } |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 434 | case InputMessage::Type::DRAG: { |
| 435 | msg->body.drag.eventId = body.drag.eventId; |
| 436 | msg->body.drag.x = body.drag.x; |
| 437 | msg->body.drag.y = body.drag.y; |
| 438 | msg->body.drag.isExiting = body.drag.isExiting; |
| 439 | break; |
| 440 | } |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 441 | case InputMessage::Type::TIMELINE: { |
| 442 | msg->body.timeline.eventId = body.timeline.eventId; |
| 443 | msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline; |
| 444 | break; |
| 445 | } |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 446 | case InputMessage::Type::TOUCH_MODE: { |
| 447 | msg->body.touchMode.eventId = body.touchMode.eventId; |
| 448 | msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode; |
| 449 | } |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 452 | |
| 453 | // --- InputChannel --- |
| 454 | |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 455 | std::unique_ptr<InputChannel> InputChannel::create(const std::string& name, |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 456 | android::base::unique_fd fd, sp<IBinder> token) { |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 457 | const int result = fcntl(fd, F_SETFL, O_NONBLOCK); |
| 458 | if (result != 0) { |
| 459 | LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(), |
| 460 | strerror(errno)); |
| 461 | return nullptr; |
| 462 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 463 | // using 'new' to access a non-public constructor |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 464 | return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token)); |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 467 | std::unique_ptr<InputChannel> InputChannel::create( |
| 468 | android::os::InputChannelCore&& parceledChannel) { |
| 469 | return InputChannel::create(parceledChannel.name, parceledChannel.fd.release(), |
| 470 | parceledChannel.token); |
| 471 | } |
| 472 | |
| 473 | InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token) { |
| 474 | this->name = std::move(name); |
| 475 | this->fd.reset(std::move(fd)); |
| 476 | this->token = std::move(token); |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 477 | ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel constructed: name='%s', fd=%d", |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 478 | getName().c_str(), getFd()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | InputChannel::~InputChannel() { |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 482 | ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel destroyed: name='%s', fd=%d", |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 483 | getName().c_str(), getFd()); |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 486 | status_t InputChannel::openInputChannelPair(const std::string& name, |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 487 | std::unique_ptr<InputChannel>& outServerChannel, |
| 488 | std::unique_ptr<InputChannel>& outClientChannel) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 489 | int sockets[2]; |
| 490 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) { |
| 491 | status_t result = -errno; |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 492 | ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(), |
| 493 | strerror(errno), errno); |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 494 | outServerChannel.reset(); |
| 495 | outClientChannel.reset(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 496 | return result; |
| 497 | } |
| 498 | |
| 499 | int bufferSize = SOCKET_BUFFER_SIZE; |
| 500 | setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); |
| 501 | setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); |
| 502 | setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); |
| 503 | setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); |
| 504 | |
Siarhei Vishniakou | 4c155eb | 2023-06-30 11:47:12 -0700 | [diff] [blame] | 505 | sp<IBinder> token = sp<BBinder>::make(); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 506 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 507 | std::string serverChannelName = name + " (server)"; |
| 508 | android::base::unique_fd serverFd(sockets[0]); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 509 | outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 510 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 511 | std::string clientChannelName = name + " (client)"; |
| 512 | android::base::unique_fd clientFd(sockets[1]); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 513 | outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 514 | return OK; |
| 515 | } |
| 516 | |
| 517 | status_t InputChannel::sendMessage(const InputMessage* msg) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 518 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 519 | StringPrintf("sendMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=0x%" PRIx32 |
| 520 | ")", |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 521 | name.c_str(), msg->header.seq, msg->header.type)); |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 522 | const size_t msgLength = msg->size(); |
| 523 | InputMessage cleanMsg; |
| 524 | msg->getSanitizedCopy(&cleanMsg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 525 | ssize_t nWrite; |
| 526 | do { |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 527 | nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 528 | } while (nWrite == -1 && errno == EINTR); |
| 529 | |
| 530 | if (nWrite < 0) { |
| 531 | int error = errno; |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 532 | ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ error sending message of type %s, %s", |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 533 | name.c_str(), ftl::enum_string(msg->header.type).c_str(), strerror(error)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 534 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 535 | return WOULD_BLOCK; |
| 536 | } |
| 537 | if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) { |
| 538 | return DEAD_OBJECT; |
| 539 | } |
| 540 | return -error; |
| 541 | } |
| 542 | |
| 543 | if (size_t(nWrite) != msgLength) { |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 544 | ALOGD_IF(DEBUG_CHANNEL_MESSAGES, |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 545 | "channel '%s' ~ error sending message type %s, send was incomplete", name.c_str(), |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 546 | ftl::enum_string(msg->header.type).c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 547 | return DEAD_OBJECT; |
| 548 | } |
| 549 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 550 | ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ sent message of type %s", name.c_str(), |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 551 | ftl::enum_string(msg->header.type).c_str()); |
Zim | d8402b6 | 2023-06-02 11:56:26 +0100 | [diff] [blame] | 552 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 553 | return OK; |
| 554 | } |
| 555 | |
| 556 | status_t InputChannel::receiveMessage(InputMessage* msg) { |
| 557 | ssize_t nRead; |
| 558 | do { |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 559 | nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 560 | } while (nRead == -1 && errno == EINTR); |
| 561 | |
| 562 | if (nRead < 0) { |
| 563 | int error = errno; |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 564 | ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ receive message failed, errno=%d", |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 565 | name.c_str(), errno); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 566 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 567 | return WOULD_BLOCK; |
| 568 | } |
| 569 | if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) { |
| 570 | return DEAD_OBJECT; |
| 571 | } |
| 572 | return -error; |
| 573 | } |
| 574 | |
| 575 | if (nRead == 0) { // check for EOF |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 576 | ALOGD_IF(DEBUG_CHANNEL_MESSAGES, |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 577 | "channel '%s' ~ receive message failed because peer was closed", name.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 578 | return DEAD_OBJECT; |
| 579 | } |
| 580 | |
| 581 | if (!msg->isValid(nRead)) { |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 582 | ALOGE("channel '%s' ~ received invalid message of size %zd", name.c_str(), nRead); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 583 | return BAD_VALUE; |
| 584 | } |
| 585 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 586 | ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ received message of type %s", name.c_str(), |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 587 | ftl::enum_string(msg->header.type).c_str()); |
Zim | d8402b6 | 2023-06-02 11:56:26 +0100 | [diff] [blame] | 588 | if (ATRACE_ENABLED()) { |
Prabir Pradhan | a37bad1 | 2023-08-18 15:55:32 +0000 | [diff] [blame] | 589 | // Add an additional trace point to include data about the received message. |
Zim | d8402b6 | 2023-06-02 11:56:26 +0100 | [diff] [blame] | 590 | std::string message = StringPrintf("receiveMessage(inputChannel=%s, seq=0x%" PRIx32 |
| 591 | ", type=0x%" PRIx32 ")", |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 592 | name.c_str(), msg->header.seq, msg->header.type); |
Zim | d8402b6 | 2023-06-02 11:56:26 +0100 | [diff] [blame] | 593 | ATRACE_NAME(message.c_str()); |
| 594 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 595 | return OK; |
| 596 | } |
| 597 | |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 598 | bool InputChannel::probablyHasInput() const { |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 599 | struct pollfd pfds = {.fd = fd.get(), .events = POLLIN}; |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 600 | if (::poll(&pfds, /*nfds=*/1, /*timeout=*/0) <= 0) { |
Egor Pasko | 5a67a56 | 2024-01-16 16:46:45 +0100 | [diff] [blame] | 601 | // This can be a false negative because EINTR and ENOMEM are not handled. The latter should |
| 602 | // be extremely rare. The EINTR is also unlikely because it happens only when the signal |
| 603 | // arrives while the syscall is executed, and the syscall is quick. Hitting EINTR too often |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 604 | // would be a sign of having too many signals, which is a bigger performance problem. A |
Egor Pasko | 5a67a56 | 2024-01-16 16:46:45 +0100 | [diff] [blame] | 605 | // common tradition is to repeat the syscall on each EINTR, but it is not necessary here. |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 606 | // In other words, the missing one liner is replaced by a multiline explanation. |
| 607 | return false; |
| 608 | } |
| 609 | // From poll(2): The bits returned in |revents| can include any of those specified in |events|, |
| 610 | // or one of the values POLLERR, POLLHUP, or POLLNVAL. |
| 611 | return (pfds.revents & POLLIN) != 0; |
| 612 | } |
| 613 | |
Egor Pasko | 5a67a56 | 2024-01-16 16:46:45 +0100 | [diff] [blame] | 614 | void InputChannel::waitForMessage(std::chrono::milliseconds timeout) const { |
| 615 | if (timeout < 0ms) { |
| 616 | LOG(FATAL) << "Timeout cannot be negative, received " << timeout.count(); |
| 617 | } |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 618 | struct pollfd pfds = {.fd = fd.get(), .events = POLLIN}; |
Egor Pasko | 5a67a56 | 2024-01-16 16:46:45 +0100 | [diff] [blame] | 619 | int ret; |
| 620 | std::chrono::time_point<std::chrono::steady_clock> stopTime = |
| 621 | std::chrono::steady_clock::now() + timeout; |
| 622 | std::chrono::milliseconds remaining = timeout; |
| 623 | do { |
| 624 | ret = ::poll(&pfds, /*nfds=*/1, /*timeout=*/remaining.count()); |
| 625 | remaining = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 626 | stopTime - std::chrono::steady_clock::now()); |
| 627 | } while (ret == -1 && errno == EINTR && remaining > 0ms); |
| 628 | } |
| 629 | |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 630 | std::unique_ptr<InputChannel> InputChannel::dup() const { |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 631 | base::unique_fd newFd(dupChannelFd(fd.get())); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 632 | return InputChannel::create(getName(), std::move(newFd), getConnectionToken()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 635 | void InputChannel::copyTo(android::os::InputChannelCore& outChannel) const { |
| 636 | outChannel.name = getName(); |
| 637 | outChannel.fd.reset(dupChannelFd(fd.get())); |
| 638 | outChannel.token = getConnectionToken(); |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 641 | void InputChannel::moveChannel(std::unique_ptr<InputChannel> from, |
| 642 | android::os::InputChannelCore& outChannel) { |
| 643 | outChannel.name = from->getName(); |
| 644 | outChannel.fd = android::os::ParcelFileDescriptor(std::move(from->fd)); |
| 645 | outChannel.token = from->getConnectionToken(); |
| 646 | } |
| 647 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 648 | sp<IBinder> InputChannel::getConnectionToken() const { |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 649 | return token; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 652 | // --- InputPublisher --- |
| 653 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 654 | InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel) |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 655 | : mChannel(channel), mInputVerifier(mChannel->getName()) {} |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 656 | |
| 657 | InputPublisher::~InputPublisher() { |
| 658 | } |
| 659 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 660 | status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId, |
| 661 | int32_t source, int32_t displayId, |
| 662 | std::array<uint8_t, 32> hmac, int32_t action, |
| 663 | int32_t flags, int32_t keyCode, int32_t scanCode, |
| 664 | int32_t metaState, int32_t repeatCount, nsecs_t downTime, |
| 665 | nsecs_t eventTime) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 666 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 667 | StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)", |
| 668 | mChannel->getName().c_str(), KeyEvent::actionToString(action), |
| 669 | KeyEvent::getLabel(keyCode))); |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 670 | ALOGD_IF(debugTransportPublisher(), |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 671 | "channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, " |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 672 | "action=%s, flags=0x%x, keyCode=%s, scanCode=%d, metaState=0x%x, repeatCount=%d," |
| 673 | "downTime=%" PRId64 ", eventTime=%" PRId64, |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 674 | mChannel->getName().c_str(), __func__, seq, eventId, deviceId, |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 675 | inputEventSourceToString(source).c_str(), KeyEvent::actionToString(action), flags, |
| 676 | KeyEvent::getLabel(keyCode), scanCode, metaState, repeatCount, downTime, eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 677 | |
| 678 | if (!seq) { |
| 679 | ALOGE("Attempted to publish a key event with sequence number 0."); |
| 680 | return BAD_VALUE; |
| 681 | } |
| 682 | |
| 683 | InputMessage msg; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 684 | msg.header.type = InputMessage::Type::KEY; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 685 | msg.header.seq = seq; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 686 | msg.body.key.eventId = eventId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 687 | msg.body.key.deviceId = deviceId; |
| 688 | msg.body.key.source = source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 689 | msg.body.key.displayId = displayId; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 690 | msg.body.key.hmac = std::move(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 691 | msg.body.key.action = action; |
| 692 | msg.body.key.flags = flags; |
| 693 | msg.body.key.keyCode = keyCode; |
| 694 | msg.body.key.scanCode = scanCode; |
| 695 | msg.body.key.metaState = metaState; |
| 696 | msg.body.key.repeatCount = repeatCount; |
| 697 | msg.body.key.downTime = downTime; |
| 698 | msg.body.key.eventTime = eventTime; |
| 699 | return mChannel->sendMessage(&msg); |
| 700 | } |
| 701 | |
| 702 | status_t InputPublisher::publishMotionEvent( |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 703 | uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 704 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags, |
| 705 | int32_t edgeFlags, int32_t metaState, int32_t buttonState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 706 | MotionClassification classification, const ui::Transform& transform, float xPrecision, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 707 | float yPrecision, float xCursorPosition, float yCursorPosition, |
| 708 | const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 709 | uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 710 | const PointerCoords* pointerCoords) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 711 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 712 | StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)", |
| 713 | mChannel->getName().c_str(), |
| 714 | MotionEvent::actionToString(action).c_str())); |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 715 | if (verifyEvents()) { |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 716 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 717 | mInputVerifier.processMovement(deviceId, source, action, pointerCount, |
| 718 | pointerProperties, pointerCoords, flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 719 | if (!result.ok()) { |
| 720 | LOG(FATAL) << "Bad stream: " << result.error(); |
| 721 | } |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 722 | } |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 723 | if (debugTransportPublisher()) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 724 | std::string transformString; |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 725 | transform.dump(transformString, "transform", " "); |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 726 | ALOGD("channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, " |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 727 | "displayId=%" PRId32 ", " |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 728 | "action=%s, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, " |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 729 | "metaState=0x%x, buttonState=0x%x, classification=%s," |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 730 | "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", " |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 731 | "pointerCount=%" PRIu32 "\n%s", |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 732 | mChannel->getName().c_str(), __func__, seq, eventId, deviceId, |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 733 | inputEventSourceToString(source).c_str(), displayId, |
| 734 | MotionEvent::actionToString(action).c_str(), actionButton, flags, edgeFlags, |
| 735 | metaState, buttonState, motionClassificationToString(classification), xPrecision, |
| 736 | yPrecision, downTime, eventTime, pointerCount, transformString.c_str()); |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 737 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 738 | |
| 739 | if (!seq) { |
| 740 | ALOGE("Attempted to publish a motion event with sequence number 0."); |
| 741 | return BAD_VALUE; |
| 742 | } |
| 743 | |
| 744 | if (pointerCount > MAX_POINTERS || pointerCount < 1) { |
Michael Wright | 63ff3a8 | 2014-06-10 13:03:17 -0700 | [diff] [blame] | 745 | ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 746 | mChannel->getName().c_str(), pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 747 | return BAD_VALUE; |
| 748 | } |
| 749 | |
| 750 | InputMessage msg; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 751 | msg.header.type = InputMessage::Type::MOTION; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 752 | msg.header.seq = seq; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 753 | msg.body.motion.eventId = eventId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 754 | msg.body.motion.deviceId = deviceId; |
| 755 | msg.body.motion.source = source; |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 756 | msg.body.motion.displayId = displayId; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 757 | msg.body.motion.hmac = std::move(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 758 | msg.body.motion.action = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 759 | msg.body.motion.actionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 760 | msg.body.motion.flags = flags; |
| 761 | msg.body.motion.edgeFlags = edgeFlags; |
| 762 | msg.body.motion.metaState = metaState; |
| 763 | msg.body.motion.buttonState = buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 764 | msg.body.motion.classification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 765 | msg.body.motion.dsdx = transform.dsdx(); |
| 766 | msg.body.motion.dtdx = transform.dtdx(); |
| 767 | msg.body.motion.dtdy = transform.dtdy(); |
| 768 | msg.body.motion.dsdy = transform.dsdy(); |
| 769 | msg.body.motion.tx = transform.tx(); |
| 770 | msg.body.motion.ty = transform.ty(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 771 | msg.body.motion.xPrecision = xPrecision; |
| 772 | msg.body.motion.yPrecision = yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 773 | msg.body.motion.xCursorPosition = xCursorPosition; |
| 774 | msg.body.motion.yCursorPosition = yCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 775 | msg.body.motion.dsdxRaw = rawTransform.dsdx(); |
| 776 | msg.body.motion.dtdxRaw = rawTransform.dtdx(); |
| 777 | msg.body.motion.dtdyRaw = rawTransform.dtdy(); |
| 778 | msg.body.motion.dsdyRaw = rawTransform.dsdy(); |
| 779 | msg.body.motion.txRaw = rawTransform.tx(); |
| 780 | msg.body.motion.tyRaw = rawTransform.ty(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 781 | msg.body.motion.downTime = downTime; |
| 782 | msg.body.motion.eventTime = eventTime; |
| 783 | msg.body.motion.pointerCount = pointerCount; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 784 | for (uint32_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 73e6d37 | 2023-07-06 18:07:21 -0700 | [diff] [blame] | 785 | msg.body.motion.pointers[i].properties = pointerProperties[i]; |
| 786 | msg.body.motion.pointers[i].coords = pointerCoords[i]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 787 | } |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 788 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 789 | return mChannel->sendMessage(&msg); |
| 790 | } |
| 791 | |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 792 | status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 793 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 794 | StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)", |
| 795 | mChannel->getName().c_str(), toString(hasFocus))); |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 796 | ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: seq=%u, id=%d, hasFocus=%s", |
| 797 | mChannel->getName().c_str(), __func__, seq, eventId, toString(hasFocus)); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 798 | |
| 799 | InputMessage msg; |
| 800 | msg.header.type = InputMessage::Type::FOCUS; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 801 | msg.header.seq = seq; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 802 | msg.body.focus.eventId = eventId; |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 803 | msg.body.focus.hasFocus = hasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 804 | return mChannel->sendMessage(&msg); |
| 805 | } |
| 806 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 807 | status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId, |
| 808 | bool pointerCaptureEnabled) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 809 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 810 | StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)", |
| 811 | mChannel->getName().c_str(), toString(pointerCaptureEnabled))); |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 812 | ALOGD_IF(debugTransportPublisher(), |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 813 | "channel '%s' publisher ~ %s: seq=%u, id=%d, pointerCaptureEnabled=%s", |
| 814 | mChannel->getName().c_str(), __func__, seq, eventId, toString(pointerCaptureEnabled)); |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 815 | |
| 816 | InputMessage msg; |
| 817 | msg.header.type = InputMessage::Type::CAPTURE; |
| 818 | msg.header.seq = seq; |
| 819 | msg.body.capture.eventId = eventId; |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 820 | msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 821 | return mChannel->sendMessage(&msg); |
| 822 | } |
| 823 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 824 | status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y, |
| 825 | bool isExiting) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 826 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 827 | StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)", |
| 828 | mChannel->getName().c_str(), x, y, toString(isExiting))); |
Prabir Pradhan | b2bd83c | 2023-02-23 02:34:40 +0000 | [diff] [blame] | 829 | ALOGD_IF(debugTransportPublisher(), |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 830 | "channel '%s' publisher ~ %s: seq=%u, id=%d, x=%f, y=%f, isExiting=%s", |
| 831 | mChannel->getName().c_str(), __func__, seq, eventId, x, y, toString(isExiting)); |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 832 | |
| 833 | InputMessage msg; |
| 834 | msg.header.type = InputMessage::Type::DRAG; |
| 835 | msg.header.seq = seq; |
| 836 | msg.body.drag.eventId = eventId; |
| 837 | msg.body.drag.isExiting = isExiting; |
| 838 | msg.body.drag.x = x; |
| 839 | msg.body.drag.y = y; |
| 840 | return mChannel->sendMessage(&msg); |
| 841 | } |
| 842 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 843 | status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 844 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 845 | StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)", |
| 846 | mChannel->getName().c_str(), toString(isInTouchMode))); |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 847 | ALOGD_IF(debugTransportPublisher(), |
| 848 | "channel '%s' publisher ~ %s: seq=%u, id=%d, isInTouchMode=%s", |
| 849 | mChannel->getName().c_str(), __func__, seq, eventId, toString(isInTouchMode)); |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 850 | |
| 851 | InputMessage msg; |
| 852 | msg.header.type = InputMessage::Type::TOUCH_MODE; |
| 853 | msg.header.seq = seq; |
| 854 | msg.body.touchMode.eventId = eventId; |
| 855 | msg.body.touchMode.isInTouchMode = isInTouchMode; |
| 856 | return mChannel->sendMessage(&msg); |
| 857 | } |
| 858 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 859 | android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 860 | InputMessage msg; |
| 861 | status_t result = mChannel->receiveMessage(&msg); |
| 862 | if (result) { |
Siarhei Vishniakou | 6911265 | 2023-08-24 08:34:18 -0700 | [diff] [blame] | 863 | if (debugTransportPublisher() && result != WOULD_BLOCK) { |
| 864 | LOG(INFO) << "channel '" << mChannel->getName() << "' publisher ~ " << __func__ << ": " |
| 865 | << strerror(result); |
| 866 | } |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 867 | return android::base::Error(result); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 868 | } |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 869 | if (msg.header.type == InputMessage::Type::FINISHED) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 870 | ALOGD_IF(debugTransportPublisher(), |
| 871 | "channel '%s' publisher ~ %s: finished: seq=%u, handled=%s", |
| 872 | mChannel->getName().c_str(), __func__, msg.header.seq, |
| 873 | toString(msg.body.finished.handled)); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 874 | return Finished{ |
| 875 | .seq = msg.header.seq, |
| 876 | .handled = msg.body.finished.handled, |
| 877 | .consumeTime = msg.body.finished.consumeTime, |
| 878 | }; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 879 | } |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 880 | |
| 881 | if (msg.header.type == InputMessage::Type::TIMELINE) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 882 | ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: timeline: id=%d", |
| 883 | mChannel->getName().c_str(), __func__, msg.body.timeline.eventId); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 884 | return Timeline{ |
| 885 | .inputEventId = msg.body.timeline.eventId, |
| 886 | .graphicsTimeline = msg.body.timeline.graphicsTimeline, |
| 887 | }; |
| 888 | } |
| 889 | |
| 890 | ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 891 | mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str()); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 892 | return android::base::Error(UNKNOWN_ERROR); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | // --- InputConsumer --- |
| 896 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 897 | InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel) |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 898 | : InputConsumer(channel, isTouchResamplingEnabled()) {} |
| 899 | |
| 900 | InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel, |
| 901 | bool enableTouchResampling) |
| 902 | : mResampleTouch(enableTouchResampling), mChannel(channel), mMsgDeferred(false) {} |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 903 | |
| 904 | InputConsumer::~InputConsumer() { |
| 905 | } |
| 906 | |
| 907 | bool InputConsumer::isTouchResamplingEnabled() { |
Siarhei Vishniakou | b5433e9 | 2019-02-21 09:27:39 -0600 | [diff] [blame] | 908 | return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 911 | status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches, |
| 912 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 913 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 914 | "channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64, |
| 915 | mChannel->getName().c_str(), toString(consumeBatches), frameTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 916 | |
| 917 | *outSeq = 0; |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 918 | *outEvent = nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 919 | |
| 920 | // Fetch the next input message. |
| 921 | // Loop until an event can be returned or no additional events are received. |
| 922 | while (!*outEvent) { |
| 923 | if (mMsgDeferred) { |
| 924 | // mMsg contains a valid input message from the previous call to consume |
| 925 | // that has not yet been processed. |
| 926 | mMsgDeferred = false; |
| 927 | } else { |
| 928 | // Receive a fresh message. |
| 929 | status_t result = mChannel->receiveMessage(&mMsg); |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 930 | if (result == OK) { |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 931 | const auto [_, inserted] = |
| 932 | mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC)); |
| 933 | LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32, |
| 934 | mMsg.header.seq); |
Siarhei Vishniakou | ca42e0d | 2024-01-12 12:10:32 -0800 | [diff] [blame] | 935 | |
| 936 | // Trace the event processing timeline - event was just read from the socket |
| 937 | ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/mMsg.header.seq); |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 938 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 939 | if (result) { |
| 940 | // Consume the next batched event unless batches are being held for later. |
| 941 | if (consumeBatches || result != WOULD_BLOCK) { |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 942 | result = consumeBatch(factory, frameTime, outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 943 | if (*outEvent) { |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 944 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 945 | "channel '%s' consumer ~ consumed batch event, seq=%u", |
| 946 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 947 | break; |
| 948 | } |
| 949 | } |
| 950 | return result; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | switch (mMsg.header.type) { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 955 | case InputMessage::Type::KEY: { |
| 956 | KeyEvent* keyEvent = factory->createKeyEvent(); |
| 957 | if (!keyEvent) return NO_MEMORY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 958 | |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 959 | initializeKeyEvent(*keyEvent, mMsg); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 960 | *outSeq = mMsg.header.seq; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 961 | *outEvent = keyEvent; |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 962 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 963 | "channel '%s' consumer ~ consumed key event, seq=%u", |
| 964 | mChannel->getName().c_str(), *outSeq); |
| 965 | break; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 966 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 967 | |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 968 | case InputMessage::Type::MOTION: { |
| 969 | ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source); |
| 970 | if (batchIndex >= 0) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 971 | Batch& batch = mBatches[batchIndex]; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 972 | if (canAddSample(batch, &mMsg)) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 973 | batch.samples.push_back(mMsg); |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 974 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 975 | "channel '%s' consumer ~ appended to batch event", |
| 976 | mChannel->getName().c_str()); |
| 977 | break; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 978 | } else if (isPointerEvent(mMsg.body.motion.source) && |
| 979 | mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 980 | // No need to process events that we are going to cancel anyways |
| 981 | const size_t count = batch.samples.size(); |
| 982 | for (size_t i = 0; i < count; i++) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 983 | const InputMessage& msg = batch.samples[i]; |
| 984 | sendFinishedSignal(msg.header.seq, false); |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 985 | } |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 986 | batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count); |
| 987 | mBatches.erase(mBatches.begin() + batchIndex); |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 988 | } else { |
| 989 | // We cannot append to the batch in progress, so we need to consume |
| 990 | // the previous batch right now and defer the new message until later. |
| 991 | mMsgDeferred = true; |
| 992 | status_t result = consumeSamples(factory, batch, batch.samples.size(), |
| 993 | outSeq, outEvent); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 994 | mBatches.erase(mBatches.begin() + batchIndex); |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 995 | if (result) { |
| 996 | return result; |
| 997 | } |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 998 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 999 | "channel '%s' consumer ~ consumed batch event and " |
| 1000 | "deferred current event, seq=%u", |
| 1001 | mChannel->getName().c_str(), *outSeq); |
| 1002 | break; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 1003 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1004 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1005 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 1006 | // Start a new batch if needed. |
| 1007 | if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE || |
| 1008 | mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1009 | Batch batch; |
| 1010 | batch.samples.push_back(mMsg); |
| 1011 | mBatches.push_back(batch); |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1012 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 1013 | "channel '%s' consumer ~ started batch event", |
| 1014 | mChannel->getName().c_str()); |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 1015 | break; |
| 1016 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1017 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1018 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 1019 | if (!motionEvent) return NO_MEMORY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1020 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1021 | updateTouchState(mMsg); |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 1022 | initializeMotionEvent(*motionEvent, mMsg); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1023 | *outSeq = mMsg.header.seq; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1024 | *outEvent = motionEvent; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1025 | |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1026 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 1027 | "channel '%s' consumer ~ consumed motion event, seq=%u", |
| 1028 | mChannel->getName().c_str(), *outSeq); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1029 | break; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 1030 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1031 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1032 | case InputMessage::Type::FINISHED: |
| 1033 | case InputMessage::Type::TIMELINE: { |
Siarhei Vishniakou | 7766c03 | 2021-03-02 20:32:20 +0000 | [diff] [blame] | 1034 | LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by " |
| 1035 | "InputConsumer!", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1036 | ftl::enum_string(mMsg.header.type).c_str()); |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 1037 | break; |
| 1038 | } |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1039 | |
| 1040 | case InputMessage::Type::FOCUS: { |
| 1041 | FocusEvent* focusEvent = factory->createFocusEvent(); |
| 1042 | if (!focusEvent) return NO_MEMORY; |
| 1043 | |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 1044 | initializeFocusEvent(*focusEvent, mMsg); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1045 | *outSeq = mMsg.header.seq; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1046 | *outEvent = focusEvent; |
| 1047 | break; |
| 1048 | } |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1049 | |
| 1050 | case InputMessage::Type::CAPTURE: { |
| 1051 | CaptureEvent* captureEvent = factory->createCaptureEvent(); |
| 1052 | if (!captureEvent) return NO_MEMORY; |
| 1053 | |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 1054 | initializeCaptureEvent(*captureEvent, mMsg); |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1055 | *outSeq = mMsg.header.seq; |
| 1056 | *outEvent = captureEvent; |
| 1057 | break; |
| 1058 | } |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1059 | |
| 1060 | case InputMessage::Type::DRAG: { |
| 1061 | DragEvent* dragEvent = factory->createDragEvent(); |
| 1062 | if (!dragEvent) return NO_MEMORY; |
| 1063 | |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 1064 | initializeDragEvent(*dragEvent, mMsg); |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1065 | *outSeq = mMsg.header.seq; |
| 1066 | *outEvent = dragEvent; |
| 1067 | break; |
| 1068 | } |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 1069 | |
| 1070 | case InputMessage::Type::TOUCH_MODE: { |
| 1071 | TouchModeEvent* touchModeEvent = factory->createTouchModeEvent(); |
| 1072 | if (!touchModeEvent) return NO_MEMORY; |
| 1073 | |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 1074 | initializeTouchModeEvent(*touchModeEvent, mMsg); |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 1075 | *outSeq = mMsg.header.seq; |
| 1076 | *outEvent = touchModeEvent; |
| 1077 | break; |
| 1078 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | return OK; |
| 1082 | } |
| 1083 | |
| 1084 | status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1085 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1086 | status_t result; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 1087 | for (size_t i = mBatches.size(); i > 0; ) { |
| 1088 | i--; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1089 | Batch& batch = mBatches[i]; |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 1090 | if (frameTime < 0) { |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1091 | result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1092 | mBatches.erase(mBatches.begin() + i); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1093 | return result; |
| 1094 | } |
| 1095 | |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 1096 | nsecs_t sampleTime = frameTime; |
| 1097 | if (mResampleTouch) { |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 1098 | sampleTime -= std::chrono::nanoseconds(RESAMPLE_LATENCY).count(); |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 1099 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1100 | ssize_t split = findSampleNoLaterThan(batch, sampleTime); |
| 1101 | if (split < 0) { |
| 1102 | continue; |
| 1103 | } |
| 1104 | |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1105 | result = consumeSamples(factory, batch, split + 1, outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1106 | const InputMessage* next; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1107 | if (batch.samples.empty()) { |
| 1108 | mBatches.erase(mBatches.begin() + i); |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 1109 | next = nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1110 | } else { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1111 | next = &batch.samples[0]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1112 | } |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 1113 | if (!result && mResampleTouch) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1114 | resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next); |
| 1115 | } |
| 1116 | return result; |
| 1117 | } |
| 1118 | |
| 1119 | return WOULD_BLOCK; |
| 1120 | } |
| 1121 | |
| 1122 | status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1123 | Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1124 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 1125 | if (! motionEvent) return NO_MEMORY; |
| 1126 | |
| 1127 | uint32_t chain = 0; |
| 1128 | for (size_t i = 0; i < count; i++) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1129 | InputMessage& msg = batch.samples[i]; |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1130 | updateTouchState(msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1131 | if (i) { |
| 1132 | SeqChain seqChain; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1133 | seqChain.seq = msg.header.seq; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1134 | seqChain.chain = chain; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1135 | mSeqChains.push_back(seqChain); |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 1136 | addSample(*motionEvent, msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1137 | } else { |
Siarhei Vishniakou | 9dbf637 | 2024-03-06 16:08:01 -0800 | [diff] [blame] | 1138 | initializeMotionEvent(*motionEvent, msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1139 | } |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1140 | chain = msg.header.seq; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1141 | } |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1142 | batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1143 | |
| 1144 | *outSeq = chain; |
| 1145 | *outEvent = motionEvent; |
| 1146 | return OK; |
| 1147 | } |
| 1148 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1149 | void InputConsumer::updateTouchState(InputMessage& msg) { |
Siarhei Vishniakou | 128eab1 | 2019-05-23 10:25:59 +0800 | [diff] [blame] | 1150 | if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1151 | return; |
| 1152 | } |
| 1153 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1154 | int32_t deviceId = msg.body.motion.deviceId; |
| 1155 | int32_t source = msg.body.motion.source; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1156 | |
| 1157 | // Update the touch state history to incorporate the new input message. |
| 1158 | // If the message is in the past relative to the most recently produced resampled |
| 1159 | // touch, then use the resampled time and coordinates instead. |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1160 | switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1161 | case AMOTION_EVENT_ACTION_DOWN: { |
| 1162 | ssize_t index = findTouchState(deviceId, source); |
| 1163 | if (index < 0) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1164 | mTouchStates.push_back({}); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1165 | index = mTouchStates.size() - 1; |
| 1166 | } |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1167 | TouchState& touchState = mTouchStates[index]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1168 | touchState.initialize(deviceId, source); |
| 1169 | touchState.addHistory(msg); |
| 1170 | break; |
| 1171 | } |
| 1172 | |
| 1173 | case AMOTION_EVENT_ACTION_MOVE: { |
| 1174 | ssize_t index = findTouchState(deviceId, source); |
| 1175 | if (index >= 0) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1176 | TouchState& touchState = mTouchStates[index]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1177 | touchState.addHistory(msg); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1178 | rewriteMessage(touchState, msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1179 | } |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | case AMOTION_EVENT_ACTION_POINTER_DOWN: { |
| 1184 | ssize_t index = findTouchState(deviceId, source); |
| 1185 | if (index >= 0) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1186 | TouchState& touchState = mTouchStates[index]; |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1187 | touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1188 | rewriteMessage(touchState, msg); |
| 1189 | } |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 1194 | ssize_t index = findTouchState(deviceId, source); |
| 1195 | if (index >= 0) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1196 | TouchState& touchState = mTouchStates[index]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1197 | rewriteMessage(touchState, msg); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1198 | touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1199 | } |
| 1200 | break; |
| 1201 | } |
| 1202 | |
| 1203 | case AMOTION_EVENT_ACTION_SCROLL: { |
| 1204 | ssize_t index = findTouchState(deviceId, source); |
| 1205 | if (index >= 0) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1206 | TouchState& touchState = mTouchStates[index]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1207 | rewriteMessage(touchState, msg); |
| 1208 | } |
| 1209 | break; |
| 1210 | } |
| 1211 | |
| 1212 | case AMOTION_EVENT_ACTION_UP: |
| 1213 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 1214 | ssize_t index = findTouchState(deviceId, source); |
| 1215 | if (index >= 0) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1216 | TouchState& touchState = mTouchStates[index]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1217 | rewriteMessage(touchState, msg); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1218 | mTouchStates.erase(mTouchStates.begin() + index); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1219 | } |
| 1220 | break; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1225 | /** |
| 1226 | * Replace the coordinates in msg with the coordinates in lastResample, if necessary. |
| 1227 | * |
| 1228 | * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time |
| 1229 | * is in the past relative to msg and the past two events do not contain identical coordinates), |
| 1230 | * then invalidate the lastResample data for that pointer. |
| 1231 | * If the two past events have identical coordinates, then lastResample data for that pointer will |
| 1232 | * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is |
| 1233 | * resampled to the new value x1, then x1 will always be used to replace x0 until some new value |
| 1234 | * not equal to x0 is received. |
| 1235 | */ |
| 1236 | void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) { |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1237 | nsecs_t eventTime = msg.body.motion.eventTime; |
| 1238 | for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) { |
| 1239 | uint32_t id = msg.body.motion.pointers[i].properties.id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1240 | if (state.lastResample.idBits.hasBit(id)) { |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1241 | if (eventTime < state.lastResample.eventTime || |
| 1242 | state.recentCoordinatesAreIdentical(id)) { |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1243 | PointerCoords& msgCoords = msg.body.motion.pointers[i].coords; |
| 1244 | const PointerCoords& resampleCoords = state.lastResample.getPointerById(id); |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1245 | ALOGD_IF(debugResampling(), "[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id, |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1246 | resampleCoords.getX(), resampleCoords.getY(), msgCoords.getX(), |
| 1247 | msgCoords.getY()); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1248 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX()); |
| 1249 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY()); |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame] | 1250 | msgCoords.isResampled = true; |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1251 | } else { |
| 1252 | state.lastResample.idBits.clearBit(id); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1253 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event, |
| 1259 | const InputMessage* next) { |
| 1260 | if (!mResampleTouch |
Siarhei Vishniakou | 128eab1 | 2019-05-23 10:25:59 +0800 | [diff] [blame] | 1261 | || !(isPointerEvent(event->getSource())) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1262 | || event->getAction() != AMOTION_EVENT_ACTION_MOVE) { |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | ssize_t index = findTouchState(event->getDeviceId(), event->getSource()); |
| 1267 | if (index < 0) { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1268 | ALOGD_IF(debugResampling(), "Not resampled, no touch state for device."); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1269 | return; |
| 1270 | } |
| 1271 | |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1272 | TouchState& touchState = mTouchStates[index]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1273 | if (touchState.historySize < 1) { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1274 | ALOGD_IF(debugResampling(), "Not resampled, no history for device."); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1275 | return; |
| 1276 | } |
| 1277 | |
| 1278 | // Ensure that the current sample has all of the pointers that need to be reported. |
| 1279 | const History* current = touchState.getHistory(0); |
| 1280 | size_t pointerCount = event->getPointerCount(); |
| 1281 | for (size_t i = 0; i < pointerCount; i++) { |
| 1282 | uint32_t id = event->getPointerId(i); |
| 1283 | if (!current->idBits.hasBit(id)) { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1284 | ALOGD_IF(debugResampling(), "Not resampled, missing id %d", id); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1285 | return; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | // Find the data to use for resampling. |
| 1290 | const History* other; |
| 1291 | History future; |
| 1292 | float alpha; |
| 1293 | if (next) { |
| 1294 | // Interpolate between current sample and future sample. |
| 1295 | // So current->eventTime <= sampleTime <= future.eventTime. |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 1296 | future.initializeFrom(*next); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1297 | other = &future; |
| 1298 | nsecs_t delta = future.eventTime - current->eventTime; |
| 1299 | if (delta < RESAMPLE_MIN_DELTA) { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1300 | ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.", |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1301 | delta); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1302 | return; |
| 1303 | } |
| 1304 | alpha = float(sampleTime - current->eventTime) / delta; |
| 1305 | } else if (touchState.historySize >= 2) { |
| 1306 | // Extrapolate future sample using current sample and past sample. |
| 1307 | // So other->eventTime <= current->eventTime <= sampleTime. |
| 1308 | other = touchState.getHistory(1); |
| 1309 | nsecs_t delta = current->eventTime - other->eventTime; |
| 1310 | if (delta < RESAMPLE_MIN_DELTA) { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1311 | ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.", |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1312 | delta); |
Andrew de los Reyes | de18f6c | 2015-10-01 15:57:25 -0700 | [diff] [blame] | 1313 | return; |
| 1314 | } else if (delta > RESAMPLE_MAX_DELTA) { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1315 | ALOGD_IF(debugResampling(), "Not resampled, delta time is too large: %" PRId64 " ns.", |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1316 | delta); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1317 | return; |
| 1318 | } |
Siarhei Vishniakou | 6c1e622 | 2024-03-07 13:39:24 -0800 | [diff] [blame^] | 1319 | nsecs_t maxPredict = current->eventTime + std::min(delta / 2, RESAMPLE_MAX_PREDICTION); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1320 | if (sampleTime > maxPredict) { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1321 | ALOGD_IF(debugResampling(), |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1322 | "Sample time is too far in the future, adjusting prediction " |
| 1323 | "from %" PRId64 " to %" PRId64 " ns.", |
| 1324 | sampleTime - current->eventTime, maxPredict - current->eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1325 | sampleTime = maxPredict; |
| 1326 | } |
| 1327 | alpha = float(current->eventTime - sampleTime) / delta; |
| 1328 | } else { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1329 | ALOGD_IF(debugResampling(), "Not resampled, insufficient data."); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1330 | return; |
| 1331 | } |
| 1332 | |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 1333 | if (current->eventTime == sampleTime) { |
| 1334 | // Prevents having 2 events with identical times and coordinates. |
| 1335 | return; |
| 1336 | } |
| 1337 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1338 | // Resample touch coordinates. |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1339 | History oldLastResample; |
| 1340 | oldLastResample.initializeFrom(touchState.lastResample); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1341 | touchState.lastResample.eventTime = sampleTime; |
| 1342 | touchState.lastResample.idBits.clear(); |
| 1343 | for (size_t i = 0; i < pointerCount; i++) { |
| 1344 | uint32_t id = event->getPointerId(i); |
| 1345 | touchState.lastResample.idToIndex[id] = i; |
| 1346 | touchState.lastResample.idBits.markBit(id); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1347 | if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) { |
| 1348 | // We maintain the previously resampled value for this pointer (stored in |
| 1349 | // oldLastResample) when the coordinates for this pointer haven't changed since then. |
| 1350 | // This way we don't introduce artificial jitter when pointers haven't actually moved. |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame] | 1351 | // The isResampled flag isn't cleared as the values don't reflect what the device is |
| 1352 | // actually reporting. |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1353 | |
| 1354 | // We know here that the coordinates for the pointer haven't changed because we |
| 1355 | // would've cleared the resampled bit in rewriteMessage if they had. We can't modify |
| 1356 | // lastResample in place becasue the mapping from pointer ID to index may have changed. |
Siarhei Vishniakou | 73e6d37 | 2023-07-06 18:07:21 -0700 | [diff] [blame] | 1357 | touchState.lastResample.pointers[i] = oldLastResample.getPointerById(id); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1358 | continue; |
| 1359 | } |
| 1360 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1361 | PointerCoords& resampledCoords = touchState.lastResample.pointers[i]; |
| 1362 | const PointerCoords& currentCoords = current->getPointerById(id); |
Siarhei Vishniakou | 73e6d37 | 2023-07-06 18:07:21 -0700 | [diff] [blame] | 1363 | resampledCoords = currentCoords; |
Philip Quinn | 4e955a2 | 2023-09-26 12:09:40 -0700 | [diff] [blame] | 1364 | resampledCoords.isResampled = true; |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1365 | if (other->idBits.hasBit(id) && shouldResampleTool(event->getToolType(i))) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1366 | const PointerCoords& otherCoords = other->getPointerById(id); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1367 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X, |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1368 | lerp(currentCoords.getX(), otherCoords.getX(), alpha)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1369 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1370 | lerp(currentCoords.getY(), otherCoords.getY(), alpha)); |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1371 | ALOGD_IF(debugResampling(), |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1372 | "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), " |
| 1373 | "other (%0.3f, %0.3f), alpha %0.3f", |
| 1374 | id, resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(), |
| 1375 | currentCoords.getY(), otherCoords.getX(), otherCoords.getY(), alpha); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1376 | } else { |
Harry Cutts | 6c658cc | 2023-08-02 14:40:40 +0000 | [diff] [blame] | 1377 | ALOGD_IF(debugResampling(), "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", id, |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1378 | resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(), |
| 1379 | currentCoords.getY()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | event->addSample(sampleTime, touchState.lastResample.pointers); |
| 1384 | } |
| 1385 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1386 | status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) { |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1387 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 1388 | "channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s", |
| 1389 | mChannel->getName().c_str(), seq, toString(handled)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1390 | |
| 1391 | if (!seq) { |
| 1392 | ALOGE("Attempted to send a finished signal with sequence number 0."); |
| 1393 | return BAD_VALUE; |
| 1394 | } |
| 1395 | |
| 1396 | // Send finished signals for the batch sequence chain first. |
| 1397 | size_t seqChainCount = mSeqChains.size(); |
| 1398 | if (seqChainCount) { |
| 1399 | uint32_t currentSeq = seq; |
| 1400 | uint32_t chainSeqs[seqChainCount]; |
| 1401 | size_t chainIndex = 0; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 1402 | for (size_t i = seqChainCount; i > 0; ) { |
| 1403 | i--; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1404 | const SeqChain& seqChain = mSeqChains[i]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1405 | if (seqChain.seq == currentSeq) { |
| 1406 | currentSeq = seqChain.chain; |
| 1407 | chainSeqs[chainIndex++] = currentSeq; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1408 | mSeqChains.erase(mSeqChains.begin() + i); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | status_t status = OK; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 1412 | while (!status && chainIndex > 0) { |
| 1413 | chainIndex--; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1414 | status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled); |
| 1415 | } |
| 1416 | if (status) { |
| 1417 | // An error occurred so at least one signal was not sent, reconstruct the chain. |
gaoshang | 9090d4f | 2017-05-17 14:36:46 +0800 | [diff] [blame] | 1418 | for (;;) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1419 | SeqChain seqChain; |
| 1420 | seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq; |
| 1421 | seqChain.chain = chainSeqs[chainIndex]; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1422 | mSeqChains.push_back(seqChain); |
gaoshang | 9090d4f | 2017-05-17 14:36:46 +0800 | [diff] [blame] | 1423 | if (!chainIndex) break; |
| 1424 | chainIndex--; |
| 1425 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1426 | return status; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | // Send finished signal for the last message in the batch. |
| 1431 | return sendUnchainedFinishedSignal(seq, handled); |
| 1432 | } |
| 1433 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1434 | status_t InputConsumer::sendTimeline(int32_t inputEventId, |
| 1435 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) { |
Prabir Pradhan | 60dd97a | 2023-02-23 02:23:02 +0000 | [diff] [blame] | 1436 | ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, |
| 1437 | "channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32 |
| 1438 | ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64, |
| 1439 | mChannel->getName().c_str(), inputEventId, |
| 1440 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME], |
| 1441 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME]); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1442 | |
| 1443 | InputMessage msg; |
| 1444 | msg.header.type = InputMessage::Type::TIMELINE; |
| 1445 | msg.header.seq = 0; |
| 1446 | msg.body.timeline.eventId = inputEventId; |
| 1447 | msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline); |
| 1448 | return mChannel->sendMessage(&msg); |
| 1449 | } |
| 1450 | |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 1451 | nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const { |
| 1452 | auto it = mConsumeTimes.find(seq); |
| 1453 | // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was |
| 1454 | // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed. |
| 1455 | LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32, |
| 1456 | seq); |
| 1457 | return it->second; |
| 1458 | } |
| 1459 | |
| 1460 | void InputConsumer::popConsumeTime(uint32_t seq) { |
| 1461 | mConsumeTimes.erase(seq); |
| 1462 | } |
| 1463 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1464 | status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) { |
| 1465 | InputMessage msg; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 1466 | msg.header.type = InputMessage::Type::FINISHED; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1467 | msg.header.seq = seq; |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 1468 | msg.body.finished.handled = handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 1469 | msg.body.finished.consumeTime = getConsumeTime(seq); |
| 1470 | status_t result = mChannel->sendMessage(&msg); |
| 1471 | if (result == OK) { |
| 1472 | // Remove the consume time if the socket write succeeded. We will not need to ack this |
| 1473 | // message anymore. If the socket write did not succeed, we will try again and will still |
| 1474 | // need consume time. |
| 1475 | popConsumeTime(seq); |
Siarhei Vishniakou | ca42e0d | 2024-01-12 12:10:32 -0800 | [diff] [blame] | 1476 | |
| 1477 | // Trace the event processing timeline - event was just finished |
| 1478 | ATRACE_ASYNC_END("InputConsumer processing", /*cookie=*/seq); |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 1479 | } |
| 1480 | return result; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1483 | bool InputConsumer::hasPendingBatch() const { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1484 | return !mBatches.empty(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
Arthur Hung | c7812be | 2020-02-27 22:40:27 +0800 | [diff] [blame] | 1487 | int32_t InputConsumer::getPendingBatchSource() const { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1488 | if (mBatches.empty()) { |
Arthur Hung | c7812be | 2020-02-27 22:40:27 +0800 | [diff] [blame] | 1489 | return AINPUT_SOURCE_CLASS_NONE; |
| 1490 | } |
| 1491 | |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1492 | const Batch& batch = mBatches[0]; |
| 1493 | const InputMessage& head = batch.samples[0]; |
Arthur Hung | c7812be | 2020-02-27 22:40:27 +0800 | [diff] [blame] | 1494 | return head.body.motion.source; |
| 1495 | } |
| 1496 | |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 1497 | bool InputConsumer::probablyHasInput() const { |
| 1498 | return hasPendingBatch() || mChannel->probablyHasInput(); |
| 1499 | } |
| 1500 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1501 | ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const { |
| 1502 | for (size_t i = 0; i < mBatches.size(); i++) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1503 | const Batch& batch = mBatches[i]; |
| 1504 | const InputMessage& head = batch.samples[0]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1505 | if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) { |
| 1506 | return i; |
| 1507 | } |
| 1508 | } |
| 1509 | return -1; |
| 1510 | } |
| 1511 | |
| 1512 | ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const { |
| 1513 | for (size_t i = 0; i < mTouchStates.size(); i++) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1514 | const TouchState& touchState = mTouchStates[i]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1515 | if (touchState.deviceId == deviceId && touchState.source == source) { |
| 1516 | return i; |
| 1517 | } |
| 1518 | } |
| 1519 | return -1; |
| 1520 | } |
| 1521 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1522 | bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1523 | const InputMessage& head = batch.samples[0]; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 1524 | uint32_t pointerCount = msg->body.motion.pointerCount; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1525 | if (head.body.motion.pointerCount != pointerCount |
| 1526 | || head.body.motion.action != msg->body.motion.action) { |
| 1527 | return false; |
| 1528 | } |
| 1529 | for (size_t i = 0; i < pointerCount; i++) { |
| 1530 | if (head.body.motion.pointers[i].properties |
| 1531 | != msg->body.motion.pointers[i].properties) { |
| 1532 | return false; |
| 1533 | } |
| 1534 | } |
| 1535 | return true; |
| 1536 | } |
| 1537 | |
| 1538 | ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) { |
| 1539 | size_t numSamples = batch.samples.size(); |
| 1540 | size_t index = 0; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1541 | while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1542 | index += 1; |
| 1543 | } |
| 1544 | return ssize_t(index) - 1; |
| 1545 | } |
| 1546 | |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1547 | std::string InputConsumer::dump() const { |
| 1548 | std::string out; |
| 1549 | out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n"; |
| 1550 | out = out + "mChannel = " + mChannel->getName() + "\n"; |
| 1551 | out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n"; |
| 1552 | if (mMsgDeferred) { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1553 | out = out + "mMsg : " + ftl::enum_string(mMsg.header.type) + "\n"; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1554 | } |
| 1555 | out += "Batches:\n"; |
| 1556 | for (const Batch& batch : mBatches) { |
| 1557 | out += " Batch:\n"; |
| 1558 | for (const InputMessage& msg : batch.samples) { |
| 1559 | out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1560 | ftl::enum_string(msg.header.type).c_str()); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1561 | switch (msg.header.type) { |
| 1562 | case InputMessage::Type::KEY: { |
| 1563 | out += android::base::StringPrintf("action=%s keycode=%" PRId32, |
| 1564 | KeyEvent::actionToString( |
| 1565 | msg.body.key.action), |
| 1566 | msg.body.key.keyCode); |
| 1567 | break; |
| 1568 | } |
| 1569 | case InputMessage::Type::MOTION: { |
| 1570 | out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action); |
| 1571 | for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) { |
| 1572 | const float x = msg.body.motion.pointers[i].coords.getX(); |
| 1573 | const float y = msg.body.motion.pointers[i].coords.getY(); |
| 1574 | out += android::base::StringPrintf("\n Pointer %" PRIu32 |
| 1575 | " : x=%.1f y=%.1f", |
| 1576 | i, x, y); |
| 1577 | } |
| 1578 | break; |
| 1579 | } |
| 1580 | case InputMessage::Type::FINISHED: { |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 1581 | out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64, |
| 1582 | toString(msg.body.finished.handled), |
| 1583 | msg.body.finished.consumeTime); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1584 | break; |
| 1585 | } |
| 1586 | case InputMessage::Type::FOCUS: { |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 1587 | out += android::base::StringPrintf("hasFocus=%s", |
| 1588 | toString(msg.body.focus.hasFocus)); |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1589 | break; |
| 1590 | } |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1591 | case InputMessage::Type::CAPTURE: { |
| 1592 | out += android::base::StringPrintf("hasCapture=%s", |
| 1593 | toString(msg.body.capture |
| 1594 | .pointerCaptureEnabled)); |
| 1595 | break; |
| 1596 | } |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1597 | case InputMessage::Type::DRAG: { |
| 1598 | out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s", |
| 1599 | msg.body.drag.x, msg.body.drag.y, |
| 1600 | toString(msg.body.drag.isExiting)); |
| 1601 | break; |
| 1602 | } |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1603 | case InputMessage::Type::TIMELINE: { |
| 1604 | const nsecs_t gpuCompletedTime = |
| 1605 | msg.body.timeline |
| 1606 | .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME]; |
| 1607 | const nsecs_t presentTime = |
| 1608 | msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME]; |
| 1609 | out += android::base::StringPrintf("inputEventId=%" PRId32 |
| 1610 | ", gpuCompletedTime=%" PRId64 |
| 1611 | ", presentTime=%" PRId64, |
| 1612 | msg.body.timeline.eventId, gpuCompletedTime, |
| 1613 | presentTime); |
| 1614 | break; |
| 1615 | } |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 1616 | case InputMessage::Type::TOUCH_MODE: { |
| 1617 | out += android::base::StringPrintf("isInTouchMode=%s", |
| 1618 | toString(msg.body.touchMode.isInTouchMode)); |
| 1619 | break; |
| 1620 | } |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1621 | } |
| 1622 | out += "\n"; |
| 1623 | } |
| 1624 | } |
| 1625 | if (mBatches.empty()) { |
| 1626 | out += " <empty>\n"; |
| 1627 | } |
| 1628 | out += "mSeqChains:\n"; |
| 1629 | for (const SeqChain& chain : mSeqChains) { |
| 1630 | out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq, |
| 1631 | chain.chain); |
| 1632 | } |
| 1633 | if (mSeqChains.empty()) { |
| 1634 | out += " <empty>\n"; |
| 1635 | } |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 1636 | out += "mConsumeTimes:\n"; |
| 1637 | for (const auto& [seq, consumeTime] : mConsumeTimes) { |
| 1638 | out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq, |
| 1639 | consumeTime); |
| 1640 | } |
| 1641 | if (mConsumeTimes.empty()) { |
| 1642 | out += " <empty>\n"; |
| 1643 | } |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 1644 | return out; |
| 1645 | } |
| 1646 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1647 | } // namespace android |