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