Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 1 | /* |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013-2018 The Android Open Source Project |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 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-OutputStream" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 21 | #include <ctime> |
| 22 | #include <fstream> |
| 23 | |
| 24 | #include <android-base/unique_fd.h> |
| 25 | #include <ui/GraphicBuffer.h> |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | #include <utils/Trace.h> |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include "api1/client2/JpegProcessor.h" |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 30 | #include "Camera3OutputStream.h" |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 31 | #include "utils/TraceHFR.h" |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 32 | |
| 33 | #ifndef container_of |
| 34 | #define container_of(ptr, type, member) \ |
| 35 | (type *)((char*)(ptr) - offsetof(type, member)) |
| 36 | #endif |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | namespace camera3 { |
| 41 | |
| 42 | Camera3OutputStream::Camera3OutputStream(int id, |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 43 | sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 44 | uint32_t width, uint32_t height, int format, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 45 | android_dataspace dataSpace, camera_stream_rotation_t rotation, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 46 | nsecs_t timestampOffset, const String8& physicalCameraId, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 47 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 48 | int setId, bool isMultiResolution) : |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 49 | Camera3IOStreamBase(id, CAMERA_STREAM_OUTPUT, width, height, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 50 | /*maxSize*/0, format, dataSpace, rotation, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 51 | physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 52 | mConsumer(consumer), |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 53 | mTransform(0), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 54 | mTraceFirstBuffer(true), |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 55 | mUseBufferManager(false), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 56 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 57 | mConsumerUsage(0), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 58 | mDropBuffers(false), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 59 | mDequeueBufferLatency(kDequeueLatencyBinSize) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 60 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 61 | if (mConsumer == NULL) { |
| 62 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 63 | mState = STATE_ERROR; |
| 64 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 65 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 66 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 67 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Camera3OutputStream::Camera3OutputStream(int id, |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 71 | sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 72 | uint32_t width, uint32_t height, size_t maxSize, int format, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 73 | android_dataspace dataSpace, camera_stream_rotation_t rotation, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 74 | nsecs_t timestampOffset, const String8& physicalCameraId, |
| 75 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
| 76 | int setId, bool isMultiResolution) : |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 77 | Camera3IOStreamBase(id, CAMERA_STREAM_OUTPUT, width, height, maxSize, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 78 | format, dataSpace, rotation, physicalCameraId, sensorPixelModesUsed, |
| 79 | setId, isMultiResolution), |
Igor Murashkin | a55b545 | 2013-04-02 16:36:33 -0700 | [diff] [blame] | 80 | mConsumer(consumer), |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 81 | mTransform(0), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 82 | mTraceFirstBuffer(true), |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 83 | mUseMonoTimestamp(false), |
| 84 | mUseBufferManager(false), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 85 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 86 | mConsumerUsage(0), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 87 | mDropBuffers(false), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 88 | mDequeueBufferLatency(kDequeueLatencyBinSize) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 89 | |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 90 | if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 91 | ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, |
| 92 | format); |
| 93 | mState = STATE_ERROR; |
| 94 | } |
| 95 | |
| 96 | if (mConsumer == NULL) { |
| 97 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 98 | mState = STATE_ERROR; |
| 99 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 100 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 101 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 102 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 105 | Camera3OutputStream::Camera3OutputStream(int id, |
| 106 | uint32_t width, uint32_t height, int format, |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 107 | uint64_t consumerUsage, android_dataspace dataSpace, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 108 | camera_stream_rotation_t rotation, nsecs_t timestampOffset, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 109 | const String8& physicalCameraId, |
| 110 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
| 111 | int setId, bool isMultiResolution) : |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 112 | Camera3IOStreamBase(id, CAMERA_STREAM_OUTPUT, width, height, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 113 | /*maxSize*/0, format, dataSpace, rotation, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 114 | physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 115 | mConsumer(nullptr), |
| 116 | mTransform(0), |
| 117 | mTraceFirstBuffer(true), |
| 118 | mUseBufferManager(false), |
| 119 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 120 | mConsumerUsage(consumerUsage), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 121 | mDropBuffers(false), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 122 | mDequeueBufferLatency(kDequeueLatencyBinSize) { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 123 | // Deferred consumer only support preview surface format now. |
| 124 | if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 125 | ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!", |
| 126 | __FUNCTION__); |
| 127 | mState = STATE_ERROR; |
| 128 | } |
| 129 | |
Ivan Lozano | c0ad82f | 2020-07-30 09:32:57 -0400 | [diff] [blame] | 130 | // Validation check for the consumer usage flag. |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 131 | if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 && |
| 132 | (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 133 | ALOGE("%s: Deferred consumer usage flag is illegal %" PRIu64 "!", |
| 134 | __FUNCTION__, consumerUsage); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 135 | mState = STATE_ERROR; |
| 136 | } |
| 137 | |
| 138 | mConsumerName = String8("Deferred"); |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 139 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 140 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 143 | Camera3OutputStream::Camera3OutputStream(int id, camera_stream_type_t type, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 144 | uint32_t width, uint32_t height, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 145 | int format, |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 146 | android_dataspace dataSpace, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 147 | camera_stream_rotation_t rotation, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 148 | const String8& physicalCameraId, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 149 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 150 | uint64_t consumerUsage, nsecs_t timestampOffset, |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 151 | int setId, bool isMultiResolution) : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 152 | Camera3IOStreamBase(id, type, width, height, |
| 153 | /*maxSize*/0, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 154 | format, dataSpace, rotation, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 155 | physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 156 | mTransform(0), |
| 157 | mTraceFirstBuffer(true), |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 158 | mUseMonoTimestamp(false), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 159 | mUseBufferManager(false), |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 160 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 161 | mConsumerUsage(consumerUsage), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 162 | mDropBuffers(false), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 163 | mDequeueBufferLatency(kDequeueLatencyBinSize) { |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 164 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 165 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 166 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 167 | |
| 168 | // Subclasses expected to initialize mConsumer themselves |
| 169 | } |
| 170 | |
| 171 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 172 | Camera3OutputStream::~Camera3OutputStream() { |
| 173 | disconnectLocked(); |
| 174 | } |
| 175 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 176 | status_t Camera3OutputStream::getBufferLocked(camera_stream_buffer *buffer, |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 177 | const std::vector<size_t>&) { |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 178 | ATRACE_HFR_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 179 | |
| 180 | ANativeWindowBuffer* anb; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 181 | int fenceFd = -1; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 182 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 183 | status_t res; |
| 184 | res = getBufferLockedCommon(&anb, &fenceFd); |
| 185 | if (res != OK) { |
| 186 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 189 | /** |
| 190 | * FenceFD now owned by HAL except in case of error, |
| 191 | * in which case we reassign it to acquire_fence |
| 192 | */ |
| 193 | handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 194 | /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/true); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 195 | |
| 196 | return OK; |
| 197 | } |
| 198 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 199 | status_t Camera3OutputStream::getBuffersLocked(std::vector<OutstandingBuffer>* outBuffers) { |
| 200 | status_t res; |
| 201 | |
| 202 | if ((res = getBufferPreconditionCheckLocked()) != OK) { |
| 203 | return res; |
| 204 | } |
| 205 | |
| 206 | if (mUseBufferManager) { |
| 207 | ALOGE("%s: stream %d is managed by buffer manager and does not support batch operation", |
| 208 | __FUNCTION__, mId); |
| 209 | return INVALID_OPERATION; |
| 210 | } |
| 211 | |
| 212 | sp<Surface> consumer = mConsumer; |
| 213 | /** |
| 214 | * Release the lock briefly to avoid deadlock for below scenario: |
| 215 | * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring(). |
| 216 | * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock. |
| 217 | * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable(). |
| 218 | * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock |
| 219 | * StreamingProcessor lock. |
| 220 | * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock |
| 221 | * and try to lock bufferQueue lock. |
| 222 | * Then there is circular locking dependency. |
| 223 | */ |
| 224 | mLock.unlock(); |
| 225 | |
| 226 | size_t numBuffersRequested = outBuffers->size(); |
| 227 | std::vector<Surface::BatchBuffer> buffers(numBuffersRequested); |
| 228 | |
| 229 | nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC); |
| 230 | res = consumer->dequeueBuffers(&buffers); |
| 231 | nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC); |
| 232 | mDequeueBufferLatency.add(dequeueStart, dequeueEnd); |
| 233 | |
| 234 | mLock.lock(); |
| 235 | |
| 236 | if (res != OK) { |
| 237 | if (shouldLogError(res, mState)) { |
| 238 | ALOGE("%s: Stream %d: Can't dequeue %zu output buffers: %s (%d)", |
| 239 | __FUNCTION__, mId, numBuffersRequested, strerror(-res), res); |
| 240 | } |
| 241 | checkRetAndSetAbandonedLocked(res); |
| 242 | return res; |
| 243 | } |
| 244 | checkRemovedBuffersLocked(); |
| 245 | |
| 246 | /** |
| 247 | * FenceFD now owned by HAL except in case of error, |
| 248 | * in which case we reassign it to acquire_fence |
| 249 | */ |
| 250 | for (size_t i = 0; i < numBuffersRequested; i++) { |
| 251 | handoutBufferLocked(*(outBuffers->at(i).outBuffer), |
| 252 | &(buffers[i].buffer->handle), /*acquireFence*/buffers[i].fenceFd, |
| 253 | /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/true); |
| 254 | } |
| 255 | return OK; |
| 256 | } |
| 257 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 258 | status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer, |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 259 | ANativeWindowBuffer* buffer, int anwReleaseFence, |
| 260 | const std::vector<size_t>&) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 261 | return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence); |
| 262 | } |
| 263 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 264 | status_t Camera3OutputStream::returnBufferLocked( |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 265 | const camera_stream_buffer &buffer, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame^] | 266 | nsecs_t timestamp, int32_t transform, const std::vector<size_t>& surface_ids) { |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 267 | ATRACE_HFR_CALL(); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 268 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 269 | if (mHandoutTotalBufferCount == 1) { |
| 270 | returnPrefetchedBuffersLocked(); |
| 271 | } |
| 272 | |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame^] | 273 | status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true, transform, surface_ids); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 274 | |
| 275 | if (res != OK) { |
| 276 | return res; |
| 277 | } |
| 278 | |
| 279 | mLastTimestamp = timestamp; |
Eino-Ville Talvala | c31dc7e | 2017-01-31 17:35:41 -0800 | [diff] [blame] | 280 | mFrameCount++; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 281 | |
| 282 | return OK; |
| 283 | } |
| 284 | |
| 285 | status_t Camera3OutputStream::returnBufferCheckedLocked( |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 286 | const camera_stream_buffer &buffer, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 287 | nsecs_t timestamp, |
| 288 | bool output, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame^] | 289 | int32_t transform, |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 290 | const std::vector<size_t>& surface_ids, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 291 | /*out*/ |
| 292 | sp<Fence> *releaseFenceOut) { |
| 293 | |
| 294 | (void)output; |
| 295 | ALOG_ASSERT(output, "Expected output to be true"); |
| 296 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 297 | status_t res; |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 298 | |
Yin-Chia Yeh | 4c9736f | 2015-03-05 15:01:36 -0800 | [diff] [blame] | 299 | // Fence management - always honor release fence from HAL |
| 300 | sp<Fence> releaseFence = new Fence(buffer.release_fence); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 301 | int anwReleaseFence = releaseFence->dup(); |
| 302 | |
| 303 | /** |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 304 | * Release the lock briefly to avoid deadlock with |
| 305 | * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this |
| 306 | * thread will go into StreamingProcessor::onFrameAvailable) during |
| 307 | * queueBuffer |
| 308 | */ |
| 309 | sp<ANativeWindow> currentConsumer = mConsumer; |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 310 | StreamState state = mState; |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 311 | mLock.unlock(); |
| 312 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 313 | ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle); |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 314 | /** |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 315 | * Return buffer back to ANativeWindow |
| 316 | */ |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 317 | if (buffer.status == CAMERA_BUFFER_STATUS_ERROR || mDropBuffers || timestamp == 0) { |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 318 | // Cancel buffer |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 319 | if (mDropBuffers) { |
| 320 | ALOGV("%s: Dropping a frame for stream %d.", __FUNCTION__, mId); |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 321 | } else if (buffer.status == CAMERA_BUFFER_STATUS_ERROR) { |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 322 | ALOGV("%s: A frame is dropped for stream %d due to buffer error.", __FUNCTION__, mId); |
Shuzhen Wang | f0c4a6b | 2018-09-05 09:36:14 -0700 | [diff] [blame] | 323 | } else { |
| 324 | ALOGE("%s: Stream %d: timestamp shouldn't be 0", __FUNCTION__, mId); |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 327 | res = currentConsumer->cancelBuffer(currentConsumer.get(), |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 328 | anwBuffer, |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 329 | anwReleaseFence); |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 330 | if (shouldLogError(res, state)) { |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 331 | ALOGE("%s: Stream %d: Error cancelling buffer to native window:" |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 332 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 333 | } |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 334 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 335 | notifyBufferReleased(anwBuffer); |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 336 | if (mUseBufferManager) { |
| 337 | // Return this buffer back to buffer manager. |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 338 | mBufferProducerListener->onBufferReleased(); |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 339 | } |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 340 | } else { |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 341 | if (mTraceFirstBuffer && (stream_type == CAMERA_STREAM_OUTPUT)) { |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 342 | { |
| 343 | char traceLog[48]; |
| 344 | snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId); |
| 345 | ATRACE_NAME(traceLog); |
| 346 | } |
| 347 | mTraceFirstBuffer = false; |
| 348 | } |
| 349 | |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame^] | 350 | if (transform != -1) { |
| 351 | setTransformLocked(transform); |
| 352 | } |
| 353 | |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 354 | /* Certain consumers (such as AudioSource or HardwareComposer) use |
| 355 | * MONOTONIC time, causing time misalignment if camera timestamp is |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 356 | * in BOOTTIME. Do the conversion if necessary. */ |
| 357 | res = native_window_set_buffers_timestamp(mConsumer.get(), |
| 358 | mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp); |
| 359 | if (res != OK) { |
| 360 | ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)", |
| 361 | __FUNCTION__, mId, strerror(-res), res); |
| 362 | return res; |
Yin-Chia Yeh | 4c9736f | 2015-03-05 15:01:36 -0800 | [diff] [blame] | 363 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 364 | // If this is a JPEG output, and image dump mask is set, save image to |
| 365 | // disk. |
| 366 | if (getFormat() == HAL_PIXEL_FORMAT_BLOB && getDataSpace() == HAL_DATASPACE_V0_JFIF && |
| 367 | mImageDumpMask) { |
| 368 | dumpImageToDisk(timestamp, anwBuffer, anwReleaseFence); |
| 369 | } |
Yin-Chia Yeh | 4c9736f | 2015-03-05 15:01:36 -0800 | [diff] [blame] | 370 | |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 371 | res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence, surface_ids); |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 372 | if (shouldLogError(res, state)) { |
| 373 | ALOGE("%s: Stream %d: Error queueing buffer to native window:" |
| 374 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 375 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 376 | } |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 377 | mLock.lock(); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 378 | |
| 379 | // Once a valid buffer has been returned to the queue, can no longer |
| 380 | // dequeue all buffers for preallocation. |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 381 | if (buffer.status != CAMERA_BUFFER_STATUS_ERROR) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 382 | mStreamUnpreparable = true; |
| 383 | } |
| 384 | |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 385 | if (res != OK) { |
| 386 | close(anwReleaseFence); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 389 | *releaseFenceOut = releaseFence; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 390 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 391 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 394 | void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const { |
| 395 | (void) args; |
| 396 | String8 lines; |
| 397 | lines.appendFormat(" Stream[%d]: Output\n", mId); |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 398 | lines.appendFormat(" Consumer name: %s\n", mConsumerName.string()); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 399 | write(fd, lines.string(), lines.size()); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 400 | |
| 401 | Camera3IOStreamBase::dump(fd, args); |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 402 | |
| 403 | mDequeueBufferLatency.dump(fd, |
| 404 | " DequeueBuffer latency histogram:"); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | status_t Camera3OutputStream::setTransform(int transform) { |
| 408 | ATRACE_CALL(); |
| 409 | Mutex::Autolock l(mLock); |
| 410 | return setTransformLocked(transform); |
| 411 | } |
| 412 | |
| 413 | status_t Camera3OutputStream::setTransformLocked(int transform) { |
| 414 | status_t res = OK; |
| 415 | if (mState == STATE_ERROR) { |
| 416 | ALOGE("%s: Stream in error state", __FUNCTION__); |
| 417 | return INVALID_OPERATION; |
| 418 | } |
| 419 | |
| 420 | mTransform = transform; |
| 421 | if (mState == STATE_CONFIGURED) { |
| 422 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 423 | transform); |
| 424 | if (res != OK) { |
| 425 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 426 | __FUNCTION__, transform, strerror(-res), res); |
| 427 | } |
| 428 | } |
| 429 | return res; |
| 430 | } |
| 431 | |
| 432 | status_t Camera3OutputStream::configureQueueLocked() { |
| 433 | status_t res; |
| 434 | |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 435 | mTraceFirstBuffer = true; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 436 | if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { |
| 437 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 440 | if ((res = configureConsumerQueueLocked()) != OK) { |
| 441 | return res; |
| 442 | } |
| 443 | |
| 444 | // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture. |
| 445 | // We need skip these cases as timeout will disable the non-blocking (async) mode. |
| 446 | if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) { |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 447 | if (mUseBufferManager) { |
| 448 | // When buffer manager is handling the buffer, we should have available buffers in |
| 449 | // buffer queue before we calls into dequeueBuffer because buffer manager is tracking |
| 450 | // free buffers. |
| 451 | // There are however some consumer side feature (ImageReader::discardFreeBuffers) that |
| 452 | // can discard free buffers without notifying buffer manager. We want the timeout to |
| 453 | // happen immediately here so buffer manager can try to update its internal state and |
| 454 | // try to allocate a buffer instead of waiting. |
| 455 | mConsumer->setDequeueTimeout(0); |
| 456 | } else { |
| 457 | mConsumer->setDequeueTimeout(kDequeueBufferTimeout); |
| 458 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return OK; |
| 462 | } |
| 463 | |
| 464 | status_t Camera3OutputStream::configureConsumerQueueLocked() { |
| 465 | status_t res; |
| 466 | |
| 467 | mTraceFirstBuffer = true; |
| 468 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 469 | ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL"); |
| 470 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 471 | // Configure consumer-side ANativeWindow interface. The listener may be used |
| 472 | // to notify buffer manager (if it is used) of the returned buffers. |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 473 | res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 474 | /*reportBufferRemoval*/true, |
| 475 | /*listener*/mBufferProducerListener); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 476 | if (res != OK) { |
| 477 | ALOGE("%s: Unable to connect to native window for stream %d", |
| 478 | __FUNCTION__, mId); |
| 479 | return res; |
| 480 | } |
| 481 | |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 482 | mConsumerName = mConsumer->getConsumerName(); |
| 483 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 484 | res = native_window_set_usage(mConsumer.get(), mUsage); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 485 | if (res != OK) { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 486 | ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d", |
| 487 | __FUNCTION__, mUsage, mId); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 488 | return res; |
| 489 | } |
| 490 | |
| 491 | res = native_window_set_scaling_mode(mConsumer.get(), |
| 492 | NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 493 | if (res != OK) { |
| 494 | ALOGE("%s: Unable to configure stream scaling: %s (%d)", |
| 495 | __FUNCTION__, strerror(-res), res); |
| 496 | return res; |
| 497 | } |
| 498 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 499 | if (mMaxSize == 0) { |
| 500 | // For buffers of known size |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 501 | res = native_window_set_buffers_dimensions(mConsumer.get(), |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 502 | camera_stream::width, camera_stream::height); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 503 | } else { |
| 504 | // For buffers with bounded size |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 505 | res = native_window_set_buffers_dimensions(mConsumer.get(), |
| 506 | mMaxSize, 1); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 507 | } |
| 508 | if (res != OK) { |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 509 | ALOGE("%s: Unable to configure stream buffer dimensions" |
| 510 | " %d x %d (maxSize %zu) for stream %d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 511 | __FUNCTION__, camera_stream::width, camera_stream::height, |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 512 | mMaxSize, mId); |
| 513 | return res; |
| 514 | } |
| 515 | res = native_window_set_buffers_format(mConsumer.get(), |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 516 | camera_stream::format); |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 517 | if (res != OK) { |
| 518 | ALOGE("%s: Unable to configure stream buffer format %#x for stream %d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 519 | __FUNCTION__, camera_stream::format, mId); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 520 | return res; |
| 521 | } |
| 522 | |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 523 | res = native_window_set_buffers_data_space(mConsumer.get(), |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 524 | camera_stream::data_space); |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 525 | if (res != OK) { |
| 526 | ALOGE("%s: Unable to configure stream dataspace %#x for stream %d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 527 | __FUNCTION__, camera_stream::data_space, mId); |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 528 | return res; |
| 529 | } |
| 530 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 531 | int maxConsumerBuffers; |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 532 | res = static_cast<ANativeWindow*>(mConsumer.get())->query( |
| 533 | mConsumer.get(), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 534 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers); |
| 535 | if (res != OK) { |
| 536 | ALOGE("%s: Unable to query consumer undequeued" |
| 537 | " buffer count for stream %d", __FUNCTION__, mId); |
| 538 | return res; |
| 539 | } |
| 540 | |
Alex Ray | 20cb300 | 2013-05-28 20:18:22 -0700 | [diff] [blame] | 541 | ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 542 | maxConsumerBuffers, camera_stream::max_buffers); |
| 543 | if (camera_stream::max_buffers == 0) { |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 544 | ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 545 | __FUNCTION__, camera_stream::max_buffers); |
Alex Ray | 20cb300 | 2013-05-28 20:18:22 -0700 | [diff] [blame] | 546 | return INVALID_OPERATION; |
| 547 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 548 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 549 | mTotalBufferCount = maxConsumerBuffers + camera_stream::max_buffers; |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 550 | mHandoutTotalBufferCount = 0; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 551 | mFrameCount = 0; |
| 552 | mLastTimestamp = 0; |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 553 | mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream()); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 554 | |
| 555 | res = native_window_set_buffer_count(mConsumer.get(), |
| 556 | mTotalBufferCount); |
| 557 | if (res != OK) { |
| 558 | ALOGE("%s: Unable to set buffer count for stream %d", |
| 559 | __FUNCTION__, mId); |
| 560 | return res; |
| 561 | } |
| 562 | |
| 563 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 564 | mTransform); |
| 565 | if (res != OK) { |
| 566 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 567 | __FUNCTION__, mTransform, strerror(-res), res); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 568 | return res; |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 571 | /** |
Zhijun He | edd41ae | 2016-02-03 14:45:53 -0800 | [diff] [blame] | 572 | * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 573 | * buffers to be statically allocated for internal static buffer registration, while the |
| 574 | * buffers provided by buffer manager are really dynamically allocated. Camera3Device only |
Zhijun He | edd41ae | 2016-02-03 14:45:53 -0800 | [diff] [blame] | 575 | * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer |
| 576 | * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some |
| 577 | * HAL3.2 devices may not support the dynamic buffer registeration. |
Yin-Chia Yeh | b657890 | 2019-04-16 13:36:16 -0700 | [diff] [blame] | 578 | * Also Camera3BufferManager does not support display/texture streams as they have its own |
| 579 | * buffer management logic. |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 580 | */ |
Yin-Chia Yeh | b657890 | 2019-04-16 13:36:16 -0700 | [diff] [blame] | 581 | if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID && |
| 582 | !(isConsumedByHWComposer() || isConsumedByHWTexture())) { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 583 | uint64_t consumerUsage = 0; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 584 | getEndpointUsage(&consumerUsage); |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 585 | uint32_t width = (mMaxSize == 0) ? getWidth() : mMaxSize; |
| 586 | uint32_t height = (mMaxSize == 0) ? getHeight() : 1; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 587 | StreamInfo streamInfo( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 588 | getId(), getStreamSetId(), width, height, getFormat(), getDataSpace(), |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 589 | mUsage | consumerUsage, mTotalBufferCount, |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 590 | /*isConfigured*/true, isMultiResolution()); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 591 | wp<Camera3OutputStream> weakThis(this); |
| 592 | res = mBufferManager->registerStream(weakThis, |
| 593 | streamInfo); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 594 | if (res == OK) { |
| 595 | // Disable buffer allocation for this BufferQueue, buffer manager will take over |
| 596 | // the buffer allocation responsibility. |
| 597 | mConsumer->getIGraphicBufferProducer()->allowAllocation(false); |
| 598 | mUseBufferManager = true; |
| 599 | } else { |
| 600 | ALOGE("%s: Unable to register stream %d to camera3 buffer manager, " |
| 601 | "(error %d %s), fall back to BufferQueue for buffer management!", |
| 602 | __FUNCTION__, mId, res, strerror(-res)); |
| 603 | } |
| 604 | } |
| 605 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 606 | return OK; |
| 607 | } |
| 608 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 609 | status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) { |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 610 | ATRACE_HFR_CALL(); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 611 | status_t res; |
| 612 | |
| 613 | if ((res = getBufferPreconditionCheckLocked()) != OK) { |
| 614 | return res; |
| 615 | } |
| 616 | |
| 617 | bool gotBufferFromManager = false; |
| 618 | |
| 619 | if (mUseBufferManager) { |
| 620 | sp<GraphicBuffer> gb; |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 621 | res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), |
| 622 | isMultiResolution(), &gb, fenceFd); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 623 | if (res == OK) { |
| 624 | // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a |
| 625 | // successful return. |
| 626 | *anb = gb.get(); |
| 627 | res = mConsumer->attachBuffer(*anb); |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 628 | if (shouldLogError(res, mState)) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 629 | ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)", |
| 630 | __FUNCTION__, mId, strerror(-res), res); |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 631 | } |
| 632 | if (res != OK) { |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 633 | checkRetAndSetAbandonedLocked(res); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 634 | return res; |
| 635 | } |
| 636 | gotBufferFromManager = true; |
| 637 | ALOGV("Stream %d: Attached new buffer", getId()); |
| 638 | } else if (res == ALREADY_EXISTS) { |
| 639 | // Have sufficient free buffers already attached, can just |
| 640 | // dequeue from buffer queue |
| 641 | ALOGV("Stream %d: Reusing attached buffer", getId()); |
| 642 | gotBufferFromManager = false; |
| 643 | } else if (res != OK) { |
| 644 | ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)", |
| 645 | __FUNCTION__, mId, strerror(-res), res); |
| 646 | return res; |
| 647 | } |
| 648 | } |
| 649 | if (!gotBufferFromManager) { |
| 650 | /** |
| 651 | * Release the lock briefly to avoid deadlock for below scenario: |
| 652 | * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring(). |
| 653 | * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock. |
| 654 | * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable(). |
| 655 | * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock |
| 656 | * StreamingProcessor lock. |
| 657 | * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock |
| 658 | * and try to lock bufferQueue lock. |
| 659 | * Then there is circular locking dependency. |
| 660 | */ |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 661 | sp<Surface> consumer = mConsumer; |
Shuzhen Wang | 6c14e31 | 2021-07-05 16:20:33 -0700 | [diff] [blame] | 662 | size_t remainingBuffers = (mState == STATE_PREPARING ? mTotalBufferCount : |
| 663 | camera_stream::max_buffers) - mHandoutTotalBufferCount; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 664 | mLock.unlock(); |
| 665 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 666 | nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 667 | |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 668 | size_t batchSize = mBatchSize.load(); |
| 669 | if (batchSize == 1) { |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 670 | sp<ANativeWindow> anw = consumer; |
| 671 | res = anw->dequeueBuffer(anw.get(), anb, fenceFd); |
| 672 | } else { |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 673 | std::unique_lock<std::mutex> batchLock(mBatchLock); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 674 | res = OK; |
| 675 | if (mBatchedBuffers.size() == 0) { |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 676 | if (remainingBuffers == 0) { |
| 677 | ALOGE("%s: cannot get buffer while all buffers are handed out", __FUNCTION__); |
| 678 | return INVALID_OPERATION; |
| 679 | } |
| 680 | if (batchSize > remainingBuffers) { |
| 681 | batchSize = remainingBuffers; |
| 682 | } |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 683 | batchLock.unlock(); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 684 | // Refill batched buffers |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 685 | std::vector<Surface::BatchBuffer> batchedBuffers; |
| 686 | batchedBuffers.resize(batchSize); |
| 687 | res = consumer->dequeueBuffers(&batchedBuffers); |
| 688 | batchLock.lock(); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 689 | if (res != OK) { |
| 690 | ALOGE("%s: batch dequeueBuffers call failed! %s (%d)", |
| 691 | __FUNCTION__, strerror(-res), res); |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 692 | } else { |
| 693 | mBatchedBuffers = std::move(batchedBuffers); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | if (res == OK) { |
| 698 | // Dispatch batch buffers |
| 699 | *anb = mBatchedBuffers.back().buffer; |
| 700 | *fenceFd = mBatchedBuffers.back().fenceFd; |
| 701 | mBatchedBuffers.pop_back(); |
| 702 | } |
| 703 | } |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 704 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 705 | nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC); |
| 706 | mDequeueBufferLatency.add(dequeueStart, dequeueEnd); |
| 707 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 708 | mLock.lock(); |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 709 | |
| 710 | if (mUseBufferManager && res == TIMED_OUT) { |
| 711 | checkRemovedBuffersLocked(); |
| 712 | |
| 713 | sp<GraphicBuffer> gb; |
| 714 | res = mBufferManager->getBufferForStream( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 715 | getId(), getStreamSetId(), isMultiResolution(), |
| 716 | &gb, fenceFd, /*noFreeBuffer*/true); |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 717 | |
| 718 | if (res == OK) { |
| 719 | // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after |
| 720 | // a successful return. |
| 721 | *anb = gb.get(); |
| 722 | res = mConsumer->attachBuffer(*anb); |
| 723 | gotBufferFromManager = true; |
| 724 | ALOGV("Stream %d: Attached new buffer", getId()); |
| 725 | |
| 726 | if (res != OK) { |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 727 | if (shouldLogError(res, mState)) { |
| 728 | ALOGE("%s: Stream %d: Can't attach the output buffer to this surface:" |
| 729 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
| 730 | } |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 731 | checkRetAndSetAbandonedLocked(res); |
| 732 | return res; |
| 733 | } |
| 734 | } else { |
| 735 | ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager:" |
| 736 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
| 737 | return res; |
| 738 | } |
| 739 | } else if (res != OK) { |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 740 | if (shouldLogError(res, mState)) { |
| 741 | ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)", |
| 742 | __FUNCTION__, mId, strerror(-res), res); |
| 743 | } |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 744 | checkRetAndSetAbandonedLocked(res); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 745 | return res; |
| 746 | } |
| 747 | } |
| 748 | |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 749 | if (res == OK) { |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 750 | checkRemovedBuffersLocked(); |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 753 | return res; |
| 754 | } |
| 755 | |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 756 | void Camera3OutputStream::checkRemovedBuffersLocked(bool notifyBufferManager) { |
| 757 | std::vector<sp<GraphicBuffer>> removedBuffers; |
| 758 | status_t res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers); |
| 759 | if (res == OK) { |
| 760 | onBuffersRemovedLocked(removedBuffers); |
| 761 | |
| 762 | if (notifyBufferManager && mUseBufferManager && removedBuffers.size() > 0) { |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 763 | mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), isMultiResolution(), |
| 764 | removedBuffers.size()); |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | void Camera3OutputStream::checkRetAndSetAbandonedLocked(status_t res) { |
| 770 | // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is |
| 771 | // STATE_PREPARING, let prepareNextBuffer handle the error.) |
| 772 | if ((res == NO_INIT || res == DEAD_OBJECT) && mState == STATE_CONFIGURED) { |
| 773 | mState = STATE_ABANDONED; |
| 774 | } |
| 775 | } |
| 776 | |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 777 | bool Camera3OutputStream::shouldLogError(status_t res, StreamState state) { |
| 778 | if (res == OK) { |
| 779 | return false; |
| 780 | } |
| 781 | if ((res == DEAD_OBJECT || res == NO_INIT) && state == STATE_ABANDONED) { |
| 782 | return false; |
| 783 | } |
| 784 | return true; |
| 785 | } |
| 786 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 787 | status_t Camera3OutputStream::disconnectLocked() { |
| 788 | status_t res; |
| 789 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 790 | if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) { |
| 791 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 794 | // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED |
| 795 | // state), don't need change the stream state, return OK. |
| 796 | if (mConsumer == nullptr) { |
| 797 | return OK; |
| 798 | } |
| 799 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 800 | returnPrefetchedBuffersLocked(); |
| 801 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 802 | ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId()); |
| 803 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 804 | res = native_window_api_disconnect(mConsumer.get(), |
| 805 | NATIVE_WINDOW_API_CAMERA); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 806 | /** |
| 807 | * This is not an error. if client calling process dies, the window will |
| 808 | * also die and all calls to it will return DEAD_OBJECT, thus it's already |
| 809 | * "disconnected" |
| 810 | */ |
| 811 | if (res == DEAD_OBJECT) { |
| 812 | ALOGW("%s: While disconnecting stream %d from native window, the" |
| 813 | " native window died from under us", __FUNCTION__, mId); |
| 814 | } |
| 815 | else if (res != OK) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 816 | ALOGE("%s: Unable to disconnect stream %d from native window " |
| 817 | "(error %d %s)", |
| 818 | __FUNCTION__, mId, res, strerror(-res)); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 819 | mState = STATE_ERROR; |
| 820 | return res; |
| 821 | } |
| 822 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 823 | // Since device is already idle, there is no getBuffer call to buffer manager, unregister the |
| 824 | // stream at this point should be safe. |
| 825 | if (mUseBufferManager) { |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 826 | res = mBufferManager->unregisterStream(getId(), getStreamSetId(), isMultiResolution()); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 827 | if (res != OK) { |
| 828 | ALOGE("%s: Unable to unregister stream %d from buffer manager " |
| 829 | "(error %d %s)", __FUNCTION__, mId, res, strerror(-res)); |
| 830 | mState = STATE_ERROR; |
| 831 | return res; |
| 832 | } |
| 833 | // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as |
| 834 | // the stream is still in usable state after this call. |
| 835 | mUseBufferManager = false; |
| 836 | } |
| 837 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 838 | mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG |
| 839 | : STATE_CONSTRUCTED; |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 840 | |
| 841 | mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId); |
| 842 | mDequeueBufferLatency.reset(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 843 | return OK; |
| 844 | } |
| 845 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 846 | status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) const { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 847 | |
| 848 | status_t res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 849 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 850 | if (mConsumer == nullptr) { |
| 851 | // mConsumerUsage was sanitized before the Camera3OutputStream was constructed. |
| 852 | *usage = mConsumerUsage; |
| 853 | return OK; |
| 854 | } |
| 855 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 856 | res = getEndpointUsageForSurface(usage, mConsumer); |
| 857 | |
| 858 | return res; |
| 859 | } |
| 860 | |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 861 | void Camera3OutputStream::applyZSLUsageQuirk(int format, uint64_t *consumerUsage /*inout*/) { |
| 862 | if (consumerUsage == nullptr) { |
| 863 | return; |
| 864 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 865 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 866 | // If an opaque output stream's endpoint is ImageReader, add |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 867 | // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 868 | // for the ZSL use case. |
| 869 | // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set: |
| 870 | // 1. GRALLOC_USAGE_HW_TEXTURE |
| 871 | // 2. GRALLOC_USAGE_HW_RENDER |
| 872 | // 3. GRALLOC_USAGE_HW_COMPOSER |
| 873 | // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 874 | if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && |
| 875 | (*consumerUsage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 876 | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) { |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 877 | *consumerUsage |= GRALLOC_USAGE_HW_CAMERA_ZSL; |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 878 | } |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 879 | } |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 880 | |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 881 | status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage, |
| 882 | const sp<Surface>& surface) const { |
| 883 | status_t res; |
| 884 | uint64_t u = 0; |
| 885 | |
| 886 | res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), &u); |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 887 | applyZSLUsageQuirk(camera_stream::format, &u); |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 888 | *usage = u; |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 889 | return res; |
| 890 | } |
| 891 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 892 | bool Camera3OutputStream::isVideoStream() const { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 893 | uint64_t usage = 0; |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 894 | status_t res = getEndpointUsage(&usage); |
| 895 | if (res != OK) { |
| 896 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0; |
| 901 | } |
| 902 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 903 | status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) { |
| 904 | Mutex::Autolock l(mLock); |
| 905 | if (mState != STATE_CONSTRUCTED) { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 906 | ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.", |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 907 | __FUNCTION__); |
| 908 | return INVALID_OPERATION; |
| 909 | } |
| 910 | mBufferManager = bufferManager; |
| 911 | |
| 912 | return OK; |
| 913 | } |
| 914 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 915 | status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/, |
| 916 | const std::vector<OutputStreamInfo> &/*outputInfo*/, |
| 917 | const std::vector<size_t> &/*removedSurfaceIds*/, |
| 918 | KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) { |
| 919 | ALOGE("%s: this method is not supported!", __FUNCTION__); |
| 920 | return INVALID_OPERATION; |
| 921 | } |
| 922 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 923 | void Camera3OutputStream::BufferProducerListener::onBufferReleased() { |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 924 | sp<Camera3OutputStream> stream = mParent.promote(); |
| 925 | if (stream == nullptr) { |
| 926 | ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__); |
| 927 | return; |
| 928 | } |
| 929 | |
| 930 | Mutex::Autolock l(stream->mLock); |
| 931 | if (!(stream->mUseBufferManager)) { |
| 932 | return; |
| 933 | } |
| 934 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 935 | ALOGV("Stream %d: Buffer released", stream->getId()); |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 936 | bool shouldFreeBuffer = false; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 937 | status_t res = stream->mBufferManager->onBufferReleased( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 938 | stream->getId(), stream->getStreamSetId(), stream->isMultiResolution(), |
| 939 | &shouldFreeBuffer); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 940 | if (res != OK) { |
| 941 | ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__, |
| 942 | strerror(-res), res); |
| 943 | stream->mState = STATE_ERROR; |
| 944 | } |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 945 | |
| 946 | if (shouldFreeBuffer) { |
| 947 | sp<GraphicBuffer> buffer; |
| 948 | // Detach and free a buffer (when buffer goes out of scope) |
| 949 | stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr); |
| 950 | if (buffer.get() != nullptr) { |
| 951 | stream->mBufferManager->notifyBufferRemoved( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 952 | stream->getId(), stream->getStreamSetId(), stream->isMultiResolution()); |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 957 | void Camera3OutputStream::BufferProducerListener::onBuffersDiscarded( |
| 958 | const std::vector<sp<GraphicBuffer>>& buffers) { |
| 959 | sp<Camera3OutputStream> stream = mParent.promote(); |
| 960 | if (stream == nullptr) { |
| 961 | ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__); |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | if (buffers.size() > 0) { |
| 966 | Mutex::Autolock l(stream->mLock); |
| 967 | stream->onBuffersRemovedLocked(buffers); |
| 968 | if (stream->mUseBufferManager) { |
| 969 | stream->mBufferManager->onBuffersRemoved(stream->getId(), |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 970 | stream->getStreamSetId(), stream->isMultiResolution(), buffers.size()); |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 971 | } |
| 972 | ALOGV("Stream %d: %zu Buffers discarded.", stream->getId(), buffers.size()); |
| 973 | } |
| 974 | } |
| 975 | |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 976 | void Camera3OutputStream::onBuffersRemovedLocked( |
| 977 | const std::vector<sp<GraphicBuffer>>& removedBuffers) { |
Yin-Chia Yeh | db1e864 | 2017-07-14 15:19:30 -0700 | [diff] [blame] | 978 | sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote(); |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 979 | if (callback != nullptr) { |
Chih-Hung Hsieh | 48fc619 | 2017-08-04 14:37:31 -0700 | [diff] [blame] | 980 | for (const auto& gb : removedBuffers) { |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 981 | callback->onBufferFreed(mId, gb->handle); |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 986 | status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) { |
| 987 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 988 | return detachBufferLocked(buffer, fenceFd); |
| 989 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 990 | |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 991 | status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) { |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 992 | ALOGV("Stream %d: detachBuffer", getId()); |
| 993 | if (buffer == nullptr) { |
| 994 | return BAD_VALUE; |
| 995 | } |
| 996 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 997 | sp<Fence> fence; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 998 | status_t res = mConsumer->detachNextBuffer(buffer, &fence); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 999 | if (res == NO_MEMORY) { |
| 1000 | // This may rarely happen, which indicates that the released buffer was freed by other |
| 1001 | // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the |
| 1002 | // buffer manager that this buffer has been freed. It's not fatal, but should be avoided, |
| 1003 | // therefore log a warning. |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1004 | *buffer = 0; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1005 | ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__); |
| 1006 | } else if (res != OK) { |
Eino-Ville Talvala | ff51b47 | 2016-06-28 15:26:19 -0700 | [diff] [blame] | 1007 | // Treat other errors as abandonment |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 1008 | if (shouldLogError(res, mState)) { |
| 1009 | ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1010 | } |
Eino-Ville Talvala | ff51b47 | 2016-06-28 15:26:19 -0700 | [diff] [blame] | 1011 | mState = STATE_ABANDONED; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1012 | return res; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1015 | if (fenceFd != nullptr) { |
| 1016 | if (fence!= 0 && fence->isValid()) { |
| 1017 | *fenceFd = fence->dup(); |
| 1018 | } else { |
| 1019 | *fenceFd = -1; |
| 1020 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1021 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1022 | |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 1023 | // Here we assume detachBuffer is called by buffer manager so it doesn't need to be notified |
| 1024 | checkRemovedBuffersLocked(/*notifyBufferManager*/false); |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 1025 | return res; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1026 | } |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 1027 | |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 1028 | status_t Camera3OutputStream::dropBuffers(bool dropping) { |
| 1029 | Mutex::Autolock l(mLock); |
| 1030 | mDropBuffers = dropping; |
| 1031 | return OK; |
| 1032 | } |
| 1033 | |
Shuzhen Wang | 5c22c15 | 2017-12-31 17:12:25 -0800 | [diff] [blame] | 1034 | const String8& Camera3OutputStream::getPhysicalCameraId() const { |
| 1035 | Mutex::Autolock l(mLock); |
| 1036 | return physicalCameraId(); |
| 1037 | } |
| 1038 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 1039 | status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1040 | return OK; |
| 1041 | } |
| 1042 | |
| 1043 | bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1044 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1045 | |
| 1046 | if (surface_id != 0) { |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1047 | ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1048 | } |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1049 | return mConsumer == nullptr; |
| 1050 | } |
| 1051 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1052 | status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 1053 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1054 | if (consumers.size() != 1) { |
| 1055 | ALOGE("%s: it's illegal to set %zu consumer surfaces!", |
| 1056 | __FUNCTION__, consumers.size()); |
| 1057 | return INVALID_OPERATION; |
| 1058 | } |
| 1059 | if (consumers[0] == nullptr) { |
| 1060 | ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1061 | return INVALID_OPERATION; |
| 1062 | } |
| 1063 | |
| 1064 | if (mConsumer != nullptr) { |
| 1065 | ALOGE("%s: consumer surface was already set!", __FUNCTION__); |
| 1066 | return INVALID_OPERATION; |
| 1067 | } |
| 1068 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1069 | mConsumer = consumers[0]; |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1070 | return OK; |
| 1071 | } |
| 1072 | |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 1073 | bool Camera3OutputStream::isConsumedByHWComposer() const { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 1074 | uint64_t usage = 0; |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 1075 | status_t res = getEndpointUsage(&usage); |
| 1076 | if (res != OK) { |
| 1077 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
| 1081 | return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0; |
| 1082 | } |
| 1083 | |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 1084 | bool Camera3OutputStream::isConsumedByHWTexture() const { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 1085 | uint64_t usage = 0; |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 1086 | status_t res = getEndpointUsage(&usage); |
| 1087 | if (res != OK) { |
| 1088 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1089 | return false; |
| 1090 | } |
| 1091 | |
| 1092 | return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0; |
| 1093 | } |
| 1094 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1095 | void Camera3OutputStream::dumpImageToDisk(nsecs_t timestamp, |
| 1096 | ANativeWindowBuffer* anwBuffer, int fence) { |
| 1097 | // Deriver output file name |
| 1098 | std::string fileExtension = "jpg"; |
| 1099 | char imageFileName[64]; |
| 1100 | time_t now = time(0); |
| 1101 | tm *localTime = localtime(&now); |
| 1102 | snprintf(imageFileName, sizeof(imageFileName), "IMG_%4d%02d%02d_%02d%02d%02d_%" PRId64 ".%s", |
Shuzhen Wang | 6a8237f | 2021-07-13 14:42:57 -0700 | [diff] [blame] | 1103 | 1900 + localTime->tm_year, localTime->tm_mon + 1, localTime->tm_mday, |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1104 | localTime->tm_hour, localTime->tm_min, localTime->tm_sec, |
| 1105 | timestamp, fileExtension.c_str()); |
| 1106 | |
| 1107 | // Lock the image for CPU read |
| 1108 | sp<GraphicBuffer> graphicBuffer = GraphicBuffer::from(anwBuffer); |
| 1109 | void* mapped = nullptr; |
| 1110 | base::unique_fd fenceFd(dup(fence)); |
| 1111 | status_t res = graphicBuffer->lockAsync(GraphicBuffer::USAGE_SW_READ_OFTEN, &mapped, |
| 1112 | fenceFd.get()); |
| 1113 | if (res != OK) { |
| 1114 | ALOGE("%s: Failed to lock the buffer: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 1115 | return; |
| 1116 | } |
| 1117 | |
| 1118 | // Figure out actual file size |
| 1119 | auto actualJpegSize = android::camera2::JpegProcessor::findJpegSize((uint8_t*)mapped, mMaxSize); |
| 1120 | if (actualJpegSize == 0) { |
| 1121 | actualJpegSize = mMaxSize; |
| 1122 | } |
| 1123 | |
| 1124 | // Output image data to file |
| 1125 | std::string filePath = "/data/misc/cameraserver/"; |
| 1126 | filePath += imageFileName; |
| 1127 | std::ofstream imageFile(filePath.c_str(), std::ofstream::binary); |
| 1128 | if (!imageFile.is_open()) { |
| 1129 | ALOGE("%s: Unable to create file %s", __FUNCTION__, filePath.c_str()); |
| 1130 | graphicBuffer->unlock(); |
| 1131 | return; |
| 1132 | } |
| 1133 | imageFile.write((const char*)mapped, actualJpegSize); |
| 1134 | |
| 1135 | graphicBuffer->unlock(); |
| 1136 | } |
| 1137 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1138 | status_t Camera3OutputStream::setBatchSize(size_t batchSize) { |
| 1139 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1140 | if (batchSize == 0) { |
| 1141 | ALOGE("%s: invalid batch size 0", __FUNCTION__); |
| 1142 | return BAD_VALUE; |
| 1143 | } |
| 1144 | |
| 1145 | if (mUseBufferManager) { |
| 1146 | ALOGE("%s: batch operation is not supported with buffer manager", __FUNCTION__); |
| 1147 | return INVALID_OPERATION; |
| 1148 | } |
| 1149 | |
| 1150 | if (!isVideoStream()) { |
| 1151 | ALOGE("%s: batch operation is not supported with non-video stream", __FUNCTION__); |
| 1152 | return INVALID_OPERATION; |
| 1153 | } |
| 1154 | |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1155 | if (camera_stream::max_buffers < batchSize) { |
| 1156 | ALOGW("%s: batch size is capped by max_buffers %d", __FUNCTION__, |
| 1157 | camera_stream::max_buffers); |
| 1158 | batchSize = camera_stream::max_buffers; |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1159 | } |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1160 | |
| 1161 | size_t defaultBatchSize = 1; |
| 1162 | if (!mBatchSize.compare_exchange_strong(defaultBatchSize, batchSize)) { |
| 1163 | ALOGE("%s: change batch size from %zu to %zu dynamically is not supported", |
| 1164 | __FUNCTION__, defaultBatchSize, batchSize); |
| 1165 | return INVALID_OPERATION; |
| 1166 | } |
| 1167 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1168 | return OK; |
| 1169 | } |
| 1170 | |
| 1171 | void Camera3OutputStream::returnPrefetchedBuffersLocked() { |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1172 | std::vector<Surface::BatchBuffer> batchedBuffers; |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1173 | |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1174 | { |
| 1175 | std::lock_guard<std::mutex> batchLock(mBatchLock); |
| 1176 | if (mBatchedBuffers.size() != 0) { |
| 1177 | ALOGW("%s: %zu extra prefetched buffers detected. Returning", |
| 1178 | __FUNCTION__, mBatchedBuffers.size()); |
| 1179 | batchedBuffers = std::move(mBatchedBuffers); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | if (batchedBuffers.size() > 0) { |
| 1184 | mConsumer->cancelBuffers(batchedBuffers); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 1188 | }; // namespace camera3 |
| 1189 | |
| 1190 | }; // namespace android |