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