blob: 8d8433b973ec0d03964b30fe9df61ec4c60051ea [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])));
270 }
271 break;
272 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700273 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800274 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000275 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800276 break;
277 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800278 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800279 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800280 msg->body.focus.hasFocus = body.focus.hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800281 break;
282 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800283 case InputMessage::Type::CAPTURE: {
284 msg->body.capture.eventId = body.capture.eventId;
285 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
286 break;
287 }
arthurhung7632c332020-12-30 16:58:01 +0800288 case InputMessage::Type::DRAG: {
289 msg->body.drag.eventId = body.drag.eventId;
290 msg->body.drag.x = body.drag.x;
291 msg->body.drag.y = body.drag.y;
292 msg->body.drag.isExiting = body.drag.isExiting;
293 break;
294 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000295 case InputMessage::Type::TIMELINE: {
296 msg->body.timeline.eventId = body.timeline.eventId;
297 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
298 break;
299 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700300 case InputMessage::Type::TOUCH_MODE: {
301 msg->body.touchMode.eventId = body.touchMode.eventId;
302 msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode;
303 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800304 }
305}
Jeff Brown5912f952013-07-01 19:10:31 -0700306
307// --- InputChannel ---
308
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500309std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500310 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700311 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
312 if (result != 0) {
313 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
314 strerror(errno));
315 return nullptr;
316 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500317 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500318 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700319}
320
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500321InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token)
322 : mName(std::move(name)), mFd(std::move(fd)), mToken(std::move(token)) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700323 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500324 ALOGD("Input channel constructed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700325 }
Jeff Brown5912f952013-07-01 19:10:31 -0700326}
327
328InputChannel::~InputChannel() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700329 if (DEBUG_CHANNEL_LIFECYCLE) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500330 ALOGD("Input channel destroyed: name='%s', fd=%d", getName().c_str(), getFd().get());
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700331 }
Robert Carr3720ed02018-08-08 16:08:27 -0700332}
333
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800334status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500335 std::unique_ptr<InputChannel>& outServerChannel,
336 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700337 int sockets[2];
338 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
339 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000340 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
341 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500342 outServerChannel.reset();
343 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700344 return result;
345 }
346
347 int bufferSize = SOCKET_BUFFER_SIZE;
348 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
349 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
350 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
351 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
352
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700353 sp<IBinder> token = new BBinder();
354
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700355 std::string serverChannelName = name + " (server)";
356 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700357 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700358
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700359 std::string clientChannelName = name + " (client)";
360 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700361 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700362 return OK;
363}
364
365status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800366 const size_t msgLength = msg->size();
367 InputMessage cleanMsg;
368 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700369 ssize_t nWrite;
370 do {
Chris Ye0783e992020-06-02 21:34:49 -0700371 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700372 } while (nWrite == -1 && errno == EINTR);
373
374 if (nWrite < 0) {
375 int error = errno;
376#if DEBUG_CHANNEL_MESSAGES
chaviw81e2bb92019-12-18 15:03:51 -0800377 ALOGD("channel '%s' ~ error sending message of type %d, %s", mName.c_str(),
378 msg->header.type, strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700379#endif
380 if (error == EAGAIN || error == EWOULDBLOCK) {
381 return WOULD_BLOCK;
382 }
383 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
384 return DEAD_OBJECT;
385 }
386 return -error;
387 }
388
389 if (size_t(nWrite) != msgLength) {
390#if DEBUG_CHANNEL_MESSAGES
391 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800392 mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700393#endif
394 return DEAD_OBJECT;
395 }
396
397#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800398 ALOGD("channel '%s' ~ sent message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700399#endif
400 return OK;
401}
402
403status_t InputChannel::receiveMessage(InputMessage* msg) {
404 ssize_t nRead;
405 do {
Chris Ye0783e992020-06-02 21:34:49 -0700406 nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700407 } while (nRead == -1 && errno == EINTR);
408
409 if (nRead < 0) {
410 int error = errno;
411#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800412 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700413#endif
414 if (error == EAGAIN || error == EWOULDBLOCK) {
415 return WOULD_BLOCK;
416 }
417 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
418 return DEAD_OBJECT;
419 }
420 return -error;
421 }
422
423 if (nRead == 0) { // check for EOF
424#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800425 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700426#endif
427 return DEAD_OBJECT;
428 }
429
430 if (!msg->isValid(nRead)) {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000431 ALOGE("channel '%s' ~ received invalid message of size %zd", mName.c_str(), nRead);
Jeff Brown5912f952013-07-01 19:10:31 -0700432 return BAD_VALUE;
433 }
434
435#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800436 ALOGD("channel '%s' ~ received message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700437#endif
438 return OK;
439}
440
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500441std::unique_ptr<InputChannel> InputChannel::dup() const {
Garfield Tan15601662020-09-22 15:32:38 -0700442 base::unique_fd newFd(dupFd());
Chris Ye0783e992020-06-02 21:34:49 -0700443 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700444}
445
Garfield Tan15601662020-09-22 15:32:38 -0700446void InputChannel::copyTo(InputChannel& outChannel) const {
447 outChannel.mName = getName();
448 outChannel.mFd = dupFd();
449 outChannel.mToken = getConnectionToken();
450}
451
Chris Ye0783e992020-06-02 21:34:49 -0700452status_t InputChannel::writeToParcel(android::Parcel* parcel) const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500453 if (parcel == nullptr) {
454 ALOGE("%s: Null parcel", __func__);
455 return BAD_VALUE;
456 }
457 return parcel->writeStrongBinder(mToken)
458 ?: parcel->writeUtf8AsUtf16(mName) ?: parcel->writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700459}
460
Chris Ye0783e992020-06-02 21:34:49 -0700461status_t InputChannel::readFromParcel(const android::Parcel* parcel) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500462 if (parcel == nullptr) {
463 ALOGE("%s: Null parcel", __func__);
464 return BAD_VALUE;
465 }
466 mToken = parcel->readStrongBinder();
467 return parcel->readUtf8FromUtf16(&mName) ?: parcel->readUniqueFileDescriptor(&mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700468}
469
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700470sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500471 return mToken;
Robert Carr803535b2018-08-02 16:38:15 -0700472}
473
Garfield Tan15601662020-09-22 15:32:38 -0700474base::unique_fd InputChannel::dupFd() const {
475 android::base::unique_fd newFd(::dup(getFd()));
476 if (!newFd.ok()) {
477 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd().get(), getName().c_str(),
478 strerror(errno));
479 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
480 // If this process is out of file descriptors, then throwing that might end up exploding
481 // on the other side of a binder call, which isn't really helpful.
482 // Better to just crash here and hope that the FD leak is slow.
483 // Other failures could be client errors, so we still propagate those back to the caller.
484 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
485 getName().c_str());
486 return {};
487 }
488 return newFd;
489}
490
Jeff Brown5912f952013-07-01 19:10:31 -0700491// --- InputPublisher ---
492
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500493InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel) : mChannel(channel) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700494
495InputPublisher::~InputPublisher() {
496}
497
Garfield Tan1c7bc862020-01-28 13:24:04 -0800498status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
499 int32_t source, int32_t displayId,
500 std::array<uint8_t, 32> hmac, int32_t action,
501 int32_t flags, int32_t keyCode, int32_t scanCode,
502 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
503 nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000504 if (ATRACE_ENABLED()) {
505 std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
506 mChannel->getName().c_str(), keyCode);
507 ATRACE_NAME(message.c_str());
508 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800509 if (DEBUG_TRANSPORT_ACTIONS) {
510 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
511 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
512 "downTime=%" PRId64 ", eventTime=%" PRId64,
513 mChannel->getName().c_str(), seq, deviceId, source, action, flags, keyCode, scanCode,
514 metaState, repeatCount, downTime, eventTime);
515 }
Jeff Brown5912f952013-07-01 19:10:31 -0700516
517 if (!seq) {
518 ALOGE("Attempted to publish a key event with sequence number 0.");
519 return BAD_VALUE;
520 }
521
522 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700523 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500524 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800525 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700526 msg.body.key.deviceId = deviceId;
527 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100528 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700529 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700530 msg.body.key.action = action;
531 msg.body.key.flags = flags;
532 msg.body.key.keyCode = keyCode;
533 msg.body.key.scanCode = scanCode;
534 msg.body.key.metaState = metaState;
535 msg.body.key.repeatCount = repeatCount;
536 msg.body.key.downTime = downTime;
537 msg.body.key.eventTime = eventTime;
538 return mChannel->sendMessage(&msg);
539}
540
541status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800542 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600543 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
544 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700545 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700546 float yPrecision, float xCursorPosition, float yCursorPosition,
547 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700548 uint32_t pointerCount, const PointerProperties* pointerProperties,
549 const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000550 if (ATRACE_ENABLED()) {
551 std::string message = StringPrintf(
552 "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")",
553 mChannel->getName().c_str(), action);
554 ATRACE_NAME(message.c_str());
555 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800556 if (DEBUG_TRANSPORT_ACTIONS) {
chaviw9eaa22c2020-07-01 16:21:27 -0700557 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700558 transform.dump(transformString, "transform", " ");
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800559 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
560 "displayId=%" PRId32 ", "
561 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700562 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800563 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
chaviw85b44202020-07-24 11:46:21 -0700564 "pointerCount=%" PRIu32 " \n%s",
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800565 mChannel->getName().c_str(), seq, deviceId, source, displayId, action, actionButton,
566 flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700567 motionClassificationToString(classification), xPrecision, yPrecision, downTime,
568 eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800569 }
Jeff Brown5912f952013-07-01 19:10:31 -0700570
571 if (!seq) {
572 ALOGE("Attempted to publish a motion event with sequence number 0.");
573 return BAD_VALUE;
574 }
575
576 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700577 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800578 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700579 return BAD_VALUE;
580 }
581
582 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700583 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500584 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800585 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700586 msg.body.motion.deviceId = deviceId;
587 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700588 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700589 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700590 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100591 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700592 msg.body.motion.flags = flags;
593 msg.body.motion.edgeFlags = edgeFlags;
594 msg.body.motion.metaState = metaState;
595 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800596 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700597 msg.body.motion.dsdx = transform.dsdx();
598 msg.body.motion.dtdx = transform.dtdx();
599 msg.body.motion.dtdy = transform.dtdy();
600 msg.body.motion.dsdy = transform.dsdy();
601 msg.body.motion.tx = transform.tx();
602 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700603 msg.body.motion.xPrecision = xPrecision;
604 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700605 msg.body.motion.xCursorPosition = xCursorPosition;
606 msg.body.motion.yCursorPosition = yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700607 msg.body.motion.dsdxRaw = rawTransform.dsdx();
608 msg.body.motion.dtdxRaw = rawTransform.dtdx();
609 msg.body.motion.dtdyRaw = rawTransform.dtdy();
610 msg.body.motion.dsdyRaw = rawTransform.dsdy();
611 msg.body.motion.txRaw = rawTransform.tx();
612 msg.body.motion.tyRaw = rawTransform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700613 msg.body.motion.downTime = downTime;
614 msg.body.motion.eventTime = eventTime;
615 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100616 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700617 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
618 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
619 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700620
Jeff Brown5912f952013-07-01 19:10:31 -0700621 return mChannel->sendMessage(&msg);
622}
623
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700624status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800625 if (ATRACE_ENABLED()) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700626 std::string message = StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
627 mChannel->getName().c_str(), toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800628 ATRACE_NAME(message.c_str());
629 }
630
631 InputMessage msg;
632 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500633 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800634 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000635 msg.body.focus.hasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800636 return mChannel->sendMessage(&msg);
637}
638
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800639status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
640 bool pointerCaptureEnabled) {
641 if (ATRACE_ENABLED()) {
642 std::string message =
643 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
644 mChannel->getName().c_str(), toString(pointerCaptureEnabled));
645 ATRACE_NAME(message.c_str());
646 }
647
648 InputMessage msg;
649 msg.header.type = InputMessage::Type::CAPTURE;
650 msg.header.seq = seq;
651 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000652 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800653 return mChannel->sendMessage(&msg);
654}
655
arthurhung7632c332020-12-30 16:58:01 +0800656status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
657 bool isExiting) {
658 if (ATRACE_ENABLED()) {
659 std::string message =
660 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
661 mChannel->getName().c_str(), x, y, toString(isExiting));
662 ATRACE_NAME(message.c_str());
663 }
664
665 InputMessage msg;
666 msg.header.type = InputMessage::Type::DRAG;
667 msg.header.seq = seq;
668 msg.body.drag.eventId = eventId;
669 msg.body.drag.isExiting = isExiting;
670 msg.body.drag.x = x;
671 msg.body.drag.y = y;
672 return mChannel->sendMessage(&msg);
673}
674
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700675status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
676 if (ATRACE_ENABLED()) {
677 std::string message =
678 StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
679 mChannel->getName().c_str(), toString(isInTouchMode));
680 ATRACE_NAME(message.c_str());
681 }
682
683 InputMessage msg;
684 msg.header.type = InputMessage::Type::TOUCH_MODE;
685 msg.header.seq = seq;
686 msg.body.touchMode.eventId = eventId;
687 msg.body.touchMode.isInTouchMode = isInTouchMode;
688 return mChannel->sendMessage(&msg);
689}
690
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000691android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800692 if (DEBUG_TRANSPORT_ACTIONS) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000693 ALOGD("channel '%s' publisher ~ %s", mChannel->getName().c_str(), __func__);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800694 }
Jeff Brown5912f952013-07-01 19:10:31 -0700695
696 InputMessage msg;
697 status_t result = mChannel->receiveMessage(&msg);
698 if (result) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000699 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700700 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000701 if (msg.header.type == InputMessage::Type::FINISHED) {
702 return Finished{
703 .seq = msg.header.seq,
704 .handled = msg.body.finished.handled,
705 .consumeTime = msg.body.finished.consumeTime,
706 };
Jeff Brown5912f952013-07-01 19:10:31 -0700707 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000708
709 if (msg.header.type == InputMessage::Type::TIMELINE) {
710 return Timeline{
711 .inputEventId = msg.body.timeline.eventId,
712 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
713 };
714 }
715
716 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
Dominik Laskowski75788452021-02-09 18:51:25 -0800717 mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000718 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700719}
720
721// --- InputConsumer ---
722
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500723InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800724 : InputConsumer(channel, isTouchResamplingEnabled()) {}
725
726InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel,
727 bool enableTouchResampling)
728 : mResampleTouch(enableTouchResampling), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700729
730InputConsumer::~InputConsumer() {
731}
732
733bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600734 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700735}
736
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800737status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
738 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800739 if (DEBUG_TRANSPORT_ACTIONS) {
740 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
741 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
742 }
Jeff Brown5912f952013-07-01 19:10:31 -0700743
744 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700745 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700746
747 // Fetch the next input message.
748 // Loop until an event can be returned or no additional events are received.
749 while (!*outEvent) {
750 if (mMsgDeferred) {
751 // mMsg contains a valid input message from the previous call to consume
752 // that has not yet been processed.
753 mMsgDeferred = false;
754 } else {
755 // Receive a fresh message.
756 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000757 if (result == OK) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800758 const auto [_, inserted] =
759 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
760 LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
761 mMsg.header.seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000762 }
Jeff Brown5912f952013-07-01 19:10:31 -0700763 if (result) {
764 // Consume the next batched event unless batches are being held for later.
765 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800766 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700767 if (*outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800768 if (DEBUG_TRANSPORT_ACTIONS) {
769 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
770 mChannel->getName().c_str(), *outSeq);
771 }
Jeff Brown5912f952013-07-01 19:10:31 -0700772 break;
773 }
774 }
775 return result;
776 }
777 }
778
779 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700780 case InputMessage::Type::KEY: {
781 KeyEvent* keyEvent = factory->createKeyEvent();
782 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700783
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700784 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500785 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700786 *outEvent = keyEvent;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800787 if (DEBUG_TRANSPORT_ACTIONS) {
788 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
789 mChannel->getName().c_str(), *outSeq);
790 }
Jeff Brown5912f952013-07-01 19:10:31 -0700791 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700792 }
Jeff Brown5912f952013-07-01 19:10:31 -0700793
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700794 case InputMessage::Type::MOTION: {
795 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
796 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500797 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700798 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500799 batch.samples.push_back(mMsg);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800800 if (DEBUG_TRANSPORT_ACTIONS) {
801 ALOGD("channel '%s' consumer ~ appended to batch event",
802 mChannel->getName().c_str());
803 }
Jeff Brown5912f952013-07-01 19:10:31 -0700804 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700805 } else if (isPointerEvent(mMsg.body.motion.source) &&
806 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
807 // No need to process events that we are going to cancel anyways
808 const size_t count = batch.samples.size();
809 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500810 const InputMessage& msg = batch.samples[i];
811 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700812 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500813 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
814 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700815 } else {
816 // We cannot append to the batch in progress, so we need to consume
817 // the previous batch right now and defer the new message until later.
818 mMsgDeferred = true;
819 status_t result = consumeSamples(factory, batch, batch.samples.size(),
820 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500821 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700822 if (result) {
823 return result;
824 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800825 if (DEBUG_TRANSPORT_ACTIONS) {
826 ALOGD("channel '%s' consumer ~ consumed batch event and "
827 "deferred current event, seq=%u",
828 mChannel->getName().c_str(), *outSeq);
829 }
Jeff Brown5912f952013-07-01 19:10:31 -0700830 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700831 }
Jeff Brown5912f952013-07-01 19:10:31 -0700832 }
Jeff Brown5912f952013-07-01 19:10:31 -0700833
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800834 // Start a new batch if needed.
835 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
836 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500837 Batch batch;
838 batch.samples.push_back(mMsg);
839 mBatches.push_back(batch);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800840 if (DEBUG_TRANSPORT_ACTIONS) {
841 ALOGD("channel '%s' consumer ~ started batch event",
842 mChannel->getName().c_str());
843 }
844 break;
845 }
Jeff Brown5912f952013-07-01 19:10:31 -0700846
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800847 MotionEvent* motionEvent = factory->createMotionEvent();
848 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700849
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800850 updateTouchState(mMsg);
851 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500852 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800853 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800854
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800855 if (DEBUG_TRANSPORT_ACTIONS) {
856 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
857 mChannel->getName().c_str(), *outSeq);
858 }
859 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700860 }
Jeff Brown5912f952013-07-01 19:10:31 -0700861
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000862 case InputMessage::Type::FINISHED:
863 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000864 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
865 "InputConsumer!",
Dominik Laskowski75788452021-02-09 18:51:25 -0800866 ftl::enum_string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800867 break;
868 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800869
870 case InputMessage::Type::FOCUS: {
871 FocusEvent* focusEvent = factory->createFocusEvent();
872 if (!focusEvent) return NO_MEMORY;
873
874 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500875 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800876 *outEvent = focusEvent;
877 break;
878 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800879
880 case InputMessage::Type::CAPTURE: {
881 CaptureEvent* captureEvent = factory->createCaptureEvent();
882 if (!captureEvent) return NO_MEMORY;
883
884 initializeCaptureEvent(captureEvent, &mMsg);
885 *outSeq = mMsg.header.seq;
886 *outEvent = captureEvent;
887 break;
888 }
arthurhung7632c332020-12-30 16:58:01 +0800889
890 case InputMessage::Type::DRAG: {
891 DragEvent* dragEvent = factory->createDragEvent();
892 if (!dragEvent) return NO_MEMORY;
893
894 initializeDragEvent(dragEvent, &mMsg);
895 *outSeq = mMsg.header.seq;
896 *outEvent = dragEvent;
897 break;
898 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700899
900 case InputMessage::Type::TOUCH_MODE: {
901 TouchModeEvent* touchModeEvent = factory->createTouchModeEvent();
902 if (!touchModeEvent) return NO_MEMORY;
903
904 initializeTouchModeEvent(touchModeEvent, &mMsg);
905 *outSeq = mMsg.header.seq;
906 *outEvent = touchModeEvent;
907 break;
908 }
Jeff Brown5912f952013-07-01 19:10:31 -0700909 }
910 }
911 return OK;
912}
913
914status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800915 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700916 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700917 for (size_t i = mBatches.size(); i > 0; ) {
918 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500919 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -0700920 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800921 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500922 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -0700923 return result;
924 }
925
Michael Wright32232172013-10-21 12:05:22 -0700926 nsecs_t sampleTime = frameTime;
927 if (mResampleTouch) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800928 sampleTime -= std::chrono::nanoseconds(RESAMPLE_LATENCY).count();
Michael Wright32232172013-10-21 12:05:22 -0700929 }
Jeff Brown5912f952013-07-01 19:10:31 -0700930 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
931 if (split < 0) {
932 continue;
933 }
934
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800935 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700936 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500937 if (batch.samples.empty()) {
938 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700939 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700940 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500941 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -0700942 }
Michael Wright32232172013-10-21 12:05:22 -0700943 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700944 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
945 }
946 return result;
947 }
948
949 return WOULD_BLOCK;
950}
951
952status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800953 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700954 MotionEvent* motionEvent = factory->createMotionEvent();
955 if (! motionEvent) return NO_MEMORY;
956
957 uint32_t chain = 0;
958 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500959 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100960 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700961 if (i) {
962 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500963 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700964 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500965 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -0700966 addSample(motionEvent, &msg);
967 } else {
968 initializeMotionEvent(motionEvent, &msg);
969 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500970 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -0700971 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500972 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -0700973
974 *outSeq = chain;
975 *outEvent = motionEvent;
976 return OK;
977}
978
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100979void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800980 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700981 return;
982 }
983
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100984 int32_t deviceId = msg.body.motion.deviceId;
985 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -0700986
987 // Update the touch state history to incorporate the new input message.
988 // If the message is in the past relative to the most recently produced resampled
989 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100990 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -0700991 case AMOTION_EVENT_ACTION_DOWN: {
992 ssize_t index = findTouchState(deviceId, source);
993 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500994 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -0700995 index = mTouchStates.size() - 1;
996 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500997 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -0700998 touchState.initialize(deviceId, source);
999 touchState.addHistory(msg);
1000 break;
1001 }
1002
1003 case AMOTION_EVENT_ACTION_MOVE: {
1004 ssize_t index = findTouchState(deviceId, source);
1005 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001006 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001007 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001008 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001009 }
1010 break;
1011 }
1012
1013 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
1014 ssize_t index = findTouchState(deviceId, source);
1015 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001016 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001017 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001018 rewriteMessage(touchState, msg);
1019 }
1020 break;
1021 }
1022
1023 case AMOTION_EVENT_ACTION_POINTER_UP: {
1024 ssize_t index = findTouchState(deviceId, source);
1025 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001026 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001027 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001028 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001029 }
1030 break;
1031 }
1032
1033 case AMOTION_EVENT_ACTION_SCROLL: {
1034 ssize_t index = findTouchState(deviceId, source);
1035 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001036 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001037 rewriteMessage(touchState, msg);
1038 }
1039 break;
1040 }
1041
1042 case AMOTION_EVENT_ACTION_UP:
1043 case AMOTION_EVENT_ACTION_CANCEL: {
1044 ssize_t index = findTouchState(deviceId, source);
1045 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001046 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001047 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001048 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -07001049 }
1050 break;
1051 }
1052 }
1053}
1054
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001055/**
1056 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1057 *
1058 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1059 * is in the past relative to msg and the past two events do not contain identical coordinates),
1060 * then invalidate the lastResample data for that pointer.
1061 * If the two past events have identical coordinates, then lastResample data for that pointer will
1062 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1063 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1064 * not equal to x0 is received.
1065 */
1066void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001067 nsecs_t eventTime = msg.body.motion.eventTime;
1068 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1069 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001070 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001071 if (eventTime < state.lastResample.eventTime ||
1072 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001073 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1074 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001075#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001076 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
1077 resampleCoords.getX(), resampleCoords.getY(),
1078 msgCoords.getX(), msgCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001079#endif
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001080 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1081 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
1082 } else {
1083 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001084 }
Jeff Brown5912f952013-07-01 19:10:31 -07001085 }
1086 }
1087}
1088
1089void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1090 const InputMessage* next) {
1091 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001092 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001093 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1094 return;
1095 }
1096
1097 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1098 if (index < 0) {
1099#if DEBUG_RESAMPLING
1100 ALOGD("Not resampled, no touch state for device.");
1101#endif
1102 return;
1103 }
1104
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001105 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001106 if (touchState.historySize < 1) {
1107#if DEBUG_RESAMPLING
1108 ALOGD("Not resampled, no history for device.");
1109#endif
1110 return;
1111 }
1112
1113 // Ensure that the current sample has all of the pointers that need to be reported.
1114 const History* current = touchState.getHistory(0);
1115 size_t pointerCount = event->getPointerCount();
1116 for (size_t i = 0; i < pointerCount; i++) {
1117 uint32_t id = event->getPointerId(i);
1118 if (!current->idBits.hasBit(id)) {
1119#if DEBUG_RESAMPLING
1120 ALOGD("Not resampled, missing id %d", id);
1121#endif
1122 return;
1123 }
1124 }
1125
1126 // Find the data to use for resampling.
1127 const History* other;
1128 History future;
1129 float alpha;
1130 if (next) {
1131 // Interpolate between current sample and future sample.
1132 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001133 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001134 other = &future;
1135 nsecs_t delta = future.eventTime - current->eventTime;
1136 if (delta < RESAMPLE_MIN_DELTA) {
1137#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001138 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001139#endif
1140 return;
1141 }
1142 alpha = float(sampleTime - current->eventTime) / delta;
1143 } else if (touchState.historySize >= 2) {
1144 // Extrapolate future sample using current sample and past sample.
1145 // So other->eventTime <= current->eventTime <= sampleTime.
1146 other = touchState.getHistory(1);
1147 nsecs_t delta = current->eventTime - other->eventTime;
1148 if (delta < RESAMPLE_MIN_DELTA) {
1149#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001150 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001151#endif
1152 return;
1153 } else if (delta > RESAMPLE_MAX_DELTA) {
1154#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001155 ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001156#endif
1157 return;
1158 }
1159 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1160 if (sampleTime > maxPredict) {
1161#if DEBUG_RESAMPLING
1162 ALOGD("Sample time is too far in the future, adjusting prediction "
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001163 "from %" PRId64 " to %" PRId64 " ns.",
Jeff Brown5912f952013-07-01 19:10:31 -07001164 sampleTime - current->eventTime, maxPredict - current->eventTime);
1165#endif
1166 sampleTime = maxPredict;
1167 }
1168 alpha = float(current->eventTime - sampleTime) / delta;
1169 } else {
1170#if DEBUG_RESAMPLING
1171 ALOGD("Not resampled, insufficient data.");
1172#endif
1173 return;
1174 }
1175
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001176 if (current->eventTime == sampleTime) {
1177 // Prevents having 2 events with identical times and coordinates.
1178 return;
1179 }
1180
Jeff Brown5912f952013-07-01 19:10:31 -07001181 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001182 History oldLastResample;
1183 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001184 touchState.lastResample.eventTime = sampleTime;
1185 touchState.lastResample.idBits.clear();
1186 for (size_t i = 0; i < pointerCount; i++) {
1187 uint32_t id = event->getPointerId(i);
1188 touchState.lastResample.idToIndex[id] = i;
1189 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001190 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1191 // We maintain the previously resampled value for this pointer (stored in
1192 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1193 // This way we don't introduce artificial jitter when pointers haven't actually moved.
1194
1195 // We know here that the coordinates for the pointer haven't changed because we
1196 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1197 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1198 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1199 continue;
1200 }
1201
Jeff Brown5912f952013-07-01 19:10:31 -07001202 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1203 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001204 resampledCoords.copyFrom(currentCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001205 if (other->idBits.hasBit(id)
1206 && shouldResampleTool(event->getToolType(i))) {
1207 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001208 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
1209 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
1210 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
1211 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
1212#if DEBUG_RESAMPLING
1213 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1214 "other (%0.3f, %0.3f), alpha %0.3f",
1215 id, resampledCoords.getX(), resampledCoords.getY(),
1216 currentCoords.getX(), currentCoords.getY(),
1217 otherCoords.getX(), otherCoords.getY(),
1218 alpha);
1219#endif
1220 } else {
Jeff Brown5912f952013-07-01 19:10:31 -07001221#if DEBUG_RESAMPLING
1222 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
1223 id, resampledCoords.getX(), resampledCoords.getY(),
1224 currentCoords.getX(), currentCoords.getY());
1225#endif
1226 }
1227 }
1228
1229 event->addSample(sampleTime, touchState.lastResample.pointers);
1230}
1231
1232bool InputConsumer::shouldResampleTool(int32_t toolType) {
1233 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1234 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1235}
1236
1237status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08001238 if (DEBUG_TRANSPORT_ACTIONS) {
1239 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1240 mChannel->getName().c_str(), seq, toString(handled));
1241 }
Jeff Brown5912f952013-07-01 19:10:31 -07001242
1243 if (!seq) {
1244 ALOGE("Attempted to send a finished signal with sequence number 0.");
1245 return BAD_VALUE;
1246 }
1247
1248 // Send finished signals for the batch sequence chain first.
1249 size_t seqChainCount = mSeqChains.size();
1250 if (seqChainCount) {
1251 uint32_t currentSeq = seq;
1252 uint32_t chainSeqs[seqChainCount];
1253 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001254 for (size_t i = seqChainCount; i > 0; ) {
1255 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001256 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001257 if (seqChain.seq == currentSeq) {
1258 currentSeq = seqChain.chain;
1259 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001260 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001261 }
1262 }
1263 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001264 while (!status && chainIndex > 0) {
1265 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001266 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1267 }
1268 if (status) {
1269 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001270 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001271 SeqChain seqChain;
1272 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1273 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001274 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001275 if (!chainIndex) break;
1276 chainIndex--;
1277 }
Jeff Brown5912f952013-07-01 19:10:31 -07001278 return status;
1279 }
1280 }
1281
1282 // Send finished signal for the last message in the batch.
1283 return sendUnchainedFinishedSignal(seq, handled);
1284}
1285
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001286status_t InputConsumer::sendTimeline(int32_t inputEventId,
1287 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
1288 if (DEBUG_TRANSPORT_ACTIONS) {
1289 ALOGD("channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1290 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1291 mChannel->getName().c_str(), inputEventId,
1292 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1293 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
1294 }
1295
1296 InputMessage msg;
1297 msg.header.type = InputMessage::Type::TIMELINE;
1298 msg.header.seq = 0;
1299 msg.body.timeline.eventId = inputEventId;
1300 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1301 return mChannel->sendMessage(&msg);
1302}
1303
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001304nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1305 auto it = mConsumeTimes.find(seq);
1306 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1307 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1308 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1309 seq);
1310 return it->second;
1311}
1312
1313void InputConsumer::popConsumeTime(uint32_t seq) {
1314 mConsumeTimes.erase(seq);
1315}
1316
Jeff Brown5912f952013-07-01 19:10:31 -07001317status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1318 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001319 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001320 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001321 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001322 msg.body.finished.consumeTime = getConsumeTime(seq);
1323 status_t result = mChannel->sendMessage(&msg);
1324 if (result == OK) {
1325 // Remove the consume time if the socket write succeeded. We will not need to ack this
1326 // message anymore. If the socket write did not succeed, we will try again and will still
1327 // need consume time.
1328 popConsumeTime(seq);
1329 }
1330 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001331}
1332
Jeff Brown5912f952013-07-01 19:10:31 -07001333bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001334 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001335}
1336
Arthur Hungc7812be2020-02-27 22:40:27 +08001337int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001338 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001339 return AINPUT_SOURCE_CLASS_NONE;
1340 }
1341
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001342 const Batch& batch = mBatches[0];
1343 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001344 return head.body.motion.source;
1345}
1346
Jeff Brown5912f952013-07-01 19:10:31 -07001347ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1348 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001349 const Batch& batch = mBatches[i];
1350 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001351 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1352 return i;
1353 }
1354 }
1355 return -1;
1356}
1357
1358ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1359 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001360 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001361 if (touchState.deviceId == deviceId && touchState.source == source) {
1362 return i;
1363 }
1364 }
1365 return -1;
1366}
1367
1368void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001369 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001370 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1371 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1372 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1373 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001374}
1375
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001376void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001377 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001378}
1379
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001380void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001381 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001382}
1383
arthurhung7632c332020-12-30 16:58:01 +08001384void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1385 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1386 msg->body.drag.isExiting);
1387}
1388
Jeff Brown5912f952013-07-01 19:10:31 -07001389void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001390 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001391 PointerProperties pointerProperties[pointerCount];
1392 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001393 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001394 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1395 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1396 }
1397
chaviw9eaa22c2020-07-01 16:21:27 -07001398 ui::Transform transform;
1399 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1400 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001401 ui::Transform displayTransform;
1402 displayTransform.set({msg->body.motion.dsdxRaw, msg->body.motion.dtdxRaw,
1403 msg->body.motion.txRaw, msg->body.motion.dtdyRaw,
1404 msg->body.motion.dsdyRaw, msg->body.motion.tyRaw, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001405 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1406 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1407 msg->body.motion.actionButton, msg->body.motion.flags,
1408 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001409 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1410 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1411 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001412 displayTransform, msg->body.motion.downTime, msg->body.motion.eventTime,
1413 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001414}
1415
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001416void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
1417 event->initialize(msg->body.touchMode.eventId, msg->body.touchMode.isInTouchMode);
1418}
1419
Jeff Brown5912f952013-07-01 19:10:31 -07001420void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001421 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001422 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001423 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001424 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1425 }
1426
1427 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1428 event->addSample(msg->body.motion.eventTime, pointerCoords);
1429}
1430
1431bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001432 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001433 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001434 if (head.body.motion.pointerCount != pointerCount
1435 || head.body.motion.action != msg->body.motion.action) {
1436 return false;
1437 }
1438 for (size_t i = 0; i < pointerCount; i++) {
1439 if (head.body.motion.pointers[i].properties
1440 != msg->body.motion.pointers[i].properties) {
1441 return false;
1442 }
1443 }
1444 return true;
1445}
1446
1447ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1448 size_t numSamples = batch.samples.size();
1449 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001450 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001451 index += 1;
1452 }
1453 return ssize_t(index) - 1;
1454}
1455
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001456std::string InputConsumer::dump() const {
1457 std::string out;
1458 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1459 out = out + "mChannel = " + mChannel->getName() + "\n";
1460 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1461 if (mMsgDeferred) {
Dominik Laskowski75788452021-02-09 18:51:25 -08001462 out = out + "mMsg : " + ftl::enum_string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001463 }
1464 out += "Batches:\n";
1465 for (const Batch& batch : mBatches) {
1466 out += " Batch:\n";
1467 for (const InputMessage& msg : batch.samples) {
1468 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Dominik Laskowski75788452021-02-09 18:51:25 -08001469 ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001470 switch (msg.header.type) {
1471 case InputMessage::Type::KEY: {
1472 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1473 KeyEvent::actionToString(
1474 msg.body.key.action),
1475 msg.body.key.keyCode);
1476 break;
1477 }
1478 case InputMessage::Type::MOTION: {
1479 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1480 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1481 const float x = msg.body.motion.pointers[i].coords.getX();
1482 const float y = msg.body.motion.pointers[i].coords.getY();
1483 out += android::base::StringPrintf("\n Pointer %" PRIu32
1484 " : x=%.1f y=%.1f",
1485 i, x, y);
1486 }
1487 break;
1488 }
1489 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001490 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1491 toString(msg.body.finished.handled),
1492 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001493 break;
1494 }
1495 case InputMessage::Type::FOCUS: {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001496 out += android::base::StringPrintf("hasFocus=%s",
1497 toString(msg.body.focus.hasFocus));
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001498 break;
1499 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001500 case InputMessage::Type::CAPTURE: {
1501 out += android::base::StringPrintf("hasCapture=%s",
1502 toString(msg.body.capture
1503 .pointerCaptureEnabled));
1504 break;
1505 }
arthurhung7632c332020-12-30 16:58:01 +08001506 case InputMessage::Type::DRAG: {
1507 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1508 msg.body.drag.x, msg.body.drag.y,
1509 toString(msg.body.drag.isExiting));
1510 break;
1511 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001512 case InputMessage::Type::TIMELINE: {
1513 const nsecs_t gpuCompletedTime =
1514 msg.body.timeline
1515 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1516 const nsecs_t presentTime =
1517 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1518 out += android::base::StringPrintf("inputEventId=%" PRId32
1519 ", gpuCompletedTime=%" PRId64
1520 ", presentTime=%" PRId64,
1521 msg.body.timeline.eventId, gpuCompletedTime,
1522 presentTime);
1523 break;
1524 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001525 case InputMessage::Type::TOUCH_MODE: {
1526 out += android::base::StringPrintf("isInTouchMode=%s",
1527 toString(msg.body.touchMode.isInTouchMode));
1528 break;
1529 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001530 }
1531 out += "\n";
1532 }
1533 }
1534 if (mBatches.empty()) {
1535 out += " <empty>\n";
1536 }
1537 out += "mSeqChains:\n";
1538 for (const SeqChain& chain : mSeqChains) {
1539 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1540 chain.chain);
1541 }
1542 if (mSeqChains.empty()) {
1543 out += " <empty>\n";
1544 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001545 out += "mConsumeTimes:\n";
1546 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1547 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1548 consumeTime);
1549 }
1550 if (mConsumeTimes.empty()) {
1551 out += " <empty>\n";
1552 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001553 return out;
1554}
1555
Jeff Brown5912f952013-07-01 19:10:31 -07001556} // namespace android