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 | |
Shuzhen Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 30 | PreviewFrameSpacer::PreviewFrameSpacer(wp<Camera3OutputStream> parent, sp<Surface> consumer) : |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 31 | mParent(parent), |
| 32 | mConsumer(consumer) { |
| 33 | } |
| 34 | |
| 35 | PreviewFrameSpacer::~PreviewFrameSpacer() { |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Shuzhen Wang | 05ce6b5 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 38 | status_t PreviewFrameSpacer::queuePreviewBuffer(nsecs_t timestamp, nsecs_t readoutTimestamp, |
| 39 | int32_t transform, ANativeWindowBuffer* anwBuffer, int releaseFence) { |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 40 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 05ce6b5 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 41 | mPendingBuffers.emplace(timestamp, readoutTimestamp, transform, anwBuffer, releaseFence); |
| 42 | ALOGV("%s: mPendingBuffers size %zu, timestamp %" PRId64 ", readoutTime %" PRId64, |
| 43 | __FUNCTION__, mPendingBuffers.size(), timestamp, readoutTimestamp); |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 44 | |
| 45 | mBufferCond.signal(); |
| 46 | return OK; |
| 47 | } |
| 48 | |
| 49 | bool PreviewFrameSpacer::threadLoop() { |
| 50 | Mutex::Autolock l(mLock); |
| 51 | if (mPendingBuffers.size() == 0) { |
| 52 | mBufferCond.waitRelative(mLock, kWaitDuration); |
Shuzhen Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 53 | if (exitPending()) { |
| 54 | return false; |
| 55 | } else { |
| 56 | return true; |
| 57 | } |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | nsecs_t currentTime = systemTime(); |
| 61 | auto buffer = mPendingBuffers.front(); |
Shuzhen Wang | 05ce6b5 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 62 | nsecs_t readoutInterval = buffer.readoutTimestamp - mLastCameraReadoutTime; |
| 63 | // If the readout interval exceeds threshold, directly queue |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 64 | // cached buffer. |
Shuzhen Wang | 05ce6b5 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 65 | if (readoutInterval >= kFrameIntervalThreshold) { |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 66 | mPendingBuffers.pop(); |
| 67 | queueBufferToClientLocked(buffer, currentTime); |
| 68 | return true; |
| 69 | } |
| 70 | |
Shuzhen Wang | 05ce6b5 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 71 | // Cache the frame to match readout time interval, for up to 33ms |
| 72 | nsecs_t expectedQueueTime = mLastCameraPresentTime + readoutInterval; |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 73 | nsecs_t frameWaitTime = std::min(kMaxFrameWaitTime, expectedQueueTime - currentTime); |
| 74 | if (frameWaitTime > 0 && mPendingBuffers.size() < 2) { |
| 75 | mBufferCond.waitRelative(mLock, frameWaitTime); |
| 76 | if (exitPending()) { |
Shuzhen Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 77 | return false; |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 78 | } |
| 79 | currentTime = systemTime(); |
| 80 | } |
Shuzhen Wang | 05ce6b5 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 81 | ALOGV("%s: readoutInterval %" PRId64 ", queueInterval %" PRId64 ", waited for %" PRId64 |
| 82 | ", timestamp %" PRId64, __FUNCTION__, readoutInterval, |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 83 | currentTime - mLastCameraPresentTime, frameWaitTime, buffer.timestamp); |
| 84 | mPendingBuffers.pop(); |
| 85 | queueBufferToClientLocked(buffer, currentTime); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | void PreviewFrameSpacer::requestExit() { |
| 90 | // Call parent to set up shutdown |
| 91 | Thread::requestExit(); |
| 92 | // Exit from other possible wait |
| 93 | mBufferCond.signal(); |
| 94 | } |
| 95 | |
| 96 | void PreviewFrameSpacer::queueBufferToClientLocked( |
| 97 | const BufferHolder& bufferHolder, nsecs_t currentTime) { |
Shuzhen Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 98 | 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 Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 105 | |
| 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 Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 113 | parent->getDynamicRangeProfile()); |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 114 | |
| 115 | res = mConsumer->queueBuffer(mConsumer.get(), bufferHolder.anwBuffer.get(), |
| 116 | bufferHolder.releaseFence); |
| 117 | if (res != OK) { |
| 118 | close(bufferHolder.releaseFence); |
Shuzhen Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 119 | if (parent->shouldLogError(res)) { |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 120 | ALOGE("%s: Failed to queue buffer to client: %s(%d)", __FUNCTION__, |
| 121 | strerror(-res), res); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | mLastCameraPresentTime = currentTime; |
Shuzhen Wang | 05ce6b5 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 126 | mLastCameraReadoutTime = bufferHolder.readoutTimestamp; |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | }; // namespace camera3 |
| 130 | |
| 131 | }; // namespace android |