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