blob: d1cd50ccff69da9d6ab1bf536ad7cc20ee3f2403 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// Provides a shared memory transport for input events.
5//
6#define LOG_TAG "InputTransport"
7
8//#define LOG_NDEBUG 0
9
10// Log debug messages about channel messages (send message, receive message)
11#define DEBUG_CHANNEL_MESSAGES 0
12
13// Log debug messages whenever InputChannel objects are created/destroyed
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070014static constexpr bool DEBUG_CHANNEL_LIFECYCLE = false;
Jeff Brown5912f952013-07-01 19:10:31 -070015
16// Log debug messages about transport actions
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -080017static constexpr bool DEBUG_TRANSPORT_ACTIONS = false;
Jeff Brown5912f952013-07-01 19:10:31 -070018
19// Log debug messages about touch event resampling
20#define DEBUG_RESAMPLING 0
21
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <errno.h>
23#include <fcntl.h>
Michael Wrightd0a4a622014-06-09 19:03:32 -070024#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <math.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <sys/socket.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027#include <sys/types.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <unistd.h>
29
Michael Wright3dd60e22019-03-27 22:06:44 +000030#include <android-base/stringprintf.h>
31#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <cutils/properties.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080033#include <ftl/enum.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000035#include <utils/Trace.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070036
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <input/InputTransport.h>
38
Michael Wright3dd60e22019-03-27 22:06:44 +000039using android::base::StringPrintf;
40
Jeff Brown5912f952013-07-01 19:10:31 -070041namespace android {
42
43// Socket buffer size. The default is typically about 128KB, which is much larger than
44// we really need. So we make it smaller. It just needs to be big enough to hold
45// a few dozen large multi-finger motion events in the case where an application gets
46// behind processing touches.
47static const size_t SOCKET_BUFFER_SIZE = 32 * 1024;
48
49// Nanoseconds per milliseconds.
50static const nsecs_t NANOS_PER_MS = 1000000;
51
52// Latency added during resampling. A few milliseconds doesn't hurt much but
53// reduces the impact of mispredicted touch positions.
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -080054const std::chrono::duration RESAMPLE_LATENCY = 5ms;
Jeff Brown5912f952013-07-01 19:10:31 -070055
56// Minimum time difference between consecutive samples before attempting to resample.
57static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
58
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -070059// Maximum time difference between consecutive samples before attempting to resample
60// by extrapolation.
61static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS;
62
Jeff Brown5912f952013-07-01 19:10:31 -070063// Maximum time to predict forward from the last known state, to avoid predicting too
64// far into the future. This time is further bounded by 50% of the last time delta.
65static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
66
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -060067/**
68 * System property for enabling / disabling touch resampling.
69 * Resampling extrapolates / interpolates the reported touch event coordinates to better
70 * align them to the VSYNC signal, thus resulting in smoother scrolling performance.
71 * Resampling is not needed (and should be disabled) on hardware that already
72 * has touch events triggered by VSYNC.
73 * Set to "1" to enable resampling (default).
74 * Set to "0" to disable resampling.
75 * Resampling is enabled by default.
76 */
77static const char* PROPERTY_RESAMPLING_ENABLED = "ro.input.resampling";
78
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -080079/**
80 * Crash if the events that are getting sent to the InputPublisher are inconsistent.
81 * Enable this via "adb shell setprop log.tag.InputTransportVerifyEvents DEBUG"
82 */
83static bool verifyEvents() {
84 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "VerifyEvents", ANDROID_LOG_INFO);
85}
86
Jeff Brown5912f952013-07-01 19:10:31 -070087template<typename T>
88inline static T min(const T& a, const T& b) {
89 return a < b ? a : b;
90}
91
92inline static float lerp(float a, float b, float alpha) {
93 return a + alpha * (b - a);
94}
95
Siarhei Vishniakou128eab12019-05-23 10:25:59 +080096inline static bool isPointerEvent(int32_t source) {
97 return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER;
98}
99
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800100inline static const char* toString(bool value) {
101 return value ? "true" : "false";
102}
103
Jeff Brown5912f952013-07-01 19:10:31 -0700104// --- InputMessage ---
105
106bool InputMessage::isValid(size_t actualSize) const {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000107 if (size() != actualSize) {
108 ALOGE("Received message of incorrect size %zu (expected %zu)", actualSize, size());
109 return false;
110 }
111
112 switch (header.type) {
113 case Type::KEY:
114 return true;
115 case Type::MOTION: {
116 const bool valid =
117 body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
118 if (!valid) {
119 ALOGE("Received invalid MOTION: pointerCount = %" PRIu32, body.motion.pointerCount);
120 }
121 return valid;
122 }
123 case Type::FINISHED:
124 case Type::FOCUS:
125 case Type::CAPTURE:
126 case Type::DRAG:
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700127 case Type::TOUCH_MODE:
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000128 return true;
129 case Type::TIMELINE: {
130 const nsecs_t gpuCompletedTime =
131 body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
132 const nsecs_t presentTime =
133 body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
134 const bool valid = presentTime > gpuCompletedTime;
135 if (!valid) {
136 ALOGE("Received invalid TIMELINE: gpuCompletedTime = %" PRId64
137 " presentTime = %" PRId64,
138 gpuCompletedTime, presentTime);
139 }
140 return valid;
Jeff Brown5912f952013-07-01 19:10:31 -0700141 }
142 }
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000143 ALOGE("Invalid message type: %" PRIu32, header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700144 return false;
145}
146
147size_t InputMessage::size() const {
148 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700149 case Type::KEY:
150 return sizeof(Header) + body.key.size();
151 case Type::MOTION:
152 return sizeof(Header) + body.motion.size();
153 case Type::FINISHED:
154 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800155 case Type::FOCUS:
156 return sizeof(Header) + body.focus.size();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800157 case Type::CAPTURE:
158 return sizeof(Header) + body.capture.size();
arthurhung7632c332020-12-30 16:58:01 +0800159 case Type::DRAG:
160 return sizeof(Header) + body.drag.size();
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000161 case Type::TIMELINE:
162 return sizeof(Header) + body.timeline.size();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700163 case Type::TOUCH_MODE:
164 return sizeof(Header) + body.touchMode.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700165 }
166 return sizeof(Header);
167}
168
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800169/**
170 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
171 * memory to zero, then only copy the valid bytes on a per-field basis.
172 */
173void InputMessage::getSanitizedCopy(InputMessage* msg) const {
174 memset(msg, 0, sizeof(*msg));
175
176 // Write the header
177 msg->header.type = header.type;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500178 msg->header.seq = header.seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800179
180 // Write the body
181 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700182 case InputMessage::Type::KEY: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800183 // int32_t eventId
184 msg->body.key.eventId = body.key.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800185 // nsecs_t eventTime
186 msg->body.key.eventTime = body.key.eventTime;
187 // int32_t deviceId
188 msg->body.key.deviceId = body.key.deviceId;
189 // int32_t source
190 msg->body.key.source = body.key.source;
191 // int32_t displayId
192 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600193 // std::array<uint8_t, 32> hmac
194 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800195 // int32_t action
196 msg->body.key.action = body.key.action;
197 // int32_t flags
198 msg->body.key.flags = body.key.flags;
199 // int32_t keyCode
200 msg->body.key.keyCode = body.key.keyCode;
201 // int32_t scanCode
202 msg->body.key.scanCode = body.key.scanCode;
203 // int32_t metaState
204 msg->body.key.metaState = body.key.metaState;
205 // int32_t repeatCount
206 msg->body.key.repeatCount = body.key.repeatCount;
207 // nsecs_t downTime
208 msg->body.key.downTime = body.key.downTime;
209 break;
210 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700211 case InputMessage::Type::MOTION: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800212 // int32_t eventId
213 msg->body.motion.eventId = body.motion.eventId;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700214 // uint32_t pointerCount
215 msg->body.motion.pointerCount = body.motion.pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800216 // nsecs_t eventTime
217 msg->body.motion.eventTime = body.motion.eventTime;
218 // int32_t deviceId
219 msg->body.motion.deviceId = body.motion.deviceId;
220 // int32_t source
221 msg->body.motion.source = body.motion.source;
222 // int32_t displayId
223 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600224 // std::array<uint8_t, 32> hmac
225 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800226 // int32_t action
227 msg->body.motion.action = body.motion.action;
228 // int32_t actionButton
229 msg->body.motion.actionButton = body.motion.actionButton;
230 // int32_t flags
231 msg->body.motion.flags = body.motion.flags;
232 // int32_t metaState
233 msg->body.motion.metaState = body.motion.metaState;
234 // int32_t buttonState
235 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800236 // MotionClassification classification
237 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800238 // int32_t edgeFlags
239 msg->body.motion.edgeFlags = body.motion.edgeFlags;
240 // nsecs_t downTime
241 msg->body.motion.downTime = body.motion.downTime;
chaviw9eaa22c2020-07-01 16:21:27 -0700242
243 msg->body.motion.dsdx = body.motion.dsdx;
244 msg->body.motion.dtdx = body.motion.dtdx;
245 msg->body.motion.dtdy = body.motion.dtdy;
246 msg->body.motion.dsdy = body.motion.dsdy;
247 msg->body.motion.tx = body.motion.tx;
248 msg->body.motion.ty = body.motion.ty;
249
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800250 // float xPrecision
251 msg->body.motion.xPrecision = body.motion.xPrecision;
252 // float yPrecision
253 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700254 // float xCursorPosition
255 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
256 // float yCursorPosition
257 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700258
259 msg->body.motion.dsdxRaw = body.motion.dsdxRaw;
260 msg->body.motion.dtdxRaw = body.motion.dtdxRaw;
261 msg->body.motion.dtdyRaw = body.motion.dtdyRaw;
262 msg->body.motion.dsdyRaw = body.motion.dsdyRaw;
263 msg->body.motion.txRaw = body.motion.txRaw;
264 msg->body.motion.tyRaw = body.motion.tyRaw;
265
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800266 //struct Pointer pointers[MAX_POINTERS]
267 for (size_t i = 0; i < body.motion.pointerCount; i++) {
268 // PointerProperties properties
269 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
270 msg->body.motion.pointers[i].properties.toolType =
271 body.motion.pointers[i].properties.toolType,
272 // PointerCoords coords
273 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
274 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
275 memcpy(&msg->body.motion.pointers[i].coords.values[0],
276 &body.motion.pointers[i].coords.values[0],
277 count * (sizeof(body.motion.pointers[i].coords.values[0])));
Philip Quinnafb31282022-12-20 18:17:55 -0800278 msg->body.motion.pointers[i].coords.isResampled =
279 body.motion.pointers[i].coords.isResampled;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800280 }
281 break;
282 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700283 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800284 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000285 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800286 break;
287 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800288 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800289 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800290 msg->body.focus.hasFocus = body.focus.hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800291 break;
292 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800293 case InputMessage::Type::CAPTURE: {
294 msg->body.capture.eventId = body.capture.eventId;
295 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
296 break;
297 }
arthurhung7632c332020-12-30 16:58:01 +0800298 case InputMessage::Type::DRAG: {
299 msg->body.drag.eventId = body.drag.eventId;
300 msg->body.drag.x = body.drag.x;
301 msg->body.drag.y = body.drag.y;
302 msg->body.drag.isExiting = body.drag.isExiting;
303 break;
304 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000305 case InputMessage::Type::TIMELINE: {
306 msg->body.timeline.eventId = body.timeline.eventId;
307 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
308 break;
309 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700310 case InputMessage::Type::TOUCH_MODE: {
311 msg->body.touchMode.eventId = body.touchMode.eventId;
312 msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode;
313 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800314 }
315}
Jeff Brown5912f952013-07-01 19:10:31 -0700316
317// --- InputChannel ---
318
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500319std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500320 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700321 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
322 if (result != 0) {
323 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
324 strerror(errno));
325 return nullptr;
326 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500327 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500328 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700329}
330
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500331InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token)
332 : mName(std::move(name)), mFd(std::move(fd)), mToken(std::move(token)) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700333 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500334 ALOGD("Input channel constructed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700335 }
Jeff Brown5912f952013-07-01 19:10:31 -0700336}
337
338InputChannel::~InputChannel() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700339 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500340 ALOGD("Input channel destroyed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700341 }
Robert Carr3720ed02018-08-08 16:08:27 -0700342}
343
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800344status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500345 std::unique_ptr<InputChannel>& outServerChannel,
346 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700347 int sockets[2];
348 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
349 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000350 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
351 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500352 outServerChannel.reset();
353 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700354 return result;
355 }
356
357 int bufferSize = SOCKET_BUFFER_SIZE;
358 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
359 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
360 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
361 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
362
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700363 sp<IBinder> token = new BBinder();
364
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700365 std::string serverChannelName = name + " (server)";
366 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700367 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700368
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700369 std::string clientChannelName = name + " (client)";
370 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700371 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700372 return OK;
373}
374
375status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800376 const size_t msgLength = msg->size();
377 InputMessage cleanMsg;
378 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700379 ssize_t nWrite;
380 do {
Chris Ye0783e992020-06-02 21:34:49 -0700381 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700382 } while (nWrite == -1 && errno == EINTR);
383
384 if (nWrite < 0) {
385 int error = errno;
386#if DEBUG_CHANNEL_MESSAGES
chaviw81e2bb92019-12-18 15:03:51 -0800387 ALOGD("channel '%s' ~ error sending message of type %d, %s", mName.c_str(),
388 msg->header.type, strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700389#endif
390 if (error == EAGAIN || error == EWOULDBLOCK) {
391 return WOULD_BLOCK;
392 }
393 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
394 return DEAD_OBJECT;
395 }
396 return -error;
397 }
398
399 if (size_t(nWrite) != msgLength) {
400#if DEBUG_CHANNEL_MESSAGES
401 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800402 mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700403#endif
404 return DEAD_OBJECT;
405 }
406
407#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800408 ALOGD("channel '%s' ~ sent message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700409#endif
410 return OK;
411}
412
413status_t InputChannel::receiveMessage(InputMessage* msg) {
414 ssize_t nRead;
415 do {
Chris Ye0783e992020-06-02 21:34:49 -0700416 nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700417 } while (nRead == -1 && errno == EINTR);
418
419 if (nRead < 0) {
420 int error = errno;
421#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800422 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700423#endif
424 if (error == EAGAIN || error == EWOULDBLOCK) {
425 return WOULD_BLOCK;
426 }
427 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
428 return DEAD_OBJECT;
429 }
430 return -error;
431 }
432
433 if (nRead == 0) { // check for EOF
434#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800435 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700436#endif
437 return DEAD_OBJECT;
438 }
439
440 if (!msg->isValid(nRead)) {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000441 ALOGE("channel '%s' ~ received invalid message of size %zd", mName.c_str(), nRead);
Jeff Brown5912f952013-07-01 19:10:31 -0700442 return BAD_VALUE;
443 }
444
445#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800446 ALOGD("channel '%s' ~ received message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700447#endif
448 return OK;
449}
450
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500451std::unique_ptr<InputChannel> InputChannel::dup() const {
Garfield Tan15601662020-09-22 15:32:38 -0700452 base::unique_fd newFd(dupFd());
Chris Ye0783e992020-06-02 21:34:49 -0700453 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700454}
455
Garfield Tan15601662020-09-22 15:32:38 -0700456void InputChannel::copyTo(InputChannel& outChannel) const {
457 outChannel.mName = getName();
458 outChannel.mFd = dupFd();
459 outChannel.mToken = getConnectionToken();
460}
461
Chris Ye0783e992020-06-02 21:34:49 -0700462status_t InputChannel::writeToParcel(android::Parcel* parcel) const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500463 if (parcel == nullptr) {
464 ALOGE("%s: Null parcel", __func__);
465 return BAD_VALUE;
466 }
467 return parcel->writeStrongBinder(mToken)
468 ?: parcel->writeUtf8AsUtf16(mName) ?: parcel->writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700469}
470
Chris Ye0783e992020-06-02 21:34:49 -0700471status_t InputChannel::readFromParcel(const android::Parcel* parcel) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500472 if (parcel == nullptr) {
473 ALOGE("%s: Null parcel", __func__);
474 return BAD_VALUE;
475 }
476 mToken = parcel->readStrongBinder();
477 return parcel->readUtf8FromUtf16(&mName) ?: parcel->readUniqueFileDescriptor(&mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700478}
479
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700480sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500481 return mToken;
Robert Carr803535b2018-08-02 16:38:15 -0700482}
483
Garfield Tan15601662020-09-22 15:32:38 -0700484base::unique_fd InputChannel::dupFd() const {
485 android::base::unique_fd newFd(::dup(getFd()));
486 if (!newFd.ok()) {
487 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd().get(), getName().c_str(),
488 strerror(errno));
489 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
490 // If this process is out of file descriptors, then throwing that might end up exploding
491 // on the other side of a binder call, which isn't really helpful.
492 // Better to just crash here and hope that the FD leak is slow.
493 // Other failures could be client errors, so we still propagate those back to the caller.
494 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
495 getName().c_str());
496 return {};
497 }
498 return newFd;
499}
500
Jeff Brown5912f952013-07-01 19:10:31 -0700501// --- InputPublisher ---
502
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800503InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel)
504 : mChannel(channel), mInputVerifier(channel->getName()) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700505
506InputPublisher::~InputPublisher() {
507}
508
Garfield Tan1c7bc862020-01-28 13:24:04 -0800509status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
510 int32_t source, int32_t displayId,
511 std::array<uint8_t, 32> hmac, int32_t action,
512 int32_t flags, int32_t keyCode, int32_t scanCode,
513 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
514 nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000515 if (ATRACE_ENABLED()) {
516 std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
517 mChannel->getName().c_str(), keyCode);
518 ATRACE_NAME(message.c_str());
519 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800520 if (DEBUG_TRANSPORT_ACTIONS) {
521 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
522 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
523 "downTime=%" PRId64 ", eventTime=%" PRId64,
524 mChannel->getName().c_str(), seq, deviceId, source, action, flags, keyCode, scanCode,
525 metaState, repeatCount, downTime, eventTime);
526 }
Jeff Brown5912f952013-07-01 19:10:31 -0700527
528 if (!seq) {
529 ALOGE("Attempted to publish a key event with sequence number 0.");
530 return BAD_VALUE;
531 }
532
533 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700534 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500535 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800536 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700537 msg.body.key.deviceId = deviceId;
538 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100539 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700540 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700541 msg.body.key.action = action;
542 msg.body.key.flags = flags;
543 msg.body.key.keyCode = keyCode;
544 msg.body.key.scanCode = scanCode;
545 msg.body.key.metaState = metaState;
546 msg.body.key.repeatCount = repeatCount;
547 msg.body.key.downTime = downTime;
548 msg.body.key.eventTime = eventTime;
549 return mChannel->sendMessage(&msg);
550}
551
552status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800553 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600554 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
555 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700556 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700557 float yPrecision, float xCursorPosition, float yCursorPosition,
558 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700559 uint32_t pointerCount, const PointerProperties* pointerProperties,
560 const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000561 if (ATRACE_ENABLED()) {
562 std::string message = StringPrintf(
563 "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")",
564 mChannel->getName().c_str(), action);
565 ATRACE_NAME(message.c_str());
566 }
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800567 if (verifyEvents()) {
568 mInputVerifier.processMovement(deviceId, action, pointerCount, pointerProperties,
569 pointerCoords, flags);
570 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800571 if (DEBUG_TRANSPORT_ACTIONS) {
chaviw9eaa22c2020-07-01 16:21:27 -0700572 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700573 transform.dump(transformString, "transform", " ");
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800574 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
575 "displayId=%" PRId32 ", "
576 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700577 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800578 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
chaviw85b44202020-07-24 11:46:21 -0700579 "pointerCount=%" PRIu32 " \n%s",
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800580 mChannel->getName().c_str(), seq, deviceId, source, displayId, action, actionButton,
581 flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700582 motionClassificationToString(classification), xPrecision, yPrecision, downTime,
583 eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800584 }
Jeff Brown5912f952013-07-01 19:10:31 -0700585
586 if (!seq) {
587 ALOGE("Attempted to publish a motion event with sequence number 0.");
588 return BAD_VALUE;
589 }
590
591 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700592 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800593 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700594 return BAD_VALUE;
595 }
596
597 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700598 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500599 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800600 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700601 msg.body.motion.deviceId = deviceId;
602 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700603 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700604 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700605 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100606 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700607 msg.body.motion.flags = flags;
608 msg.body.motion.edgeFlags = edgeFlags;
609 msg.body.motion.metaState = metaState;
610 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800611 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700612 msg.body.motion.dsdx = transform.dsdx();
613 msg.body.motion.dtdx = transform.dtdx();
614 msg.body.motion.dtdy = transform.dtdy();
615 msg.body.motion.dsdy = transform.dsdy();
616 msg.body.motion.tx = transform.tx();
617 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700618 msg.body.motion.xPrecision = xPrecision;
619 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700620 msg.body.motion.xCursorPosition = xCursorPosition;
621 msg.body.motion.yCursorPosition = yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700622 msg.body.motion.dsdxRaw = rawTransform.dsdx();
623 msg.body.motion.dtdxRaw = rawTransform.dtdx();
624 msg.body.motion.dtdyRaw = rawTransform.dtdy();
625 msg.body.motion.dsdyRaw = rawTransform.dsdy();
626 msg.body.motion.txRaw = rawTransform.tx();
627 msg.body.motion.tyRaw = rawTransform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700628 msg.body.motion.downTime = downTime;
629 msg.body.motion.eventTime = eventTime;
630 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100631 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700632 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
633 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
634 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700635
Jeff Brown5912f952013-07-01 19:10:31 -0700636 return mChannel->sendMessage(&msg);
637}
638
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700639status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800640 if (ATRACE_ENABLED()) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700641 std::string message = StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
642 mChannel->getName().c_str(), toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800643 ATRACE_NAME(message.c_str());
644 }
645
646 InputMessage msg;
647 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500648 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800649 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000650 msg.body.focus.hasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800651 return mChannel->sendMessage(&msg);
652}
653
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800654status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
655 bool pointerCaptureEnabled) {
656 if (ATRACE_ENABLED()) {
657 std::string message =
658 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
659 mChannel->getName().c_str(), toString(pointerCaptureEnabled));
660 ATRACE_NAME(message.c_str());
661 }
662
663 InputMessage msg;
664 msg.header.type = InputMessage::Type::CAPTURE;
665 msg.header.seq = seq;
666 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000667 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800668 return mChannel->sendMessage(&msg);
669}
670
arthurhung7632c332020-12-30 16:58:01 +0800671status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
672 bool isExiting) {
673 if (ATRACE_ENABLED()) {
674 std::string message =
675 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
676 mChannel->getName().c_str(), x, y, toString(isExiting));
677 ATRACE_NAME(message.c_str());
678 }
679
680 InputMessage msg;
681 msg.header.type = InputMessage::Type::DRAG;
682 msg.header.seq = seq;
683 msg.body.drag.eventId = eventId;
684 msg.body.drag.isExiting = isExiting;
685 msg.body.drag.x = x;
686 msg.body.drag.y = y;
687 return mChannel->sendMessage(&msg);
688}
689
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700690status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
691 if (ATRACE_ENABLED()) {
692 std::string message =
693 StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
694 mChannel->getName().c_str(), toString(isInTouchMode));
695 ATRACE_NAME(message.c_str());
696 }
697
698 InputMessage msg;
699 msg.header.type = InputMessage::Type::TOUCH_MODE;
700 msg.header.seq = seq;
701 msg.body.touchMode.eventId = eventId;
702 msg.body.touchMode.isInTouchMode = isInTouchMode;
703 return mChannel->sendMessage(&msg);
704}
705
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000706android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800707 if (DEBUG_TRANSPORT_ACTIONS) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000708 ALOGD("channel '%s' publisher ~ %s", mChannel->getName().c_str(), __func__);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800709 }
Jeff Brown5912f952013-07-01 19:10:31 -0700710
711 InputMessage msg;
712 status_t result = mChannel->receiveMessage(&msg);
713 if (result) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000714 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700715 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000716 if (msg.header.type == InputMessage::Type::FINISHED) {
717 return Finished{
718 .seq = msg.header.seq,
719 .handled = msg.body.finished.handled,
720 .consumeTime = msg.body.finished.consumeTime,
721 };
Jeff Brown5912f952013-07-01 19:10:31 -0700722 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000723
724 if (msg.header.type == InputMessage::Type::TIMELINE) {
725 return Timeline{
726 .inputEventId = msg.body.timeline.eventId,
727 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
728 };
729 }
730
731 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
Dominik Laskowski75788452021-02-09 18:51:25 -0800732 mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000733 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700734}
735
736// --- InputConsumer ---
737
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500738InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800739 : InputConsumer(channel, isTouchResamplingEnabled()) {}
740
741InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel,
742 bool enableTouchResampling)
743 : mResampleTouch(enableTouchResampling), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700744
745InputConsumer::~InputConsumer() {
746}
747
748bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600749 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700750}
751
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800752status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
753 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800754 if (DEBUG_TRANSPORT_ACTIONS) {
755 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
756 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
757 }
Jeff Brown5912f952013-07-01 19:10:31 -0700758
759 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700760 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700761
762 // Fetch the next input message.
763 // Loop until an event can be returned or no additional events are received.
764 while (!*outEvent) {
765 if (mMsgDeferred) {
766 // mMsg contains a valid input message from the previous call to consume
767 // that has not yet been processed.
768 mMsgDeferred = false;
769 } else {
770 // Receive a fresh message.
771 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000772 if (result == OK) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800773 const auto [_, inserted] =
774 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
775 LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
776 mMsg.header.seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000777 }
Jeff Brown5912f952013-07-01 19:10:31 -0700778 if (result) {
779 // Consume the next batched event unless batches are being held for later.
780 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800781 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700782 if (*outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800783 if (DEBUG_TRANSPORT_ACTIONS) {
784 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
785 mChannel->getName().c_str(), *outSeq);
786 }
Jeff Brown5912f952013-07-01 19:10:31 -0700787 break;
788 }
789 }
790 return result;
791 }
792 }
793
794 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700795 case InputMessage::Type::KEY: {
796 KeyEvent* keyEvent = factory->createKeyEvent();
797 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700798
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700799 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500800 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700801 *outEvent = keyEvent;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800802 if (DEBUG_TRANSPORT_ACTIONS) {
803 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
804 mChannel->getName().c_str(), *outSeq);
805 }
Jeff Brown5912f952013-07-01 19:10:31 -0700806 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700807 }
Jeff Brown5912f952013-07-01 19:10:31 -0700808
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700809 case InputMessage::Type::MOTION: {
810 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
811 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500812 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700813 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500814 batch.samples.push_back(mMsg);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800815 if (DEBUG_TRANSPORT_ACTIONS) {
816 ALOGD("channel '%s' consumer ~ appended to batch event",
817 mChannel->getName().c_str());
818 }
Jeff Brown5912f952013-07-01 19:10:31 -0700819 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700820 } else if (isPointerEvent(mMsg.body.motion.source) &&
821 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
822 // No need to process events that we are going to cancel anyways
823 const size_t count = batch.samples.size();
824 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500825 const InputMessage& msg = batch.samples[i];
826 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700827 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500828 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
829 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700830 } else {
831 // We cannot append to the batch in progress, so we need to consume
832 // the previous batch right now and defer the new message until later.
833 mMsgDeferred = true;
834 status_t result = consumeSamples(factory, batch, batch.samples.size(),
835 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500836 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700837 if (result) {
838 return result;
839 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800840 if (DEBUG_TRANSPORT_ACTIONS) {
841 ALOGD("channel '%s' consumer ~ consumed batch event and "
842 "deferred current event, seq=%u",
843 mChannel->getName().c_str(), *outSeq);
844 }
Jeff Brown5912f952013-07-01 19:10:31 -0700845 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700846 }
Jeff Brown5912f952013-07-01 19:10:31 -0700847 }
Jeff Brown5912f952013-07-01 19:10:31 -0700848
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800849 // Start a new batch if needed.
850 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
851 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500852 Batch batch;
853 batch.samples.push_back(mMsg);
854 mBatches.push_back(batch);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800855 if (DEBUG_TRANSPORT_ACTIONS) {
856 ALOGD("channel '%s' consumer ~ started batch event",
857 mChannel->getName().c_str());
858 }
859 break;
860 }
Jeff Brown5912f952013-07-01 19:10:31 -0700861
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800862 MotionEvent* motionEvent = factory->createMotionEvent();
863 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700864
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800865 updateTouchState(mMsg);
866 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500867 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800868 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800869
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800870 if (DEBUG_TRANSPORT_ACTIONS) {
871 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
872 mChannel->getName().c_str(), *outSeq);
873 }
874 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700875 }
Jeff Brown5912f952013-07-01 19:10:31 -0700876
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000877 case InputMessage::Type::FINISHED:
878 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000879 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
880 "InputConsumer!",
Dominik Laskowski75788452021-02-09 18:51:25 -0800881 ftl::enum_string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800882 break;
883 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800884
885 case InputMessage::Type::FOCUS: {
886 FocusEvent* focusEvent = factory->createFocusEvent();
887 if (!focusEvent) return NO_MEMORY;
888
889 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500890 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800891 *outEvent = focusEvent;
892 break;
893 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800894
895 case InputMessage::Type::CAPTURE: {
896 CaptureEvent* captureEvent = factory->createCaptureEvent();
897 if (!captureEvent) return NO_MEMORY;
898
899 initializeCaptureEvent(captureEvent, &mMsg);
900 *outSeq = mMsg.header.seq;
901 *outEvent = captureEvent;
902 break;
903 }
arthurhung7632c332020-12-30 16:58:01 +0800904
905 case InputMessage::Type::DRAG: {
906 DragEvent* dragEvent = factory->createDragEvent();
907 if (!dragEvent) return NO_MEMORY;
908
909 initializeDragEvent(dragEvent, &mMsg);
910 *outSeq = mMsg.header.seq;
911 *outEvent = dragEvent;
912 break;
913 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700914
915 case InputMessage::Type::TOUCH_MODE: {
916 TouchModeEvent* touchModeEvent = factory->createTouchModeEvent();
917 if (!touchModeEvent) return NO_MEMORY;
918
919 initializeTouchModeEvent(touchModeEvent, &mMsg);
920 *outSeq = mMsg.header.seq;
921 *outEvent = touchModeEvent;
922 break;
923 }
Jeff Brown5912f952013-07-01 19:10:31 -0700924 }
925 }
926 return OK;
927}
928
929status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800930 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700931 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700932 for (size_t i = mBatches.size(); i > 0; ) {
933 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500934 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -0700935 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800936 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500937 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -0700938 return result;
939 }
940
Michael Wright32232172013-10-21 12:05:22 -0700941 nsecs_t sampleTime = frameTime;
942 if (mResampleTouch) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800943 sampleTime -= std::chrono::nanoseconds(RESAMPLE_LATENCY).count();
Michael Wright32232172013-10-21 12:05:22 -0700944 }
Jeff Brown5912f952013-07-01 19:10:31 -0700945 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
946 if (split < 0) {
947 continue;
948 }
949
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800950 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700951 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500952 if (batch.samples.empty()) {
953 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700954 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700955 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500956 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -0700957 }
Michael Wright32232172013-10-21 12:05:22 -0700958 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700959 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
960 }
961 return result;
962 }
963
964 return WOULD_BLOCK;
965}
966
967status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800968 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700969 MotionEvent* motionEvent = factory->createMotionEvent();
970 if (! motionEvent) return NO_MEMORY;
971
972 uint32_t chain = 0;
973 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500974 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100975 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700976 if (i) {
977 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500978 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700979 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500980 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -0700981 addSample(motionEvent, &msg);
982 } else {
983 initializeMotionEvent(motionEvent, &msg);
984 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500985 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700986 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500987 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -0700988
989 *outSeq = chain;
990 *outEvent = motionEvent;
991 return OK;
992}
993
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100994void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800995 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700996 return;
997 }
998
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100999 int32_t deviceId = msg.body.motion.deviceId;
1000 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -07001001
1002 // Update the touch state history to incorporate the new input message.
1003 // If the message is in the past relative to the most recently produced resampled
1004 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001005 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -07001006 case AMOTION_EVENT_ACTION_DOWN: {
1007 ssize_t index = findTouchState(deviceId, source);
1008 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001009 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -07001010 index = mTouchStates.size() - 1;
1011 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001012 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001013 touchState.initialize(deviceId, source);
1014 touchState.addHistory(msg);
1015 break;
1016 }
1017
1018 case AMOTION_EVENT_ACTION_MOVE: {
1019 ssize_t index = findTouchState(deviceId, source);
1020 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001021 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001022 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001023 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001024 }
1025 break;
1026 }
1027
1028 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
1029 ssize_t index = findTouchState(deviceId, source);
1030 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001031 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001032 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001033 rewriteMessage(touchState, msg);
1034 }
1035 break;
1036 }
1037
1038 case AMOTION_EVENT_ACTION_POINTER_UP: {
1039 ssize_t index = findTouchState(deviceId, source);
1040 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001041 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001042 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001043 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001044 }
1045 break;
1046 }
1047
1048 case AMOTION_EVENT_ACTION_SCROLL: {
1049 ssize_t index = findTouchState(deviceId, source);
1050 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001051 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001052 rewriteMessage(touchState, msg);
1053 }
1054 break;
1055 }
1056
1057 case AMOTION_EVENT_ACTION_UP:
1058 case AMOTION_EVENT_ACTION_CANCEL: {
1059 ssize_t index = findTouchState(deviceId, source);
1060 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001061 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001062 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001063 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -07001064 }
1065 break;
1066 }
1067 }
1068}
1069
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001070/**
1071 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1072 *
1073 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1074 * is in the past relative to msg and the past two events do not contain identical coordinates),
1075 * then invalidate the lastResample data for that pointer.
1076 * If the two past events have identical coordinates, then lastResample data for that pointer will
1077 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1078 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1079 * not equal to x0 is received.
1080 */
1081void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001082 nsecs_t eventTime = msg.body.motion.eventTime;
1083 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1084 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001085 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001086 if (eventTime < state.lastResample.eventTime ||
1087 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001088 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1089 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001090#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001091 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
1092 resampleCoords.getX(), resampleCoords.getY(),
1093 msgCoords.getX(), msgCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001094#endif
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001095 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1096 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
Philip Quinnafb31282022-12-20 18:17:55 -08001097 msgCoords.isResampled = true;
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001098 } else {
1099 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001100 }
Jeff Brown5912f952013-07-01 19:10:31 -07001101 }
1102 }
1103}
1104
1105void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1106 const InputMessage* next) {
1107 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001108 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001109 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1110 return;
1111 }
1112
1113 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1114 if (index < 0) {
1115#if DEBUG_RESAMPLING
1116 ALOGD("Not resampled, no touch state for device.");
1117#endif
1118 return;
1119 }
1120
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001121 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001122 if (touchState.historySize < 1) {
1123#if DEBUG_RESAMPLING
1124 ALOGD("Not resampled, no history for device.");
1125#endif
1126 return;
1127 }
1128
1129 // Ensure that the current sample has all of the pointers that need to be reported.
1130 const History* current = touchState.getHistory(0);
1131 size_t pointerCount = event->getPointerCount();
1132 for (size_t i = 0; i < pointerCount; i++) {
1133 uint32_t id = event->getPointerId(i);
1134 if (!current->idBits.hasBit(id)) {
1135#if DEBUG_RESAMPLING
1136 ALOGD("Not resampled, missing id %d", id);
1137#endif
1138 return;
1139 }
1140 }
1141
1142 // Find the data to use for resampling.
1143 const History* other;
1144 History future;
1145 float alpha;
1146 if (next) {
1147 // Interpolate between current sample and future sample.
1148 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001149 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001150 other = &future;
1151 nsecs_t delta = future.eventTime - current->eventTime;
1152 if (delta < RESAMPLE_MIN_DELTA) {
1153#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001154 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001155#endif
1156 return;
1157 }
1158 alpha = float(sampleTime - current->eventTime) / delta;
1159 } else if (touchState.historySize >= 2) {
1160 // Extrapolate future sample using current sample and past sample.
1161 // So other->eventTime <= current->eventTime <= sampleTime.
1162 other = touchState.getHistory(1);
1163 nsecs_t delta = current->eventTime - other->eventTime;
1164 if (delta < RESAMPLE_MIN_DELTA) {
1165#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001166 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001167#endif
1168 return;
1169 } else if (delta > RESAMPLE_MAX_DELTA) {
1170#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001171 ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001172#endif
1173 return;
1174 }
1175 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1176 if (sampleTime > maxPredict) {
1177#if DEBUG_RESAMPLING
1178 ALOGD("Sample time is too far in the future, adjusting prediction "
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001179 "from %" PRId64 " to %" PRId64 " ns.",
Jeff Brown5912f952013-07-01 19:10:31 -07001180 sampleTime - current->eventTime, maxPredict - current->eventTime);
1181#endif
1182 sampleTime = maxPredict;
1183 }
1184 alpha = float(current->eventTime - sampleTime) / delta;
1185 } else {
1186#if DEBUG_RESAMPLING
1187 ALOGD("Not resampled, insufficient data.");
1188#endif
1189 return;
1190 }
1191
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001192 if (current->eventTime == sampleTime) {
1193 // Prevents having 2 events with identical times and coordinates.
1194 return;
1195 }
1196
Jeff Brown5912f952013-07-01 19:10:31 -07001197 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001198 History oldLastResample;
1199 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001200 touchState.lastResample.eventTime = sampleTime;
1201 touchState.lastResample.idBits.clear();
1202 for (size_t i = 0; i < pointerCount; i++) {
1203 uint32_t id = event->getPointerId(i);
1204 touchState.lastResample.idToIndex[id] = i;
1205 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001206 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1207 // We maintain the previously resampled value for this pointer (stored in
1208 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1209 // This way we don't introduce artificial jitter when pointers haven't actually moved.
Philip Quinnafb31282022-12-20 18:17:55 -08001210 // The isResampled flag isn't cleared as the values don't reflect what the device is
1211 // actually reporting.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001212
1213 // We know here that the coordinates for the pointer haven't changed because we
1214 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1215 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1216 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1217 continue;
1218 }
1219
Jeff Brown5912f952013-07-01 19:10:31 -07001220 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1221 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001222 resampledCoords.copyFrom(currentCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001223 if (other->idBits.hasBit(id)
1224 && shouldResampleTool(event->getToolType(i))) {
1225 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001226 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
1227 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
1228 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
1229 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
Philip Quinnafb31282022-12-20 18:17:55 -08001230 resampledCoords.isResampled = true;
Jeff Brown5912f952013-07-01 19:10:31 -07001231#if DEBUG_RESAMPLING
1232 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1233 "other (%0.3f, %0.3f), alpha %0.3f",
1234 id, resampledCoords.getX(), resampledCoords.getY(),
1235 currentCoords.getX(), currentCoords.getY(),
1236 otherCoords.getX(), otherCoords.getY(),
1237 alpha);
1238#endif
1239 } else {
Jeff Brown5912f952013-07-01 19:10:31 -07001240#if DEBUG_RESAMPLING
1241 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
1242 id, resampledCoords.getX(), resampledCoords.getY(),
1243 currentCoords.getX(), currentCoords.getY());
1244#endif
1245 }
1246 }
1247
1248 event->addSample(sampleTime, touchState.lastResample.pointers);
1249}
1250
1251bool InputConsumer::shouldResampleTool(int32_t toolType) {
1252 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1253 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1254}
1255
1256status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08001257 if (DEBUG_TRANSPORT_ACTIONS) {
1258 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1259 mChannel->getName().c_str(), seq, toString(handled));
1260 }
Jeff Brown5912f952013-07-01 19:10:31 -07001261
1262 if (!seq) {
1263 ALOGE("Attempted to send a finished signal with sequence number 0.");
1264 return BAD_VALUE;
1265 }
1266
1267 // Send finished signals for the batch sequence chain first.
1268 size_t seqChainCount = mSeqChains.size();
1269 if (seqChainCount) {
1270 uint32_t currentSeq = seq;
1271 uint32_t chainSeqs[seqChainCount];
1272 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001273 for (size_t i = seqChainCount; i > 0; ) {
1274 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001275 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001276 if (seqChain.seq == currentSeq) {
1277 currentSeq = seqChain.chain;
1278 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001279 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001280 }
1281 }
1282 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001283 while (!status && chainIndex > 0) {
1284 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001285 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1286 }
1287 if (status) {
1288 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001289 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001290 SeqChain seqChain;
1291 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1292 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001293 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001294 if (!chainIndex) break;
1295 chainIndex--;
1296 }
Jeff Brown5912f952013-07-01 19:10:31 -07001297 return status;
1298 }
1299 }
1300
1301 // Send finished signal for the last message in the batch.
1302 return sendUnchainedFinishedSignal(seq, handled);
1303}
1304
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001305status_t InputConsumer::sendTimeline(int32_t inputEventId,
1306 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
1307 if (DEBUG_TRANSPORT_ACTIONS) {
1308 ALOGD("channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1309 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1310 mChannel->getName().c_str(), inputEventId,
1311 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1312 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
1313 }
1314
1315 InputMessage msg;
1316 msg.header.type = InputMessage::Type::TIMELINE;
1317 msg.header.seq = 0;
1318 msg.body.timeline.eventId = inputEventId;
1319 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1320 return mChannel->sendMessage(&msg);
1321}
1322
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001323nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1324 auto it = mConsumeTimes.find(seq);
1325 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1326 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1327 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1328 seq);
1329 return it->second;
1330}
1331
1332void InputConsumer::popConsumeTime(uint32_t seq) {
1333 mConsumeTimes.erase(seq);
1334}
1335
Jeff Brown5912f952013-07-01 19:10:31 -07001336status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1337 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001338 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001339 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001340 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001341 msg.body.finished.consumeTime = getConsumeTime(seq);
1342 status_t result = mChannel->sendMessage(&msg);
1343 if (result == OK) {
1344 // Remove the consume time if the socket write succeeded. We will not need to ack this
1345 // message anymore. If the socket write did not succeed, we will try again and will still
1346 // need consume time.
1347 popConsumeTime(seq);
1348 }
1349 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001350}
1351
Jeff Brown5912f952013-07-01 19:10:31 -07001352bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001353 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001354}
1355
Arthur Hungc7812be2020-02-27 22:40:27 +08001356int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001357 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001358 return AINPUT_SOURCE_CLASS_NONE;
1359 }
1360
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001361 const Batch& batch = mBatches[0];
1362 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001363 return head.body.motion.source;
1364}
1365
Jeff Brown5912f952013-07-01 19:10:31 -07001366ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1367 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001368 const Batch& batch = mBatches[i];
1369 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001370 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1371 return i;
1372 }
1373 }
1374 return -1;
1375}
1376
1377ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1378 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001379 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001380 if (touchState.deviceId == deviceId && touchState.source == source) {
1381 return i;
1382 }
1383 }
1384 return -1;
1385}
1386
1387void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001388 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001389 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1390 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1391 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1392 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001393}
1394
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001395void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001396 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001397}
1398
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001399void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001400 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001401}
1402
arthurhung7632c332020-12-30 16:58:01 +08001403void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1404 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1405 msg->body.drag.isExiting);
1406}
1407
Jeff Brown5912f952013-07-01 19:10:31 -07001408void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001409 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001410 PointerProperties pointerProperties[pointerCount];
1411 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001412 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001413 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1414 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1415 }
1416
chaviw9eaa22c2020-07-01 16:21:27 -07001417 ui::Transform transform;
1418 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1419 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001420 ui::Transform displayTransform;
1421 displayTransform.set({msg->body.motion.dsdxRaw, msg->body.motion.dtdxRaw,
1422 msg->body.motion.txRaw, msg->body.motion.dtdyRaw,
1423 msg->body.motion.dsdyRaw, msg->body.motion.tyRaw, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001424 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1425 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1426 msg->body.motion.actionButton, msg->body.motion.flags,
1427 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001428 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1429 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1430 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001431 displayTransform, msg->body.motion.downTime, msg->body.motion.eventTime,
1432 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001433}
1434
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001435void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
1436 event->initialize(msg->body.touchMode.eventId, msg->body.touchMode.isInTouchMode);
1437}
1438
Jeff Brown5912f952013-07-01 19:10:31 -07001439void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001440 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001441 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001442 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001443 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1444 }
1445
1446 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1447 event->addSample(msg->body.motion.eventTime, pointerCoords);
1448}
1449
1450bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001451 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001452 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001453 if (head.body.motion.pointerCount != pointerCount
1454 || head.body.motion.action != msg->body.motion.action) {
1455 return false;
1456 }
1457 for (size_t i = 0; i < pointerCount; i++) {
1458 if (head.body.motion.pointers[i].properties
1459 != msg->body.motion.pointers[i].properties) {
1460 return false;
1461 }
1462 }
1463 return true;
1464}
1465
1466ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1467 size_t numSamples = batch.samples.size();
1468 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001469 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001470 index += 1;
1471 }
1472 return ssize_t(index) - 1;
1473}
1474
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001475std::string InputConsumer::dump() const {
1476 std::string out;
1477 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1478 out = out + "mChannel = " + mChannel->getName() + "\n";
1479 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1480 if (mMsgDeferred) {
Dominik Laskowski75788452021-02-09 18:51:25 -08001481 out = out + "mMsg : " + ftl::enum_string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001482 }
1483 out += "Batches:\n";
1484 for (const Batch& batch : mBatches) {
1485 out += " Batch:\n";
1486 for (const InputMessage& msg : batch.samples) {
1487 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Dominik Laskowski75788452021-02-09 18:51:25 -08001488 ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001489 switch (msg.header.type) {
1490 case InputMessage::Type::KEY: {
1491 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1492 KeyEvent::actionToString(
1493 msg.body.key.action),
1494 msg.body.key.keyCode);
1495 break;
1496 }
1497 case InputMessage::Type::MOTION: {
1498 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1499 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1500 const float x = msg.body.motion.pointers[i].coords.getX();
1501 const float y = msg.body.motion.pointers[i].coords.getY();
1502 out += android::base::StringPrintf("\n Pointer %" PRIu32
1503 " : x=%.1f y=%.1f",
1504 i, x, y);
1505 }
1506 break;
1507 }
1508 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001509 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1510 toString(msg.body.finished.handled),
1511 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001512 break;
1513 }
1514 case InputMessage::Type::FOCUS: {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001515 out += android::base::StringPrintf("hasFocus=%s",
1516 toString(msg.body.focus.hasFocus));
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001517 break;
1518 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001519 case InputMessage::Type::CAPTURE: {
1520 out += android::base::StringPrintf("hasCapture=%s",
1521 toString(msg.body.capture
1522 .pointerCaptureEnabled));
1523 break;
1524 }
arthurhung7632c332020-12-30 16:58:01 +08001525 case InputMessage::Type::DRAG: {
1526 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1527 msg.body.drag.x, msg.body.drag.y,
1528 toString(msg.body.drag.isExiting));
1529 break;
1530 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001531 case InputMessage::Type::TIMELINE: {
1532 const nsecs_t gpuCompletedTime =
1533 msg.body.timeline
1534 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1535 const nsecs_t presentTime =
1536 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1537 out += android::base::StringPrintf("inputEventId=%" PRId32
1538 ", gpuCompletedTime=%" PRId64
1539 ", presentTime=%" PRId64,
1540 msg.body.timeline.eventId, gpuCompletedTime,
1541 presentTime);
1542 break;
1543 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001544 case InputMessage::Type::TOUCH_MODE: {
1545 out += android::base::StringPrintf("isInTouchMode=%s",
1546 toString(msg.body.touchMode.isInTouchMode));
1547 break;
1548 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001549 }
1550 out += "\n";
1551 }
1552 }
1553 if (mBatches.empty()) {
1554 out += " <empty>\n";
1555 }
1556 out += "mSeqChains:\n";
1557 for (const SeqChain& chain : mSeqChains) {
1558 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1559 chain.chain);
1560 }
1561 if (mSeqChains.empty()) {
1562 out += " <empty>\n";
1563 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001564 out += "mConsumeTimes:\n";
1565 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1566 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1567 consumeTime);
1568 }
1569 if (mConsumeTimes.empty()) {
1570 out += " <empty>\n";
1571 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001572 return out;
1573}
1574
Jeff Brown5912f952013-07-01 19:10:31 -07001575} // namespace android