blob: 496580f4f34e8491c075879ee6dfb623951772c0 [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
30PreviewFrameSpacer::PreviewFrameSpacer(Camera3OutputStream& parent, sp<Surface> consumer) :
31 mParent(parent),
32 mConsumer(consumer) {
33}
34
35PreviewFrameSpacer::~PreviewFrameSpacer() {
36 Thread::requestExitAndWait();
37}
38
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070039status_t PreviewFrameSpacer::queuePreviewBuffer(nsecs_t timestamp, nsecs_t readoutTimestamp,
40 int32_t transform, ANativeWindowBuffer* anwBuffer, int releaseFence) {
Shuzhen Wangba92d772022-04-11 11:47:24 -070041 Mutex::Autolock l(mLock);
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070042 mPendingBuffers.emplace(timestamp, readoutTimestamp, transform, anwBuffer, releaseFence);
43 ALOGV("%s: mPendingBuffers size %zu, timestamp %" PRId64 ", readoutTime %" PRId64,
44 __FUNCTION__, mPendingBuffers.size(), timestamp, readoutTimestamp);
Shuzhen Wangba92d772022-04-11 11:47:24 -070045
46 mBufferCond.signal();
47 return OK;
48}
49
50bool PreviewFrameSpacer::threadLoop() {
51 Mutex::Autolock l(mLock);
52 if (mPendingBuffers.size() == 0) {
53 mBufferCond.waitRelative(mLock, kWaitDuration);
54 return true;
55 }
56
57 nsecs_t currentTime = systemTime();
58 auto buffer = mPendingBuffers.front();
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070059 nsecs_t readoutInterval = buffer.readoutTimestamp - mLastCameraReadoutTime;
60 // If the readout interval exceeds threshold, directly queue
Shuzhen Wangba92d772022-04-11 11:47:24 -070061 // cached buffer.
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070062 if (readoutInterval >= kFrameIntervalThreshold) {
Shuzhen Wangba92d772022-04-11 11:47:24 -070063 mPendingBuffers.pop();
64 queueBufferToClientLocked(buffer, currentTime);
65 return true;
66 }
67
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070068 // Cache the frame to match readout time interval, for up to 33ms
69 nsecs_t expectedQueueTime = mLastCameraPresentTime + readoutInterval;
Shuzhen Wangba92d772022-04-11 11:47:24 -070070 nsecs_t frameWaitTime = std::min(kMaxFrameWaitTime, expectedQueueTime - currentTime);
71 if (frameWaitTime > 0 && mPendingBuffers.size() < 2) {
72 mBufferCond.waitRelative(mLock, frameWaitTime);
73 if (exitPending()) {
74 return true;
75 }
76 currentTime = systemTime();
77 }
Shuzhen Wang05ce6b52022-05-10 18:18:54 -070078 ALOGV("%s: readoutInterval %" PRId64 ", queueInterval %" PRId64 ", waited for %" PRId64
79 ", timestamp %" PRId64, __FUNCTION__, readoutInterval,
Shuzhen Wangba92d772022-04-11 11:47:24 -070080 currentTime - mLastCameraPresentTime, frameWaitTime, buffer.timestamp);
81 mPendingBuffers.pop();
82 queueBufferToClientLocked(buffer, currentTime);
83 return true;
84}
85
86void PreviewFrameSpacer::requestExit() {
87 // Call parent to set up shutdown
88 Thread::requestExit();
89 // Exit from other possible wait
90 mBufferCond.signal();
91}
92
93void PreviewFrameSpacer::queueBufferToClientLocked(
94 const BufferHolder& bufferHolder, nsecs_t currentTime) {
95 mParent.setTransform(bufferHolder.transform, true/*mayChangeMirror*/);
96
97 status_t res = native_window_set_buffers_timestamp(mConsumer.get(), bufferHolder.timestamp);
98 if (res != OK) {
99 ALOGE("%s: Preview Stream: Error setting timestamp: %s (%d)",
100 __FUNCTION__, strerror(-res), res);
101 }
102
103 Camera3Stream::queueHDRMetadata(bufferHolder.anwBuffer.get()->handle, mConsumer,
104 mParent.getDynamicRangeProfile());
105
106 res = mConsumer->queueBuffer(mConsumer.get(), bufferHolder.anwBuffer.get(),
107 bufferHolder.releaseFence);
108 if (res != OK) {
109 close(bufferHolder.releaseFence);
110 if (mParent.shouldLogError(res)) {
111 ALOGE("%s: Failed to queue buffer to client: %s(%d)", __FUNCTION__,
112 strerror(-res), res);
113 }
114 }
115
116 mLastCameraPresentTime = currentTime;
Shuzhen Wang05ce6b52022-05-10 18:18:54 -0700117 mLastCameraReadoutTime = bufferHolder.readoutTimestamp;
Shuzhen Wangba92d772022-04-11 11:47:24 -0700118}
119
120}; // namespace camera3
121
122}; // namespace android