Camera: Avoid dequeue too many buffers from buffer queue
The PreviewFrameSpacer could defer 1 or 2 buffers to reduce jitter.
And we update the handout buffer count at the time of queuing to
the preview frame spacer, not at the time of queuing to the buffer
queue.
This results in accounting mismatch where the framework could allow
the HAL to dequeue more buffers than supported.
The fix is to track the framework cached buffer count in addition to the
handout buffer count.
Test: Camera CTS, vendor testing, observe logcat with GoogleCamera
Bug: 242674531
Change-Id: I579ae7465b59f5bd9e12dfda332998b401793eb4
diff --git a/services/camera/libcameraservice/device3/Camera3OutputStream.cpp b/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
index 50314cc..8b3cf44 100644
--- a/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
+++ b/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
@@ -419,6 +419,7 @@
mLock.unlock();
ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
+ bool bufferDeferred = false;
/**
* Return buffer back to ANativeWindow
*/
@@ -478,6 +479,7 @@
__FUNCTION__, mId, strerror(-res), res);
return res;
}
+ bufferDeferred = true;
} else {
nsecs_t presentTime = mSyncToDisplay ?
syncTimestampToDisplayLocked(captureTime) : captureTime;
@@ -501,6 +503,10 @@
}
mLock.lock();
+ if (bufferDeferred) {
+ mCachedOutputBufferCount++;
+ }
+
// Once a valid buffer has been returned to the queue, can no longer
// dequeue all buffers for preallocation.
if (buffer.status != CAMERA_BUFFER_STATUS_ERROR) {
@@ -696,10 +702,15 @@
!isVideoStream());
if (forceChoreographer || defaultToChoreographer) {
mSyncToDisplay = true;
+ // For choreographer synced stream, extra buffers aren't kept by
+ // camera service. So no need to update mMaxCachedBufferCount.
mTotalBufferCount += kDisplaySyncExtraBuffer;
} else if (defaultToSpacer) {
mPreviewFrameSpacer = new PreviewFrameSpacer(this, mConsumer);
- mTotalBufferCount ++;
+ // For preview frame spacer, the extra buffer is kept by camera
+ // service. So update mMaxCachedBufferCount.
+ mMaxCachedBufferCount = 1;
+ mTotalBufferCount += mMaxCachedBufferCount;
res = mPreviewFrameSpacer->run(String8::format("PreviewSpacer-%d", mId).string());
if (res != OK) {
ALOGE("%s: Unable to start preview spacer", __FUNCTION__);
@@ -968,6 +979,14 @@
return true;
}
+void Camera3OutputStream::onCachedBufferQueued() {
+ Mutex::Autolock l(mLock);
+ mCachedOutputBufferCount--;
+ // Signal whoever is waiting for the buffer to be returned to the buffer
+ // queue.
+ mOutputBufferReturnedSignal.signal();
+}
+
status_t Camera3OutputStream::disconnectLocked() {
status_t res;