根據(jù)http://m.itdecent.cn/p/c2e26c6d4ac1
Android處理down的時候是直接處理的, 但是處理move的時候需要等待vsync.
但是為什么要等待vsync呢? 這個等待時間能不能去掉, 以優(yōu)化latency?
我的理解是: 不能. Android這么設(shè)計是有原因的.
前提假設(shè):
1. 一個batch中的move間隔很短. (<16ms)
2. 下一幀的畫面更新只能根據(jù)一個move事件來update.
(因為假設(shè)app在一幀以內(nèi)收到了好幾個move事件, 它不可能在下一幀同時響應(yīng)這幾個move事件. 一次畫面更新只能對應(yīng)一個事件)
所以這兩個前提推出一個結(jié)論就是:
1. 一個batch中的move事件需要進行合并, 合并成一個或兩個Motionevent.
那么以什么標(biāo)準(zhǔn), 怎么樣去合并move呢?
我只能想到以vsync來作為標(biāo)準(zhǔn). 因為很可能會出現(xiàn)下面這種情況. 一個batch的move跨越了一個vsync. 所以這個batch的move應(yīng)該根據(jù)vsync時間來劃分成兩個event.

所以move的合并過程會依賴于vsync的時間戳. 所以只能等vsync到了, 才能做這個事情.
相關(guān)代碼: (合并多個move為一個MotionEvent)
661 status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
662 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent, int32_t* displayId) {
663 MotionEvent* motionEvent = factory->createMotionEvent();
664 if (! motionEvent) return NO_MEMORY;
665
666 uint32_t chain = 0;
667 for (size_t i = 0; i < count; i++) { // count指的是batch中第一個晚于vsync的move的index
668 InputMessage& msg = batch.samples.editItemAt(i);
669 updateTouchState(msg);
670 if (i) {
671 SeqChain seqChain;
672 seqChain.seq = msg.body.motion.seq;
673 seqChain.chain = chain;
674 mSeqChains.push(seqChain);
675 addSample(motionEvent, &msg);
676 } else {
677 *displayId = msg.body.motion.displayId;
678 initializeMotionEvent(motionEvent, &msg);
679 }
680 chain = msg.body.motion.seq;
681 }
682 batch.samples.removeItemsAt(0, count);
683
684 *outSeq = chain;
685 *outEvent = motionEvent;
686 return OK;
687 }