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