Camera2: Fix deadlock issues with getInProgressCount

Bug: 6243944
Change-Id: Icb32e4b8c4a916eb7c97ac18767cadfc5122ec88
diff --git a/tools/emulator/system/camera/EmulatedFakeCamera2.cpp b/tools/emulator/system/camera/EmulatedFakeCamera2.cpp
index 711c2d2..9fcab91 100644
--- a/tools/emulator/system/camera/EmulatedFakeCamera2.cpp
+++ b/tools/emulator/system/camera/EmulatedFakeCamera2.cpp
@@ -517,7 +517,9 @@
 
 EmulatedFakeCamera2::ConfigureThread::ConfigureThread(EmulatedFakeCamera2 *parent):
         Thread(false),
-        mParent(parent) {
+        mParent(parent),
+        mNextBuffers(NULL),
+        mRequestCount(0) {
     mRunning = false;
 }
 
@@ -567,8 +569,8 @@
 }
 
 int EmulatedFakeCamera2::ConfigureThread::getInProgressCount() {
-    Mutex::Autolock lock(mInternalsMutex);
-    return mNextBuffers == NULL ? 0 : 1;
+    Mutex::Autolock lock(mInputMutex);
+    return mRequestCount;
 }
 
 bool EmulatedFakeCamera2::ConfigureThread::threadLoop() {
@@ -610,6 +612,9 @@
             Mutex::Autolock lock(mInputMutex);
             mActive = false;
             return true;
+        } else {
+            Mutex::Autolock lock(mInputMutex);
+            mRequestCount++;
         }
         // Get necessary parameters for sensor config
 
@@ -770,6 +775,10 @@
     mRequest = NULL;
     mNextBuffers = NULL;
 
+    Mutex::Autolock lock(mInputMutex);
+    mRequestCount--;
+
+
     return true;
 }
 
@@ -778,7 +787,9 @@
         mParent(parent),
         mRunning(false),
         mActive(false),
-        mRequest(NULL)
+        mRequest(NULL),
+        mBuffers(NULL),
+        mRequestCount(0)
 {
     mInFlightQueue = new InFlightQueue[kInFlightQueueSize];
     mInFlightHead = 0;
@@ -818,6 +829,7 @@
     mInFlightQueue[mInFlightTail].request = request;
     mInFlightQueue[mInFlightTail].buffers = buffers;
     mInFlightTail = (mInFlightTail + 1) % kInFlightQueueSize;
+    mRequestCount++;
 
     if (!mActive) {
         mActive = true;
@@ -850,14 +862,8 @@
 
 int EmulatedFakeCamera2::ReadoutThread::getInProgressCount() {
     Mutex::Autolock lock(mInputMutex);
-    Mutex::Autolock iLock(mInternalsMutex);
 
-    int requestCount =
-            ((mInFlightTail + kInFlightQueueSize) - mInFlightHead)
-            % kInFlightQueueSize;
-    requestCount += (mBuffers == NULL) ? 0 : 1;
-
-    return requestCount;
+    return mRequestCount;
 }
 
 bool EmulatedFakeCamera2::ReadoutThread::threadLoop() {
@@ -996,9 +1002,8 @@
                 ALOGV("Sending image buffer %d to output stream %d",
                         i, b.streamId);
                 GraphicBufferMapper::get().unlock(*(b.buffer));
-                res = mParent->getStreamInfo(b.streamId).ops->enqueue_buffer(
-                    mParent->getStreamInfo(b.streamId).ops,
-                    captureTime, b.buffer);
+                const Stream &s = mParent->getStreamInfo(b.streamId);
+                res = s.ops->enqueue_buffer(s.ops, captureTime, b.buffer);
                 if (res != OK) {
                     ALOGE("Error enqueuing image buffer %p: %s (%d)", b.buffer,
                             strerror(-res), res);
@@ -1018,6 +1023,9 @@
         mBuffers = NULL;
     }
 
+    Mutex::Autolock l(mInputMutex);
+    mRequestCount--;
+
     return true;
 }
 
diff --git a/tools/emulator/system/camera/EmulatedFakeCamera2.h b/tools/emulator/system/camera/EmulatedFakeCamera2.h
index 9a0b676..8af700b 100644
--- a/tools/emulator/system/camera/EmulatedFakeCamera2.h
+++ b/tools/emulator/system/camera/EmulatedFakeCamera2.h
@@ -185,10 +185,11 @@
         bool mRunning;
         bool threadLoop();
 
-        Mutex mInputMutex; // Protects mActive
+        Mutex mInputMutex; // Protects mActive, mRequestCount
         Condition mInputSignal;
         bool mActive; // Whether we're waiting for input requests or actively
                       // working on them
+        size_t mRequestCount;
 
         camera_metadata_t *mRequest;
 
@@ -222,7 +223,7 @@
         bool threadLoop();
 
         // Inputs
-        Mutex mInputMutex; // Protects mActive, mInFlightQueue
+        Mutex mInputMutex; // Protects mActive, mInFlightQueue, mRequestCount
         Condition mInputSignal;
         bool mActive;
 
@@ -235,6 +236,8 @@
         size_t mInFlightHead;
         size_t mInFlightTail;
 
+        size_t mRequestCount;
+
         // Internals
         Mutex mInternalsMutex;
         camera_metadata_t *mRequest;