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