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