blob: ea8b9a7ec8edbaf7cb973fc088ac72c6e082da9a [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// Provides a shared memory transport for input events.
5//
6#define LOG_TAG "InputTransport"
7
8//#define LOG_NDEBUG 0
9
10// Log debug messages about channel messages (send message, receive message)
11#define DEBUG_CHANNEL_MESSAGES 0
12
13// Log debug messages whenever InputChannel objects are created/destroyed
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070014static constexpr bool DEBUG_CHANNEL_LIFECYCLE = false;
Jeff Brown5912f952013-07-01 19:10:31 -070015
16// Log debug messages about transport actions
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -080017static constexpr bool DEBUG_TRANSPORT_ACTIONS = false;
Jeff Brown5912f952013-07-01 19:10:31 -070018
19// Log debug messages about touch event resampling
20#define DEBUG_RESAMPLING 0
21
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <errno.h>
23#include <fcntl.h>
Michael Wrightd0a4a622014-06-09 19:03:32 -070024#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <math.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <sys/socket.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027#include <sys/types.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <unistd.h>
29
Michael Wright3dd60e22019-03-27 22:06:44 +000030#include <android-base/stringprintf.h>
31#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070033#include <log/log.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000034#include <utils/Trace.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070035
chaviw3277faf2021-05-19 16:45:23 -050036#include <ftl/NamedEnum.h>
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <input/InputTransport.h>
38
Michael Wright3dd60e22019-03-27 22:06:44 +000039using android::base::StringPrintf;
40
Jeff Brown5912f952013-07-01 19:10:31 -070041namespace 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.
47static const size_t SOCKET_BUFFER_SIZE = 32 * 1024;
48
49// Nanoseconds per milliseconds.
50static 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.
54static const nsecs_t RESAMPLE_LATENCY = 5 * NANOS_PER_MS;
55
56// Minimum time difference between consecutive samples before attempting to resample.
57static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
58
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -070059// Maximum time difference between consecutive samples before attempting to resample
60// by extrapolation.
61static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS;
62
Jeff Brown5912f952013-07-01 19:10:31 -070063// 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.
65static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
66
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -060067/**
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 */
77static const char* PROPERTY_RESAMPLING_ENABLED = "ro.input.resampling";
78
Jeff Brown5912f952013-07-01 19:10:31 -070079template<typename T>
80inline static T min(const T& a, const T& b) {
81 return a < b ? a : b;
82}
83
84inline static float lerp(float a, float b, float alpha) {
85 return a + alpha * (b - a);
86}
87
Siarhei Vishniakou128eab12019-05-23 10:25:59 +080088inline static bool isPointerEvent(int32_t source) {
89 return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER;
90}
91
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -080092inline static const char* toString(bool value) {
93 return value ? "true" : "false";
94}
95
Jeff Brown5912f952013-07-01 19:10:31 -070096// --- InputMessage ---
97
98bool InputMessage::isValid(size_t actualSize) const {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +000099 if (size() != actualSize) {
100 ALOGE("Received message of incorrect size %zu (expected %zu)", actualSize, size());
101 return false;
102 }
103
104 switch (header.type) {
105 case Type::KEY:
106 return true;
107 case Type::MOTION: {
108 const bool valid =
109 body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
110 if (!valid) {
111 ALOGE("Received invalid MOTION: pointerCount = %" PRIu32, body.motion.pointerCount);
112 }
113 return valid;
114 }
115 case Type::FINISHED:
116 case Type::FOCUS:
117 case Type::CAPTURE:
118 case Type::DRAG:
119 return true;
120 case Type::TIMELINE: {
121 const nsecs_t gpuCompletedTime =
122 body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
123 const nsecs_t presentTime =
124 body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
125 const bool valid = presentTime > gpuCompletedTime;
126 if (!valid) {
127 ALOGE("Received invalid TIMELINE: gpuCompletedTime = %" PRId64
128 " presentTime = %" PRId64,
129 gpuCompletedTime, presentTime);
130 }
131 return valid;
Jeff Brown5912f952013-07-01 19:10:31 -0700132 }
133 }
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000134 ALOGE("Invalid message type: %" PRIu32, header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700135 return false;
136}
137
138size_t InputMessage::size() const {
139 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700140 case Type::KEY:
141 return sizeof(Header) + body.key.size();
142 case Type::MOTION:
143 return sizeof(Header) + body.motion.size();
144 case Type::FINISHED:
145 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800146 case Type::FOCUS:
147 return sizeof(Header) + body.focus.size();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800148 case Type::CAPTURE:
149 return sizeof(Header) + body.capture.size();
arthurhung7632c332020-12-30 16:58:01 +0800150 case Type::DRAG:
151 return sizeof(Header) + body.drag.size();
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000152 case Type::TIMELINE:
153 return sizeof(Header) + body.timeline.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700154 }
155 return sizeof(Header);
156}
157
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800158/**
159 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
160 * memory to zero, then only copy the valid bytes on a per-field basis.
161 */
162void InputMessage::getSanitizedCopy(InputMessage* msg) const {
163 memset(msg, 0, sizeof(*msg));
164
165 // Write the header
166 msg->header.type = header.type;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500167 msg->header.seq = header.seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800168
169 // Write the body
170 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700171 case InputMessage::Type::KEY: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800172 // int32_t eventId
173 msg->body.key.eventId = body.key.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800174 // nsecs_t eventTime
175 msg->body.key.eventTime = body.key.eventTime;
176 // int32_t deviceId
177 msg->body.key.deviceId = body.key.deviceId;
178 // int32_t source
179 msg->body.key.source = body.key.source;
180 // int32_t displayId
181 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600182 // std::array<uint8_t, 32> hmac
183 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800184 // int32_t action
185 msg->body.key.action = body.key.action;
186 // int32_t flags
187 msg->body.key.flags = body.key.flags;
188 // int32_t keyCode
189 msg->body.key.keyCode = body.key.keyCode;
190 // int32_t scanCode
191 msg->body.key.scanCode = body.key.scanCode;
192 // int32_t metaState
193 msg->body.key.metaState = body.key.metaState;
194 // int32_t repeatCount
195 msg->body.key.repeatCount = body.key.repeatCount;
196 // nsecs_t downTime
197 msg->body.key.downTime = body.key.downTime;
198 break;
199 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700200 case InputMessage::Type::MOTION: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800201 // int32_t eventId
202 msg->body.motion.eventId = body.motion.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800203 // nsecs_t eventTime
204 msg->body.motion.eventTime = body.motion.eventTime;
205 // int32_t deviceId
206 msg->body.motion.deviceId = body.motion.deviceId;
207 // int32_t source
208 msg->body.motion.source = body.motion.source;
209 // int32_t displayId
210 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600211 // std::array<uint8_t, 32> hmac
212 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800213 // int32_t action
214 msg->body.motion.action = body.motion.action;
215 // int32_t actionButton
216 msg->body.motion.actionButton = body.motion.actionButton;
217 // int32_t flags
218 msg->body.motion.flags = body.motion.flags;
219 // int32_t metaState
220 msg->body.motion.metaState = body.motion.metaState;
221 // int32_t buttonState
222 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800223 // MotionClassification classification
224 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800225 // int32_t edgeFlags
226 msg->body.motion.edgeFlags = body.motion.edgeFlags;
227 // nsecs_t downTime
228 msg->body.motion.downTime = body.motion.downTime;
chaviw9eaa22c2020-07-01 16:21:27 -0700229
230 msg->body.motion.dsdx = body.motion.dsdx;
231 msg->body.motion.dtdx = body.motion.dtdx;
232 msg->body.motion.dtdy = body.motion.dtdy;
233 msg->body.motion.dsdy = body.motion.dsdy;
234 msg->body.motion.tx = body.motion.tx;
235 msg->body.motion.ty = body.motion.ty;
236
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800237 // float xPrecision
238 msg->body.motion.xPrecision = body.motion.xPrecision;
239 // float yPrecision
240 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700241 // float xCursorPosition
242 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
243 // float yCursorPosition
244 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700245 // uint32_t displayOrientation
246 msg->body.motion.displayOrientation = body.motion.displayOrientation;
247 // int32_t displayWidth
Evan Rosky84f07f02021-04-16 10:42:42 -0700248 msg->body.motion.displayWidth = body.motion.displayWidth;
Evan Rosky09576692021-07-01 12:22:09 -0700249 // int32_t displayHeight
Evan Rosky84f07f02021-04-16 10:42:42 -0700250 msg->body.motion.displayHeight = body.motion.displayHeight;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800251 // uint32_t pointerCount
252 msg->body.motion.pointerCount = body.motion.pointerCount;
253 //struct Pointer pointers[MAX_POINTERS]
254 for (size_t i = 0; i < body.motion.pointerCount; i++) {
255 // PointerProperties properties
256 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
257 msg->body.motion.pointers[i].properties.toolType =
258 body.motion.pointers[i].properties.toolType,
259 // PointerCoords coords
260 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
261 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
262 memcpy(&msg->body.motion.pointers[i].coords.values[0],
263 &body.motion.pointers[i].coords.values[0],
264 count * (sizeof(body.motion.pointers[i].coords.values[0])));
265 }
266 break;
267 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700268 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800269 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000270 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800271 break;
272 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800273 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800274 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800275 msg->body.focus.hasFocus = body.focus.hasFocus;
276 msg->body.focus.inTouchMode = body.focus.inTouchMode;
277 break;
278 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800279 case InputMessage::Type::CAPTURE: {
280 msg->body.capture.eventId = body.capture.eventId;
281 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
282 break;
283 }
arthurhung7632c332020-12-30 16:58:01 +0800284 case InputMessage::Type::DRAG: {
285 msg->body.drag.eventId = body.drag.eventId;
286 msg->body.drag.x = body.drag.x;
287 msg->body.drag.y = body.drag.y;
288 msg->body.drag.isExiting = body.drag.isExiting;
289 break;
290 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000291 case InputMessage::Type::TIMELINE: {
292 msg->body.timeline.eventId = body.timeline.eventId;
293 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
294 break;
295 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800296 }
297}
Jeff Brown5912f952013-07-01 19:10:31 -0700298
299// --- InputChannel ---
300
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500301std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500302 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700303 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
304 if (result != 0) {
305 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
306 strerror(errno));
307 return nullptr;
308 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500309 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500310 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700311}
312
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500313InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token)
314 : mName(std::move(name)), mFd(std::move(fd)), mToken(std::move(token)) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700315 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500316 ALOGD("Input channel constructed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700317 }
Jeff Brown5912f952013-07-01 19:10:31 -0700318}
319
320InputChannel::~InputChannel() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700321 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500322 ALOGD("Input channel destroyed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700323 }
Robert Carr3720ed02018-08-08 16:08:27 -0700324}
325
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800326status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500327 std::unique_ptr<InputChannel>& outServerChannel,
328 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700329 int sockets[2];
330 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
331 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000332 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
333 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500334 outServerChannel.reset();
335 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700336 return result;
337 }
338
339 int bufferSize = SOCKET_BUFFER_SIZE;
340 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
341 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
342 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
343 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
344
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700345 sp<IBinder> token = new BBinder();
346
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700347 std::string serverChannelName = name + " (server)";
348 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700349 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700350
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700351 std::string clientChannelName = name + " (client)";
352 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700353 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700354 return OK;
355}
356
357status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800358 const size_t msgLength = msg->size();
359 InputMessage cleanMsg;
360 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700361 ssize_t nWrite;
362 do {
Chris Ye0783e992020-06-02 21:34:49 -0700363 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700364 } while (nWrite == -1 && errno == EINTR);
365
366 if (nWrite < 0) {
367 int error = errno;
368#if DEBUG_CHANNEL_MESSAGES
chaviw81e2bb92019-12-18 15:03:51 -0800369 ALOGD("channel '%s' ~ error sending message of type %d, %s", mName.c_str(),
370 msg->header.type, strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700371#endif
372 if (error == EAGAIN || error == EWOULDBLOCK) {
373 return WOULD_BLOCK;
374 }
375 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
376 return DEAD_OBJECT;
377 }
378 return -error;
379 }
380
381 if (size_t(nWrite) != msgLength) {
382#if DEBUG_CHANNEL_MESSAGES
383 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800384 mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700385#endif
386 return DEAD_OBJECT;
387 }
388
389#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800390 ALOGD("channel '%s' ~ sent message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700391#endif
392 return OK;
393}
394
395status_t InputChannel::receiveMessage(InputMessage* msg) {
396 ssize_t nRead;
397 do {
Chris Ye0783e992020-06-02 21:34:49 -0700398 nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700399 } while (nRead == -1 && errno == EINTR);
400
401 if (nRead < 0) {
402 int error = errno;
403#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800404 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700405#endif
406 if (error == EAGAIN || error == EWOULDBLOCK) {
407 return WOULD_BLOCK;
408 }
409 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
410 return DEAD_OBJECT;
411 }
412 return -error;
413 }
414
415 if (nRead == 0) { // check for EOF
416#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800417 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700418#endif
419 return DEAD_OBJECT;
420 }
421
422 if (!msg->isValid(nRead)) {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000423 ALOGE("channel '%s' ~ received invalid message of size %zd", mName.c_str(), nRead);
Jeff Brown5912f952013-07-01 19:10:31 -0700424 return BAD_VALUE;
425 }
426
427#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800428 ALOGD("channel '%s' ~ received message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700429#endif
430 return OK;
431}
432
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500433std::unique_ptr<InputChannel> InputChannel::dup() const {
Garfield Tan15601662020-09-22 15:32:38 -0700434 base::unique_fd newFd(dupFd());
Chris Ye0783e992020-06-02 21:34:49 -0700435 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700436}
437
Garfield Tan15601662020-09-22 15:32:38 -0700438void InputChannel::copyTo(InputChannel& outChannel) const {
439 outChannel.mName = getName();
440 outChannel.mFd = dupFd();
441 outChannel.mToken = getConnectionToken();
442}
443
Chris Ye0783e992020-06-02 21:34:49 -0700444status_t InputChannel::writeToParcel(android::Parcel* parcel) const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500445 if (parcel == nullptr) {
446 ALOGE("%s: Null parcel", __func__);
447 return BAD_VALUE;
448 }
449 return parcel->writeStrongBinder(mToken)
450 ?: parcel->writeUtf8AsUtf16(mName) ?: parcel->writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700451}
452
Chris Ye0783e992020-06-02 21:34:49 -0700453status_t InputChannel::readFromParcel(const android::Parcel* parcel) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500454 if (parcel == nullptr) {
455 ALOGE("%s: Null parcel", __func__);
456 return BAD_VALUE;
457 }
458 mToken = parcel->readStrongBinder();
459 return parcel->readUtf8FromUtf16(&mName) ?: parcel->readUniqueFileDescriptor(&mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700460}
461
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700462sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500463 return mToken;
Robert Carr803535b2018-08-02 16:38:15 -0700464}
465
Garfield Tan15601662020-09-22 15:32:38 -0700466base::unique_fd InputChannel::dupFd() const {
467 android::base::unique_fd newFd(::dup(getFd()));
468 if (!newFd.ok()) {
469 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd().get(), getName().c_str(),
470 strerror(errno));
471 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
472 // If this process is out of file descriptors, then throwing that might end up exploding
473 // on the other side of a binder call, which isn't really helpful.
474 // Better to just crash here and hope that the FD leak is slow.
475 // Other failures could be client errors, so we still propagate those back to the caller.
476 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
477 getName().c_str());
478 return {};
479 }
480 return newFd;
481}
482
Jeff Brown5912f952013-07-01 19:10:31 -0700483// --- InputPublisher ---
484
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500485InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel) : mChannel(channel) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700486
487InputPublisher::~InputPublisher() {
488}
489
Garfield Tan1c7bc862020-01-28 13:24:04 -0800490status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
491 int32_t source, int32_t displayId,
492 std::array<uint8_t, 32> hmac, int32_t action,
493 int32_t flags, int32_t keyCode, int32_t scanCode,
494 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
495 nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000496 if (ATRACE_ENABLED()) {
497 std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
498 mChannel->getName().c_str(), keyCode);
499 ATRACE_NAME(message.c_str());
500 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800501 if (DEBUG_TRANSPORT_ACTIONS) {
502 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
503 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
504 "downTime=%" PRId64 ", eventTime=%" PRId64,
505 mChannel->getName().c_str(), seq, deviceId, source, action, flags, keyCode, scanCode,
506 metaState, repeatCount, downTime, eventTime);
507 }
Jeff Brown5912f952013-07-01 19:10:31 -0700508
509 if (!seq) {
510 ALOGE("Attempted to publish a key event with sequence number 0.");
511 return BAD_VALUE;
512 }
513
514 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700515 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500516 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800517 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700518 msg.body.key.deviceId = deviceId;
519 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100520 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700521 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700522 msg.body.key.action = action;
523 msg.body.key.flags = flags;
524 msg.body.key.keyCode = keyCode;
525 msg.body.key.scanCode = scanCode;
526 msg.body.key.metaState = metaState;
527 msg.body.key.repeatCount = repeatCount;
528 msg.body.key.downTime = downTime;
529 msg.body.key.eventTime = eventTime;
530 return mChannel->sendMessage(&msg);
531}
532
533status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800534 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600535 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
536 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700537 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Evan Rosky09576692021-07-01 12:22:09 -0700538 float yPrecision, float xCursorPosition, float yCursorPosition, uint32_t displayOrientation,
539 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
540 uint32_t pointerCount, const PointerProperties* pointerProperties,
541 const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000542 if (ATRACE_ENABLED()) {
543 std::string message = StringPrintf(
544 "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")",
545 mChannel->getName().c_str(), action);
546 ATRACE_NAME(message.c_str());
547 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800548 if (DEBUG_TRANSPORT_ACTIONS) {
chaviw9eaa22c2020-07-01 16:21:27 -0700549 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700550 transform.dump(transformString, "transform", " ");
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800551 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
552 "displayId=%" PRId32 ", "
553 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700554 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800555 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
chaviw85b44202020-07-24 11:46:21 -0700556 "pointerCount=%" PRIu32 " \n%s",
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800557 mChannel->getName().c_str(), seq, deviceId, source, displayId, action, actionButton,
558 flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700559 motionClassificationToString(classification), xPrecision, yPrecision, downTime,
560 eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800561 }
Jeff Brown5912f952013-07-01 19:10:31 -0700562
563 if (!seq) {
564 ALOGE("Attempted to publish a motion event with sequence number 0.");
565 return BAD_VALUE;
566 }
567
568 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700569 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800570 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700571 return BAD_VALUE;
572 }
573
574 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700575 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500576 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800577 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700578 msg.body.motion.deviceId = deviceId;
579 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700580 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700581 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700582 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100583 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700584 msg.body.motion.flags = flags;
585 msg.body.motion.edgeFlags = edgeFlags;
586 msg.body.motion.metaState = metaState;
587 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800588 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700589 msg.body.motion.dsdx = transform.dsdx();
590 msg.body.motion.dtdx = transform.dtdx();
591 msg.body.motion.dtdy = transform.dtdy();
592 msg.body.motion.dsdy = transform.dsdy();
593 msg.body.motion.tx = transform.tx();
594 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700595 msg.body.motion.xPrecision = xPrecision;
596 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700597 msg.body.motion.xCursorPosition = xCursorPosition;
598 msg.body.motion.yCursorPosition = yCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700599 msg.body.motion.displayOrientation = displayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700600 msg.body.motion.displayWidth = displayWidth;
601 msg.body.motion.displayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700602 msg.body.motion.downTime = downTime;
603 msg.body.motion.eventTime = eventTime;
604 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100605 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700606 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
607 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
608 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700609
Jeff Brown5912f952013-07-01 19:10:31 -0700610 return mChannel->sendMessage(&msg);
611}
612
Garfield Tan1c7bc862020-01-28 13:24:04 -0800613status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus,
614 bool inTouchMode) {
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800615 if (ATRACE_ENABLED()) {
616 std::string message =
617 StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s, inTouchMode=%s)",
618 mChannel->getName().c_str(), toString(hasFocus),
619 toString(inTouchMode));
620 ATRACE_NAME(message.c_str());
621 }
622
623 InputMessage msg;
624 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500625 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800626 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000627 msg.body.focus.hasFocus = hasFocus;
628 msg.body.focus.inTouchMode = inTouchMode;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800629 return mChannel->sendMessage(&msg);
630}
631
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800632status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
633 bool pointerCaptureEnabled) {
634 if (ATRACE_ENABLED()) {
635 std::string message =
636 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
637 mChannel->getName().c_str(), toString(pointerCaptureEnabled));
638 ATRACE_NAME(message.c_str());
639 }
640
641 InputMessage msg;
642 msg.header.type = InputMessage::Type::CAPTURE;
643 msg.header.seq = seq;
644 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000645 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800646 return mChannel->sendMessage(&msg);
647}
648
arthurhung7632c332020-12-30 16:58:01 +0800649status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
650 bool isExiting) {
651 if (ATRACE_ENABLED()) {
652 std::string message =
653 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
654 mChannel->getName().c_str(), x, y, toString(isExiting));
655 ATRACE_NAME(message.c_str());
656 }
657
658 InputMessage msg;
659 msg.header.type = InputMessage::Type::DRAG;
660 msg.header.seq = seq;
661 msg.body.drag.eventId = eventId;
662 msg.body.drag.isExiting = isExiting;
663 msg.body.drag.x = x;
664 msg.body.drag.y = y;
665 return mChannel->sendMessage(&msg);
666}
667
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000668android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800669 if (DEBUG_TRANSPORT_ACTIONS) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000670 ALOGD("channel '%s' publisher ~ %s", mChannel->getName().c_str(), __func__);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800671 }
Jeff Brown5912f952013-07-01 19:10:31 -0700672
673 InputMessage msg;
674 status_t result = mChannel->receiveMessage(&msg);
675 if (result) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000676 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700677 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000678 if (msg.header.type == InputMessage::Type::FINISHED) {
679 return Finished{
680 .seq = msg.header.seq,
681 .handled = msg.body.finished.handled,
682 .consumeTime = msg.body.finished.consumeTime,
683 };
Jeff Brown5912f952013-07-01 19:10:31 -0700684 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000685
686 if (msg.header.type == InputMessage::Type::TIMELINE) {
687 return Timeline{
688 .inputEventId = msg.body.timeline.eventId,
689 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
690 };
691 }
692
693 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
694 mChannel->getName().c_str(), NamedEnum::string(msg.header.type).c_str());
695 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700696}
697
698// --- InputConsumer ---
699
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500700InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
701 : mResampleTouch(isTouchResamplingEnabled()), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700702
703InputConsumer::~InputConsumer() {
704}
705
706bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600707 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700708}
709
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800710status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
711 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800712 if (DEBUG_TRANSPORT_ACTIONS) {
713 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
714 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
715 }
Jeff Brown5912f952013-07-01 19:10:31 -0700716
717 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700718 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700719
720 // Fetch the next input message.
721 // Loop until an event can be returned or no additional events are received.
722 while (!*outEvent) {
723 if (mMsgDeferred) {
724 // mMsg contains a valid input message from the previous call to consume
725 // that has not yet been processed.
726 mMsgDeferred = false;
727 } else {
728 // Receive a fresh message.
729 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000730 if (result == OK) {
731 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
732 }
Jeff Brown5912f952013-07-01 19:10:31 -0700733 if (result) {
734 // Consume the next batched event unless batches are being held for later.
735 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800736 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700737 if (*outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800738 if (DEBUG_TRANSPORT_ACTIONS) {
739 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
740 mChannel->getName().c_str(), *outSeq);
741 }
Jeff Brown5912f952013-07-01 19:10:31 -0700742 break;
743 }
744 }
745 return result;
746 }
747 }
748
749 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700750 case InputMessage::Type::KEY: {
751 KeyEvent* keyEvent = factory->createKeyEvent();
752 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700753
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700754 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500755 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700756 *outEvent = keyEvent;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800757 if (DEBUG_TRANSPORT_ACTIONS) {
758 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
759 mChannel->getName().c_str(), *outSeq);
760 }
Jeff Brown5912f952013-07-01 19:10:31 -0700761 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700762 }
Jeff Brown5912f952013-07-01 19:10:31 -0700763
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700764 case InputMessage::Type::MOTION: {
765 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
766 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500767 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700768 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500769 batch.samples.push_back(mMsg);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800770 if (DEBUG_TRANSPORT_ACTIONS) {
771 ALOGD("channel '%s' consumer ~ appended to batch event",
772 mChannel->getName().c_str());
773 }
Jeff Brown5912f952013-07-01 19:10:31 -0700774 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700775 } else if (isPointerEvent(mMsg.body.motion.source) &&
776 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
777 // No need to process events that we are going to cancel anyways
778 const size_t count = batch.samples.size();
779 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500780 const InputMessage& msg = batch.samples[i];
781 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700782 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500783 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
784 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700785 } else {
786 // We cannot append to the batch in progress, so we need to consume
787 // the previous batch right now and defer the new message until later.
788 mMsgDeferred = true;
789 status_t result = consumeSamples(factory, batch, batch.samples.size(),
790 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500791 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700792 if (result) {
793 return result;
794 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800795 if (DEBUG_TRANSPORT_ACTIONS) {
796 ALOGD("channel '%s' consumer ~ consumed batch event and "
797 "deferred current event, seq=%u",
798 mChannel->getName().c_str(), *outSeq);
799 }
Jeff Brown5912f952013-07-01 19:10:31 -0700800 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700801 }
Jeff Brown5912f952013-07-01 19:10:31 -0700802 }
Jeff Brown5912f952013-07-01 19:10:31 -0700803
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800804 // Start a new batch if needed.
805 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
806 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500807 Batch batch;
808 batch.samples.push_back(mMsg);
809 mBatches.push_back(batch);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800810 if (DEBUG_TRANSPORT_ACTIONS) {
811 ALOGD("channel '%s' consumer ~ started batch event",
812 mChannel->getName().c_str());
813 }
814 break;
815 }
Jeff Brown5912f952013-07-01 19:10:31 -0700816
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800817 MotionEvent* motionEvent = factory->createMotionEvent();
818 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700819
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800820 updateTouchState(mMsg);
821 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500822 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800823 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800824
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800825 if (DEBUG_TRANSPORT_ACTIONS) {
826 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
827 mChannel->getName().c_str(), *outSeq);
828 }
829 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700830 }
Jeff Brown5912f952013-07-01 19:10:31 -0700831
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000832 case InputMessage::Type::FINISHED:
833 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000834 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
835 "InputConsumer!",
836 NamedEnum::string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800837 break;
838 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800839
840 case InputMessage::Type::FOCUS: {
841 FocusEvent* focusEvent = factory->createFocusEvent();
842 if (!focusEvent) return NO_MEMORY;
843
844 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500845 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800846 *outEvent = focusEvent;
847 break;
848 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800849
850 case InputMessage::Type::CAPTURE: {
851 CaptureEvent* captureEvent = factory->createCaptureEvent();
852 if (!captureEvent) return NO_MEMORY;
853
854 initializeCaptureEvent(captureEvent, &mMsg);
855 *outSeq = mMsg.header.seq;
856 *outEvent = captureEvent;
857 break;
858 }
arthurhung7632c332020-12-30 16:58:01 +0800859
860 case InputMessage::Type::DRAG: {
861 DragEvent* dragEvent = factory->createDragEvent();
862 if (!dragEvent) return NO_MEMORY;
863
864 initializeDragEvent(dragEvent, &mMsg);
865 *outSeq = mMsg.header.seq;
866 *outEvent = dragEvent;
867 break;
868 }
Jeff Brown5912f952013-07-01 19:10:31 -0700869 }
870 }
871 return OK;
872}
873
874status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800875 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700876 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700877 for (size_t i = mBatches.size(); i > 0; ) {
878 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500879 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -0700880 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800881 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500882 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -0700883 return result;
884 }
885
Michael Wright32232172013-10-21 12:05:22 -0700886 nsecs_t sampleTime = frameTime;
887 if (mResampleTouch) {
888 sampleTime -= RESAMPLE_LATENCY;
889 }
Jeff Brown5912f952013-07-01 19:10:31 -0700890 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
891 if (split < 0) {
892 continue;
893 }
894
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800895 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700896 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500897 if (batch.samples.empty()) {
898 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700899 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700900 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500901 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -0700902 }
Michael Wright32232172013-10-21 12:05:22 -0700903 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700904 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
905 }
906 return result;
907 }
908
909 return WOULD_BLOCK;
910}
911
912status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800913 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700914 MotionEvent* motionEvent = factory->createMotionEvent();
915 if (! motionEvent) return NO_MEMORY;
916
917 uint32_t chain = 0;
918 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500919 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100920 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700921 if (i) {
922 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500923 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700924 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500925 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -0700926 addSample(motionEvent, &msg);
927 } else {
928 initializeMotionEvent(motionEvent, &msg);
929 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500930 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700931 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500932 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -0700933
934 *outSeq = chain;
935 *outEvent = motionEvent;
936 return OK;
937}
938
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100939void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800940 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700941 return;
942 }
943
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100944 int32_t deviceId = msg.body.motion.deviceId;
945 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -0700946
947 // Update the touch state history to incorporate the new input message.
948 // If the message is in the past relative to the most recently produced resampled
949 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100950 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -0700951 case AMOTION_EVENT_ACTION_DOWN: {
952 ssize_t index = findTouchState(deviceId, source);
953 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500954 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -0700955 index = mTouchStates.size() - 1;
956 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500957 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700958 touchState.initialize(deviceId, source);
959 touchState.addHistory(msg);
960 break;
961 }
962
963 case AMOTION_EVENT_ACTION_MOVE: {
964 ssize_t index = findTouchState(deviceId, source);
965 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500966 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700967 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800968 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700969 }
970 break;
971 }
972
973 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
974 ssize_t index = findTouchState(deviceId, source);
975 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500976 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100977 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700978 rewriteMessage(touchState, msg);
979 }
980 break;
981 }
982
983 case AMOTION_EVENT_ACTION_POINTER_UP: {
984 ssize_t index = findTouchState(deviceId, source);
985 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500986 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700987 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100988 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700989 }
990 break;
991 }
992
993 case AMOTION_EVENT_ACTION_SCROLL: {
994 ssize_t index = findTouchState(deviceId, source);
995 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500996 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700997 rewriteMessage(touchState, msg);
998 }
999 break;
1000 }
1001
1002 case AMOTION_EVENT_ACTION_UP:
1003 case AMOTION_EVENT_ACTION_CANCEL: {
1004 ssize_t index = findTouchState(deviceId, source);
1005 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001006 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001007 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001008 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -07001009 }
1010 break;
1011 }
1012 }
1013}
1014
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001015/**
1016 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1017 *
1018 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1019 * is in the past relative to msg and the past two events do not contain identical coordinates),
1020 * then invalidate the lastResample data for that pointer.
1021 * If the two past events have identical coordinates, then lastResample data for that pointer will
1022 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1023 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1024 * not equal to x0 is received.
1025 */
1026void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001027 nsecs_t eventTime = msg.body.motion.eventTime;
1028 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1029 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001030 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001031 if (eventTime < state.lastResample.eventTime ||
1032 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001033 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1034 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001035#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001036 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
1037 resampleCoords.getX(), resampleCoords.getY(),
1038 msgCoords.getX(), msgCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001039#endif
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001040 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1041 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
1042 } else {
1043 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001044 }
Jeff Brown5912f952013-07-01 19:10:31 -07001045 }
1046 }
1047}
1048
1049void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1050 const InputMessage* next) {
1051 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001052 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001053 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1054 return;
1055 }
1056
1057 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1058 if (index < 0) {
1059#if DEBUG_RESAMPLING
1060 ALOGD("Not resampled, no touch state for device.");
1061#endif
1062 return;
1063 }
1064
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001065 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001066 if (touchState.historySize < 1) {
1067#if DEBUG_RESAMPLING
1068 ALOGD("Not resampled, no history for device.");
1069#endif
1070 return;
1071 }
1072
1073 // Ensure that the current sample has all of the pointers that need to be reported.
1074 const History* current = touchState.getHistory(0);
1075 size_t pointerCount = event->getPointerCount();
1076 for (size_t i = 0; i < pointerCount; i++) {
1077 uint32_t id = event->getPointerId(i);
1078 if (!current->idBits.hasBit(id)) {
1079#if DEBUG_RESAMPLING
1080 ALOGD("Not resampled, missing id %d", id);
1081#endif
1082 return;
1083 }
1084 }
1085
1086 // Find the data to use for resampling.
1087 const History* other;
1088 History future;
1089 float alpha;
1090 if (next) {
1091 // Interpolate between current sample and future sample.
1092 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001093 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001094 other = &future;
1095 nsecs_t delta = future.eventTime - current->eventTime;
1096 if (delta < RESAMPLE_MIN_DELTA) {
1097#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001098 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001099#endif
1100 return;
1101 }
1102 alpha = float(sampleTime - current->eventTime) / delta;
1103 } else if (touchState.historySize >= 2) {
1104 // Extrapolate future sample using current sample and past sample.
1105 // So other->eventTime <= current->eventTime <= sampleTime.
1106 other = touchState.getHistory(1);
1107 nsecs_t delta = current->eventTime - other->eventTime;
1108 if (delta < RESAMPLE_MIN_DELTA) {
1109#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001110 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001111#endif
1112 return;
1113 } else if (delta > RESAMPLE_MAX_DELTA) {
1114#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001115 ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001116#endif
1117 return;
1118 }
1119 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1120 if (sampleTime > maxPredict) {
1121#if DEBUG_RESAMPLING
1122 ALOGD("Sample time is too far in the future, adjusting prediction "
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001123 "from %" PRId64 " to %" PRId64 " ns.",
Jeff Brown5912f952013-07-01 19:10:31 -07001124 sampleTime - current->eventTime, maxPredict - current->eventTime);
1125#endif
1126 sampleTime = maxPredict;
1127 }
1128 alpha = float(current->eventTime - sampleTime) / delta;
1129 } else {
1130#if DEBUG_RESAMPLING
1131 ALOGD("Not resampled, insufficient data.");
1132#endif
1133 return;
1134 }
1135
1136 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001137 History oldLastResample;
1138 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001139 touchState.lastResample.eventTime = sampleTime;
1140 touchState.lastResample.idBits.clear();
1141 for (size_t i = 0; i < pointerCount; i++) {
1142 uint32_t id = event->getPointerId(i);
1143 touchState.lastResample.idToIndex[id] = i;
1144 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001145 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1146 // We maintain the previously resampled value for this pointer (stored in
1147 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1148 // This way we don't introduce artificial jitter when pointers haven't actually moved.
1149
1150 // We know here that the coordinates for the pointer haven't changed because we
1151 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1152 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1153 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1154 continue;
1155 }
1156
Jeff Brown5912f952013-07-01 19:10:31 -07001157 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1158 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001159 resampledCoords.copyFrom(currentCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001160 if (other->idBits.hasBit(id)
1161 && shouldResampleTool(event->getToolType(i))) {
1162 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001163 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
1164 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
1165 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
1166 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
1167#if DEBUG_RESAMPLING
1168 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1169 "other (%0.3f, %0.3f), alpha %0.3f",
1170 id, resampledCoords.getX(), resampledCoords.getY(),
1171 currentCoords.getX(), currentCoords.getY(),
1172 otherCoords.getX(), otherCoords.getY(),
1173 alpha);
1174#endif
1175 } else {
Jeff Brown5912f952013-07-01 19:10:31 -07001176#if DEBUG_RESAMPLING
1177 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
1178 id, resampledCoords.getX(), resampledCoords.getY(),
1179 currentCoords.getX(), currentCoords.getY());
1180#endif
1181 }
1182 }
1183
1184 event->addSample(sampleTime, touchState.lastResample.pointers);
1185}
1186
1187bool InputConsumer::shouldResampleTool(int32_t toolType) {
1188 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1189 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1190}
1191
1192status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08001193 if (DEBUG_TRANSPORT_ACTIONS) {
1194 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1195 mChannel->getName().c_str(), seq, toString(handled));
1196 }
Jeff Brown5912f952013-07-01 19:10:31 -07001197
1198 if (!seq) {
1199 ALOGE("Attempted to send a finished signal with sequence number 0.");
1200 return BAD_VALUE;
1201 }
1202
1203 // Send finished signals for the batch sequence chain first.
1204 size_t seqChainCount = mSeqChains.size();
1205 if (seqChainCount) {
1206 uint32_t currentSeq = seq;
1207 uint32_t chainSeqs[seqChainCount];
1208 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001209 for (size_t i = seqChainCount; i > 0; ) {
1210 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001211 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001212 if (seqChain.seq == currentSeq) {
1213 currentSeq = seqChain.chain;
1214 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001215 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001216 }
1217 }
1218 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001219 while (!status && chainIndex > 0) {
1220 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001221 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1222 }
1223 if (status) {
1224 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001225 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001226 SeqChain seqChain;
1227 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1228 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001229 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001230 if (!chainIndex) break;
1231 chainIndex--;
1232 }
Jeff Brown5912f952013-07-01 19:10:31 -07001233 return status;
1234 }
1235 }
1236
1237 // Send finished signal for the last message in the batch.
1238 return sendUnchainedFinishedSignal(seq, handled);
1239}
1240
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001241status_t InputConsumer::sendTimeline(int32_t inputEventId,
1242 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
1243 if (DEBUG_TRANSPORT_ACTIONS) {
1244 ALOGD("channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1245 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1246 mChannel->getName().c_str(), inputEventId,
1247 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1248 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
1249 }
1250
1251 InputMessage msg;
1252 msg.header.type = InputMessage::Type::TIMELINE;
1253 msg.header.seq = 0;
1254 msg.body.timeline.eventId = inputEventId;
1255 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1256 return mChannel->sendMessage(&msg);
1257}
1258
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001259nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1260 auto it = mConsumeTimes.find(seq);
1261 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1262 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1263 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1264 seq);
1265 return it->second;
1266}
1267
1268void InputConsumer::popConsumeTime(uint32_t seq) {
1269 mConsumeTimes.erase(seq);
1270}
1271
Jeff Brown5912f952013-07-01 19:10:31 -07001272status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1273 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001274 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001275 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001276 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001277 msg.body.finished.consumeTime = getConsumeTime(seq);
1278 status_t result = mChannel->sendMessage(&msg);
1279 if (result == OK) {
1280 // Remove the consume time if the socket write succeeded. We will not need to ack this
1281 // message anymore. If the socket write did not succeed, we will try again and will still
1282 // need consume time.
1283 popConsumeTime(seq);
1284 }
1285 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001286}
1287
1288bool InputConsumer::hasDeferredEvent() const {
1289 return mMsgDeferred;
1290}
1291
1292bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001293 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001294}
1295
Arthur Hungc7812be2020-02-27 22:40:27 +08001296int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001297 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001298 return AINPUT_SOURCE_CLASS_NONE;
1299 }
1300
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001301 const Batch& batch = mBatches[0];
1302 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001303 return head.body.motion.source;
1304}
1305
Jeff Brown5912f952013-07-01 19:10:31 -07001306ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1307 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001308 const Batch& batch = mBatches[i];
1309 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001310 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1311 return i;
1312 }
1313 }
1314 return -1;
1315}
1316
1317ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1318 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001319 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001320 if (touchState.deviceId == deviceId && touchState.source == source) {
1321 return i;
1322 }
1323 }
1324 return -1;
1325}
1326
1327void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001328 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001329 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1330 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1331 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1332 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001333}
1334
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001335void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001336 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus,
1337 msg->body.focus.inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001338}
1339
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001340void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001341 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001342}
1343
arthurhung7632c332020-12-30 16:58:01 +08001344void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1345 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1346 msg->body.drag.isExiting);
1347}
1348
Jeff Brown5912f952013-07-01 19:10:31 -07001349void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001350 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001351 PointerProperties pointerProperties[pointerCount];
1352 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001353 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001354 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1355 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1356 }
1357
chaviw9eaa22c2020-07-01 16:21:27 -07001358 ui::Transform transform;
1359 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1360 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001361 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1362 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1363 msg->body.motion.actionButton, msg->body.motion.flags,
1364 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001365 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1366 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1367 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -07001368 msg->body.motion.displayOrientation, msg->body.motion.displayWidth,
1369 msg->body.motion.displayHeight, msg->body.motion.downTime,
1370 msg->body.motion.eventTime, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001371}
1372
1373void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001374 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001375 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001376 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001377 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1378 }
1379
1380 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1381 event->addSample(msg->body.motion.eventTime, pointerCoords);
1382}
1383
1384bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001385 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001386 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001387 if (head.body.motion.pointerCount != pointerCount
1388 || head.body.motion.action != msg->body.motion.action) {
1389 return false;
1390 }
1391 for (size_t i = 0; i < pointerCount; i++) {
1392 if (head.body.motion.pointers[i].properties
1393 != msg->body.motion.pointers[i].properties) {
1394 return false;
1395 }
1396 }
1397 return true;
1398}
1399
1400ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1401 size_t numSamples = batch.samples.size();
1402 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001403 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001404 index += 1;
1405 }
1406 return ssize_t(index) - 1;
1407}
1408
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001409std::string InputConsumer::dump() const {
1410 std::string out;
1411 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1412 out = out + "mChannel = " + mChannel->getName() + "\n";
1413 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1414 if (mMsgDeferred) {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +00001415 out = out + "mMsg : " + NamedEnum::string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001416 }
1417 out += "Batches:\n";
1418 for (const Batch& batch : mBatches) {
1419 out += " Batch:\n";
1420 for (const InputMessage& msg : batch.samples) {
1421 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Siarhei Vishniakou7766c032021-03-02 20:32:20 +00001422 NamedEnum::string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001423 switch (msg.header.type) {
1424 case InputMessage::Type::KEY: {
1425 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1426 KeyEvent::actionToString(
1427 msg.body.key.action),
1428 msg.body.key.keyCode);
1429 break;
1430 }
1431 case InputMessage::Type::MOTION: {
1432 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1433 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1434 const float x = msg.body.motion.pointers[i].coords.getX();
1435 const float y = msg.body.motion.pointers[i].coords.getY();
1436 out += android::base::StringPrintf("\n Pointer %" PRIu32
1437 " : x=%.1f y=%.1f",
1438 i, x, y);
1439 }
1440 break;
1441 }
1442 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001443 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1444 toString(msg.body.finished.handled),
1445 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001446 break;
1447 }
1448 case InputMessage::Type::FOCUS: {
1449 out += android::base::StringPrintf("hasFocus=%s inTouchMode=%s",
1450 toString(msg.body.focus.hasFocus),
1451 toString(msg.body.focus.inTouchMode));
1452 break;
1453 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001454 case InputMessage::Type::CAPTURE: {
1455 out += android::base::StringPrintf("hasCapture=%s",
1456 toString(msg.body.capture
1457 .pointerCaptureEnabled));
1458 break;
1459 }
arthurhung7632c332020-12-30 16:58:01 +08001460 case InputMessage::Type::DRAG: {
1461 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1462 msg.body.drag.x, msg.body.drag.y,
1463 toString(msg.body.drag.isExiting));
1464 break;
1465 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001466 case InputMessage::Type::TIMELINE: {
1467 const nsecs_t gpuCompletedTime =
1468 msg.body.timeline
1469 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1470 const nsecs_t presentTime =
1471 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1472 out += android::base::StringPrintf("inputEventId=%" PRId32
1473 ", gpuCompletedTime=%" PRId64
1474 ", presentTime=%" PRId64,
1475 msg.body.timeline.eventId, gpuCompletedTime,
1476 presentTime);
1477 break;
1478 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001479 }
1480 out += "\n";
1481 }
1482 }
1483 if (mBatches.empty()) {
1484 out += " <empty>\n";
1485 }
1486 out += "mSeqChains:\n";
1487 for (const SeqChain& chain : mSeqChains) {
1488 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1489 chain.chain);
1490 }
1491 if (mSeqChains.empty()) {
1492 out += " <empty>\n";
1493 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001494 out += "mConsumeTimes:\n";
1495 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1496 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1497 consumeTime);
1498 }
1499 if (mConsumeTimes.empty()) {
1500 out += " <empty>\n";
1501 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001502 return out;
1503}
1504
Jeff Brown5912f952013-07-01 19:10:31 -07001505} // namespace android