@@ -162,12 +162,12 @@ int32_t AInputQueue::hasEvents() {
162162int32_t AInputQueue::getEvent (AInputEvent** outEvent) {
163163 *outEvent = NULL ;
164164
165- bool finishNow = false ;
166-
167165 char byteread;
168166 ssize_t nRead = read (mDispatchKeyRead , &byteread, 1 );
167+
168+ Mutex::Autolock _l (mLock );
169+
169170 if (nRead == 1 ) {
170- mLock .lock ();
171171 if (mDispatchingKeys .size () > 0 ) {
172172 KeyEvent* kevent = mDispatchingKeys [0 ];
173173 *outEvent = kevent;
@@ -178,6 +178,8 @@ int32_t AInputQueue::getEvent(AInputEvent** outEvent) {
178178 inflight.finishSeq = 0 ;
179179 mInFlightEvents .push (inflight);
180180 }
181+
182+ bool finishNow = false ;
181183 if (mFinishPreDispatches .size () > 0 ) {
182184 finish_pre_dispatch finish (mFinishPreDispatches [0 ]);
183185 mFinishPreDispatches .removeAt (0 );
@@ -193,7 +195,6 @@ int32_t AInputQueue::getEvent(AInputEvent** outEvent) {
193195 ALOGW (" getEvent couldn't find inflight for seq %d" , finish.seq );
194196 }
195197 }
196- mLock .unlock ();
197198
198199 if (finishNow) {
199200 finishEvent (*outEvent, true , false );
@@ -206,7 +207,8 @@ int32_t AInputQueue::getEvent(AInputEvent** outEvent) {
206207
207208 uint32_t consumerSeq;
208209 InputEvent* myEvent = NULL ;
209- status_t res = mConsumer .consume (this , true /* consumeBatches*/ , &consumerSeq, &myEvent);
210+ status_t res = mConsumer .consume (&mPooledInputEventFactory , true /* consumeBatches*/ ,
211+ &consumerSeq, &myEvent);
210212 if (res != android::OK) {
211213 if (res != android::WOULD_BLOCK) {
212214 ALOGW (" channel '%s' ~ Failed to consume input event. status=%d" ,
@@ -215,6 +217,10 @@ int32_t AInputQueue::getEvent(AInputEvent** outEvent) {
215217 return -1 ;
216218 }
217219
220+ if (mConsumer .hasDeferredEvent ()) {
221+ wakeupDispatchLocked ();
222+ }
223+
218224 in_flight_event inflight;
219225 inflight.event = myEvent;
220226 inflight.seq = -1 ;
@@ -255,7 +261,8 @@ void AInputQueue::finishEvent(AInputEvent* event, bool handled, bool didDefaultH
255261 return ;
256262 }
257263
258- mLock .lock ();
264+ Mutex::Autolock _l (mLock );
265+
259266 const size_t N = mInFlightEvents .size ();
260267 for (size_t i=0 ; i<N; i++) {
261268 const in_flight_event& inflight (mInFlightEvents [i]);
@@ -267,111 +274,82 @@ void AInputQueue::finishEvent(AInputEvent* event, bool handled, bool didDefaultH
267274 mConsumer .getChannel ()->getName ().string (), res);
268275 }
269276 }
270- if (static_cast <InputEvent*>(event)->getType () == AINPUT_EVENT_TYPE_KEY) {
271- mAvailKeyEvents .push (static_cast <KeyEvent*>(event));
272- } else {
273- mAvailMotionEvents .push (static_cast <MotionEvent*>(event));
274- }
277+ mPooledInputEventFactory .recycle (static_cast <InputEvent*>(event));
275278 mInFlightEvents .removeAt (i);
276- mLock .unlock ();
277279 return ;
278280 }
279281 }
280- mLock .unlock ();
281-
282+
282283 ALOGW (" finishEvent called for unknown event: %p" , event);
283284}
284285
285286void AInputQueue::dispatchEvent (android::KeyEvent* event) {
286- mLock .lock ();
287+ Mutex::Autolock _l (mLock );
288+
287289 LOG_TRACE (" dispatchEvent: dispatching=%d write=%d\n " , mDispatchingKeys .size (),
288290 mDispatchKeyWrite );
289291 mDispatchingKeys .add (event);
290- wakeupDispatch ();
291- mLock .unlock ();
292+ wakeupDispatchLocked ();
292293}
293294
294295void AInputQueue::finishPreDispatch (int seq, bool handled) {
295- mLock .lock ();
296+ Mutex::Autolock _l (mLock );
297+
296298 LOG_TRACE (" finishPreDispatch: seq=%d handled=%d\n " , seq, handled ? 1 : 0 );
297299 finish_pre_dispatch finish;
298300 finish.seq = seq;
299301 finish.handled = handled;
300302 mFinishPreDispatches .add (finish);
301- wakeupDispatch ();
302- mLock .unlock ();
303+ wakeupDispatchLocked ();
303304}
304305
305306KeyEvent* AInputQueue::consumeUnhandledEvent () {
306- KeyEvent* event = NULL ;
307+ Mutex::Autolock _l ( mLock ) ;
307308
308- mLock . lock () ;
309+ KeyEvent* event = NULL ;
309310 if (mUnhandledKeys .size () > 0 ) {
310311 event = mUnhandledKeys [0 ];
311312 mUnhandledKeys .removeAt (0 );
312313 }
313- mLock .unlock ();
314314
315315 LOG_TRACE (" consumeUnhandledEvent: KeyEvent=%p" , event);
316-
317316 return event;
318317}
319318
320319KeyEvent* AInputQueue::consumePreDispatchingEvent (int * outSeq) {
321- KeyEvent* event = NULL ;
320+ Mutex::Autolock _l ( mLock ) ;
322321
323- mLock . lock () ;
322+ KeyEvent* event = NULL ;
324323 if (mPreDispatchingKeys .size () > 0 ) {
325324 const in_flight_event& inflight (mPreDispatchingKeys [0 ]);
326325 event = static_cast <KeyEvent*>(inflight.event );
327326 *outSeq = inflight.seq ;
328327 mPreDispatchingKeys .removeAt (0 );
329328 }
330- mLock .unlock ();
331329
332330 LOG_TRACE (" consumePreDispatchingEvent: KeyEvent=%p" , event);
333-
334331 return event;
335332}
336333
337334KeyEvent* AInputQueue::createKeyEvent () {
338- mLock .lock ();
339- KeyEvent* event;
340- if (mAvailKeyEvents .size () <= 0 ) {
341- event = new KeyEvent ();
342- } else {
343- event = mAvailKeyEvents .top ();
344- mAvailKeyEvents .pop ();
345- }
346- mLock .unlock ();
347- return event;
348- }
335+ Mutex::Autolock _l (mLock );
349336
350- MotionEvent* AInputQueue::createMotionEvent () {
351- mLock .lock ();
352- MotionEvent* event;
353- if (mAvailMotionEvents .size () <= 0 ) {
354- event = new MotionEvent ();
355- } else {
356- event = mAvailMotionEvents .top ();
357- mAvailMotionEvents .pop ();
358- }
359- mLock .unlock ();
360- return event;
337+ return mPooledInputEventFactory .createKeyEvent ();
361338}
362339
363340void AInputQueue::doUnhandledKey (KeyEvent* keyEvent) {
364- mLock .lock ();
341+ Mutex::Autolock _l (mLock );
342+
365343 LOG_TRACE (" Unhandled key: pending=%d write=%d\n " , mUnhandledKeys .size (), mWorkWrite );
366344 if (mUnhandledKeys .size () <= 0 && mWorkWrite >= 0 ) {
367345 write_work (mWorkWrite , CMD_DEF_KEY);
368346 }
369347 mUnhandledKeys .add (keyEvent);
370- mLock .unlock ();
371348}
372349
373350bool AInputQueue::preDispatchKey (KeyEvent* keyEvent) {
374- mLock .lock ();
351+ Mutex::Autolock _l (mLock );
352+
375353 LOG_TRACE (" preDispatch key: pending=%d write=%d\n " , mPreDispatchingKeys .size (), mWorkWrite );
376354 const size_t N = mInFlightEvents .size ();
377355 for (size_t i=0 ; i<N; i++) {
@@ -380,7 +358,6 @@ bool AInputQueue::preDispatchKey(KeyEvent* keyEvent) {
380358 if (inflight.seq >= 0 ) {
381359 // This event has already been pre-dispatched!
382360 LOG_TRACE (" Event already pre-dispatched!" );
383- mLock .unlock ();
384361 return false ;
385362 }
386363 mSeq ++;
@@ -391,7 +368,6 @@ bool AInputQueue::preDispatchKey(KeyEvent* keyEvent) {
391368 write_work (mWorkWrite , CMD_DEF_KEY);
392369 }
393370 mPreDispatchingKeys .add (inflight);
394- mLock .unlock ();
395371 return true ;
396372 }
397373 }
@@ -400,7 +376,7 @@ bool AInputQueue::preDispatchKey(KeyEvent* keyEvent) {
400376 return false ;
401377}
402378
403- void AInputQueue::wakeupDispatch () {
379+ void AInputQueue::wakeupDispatchLocked () {
404380restart:
405381 char dummy = 0 ;
406382 int res = write (mDispatchKeyWrite , &dummy, sizeof (dummy));
0 commit comments