Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 26 | namespace android { |
| 27 | |
| 28 | namespace camera3 { |
| 29 | |
| 30 | PreviewFrameSpacer::PreviewFrameSpacer(Camera3OutputStream& parent, sp<Surface> consumer) : |
| 31 | mParent(parent), |
| 32 | mConsumer(consumer) { |
| 33 | } |
| 34 | |
| 35 | PreviewFrameSpacer::~PreviewFrameSpacer() { |
| 36 | Thread::requestExitAndWait(); |
| 37 | } |
| 38 | |
| 39 | status_t PreviewFrameSpacer::queuePreviewBuffer(nsecs_t timestamp, int32_t transform, |
| 40 | ANativeWindowBuffer* anwBuffer, int releaseFence) { |
| 41 | Mutex::Autolock l(mLock); |
| 42 | mPendingBuffers.emplace(timestamp, transform, anwBuffer, releaseFence); |
| 43 | ALOGV("%s: mPendingBuffers size %zu, timestamp %" PRId64, __FUNCTION__, |
| 44 | mPendingBuffers.size(), timestamp); |
| 45 | |
| 46 | mBufferCond.signal(); |
| 47 | return OK; |
| 48 | } |
| 49 | |
| 50 | bool 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(); |
| 59 | nsecs_t captureInterval = buffer.timestamp - mLastCameraCaptureTime; |
| 60 | // If the capture interval exceeds threshold, directly queue |
| 61 | // cached buffer. |
| 62 | if (captureInterval >= kFrameIntervalThreshold) { |
| 63 | mPendingBuffers.pop(); |
| 64 | queueBufferToClientLocked(buffer, currentTime); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | // Cache the frame to match capture time interval, for up to 33ms |
| 69 | nsecs_t expectedQueueTime = mLastCameraPresentTime + captureInterval; |
| 70 | 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 | } |
| 78 | ALOGV("%s: captureInterval %" PRId64 ", queueInterval %" PRId64 ", waited for %" PRId64 |
| 79 | ", timestamp %" PRId64, __FUNCTION__, captureInterval, |
| 80 | currentTime - mLastCameraPresentTime, frameWaitTime, buffer.timestamp); |
| 81 | mPendingBuffers.pop(); |
| 82 | queueBufferToClientLocked(buffer, currentTime); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | void PreviewFrameSpacer::requestExit() { |
| 87 | // Call parent to set up shutdown |
| 88 | Thread::requestExit(); |
| 89 | // Exit from other possible wait |
| 90 | mBufferCond.signal(); |
| 91 | } |
| 92 | |
| 93 | void 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; |
| 117 | mLastCameraCaptureTime = bufferHolder.timestamp; |
| 118 | } |
| 119 | |
| 120 | }; // namespace camera3 |
| 121 | |
| 122 | }; // namespace android |