blob: 9f0a314041945ac26536ff37fb55c6c2658dd19c [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
Jeff Brown5912f952013-07-01 19:10:31 -070079template<typename T>
80inline static T min(const T& a, const T& b) {
81 return a < b ? a : b;
82}
83
84inline static float lerp(float a, float b, float alpha) {
85 return a + alpha * (b - a);
86}
87
Siarhei Vishniakou128eab12019-05-23 10:25:59 +080088inline static bool isPointerEvent(int32_t source) {
89 return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER;
90}
91
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -080092inline static const char* toString(bool value) {
93 return value ? "true" : "false";
94}
95
Jeff Brown5912f952013-07-01 19:10:31 -070096// --- InputMessage ---
97
98bool InputMessage::isValid(size_t actualSize) const {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +000099 if (size() != actualSize) {
100 ALOGE("Received message of incorrect size %zu (expected %zu)", actualSize, size());
101 return false;
102 }
103
104 switch (header.type) {
105 case Type::KEY:
106 return true;
107 case Type::MOTION: {
108 const bool valid =
109 body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
110 if (!valid) {
111 ALOGE("Received invalid MOTION: pointerCount = %" PRIu32, body.motion.pointerCount);
112 }
113 return valid;
114 }
115 case Type::FINISHED:
116 case Type::FOCUS:
117 case Type::CAPTURE:
118 case Type::DRAG:
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700119 case Type::TOUCH_MODE:
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000120 return true;
121 case Type::TIMELINE: {
122 const nsecs_t gpuCompletedTime =
123 body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
124 const nsecs_t presentTime =
125 body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
126 const bool valid = presentTime > gpuCompletedTime;
127 if (!valid) {
128 ALOGE("Received invalid TIMELINE: gpuCompletedTime = %" PRId64
129 " presentTime = %" PRId64,
130 gpuCompletedTime, presentTime);
131 }
132 return valid;
Jeff Brown5912f952013-07-01 19:10:31 -0700133 }
134 }
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000135 ALOGE("Invalid message type: %" PRIu32, header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700136 return false;
137}
138
139size_t InputMessage::size() const {
140 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700141 case Type::KEY:
142 return sizeof(Header) + body.key.size();
143 case Type::MOTION:
144 return sizeof(Header) + body.motion.size();
145 case Type::FINISHED:
146 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800147 case Type::FOCUS:
148 return sizeof(Header) + body.focus.size();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800149 case Type::CAPTURE:
150 return sizeof(Header) + body.capture.size();
arthurhung7632c332020-12-30 16:58:01 +0800151 case Type::DRAG:
152 return sizeof(Header) + body.drag.size();
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000153 case Type::TIMELINE:
154 return sizeof(Header) + body.timeline.size();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700155 case Type::TOUCH_MODE:
156 return sizeof(Header) + body.touchMode.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700157 }
158 return sizeof(Header);
159}
160
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800161/**
162 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
163 * memory to zero, then only copy the valid bytes on a per-field basis.
164 */
165void InputMessage::getSanitizedCopy(InputMessage* msg) const {
166 memset(msg, 0, sizeof(*msg));
167
168 // Write the header
169 msg->header.type = header.type;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500170 msg->header.seq = header.seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800171
172 // Write the body
173 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700174 case InputMessage::Type::KEY: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800175 // int32_t eventId
176 msg->body.key.eventId = body.key.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800177 // nsecs_t eventTime
178 msg->body.key.eventTime = body.key.eventTime;
179 // int32_t deviceId
180 msg->body.key.deviceId = body.key.deviceId;
181 // int32_t source
182 msg->body.key.source = body.key.source;
183 // int32_t displayId
184 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600185 // std::array<uint8_t, 32> hmac
186 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800187 // int32_t action
188 msg->body.key.action = body.key.action;
189 // int32_t flags
190 msg->body.key.flags = body.key.flags;
191 // int32_t keyCode
192 msg->body.key.keyCode = body.key.keyCode;
193 // int32_t scanCode
194 msg->body.key.scanCode = body.key.scanCode;
195 // int32_t metaState
196 msg->body.key.metaState = body.key.metaState;
197 // int32_t repeatCount
198 msg->body.key.repeatCount = body.key.repeatCount;
199 // nsecs_t downTime
200 msg->body.key.downTime = body.key.downTime;
201 break;
202 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700203 case InputMessage::Type::MOTION: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800204 // int32_t eventId
205 msg->body.motion.eventId = body.motion.eventId;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700206 // uint32_t pointerCount
207 msg->body.motion.pointerCount = body.motion.pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800208 // nsecs_t eventTime
209 msg->body.motion.eventTime = body.motion.eventTime;
210 // int32_t deviceId
211 msg->body.motion.deviceId = body.motion.deviceId;
212 // int32_t source
213 msg->body.motion.source = body.motion.source;
214 // int32_t displayId
215 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600216 // std::array<uint8_t, 32> hmac
217 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800218 // int32_t action
219 msg->body.motion.action = body.motion.action;
220 // int32_t actionButton
221 msg->body.motion.actionButton = body.motion.actionButton;
222 // int32_t flags
223 msg->body.motion.flags = body.motion.flags;
224 // int32_t metaState
225 msg->body.motion.metaState = body.motion.metaState;
226 // int32_t buttonState
227 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800228 // MotionClassification classification
229 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800230 // int32_t edgeFlags
231 msg->body.motion.edgeFlags = body.motion.edgeFlags;
232 // nsecs_t downTime
233 msg->body.motion.downTime = body.motion.downTime;
chaviw9eaa22c2020-07-01 16:21:27 -0700234
235 msg->body.motion.dsdx = body.motion.dsdx;
236 msg->body.motion.dtdx = body.motion.dtdx;
237 msg->body.motion.dtdy = body.motion.dtdy;
238 msg->body.motion.dsdy = body.motion.dsdy;
239 msg->body.motion.tx = body.motion.tx;
240 msg->body.motion.ty = body.motion.ty;
241
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800242 // float xPrecision
243 msg->body.motion.xPrecision = body.motion.xPrecision;
244 // float yPrecision
245 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700246 // float xCursorPosition
247 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
248 // float yCursorPosition
249 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700250
251 msg->body.motion.dsdxRaw = body.motion.dsdxRaw;
252 msg->body.motion.dtdxRaw = body.motion.dtdxRaw;
253 msg->body.motion.dtdyRaw = body.motion.dtdyRaw;
254 msg->body.motion.dsdyRaw = body.motion.dsdyRaw;
255 msg->body.motion.txRaw = body.motion.txRaw;
256 msg->body.motion.tyRaw = body.motion.tyRaw;
257
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800258 //struct Pointer pointers[MAX_POINTERS]
259 for (size_t i = 0; i < body.motion.pointerCount; i++) {
260 // PointerProperties properties
261 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
262 msg->body.motion.pointers[i].properties.toolType =
263 body.motion.pointers[i].properties.toolType,
264 // PointerCoords coords
265 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
266 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
267 memcpy(&msg->body.motion.pointers[i].coords.values[0],
268 &body.motion.pointers[i].coords.values[0],
269 count * (sizeof(body.motion.pointers[i].coords.values[0])));
Philip Quinnafb31282022-12-20 18:17:55 -0800270 msg->body.motion.pointers[i].coords.isResampled =
271 body.motion.pointers[i].coords.isResampled;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800272 }
273 break;
274 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700275 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800276 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000277 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800278 break;
279 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800280 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800281 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800282 msg->body.focus.hasFocus = body.focus.hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800283 break;
284 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800285 case InputMessage::Type::CAPTURE: {
286 msg->body.capture.eventId = body.capture.eventId;
287 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
288 break;
289 }
arthurhung7632c332020-12-30 16:58:01 +0800290 case InputMessage::Type::DRAG: {
291 msg->body.drag.eventId = body.drag.eventId;
292 msg->body.drag.x = body.drag.x;
293 msg->body.drag.y = body.drag.y;
294 msg->body.drag.isExiting = body.drag.isExiting;
295 break;
296 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000297 case InputMessage::Type::TIMELINE: {
298 msg->body.timeline.eventId = body.timeline.eventId;
299 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
300 break;
301 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700302 case InputMessage::Type::TOUCH_MODE: {
303 msg->body.touchMode.eventId = body.touchMode.eventId;
304 msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode;
305 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800306 }
307}
Jeff Brown5912f952013-07-01 19:10:31 -0700308
309// --- InputChannel ---
310
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500311std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500312 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700313 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
314 if (result != 0) {
315 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
316 strerror(errno));
317 return nullptr;
318 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500319 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500320 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700321}
322
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500323InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token)
324 : mName(std::move(name)), mFd(std::move(fd)), mToken(std::move(token)) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700325 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500326 ALOGD("Input channel constructed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700327 }
Jeff Brown5912f952013-07-01 19:10:31 -0700328}
329
330InputChannel::~InputChannel() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700331 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500332 ALOGD("Input channel destroyed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700333 }
Robert Carr3720ed02018-08-08 16:08:27 -0700334}
335
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800336status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500337 std::unique_ptr<InputChannel>& outServerChannel,
338 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700339 int sockets[2];
340 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
341 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000342 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
343 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500344 outServerChannel.reset();
345 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700346 return result;
347 }
348
349 int bufferSize = SOCKET_BUFFER_SIZE;
350 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
351 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
352 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
353 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
354
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700355 sp<IBinder> token = new BBinder();
356
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700357 std::string serverChannelName = name + " (server)";
358 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700359 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700360
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700361 std::string clientChannelName = name + " (client)";
362 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700363 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700364 return OK;
365}
366
367status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800368 const size_t msgLength = msg->size();
369 InputMessage cleanMsg;
370 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700371 ssize_t nWrite;
372 do {
Chris Ye0783e992020-06-02 21:34:49 -0700373 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700374 } while (nWrite == -1 && errno == EINTR);
375
376 if (nWrite < 0) {
377 int error = errno;
378#if DEBUG_CHANNEL_MESSAGES
chaviw81e2bb92019-12-18 15:03:51 -0800379 ALOGD("channel '%s' ~ error sending message of type %d, %s", mName.c_str(),
380 msg->header.type, strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700381#endif
382 if (error == EAGAIN || error == EWOULDBLOCK) {
383 return WOULD_BLOCK;
384 }
385 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
386 return DEAD_OBJECT;
387 }
388 return -error;
389 }
390
391 if (size_t(nWrite) != msgLength) {
392#if DEBUG_CHANNEL_MESSAGES
393 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800394 mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700395#endif
396 return DEAD_OBJECT;
397 }
398
399#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800400 ALOGD("channel '%s' ~ sent message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700401#endif
402 return OK;
403}
404
405status_t InputChannel::receiveMessage(InputMessage* msg) {
406 ssize_t nRead;
407 do {
Chris Ye0783e992020-06-02 21:34:49 -0700408 nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700409 } while (nRead == -1 && errno == EINTR);
410
411 if (nRead < 0) {
412 int error = errno;
413#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800414 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700415#endif
416 if (error == EAGAIN || error == EWOULDBLOCK) {
417 return WOULD_BLOCK;
418 }
419 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
420 return DEAD_OBJECT;
421 }
422 return -error;
423 }
424
425 if (nRead == 0) { // check for EOF
426#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800427 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700428#endif
429 return DEAD_OBJECT;
430 }
431
432 if (!msg->isValid(nRead)) {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000433 ALOGE("channel '%s' ~ received invalid message of size %zd", mName.c_str(), nRead);
Jeff Brown5912f952013-07-01 19:10:31 -0700434 return BAD_VALUE;
435 }
436
437#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800438 ALOGD("channel '%s' ~ received message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700439#endif
440 return OK;
441}
442
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500443std::unique_ptr<InputChannel> InputChannel::dup() const {
Garfield Tan15601662020-09-22 15:32:38 -0700444 base::unique_fd newFd(dupFd());
Chris Ye0783e992020-06-02 21:34:49 -0700445 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700446}
447
Garfield Tan15601662020-09-22 15:32:38 -0700448void InputChannel::copyTo(InputChannel& outChannel) const {
449 outChannel.mName = getName();
450 outChannel.mFd = dupFd();
451 outChannel.mToken = getConnectionToken();
452}
453
Chris Ye0783e992020-06-02 21:34:49 -0700454status_t InputChannel::writeToParcel(android::Parcel* parcel) const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500455 if (parcel == nullptr) {
456 ALOGE("%s: Null parcel", __func__);
457 return BAD_VALUE;
458 }
459 return parcel->writeStrongBinder(mToken)
460 ?: parcel->writeUtf8AsUtf16(mName) ?: parcel->writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700461}
462
Chris Ye0783e992020-06-02 21:34:49 -0700463status_t InputChannel::readFromParcel(const android::Parcel* parcel) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500464 if (parcel == nullptr) {
465 ALOGE("%s: Null parcel", __func__);
466 return BAD_VALUE;
467 }
468 mToken = parcel->readStrongBinder();
469 return parcel->readUtf8FromUtf16(&mName) ?: parcel->readUniqueFileDescriptor(&mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700470}
471
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700472sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500473 return mToken;
Robert Carr803535b2018-08-02 16:38:15 -0700474}
475
Garfield Tan15601662020-09-22 15:32:38 -0700476base::unique_fd InputChannel::dupFd() const {
477 android::base::unique_fd newFd(::dup(getFd()));
478 if (!newFd.ok()) {
479 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd().get(), getName().c_str(),
480 strerror(errno));
481 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
482 // If this process is out of file descriptors, then throwing that might end up exploding
483 // on the other side of a binder call, which isn't really helpful.
484 // Better to just crash here and hope that the FD leak is slow.
485 // Other failures could be client errors, so we still propagate those back to the caller.
486 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
487 getName().c_str());
488 return {};
489 }
490 return newFd;
491}
492
Jeff Brown5912f952013-07-01 19:10:31 -0700493// --- InputPublisher ---
494
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500495InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel) : mChannel(channel) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700496
497InputPublisher::~InputPublisher() {
498}
499
Garfield Tan1c7bc862020-01-28 13:24:04 -0800500status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
501 int32_t source, int32_t displayId,
502 std::array<uint8_t, 32> hmac, int32_t action,
503 int32_t flags, int32_t keyCode, int32_t scanCode,
504 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
505 nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000506 if (ATRACE_ENABLED()) {
507 std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
508 mChannel->getName().c_str(), keyCode);
509 ATRACE_NAME(message.c_str());
510 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800511 if (DEBUG_TRANSPORT_ACTIONS) {
512 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
513 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
514 "downTime=%" PRId64 ", eventTime=%" PRId64,
515 mChannel->getName().c_str(), seq, deviceId, source, action, flags, keyCode, scanCode,
516 metaState, repeatCount, downTime, eventTime);
517 }
Jeff Brown5912f952013-07-01 19:10:31 -0700518
519 if (!seq) {
520 ALOGE("Attempted to publish a key event with sequence number 0.");
521 return BAD_VALUE;
522 }
523
524 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700525 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500526 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800527 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700528 msg.body.key.deviceId = deviceId;
529 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100530 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700531 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700532 msg.body.key.action = action;
533 msg.body.key.flags = flags;
534 msg.body.key.keyCode = keyCode;
535 msg.body.key.scanCode = scanCode;
536 msg.body.key.metaState = metaState;
537 msg.body.key.repeatCount = repeatCount;
538 msg.body.key.downTime = downTime;
539 msg.body.key.eventTime = eventTime;
540 return mChannel->sendMessage(&msg);
541}
542
543status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800544 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600545 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
546 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700547 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700548 float yPrecision, float xCursorPosition, float yCursorPosition,
549 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700550 uint32_t pointerCount, const PointerProperties* pointerProperties,
551 const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000552 if (ATRACE_ENABLED()) {
553 std::string message = StringPrintf(
554 "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")",
555 mChannel->getName().c_str(), action);
556 ATRACE_NAME(message.c_str());
557 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800558 if (DEBUG_TRANSPORT_ACTIONS) {
chaviw9eaa22c2020-07-01 16:21:27 -0700559 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700560 transform.dump(transformString, "transform", " ");
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800561 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
562 "displayId=%" PRId32 ", "
563 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700564 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800565 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
chaviw85b44202020-07-24 11:46:21 -0700566 "pointerCount=%" PRIu32 " \n%s",
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800567 mChannel->getName().c_str(), seq, deviceId, source, displayId, action, actionButton,
568 flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700569 motionClassificationToString(classification), xPrecision, yPrecision, downTime,
570 eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800571 }
Jeff Brown5912f952013-07-01 19:10:31 -0700572
573 if (!seq) {
574 ALOGE("Attempted to publish a motion event with sequence number 0.");
575 return BAD_VALUE;
576 }
577
578 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700579 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800580 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700581 return BAD_VALUE;
582 }
583
584 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700585 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500586 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800587 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700588 msg.body.motion.deviceId = deviceId;
589 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700590 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700591 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700592 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100593 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700594 msg.body.motion.flags = flags;
595 msg.body.motion.edgeFlags = edgeFlags;
596 msg.body.motion.metaState = metaState;
597 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800598 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700599 msg.body.motion.dsdx = transform.dsdx();
600 msg.body.motion.dtdx = transform.dtdx();
601 msg.body.motion.dtdy = transform.dtdy();
602 msg.body.motion.dsdy = transform.dsdy();
603 msg.body.motion.tx = transform.tx();
604 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700605 msg.body.motion.xPrecision = xPrecision;
606 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700607 msg.body.motion.xCursorPosition = xCursorPosition;
608 msg.body.motion.yCursorPosition = yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700609 msg.body.motion.dsdxRaw = rawTransform.dsdx();
610 msg.body.motion.dtdxRaw = rawTransform.dtdx();
611 msg.body.motion.dtdyRaw = rawTransform.dtdy();
612 msg.body.motion.dsdyRaw = rawTransform.dsdy();
613 msg.body.motion.txRaw = rawTransform.tx();
614 msg.body.motion.tyRaw = rawTransform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700615 msg.body.motion.downTime = downTime;
616 msg.body.motion.eventTime = eventTime;
617 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100618 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700619 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
620 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
621 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700622
Jeff Brown5912f952013-07-01 19:10:31 -0700623 return mChannel->sendMessage(&msg);
624}
625
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700626status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800627 if (ATRACE_ENABLED()) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700628 std::string message = StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
629 mChannel->getName().c_str(), toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800630 ATRACE_NAME(message.c_str());
631 }
632
633 InputMessage msg;
634 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500635 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800636 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000637 msg.body.focus.hasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800638 return mChannel->sendMessage(&msg);
639}
640
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800641status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
642 bool pointerCaptureEnabled) {
643 if (ATRACE_ENABLED()) {
644 std::string message =
645 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
646 mChannel->getName().c_str(), toString(pointerCaptureEnabled));
647 ATRACE_NAME(message.c_str());
648 }
649
650 InputMessage msg;
651 msg.header.type = InputMessage::Type::CAPTURE;
652 msg.header.seq = seq;
653 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000654 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800655 return mChannel->sendMessage(&msg);
656}
657
arthurhung7632c332020-12-30 16:58:01 +0800658status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
659 bool isExiting) {
660 if (ATRACE_ENABLED()) {
661 std::string message =
662 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
663 mChannel->getName().c_str(), x, y, toString(isExiting));
664 ATRACE_NAME(message.c_str());
665 }
666
667 InputMessage msg;
668 msg.header.type = InputMessage::Type::DRAG;
669 msg.header.seq = seq;
670 msg.body.drag.eventId = eventId;
671 msg.body.drag.isExiting = isExiting;
672 msg.body.drag.x = x;
673 msg.body.drag.y = y;
674 return mChannel->sendMessage(&msg);
675}
676
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700677status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
678 if (ATRACE_ENABLED()) {
679 std::string message =
680 StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
681 mChannel->getName().c_str(), toString(isInTouchMode));
682 ATRACE_NAME(message.c_str());
683 }
684
685 InputMessage msg;
686 msg.header.type = InputMessage::Type::TOUCH_MODE;
687 msg.header.seq = seq;
688 msg.body.touchMode.eventId = eventId;
689 msg.body.touchMode.isInTouchMode = isInTouchMode;
690 return mChannel->sendMessage(&msg);
691}
692
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000693android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800694 if (DEBUG_TRANSPORT_ACTIONS) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000695 ALOGD("channel '%s' publisher ~ %s", mChannel->getName().c_str(), __func__);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800696 }
Jeff Brown5912f952013-07-01 19:10:31 -0700697
698 InputMessage msg;
699 status_t result = mChannel->receiveMessage(&msg);
700 if (result) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000701 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700702 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000703 if (msg.header.type == InputMessage::Type::FINISHED) {
704 return Finished{
705 .seq = msg.header.seq,
706 .handled = msg.body.finished.handled,
707 .consumeTime = msg.body.finished.consumeTime,
708 };
Jeff Brown5912f952013-07-01 19:10:31 -0700709 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000710
711 if (msg.header.type == InputMessage::Type::TIMELINE) {
712 return Timeline{
713 .inputEventId = msg.body.timeline.eventId,
714 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
715 };
716 }
717
718 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
Dominik Laskowski75788452021-02-09 18:51:25 -0800719 mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000720 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700721}
722
723// --- InputConsumer ---
724
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500725InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800726 : InputConsumer(channel, isTouchResamplingEnabled()) {}
727
728InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel,
729 bool enableTouchResampling)
730 : mResampleTouch(enableTouchResampling), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700731
732InputConsumer::~InputConsumer() {
733}
734
735bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600736 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700737}
738
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800739status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
740 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800741 if (DEBUG_TRANSPORT_ACTIONS) {
742 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
743 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
744 }
Jeff Brown5912f952013-07-01 19:10:31 -0700745
746 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700747 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700748
749 // Fetch the next input message.
750 // Loop until an event can be returned or no additional events are received.
751 while (!*outEvent) {
752 if (mMsgDeferred) {
753 // mMsg contains a valid input message from the previous call to consume
754 // that has not yet been processed.
755 mMsgDeferred = false;
756 } else {
757 // Receive a fresh message.
758 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000759 if (result == OK) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800760 const auto [_, inserted] =
761 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
762 LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
763 mMsg.header.seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000764 }
Jeff Brown5912f952013-07-01 19:10:31 -0700765 if (result) {
766 // Consume the next batched event unless batches are being held for later.
767 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800768 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700769 if (*outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800770 if (DEBUG_TRANSPORT_ACTIONS) {
771 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
772 mChannel->getName().c_str(), *outSeq);
773 }
Jeff Brown5912f952013-07-01 19:10:31 -0700774 break;
775 }
776 }
777 return result;
778 }
779 }
780
781 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700782 case InputMessage::Type::KEY: {
783 KeyEvent* keyEvent = factory->createKeyEvent();
784 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700785
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700786 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500787 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700788 *outEvent = keyEvent;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800789 if (DEBUG_TRANSPORT_ACTIONS) {
790 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
791 mChannel->getName().c_str(), *outSeq);
792 }
Jeff Brown5912f952013-07-01 19:10:31 -0700793 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700794 }
Jeff Brown5912f952013-07-01 19:10:31 -0700795
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700796 case InputMessage::Type::MOTION: {
797 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
798 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500799 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700800 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500801 batch.samples.push_back(mMsg);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800802 if (DEBUG_TRANSPORT_ACTIONS) {
803 ALOGD("channel '%s' consumer ~ appended to batch event",
804 mChannel->getName().c_str());
805 }
Jeff Brown5912f952013-07-01 19:10:31 -0700806 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700807 } else if (isPointerEvent(mMsg.body.motion.source) &&
808 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
809 // No need to process events that we are going to cancel anyways
810 const size_t count = batch.samples.size();
811 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500812 const InputMessage& msg = batch.samples[i];
813 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700814 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500815 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
816 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700817 } else {
818 // We cannot append to the batch in progress, so we need to consume
819 // the previous batch right now and defer the new message until later.
820 mMsgDeferred = true;
821 status_t result = consumeSamples(factory, batch, batch.samples.size(),
822 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500823 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700824 if (result) {
825 return result;
826 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800827 if (DEBUG_TRANSPORT_ACTIONS) {
828 ALOGD("channel '%s' consumer ~ consumed batch event and "
829 "deferred current event, seq=%u",
830 mChannel->getName().c_str(), *outSeq);
831 }
Jeff Brown5912f952013-07-01 19:10:31 -0700832 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700833 }
Jeff Brown5912f952013-07-01 19:10:31 -0700834 }
Jeff Brown5912f952013-07-01 19:10:31 -0700835
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800836 // Start a new batch if needed.
837 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
838 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500839 Batch batch;
840 batch.samples.push_back(mMsg);
841 mBatches.push_back(batch);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800842 if (DEBUG_TRANSPORT_ACTIONS) {
843 ALOGD("channel '%s' consumer ~ started batch event",
844 mChannel->getName().c_str());
845 }
846 break;
847 }
Jeff Brown5912f952013-07-01 19:10:31 -0700848
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800849 MotionEvent* motionEvent = factory->createMotionEvent();
850 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700851
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800852 updateTouchState(mMsg);
853 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500854 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800855 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800856
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800857 if (DEBUG_TRANSPORT_ACTIONS) {
858 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
859 mChannel->getName().c_str(), *outSeq);
860 }
861 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700862 }
Jeff Brown5912f952013-07-01 19:10:31 -0700863
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000864 case InputMessage::Type::FINISHED:
865 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000866 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
867 "InputConsumer!",
Dominik Laskowski75788452021-02-09 18:51:25 -0800868 ftl::enum_string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800869 break;
870 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800871
872 case InputMessage::Type::FOCUS: {
873 FocusEvent* focusEvent = factory->createFocusEvent();
874 if (!focusEvent) return NO_MEMORY;
875
876 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500877 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800878 *outEvent = focusEvent;
879 break;
880 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800881
882 case InputMessage::Type::CAPTURE: {
883 CaptureEvent* captureEvent = factory->createCaptureEvent();
884 if (!captureEvent) return NO_MEMORY;
885
886 initializeCaptureEvent(captureEvent, &mMsg);
887 *outSeq = mMsg.header.seq;
888 *outEvent = captureEvent;
889 break;
890 }
arthurhung7632c332020-12-30 16:58:01 +0800891
892 case InputMessage::Type::DRAG: {
893 DragEvent* dragEvent = factory->createDragEvent();
894 if (!dragEvent) return NO_MEMORY;
895
896 initializeDragEvent(dragEvent, &mMsg);
897 *outSeq = mMsg.header.seq;
898 *outEvent = dragEvent;
899 break;
900 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700901
902 case InputMessage::Type::TOUCH_MODE: {
903 TouchModeEvent* touchModeEvent = factory->createTouchModeEvent();
904 if (!touchModeEvent) return NO_MEMORY;
905
906 initializeTouchModeEvent(touchModeEvent, &mMsg);
907 *outSeq = mMsg.header.seq;
908 *outEvent = touchModeEvent;
909 break;
910 }
Jeff Brown5912f952013-07-01 19:10:31 -0700911 }
912 }
913 return OK;
914}
915
916status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800917 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700918 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700919 for (size_t i = mBatches.size(); i > 0; ) {
920 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500921 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -0700922 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800923 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500924 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -0700925 return result;
926 }
927
Michael Wright32232172013-10-21 12:05:22 -0700928 nsecs_t sampleTime = frameTime;
929 if (mResampleTouch) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800930 sampleTime -= std::chrono::nanoseconds(RESAMPLE_LATENCY).count();
Michael Wright32232172013-10-21 12:05:22 -0700931 }
Jeff Brown5912f952013-07-01 19:10:31 -0700932 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
933 if (split < 0) {
934 continue;
935 }
936
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800937 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700938 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500939 if (batch.samples.empty()) {
940 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700941 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700942 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500943 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -0700944 }
Michael Wright32232172013-10-21 12:05:22 -0700945 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700946 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
947 }
948 return result;
949 }
950
951 return WOULD_BLOCK;
952}
953
954status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800955 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700956 MotionEvent* motionEvent = factory->createMotionEvent();
957 if (! motionEvent) return NO_MEMORY;
958
959 uint32_t chain = 0;
960 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500961 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100962 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700963 if (i) {
964 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500965 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700966 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500967 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -0700968 addSample(motionEvent, &msg);
969 } else {
970 initializeMotionEvent(motionEvent, &msg);
971 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500972 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700973 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500974 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -0700975
976 *outSeq = chain;
977 *outEvent = motionEvent;
978 return OK;
979}
980
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100981void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800982 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700983 return;
984 }
985
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100986 int32_t deviceId = msg.body.motion.deviceId;
987 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -0700988
989 // Update the touch state history to incorporate the new input message.
990 // If the message is in the past relative to the most recently produced resampled
991 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100992 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -0700993 case AMOTION_EVENT_ACTION_DOWN: {
994 ssize_t index = findTouchState(deviceId, source);
995 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500996 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -0700997 index = mTouchStates.size() - 1;
998 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500999 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001000 touchState.initialize(deviceId, source);
1001 touchState.addHistory(msg);
1002 break;
1003 }
1004
1005 case AMOTION_EVENT_ACTION_MOVE: {
1006 ssize_t index = findTouchState(deviceId, source);
1007 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001008 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001009 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001010 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001011 }
1012 break;
1013 }
1014
1015 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
1016 ssize_t index = findTouchState(deviceId, source);
1017 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001018 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001019 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001020 rewriteMessage(touchState, msg);
1021 }
1022 break;
1023 }
1024
1025 case AMOTION_EVENT_ACTION_POINTER_UP: {
1026 ssize_t index = findTouchState(deviceId, source);
1027 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001028 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001029 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001030 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001031 }
1032 break;
1033 }
1034
1035 case AMOTION_EVENT_ACTION_SCROLL: {
1036 ssize_t index = findTouchState(deviceId, source);
1037 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001038 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001039 rewriteMessage(touchState, msg);
1040 }
1041 break;
1042 }
1043
1044 case AMOTION_EVENT_ACTION_UP:
1045 case AMOTION_EVENT_ACTION_CANCEL: {
1046 ssize_t index = findTouchState(deviceId, source);
1047 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001048 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001049 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001050 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -07001051 }
1052 break;
1053 }
1054 }
1055}
1056
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001057/**
1058 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1059 *
1060 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1061 * is in the past relative to msg and the past two events do not contain identical coordinates),
1062 * then invalidate the lastResample data for that pointer.
1063 * If the two past events have identical coordinates, then lastResample data for that pointer will
1064 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1065 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1066 * not equal to x0 is received.
1067 */
1068void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001069 nsecs_t eventTime = msg.body.motion.eventTime;
1070 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1071 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001072 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001073 if (eventTime < state.lastResample.eventTime ||
1074 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001075 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1076 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001077#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001078 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
1079 resampleCoords.getX(), resampleCoords.getY(),
1080 msgCoords.getX(), msgCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001081#endif
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001082 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1083 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
Philip Quinnafb31282022-12-20 18:17:55 -08001084 msgCoords.isResampled = true;
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001085 } else {
1086 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001087 }
Jeff Brown5912f952013-07-01 19:10:31 -07001088 }
1089 }
1090}
1091
1092void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1093 const InputMessage* next) {
1094 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001095 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001096 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1097 return;
1098 }
1099
1100 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1101 if (index < 0) {
1102#if DEBUG_RESAMPLING
1103 ALOGD("Not resampled, no touch state for device.");
1104#endif
1105 return;
1106 }
1107
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001108 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001109 if (touchState.historySize < 1) {
1110#if DEBUG_RESAMPLING
1111 ALOGD("Not resampled, no history for device.");
1112#endif
1113 return;
1114 }
1115
1116 // Ensure that the current sample has all of the pointers that need to be reported.
1117 const History* current = touchState.getHistory(0);
1118 size_t pointerCount = event->getPointerCount();
1119 for (size_t i = 0; i < pointerCount; i++) {
1120 uint32_t id = event->getPointerId(i);
1121 if (!current->idBits.hasBit(id)) {
1122#if DEBUG_RESAMPLING
1123 ALOGD("Not resampled, missing id %d", id);
1124#endif
1125 return;
1126 }
1127 }
1128
1129 // Find the data to use for resampling.
1130 const History* other;
1131 History future;
1132 float alpha;
1133 if (next) {
1134 // Interpolate between current sample and future sample.
1135 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001136 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001137 other = &future;
1138 nsecs_t delta = future.eventTime - current->eventTime;
1139 if (delta < RESAMPLE_MIN_DELTA) {
1140#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001141 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001142#endif
1143 return;
1144 }
1145 alpha = float(sampleTime - current->eventTime) / delta;
1146 } else if (touchState.historySize >= 2) {
1147 // Extrapolate future sample using current sample and past sample.
1148 // So other->eventTime <= current->eventTime <= sampleTime.
1149 other = touchState.getHistory(1);
1150 nsecs_t delta = current->eventTime - other->eventTime;
1151 if (delta < RESAMPLE_MIN_DELTA) {
1152#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001153 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001154#endif
1155 return;
1156 } else if (delta > RESAMPLE_MAX_DELTA) {
1157#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001158 ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001159#endif
1160 return;
1161 }
1162 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1163 if (sampleTime > maxPredict) {
1164#if DEBUG_RESAMPLING
1165 ALOGD("Sample time is too far in the future, adjusting prediction "
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001166 "from %" PRId64 " to %" PRId64 " ns.",
Jeff Brown5912f952013-07-01 19:10:31 -07001167 sampleTime - current->eventTime, maxPredict - current->eventTime);
1168#endif
1169 sampleTime = maxPredict;
1170 }
1171 alpha = float(current->eventTime - sampleTime) / delta;
1172 } else {
1173#if DEBUG_RESAMPLING
1174 ALOGD("Not resampled, insufficient data.");
1175#endif
1176 return;
1177 }
1178
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001179 if (current->eventTime == sampleTime) {
1180 // Prevents having 2 events with identical times and coordinates.
1181 return;
1182 }
1183
Jeff Brown5912f952013-07-01 19:10:31 -07001184 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001185 History oldLastResample;
1186 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001187 touchState.lastResample.eventTime = sampleTime;
1188 touchState.lastResample.idBits.clear();
1189 for (size_t i = 0; i < pointerCount; i++) {
1190 uint32_t id = event->getPointerId(i);
1191 touchState.lastResample.idToIndex[id] = i;
1192 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001193 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1194 // We maintain the previously resampled value for this pointer (stored in
1195 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1196 // This way we don't introduce artificial jitter when pointers haven't actually moved.
Philip Quinnafb31282022-12-20 18:17:55 -08001197 // The isResampled flag isn't cleared as the values don't reflect what the device is
1198 // actually reporting.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001199
1200 // We know here that the coordinates for the pointer haven't changed because we
1201 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1202 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1203 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1204 continue;
1205 }
1206
Jeff Brown5912f952013-07-01 19:10:31 -07001207 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1208 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001209 resampledCoords.copyFrom(currentCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001210 if (other->idBits.hasBit(id)
1211 && shouldResampleTool(event->getToolType(i))) {
1212 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001213 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
1214 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
1215 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
1216 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
Philip Quinnafb31282022-12-20 18:17:55 -08001217 resampledCoords.isResampled = true;
Jeff Brown5912f952013-07-01 19:10:31 -07001218#if DEBUG_RESAMPLING
1219 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1220 "other (%0.3f, %0.3f), alpha %0.3f",
1221 id, resampledCoords.getX(), resampledCoords.getY(),
1222 currentCoords.getX(), currentCoords.getY(),
1223 otherCoords.getX(), otherCoords.getY(),
1224 alpha);
1225#endif
1226 } else {
Jeff Brown5912f952013-07-01 19:10:31 -07001227#if DEBUG_RESAMPLING
1228 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
1229 id, resampledCoords.getX(), resampledCoords.getY(),
1230 currentCoords.getX(), currentCoords.getY());
1231#endif
1232 }
1233 }
1234
1235 event->addSample(sampleTime, touchState.lastResample.pointers);
1236}
1237
1238bool InputConsumer::shouldResampleTool(int32_t toolType) {
1239 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1240 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1241}
1242
1243status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08001244 if (DEBUG_TRANSPORT_ACTIONS) {
1245 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1246 mChannel->getName().c_str(), seq, toString(handled));
1247 }
Jeff Brown5912f952013-07-01 19:10:31 -07001248
1249 if (!seq) {
1250 ALOGE("Attempted to send a finished signal with sequence number 0.");
1251 return BAD_VALUE;
1252 }
1253
1254 // Send finished signals for the batch sequence chain first.
1255 size_t seqChainCount = mSeqChains.size();
1256 if (seqChainCount) {
1257 uint32_t currentSeq = seq;
1258 uint32_t chainSeqs[seqChainCount];
1259 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001260 for (size_t i = seqChainCount; i > 0; ) {
1261 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001262 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001263 if (seqChain.seq == currentSeq) {
1264 currentSeq = seqChain.chain;
1265 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001266 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001267 }
1268 }
1269 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001270 while (!status && chainIndex > 0) {
1271 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001272 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1273 }
1274 if (status) {
1275 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001276 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001277 SeqChain seqChain;
1278 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1279 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001280 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001281 if (!chainIndex) break;
1282 chainIndex--;
1283 }
Jeff Brown5912f952013-07-01 19:10:31 -07001284 return status;
1285 }
1286 }
1287
1288 // Send finished signal for the last message in the batch.
1289 return sendUnchainedFinishedSignal(seq, handled);
1290}
1291
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001292status_t InputConsumer::sendTimeline(int32_t inputEventId,
1293 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
1294 if (DEBUG_TRANSPORT_ACTIONS) {
1295 ALOGD("channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1296 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1297 mChannel->getName().c_str(), inputEventId,
1298 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1299 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
1300 }
1301
1302 InputMessage msg;
1303 msg.header.type = InputMessage::Type::TIMELINE;
1304 msg.header.seq = 0;
1305 msg.body.timeline.eventId = inputEventId;
1306 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1307 return mChannel->sendMessage(&msg);
1308}
1309
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001310nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1311 auto it = mConsumeTimes.find(seq);
1312 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1313 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1314 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1315 seq);
1316 return it->second;
1317}
1318
1319void InputConsumer::popConsumeTime(uint32_t seq) {
1320 mConsumeTimes.erase(seq);
1321}
1322
Jeff Brown5912f952013-07-01 19:10:31 -07001323status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1324 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001325 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001326 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001327 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001328 msg.body.finished.consumeTime = getConsumeTime(seq);
1329 status_t result = mChannel->sendMessage(&msg);
1330 if (result == OK) {
1331 // Remove the consume time if the socket write succeeded. We will not need to ack this
1332 // message anymore. If the socket write did not succeed, we will try again and will still
1333 // need consume time.
1334 popConsumeTime(seq);
1335 }
1336 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001337}
1338
Jeff Brown5912f952013-07-01 19:10:31 -07001339bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001340 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001341}
1342
Arthur Hungc7812be2020-02-27 22:40:27 +08001343int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001344 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001345 return AINPUT_SOURCE_CLASS_NONE;
1346 }
1347
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001348 const Batch& batch = mBatches[0];
1349 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001350 return head.body.motion.source;
1351}
1352
Jeff Brown5912f952013-07-01 19:10:31 -07001353ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1354 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001355 const Batch& batch = mBatches[i];
1356 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001357 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1358 return i;
1359 }
1360 }
1361 return -1;
1362}
1363
1364ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1365 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001366 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001367 if (touchState.deviceId == deviceId && touchState.source == source) {
1368 return i;
1369 }
1370 }
1371 return -1;
1372}
1373
1374void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001375 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001376 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1377 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1378 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1379 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001380}
1381
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001382void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001383 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001384}
1385
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001386void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001387 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001388}
1389
arthurhung7632c332020-12-30 16:58:01 +08001390void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1391 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1392 msg->body.drag.isExiting);
1393}
1394
Jeff Brown5912f952013-07-01 19:10:31 -07001395void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001396 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001397 PointerProperties pointerProperties[pointerCount];
1398 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001399 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001400 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1401 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1402 }
1403
chaviw9eaa22c2020-07-01 16:21:27 -07001404 ui::Transform transform;
1405 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1406 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001407 ui::Transform displayTransform;
1408 displayTransform.set({msg->body.motion.dsdxRaw, msg->body.motion.dtdxRaw,
1409 msg->body.motion.txRaw, msg->body.motion.dtdyRaw,
1410 msg->body.motion.dsdyRaw, msg->body.motion.tyRaw, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001411 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1412 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1413 msg->body.motion.actionButton, msg->body.motion.flags,
1414 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001415 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1416 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1417 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001418 displayTransform, msg->body.motion.downTime, msg->body.motion.eventTime,
1419 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001420}
1421
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001422void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
1423 event->initialize(msg->body.touchMode.eventId, msg->body.touchMode.isInTouchMode);
1424}
1425
Jeff Brown5912f952013-07-01 19:10:31 -07001426void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001427 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001428 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001429 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001430 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1431 }
1432
1433 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1434 event->addSample(msg->body.motion.eventTime, pointerCoords);
1435}
1436
1437bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001438 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001439 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001440 if (head.body.motion.pointerCount != pointerCount
1441 || head.body.motion.action != msg->body.motion.action) {
1442 return false;
1443 }
1444 for (size_t i = 0; i < pointerCount; i++) {
1445 if (head.body.motion.pointers[i].properties
1446 != msg->body.motion.pointers[i].properties) {
1447 return false;
1448 }
1449 }
1450 return true;
1451}
1452
1453ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1454 size_t numSamples = batch.samples.size();
1455 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001456 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001457 index += 1;
1458 }
1459 return ssize_t(index) - 1;
1460}
1461
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001462std::string InputConsumer::dump() const {
1463 std::string out;
1464 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1465 out = out + "mChannel = " + mChannel->getName() + "\n";
1466 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1467 if (mMsgDeferred) {
Dominik Laskowski75788452021-02-09 18:51:25 -08001468 out = out + "mMsg : " + ftl::enum_string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001469 }
1470 out += "Batches:\n";
1471 for (const Batch& batch : mBatches) {
1472 out += " Batch:\n";
1473 for (const InputMessage& msg : batch.samples) {
1474 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Dominik Laskowski75788452021-02-09 18:51:25 -08001475 ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001476 switch (msg.header.type) {
1477 case InputMessage::Type::KEY: {
1478 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1479 KeyEvent::actionToString(
1480 msg.body.key.action),
1481 msg.body.key.keyCode);
1482 break;
1483 }
1484 case InputMessage::Type::MOTION: {
1485 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1486 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1487 const float x = msg.body.motion.pointers[i].coords.getX();
1488 const float y = msg.body.motion.pointers[i].coords.getY();
1489 out += android::base::StringPrintf("\n Pointer %" PRIu32
1490 " : x=%.1f y=%.1f",
1491 i, x, y);
1492 }
1493 break;
1494 }
1495 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001496 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1497 toString(msg.body.finished.handled),
1498 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001499 break;
1500 }
1501 case InputMessage::Type::FOCUS: {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001502 out += android::base::StringPrintf("hasFocus=%s",
1503 toString(msg.body.focus.hasFocus));
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001504 break;
1505 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001506 case InputMessage::Type::CAPTURE: {
1507 out += android::base::StringPrintf("hasCapture=%s",
1508 toString(msg.body.capture
1509 .pointerCaptureEnabled));
1510 break;
1511 }
arthurhung7632c332020-12-30 16:58:01 +08001512 case InputMessage::Type::DRAG: {
1513 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1514 msg.body.drag.x, msg.body.drag.y,
1515 toString(msg.body.drag.isExiting));
1516 break;
1517 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001518 case InputMessage::Type::TIMELINE: {
1519 const nsecs_t gpuCompletedTime =
1520 msg.body.timeline
1521 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1522 const nsecs_t presentTime =
1523 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1524 out += android::base::StringPrintf("inputEventId=%" PRId32
1525 ", gpuCompletedTime=%" PRId64
1526 ", presentTime=%" PRId64,
1527 msg.body.timeline.eventId, gpuCompletedTime,
1528 presentTime);
1529 break;
1530 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001531 case InputMessage::Type::TOUCH_MODE: {
1532 out += android::base::StringPrintf("isInTouchMode=%s",
1533 toString(msg.body.touchMode.isInTouchMode));
1534 break;
1535 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001536 }
1537 out += "\n";
1538 }
1539 }
1540 if (mBatches.empty()) {
1541 out += " <empty>\n";
1542 }
1543 out += "mSeqChains:\n";
1544 for (const SeqChain& chain : mSeqChains) {
1545 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1546 chain.chain);
1547 }
1548 if (mSeqChains.empty()) {
1549 out += " <empty>\n";
1550 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001551 out += "mConsumeTimes:\n";
1552 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1553 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1554 consumeTime);
1555 }
1556 if (mConsumeTimes.empty()) {
1557 out += " <empty>\n";
1558 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001559 return out;
1560}
1561
Jeff Brown5912f952013-07-01 19:10:31 -07001562} // namespace android