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