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 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 31 | #include <log/log.h> |
| 32 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 33 | #include <input/InputTransport.h> |
| 34 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | // Socket buffer size. The default is typically about 128KB, which is much larger than |
| 38 | // we really need. So we make it smaller. It just needs to be big enough to hold |
| 39 | // a few dozen large multi-finger motion events in the case where an application gets |
| 40 | // behind processing touches. |
| 41 | static const size_t SOCKET_BUFFER_SIZE = 32 * 1024; |
| 42 | |
| 43 | // Nanoseconds per milliseconds. |
| 44 | static const nsecs_t NANOS_PER_MS = 1000000; |
| 45 | |
| 46 | // Latency added during resampling. A few milliseconds doesn't hurt much but |
| 47 | // reduces the impact of mispredicted touch positions. |
| 48 | static const nsecs_t RESAMPLE_LATENCY = 5 * NANOS_PER_MS; |
| 49 | |
| 50 | // Minimum time difference between consecutive samples before attempting to resample. |
| 51 | static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS; |
| 52 | |
Andrew de los Reyes | de18f6c | 2015-10-01 15:57:25 -0700 | [diff] [blame] | 53 | // Maximum time difference between consecutive samples before attempting to resample |
| 54 | // by extrapolation. |
| 55 | static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS; |
| 56 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 57 | // Maximum time to predict forward from the last known state, to avoid predicting too |
| 58 | // far into the future. This time is further bounded by 50% of the last time delta. |
| 59 | static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS; |
| 60 | |
| 61 | template<typename T> |
| 62 | inline static T min(const T& a, const T& b) { |
| 63 | return a < b ? a : b; |
| 64 | } |
| 65 | |
| 66 | inline static float lerp(float a, float b, float alpha) { |
| 67 | return a + alpha * (b - a); |
| 68 | } |
| 69 | |
| 70 | // --- InputMessage --- |
| 71 | |
| 72 | bool InputMessage::isValid(size_t actualSize) const { |
| 73 | if (size() == actualSize) { |
| 74 | switch (header.type) { |
| 75 | case TYPE_KEY: |
| 76 | return true; |
| 77 | case TYPE_MOTION: |
| 78 | return body.motion.pointerCount > 0 |
| 79 | && body.motion.pointerCount <= MAX_POINTERS; |
| 80 | case TYPE_FINISHED: |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | size_t InputMessage::size() const { |
| 88 | switch (header.type) { |
| 89 | case TYPE_KEY: |
| 90 | return sizeof(Header) + body.key.size(); |
| 91 | case TYPE_MOTION: |
| 92 | return sizeof(Header) + body.motion.size(); |
| 93 | case TYPE_FINISHED: |
| 94 | return sizeof(Header) + body.finished.size(); |
| 95 | } |
| 96 | return sizeof(Header); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | // --- InputChannel --- |
| 101 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 102 | InputChannel::InputChannel(const std::string& name, int fd) : |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 103 | mName(name), mFd(fd) { |
| 104 | #if DEBUG_CHANNEL_LIFECYCLE |
| 105 | ALOGD("Input channel constructed: name='%s', fd=%d", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 106 | mName.c_str(), fd); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | int result = fcntl(mFd, F_SETFL, O_NONBLOCK); |
| 110 | LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make socket " |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 111 | "non-blocking. errno=%d", mName.c_str(), errno); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | InputChannel::~InputChannel() { |
| 115 | #if DEBUG_CHANNEL_LIFECYCLE |
| 116 | ALOGD("Input channel destroyed: name='%s', fd=%d", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 117 | mName.c_str(), mFd); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | ::close(mFd); |
| 121 | } |
| 122 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 123 | status_t InputChannel::openInputChannelPair(const std::string& name, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 124 | sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) { |
| 125 | int sockets[2]; |
| 126 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) { |
| 127 | status_t result = -errno; |
| 128 | ALOGE("channel '%s' ~ Could not create socket pair. errno=%d", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 129 | name.c_str(), errno); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 130 | outServerChannel.clear(); |
| 131 | outClientChannel.clear(); |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | int bufferSize = SOCKET_BUFFER_SIZE; |
| 136 | setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); |
| 137 | setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); |
| 138 | setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); |
| 139 | setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); |
| 140 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 141 | std::string serverChannelName = name; |
| 142 | serverChannelName += " (server)"; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 143 | outServerChannel = new InputChannel(serverChannelName, sockets[0]); |
| 144 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 145 | std::string clientChannelName = name; |
| 146 | clientChannelName += " (client)"; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 147 | outClientChannel = new InputChannel(clientChannelName, sockets[1]); |
| 148 | return OK; |
| 149 | } |
| 150 | |
| 151 | status_t InputChannel::sendMessage(const InputMessage* msg) { |
| 152 | size_t msgLength = msg->size(); |
| 153 | ssize_t nWrite; |
| 154 | do { |
| 155 | nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 156 | } while (nWrite == -1 && errno == EINTR); |
| 157 | |
| 158 | if (nWrite < 0) { |
| 159 | int error = errno; |
| 160 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 161 | 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] | 162 | msg->header.type, error); |
| 163 | #endif |
| 164 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 165 | return WOULD_BLOCK; |
| 166 | } |
| 167 | if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) { |
| 168 | return DEAD_OBJECT; |
| 169 | } |
| 170 | return -error; |
| 171 | } |
| 172 | |
| 173 | if (size_t(nWrite) != msgLength) { |
| 174 | #if DEBUG_CHANNEL_MESSAGES |
| 175 | ALOGD("channel '%s' ~ error sending message type %d, send was incomplete", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 176 | mName.c_str(), msg->header.type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 177 | #endif |
| 178 | return DEAD_OBJECT; |
| 179 | } |
| 180 | |
| 181 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 182 | 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] | 183 | #endif |
| 184 | return OK; |
| 185 | } |
| 186 | |
| 187 | status_t InputChannel::receiveMessage(InputMessage* msg) { |
| 188 | ssize_t nRead; |
| 189 | do { |
| 190 | nRead = ::recv(mFd, msg, sizeof(InputMessage), MSG_DONTWAIT); |
| 191 | } while (nRead == -1 && errno == EINTR); |
| 192 | |
| 193 | if (nRead < 0) { |
| 194 | int error = errno; |
| 195 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 196 | ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 197 | #endif |
| 198 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 199 | return WOULD_BLOCK; |
| 200 | } |
| 201 | if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) { |
| 202 | return DEAD_OBJECT; |
| 203 | } |
| 204 | return -error; |
| 205 | } |
| 206 | |
| 207 | if (nRead == 0) { // check for EOF |
| 208 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 209 | 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] | 210 | #endif |
| 211 | return DEAD_OBJECT; |
| 212 | } |
| 213 | |
| 214 | if (!msg->isValid(nRead)) { |
| 215 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 216 | ALOGD("channel '%s' ~ received invalid message", mName.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 217 | #endif |
| 218 | return BAD_VALUE; |
| 219 | } |
| 220 | |
| 221 | #if DEBUG_CHANNEL_MESSAGES |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 222 | 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] | 223 | #endif |
| 224 | return OK; |
| 225 | } |
| 226 | |
| 227 | sp<InputChannel> InputChannel::dup() const { |
| 228 | int fd = ::dup(getFd()); |
| 229 | return fd >= 0 ? new InputChannel(getName(), fd) : NULL; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | // --- InputPublisher --- |
| 234 | |
| 235 | InputPublisher::InputPublisher(const sp<InputChannel>& channel) : |
| 236 | mChannel(channel) { |
| 237 | } |
| 238 | |
| 239 | InputPublisher::~InputPublisher() { |
| 240 | } |
| 241 | |
| 242 | status_t InputPublisher::publishKeyEvent( |
| 243 | uint32_t seq, |
| 244 | int32_t deviceId, |
| 245 | int32_t source, |
| 246 | int32_t action, |
| 247 | int32_t flags, |
| 248 | int32_t keyCode, |
| 249 | int32_t scanCode, |
| 250 | int32_t metaState, |
| 251 | int32_t repeatCount, |
| 252 | nsecs_t downTime, |
| 253 | nsecs_t eventTime) { |
| 254 | #if DEBUG_TRANSPORT_ACTIONS |
| 255 | ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, " |
| 256 | "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] | 257 | "downTime=%" PRId64 ", eventTime=%" PRId64, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 258 | mChannel->getName().c_str(), seq, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 259 | deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, |
| 260 | downTime, eventTime); |
| 261 | #endif |
| 262 | |
| 263 | if (!seq) { |
| 264 | ALOGE("Attempted to publish a key event with sequence number 0."); |
| 265 | return BAD_VALUE; |
| 266 | } |
| 267 | |
| 268 | InputMessage msg; |
| 269 | msg.header.type = InputMessage::TYPE_KEY; |
| 270 | msg.body.key.seq = seq; |
| 271 | msg.body.key.deviceId = deviceId; |
| 272 | msg.body.key.source = source; |
| 273 | msg.body.key.action = action; |
| 274 | msg.body.key.flags = flags; |
| 275 | msg.body.key.keyCode = keyCode; |
| 276 | msg.body.key.scanCode = scanCode; |
| 277 | msg.body.key.metaState = metaState; |
| 278 | msg.body.key.repeatCount = repeatCount; |
| 279 | msg.body.key.downTime = downTime; |
| 280 | msg.body.key.eventTime = eventTime; |
| 281 | return mChannel->sendMessage(&msg); |
| 282 | } |
| 283 | |
| 284 | status_t InputPublisher::publishMotionEvent( |
| 285 | uint32_t seq, |
| 286 | int32_t deviceId, |
| 287 | int32_t source, |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 288 | int32_t displayId, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 289 | int32_t action, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 290 | int32_t actionButton, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 291 | int32_t flags, |
| 292 | int32_t edgeFlags, |
| 293 | int32_t metaState, |
| 294 | int32_t buttonState, |
| 295 | float xOffset, |
| 296 | float yOffset, |
| 297 | float xPrecision, |
| 298 | float yPrecision, |
| 299 | nsecs_t downTime, |
| 300 | nsecs_t eventTime, |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 301 | uint32_t pointerCount, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 302 | const PointerProperties* pointerProperties, |
| 303 | const PointerCoords* pointerCoords) { |
| 304 | #if DEBUG_TRANSPORT_ACTIONS |
| 305 | ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, " |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 306 | "displayId=%" PRId32 ", " |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 307 | "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, " |
| 308 | "metaState=0x%x, buttonState=0x%x, xOffset=%f, yOffset=%f, " |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 309 | "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", " |
Michael Wright | 63ff3a8 | 2014-06-10 13:03:17 -0700 | [diff] [blame] | 310 | "pointerCount=%" PRIu32, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 311 | mChannel->getName().c_str(), seq, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 312 | deviceId, source, displayId, action, actionButton, flags, edgeFlags, metaState, |
| 313 | buttonState, xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, |
| 314 | pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 315 | #endif |
| 316 | |
| 317 | if (!seq) { |
| 318 | ALOGE("Attempted to publish a motion event with sequence number 0."); |
| 319 | return BAD_VALUE; |
| 320 | } |
| 321 | |
| 322 | if (pointerCount > MAX_POINTERS || pointerCount < 1) { |
Michael Wright | 63ff3a8 | 2014-06-10 13:03:17 -0700 | [diff] [blame] | 323 | ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 324 | mChannel->getName().c_str(), pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 325 | return BAD_VALUE; |
| 326 | } |
| 327 | |
| 328 | InputMessage msg; |
| 329 | msg.header.type = InputMessage::TYPE_MOTION; |
| 330 | msg.body.motion.seq = seq; |
| 331 | msg.body.motion.deviceId = deviceId; |
| 332 | msg.body.motion.source = source; |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 333 | msg.body.motion.displayId = displayId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 334 | msg.body.motion.action = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 335 | msg.body.motion.actionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 336 | msg.body.motion.flags = flags; |
| 337 | msg.body.motion.edgeFlags = edgeFlags; |
| 338 | msg.body.motion.metaState = metaState; |
| 339 | msg.body.motion.buttonState = buttonState; |
| 340 | msg.body.motion.xOffset = xOffset; |
| 341 | msg.body.motion.yOffset = yOffset; |
| 342 | msg.body.motion.xPrecision = xPrecision; |
| 343 | msg.body.motion.yPrecision = yPrecision; |
| 344 | msg.body.motion.downTime = downTime; |
| 345 | msg.body.motion.eventTime = eventTime; |
| 346 | msg.body.motion.pointerCount = pointerCount; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 347 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 348 | msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]); |
| 349 | msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]); |
| 350 | } |
| 351 | return mChannel->sendMessage(&msg); |
| 352 | } |
| 353 | |
| 354 | status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) { |
| 355 | #if DEBUG_TRANSPORT_ACTIONS |
| 356 | ALOGD("channel '%s' publisher ~ receiveFinishedSignal", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 357 | mChannel->getName().c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 358 | #endif |
| 359 | |
| 360 | InputMessage msg; |
| 361 | status_t result = mChannel->receiveMessage(&msg); |
| 362 | if (result) { |
| 363 | *outSeq = 0; |
| 364 | *outHandled = false; |
| 365 | return result; |
| 366 | } |
| 367 | if (msg.header.type != InputMessage::TYPE_FINISHED) { |
| 368 | ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 369 | mChannel->getName().c_str(), msg.header.type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 370 | return UNKNOWN_ERROR; |
| 371 | } |
| 372 | *outSeq = msg.body.finished.seq; |
| 373 | *outHandled = msg.body.finished.handled; |
| 374 | return OK; |
| 375 | } |
| 376 | |
| 377 | // --- InputConsumer --- |
| 378 | |
| 379 | InputConsumer::InputConsumer(const sp<InputChannel>& channel) : |
| 380 | mResampleTouch(isTouchResamplingEnabled()), |
| 381 | mChannel(channel), mMsgDeferred(false) { |
| 382 | } |
| 383 | |
| 384 | InputConsumer::~InputConsumer() { |
| 385 | } |
| 386 | |
| 387 | bool InputConsumer::isTouchResamplingEnabled() { |
| 388 | char value[PROPERTY_VALUE_MAX]; |
Michael Wright | ad526f8 | 2013-10-10 18:54:12 -0700 | [diff] [blame] | 389 | int length = property_get("ro.input.noresample", value, NULL); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 390 | if (length > 0) { |
Michael Wright | ad526f8 | 2013-10-10 18:54:12 -0700 | [diff] [blame] | 391 | if (!strcmp("1", value)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 392 | return false; |
| 393 | } |
Michael Wright | ad526f8 | 2013-10-10 18:54:12 -0700 | [diff] [blame] | 394 | if (strcmp("0", value)) { |
| 395 | ALOGD("Unrecognized property value for 'ro.input.noresample'. " |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 396 | "Use '1' or '0'."); |
| 397 | } |
| 398 | } |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | status_t InputConsumer::consume(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 403 | bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 404 | #if DEBUG_TRANSPORT_ACTIONS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 405 | ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 406 | mChannel->getName().c_str(), consumeBatches ? "true" : "false", frameTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 407 | #endif |
| 408 | |
| 409 | *outSeq = 0; |
| 410 | *outEvent = NULL; |
| 411 | |
| 412 | // Fetch the next input message. |
| 413 | // Loop until an event can be returned or no additional events are received. |
| 414 | while (!*outEvent) { |
| 415 | if (mMsgDeferred) { |
| 416 | // mMsg contains a valid input message from the previous call to consume |
| 417 | // that has not yet been processed. |
| 418 | mMsgDeferred = false; |
| 419 | } else { |
| 420 | // Receive a fresh message. |
| 421 | status_t result = mChannel->receiveMessage(&mMsg); |
| 422 | if (result) { |
| 423 | // Consume the next batched event unless batches are being held for later. |
| 424 | if (consumeBatches || result != WOULD_BLOCK) { |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 425 | result = consumeBatch(factory, frameTime, outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 426 | if (*outEvent) { |
| 427 | #if DEBUG_TRANSPORT_ACTIONS |
| 428 | ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 429 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 430 | #endif |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | return result; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | switch (mMsg.header.type) { |
| 439 | case InputMessage::TYPE_KEY: { |
| 440 | KeyEvent* keyEvent = factory->createKeyEvent(); |
| 441 | if (!keyEvent) return NO_MEMORY; |
| 442 | |
| 443 | initializeKeyEvent(keyEvent, &mMsg); |
| 444 | *outSeq = mMsg.body.key.seq; |
| 445 | *outEvent = keyEvent; |
| 446 | #if DEBUG_TRANSPORT_ACTIONS |
| 447 | ALOGD("channel '%s' consumer ~ consumed key event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 448 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 449 | #endif |
| 450 | break; |
| 451 | } |
| 452 | |
gaoshang | e3e11a7 | 2017-08-08 16:11:31 +0800 | [diff] [blame] | 453 | case InputMessage::TYPE_MOTION: { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 454 | ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source); |
| 455 | if (batchIndex >= 0) { |
| 456 | Batch& batch = mBatches.editItemAt(batchIndex); |
| 457 | if (canAddSample(batch, &mMsg)) { |
| 458 | batch.samples.push(mMsg); |
| 459 | #if DEBUG_TRANSPORT_ACTIONS |
| 460 | ALOGD("channel '%s' consumer ~ appended to batch event", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 461 | mChannel->getName().c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 462 | #endif |
| 463 | break; |
| 464 | } else { |
| 465 | // We cannot append to the batch in progress, so we need to consume |
| 466 | // the previous batch right now and defer the new message until later. |
| 467 | mMsgDeferred = true; |
| 468 | status_t result = consumeSamples(factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 469 | batch, batch.samples.size(), outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 470 | mBatches.removeAt(batchIndex); |
| 471 | if (result) { |
| 472 | return result; |
| 473 | } |
| 474 | #if DEBUG_TRANSPORT_ACTIONS |
| 475 | ALOGD("channel '%s' consumer ~ consumed batch event and " |
| 476 | "deferred current event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 477 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 478 | #endif |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // Start a new batch if needed. |
| 484 | if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE |
| 485 | || mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 486 | mBatches.push(); |
| 487 | Batch& batch = mBatches.editTop(); |
| 488 | batch.samples.push(mMsg); |
| 489 | #if DEBUG_TRANSPORT_ACTIONS |
| 490 | ALOGD("channel '%s' consumer ~ started batch event", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 491 | mChannel->getName().c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 492 | #endif |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 497 | if (! motionEvent) return NO_MEMORY; |
| 498 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 499 | updateTouchState(mMsg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 500 | initializeMotionEvent(motionEvent, &mMsg); |
| 501 | *outSeq = mMsg.body.motion.seq; |
| 502 | *outEvent = motionEvent; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 503 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 504 | #if DEBUG_TRANSPORT_ACTIONS |
| 505 | ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 506 | mChannel->getName().c_str(), *outSeq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 507 | #endif |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | default: |
| 512 | ALOGE("channel '%s' consumer ~ Received unexpected message of type %d", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 513 | mChannel->getName().c_str(), mMsg.header.type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 514 | return UNKNOWN_ERROR; |
| 515 | } |
| 516 | } |
| 517 | return OK; |
| 518 | } |
| 519 | |
| 520 | status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 521 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 522 | status_t result; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 523 | for (size_t i = mBatches.size(); i > 0; ) { |
| 524 | i--; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 525 | Batch& batch = mBatches.editItemAt(i); |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 526 | if (frameTime < 0) { |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 527 | result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 528 | mBatches.removeAt(i); |
| 529 | return result; |
| 530 | } |
| 531 | |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 532 | nsecs_t sampleTime = frameTime; |
| 533 | if (mResampleTouch) { |
| 534 | sampleTime -= RESAMPLE_LATENCY; |
| 535 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 536 | ssize_t split = findSampleNoLaterThan(batch, sampleTime); |
| 537 | if (split < 0) { |
| 538 | continue; |
| 539 | } |
| 540 | |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 541 | result = consumeSamples(factory, batch, split + 1, outSeq, outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 542 | const InputMessage* next; |
| 543 | if (batch.samples.isEmpty()) { |
| 544 | mBatches.removeAt(i); |
| 545 | next = NULL; |
| 546 | } else { |
| 547 | next = &batch.samples.itemAt(0); |
| 548 | } |
Michael Wright | 3223217 | 2013-10-21 12:05:22 -0700 | [diff] [blame] | 549 | if (!result && mResampleTouch) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 550 | resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next); |
| 551 | } |
| 552 | return result; |
| 553 | } |
| 554 | |
| 555 | return WOULD_BLOCK; |
| 556 | } |
| 557 | |
| 558 | status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 559 | Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 560 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 561 | if (! motionEvent) return NO_MEMORY; |
| 562 | |
| 563 | uint32_t chain = 0; |
| 564 | for (size_t i = 0; i < count; i++) { |
| 565 | InputMessage& msg = batch.samples.editItemAt(i); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 566 | updateTouchState(msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 567 | if (i) { |
| 568 | SeqChain seqChain; |
| 569 | seqChain.seq = msg.body.motion.seq; |
| 570 | seqChain.chain = chain; |
| 571 | mSeqChains.push(seqChain); |
| 572 | addSample(motionEvent, &msg); |
| 573 | } else { |
| 574 | initializeMotionEvent(motionEvent, &msg); |
| 575 | } |
| 576 | chain = msg.body.motion.seq; |
| 577 | } |
| 578 | batch.samples.removeItemsAt(0, count); |
| 579 | |
| 580 | *outSeq = chain; |
| 581 | *outEvent = motionEvent; |
| 582 | return OK; |
| 583 | } |
| 584 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 585 | void InputConsumer::updateTouchState(InputMessage& msg) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 586 | if (!mResampleTouch || |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 587 | !(msg.body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 591 | int32_t deviceId = msg.body.motion.deviceId; |
| 592 | int32_t source = msg.body.motion.source; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 593 | |
| 594 | // Update the touch state history to incorporate the new input message. |
| 595 | // If the message is in the past relative to the most recently produced resampled |
| 596 | // touch, then use the resampled time and coordinates instead. |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 597 | switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 598 | case AMOTION_EVENT_ACTION_DOWN: { |
| 599 | ssize_t index = findTouchState(deviceId, source); |
| 600 | if (index < 0) { |
| 601 | mTouchStates.push(); |
| 602 | index = mTouchStates.size() - 1; |
| 603 | } |
| 604 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 605 | touchState.initialize(deviceId, source); |
| 606 | touchState.addHistory(msg); |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | case AMOTION_EVENT_ACTION_MOVE: { |
| 611 | ssize_t index = findTouchState(deviceId, source); |
| 612 | if (index >= 0) { |
| 613 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 614 | touchState.addHistory(msg); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 615 | rewriteMessage(touchState, msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 616 | } |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | case AMOTION_EVENT_ACTION_POINTER_DOWN: { |
| 621 | ssize_t index = findTouchState(deviceId, source); |
| 622 | if (index >= 0) { |
| 623 | TouchState& touchState = mTouchStates.editItemAt(index); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 624 | touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 625 | rewriteMessage(touchState, msg); |
| 626 | } |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 631 | ssize_t index = findTouchState(deviceId, source); |
| 632 | if (index >= 0) { |
| 633 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 634 | rewriteMessage(touchState, msg); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 635 | touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 636 | } |
| 637 | break; |
| 638 | } |
| 639 | |
| 640 | case AMOTION_EVENT_ACTION_SCROLL: { |
| 641 | ssize_t index = findTouchState(deviceId, source); |
| 642 | if (index >= 0) { |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 643 | TouchState& touchState = mTouchStates.editItemAt(index); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 644 | rewriteMessage(touchState, msg); |
| 645 | } |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | case AMOTION_EVENT_ACTION_UP: |
| 650 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 651 | ssize_t index = findTouchState(deviceId, source); |
| 652 | if (index >= 0) { |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 653 | TouchState& touchState = mTouchStates.editItemAt(index); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 654 | rewriteMessage(touchState, msg); |
| 655 | mTouchStates.removeAt(index); |
| 656 | } |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 662 | /** |
| 663 | * Replace the coordinates in msg with the coordinates in lastResample, if necessary. |
| 664 | * |
| 665 | * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time |
| 666 | * is in the past relative to msg and the past two events do not contain identical coordinates), |
| 667 | * then invalidate the lastResample data for that pointer. |
| 668 | * If the two past events have identical coordinates, then lastResample data for that pointer will |
| 669 | * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is |
| 670 | * resampled to the new value x1, then x1 will always be used to replace x0 until some new value |
| 671 | * not equal to x0 is received. |
| 672 | */ |
| 673 | void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) { |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 674 | nsecs_t eventTime = msg.body.motion.eventTime; |
| 675 | for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) { |
| 676 | uint32_t id = msg.body.motion.pointers[i].properties.id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 677 | if (state.lastResample.idBits.hasBit(id)) { |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 678 | if (eventTime < state.lastResample.eventTime || |
| 679 | state.recentCoordinatesAreIdentical(id)) { |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 680 | PointerCoords& msgCoords = msg.body.motion.pointers[i].coords; |
| 681 | const PointerCoords& resampleCoords = state.lastResample.getPointerById(id); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 682 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 683 | ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id, |
| 684 | resampleCoords.getX(), resampleCoords.getY(), |
| 685 | msgCoords.getX(), msgCoords.getY()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 686 | #endif |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 687 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX()); |
| 688 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY()); |
| 689 | } else { |
| 690 | state.lastResample.idBits.clearBit(id); |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 691 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event, |
| 697 | const InputMessage* next) { |
| 698 | if (!mResampleTouch |
| 699 | || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER) |
| 700 | || event->getAction() != AMOTION_EVENT_ACTION_MOVE) { |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | ssize_t index = findTouchState(event->getDeviceId(), event->getSource()); |
| 705 | if (index < 0) { |
| 706 | #if DEBUG_RESAMPLING |
| 707 | ALOGD("Not resampled, no touch state for device."); |
| 708 | #endif |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 713 | if (touchState.historySize < 1) { |
| 714 | #if DEBUG_RESAMPLING |
| 715 | ALOGD("Not resampled, no history for device."); |
| 716 | #endif |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | // Ensure that the current sample has all of the pointers that need to be reported. |
| 721 | const History* current = touchState.getHistory(0); |
| 722 | size_t pointerCount = event->getPointerCount(); |
| 723 | for (size_t i = 0; i < pointerCount; i++) { |
| 724 | uint32_t id = event->getPointerId(i); |
| 725 | if (!current->idBits.hasBit(id)) { |
| 726 | #if DEBUG_RESAMPLING |
| 727 | ALOGD("Not resampled, missing id %d", id); |
| 728 | #endif |
| 729 | return; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // Find the data to use for resampling. |
| 734 | const History* other; |
| 735 | History future; |
| 736 | float alpha; |
| 737 | if (next) { |
| 738 | // Interpolate between current sample and future sample. |
| 739 | // So current->eventTime <= sampleTime <= future.eventTime. |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 740 | future.initializeFrom(*next); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 741 | other = &future; |
| 742 | nsecs_t delta = future.eventTime - current->eventTime; |
| 743 | if (delta < RESAMPLE_MIN_DELTA) { |
| 744 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 745 | ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 746 | #endif |
| 747 | return; |
| 748 | } |
| 749 | alpha = float(sampleTime - current->eventTime) / delta; |
| 750 | } else if (touchState.historySize >= 2) { |
| 751 | // Extrapolate future sample using current sample and past sample. |
| 752 | // So other->eventTime <= current->eventTime <= sampleTime. |
| 753 | other = touchState.getHistory(1); |
| 754 | nsecs_t delta = current->eventTime - other->eventTime; |
| 755 | if (delta < RESAMPLE_MIN_DELTA) { |
| 756 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 757 | 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] | 758 | #endif |
| 759 | return; |
| 760 | } else if (delta > RESAMPLE_MAX_DELTA) { |
| 761 | #if DEBUG_RESAMPLING |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 762 | ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 763 | #endif |
| 764 | return; |
| 765 | } |
| 766 | nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION); |
| 767 | if (sampleTime > maxPredict) { |
| 768 | #if DEBUG_RESAMPLING |
| 769 | ALOGD("Sample time is too far in the future, adjusting prediction " |
Siarhei Vishniakou | 0aeec07 | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 770 | "from %" PRId64 " to %" PRId64 " ns.", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 771 | sampleTime - current->eventTime, maxPredict - current->eventTime); |
| 772 | #endif |
| 773 | sampleTime = maxPredict; |
| 774 | } |
| 775 | alpha = float(current->eventTime - sampleTime) / delta; |
| 776 | } else { |
| 777 | #if DEBUG_RESAMPLING |
| 778 | ALOGD("Not resampled, insufficient data."); |
| 779 | #endif |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | // Resample touch coordinates. |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 784 | History oldLastResample; |
| 785 | oldLastResample.initializeFrom(touchState.lastResample); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 786 | touchState.lastResample.eventTime = sampleTime; |
| 787 | touchState.lastResample.idBits.clear(); |
| 788 | for (size_t i = 0; i < pointerCount; i++) { |
| 789 | uint32_t id = event->getPointerId(i); |
| 790 | touchState.lastResample.idToIndex[id] = i; |
| 791 | touchState.lastResample.idBits.markBit(id); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 792 | if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) { |
| 793 | // We maintain the previously resampled value for this pointer (stored in |
| 794 | // oldLastResample) when the coordinates for this pointer haven't changed since then. |
| 795 | // This way we don't introduce artificial jitter when pointers haven't actually moved. |
| 796 | |
| 797 | // We know here that the coordinates for the pointer haven't changed because we |
| 798 | // would've cleared the resampled bit in rewriteMessage if they had. We can't modify |
| 799 | // lastResample in place becasue the mapping from pointer ID to index may have changed. |
| 800 | touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id)); |
| 801 | continue; |
| 802 | } |
| 803 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 804 | PointerCoords& resampledCoords = touchState.lastResample.pointers[i]; |
| 805 | const PointerCoords& currentCoords = current->getPointerById(id); |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 806 | resampledCoords.copyFrom(currentCoords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 807 | if (other->idBits.hasBit(id) |
| 808 | && shouldResampleTool(event->getToolType(i))) { |
| 809 | const PointerCoords& otherCoords = other->getPointerById(id); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 810 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X, |
| 811 | lerp(currentCoords.getX(), otherCoords.getX(), alpha)); |
| 812 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 813 | lerp(currentCoords.getY(), otherCoords.getY(), alpha)); |
| 814 | #if DEBUG_RESAMPLING |
| 815 | ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), " |
| 816 | "other (%0.3f, %0.3f), alpha %0.3f", |
| 817 | id, resampledCoords.getX(), resampledCoords.getY(), |
| 818 | currentCoords.getX(), currentCoords.getY(), |
| 819 | otherCoords.getX(), otherCoords.getY(), |
| 820 | alpha); |
| 821 | #endif |
| 822 | } else { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 823 | #if DEBUG_RESAMPLING |
| 824 | ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", |
| 825 | id, resampledCoords.getX(), resampledCoords.getY(), |
| 826 | currentCoords.getX(), currentCoords.getY()); |
| 827 | #endif |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | event->addSample(sampleTime, touchState.lastResample.pointers); |
| 832 | } |
| 833 | |
| 834 | bool InputConsumer::shouldResampleTool(int32_t toolType) { |
| 835 | return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 836 | || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 837 | } |
| 838 | |
| 839 | status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) { |
| 840 | #if DEBUG_TRANSPORT_ACTIONS |
| 841 | ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 842 | mChannel->getName().c_str(), seq, handled ? "true" : "false"); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 843 | #endif |
| 844 | |
| 845 | if (!seq) { |
| 846 | ALOGE("Attempted to send a finished signal with sequence number 0."); |
| 847 | return BAD_VALUE; |
| 848 | } |
| 849 | |
| 850 | // Send finished signals for the batch sequence chain first. |
| 851 | size_t seqChainCount = mSeqChains.size(); |
| 852 | if (seqChainCount) { |
| 853 | uint32_t currentSeq = seq; |
| 854 | uint32_t chainSeqs[seqChainCount]; |
| 855 | size_t chainIndex = 0; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 856 | for (size_t i = seqChainCount; i > 0; ) { |
| 857 | i--; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 858 | const SeqChain& seqChain = mSeqChains.itemAt(i); |
| 859 | if (seqChain.seq == currentSeq) { |
| 860 | currentSeq = seqChain.chain; |
| 861 | chainSeqs[chainIndex++] = currentSeq; |
| 862 | mSeqChains.removeAt(i); |
| 863 | } |
| 864 | } |
| 865 | status_t status = OK; |
Dan Austin | 1faef80 | 2015-09-22 14:28:07 -0700 | [diff] [blame] | 866 | while (!status && chainIndex > 0) { |
| 867 | chainIndex--; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 868 | status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled); |
| 869 | } |
| 870 | if (status) { |
| 871 | // 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] | 872 | for (;;) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 873 | SeqChain seqChain; |
| 874 | seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq; |
| 875 | seqChain.chain = chainSeqs[chainIndex]; |
| 876 | mSeqChains.push(seqChain); |
gaoshang | 9090d4f | 2017-05-17 14:36:46 +0800 | [diff] [blame] | 877 | if (!chainIndex) break; |
| 878 | chainIndex--; |
| 879 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 880 | return status; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | // Send finished signal for the last message in the batch. |
| 885 | return sendUnchainedFinishedSignal(seq, handled); |
| 886 | } |
| 887 | |
| 888 | status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) { |
| 889 | InputMessage msg; |
| 890 | msg.header.type = InputMessage::TYPE_FINISHED; |
| 891 | msg.body.finished.seq = seq; |
| 892 | msg.body.finished.handled = handled; |
| 893 | return mChannel->sendMessage(&msg); |
| 894 | } |
| 895 | |
| 896 | bool InputConsumer::hasDeferredEvent() const { |
| 897 | return mMsgDeferred; |
| 898 | } |
| 899 | |
| 900 | bool InputConsumer::hasPendingBatch() const { |
| 901 | return !mBatches.isEmpty(); |
| 902 | } |
| 903 | |
| 904 | ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const { |
| 905 | for (size_t i = 0; i < mBatches.size(); i++) { |
| 906 | const Batch& batch = mBatches.itemAt(i); |
| 907 | const InputMessage& head = batch.samples.itemAt(0); |
| 908 | if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) { |
| 909 | return i; |
| 910 | } |
| 911 | } |
| 912 | return -1; |
| 913 | } |
| 914 | |
| 915 | ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const { |
| 916 | for (size_t i = 0; i < mTouchStates.size(); i++) { |
| 917 | const TouchState& touchState = mTouchStates.itemAt(i); |
| 918 | if (touchState.deviceId == deviceId && touchState.source == source) { |
| 919 | return i; |
| 920 | } |
| 921 | } |
| 922 | return -1; |
| 923 | } |
| 924 | |
| 925 | void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) { |
| 926 | event->initialize( |
| 927 | msg->body.key.deviceId, |
| 928 | msg->body.key.source, |
| 929 | msg->body.key.action, |
| 930 | msg->body.key.flags, |
| 931 | msg->body.key.keyCode, |
| 932 | msg->body.key.scanCode, |
| 933 | msg->body.key.metaState, |
| 934 | msg->body.key.repeatCount, |
| 935 | msg->body.key.downTime, |
| 936 | msg->body.key.eventTime); |
| 937 | } |
| 938 | |
| 939 | void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) { |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 940 | uint32_t pointerCount = msg->body.motion.pointerCount; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 941 | PointerProperties pointerProperties[pointerCount]; |
| 942 | PointerCoords pointerCoords[pointerCount]; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 943 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 944 | pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties); |
| 945 | pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords); |
| 946 | } |
| 947 | |
| 948 | event->initialize( |
| 949 | msg->body.motion.deviceId, |
| 950 | msg->body.motion.source, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame^] | 951 | msg->body.motion.displayId, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 952 | msg->body.motion.action, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 953 | msg->body.motion.actionButton, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 954 | msg->body.motion.flags, |
| 955 | msg->body.motion.edgeFlags, |
| 956 | msg->body.motion.metaState, |
| 957 | msg->body.motion.buttonState, |
| 958 | msg->body.motion.xOffset, |
| 959 | msg->body.motion.yOffset, |
| 960 | msg->body.motion.xPrecision, |
| 961 | msg->body.motion.yPrecision, |
| 962 | msg->body.motion.downTime, |
| 963 | msg->body.motion.eventTime, |
| 964 | pointerCount, |
| 965 | pointerProperties, |
| 966 | pointerCoords); |
| 967 | } |
| 968 | |
| 969 | void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) { |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 970 | uint32_t pointerCount = msg->body.motion.pointerCount; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 971 | PointerCoords pointerCoords[pointerCount]; |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 972 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 973 | pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords); |
| 974 | } |
| 975 | |
| 976 | event->setMetaState(event->getMetaState() | msg->body.motion.metaState); |
| 977 | event->addSample(msg->body.motion.eventTime, pointerCoords); |
| 978 | } |
| 979 | |
| 980 | bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) { |
| 981 | const InputMessage& head = batch.samples.itemAt(0); |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 982 | uint32_t pointerCount = msg->body.motion.pointerCount; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 983 | if (head.body.motion.pointerCount != pointerCount |
| 984 | || head.body.motion.action != msg->body.motion.action) { |
| 985 | return false; |
| 986 | } |
| 987 | for (size_t i = 0; i < pointerCount; i++) { |
| 988 | if (head.body.motion.pointers[i].properties |
| 989 | != msg->body.motion.pointers[i].properties) { |
| 990 | return false; |
| 991 | } |
| 992 | } |
| 993 | return true; |
| 994 | } |
| 995 | |
| 996 | ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) { |
| 997 | size_t numSamples = batch.samples.size(); |
| 998 | size_t index = 0; |
| 999 | while (index < numSamples |
| 1000 | && batch.samples.itemAt(index).body.motion.eventTime <= time) { |
| 1001 | index += 1; |
| 1002 | } |
| 1003 | return ssize_t(index) - 1; |
| 1004 | } |
| 1005 | |
| 1006 | } // namespace android |