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