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