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