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 |
| 14 | #define DEBUG_CHANNEL_LIFECYCLE 0 |
| 15 | |
| 16 | // Log debug messages about transport actions |
| 17 | #define DEBUG_TRANSPORT_ACTIONS 0 |
| 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> |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 37 | #include <statslog.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 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 92 | // --- InputMessage --- |
| 93 | |
| 94 | bool InputMessage::isValid(size_t actualSize) const { |
| 95 | if (size() == actualSize) { |
| 96 | switch (header.type) { |
| 97 | case TYPE_KEY: |
| 98 | return true; |
| 99 | case TYPE_MOTION: |
| 100 | return body.motion.pointerCount > 0 |
| 101 | && body.motion.pointerCount <= MAX_POINTERS; |
| 102 | case TYPE_FINISHED: |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | size_t InputMessage::size() const { |
| 110 | switch (header.type) { |
| 111 | case TYPE_KEY: |
| 112 | return sizeof(Header) + body.key.size(); |
| 113 | case TYPE_MOTION: |
| 114 | return sizeof(Header) + body.motion.size(); |
| 115 | case TYPE_FINISHED: |
| 116 | return sizeof(Header) + body.finished.size(); |
| 117 | } |
| 118 | return sizeof(Header); |
| 119 | } |
| 120 | |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 121 | /** |
| 122 | * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire |
| 123 | * memory to zero, then only copy the valid bytes on a per-field basis. |
| 124 | */ |
| 125 | void InputMessage::getSanitizedCopy(InputMessage* msg) const { |
| 126 | memset(msg, 0, sizeof(*msg)); |
| 127 | |
| 128 | // Write the header |
| 129 | msg->header.type = header.type; |
| 130 | |
| 131 | // Write the body |
| 132 | switch(header.type) { |
| 133 | case InputMessage::TYPE_KEY: { |
| 134 | // uint32_t seq |
| 135 | msg->body.key.seq = body.key.seq; |
| 136 | // nsecs_t eventTime |
| 137 | msg->body.key.eventTime = body.key.eventTime; |
| 138 | // int32_t deviceId |
| 139 | msg->body.key.deviceId = body.key.deviceId; |
| 140 | // int32_t source |
| 141 | msg->body.key.source = body.key.source; |
| 142 | // int32_t displayId |
| 143 | msg->body.key.displayId = body.key.displayId; |
| 144 | // int32_t action |
| 145 | msg->body.key.action = body.key.action; |
| 146 | // int32_t flags |
| 147 | msg->body.key.flags = body.key.flags; |
| 148 | // int32_t keyCode |
| 149 | msg->body.key.keyCode = body.key.keyCode; |
| 150 | // int32_t scanCode |
| 151 | msg->body.key.scanCode = body.key.scanCode; |
| 152 | // int32_t metaState |
| 153 | msg->body.key.metaState = body.key.metaState; |
| 154 | // int32_t repeatCount |
| 155 | msg->body.key.repeatCount = body.key.repeatCount; |
| 156 | // nsecs_t downTime |
| 157 | msg->body.key.downTime = body.key.downTime; |
| 158 | break; |
| 159 | } |
| 160 | case InputMessage::TYPE_MOTION: { |
| 161 | // uint32_t seq |
| 162 | msg->body.motion.seq = body.motion.seq; |
| 163 | // nsecs_t eventTime |
| 164 | msg->body.motion.eventTime = body.motion.eventTime; |
| 165 | // int32_t deviceId |
| 166 | msg->body.motion.deviceId = body.motion.deviceId; |
| 167 | // int32_t source |
| 168 | msg->body.motion.source = body.motion.source; |
| 169 | // int32_t displayId |
| 170 | msg->body.motion.displayId = body.motion.displayId; |
| 171 | // int32_t action |
| 172 | msg->body.motion.action = body.motion.action; |
| 173 | // int32_t actionButton |
| 174 | msg->body.motion.actionButton = body.motion.actionButton; |
| 175 | // int32_t flags |
| 176 | msg->body.motion.flags = body.motion.flags; |
| 177 | // int32_t metaState |
| 178 | msg->body.motion.metaState = body.motion.metaState; |
| 179 | // int32_t buttonState |
| 180 | msg->body.motion.buttonState = body.motion.buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 181 | // MotionClassification classification |
| 182 | msg->body.motion.classification = body.motion.classification; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 183 | // int32_t edgeFlags |
| 184 | msg->body.motion.edgeFlags = body.motion.edgeFlags; |
| 185 | // nsecs_t downTime |
| 186 | msg->body.motion.downTime = body.motion.downTime; |
| 187 | // float xOffset |
| 188 | msg->body.motion.xOffset = body.motion.xOffset; |
| 189 | // float yOffset |
| 190 | msg->body.motion.yOffset = body.motion.yOffset; |
| 191 | // float xPrecision |
| 192 | msg->body.motion.xPrecision = body.motion.xPrecision; |
| 193 | // float yPrecision |
| 194 | msg->body.motion.yPrecision = body.motion.yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 195 | // float xCursorPosition |
| 196 | msg->body.motion.xCursorPosition = body.motion.xCursorPosition; |
| 197 | // float yCursorPosition |
| 198 | msg->body.motion.yCursorPosition = body.motion.yCursorPosition; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 199 | // uint32_t pointerCount |
| 200 | msg->body.motion.pointerCount = body.motion.pointerCount; |
| 201 | //struct Pointer pointers[MAX_POINTERS] |
| 202 | for (size_t i = 0; i < body.motion.pointerCount; i++) { |
| 203 | // PointerProperties properties |
| 204 | msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id; |
| 205 | msg->body.motion.pointers[i].properties.toolType = |
| 206 | body.motion.pointers[i].properties.toolType, |
| 207 | // PointerCoords coords |
| 208 | msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits; |
| 209 | const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits); |
| 210 | memcpy(&msg->body.motion.pointers[i].coords.values[0], |
| 211 | &body.motion.pointers[i].coords.values[0], |
| 212 | count * (sizeof(body.motion.pointers[i].coords.values[0]))); |
| 213 | } |
| 214 | break; |
| 215 | } |
| 216 | case InputMessage::TYPE_FINISHED: { |
| 217 | msg->body.finished.seq = body.finished.seq; |
| 218 | msg->body.finished.handled = body.finished.handled; |
| 219 | break; |
| 220 | } |
| 221 | default: { |
| 222 | LOG_FATAL("Unexpected message type %i", header.type); |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 227 | |
| 228 | // --- InputChannel --- |
| 229 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 230 | sp<InputChannel> InputChannel::create(const std::string& name, android::base::unique_fd fd) { |
| 231 | const int result = fcntl(fd, F_SETFL, O_NONBLOCK); |
| 232 | if (result != 0) { |
| 233 | LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(), |
| 234 | strerror(errno)); |
| 235 | return nullptr; |
| 236 | } |
| 237 | return new InputChannel(name, std::move(fd)); |
| 238 | } |
| 239 | |
| 240 | InputChannel::InputChannel(const std::string& name, android::base::unique_fd fd) |
| 241 | : mName(name), mFd(std::move(fd)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 242 | #if DEBUG_CHANNEL_LIFECYCLE |
| 243 | ALOGD("Input channel constructed: name='%s', fd=%d", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 244 | mName.c_str(), fd); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 245 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | InputChannel::~InputChannel() { |
| 249 | #if DEBUG_CHANNEL_LIFECYCLE |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 250 | ALOGD("Input channel destroyed: name='%s', fd=%d", mName.c_str(), mFd.get()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 251 | #endif |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 254 | status_t InputChannel::openInputChannelPair(const std::string& name, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 255 | sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) { |
| 256 | int sockets[2]; |
| 257 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) { |
| 258 | status_t result = -errno; |
| 259 | ALOGE("channel '%s' ~ Could not create socket pair. errno=%d", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 260 | name.c_str(), errno); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 261 | outServerChannel.clear(); |
| 262 | outClientChannel.clear(); |
| 263 | return result; |
| 264 | } |
| 265 | |
| 266 | int bufferSize = SOCKET_BUFFER_SIZE; |
| 267 | setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); |
| 268 | setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); |
| 269 | setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); |
| 270 | setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); |
| 271 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 272 | std::string serverChannelName = name + " (server)"; |
| 273 | android::base::unique_fd serverFd(sockets[0]); |
| 274 | outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 275 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 276 | std::string clientChannelName = name + " (client)"; |
| 277 | android::base::unique_fd clientFd(sockets[1]); |
| 278 | outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 279 | return OK; |
| 280 | } |
| 281 | |
| 282 | status_t InputChannel::sendMessage(const InputMessage* msg) { |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 283 | const size_t msgLength = msg->size(); |
| 284 | InputMessage cleanMsg; |
| 285 | msg->getSanitizedCopy(&cleanMsg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 286 | ssize_t nWrite; |
| 287 | do { |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 288 | nWrite = ::send(mFd.get(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 289 | } while (nWrite == -1 && errno == EINTR); |
| 290 | |
| 291 | if (nWrite < 0) { |
| 292 | int error = errno; |
| 293 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 294 | ALOGD("channel '%s' ~ error sending message of type %d, errno=%d", mName.c_str(), |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 295 | msg->header.type, error); |
| 296 | #endif |
| 297 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 298 | return WOULD_BLOCK; |
| 299 | } |
| 300 | if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) { |
| 301 | return DEAD_OBJECT; |
| 302 | } |
| 303 | return -error; |
| 304 | } |
| 305 | |
| 306 | if (size_t(nWrite) != msgLength) { |
| 307 | #if DEBUG_CHANNEL_MESSAGES |
| 308 | ALOGD("channel '%s' ~ error sending message type %d, send was incomplete", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 309 | mName.c_str(), msg->header.type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 310 | #endif |
| 311 | return DEAD_OBJECT; |
| 312 | } |
| 313 | |
| 314 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 315 | 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] | 316 | #endif |
| 317 | return OK; |
| 318 | } |
| 319 | |
| 320 | status_t InputChannel::receiveMessage(InputMessage* msg) { |
| 321 | ssize_t nRead; |
| 322 | do { |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 323 | nRead = ::recv(mFd.get(), msg, sizeof(InputMessage), MSG_DONTWAIT); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 324 | } while (nRead == -1 && errno == EINTR); |
| 325 | |
| 326 | if (nRead < 0) { |
| 327 | int error = errno; |
| 328 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 329 | ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 330 | #endif |
| 331 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 332 | return WOULD_BLOCK; |
| 333 | } |
| 334 | if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) { |
| 335 | return DEAD_OBJECT; |
| 336 | } |
| 337 | return -error; |
| 338 | } |
| 339 | |
| 340 | if (nRead == 0) { // check for EOF |
| 341 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 342 | 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] | 343 | #endif |
| 344 | return DEAD_OBJECT; |
| 345 | } |
| 346 | |
| 347 | if (!msg->isValid(nRead)) { |
| 348 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 349 | ALOGD("channel '%s' ~ received invalid message", mName.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 350 | #endif |
| 351 | return BAD_VALUE; |
| 352 | } |
| 353 | |
| 354 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 355 | 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] | 356 | #endif |
| 357 | return OK; |
| 358 | } |
| 359 | |
| 360 | sp<InputChannel> InputChannel::dup() const { |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 361 | android::base::unique_fd newFd(::dup(getFd())); |
| 362 | if (!newFd.ok()) { |
| 363 | ALOGE("Could not duplicate fd %i for channel %s: %s", getFd(), mName.c_str(), |
| 364 | strerror(errno)); |
Siarhei Vishniakou | 3d8df0e | 2019-09-17 14:53:07 +0100 | [diff] [blame^] | 365 | const bool hitFdLimit = errno == EMFILE || errno == ENFILE; |
| 366 | // If this process is out of file descriptors, then throwing that might end up exploding |
| 367 | // on the other side of a binder call, which isn't really helpful. |
| 368 | // Better to just crash here and hope that the FD leak is slow. |
| 369 | // Other failures could be client errors, so we still propagate those back to the caller. |
| 370 | LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s", |
| 371 | getName().c_str()); |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 372 | return nullptr; |
| 373 | } |
| 374 | return InputChannel::create(mName, std::move(newFd)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 377 | status_t InputChannel::write(Parcel& out) const { |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 378 | status_t s = out.writeCString(getName().c_str()); |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 379 | if (s != OK) { |
| 380 | return s; |
| 381 | } |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 382 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 383 | s = out.writeStrongBinder(mToken); |
| 384 | if (s != OK) { |
| 385 | return s; |
| 386 | } |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 387 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 388 | s = out.writeUniqueFileDescriptor(mFd); |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 389 | return s; |
| 390 | } |
| 391 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 392 | sp<InputChannel> InputChannel::read(const Parcel& from) { |
| 393 | std::string name = from.readCString(); |
| 394 | sp<IBinder> token = from.readStrongBinder(); |
| 395 | android::base::unique_fd rawFd; |
| 396 | status_t fdResult = from.readUniqueFileDescriptor(&rawFd); |
| 397 | if (fdResult != OK) { |
| 398 | return nullptr; |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 401 | sp<InputChannel> channel = InputChannel::create(name, std::move(rawFd)); |
| 402 | if (channel != nullptr) { |
| 403 | channel->setToken(token); |
| 404 | } |
| 405 | return channel; |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 408 | sp<IBinder> InputChannel::getToken() const { |
| 409 | return mToken; |
| 410 | } |
| 411 | |
| 412 | void InputChannel::setToken(const sp<IBinder>& token) { |
| 413 | if (mToken != nullptr) { |
| 414 | ALOGE("Assigning InputChannel (%s) a second handle?", mName.c_str()); |
| 415 | } |
| 416 | mToken = token; |
| 417 | } |
| 418 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 419 | // --- InputPublisher --- |
| 420 | |
| 421 | InputPublisher::InputPublisher(const sp<InputChannel>& channel) : |
| 422 | mChannel(channel) { |
| 423 | } |
| 424 | |
| 425 | InputPublisher::~InputPublisher() { |
| 426 | } |
| 427 | |
| 428 | status_t InputPublisher::publishKeyEvent( |
| 429 | uint32_t seq, |
| 430 | int32_t deviceId, |
| 431 | int32_t source, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 432 | int32_t displayId, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 433 | int32_t action, |
| 434 | int32_t flags, |
| 435 | int32_t keyCode, |
| 436 | int32_t scanCode, |
| 437 | int32_t metaState, |
| 438 | int32_t repeatCount, |
| 439 | nsecs_t downTime, |
| 440 | nsecs_t eventTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 441 | if (ATRACE_ENABLED()) { |
| 442 | std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")", |
| 443 | mChannel->getName().c_str(), keyCode); |
| 444 | ATRACE_NAME(message.c_str()); |
| 445 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 446 | #if DEBUG_TRANSPORT_ACTIONS |
| 447 | ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, " |
| 448 | "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d," |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 449 | "downTime=%" PRId64 ", eventTime=%" PRId64, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 450 | mChannel->getName().c_str(), seq, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 451 | deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, |
| 452 | downTime, eventTime); |
| 453 | #endif |
| 454 | |
| 455 | if (!seq) { |
| 456 | ALOGE("Attempted to publish a key event with sequence number 0."); |
| 457 | return BAD_VALUE; |
| 458 | } |
| 459 | |
| 460 | InputMessage msg; |
| 461 | msg.header.type = InputMessage::TYPE_KEY; |
| 462 | msg.body.key.seq = seq; |
| 463 | msg.body.key.deviceId = deviceId; |
| 464 | msg.body.key.source = source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 465 | msg.body.key.displayId = displayId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 466 | msg.body.key.action = action; |
| 467 | msg.body.key.flags = flags; |
| 468 | msg.body.key.keyCode = keyCode; |
| 469 | msg.body.key.scanCode = scanCode; |
| 470 | msg.body.key.metaState = metaState; |
| 471 | msg.body.key.repeatCount = repeatCount; |
| 472 | msg.body.key.downTime = downTime; |
| 473 | msg.body.key.eventTime = eventTime; |
| 474 | return mChannel->sendMessage(&msg); |
| 475 | } |
| 476 | |
| 477 | status_t InputPublisher::publishMotionEvent( |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 478 | uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId, int32_t action, |
| 479 | int32_t actionButton, int32_t flags, int32_t edgeFlags, int32_t metaState, |
| 480 | int32_t buttonState, MotionClassification classification, float xOffset, float yOffset, |
| 481 | float xPrecision, float yPrecision, float xCursorPosition, float yCursorPosition, |
| 482 | nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount, |
| 483 | const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 484 | if (ATRACE_ENABLED()) { |
| 485 | std::string message = StringPrintf( |
| 486 | "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")", |
| 487 | mChannel->getName().c_str(), action); |
| 488 | ATRACE_NAME(message.c_str()); |
| 489 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 490 | #if DEBUG_TRANSPORT_ACTIONS |
| 491 | ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, " |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 492 | "displayId=%" PRId32 ", " |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 493 | "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, " |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 494 | "metaState=0x%x, buttonState=0x%x, classification=%s, xOffset=%f, yOffset=%f, " |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 495 | "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", " |
Michael Wright | 63ff3a8 | 2014-06-10 13:03:17 -0700 | [diff] [blame] | 496 | "pointerCount=%" PRIu32, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 497 | mChannel->getName().c_str(), seq, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 498 | deviceId, source, displayId, action, actionButton, flags, edgeFlags, metaState, |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 499 | buttonState, motionClassificationToString(classification), |
| 500 | xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 501 | #endif |
| 502 | |
| 503 | if (!seq) { |
| 504 | ALOGE("Attempted to publish a motion event with sequence number 0."); |
| 505 | return BAD_VALUE; |
| 506 | } |
| 507 | |
| 508 | if (pointerCount > MAX_POINTERS || pointerCount < 1) { |
Michael Wright | 63ff3a8 | 2014-06-10 13:03:17 -0700 | [diff] [blame] | 509 | ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 510 | mChannel->getName().c_str(), pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 511 | return BAD_VALUE; |
| 512 | } |
| 513 | |
| 514 | InputMessage msg; |
| 515 | msg.header.type = InputMessage::TYPE_MOTION; |
| 516 | msg.body.motion.seq = seq; |
| 517 | msg.body.motion.deviceId = deviceId; |
| 518 | msg.body.motion.source = source; |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 519 | msg.body.motion.displayId = displayId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 520 | msg.body.motion.action = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 521 | msg.body.motion.actionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 522 | msg.body.motion.flags = flags; |
| 523 | msg.body.motion.edgeFlags = edgeFlags; |
| 524 | msg.body.motion.metaState = metaState; |
| 525 | msg.body.motion.buttonState = buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 526 | msg.body.motion.classification = classification; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 527 | msg.body.motion.xOffset = xOffset; |
| 528 | msg.body.motion.yOffset = yOffset; |
| 529 | msg.body.motion.xPrecision = xPrecision; |
| 530 | msg.body.motion.yPrecision = yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 531 | msg.body.motion.xCursorPosition = xCursorPosition; |
| 532 | msg.body.motion.yCursorPosition = yCursorPosition; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 533 | msg.body.motion.downTime = downTime; |
| 534 | msg.body.motion.eventTime = eventTime; |
| 535 | msg.body.motion.pointerCount = pointerCount; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 536 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 537 | msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]); |
| 538 | msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]); |
| 539 | } |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 540 | |
| 541 | if (source == AINPUT_SOURCE_TOUCHSCREEN) { |
| 542 | reportTouchEventForStatistics(eventTime); |
| 543 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 544 | return mChannel->sendMessage(&msg); |
| 545 | } |
| 546 | |
| 547 | status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) { |
| 548 | #if DEBUG_TRANSPORT_ACTIONS |
| 549 | ALOGD("channel '%s' publisher ~ receiveFinishedSignal", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 550 | mChannel->getName().c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 551 | #endif |
| 552 | |
| 553 | InputMessage msg; |
| 554 | status_t result = mChannel->receiveMessage(&msg); |
| 555 | if (result) { |
| 556 | *outSeq = 0; |
| 557 | *outHandled = false; |
| 558 | return result; |
| 559 | } |
| 560 | if (msg.header.type != InputMessage::TYPE_FINISHED) { |
| 561 | ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 562 | mChannel->getName().c_str(), msg.header.type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 563 | return UNKNOWN_ERROR; |
| 564 | } |
| 565 | *outSeq = msg.body.finished.seq; |
| 566 | *outHandled = msg.body.finished.handled; |
| 567 | return OK; |
| 568 | } |
| 569 | |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 570 | void InputPublisher::reportTouchEventForStatistics(nsecs_t evdevTime) { |
| 571 | if (mTouchStatistics.shouldReport()) { |
| 572 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 573 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 574 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 575 | mTouchStatistics.reset(); |
| 576 | } |
| 577 | nsecs_t latency = nanoseconds_to_microseconds(systemTime(CLOCK_MONOTONIC) - evdevTime); |
| 578 | mTouchStatistics.addValue(latency); |
| 579 | } |
| 580 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 581 | // --- InputConsumer --- |
| 582 | |
| 583 | InputConsumer::InputConsumer(const sp<InputChannel>& channel) : |
| 584 | mResampleTouch(isTouchResamplingEnabled()), |
| 585 | mChannel(channel), mMsgDeferred(false) { |
| 586 | } |
| 587 | |
| 588 | InputConsumer::~InputConsumer() { |
| 589 | } |
| 590 | |
| 591 | bool InputConsumer::isTouchResamplingEnabled() { |
Siarhei Vishniakou | b5433e9 | 2019-02-21 09:27:39 -0600 | [diff] [blame] | 592 | return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | status_t InputConsumer::consume(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 596 | bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 597 | #if DEBUG_TRANSPORT_ACTIONS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 598 | ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 599 | mChannel->getName().c_str(), consumeBatches ? "true" : "false", frameTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 600 | #endif |
| 601 | |
| 602 | *outSeq = 0; |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 603 | *outEvent = nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 604 | |
| 605 | // Fetch the next input message. |
| 606 | // Loop until an event can be returned or no additional events are received. |
| 607 | while (!*outEvent) { |
| 608 | if (mMsgDeferred) { |
| 609 | // mMsg contains a valid input message from the previous call to consume |
| 610 | // that has not yet been processed. |
| 611 | mMsgDeferred = false; |
| 612 | } else { |
| 613 | // Receive a fresh message. |
| 614 | status_t result = mChannel->receiveMessage(&mMsg); |
| 615 | if (result) { |
| 616 | // Consume the next batched event unless batches are being held for later. |
| 617 | if (consumeBatches || result != WOULD_BLOCK) { |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 618 | result = consumeBatch(factory, frameTime, outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 619 | if (*outEvent) { |
| 620 | #if DEBUG_TRANSPORT_ACTIONS |
| 621 | ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 622 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 623 | #endif |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | return result; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | switch (mMsg.header.type) { |
| 632 | case InputMessage::TYPE_KEY: { |
| 633 | KeyEvent* keyEvent = factory->createKeyEvent(); |
| 634 | if (!keyEvent) return NO_MEMORY; |
| 635 | |
| 636 | initializeKeyEvent(keyEvent, &mMsg); |
| 637 | *outSeq = mMsg.body.key.seq; |
| 638 | *outEvent = keyEvent; |
| 639 | #if DEBUG_TRANSPORT_ACTIONS |
| 640 | ALOGD("channel '%s' consumer ~ consumed key event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 641 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 642 | #endif |
| 643 | break; |
| 644 | } |
| 645 | |
gaoshang | e3e11a7 | 2017-08-08 16:11:31 +0800 | [diff] [blame] | 646 | case InputMessage::TYPE_MOTION: { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 647 | ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source); |
| 648 | if (batchIndex >= 0) { |
| 649 | Batch& batch = mBatches.editItemAt(batchIndex); |
| 650 | if (canAddSample(batch, &mMsg)) { |
| 651 | batch.samples.push(mMsg); |
| 652 | #if DEBUG_TRANSPORT_ACTIONS |
| 653 | ALOGD("channel '%s' consumer ~ appended to batch event", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 654 | mChannel->getName().c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 655 | #endif |
| 656 | break; |
Siarhei Vishniakou | 128eab1 | 2019-05-23 10:25:59 +0800 | [diff] [blame] | 657 | } else if (isPointerEvent(mMsg.body.motion.source) && |
| 658 | mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 659 | // No need to process events that we are going to cancel anyways |
| 660 | const size_t count = batch.samples.size(); |
| 661 | for (size_t i = 0; i < count; i++) { |
| 662 | const InputMessage& msg = batch.samples.itemAt(i); |
| 663 | sendFinishedSignal(msg.body.motion.seq, false); |
| 664 | } |
| 665 | batch.samples.removeItemsAt(0, count); |
| 666 | mBatches.removeAt(batchIndex); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 667 | } else { |
| 668 | // We cannot append to the batch in progress, so we need to consume |
| 669 | // the previous batch right now and defer the new message until later. |
| 670 | mMsgDeferred = true; |
| 671 | status_t result = consumeSamples(factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 672 | batch, batch.samples.size(), outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 673 | mBatches.removeAt(batchIndex); |
| 674 | if (result) { |
| 675 | return result; |
| 676 | } |
| 677 | #if DEBUG_TRANSPORT_ACTIONS |
| 678 | ALOGD("channel '%s' consumer ~ consumed batch event and " |
| 679 | "deferred current event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 680 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 681 | #endif |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | // Start a new batch if needed. |
| 687 | if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE |
| 688 | || mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 689 | mBatches.push(); |
| 690 | Batch& batch = mBatches.editTop(); |
| 691 | batch.samples.push(mMsg); |
| 692 | #if DEBUG_TRANSPORT_ACTIONS |
| 693 | ALOGD("channel '%s' consumer ~ started batch event", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 694 | mChannel->getName().c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 695 | #endif |
| 696 | break; |
| 697 | } |
| 698 | |
| 699 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 700 | if (! motionEvent) return NO_MEMORY; |
| 701 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 702 | updateTouchState(mMsg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 703 | initializeMotionEvent(motionEvent, &mMsg); |
| 704 | *outSeq = mMsg.body.motion.seq; |
| 705 | *outEvent = motionEvent; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 706 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 707 | #if DEBUG_TRANSPORT_ACTIONS |
| 708 | ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 709 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 710 | #endif |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | default: |
| 715 | ALOGE("channel '%s' consumer ~ Received unexpected message of type %d", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 716 | mChannel->getName().c_str(), mMsg.header.type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 717 | return UNKNOWN_ERROR; |
| 718 | } |
| 719 | } |
| 720 | return OK; |
| 721 | } |
| 722 | |
| 723 | status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 724 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 725 | status_t result; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 726 | for (size_t i = mBatches.size(); i > 0; ) { |
| 727 | i--; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 728 | Batch& batch = mBatches.editItemAt(i); |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 729 | if (frameTime < 0) { |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 730 | result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 731 | mBatches.removeAt(i); |
| 732 | return result; |
| 733 | } |
| 734 | |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 735 | nsecs_t sampleTime = frameTime; |
| 736 | if (mResampleTouch) { |
| 737 | sampleTime -= RESAMPLE_LATENCY; |
| 738 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 739 | ssize_t split = findSampleNoLaterThan(batch, sampleTime); |
| 740 | if (split < 0) { |
| 741 | continue; |
| 742 | } |
| 743 | |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 744 | result = consumeSamples(factory, batch, split + 1, outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 745 | const InputMessage* next; |
| 746 | if (batch.samples.isEmpty()) { |
| 747 | mBatches.removeAt(i); |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 748 | next = nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 749 | } else { |
| 750 | next = &batch.samples.itemAt(0); |
| 751 | } |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 752 | if (!result && mResampleTouch) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 753 | resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next); |
| 754 | } |
| 755 | return result; |
| 756 | } |
| 757 | |
| 758 | return WOULD_BLOCK; |
| 759 | } |
| 760 | |
| 761 | status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 762 | Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 763 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 764 | if (! motionEvent) return NO_MEMORY; |
| 765 | |
| 766 | uint32_t chain = 0; |
| 767 | for (size_t i = 0; i < count; i++) { |
| 768 | InputMessage& msg = batch.samples.editItemAt(i); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 769 | updateTouchState(msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 770 | if (i) { |
| 771 | SeqChain seqChain; |
| 772 | seqChain.seq = msg.body.motion.seq; |
| 773 | seqChain.chain = chain; |
| 774 | mSeqChains.push(seqChain); |
| 775 | addSample(motionEvent, &msg); |
| 776 | } else { |
| 777 | initializeMotionEvent(motionEvent, &msg); |
| 778 | } |
| 779 | chain = msg.body.motion.seq; |
| 780 | } |
| 781 | batch.samples.removeItemsAt(0, count); |
| 782 | |
| 783 | *outSeq = chain; |
| 784 | *outEvent = motionEvent; |
| 785 | return OK; |
| 786 | } |
| 787 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 788 | void InputConsumer::updateTouchState(InputMessage& msg) { |
Siarhei Vishniakou | 128eab1 | 2019-05-23 10:25:59 +0800 | [diff] [blame] | 789 | if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 790 | return; |
| 791 | } |
| 792 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 793 | int32_t deviceId = msg.body.motion.deviceId; |
| 794 | int32_t source = msg.body.motion.source; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 795 | |
| 796 | // Update the touch state history to incorporate the new input message. |
| 797 | // If the message is in the past relative to the most recently produced resampled |
| 798 | // touch, then use the resampled time and coordinates instead. |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 799 | switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 800 | case AMOTION_EVENT_ACTION_DOWN: { |
| 801 | ssize_t index = findTouchState(deviceId, source); |
| 802 | if (index < 0) { |
| 803 | mTouchStates.push(); |
| 804 | index = mTouchStates.size() - 1; |
| 805 | } |
| 806 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 807 | touchState.initialize(deviceId, source); |
| 808 | touchState.addHistory(msg); |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | case AMOTION_EVENT_ACTION_MOVE: { |
| 813 | ssize_t index = findTouchState(deviceId, source); |
| 814 | if (index >= 0) { |
| 815 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 816 | touchState.addHistory(msg); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 817 | rewriteMessage(touchState, msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 818 | } |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | case AMOTION_EVENT_ACTION_POINTER_DOWN: { |
| 823 | ssize_t index = findTouchState(deviceId, source); |
| 824 | if (index >= 0) { |
| 825 | TouchState& touchState = mTouchStates.editItemAt(index); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 826 | touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 827 | rewriteMessage(touchState, msg); |
| 828 | } |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 833 | ssize_t index = findTouchState(deviceId, source); |
| 834 | if (index >= 0) { |
| 835 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 836 | rewriteMessage(touchState, msg); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 837 | touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 838 | } |
| 839 | break; |
| 840 | } |
| 841 | |
| 842 | case AMOTION_EVENT_ACTION_SCROLL: { |
| 843 | ssize_t index = findTouchState(deviceId, source); |
| 844 | if (index >= 0) { |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 845 | TouchState& touchState = mTouchStates.editItemAt(index); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 846 | rewriteMessage(touchState, msg); |
| 847 | } |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | case AMOTION_EVENT_ACTION_UP: |
| 852 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 853 | ssize_t index = findTouchState(deviceId, source); |
| 854 | if (index >= 0) { |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 855 | TouchState& touchState = mTouchStates.editItemAt(index); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 856 | rewriteMessage(touchState, msg); |
| 857 | mTouchStates.removeAt(index); |
| 858 | } |
| 859 | break; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 864 | /** |
| 865 | * Replace the coordinates in msg with the coordinates in lastResample, if necessary. |
| 866 | * |
| 867 | * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time |
| 868 | * is in the past relative to msg and the past two events do not contain identical coordinates), |
| 869 | * then invalidate the lastResample data for that pointer. |
| 870 | * If the two past events have identical coordinates, then lastResample data for that pointer will |
| 871 | * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is |
| 872 | * resampled to the new value x1, then x1 will always be used to replace x0 until some new value |
| 873 | * not equal to x0 is received. |
| 874 | */ |
| 875 | void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) { |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 876 | nsecs_t eventTime = msg.body.motion.eventTime; |
| 877 | for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) { |
| 878 | uint32_t id = msg.body.motion.pointers[i].properties.id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 879 | if (state.lastResample.idBits.hasBit(id)) { |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 880 | if (eventTime < state.lastResample.eventTime || |
| 881 | state.recentCoordinatesAreIdentical(id)) { |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 882 | PointerCoords& msgCoords = msg.body.motion.pointers[i].coords; |
| 883 | const PointerCoords& resampleCoords = state.lastResample.getPointerById(id); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 884 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 885 | ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id, |
| 886 | resampleCoords.getX(), resampleCoords.getY(), |
| 887 | msgCoords.getX(), msgCoords.getY()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 888 | #endif |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 889 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX()); |
| 890 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY()); |
| 891 | } else { |
| 892 | state.lastResample.idBits.clearBit(id); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 893 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event, |
| 899 | const InputMessage* next) { |
| 900 | if (!mResampleTouch |
Siarhei Vishniakou | 128eab1 | 2019-05-23 10:25:59 +0800 | [diff] [blame] | 901 | || !(isPointerEvent(event->getSource())) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 902 | || event->getAction() != AMOTION_EVENT_ACTION_MOVE) { |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | ssize_t index = findTouchState(event->getDeviceId(), event->getSource()); |
| 907 | if (index < 0) { |
| 908 | #if DEBUG_RESAMPLING |
| 909 | ALOGD("Not resampled, no touch state for device."); |
| 910 | #endif |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 915 | if (touchState.historySize < 1) { |
| 916 | #if DEBUG_RESAMPLING |
| 917 | ALOGD("Not resampled, no history for device."); |
| 918 | #endif |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | // Ensure that the current sample has all of the pointers that need to be reported. |
| 923 | const History* current = touchState.getHistory(0); |
| 924 | size_t pointerCount = event->getPointerCount(); |
| 925 | for (size_t i = 0; i < pointerCount; i++) { |
| 926 | uint32_t id = event->getPointerId(i); |
| 927 | if (!current->idBits.hasBit(id)) { |
| 928 | #if DEBUG_RESAMPLING |
| 929 | ALOGD("Not resampled, missing id %d", id); |
| 930 | #endif |
| 931 | return; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | // Find the data to use for resampling. |
| 936 | const History* other; |
| 937 | History future; |
| 938 | float alpha; |
| 939 | if (next) { |
| 940 | // Interpolate between current sample and future sample. |
| 941 | // So current->eventTime <= sampleTime <= future.eventTime. |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 942 | future.initializeFrom(*next); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 943 | other = &future; |
| 944 | nsecs_t delta = future.eventTime - current->eventTime; |
| 945 | if (delta < RESAMPLE_MIN_DELTA) { |
| 946 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 947 | ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 948 | #endif |
| 949 | return; |
| 950 | } |
| 951 | alpha = float(sampleTime - current->eventTime) / delta; |
| 952 | } else if (touchState.historySize >= 2) { |
| 953 | // Extrapolate future sample using current sample and past sample. |
| 954 | // So other->eventTime <= current->eventTime <= sampleTime. |
| 955 | other = touchState.getHistory(1); |
| 956 | nsecs_t delta = current->eventTime - other->eventTime; |
| 957 | if (delta < RESAMPLE_MIN_DELTA) { |
| 958 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 959 | 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] | 960 | #endif |
| 961 | return; |
| 962 | } else if (delta > RESAMPLE_MAX_DELTA) { |
| 963 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 964 | ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 965 | #endif |
| 966 | return; |
| 967 | } |
| 968 | nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION); |
| 969 | if (sampleTime > maxPredict) { |
| 970 | #if DEBUG_RESAMPLING |
| 971 | ALOGD("Sample time is too far in the future, adjusting prediction " |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 972 | "from %" PRId64 " to %" PRId64 " ns.", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 973 | sampleTime - current->eventTime, maxPredict - current->eventTime); |
| 974 | #endif |
| 975 | sampleTime = maxPredict; |
| 976 | } |
| 977 | alpha = float(current->eventTime - sampleTime) / delta; |
| 978 | } else { |
| 979 | #if DEBUG_RESAMPLING |
| 980 | ALOGD("Not resampled, insufficient data."); |
| 981 | #endif |
| 982 | return; |
| 983 | } |
| 984 | |
| 985 | // Resample touch coordinates. |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 986 | History oldLastResample; |
| 987 | oldLastResample.initializeFrom(touchState.lastResample); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 988 | touchState.lastResample.eventTime = sampleTime; |
| 989 | touchState.lastResample.idBits.clear(); |
| 990 | for (size_t i = 0; i < pointerCount; i++) { |
| 991 | uint32_t id = event->getPointerId(i); |
| 992 | touchState.lastResample.idToIndex[id] = i; |
| 993 | touchState.lastResample.idBits.markBit(id); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 994 | if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) { |
| 995 | // We maintain the previously resampled value for this pointer (stored in |
| 996 | // oldLastResample) when the coordinates for this pointer haven't changed since then. |
| 997 | // This way we don't introduce artificial jitter when pointers haven't actually moved. |
| 998 | |
| 999 | // We know here that the coordinates for the pointer haven't changed because we |
| 1000 | // would've cleared the resampled bit in rewriteMessage if they had. We can't modify |
| 1001 | // lastResample in place becasue the mapping from pointer ID to index may have changed. |
| 1002 | touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id)); |
| 1003 | continue; |
| 1004 | } |
| 1005 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1006 | PointerCoords& resampledCoords = touchState.lastResample.pointers[i]; |
| 1007 | const PointerCoords& currentCoords = current->getPointerById(id); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 1008 | resampledCoords.copyFrom(currentCoords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1009 | if (other->idBits.hasBit(id) |
| 1010 | && shouldResampleTool(event->getToolType(i))) { |
| 1011 | const PointerCoords& otherCoords = other->getPointerById(id); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1012 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X, |
| 1013 | lerp(currentCoords.getX(), otherCoords.getX(), alpha)); |
| 1014 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 1015 | lerp(currentCoords.getY(), otherCoords.getY(), alpha)); |
| 1016 | #if DEBUG_RESAMPLING |
| 1017 | ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), " |
| 1018 | "other (%0.3f, %0.3f), alpha %0.3f", |
| 1019 | id, resampledCoords.getX(), resampledCoords.getY(), |
| 1020 | currentCoords.getX(), currentCoords.getY(), |
| 1021 | otherCoords.getX(), otherCoords.getY(), |
| 1022 | alpha); |
| 1023 | #endif |
| 1024 | } else { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1025 | #if DEBUG_RESAMPLING |
| 1026 | ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", |
| 1027 | id, resampledCoords.getX(), resampledCoords.getY(), |
| 1028 | currentCoords.getX(), currentCoords.getY()); |
| 1029 | #endif |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | event->addSample(sampleTime, touchState.lastResample.pointers); |
| 1034 | } |
| 1035 | |
| 1036 | bool InputConsumer::shouldResampleTool(int32_t toolType) { |
| 1037 | return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 1038 | || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1039 | } |
| 1040 | |
| 1041 | status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) { |
| 1042 | #if DEBUG_TRANSPORT_ACTIONS |
| 1043 | ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1044 | mChannel->getName().c_str(), seq, handled ? "true" : "false"); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1045 | #endif |
| 1046 | |
| 1047 | if (!seq) { |
| 1048 | ALOGE("Attempted to send a finished signal with sequence number 0."); |
| 1049 | return BAD_VALUE; |
| 1050 | } |
| 1051 | |
| 1052 | // Send finished signals for the batch sequence chain first. |
| 1053 | size_t seqChainCount = mSeqChains.size(); |
| 1054 | if (seqChainCount) { |
| 1055 | uint32_t currentSeq = seq; |
| 1056 | uint32_t chainSeqs[seqChainCount]; |
| 1057 | size_t chainIndex = 0; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 1058 | for (size_t i = seqChainCount; i > 0; ) { |
| 1059 | i--; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1060 | const SeqChain& seqChain = mSeqChains.itemAt(i); |
| 1061 | if (seqChain.seq == currentSeq) { |
| 1062 | currentSeq = seqChain.chain; |
| 1063 | chainSeqs[chainIndex++] = currentSeq; |
| 1064 | mSeqChains.removeAt(i); |
| 1065 | } |
| 1066 | } |
| 1067 | status_t status = OK; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 1068 | while (!status && chainIndex > 0) { |
| 1069 | chainIndex--; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1070 | status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled); |
| 1071 | } |
| 1072 | if (status) { |
| 1073 | // 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] | 1074 | for (;;) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1075 | SeqChain seqChain; |
| 1076 | seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq; |
| 1077 | seqChain.chain = chainSeqs[chainIndex]; |
| 1078 | mSeqChains.push(seqChain); |
gaoshang | 9090d4f | 2017-05-17 14:36:46 +0800 | [diff] [blame] | 1079 | if (!chainIndex) break; |
| 1080 | chainIndex--; |
| 1081 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1082 | return status; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | // Send finished signal for the last message in the batch. |
| 1087 | return sendUnchainedFinishedSignal(seq, handled); |
| 1088 | } |
| 1089 | |
| 1090 | status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) { |
| 1091 | InputMessage msg; |
| 1092 | msg.header.type = InputMessage::TYPE_FINISHED; |
| 1093 | msg.body.finished.seq = seq; |
| 1094 | msg.body.finished.handled = handled; |
| 1095 | return mChannel->sendMessage(&msg); |
| 1096 | } |
| 1097 | |
| 1098 | bool InputConsumer::hasDeferredEvent() const { |
| 1099 | return mMsgDeferred; |
| 1100 | } |
| 1101 | |
| 1102 | bool InputConsumer::hasPendingBatch() const { |
| 1103 | return !mBatches.isEmpty(); |
| 1104 | } |
| 1105 | |
| 1106 | ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const { |
| 1107 | for (size_t i = 0; i < mBatches.size(); i++) { |
| 1108 | const Batch& batch = mBatches.itemAt(i); |
| 1109 | const InputMessage& head = batch.samples.itemAt(0); |
| 1110 | if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) { |
| 1111 | return i; |
| 1112 | } |
| 1113 | } |
| 1114 | return -1; |
| 1115 | } |
| 1116 | |
| 1117 | ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const { |
| 1118 | for (size_t i = 0; i < mTouchStates.size(); i++) { |
| 1119 | const TouchState& touchState = mTouchStates.itemAt(i); |
| 1120 | if (touchState.deviceId == deviceId && touchState.source == source) { |
| 1121 | return i; |
| 1122 | } |
| 1123 | } |
| 1124 | return -1; |
| 1125 | } |
| 1126 | |
| 1127 | void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) { |
| 1128 | event->initialize( |
| 1129 | msg->body.key.deviceId, |
| 1130 | msg->body.key.source, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1131 | msg->body.key.displayId, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1132 | msg->body.key.action, |
| 1133 | msg->body.key.flags, |
| 1134 | msg->body.key.keyCode, |
| 1135 | msg->body.key.scanCode, |
| 1136 | msg->body.key.metaState, |
| 1137 | msg->body.key.repeatCount, |
| 1138 | msg->body.key.downTime, |
| 1139 | msg->body.key.eventTime); |
| 1140 | } |
| 1141 | |
| 1142 | void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) { |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 1143 | uint32_t pointerCount = msg->body.motion.pointerCount; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1144 | PointerProperties pointerProperties[pointerCount]; |
| 1145 | PointerCoords pointerCoords[pointerCount]; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 1146 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1147 | pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties); |
| 1148 | pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords); |
| 1149 | } |
| 1150 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1151 | event->initialize(msg->body.motion.deviceId, msg->body.motion.source, |
| 1152 | msg->body.motion.displayId, msg->body.motion.action, |
| 1153 | msg->body.motion.actionButton, msg->body.motion.flags, |
| 1154 | msg->body.motion.edgeFlags, msg->body.motion.metaState, |
| 1155 | msg->body.motion.buttonState, msg->body.motion.classification, |
| 1156 | msg->body.motion.xOffset, msg->body.motion.yOffset, |
| 1157 | msg->body.motion.xPrecision, msg->body.motion.yPrecision, |
| 1158 | msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition, |
| 1159 | msg->body.motion.downTime, msg->body.motion.eventTime, pointerCount, |
| 1160 | pointerProperties, pointerCoords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) { |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 1164 | uint32_t pointerCount = msg->body.motion.pointerCount; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1165 | PointerCoords pointerCoords[pointerCount]; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 1166 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1167 | pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords); |
| 1168 | } |
| 1169 | |
| 1170 | event->setMetaState(event->getMetaState() | msg->body.motion.metaState); |
| 1171 | event->addSample(msg->body.motion.eventTime, pointerCoords); |
| 1172 | } |
| 1173 | |
| 1174 | bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) { |
| 1175 | const InputMessage& head = batch.samples.itemAt(0); |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 1176 | uint32_t pointerCount = msg->body.motion.pointerCount; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1177 | if (head.body.motion.pointerCount != pointerCount |
| 1178 | || head.body.motion.action != msg->body.motion.action) { |
| 1179 | return false; |
| 1180 | } |
| 1181 | for (size_t i = 0; i < pointerCount; i++) { |
| 1182 | if (head.body.motion.pointers[i].properties |
| 1183 | != msg->body.motion.pointers[i].properties) { |
| 1184 | return false; |
| 1185 | } |
| 1186 | } |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
| 1190 | ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) { |
| 1191 | size_t numSamples = batch.samples.size(); |
| 1192 | size_t index = 0; |
| 1193 | while (index < numSamples |
| 1194 | && batch.samples.itemAt(index).body.motion.eventTime <= time) { |
| 1195 | index += 1; |
| 1196 | } |
| 1197 | return ssize_t(index) - 1; |
| 1198 | } |
| 1199 | |
| 1200 | } // namespace android |