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