Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [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 |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [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-IOStreamBase" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 22 | |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/Trace.h> |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 25 | #include "device3/Camera3IOStreamBase.h" |
| 26 | #include "device3/StatusTracker.h" |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | namespace camera3 { |
| 31 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 32 | Camera3IOStreamBase::Camera3IOStreamBase(int id, camera_stream_type_t type, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 33 | uint32_t width, uint32_t height, size_t maxSize, int format, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 34 | android_dataspace dataSpace, camera_stream_rotation_t rotation, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 35 | const String8& physicalCameraId, |
| 36 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 37 | int setId, bool isMultiResolution, int dynamicRangeProfile) : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 38 | Camera3Stream(id, type, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 39 | width, height, maxSize, format, dataSpace, rotation, |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 40 | physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution, |
| 41 | dynamicRangeProfile), |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 42 | mTotalBufferCount(0), |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 43 | mHandoutTotalBufferCount(0), |
| 44 | mHandoutOutputBufferCount(0), |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 45 | mFrameCount(0), |
| 46 | mLastTimestamp(0) { |
| 47 | |
| 48 | mCombinedFence = new Fence(); |
| 49 | |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 50 | if (maxSize > 0 && |
| 51 | (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE)) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 52 | ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, |
| 53 | format); |
| 54 | mState = STATE_ERROR; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | Camera3IOStreamBase::~Camera3IOStreamBase() { |
| 59 | disconnectLocked(); |
| 60 | } |
| 61 | |
| 62 | bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const { |
| 63 | nsecs_t signalTime = mCombinedFence->getSignalTime(); |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 64 | ALOGV("%s: Stream %d: Has %zu outstanding buffers," |
| 65 | " buffer signal time is %" PRId64, |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 66 | __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime); |
| 67 | if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 73 | void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const { |
| 74 | (void) args; |
| 75 | String8 lines; |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 76 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 77 | uint64_t consumerUsage = 0; |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 78 | status_t res = getEndpointUsage(&consumerUsage); |
| 79 | if (res != OK) consumerUsage = 0; |
| 80 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 81 | lines.appendFormat(" State: %d\n", mState); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 82 | lines.appendFormat(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 83 | camera_stream::width, camera_stream::height, |
| 84 | camera_stream::format, camera_stream::data_space); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 85 | lines.appendFormat(" Max size: %zu\n", mMaxSize); |
Shuzhen Wang | 93b1079 | 2021-07-26 09:46:31 -0700 | [diff] [blame] | 86 | lines.appendFormat(" Combined usage: 0x%" PRIx64 ", max HAL buffers: %d\n", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 87 | mUsage | consumerUsage, camera_stream::max_buffers); |
| 88 | if (strlen(camera_stream::physical_camera_id) > 0) { |
| 89 | lines.appendFormat(" Physical camera id: %s\n", camera_stream::physical_camera_id); |
Shuzhen Wang | c2cba12 | 2018-05-17 18:10:24 -0700 | [diff] [blame] | 90 | } |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 91 | lines.appendFormat(" Dynamic Range Profile: 0x%x", camera_stream::dynamic_range_profile); |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 92 | lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n", |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 93 | mFrameCount, mLastTimestamp); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 94 | lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n", |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 95 | mTotalBufferCount, mHandoutTotalBufferCount); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 96 | write(fd, lines.string(), lines.size()); |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 97 | |
| 98 | Camera3Stream::dump(fd, args); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | status_t Camera3IOStreamBase::configureQueueLocked() { |
| 102 | status_t res; |
| 103 | |
| 104 | switch (mState) { |
| 105 | case STATE_IN_RECONFIG: |
| 106 | res = disconnectLocked(); |
| 107 | if (res != OK) { |
| 108 | return res; |
| 109 | } |
| 110 | break; |
| 111 | case STATE_IN_CONFIG: |
| 112 | // OK |
| 113 | break; |
| 114 | default: |
| 115 | ALOGE("%s: Bad state: %d", __FUNCTION__, mState); |
| 116 | return INVALID_OPERATION; |
| 117 | } |
| 118 | |
| 119 | return OK; |
| 120 | } |
| 121 | |
| 122 | size_t Camera3IOStreamBase::getBufferCountLocked() { |
| 123 | return mTotalBufferCount; |
| 124 | } |
| 125 | |
Yin-Chia Yeh | d5cd5ff | 2018-10-01 14:43:04 -0700 | [diff] [blame] | 126 | size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() const { |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 127 | return mHandoutOutputBufferCount; |
| 128 | } |
| 129 | |
| 130 | size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() { |
| 131 | return (mHandoutTotalBufferCount - mHandoutOutputBufferCount); |
| 132 | } |
| 133 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 134 | status_t Camera3IOStreamBase::disconnectLocked() { |
| 135 | switch (mState) { |
| 136 | case STATE_IN_RECONFIG: |
| 137 | case STATE_CONFIGURED: |
Eino-Ville Talvala | ff51b47 | 2016-06-28 15:26:19 -0700 | [diff] [blame] | 138 | case STATE_ABANDONED: |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 139 | // OK |
| 140 | break; |
| 141 | default: |
| 142 | // No connection, nothing to do |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 143 | ALOGV("%s: Stream %d: Already disconnected", |
| 144 | __FUNCTION__, mId); |
| 145 | return -ENOTCONN; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 148 | if (mHandoutTotalBufferCount > 0) { |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 149 | ALOGE("%s: Can't disconnect with %zu buffers still dequeued!", |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 150 | __FUNCTION__, mHandoutTotalBufferCount); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 151 | return INVALID_OPERATION; |
| 152 | } |
| 153 | |
| 154 | return OK; |
| 155 | } |
| 156 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 157 | void Camera3IOStreamBase::handoutBufferLocked(camera_stream_buffer &buffer, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 158 | buffer_handle_t *handle, |
| 159 | int acquireFence, |
| 160 | int releaseFence, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 161 | camera_buffer_status_t status, |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 162 | bool output) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Note that all fences are now owned by HAL. |
| 165 | */ |
| 166 | |
| 167 | // Handing out a raw pointer to this object. Increment internal refcount. |
| 168 | incStrong(this); |
| 169 | buffer.stream = this; |
| 170 | buffer.buffer = handle; |
| 171 | buffer.acquire_fence = acquireFence; |
| 172 | buffer.release_fence = releaseFence; |
| 173 | buffer.status = status; |
| 174 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 175 | // Inform tracker about becoming busy |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 176 | if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG && |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 177 | mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) { |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers |
| 180 | * before/after register_stream_buffers during initial configuration |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 181 | * or re-configuration, or during prepare pre-allocation |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 182 | */ |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 183 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 184 | if (statusTracker != 0) { |
| 185 | statusTracker->markComponentActive(mStatusId); |
| 186 | } |
| 187 | } |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 188 | mHandoutTotalBufferCount++; |
| 189 | |
| 190 | if (output) { |
| 191 | mHandoutOutputBufferCount++; |
| 192 | } |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 196 | // Allow dequeue during IN_[RE]CONFIG for registration, in |
| 197 | // PREPARING for pre-allocation |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 198 | if (mState != STATE_CONFIGURED && |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 199 | mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG && |
| 200 | mState != STATE_PREPARING) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 201 | ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d", |
| 202 | __FUNCTION__, mId, mState); |
| 203 | return INVALID_OPERATION; |
| 204 | } |
| 205 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 206 | return OK; |
| 207 | } |
| 208 | |
| 209 | status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const { |
| 210 | // Allow buffers to be returned in the error state, to allow for disconnect |
| 211 | // and in the in-config states for registration |
| 212 | if (mState == STATE_CONSTRUCTED) { |
| 213 | ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d", |
| 214 | __FUNCTION__, mId, mState); |
| 215 | return INVALID_OPERATION; |
| 216 | } |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 217 | if (mHandoutTotalBufferCount == 0) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 218 | ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__, |
| 219 | mId); |
| 220 | return INVALID_OPERATION; |
| 221 | } |
| 222 | |
| 223 | return OK; |
| 224 | } |
| 225 | |
| 226 | status_t Camera3IOStreamBase::returnAnyBufferLocked( |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 227 | const camera_stream_buffer &buffer, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 228 | nsecs_t timestamp, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 229 | nsecs_t readoutTimestamp, |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 230 | bool output, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 231 | int32_t transform, |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 232 | const std::vector<size_t>& surface_ids) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 233 | status_t res; |
| 234 | |
| 235 | // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be |
| 236 | // decrementing the internal refcount next. In case this is the last ref, we |
| 237 | // might get destructed on the decStrong(), so keep an sp around until the |
| 238 | // end of the call - otherwise have to sprinkle the decStrong on all exit |
| 239 | // points. |
| 240 | sp<Camera3IOStreamBase> keepAlive(this); |
| 241 | decStrong(this); |
| 242 | |
| 243 | if ((res = returnBufferPreconditionCheckLocked()) != OK) { |
| 244 | return res; |
| 245 | } |
| 246 | |
| 247 | sp<Fence> releaseFence; |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 248 | res = returnBufferCheckedLocked(buffer, timestamp, readoutTimestamp, |
| 249 | output, transform, surface_ids, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 250 | &releaseFence); |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 251 | // Res may be an error, but we still want to decrement our owned count |
| 252 | // to enable clean shutdown. So we'll just return the error but otherwise |
| 253 | // carry on |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 254 | |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 255 | if (releaseFence != 0) { |
| 256 | mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence); |
| 257 | } |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 258 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 259 | if (output) { |
| 260 | mHandoutOutputBufferCount--; |
| 261 | } |
| 262 | |
| 263 | mHandoutTotalBufferCount--; |
| 264 | if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG && |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 265 | mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) { |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 266 | /** |
| 267 | * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers |
| 268 | * before/after register_stream_buffers during initial configuration |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 269 | * or re-configuration, or during prepare pre-allocation |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 270 | */ |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 271 | ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__, |
| 272 | mId); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 273 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 274 | if (statusTracker != 0) { |
| 275 | statusTracker->markComponentIdle(mStatusId, mCombinedFence); |
| 276 | } |
| 277 | } |
| 278 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 279 | if (output) { |
| 280 | mLastTimestamp = timestamp; |
| 281 | } |
| 282 | |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 283 | return res; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | }; // namespace camera3 |
| 289 | |
| 290 | }; // namespace android |