blob: 0439501733856e8022bf3827bcf909155717d79f [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 Wang05ce6b52022-05-10 18:18:54 -070071 // Cache the frame to match readout time interval, for up to 33ms
72 nsecs_t expectedQueueTime = mLastCameraPresentTime + readoutInterval;
Shuzhen Wangba92d772022-04-11 11:47:24 -070073 nsecs_t frameWaitTime = std::min(kMaxFrameWaitTime, expectedQueueTime - currentTime);
74 if (frameWaitTime > 0 && mPendingBuffers.size() < 2) {
75 mBufferCond.waitRelative(mLock, frameWaitTime);
76 if (exitPending()) {
Shuzhen Wangdc9aa822022-05-16 10:04:17 -070077 return false;
Shuzhen Wangba92d772022-04-11 11:47:24 -070078 }
79 currentTime = systemTime();
80 }
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070081 ALOGV("%s: readoutInterval %" PRId64 ", queueInterval %" PRId64 ", waited for %" PRId64
82 ", timestamp %" PRId64, __FUNCTION__, readoutInterval,
Shuzhen Wangba92d772022-04-11 11:47:24 -070083 currentTime - mLastCameraPresentTime, frameWaitTime, buffer.timestamp);
84 mPendingBuffers.pop();
85 queueBufferToClientLocked(buffer, currentTime);
86 return true;
87}
88
89void PreviewFrameSpacer::requestExit() {
90 // Call parent to set up shutdown
91 Thread::requestExit();
92 // Exit from other possible wait
93 mBufferCond.signal();
94}
95
96void PreviewFrameSpacer::queueBufferToClientLocked(
97 const BufferHolder& bufferHolder, nsecs_t currentTime) {
Shuzhen Wangdc9aa822022-05-16 10:04:17 -070098 sp<Camera3OutputStream> parent = mParent.promote();
99 if (parent == nullptr) {
100 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
101 return;
102 }
103
104 parent->setTransform(bufferHolder.transform, true/*mayChangeMirror*/);
Shuzhen Wangba92d772022-04-11 11:47:24 -0700105
106 status_t res = native_window_set_buffers_timestamp(mConsumer.get(), bufferHolder.timestamp);
107 if (res != OK) {
108 ALOGE("%s: Preview Stream: Error setting timestamp: %s (%d)",
109 __FUNCTION__, strerror(-res), res);
110 }
111
112 Camera3Stream::queueHDRMetadata(bufferHolder.anwBuffer.get()->handle, mConsumer,
Shuzhen Wangdc9aa822022-05-16 10:04:17 -0700113 parent->getDynamicRangeProfile());
Shuzhen Wangba92d772022-04-11 11:47:24 -0700114
115 res = mConsumer->queueBuffer(mConsumer.get(), bufferHolder.anwBuffer.get(),
116 bufferHolder.releaseFence);
117 if (res != OK) {
118 close(bufferHolder.releaseFence);
Shuzhen Wangdc9aa822022-05-16 10:04:17 -0700119 if (parent->shouldLogError(res)) {
Shuzhen Wangba92d772022-04-11 11:47:24 -0700120 ALOGE("%s: Failed to queue buffer to client: %s(%d)", __FUNCTION__,
121 strerror(-res), res);
122 }
123 }
124
125 mLastCameraPresentTime = currentTime;
Shuzhen Wang05ce6b52022-05-10 18:18:54 -0700126 mLastCameraReadoutTime = bufferHolder.readoutTimestamp;
Shuzhen Wangba92d772022-04-11 11:47:24 -0700127}
128
129}; // namespace camera3
130
131}; // namespace android