TimedEventQueue: improve wakelock management

Do not acquire a wakelock unless the event is delayed by more
than a given time. This prevents from acquiring/releasing
wakelocks too often which has adverse effects on performance.

Bug: 11509471.
Change-Id: Id06803b393b40c4db5095a6e7ec02339fa581a38
diff --git a/media/libstagefright/TimedEventQueue.cpp b/media/libstagefright/TimedEventQueue.cpp
index 6a16bb4..1a9a26b 100644
--- a/media/libstagefright/TimedEventQueue.cpp
+++ b/media/libstagefright/TimedEventQueue.cpp
@@ -38,11 +38,14 @@
 
 namespace android {
 
+static int64_t kWakelockMinDelay = 100000ll;  // 100ms
+
 TimedEventQueue::TimedEventQueue()
     : mNextEventID(1),
       mRunning(false),
       mStopped(false),
-      mDeathRecipient(new PMDeathRecipient(this)) {
+      mDeathRecipient(new PMDeathRecipient(this)),
+      mWakeLockCount(0) {
 }
 
 TimedEventQueue::~TimedEventQueue() {
@@ -87,9 +90,7 @@
 
     // some events may be left in the queue if we did not flush and the wake lock
     // must be released.
-    if (!mQueue.empty()) {
-        releaseWakeLock_l();
-    }
+    releaseWakeLock_l(true /*force*/);
     mQueue.clear();
 
     mRunning = false;
@@ -126,13 +127,15 @@
     QueueItem item;
     item.event = event;
     item.realtime_us = realtime_us;
+    item.has_wakelock = false;
 
     if (it == mQueue.begin()) {
         mQueueHeadChangedCondition.signal();
     }
 
-    if (mQueue.empty()) {
+    if (realtime_us > ALooper::GetNowUs() + kWakelockMinDelay) {
         acquireWakeLock_l();
+        item.has_wakelock = true;
     }
     mQueue.insert(it, item);
 
@@ -188,10 +191,10 @@
         ALOGV("cancelling event %d", (*it).event->eventID());
 
         (*it).event->setEventID(0);
-        it = mQueue.erase(it);
-        if (mQueue.empty()) {
+        if ((*it).has_wakelock) {
             releaseWakeLock_l();
         }
+        it = mQueue.erase(it);
         if (stopAfterFirstMatch) {
             return;
         }
@@ -297,11 +300,10 @@
         if ((*it).event->eventID() == id) {
             sp<Event> event = (*it).event;
             event->setEventID(0);
-
-            mQueue.erase(it);
-            if (mQueue.empty()) {
+            if ((*it).has_wakelock) {
                 releaseWakeLock_l();
             }
+            mQueue.erase(it);
             return event;
         }
     }
@@ -313,51 +315,59 @@
 
 void TimedEventQueue::acquireWakeLock_l()
 {
-    if (mWakeLockToken != 0) {
-        return;
-    }
-    if (mPowerManager == 0) {
-        // use checkService() to avoid blocking if power service is not up yet
-        sp<IBinder> binder =
-            defaultServiceManager()->checkService(String16("power"));
-        if (binder == 0) {
-            ALOGW("cannot connect to the power manager service");
-        } else {
-            mPowerManager = interface_cast<IPowerManager>(binder);
-            binder->linkToDeath(mDeathRecipient);
+    if (mWakeLockCount++ == 0) {
+        CHECK(mWakeLockToken == 0);
+        if (mPowerManager == 0) {
+            // use checkService() to avoid blocking if power service is not up yet
+            sp<IBinder> binder =
+                defaultServiceManager()->checkService(String16("power"));
+            if (binder == 0) {
+                ALOGW("cannot connect to the power manager service");
+            } else {
+                mPowerManager = interface_cast<IPowerManager>(binder);
+                binder->linkToDeath(mDeathRecipient);
+            }
         }
-    }
-    if (mPowerManager != 0) {
-        sp<IBinder> binder = new BBinder();
-        int64_t token = IPCThreadState::self()->clearCallingIdentity();
-        status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
-                                                         binder,
-                                                         String16("TimedEventQueue"),
-                                                         String16("media"));
-        IPCThreadState::self()->restoreCallingIdentity(token);
-        if (status == NO_ERROR) {
-            mWakeLockToken = binder;
+        if (mPowerManager != 0) {
+            sp<IBinder> binder = new BBinder();
+            int64_t token = IPCThreadState::self()->clearCallingIdentity();
+            status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
+                                                             binder,
+                                                             String16("TimedEventQueue"),
+                                                             String16("media"));
+            IPCThreadState::self()->restoreCallingIdentity(token);
+            if (status == NO_ERROR) {
+                mWakeLockToken = binder;
+            }
         }
     }
 }
 
-void TimedEventQueue::releaseWakeLock_l()
+void TimedEventQueue::releaseWakeLock_l(bool force)
 {
-    if (mWakeLockToken == 0) {
-        return;
+    if (force) {
+        if (mWakeLockCount == 0) {
+            return;
+        }
+        // Force wakelock release below by setting reference count to 1.
+        mWakeLockCount = 1;
     }
-    if (mPowerManager != 0) {
-        int64_t token = IPCThreadState::self()->clearCallingIdentity();
-        mPowerManager->releaseWakeLock(mWakeLockToken, 0);
-        IPCThreadState::self()->restoreCallingIdentity(token);
+    CHECK(mWakeLockCount != 0);
+    if (--mWakeLockCount == 0) {
+        CHECK(mWakeLockToken != 0);
+        if (mPowerManager != 0) {
+            int64_t token = IPCThreadState::self()->clearCallingIdentity();
+            mPowerManager->releaseWakeLock(mWakeLockToken, 0);
+            IPCThreadState::self()->restoreCallingIdentity(token);
+        }
+        mWakeLockToken.clear();
     }
-    mWakeLockToken.clear();
 }
 
 void TimedEventQueue::clearPowerManager()
 {
     Mutex::Autolock _l(mLock);
-    releaseWakeLock_l();
+    releaseWakeLock_l(true /*force*/);
     mPowerManager.clear();
 }
 
diff --git a/media/libstagefright/include/TimedEventQueue.h b/media/libstagefright/include/TimedEventQueue.h
index 4e49c83..38a08b1 100644
--- a/media/libstagefright/include/TimedEventQueue.h
+++ b/media/libstagefright/include/TimedEventQueue.h
@@ -118,6 +118,7 @@
     struct QueueItem {
         sp<Event> event;
         int64_t realtime_us;
+        bool has_wakelock;
     };
 
     struct StopEvent : public TimedEventQueue::Event {
@@ -139,6 +140,7 @@
     sp<IPowerManager>       mPowerManager;
     sp<IBinder>             mWakeLockToken;
     const sp<PMDeathRecipient> mDeathRecipient;
+    uint32_t                mWakeLockCount;
 
     static void *ThreadWrapper(void *me);
     void threadEntry();
@@ -146,7 +148,7 @@
     sp<Event> removeEventFromQueue_l(event_id id);
 
     void acquireWakeLock_l();
-    void releaseWakeLock_l();
+    void releaseWakeLock_l(bool force = false);
 
     TimedEventQueue(const TimedEventQueue &);
     TimedEventQueue &operator=(const TimedEventQueue &);