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