CameraHal: Check return frames count before waiting on condition

- The Thread responsible for retrieving free buffers
  from the tapout is possible to miss some incoming
  signals and wait while buffers are available in the
  ST. Solution is to count all outgoing buffers and
  not wait on the condition when buffers are available.

Change-Id: I31526dd334a08f52f0c6069cea847ae14e2cffbc
Signed-off-by: Emilian Peev <epeev@mm-sol.com>
Signed-off-by: Vladimir Petrov <vppetrov@mm-sol.com>
diff --git a/camera/BufferSourceAdapter.cpp b/camera/BufferSourceAdapter.cpp
index 036ce72..ba517a8 100644
--- a/camera/BufferSourceAdapter.cpp
+++ b/camera/BufferSourceAdapter.cpp
@@ -904,9 +904,6 @@
     }
 
     mFramesWithCameraAdapterMap.removeItem((buffer_handle_t *) frame->mBuffer->opaque);
-
-    // signal return frame thread that it can dequeue a buffer now
-    mReturnFrame->signal();
 }
 
 
diff --git a/camera/inc/BufferSourceAdapter.h b/camera/inc/BufferSourceAdapter.h
index c62c3c8..8d1fa7c 100644
--- a/camera/inc/BufferSourceAdapter.h
+++ b/camera/inc/BufferSourceAdapter.h
@@ -47,6 +47,7 @@
         ReturnFrame(BufferSourceAdapter* __this) : mBufferSourceAdapter(__this) {
             android::AutoMutex lock(mReturnFrameMutex);
             mDestroying = false;
+            mFrameCount = 0;
         }
 
         ~ReturnFrame() {
@@ -54,6 +55,8 @@
          }
 
         void signal() {
+            android::AutoMutex lock(mReturnFrameMutex);
+            mFrameCount++;
             mReturnFrameCondition.signal();
         }
 
@@ -67,8 +70,13 @@
 
         virtual bool threadLoop() {
             android::AutoMutex lock(mReturnFrameMutex);
-            mReturnFrameCondition.wait(mReturnFrameMutex);
-            if (!mDestroying) mBufferSourceAdapter->handleFrameReturn();
+            if ( 0 >= mFrameCount ) {
+                mReturnFrameCondition.wait(mReturnFrameMutex);
+            }
+            if (!mDestroying) {
+                mBufferSourceAdapter->handleFrameReturn();
+                mFrameCount--;
+            }
             return true;
         }
 
@@ -76,6 +84,7 @@
         BufferSourceAdapter* mBufferSourceAdapter;
         android::Condition mReturnFrameCondition;
         android::Mutex mReturnFrameMutex;
+        int mFrameCount;
         bool mDestroying;
     };
 
@@ -124,6 +133,10 @@
             if (frame) {
                 mBufferSourceAdapter->handleFrameCallback(frame);
                 frame->mMetaData.clear();
+
+                // signal return frame thread that it can dequeue a buffer now
+                mBufferSourceAdapter->mReturnFrame->signal();
+
                 delete frame;
             }