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