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