Camera2: Erase ZSL queue after each use.

Since preview stops after a capture is submitted, need to clear out
the ZSL queue to avoid using stale buffers when the preview starts up
again.

Bug: 7189765
Change-Id: I9ae2382d0af132208aca5ccba49b5464d18a263e
diff --git a/services/camera/libcameraservice/camera2/CaptureSequencer.cpp b/services/camera/libcameraservice/camera2/CaptureSequencer.cpp
index 941ad15..a836321 100644
--- a/services/camera/libcameraservice/camera2/CaptureSequencer.cpp
+++ b/services/camera/libcameraservice/camera2/CaptureSequencer.cpp
@@ -223,6 +223,11 @@
                 res = INVALID_OPERATION;
         }
     }
+    sp<ZslProcessor> processor = mZslProcessor.promote();
+    if (processor != 0) {
+        processor->clearZslQueue();
+    }
+
     if (mCaptureBuffer != 0 && res == OK) {
         Camera2Client::SharedCameraClient::Lock l(client->mSharedCameraClient);
         ALOGV("%s: Sending still image to client", __FUNCTION__);
diff --git a/services/camera/libcameraservice/camera2/ZslProcessor.cpp b/services/camera/libcameraservice/camera2/ZslProcessor.cpp
index 8906cd7..c7a679e 100644
--- a/services/camera/libcameraservice/camera2/ZslProcessor.cpp
+++ b/services/camera/libcameraservice/camera2/ZslProcessor.cpp
@@ -97,6 +97,10 @@
                 __FUNCTION__, handle);
     }
 
+    // Erase entire ZSL queue since we've now completed the capture and preview
+    // is stopped.
+    clearZslQueueLocked();
+
     mState = RUNNING;
 }
 
@@ -240,7 +244,6 @@
         dumpZslQueue(-1);
     }
 
-
     if (mZslQueueTail != mZslQueueHead) {
         CameraMetadata request;
         size_t index = mZslQueueTail;
@@ -312,6 +315,26 @@
     return OK;
 }
 
+status_t ZslProcessor::clearZslQueue() {
+    Mutex::Autolock l(mInputMutex);
+    // If in middle of capture, can't clear out queue
+    if (mState == LOCKED) return OK;
+
+    return clearZslQueueLocked();
+}
+
+status_t ZslProcessor::clearZslQueueLocked() {
+    for (size_t i = 0; i < mZslQueue.size(); i++) {
+        if (mZslQueue[i].buffer.mTimestamp != 0) {
+            mZslConsumer->releaseBuffer(mZslQueue[i].buffer);
+        }
+        mZslQueue.replaceAt(i);
+    }
+    mZslQueueHead = 0;
+    mZslQueueTail = 0;
+    return OK;
+}
+
 void ZslProcessor::dump(int fd, const Vector<String16>& args) const {
     Mutex::Autolock l(mInputMutex);
     dumpZslQueue(fd);
diff --git a/services/camera/libcameraservice/camera2/ZslProcessor.h b/services/camera/libcameraservice/camera2/ZslProcessor.h
index 268f4f5..96ad5fb 100644
--- a/services/camera/libcameraservice/camera2/ZslProcessor.h
+++ b/services/camera/libcameraservice/camera2/ZslProcessor.h
@@ -62,6 +62,7 @@
     int getReprocessStreamId() const;
 
     status_t pushToReprocess(int32_t requestId);
+    status_t clearZslQueue();
 
     void dump(int fd, const Vector<String16>& args) const;
   private:
@@ -111,6 +112,8 @@
     // Match up entries from frame list to buffers in ZSL queue
     void findMatchesLocked();
 
+    status_t clearZslQueueLocked();
+
     void dumpZslQueue(int id) const;
 };