Skip to content

Commit 47a4a50

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Improve touch event resampling." into jb-dev
2 parents 8ce2d78 + 7174a49 commit 47a4a50

File tree

4 files changed

+176
-70
lines changed

4 files changed

+176
-70
lines changed

include/androidfw/Input.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ struct PointerCoords {
176176
status_t setAxisValue(int32_t axis, float value);
177177

178178
void scale(float scale);
179-
void lerp(const PointerCoords& a, const PointerCoords& b, float alpha);
180179

181180
inline float getX() const {
182181
return getAxisValue(AMOTION_EVENT_AXIS_X);

include/androidfw/InputTransport.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ struct InputMessage {
9292
PointerCoords coords;
9393
} pointers[MAX_POINTERS];
9494

95+
int32_t getActionId() const {
96+
uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
97+
>> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
98+
return pointers[index].properties.id;
99+
}
100+
95101
inline size_t size() const {
96102
return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
97103
+ sizeof(Pointer) * pointerCount;
@@ -322,6 +328,10 @@ class InputConsumer {
322328
bool hasPendingBatch() const;
323329

324330
private:
331+
// True if touch resampling is enabled.
332+
const bool mResampleTouch;
333+
334+
// The input channel.
325335
sp<InputChannel> mChannel;
326336

327337
// The current input message.
@@ -341,6 +351,7 @@ class InputConsumer {
341351
struct History {
342352
nsecs_t eventTime;
343353
BitSet32 idBits;
354+
int32_t idToIndex[MAX_POINTER_ID + 1];
344355
PointerCoords pointers[MAX_POINTERS];
345356

346357
void initializeFrom(const InputMessage* msg) {
@@ -349,23 +360,30 @@ class InputConsumer {
349360
for (size_t i = 0; i < msg->body.motion.pointerCount; i++) {
350361
uint32_t id = msg->body.motion.pointers[i].properties.id;
351362
idBits.markBit(id);
352-
size_t index = idBits.getIndexOfBit(id);
353-
pointers[index].copyFrom(msg->body.motion.pointers[i].coords);
363+
idToIndex[id] = i;
364+
pointers[i].copyFrom(msg->body.motion.pointers[i].coords);
354365
}
355366
}
367+
368+
const PointerCoords& getPointerById(uint32_t id) const {
369+
return pointers[idToIndex[id]];
370+
}
356371
};
357372
struct TouchState {
358373
int32_t deviceId;
359374
int32_t source;
360375
size_t historyCurrent;
361376
size_t historySize;
362377
History history[2];
378+
History lastResample;
363379

364380
void initialize(int32_t deviceId, int32_t source) {
365381
this->deviceId = deviceId;
366382
this->source = source;
367383
historyCurrent = 0;
368384
historySize = 0;
385+
lastResample.eventTime = 0;
386+
lastResample.idBits.clear();
369387
}
370388

371389
void addHistory(const InputMessage* msg) {
@@ -398,6 +416,7 @@ class InputConsumer {
398416
Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
399417

400418
void updateTouchState(InputMessage* msg);
419+
void rewriteMessage(const TouchState& state, InputMessage* msg);
401420
void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
402421
const InputMessage *next);
403422

@@ -412,6 +431,8 @@ class InputConsumer {
412431
static bool canAddSample(const Batch& batch, const InputMessage* msg);
413432
static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
414433
static bool shouldResampleTool(int32_t toolType);
434+
435+
static bool isTouchResamplingEnabled();
415436
};
416437

417438
} // namespace android

libs/androidfw/Input.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,6 @@ void PointerCoords::scale(float scaleFactor) {
211211
scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, scaleFactor);
212212
}
213213

214-
void PointerCoords::lerp(const PointerCoords& a, const PointerCoords& b, float alpha) {
215-
bits = 0;
216-
for (uint64_t bitsRemaining = a.bits | b.bits; bitsRemaining; ) {
217-
int32_t axis = __builtin_ctz(bitsRemaining);
218-
uint64_t axisBit = 1LL << axis;
219-
bitsRemaining &= ~axisBit;
220-
if (a.bits & axisBit) {
221-
if (b.bits & axisBit) {
222-
float aval = a.getAxisValue(axis);
223-
float bval = b.getAxisValue(axis);
224-
setAxisValue(axis, aval + alpha * (bval - aval));
225-
} else {
226-
setAxisValue(axis, a.getAxisValue(axis));
227-
}
228-
} else {
229-
setAxisValue(axis, b.getAxisValue(axis));
230-
}
231-
}
232-
}
233-
234214
#ifdef HAVE_ANDROID_OS
235215
status_t PointerCoords::readFromParcel(Parcel* parcel) {
236216
bits = parcel->readInt64();

0 commit comments

Comments
 (0)