blob: de75691cc791a1f9aaeaa3e3f9ab742d24052a65 [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
Jeff Brown5912f952013-07-01 19:10:31 -070036#include <input/InputTransport.h>
Siarhei Vishniakou7766c032021-03-02 20:32:20 +000037#include <input/NamedEnum.h>
Jeff Brown5912f952013-07-01 19:10:31 -070038
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 {
99 if (size() == actualSize) {
100 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700101 case Type::KEY:
102 return true;
103 case Type::MOTION:
104 return body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
105 case Type::FINISHED:
106 return true;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800107 case Type::FOCUS:
108 return true;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800109 case Type::CAPTURE:
110 return true;
arthurhung7632c332020-12-30 16:58:01 +0800111 case Type::DRAG:
112 return true;
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000113 case Type::TIMELINE:
114 const nsecs_t gpuCompletedTime =
115 body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
116 const nsecs_t presentTime =
117 body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
118 return presentTime > gpuCompletedTime;
Jeff Brown5912f952013-07-01 19:10:31 -0700119 }
120 }
121 return false;
122}
123
124size_t InputMessage::size() const {
125 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700126 case Type::KEY:
127 return sizeof(Header) + body.key.size();
128 case Type::MOTION:
129 return sizeof(Header) + body.motion.size();
130 case Type::FINISHED:
131 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800132 case Type::FOCUS:
133 return sizeof(Header) + body.focus.size();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800134 case Type::CAPTURE:
135 return sizeof(Header) + body.capture.size();
arthurhung7632c332020-12-30 16:58:01 +0800136 case Type::DRAG:
137 return sizeof(Header) + body.drag.size();
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000138 case Type::TIMELINE:
139 return sizeof(Header) + body.timeline.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700140 }
141 return sizeof(Header);
142}
143
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800144/**
145 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
146 * memory to zero, then only copy the valid bytes on a per-field basis.
147 */
148void InputMessage::getSanitizedCopy(InputMessage* msg) const {
149 memset(msg, 0, sizeof(*msg));
150
151 // Write the header
152 msg->header.type = header.type;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500153 msg->header.seq = header.seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800154
155 // Write the body
156 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700157 case InputMessage::Type::KEY: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800158 // int32_t eventId
159 msg->body.key.eventId = body.key.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800160 // nsecs_t eventTime
161 msg->body.key.eventTime = body.key.eventTime;
162 // int32_t deviceId
163 msg->body.key.deviceId = body.key.deviceId;
164 // int32_t source
165 msg->body.key.source = body.key.source;
166 // int32_t displayId
167 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600168 // std::array<uint8_t, 32> hmac
169 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800170 // int32_t action
171 msg->body.key.action = body.key.action;
172 // int32_t flags
173 msg->body.key.flags = body.key.flags;
174 // int32_t keyCode
175 msg->body.key.keyCode = body.key.keyCode;
176 // int32_t scanCode
177 msg->body.key.scanCode = body.key.scanCode;
178 // int32_t metaState
179 msg->body.key.metaState = body.key.metaState;
180 // int32_t repeatCount
181 msg->body.key.repeatCount = body.key.repeatCount;
182 // nsecs_t downTime
183 msg->body.key.downTime = body.key.downTime;
184 break;
185 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700186 case InputMessage::Type::MOTION: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800187 // int32_t eventId
188 msg->body.motion.eventId = body.motion.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800189 // nsecs_t eventTime
190 msg->body.motion.eventTime = body.motion.eventTime;
191 // int32_t deviceId
192 msg->body.motion.deviceId = body.motion.deviceId;
193 // int32_t source
194 msg->body.motion.source = body.motion.source;
195 // int32_t displayId
196 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600197 // std::array<uint8_t, 32> hmac
198 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800199 // int32_t action
200 msg->body.motion.action = body.motion.action;
201 // int32_t actionButton
202 msg->body.motion.actionButton = body.motion.actionButton;
203 // int32_t flags
204 msg->body.motion.flags = body.motion.flags;
205 // int32_t metaState
206 msg->body.motion.metaState = body.motion.metaState;
207 // int32_t buttonState
208 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800209 // MotionClassification classification
210 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800211 // int32_t edgeFlags
212 msg->body.motion.edgeFlags = body.motion.edgeFlags;
213 // nsecs_t downTime
214 msg->body.motion.downTime = body.motion.downTime;
chaviw9eaa22c2020-07-01 16:21:27 -0700215
216 msg->body.motion.dsdx = body.motion.dsdx;
217 msg->body.motion.dtdx = body.motion.dtdx;
218 msg->body.motion.dtdy = body.motion.dtdy;
219 msg->body.motion.dsdy = body.motion.dsdy;
220 msg->body.motion.tx = body.motion.tx;
221 msg->body.motion.ty = body.motion.ty;
222
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800223 // float xPrecision
224 msg->body.motion.xPrecision = body.motion.xPrecision;
225 // float yPrecision
226 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700227 // float xCursorPosition
228 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
229 // float yCursorPosition
230 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700231 // int32_t displayW
232 msg->body.motion.displayWidth = body.motion.displayWidth;
233 // int32_t displayH
234 msg->body.motion.displayHeight = body.motion.displayHeight;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800235 // uint32_t pointerCount
236 msg->body.motion.pointerCount = body.motion.pointerCount;
237 //struct Pointer pointers[MAX_POINTERS]
238 for (size_t i = 0; i < body.motion.pointerCount; i++) {
239 // PointerProperties properties
240 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
241 msg->body.motion.pointers[i].properties.toolType =
242 body.motion.pointers[i].properties.toolType,
243 // PointerCoords coords
244 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
245 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
246 memcpy(&msg->body.motion.pointers[i].coords.values[0],
247 &body.motion.pointers[i].coords.values[0],
248 count * (sizeof(body.motion.pointers[i].coords.values[0])));
249 }
250 break;
251 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700252 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800253 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000254 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800255 break;
256 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800257 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800258 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800259 msg->body.focus.hasFocus = body.focus.hasFocus;
260 msg->body.focus.inTouchMode = body.focus.inTouchMode;
261 break;
262 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800263 case InputMessage::Type::CAPTURE: {
264 msg->body.capture.eventId = body.capture.eventId;
265 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
266 break;
267 }
arthurhung7632c332020-12-30 16:58:01 +0800268 case InputMessage::Type::DRAG: {
269 msg->body.drag.eventId = body.drag.eventId;
270 msg->body.drag.x = body.drag.x;
271 msg->body.drag.y = body.drag.y;
272 msg->body.drag.isExiting = body.drag.isExiting;
273 break;
274 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000275 case InputMessage::Type::TIMELINE: {
276 msg->body.timeline.eventId = body.timeline.eventId;
277 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
278 break;
279 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800280 }
281}
Jeff Brown5912f952013-07-01 19:10:31 -0700282
283// --- InputChannel ---
284
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500285std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500286 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700287 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
288 if (result != 0) {
289 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
290 strerror(errno));
291 return nullptr;
292 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500293 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500294 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700295}
296
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500297InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token)
298 : mName(std::move(name)), mFd(std::move(fd)), mToken(std::move(token)) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700299 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500300 ALOGD("Input channel constructed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700301 }
Jeff Brown5912f952013-07-01 19:10:31 -0700302}
303
304InputChannel::~InputChannel() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700305 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500306 ALOGD("Input channel destroyed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700307 }
Robert Carr3720ed02018-08-08 16:08:27 -0700308}
309
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800310status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500311 std::unique_ptr<InputChannel>& outServerChannel,
312 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700313 int sockets[2];
314 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
315 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000316 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
317 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500318 outServerChannel.reset();
319 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700320 return result;
321 }
322
323 int bufferSize = SOCKET_BUFFER_SIZE;
324 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
325 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
326 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
327 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
328
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700329 sp<IBinder> token = new BBinder();
330
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700331 std::string serverChannelName = name + " (server)";
332 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700333 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700334
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700335 std::string clientChannelName = name + " (client)";
336 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700337 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700338 return OK;
339}
340
341status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800342 const size_t msgLength = msg->size();
343 InputMessage cleanMsg;
344 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700345 ssize_t nWrite;
346 do {
Chris Ye0783e992020-06-02 21:34:49 -0700347 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700348 } while (nWrite == -1 && errno == EINTR);
349
350 if (nWrite < 0) {
351 int error = errno;
352#if DEBUG_CHANNEL_MESSAGES
chaviw81e2bb92019-12-18 15:03:51 -0800353 ALOGD("channel '%s' ~ error sending message of type %d, %s", mName.c_str(),
354 msg->header.type, strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700355#endif
356 if (error == EAGAIN || error == EWOULDBLOCK) {
357 return WOULD_BLOCK;
358 }
359 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
360 return DEAD_OBJECT;
361 }
362 return -error;
363 }
364
365 if (size_t(nWrite) != msgLength) {
366#if DEBUG_CHANNEL_MESSAGES
367 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800368 mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700369#endif
370 return DEAD_OBJECT;
371 }
372
373#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800374 ALOGD("channel '%s' ~ sent message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700375#endif
376 return OK;
377}
378
379status_t InputChannel::receiveMessage(InputMessage* msg) {
380 ssize_t nRead;
381 do {
Chris Ye0783e992020-06-02 21:34:49 -0700382 nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700383 } while (nRead == -1 && errno == EINTR);
384
385 if (nRead < 0) {
386 int error = errno;
387#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800388 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700389#endif
390 if (error == EAGAIN || error == EWOULDBLOCK) {
391 return WOULD_BLOCK;
392 }
393 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
394 return DEAD_OBJECT;
395 }
396 return -error;
397 }
398
399 if (nRead == 0) { // check for EOF
400#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800401 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700402#endif
403 return DEAD_OBJECT;
404 }
405
406 if (!msg->isValid(nRead)) {
407#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800408 ALOGD("channel '%s' ~ received invalid message", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700409#endif
410 return BAD_VALUE;
411 }
412
413#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800414 ALOGD("channel '%s' ~ received message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700415#endif
416 return OK;
417}
418
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500419std::unique_ptr<InputChannel> InputChannel::dup() const {
Garfield Tan15601662020-09-22 15:32:38 -0700420 base::unique_fd newFd(dupFd());
Chris Ye0783e992020-06-02 21:34:49 -0700421 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700422}
423
Garfield Tan15601662020-09-22 15:32:38 -0700424void InputChannel::copyTo(InputChannel& outChannel) const {
425 outChannel.mName = getName();
426 outChannel.mFd = dupFd();
427 outChannel.mToken = getConnectionToken();
428}
429
Chris Ye0783e992020-06-02 21:34:49 -0700430status_t InputChannel::writeToParcel(android::Parcel* parcel) const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500431 if (parcel == nullptr) {
432 ALOGE("%s: Null parcel", __func__);
433 return BAD_VALUE;
434 }
435 return parcel->writeStrongBinder(mToken)
436 ?: parcel->writeUtf8AsUtf16(mName) ?: parcel->writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700437}
438
Chris Ye0783e992020-06-02 21:34:49 -0700439status_t InputChannel::readFromParcel(const android::Parcel* parcel) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500440 if (parcel == nullptr) {
441 ALOGE("%s: Null parcel", __func__);
442 return BAD_VALUE;
443 }
444 mToken = parcel->readStrongBinder();
445 return parcel->readUtf8FromUtf16(&mName) ?: parcel->readUniqueFileDescriptor(&mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700446}
447
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700448sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500449 return mToken;
Robert Carr803535b2018-08-02 16:38:15 -0700450}
451
Garfield Tan15601662020-09-22 15:32:38 -0700452base::unique_fd InputChannel::dupFd() const {
453 android::base::unique_fd newFd(::dup(getFd()));
454 if (!newFd.ok()) {
455 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd().get(), getName().c_str(),
456 strerror(errno));
457 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
458 // If this process is out of file descriptors, then throwing that might end up exploding
459 // on the other side of a binder call, which isn't really helpful.
460 // Better to just crash here and hope that the FD leak is slow.
461 // Other failures could be client errors, so we still propagate those back to the caller.
462 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
463 getName().c_str());
464 return {};
465 }
466 return newFd;
467}
468
Jeff Brown5912f952013-07-01 19:10:31 -0700469// --- InputPublisher ---
470
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500471InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel) : mChannel(channel) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700472
473InputPublisher::~InputPublisher() {
474}
475
Garfield Tan1c7bc862020-01-28 13:24:04 -0800476status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
477 int32_t source, int32_t displayId,
478 std::array<uint8_t, 32> hmac, int32_t action,
479 int32_t flags, int32_t keyCode, int32_t scanCode,
480 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
481 nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000482 if (ATRACE_ENABLED()) {
483 std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
484 mChannel->getName().c_str(), keyCode);
485 ATRACE_NAME(message.c_str());
486 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800487 if (DEBUG_TRANSPORT_ACTIONS) {
488 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
489 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
490 "downTime=%" PRId64 ", eventTime=%" PRId64,
491 mChannel->getName().c_str(), seq, deviceId, source, action, flags, keyCode, scanCode,
492 metaState, repeatCount, downTime, eventTime);
493 }
Jeff Brown5912f952013-07-01 19:10:31 -0700494
495 if (!seq) {
496 ALOGE("Attempted to publish a key event with sequence number 0.");
497 return BAD_VALUE;
498 }
499
500 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700501 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500502 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800503 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700504 msg.body.key.deviceId = deviceId;
505 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100506 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700507 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700508 msg.body.key.action = action;
509 msg.body.key.flags = flags;
510 msg.body.key.keyCode = keyCode;
511 msg.body.key.scanCode = scanCode;
512 msg.body.key.metaState = metaState;
513 msg.body.key.repeatCount = repeatCount;
514 msg.body.key.downTime = downTime;
515 msg.body.key.eventTime = eventTime;
516 return mChannel->sendMessage(&msg);
517}
518
519status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800520 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600521 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
522 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700523 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700524 float yPrecision, float xCursorPosition, float yCursorPosition, int32_t displayWidth,
525 int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount,
526 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000527 if (ATRACE_ENABLED()) {
528 std::string message = StringPrintf(
529 "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")",
530 mChannel->getName().c_str(), action);
531 ATRACE_NAME(message.c_str());
532 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800533 if (DEBUG_TRANSPORT_ACTIONS) {
chaviw9eaa22c2020-07-01 16:21:27 -0700534 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700535 transform.dump(transformString, "transform", " ");
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800536 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
537 "displayId=%" PRId32 ", "
538 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700539 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800540 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
chaviw85b44202020-07-24 11:46:21 -0700541 "pointerCount=%" PRIu32 " \n%s",
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800542 mChannel->getName().c_str(), seq, deviceId, source, displayId, action, actionButton,
543 flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700544 motionClassificationToString(classification), xPrecision, yPrecision, downTime,
545 eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800546 }
Jeff Brown5912f952013-07-01 19:10:31 -0700547
548 if (!seq) {
549 ALOGE("Attempted to publish a motion event with sequence number 0.");
550 return BAD_VALUE;
551 }
552
553 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700554 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800555 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700556 return BAD_VALUE;
557 }
558
559 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700560 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500561 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800562 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700563 msg.body.motion.deviceId = deviceId;
564 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700565 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700566 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700567 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100568 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700569 msg.body.motion.flags = flags;
570 msg.body.motion.edgeFlags = edgeFlags;
571 msg.body.motion.metaState = metaState;
572 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800573 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700574 msg.body.motion.dsdx = transform.dsdx();
575 msg.body.motion.dtdx = transform.dtdx();
576 msg.body.motion.dtdy = transform.dtdy();
577 msg.body.motion.dsdy = transform.dsdy();
578 msg.body.motion.tx = transform.tx();
579 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700580 msg.body.motion.xPrecision = xPrecision;
581 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700582 msg.body.motion.xCursorPosition = xCursorPosition;
583 msg.body.motion.yCursorPosition = yCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700584 msg.body.motion.displayWidth = displayWidth;
585 msg.body.motion.displayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700586 msg.body.motion.downTime = downTime;
587 msg.body.motion.eventTime = eventTime;
588 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100589 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700590 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
591 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
592 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700593
Jeff Brown5912f952013-07-01 19:10:31 -0700594 return mChannel->sendMessage(&msg);
595}
596
Garfield Tan1c7bc862020-01-28 13:24:04 -0800597status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus,
598 bool inTouchMode) {
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800599 if (ATRACE_ENABLED()) {
600 std::string message =
601 StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s, inTouchMode=%s)",
602 mChannel->getName().c_str(), toString(hasFocus),
603 toString(inTouchMode));
604 ATRACE_NAME(message.c_str());
605 }
606
607 InputMessage msg;
608 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500609 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800610 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000611 msg.body.focus.hasFocus = hasFocus;
612 msg.body.focus.inTouchMode = inTouchMode;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800613 return mChannel->sendMessage(&msg);
614}
615
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800616status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
617 bool pointerCaptureEnabled) {
618 if (ATRACE_ENABLED()) {
619 std::string message =
620 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
621 mChannel->getName().c_str(), toString(pointerCaptureEnabled));
622 ATRACE_NAME(message.c_str());
623 }
624
625 InputMessage msg;
626 msg.header.type = InputMessage::Type::CAPTURE;
627 msg.header.seq = seq;
628 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000629 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800630 return mChannel->sendMessage(&msg);
631}
632
arthurhung7632c332020-12-30 16:58:01 +0800633status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
634 bool isExiting) {
635 if (ATRACE_ENABLED()) {
636 std::string message =
637 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
638 mChannel->getName().c_str(), x, y, toString(isExiting));
639 ATRACE_NAME(message.c_str());
640 }
641
642 InputMessage msg;
643 msg.header.type = InputMessage::Type::DRAG;
644 msg.header.seq = seq;
645 msg.body.drag.eventId = eventId;
646 msg.body.drag.isExiting = isExiting;
647 msg.body.drag.x = x;
648 msg.body.drag.y = y;
649 return mChannel->sendMessage(&msg);
650}
651
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000652android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800653 if (DEBUG_TRANSPORT_ACTIONS) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000654 ALOGD("channel '%s' publisher ~ %s", mChannel->getName().c_str(), __func__);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800655 }
Jeff Brown5912f952013-07-01 19:10:31 -0700656
657 InputMessage msg;
658 status_t result = mChannel->receiveMessage(&msg);
659 if (result) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000660 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700661 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000662 if (msg.header.type == InputMessage::Type::FINISHED) {
663 return Finished{
664 .seq = msg.header.seq,
665 .handled = msg.body.finished.handled,
666 .consumeTime = msg.body.finished.consumeTime,
667 };
Jeff Brown5912f952013-07-01 19:10:31 -0700668 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000669
670 if (msg.header.type == InputMessage::Type::TIMELINE) {
671 return Timeline{
672 .inputEventId = msg.body.timeline.eventId,
673 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
674 };
675 }
676
677 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
678 mChannel->getName().c_str(), NamedEnum::string(msg.header.type).c_str());
679 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700680}
681
682// --- InputConsumer ---
683
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500684InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
685 : mResampleTouch(isTouchResamplingEnabled()), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700686
687InputConsumer::~InputConsumer() {
688}
689
690bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600691 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700692}
693
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800694status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
695 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800696 if (DEBUG_TRANSPORT_ACTIONS) {
697 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
698 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
699 }
Jeff Brown5912f952013-07-01 19:10:31 -0700700
701 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700702 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700703
704 // Fetch the next input message.
705 // Loop until an event can be returned or no additional events are received.
706 while (!*outEvent) {
707 if (mMsgDeferred) {
708 // mMsg contains a valid input message from the previous call to consume
709 // that has not yet been processed.
710 mMsgDeferred = false;
711 } else {
712 // Receive a fresh message.
713 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000714 if (result == OK) {
715 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
716 }
Jeff Brown5912f952013-07-01 19:10:31 -0700717 if (result) {
718 // Consume the next batched event unless batches are being held for later.
719 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800720 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700721 if (*outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800722 if (DEBUG_TRANSPORT_ACTIONS) {
723 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
724 mChannel->getName().c_str(), *outSeq);
725 }
Jeff Brown5912f952013-07-01 19:10:31 -0700726 break;
727 }
728 }
729 return result;
730 }
731 }
732
733 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700734 case InputMessage::Type::KEY: {
735 KeyEvent* keyEvent = factory->createKeyEvent();
736 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700737
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700738 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500739 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700740 *outEvent = keyEvent;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800741 if (DEBUG_TRANSPORT_ACTIONS) {
742 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
743 mChannel->getName().c_str(), *outSeq);
744 }
Jeff Brown5912f952013-07-01 19:10:31 -0700745 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700746 }
Jeff Brown5912f952013-07-01 19:10:31 -0700747
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700748 case InputMessage::Type::MOTION: {
749 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
750 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500751 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700752 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500753 batch.samples.push_back(mMsg);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800754 if (DEBUG_TRANSPORT_ACTIONS) {
755 ALOGD("channel '%s' consumer ~ appended to batch event",
756 mChannel->getName().c_str());
757 }
Jeff Brown5912f952013-07-01 19:10:31 -0700758 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700759 } else if (isPointerEvent(mMsg.body.motion.source) &&
760 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
761 // No need to process events that we are going to cancel anyways
762 const size_t count = batch.samples.size();
763 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500764 const InputMessage& msg = batch.samples[i];
765 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700766 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500767 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
768 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700769 } else {
770 // We cannot append to the batch in progress, so we need to consume
771 // the previous batch right now and defer the new message until later.
772 mMsgDeferred = true;
773 status_t result = consumeSamples(factory, batch, batch.samples.size(),
774 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500775 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700776 if (result) {
777 return result;
778 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800779 if (DEBUG_TRANSPORT_ACTIONS) {
780 ALOGD("channel '%s' consumer ~ consumed batch event and "
781 "deferred current event, seq=%u",
782 mChannel->getName().c_str(), *outSeq);
783 }
Jeff Brown5912f952013-07-01 19:10:31 -0700784 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700785 }
Jeff Brown5912f952013-07-01 19:10:31 -0700786 }
Jeff Brown5912f952013-07-01 19:10:31 -0700787
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800788 // Start a new batch if needed.
789 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
790 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500791 Batch batch;
792 batch.samples.push_back(mMsg);
793 mBatches.push_back(batch);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800794 if (DEBUG_TRANSPORT_ACTIONS) {
795 ALOGD("channel '%s' consumer ~ started batch event",
796 mChannel->getName().c_str());
797 }
798 break;
799 }
Jeff Brown5912f952013-07-01 19:10:31 -0700800
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800801 MotionEvent* motionEvent = factory->createMotionEvent();
802 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700803
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800804 updateTouchState(mMsg);
805 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500806 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800807 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800808
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800809 if (DEBUG_TRANSPORT_ACTIONS) {
810 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
811 mChannel->getName().c_str(), *outSeq);
812 }
813 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700814 }
Jeff Brown5912f952013-07-01 19:10:31 -0700815
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000816 case InputMessage::Type::FINISHED:
817 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000818 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
819 "InputConsumer!",
820 NamedEnum::string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800821 break;
822 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800823
824 case InputMessage::Type::FOCUS: {
825 FocusEvent* focusEvent = factory->createFocusEvent();
826 if (!focusEvent) return NO_MEMORY;
827
828 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500829 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800830 *outEvent = focusEvent;
831 break;
832 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800833
834 case InputMessage::Type::CAPTURE: {
835 CaptureEvent* captureEvent = factory->createCaptureEvent();
836 if (!captureEvent) return NO_MEMORY;
837
838 initializeCaptureEvent(captureEvent, &mMsg);
839 *outSeq = mMsg.header.seq;
840 *outEvent = captureEvent;
841 break;
842 }
arthurhung7632c332020-12-30 16:58:01 +0800843
844 case InputMessage::Type::DRAG: {
845 DragEvent* dragEvent = factory->createDragEvent();
846 if (!dragEvent) return NO_MEMORY;
847
848 initializeDragEvent(dragEvent, &mMsg);
849 *outSeq = mMsg.header.seq;
850 *outEvent = dragEvent;
851 break;
852 }
Jeff Brown5912f952013-07-01 19:10:31 -0700853 }
854 }
855 return OK;
856}
857
858status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800859 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700860 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700861 for (size_t i = mBatches.size(); i > 0; ) {
862 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500863 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -0700864 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800865 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500866 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -0700867 return result;
868 }
869
Michael Wright32232172013-10-21 12:05:22 -0700870 nsecs_t sampleTime = frameTime;
871 if (mResampleTouch) {
872 sampleTime -= RESAMPLE_LATENCY;
873 }
Jeff Brown5912f952013-07-01 19:10:31 -0700874 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
875 if (split < 0) {
876 continue;
877 }
878
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800879 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700880 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500881 if (batch.samples.empty()) {
882 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700883 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700884 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500885 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -0700886 }
Michael Wright32232172013-10-21 12:05:22 -0700887 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700888 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
889 }
890 return result;
891 }
892
893 return WOULD_BLOCK;
894}
895
896status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800897 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700898 MotionEvent* motionEvent = factory->createMotionEvent();
899 if (! motionEvent) return NO_MEMORY;
900
901 uint32_t chain = 0;
902 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500903 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100904 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700905 if (i) {
906 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500907 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700908 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500909 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -0700910 addSample(motionEvent, &msg);
911 } else {
912 initializeMotionEvent(motionEvent, &msg);
913 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500914 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700915 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500916 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -0700917
918 *outSeq = chain;
919 *outEvent = motionEvent;
920 return OK;
921}
922
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100923void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800924 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700925 return;
926 }
927
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100928 int32_t deviceId = msg.body.motion.deviceId;
929 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -0700930
931 // Update the touch state history to incorporate the new input message.
932 // If the message is in the past relative to the most recently produced resampled
933 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100934 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -0700935 case AMOTION_EVENT_ACTION_DOWN: {
936 ssize_t index = findTouchState(deviceId, source);
937 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500938 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -0700939 index = mTouchStates.size() - 1;
940 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500941 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700942 touchState.initialize(deviceId, source);
943 touchState.addHistory(msg);
944 break;
945 }
946
947 case AMOTION_EVENT_ACTION_MOVE: {
948 ssize_t index = findTouchState(deviceId, source);
949 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500950 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700951 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800952 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700953 }
954 break;
955 }
956
957 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
958 ssize_t index = findTouchState(deviceId, source);
959 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500960 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100961 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700962 rewriteMessage(touchState, msg);
963 }
964 break;
965 }
966
967 case AMOTION_EVENT_ACTION_POINTER_UP: {
968 ssize_t index = findTouchState(deviceId, source);
969 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500970 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700971 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100972 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700973 }
974 break;
975 }
976
977 case AMOTION_EVENT_ACTION_SCROLL: {
978 ssize_t index = findTouchState(deviceId, source);
979 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500980 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700981 rewriteMessage(touchState, msg);
982 }
983 break;
984 }
985
986 case AMOTION_EVENT_ACTION_UP:
987 case AMOTION_EVENT_ACTION_CANCEL: {
988 ssize_t index = findTouchState(deviceId, source);
989 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500990 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700991 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500992 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -0700993 }
994 break;
995 }
996 }
997}
998
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800999/**
1000 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1001 *
1002 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1003 * is in the past relative to msg and the past two events do not contain identical coordinates),
1004 * then invalidate the lastResample data for that pointer.
1005 * If the two past events have identical coordinates, then lastResample data for that pointer will
1006 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1007 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1008 * not equal to x0 is received.
1009 */
1010void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001011 nsecs_t eventTime = msg.body.motion.eventTime;
1012 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1013 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001014 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001015 if (eventTime < state.lastResample.eventTime ||
1016 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001017 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1018 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001019#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001020 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
1021 resampleCoords.getX(), resampleCoords.getY(),
1022 msgCoords.getX(), msgCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001023#endif
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001024 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1025 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
1026 } else {
1027 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001028 }
Jeff Brown5912f952013-07-01 19:10:31 -07001029 }
1030 }
1031}
1032
1033void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1034 const InputMessage* next) {
1035 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001036 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001037 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1038 return;
1039 }
1040
1041 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1042 if (index < 0) {
1043#if DEBUG_RESAMPLING
1044 ALOGD("Not resampled, no touch state for device.");
1045#endif
1046 return;
1047 }
1048
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001049 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001050 if (touchState.historySize < 1) {
1051#if DEBUG_RESAMPLING
1052 ALOGD("Not resampled, no history for device.");
1053#endif
1054 return;
1055 }
1056
1057 // Ensure that the current sample has all of the pointers that need to be reported.
1058 const History* current = touchState.getHistory(0);
1059 size_t pointerCount = event->getPointerCount();
1060 for (size_t i = 0; i < pointerCount; i++) {
1061 uint32_t id = event->getPointerId(i);
1062 if (!current->idBits.hasBit(id)) {
1063#if DEBUG_RESAMPLING
1064 ALOGD("Not resampled, missing id %d", id);
1065#endif
1066 return;
1067 }
1068 }
1069
1070 // Find the data to use for resampling.
1071 const History* other;
1072 History future;
1073 float alpha;
1074 if (next) {
1075 // Interpolate between current sample and future sample.
1076 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001077 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001078 other = &future;
1079 nsecs_t delta = future.eventTime - current->eventTime;
1080 if (delta < RESAMPLE_MIN_DELTA) {
1081#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001082 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001083#endif
1084 return;
1085 }
1086 alpha = float(sampleTime - current->eventTime) / delta;
1087 } else if (touchState.historySize >= 2) {
1088 // Extrapolate future sample using current sample and past sample.
1089 // So other->eventTime <= current->eventTime <= sampleTime.
1090 other = touchState.getHistory(1);
1091 nsecs_t delta = current->eventTime - other->eventTime;
1092 if (delta < RESAMPLE_MIN_DELTA) {
1093#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001094 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001095#endif
1096 return;
1097 } else if (delta > RESAMPLE_MAX_DELTA) {
1098#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001099 ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001100#endif
1101 return;
1102 }
1103 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1104 if (sampleTime > maxPredict) {
1105#if DEBUG_RESAMPLING
1106 ALOGD("Sample time is too far in the future, adjusting prediction "
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001107 "from %" PRId64 " to %" PRId64 " ns.",
Jeff Brown5912f952013-07-01 19:10:31 -07001108 sampleTime - current->eventTime, maxPredict - current->eventTime);
1109#endif
1110 sampleTime = maxPredict;
1111 }
1112 alpha = float(current->eventTime - sampleTime) / delta;
1113 } else {
1114#if DEBUG_RESAMPLING
1115 ALOGD("Not resampled, insufficient data.");
1116#endif
1117 return;
1118 }
1119
1120 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001121 History oldLastResample;
1122 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001123 touchState.lastResample.eventTime = sampleTime;
1124 touchState.lastResample.idBits.clear();
1125 for (size_t i = 0; i < pointerCount; i++) {
1126 uint32_t id = event->getPointerId(i);
1127 touchState.lastResample.idToIndex[id] = i;
1128 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001129 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1130 // We maintain the previously resampled value for this pointer (stored in
1131 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1132 // This way we don't introduce artificial jitter when pointers haven't actually moved.
1133
1134 // We know here that the coordinates for the pointer haven't changed because we
1135 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1136 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1137 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1138 continue;
1139 }
1140
Jeff Brown5912f952013-07-01 19:10:31 -07001141 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1142 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001143 resampledCoords.copyFrom(currentCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001144 if (other->idBits.hasBit(id)
1145 && shouldResampleTool(event->getToolType(i))) {
1146 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001147 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
1148 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
1149 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
1150 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
1151#if DEBUG_RESAMPLING
1152 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1153 "other (%0.3f, %0.3f), alpha %0.3f",
1154 id, resampledCoords.getX(), resampledCoords.getY(),
1155 currentCoords.getX(), currentCoords.getY(),
1156 otherCoords.getX(), otherCoords.getY(),
1157 alpha);
1158#endif
1159 } else {
Jeff Brown5912f952013-07-01 19:10:31 -07001160#if DEBUG_RESAMPLING
1161 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
1162 id, resampledCoords.getX(), resampledCoords.getY(),
1163 currentCoords.getX(), currentCoords.getY());
1164#endif
1165 }
1166 }
1167
1168 event->addSample(sampleTime, touchState.lastResample.pointers);
1169}
1170
1171bool InputConsumer::shouldResampleTool(int32_t toolType) {
1172 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1173 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1174}
1175
1176status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08001177 if (DEBUG_TRANSPORT_ACTIONS) {
1178 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1179 mChannel->getName().c_str(), seq, toString(handled));
1180 }
Jeff Brown5912f952013-07-01 19:10:31 -07001181
1182 if (!seq) {
1183 ALOGE("Attempted to send a finished signal with sequence number 0.");
1184 return BAD_VALUE;
1185 }
1186
1187 // Send finished signals for the batch sequence chain first.
1188 size_t seqChainCount = mSeqChains.size();
1189 if (seqChainCount) {
1190 uint32_t currentSeq = seq;
1191 uint32_t chainSeqs[seqChainCount];
1192 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001193 for (size_t i = seqChainCount; i > 0; ) {
1194 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001195 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001196 if (seqChain.seq == currentSeq) {
1197 currentSeq = seqChain.chain;
1198 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001199 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001200 }
1201 }
1202 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001203 while (!status && chainIndex > 0) {
1204 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001205 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1206 }
1207 if (status) {
1208 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001209 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001210 SeqChain seqChain;
1211 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1212 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001213 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001214 if (!chainIndex) break;
1215 chainIndex--;
1216 }
Jeff Brown5912f952013-07-01 19:10:31 -07001217 return status;
1218 }
1219 }
1220
1221 // Send finished signal for the last message in the batch.
1222 return sendUnchainedFinishedSignal(seq, handled);
1223}
1224
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001225status_t InputConsumer::sendTimeline(int32_t inputEventId,
1226 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
1227 if (DEBUG_TRANSPORT_ACTIONS) {
1228 ALOGD("channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1229 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1230 mChannel->getName().c_str(), inputEventId,
1231 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1232 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
1233 }
1234
1235 InputMessage msg;
1236 msg.header.type = InputMessage::Type::TIMELINE;
1237 msg.header.seq = 0;
1238 msg.body.timeline.eventId = inputEventId;
1239 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1240 return mChannel->sendMessage(&msg);
1241}
1242
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001243nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1244 auto it = mConsumeTimes.find(seq);
1245 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1246 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1247 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1248 seq);
1249 return it->second;
1250}
1251
1252void InputConsumer::popConsumeTime(uint32_t seq) {
1253 mConsumeTimes.erase(seq);
1254}
1255
Jeff Brown5912f952013-07-01 19:10:31 -07001256status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1257 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001258 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001259 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001260 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001261 msg.body.finished.consumeTime = getConsumeTime(seq);
1262 status_t result = mChannel->sendMessage(&msg);
1263 if (result == OK) {
1264 // Remove the consume time if the socket write succeeded. We will not need to ack this
1265 // message anymore. If the socket write did not succeed, we will try again and will still
1266 // need consume time.
1267 popConsumeTime(seq);
1268 }
1269 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001270}
1271
1272bool InputConsumer::hasDeferredEvent() const {
1273 return mMsgDeferred;
1274}
1275
1276bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001277 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001278}
1279
Arthur Hungc7812be2020-02-27 22:40:27 +08001280int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001281 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001282 return AINPUT_SOURCE_CLASS_NONE;
1283 }
1284
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001285 const Batch& batch = mBatches[0];
1286 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001287 return head.body.motion.source;
1288}
1289
Jeff Brown5912f952013-07-01 19:10:31 -07001290ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1291 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001292 const Batch& batch = mBatches[i];
1293 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001294 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1295 return i;
1296 }
1297 }
1298 return -1;
1299}
1300
1301ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1302 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001303 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001304 if (touchState.deviceId == deviceId && touchState.source == source) {
1305 return i;
1306 }
1307 }
1308 return -1;
1309}
1310
1311void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001312 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001313 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1314 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1315 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1316 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001317}
1318
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001319void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001320 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus,
1321 msg->body.focus.inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001322}
1323
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001324void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001325 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001326}
1327
arthurhung7632c332020-12-30 16:58:01 +08001328void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1329 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1330 msg->body.drag.isExiting);
1331}
1332
Jeff Brown5912f952013-07-01 19:10:31 -07001333void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001334 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001335 PointerProperties pointerProperties[pointerCount];
1336 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001337 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001338 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1339 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1340 }
1341
chaviw9eaa22c2020-07-01 16:21:27 -07001342 ui::Transform transform;
1343 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1344 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001345 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1346 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1347 msg->body.motion.actionButton, msg->body.motion.flags,
1348 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001349 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1350 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1351 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Evan Rosky84f07f02021-04-16 10:42:42 -07001352 msg->body.motion.displayWidth, msg->body.motion.displayHeight,
chaviw9eaa22c2020-07-01 16:21:27 -07001353 msg->body.motion.downTime, msg->body.motion.eventTime, pointerCount,
1354 pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001355}
1356
1357void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001358 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001359 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001360 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001361 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1362 }
1363
1364 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1365 event->addSample(msg->body.motion.eventTime, pointerCoords);
1366}
1367
1368bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001369 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001370 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001371 if (head.body.motion.pointerCount != pointerCount
1372 || head.body.motion.action != msg->body.motion.action) {
1373 return false;
1374 }
1375 for (size_t i = 0; i < pointerCount; i++) {
1376 if (head.body.motion.pointers[i].properties
1377 != msg->body.motion.pointers[i].properties) {
1378 return false;
1379 }
1380 }
1381 return true;
1382}
1383
1384ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1385 size_t numSamples = batch.samples.size();
1386 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001387 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001388 index += 1;
1389 }
1390 return ssize_t(index) - 1;
1391}
1392
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001393std::string InputConsumer::dump() const {
1394 std::string out;
1395 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1396 out = out + "mChannel = " + mChannel->getName() + "\n";
1397 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1398 if (mMsgDeferred) {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +00001399 out = out + "mMsg : " + NamedEnum::string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001400 }
1401 out += "Batches:\n";
1402 for (const Batch& batch : mBatches) {
1403 out += " Batch:\n";
1404 for (const InputMessage& msg : batch.samples) {
1405 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Siarhei Vishniakou7766c032021-03-02 20:32:20 +00001406 NamedEnum::string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001407 switch (msg.header.type) {
1408 case InputMessage::Type::KEY: {
1409 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1410 KeyEvent::actionToString(
1411 msg.body.key.action),
1412 msg.body.key.keyCode);
1413 break;
1414 }
1415 case InputMessage::Type::MOTION: {
1416 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1417 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1418 const float x = msg.body.motion.pointers[i].coords.getX();
1419 const float y = msg.body.motion.pointers[i].coords.getY();
1420 out += android::base::StringPrintf("\n Pointer %" PRIu32
1421 " : x=%.1f y=%.1f",
1422 i, x, y);
1423 }
1424 break;
1425 }
1426 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001427 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1428 toString(msg.body.finished.handled),
1429 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001430 break;
1431 }
1432 case InputMessage::Type::FOCUS: {
1433 out += android::base::StringPrintf("hasFocus=%s inTouchMode=%s",
1434 toString(msg.body.focus.hasFocus),
1435 toString(msg.body.focus.inTouchMode));
1436 break;
1437 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001438 case InputMessage::Type::CAPTURE: {
1439 out += android::base::StringPrintf("hasCapture=%s",
1440 toString(msg.body.capture
1441 .pointerCaptureEnabled));
1442 break;
1443 }
arthurhung7632c332020-12-30 16:58:01 +08001444 case InputMessage::Type::DRAG: {
1445 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1446 msg.body.drag.x, msg.body.drag.y,
1447 toString(msg.body.drag.isExiting));
1448 break;
1449 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001450 case InputMessage::Type::TIMELINE: {
1451 const nsecs_t gpuCompletedTime =
1452 msg.body.timeline
1453 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1454 const nsecs_t presentTime =
1455 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1456 out += android::base::StringPrintf("inputEventId=%" PRId32
1457 ", gpuCompletedTime=%" PRId64
1458 ", presentTime=%" PRId64,
1459 msg.body.timeline.eventId, gpuCompletedTime,
1460 presentTime);
1461 break;
1462 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001463 }
1464 out += "\n";
1465 }
1466 }
1467 if (mBatches.empty()) {
1468 out += " <empty>\n";
1469 }
1470 out += "mSeqChains:\n";
1471 for (const SeqChain& chain : mSeqChains) {
1472 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1473 chain.chain);
1474 }
1475 if (mSeqChains.empty()) {
1476 out += " <empty>\n";
1477 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001478 out += "mConsumeTimes:\n";
1479 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1480 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1481 consumeTime);
1482 }
1483 if (mConsumeTimes.empty()) {
1484 out += " <empty>\n";
1485 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001486 return out;
1487}
1488
Jeff Brown5912f952013-07-01 19:10:31 -07001489} // namespace android