blob: 83caa006adf2bfbbd4b457c8157270bc3051a0eb [file] [log] [blame]
Shuzhen Wangba92d772022-04-11 11:47:24 -07001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-PreviewFrameSpacer"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22
23#include "PreviewFrameSpacer.h"
24#include "Camera3OutputStream.h"
25
26namespace android {
27
28namespace camera3 {
29
Shuzhen Wangdc9aa822022-05-16 10:04:17 -070030PreviewFrameSpacer::PreviewFrameSpacer(wp<Camera3OutputStream> parent, sp<Surface> consumer) :
Shuzhen Wangba92d772022-04-11 11:47:24 -070031 mParent(parent),
32 mConsumer(consumer) {
33}
34
35PreviewFrameSpacer::~PreviewFrameSpacer() {
Shuzhen Wangba92d772022-04-11 11:47:24 -070036}
37
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070038status_t PreviewFrameSpacer::queuePreviewBuffer(nsecs_t timestamp, nsecs_t readoutTimestamp,
39 int32_t transform, ANativeWindowBuffer* anwBuffer, int releaseFence) {
Shuzhen Wangba92d772022-04-11 11:47:24 -070040 Mutex::Autolock l(mLock);
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070041 mPendingBuffers.emplace(timestamp, readoutTimestamp, transform, anwBuffer, releaseFence);
42 ALOGV("%s: mPendingBuffers size %zu, timestamp %" PRId64 ", readoutTime %" PRId64,
43 __FUNCTION__, mPendingBuffers.size(), timestamp, readoutTimestamp);
Shuzhen Wangba92d772022-04-11 11:47:24 -070044
45 mBufferCond.signal();
46 return OK;
47}
48
49bool PreviewFrameSpacer::threadLoop() {
50 Mutex::Autolock l(mLock);
51 if (mPendingBuffers.size() == 0) {
52 mBufferCond.waitRelative(mLock, kWaitDuration);
Shuzhen Wangdc9aa822022-05-16 10:04:17 -070053 if (exitPending()) {
54 return false;
55 } else {
56 return true;
57 }
Shuzhen Wangba92d772022-04-11 11:47:24 -070058 }
59
60 nsecs_t currentTime = systemTime();
61 auto buffer = mPendingBuffers.front();
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070062 nsecs_t readoutInterval = buffer.readoutTimestamp - mLastCameraReadoutTime;
63 // If the readout interval exceeds threshold, directly queue
Shuzhen Wangba92d772022-04-11 11:47:24 -070064 // cached buffer.
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070065 if (readoutInterval >= kFrameIntervalThreshold) {
Shuzhen Wangba92d772022-04-11 11:47:24 -070066 mPendingBuffers.pop();
67 queueBufferToClientLocked(buffer, currentTime);
68 return true;
69 }
70
Shuzhen Wang35bd3552022-09-21 16:56:04 -070071 // Cache the frame to match readout time interval, for up to kMaxFrameWaitTime
Shuzhen Wang4b35b1d2022-10-03 15:53:14 -070072 // Because the code between here and queueBuffer() takes time to execute, make sure the
73 // presentationInterval is slightly shorter than readoutInterval.
74 nsecs_t expectedQueueTime = mLastCameraPresentTime + readoutInterval - kFrameAdjustThreshold;
Shuzhen Wangba92d772022-04-11 11:47:24 -070075 nsecs_t frameWaitTime = std::min(kMaxFrameWaitTime, expectedQueueTime - currentTime);
76 if (frameWaitTime > 0 && mPendingBuffers.size() < 2) {
77 mBufferCond.waitRelative(mLock, frameWaitTime);
78 if (exitPending()) {
Shuzhen Wangdc9aa822022-05-16 10:04:17 -070079 return false;
Shuzhen Wangba92d772022-04-11 11:47:24 -070080 }
81 currentTime = systemTime();
82 }
Shuzhen Wang4b35b1d2022-10-03 15:53:14 -070083 ALOGV("%s: readoutInterval %" PRId64 ", waited for %" PRId64
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070084 ", timestamp %" PRId64, __FUNCTION__, readoutInterval,
Shuzhen Wang4b35b1d2022-10-03 15:53:14 -070085 mPendingBuffers.size() < 2 ? frameWaitTime : 0, buffer.timestamp);
Shuzhen Wangba92d772022-04-11 11:47:24 -070086 mPendingBuffers.pop();
87 queueBufferToClientLocked(buffer, currentTime);
88 return true;
89}
90
91void PreviewFrameSpacer::requestExit() {
92 // Call parent to set up shutdown
93 Thread::requestExit();
94 // Exit from other possible wait
95 mBufferCond.signal();
96}
97
98void PreviewFrameSpacer::queueBufferToClientLocked(
99 const BufferHolder& bufferHolder, nsecs_t currentTime) {
Shuzhen Wangdc9aa822022-05-16 10:04:17 -0700100 sp<Camera3OutputStream> parent = mParent.promote();
101 if (parent == nullptr) {
102 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
103 return;
104 }
105
106 parent->setTransform(bufferHolder.transform, true/*mayChangeMirror*/);
Shuzhen Wangba92d772022-04-11 11:47:24 -0700107
108 status_t res = native_window_set_buffers_timestamp(mConsumer.get(), bufferHolder.timestamp);
109 if (res != OK) {
110 ALOGE("%s: Preview Stream: Error setting timestamp: %s (%d)",
111 __FUNCTION__, strerror(-res), res);
112 }
113
114 Camera3Stream::queueHDRMetadata(bufferHolder.anwBuffer.get()->handle, mConsumer,
Shuzhen Wangdc9aa822022-05-16 10:04:17 -0700115 parent->getDynamicRangeProfile());
Shuzhen Wangba92d772022-04-11 11:47:24 -0700116
117 res = mConsumer->queueBuffer(mConsumer.get(), bufferHolder.anwBuffer.get(),
118 bufferHolder.releaseFence);
119 if (res != OK) {
120 close(bufferHolder.releaseFence);
Shuzhen Wangdc9aa822022-05-16 10:04:17 -0700121 if (parent->shouldLogError(res)) {
Shuzhen Wangba92d772022-04-11 11:47:24 -0700122 ALOGE("%s: Failed to queue buffer to client: %s(%d)", __FUNCTION__,
123 strerror(-res), res);
124 }
125 }
126
Shuzhen Wangc2352702022-09-06 18:36:31 -0700127 parent->onCachedBufferQueued();
Shuzhen Wangba92d772022-04-11 11:47:24 -0700128 mLastCameraPresentTime = currentTime;
Shuzhen Wang05ce6b52022-05-10 18:18:54 -0700129 mLastCameraReadoutTime = bufferHolder.readoutTimestamp;
Shuzhen Wangba92d772022-04-11 11:47:24 -0700130}
131
132}; // namespace camera3
133
134}; // namespace android